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 32 2e 20 20 ersion 3.6.12.
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 the individual
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74 C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68 e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61 e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73 n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69 a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69 on.** unit. Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70 mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74 timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65 possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70 files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66 provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65 5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77 commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67 mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79 is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69 ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75 le SQLite. To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68 se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20 er.** programs,
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69 you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69 le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69 te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69 ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 ng interface to
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20 ry. (If you do
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65 not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61 "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64 der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20 , you will find
0340: 61 20 63 6f 70 79 20 69 6e 20 74 68 65 20 66 69 a copy in the fi
0350: 72 73 74 0a 2a 2a 20 35 34 38 37 20 6c 69 6e 65 rst.** 5487 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 33 2d 33 31 20 31 32 3a 31 2009-03-31 12:1
04c0: 39 3a 35 39 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 9:59 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 34 38 20 32 30 30 39 2f 30 33 2f 32 35 20 .848 2009/03/25
0790: 31 36 3a 35 31 3a 34 33 20 64 72 68 20 45 78 70 16:51:43 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 33 36 20 32 30 30 39 2f 30 33 2f 32 1.436 2009/03/2
4970: 30 20 31 33 3a 31 35 3a 33 30 20 64 72 68 20 45 0 13:15:30 drh E
4980: 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 xp $.*/.#ifndef
4990: 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 _SQLITE3_H_.#def
49a0: 69 6e 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a ine _SQLITE3_H_.
49b0: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 #include <stdarg
49c0: 2e 68 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 .h> /* Neede
49d0: 64 20 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 d for the defini
49e0: 74 69 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 tion of va_list
49f0: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 */../*.** Make s
4a00: 75 72 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 ure we can call
4a10: 74 68 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 this stuff from
4a20: 43 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 C++..*/.#if 0.ex
4a30: 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 tern "C" {.#endi
4a40: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 f.../*.** Add th
4a50: 65 20 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 e ability to ove
4a60: 72 72 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a rride 'extern'.*
4a70: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
4a80: 5f 45 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 _EXTERN.# define
4a90: 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 SQLITE_EXTERN e
4aa0: 78 74 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 2f 2a xtern.#endif../*
4ab0: 0a 2a 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20 .** These no-op
4ac0: 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 macros are used
4ad0: 69 6e 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65 in front of inte
4ae0: 72 66 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74 rfaces to mark t
4af0: 68 6f 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 hose.** interfac
4b00: 65 73 20 61 73 20 65 69 74 68 65 72 20 64 65 70 es as either dep
4b10: 72 65 63 61 74 65 64 20 6f 72 20 65 78 70 65 72 recated or exper
4b20: 69 6d 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70 imental. New ap
4b30: 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 plications.** sh
4b40: 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70 ould not use dep
4b50: 72 65 63 61 74 65 64 20 69 6e 74 72 66 61 63 65 recated intrface
4b60: 73 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70 s - they are sup
4b70: 70 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72 port for backwar
4b80: 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c ds.** compatibil
4b90: 69 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 ity only. Appli
4ba0: 63 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73 cation writers s
4bb0: 68 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74 hould be aware t
4bc0: 68 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e hat.** experimen
4bd0: 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 tal interfaces a
4be0: 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 re subject to ch
4bf0: 61 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65 ange in point re
4c00: 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 leases..**.** Th
4c10: 65 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20 ese macros used
4c20: 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61 to resolve to va
4c30: 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63 rious kinds of c
4c40: 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68 ompiler magic th
4c50: 61 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65 at.** would gene
4c60: 72 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73 rate warning mes
4c70: 73 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20 sages when they
4c80: 77 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20 were used. But
4c90: 74 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 that.** compiler
4ca0: 20 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20 magic ended up
4cb0: 67 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20 generating such
4cc0: 61 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20 a flurry of bug
4cd0: 72 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20 reports.** that
4ce0: 77 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74 we have taken it
4cf0: 20 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e all out and gon
4d00: 65 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 e back to using
4d10: 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d simple.** noop m
4d20: 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e acros..*/.#defin
4d30: 65 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 e SQLITE_DEPRECA
4d40: 54 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 TED.#define SQLI
4d50: 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a TE_EXPERIMENTAL.
4d60: 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 ./*.** Ensure th
4d70: 65 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65 ese symbols were
4d80: 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20 not defined by
4d90: 73 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65 some previous he
4da0: 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 ader file..*/.#i
4db0: 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 fdef SQLITE_VERS
4dc0: 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 ION.# undef SQLI
4dd0: 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 TE_VERSION.#endi
4de0: 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
4df0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 VERSION_NUMBER.#
4e00: 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45 undef SQLITE_VE
4e10: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e RSION_NUMBER.#en
4e20: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 dif../*.** CAPI3
4e30: 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d REF: Compile-Tim
4e40: 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f e Library Versio
4e50: 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31 n Numbers {H1001
4e60: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S60100>.**.*
4e70: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 * The SQLITE_VER
4e80: 53 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f SION and SQLITE_
4e90: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 VERSION_NUMBER #
4ea0: 64 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 defines in.** th
4eb0: 65 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65 e sqlite3.h file
4ec0: 20 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72 specify the ver
4ed0: 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77 sion of SQLite w
4ee0: 69 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61 ith which.** tha
4ef0: 74 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 t header file is
4f00: 20 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a associated..**.
4f10: 2a 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22 ** The "version"
4f20: 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20 of SQLite is a
4f30: 73 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f string of the fo
4f40: 72 6d 20 22 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 rm "X.Y.Z"..** T
4f50: 68 65 20 70 68 72 61 73 65 20 22 61 6c 70 68 61 he phrase "alpha
4f60: 22 20 6f 72 20 22 62 65 74 61 22 20 6d 69 67 68 " or "beta" migh
4f70: 74 20 62 65 20 61 70 70 65 6e 64 65 64 20 61 66 t be appended af
4f80: 74 65 72 20 74 68 65 20 5a 2e 0a 2a 2a 20 54 68 ter the Z..** Th
4f90: 65 20 58 20 76 61 6c 75 65 20 69 73 20 6d 61 6a e X value is maj
4fa0: 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 or version numbe
4fb0: 72 20 61 6c 77 61 79 73 20 33 20 69 6e 20 53 51 r always 3 in SQ
4fc0: 4c 69 74 65 33 2e 0a 2a 2a 20 54 68 65 20 58 20 Lite3..** The X
4fd0: 76 61 6c 75 65 20 6f 6e 6c 79 20 63 68 61 6e 67 value only chang
4fe0: 65 73 20 77 68 65 6e 20 62 61 63 6b 77 61 72 64 es when backward
4ff0: 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 s compatibility
5000: 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e 20 61 6e 64 is.** broken and
5010: 20 77 65 20 69 6e 74 65 6e 64 20 74 6f 20 6e 65 we intend to ne
5020: 76 65 72 20 62 72 65 61 6b 20 62 61 63 6b 77 61 ver break backwa
5030: 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 rds compatibilit
5040: 79 2e 0a 2a 2a 20 54 68 65 20 59 20 76 61 6c 75 y..** The Y valu
5050: 65 20 69 73 20 74 68 65 20 6d 69 6e 6f 72 20 76 e is the minor v
5060: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e ersion number an
5070: 64 20 6f 6e 6c 79 20 63 68 61 6e 67 65 73 20 77 d only changes w
5080: 68 65 6e 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 hen.** there are
5090: 20 6d 61 6a 6f 72 20 66 65 61 74 75 72 65 20 65 major feature e
50a0: 6e 68 61 6e 63 65 6d 65 6e 74 73 20 74 68 61 74 nhancements that
50b0: 20 61 72 65 20 66 6f 72 77 61 72 64 73 20 63 6f are forwards co
50c0: 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 62 75 74 20 mpatible.** but
50d0: 6e 6f 74 20 62 61 63 6b 77 61 72 64 73 20 63 6f not backwards co
50e0: 6d 70 61 74 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 mpatible..** The
50f0: 20 5a 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 Z value is the
5100: 72 65 6c 65 61 73 65 20 6e 75 6d 62 65 72 20 61 release number a
5110: 6e 64 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 nd is incremente
5120: 64 20 77 69 74 68 0a 2a 2a 20 65 61 63 68 20 72 d with.** each r
5130: 65 6c 65 61 73 65 20 62 75 74 20 72 65 73 65 74 elease but reset
5140: 73 20 62 61 63 6b 20 74 6f 20 30 20 77 68 65 6e s back to 0 when
5150: 65 76 65 72 20 59 20 69 73 20 69 6e 63 72 65 6d ever Y is increm
5160: 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 ented..**.** See
5170: 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
5180: 6c 69 62 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e libversion()] an
5190: 64 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 d [sqlite3_libve
51a0: 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d 2e rsion_number()].
51b0: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
51c0: 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b 48 nts: [H10011] [H
51d0: 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 6e 10014].*/.#defin
51e0: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e e SQLITE_VERSION
51f0: 20 20 20 20 20 20 20 20 20 22 33 2e 36 2e 31 32 "3.6.12
5200: 22 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ".#define SQLITE
5210: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 _VERSION_NUMBER
5220: 20 33 30 30 36 30 31 32 0a 0a 2f 2a 0a 2a 2a 20 3006012../*.**
5230: 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 CAPI3REF: Run-Ti
5240: 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 me Library Versi
5250: 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 on Numbers {H100
5260: 32 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 20 20} <S60100>.**
5270: 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 KEYWORDS: sqlite
5280: 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3_version.**.**
5290: 54 68 65 73 65 20 66 65 61 74 75 72 65 73 20 70 These features p
52a0: 72 6f 76 69 64 65 20 74 68 65 20 73 61 6d 65 20 rovide the same
52b0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74 information as t
52c0: 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 he [SQLITE_VERSI
52d0: 4f 4e 5d 0a 2a 2a 20 61 6e 64 20 5b 53 51 4c 49 ON].** and [SQLI
52e0: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 TE_VERSION_NUMBE
52f0: 52 5d 20 23 64 65 66 69 6e 65 73 20 69 6e 20 74 R] #defines in t
5300: 68 65 20 68 65 61 64 65 72 2c 20 62 75 74 20 61 he header, but a
5310: 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a re associated.**
5320: 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 with the librar
5330: 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 y instead of the
5340: 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20 20 43 header file. C
5350: 61 75 74 69 6f 75 73 20 70 72 6f 67 72 61 6d 6d autious programm
5360: 65 72 73 20 6d 69 67 68 74 0a 2a 2a 20 69 6e 63 ers might.** inc
5370: 6c 75 64 65 20 61 20 63 68 65 63 6b 20 69 6e 20 lude a check in
5380: 74 68 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f their applicatio
5390: 6e 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 n to verify that
53a0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 .** sqlite3_libv
53b0: 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 ersion_number()
53c0: 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 74 always returns t
53d0: 68 65 20 76 61 6c 75 65 0a 2a 2a 20 5b 53 51 4c he value.** [SQL
53e0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 ITE_VERSION_NUMB
53f0: 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ER]..**.** The s
5400: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f qlite3_libversio
5410: 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 n() function ret
5420: 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 69 6e urns the same in
5430: 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 69 73 0a formation as is.
5440: 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 ** in the sqlite
5450: 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69 3_version[] stri
5460: 6e 67 20 63 6f 6e 73 74 61 6e 74 2e 20 20 54 68 ng constant. Th
5470: 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 72 e function is pr
5480: 6f 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20 75 73 ovided.** for us
5490: 65 20 69 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20 e in DLLs since
54a0: 44 4c 4c 20 75 73 65 72 73 20 75 73 75 61 6c 6c DLL users usuall
54b0: 79 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 64 69 y do not have di
54c0: 72 65 63 74 20 61 63 63 65 73 73 20 74 6f 20 73 rect access to s
54d0: 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 74 61 6e tring.** constan
54e0: 74 73 20 77 69 74 68 69 6e 20 74 68 65 20 44 4c ts within the DL
54f0: 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 L..**.** Require
5500: 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d 20 ments: [H10021]
5510: 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32 33 [H10022] [H10023
5520: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
5530: 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 const char sqlit
5540: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 e3_version[] = S
5550: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 QLITE_VERSION;.S
5560: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
5570: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 char *sqlite3_li
5580: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a bversion(void);.
5590: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
55a0: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f qlite3_libversio
55b0: 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a n_number(void);.
55c0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
55d0: 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 Test To See If
55e0: 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 The Library Is T
55f0: 68 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 hreadsafe {H1010
5600: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S60100>.**.*
5610: 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 * SQLite can be
5620: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 compiled with or
5630: 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 without mutexes
5640: 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b . When.** the [
5650: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
5660: 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f E] C preprocesso
5670: 72 20 6d 61 63 72 6f 20 31 20 6f 72 20 32 2c 20 r macro 1 or 2,
5680: 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 mutexes.** are e
5690: 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74 nabled and SQLit
56a0: 65 20 69 73 20 74 68 72 65 61 64 73 61 66 65 2e e is threadsafe.
56b0: 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53 When the.** [S
56c0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
56d0: 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a ] macro is 0, .*
56e0: 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 72 * the mutexes ar
56f0: 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68 e omitted. With
5700: 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c out the mutexes,
5710: 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a it is not safe.
5720: 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 74 65 ** to use SQLite
5730: 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66 72 concurrently fr
5740: 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 om more than one
5750: 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45 thread..**.** E
5760: 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 73 20 nabling mutexes
5770: 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 72 61 incurs a measura
5780: 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ble performance
5790: 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69 penalty..** So i
57a0: 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 75 74 f speed is of ut
57b0: 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c most importance,
57c0: 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 65 20 it makes sense
57d0: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 to disable.** th
57e0: 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 74 20 e mutexes. But
57f0: 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 66 65 for maximum safe
5800: 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 6f 75 ty, mutexes shou
5810: 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a ld be enabled..*
5820: 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 65 * The default be
5830: 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75 havior is for mu
5840: 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62 texes to be enab
5850: 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 led..**.** This
5860: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 interface can be
5870: 20 75 73 65 64 20 62 79 20 61 20 70 72 6f 67 72 used by a progr
5880: 61 6d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 am to make sure
5890: 74 68 61 74 20 74 68 65 0a 2a 2a 20 76 65 72 73 that the.** vers
58a0: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68 ion of SQLite th
58b0: 61 74 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67 at it is linking
58c0: 20 61 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d against was com
58d0: 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 piled with.** th
58e0: 65 20 64 65 73 69 72 65 64 20 73 65 74 74 69 6e e desired settin
58f0: 67 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 g of the [SQLITE
5900: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 _THREADSAFE] mac
5910: 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ro..**.** This i
5920: 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79 20 72 65 nterface only re
5930: 70 6f 72 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d ports on the com
5940: 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74 65 78 20 pile-time mutex
5950: 73 65 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 setting.** of th
5960: 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 e [SQLITE_THREAD
5970: 53 41 46 45 5d 20 66 6c 61 67 2e 20 20 49 66 20 SAFE] flag. If
5980: 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
5990: 65 64 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 ed with.** SQLIT
59a0: 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 20 74 E_THREADSAFE=1 t
59b0: 68 65 6e 20 6d 75 74 65 78 65 73 20 61 72 65 20 hen mutexes are
59c0: 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 enabled by defau
59d0: 6c 74 20 62 75 74 0a 2a 2a 20 63 61 6e 20 62 65 lt but.** can be
59e0: 20 66 75 6c 6c 79 20 6f 72 20 70 61 72 74 69 61 fully or partia
59f0: 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 75 73 69 lly disabled usi
5a00: 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 ng a call to [sq
5a10: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 0a lite3_config()].
5a20: 2a 2a 20 77 69 74 68 20 74 68 65 20 76 65 72 62 ** with the verb
5a30: 73 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 s [SQLITE_CONFIG
5a40: 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 2c 20 _SINGLETHREAD],
5a50: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
5a60: 55 4c 54 49 54 48 52 45 41 44 5d 2c 0a 2a 2a 20 ULTITHREAD],.**
5a70: 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 or [SQLITE_CONFI
5a80: 47 5f 4d 55 54 45 58 5d 2e 20 20 54 68 65 20 72 G_MUTEX]. The r
5a90: 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 eturn value of t
5aa0: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f his function sho
5ab0: 77 73 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 20 64 ws.** only the d
5ac0: 65 66 61 75 6c 74 20 63 6f 6d 70 69 6c 65 2d 74 efault compile-t
5ad0: 69 6d 65 20 73 65 74 74 69 6e 67 2c 20 6e 6f 74 ime setting, not
5ae0: 20 61 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63 68 any run-time ch
5af0: 61 6e 67 65 73 0a 2a 2a 20 74 6f 20 74 68 61 74 anges.** to that
5b00: 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 setting..**.**
5b10: 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 See the [threadi
5b20: 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e ng mode] documen
5b30: 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 tation for addit
5b40: 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
5b50: 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 n..**.** Require
5b60: 6d 65 6e 74 73 3a 20 5b 48 31 30 31 30 31 5d 20 ments: [H10101]
5b70: 5b 48 31 30 31 30 32 5d 0a 2a 2f 0a 53 51 4c 49 [H10102].*/.SQLI
5b80: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
5b90: 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 76 6f e3_threadsafe(vo
5ba0: 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 id);../*.** CAPI
5bb0: 33 52 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 3REF: Database C
5bc0: 6f 6e 6e 65 63 74 69 6f 6e 20 48 61 6e 64 6c 65 onnection Handle
5bd0: 20 7b 48 31 32 30 30 30 7d 20 3c 53 34 30 32 30 {H12000} <S4020
5be0: 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
5bf0: 7b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 {database connec
5c00: 74 69 6f 6e 7d 20 7b 64 61 74 61 62 61 73 65 20 tion} {database
5c10: 63 6f 6e 6e 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a connections}.**.
5c20: 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 53 51 4c ** Each open SQL
5c30: 69 74 65 20 64 61 74 61 62 61 73 65 20 69 73 20 ite database is
5c40: 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 represented by a
5c50: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
5c60: 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 nstance of.** th
5c70: 65 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 e opaque structu
5c80: 72 65 20 6e 61 6d 65 64 20 22 73 71 6c 69 74 65 re named "sqlite
5c90: 33 22 2e 20 20 49 74 20 69 73 20 75 73 65 66 75 3". It is usefu
5ca0: 6c 20 74 6f 20 74 68 69 6e 6b 20 6f 66 20 61 6e l to think of an
5cb0: 20 73 71 6c 69 74 65 33 0a 2a 2a 20 70 6f 69 6e sqlite3.** poin
5cc0: 74 65 72 20 61 73 20 61 6e 20 6f 62 6a 65 63 74 ter as an object
5cd0: 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f . The [sqlite3_
5ce0: 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 open()], [sqlite
5cf0: 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 61 6e 64 3_open16()], and
5d00: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 .** [sqlite3_ope
5d10: 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 n_v2()] interfac
5d20: 65 73 20 61 72 65 20 69 74 73 20 63 6f 6e 73 74 es are its const
5d30: 72 75 63 74 6f 72 73 2c 20 61 6e 64 20 5b 73 71 ructors, and [sq
5d40: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 0a 2a lite3_close()].*
5d50: 2a 20 69 73 20 69 74 73 20 64 65 73 74 72 75 63 * is its destruc
5d60: 74 6f 72 2e 20 20 54 68 65 72 65 20 61 72 65 20 tor. There are
5d70: 6d 61 6e 79 20 6f 74 68 65 72 20 69 6e 74 65 72 many other inter
5d80: 66 61 63 65 73 20 28 73 75 63 68 20 61 73 0a 2a faces (such as.*
5d90: 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
5da0: 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 re_v2()], [sqlit
5db0: 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
5dc0: 6f 6e 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 on()], and.** [s
5dd0: 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 qlite3_busy_time
5de0: 6f 75 74 28 29 5d 20 74 6f 20 6e 61 6d 65 20 62 out()] to name b
5df0: 75 74 20 74 68 72 65 65 29 20 74 68 61 74 20 61 ut three) that a
5e00: 72 65 20 6d 65 74 68 6f 64 73 20 6f 6e 20 61 6e re methods on an
5e10: 0a 2a 2a 20 73 71 6c 69 74 65 33 20 6f 62 6a 65 .** sqlite3 obje
5e20: 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ct..*/.typedef s
5e30: 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 73 71 truct sqlite3 sq
5e40: 6c 69 74 65 33 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 lite3;../*.** CA
5e50: 50 49 33 52 45 46 3a 20 36 34 2d 42 69 74 20 49 PI3REF: 64-Bit I
5e60: 6e 74 65 67 65 72 20 54 79 70 65 73 20 7b 48 31 nteger Types {H1
5e70: 30 32 30 30 7d 20 3c 53 31 30 31 31 30 3e 0a 2a 0200} <S10110>.*
5e80: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 * KEYWORDS: sqli
5e90: 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f te_int64 sqlite_
5ea0: 75 69 6e 74 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63 uint64.**.** Bec
5eb0: 61 75 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f ause there is no
5ec0: 20 63 72 6f 73 73 2d 70 6c 61 74 66 6f 72 6d 20 cross-platform
5ed0: 77 61 79 20 74 6f 20 73 70 65 63 69 66 79 20 36 way to specify 6
5ee0: 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74 79 4-bit integer ty
5ef0: 70 65 73 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e pes.** SQLite in
5f00: 63 6c 75 64 65 73 20 74 79 70 65 64 65 66 73 20 cludes typedefs
5f10: 66 6f 72 20 36 34 2d 62 69 74 20 73 69 67 6e 65 for 64-bit signe
5f20: 64 20 61 6e 64 20 75 6e 73 69 67 6e 65 64 20 69 d and unsigned i
5f30: 6e 74 65 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 ntegers..**.** T
5f40: 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 he sqlite3_int64
5f50: 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 75 69 6e and sqlite3_uin
5f60: 74 36 34 20 61 72 65 20 74 68 65 20 70 72 65 66 t64 are the pref
5f70: 65 72 72 65 64 20 74 79 70 65 20 64 65 66 69 6e erred type defin
5f80: 69 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 itions..** The s
5f90: 71 6c 69 74 65 5f 69 6e 74 36 34 20 61 6e 64 20 qlite_int64 and
5fa0: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 74 79 sqlite_uint64 ty
5fb0: 70 65 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 pes are supporte
5fc0: 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 0a d for backwards.
5fd0: 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 ** compatibility
5fe0: 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 only..**.** Req
5ff0: 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 32 uirements: [H102
6000: 30 31 5d 20 5b 48 31 30 32 30 32 5d 0a 2a 2f 0a 01] [H10202].*/.
6010: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 4e #ifdef SQLITE_IN
6020: 54 36 34 5f 54 59 50 45 0a 20 20 74 79 70 65 64 T64_TYPE. typed
6030: 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f ef SQLITE_INT64_
6040: 54 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 TYPE sqlite_int6
6050: 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 4;. typedef uns
6060: 69 67 6e 65 64 20 53 51 4c 49 54 45 5f 49 4e 54 igned SQLITE_INT
6070: 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 75 64_TYPE sqlite_u
6080: 69 6e 74 36 34 3b 0a 23 65 6c 69 66 20 64 65 66 int64;.#elif def
6090: 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 7c ined(_MSC_VER) |
60a0: 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c | defined(__BORL
60b0: 41 4e 44 43 5f 5f 29 0a 20 20 74 79 70 65 64 65 ANDC__). typede
60c0: 66 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 f __int64 sqlite
60d0: 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 _int64;. typede
60e0: 66 20 75 6e 73 69 67 6e 65 64 20 5f 5f 69 6e 74 f unsigned __int
60f0: 36 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 64 sqlite_uint64
6100: 3b 0a 23 65 6c 73 65 0a 20 20 74 79 70 65 64 65 ;.#else. typede
6110: 66 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 f long long int
6120: 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 sqlite_int64;.
6130: 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 typedef unsigned
6140: 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 long long int s
6150: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 qlite_uint64;.#e
6160: 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c ndif.typedef sql
6170: 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 ite_int64 sqlite
6180: 33 5f 69 6e 74 36 34 3b 0a 74 79 70 65 64 65 66 3_int64;.typedef
6190: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
61a0: 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 3b 0a 0a qlite3_uint64;..
61b0: 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 /*.** If compili
61c0: 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 ng for a process
61d0: 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c or that lacks fl
61e0: 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 oating point sup
61f0: 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 port,.** substit
6200: 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 ute integer for
6210: 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 2e 0a floating-point..
6220: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
6230: 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
6240: 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f OINT.# define do
6250: 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 uble sqlite3_int
6260: 36 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 64.#endif../*.**
6270: 20 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 69 CAPI3REF: Closi
6280: 6e 67 20 41 20 44 61 74 61 62 61 73 65 20 43 6f ng A Database Co
6290: 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32 30 31 30 nnection {H12010
62a0: 7d 20 3c 53 33 30 31 30 30 3e 3c 53 34 30 32 30 } <S30100><S4020
62b0: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0>.**.** This ro
62c0: 75 74 69 6e 65 20 69 73 20 74 68 65 20 64 65 73 utine is the des
62d0: 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 tructor for the
62e0: 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 [sqlite3] object
62f0: 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 ..**.** Applicat
6300: 69 6f 6e 73 20 73 68 6f 75 6c 64 20 5b 73 71 6c ions should [sql
6310: 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 ite3_finalize |
6320: 66 69 6e 61 6c 69 7a 65 5d 20 61 6c 6c 20 5b 70 finalize] all [p
6330: 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
6340: 74 73 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 ts].** and [sqli
6350: 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 7c te3_blob_close |
6360: 20 63 6c 6f 73 65 5d 20 61 6c 6c 20 5b 42 4c 4f close] all [BLO
6370: 42 20 68 61 6e 64 6c 65 73 5d 20 61 73 73 6f 63 B handles] assoc
6380: 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 iated with.** th
6390: 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 e [sqlite3] obje
63a0: 63 74 20 70 72 69 6f 72 20 74 6f 20 61 74 74 65 ct prior to atte
63b0: 6d 70 74 69 6e 67 20 74 6f 20 63 6c 6f 73 65 20 mpting to close
63c0: 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 the object..** T
63d0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74 he [sqlite3_next
63e0: 5f 73 74 6d 74 28 29 5d 20 69 6e 74 65 72 66 61 _stmt()] interfa
63f0: 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 ce can be used t
6400: 6f 20 6c 6f 63 61 74 65 20 61 6c 6c 0a 2a 2a 20 o locate all.**
6410: 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
6420: 65 6e 74 73 5d 20 61 73 73 6f 63 69 61 74 65 64 ents] associated
6430: 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 73 with a [databas
6440: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 66 e connection] if
6450: 20 64 65 73 69 72 65 64 2e 0a 2a 2a 20 54 79 70 desired..** Typ
6460: 69 63 61 6c 20 63 6f 64 65 20 6d 69 67 68 74 20 ical code might
6470: 6c 6f 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a 0a look like this:.
6480: 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
6490: 65 3e 3c 70 72 65 3e 0a 2a 2a 20 73 71 6c 69 74 e><pre>.** sqlit
64a0: 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a e3_stmt *pStmt;.
64b0: 2a 2a 20 77 68 69 6c 65 28 20 28 70 53 74 6d 74 ** while( (pStmt
64c0: 20 3d 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f = sqlite3_next_
64d0: 73 74 6d 74 28 64 62 2c 20 30 29 29 21 3d 30 20 stmt(db, 0))!=0
64e0: 29 7b 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 73 ){.** s
64f0: 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
6500: 70 53 74 6d 74 29 3b 0a 2a 2a 20 7d 0a 2a 2a 20 pStmt);.** }.**
6510: 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f </pre></blockquo
6520: 74 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 te>.**.** If [sq
6530: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 lite3_close()] i
6540: 73 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20 s invoked while
6550: 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
6560: 20 6f 70 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 open,.** the tr
6570: 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 75 74 ansaction is aut
6580: 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 omatically rolle
6590: 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 d back..**.** Th
65a0: 65 20 43 20 70 61 72 61 6d 65 74 65 72 20 74 6f e C parameter to
65b0: 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 [sqlite3_close(
65c0: 43 29 5d 20 6d 75 73 74 20 62 65 20 65 69 74 68 C)] must be eith
65d0: 65 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 er a NULL.** poi
65e0: 6e 74 65 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 nter or an [sqli
65f0: 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e te3] object poin
6600: 74 65 72 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 ter obtained.**
6610: 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 from [sqlite3_op
6620: 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f en()], [sqlite3_
6630: 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a open16()], or.**
6640: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
6650: 32 28 29 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72 2()], and not pr
6660: 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e eviously closed.
6670: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
6680: 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d nts:.** [H12011]
6690: 20 5b 48 31 32 30 31 32 5d 20 5b 48 31 32 30 31 [H12012] [H1201
66a0: 33 5d 20 5b 48 31 32 30 31 34 5d 20 5b 48 31 32 3] [H12014] [H12
66b0: 30 31 35 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 015] [H12019].*/
66c0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
66d0: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 sqlite3_close(sq
66e0: 6c 69 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a lite3 *);../*.**
66f0: 20 54 68 65 20 74 79 70 65 20 66 6f 72 20 61 20 The type for a
6700: 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
6710: 6e 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 n..** This is le
6720: 67 61 63 79 20 61 6e 64 20 64 65 70 72 65 63 61 gacy and depreca
6730: 74 65 64 2e 20 20 49 74 20 69 73 20 69 6e 63 6c ted. It is incl
6740: 75 64 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69 uded for histori
6750: 63 61 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 cal.** compatibi
6760: 6c 69 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 lity and is not
6770: 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 documented..*/.t
6780: 79 70 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c ypedef int (*sql
6790: 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76 ite3_callback)(v
67a0: 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c oid*,int,char**,
67b0: 20 63 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a char**);../*.**
67c0: 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53 CAPI3REF: One-S
67d0: 74 65 70 20 51 75 65 72 79 20 45 78 65 63 75 74 tep Query Execut
67e0: 69 6f 6e 20 49 6e 74 65 72 66 61 63 65 20 7b 48 ion Interface {H
67f0: 31 32 31 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 12100} <S10000>.
6800: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
6810: 33 5f 65 78 65 63 28 29 20 69 6e 74 65 72 66 61 3_exec() interfa
6820: 63 65 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 ce is a convenie
6830: 6e 74 20 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e nt way of runnin
6840: 67 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a g one or more.**
6850: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 SQL statements
6860: 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 without having t
6870: 6f 20 77 72 69 74 65 20 61 20 6c 6f 74 20 6f 66 o write a lot of
6880: 20 43 20 63 6f 64 65 2e 20 20 54 68 65 20 55 54 C code. The UT
6890: 46 2d 38 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 F-8 encoded.** S
68a0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 QL statements ar
68b0: 65 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 e passed in as t
68c0: 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
68d0: 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 ter to sqlite3_e
68e0: 78 65 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74 xec()..** The st
68f0: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 atements are eva
6900: 6c 75 61 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e luated one by on
6910: 65 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 e until either a
6920: 6e 20 65 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e n error or.** an
6930: 20 69 6e 74 65 72 72 75 70 74 20 69 73 20 65 6e interrupt is en
6940: 63 6f 75 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e countered, or un
6950: 74 69 6c 20 74 68 65 79 20 61 72 65 20 61 6c 6c til they are all
6960: 20 64 6f 6e 65 2e 20 20 54 68 65 20 33 72 64 20 done. The 3rd
6970: 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 parameter.** is
6980: 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c an optional call
6990: 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 6e 76 back that is inv
69a0: 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 oked once for ea
69b0: 63 68 20 72 6f 77 20 6f 66 20 61 6e 79 20 71 75 ch row of any qu
69c0: 65 72 79 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70 ery.** results p
69d0: 72 6f 64 75 63 65 64 20 62 79 20 74 68 65 20 53 roduced by the S
69e0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 QL statements.
69f0: 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 The 5th paramete
6a00: 72 20 74 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a r tells where.**
6a10: 20 74 6f 20 77 72 69 74 65 20 61 6e 79 20 65 72 to write any er
6a20: 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a ror messages..**
6a30: 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 65 .** The error me
6a40: 73 73 61 67 65 20 70 61 73 73 65 64 20 62 61 63 ssage passed bac
6a50: 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 k through the 5t
6a60: 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 68 h parameter is h
6a70: 65 6c 64 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 eld.** in memory
6a80: 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
6a90: 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
6aa0: 5d 2e 20 20 54 6f 20 61 76 6f 69 64 20 61 20 6d ]. To avoid a m
6ab0: 65 6d 6f 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 emory leak,.** t
6ac0: 68 65 20 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69 he calling appli
6ad0: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61 cation should ca
6ae0: 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 ll [sqlite3_free
6af0: 28 29 5d 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 ()] on any error
6b00: 0a 2a 2a 20 6d 65 73 73 61 67 65 20 72 65 74 75 .** message retu
6b10: 72 6e 65 64 20 74 68 72 6f 75 67 68 20 74 68 65 rned through the
6b20: 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 77 5th parameter w
6b30: 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 hen it has finis
6b40: 68 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 hed using.** the
6b50: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a error message..
6b60: 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
6b70: 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 statement in th
6b80: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 e 2nd parameter
6b90: 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d is NULL or an em
6ba0: 70 74 79 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 pty string.** or
6bb0: 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 a string contai
6bc0: 6e 69 6e 67 20 6f 6e 6c 79 20 77 68 69 74 65 73 ning only whites
6bd0: 70 61 63 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 pace and comment
6be0: 73 2c 20 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a s, then no SQL.*
6bf0: 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 * statements are
6c00: 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 74 evaluated and t
6c10: 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e he database is n
6c20: 6f 74 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a ot changed..**.*
6c30: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 * The sqlite3_ex
6c40: 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 ec() interface i
6c50: 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e s implemented in
6c60: 20 74 65 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 terms of.** [sq
6c70: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
6c80: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 ()], [sqlite3_st
6c90: 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 ep()], and [sqli
6ca0: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e te3_finalize()].
6cb0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
6cc0: 65 78 65 63 28 29 20 72 6f 75 74 69 6e 65 20 64 exec() routine d
6cd0: 6f 65 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 oes nothing to t
6ce0: 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 he database that
6cf0: 20 63 61 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a cannot be done.
6d00: 2a 2a 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 ** by [sqlite3_p
6d10: 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 repare_v2()], [s
6d20: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 qlite3_step()],
6d30: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e and [sqlite3_fin
6d40: 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 alize()]..**.**
6d50: 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
6d60: 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ter to [sqlite3_
6d70: 65 78 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 exec()] must be
6d80: 61 6e 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 an valid and ope
6d90: 6e 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 n.** [database c
6da0: 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a onnection]..**.*
6db0: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
6dc0: 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e onnection must n
6dd0: 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 ot be closed whi
6de0: 6c 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 le.** [sqlite3_e
6df0: 78 65 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e xec()] is runnin
6e00: 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c g..**.** The cal
6e10: 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 ling function sh
6e20: 6f 75 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 ould use [sqlite
6e30: 33 5f 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 3_free()] to fre
6e40: 65 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 e.** the memory
6e50: 74 68 61 74 20 2a 65 72 72 6d 73 67 20 69 73 20 that *errmsg is
6e60: 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
6e70: 20 6f 6e 63 65 20 74 68 65 20 65 72 72 6f 72 0a once the error.
6e80: 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f ** message is no
6e90: 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a longer needed..
6ea0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 73 74 **.** The SQL st
6eb0: 61 74 65 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 atement text in
6ec0: 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
6ed0: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 r to [sqlite3_ex
6ee0: 65 63 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 ec()].** must re
6ef0: 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 77 main unchanged w
6f00: 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 hile [sqlite3_ex
6f10: 65 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 ec()] is running
6f20: 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
6f30: 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 ents:.** [H12101
6f40: 5d 20 5b 48 31 32 31 30 32 5d 20 5b 48 31 32 31 ] [H12102] [H121
6f50: 30 34 5d 20 5b 48 31 32 31 30 35 5d 20 5b 48 31 04] [H12105] [H1
6f60: 32 31 30 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b 2107] [H12110] [
6f70: 48 31 32 31 31 33 5d 20 5b 48 31 32 31 31 36 5d H12113] [H12116]
6f80: 0a 2a 2a 20 5b 48 31 32 31 31 39 5d 20 5b 48 31 .** [H12119] [H1
6f90: 32 31 32 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b 2122] [H12125] [
6fa0: 48 31 32 31 33 31 5d 20 5b 48 31 32 31 33 34 5d H12131] [H12134]
6fb0: 20 5b 48 31 32 31 33 37 5d 20 5b 48 31 32 31 33 [H12137] [H1213
6fc0: 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 8].*/.SQLITE_API
6fd0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 int sqlite3_exe
6fe0: 63 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 c(. sqlite3*,
6ff0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7010: 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 /* An open datab
7020: 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ase */. const c
7030: 68 61 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 har *sql,
7040: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7050: 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 /* SQL to be
7060: 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 evaluated */.
7070: 69 6e 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 int (*callback)(
7080: 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a void*,int,char**
7090: 2c 63 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 ,char**), /* Ca
70a0: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 llback function
70b0: 2a 2f 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 */. void *,
70c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70e0: 2f 2a 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 /* 1st argument
70f0: 74 6f 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 to callback */.
7100: 20 63 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 char **errmsg
7110: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7120: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
7130: 72 72 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e rror msg written
7140: 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a here */.);../*.
7150: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 ** CAPI3REF: Res
7160: 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 30 32 31 ult Codes {H1021
7170: 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 0} <S10700>.** K
7180: 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f EYWORDS: SQLITE_
7190: 4f 4b 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 OK {error code}
71a0: 7b 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a {error codes}.**
71b0: 20 4b 45 59 57 4f 52 44 53 3a 20 7b 72 65 73 75 KEYWORDS: {resu
71c0: 6c 74 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74 lt code} {result
71d0: 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 codes}.**.** Ma
71e0: 6e 79 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69 ny SQLite functi
71f0: 6f 6e 73 20 72 65 74 75 72 6e 20 61 6e 20 69 6e ons return an in
7200: 74 65 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64 teger result cod
7210: 65 20 66 72 6f 6d 20 74 68 65 20 73 65 74 20 73 e from the set s
7220: 68 6f 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20 hown.** here in
7230: 6f 72 64 65 72 20 74 6f 20 69 6e 64 69 63 61 74 order to indicat
7240: 65 73 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 es success or fa
7250: 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 ilure..**.** New
7260: 20 65 72 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 error codes may
7270: 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 be added in fut
7280: 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ure versions of
7290: 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 SQLite..**.** Se
72a0: 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f e also: [SQLITE_
72b0: 49 4f 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74 IOERR_READ | ext
72c0: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
72d0: 65 73 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 es].*/.#define S
72e0: 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 QLITE_OK
72f0: 20 20 20 30 20 20 20 2f 2a 20 53 75 63 63 65 73 0 /* Succes
7300: 73 66 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f sful result */./
7310: 2a 20 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 * beginning-of-e
7320: 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 rror-codes */.#d
7330: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 52 52 efine SQLITE_ERR
7340: 4f 52 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a OR 1 /*
7350: 20 53 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69 SQL error or mi
7360: 73 73 69 6e 67 20 64 61 74 61 62 61 73 65 20 2a ssing database *
7370: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7380: 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20 20 32 20 _INTERNAL 2
7390: 20 20 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f /* Internal lo
73a0: 67 69 63 20 65 72 72 6f 72 20 69 6e 20 53 51 4c gic error in SQL
73b0: 69 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ite */.#define S
73c0: 51 4c 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20 QLITE_PERM
73d0: 20 20 20 33 20 20 20 2f 2a 20 41 63 63 65 73 73 3 /* Access
73e0: 20 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 permission deni
73f0: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ed */.#define SQ
7400: 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20 LITE_ABORT
7410: 20 20 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 4 /* Callbac
7420: 6b 20 72 6f 75 74 69 6e 65 20 72 65 71 75 65 73 k routine reques
7430: 74 65 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a ted an abort */.
7440: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 #define SQLITE_B
7450: 55 53 59 20 20 20 20 20 20 20 20 20 35 20 20 20 USY 5
7460: 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
7470: 66 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a file is locked *
7480: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7490: 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 36 20 _LOCKED 6
74a0: 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 /* A table in
74b0: 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
74c0: 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e locked */.#defin
74d0: 65 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 e SQLITE_NOMEM
74e0: 20 20 20 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 7 /* A m
74f0: 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 2a alloc() failed *
7500: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7510: 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 _READONLY 8
7520: 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 /* Attempt to
7530: 77 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 write a readonly
7540: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 database */.#de
7550: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 fine SQLITE_INTE
7560: 52 52 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20 RRUPT 9 /*
7570: 4f 70 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e Operation termin
7580: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f ated by sqlite3_
7590: 69 6e 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 64 interrupt()*/.#d
75a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
75b0: 52 52 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a RR 10 /*
75c0: 20 53 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69 Some kind of di
75d0: 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 sk I/O error occ
75e0: 75 72 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 urred */.#define
75f0: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
7600: 20 20 20 20 31 31 20 20 20 2f 2a 20 54 68 65 20 11 /* The
7610: 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d database disk im
7620: 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 age is malformed
7630: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
7640: 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31 TE_NOTFOUND 1
7650: 32 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 2 /* NOT USED.
7660: 20 54 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 64 Table or record
7670: 20 6e 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 not found */.#d
7680: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c efine SQLITE_FUL
7690: 4c 20 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a L 13 /*
76a0: 20 49 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 Insertion faile
76b0: 64 20 62 65 63 61 75 73 65 20 64 61 74 61 62 61 d because databa
76c0: 73 65 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 se is full */.#d
76d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e efine SQLITE_CAN
76e0: 54 4f 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a TOPEN 14 /*
76f0: 20 55 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 Unable to open
7700: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
7710: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
7720: 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 ITE_PROTOCOL
7730: 31 35 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 15 /* NOT USED
7740: 2e 20 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 . Database lock
7750: 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a protocol error *
7760: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7770: 5f 45 4d 50 54 59 20 20 20 20 20 20 20 31 36 20 _EMPTY 16
7780: 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 /* Database is
7790: 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e empty */.#defin
77a0: 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 e SQLITE_SCHEMA
77b0: 20 20 20 20 20 31 37 20 20 20 2f 2a 20 54 68 65 17 /* The
77c0: 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
77d0: 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 changed */.#def
77e0: 69 6e 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 ine SQLITE_TOOBI
77f0: 47 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20 53 G 18 /* S
7800: 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 tring or BLOB ex
7810: 63 65 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 74 ceeds size limit
7820: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
7830: 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 31 TE_CONSTRAINT 1
7840: 39 20 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 65 9 /* Abort due
7850: 20 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 to constraint v
7860: 69 6f 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 iolation */.#def
7870: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 ine SQLITE_MISMA
7880: 54 43 48 20 20 20 20 32 30 20 20 20 2f 2a 20 44 TCH 20 /* D
7890: 61 74 61 20 74 79 70 65 20 6d 69 73 6d 61 74 63 ata type mismatc
78a0: 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c h */.#define SQL
78b0: 49 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20 ITE_MISUSE
78c0: 32 31 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 21 /* Library
78d0: 75 73 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 used incorrectly
78e0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
78f0: 54 45 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 TE_NOLFS 2
7900: 32 20 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 2 /* Uses OS f
7910: 65 61 74 75 72 65 73 20 6e 6f 74 20 73 75 70 70 eatures not supp
7920: 6f 72 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f orted on host */
7930: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7940: 41 55 54 48 20 20 20 20 20 20 20 20 32 33 20 20 AUTH 23
7950: 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f /* Authorizatio
7960: 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 n denied */.#def
7970: 69 6e 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 ine SQLITE_FORMA
7980: 54 20 20 20 20 20 20 32 34 20 20 20 2f 2a 20 41 T 24 /* A
7990: 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
79a0: 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a e format error *
79b0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
79c0: 5f 52 41 4e 47 45 20 20 20 20 20 20 20 32 35 20 _RANGE 25
79d0: 20 20 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 /* 2nd paramet
79e0: 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 er to sqlite3_bi
79f0: 6e 64 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 nd out of range
7a00: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7a10: 45 5f 4e 4f 54 41 44 42 20 20 20 20 20 20 32 36 E_NOTADB 26
7a20: 20 20 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 /* File opene
7a30: 64 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 d that is not a
7a40: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
7a50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7a60: 52 4f 57 20 20 20 20 20 20 20 20 20 31 30 30 20 ROW 100
7a70: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 /* sqlite3_step
7a80: 28 29 20 68 61 73 20 61 6e 6f 74 68 65 72 20 72 () has another r
7a90: 6f 77 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 ow ready */.#def
7aa0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 ine SQLITE_DONE
7ab0: 20 20 20 20 20 20 20 31 30 31 20 20 2f 2a 20 73 101 /* s
7ac0: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 qlite3_step() ha
7ad0: 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 s finished execu
7ae0: 74 69 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f ting */./* end-o
7af0: 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f f-error-codes */
7b00: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
7b10: 3a 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c : Extended Resul
7b20: 74 20 43 6f 64 65 73 20 7b 48 31 30 32 32 30 7d t Codes {H10220}
7b30: 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 <S10700>.** KEY
7b40: 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 WORDS: {extended
7b50: 20 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78 error code} {ex
7b60: 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
7b70: 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a es}.** KEYWORDS:
7b80: 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c {extended resul
7b90: 74 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 t code} {extende
7ba0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a d result codes}.
7bb0: 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 **.** In its def
7bc0: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 ault configurati
7bd0: 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50 49 20 72 on, SQLite API r
7be0: 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6f outines return o
7bf0: 6e 65 20 6f 66 20 32 36 20 69 6e 74 65 67 65 72 ne of 26 integer
7c00: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c .** [SQLITE_OK |
7c10: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20 result codes].
7c20: 20 48 6f 77 65 76 65 72 2c 20 65 78 70 65 72 69 However, experi
7c30: 65 6e 63 65 20 68 61 73 20 73 68 6f 77 6e 20 74 ence has shown t
7c40: 68 61 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 hat many of.** t
7c50: 68 65 73 65 20 72 65 73 75 6c 74 20 63 6f 64 65 hese result code
7c60: 73 20 61 72 65 20 74 6f 6f 20 63 6f 61 72 73 65 s are too coarse
7c70: 2d 67 72 61 69 6e 65 64 2e 20 20 54 68 65 79 20 -grained. They
7c80: 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 do not provide a
7c90: 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d s.** much inform
7ca0: 61 74 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62 ation about prob
7cb0: 6c 65 6d 73 20 61 73 20 70 72 6f 67 72 61 6d 6d lems as programm
7cc0: 65 72 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 ers might like.
7cd0: 20 49 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f In an effort to
7ce0: 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 68 69 73 .** address this
7cf0: 2c 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 , newer versions
7d00: 20 6f 66 20 53 51 4c 69 74 65 20 28 76 65 72 73 of SQLite (vers
7d10: 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 ion 3.3.8 and la
7d20: 74 65 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 ter) include.**
7d30: 73 75 70 70 6f 72 74 20 66 6f 72 20 61 64 64 69 support for addi
7d40: 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f tional result co
7d50: 64 65 73 20 74 68 61 74 20 70 72 6f 76 69 64 65 des that provide
7d60: 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 69 more detailed i
7d70: 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 nformation.** ab
7d80: 6f 75 74 20 65 72 72 6f 72 73 2e 20 54 68 65 20 out errors. The
7d90: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
7da0: 63 6f 64 65 73 20 61 72 65 20 65 6e 61 62 6c 65 codes are enable
7db0: 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a d or disabled.**
7dc0: 20 6f 6e 20 61 20 70 65 72 20 64 61 74 61 62 61 on a per databa
7dd0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 se connection ba
7de0: 73 69 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a sis using the.**
7df0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 [sqlite3_extend
7e00: 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 ed_result_codes(
7e10: 29 5d 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f )] API..**.** So
7e20: 6d 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61 me of the availa
7e30: 62 6c 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 ble extended res
7e40: 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 6c 69 ult codes are li
7e50: 73 74 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e sted here..** On
7e60: 65 20 6d 61 79 20 65 78 70 65 63 74 20 74 68 65 e may expect the
7e70: 20 6e 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e number of exten
7e80: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 ded result codes
7e90: 20 77 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a will be expand.
7ea0: 2a 2a 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 ** over time. S
7eb0: 6f 66 74 77 61 72 65 20 74 68 61 74 20 75 73 65 oftware that use
7ec0: 73 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c s extended resul
7ed0: 74 20 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 t codes should e
7ee0: 78 70 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 xpect.** to see
7ef0: 6e 65 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73 new result codes
7f00: 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 in future relea
7f10: 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ses of SQLite..*
7f20: 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
7f30: 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77 OK result code w
7f40: 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74 ill never be ext
7f50: 65 6e 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 ended. It will
7f60: 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 always.** be exa
7f70: 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 ctly zero..*/.#d
7f80: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
7f90: 52 52 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 RR_READ
7fa0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
7fb0: 52 52 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 RR | (1<<8)).#de
7fc0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 fine SQLITE_IOER
7fd0: 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 20 20 20 R_SHORT_READ
7fe0: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 (SQLITE_IOER
7ff0: 52 20 7c 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 R | (2<<8)).#def
8000: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8010: 5f 57 52 49 54 45 20 20 20 20 20 20 20 20 20 20 _WRITE
8020: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8030: 20 7c 20 28 33 3c 3c 38 29 29 0a 23 64 65 66 69 | (3<<8)).#defi
8040: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ne SQLITE_IOERR_
8050: 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 20 20 FSYNC
8060: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 (SQLITE_IOERR
8070: 7c 20 28 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e | (4<<8)).#defin
8080: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 e SQLITE_IOERR_D
8090: 49 52 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20 IR_FSYNC
80a0: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
80b0: 20 28 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 (5<<8)).#define
80c0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 SQLITE_IOERR_TR
80d0: 55 4e 43 41 54 45 20 20 20 20 20 20 20 20 20 20 UNCATE
80e0: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 (SQLITE_IOERR |
80f0: 28 36 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 (6<<8)).#define
8100: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 SQLITE_IOERR_FST
8110: 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 28 AT (
8120: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
8130: 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 7<<8)).#define S
8140: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
8150: 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 28 53 CK (S
8160: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 QLITE_IOERR | (8
8170: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
8180: 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 LITE_IOERR_RDLOC
8190: 4b 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 K (SQ
81a0: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c LITE_IOERR | (9<
81b0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
81c0: 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 ITE_IOERR_DELETE
81d0: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
81e0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c ITE_IOERR | (10<
81f0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8200: 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
8210: 44 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c D (SQL
8220: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c ITE_IOERR | (11<
8230: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8240: 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 ITE_IOERR_NOMEM
8250: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
8260: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c ITE_IOERR | (12<
8270: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8280: 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 ITE_IOERR_ACCESS
8290: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
82a0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c ITE_IOERR | (13<
82b0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
82c0: 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
82d0: 45 53 45 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c ESERVEDLOCK (SQL
82e0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c ITE_IOERR | (14<
82f0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8300: 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 ITE_IOERR_LOCK
8310: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
8320: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c ITE_IOERR | (15<
8330: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8340: 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 ITE_IOERR_CLOSE
8350: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
8360: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c ITE_IOERR | (16<
8370: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8380: 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c ITE_IOERR_DIR_CL
8390: 4f 53 45 20 20 20 20 20 20 20 20 20 28 53 51 4c OSE (SQL
83a0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c ITE_IOERR | (17<
83b0: 3c 38 29 29 0a 0a 23 64 65 66 69 6e 65 20 53 51 <8))..#define SQ
83c0: 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 LITE_LOCKED_SHAR
83d0: 45 44 43 41 43 48 45 20 20 20 20 20 20 28 53 51 EDCACHE (SQ
83e0: 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 7c 20 28 31 LITE_LOCKED | (1
83f0: 3c 3c 38 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 <<8) )../*.** CA
8400: 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20 46 6f PI3REF: Flags Fo
8410: 72 20 46 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72 r File Open Oper
8420: 61 74 69 6f 6e 73 20 7b 48 31 30 32 33 30 7d 20 ations {H10230}
8430: 3c 48 31 31 31 32 30 3e 20 3c 48 31 32 37 30 30 <H11120> <H12700
8440: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 62 69 >.**.** These bi
8450: 74 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 74 t values are int
8460: 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e ended for use in
8470: 20 74 68 65 0a 2a 2a 20 33 72 64 20 70 61 72 61 the.** 3rd para
8480: 6d 65 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 meter to the [sq
8490: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
84a0: 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 0a 2a interface and.*
84b0: 2a 20 69 6e 20 74 68 65 20 34 74 68 20 70 61 72 * in the 4th par
84c0: 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 78 4f ameter to the xO
84d0: 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 pen method of th
84e0: 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 e.** [sqlite3_vf
84f0: 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 s] object..*/.#d
8500: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8510: 4e 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 N_READONLY
8520: 20 20 20 30 78 30 30 30 30 30 30 30 31 0a 23 64 0x00000001.#d
8530: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8540: 4e 5f 52 45 41 44 57 52 49 54 45 20 20 20 20 20 N_READWRITE
8550: 20 20 20 30 78 30 30 30 30 30 30 30 32 0a 23 64 0x00000002.#d
8560: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8570: 4e 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 20 N_CREATE
8580: 20 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 0x00000004.#d
8590: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
85a0: 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 N_DELETEONCLOSE
85b0: 20 20 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 0x00000008.#d
85c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
85d0: 4e 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 N_EXCLUSIVE
85e0: 20 20 20 30 78 30 30 30 30 30 30 31 30 0a 23 64 0x00000010.#d
85f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8600: 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 20 20 N_MAIN_DB
8610: 20 20 20 30 78 30 30 30 30 30 31 30 30 0a 23 64 0x00000100.#d
8620: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8630: 4e 5f 54 45 4d 50 5f 44 42 20 20 20 20 20 20 20 N_TEMP_DB
8640: 20 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 0x00000200.#d
8650: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8660: 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 20 N_TRANSIENT_DB
8670: 20 20 20 30 78 30 30 30 30 30 34 30 30 0a 23 64 0x00000400.#d
8680: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8690: 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 N_MAIN_JOURNAL
86a0: 20 20 20 30 78 30 30 30 30 30 38 30 30 0a 23 64 0x00000800.#d
86b0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
86c0: 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 N_TEMP_JOURNAL
86d0: 20 20 20 30 78 30 30 30 30 31 30 30 30 0a 23 64 0x00001000.#d
86e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
86f0: 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 20 N_SUBJOURNAL
8700: 20 20 20 30 78 30 30 30 30 32 30 30 30 0a 23 64 0x00002000.#d
8710: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8720: 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c N_MASTER_JOURNAL
8730: 20 20 20 30 78 30 30 30 30 34 30 30 30 0a 23 64 0x00004000.#d
8740: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8750: 4e 5f 4e 4f 4d 55 54 45 58 20 20 20 20 20 20 20 N_NOMUTEX
8760: 20 20 20 30 78 30 30 30 30 38 30 30 30 0a 23 64 0x00008000.#d
8770: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 efine SQLITE_OPE
8780: 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 20 20 20 20 N_FULLMUTEX
8790: 20 20 20 30 78 30 30 30 31 30 30 30 30 0a 0a 2f 0x00010000../
87a0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
87b0: 65 76 69 63 65 20 43 68 61 72 61 63 74 65 72 69 evice Characteri
87c0: 73 74 69 63 73 20 7b 48 31 30 32 34 30 7d 20 3c stics {H10240} <
87d0: 48 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 H11120>.**.** Th
87e0: 65 20 78 44 65 76 69 63 65 43 61 70 61 62 69 6c e xDeviceCapabil
87f0: 69 74 69 65 73 20 6d 65 74 68 6f 64 20 6f 66 20 ities method of
8800: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f the [sqlite3_io_
8810: 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65 methods].** obje
8820: 63 74 20 72 65 74 75 72 6e 73 20 61 6e 20 69 6e ct returns an in
8830: 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 61 teger which is a
8840: 20 76 65 63 74 6f 72 20 6f 66 20 74 68 65 20 74 vector of the t
8850: 68 65 73 65 0a 2a 2a 20 62 69 74 20 76 61 6c 75 hese.** bit valu
8860: 65 73 20 65 78 70 72 65 73 73 69 6e 67 20 49 2f es expressing I/
8870: 4f 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 O characteristic
8880: 73 20 6f 66 20 74 68 65 20 6d 61 73 73 20 73 74 s of the mass st
8890: 6f 72 61 67 65 0a 2a 2a 20 64 65 76 69 63 65 20 orage.** device
88a0: 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 66 that holds the f
88b0: 69 6c 65 20 74 68 61 74 20 74 68 65 20 5b 73 71 ile that the [sq
88c0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
88d0: 5d 0a 2a 2a 20 72 65 66 65 72 73 20 74 6f 2e 0a ].** refers to..
88e0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
88f0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 _IOCAP_ATOMIC pr
8900: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 operty means tha
8910: 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a t all writes of.
8920: 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20 ** any size are
8930: 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c atomic. The SQL
8940: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
8950: 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 nnn values.** me
8960: 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 20 6f an that writes o
8970: 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 f blocks that ar
8980: 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 e nnn bytes in s
8990: 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 ize and.** are a
89a0: 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 ligned to an add
89b0: 72 65 73 73 20 77 68 69 63 68 20 69 73 20 61 6e ress which is an
89c0: 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c integer multipl
89d0: 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 e of.** nnn are
89e0: 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c atomic. The SQL
89f0: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
8a00: 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e PPEND value mean
8a10: 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64 s.** that when d
8a20: 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 20 ata is appended
8a30: 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64 to a file, the d
8a40: 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a ata is appended.
8a50: 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 ** first then th
8a60: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 e size of the fi
8a70: 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 le is extended,
8a80: 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a never the other.
8a90: 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 ** way around.
8aa0: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 The SQLITE_IOCAP
8ab0: 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 _SEQUENTIAL prop
8ac0: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a erty means that.
8ad0: 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ** information i
8ae0: 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 s written to dis
8af0: 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 k in the same or
8b00: 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 der as calls.**
8b10: 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a 2f 0a to xWrite()..*/.
8b20: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8b30: 4f 43 41 50 5f 41 54 4f 4d 49 43 20 20 20 20 20 OCAP_ATOMIC
8b40: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31 0a 0x00000001.
8b50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8b60: 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 20 20 OCAP_ATOMIC512
8b70: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 32 0a 0x00000002.
8b80: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8b90: 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 20 20 20 OCAP_ATOMIC1K
8ba0: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 34 0a 0x00000004.
8bb0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8bc0: 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20 OCAP_ATOMIC2K
8bd0: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 38 0a 0x00000008.
8be0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8bf0: 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 20 20 20 OCAP_ATOMIC4K
8c00: 20 20 20 20 20 30 78 30 30 30 30 30 30 31 30 0a 0x00000010.
8c10: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8c20: 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 20 20 20 OCAP_ATOMIC8K
8c30: 20 20 20 20 20 30 78 30 30 30 30 30 30 32 30 0a 0x00000020.
8c40: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8c50: 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b 20 20 OCAP_ATOMIC16K
8c60: 20 20 20 20 20 30 78 30 30 30 30 30 30 34 30 0a 0x00000040.
8c70: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8c80: 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 20 20 OCAP_ATOMIC32K
8c90: 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 0a 0x00000080.
8ca0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8cb0: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20 OCAP_ATOMIC64K
8cc0: 20 20 20 20 20 30 78 30 30 30 30 30 31 30 30 0a 0x00000100.
8cd0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8ce0: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
8cf0: 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30 0a 0x00000200.
8d00: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8d10: 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 OCAP_SEQUENTIAL
8d20: 20 20 20 20 20 30 78 30 30 30 30 30 34 30 30 0a 0x00000400.
8d30: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
8d40: 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4c 65 File Locking Le
8d50: 76 65 6c 73 20 7b 48 31 30 32 35 30 7d 20 3c 48 vels {H10250} <H
8d60: 31 31 31 32 30 3e 20 3c 48 31 31 33 31 30 3e 0a 11120> <H11310>.
8d70: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 **.** SQLite use
8d80: 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 s one of these i
8d90: 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 73 nteger values as
8da0: 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 the second.** a
8db0: 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 73 rgument to calls
8dc0: 20 69 74 20 6d 61 6b 65 73 20 74 6f 20 74 68 65 it makes to the
8dd0: 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e xLock() and xUn
8de0: 6c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 73 0a 2a lock() methods.*
8df0: 2a 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 * of an [sqlite3
8e00: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a _io_methods] obj
8e10: 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ect..*/.#define
8e20: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 SQLITE_LOCK_NONE
8e30: 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 0.#def
8e40: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f ine SQLITE_LOCK_
8e50: 53 48 41 52 45 44 20 20 20 20 20 20 20 20 31 0a SHARED 1.
8e60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
8e70: 4f 43 4b 5f 52 45 53 45 52 56 45 44 20 20 20 20 OCK_RESERVED
8e80: 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 2.#define SQLI
8e90: 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 20 TE_LOCK_PENDING
8ea0: 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 3.#define
8eb0: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c SQLITE_LOCK_EXCL
8ec0: 55 53 49 56 45 20 20 20 20 20 34 0a 0a 2f 2a 0a USIVE 4../*.
8ed0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 79 6e ** CAPI3REF: Syn
8ee0: 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 20 54 79 70 chronization Typ
8ef0: 65 20 46 6c 61 67 73 20 7b 48 31 30 32 36 30 7d e Flags {H10260}
8f00: 20 3c 48 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 <H11120>.**.**
8f10: 57 68 65 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f When SQLite invo
8f20: 6b 65 73 20 74 68 65 20 78 53 79 6e 63 28 29 20 kes the xSync()
8f30: 6d 65 74 68 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 method of an.**
8f40: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 [sqlite3_io_meth
8f50: 6f 64 73 5d 20 6f 62 6a 65 63 74 20 69 74 20 75 ods] object it u
8f60: 73 65 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f ses a combinatio
8f70: 6e 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 69 6e n of.** these in
8f80: 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 73 20 teger values as
8f90: 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
8fa0: 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ent..**.** When
8fb0: 74 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f the SQLITE_SYNC_
8fc0: 44 41 54 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73 DATAONLY flag is
8fd0: 20 75 73 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 used, it means
8fe0: 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 79 6e 63 that the.** sync
8ff0: 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 operation only
9000: 6e 65 65 64 73 20 74 6f 20 66 6c 75 73 68 20 64 needs to flush d
9010: 61 74 61 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 ata to mass stor
9020: 61 67 65 2e 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 age. Inode.** i
9030: 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20 nformation need
9040: 6e 6f 74 20 62 65 20 66 6c 75 73 68 65 64 2e 20 not be flushed.
9050: 54 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f The SQLITE_SYNC_
9060: 4e 4f 52 4d 41 4c 20 66 6c 61 67 20 6d 65 61 6e NORMAL flag mean
9070: 73 0a 2a 2a 20 74 6f 20 75 73 65 20 6e 6f 72 6d s.** to use norm
9080: 61 6c 20 66 73 79 6e 63 28 29 20 73 65 6d 61 6e al fsync() seman
9090: 74 69 63 73 2e 20 54 68 65 20 53 51 4c 49 54 45 tics. The SQLITE
90a0: 5f 53 59 4e 43 5f 46 55 4c 4c 20 66 6c 61 67 20 _SYNC_FULL flag
90b0: 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 means.** to use
90c0: 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 Mac OS X style f
90d0: 75 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20 ullsync instead
90e0: 6f 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 of fsync()..*/.#
90f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 define SQLITE_SY
9100: 4e 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 NC_NORMAL
9110: 20 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65 0x00002.#define
9120: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c SQLITE_SYNC_FUL
9130: 4c 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 L 0x000
9140: 30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 03.#define SQLIT
9150: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 E_SYNC_DATAONLY
9160: 20 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0x00010../*
9170: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 .** CAPI3REF: OS
9180: 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20 Interface Open
9190: 46 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 File Handle {H11
91a0: 31 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 110} <S20110>.**
91b0: 0a 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f .** An [sqlite3_
91c0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 file] object rep
91d0: 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 resents an open
91e0: 66 69 6c 65 20 69 6e 20 74 68 65 20 4f 53 0a 2a file in the OS.*
91f0: 2a 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 * interface laye
9200: 72 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 4f r. Individual O
9210: 53 20 69 6e 74 65 72 66 61 63 65 20 69 6d 70 6c S interface impl
9220: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c ementations will
9230: 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73 75 62 63 .** want to subc
9240: 6c 61 73 73 20 74 68 69 73 20 6f 62 6a 65 63 74 lass this object
9250: 20 62 79 20 61 70 70 65 6e 64 69 6e 67 20 61 64 by appending ad
9260: 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 0a ditional fields.
9270: 2a 2a 20 66 6f 72 20 74 68 65 69 72 20 6f 77 6e ** for their own
9280: 20 75 73 65 2e 20 20 54 68 65 20 70 4d 65 74 68 use. The pMeth
9290: 6f 64 73 20 65 6e 74 72 79 20 69 73 20 61 20 70 ods entry is a p
92a0: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 ointer to an.**
92b0: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 [sqlite3_io_meth
92c0: 6f 64 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 ods] object that
92d0: 20 64 65 66 69 6e 65 73 20 6d 65 74 68 6f 64 73 defines methods
92e0: 20 66 6f 72 20 70 65 72 66 6f 72 6d 69 6e 67 0a for performing.
92f0: 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 74 69 6f 6e ** I/O operation
9300: 73 20 6f 6e 20 74 68 65 20 6f 70 65 6e 20 66 69 s on the open fi
9310: 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 le..*/.typedef s
9320: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 66 69 truct sqlite3_fi
9330: 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 3b le sqlite3_file;
9340: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
9350: 66 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 file {. const s
9360: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f truct sqlite3_io
9370: 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f _methods *pMetho
9380: 64 73 3b 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 ds; /* Methods
9390: 66 6f 72 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 for an open file
93a0: 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.};../*.** CA
93b0: 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 PI3REF: OS Inter
93c0: 66 61 63 65 20 46 69 6c 65 20 56 69 72 74 75 61 face File Virtua
93d0: 6c 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 l Methods Object
93e0: 20 7b 48 31 31 31 32 30 7d 20 3c 53 32 30 31 31 {H11120} <S2011
93f0: 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 66 0>.**.** Every f
9400: 69 6c 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 ile opened by th
9410: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 e [sqlite3_vfs]
9420: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 70 6f 70 xOpen method pop
9430: 75 6c 61 74 65 73 20 61 6e 0a 2a 2a 20 5b 73 71 ulates an.** [sq
9440: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 lite3_file] obje
9450: 63 74 20 28 6f 72 2c 20 6d 6f 72 65 20 63 6f 6d ct (or, more com
9460: 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 63 6c 61 73 monly, a subclas
9470: 73 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c s of the.** [sql
9480: 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 ite3_file] objec
9490: 74 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 t) with a pointe
94a0: 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
94b0: 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 2e of this object.
94c0: 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 .** This object
94d0: 64 65 66 69 6e 65 73 20 74 68 65 20 6d 65 74 68 defines the meth
94e0: 6f 64 73 20 75 73 65 64 20 74 6f 20 70 65 72 66 ods used to perf
94f0: 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f 70 65 72 orm various oper
9500: 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 61 69 6e 73 ations.** agains
9510: 74 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 t the open file
9520: 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 74 represented by t
9530: 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 he [sqlite3_file
9540: 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 ] object..**.**
9550: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 The flags argume
9560: 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d 61 79 20 nt to xSync may
9570: 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 be one of [SQLIT
9580: 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 5d 20 6f E_SYNC_NORMAL] o
9590: 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 59 4e r.** [SQLITE_SYN
95a0: 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 20 66 69 C_FULL]. The fi
95b0: 72 73 74 20 63 68 6f 69 63 65 20 69 73 20 74 68 rst choice is th
95c0: 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 e normal fsync()
95d0: 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ..** The second
95e0: 63 68 6f 69 63 65 20 69 73 20 61 20 4d 61 63 20 choice is a Mac
95f0: 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 OS X style fulls
9600: 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 ync. The [SQLIT
9610: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 5d E_SYNC_DATAONLY]
9620: 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 62 65 20 .** flag may be
9630: 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e 64 69 63 ORed in to indic
9640: 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 ate that only th
9650: 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 66 69 e data of the fi
9660: 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 20 69 74 le.** and not it
9670: 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 20 74 6f s inode needs to
9680: 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a be synced..**.*
9690: 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61 * The integer va
96a0: 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28 29 20 lues to xLock()
96b0: 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 61 72 and xUnlock() ar
96c0: 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75 6c 3e e one of.** <ul>
96d0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
96e0: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20 _LOCK_NONE],.**
96f0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 <li> [SQLITE_LOC
9700: 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 3c 6c K_SHARED],.** <l
9710: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f i> [SQLITE_LOCK_
9720: 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20 3c 6c RESERVED],.** <l
9730: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f i> [SQLITE_LOCK_
9740: 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a 2a 20 PENDING], or.**
9750: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 <li> [SQLITE_LOC
9760: 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a 2a 2a K_EXCLUSIVE]..**
9770: 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b 28 </ul>.** xLock(
9780: 29 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20 ) increases the
9790: 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28 29 20 lock. xUnlock()
97a0: 64 65 63 72 65 61 73 65 73 20 74 68 65 20 6c 6f decreases the lo
97b0: 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 68 65 63 ck..** The xChec
97c0: 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 20 kReservedLock()
97d0: 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 20 77 68 method checks wh
97e0: 65 74 68 65 72 20 61 6e 79 20 64 61 74 61 62 61 ether any databa
97f0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 2a se connection,.*
9800: 2a 20 65 69 74 68 65 72 20 69 6e 20 74 68 69 73 * either in this
9810: 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 73 process or in s
9820: 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ome other proces
9830: 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 s, is holding a
9840: 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50 45 4e RESERVED,.** PEN
9850: 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55 53 49 DING, or EXCLUSI
9860: 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 VE lock on the f
9870: 69 6c 65 2e 20 20 49 74 20 72 65 74 75 72 6e 73 ile. It returns
9880: 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 75 63 68 true.** if such
9890: 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 61 a lock exists a
98a0: 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 nd false otherwi
98b0: 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 se..**.** The xF
98c0: 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 65 74 ileControl() met
98d0: 68 6f 64 20 69 73 20 61 20 67 65 6e 65 72 69 63 hod is a generic
98e0: 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 interface that
98f0: 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d 0a 2a 2a allows custom.**
9900: 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 VFS implementat
9910: 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 74 6c 79 ions to directly
9920: 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f 70 65 6e control an open
9930: 20 66 69 6c 65 20 75 73 69 6e 67 20 74 68 65 0a file using the.
9940: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 ** [sqlite3_file
9950: 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 _control()] inte
9960: 72 66 61 63 65 2e 20 20 54 68 65 20 73 65 63 6f rface. The seco
9970: 6e 64 20 22 6f 70 22 20 61 72 67 75 6d 65 6e 74 nd "op" argument
9980: 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 is an.** intege
9990: 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 74 r opcode. The t
99a0: 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 hird argument is
99b0: 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69 6e 74 a generic point
99c0: 65 72 20 69 6e 74 65 6e 64 65 64 20 74 6f 0a 2a er intended to.*
99d0: 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 * point to a str
99e0: 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 20 ucture that may
99f0: 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65 6e 74 contain argument
9a00: 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20 77 68 s or space in wh
9a10: 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 ich to.** write
9a20: 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e 20 20 return values.
9a30: 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73 20 66 Potential uses f
9a40: 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 or xFileControl(
9a50: 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 66 75 ) might be.** fu
9a60: 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61 62 6c nctions to enabl
9a70: 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 6b 73 e blocking locks
9a80: 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73 2c 20 with timeouts,
9a90: 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a to change the.**
9aa0: 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
9ab0: 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 74 y (for example t
9ac0: 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 20 6c o use dot-file l
9ad0: 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 69 72 ocks), to inquir
9ae0: 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 73 e.** about the s
9af0: 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 6b 2c tatus of a lock,
9b00: 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 74 61 or to break sta
9b10: 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 53 le locks. The S
9b20: 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 72 65 QLite.** core re
9b30: 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63 6f 64 serves all opcod
9b40: 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 es less than 100
9b50: 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75 73 65 for its own use
9b60: 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45 5f 46 ..** A [SQLITE_F
9b70: 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 7c CNTL_LOCKSTATE |
9b80: 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64 65 73 list of opcodes
9b90: 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20 ] less than 100
9ba0: 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a is available..**
9bb0: 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 Applications th
9bc0: 61 74 20 64 65 66 69 6e 65 20 61 20 63 75 73 74 at define a cust
9bd0: 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 om xFileControl
9be0: 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 75 73 method should us
9bf0: 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 67 72 65 e opcodes.** gre
9c00: 61 74 65 72 20 74 68 61 6e 20 31 30 30 20 74 6f ater than 100 to
9c10: 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63 74 73 avoid conflicts
9c20: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 65 63 ..**.** The xSec
9c30: 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 torSize() method
9c40: 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 65 63 returns the sec
9c50: 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 0a tor size of the.
9c60: 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 75 ** device that u
9c70: 6e 64 65 72 6c 69 65 73 20 74 68 65 20 66 69 6c nderlies the fil
9c80: 65 2e 20 20 54 68 65 20 73 65 63 74 6f 72 20 73 e. The sector s
9c90: 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 6d 69 ize is the.** mi
9ca0: 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 68 61 74 nimum write that
9cb0: 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 can be performe
9cc0: 64 20 77 69 74 68 6f 75 74 20 64 69 73 74 75 72 d without distur
9cd0: 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 62 79 bing.** other by
9ce0: 74 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e tes in the file.
9cf0: 20 20 54 68 65 20 78 44 65 76 69 63 65 43 68 61 The xDeviceCha
9d00: 72 61 63 74 65 72 69 73 74 69 63 73 28 29 0a 2a racteristics().*
9d10: 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 * method returns
9d20: 20 61 20 62 69 74 20 76 65 63 74 6f 72 20 64 65 a bit vector de
9d30: 73 63 72 69 62 69 6e 67 20 62 65 68 61 76 69 6f scribing behavio
9d40: 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75 6e 64 rs of the.** und
9d50: 65 72 6c 79 69 6e 67 20 64 65 76 69 63 65 3a 0a erlying device:.
9d60: 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
9d70: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
9d80: 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c 6c 69 3e _ATOMIC].** <li>
9d90: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 [SQLITE_IOCAP_A
9da0: 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c 6c 69 TOMIC512].** <li
9db0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
9dc0: 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c 6c 69 ATOMIC1K].** <li
9dd0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
9de0: 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 3c 6c 69 ATOMIC2K].** <li
9df0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
9e00: 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c 6c 69 ATOMIC4K].** <li
9e10: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
9e20: 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c 6c 69 ATOMIC8K].** <li
9e30: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
9e40: 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20 3c 6c ATOMIC16K].** <l
9e50: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
9e60: 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a 20 3c _ATOMIC32K].** <
9e70: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
9e80: 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a 2a 20 P_ATOMIC64K].**
9e90: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
9ea0: 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 5d 0a AP_SAFE_APPEND].
9eb0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
9ec0: 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
9ed0: 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a ].** </ul>.**.**
9ee0: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 The SQLITE_IOCA
9ef0: 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 P_ATOMIC propert
9f00: 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c y means that all
9f10: 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e writes of.** an
9f20: 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 y size are atomi
9f30: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 c. The SQLITE_I
9f40: 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 OCAP_ATOMICnnn v
9f50: 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 alues.** mean th
9f60: 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f at writes of blo
9f70: 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e cks that are nnn
9f80: 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 bytes in size a
9f90: 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 nd.** are aligne
9fa0: 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 d to an address
9fb0: 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 which is an inte
9fc0: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a ger multiple of.
9fd0: 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 ** nnn are atomi
9fe0: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 c. The SQLITE_I
9ff0: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
a000: 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 value means.**
a010: 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 that when data i
a020: 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 s appended to a
a030: 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 file, the data i
a040: 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 s appended.** fi
a050: 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a rst then the siz
a060: 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 e of the file is
a070: 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 extended, never
a080: 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 the other.** wa
a090: 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 y around. The S
a0a0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 QLITE_IOCAP_SEQU
a0b0: 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 ENTIAL property
a0c0: 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e means that.** in
a0d0: 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 formation is wri
a0e0: 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 tten to disk in
a0f0: 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 the same order a
a100: 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 s calls.** to xW
a110: 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 rite()..**.** If
a120: 20 78 52 65 61 64 28 29 20 72 65 74 75 72 6e 73 xRead() returns
a130: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 SQLITE_IOERR_SH
a140: 4f 52 54 5f 52 45 41 44 20 69 74 20 6d 75 73 74 ORT_READ it must
a150: 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a 20 69 6e also fill.** in
a160: 20 74 68 65 20 75 6e 72 65 61 64 20 70 6f 72 74 the unread port
a170: 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 75 66 66 ions of the buff
a180: 65 72 20 77 69 74 68 20 7a 65 72 6f 73 2e 20 20 er with zeros.
a190: 41 20 56 46 53 20 74 68 61 74 0a 2a 2a 20 66 61 A VFS that.** fa
a1a0: 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c ils to zero-fill
a1b0: 20 73 68 6f 72 74 20 72 65 61 64 73 20 6d 69 67 short reads mig
a1c0: 68 74 20 73 65 65 6d 20 74 6f 20 77 6f 72 6b 2e ht seem to work.
a1d0: 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 61 However,.** fa
a1e0: 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f 2d 66 69 ilure to zero-fi
a1f0: 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 77 ll short reads w
a200: 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 6c ill eventually l
a210: 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 61 62 61 ead to.** databa
a220: 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a se corruption..*
a230: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
a240: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
a250: 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d ods sqlite3_io_m
a260: 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 ethods;.struct s
a270: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
a280: 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73 69 s {. int iVersi
a290: 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f on;. int (*xClo
a2a0: 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 se)(sqlite3_file
a2b0: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65 61 *);. int (*xRea
a2c0: 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a d)(sqlite3_file*
a2d0: 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d , void*, int iAm
a2e0: 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 t, sqlite3_int64
a2f0: 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 iOfst);. int (
a300: 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74 65 33 *xWrite)(sqlite3
a310: 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f _file*, const vo
a320: 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73 id*, int iAmt, s
a330: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 qlite3_int64 iOf
a340: 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 54 72 st);. int (*xTr
a350: 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f uncate)(sqlite3_
a360: 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69 file*, sqlite3_i
a370: 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20 69 6e nt64 size);. in
a380: 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 t (*xSync)(sqlit
a390: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 66 6c e3_file*, int fl
a3a0: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 ags);. int (*xF
a3b0: 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74 65 33 ileSize)(sqlite3
a3c0: 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f _file*, sqlite3_
a3d0: 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b 0a 20 int64 *pSize);.
a3e0: 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28 73 71 int (*xLock)(sq
a3f0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
a400: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e 6c 6f );. int (*xUnlo
a410: 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck)(sqlite3_file
a420: 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 *, int);. int (
a430: 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c *xCheckReservedL
a440: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ock)(sqlite3_fil
a450: 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 e*, int *pResOut
a460: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 65 );. int (*xFile
a470: 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74 65 33 Control)(sqlite3
a480: 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70 2c 20 _file*, int op,
a490: 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20 20 69 void *pArg);. i
a4a0: 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a 65 nt (*xSectorSize
a4b0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 )(sqlite3_file*)
a4c0: 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76 69 63 ;. int (*xDevic
a4d0: 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
a4e0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 )(sqlite3_file*)
a4f0: 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e 61 ;. /* Additiona
a500: 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20 62 65 l methods may be
a510: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 added in future
a520: 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d 3b 0a releases */.};.
a530: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
a540: 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65 20 43 Standard File C
a550: 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73 20 7b ontrol Opcodes {
a560: 48 31 31 33 31 30 7d 20 3c 53 33 30 38 30 30 3e H11310} <S30800>
a570: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 .**.** These int
a580: 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 61 eger constants a
a590: 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 74 re opcodes for t
a5a0: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 he xFileControl
a5b0: 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 method.** of the
a5c0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 [sqlite3_io_met
a5d0: 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 6e 64 hods] object and
a5e0: 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 for the [sqlite
a5f0: 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 3_file_control()
a600: 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a ].** interface..
a610: 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 **.** The [SQLIT
a620: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 E_FCNTL_LOCKSTAT
a630: 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 E] opcode is use
a640: 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e d for debugging.
a650: 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f 64 65 This.** opcode
a660: 20 63 61 75 73 65 73 20 74 68 65 20 78 46 69 6c causes the xFil
a670: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 eControl method
a680: 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72 to write the cur
a690: 72 65 6e 74 20 73 74 61 74 65 20 6f 66 0a 2a 2a rent state of.**
a6a0: 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e 65 20 6f the lock (one o
a6b0: 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e f [SQLITE_LOCK_N
a6c0: 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f ONE], [SQLITE_LO
a6d0: 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 5b CK_SHARED],.** [
a6e0: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 SQLITE_LOCK_RESE
a6f0: 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c RVED], [SQLITE_L
a700: 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 OCK_PENDING], or
a710: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 [SQLITE_LOCK_EX
a720: 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 69 6e 74 CLUSIVE]).** int
a730: 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 o an integer tha
a740: 74 20 74 68 65 20 70 41 72 67 20 61 72 67 75 6d t the pArg argum
a750: 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 54 ent points to. T
a760: 68 69 73 20 63 61 70 61 62 69 6c 69 74 79 0a 2a his capability.*
a770: 2a 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 * is used during
a780: 20 74 65 73 74 69 6e 67 20 61 6e 64 20 6f 6e 6c testing and onl
a790: 79 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 75 y needs to be su
a7a0: 70 70 6f 72 74 65 64 20 77 68 65 6e 20 53 51 4c pported when SQL
a7b0: 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 73 20 64 ITE_TEST.** is d
a7c0: 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 efined..*/.#defi
a7d0: 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f ne SQLITE_FCNTL_
a7e0: 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 20 20 20 LOCKSTATE
a7f0: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
a800: 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_GET_LOCKPROXYF
a810: 49 4c 45 20 20 20 20 20 20 32 0a 23 64 65 66 69 ILE 2.#defi
a820: 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f ne SQLITE_SET_LO
a830: 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20 CKPROXYFILE
a840: 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 3.#define SQLIT
a850: 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 20 20 20 E_LAST_ERRNO
a860: 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 4../*.*
a870: 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
a880: 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 31 30 x Handle {H17110
a890: 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a 2a } <S20130>.**.**
a8a0: 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75 6c The mutex modul
a8b0: 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 20 e within SQLite
a8c0: 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65 33 defines [sqlite3
a8d0: 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61 6e _mutex] to be an
a8e0: 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 79 70 .** abstract typ
a8f0: 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 6f 62 e for a mutex ob
a900: 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c 69 74 ject. The SQLit
a910: 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c 6f 6f e core never loo
a920: 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 6e 74 ks.** at the int
a930: 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
a940: 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 6c 69 tion of an [sqli
a950: 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 74 20 te3_mutex]. It
a960: 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 77 69 only.** deals wi
a970: 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 th pointers to t
a980: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 he [sqlite3_mute
a990: 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a x] object..**.**
a9a0: 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 72 65 Mutexes are cre
a9b0: 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 ated using [sqli
a9c0: 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
a9d0: 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 )]..*/.typedef s
a9e0: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 truct sqlite3_mu
a9f0: 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tex sqlite3_mute
aa00: 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 x;../*.** CAPI3R
aa10: 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 EF: OS Interface
aa20: 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 30 7d Object {H11140}
aa30: 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20100>.**.**
aa40: 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
aa50: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f he sqlite3_vfs o
aa60: 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 bject defines th
aa70: 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 e interface betw
aa80: 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 een.** the SQLit
aa90: 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20 75 e core and the u
aaa0: 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 nderlying operat
aab0: 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68 65 ing system. The
aac0: 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68 65 "vfs".** in the
aad0: 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62 6a name of the obj
aae0: 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20 22 ect stands for "
aaf0: 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 virtual file sys
ab00: 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tem"..**.** The
ab10: 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56 65 value of the iVe
ab20: 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 69 rsion field is i
ab30: 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20 6d nitially 1 but m
ab40: 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e 0a ay be larger in.
ab50: 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69 6f ** future versio
ab60: 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 ns of SQLite. A
ab70: 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 dditional fields
ab80: 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64 may be appended
ab90: 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 to this.** obje
aba0: 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65 72 ct when the iVer
abb0: 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69 6e sion value is in
abc0: 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20 74 creased. Note t
abd0: 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72 hat the structur
abe0: 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 e.** of the sqli
abf0: 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 63 te3_vfs object c
ac00: 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 74 72 hanges in the tr
ac10: 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 65 65 ansaction betwee
ac20: 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73 n.** SQLite vers
ac30: 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 33 2e ion 3.5.9 and 3.
ac40: 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 65 20 6.0 and yet the
ac50: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 77 iVersion field w
ac60: 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66 69 as not.** modifi
ac70: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a ed..**.** The sz
ac80: 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 73 20 OsFile field is
ac90: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
aca0: 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 6c 69 subclassed [sqli
acb0: 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74 72 te3_file].** str
acc0: 75 63 74 75 72 65 20 75 73 65 64 20 62 79 20 74 ucture used by t
acd0: 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 74 68 his VFS. mxPath
ace0: 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 78 69 name is the maxi
acf0: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a mum length of.**
ad00: 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 74 a pathname in t
ad10: 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 his VFS..**.** R
ad20: 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 74 65 egistered sqlite
ad30: 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 61 72 3_vfs objects ar
ad40: 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b e kept on a link
ad50: 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20 62 ed list formed b
ad60: 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20 70 y.** the pNext p
ad70: 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73 71 ointer. The [sq
ad80: 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
ad90: 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 er()].** and [sq
ada0: 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 lite3_vfs_unregi
adb0: 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63 ster()] interfac
adc0: 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20 6c es manage this l
add0: 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72 65 ist.** in a thre
ade0: 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54 68 ad-safe way. Th
adf0: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 e [sqlite3_vfs_f
ae00: 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63 65 ind()] interface
ae10: 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68 65 .** searches the
ae20: 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 72 20 list. Neither
ae30: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
ae40: 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 46 53 code nor the VFS
ae50: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
ae60: 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 on should use th
ae70: 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e e pNext pointer.
ae80: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 78 74 .**.** The pNext
ae90: 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f 6e field is the on
aea0: 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 ly field in the
aeb0: 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 73 sqlite3_vfs.** s
aec0: 74 72 75 63 74 75 72 65 20 74 68 61 74 20 53 51 tructure that SQ
aed0: 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 20 6d Lite will ever m
aee0: 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 20 77 odify. SQLite w
aef0: 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 73 0a ill only access.
af00: 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 ** or modify thi
af10: 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 68 6f s field while ho
af20: 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c lding a particul
af30: 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 78 2e ar static mutex.
af40: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 .** The applicat
af50: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 ion should never
af60: 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 6e 67 modify anything
af70: 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 6c 69 within the sqli
af80: 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65 63 te3_vfs.** objec
af90: 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a 65 63 t once the objec
afa0: 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73 t has been regis
afb0: 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tered..**.** The
afc0: 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 6f 6c zName field hol
afd0: 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 ds the name of t
afe0: 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e 20 20 he VFS module.
aff0: 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a 2a 2a The name must.**
b000: 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 6f 73 be unique acros
b010: 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 6c 65 s all VFS module
b020: 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 s..**.** SQLite
b030: 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65 20 74 will guarantee t
b040: 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d hat the zFilenam
b050: 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78 e parameter to x
b060: 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 74 68 65 Open.** is eithe
b070: 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 r a NULL pointer
b080: 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74 61 69 or string obtai
b090: 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 46 75 6c ned.** from xFul
b0a0: 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 20 53 51 lPathname(). SQ
b0b0: 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 75 61 Lite further gua
b0c0: 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a 20 rantees that.**
b0d0: 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 the string will
b0e0: 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 6e 63 be valid and unc
b0f0: 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 43 6c hanged until xCl
b100: 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c ose() is.** call
b110: 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 ed. Because of t
b120: 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 he previous sent
b130: 65 6e 73 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 71 ense,.** the [sq
b140: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e 20 lite3_file] can
b150: 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20 70 safely store a p
b160: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a ointer to the.**
b170: 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74 20 filename if it
b180: 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62 65 needs to remembe
b190: 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 66 r the filename f
b1a0: 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a or some reason..
b1b0: 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65 6e ** If the zFilen
b1c0: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 ame parameter is
b1d0: 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c 4c xOpen is a NULL
b1e0: 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78 4f pointer then xO
b1f0: 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 69 pen.** must invi
b200: 74 65 20 69 74 73 20 6f 77 6e 20 74 65 6d 70 6f te its own tempo
b210: 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74 68 rary name for th
b220: 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 76 65 e file. Wheneve
b230: 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c 65 6e r the .** xFilen
b240: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 ame parameter is
b250: 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 61 6c NULL it will al
b260: 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 74 so be the case t
b270: 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 67 73 hat the.** flags
b280: 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c 20 parameter will
b290: 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f include [SQLITE_
b2a0: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
b2b0: 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 SE]..**.** The f
b2c0: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f lags argument to
b2d0: 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75 64 65 xOpen() include
b2e0: 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74 20 69 s all bits set i
b2f0: 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73 20 61 n.** the flags a
b300: 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 rgument to [sqli
b310: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 te3_open_v2()].
b320: 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f Or if [sqlite3_
b330: 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 open()].** or [s
b340: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
b350: 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 66 is used, then f
b360: 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20 61 74 lags includes at
b370: 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c 49 54 least.** [SQLIT
b380: 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
b390: 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e ] | [SQLITE_OPEN
b3a0: 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20 49 66 _CREATE]. .** If
b3b0: 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20 61 xOpen() opens a
b3c0: 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79 20 file read-only
b3d0: 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70 4f then it sets *pO
b3e0: 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69 6e utFlags to.** in
b3f0: 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 clude [SQLITE_OP
b400: 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f EN_READONLY]. O
b410: 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70 4f ther bits in *pO
b420: 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 20 73 utFlags may be s
b430: 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 et..**.** SQLite
b440: 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64 20 6f will also add o
b450: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
b460: 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 ing flags to the
b470: 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 61 6c 6c xOpen().** call
b480: 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 , depending on t
b490: 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e 67 20 he object being
b4a0: 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 opened:.**.** <u
b4b0: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c l>.** <li> [SQL
b4c0: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
b4d0: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 ].** <li> [SQLI
b4e0: 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
b4f0: 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b RNAL].** <li> [
b500: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
b510: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 _DB].** <li> [S
b520: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f QLITE_OPEN_TEMP_
b530: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e JOURNAL].** <li>
b540: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 [SQLITE_OPEN_T
b550: 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a 2a 20 RANSIENT_DB].**
b560: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
b570: 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d 0a 2a EN_SUBJOURNAL].*
b580: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f * <li> [SQLITE_
b590: 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
b5a0: 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a NAL].** </ul>.**
b5b0: 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 2f 4f .** The file I/O
b5c0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
b5d0: 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 6a 65 can use the obje
b5e0: 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 74 6f ct type flags to
b5f0: 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20 77 .** change the w
b600: 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 74 68 ay it deals with
b610: 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 78 61 files. For exa
b620: 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 mple, an applica
b630: 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f 65 tion.** that doe
b640: 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 s not care about
b650: 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 79 20 crash recovery
b660: 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 68 or rollback migh
b670: 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f 70 t make.** the op
b680: 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 en of a journal
b690: 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 57 file a no-op. W
b6a0: 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a 6f rites to this jo
b6b0: 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20 61 urnal would.** a
b6c0: 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 61 lso be no-ops, a
b6d0: 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74 nd any attempt t
b6e0: 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72 6e o read the journ
b6f0: 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 0a al would return.
b700: 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2e ** SQLITE_IOERR.
b710: 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65 Or the impleme
b720: 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 65 ntation might re
b730: 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20 64 cognize that a d
b740: 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 atabase.** file
b750: 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70 61 will be doing pa
b760: 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 6f ge-aligned secto
b770: 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 r reads and writ
b780: 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a 2a es in a random.*
b790: 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 74 20 * order and set
b7a0: 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73 79 up its I/O subsy
b7b0: 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c 79 stem accordingly
b7c0: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d ..**.** SQLite m
b7d0: 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f 6e ight also add on
b7e0: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
b7f0: 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 20 ng flags to the
b800: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a 2a xOpen method:.**
b810: 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
b820: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 [SQLITE_OPEN_DE
b830: 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 LETEONCLOSE].**
b840: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 <li> [SQLITE_OPE
b850: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a 20 N_EXCLUSIVE].**
b860: 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 </ul>.**.** The
b870: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c [SQLITE_OPEN_DEL
b880: 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c 61 67 ETEONCLOSE] flag
b890: 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20 means the file
b8a0: 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 65 6c should be.** del
b8b0: 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 eted when it is
b8c0: 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 5b 53 51 closed. The [SQ
b8d0: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
b8e0: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c ONCLOSE].** will
b8f0: 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50 be set for TEMP
b900: 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f 75 databases, jou
b910: 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73 75 rnals and for su
b920: 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a bjournals..**.**
b930: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 The [SQLITE_OPE
b940: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c 61 N_EXCLUSIVE] fla
b950: 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 g means the file
b960: 20 73 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 should be opene
b970: 64 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 d.** for exclusi
b980: 76 65 20 61 63 63 65 73 73 2e 20 20 54 68 69 73 ve access. This
b990: 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 flag is set for
b9a0: 20 61 6c 6c 20 66 69 6c 65 73 20 65 78 63 65 70 all files excep
b9b0: 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6d 61 69 t.** for the mai
b9c0: 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
b9d0: 0a 2a 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 .**.** At least
b9e0: 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 20 6f szOsFile bytes o
b9f0: 66 20 6d 65 6d 6f 72 79 20 61 72 65 20 61 6c 6c f memory are all
ba00: 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 ocated by SQLite
ba10: 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 .** to hold the
ba20: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 [sqlite3_file]
ba30: 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 structure passed
ba40: 20 61 73 20 74 68 65 20 74 68 69 72 64 0a 2a 2a as the third.**
ba50: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 argument to xOp
ba60: 65 6e 2e 20 20 54 68 65 20 78 4f 70 65 6e 20 6d en. The xOpen m
ba70: 65 74 68 6f 64 20 64 6f 65 73 20 6e 6f 74 20 68 ethod does not h
ba80: 61 76 65 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 61 ave to.** alloca
ba90: 74 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65 te the structure
baa0: 3b 20 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 74 ; it should just
bab0: 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a fill it in..**.
bac0: 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 ** The flags arg
bad0: 75 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73 ument to xAccess
bae0: 28 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 () may be [SQLIT
baf0: 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d E_ACCESS_EXISTS]
bb00: 0a 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 .** to test for
bb10: 74 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 the existence of
bb20: 20 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c a file, or [SQL
bb30: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 ITE_ACCESS_READW
bb40: 52 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 RITE] to.** test
bb50: 20 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 whether a file
bb60: 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 is readable and
bb70: 77 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 writable, or [SQ
bb80: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
bb90: 5d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65 ].** to test whe
bba0: 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 61 ther a file is a
bbb0: 74 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65 t least readable
bbc0: 2e 20 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e . The file can
bbd0: 20 62 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f be a.** directo
bbe0: 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 ry..**.** SQLite
bbf0: 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c will always all
bc00: 6f 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d ocate at least m
bc10: 78 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 xPathname+1 byte
bc20: 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 s for the.** out
bc30: 70 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c put buffer xFull
bc40: 50 61 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 Pathname. The e
bc50: 78 61 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65 xact size of the
bc60: 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a output buffer.*
bc70: 2a 20 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64 * is also passed
bc80: 20 61 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 as a parameter
bc90: 74 6f 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 to both methods
bca0: 2e 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 . If the output
bcb0: 62 75 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 buffer.** is not
bcc0: 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b large enough, [
bcd0: 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d SQLITE_CANTOPEN]
bce0: 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 should be retur
bcf0: 6e 65 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 ned. Since this
bd00: 69 73 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 is.** handled as
bd10: 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 a fatal error b
bd20: 79 20 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d y SQLite, vfs im
bd30: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 plementations sh
bd40: 6f 75 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a ould endeavor.**
bd50: 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 to prevent this
bd60: 20 62 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 by setting mxPa
bd70: 74 68 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 thname to a suff
bd80: 69 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 iciently large v
bd90: 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 alue..**.** The
bda0: 78 52 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 xRandomness(), x
bdb0: 53 6c 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 Sleep(), and xCu
bdc0: 72 72 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 rrentTime() inte
bdd0: 72 66 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f rfaces.** are no
bde0: 74 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 t strictly a par
bdf0: 74 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 t of the filesys
be00: 74 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 tem, but they ar
be10: 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e e.** included in
be20: 20 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 the VFS structu
be30: 72 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e re for completen
be40: 65 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e ess..** The xRan
be50: 64 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 domness() functi
be60: 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 on attempts to r
be70: 65 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 eturn nBytes byt
be80: 65 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 es.** of good-qu
be90: 61 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 ality randomness
bea0: 20 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 into zOut. The
beb0: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
bec0: 0a 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e .** the actual n
bed0: 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
bee0: 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 f randomness obt
bef0: 61 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 ained..** The xS
bf00: 6c 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 leep() method ca
bf10: 75 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 uses the calling
bf20: 20 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 thread to sleep
bf30: 20 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 for at.** least
bf40: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d the number of m
bf50: 69 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 icroseconds give
bf60: 6e 2e 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 n. The xCurrent
bf70: 54 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 Time().** method
bf80: 20 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 returns a Julia
bf90: 6e 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 n Day Number for
bfa0: 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 the current dat
bfb0: 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a e and time..**.*
bfc0: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
bfd0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c sqlite3_vfs sql
bfe0: 69 74 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 ite3_vfs;.struct
bff0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 sqlite3_vfs {.
c000: 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 int iVersion;
c010: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 /* Str
c020: 75 63 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e ucture version n
c030: 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 umber */. int s
c040: 7a 4f 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20 zOsFile;
c050: 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 /* Size of s
c060: 75 62 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65 ubclassed sqlite
c070: 33 5f 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 3_file */. int
c080: 6d 78 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 mxPathname;
c090: 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
c0a0: 66 69 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 file pathname le
c0b0: 6e 67 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ngth */. sqlite
c0c0: 33 5f 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 3_vfs *pNext;
c0d0: 20 20 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 /* Next regis
c0e0: 74 65 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 tered VFS */. c
c0f0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
c100: 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 ; /* Name
c110: 6f 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 of this virtual
c120: 66 69 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 file system */.
c130: 20 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b void *pAppData;
c140: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
c150: 6e 74 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74 nter to applicat
c160: 69 6f 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74 ion-specific dat
c170: 61 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 a */. int (*xOp
c180: 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a en)(sqlite3_vfs*
c190: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e , const char *zN
c1a0: 61 6d 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c ame, sqlite3_fil
c1b0: 65 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 e*,.
c1c0: 20 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e int flags, in
c1d0: 74 20 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 t *pOutFlags);.
c1e0: 20 69 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28 int (*xDelete)(
c1f0: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f sqlite3_vfs*, co
c200: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
c210: 20 69 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 int syncDir);.
c220: 20 69 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28 int (*xAccess)(
c230: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f sqlite3_vfs*, co
c240: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
c250: 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 int flags, int
c260: 2a 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 *pResOut);. int
c270: 20 28 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 (*xFullPathname
c280: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 )(sqlite3_vfs*,
c290: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
c2a0: 65 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 e, int nOut, cha
c2b0: 72 20 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 r *zOut);. void
c2c0: 20 2a 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c *(*xDlOpen)(sql
c2d0: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 ite3_vfs*, const
c2e0: 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
c2f0: 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 );. void (*xDlE
c300: 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 rror)(sqlite3_vf
c310: 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 s*, int nByte, c
c320: 68 61 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 har *zErrMsg);.
c330: 20 76 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d void (*(*xDlSym
c340: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 )(sqlite3_vfs*,v
c350: 6f 69 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 oid*, const char
c360: 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 *zSymbol))(void
c370: 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 );. void (*xDlC
c380: 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 lose)(sqlite3_vf
c390: 73 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e s*, void*);. in
c3a0: 74 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 t (*xRandomness)
c3b0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 (sqlite3_vfs*, i
c3c0: 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a nt nByte, char *
c3d0: 7a 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 zOut);. int (*x
c3e0: 53 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 Sleep)(sqlite3_v
c3f0: 66 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 fs*, int microse
c400: 63 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a conds);. int (*
c410: 78 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 xCurrentTime)(sq
c420: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 lite3_vfs*, doub
c430: 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 le*);. int (*xG
c440: 65 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c etLastError)(sql
c450: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 ite3_vfs*, int,
c460: 63 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 char *);. /* Ne
c470: 77 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 w fields may be
c480: 61 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 appended in figu
c490: 72 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 re versions. Th
c4a0: 65 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 e iVersion. **
c4b0: 76 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 value will incre
c4c0: 6d 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 ment whenever th
c4d0: 69 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d is happens. */.}
c4e0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
c4f0: 46 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 F: Flags for the
c500: 20 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 xAccess VFS met
c510: 68 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 hod {H11190} <H1
c520: 31 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 1140>.**.** Thes
c530: 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 e integer consta
c540: 6e 74 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 nts can be used
c550: 61 73 20 74 68 65 20 74 68 69 72 64 20 70 61 72 as the third par
c560: 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 ameter to.** the
c570: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 xAccess method
c580: 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 of an [sqlite3_v
c590: 66 73 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 fs] object. {END
c5a0: 7d 20 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e } They determin
c5b0: 65 0a 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f e.** what kind o
c5c0: 66 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 f permissions th
c5d0: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 e xAccess method
c5e0: 20 69 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e is looking for.
c5f0: 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f .** With SQLITE_
c600: 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 ACCESS_EXISTS, t
c610: 68 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f he xAccess metho
c620: 64 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 d.** simply chec
c630: 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 ks whether the f
c640: 69 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 ile exists..** W
c650: 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ith SQLITE_ACCES
c660: 53 5f 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 S_READWRITE, the
c670: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a xAccess method.
c680: 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 ** checks whethe
c690: 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f r the file is bo
c6a0: 74 68 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 th readable and
c6b0: 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 writable..** Wit
c6c0: 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f h SQLITE_ACCESS_
c6d0: 52 45 41 44 2c 20 74 68 65 20 78 41 63 63 65 73 READ, the xAcces
c6e0: 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 s method.** chec
c6f0: 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 ks whether the f
c700: 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e ile is readable.
c710: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
c720: 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
c730: 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 0.#define SQ
c740: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
c750: 57 52 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 WRITE 1.#define
c760: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 SQLITE_ACCESS_RE
c770: 41 44 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a AD 2../*.**
c780: 20 43 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 CAPI3REF: Initi
c790: 61 6c 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 alize The SQLite
c7a0: 20 4c 69 62 72 61 72 79 20 7b 48 31 30 31 33 30 Library {H10130
c7b0: 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30 } <S20000><S3010
c7c0: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 0>.**.** The sql
c7d0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
c7e0: 29 20 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 ) routine initia
c7f0: 6c 69 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c lizes the.** SQL
c800: 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 ite library. Th
c810: 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f e sqlite3_shutdo
c820: 77 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 wn() routine.**
c830: 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 deallocates any
c840: 72 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 resources that w
c850: 65 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 ere allocated by
c860: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
c870: 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 ize()..**.** A c
c880: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 all to sqlite3_i
c890: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 61 nitialize() is a
c8a0: 6e 20 22 65 66 66 65 63 74 69 76 65 22 20 63 61 n "effective" ca
c8b0: 6c 6c 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 74 ll if it is.** t
c8c0: 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 73 71 he first time sq
c8d0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
c8e0: 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 64 75 () is invoked du
c8f0: 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d ring the lifetim
c900: 65 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 63 e of.** the proc
c910: 65 73 73 2c 20 6f 72 20 69 66 20 69 74 20 69 73 ess, or if it is
c920: 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 the first time
c930: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
c940: 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a ze() is invoked.
c950: 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 ** following a c
c960: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 all to sqlite3_s
c970: 68 75 74 64 6f 77 6e 28 29 2e 20 20 4f 6e 6c 79 hutdown(). Only
c980: 20 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 an effective ca
c990: 6c 6c 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 ll.** of sqlite3
c9a0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 6f _initialize() do
c9b0: 65 73 20 61 6e 79 20 69 6e 69 74 69 61 6c 69 7a es any initializ
c9c0: 61 74 69 6f 6e 2e 20 20 41 6c 6c 20 6f 74 68 65 ation. All othe
c9d0: 72 20 63 61 6c 6c 73 0a 2a 2a 20 61 72 65 20 68 r calls.** are h
c9e0: 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 0a armless no-ops..
c9f0: 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68 65 **.** Among othe
ca00: 72 20 74 68 69 6e 67 73 2c 20 73 71 6c 69 74 65 r things, sqlite
ca10: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 3_initialize() s
ca20: 68 61 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 hall invoke.** s
ca30: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
ca40: 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 73 71 . Similarly, sq
ca50: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
ca60: 0a 2a 2a 20 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 .** shall invoke
ca70: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
ca80: 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c )..**.** The sql
ca90: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
caa0: 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e ) routine return
cab0: 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e s [SQLITE_OK] on
cac0: 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 49 66 20 success..** If
cad0: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2c for some reason,
cae0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
caf0: 69 7a 65 28 29 20 69 73 20 75 6e 61 62 6c 65 20 ize() is unable
cb00: 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a to initialize.**
cb10: 20 74 68 65 20 6c 69 62 72 61 72 79 20 28 70 65 the library (pe
cb20: 72 68 61 70 73 20 69 74 20 69 73 20 75 6e 61 62 rhaps it is unab
cb30: 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 le to allocate a
cb40: 20 6e 65 65 64 65 64 20 72 65 73 6f 75 72 63 65 needed resource
cb50: 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 20 6d 75 such.** as a mu
cb60: 74 65 78 29 20 69 74 20 72 65 74 75 72 6e 73 20 tex) it returns
cb70: 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 an [error code]
cb80: 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 other than [SQLI
cb90: 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 TE_OK]..**.** Th
cba0: 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 e sqlite3_initia
cbb0: 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 69 lize() routine i
cbc0: 73 20 63 61 6c 6c 65 64 20 69 6e 74 65 72 6e 61 s called interna
cbd0: 6c 6c 79 20 62 79 20 6d 61 6e 79 20 6f 74 68 65 lly by many othe
cbe0: 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 r.** SQLite inte
cbf0: 72 66 61 63 65 73 20 73 6f 20 74 68 61 74 20 61 rfaces so that a
cc00: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 75 73 n application us
cc10: 75 61 6c 6c 79 20 64 6f 65 73 20 6e 6f 74 20 6e ually does not n
cc20: 65 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 eed to.** invoke
cc30: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
cc40: 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 2e 20 ize() directly.
cc50: 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 5b 73 For example, [s
cc60: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a qlite3_open()].*
cc70: 2a 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 5f * calls sqlite3_
cc80: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 6f 20 initialize() so
cc90: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
cca0: 72 79 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d ry will be autom
ccb0: 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 69 6e 69 74 atically.** init
ccc0: 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 5b 73 71 ialized when [sq
ccd0: 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 69 73 lite3_open()] is
cce0: 20 63 61 6c 6c 65 64 20 69 66 20 69 74 20 68 61 called if it ha
ccf0: 73 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c s not be initial
cd00: 69 7a 65 64 0a 2a 2a 20 61 6c 72 65 61 64 79 2e ized.** already.
cd10: 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51 However, if SQ
cd20: 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
cd30: 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c 49 54 with the [SQLIT
cd40: 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d E_OMIT_AUTOINIT]
cd50: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 .** compile-time
cd60: 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 option, then th
cd70: 65 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c e automatic call
cd80: 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 s to sqlite3_ini
cd90: 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 61 72 65 tialize().** are
cda0: 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 74 68 65 omitted and the
cdb0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 application mus
cdc0: 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 69 t call sqlite3_i
cdd0: 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72 65 nitialize() dire
cde0: 63 74 6c 79 0a 2a 2a 20 70 72 69 6f 72 20 74 6f ctly.** prior to
cdf0: 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 using any other
ce00: 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 SQLite interfac
ce10: 65 2e 20 20 46 6f 72 20 6d 61 78 69 6d 75 6d 20 e. For maximum
ce20: 70 6f 72 74 61 62 69 6c 69 74 79 2c 0a 2a 2a 20 portability,.**
ce30: 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 it is recommende
ce40: 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69 d that applicati
ce50: 6f 6e 73 20 61 6c 77 61 79 73 20 69 6e 76 6f 6b ons always invok
ce60: 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 e sqlite3_initia
ce70: 6c 69 7a 65 28 29 0a 2a 2a 20 64 69 72 65 63 74 lize().** direct
ce80: 6c 79 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e ly prior to usin
ce90: 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 g any other SQLi
cea0: 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46 te interface. F
ceb0: 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 0a 2a uture releases.*
cec0: 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 * of SQLite may
ced0: 72 65 71 75 69 72 65 20 74 68 69 73 2e 20 20 49 require this. I
cee0: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 n other words, t
cef0: 68 65 20 62 65 68 61 76 69 6f 72 20 65 78 68 69 he behavior exhi
cf00: 62 69 74 65 64 0a 2a 2a 20 77 68 65 6e 20 53 51 bited.** when SQ
cf10: 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
cf20: 20 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 4f 4d with [SQLITE_OM
cf30: 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20 6d 69 67 IT_AUTOINIT] mig
cf40: 68 74 20 62 65 63 6f 6d 65 20 74 68 65 0a 2a 2a ht become the.**
cf50: 20 64 65 66 61 75 6c 74 20 62 65 68 61 76 69 6f default behavio
cf60: 72 20 69 6e 20 73 6f 6d 65 20 66 75 74 75 72 65 r in some future
cf70: 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 release of SQLi
cf80: 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 te..**.** The sq
cf90: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 lite3_os_init()
cfa0: 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6f 70 65 routine does ope
cfb0: 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 rating-system sp
cfc0: 65 63 69 66 69 63 0a 2a 2a 20 69 6e 69 74 69 61 ecific.** initia
cfd0: 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 lization of the
cfe0: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 SQLite library.
cff0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f The sqlite3_os_
d000: 65 6e 64 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 end().** routine
d010: 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66 65 undoes the effe
d020: 63 74 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 ct of sqlite3_os
d030: 5f 69 6e 69 74 28 29 2e 20 20 54 79 70 69 63 61 _init(). Typica
d040: 6c 20 74 61 73 6b 73 0a 2a 2a 20 70 65 72 66 6f l tasks.** perfo
d050: 72 6d 65 64 20 62 79 20 74 68 65 73 65 20 72 6f rmed by these ro
d060: 75 74 69 6e 65 73 20 69 6e 63 6c 75 64 65 20 61 utines include a
d070: 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 64 65 61 llocation or dea
d080: 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 llocation.** of
d090: 73 74 61 74 69 63 20 72 65 73 6f 75 72 63 65 73 static resources
d0a0: 2c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e , initialization
d0b0: 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 of global varia
d0c0: 62 6c 65 73 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 bles,.** setting
d0d0: 20 75 70 20 61 20 64 65 66 61 75 6c 74 20 5b 73 up a default [s
d0e0: 71 6c 69 74 65 33 5f 76 66 73 5d 20 6d 6f 64 75 qlite3_vfs] modu
d0f0: 6c 65 2c 20 6f 72 20 73 65 74 74 69 6e 67 20 75 le, or setting u
d100: 70 0a 2a 2a 20 61 20 64 65 66 61 75 6c 74 20 63 p.** a default c
d110: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 75 73 69 onfiguration usi
d120: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ng [sqlite3_conf
d130: 69 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ig()]..**.** The
d140: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f application sho
d150: 75 6c 64 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 uld never invoke
d160: 20 65 69 74 68 65 72 20 73 71 6c 69 74 65 33 5f either sqlite3_
d170: 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 6f 72 20 os_init().** or
d180: 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 sqlite3_os_end()
d190: 20 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 20 directly. The
d1a0: 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 application shou
d1b0: 6c 64 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 0a 2a ld only invoke.*
d1c0: 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 * sqlite3_initia
d1d0: 6c 69 7a 65 28 29 20 61 6e 64 20 73 71 6c 69 74 lize() and sqlit
d1e0: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 e3_shutdown().
d1f0: 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 The sqlite3_os_i
d200: 6e 69 74 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 nit().** interfa
d210: 63 65 20 69 73 20 63 61 6c 6c 65 64 20 61 75 74 ce is called aut
d220: 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 73 71 omatically by sq
d230: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
d240: 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 () and.** sqlite
d250: 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73 20 63 61 3_os_end() is ca
d260: 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f lled by sqlite3_
d270: 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 41 70 70 shutdown(). App
d280: 72 6f 70 72 69 61 74 65 0a 2a 2a 20 69 6d 70 6c ropriate.** impl
d290: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 ementations for
d2a0: 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
d2b0: 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 ) and sqlite3_os
d2c0: 5f 65 6e 64 28 29 0a 2a 2a 20 61 72 65 20 62 75 _end().** are bu
d2d0: 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 ilt into SQLite
d2e0: 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70 69 when it is compi
d2f0: 6c 65 64 20 66 6f 72 20 75 6e 69 78 2c 20 77 69 led for unix, wi
d300: 6e 64 6f 77 73 2c 20 6f 72 20 6f 73 2f 32 2e 0a ndows, or os/2..
d310: 2a 2a 20 57 68 65 6e 20 62 75 69 6c 74 20 66 6f ** When built fo
d320: 72 20 6f 74 68 65 72 20 70 6c 61 74 66 6f 72 6d r other platform
d330: 73 20 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51 s (using the [SQ
d340: 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d LITE_OS_OTHER=1]
d350: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a compile-time.**
d360: 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70 option) the app
d370: 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 lication must su
d380: 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20 pply a suitable
d390: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 implementation f
d3a0: 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 or.** sqlite3_os
d3b0: 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 _init() and sqli
d3c0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41 te3_os_end(). A
d3d0: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 n application-su
d3e0: 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d pplied.** implem
d3f0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
d400: 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72 te3_os_init() or
d410: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
d420: 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e ).** must return
d430: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 [SQLITE_OK] on
d440: 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65 success and some
d450: 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f other [error co
d460: 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c de] upon.** fail
d470: 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ure..*/.SQLITE_A
d480: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
d490: 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b nitialize(void);
d4a0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
d4b0: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
d4c0: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 (void);.SQLITE_A
d4d0: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
d4e0: 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 s_init(void);.SQ
d4f0: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
d500: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 ite3_os_end(void
d510: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
d520: 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20 EF: Configuring
d530: 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 The SQLite Libra
d540: 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 30 ry {H14100} <S20
d550: 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 20 000><S30200>.**
d560: 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
d570: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
d580: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
d590: 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b e is used to mak
d5a0: 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 e global configu
d5b0: 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 ration.** change
d5c0: 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f s to SQLite in o
d5d0: 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c rder to tune SQL
d5e0: 69 74 65 20 74 6f 20 74 68 65 20 73 70 65 63 69 ite to the speci
d5f0: 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a 20 fic needs of.**
d600: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e the application.
d610: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f The default co
d620: 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73 20 72 nfiguration is r
d630: 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6d ecommended for m
d640: 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 ost.** applicati
d650: 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69 73 20 ons and so this
d660: 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c routine is usual
d670: 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 ly not necessary
d680: 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 . It is.** prov
d690: 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 ided to support
d6a0: 72 61 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e rare application
d6b0: 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c 20 6e s with unusual n
d6c0: 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eeds..**.** The
d6d0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
d6e0: 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f interface is no
d6f0: 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 t threadsafe. T
d700: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a he application.*
d710: 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20 74 68 * must insure th
d720: 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c 69 at no other SQLi
d730: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 te interfaces ar
d740: 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 68 e invoked by oth
d750: 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 68 er.** threads wh
d760: 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ile sqlite3_conf
d770: 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 2e ig() is running.
d780: 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 73 Furthermore, s
d790: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 0a qlite3_config().
d7a0: 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 69 ** may only be i
d7b0: 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 nvoked prior to
d7c0: 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61 6c 69 library initiali
d7d0: 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 zation using.**
d7e0: 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c [sqlite3_initial
d7f0: 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 ize()] or after
d800: 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 6c shutdown by [sql
d810: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d ite3_shutdown()]
d820: 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 76 ..** Note, howev
d830: 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74 65 33 er, that sqlite3
d840: 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 65 _config() can be
d850: 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74 20 called as part
d860: 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d of the.** implem
d870: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 61 entation of an a
d880: 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
d890: 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f 69 ed [sqlite3_os_i
d8a0: 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 nit()]..**.** Th
d8b0: 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
d8c0: 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 to sqlite3_conf
d8d0: 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74 65 67 ig() is an integ
d8e0: 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f er.** [SQLITE_CO
d8f0: 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 NFIG_SINGLETHREA
d900: 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f D | configuratio
d910: 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64 n option] that d
d920: 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 etermines.** wha
d930: 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51 t property of SQ
d940: 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f Lite is to be co
d950: 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62 73 65 nfigured. Subse
d960: 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a quent arguments.
d970: 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e ** vary dependin
d980: 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 45 g on the [SQLITE
d990: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
d9a0: 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 READ | configura
d9b0: 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20 tion option].**
d9c0: 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 in the first arg
d9d0: 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 ument..**.** Whe
d9e0: 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f n a configuratio
d9f0: 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2c n option is set,
da00: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
da10: 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 ) returns [SQLIT
da20: 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 E_OK]..** If the
da30: 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f option is unkno
da40: 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69 73 20 wn or SQLite is
da50: 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20 74 68 unable to set th
da60: 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e e option.** then
da70: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
da80: 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f turns a non-zero
da90: 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a [error code]..*
daa0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
dab0: 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 5b s:.** [H14103] [
dac0: 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32 30 5d H14106] [H14120]
dad0: 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34 31 32 [H14123] [H1412
dae0: 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 34 6] [H14129] [H14
daf0: 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 2a 132] [H14135].**
db00: 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34 31 34 [H14138] [H1414
db10: 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 34 1] [H14144] [H14
db20: 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 48 147] [H14150] [H
db30: 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36 5d 20 14153] [H14156]
db40: 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 34 [H14159].** [H14
db50: 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 48 162] [H14165] [H
db60: 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 14168].*/.SQLITE
db70: 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
db80: 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c RIMENTAL int sql
db90: 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 2c ite3_config(int,
dba0: 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ...);../*.** CA
dbb0: 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 PI3REF: Configur
dbc0: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
dbd0: 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 7d ctions {H14200}
dbe0: 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 <S20000>.** EXP
dbf0: 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
dc00: 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 The sqlite3_db_c
dc10: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
dc20: 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b e is used to mak
dc30: 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a e configuration.
dc40: 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 ** changes to a
dc50: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
dc60: 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65 tion]. The inte
dc70: 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72 rface is similar
dc80: 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
dc90: 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74 config()] except
dca0: 20 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 that the change
dcb0: 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e s apply to a sin
dcc0: 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 gle.** [database
dcd0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70 connection] (sp
dce0: 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 66 ecified in the f
dcf0: 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 20 irst argument).
dd00: 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f The.** sqlite3_
dd10: 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 db_config() inte
dd20: 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 62 rface can only b
dd30: 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61 74 65 e used immediate
dd40: 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 ly after.** the
dd50: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
dd60: 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 20 75 ion is created u
dd70: 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 sing [sqlite3_op
dd80: 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 en()],.** [sqlit
dd90: 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72 e3_open16()], or
dda0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
ddb0: 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 2()]. .**.** Th
ddc0: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
ddd0: 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f t to sqlite3_db_
dde0: 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 config(D,V,...)
ddf0: 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 is the.** confi
de00: 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20 2d 20 guration verb -
de10: 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 an integer code
de20: 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 that indicates w
de30: 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f 66 hat.** aspect of
de40: 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
de50: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 onnection] is be
de60: 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a ing configured..
de70: 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f 69 ** The only choi
de80: 63 65 20 66 6f 72 20 74 68 69 73 20 76 61 6c 75 ce for this valu
de90: 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 43 e is [SQLITE_DBC
dea0: 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d ONFIG_LOOKASIDE]
deb0: 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 61 ..** New verbs a
dec0: 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 re likely to be
ded0: 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 added in future
dee0: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 releases of SQLi
def0: 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 te..** Additiona
df00: 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65 70 65 l arguments depe
df10: 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62 2e 0a nd on the verb..
df20: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
df30: 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d 20 ts:.** [H14203]
df40: 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32 30 39 [H14206] [H14209
df50: 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 32 ] [H14212] [H142
df60: 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 15].*/.SQLITE_AP
df70: 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
df80: 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
df90: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 3_db_config(sqli
dfa0: 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e te3*, int op, ..
dfb0: 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
dfc0: 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f REF: Memory Allo
dfd0: 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 cation Routines
dfe0: 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30 {H10155} <S20120
dff0: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
e000: 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 L.**.** An insta
e010: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
e020: 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 ct defines the i
e030: 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e nterface between
e040: 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c SQLite.** and l
e050: 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
e060: 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 allocation routi
e070: 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 nes..**.** This
e080: 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 object is used i
e090: 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 n only one place
e0a0: 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 in the SQLite i
e0b0: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 nterface..** A p
e0c0: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
e0d0: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
e0e0: 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 67 75 ject is the argu
e0f0: 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 ment to.** [sqli
e100: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 te3_config()] wh
e110: 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 en the configura
e120: 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a tion option is.*
e130: 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
e140: 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 42 79 20 63 72 _MALLOC]. By cr
e150: 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e eating an instan
e160: 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
e170: 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 t.** and passing
e180: 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f it to [sqlite3_
e190: 63 6f 6e 66 69 67 28 29 5d 20 64 75 72 69 6e 67 config()] during
e1a0: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 configuration,
e1b0: 61 6e 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f an.** applicatio
e1c0: 6e 20 63 61 6e 20 73 70 65 63 69 66 79 20 61 6e n can specify an
e1d0: 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 6d alternative mem
e1e0: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
e1f0: 75 62 73 79 73 74 65 6d 0a 2a 2a 20 66 6f 72 20 ubsystem.** for
e200: 53 51 4c 69 74 65 20 74 6f 20 75 73 65 20 66 6f SQLite to use fo
e210: 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e r all of its dyn
e220: 61 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65 64 amic memory need
e230: 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 s..**.** Note th
e240: 61 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73 20 at SQLite comes
e250: 77 69 74 68 20 61 20 62 75 69 6c 74 2d 69 6e 20 with a built-in
e260: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
e270: 20 74 68 61 74 20 69 73 0a 2a 2a 20 70 65 72 66 that is.** perf
e280: 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 20 66 ectly adequate f
e290: 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c 6d or the overwhelm
e2a0: 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 ing majority of
e2b0: 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 applications.**
e2c0: 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f 62 and that this ob
e2d0: 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 ject is only use
e2e0: 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d 69 ful to a tiny mi
e2f0: 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 63 nority of applic
e300: 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 73 ations.** with s
e310: 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f 72 pecialized memor
e320: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
e330: 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 uirements. This
e340: 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61 6c object is.** al
e350: 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 so used during t
e360: 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74 65 esting of SQLite
e370: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65 in order to spe
e380: 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 cify an alternat
e390: 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c ive.** memory al
e3a0: 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69 6d locator that sim
e3b0: 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75 ulates memory ou
e3c0: 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 t-of-memory cond
e3d0: 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 64 itions in.** ord
e3e0: 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 er to verify tha
e3f0: 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 72 t SQLite recover
e400: 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 6f s gracefully fro
e410: 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 74 m such.** condit
e420: 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ions..**.** The
e430: 78 4d 61 6c 6c 6f 63 2c 20 78 46 72 65 65 2c 20 xMalloc, xFree,
e440: 61 6e 64 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 and xRealloc met
e450: 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20 6c hods must work l
e460: 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c 6f ike the.** mallo
e470: 63 28 29 2c 20 66 72 65 65 28 29 2c 20 61 6e 64 c(), free(), and
e480: 20 72 65 61 6c 6c 6f 63 28 29 20 66 75 6e 63 74 realloc() funct
e490: 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 74 ions from the st
e4a0: 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 2e 0a andard library..
e4b0: 2a 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75 **.** xSize shou
e4c0: 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 61 6c ld return the al
e4d0: 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 located size of
e4e0: 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
e4f0: 69 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c ion.** previousl
e500: 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 y obtained from
e510: 78 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c xMalloc or xReal
e520: 6c 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 loc. The alloca
e530: 74 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20 61 ted size.** is a
e540: 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 61 lways at least a
e550: 73 20 62 69 67 20 61 73 20 74 68 65 20 72 65 71 s big as the req
e560: 75 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 20 uested size but
e570: 6d 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a 2a may be larger..*
e580: 2a 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64 75 *.** The xRoundu
e590: 70 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 p method returns
e5a0: 20 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20 74 what would be t
e5b0: 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a he allocated siz
e5c0: 65 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 79 e of.** a memory
e5d0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76 65 allocation give
e5e0: 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 n a particular r
e5f0: 65 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20 20 equested size.
e600: 4d 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 Most memory.** a
e610: 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 llocators round
e620: 75 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 up memory alloca
e630: 74 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20 74 tions at least t
e640: 6f 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74 69 o the next multi
e650: 70 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 6f ple.** of 8. So
e660: 6d 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f me allocators ro
e670: 75 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72 67 und up to a larg
e680: 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20 74 er multiple or t
e690: 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a o a power of 2..
e6a0: 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 20 **.** The xInit
e6b0: 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69 7a method initializ
e6c0: 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c es the memory al
e6d0: 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20 65 locator. (For e
e6e0: 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d 69 xample,.** it mi
e6f0: 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e 79 ght allocate any
e700: 20 72 65 71 75 69 72 65 20 6d 75 74 65 78 65 73 require mutexes
e710: 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 or initialize i
e720: 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 nternal data.**
e730: 73 74 72 75 63 74 75 72 65 73 2e 20 20 54 68 65 structures. The
e740: 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f xShutdown metho
e750: 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 6e d is invoked (in
e760: 64 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a 20 directly) by.**
e770: 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 [sqlite3_shutdow
e780: 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64 20 n()] and should
e790: 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 deallocate any r
e7a0: 65 73 6f 75 72 63 65 73 20 61 63 71 75 69 72 65 esources acquire
e7b0: 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20 20 d.** by xInit.
e7c0: 54 68 65 20 70 41 70 70 44 61 74 61 20 70 6f 69 The pAppData poi
e7d0: 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20 nter is used as
e7e0: 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65 74 the only paramet
e7f0: 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 61 er to.** xInit a
e800: 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a 2f nd xShutdown..*/
e810: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
e820: 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
e830: 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f ods sqlite3_mem_
e840: 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 methods;.struct
e850: 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
e860: 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a ods {. void *(*
e870: 78 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20 xMalloc)(int);
e880: 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
e890: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 allocation func
e8a0: 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 tion */. void (
e8b0: 2a 78 46 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 *xFree)(void*);
e8c0: 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 /* Free
e8d0: 20 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 a prior allocat
e8e0: 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 ion */. void *(
e8f0: 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a *xRealloc)(void*
e900: 2c 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a ,int); /* Resiz
e910: 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e an allocation
e920: 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 7a 65 */. int (*xSize
e930: 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 )(void*);
e940: 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 /* Return th
e950: 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c e size of an all
e960: 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 ocation */. int
e970: 20 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 6e 74 (*xRoundup)(int
e980: 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 ); /* R
e990: 6f 75 6e 64 20 75 70 20 72 65 71 75 65 73 74 20 ound up request
e9a0: 73 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69 size to allocati
e9b0: 6f 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 on size */. int
e9c0: 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 (*xInit)(void*)
e9d0: 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 ; /* I
e9e0: 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
e9f0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a mory allocator *
ea00: 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75 74 /. void (*xShut
ea10: 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20 down)(void*);
ea20: 20 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69 /* Deinitiali
ea30: 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
ea40: 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 locator */. voi
ea50: 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20 d *pAppData;
ea60: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
ea70: 72 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74 rgument to xInit
ea80: 28 29 20 61 6e 64 20 78 53 68 75 74 64 6f 77 6e () and xShutdown
ea90: 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 () */.};../*.**
eaa0: 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 CAPI3REF: Config
eab0: 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 uration Options
eac0: 7b 48 31 30 31 36 30 7d 20 3c 53 32 30 30 30 30 {H10160} <S20000
ead0: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
eae0: 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f L.**.** These co
eaf0: 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 nstants are the
eb00: 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 available intege
eb10: 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 r configuration
eb20: 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 options that.**
eb30: 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 73 can be passed as
eb40: 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
eb50: 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 ent to the [sqli
eb60: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e te3_config()] in
eb70: 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e terface..**.** N
eb80: 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ew configuration
eb90: 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 options may be
eba0: 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 added in future
ebb0: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 releases of SQLi
ebc0: 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 te..** Existing
ebd0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
ebe0: 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 tions might be d
ebf0: 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 iscontinued. Ap
ec00: 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 plications.** sh
ec10: 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 ould check the r
ec20: 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
ec30: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
ec40: 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 )] to make sure
ec50: 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c that.** the call
ec60: 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 worked. The [s
ec70: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
ec80: 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 interface will
ec90: 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d return a.** non-
eca0: 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 zero [error code
ecb0: 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e ] if a discontin
ecc0: 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 ued or unsupport
ecd0: 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ed configuration
ece0: 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e option.** is in
ecf0: 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c voked..**.** <dl
ed00: 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f >.** <dt>SQLITE_
ed10: 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 CONFIG_SINGLETHR
ed20: 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e EAD</dt>.** <dd>
ed30: 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 There are no arg
ed40: 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f uments to this o
ed50: 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 ption. This opt
ed60: 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 ion disables.**
ed70: 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e 64 all mutexing and
ed80: 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e 74 puts SQLite int
ed90: 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20 69 o a mode where i
eda0: 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 t can only be us
edb0: 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c ed.** by a singl
edc0: 65 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a e thread.</dd>.*
edd0: 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
ede0: 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 CONFIG_MULTITHRE
edf0: 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 AD</dt>.** <dd>T
ee00: 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 here are no argu
ee10: 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 ments to this op
ee20: 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 tion. This opti
ee30: 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d on disables.** m
ee40: 75 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61 utexing on [data
ee50: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
ee60: 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 and [prepared s
ee70: 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 tatement] object
ee80: 73 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 s..** The applic
ee90: 61 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 ation is respons
eea0: 69 62 6c 65 20 66 6f 72 20 73 65 72 69 61 6c 69 ible for seriali
eeb0: 7a 69 6e 67 20 61 63 63 65 73 73 20 74 6f 0a 2a zing access to.*
eec0: 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
eed0: 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 ections] and [pr
eee0: 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
eef0: 73 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d s]. But other m
ef00: 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e utexes.** are en
ef10: 61 62 6c 65 64 20 73 6f 20 74 68 61 74 20 53 51 abled so that SQ
ef20: 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 73 61 66 Lite will be saf
ef30: 65 20 74 6f 20 75 73 65 20 69 6e 20 61 20 6d 75 e to use in a mu
ef40: 6c 74 69 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 lti-threaded.**
ef50: 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c environment as l
ef60: 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 ong as no two th
ef70: 72 65 61 64 73 20 61 74 74 65 6d 70 74 20 74 6f reads attempt to
ef80: 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a use the same.**
ef90: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
efa0: 63 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 ction] at the sa
efb0: 6d 65 20 74 69 6d 65 2e 20 20 53 65 65 20 74 68 me time. See th
efc0: 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 e [threading mod
efd0: 65 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 e].** documentat
efe0: 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ion for addition
eff0: 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c al information.<
f000: 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
f010: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 QLITE_CONFIG_SER
f020: 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20 IALIZED</dt>.**
f030: 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f <dd>There are no
f040: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 arguments to th
f050: 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 is option. This
f060: 20 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a option enables.
f070: 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 20 69 ** all mutexes i
f080: 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 72 65 63 ncluding the rec
f090: 75 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65 ursive.** mutexe
f0a0: 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 s on [database c
f0b0: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b onnection] and [
f0c0: 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
f0d0: 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 nt] objects..**
f0e0: 49 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 77 68 In this mode (wh
f0f0: 69 63 68 20 69 73 20 74 68 65 20 64 65 66 61 75 ich is the defau
f100: 6c 74 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 lt when SQLite i
f110: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a s compiled with.
f120: 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 ** [SQLITE_THREA
f130: 44 53 41 46 45 3d 31 5d 29 20 74 68 65 20 53 51 DSAFE=1]) the SQ
f140: 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c Lite library wil
f150: 6c 20 69 74 73 65 6c 66 20 73 65 72 69 61 6c 69 l itself seriali
f160: 7a 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 ze access.** to
f170: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
f180: 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 tions] and [prep
f190: 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d ared statements]
f1a0: 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 so that the.**
f1b0: 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 66 application is f
f1c0: 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 20 73 ree to use the s
f1d0: 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f ame [database co
f1e0: 6e 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65 nnection] or the
f1f0: 0a 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 61 72 .** same [prepar
f200: 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e ed statement] in
f210: 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 different threa
f220: 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ds at the same t
f230: 69 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 ime..** See the
f240: 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d [threading mode]
f250: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 documentation f
f260: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
f270: 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a formation.</dd>.
f280: 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
f290: 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f _CONFIG_MALLOC</
f2a0: 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
f2b0: 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 option takes a s
f2c0: 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 ingle argument w
f2d0: 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 hich is a pointe
f2e0: 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 r to an.** insta
f2f0: 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 nce of the [sqli
f300: 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d te3_mem_methods]
f310: 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
f320: 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 argument specif
f330: 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 ies.** alternati
f340: 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d ve low-level mem
f350: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
f360: 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 outines to be us
f370: 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a ed in place of.*
f380: 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c * the memory all
f390: 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 ocation routines
f3a0: 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 built into SQLi
f3b0: 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c te.</dd>.**.** <
f3c0: 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
f3d0: 5f 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a _GETMALLOC</dt>.
f3e0: 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 ** <dd>This opti
f3f0: 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c on takes a singl
f400: 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 e argument which
f410: 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
f420: 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 an.** instance
f430: 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
f440: 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 mem_methods] str
f450: 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73 71 ucture. The [sq
f460: 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
f470: 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 s].** structure
f480: 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 is filled with t
f490: 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 he currently def
f4a0: 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ined memory allo
f4b0: 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e cation routines.
f4c0: 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 .** This option
f4d0: 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f can be used to o
f4e0: 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 verload the defa
f4f0: 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ult memory alloc
f500: 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 ation.** routine
f510: 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65 72 s with a wrapper
f520: 20 74 68 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e that simulation
f530: 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 s memory allocat
f540: 69 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a ion failure or.*
f550: 2a 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 * tracks memory
f560: 75 73 61 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 usage, for examp
f570: 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c le.</dd>.**.** <
f580: 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
f590: 5f 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a _MEMSTATUS</dt>.
f5a0: 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 ** <dd>This opti
f5b0: 6f 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 on takes single
f5c0: 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 argument of type
f5d0: 20 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 74 65 int, interprete
f5e0: 64 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 d as a .** boole
f5f0: 61 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 6c 65 an, which enable
f600: 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 s or disables th
f610: 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 e collection of
f620: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
f630: 6e 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 63 73 n .** statistics
f640: 2e 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64 2c . When disabled,
f650: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 the following S
f660: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 QLite interfaces
f670: 20 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d become .** non-
f680: 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 operational:.**
f690: 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e <ul>.** <li>
f6a0: 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 [sqlite3_memory
f6b0: 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c _used()].** <l
f6c0: 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f i> [sqlite3_memo
f6d0: 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d 0a ry_highwater()].
f6e0: 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 ** <li> [sqlit
f6f0: 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d e3_soft_heap_lim
f700: 69 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 it()].** <li>
f710: 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 [sqlite3_status(
f720: 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a )].** </ul>.**
f730: 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 </dd>.**.** <dt
f740: 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 >SQLITE_CONFIG_S
f750: 43 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c CRATCH</dt>.** <
f760: 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 dd>This option s
f770: 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69 pecifies a stati
f780: 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 c memory buffer
f790: 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 that SQLite can
f7a0: 75 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 use for.** scrat
f7b0: 63 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 ch memory. Ther
f7c0: 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67 75 e are three argu
f7d0: 6d 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 ments: A pointe
f7e0: 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c r to the memory,
f7f0: 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 the.** size of
f800: 65 61 63 68 20 73 63 72 61 74 63 68 20 62 75 66 each scratch buf
f810: 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 74 68 fer (sz), and th
f820: 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 66 66 e number of buff
f830: 65 72 73 20 28 4e 29 2e 20 20 54 68 65 20 73 7a ers (N). The sz
f840: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 .** argument mus
f850: 74 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 t be a multiple
f860: 6f 66 20 31 36 2e 20 54 68 65 20 73 7a 20 70 61 of 16. The sz pa
f870: 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 rameter should b
f880: 65 20 61 20 66 65 77 20 62 79 74 65 73 0a 2a 2a e a few bytes.**
f890: 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
f8a0: 20 61 63 74 75 61 6c 20 73 63 72 61 74 63 68 20 actual scratch
f8b0: 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 64 space required d
f8c0: 75 65 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72 ue internal over
f8d0: 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 head..** The fir
f8e0: 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 73 st.** argument s
f8f0: 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 61 hould point to a
f900: 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 n allocation of
f910: 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 at least sz*N by
f920: 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a tes of memory..*
f930: 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 * SQLite will us
f940: 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f e no more than o
f950: 6e 65 20 73 63 72 61 74 63 68 20 62 75 66 66 65 ne scratch buffe
f960: 72 20 61 74 20 6f 6e 63 65 20 70 65 72 20 74 68 r at once per th
f970: 72 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68 read, so.** N sh
f980: 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 74 ould be set to t
f990: 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 78 69 he expected maxi
f9a0: 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 mum number of th
f9b0: 72 65 61 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a reads. The sz.*
f9c0: 2a 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 * parameter shou
f9d0: 6c 64 20 62 65 20 36 20 74 69 6d 65 73 20 74 68 ld be 6 times th
f9e0: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 e size of the la
f9f0: 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 rgest database p
fa00: 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72 age size..** Scr
fa10: 61 74 63 68 20 62 75 66 66 65 72 73 20 61 72 65 atch buffers are
fa20: 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 used as part of
fa30: 20 74 68 65 20 62 74 72 65 65 20 62 61 6c 61 6e the btree balan
fa40: 63 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 ce operation. I
fa50: 66 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 20 62 f.** The btree b
fa60: 61 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 61 64 alancer needs ad
fa70: 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 ditional memory
fa80: 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70 beyond what is p
fa90: 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73 63 rovided by.** sc
faa0: 72 61 74 63 68 20 62 75 66 66 65 72 73 20 6f 72 ratch buffers or
fab0: 20 69 66 20 6e 6f 20 73 63 72 61 74 63 68 20 62 if no scratch b
fac0: 75 66 66 65 72 20 73 70 61 63 65 20 69 73 20 73 uffer space is s
fad0: 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 53 pecified, then S
fae0: 51 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f QLite.** goes to
faf0: 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
fb00: 28 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 ()] to obtain th
fb10: 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 e memory it need
fb20: 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 s.</dd>.**.** <d
fb30: 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
fb40: 50 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a PAGECACHE</dt>.*
fb50: 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
fb60: 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 n specifies a st
fb70: 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 atic memory buff
fb80: 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 er that SQLite c
fb90: 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 an use for.** th
fba0: 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
fbb0: 63 61 63 68 65 20 77 69 74 68 20 74 68 65 20 64 cache with the d
fbc0: 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 efault page cach
fbd0: 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e e implemenation.
fbe0: 20 20 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69 .** This confi
fbf0: 67 75 72 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 guration should
fc00: 6e 6f 74 20 62 65 20 75 73 65 64 20 69 66 20 61 not be used if a
fc10: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 n application-de
fc20: 66 69 6e 65 20 70 61 67 65 0a 2a 2a 20 63 61 63 fine page.** cac
fc30: 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
fc40: 6e 20 69 73 20 6c 6f 61 64 65 64 20 75 73 69 6e n is loaded usin
fc50: 67 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e g the SQLITE_CON
fc60: 46 49 47 5f 50 43 41 43 48 45 20 6f 70 74 69 6f FIG_PCACHE optio
fc70: 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 n..** There are
fc80: 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 three arguments
fc90: 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20 to this option:
fca0: 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 A pointer to the
fcb0: 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 .** memory, the
fcc0: 73 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 67 size of each pag
fcd0: 65 20 62 75 66 66 65 72 20 28 73 7a 29 2c 20 61 e buffer (sz), a
fce0: 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 nd the number of
fcf0: 20 70 61 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 54 pages (N)..** T
fd00: 68 65 20 73 7a 20 61 72 67 75 6d 65 6e 74 20 6d he sz argument m
fd10: 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f ust be a power o
fd20: 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 f two between 51
fd30: 32 20 61 6e 64 20 33 32 37 36 38 2e 20 20 54 68 2 and 32768. Th
fd40: 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d e first.** argum
fd50: 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 ent should point
fd60: 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f to an allocatio
fd70: 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a n of at least sz
fd80: 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f *N bytes of memo
fd90: 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 ry..** SQLite wi
fda0: 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 ll use the memor
fdb0: 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 y provided by th
fdc0: 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
fdd0: 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 0a to satisfy its.
fde0: 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 ** memory needs
fdf0: 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 for the first N
fe00: 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61 64 pages that it ad
fe10: 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49 66 ds to cache. If
fe20: 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 additional.** p
fe30: 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 age cache memory
fe40: 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f 6e is needed beyon
fe50: 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 d what is provid
fe60: 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 6f ed by this optio
fe70: 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 n, then.** SQLit
fe80: 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 e goes to [sqlit
fe90: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 e3_malloc()] for
fea0: 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 the additional
feb0: 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a 2a storage space..*
fec0: 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 * The implementa
fed0: 74 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20 6f tion might use o
fee0: 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
fef0: 65 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20 68 e N buffers to h
ff00: 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 old .** memory a
ff10: 63 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d ccounting inform
ff20: 61 74 69 6f 6e 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a ation. </dd>.**.
ff30: 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
ff40: 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a NFIG_HEAP</dt>.*
ff50: 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
ff60: 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 n specifies a st
ff70: 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 atic memory buff
ff80: 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 er that SQLite w
ff90: 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 ill use.** for a
ffa0: 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 ll of its dynami
ffb0: 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 c memory allocat
ffc0: 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 ion needs beyond
ffd0: 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a those provided.
ffe0: 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 ** for by [SQLIT
fff0: 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
10000 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f ] and [SQLITE_CO
10010 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e NFIG_PAGECACHE].
10020 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 .** There are th
10030 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 ree arguments: A
10040 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
10050 6d 65 6d 6f 72 79 2c 20 74 68 65 20 6e 75 6d 62 memory, the numb
10060 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 er of.** bytes i
10070 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 n the memory buf
10080 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e fer, and the min
10090 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 imum allocation
100a0 73 69 7a 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 size. If.** the
100b0 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 28 first pointer (
100c0 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 the memory point
100d0 65 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 er) is NULL, the
100e0 6e 20 53 51 4c 69 74 65 20 72 65 76 65 72 74 73 n SQLite reverts
100f0 0a 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74 73 .** to using its
10100 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 default memory
10110 61 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20 73 allocator (the s
10120 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 ystem malloc() i
10130 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a mplementation),.
10140 2a 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 ** undoing any p
10150 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 rior invocation
10160 6f 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 of [SQLITE_CONFI
10170 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 G_MALLOC]. If t
10180 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 he.** memory poi
10190 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c nter is not NULL
101a0 20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 4c and either [SQL
101b0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
101c0 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 S3] or.** [SQLIT
101d0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 E_ENABLE_MEMSYS5
101e0 5d 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 ] are defined, t
101f0 68 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74 hen the alternat
10200 69 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c ive memory.** al
10210 6c 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61 67 locator is engag
10220 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c ed to handle all
10230 20 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f of SQLites memo
10240 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 ry allocation ne
10250 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 eds.</dd>.**.**
10260 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
10270 47 5f 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 G_MUTEX</dt>.**
10280 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
10290 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
102a0 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
102b0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
102c0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
102d0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 the [sqlite3_mut
102e0 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 ex_methods] stru
102f0 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 67 75 cture. The argu
10300 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a ment specifies.*
10310 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f * alternative lo
10320 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 72 6f w-level mutex ro
10330 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 utines to be use
10340 64 20 69 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 68 d in place.** th
10350 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 e mutex routines
10360 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 built into SQLi
10370 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c te.</dd>.**.** <
10380 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
10390 5f 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a _GETMUTEX</dt>.*
103a0 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
103b0 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 n takes a single
103c0 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 argument which
103d0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
103e0 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
103f0 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d f the [sqlite3_m
10400 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 utex_methods] st
10410 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a ructure. The.**
10420 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
10430 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 methods].** stru
10440 63 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64 20 cture is filled
10450 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 with the current
10460 6c 79 20 64 65 66 69 6e 65 64 20 6d 75 74 65 78 ly defined mutex
10470 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 routines..** Th
10480 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 is option can be
10490 20 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 used to overloa
104a0 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 75 d the default mu
104b0 74 65 78 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a tex allocation.*
104c0 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20 * routines with
104d0 61 20 77 72 61 70 70 65 72 20 75 73 65 64 20 74 a wrapper used t
104e0 6f 20 74 72 61 63 6b 20 6d 75 74 65 78 20 75 73 o track mutex us
104f0 61 67 65 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 age for performa
10500 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 nce.** profiling
10510 20 6f 72 20 74 65 73 74 69 6e 67 2c 20 66 6f 72 or testing, for
10520 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a example.</dd>.*
10530 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
10540 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 CONFIG_LOOKASIDE
10550 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
10560 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 s option takes t
10570 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 wo arguments tha
10580 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 t determine the
10590 64 65 66 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f 72 default.** memor
105a0 79 20 61 6c 6c 63 61 74 69 6f 6e 20 6c 6f 6f 6b y allcation look
105b0 61 73 69 64 65 20 6f 70 74 69 6d 69 7a 61 74 69 aside optimizati
105c0 6f 6e 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 on. The first a
105d0 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a rgument is the.*
105e0 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c * size of each l
105f0 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 ookaside buffer
10600 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 73 65 63 slot and the sec
10610 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ond is the numbe
10620 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 6c r of.** slots al
10630 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 20 located to each
10640 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
10650 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ion.</dd>.**.**
10660 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
10670 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a G_PCACHE</dt>.**
10680 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
10690 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 takes a single
106a0 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 argument which i
106b0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a s a pointer to.*
106c0 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 * an [sqlite3_pc
106d0 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 ache_methods] ob
106e0 6a 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 ject. This obje
106f0 63 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 ct specifies the
10700 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f interface.** to
10710 20 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 a custom page c
10720 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ache implementat
10730 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b ion. SQLite mak
10740 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 es a copy of the
10750 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 .** object and u
10760 73 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 20 ses it for page
10770 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c cache memory all
10780 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a ocations.</dd>.*
10790 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
107a0 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 CONFIG_GETPCACHE
107b0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
107c0 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 s option takes a
107d0 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 single argument
107e0 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e which is a poin
107f0 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 ter to an.** [sq
10800 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
10810 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 hods] object. S
10820 51 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 20 QLite copies of
10830 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 the current.** p
10840 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d age cache implem
10850 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 entation into th
10860 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a at object.</dd>.
10870 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 **.** </dl>.*/.#
10880 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
10890 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 NFIG_SINGLETHREA
108a0 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a D 1 /* nil */.
108b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
108c0 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 ONFIG_MULTITHREA
108d0 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f D 2 /* nil */
108e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
108f0 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 CONFIG_SERIALIZE
10900 44 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a D 3 /* nil *
10910 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
10920 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 _CONFIG_MALLOC
10930 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 4 /* sqli
10940 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a te3_mem_methods*
10950 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
10960 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c TE_CONFIG_GETMAL
10970 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 LOC 5 /* sq
10980 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
10990 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 s* */.#define SQ
109a0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 LITE_CONFIG_SCRA
109b0 54 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 TCH 6 /*
109c0 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 void*, int sz, i
109d0 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nt N */.#define
109e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
109f0 47 45 43 41 43 48 45 20 20 20 20 20 37 20 20 2f GECACHE 7 /
10a00 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c * void*, int sz,
10a10 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e int N */.#defin
10a20 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
10a30 48 45 41 50 20 20 20 20 20 20 20 20 20 20 38 20 HEAP 8
10a40 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e /* void*, int n
10a50 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f Byte, int min */
10a60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
10a70 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 CONFIG_MEMSTATUS
10a80 20 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 9 /* boole
10a90 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 an */.#define SQ
10aa0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 LITE_CONFIG_MUTE
10ab0 58 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 X 10 /*
10ac0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
10ad0 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e thods* */.#defin
10ae0 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
10af0 47 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 GETMUTEX 11
10b00 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 /* sqlite3_mute
10b10 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a x_methods* */./*
10b20 20 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 previously SQLI
10b30 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 TE_CONFIG_CHUNKA
10b40 4c 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73 LLOC 12 which is
10b50 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 now unused. */
10b60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
10b70 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 CONFIG_LOOKASIDE
10b80 20 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 13 /* int i
10b90 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 nt */.#define SQ
10ba0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 LITE_CONFIG_PCAC
10bb0 48 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a 20 HE 14 /*
10bc0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
10bd0 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 ethods* */.#defi
10be0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
10bf0 5f 47 45 54 50 43 41 43 48 45 20 20 20 20 31 35 _GETPCACHE 15
10c00 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 /* sqlite3_pca
10c10 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a che_methods* */.
10c20 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
10c30 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f Configuration O
10c40 70 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 ptions {H10170}
10c50 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 <S20000>.** EXPE
10c60 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
10c70 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 hese constants a
10c80 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 re the available
10c90 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 integer configu
10ca0 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 ration options t
10cb0 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 hat.** can be pa
10cc0 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f ssed as the seco
10cd0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 nd argument to t
10ce0 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 he [sqlite3_db_c
10cf0 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 onfig()] interfa
10d00 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f ce..**.** New co
10d10 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
10d20 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 ons may be added
10d30 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 in future relea
10d40 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ses of SQLite..*
10d50 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 * Existing confi
10d60 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 guration options
10d70 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e might be discon
10d80 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 tinued. Applica
10d90 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 tions.** should
10da0 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e check the return
10db0 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 code from [sqli
10dc0 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d te3_db_config()]
10dd0 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
10de0 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 at.** the call w
10df0 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c orked. The [sql
10e00 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 ite3_db_config()
10e10 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c ] interface will
10e20 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e return a.** non
10e30 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 -zero [error cod
10e40 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 e] if a disconti
10e50 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 nued or unsuppor
10e60 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ted configuratio
10e70 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 n option.** is i
10e80 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 nvoked..**.** <d
10e90 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 l>.** <dt>SQLITE
10ea0 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 _DBCONFIG_LOOKAS
10eb0 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e IDE</dt>.** <dd>
10ec0 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 This option take
10ed0 73 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e s three addition
10ee0 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 al arguments tha
10ef0 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 t determine the
10f00 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d .** [lookaside m
10f10 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d emory allocator]
10f20 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 configuration f
10f30 6f 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 or the [database
10f40 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a connection]..**
10f50 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
10f60 65 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 70 ent (the third p
10f70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
10f80 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 ite3_db_config()
10f90 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 ] is a.** pointe
10fa0 72 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 62 75 r to a memory bu
10fb0 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20 ffer to use for
10fc0 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 lookaside memory
10fd0 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 . The first.**
10fe0 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 argument may be
10ff0 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61 NULL in which ca
11000 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 se SQLite will a
11010 6c 6c 6f 63 61 74 65 20 74 68 65 20 6c 6f 6f 6b llocate the look
11020 61 73 69 64 65 0a 2a 2a 20 62 75 66 66 65 72 20 aside.** buffer
11030 69 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 itself using [sq
11040 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e lite3_malloc()].
11050 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 The second arg
11060 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 ument is the.**
11070 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f size of each loo
11080 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c kaside buffer sl
11090 6f 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 ot and the third
110a0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
110b0 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c number of.** sl
110c0 6f 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 6f ots. The size o
110d0 66 20 74 68 65 20 62 75 66 66 65 72 20 69 6e 20 f the buffer in
110e0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
110f0 6e 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 nt must be great
11100 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 er than.** or eq
11110 75 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 75 ual to the produ
11120 63 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 ct of the second
11130 20 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d and third argum
11140 65 6e 74 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ents.</dd>.**.**
11150 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e </dl>.*/.#defin
11160 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 e SQLITE_DBCONFI
11170 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 G_LOOKASIDE 1
11180 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 001 /* void* in
11190 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a t int */.../*.**
111a0 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c CAPI3REF: Enabl
111b0 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 e Or Disable Ext
111c0 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 ended Result Cod
111d0 65 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 30 es {H12200} <S10
111e0 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 700>.**.** The s
111f0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f qlite3_extended_
11200 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 72 result_codes() r
11210 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f outine enables o
11220 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a 2a r disables the.*
11230 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 * [extended resu
11240 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 72 lt codes] featur
11250 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 65 e of SQLite. The
11260 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 extended result
11270 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 69 .** codes are di
11280 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c sabled by defaul
11290 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c t for historical
112a0 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 63 compatibility c
112b0 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a 2a onsiderations..*
112c0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
112d0 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20 5b s:.** [H12201] [
112e0 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49 54 H12202].*/.SQLIT
112f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
11300 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 3_extended_resul
11310 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 2a t_codes(sqlite3*
11320 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f , int onoff);../
11330 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c *.** CAPI3REF: L
11340 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 64 ast Insert Rowid
11350 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 30 {H12220} <S1070
11360 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 0>.**.** Each en
11370 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 try in an SQLite
11380 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69 table has a uni
11390 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 que 64-bit signe
113a0 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 d.** integer key
113b0 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f 57 called the [ROW
113c0 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20 54 ID | "rowid"]. T
113d0 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77 61 he rowid is alwa
113e0 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 ys available.**
113f0 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 64 as an undeclared
11400 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 4f column named RO
11410 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 4f WID, OID, or _RO
11420 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 20 WID_ as long as
11430 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20 61 those.** names a
11440 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 64 re not also used
11450 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 64 by explicitly d
11460 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 2e eclared columns.
11470 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 If.** the table
11480 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 has a column of
11490 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20 50 type [INTEGER P
114a0 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65 6e RIMARY KEY] then
114b0 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 that column.**
114c0 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61 73 is another alias
114d0 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 0a for the rowid..
114e0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
114f0 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b ne returns the [
11500 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d 6f rowid] of the mo
11510 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 63 st recent.** suc
11520 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d cessful [INSERT]
11530 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
11540 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 74 se from the [dat
11550 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
11560 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73 ].** in the firs
11570 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 t argument. If
11580 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 no successful [I
11590 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 20 NSERT]s.** have
115a0 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f 6e ever occurred on
115b0 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63 that database c
115c0 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f 20 onnection, zero
115d0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
115e0 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 54 ** If an [INSERT
115f0 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 ] occurs within
11600 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e 20 a trigger, then
11610 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 the [rowid] of t
11620 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 72 he inserted.** r
11630 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 ow is returned b
11640 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 y this routine a
11650 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 s long as the tr
11660 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 igger is running
11670 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 68 ..** But once th
11680 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 6e e trigger termin
11690 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 ates, the value
116a0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 returned by this
116b0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 65 routine.** reve
116c0 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 rts to the last
116d0 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 62 value inserted b
116e0 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 efore the trigge
116f0 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 r fired..**.** A
11700 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 20 n [INSERT] that
11710 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 63 fails due to a c
11720 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 onstraint violat
11730 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 ion is not a.**
11740 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 successful [INSE
11750 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 RT] and does not
11760 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 change the valu
11770 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 e returned by th
11780 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 20 is.** routine.
11790 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 46 Thus INSERT OR F
117a0 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 AIL, INSERT OR I
117b0 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 GNORE, INSERT OR
117c0 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e ROLLBACK,.** an
117d0 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 d INSERT OR ABOR
117e0 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 T make no change
117f0 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 s to the return
11800 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a value of this.**
11810 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 routine when th
11820 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 eir insertion fa
11830 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 52 ils. When INSER
11840 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 T OR REPLACE.**
11850 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e encounters a con
11860 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f straint violatio
11870 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 n, it does not f
11880 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 ail. The.** INS
11890 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f ERT continues to
118a0 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 completion afte
118b0 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 r deleting rows
118c0 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 that caused.** t
118d0 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 he constraint pr
118e0 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 oblem so INSERT
118f0 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 OR REPLACE will
11900 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a always change.**
11910 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
11920 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 e of this interf
11930 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 ace..**.** For t
11940 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 he purposes of t
11950 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 20 his routine, an
11960 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e 73 [INSERT] is cons
11970 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 20 idered to.** be
11980 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e 20 successful even
11990 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71 75 if it is subsequ
119a0 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 ently rolled bac
119b0 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 k..**.** Require
119c0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 32 ments:.** [H1222
119d0 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a 2a 1] [H12223].**.*
119e0 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 * If a separate
119f0 74 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73 20 thread performs
11a00 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f a new [INSERT] o
11a10 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 61 n the same.** da
11a20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
11a30 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71 6c n while the [sql
11a40 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 ite3_last_insert
11a50 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75 6e _rowid()].** fun
11a60 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 ction is running
11a70 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65 and thus change
11a80 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 s the last inser
11a90 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74 68 t [rowid],.** th
11aa0 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 en the value ret
11ab0 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 urned by [sqlite
11ac0 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 3_last_insert_ro
11ad0 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e 70 wid()] is.** unp
11ae0 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6d redictable and m
11af0 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 65 ight not equal e
11b00 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f 72 ither the old or
11b10 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73 74 the new.** last
11b20 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2e insert [rowid].
11b30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
11b40 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c qlite3_int64 sql
11b50 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 ite3_last_insert
11b60 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a 29 _rowid(sqlite3*)
11b70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
11b80 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75 6d F: Count The Num
11b90 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 ber Of Rows Modi
11ba0 66 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c 53 fied {H12240} <S
11bb0 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 10600>.**.** Thi
11bc0 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
11bd0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
11be0 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74 database rows t
11bf0 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 hat were changed
11c00 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64 20 .** or inserted
11c10 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74 68 or deleted by th
11c20 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
11c30 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73 74 completed SQL st
11c40 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74 68 atement.** on th
11c50 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
11c60 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 ection] specifie
11c70 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 70 d by the first p
11c80 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e 6c arameter..** Onl
11c90 79 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 61 y changes that a
11ca0 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65 63 re directly spec
11cb0 69 66 69 65 64 20 62 79 20 74 68 65 20 5b 49 4e ified by the [IN
11cc0 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d 2c SERT], [UPDATE],
11cd0 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 .** or [DELETE]
11ce0 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f statement are co
11cf0 75 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61 72 unted. Auxiliar
11d00 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 y changes caused
11d10 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 by.** triggers
11d20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e are not counted.
11d30 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65 Use the [sqlite
11d40 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 3_total_changes(
11d50 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 )] function.** t
11d60 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61 6c o find the total
11d70 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 number of chang
11d80 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 61 es including cha
11d90 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20 74 nges caused by t
11da0 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 riggers..**.** A
11db0 20 22 72 6f 77 20 63 68 61 6e 67 65 22 20 69 73 "row change" is
11dc0 20 61 20 63 68 61 6e 67 65 20 74 6f 20 61 20 73 a change to a s
11dd0 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 ingle row of a s
11de0 69 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 63 ingle table.** c
11df0 61 75 73 65 64 20 62 79 20 61 6e 20 49 4e 53 45 aused by an INSE
11e00 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 RT, DELETE, or U
11e10 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e PDATE statement.
11e20 20 20 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20 61 Rows that.** a
11e30 72 65 20 63 68 61 6e 67 65 64 20 61 73 20 73 69 re changed as si
11e40 64 65 20 65 66 66 65 63 74 73 20 6f 66 20 52 45 de effects of RE
11e50 50 4c 41 43 45 20 63 6f 6e 73 74 72 61 69 6e 74 PLACE constraint
11e60 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 resolution,.**
11e70 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54 20 rollback, ABORT
11e80 70 72 6f 63 65 73 73 69 6e 67 2c 20 44 52 4f 50 processing, DROP
11e90 20 54 41 42 4c 45 2c 20 6f 72 20 62 79 20 61 6e TABLE, or by an
11ea0 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 63 68 61 y other.** mecha
11eb0 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20 63 6f 75 nisms do not cou
11ec0 6e 74 20 61 73 20 64 69 72 65 63 74 20 72 6f 77 nt as direct row
11ed0 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 changes..**.**
11ee0 41 20 22 74 72 69 67 67 65 72 20 63 6f 6e 74 65 A "trigger conte
11ef0 78 74 22 20 69 73 20 61 20 73 63 6f 70 65 20 6f xt" is a scope o
11f00 66 20 65 78 65 63 75 74 69 6f 6e 20 74 68 61 74 f execution that
11f10 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a 2a 20 65 begins and.** e
11f20 6e 64 73 20 77 69 74 68 20 74 68 65 20 73 63 72 nds with the scr
11f30 69 70 74 20 6f 66 20 61 20 74 72 69 67 67 65 72 ipt of a trigger
11f40 2e 20 20 4d 6f 73 74 20 53 51 4c 20 73 74 61 74 . Most SQL stat
11f50 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 65 76 ements are.** ev
11f60 61 6c 75 61 74 65 64 20 6f 75 74 73 69 64 65 20 aluated outside
11f70 6f 66 20 61 6e 79 20 74 72 69 67 67 65 72 2e 20 of any trigger.
11f80 20 54 68 69 73 20 69 73 20 74 68 65 20 22 74 6f This is the "to
11f90 70 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 69 67 p level".** trig
11fa0 67 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 49 66 ger context. If
11fb0 20 61 20 74 72 69 67 67 65 72 20 66 69 72 65 73 a trigger fires
11fc0 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65 from the top le
11fd0 76 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 74 72 vel, a.** new tr
11fe0 69 67 67 65 72 20 63 6f 6e 74 65 78 74 20 69 73 igger context is
11ff0 20 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 entered for the
12000 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 61 duration of tha
12010 74 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 65 72 t one.** trigger
12020 2e 20 20 53 75 62 74 72 69 67 67 65 72 73 20 63 . Subtriggers c
12030 72 65 61 74 65 20 73 75 62 63 6f 6e 74 65 78 74 reate subcontext
12040 73 20 66 6f 72 20 74 68 65 69 72 20 64 75 72 61 s for their dura
12050 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c tion..**.** Call
12060 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 ing [sqlite3_exe
12070 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 c()] or [sqlite3
12080 5f 73 74 65 70 28 29 5d 20 72 65 63 75 72 73 69 _step()] recursi
12090 76 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 vely does.** not
120a0 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 74 72 create a new tr
120b0 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a igger context..*
120c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
120d0 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e on returns the n
120e0 75 6d 62 65 72 20 6f 66 20 64 69 72 65 63 74 20 umber of direct
120f0 72 6f 77 20 63 68 61 6e 67 65 73 20 69 6e 20 74 row changes in t
12100 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e he.** most recen
12110 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 t INSERT, UPDATE
12120 2c 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 , or DELETE stat
12130 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65 ement within the
12140 20 73 61 6d 65 0a 2a 2a 20 74 72 69 67 67 65 72 same.** trigger
12150 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 context..**.**
12160 54 68 75 73 2c 20 77 68 65 6e 20 63 61 6c 6c 65 Thus, when calle
12170 64 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c d from the top l
12180 65 76 65 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 evel, this funct
12190 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 0a ion returns the.
121a0 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 ** number of cha
121b0 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 nges in the most
121c0 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 recent INSERT,
121d0 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 UPDATE, or DELET
121e0 45 0a 2a 2a 20 74 68 61 74 20 61 6c 73 6f 20 6f E.** that also o
121f0 63 63 75 72 72 65 64 20 61 74 20 74 68 65 20 74 ccurred at the t
12200 6f 70 20 6c 65 76 65 6c 2e 20 20 57 69 74 68 69 op level. Withi
12210 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 n the body of a
12220 74 72 69 67 67 65 72 2c 0a 2a 2a 20 74 68 65 20 trigger,.** the
12230 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 sqlite3_changes(
12240 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 ) interface can
12250 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 6e be called to fin
12260 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a d the number of.
12270 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 ** changes in th
12280 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
12290 63 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 54 completed INSERT
122a0 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c , UPDATE, or DEL
122b0 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ETE.** statement
122c0 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 within the body
122d0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72 69 of the same tri
122e0 67 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 gger..** However
122f0 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 72 65 74 , the number ret
12300 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 urned does not i
12310 6e 63 6c 75 64 65 20 63 68 61 6e 67 65 73 0a 2a nclude changes.*
12320 2a 20 63 61 75 73 65 64 20 62 79 20 73 75 62 74 * caused by subt
12330 72 69 67 67 65 72 73 20 73 69 6e 63 65 20 74 68 riggers since th
12340 6f 73 65 20 68 61 76 65 20 74 68 65 69 72 20 6f ose have their o
12350 77 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a wn context..**.*
12360 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 * SQLite impleme
12370 6e 74 73 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 nts the command
12380 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 "DELETE FROM tab
12390 6c 65 22 20 77 69 74 68 6f 75 74 20 61 20 57 48 le" without a WH
123a0 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 ERE clause.** by
123b0 20 64 72 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 dropping and re
123c0 63 72 65 61 74 69 6e 67 20 74 68 65 20 74 61 62 creating the tab
123d0 6c 65 2e 20 20 44 6f 69 6e 67 20 73 6f 20 69 73 le. Doing so is
123e0 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 61 much faster tha
123f0 6e 20 67 6f 69 6e 67 0a 2a 2a 20 74 68 72 6f 75 n going.** throu
12400 67 68 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20 gh and deleting
12410 69 6e 64 69 76 69 64 75 61 6c 20 65 6c 65 6d 65 individual eleme
12420 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62 nts from the tab
12430 6c 65 2e 20 20 42 65 63 61 75 73 65 20 6f 66 20 le. Because of
12440 74 68 69 73 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 this.** optimiza
12450 74 69 6f 6e 2c 20 74 68 65 20 64 65 6c 65 74 69 tion, the deleti
12460 6f 6e 73 20 69 6e 20 22 44 45 4c 45 54 45 20 46 ons in "DELETE F
12470 52 4f 4d 20 74 61 62 6c 65 22 20 61 72 65 20 6e ROM table" are n
12480 6f 74 20 72 6f 77 20 63 68 61 6e 67 65 73 20 61 ot row changes a
12490 6e 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 nd.** will not b
124a0 65 20 63 6f 75 6e 74 65 64 20 62 79 20 74 68 65 e counted by the
124b0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
124c0 28 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 74 () or [sqlite3_t
124d0 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 0a otal_changes()].
124e0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2c 20 72 65 ** functions, re
124f0 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 gardless of the
12500 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e number of elemen
12510 74 73 20 74 68 61 74 20 77 65 72 65 20 6f 72 69 ts that were ori
12520 67 69 6e 61 6c 6c 79 0a 2a 2a 20 69 6e 20 74 68 ginally.** in th
12530 65 20 74 61 62 6c 65 2e 20 20 54 6f 20 67 65 74 e table. To get
12540 20 61 6e 20 61 63 63 75 72 61 74 65 20 63 6f 75 an accurate cou
12550 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 nt of the number
12560 20 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65 64 of rows deleted
12570 2c 20 75 73 65 0a 2a 2a 20 22 44 45 4c 45 54 45 , use.** "DELETE
12580 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 FROM table WHER
12590 45 20 31 22 20 69 6e 73 74 65 61 64 2e 20 20 4f E 1" instead. O
125a0 72 20 72 65 63 6f 6d 70 69 6c 65 20 75 73 69 6e r recompile usin
125b0 67 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 g the.** [SQLITE
125c0 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f _OMIT_TRUNCATE_O
125d0 50 54 49 4d 49 5a 41 54 49 4f 4e 5d 20 63 6f 6d PTIMIZATION] com
125e0 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e pile-time option
125f0 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 0a to disable the.
12600 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 ** optimization
12610 6f 6e 20 61 6c 6c 20 71 75 65 72 69 65 73 2e 0a on all queries..
12620 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
12630 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d 20 ts:.** [H12241]
12640 5b 48 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20 49 [H12243].**.** I
12650 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 f a separate thr
12660 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 ead makes change
12670 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 s on the same da
12680 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
12690 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 n.** while [sqli
126a0 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 te3_changes()] i
126b0 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 s running then t
126c0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
126d0 64 0a 2a 2a 20 69 73 20 75 6e 70 72 65 64 69 63 d.** is unpredic
126e0 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 table and not me
126f0 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c aningful..*/.SQL
12700 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
12710 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 te3_changes(sqli
12720 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 te3*);../*.** CA
12730 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e 75 PI3REF: Total Nu
12740 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 mber Of Rows Mod
12750 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20 3c ified {H12260} <
12760 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10600>.**.** Th
12770 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
12780 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
12790 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 f row changes ca
127a0 75 73 65 64 20 62 79 20 49 4e 53 45 52 54 2c 0a used by INSERT,.
127b0 2a 2a 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c ** UPDATE or DEL
127c0 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 73 ETE statements s
127d0 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62 61 ince the [databa
127e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 se connection] w
127f0 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54 68 as opened..** Th
12800 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65 73 e count includes
12810 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72 6f all changes fro
12820 6d 20 61 6c 6c 20 74 72 69 67 67 65 72 20 63 6f m all trigger co
12830 6e 74 65 78 74 73 2e 20 20 48 6f 77 65 76 65 72 ntexts. However
12840 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 ,.** the count d
12850 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 oes not include
12860 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f 20 changes used to
12870 69 6d 70 6c 65 6d 65 6e 74 20 52 45 50 4c 41 43 implement REPLAC
12880 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a E constraints,.*
12890 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f * do rollbacks o
128a0 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 r ABORT processi
128b0 6e 67 2c 20 6f 72 20 44 52 4f 50 20 74 61 62 6c ng, or DROP tabl
128c0 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a e processing..**
128d0 20 54 68 65 20 63 68 61 6e 67 65 73 20 61 72 65 The changes are
128e0 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e counted as soon
128f0 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e as the statemen
12900 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 t that makes the
12910 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 m is.** complete
12920 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 61 74 d (when the stat
12930 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 20 ement handle is
12940 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 passed to [sqlit
12950 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a e3_reset()] or.*
12960 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c * [sqlite3_final
12970 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 ize()])..**.** S
12980 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 QLite implements
12990 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22 44 45 the command "DE
129a0 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 22 LETE FROM table"
129b0 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52 45 without a WHERE
129c0 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 20 64 72 clause.** by dr
129d0 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 63 72 65 opping and recre
129e0 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e ating the table.
129f0 20 20 28 54 68 69 73 20 69 73 20 6d 75 63 68 20 (This is much
12a00 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f 69 6e faster than goin
12a10 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e 64 g.** through and
12a20 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69 76 69 deleting indivi
12a30 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20 66 72 dual elements fr
12a40 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 29 20 20 om the table.)
12a50 42 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 0a Because of this.
12a60 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c ** optimization,
12a70 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 73 20 69 the deletions i
12a80 6e 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 n "DELETE FROM t
12a90 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20 72 6f able" are not ro
12aa0 77 20 63 68 61 6e 67 65 73 20 61 6e 64 0a 2a 2a w changes and.**
12ab0 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 75 will not be cou
12ac0 6e 74 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 nted by the sqli
12ad0 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 72 te3_changes() or
12ae0 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f [sqlite3_total_
12af0 63 68 61 6e 67 65 73 28 29 5d 0a 2a 2a 20 66 75 changes()].** fu
12b00 6e 63 74 69 6f 6e 73 2c 20 72 65 67 61 72 64 6c nctions, regardl
12b10 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ess of the numbe
12b20 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 74 68 r of elements th
12b30 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e 61 6c at were original
12b40 6c 79 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 ly.** in the tab
12b50 6c 65 2e 20 20 54 6f 20 67 65 74 20 61 6e 20 61 le. To get an a
12b60 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20 6f 66 ccurate count of
12b70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
12b80 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75 73 65 ows deleted, use
12b90 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52 4f 4d .** "DELETE FROM
12ba0 20 74 61 62 6c 65 20 57 48 45 52 45 20 31 22 20 table WHERE 1"
12bb0 69 6e 73 74 65 61 64 2e 20 20 20 4f 72 20 72 65 instead. Or re
12bc0 63 6f 6d 70 69 6c 65 20 75 73 69 6e 67 20 74 68 compile using th
12bd0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 e.** [SQLITE_OMI
12be0 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d T_TRUNCATE_OPTIM
12bf0 49 5a 41 54 49 4f 4e 5d 20 63 6f 6d 70 69 6c 65 IZATION] compile
12c00 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 6f 20 -time option to
12c10 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 6f disable the.** o
12c20 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 ptimization on a
12c30 6c 6c 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a ll queries..**.*
12c40 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b * See also the [
12c50 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 sqlite3_changes(
12c60 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a )] interface..**
12c70 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
12c80 3a 0a 2a 2a 20 5b 48 31 32 32 36 31 5d 20 5b 48 :.** [H12261] [H
12c90 31 32 32 36 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 12263].**.** If
12ca0 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61 a separate threa
12cb0 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 d makes changes
12cc0 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 on the same data
12cd0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a base connection.
12ce0 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 ** while [sqlite
12cf0 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 3_total_changes(
12d00 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 )] is running th
12d10 65 6e 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 en the value.**
12d20 72 65 74 75 72 6e 65 64 20 69 73 20 75 6e 70 72 returned is unpr
12d30 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f edictable and no
12d40 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f t meaningful..*/
12d50 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
12d60 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 sqlite3_total_ch
12d70 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b anges(sqlite3*);
12d80 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
12d90 3a 20 49 6e 74 65 72 72 75 70 74 20 41 20 4c 6f : Interrupt A Lo
12da0 6e 67 2d 52 75 6e 6e 69 6e 67 20 51 75 65 72 79 ng-Running Query
12db0 20 7b 48 31 32 32 37 30 7d 20 3c 53 33 30 35 30 {H12270} <S3050
12dc0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 0>.**.** This fu
12dd0 6e 63 74 69 6f 6e 20 63 61 75 73 65 73 20 61 6e nction causes an
12de0 79 20 70 65 6e 64 69 6e 67 20 64 61 74 61 62 61 y pending databa
12df0 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 se operation to
12e00 61 62 6f 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74 abort and.** ret
12e10 75 72 6e 20 61 74 20 69 74 73 20 65 61 72 6c 69 urn at its earli
12e20 65 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e est opportunity.
12e30 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
12e40 20 74 79 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 typically.** ca
12e50 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 lled in response
12e60 20 74 6f 20 61 20 75 73 65 72 20 61 63 74 69 6f to a user actio
12e70 6e 20 73 75 63 68 20 61 73 20 70 72 65 73 73 69 n such as pressi
12e80 6e 67 20 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f ng "Cancel".** o
12e90 72 20 43 74 72 6c 2d 43 20 77 68 65 72 65 20 74 r Ctrl-C where t
12ea0 68 65 20 75 73 65 72 20 77 61 6e 74 73 20 61 20 he user wants a
12eb0 6c 6f 6e 67 20 71 75 65 72 79 20 6f 70 65 72 61 long query opera
12ec0 74 69 6f 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 tion to halt.**
12ed0 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a immediately..**.
12ee0 2a 2a 20 49 74 20 69 73 20 73 61 66 65 20 74 6f ** It is safe to
12ef0 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 call this routi
12f00 6e 65 20 66 72 6f 6d 20 61 20 74 68 72 65 61 64 ne from a thread
12f10 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
12f20 74 68 65 0a 2a 2a 20 74 68 72 65 61 64 20 74 68 the.** thread th
12f30 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 at is currently
12f40 72 75 6e 6e 69 6e 67 20 74 68 65 20 64 61 74 61 running the data
12f50 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 base operation.
12f60 20 42 75 74 20 69 74 0a 2a 2a 20 69 73 20 6e 6f But it.** is no
12f70 74 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 t safe to call t
12f80 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 his routine with
12f90 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e a [database con
12fa0 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a nection] that.**
12fb0 20 69 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69 is closed or mi
12fc0 67 68 74 20 63 6c 6f 73 65 20 62 65 66 6f 72 65 ght close before
12fd0 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
12fe0 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a pt() returns..**
12ff0 0a 2a 2a 20 49 66 20 61 6e 20 53 51 4c 20 6f 70 .** If an SQL op
13000 65 72 61 74 69 6f 6e 20 69 73 20 76 65 72 79 20 eration is very
13010 6e 65 61 72 6c 79 20 66 69 6e 69 73 68 65 64 20 nearly finished
13020 61 74 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e at the time when
13030 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 .** sqlite3_inte
13040 72 72 75 70 74 28 29 20 69 73 20 63 61 6c 6c 65 rrupt() is calle
13050 64 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 d, then it might
13060 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f 70 70 not have an opp
13070 6f 72 74 75 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 ortunity.** to b
13080 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 6e e interrupted an
13090 64 20 6d 69 67 68 74 20 63 6f 6e 74 69 6e 75 65 d might continue
130a0 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a to completion..
130b0 2a 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 6f 70 65 **.** An SQL ope
130c0 72 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 ration that is i
130d0 6e 74 65 72 72 75 70 74 65 64 20 77 69 6c 6c 20 nterrupted will
130e0 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 return [SQLITE_I
130f0 4e 54 45 52 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 NTERRUPT]..** If
13100 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 65 64 the interrupted
13110 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 SQL operation i
13120 73 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 s an INSERT, UPD
13130 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a ATE, or DELETE.*
13140 2a 20 74 68 61 74 20 69 73 20 69 6e 73 69 64 65 * that is inside
13150 20 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 an explicit tra
13160 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 nsaction, then t
13170 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 he entire transa
13180 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 ction.** will be
13190 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 rolled back aut
131a0 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a omatically..**.*
131b0 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 * A call to sqli
131c0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 te3_interrupt()
131d0 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e has no effect on
131e0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a SQL statements.
131f0 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 72 ** that are star
13200 74 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 ted after sqlite
13210 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 65 3_interrupt() re
13220 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 turns..**.** Req
13230 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
13240 31 32 32 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a 12271] [H12272].
13250 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 **.** If the dat
13260 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13270 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 closes while [s
13280 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
13290 28 29 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e ()].** is runnin
132a0 67 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 g then bad thing
132b0 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 s will likely ha
132c0 70 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ppen..*/.SQLITE_
132d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
132e0 5f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 _interrupt(sqlit
132f0 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 e3*);../*.** CAP
13300 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 I3REF: Determine
13310 20 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 If An SQL State
13320 6d 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 ment Is Complete
13330 20 7b 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30 {H10510} <S7020
13340 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
13350 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 outines are usef
13360 75 6c 20 66 6f 72 20 63 6f 6d 6d 61 6e 64 2d 6c ul for command-l
13370 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 74 ine input to det
13380 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a ermine if the.**
13390 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
133a0 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 6f ed text seems to
133b0 20 66 6f 72 6d 20 63 6f 6d 70 6c 65 74 65 20 61 form complete a
133c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f SQL statement o
133d0 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e r.** if addition
133e0 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64 al input is need
133f0 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e ed before sendin
13400 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0a g the text into.
13410 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61 ** SQLite for pa
13420 72 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 6f rsing. These ro
13430 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 72 utines return tr
13440 75 65 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 ue if the input
13450 73 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 string.** appear
13460 73 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 s to be a comple
13470 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 te SQL statement
13480 2e 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69 . A statement i
13490 73 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a s judged to be.*
134a0 2a 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 * complete if it
134b0 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d ends with a sem
134c0 69 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 icolon token and
134d0 20 69 73 20 6e 6f 74 20 61 20 66 72 61 67 6d 65 is not a fragme
134e0 6e 74 20 6f 66 20 61 0a 2a 2a 20 43 52 45 41 54 nt of a.** CREAT
134f0 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d E TRIGGER statem
13500 65 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e 73 ent. Semicolons
13510 20 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 64 that are embedd
13520 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 72 ed within.** str
13530 69 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 20 ing literals or
13540 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65 quoted identifie
13550 72 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65 r names or comme
13560 6e 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 69 nts are not.** i
13570 6e 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e ndependent token
13580 73 20 28 74 68 65 79 20 61 72 65 20 70 61 72 74 s (they are part
13590 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e of the token in
135a0 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65 0a which they are.
135b0 2a 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64 ** embedded) and
135c0 20 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75 thus do not cou
135d0 6e 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e nt as a statemen
135e0 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a t terminator..**
135f0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
13600 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20 es do not parse
13610 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
13620 74 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 ts thus.** will
13630 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 61 not detect synta
13640 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65 ctically incorre
13650 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 ct SQL..**.** Re
13660 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 quirements: [H10
13670 35 31 31 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 511] [H10512].**
13680 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f .** The input to
13690 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 [sqlite3_comple
136a0 74 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 te()] must be a
136b0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a zero-terminated.
136c0 2a 2a 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e ** UTF-8 string.
136d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 .**.** The input
136e0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d to [sqlite3_com
136f0 70 6c 65 74 65 31 36 28 29 5d 20 6d 75 73 74 20 plete16()] must
13700 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e be a zero-termin
13710 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 ated.** UTF-16 s
13720 74 72 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 tring in native
13730 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 byte order..*/.S
13740 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
13750 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 lite3_complete(c
13760 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 29 3b onst char *sql);
13770 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
13780 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 sqlite3_complete
13790 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 16(const void *s
137a0 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ql);../*.** CAPI
137b0 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 3REF: Register A
137c0 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e Callback To Han
137d0 64 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 dle SQLITE_BUSY
137e0 45 72 72 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 Errors {H12310}
137f0 3c 53 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S40400>.**.** T
13800 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 his routine sets
13810 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 a callback func
13820 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20 tion that might
13830 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 be invoked whene
13840 76 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 ver.** an attemp
13850 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 t is made to ope
13860 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 n a database tab
13870 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 le that another
13880 74 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f thread.** or pro
13890 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e cess has locked.
138a0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 .**.** If the bu
138b0 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e sy callback is N
138c0 55 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 ULL, then [SQLIT
138d0 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 E_BUSY] or [SQLI
138e0 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 TE_IOERR_BLOCKED
138f0 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 ].** is returned
13900 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 6f immediately upo
13910 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 n encountering t
13920 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 he lock. If the
13930 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a busy callback.**
13940 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 is not NULL, th
13950 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 en the callback
13960 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 will be invoked
13970 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e with two argumen
13980 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ts..**.** The fi
13990 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
139a0 74 68 65 20 68 61 6e 64 6c 65 72 20 69 73 20 61 the handler is a
139b0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 copy of the voi
139c0 64 2a 20 70 6f 69 6e 74 65 72 20 77 68 69 63 68 d* pointer which
139d0 0a 2a 2a 20 69 73 20 74 68 65 20 74 68 69 72 64 .** is the third
139e0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
139f0 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 ite3_busy_handle
13a00 72 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 r(). The second
13a10 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 argument to.**
13a20 74 68 65 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c the handler call
13a30 62 61 63 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 back is the numb
13a40 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 er of times that
13a50 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
13a60 72 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e r has.** been in
13a70 76 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c voked for this l
13a80 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 ocking event. I
13a90 66 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 f the.** busy ca
13aa0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30 llback returns 0
13ab0 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 , then no additi
13ac0 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72 onal attempts ar
13ad0 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 e made to.** acc
13ae0 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ess the database
13af0 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 and [SQLITE_BUS
13b00 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f Y] or [SQLITE_IO
13b10 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 ERR_BLOCKED] is
13b20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 returned..** If
13b30 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 the callback ret
13b40 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 urns non-zero, t
13b50 68 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65 hen another atte
13b60 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 mpt.** is made t
13b70 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 o open the datab
13b80 61 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 ase for reading
13b90 61 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72 65 and the cycle re
13ba0 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 peats..**.** The
13bb0 20 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62 presence of a b
13bc0 75 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 usy handler does
13bd0 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74 not guarantee t
13be0 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 69 hat it will be i
13bf0 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 nvoked.** when t
13c00 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e here is lock con
13c10 74 65 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 tention. If SQLi
13c20 74 65 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 te determines th
13c30 61 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 at invoking the
13c40 62 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 busy.** handler
13c50 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 could result in
13c60 61 20 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 a deadlock, it w
13c70 69 6c 6c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 ill go ahead and
13c80 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
13c90 42 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c BUSY].** or [SQL
13ca0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
13cb0 44 5d 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e D] instead of in
13cc0 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 voking the busy
13cd0 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 handler..** Cons
13ce0 69 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20 ider a scenario
13cf0 77 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 where one proces
13d00 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 s is holding a r
13d10 65 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a ead lock that.**
13d20 20 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f it is trying to
13d30 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 promote to a re
13d40 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a served lock and.
13d50 2a 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 ** a second proc
13d60 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 ess is holding a
13d70 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 reserved lock t
13d80 68 61 74 20 69 74 20 69 73 20 74 72 79 69 6e 67 hat it is trying
13d90 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 .** to promote t
13da0 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c o an exclusive l
13db0 6f 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20 ock. The first
13dc0 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 process cannot p
13dd0 72 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 roceed.** becaus
13de0 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 e it is blocked
13df0 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e by the second an
13e00 64 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f d the second pro
13e10 63 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 cess cannot.** p
13e20 72 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 69 roceed because i
13e30 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 t is blocked by
13e40 74 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 62 the first. If b
13e50 6f 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a oth processes.**
13e60 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 invoke the busy
13e70 20 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 handlers, neith
13e80 65 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 er will make any
13e90 20 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72 progress. Ther
13ea0 65 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 efore,.** SQLite
13eb0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
13ec0 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66 _BUSY] for the f
13ed0 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f irst process, ho
13ee0 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a ping that this.*
13ef0 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 * will induce th
13f00 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 e first process
13f10 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 72 to release its r
13f20 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c ead lock and all
13f30 6f 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 ow.** the second
13f40 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 process to proc
13f50 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 eed..**.** The d
13f60 65 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c efault busy call
13f70 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a back is NULL..**
13f80 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f .** The [SQLITE_
13f90 42 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63 BUSY] error is c
13fa0 6f 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c onverted to [SQL
13fb0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
13fc0 44 5d 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 D].** when SQLit
13fd0 65 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64 e is in the midd
13fe0 6c 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72 le of a large tr
13ff0 61 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20 ansaction where
14000 61 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 all the.** chang
14010 65 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 es will not fit
14020 69 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f into the in-memo
14030 72 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74 ry cache. SQLit
14040 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 e will.** alread
14050 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 y hold a RESERVE
14060 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 D lock on the da
14070 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 tabase file, but
14080 20 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 it needs.** to
14090 70 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 promote this loc
140a0 6b 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73 k to EXCLUSIVE s
140b0 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 70 o that it can sp
140c0 69 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 ill cache.** pag
140d0 65 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 es into the data
140e0 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 base file withou
140f0 74 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 t harm to concur
14100 72 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e rent.** readers.
14110 20 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c If it is unabl
14120 65 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 e to promote the
14130 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 lock, then the
14140 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 in-memory.** cac
14150 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 he will be left
14160 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 in an inconsiste
14170 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20 nt state and so
14180 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 the error.** cod
14190 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 e is promoted fr
141a0 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c om the relativel
141b0 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 y benign [SQLITE
141c0 5f 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 _BUSY] to.** the
141d0 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51 more severe [SQ
141e0 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
141f0 45 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72 ED]. This error
14200 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a code promotion.
14210 2a 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74 ** forces an aut
14220 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 omatic rollback
14230 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20 of the changes.
14240 20 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 See the.** <a h
14250 72 65 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 ref="/cvstrac/wi
14260 6b 69 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 ki?p=CorruptionF
14270 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f ollowingBusyErro
14280 72 22 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f r">.** Corruptio
14290 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 nFollowingBusyEr
142a0 72 6f 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 ror</a> wiki pag
142b0 65 20 66 6f 72 20 61 20 64 69 73 63 75 73 73 69 e for a discussi
142c0 6f 6e 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 on of why.** thi
142d0 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a s is important..
142e0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 **.** There can
142f0 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 only be a single
14300 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 65 busy handler de
14310 66 69 6e 65 64 20 66 6f 72 20 65 61 63 68 0a 2a fined for each.*
14320 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
14330 65 63 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e ection]. Settin
14340 67 20 61 20 6e 65 77 20 62 75 73 79 20 68 61 6e g a new busy han
14350 64 6c 65 72 20 63 6c 65 61 72 73 20 61 6e 79 0a dler clears any.
14360 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 ** previously se
14370 74 20 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 t handler. Note
14380 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 that calling [s
14390 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 qlite3_busy_time
143a0 6f 75 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 out()].** will a
143b0 6c 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 lso set or clear
143c0 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
143d0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 r..**.** The bus
143e0 79 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c y callback shoul
143f0 64 20 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 d not take any a
14400 63 74 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 ctions which mod
14410 69 66 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 ify the.** datab
14420 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
14430 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 hat invoked the
14440 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 busy handler. A
14450 6e 79 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a ny such actions.
14460 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 ** result in und
14470 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e efined behavior.
14480 0a 2a 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d .** .** Requirem
14490 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 ents:.** [H12311
144a0 5d 20 5b 48 31 32 33 31 32 5d 20 5b 48 31 32 33 ] [H12312] [H123
144b0 31 34 5d 20 5b 48 31 32 33 31 36 5d 20 5b 48 31 14] [H12316] [H1
144c0 32 33 31 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 2318].**.** A bu
144d0 73 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 sy handler must
144e0 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61 not close the da
144f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14500 6e 0a 2a 2a 20 6f 72 20 5b 70 72 65 70 61 72 65 n.** or [prepare
14510 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 d statement] tha
14520 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 t invoked the bu
14530 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 sy handler..*/.S
14540 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
14550 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c lite3_busy_handl
14560 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 er(sqlite3*, int
14570 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 (*)(void*,int),
14580 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 void*);../*.** C
14590 41 50 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 API3REF: Set A B
145a0 75 73 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 usy Timeout {H12
145b0 33 34 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 340} <S40410>.**
145c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
145d0 20 73 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33 sets a [sqlite3
145e0 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 _busy_handler |
145f0 62 75 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 busy handler] th
14600 61 74 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 at sleeps.** for
14610 20 61 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f a specified amo
14620 75 6e 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e unt of time when
14630 20 61 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b a table is lock
14640 65 64 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72 ed. The handler
14650 0a 2a 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d .** will sleep m
14660 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e ultiple times un
14670 74 69 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73 til at least "ms
14680 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f " milliseconds o
14690 66 20 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 f sleeping.** ha
146a0 76 65 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 ve accumulated.
146b0 7b 48 31 32 33 34 33 7d 20 41 66 74 65 72 20 22 {H12343} After "
146c0 6d 73 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 ms" milliseconds
146d0 20 6f 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a of sleeping,.**
146e0 20 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 the handler ret
146f0 75 72 6e 73 20 30 20 77 68 69 63 68 20 63 61 75 urns 0 which cau
14700 73 65 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 ses [sqlite3_ste
14710 70 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a p()] to return.*
14720 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 * [SQLITE_BUSY]
14730 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 or [SQLITE_IOERR
14740 5f 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a _BLOCKED]..**.**
14750 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f Calling this ro
14760 75 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 utine with an ar
14770 67 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e gument less than
14780 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 or equal to zer
14790 6f 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 o.** turns off a
147a0 6c 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 ll busy handlers
147b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 ..**.** There ca
147c0 6e 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 n only be a sing
147d0 6c 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 le busy handler
147e0 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 for a particular
147f0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f .** [database co
14800 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e nnection] any an
14810 79 20 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 y given moment.
14820 20 49 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 If another busy
14830 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 handler.** was
14840 64 65 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 defined (using
14850 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 [sqlite3_busy_ha
14860 6e 64 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 ndler()]) prior
14870 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 to calling.** th
14880 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 is routine, that
14890 20 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 other busy hand
148a0 6c 65 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a ler is cleared..
148b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
148c0 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20 ts:.** [H12341]
148d0 5b 48 31 32 33 34 33 5d 20 5b 48 31 32 33 34 34 [H12343] [H12344
148e0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
148f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 int sqlite3_busy
14900 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 _timeout(sqlite3
14910 2a 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a *, int ms);../*.
14920 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e ** CAPI3REF: Con
14930 76 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 venience Routine
14940 73 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 s For Running Qu
14950 65 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c eries {H12370} <
14960 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 S10000>.**.** De
14970 66 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 finition: A <b>r
14980 65 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 esult table</b>
14990 69 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 is memory data s
149a0 74 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64 tructure created
149b0 20 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 by the.** [sqli
149c0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d te3_get_table()]
149d0 20 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 interface. A r
149e0 65 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f esult table reco
149f0 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c rds the.** compl
14a00 65 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 ete query result
14a10 73 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f s from one or mo
14a20 72 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a re queries..**.*
14a30 2a 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 * The table conc
14a40 65 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e eptually has a n
14a50 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e umber of rows an
14a60 64 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a d columns. But.
14a70 2a 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 ** these numbers
14a80 20 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 are not part of
14a90 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c the result tabl
14aa0 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 e itself. These
14ab0 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 .** numbers are
14ac0 6f 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 obtained separat
14ad0 65 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 ely. Let N be t
14ae0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
14af0 73 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 s.** and M be th
14b00 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
14b10 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 mns..**.** A res
14b20 75 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 ult table is an
14b30 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 array of pointer
14b40 73 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e s to zero-termin
14b50 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e ated UTF-8 strin
14b60 67 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 gs..** There are
14b70 20 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 (N+1)*M element
14b80 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 s in the array.
14b90 20 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 The first M poi
14ba0 6e 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 nters point.** t
14bb0 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 o zero-terminate
14bc0 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 d strings that
14bd0 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 contain the name
14be0 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 s of the columns
14bf0 2e 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 ..** The remaini
14c00 6e 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 ng entries all p
14c10 6f 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 oint to query re
14c20 73 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c sults. NULL val
14c30 75 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e ues result.** in
14c40 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 NULL pointers.
14c50 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 All other value
14c60 73 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 s are in their U
14c70 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e TF-8 zero-termin
14c80 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 ated.** string r
14c90 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 epresentation as
14ca0 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 returned by [sq
14cb0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
14cc0 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 t()]..**.** A re
14cd0 73 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 sult table might
14ce0 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 consist of one
14cf0 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 or more memory a
14d00 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 llocations..** I
14d10 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f t is not safe to
14d20 20 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 pass a result t
14d30 61 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f able directly to
14d40 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
14d50 5d 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 ]..** A result t
14d60 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 able should be d
14d70 65 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 eallocated using
14d80 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 [sqlite3_free_t
14d90 61 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 able()]..**.** A
14da0 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 s an example of
14db0 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 the result table
14dc0 20 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 format, suppose
14dd0 20 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a a query result.
14de0 2a 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 ** is as follows
14df0 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
14e00 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 ote><pre>.**
14e10 20 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 Name
14e20 7c 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 | Age.**
14e30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14e40 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 -------.**
14e50 20 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 Alice |
14e60 34 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 43.** Bob
14e70 20 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a | 28.**
14e80 20 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 Cindy
14e90 20 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 | 21.** </pr
14ea0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
14eb0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
14ec0 74 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 two column (M==2
14ed0 29 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 ) and three rows
14ee0 20 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 (N==3). Thus t
14ef0 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 he.** result tab
14f00 6c 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 le has 8 entries
14f10 2e 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 . Suppose the r
14f20 65 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 esult table is s
14f30 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 tored.** in an a
14f40 72 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 rray names azRes
14f50 75 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 ult. Then azRes
14f60 75 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 ult holds this c
14f70 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 ontent:.**.** <b
14f80 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
14f90 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
14fa0 6c 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d lt[0] = "Nam
14fb0 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a e";.** az
14fc0 52 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 Result[1] =
14fd0 22 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 "Age";.**
14fe0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d azResult[2]
14ff0 20 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 = "Alice";.**
15000 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 azResult&#
15010 39 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 91;3] = "43";.**
15020 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 azResult
15030 26 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b [4] = "Bob";
15040 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 .** azRes
15050 75 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 ult[5] = "28
15060 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
15070 65 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 esult[6] = "
15080 43 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 Cindy";.**
15090 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 azResult[7
150a0 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 ] = "21";.** </p
150b0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
150c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
150d0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 e3_get_table() f
150e0 75 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 unction evaluate
150f0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a s one or more.**
15100 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 semicolon-separ
15110 61 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 ated SQL stateme
15120 6e 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d nts in the zero-
15130 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
15140 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 .** string of it
15150 73 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e s 2nd parameter.
15160 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 It returns a r
15170 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 esult table to t
15180 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 he.** pointer gi
15190 76 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 ven in its 3rd p
151a0 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 arameter..**.**
151b0 41 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e After the callin
151c0 67 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 g function has f
151d0 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 inished using th
151e0 65 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f e result, it sho
151f0 75 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 uld.** pass the
15200 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
15210 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 esult table to s
15220 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c qlite3_free_tabl
15230 65 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a e() in order to.
15240 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d ** release the m
15250 65 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d emory that was m
15260 61 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 alloced. Becaus
15270 65 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 e of the way the
15280 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c .** [sqlite3_mal
15290 6c 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 loc()] happens w
152a0 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 ithin sqlite3_ge
152b0 74 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 t_table(), the c
152c0 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 alling.** functi
152d0 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 on must not try
152e0 74 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 to call [sqlite3
152f0 5f 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c _free()] directl
15300 79 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c y. Only.** [sql
15310 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 ite3_free_table(
15320 29 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 )] is able to re
15330 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 lease the memory
15340 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 properly and sa
15350 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fely..**.** The
15360 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c sqlite3_get_tabl
15370 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 e() interface is
15380 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
15390 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
153a0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 .** [sqlite3_exe
153b0 63 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 c()]. The sqlit
153c0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 e3_get_table() r
153d0 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 outine does not
153e0 68 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 have access.** t
153f0 6f 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 o any internal d
15400 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f ata structures o
15410 66 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 f SQLite. It us
15420 65 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c es only the publ
15430 69 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 ic.** interface
15440 64 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 defined here. A
15450 73 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c s a consequence,
15460 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 errors that occ
15470 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 ur in the.** wra
15480 70 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 pper layer outsi
15490 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e de of the intern
154a0 61 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 al [sqlite3_exec
154b0 28 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 ()] call are not
154c0 0a 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e .** reflected in
154d0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
154e0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 s to [sqlite3_er
154f0 72 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c rcode()] or [sql
15500 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a ite3_errmsg()]..
15510 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
15520 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20 ts:.** [H12371]
15530 5b 48 31 32 33 37 33 5d 20 5b 48 31 32 33 37 34 [H12373] [H12374
15540 5d 20 5b 48 31 32 33 37 36 5d 20 5b 48 31 32 33 ] [H12376] [H123
15550 37 39 5d 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 79] [H12382].*/.
15560 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
15570 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 qlite3_get_table
15580 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
15590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 /* An
155a0 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f open database */
155b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
155c0 53 71 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 Sql, /* SQL
155d0 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 to be evaluated
155e0 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a */. char ***paz
155f0 52 65 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 Result, /* Re
15600 73 75 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65 sults of the que
15610 72 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 ry */. int *pnR
15620 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ow, /*
15630 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c Number of resul
15640 74 20 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68 t rows written h
15650 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e ere */. int *pn
15660 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f Column, /
15670 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 * Number of resu
15680 6c 74 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 lt columns writt
15690 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 en here */. cha
156a0 72 20 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 r **pzErrmsg
156b0 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 /* Error msg
156c0 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a written here */.
156d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
156e0 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f id sqlite3_free_
156f0 74 61 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 table(char **res
15700 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ult);../*.** CAP
15710 49 33 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 I3REF: Formatted
15720 20 53 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 String Printing
15730 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34 Functions {H174
15740 30 30 7d 20 3c 53 37 30 30 30 30 3e 3c 53 32 30 00} <S70000><S20
15750 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 000>.**.** These
15760 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 77 6f routines are wo
15770 72 6b 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 20 rkalikes of the
15780 22 70 72 69 6e 74 66 28 29 22 20 66 61 6d 69 6c "printf()" famil
15790 79 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a y of functions.*
157a0 2a 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 * from the stand
157b0 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a ard C library..*
157c0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
157d0 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 _mprintf() and s
157e0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 qlite3_vmprintf(
157f0 29 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65 ) routines write
15800 20 74 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74 their.** result
15810 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 s into memory ob
15820 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
15830 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a ite3_malloc()]..
15840 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 72 ** The strings r
15850 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 eturned by these
15860 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 68 two routines sh
15870 6f 75 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61 ould be.** relea
15880 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f sed by [sqlite3_
15890 66 72 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 72 free()]. Both r
158a0 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 61 outines return a
158b0 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 .** NULL pointer
158c0 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c if [sqlite3_mal
158d0 6c 6f 63 28 29 5d 20 69 73 20 75 6e 61 62 6c 65 loc()] is unable
158e0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f to allocate eno
158f0 75 67 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f ugh.** memory to
15900 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 hold the result
15910 69 6e 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a ing string..**.*
15920 2a 20 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70 * In sqlite3_snp
15930 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20 rintf() routine
15940 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 73 is similar to "s
15950 6e 70 72 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a nprintf()" from.
15960 2a 2a 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 ** the standard
15970 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 C library. The
15980 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 result is writte
15990 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 n into the.** bu
159a0 66 66 65 72 20 73 75 70 70 6c 69 65 64 20 61 73 ffer supplied as
159b0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
159c0 6d 65 74 65 72 20 77 68 6f 73 65 20 73 69 7a 65 meter whose size
159d0 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 is given by.**
159e0 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 the first parame
159f0 74 65 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 ter. Note that t
15a00 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a he order of the.
15a10 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61 72 ** first two par
15a20 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 72 ameters is rever
15a30 73 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 sed from snprint
15a40 66 28 29 2e 20 20 54 68 69 73 20 69 73 20 61 6e f(). This is an
15a50 0a 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 61 .** historical a
15a60 63 63 69 64 65 6e 74 20 74 68 61 74 20 63 61 6e ccident that can
15a70 6e 6f 74 20 62 65 20 66 69 78 65 64 20 77 69 74 not be fixed wit
15a80 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a hout breaking.**
15a90 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
15aa0 74 69 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 tibility. Note
15ab0 61 6c 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 also that sqlite
15ac0 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 3_snprintf().**
15ad0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
15ae0 72 20 74 6f 20 69 74 73 20 62 75 66 66 65 72 20 r to its buffer
15af0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e instead of the n
15b00 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 umber of.** char
15b10 61 63 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20 acters actually
15b20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
15b30 20 62 75 66 66 65 72 2e 20 20 57 65 20 61 64 6d buffer. We adm
15b40 69 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e it that.** the n
15b50 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
15b60 65 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c ers written woul
15b70 64 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66 d be a more usef
15b80 75 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c ul return.** val
15b90 75 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 ue but we cannot
15ba0 20 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c change the impl
15bb0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
15bc0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
15bd0 0a 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 .** now without
15be0 62 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 breaking compati
15bf0 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 bility..**.** As
15c00 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66 long as the buf
15c10 66 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 61 fer size is grea
15c20 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 ter than zero, s
15c30 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
15c40 29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20 ).** guarantees
15c50 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72 20 that the buffer
15c60 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 is always zero-t
15c70 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 erminated. The
15c80 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 first.** paramet
15c90 65 72 20 22 6e 22 20 69 73 20 74 68 65 20 74 6f er "n" is the to
15ca0 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 tal size of the
15cb0 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e buffer, includin
15cc0 67 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 g space for.** t
15cd0 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 he zero terminat
15ce0 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 or. So the long
15cf0 65 73 74 20 73 74 72 69 6e 67 20 74 68 61 74 20 est string that
15d00 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c can be completel
15d10 79 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 6c y.** written wil
15d20 6c 20 62 65 20 6e 2d 31 20 63 68 61 72 61 63 74 l be n-1 charact
15d30 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ers..**.** These
15d40 20 72 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d routines all im
15d50 70 6c 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 plement some add
15d60 69 74 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 itional formatti
15d70 6e 67 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 ng.** options th
15d80 61 74 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f at are useful fo
15d90 72 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 r constructing S
15da0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a QL statements..*
15db0 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 * All of the usu
15dc0 61 6c 20 70 72 69 6e 74 66 28 29 20 66 6f 72 6d al printf() form
15dd0 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 atting options a
15de0 70 70 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 69 pply. In additi
15df0 6f 6e 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 on, there.** is
15e00 61 72 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20 are "%q", "%Q",
15e10 61 6e 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 and "%z" options
15e20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f ..**.** The %q o
15e30 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 ption works like
15e40 20 25 73 20 69 6e 20 74 68 61 74 20 69 74 20 73 %s in that it s
15e50 75 62 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c ubstitutes a nul
15e60 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 l-terminated.**
15e70 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 string from the
15e80 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 argument list.
15e90 42 75 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 But %q also doub
15ea0 6c 65 73 20 65 76 65 72 79 20 27 5c 27 27 20 63 les every '\'' c
15eb0 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 haracter..** %q
15ec0 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 is designed for
15ed0 75 73 65 20 69 6e 73 69 64 65 20 61 20 73 74 72 use inside a str
15ee0 69 6e 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 ing literal. By
15ef0 20 64 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27 doubling each '
15f00 5c 27 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 \''.** character
15f10 20 69 74 20 65 73 63 61 70 65 73 20 74 68 61 74 it escapes that
15f20 20 63 68 61 72 61 63 74 65 72 20 61 6e 64 20 61 character and a
15f30 6c 6c 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69 llows it to be i
15f40 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 nserted into.**
15f50 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a the string..**.*
15f60 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 * For example, a
15f70 73 73 75 6d 65 20 74 68 65 20 73 74 72 69 6e 67 ssume the string
15f80 20 76 61 72 69 61 62 6c 65 20 7a 54 65 78 74 20 variable zText
15f90 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61 73 contains text as
15fa0 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
15fb0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 <blockquote><pre
15fc0 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 78 >.** char *zTex
15fd0 74 20 3d 20 22 49 74 27 73 20 61 20 68 61 70 70 t = "It's a happ
15fe0 79 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72 y day!";.** </pr
15ff0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
16000 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 **.** One can us
16010 65 20 74 68 69 73 20 74 65 78 74 20 69 6e 20 61 e this text in a
16020 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
16030 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
16040 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
16050 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 re>.** char *zS
16060 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 QL = sqlite3_mpr
16070 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 intf("INSERT INT
16080 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 27 O table VALUES('
16090 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a %q')", zText);.*
160a0 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 * sqlite3_exec(
160b0 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 db, zSQL, 0, 0,
160c0 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 0);.** sqlite3_
160d0 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c free(zSQL);.** <
160e0 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 /pre></blockquot
160f0 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 e>.**.** Because
16100 20 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20 73 the %q format s
16110 74 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74 tring is used, t
16120 68 65 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 he '\'' characte
16130 72 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73 r in zText.** is
16140 20 65 73 63 61 70 65 64 20 61 6e 64 20 74 68 65 escaped and the
16150 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20 69 SQL generated i
16160 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
16170 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
16180 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 <pre>.** INSERT
16190 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c INTO table1 VAL
161a0 55 45 53 28 27 49 74 27 27 73 20 61 20 68 61 70 UES('It''s a hap
161b0 70 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70 py day!').** </p
161c0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
161d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 .**.** This is c
161e0 6f 72 72 65 63 74 2e 20 20 48 61 64 20 77 65 20 orrect. Had we
161f0 75 73 65 64 20 25 73 20 69 6e 73 74 65 61 64 20 used %s instead
16200 6f 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72 of %q, the gener
16210 61 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c ated SQL.** woul
16220 64 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 d have looked li
16230 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c ke this:.**.** <
16240 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
16250 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f .** INSERT INTO
16260 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 table1 VALUES('
16270 49 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79 It's a happy day
16280 21 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f !');.** </pre></
16290 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a blockquote>.**.*
162a0 2a 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65 78 * This second ex
162b0 61 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c 20 ample is an SQL
162c0 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 41 syntax error. A
162d0 73 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c 65 s a general rule
162e0 20 79 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 you should.** a
162f0 6c 77 61 79 73 20 75 73 65 20 25 71 20 69 6e 73 lways use %q ins
16300 74 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e 20 tead of %s when
16310 69 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20 69 inserting text i
16320 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74 nto a string lit
16330 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eral..**.** The
16340 25 51 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 %Q option works
16350 6c 69 6b 65 20 25 71 20 65 78 63 65 70 74 20 69 like %q except i
16360 74 20 61 6c 73 6f 20 61 64 64 73 20 73 69 6e 67 t also adds sing
16370 6c 65 20 71 75 6f 74 65 73 20 61 72 6f 75 6e 64 le quotes around
16380 0a 2a 2a 20 74 68 65 20 6f 75 74 73 69 64 65 20 .** the outside
16390 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 73 74 72 of the total str
163a0 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c ing. Additional
163b0 6c 79 2c 20 69 66 20 74 68 65 20 70 61 72 61 6d ly, if the param
163c0 65 74 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 eter in the.** a
163d0 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 20 rgument list is
163e0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
163f0 25 51 20 73 75 62 73 74 69 74 75 74 65 73 20 74 %Q substitutes t
16400 68 65 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 he text "NULL" (
16410 77 69 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c without.** singl
16420 65 20 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 e quotes) in pla
16430 63 65 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74 ce of the %Q opt
16440 69 6f 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 ion. So, for ex
16450 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 ample, one could
16460 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f say:.**.** <blo
16470 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
16480 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 char *zSQL = s
16490 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 qlite3_mprintf("
164a0 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c INSERT INTO tabl
164b0 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a e VALUES(%Q)", z
164c0 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 Text);.** sqlit
164d0 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c e3_exec(db, zSQL
164e0 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 , 0, 0, 0);.**
164f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 sqlite3_free(zSQ
16500 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 L);.** </pre></b
16510 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
16520 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 The code above
16530 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f will render a co
16540 72 72 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d rrect SQL statem
16550 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a ent in the zSQL.
16560 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e ** variable even
16570 20 69 66 20 74 68 65 20 7a 54 65 78 74 20 76 61 if the zText va
16580 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c riable is a NULL
16590 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
165a0 54 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74 The "%z" formatt
165b0 69 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 ing option works
165c0 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25 exactly like "%
165d0 73 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 s" with the.** a
165e0 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74 ddition that aft
165f0 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61 er the string ha
16600 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20 s been read and
16610 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 copied into.** t
16620 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 he result, [sqli
16630 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63 te3_free()] is c
16640 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 alled on the inp
16650 75 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d ut string. {END}
16660 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
16670 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d nts:.** [H17403]
16680 20 5b 48 31 37 34 30 36 5d 20 5b 48 31 37 34 30 [H17406] [H1740
16690 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 7].*/.SQLITE_API
166a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d char *sqlite3_m
166b0 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 printf(const cha
166c0 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f r*,...);.SQLITE_
166d0 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
166e0 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 3_vmprintf(const
166f0 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 char*, va_list)
16700 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 ;.SQLITE_API cha
16710 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 r *sqlite3_snpri
16720 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f ntf(int,char*,co
16730 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
16740 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
16750 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 : Memory Allocat
16760 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48 ion Subsystem {H
16770 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 17300} <S20000>.
16780 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 **.** The SQLite
16790 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 73 core uses thes
167a0 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 e three routines
167b0 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 for all of its
167c0 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 own.** internal
167d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
167e0 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20 n needs. "Core"
167f0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 in the previous
16800 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 sentence.** does
16810 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 not include ope
16820 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 rating-system sp
16830 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65 ecific VFS imple
16840 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a mentation. The.
16850 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75 ** Windows VFS u
16860 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f ses native mallo
16870 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 c() and free() f
16880 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f or some operatio
16890 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ns..**.** The sq
168a0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 lite3_malloc() r
168b0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 outine returns a
168c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c pointer to a bl
168d0 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 ock.** of memory
168e0 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 at least N byte
168f0 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 s in length, whe
16900 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 61 re N is the para
16910 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c meter..** If sql
16920 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 ite3_malloc() is
16930 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 unable to obtai
16940 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 65 n sufficient fre
16950 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 e.** memory, it
16960 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 returns a NULL p
16970 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 ointer. If the
16980 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a parameter N to.*
16990 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 * sqlite3_malloc
169a0 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 () is zero or ne
169b0 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 gative then sqli
169c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 te3_malloc() ret
169d0 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 urns.** a NULL p
169e0 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 ointer..**.** Ca
169f0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 lling sqlite3_fr
16a00 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e ee() with a poin
16a10 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 ter previously r
16a20 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 eturned.** by sq
16a30 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f lite3_malloc() o
16a40 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f r sqlite3_reallo
16a50 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 c() releases tha
16a60 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 t memory so.** t
16a70 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 hat it might be
16a80 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c reused. The sql
16a90 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74 ite3_free() rout
16aa0 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f ine is.** a no-o
16ab0 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77 p if is called w
16ac0 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ith a NULL point
16ad0 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e er. Passing a N
16ae0 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 ULL pointer.** t
16af0 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 o sqlite3_free()
16b00 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 is harmless. A
16b10 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64 fter being freed
16b20 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 , memory.** shou
16b30 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65 ld neither be re
16b40 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 ad nor written.
16b50 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72 Even reading pr
16b60 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a eviously freed.*
16b70 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 * memory might r
16b80 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 esult in a segme
16b90 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 ntation fault or
16ba0 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72 other severe er
16bb0 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 ror..** Memory c
16bc0 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 orruption, a seg
16bd0 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c mentation fault,
16be0 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 or other severe
16bf0 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 error.** might
16c00 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 result if sqlite
16c10 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 3_free() is call
16c20 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 ed with a non-NU
16c30 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a LL pointer that.
16c40 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 ** was not obtai
16c50 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
16c60 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c _malloc() or sql
16c70 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a ite3_realloc()..
16c80 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
16c90 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 3_realloc() inte
16ca0 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 rface attempts t
16cb0 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 o resize a.** pr
16cc0 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ior memory alloc
16cd0 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c ation to be at l
16ce0 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68 east N bytes, wh
16cf0 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 ere N is the.**
16d00 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
16d10 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c . The memory al
16d20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 location to be r
16d30 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 69 esized is the fi
16d40 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 rst.** parameter
16d50 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74 20 . If the first
16d60 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
16d70 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a ite3_realloc().*
16d80 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e * is a NULL poin
16d90 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 68 ter then its beh
16da0 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63 avior is identic
16db0 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a al to calling.**
16dc0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
16dd0 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 N) where N is th
16de0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
16df0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 er to sqlite3_re
16e00 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 alloc()..** If t
16e10 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
16e20 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
16e30 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f ealloc() is zero
16e40 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 or.** negative
16e50 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f then the behavio
16e60 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 r is exactly the
16e70 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 same as calling
16e80 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 .** sqlite3_free
16e90 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74 (P) where P is t
16ea0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
16eb0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 er to sqlite3_re
16ec0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 alloc()..** sqli
16ed0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 te3_realloc() re
16ee0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
16ef0 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f to a memory allo
16f00 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 cation.** of at
16f10 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e least N bytes in
16f20 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 size or NULL if
16f30 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f sufficient memo
16f40 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c ry is unavailabl
16f50 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 e..** If M is th
16f60 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72 e size of the pr
16f70 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 ior allocation,
16f80 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 then min(N,M) by
16f90 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 tes.** of the pr
16fa0 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 ior allocation a
16fb0 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 re copied into t
16fc0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
16fd0 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a buffer returned.
16fe0 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65 ** by sqlite3_re
16ff0 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20 alloc() and the
17000 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e prior allocation
17010 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 is freed..** If
17020 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
17030 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c () returns NULL,
17040 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 then the prior
17050 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 allocation.** is
17060 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a not freed..**.*
17070 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 * The memory ret
17080 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
17090 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 _malloc() and sq
170a0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a lite3_realloc().
170b0 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69 ** is always ali
170c0 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74 gned to at least
170d0 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 an 8 byte bound
170e0 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a ary. {END}.**.**
170f0 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 The default imp
17100 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
17110 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
17120 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75 tion subsystem u
17130 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f ses.** the mallo
17140 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 c(), realloc() a
17150 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 64 nd free() provid
17160 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 ed by the standa
17170 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a rd C library..**
17180 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65 {H17382} Howeve
17190 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 r, if SQLite is
171a0 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
171b0 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f e.** SQLITE_MEMO
171c0 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f RY_SIZE=<i>NNN</
171d0 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f i> C preprocesso
171e0 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c r macro (where <
171f0 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 i>NNN</i>.** is
17200 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65 an integer), the
17210 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20 n SQLite create
17220 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20 6f a static array o
17230 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 f at least.** <i
17240 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 >NNN</i> bytes i
17250 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20 n size and uses
17260 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 61 that array for a
17270 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 ll of its dynami
17280 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f c.** memory allo
17290 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 cation needs. {E
172a0 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 ND} Additional
172b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
172c0 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 options.** may
172d0 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 be added in futu
172e0 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a re releases..**.
172f0 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72 ** In SQLite ver
17300 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 sion 3.5.0 and 3
17310 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73 .5.1, it was pos
17320 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a sible to define.
17330 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d ** the SQLITE_OM
17340 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 IT_MEMORY_ALLOCA
17350 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 TION which would
17360 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 74 cause the built
17370 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 -in.** implement
17380 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 ation of these r
17390 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d outines to be om
173a0 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 70 itted. That cap
173b0 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f ability.** is no
173c0 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64 longer provided
173d0 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e . Only built-in
173e0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
173f0 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a rs can be used..
17400 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 **.** The Window
17410 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c s OS interface l
17420 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 ayer calls.** th
17430 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 e system malloc(
17440 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72 ) and free() dir
17450 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 ectly when conve
17460 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d rting.** filenam
17470 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 55 es between the U
17480 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 TF-8 encoding us
17490 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 ed by SQLite.**
174a0 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 6c and whatever fil
174b0 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 ename encoding i
174c0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 s used by the pa
174d0 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 rticular Windows
174e0 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e .** installation
174f0 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 . Memory alloca
17500 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20 tion errors are
17510 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a detected, but.**
17520 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 74 they are report
17530 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 ed back as [SQLI
17540 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a TE_CANTOPEN] or.
17550 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 ** [SQLITE_IOERR
17560 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53 ] rather than [S
17570 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a QLITE_NOMEM]..**
17580 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
17590 3a 0a 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48 :.** [H17303] [H
175a0 31 37 33 30 34 5d 20 5b 48 31 37 33 30 35 5d 20 17304] [H17305]
175b0 5b 48 31 37 33 30 36 5d 20 5b 48 31 37 33 31 30 [H17306] [H17310
175c0 5d 20 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 33 ] [H17312] [H173
175d0 31 35 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20 15] [H17318].**
175e0 5b 48 31 37 33 32 31 5d 20 5b 48 31 37 33 32 32 [H17321] [H17322
175f0 5d 20 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a ] [H17323].**.**
17600 20 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 The pointer arg
17610 75 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 uments to [sqlit
17620 65 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b e3_free()] and [
17630 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
17640 29 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 )].** must be ei
17650 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 ther NULL or els
17660 65 20 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 e pointers obtai
17670 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 ned from a prior
17680 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f .** invocation o
17690 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f f [sqlite3_mallo
176a0 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 c()] or [sqlite3
176b0 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74 _realloc()] that
176c0 20 68 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74 have.** not yet
176d0 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a been released..
176e0 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 **.** The applic
176f0 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 ation must not r
17700 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 ead or write any
17710 20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c part of.** a bl
17720 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66 ock of memory af
17730 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 ter it has been
17740 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a 2a released using.*
17750 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 * [sqlite3_free(
17760 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 )] or [sqlite3_r
17770 65 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 ealloc()]..*/.SQ
17780 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
17790 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e qlite3_malloc(in
177a0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 t);.SQLITE_API v
177b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 oid *sqlite3_rea
177c0 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 lloc(void*, int)
177d0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
177e0 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 d sqlite3_free(v
177f0 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid*);../*.** CA
17800 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 PI3REF: Memory A
17810 6c 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74 llocator Statist
17820 69 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 ics {H17370} <S3
17830 30 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 0210>.**.** SQLi
17840 74 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73 te provides thes
17850 65 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73 e two interfaces
17860 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f for reporting o
17870 6e 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 n the status.**
17880 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
17890 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 malloc()], [sqli
178a0 74 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 te3_free()], and
178b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
178c0 63 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 c()].** routines
178d0 2c 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65 , which form the
178e0 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 built-in memory
178f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
17900 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 ystem..**.** Req
17910 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
17920 31 37 33 37 31 5d 20 5b 48 31 37 33 37 33 5d 20 17371] [H17373]
17930 5b 48 31 37 33 37 34 5d 20 5b 48 31 37 33 37 35 [H17374] [H17375
17940 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
17950 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
17960 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 lite3_memory_use
17970 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f d(void);.SQLITE_
17980 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 API sqlite3_int6
17990 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 4 sqlite3_memory
179a0 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 _highwater(int r
179b0 65 73 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a esetFlag);../*.*
179c0 2a 20 43 41 50 49 33 52 45 46 3a 20 50 73 65 75 * CAPI3REF: Pseu
179d0 64 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 do-Random Number
179e0 20 47 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 33 Generator {H173
179f0 39 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 90} <S20000>.**.
17a00 2a 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69 ** SQLite contai
17a10 6e 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74 ns a high-qualit
17a20 79 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 y pseudo-random
17a30 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
17a40 20 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0a (PRNG) used to.
17a50 2a 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d ** select random
17a60 20 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 73 [ROWID | ROWIDs
17a70 5d 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 ] when inserting
17a80 20 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74 new records int
17a90 6f 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a o a table that.*
17aa0 2a 20 61 6c 72 65 61 64 79 20 75 73 65 73 20 74 * already uses t
17ab0 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 he largest possi
17ac0 62 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68 ble [ROWID]. Th
17ad0 65 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 e PRNG is also u
17ae0 73 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 sed for.** the b
17af0 75 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 uild-in random()
17b00 20 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 and randomblob(
17b10 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e ) SQL functions.
17b20 20 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 This interface
17b30 20 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 allows.** appli
17b40 63 61 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 cations to acces
17b50 73 20 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20 s the same PRNG
17b60 66 6f 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73 for other purpos
17b70 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c es..**.** A call
17b80 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
17b90 20 73 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20 stores N bytes
17ba0 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e of randomness in
17bb0 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a to buffer P..**.
17bc0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d ** The first tim
17bd0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 e this routine i
17be0 73 20 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 s invoked (eithe
17bf0 72 20 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 r internally or
17c00 62 79 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 by.** the applic
17c10 61 74 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 ation) the PRNG
17c20 69 73 20 73 65 65 64 65 64 20 75 73 69 6e 67 20 is seeded using
17c30 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 randomness obtai
17c40 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ned.** from the
17c50 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 xRandomness meth
17c60 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c od of the defaul
17c70 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 t [sqlite3_vfs]
17c80 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c object..** On al
17c90 6c 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 l subsequent inv
17ca0 6f 63 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 ocations, the ps
17cb0 65 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 eudo-randomness
17cc0 69 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 is generated.**
17cd0 69 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 internally and w
17ce0 69 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20 ithout recourse
17cf0 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f to the [sqlite3_
17d00 76 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 vfs] xRandomness
17d10 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a .** method..**.*
17d20 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
17d30 2a 2a 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53 ** [H17392].*/.S
17d40 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
17d50 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
17d60 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 s(int N, void *P
17d70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
17d80 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 EF: Compile-Time
17d90 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 Authorization C
17da0 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30 allbacks {H12500
17db0 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S70100>.**.**
17dc0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
17dd0 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f 72 gisters a author
17de0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 izer callback wi
17df0 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a th a particular.
17e00 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
17e10 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 nection], suppli
17e20 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 ed in the first
17e30 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 argument..** The
17e40 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
17e50 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
17e60 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 as SQL statement
17e70 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 s are being comp
17e80 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 iled.** by [sqli
17e90 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f te3_prepare()] o
17ea0 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b r its variants [
17eb0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
17ec0 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 v2()],.** [sqlit
17ed0 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 e3_prepare16()]
17ee0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 and [sqlite3_pre
17ef0 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 pare16_v2()]. A
17f00 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 t various.** poi
17f10 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 63 nts during the c
17f20 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 ompilation proce
17f30 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20 ss, as logic is
17f40 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a being created.**
17f50 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 to perform vari
17f60 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 ous actions, the
17f70 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
17f80 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
17f90 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f to.** see if tho
17fa0 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61 se actions are a
17fb0 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74 llowed. The aut
17fc0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
17fd0 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 should.** retur
17fe0 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f n [SQLITE_OK] to
17ff0 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f allow the actio
18000 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 n, [SQLITE_IGNOR
18010 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 E] to disallow t
18020 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61 he.** specific a
18030 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 ction but allow
18040 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
18050 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f t to continue to
18060 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c be.** compiled,
18070 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 or [SQLITE_DENY
18080 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65 ] to cause the e
18090 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d ntire SQL statem
180a0 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a ent to be.** rej
180b0 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 ected with an er
180c0 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 74 ror. If the aut
180d0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
180e0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 returns.** any
180f0 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e value other than
18100 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
18110 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f , [SQLITE_OK], o
18120 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a r [SQLITE_DENY].
18130 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c ** then the [sql
18140 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
18150 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 )] or equivalent
18160 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 call that trigg
18170 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 ered.** the auth
18180 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c orizer will fail
18190 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d with an error m
181a0 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 essage..**.** Wh
181b0 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 en the callback
181c0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
181d0 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 OK], that means
181e0 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a the operation.**
181f0 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b requested is ok
18200 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c . When the call
18210 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 back returns [SQ
18220 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a LITE_DENY], the.
18230 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
18240 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 are_v2()] or equ
18250 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 ivalent call tha
18260 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 0a t triggered the.
18270 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 ** authorizer wi
18280 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 ll fail with an
18290 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78 error message ex
182a0 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a plaining that.**
182b0 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65 access is denie
182c0 64 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 6f d. If the autho
182d0 72 69 7a 65 72 20 63 6f 64 65 20 69 73 20 5b 53 rizer code is [S
182e0 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 61 QLITE_READ].** a
182f0 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 nd the callback
18300 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
18310 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65 IGNORE] then the
18320 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
18330 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d 65 atement] stateme
18340 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 nt is constructe
18350 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 0a d to substitute.
18360 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 ** a NULL value
18370 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 in place of the
18380 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 table column tha
18390 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 t would have.**
183a0 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 been read if [SQ
183b0 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 LITE_OK] had bee
183c0 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 n returned. The
183d0 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
183e0 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 62 .** return can b
183f0 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 61 e used to deny a
18400 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 72 n untrusted user
18410 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 76 access to indiv
18420 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 idual.** columns
18430 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 0a of a table..**.
18440 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 ** The first par
18450 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 ameter to the au
18460 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
18470 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 k is a copy of t
18480 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 he third.** para
18490 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c meter to the sql
184a0 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
184b0 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e zer() interface.
184c0 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 The second para
184d0 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 meter.** to the
184e0 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 callback is an i
184f0 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 nteger [SQLITE_C
18500 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 OPY | action cod
18510 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 65 e] that specifie
18520 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 75 s.** the particu
18530 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 lar action to be
18540 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 authorized. The
18550 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 73 third through s
18560 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 0a ixth parameters.
18570 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 ** to the callba
18580 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d ck are zero-term
18590 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 inated strings t
185a0 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 hat contain addi
185b0 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c tional.** detail
185c0 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 69 s about the acti
185d0 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 on to be authori
185e0 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75 zed..**.** An au
185f0 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 64 thorizer is used
18600 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 when [sqlite3_p
18610 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 69 repare | prepari
18620 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 ng].** SQL state
18630 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e ments from an un
18640 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c 20 trusted source,
18650 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 to ensure that t
18660 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
18670 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20 s.** do not try
18680 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 74 to access data t
18690 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f hey are not allo
186a0 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 74 wed to see, or t
186b0 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 0a hat they do not.
186c0 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 74 ** try to execut
186d0 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74 e malicious stat
186e0 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61 ements that dama
186f0 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ge the database.
18700 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 For.** example
18710 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e , an application
18720 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65 may allow a use
18730 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74 r to enter arbit
18740 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 72 rary.** SQL quer
18750 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69 ies for evaluati
18760 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65 on by a database
18770 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69 . But the appli
18780 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e cation does.** n
18790 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 72 ot want the user
187a0 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6d to be able to m
187b0 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 68 ake arbitrary ch
187c0 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 anges to the.**
187d0 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 75 database. An au
187e0 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 74 thorizer could t
187f0 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 6c hen be put in pl
18800 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a 2a ace while the.**
18810 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51 user-entered SQ
18820 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c 69 L is being [sqli
18830 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 te3_prepare | pr
18840 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a 20 epared] that.**
18850 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74 disallows everyt
18860 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 4c hing except [SEL
18870 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 2e ECT] statements.
18880 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 .**.** Applicati
18890 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f ons that need to
188a0 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 6f process SQL fro
188b0 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 m untrusted sour
188c0 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c 73 ces.** might als
188d0 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 72 o consider lower
188e0 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 6d ing resource lim
188f0 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 its using [sqlit
18900 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 61 e3_limit()].** a
18910 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 61 nd limiting data
18920 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 20 base size using
18930 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f the [max_page_co
18940 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a unt] [PRAGMA].**
18950 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 in addition to
18960 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 using an authori
18970 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 zer..**.** Only
18980 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69 a single authori
18990 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c zer can be in pl
189a0 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73 ace on a databas
189b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
189c0 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68 at a time. Each
189d0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
189e0 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 _set_authorizer
189f0 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a overrides the.**
18a00 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20 previous call.
18a10 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 74 Disable the aut
18a20 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 61 horizer by insta
18a30 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 6c lling a NULL cal
18a40 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 75 lback..** The au
18a50 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 61 thorizer is disa
18a60 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e bled by default.
18a70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
18a80 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d rizer callback m
18a90 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 ust not do anyth
18aa0 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f ing that will mo
18ab0 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 dify.** the data
18ac0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
18ad0 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 that invoked the
18ae0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
18af0 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 back..** Note th
18b00 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 at [sqlite3_prep
18b10 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 are_v2()] and [s
18b20 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 qlite3_step()] b
18b30 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 oth modify their
18b40 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
18b50 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 nections for the
18b60 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 meaning of "mod
18b70 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 ify" in this par
18b80 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68 agraph..**.** Wh
18b90 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 en [sqlite3_prep
18ba0 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 65 are_v2()] is use
18bb0 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 73 d to prepare a s
18bc0 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a tatement, the.**
18bd0 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 74 statement might
18be0 20 62 65 20 72 65 70 72 65 70 61 72 65 64 20 64 be reprepared d
18bf0 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 uring [sqlite3_s
18c00 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 20 tep()] due to a
18c10 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 .** schema chang
18c20 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 61 e. Hence, the a
18c30 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c pplication shoul
18c40 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 d ensure that th
18c50 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 74 e.** correct aut
18c60 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
18c70 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 63 remains in plac
18c80 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 71 e during the [sq
18c90 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a lite3_step()]..*
18ca0 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 *.** Note that t
18cb0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
18cc0 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 llback is invoke
18cd0 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a 2a d only during.**
18ce0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
18cf0 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 e()] or its vari
18d00 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a 61 ants. Authoriza
18d10 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 tion is not.** p
18d20 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 20 erformed during
18d30 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 61 statement evalua
18d40 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 33 tion in [sqlite3
18d50 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 _step()]..**.**
18d60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
18d70 20 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 35 30 [H12501] [H1250
18d80 32 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 32 2] [H12503] [H12
18d90 35 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 48 504] [H12505] [H
18da0 31 32 35 30 36 5d 20 5b 48 31 32 35 30 37 5d 20 12506] [H12507]
18db0 5b 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 32 [H12510].** [H12
18dc0 35 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 48 511] [H12512] [H
18dd0 31 32 35 32 30 5d 20 5b 48 31 32 35 32 31 5d 20 12520] [H12521]
18de0 5b 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c 49 [H12522].*/.SQLI
18df0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
18e00 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 e3_set_authorize
18e10 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20 r(. sqlite3*,.
18e20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f int (*xAuth)(vo
18e30 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 id*,int,const ch
18e40 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
18e50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
18e60 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 t char*),. void
18e70 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a *pUserData.);..
18e80 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
18e90 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72 Authorizer Retur
18ea0 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 39 30 7d n Codes {H12590}
18eb0 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 <H12500>.**.**
18ec0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 The [sqlite3_set
18ed0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 _authorizer | au
18ee0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
18ef0 6b 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74 k function] must
18f00 0a 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65 .** return eithe
18f10 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 r [SQLITE_OK] or
18f20 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77 one of these tw
18f30 6f 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f o constants in o
18f40 72 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61 rder.** to signa
18f50 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 72 l SQLite whether
18f60 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69 or not the acti
18f70 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e on is permitted.
18f80 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71 See the.** [sq
18f90 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
18fa0 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 izer | authorize
18fb0 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d r documentation]
18fc0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a for additional.
18fd0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a ** information..
18fe0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
18ff0 45 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 E_DENY 1 /*
19000 41 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 Abort the SQL st
19010 61 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 atement with an
19020 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 error */.#define
19030 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 SQLITE_IGNORE 2
19040 20 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f /* Don't allo
19050 77 20 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f w access, but do
19060 6e 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 n't generate an
19070 65 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 error */../*.**
19080 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 CAPI3REF: Author
19090 69 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 izer Action Code
190a0 73 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 35 s {H12550} <H125
190b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 00>.**.** The [s
190c0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
190d0 72 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61 rizer()] interfa
190e0 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63 ce registers a c
190f0 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
19100 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f .** that is invo
19110 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65 ked to authorize
19120 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61 certain SQL sta
19130 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20 tement actions.
19140 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 The.** second p
19150 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
19160 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 callback is an i
19170 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 nteger code that
19180 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 specifies.** wh
19190 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69 at action is bei
191a0 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 ng authorized.
191b0 54 68 65 73 65 20 61 72 65 20 74 68 65 20 69 6e These are the in
191c0 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64 teger action cod
191d0 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61 es that.** the a
191e0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
191f0 63 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 ck may be passed
19200 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63 ..**.** These ac
19210 74 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73 tion code values
19220 20 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69 signify what ki
19230 6e 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 nd of operation
19240 69 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 68 is to be.** auth
19250 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64 orized. The 3rd
19260 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74 and 4th paramet
19270 65 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f ers to the autho
19280 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c rization.** call
19290 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 back function wi
192a0 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 73 ll be parameters
192b0 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69 or NULL dependi
192c0 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74 ng on which of t
192d0 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73 hese.** codes is
192e0 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 used as the sec
192f0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 ond parameter.
19300 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 The 5th paramete
19310 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 r to the.** auth
19320 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
19330 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
19340 68 65 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 he database ("ma
19350 69 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20 in", "temp",.**
19360 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61 etc.) if applica
19370 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70 61 ble. The 6th pa
19380 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 rameter to the a
19390 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
193a0 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d ck.** is the nam
193b0 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d e of the inner-m
193c0 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76 ost trigger or v
193d0 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70 iew that is resp
193e0 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 onsible for.** t
193f0 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 he access attemp
19400 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69 t or NULL if thi
19410 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 s access attempt
19420 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f is directly fro
19430 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53 m.** top-level S
19440 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 52 QL code..**.** R
19450 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
19460 5b 48 31 32 35 35 31 5d 20 5b 48 31 32 35 35 32 [H12551] [H12552
19470 5d 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 35 ] [H12553] [H125
19480 35 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 54].*/./********
19490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
194b0 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a *** 3rd ********
194c0 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a **** 4th *******
194d0 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ****/.#define SQ
194e0 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 LITE_CREATE_INDE
194f0 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 2f X 1 /
19500 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 * Index Name
19510 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 Table Name
19520 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
19530 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 ITE_CREATE_TABLE
19540 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 2 /*
19550 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
19560 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
19570 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
19580 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 TE_CREATE_TEMP_I
19590 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20 NDEX 3 /*
195a0 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 Index Name
195b0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
195c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
195d0 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 E_CREATE_TEMP_TA
195e0 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54 BLE 4 /* T
195f0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e able Name N
19600 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
19610 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
19620 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 _CREATE_TEMP_TRI
19630 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 GGER 5 /* Tr
19640 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 igger Name Ta
19650 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
19660 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
19670 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 CREATE_TEMP_VIEW
19680 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 6 /* Vie
19690 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c w Name NUL
196a0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
196b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
196c0 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20 REATE_TRIGGER
196d0 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67 7 /* Trig
196e0 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c ger Name Tabl
196f0 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 e Name */.#
19700 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 define SQLITE_CR
19710 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 20 EATE_VIEW
19720 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20 8 /* View
19730 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
19740 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
19750 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c efine SQLITE_DEL
19760 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ETE
19770 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 9 /* Table
19780 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 Name NULL
19790 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
197a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
197b0 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 _INDEX
197c0 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 10 /* Index N
197d0 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e ame Table N
197e0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
197f0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
19800 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 TABLE
19810 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 11 /* Table Na
19820 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
19830 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
19840 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 ne SQLITE_DROP_T
19850 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31 EMP_INDEX 1
19860 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 2 /* Index Nam
19870 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d e Table Nam
19880 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
19890 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 e SQLITE_DROP_TE
198a0 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33 MP_TABLE 13
198b0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
198c0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
198d0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
198e0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
198f0 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 20 P_TRIGGER 14
19900 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d /* Trigger Nam
19910 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 e Table Name
19920 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
19930 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
19940 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 20 _VIEW 15
19950 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 /* View Name
19960 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
19970 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
19980 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 QLITE_DROP_TRIGG
19990 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 20 ER 16
199a0 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 /* Trigger Name
199b0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
199c0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
199d0 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20 LITE_DROP_VIEW
199e0 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 2f 17 /
199f0 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 * View Name
19a00 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
19a10 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
19a20 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20 ITE_INSERT
19a30 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a 18 /*
19a40 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
19a50 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
19a60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
19a70 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 TE_PRAGMA
19a80 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 19 /*
19a90 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20 Pragma Name
19aa0 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20 1st arg or NULL
19ab0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
19ac0 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 E_READ
19ad0 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54 20 /* T
19ae0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 able Name C
19af0 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a olumn Name *
19b00 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
19b10 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 _SELECT
19b20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 21 /* NU
19b30 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 LL NU
19b40 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
19b50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
19b60 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 TRANSACTION
19b70 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65 22 /* Ope
19b80 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c ration NUL
19b90 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
19ba0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 #define SQLITE_U
19bb0 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 PDATE
19bc0 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c 23 /* Tabl
19bd0 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 e Name Colu
19be0 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 mn Name */.#
19bf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54 define SQLITE_AT
19c00 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 TACH
19c10 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e 24 /* Filen
19c20 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 ame NULL
19c30 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
19c40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54 efine SQLITE_DET
19c50 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 ACH
19c60 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61 25 /* Databa
19c70 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20 se Name NULL
19c80 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
19c90 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45 fine SQLITE_ALTE
19ca0 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 R_TABLE
19cb0 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 26 /* Databas
19cc0 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e e Name Table N
19cd0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
19ce0 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 ine SQLITE_REIND
19cf0 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EX
19d00 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 27 /* Index Na
19d10 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
19d20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
19d30 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a ne SQLITE_ANALYZ
19d40 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 E 2
19d50 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 8 /* Table Nam
19d60 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
19d70 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
19d80 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f e SQLITE_CREATE_
19d90 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 39 VTABLE 29
19da0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
19db0 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d Module Nam
19dc0 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 e */.#define
19dd0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 SQLITE_DROP_VTA
19de0 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 20 BLE 30
19df0 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
19e00 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 Module Name
19e10 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
19e20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20 SQLITE_FUNCTION
19e30 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 20 31
19e40 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 /* NULL
19e50 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d Function Nam
19e60 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 e */.#define S
19e70 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 20 QLITE_SAVEPOINT
19e80 20 20 20 20 20 20 20 20 20 20 20 33 32 20 20 20 32
19e90 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 /* Operation
19ea0 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 6d Savepoint Nam
19eb0 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e */.#define SQ
19ec0 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20 LITE_COPY
19ed0 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 0 /
19ee0 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64 * No longer used
19ef0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 */../*.** CAPI3
19f00 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64 REF: Tracing And
19f10 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74 Profiling Funct
19f20 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c 53 ions {H12280} <S
19f30 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 60400>.** EXPERI
19f40 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 MENTAL.**.** The
19f50 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 69 se routines regi
19f60 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 ster callback fu
19f70 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e nctions that can
19f80 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 be used for.**
19f90 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66 tracing and prof
19fa0 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 iling the execut
19fb0 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65 ion of SQL state
19fc0 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ments..**.** The
19fd0 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
19fe0 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79 on registered by
19ff0 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 sqlite3_trace()
1a000 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a 2a is invoked at.*
1a010 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 20 * various times
1a020 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 when an SQL stat
1a030 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 72 ement is being r
1a040 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 un by [sqlite3_s
1a050 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 63 tep()]..** The c
1a060 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
1a070 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 6e a UTF-8 renderin
1a080 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 g of the SQL sta
1a090 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 61 tement text.** a
1a0a0 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 s the statement
1a0b0 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 65 first begins exe
1a0c0 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 6f cuting. Additio
1a0d0 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f 63 nal callbacks oc
1a0e0 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 74 cur.** as each t
1a0f0 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f 67 riggered subprog
1a100 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20 ram is entered.
1a110 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66 The callbacks f
1a120 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63 or triggers.** c
1a130 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53 ontain a UTF-8 S
1a140 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 QL comment that
1a150 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 74 identifies the t
1a160 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 rigger..**.** Th
1a170 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
1a180 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 ion registered b
1a190 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c y sqlite3_profil
1a1a0 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a e() is invoked.*
1a1b0 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 74 * as each SQL st
1a1c0 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73 atement finishes
1a1d0 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 63 . The profile c
1a1e0 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 allback contains
1a1f0 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c .** the original
1a200 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20 statement text
1a210 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 20 and an estimate
1a220 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69 of wall-clock ti
1a230 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f 6e me.** of how lon
1a240 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 g that statement
1a250 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a 2a took to run..**
1a260 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
1a270 3a 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b 48 :.** [H12281] [H
1a280 31 32 32 38 32 5d 20 5b 48 31 32 32 38 33 5d 20 12282] [H12283]
1a290 5b 48 31 32 32 38 34 5d 20 5b 48 31 32 32 38 35 [H12284] [H12285
1a2a0 5d 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 32 ] [H12287] [H122
1a2b0 38 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 20 88] [H12289].**
1a2c0 5b 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c 49 [H12290].*/.SQLI
1a2d0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
1a2e0 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 PERIMENTAL void
1a2f0 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 *sqlite3_trace(s
1a300 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 qlite3*, void(*x
1a310 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e Trace)(void*,con
1a320 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a st char*), void*
1a330 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 );.SQLITE_API SQ
1a340 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
1a350 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f L void *sqlite3_
1a360 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a profile(sqlite3*
1a370 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 ,. void(*xProf
1a380 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 ile)(void*,const
1a390 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 char*,sqlite3_u
1a3a0 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a int64), void*);.
1a3b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
1a3c0 20 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 Query Progress
1a3d0 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 Callbacks {H1291
1a3e0 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 0} <S60400>.**.*
1a3f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
1a400 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c onfigures a call
1a410 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 back function -
1a420 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 the.** progress
1a430 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 callback - that
1a440 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f is invoked perio
1a450 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c dically during l
1a460 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 ong.** running c
1a470 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
1a480 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 _exec()], [sqlit
1a490 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a e3_step()] and.*
1a4a0 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 * [sqlite3_get_t
1a4b0 61 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 able()]. An exa
1a4c0 6d 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 mple use for thi
1a4d0 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 s.** interface i
1a4e0 73 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 s to keep a GUI
1a4f0 75 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 updated during a
1a500 20 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a large query..**
1a510 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 .** If the progr
1a520 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 ess callback ret
1a530 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 urns non-zero, t
1a540 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a he operation is.
1a550 2a 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 ** interrupted.
1a560 20 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61 This feature ca
1a570 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 n be used to imp
1a580 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e lement a.** "Can
1a590 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 cel" button on a
1a5a0 20 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 GUI progress di
1a5b0 61 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 alog box..**.**
1a5c0 54 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e The progress han
1a5d0 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f dler must not do
1a5e0 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 anything that w
1a5f0 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 ill modify.** th
1a600 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1a610 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b ction that invok
1a620 65 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 ed the progress
1a630 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 handler..** Note
1a640 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 that [sqlite3_p
1a650 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 repare_v2()] and
1a660 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1a670 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 ] both modify th
1a680 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 eir.** database
1a690 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 connections for
1a6a0 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 the meaning of "
1a6b0 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 modify" in this
1a6c0 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a paragraph..**.**
1a6d0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
1a6e0 2a 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 39 * [H12911] [H129
1a6f0 31 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 31 12] [H12913] [H1
1a700 32 39 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 5b 2914] [H12915] [
1a710 48 31 32 39 31 36 5d 20 5b 48 31 32 39 31 37 5d H12916] [H12917]
1a720 20 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f 0a [H12918].**.*/.
1a730 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1a740 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 sqlite3_progress
1a750 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 _handler(sqlite3
1a760 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76 *, int, int(*)(v
1a770 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a oid*), void*);..
1a780 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1a790 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 Opening A New Da
1a7a0 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f tabase Connectio
1a7b0 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32 n {H12700} <S402
1a7c0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
1a7d0 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e routines open an
1a7e0 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 SQLite database
1a7f0 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65 file whose name
1a800 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 is given by the
1a810 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 .** filename arg
1a820 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e ument. The filen
1a830 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ame argument is
1a840 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55 interpreted as U
1a850 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 TF-8 for.** sqli
1a860 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 te3_open() and s
1a870 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
1a880 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69 and as UTF-16 i
1a890 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 n the native byt
1a8a0 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 e.** order for s
1a8b0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e qlite3_open16().
1a8c0 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e A [database con
1a8d0 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 nection] handle
1a8e0 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65 is usually.** re
1a8f0 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c turned in *ppDb,
1a900 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f even if an erro
1a910 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f r occurs. The o
1a920 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73 nly exception is
1a930 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69 that.** if SQLi
1a940 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 te is unable to
1a950 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 allocate memory
1a960 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c to hold the [sql
1a970 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a ite3] object,.**
1a980 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20 a NULL will be
1a990 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70 written into *pp
1a9a0 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 Db instead of a
1a9b0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b pointer to the [
1a9c0 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65 sqlite3].** obje
1a9d0 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 ct. If the datab
1a9e0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61 ase is opened (a
1a9f0 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73 nd/or created) s
1aa00 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65 uccessfully, the
1aa10 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d n.** [SQLITE_OK]
1aa20 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f is returned. O
1aa30 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 therwise an [err
1aa40 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
1aa50 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 rned. The.** [s
1aa60 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d qlite3_errmsg()]
1aa70 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 or [sqlite3_err
1aa80 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 msg16()] routine
1aa90 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f s can be used to
1aaa0 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e obtain.** an En
1aab0 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64 glish language d
1aac0 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 escription of th
1aad0 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 e error..**.** T
1aae0 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 he default encod
1aaf0 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 ing for the data
1ab00 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46 base will be UTF
1ab10 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 -8 if.** sqlite3
1ab20 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 _open() or sqlit
1ab30 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 e3_open_v2() is
1ab40 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 called and.** UT
1ab50 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 F-16 in the nati
1ab60 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 ve byte order if
1ab70 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
1ab80 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a ) is used..**.**
1ab90 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 Whether or not
1aba0 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
1abb0 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 when it is opene
1abc0 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 d, resources.**
1abd0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1abe0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
1abf0 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 nnection] handle
1ac00 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 should be relea
1ac10 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e sed by.** passin
1ac20 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 g it to [sqlite3
1ac30 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69 _close()] when i
1ac40 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 t is no longer r
1ac50 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 equired..**.** T
1ac60 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f he sqlite3_open_
1ac70 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77 v2() interface w
1ac80 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 orks like sqlite
1ac90 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65 3_open().** exce
1aca0 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70 pt that it accep
1acb0 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 ts two additiona
1acc0 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 l parameters for
1acd0 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 additional cont
1ace0 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 rol.** over the
1acf0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e new database con
1ad00 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c nection. The fl
1ad10 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 ags parameter ca
1ad20 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a n take one of.**
1ad30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
1ad40 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74 hree values, opt
1ad50 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64 ionally combined
1ad60 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53 with the .** [S
1ad70 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 QLITE_OPEN_NOMUT
1ad80 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4f EX] or [SQLITE_O
1ad90 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 PEN_FULLMUTEX] f
1ada0 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e lags:.**.** <dl>
1adb0 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f .** <dt>[SQLITE_
1adc0 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f OPEN_READONLY]</
1add0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 dt>.** <dd>The d
1ade0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 atabase is opene
1adf0 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d d in read-only m
1ae00 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 ode. If the dat
1ae10 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a abase does not.*
1ae20 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c * already exist,
1ae30 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 an error is ret
1ae40 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a urned.</dd>.**.*
1ae50 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 * <dt>[SQLITE_OP
1ae60 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 EN_READWRITE]</d
1ae70 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 t>.** <dd>The da
1ae80 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
1ae90 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 for reading and
1aea0 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73 writing if poss
1aeb0 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 ible, or reading
1aec0 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 .** only if the
1aed0 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 file is write pr
1aee0 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f otected by the o
1aef0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e perating system.
1af00 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 In either.** c
1af10 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ase the database
1af20 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 must already ex
1af30 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61 ist, otherwise a
1af40 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
1af50 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
1af60 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e <dt>[SQLITE_OPEN
1af70 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 _READWRITE] | [S
1af80 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
1af90 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 E]</dt>.** <dd>T
1afa0 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f he database is o
1afb0 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e pened for readin
1afc0 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 g and writing, a
1afd0 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74 nd is creates it
1afe0 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e if.** it does n
1aff0 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ot already exist
1b000 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65 . This is the be
1b010 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 havior that is a
1b020 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a lways used for.*
1b030 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 * sqlite3_open()
1b040 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 and sqlite3_ope
1b050 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c n16().</dd>.** <
1b060 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 /dl>.**.** If th
1b070 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 e 3rd parameter
1b080 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f to sqlite3_open_
1b090 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 v2() is not one
1b0a0 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e of the.** combin
1b0b0 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f ations shown abo
1b0c0 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 ve or one of the
1b0d0 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 combinations sh
1b0e0 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e own above combin
1b0f0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b ed.** with the [
1b100 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 SQLITE_OPEN_NOMU
1b110 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f TEX] or [SQLITE_
1b120 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 OPEN_FULLMUTEX]
1b130 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 flags,.** then t
1b140 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
1b150 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
1b160 49 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f If the [SQLITE_O
1b170 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 PEN_NOMUTEX] fla
1b180 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 g is set, then t
1b190 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1b1a0 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 ection.** opens
1b1b0 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 in the multi-thr
1b1c0 65 61 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d ead [threading m
1b1d0 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20 ode] as long as
1b1e0 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 the single-threa
1b1f0 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f d.** mode has no
1b200 74 20 62 65 65 6e 20 73 65 74 20 61 74 20 63 6f t been set at co
1b210 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 mpile-time or st
1b220 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 68 art-time. If th
1b230 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 e.** [SQLITE_OPE
1b240 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 N_FULLMUTEX] fla
1b250 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 74 68 g is set then th
1b260 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1b270 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 ction opens.** i
1b280 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 n the serialized
1b290 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
1b2a0 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d ] unless single-
1b2b0 74 68 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 72 thread was.** pr
1b2c0 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 65 eviously selecte
1b2d0 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d d at compile-tim
1b2e0 65 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e e or start-time.
1b2f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
1b300 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f lename is ":memo
1b310 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 69 ry:", then a pri
1b320 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 20 vate, temporary
1b330 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
1b340 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 64 se.** is created
1b350 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 for the connect
1b360 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d 65 ion. This in-me
1b370 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69 mory database wi
1b380 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a 2a ll vanish when.*
1b390 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 * the database c
1b3a0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f onnection is clo
1b3b0 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72 sed. Future ver
1b3c0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
1b3d0 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 73 might.** make us
1b3e0 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 e of additional
1b3f0 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65 special filename
1b400 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 s that begin wit
1b410 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63 h the ":" charac
1b420 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 65 ter..** It is re
1b430 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 77 commended that w
1b440 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 hen a database f
1b450 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c 79 ilename actually
1b460 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 68 does begin with
1b470 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 63 .** a ":" charac
1b480 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 70 ter you should p
1b490 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61 refix the filena
1b4a0 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e 61 me with a pathna
1b4b0 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 2e me such as.** ".
1b4c0 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 69 /" to avoid ambi
1b4d0 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 guity..**.** If
1b4e0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 the filename is
1b4f0 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c an empty string,
1b500 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c then a private,
1b510 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f 6e temporary.** on
1b520 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77 -disk database w
1b530 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 20 ill be created.
1b540 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 61 This private da
1b550 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a 2a tabase will be.*
1b560 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
1b570 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e 20 deleted as soon
1b580 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 as the database
1b590 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c connection is cl
1b5a0 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 osed..**.** The
1b5b0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
1b5c0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e to sqlite3_open
1b5d0 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 6d _v2() is the nam
1b5e0 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c e of the.** [sql
1b5f0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
1b600 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
1b610 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
1b620 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 em interface tha
1b630 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 t.** the new dat
1b640 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1b650 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 66 should use. If
1b660 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 the fourth para
1b670 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e 55 meter is.** a NU
1b680 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 LL pointer then
1b690 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c the default [sql
1b6a0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
1b6b0 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 is used..**.**
1b6c0 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 6f <b>Note to Windo
1b6d0 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54 ws users:</b> T
1b6e0 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 he encoding used
1b6f0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d for the filenam
1b700 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66 e argument.** of
1b710 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 sqlite3_open()
1b720 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e and sqlite3_open
1b730 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 54 _v2() must be UT
1b740 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65 F-8, not whateve
1b750 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73 r.** codepage is
1b760 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e currently defin
1b770 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63 ed. Filenames c
1b780 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e ontaining intern
1b790 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61 ational.** chara
1b7a0 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f cters must be co
1b7b0 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38 nverted to UTF-8
1b7c0 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e prior to passin
1b7d0 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 73 g them into.** s
1b7e0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72 qlite3_open() or
1b7f0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
1b800 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ()..**.** Requir
1b810 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37 ements:.** [H127
1b820 30 31 5d 20 5b 48 31 32 37 30 32 5d 20 5b 48 31 01] [H12702] [H1
1b830 32 37 30 33 5d 20 5b 48 31 32 37 30 34 5d 20 5b 2703] [H12704] [
1b840 48 31 32 37 30 36 5d 20 5b 48 31 32 37 30 37 5d H12706] [H12707]
1b850 20 5b 48 31 32 37 30 39 5d 20 5b 48 31 32 37 31 [H12709] [H1271
1b860 31 5d 0a 2a 2a 20 5b 48 31 32 37 31 32 5d 20 5b 1].** [H12712] [
1b870 48 31 32 37 31 33 5d 20 5b 48 31 32 37 31 34 5d H12713] [H12714]
1b880 20 5b 48 31 32 37 31 37 5d 20 5b 48 31 32 37 31 [H12717] [H1271
1b890 39 5d 20 5b 48 31 32 37 32 31 5d 20 5b 48 31 32 9] [H12721] [H12
1b8a0 37 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 723].*/.SQLITE_A
1b8b0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
1b8c0 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 pen(. const cha
1b8d0 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f r *filename, /
1b8e0 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e * Database filen
1b8f0 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 ame (UTF-8) */.
1b900 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 sqlite3 **ppDb
1b910 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
1b920 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c SQLite db handl
1b930 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 e */.);.SQLITE_A
1b940 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
1b950 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 76 pen16(. const v
1b960 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 oid *filename,
1b970 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c /* Database fil
1b980 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 20 2a ename (UTF-16) *
1b990 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 /. sqlite3 **pp
1b9a0 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f Db /* O
1b9b0 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 UT: SQLite db ha
1b9c0 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 ndle */.);.SQLIT
1b9d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b9e0 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 3_open_v2(. con
1b9f0 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d st char *filenam
1ba00 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 e, /* Database
1ba10 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 filename (UTF-8
1ba20 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a ) */. sqlite3 *
1ba30 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20 2f *ppDb, /
1ba40 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 * OUT: SQLite db
1ba50 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 handle */. int
1ba60 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
1ba70 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f /* Flags */
1ba80 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1ba90 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 Vfs /* Na
1baa0 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65 me of VFS module
1bab0 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a 2f to use */.);../
1bac0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
1bad0 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 4d rror Codes And M
1bae0 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 30 7d essages {H12800}
1baf0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S60200>.**.**
1bb00 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 63 The sqlite3_errc
1bb10 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 ode() interface
1bb20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 65 returns the nume
1bb30 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 ric [result code
1bb40 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 ] or.** [extende
1bb50 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 66 d result code] f
1bb60 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 or the most rece
1bb70 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74 65 nt failed sqlite
1bb80 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a 2a 20 3_* API call.**
1bb90 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1bba0 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a [database conn
1bbb0 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 70 72 ection]. If a pr
1bbc0 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61 69 ior API call fai
1bbd0 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 6d led.** but the m
1bbe0 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 20 63 ost recent API c
1bbf0 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20 74 all succeeded, t
1bc00 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
1bc10 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f from.** sqlite3_
1bc20 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 6e 64 errcode() is und
1bc30 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 71 6c efined. The sql
1bc40 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 ite3_extended_er
1bc50 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65 72 rcode().** inter
1bc60 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d 65 face is the same
1bc70 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 except that it
1bc80 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 74 always returns t
1bc90 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 he .** [extended
1bca0 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 65 76 result code] ev
1bcb0 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 65 64 en when extended
1bcc0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 result codes ar
1bcd0 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a 2a e.** disabled..*
1bce0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
1bcf0 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 _errmsg() and sq
1bd00 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 lite3_errmsg16()
1bd10 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d return English-
1bd20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78 74 language.** text
1bd30 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
1bd40 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65 69 the error, as ei
1bd50 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 ther UTF-8 or UT
1bd60 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 65 6c F-16 respectivel
1bd70 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f 20 y..** Memory to
1bd80 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6d hold the error m
1bd90 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 73 essage string is
1bda0 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e 61 managed interna
1bdb0 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c lly..** The appl
1bdc0 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 ication does not
1bdd0 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 need to worry a
1bde0 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68 65 bout freeing the
1bdf0 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77 65 result..** Howe
1be00 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 73 ver, the error s
1be10 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6f tring might be o
1be20 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64 65 verwritten or de
1be30 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 allocated by.**
1be40 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1be50 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65 to other SQLite
1be60 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 74 interface funct
1be70 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e ions..**.** When
1be80 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 the serialized
1be90 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d [threading mode]
1bea0 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 20 6d is in use, it m
1beb0 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a 20 63 ight be the.** c
1bec0 61 73 65 20 74 68 61 74 20 61 20 73 65 63 6f 6e ase that a secon
1bed0 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 6f d error occurs o
1bee0 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 n a separate thr
1bef0 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e 0a 2a ead in between.*
1bf00 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 74 68 * the time of th
1bf10 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 61 6e e first error an
1bf20 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 d the call to th
1bf30 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 2e 0a ese interfaces..
1bf40 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 ** When that hap
1bf50 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e 64 pens, the second
1bf60 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 error will be r
1bf70 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74 68 eported since th
1bf80 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 ese.** interface
1bf90 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74 20 s always report
1bfa0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
1bfb0 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f 69 result. To avoi
1bfc0 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68 20 d.** this, each
1bfd0 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61 69 thread can obtai
1bfe0 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65 20 n exclusive use
1bff0 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 of the [database
1c000 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a 2a connection] D.*
1c010 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 * by invoking [s
1c020 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1c030 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f er]([sqlite3_db_
1c040 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 6f 72 mutex](D)) befor
1c050 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 74 e beginning.** t
1c060 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e 76 6f o use D and invo
1c070 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 king [sqlite3_mu
1c080 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 6c 69 tex_leave]([sqli
1c090 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 29 te3_db_mutex](D)
1c0a0 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c 20 63 ) after.** all c
1c0b0 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e 74 65 alls to the inte
1c0c0 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 68 65 rfaces listed he
1c0d0 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 65 64 re are completed
1c0e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e ..**.** If an in
1c0f0 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77 69 terface fails wi
1c100 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 th SQLITE_MISUSE
1c110 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 , that means the
1c120 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 77 61 interface.** wa
1c130 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f 72 72 s invoked incorr
1c140 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 70 70 ectly by the app
1c150 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 74 68 lication. In th
1c160 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a 2a 20 at case, the.**
1c170 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d error code and m
1c180 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 6d 61 essage may or ma
1c190 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2a y not be set..**
1c1a0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
1c1b0 3a 0a 2a 2a 20 5b 48 31 32 38 30 31 5d 20 5b 48 :.** [H12801] [H
1c1c0 31 32 38 30 32 5d 20 5b 48 31 32 38 30 33 5d 20 12802] [H12803]
1c1d0 5b 48 31 32 38 30 37 5d 20 5b 48 31 32 38 30 38 [H12807] [H12808
1c1e0 5d 20 5b 48 31 32 38 30 39 5d 0a 2a 2f 0a 53 51 ] [H12809].*/.SQ
1c1f0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1c200 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73 71 6c ite3_errcode(sql
1c210 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 ite3 *db);.SQLIT
1c220 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1c230 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 3_extended_errco
1c240 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b de(sqlite3 *db);
1c250 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1c260 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
1c270 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a 29 errmsg(sqlite3*)
1c280 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e ;.SQLITE_API con
1c290 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1c2a0 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65 _errmsg16(sqlite
1c2b0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 3*);../*.** CAPI
1c2c0 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74 65 6d 3REF: SQL Statem
1c2d0 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 30 ent Object {H130
1c2e0 30 30 7d 20 3c 48 31 33 30 31 30 3e 0a 2a 2a 20 00} <H13010>.**
1c2f0 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 65 70 61 KEYWORDS: {prepa
1c300 72 65 64 20 73 74 61 74 65 6d 65 6e 74 7d 20 7b red statement} {
1c310 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1c320 6e 74 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e nts}.**.** An in
1c330 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
1c340 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 bject represents
1c350 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 a single SQL st
1c360 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 69 73 atement..** This
1c370 20 6f 62 6a 65 63 74 20 69 73 20 76 61 72 69 6f object is vario
1c380 75 73 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 61 20 usly known as a
1c390 22 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d "prepared statem
1c3a0 65 6e 74 22 20 6f 72 20 61 0a 2a 2a 20 22 63 6f ent" or a.** "co
1c3b0 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 mpiled SQL state
1c3c0 6d 65 6e 74 22 20 6f 72 20 73 69 6d 70 6c 79 20 ment" or simply
1c3d0 61 73 20 61 20 22 73 74 61 74 65 6d 65 6e 74 22 as a "statement"
1c3e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 66 65 ..**.** The life
1c3f0 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 of a statement
1c400 6f 62 6a 65 63 74 20 67 6f 65 73 20 73 6f 6d 65 object goes some
1c410 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a thing like this:
1c420 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c .**.** <ol>.** <
1c430 6c 69 3e 20 43 72 65 61 74 65 20 74 68 65 20 6f li> Create the o
1c440 62 6a 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c bject using [sql
1c450 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
1c460 29 5d 20 6f 72 20 61 20 72 65 6c 61 74 65 64 0a )] or a related.
1c470 2a 2a 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e ** function
1c480 2e 0a 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64 20 76 ..** <li> Bind v
1c490 61 6c 75 65 73 20 74 6f 20 5b 68 6f 73 74 20 70 alues to [host p
1c4a0 61 72 61 6d 65 74 65 72 73 5d 20 75 73 69 6e 67 arameters] using
1c4b0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e the sqlite3_bin
1c4c0 64 5f 2a 28 29 0a 2a 2a 20 20 20 20 20 20 69 6e d_*().** in
1c4d0 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 3c 6c 69 terfaces..** <li
1c4e0 3e 20 52 75 6e 20 74 68 65 20 53 51 4c 20 62 79 > Run the SQL by
1c4f0 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 calling [sqlite
1c500 33 5f 73 74 65 70 28 29 5d 20 6f 6e 65 20 6f 72 3_step()] one or
1c510 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 more times..**
1c520 3c 6c 69 3e 20 52 65 73 65 74 20 74 68 65 20 73 <li> Reset the s
1c530 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b tatement using [
1c540 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
1c550 20 74 68 65 6e 20 67 6f 20 62 61 63 6b 0a 2a 2a then go back.**
1c560 20 20 20 20 20 20 74 6f 20 73 74 65 70 20 32 2e to step 2.
1c570 20 20 44 6f 20 74 68 69 73 20 7a 65 72 6f 20 6f Do this zero o
1c580 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a r more times..**
1c590 20 3c 6c 69 3e 20 44 65 73 74 72 6f 79 20 74 68 <li> Destroy th
1c5a0 65 20 6f 62 6a 65 63 74 20 75 73 69 6e 67 20 5b e object using [
1c5b0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
1c5c0 28 29 5d 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a ()]..** </ol>.**
1c5d0 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 64 6f 63 .** Refer to doc
1c5e0 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 69 6e umentation on in
1c5f0 64 69 76 69 64 75 61 6c 20 6d 65 74 68 6f 64 73 dividual methods
1c600 20 61 62 6f 76 65 20 66 6f 72 20 61 64 64 69 74 above for addit
1c610 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 ional.** informa
1c620 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
1c630 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
1c640 73 74 6d 74 20 73 71 6c 69 74 65 33 5f 73 74 6d stmt sqlite3_stm
1c650 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 t;../*.** CAPI3R
1c660 45 46 3a 20 52 75 6e 2d 74 69 6d 65 20 4c 69 6d EF: Run-time Lim
1c670 69 74 73 20 7b 48 31 32 37 36 30 7d 20 3c 53 32 its {H12760} <S2
1c680 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0600>.**.** This
1c690 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77 interface allow
1c6a0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 76 61 s the size of va
1c6b0 72 69 6f 75 73 20 63 6f 6e 73 74 72 75 63 74 73 rious constructs
1c6c0 20 74 6f 20 62 65 20 6c 69 6d 69 74 65 64 0a 2a to be limited.*
1c6d0 2a 20 6f 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f * on a connectio
1c6e0 6e 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 n by connection
1c6f0 62 61 73 69 73 2e 20 20 54 68 65 20 66 69 72 73 basis. The firs
1c700 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 t parameter is t
1c710 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 he.** [database
1c720 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 68 6f 73 connection] whos
1c730 65 20 6c 69 6d 69 74 20 69 73 20 74 6f 20 62 65 e limit is to be
1c740 20 73 65 74 20 6f 72 20 71 75 65 72 69 65 64 2e set or queried.
1c750 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 The.** second
1c760 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 65 parameter is one
1c770 20 6f 66 20 74 68 65 20 5b 6c 69 6d 69 74 20 63 of the [limit c
1c780 61 74 65 67 6f 72 69 65 73 5d 20 74 68 61 74 20 ategories] that
1c790 64 65 66 69 6e 65 20 61 0a 2a 2a 20 63 6c 61 73 define a.** clas
1c7a0 73 20 6f 66 20 63 6f 6e 73 74 72 75 63 74 73 20 s of constructs
1c7b0 74 6f 20 62 65 20 73 69 7a 65 20 6c 69 6d 69 74 to be size limit
1c7c0 65 64 2e 20 20 54 68 65 20 74 68 69 72 64 20 70 ed. The third p
1c7d0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a arameter is the.
1c7e0 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 66 6f 72 ** new limit for
1c7f0 20 74 68 61 74 20 63 6f 6e 73 74 72 75 63 74 2e that construct.
1c800 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 The function r
1c810 65 74 75 72 6e 73 20 74 68 65 20 6f 6c 64 20 6c eturns the old l
1c820 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 imit..**.** If t
1c830 68 65 20 6e 65 77 20 6c 69 6d 69 74 20 69 73 20 he new limit is
1c840 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 a negative numbe
1c850 72 2c 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20 r, the limit is
1c860 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 46 6f unchanged..** Fo
1c870 72 20 74 68 65 20 6c 69 6d 69 74 20 63 61 74 65 r the limit cate
1c880 67 6f 72 79 20 6f 66 20 53 51 4c 49 54 45 5f 4c gory of SQLITE_L
1c890 49 4d 49 54 5f 58 59 5a 20 74 68 65 72 65 20 69 IMIT_XYZ there i
1c8a0 73 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 s a .** [limits
1c8b0 7c 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 | hard upper bou
1c8c0 6e 64 5d 0a 2a 2a 20 73 65 74 20 62 79 20 61 20 nd].** set by a
1c8d0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 43 20 70 compile-time C p
1c8e0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 reprocessor macr
1c8f0 6f 20 6e 61 6d 65 64 20 0a 2a 2a 20 5b 6c 69 6d o named .** [lim
1c900 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d 41 58 its | SQLITE_MAX
1c910 5f 58 59 5a 5d 2e 0a 2a 2a 20 28 54 68 65 20 22 _XYZ]..** (The "
1c920 5f 4c 49 4d 49 54 5f 22 20 69 6e 20 74 68 65 20 _LIMIT_" in the
1c930 6e 61 6d 65 20 69 73 20 63 68 61 6e 67 65 64 20 name is changed
1c940 74 6f 20 22 5f 4d 41 58 5f 22 2e 29 0a 2a 2a 20 to "_MAX_".).**
1c950 41 74 74 65 6d 70 74 73 20 74 6f 20 69 6e 63 72 Attempts to incr
1c960 65 61 73 65 20 61 20 6c 69 6d 69 74 20 61 62 6f ease a limit abo
1c970 76 65 20 69 74 73 20 68 61 72 64 20 75 70 70 65 ve its hard uppe
1c980 72 20 62 6f 75 6e 64 20 61 72 65 0a 2a 2a 20 73 r bound are.** s
1c990 69 6c 65 6e 74 6c 79 20 74 72 75 6e 63 61 74 65 ilently truncate
1c9a0 64 20 74 6f 20 74 68 65 20 68 61 72 64 20 75 70 d to the hard up
1c9b0 70 65 72 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a per limit..**.**
1c9c0 20 52 75 6e 20 74 69 6d 65 20 6c 69 6d 69 74 73 Run time limits
1c9d0 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f are intended fo
1c9e0 72 20 75 73 65 20 69 6e 20 61 70 70 6c 69 63 61 r use in applica
1c9f0 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6e 61 67 tions that manag
1ca00 65 0a 2a 2a 20 62 6f 74 68 20 74 68 65 69 72 20 e.** both their
1ca10 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 own internal dat
1ca20 61 62 61 73 65 20 61 6e 64 20 61 6c 73 6f 20 64 abase and also d
1ca30 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61 72 atabases that ar
1ca40 65 20 63 6f 6e 74 72 6f 6c 6c 65 64 0a 2a 2a 20 e controlled.**
1ca50 62 79 20 75 6e 74 72 75 73 74 65 64 20 65 78 74 by untrusted ext
1ca60 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 2e 20 20 ernal sources.
1ca70 41 6e 20 65 78 61 6d 70 6c 65 20 61 70 70 6c 69 An example appli
1ca80 63 61 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 cation might be
1ca90 61 0a 2a 2a 20 77 65 62 20 62 72 6f 77 73 65 72 a.** web browser
1caa0 20 74 68 61 74 20 68 61 73 20 69 74 73 20 6f 77 that has its ow
1cab0 6e 20 64 61 74 61 62 61 73 65 73 20 66 6f 72 20 n databases for
1cac0 73 74 6f 72 69 6e 67 20 68 69 73 74 6f 72 79 20 storing history
1cad0 61 6e 64 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 and.** separate
1cae0 64 61 74 61 62 61 73 65 73 20 63 6f 6e 74 72 6f databases contro
1caf0 6c 6c 65 64 20 62 79 20 4a 61 76 61 53 63 72 69 lled by JavaScri
1cb00 70 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 pt applications
1cb10 64 6f 77 6e 6c 6f 61 64 65 64 0a 2a 2a 20 6f 66 downloaded.** of
1cb20 66 20 74 68 65 20 49 6e 74 65 72 6e 65 74 2e 20 f the Internet.
1cb30 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 The internal da
1cb40 74 61 62 61 73 65 73 20 63 61 6e 20 62 65 20 67 tabases can be g
1cb50 69 76 65 6e 20 74 68 65 0a 2a 2a 20 6c 61 72 67 iven the.** larg
1cb60 65 2c 20 64 65 66 61 75 6c 74 20 6c 69 6d 69 74 e, default limit
1cb70 73 2e 20 20 44 61 74 61 62 61 73 65 73 20 6d 61 s. Databases ma
1cb80 6e 61 67 65 64 20 62 79 20 65 78 74 65 72 6e 61 naged by externa
1cb90 6c 20 73 6f 75 72 63 65 73 20 63 61 6e 0a 2a 2a l sources can.**
1cba0 20 62 65 20 67 69 76 65 6e 20 6d 75 63 68 20 73 be given much s
1cbb0 6d 61 6c 6c 65 72 20 6c 69 6d 69 74 73 20 64 65 maller limits de
1cbc0 73 69 67 6e 65 64 20 74 6f 20 70 72 65 76 65 6e signed to preven
1cbd0 74 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 t a denial of se
1cbe0 72 76 69 63 65 0a 2a 2a 20 61 74 74 61 63 6b 2e rvice.** attack.
1cbf0 20 20 44 65 76 65 6c 6f 70 65 72 73 20 6d 69 67 Developers mig
1cc00 68 74 20 61 6c 73 6f 20 77 61 6e 74 20 74 6f 20 ht also want to
1cc10 75 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33 use the [sqlite3
1cc20 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 _set_authorizer(
1cc30 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 )].** interface
1cc40 74 6f 20 66 75 72 74 68 65 72 20 63 6f 6e 74 72 to further contr
1cc50 6f 6c 20 75 6e 74 72 75 73 74 65 64 20 53 51 4c ol untrusted SQL
1cc60 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 . The size of t
1cc70 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 he database.** c
1cc80 72 65 61 74 65 64 20 62 79 20 61 6e 20 75 6e 74 reated by an unt
1cc90 72 75 73 74 65 64 20 73 63 72 69 70 74 20 63 61 rusted script ca
1cca0 6e 20 62 65 20 63 6f 6e 74 61 69 6e 65 64 20 75 n be contained u
1ccb0 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 6d 61 78 sing the.** [max
1ccc0 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52 _page_count] [PR
1ccd0 41 47 4d 41 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 AGMA]..**.** New
1cce0 20 72 75 6e 2d 74 69 6d 65 20 6c 69 6d 69 74 20 run-time limit
1ccf0 63 61 74 65 67 6f 72 69 65 73 20 6d 61 79 20 62 categories may b
1cd00 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 e added in futur
1cd10 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a e releases..**.*
1cd20 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
1cd30 2a 2a 20 5b 48 31 32 37 36 32 5d 20 5b 48 31 32 ** [H12762] [H12
1cd40 37 36 36 5d 20 5b 48 31 32 37 36 39 5d 0a 2a 2f 766] [H12769].*/
1cd50 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1cd60 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 sqlite3_limit(sq
1cd70 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20 lite3*, int id,
1cd80 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a int newVal);../*
1cd90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 .** CAPI3REF: Ru
1cda0 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74 n-Time Limit Cat
1cdb0 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d egories {H12790}
1cdc0 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59 <H12760>.** KEY
1cdd0 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61 WORDS: {limit ca
1cde0 74 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63 tegory} {limit c
1cdf0 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a ategories}.**.**
1ce00 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 These constants
1ce10 20 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 define various
1ce20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6c 69 6d 69 performance limi
1ce30 74 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 ts.** that can b
1ce40 65 20 6c 6f 77 65 72 65 64 20 61 74 20 72 75 6e e lowered at run
1ce50 2d 74 69 6d 65 20 75 73 69 6e 67 20 5b 73 71 6c -time using [sql
1ce60 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e 0a 2a ite3_limit()]..*
1ce70 2a 20 54 68 65 20 73 79 6e 6f 70 73 69 73 20 6f * The synopsis o
1ce80 66 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f f the meanings o
1ce90 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 69 f the various li
1cea0 6d 69 74 73 20 69 73 20 73 68 6f 77 6e 20 62 65 mits is shown be
1ceb0 6c 6f 77 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e low..** Addition
1cec0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 al information i
1ced0 73 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 5b s available at [
1cee0 6c 69 6d 69 74 73 20 7c 20 4c 69 6d 69 74 73 20 limits | Limits
1cef0 69 6e 20 53 51 4c 69 74 65 5d 2e 0a 2a 2a 0a 2a in SQLite]..**.*
1cf00 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
1cf10 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1cf20 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 H</dt>.** <dd>Th
1cf30 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f e maximum size o
1cf40 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 f any string or
1cf50 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65 20 72 6f BLOB or table ro
1cf60 77 2e 3c 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 w.<dd>.**.** <dt
1cf70 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 >SQLITE_LIMIT_SQ
1cf80 4c 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a L_LENGTH</dt>.**
1cf90 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d <dd>The maximum
1cfa0 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e 20 53 51 length of an SQ
1cfb0 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 L statement.</dd
1cfc0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
1cfd0 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 3c TE_LIMIT_COLUMN<
1cfe0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
1cff0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1d000 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 f columns in a t
1d010 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 able definition
1d020 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 72 65 73 or in the.** res
1d030 75 6c 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45 ult set of a [SE
1d040 4c 45 43 54 5d 20 6f 72 20 74 68 65 20 6d 61 78 LECT] or the max
1d050 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 imum number of c
1d060 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 olumns in an ind
1d070 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61 6e 20 4f ex.** or in an O
1d080 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 RDER BY or GROUP
1d090 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f 64 64 3e BY clause.</dd>
1d0a0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
1d0b0 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 E_LIMIT_EXPR_DEP
1d0c0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 TH</dt>.** <dd>T
1d0d0 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 he maximum depth
1d0e0 20 6f 66 20 74 68 65 20 70 61 72 73 65 20 74 72 of the parse tr
1d0f0 65 65 20 6f 6e 20 61 6e 79 20 65 78 70 72 65 73 ee on any expres
1d100 73 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a sion.</dd>.**.**
1d110 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 <dt>SQLITE_LIMI
1d120 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 T_COMPOUND_SELEC
1d130 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 T</dt>.** <dd>Th
1d140 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1d150 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63 of terms in a c
1d160 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 ompound SELECT s
1d170 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a tatement.</dd>.*
1d180 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
1d190 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50 3c 2f 64 LIMIT_VDBE_OP</d
1d1a0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
1d1b0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
1d1c0 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 instructions in
1d1d0 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e a virtual machin
1d1e0 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 75 73 65 e program.** use
1d1f0 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 d to implement a
1d200 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e n SQL statement.
1d210 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
1d220 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e SQLITE_LIMIT_FUN
1d230 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74 3e 0a 2a CTION_ARG</dt>.*
1d240 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1d250 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 m number of argu
1d260 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75 6e 63 74 ments on a funct
1d270 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ion.</dd>.**.**
1d280 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1d290 5f 41 54 54 41 43 48 45 44 3c 2f 64 74 3e 0a 2a _ATTACHED</dt>.*
1d2a0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1d2b0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 5b 41 54 54 m number of [ATT
1d2c0 41 43 48 20 7c 20 61 74 74 61 63 68 65 64 20 64 ACH | attached d
1d2d0 61 74 61 62 61 73 65 73 5d 2e 3c 2f 64 64 3e 0a atabases].</dd>.
1d2e0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
1d2f0 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 _LIMIT_LIKE_PATT
1d300 45 52 4e 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a ERN_LENGTH</dt>.
1d310 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
1d320 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 um length of the
1d330 20 70 61 74 74 65 72 6e 20 61 72 67 75 6d 65 6e pattern argumen
1d340 74 20 74 6f 20 74 68 65 20 5b 4c 49 4b 45 5d 20 t to the [LIKE]
1d350 6f 72 0a 2a 2a 20 5b 47 4c 4f 42 5d 20 6f 70 65 or.** [GLOB] ope
1d360 72 61 74 6f 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a rators.</dd>.**.
1d370 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
1d380 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
1d390 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e BER</dt>.** <dd>
1d3a0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1d3b0 65 72 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 er of variables
1d3c0 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d in an SQL statem
1d3d0 65 6e 74 20 74 68 61 74 20 63 61 6e 0a 2a 2a 20 ent that can.**
1d3e0 62 65 20 62 6f 75 6e 64 2e 3c 2f 64 64 3e 0a 2a be bound.</dd>.*
1d3f0 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 * </dl>.*/.#defi
1d400 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
1d410 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 LENGTH
1d420 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 0.#def
1d430 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
1d440 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 20 20 20 20 _SQL_LENGTH
1d450 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 1.#de
1d460 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
1d470 54 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 T_COLUMN
1d480 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 2.#d
1d490 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
1d4a0 49 54 5f 45 58 50 52 5f 44 45 50 54 48 20 20 20 IT_EXPR_DEPTH
1d4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 3.#
1d4c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 define SQLITE_LI
1d4d0 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c MIT_COMPOUND_SEL
1d4e0 45 43 54 20 20 20 20 20 20 20 20 20 20 20 34 0a ECT 4.
1d4f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
1d500 49 4d 49 54 5f 56 44 42 45 5f 4f 50 20 20 20 20 IMIT_VDBE_OP
1d510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
1d520 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1d530 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 LIMIT_FUNCTION_A
1d540 52 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 RG
1d550 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 6.#define SQLITE
1d560 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 20 _LIMIT_ATTACHED
1d570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d580 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 7.#define SQLIT
1d590 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 E_LIMIT_LIKE_PAT
1d5a0 54 45 52 4e 5f 4c 45 4e 47 54 48 20 20 20 20 20 TERN_LENGTH
1d5b0 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8.#define SQLI
1d5c0 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c TE_LIMIT_VARIABL
1d5d0 45 5f 4e 55 4d 42 45 52 20 20 20 20 20 20 20 20 E_NUMBER
1d5e0 20 20 20 39 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 9../*.** CAPI
1d5f0 33 52 45 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 3REF: Compiling
1d600 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 An SQL Statement
1d610 20 7b 48 31 33 30 31 30 7d 20 3c 53 31 30 30 30 {H13010} <S1000
1d620 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
1d630 7b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 {SQL statement c
1d640 6f 6d 70 69 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 ompiler}.**.** T
1d650 6f 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c o execute an SQL
1d660 20 71 75 65 72 79 2c 20 69 74 20 6d 75 73 74 20 query, it must
1d670 66 69 72 73 74 20 62 65 20 63 6f 6d 70 69 6c 65 first be compile
1d680 64 20 69 6e 74 6f 20 61 20 62 79 74 65 2d 63 6f d into a byte-co
1d690 64 65 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73 de.** program us
1d6a0 69 6e 67 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 ing one of these
1d6b0 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a routines..**.**
1d6c0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
1d6d0 65 6e 74 2c 20 22 64 62 22 2c 20 69 73 20 61 20 ent, "db", is a
1d6e0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
1d6f0 74 69 6f 6e 5d 20 6f 62 74 61 69 6e 65 64 20 66 tion] obtained f
1d700 72 6f 6d 20 61 0a 2a 2a 20 70 72 69 6f 72 20 63 rom a.** prior c
1d710 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
1d720 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 open()], [sqlite
1d730 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f 72 20 3_open_v2()] or
1d740 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 [sqlite3_open16(
1d750 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 )]..**.** The se
1d760 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 cond argument, "
1d770 7a 53 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74 zSql", is the st
1d780 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f atement to be co
1d790 6d 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a mpiled, encoded.
1d7a0 2a 2a 20 61 73 20 65 69 74 68 65 72 20 55 54 46 ** as either UTF
1d7b0 2d 38 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 -8 or UTF-16. T
1d7c0 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 he sqlite3_prepa
1d7d0 72 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 re() and sqlite3
1d7e0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a _prepare_v2().**
1d7f0 20 69 6e 74 65 72 66 61 63 65 73 20 75 73 65 20 interfaces use
1d800 55 54 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 UTF-8, and sqlit
1d810 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 20 61 e3_prepare16() a
1d820 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 nd sqlite3_prepa
1d830 72 65 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 re16_v2().** use
1d840 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 UTF-16..**.** I
1d850 66 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 f the nByte argu
1d860 6d 65 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 ment is less tha
1d870 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 n zero, then zSq
1d880 6c 20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 l is read up to
1d890 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 the.** first zer
1d8a0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66 o terminator. If
1d8b0 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 nByte is non-ne
1d8c0 67 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 gative, then it
1d8d0 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a is the maximum.*
1d8e0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 * number of byt
1d8f0 65 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 es read from zSq
1d900 6c 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 l. When nByte i
1d910 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 s non-negative,
1d920 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 the.** zSql stri
1d930 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 ng ends at eithe
1d940 72 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 r the first '\00
1d950 30 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 0' or '\u0000' c
1d960 68 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 haracter or.** t
1d970 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 he nByte-th byte
1d980 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 , whichever come
1d990 73 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 s first. If the
1d9a0 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 caller knows.**
1d9b0 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 that the supplie
1d9c0 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d d string is nul-
1d9d0 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e terminated, then
1d9e0 20 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c there is a smal
1d9f0 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 l.** performance
1da00 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 advantage to be
1da10 20 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 gained by passi
1da20 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 ng an nByte para
1da30 6d 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 meter that.** is
1da40 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 equal to the nu
1da50 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
1da60 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
1da70 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f g <i>including</
1da80 69 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 i>.** the nul-te
1da90 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a rminator bytes..
1daa0 2a 2a 0a 2a 2a 20 2a 70 7a 54 61 69 6c 20 69 73 **.** *pzTail is
1dab0 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 made to point t
1dac0 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 o the first byte
1dad0 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
1dae0 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 53 51 the.** first SQ
1daf0 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a L statement in z
1db00 53 71 6c 2e 20 20 54 68 65 73 65 20 72 6f 75 74 Sql. These rout
1db10 69 6e 65 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c ines only compil
1db20 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 73 e the first.** s
1db30 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c tatement in zSql
1db40 2c 20 73 6f 20 2a 70 7a 54 61 69 6c 20 69 73 20 , so *pzTail is
1db50 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f left pointing to
1db60 20 77 68 61 74 20 72 65 6d 61 69 6e 73 0a 2a 2a what remains.**
1db70 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 0a uncompiled..**.
1db80 2a 2a 20 2a 70 70 53 74 6d 74 20 69 73 20 6c 65 ** *ppStmt is le
1db90 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 ft pointing to a
1dba0 20 63 6f 6d 70 69 6c 65 64 20 5b 70 72 65 70 61 compiled [prepa
1dbb0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 red statement] t
1dbc0 68 61 74 20 63 61 6e 20 62 65 0a 2a 2a 20 65 78 hat can be.** ex
1dbd0 65 63 75 74 65 64 20 75 73 69 6e 67 20 5b 73 71 ecuted using [sq
1dbe0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 20 20 lite3_step()].
1dbf0 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 If there is an e
1dc00 72 72 6f 72 2c 20 2a 70 70 53 74 6d 74 20 69 73 rror, *ppStmt is
1dc10 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e set.** to NULL.
1dc20 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 74 If the input t
1dc30 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 ext contains no
1dc40 53 51 4c 20 28 69 66 20 74 68 65 20 69 6e 70 75 SQL (if the inpu
1dc50 74 20 69 73 20 61 6e 20 65 6d 70 74 79 0a 2a 2a t is an empty.**
1dc60 20 73 74 72 69 6e 67 20 6f 72 20 61 20 63 6f 6d string or a com
1dc70 6d 65 6e 74 29 20 74 68 65 6e 20 2a 70 70 53 74 ment) then *ppSt
1dc80 6d 74 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c mt is set to NUL
1dc90 4c 2e 0a 2a 2a 20 7b 41 31 33 30 31 38 7d 20 54 L..** {A13018} T
1dca0 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 he calling proce
1dcb0 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e 73 69 dure is responsi
1dcc0 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 6e 67 ble for deleting
1dcd0 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a the compiled.**
1dce0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 SQL statement u
1dcf0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
1dd00 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 20 nalize()] after
1dd10 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 it has finished
1dd20 77 69 74 68 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f with it..**.** O
1dd30 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 n success, [SQLI
1dd40 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e TE_OK] is return
1dd50 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e ed, otherwise an
1dd60 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
1dd70 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
1dd80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 The sqlite3_pre
1dd90 70 61 72 65 5f 76 32 28 29 20 61 6e 64 20 73 71 pare_v2() and sq
1dda0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
1ddb0 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 73 20 v2() interfaces
1ddc0 61 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 are.** recommend
1ddd0 65 64 20 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70 ed for all new p
1dde0 72 6f 67 72 61 6d 73 2e 20 54 68 65 20 74 77 6f rograms. The two
1ddf0 20 6f 6c 64 65 72 20 69 6e 74 65 72 66 61 63 65 older interface
1de00 73 20 61 72 65 20 72 65 74 61 69 6e 65 64 0a 2a s are retained.*
1de10 2a 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20 * for backwards
1de20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 62 compatibility, b
1de30 75 74 20 74 68 65 69 72 20 75 73 65 20 69 73 20 ut their use is
1de40 64 69 73 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 discouraged..**
1de50 49 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 In the "v2" inte
1de60 72 66 61 63 65 73 2c 20 74 68 65 20 70 72 65 70 rfaces, the prep
1de70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a ared statement.*
1de80 2a 20 74 68 61 74 20 69 73 20 72 65 74 75 72 6e * that is return
1de90 65 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33 ed (the [sqlite3
1dea0 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 29 20 63 _stmt] object) c
1deb0 6f 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20 6f ontains a copy o
1dec0 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 f the.** origina
1ded0 6c 20 53 51 4c 20 74 65 78 74 2e 20 54 68 69 73 l SQL text. This
1dee0 20 63 61 75 73 65 73 20 74 68 65 20 5b 73 71 6c causes the [sql
1def0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 69 6e 74 ite3_step()] int
1df00 65 72 66 61 63 65 20 74 6f 0a 2a 2a 20 62 65 68 erface to.** beh
1df10 61 76 65 20 61 20 64 69 66 66 65 72 65 6e 74 6c ave a differentl
1df20 79 20 69 6e 20 74 77 6f 20 77 61 79 73 3a 0a 2a y in two ways:.*
1df30 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ol>.** <li
1df40 3e 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 >.** If the data
1df50 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e base schema chan
1df60 67 65 73 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 ges, instead of
1df70 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 returning [SQLIT
1df80 45 5f 53 43 48 45 4d 41 5d 20 61 73 20 69 74 0a E_SCHEMA] as it.
1df90 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 64 20 74 ** always used t
1dfa0 6f 20 64 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 o do, [sqlite3_s
1dfb0 74 65 70 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f tep()] will auto
1dfc0 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 matically recomp
1dfd0 69 6c 65 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 ile the SQL.** s
1dfe0 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 tatement and try
1dff0 20 74 6f 20 72 75 6e 20 69 74 20 61 67 61 69 6e to run it again
1e000 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 . If the schema
1e010 20 68 61 73 20 63 68 61 6e 67 65 64 20 69 6e 0a has changed in.
1e020 2a 2a 20 61 20 77 61 79 20 74 68 61 74 20 6d 61 ** a way that ma
1e030 6b 65 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e kes the statemen
1e040 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 t no longer vali
1e050 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 d, [sqlite3_step
1e060 28 29 5d 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a ()] will still.*
1e070 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 * return [SQLITE
1e080 5f 53 43 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 _SCHEMA]. But u
1e090 6e 6c 69 6b 65 20 74 68 65 20 6c 65 67 61 63 79 nlike the legacy
1e0a0 20 62 65 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 behavior, [SQLI
1e0b0 54 45 5f 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a TE_SCHEMA] is.**
1e0c0 20 6e 6f 77 20 61 20 66 61 74 61 6c 20 65 72 72 now a fatal err
1e0d0 6f 72 2e 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 or. Calling [sq
1e0e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1e0f0 28 29 5d 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e ()] again will n
1e100 6f 74 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 ot make the.** e
1e110 72 72 6f 72 20 67 6f 20 61 77 61 79 2e 20 20 4e rror go away. N
1e120 6f 74 65 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 ote: use [sqlite
1e130 33 5f 65 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 3_errmsg()] to f
1e140 69 6e 64 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 ind the text.**
1e150 6f 66 20 74 68 65 20 70 61 72 73 69 6e 67 20 65 of the parsing e
1e160 72 72 6f 72 20 74 68 61 74 20 72 65 73 75 6c 74 rror that result
1e170 73 20 69 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f s in an [SQLITE_
1e180 53 43 48 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a SCHEMA] return..
1e190 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c ** </li>.**.** <
1e1a0 6c 69 3e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 li>.** When an e
1e1b0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 rror occurs, [sq
1e1c0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 lite3_step()] wi
1e1d0 6c 6c 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 ll return one of
1e1e0 20 74 68 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a the detailed.**
1e1f0 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f [error codes] o
1e200 72 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f r [extended erro
1e210 72 20 63 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c r codes]. The l
1e220 65 67 61 63 79 20 62 65 68 61 76 69 6f 72 20 77 egacy behavior w
1e230 61 73 20 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 as that.** [sqli
1e240 74 65 33 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c te3_step()] woul
1e250 64 20 6f 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 d only return a
1e260 67 65 6e 65 72 69 63 20 5b 53 51 4c 49 54 45 5f generic [SQLITE_
1e270 45 52 52 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f ERROR] result co
1e280 64 65 0a 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f de.** and you wo
1e290 75 6c 64 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 uld have to make
1e2a0 20 61 20 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 a second call t
1e2b0 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 o [sqlite3_reset
1e2c0 28 29 5d 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 ()] in order.**
1e2d0 74 6f 20 66 69 6e 64 20 74 68 65 20 75 6e 64 65 to find the unde
1e2e0 72 6c 79 69 6e 67 20 63 61 75 73 65 20 6f 66 20 rlying cause of
1e2f0 74 68 65 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 the problem. Wit
1e300 68 20 74 68 65 20 22 76 32 22 20 70 72 65 70 61 h the "v2" prepa
1e310 72 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 re.** interfaces
1e320 2c 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 , the underlying
1e330 20 72 65 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 reason for the
1e340 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
1e350 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a d immediately..*
1e360 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e * </li>.** </ol>
1e370 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
1e380 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 31 31 5d nts:.** [H13011]
1e390 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 33 30 31 [H13012] [H1301
1e3a0 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 48 31 33 3] [H13014] [H13
1e3b0 30 31 35 5d 20 5b 48 31 33 30 31 36 5d 20 5b 48 015] [H13016] [H
1e3c0 31 33 30 31 39 5d 20 5b 48 31 33 30 32 31 5d 0a 13019] [H13021].
1e3d0 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 **.*/.SQLITE_API
1e3e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 int sqlite3_pre
1e3f0 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20 pare(. sqlite3
1e400 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
1e410 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 /* Database hand
1e420 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 le */. const ch
1e430 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 ar *zSql,
1e440 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 /* SQL statement
1e450 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 , UTF-8 encoded
1e460 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 */. int nByte,
1e470 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e480 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f Maximum length o
1e490 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e f zSql in bytes.
1e4a0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 */. sqlite3_st
1e4b0 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a mt **ppStmt, /*
1e4c0 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 OUT: Statement
1e4d0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 handle */. cons
1e4e0 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 t char **pzTail
1e4f0 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e /* OUT: Poin
1e500 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f ter to unused po
1e510 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f rtion of zSql */
1e520 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
1e530 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 nt sqlite3_prepa
1e540 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 re_v2(. sqlite3
1e550 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
1e560 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e /* Database han
1e570 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 dle */. const c
1e580 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 har *zSql,
1e590 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e /* SQL statemen
1e5a0 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 t, UTF-8 encoded
1e5b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c */. int nByte,
1e5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e5d0 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 Maximum length
1e5e0 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 of zSql in bytes
1e5f0 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 . */. sqlite3_s
1e600 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f tmt **ppStmt, /
1e610 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 * OUT: Statement
1e620 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
1e630 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c st char **pzTail
1e640 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 /* OUT: Poi
1e650 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 nter to unused p
1e660 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a ortion of zSql *
1e670 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 /.);.SQLITE_API
1e680 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 int sqlite3_prep
1e690 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 are16(. sqlite3
1e6a0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
1e6b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e /* Database han
1e6c0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 dle */. const v
1e6d0 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 oid *zSql,
1e6e0 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e /* SQL statemen
1e6f0 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 t, UTF-16 encode
1e700 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 d */. int nByte
1e710 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1e720 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
1e730 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 of zSql in byte
1e740 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f s. */. sqlite3_
1e750 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 stmt **ppStmt,
1e760 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e /* OUT: Statemen
1e770 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f t handle */. co
1e780 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 nst void **pzTai
1e790 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f l /* OUT: Po
1e7a0 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 inter to unused
1e7b0 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 portion of zSql
1e7c0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 */.);.SQLITE_API
1e7d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 int sqlite3_pre
1e7e0 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 71 6c pare16_v2(. sql
1e7f0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
1e800 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1e810 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
1e820 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 st void *zSql,
1e830 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 /* SQL stat
1e840 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e ement, UTF-16 en
1e850 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e coded */. int n
1e860 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 Byte,
1e870 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 /* Maximum le
1e880 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 ngth of zSql in
1e890 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 bytes. */. sqli
1e8a0 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d te3_stmt **ppStm
1e8b0 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 t, /* OUT: Stat
1e8c0 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a ement handle */.
1e8d0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 const void **p
1e8e0 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 zTail /* OUT
1e8f0 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 : Pointer to unu
1e900 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a sed portion of z
1e910 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a Sql */.);../*.**
1e920 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69 CAPI3REF: Retri
1e930 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e 74 20 eving Statement
1e940 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 3c 48 31 SQL {H13100} <H1
1e950 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 3000>.**.** This
1e960 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 interface can b
1e970 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 e used to retrie
1e980 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 79 20 ve a saved copy
1e990 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a of the original.
1e9a0 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73 65 64 ** SQL text used
1e9b0 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b 70 72 to create a [pr
1e9c0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1e9d0 5d 20 69 66 20 74 68 61 74 20 73 74 61 74 65 6d ] if that statem
1e9e0 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 ent was.** compi
1e9f0 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 led using either
1ea00 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
1ea10 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 e_v2()] or [sqli
1ea20 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 te3_prepare16_v2
1ea30 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ()]..**.** Requi
1ea40 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
1ea50 31 30 31 5d 20 5b 48 31 33 31 30 32 5d 20 5b 48 101] [H13102] [H
1ea60 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13103].*/.SQLITE
1ea70 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
1ea80 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c *sqlite3_sql(sql
1ea90 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1eaa0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
1eab0 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 EF: Dynamically
1eac0 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62 6a 65 Typed Value Obje
1ead0 63 74 20 7b 48 31 35 30 30 30 7d 20 3c 53 32 30 ct {H15000} <S20
1eae0 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 200>.** KEYWORDS
1eaf0 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 73 71 6c : {protected sql
1eb00 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75 6e 70 ite3_value} {unp
1eb10 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
1eb20 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53 51 _value}.**.** SQ
1eb30 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 71 Lite uses the sq
1eb40 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
1eb50 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 ct to represent
1eb60 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a 20 74 68 all values.** th
1eb70 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 at can be stored
1eb80 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 in a database t
1eb90 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75 73 65 able. SQLite use
1eba0 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69 6e 67 s dynamic typing
1ebb0 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 61 6c 75 .** for the valu
1ebc0 65 73 20 69 74 20 73 74 6f 72 65 73 2e 20 56 61 es it stores. Va
1ebd0 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 73 lues stored in s
1ebe0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
1ebf0 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 69 ects.** can be i
1ec00 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 74 69 6e ntegers, floatin
1ec10 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 2c 20 g point values,
1ec20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 73 2c 20 strings, BLOBs,
1ec30 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 or NULL..**.** A
1ec40 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 n sqlite3_value
1ec50 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20 65 69 object may be ei
1ec60 74 68 65 72 20 22 70 72 6f 74 65 63 74 65 64 22 ther "protected"
1ec70 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 74 65 64 or "unprotected
1ec80 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e 74 65 72 "..** Some inter
1ec90 66 61 63 65 73 20 72 65 71 75 69 72 65 20 61 20 faces require a
1eca0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
1ecb0 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 65 72 20 3_value. Other
1ecc0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 77 69 interfaces.** wi
1ecd0 6c 6c 20 61 63 63 65 70 74 20 65 69 74 68 65 72 ll accept either
1ece0 20 61 20 70 72 6f 74 65 63 74 65 64 20 6f 72 20 a protected or
1ecf0 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 an unprotected s
1ed00 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a qlite3_value..**
1ed10 20 45 76 65 72 79 20 69 6e 74 65 72 66 61 63 65 Every interface
1ed20 20 74 68 61 74 20 61 63 63 65 70 74 73 20 73 71 that accepts sq
1ed30 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 72 67 75 lite3_value argu
1ed40 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 73 0a ments specifies.
1ed50 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ** whether or no
1ed60 74 20 69 74 20 72 65 71 75 69 72 65 73 20 61 20 t it requires a
1ed70 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
1ed80 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 3_value..**.** T
1ed90 68 65 20 74 65 72 6d 73 20 22 70 72 6f 74 65 63 he terms "protec
1eda0 74 65 64 22 20 61 6e 64 20 22 75 6e 70 72 6f 74 ted" and "unprot
1edb0 65 63 74 65 64 22 20 72 65 66 65 72 20 74 6f 20 ected" refer to
1edc0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a whether or not.*
1edd0 2a 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c * a mutex is hel
1ede0 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 6c 20 6d d. A internal m
1edf0 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72 utex is held for
1ee00 20 61 20 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 a protected.**
1ee10 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
1ee20 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75 74 65 ject but no mute
1ee30 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 6e x is held for an
1ee40 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 unprotected.**
1ee50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
1ee60 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69 74 65 ject. If SQLite
1ee70 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 is compiled to
1ee80 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 be single-thread
1ee90 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b 53 51 4c ed.** (with [SQL
1eea0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30 ITE_THREADSAFE=0
1eeb0 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 71 6c 69 ] and with [sqli
1eec0 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 29 te3_threadsafe()
1eed0 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 29 0a 2a ] returning 0).*
1eee0 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 65 20 69 * or if SQLite i
1eef0 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 20 s run in one of
1ef00 72 65 64 75 63 65 64 20 6d 75 74 65 78 20 6d 6f reduced mutex mo
1ef10 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f des .** [SQLITE_
1ef20 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 CONFIG_SINGLETHR
1ef30 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f EAD] or [SQLITE_
1ef40 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 CONFIG_MULTITHRE
1ef50 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72 AD].** then ther
1ef60 65 20 69 73 20 6e 6f 20 64 69 73 74 69 6e 63 74 e is no distinct
1ef70 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74 ion between prot
1ef80 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 ected and unprot
1ef90 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ected.** sqlite3
1efa0 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 61 _value objects a
1efb0 6e 64 20 74 68 65 79 20 63 61 6e 20 62 65 20 75 nd they can be u
1efc0 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67 65 61 sed interchangea
1efd0 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a bly. However,.*
1efe0 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f * for maximum co
1eff0 64 65 20 70 6f 72 74 61 62 69 6c 69 74 79 20 69 de portability i
1f000 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 t is recommended
1f010 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69 6f that applicatio
1f020 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b 65 ns.** still make
1f030 20 74 68 65 20 64 69 73 74 69 6e 63 74 69 6f 6e the distinction
1f040 20 62 65 74 77 65 65 6e 20 62 65 74 77 65 65 6e between between
1f050 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75 protected and u
1f060 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 nprotected.** sq
1f070 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
1f080 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20 6e 6f cts even when no
1f090 74 20 73 74 72 69 63 74 6c 79 20 72 65 71 75 69 t strictly requi
1f0a0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 red..**.** The s
1f0b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
1f0c0 65 63 74 73 20 74 68 61 74 20 61 72 65 20 70 61 ects that are pa
1f0d0 73 73 65 64 20 61 73 20 70 61 72 61 6d 65 74 65 ssed as paramete
1f0e0 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 rs into the.** i
1f0f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
1f100 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 [application-de
1f110 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 fined SQL functi
1f120 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 65 63 74 ons] are protect
1f130 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ed..** The sqlit
1f140 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 e3_value object
1f150 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b returned by.** [
1f160 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 sqlite3_column_v
1f170 61 6c 75 65 28 29 5d 20 69 73 20 75 6e 70 72 6f alue()] is unpro
1f180 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e 70 72 6f tected..** Unpro
1f190 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
1f1a0 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 alue objects may
1f1b0 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69 only be used wi
1f1c0 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 th.** [sqlite3_r
1f1d0 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 20 61 esult_value()] a
1f1e0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 nd [sqlite3_bind
1f1f0 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 54 68 _value()]..** Th
1f200 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
1f210 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f _blob | sqlite3_
1f220 76 61 6c 75 65 5f 74 79 70 65 28 29 5d 20 66 61 value_type()] fa
1f230 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e 74 65 72 mily of.** inter
1f240 66 61 63 65 73 20 72 65 71 75 69 72 65 20 70 72 faces require pr
1f250 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
1f260 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a value objects..*
1f270 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
1f280 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 61 6c Mem sqlite3_val
1f290 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 ue;../*.** CAPI3
1f2a0 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74 69 6f REF: SQL Functio
1f2b0 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65 63 74 n Context Object
1f2c0 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 30 32 30 {H16001} <S2020
1f2d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 0>.**.** The con
1f2e0 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 61 6e text in which an
1f2f0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 65 78 SQL function ex
1f300 65 63 75 74 65 73 20 69 73 20 73 74 6f 72 65 64 ecutes is stored
1f310 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 in an.** sqlite
1f320 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 3_context object
1f330 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 . A pointer to
1f340 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 an sqlite3_conte
1f350 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 69 73 20 xt object.** is
1f360 61 6c 77 61 79 73 20 66 69 72 73 74 20 70 61 72 always first par
1f370 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 70 6c 69 ameter to [appli
1f380 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 cation-defined S
1f390 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a QL functions]..*
1f3a0 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f * The applicatio
1f3b0 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 n-defined SQL fu
1f3c0 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
1f3d0 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 73 73 20 ation will pass
1f3e0 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 this.** pointer
1f3f0 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 63 61 6c through into cal
1f400 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 ls to [sqlite3_r
1f410 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 71 6c 69 esult_int | sqli
1f420 74 65 33 5f 72 65 73 75 6c 74 28 29 5d 2c 0a 2a te3_result()],.*
1f430 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65 * [sqlite3_aggre
1f440 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 5d 2c gate_context()],
1f450 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 [sqlite3_user_d
1f460 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 ata()],.** [sqli
1f470 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
1f480 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 6c 69 74 andle()], [sqlit
1f490 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 e3_get_auxdata()
1f4a0 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 73 71 ],.** and/or [sq
1f4b0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 lite3_set_auxdat
1f4c0 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 a()]..*/.typedef
1f4d0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
1f4e0 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 65 33 5f context sqlite3_
1f4f0 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20 context;../*.**
1f500 43 41 50 49 33 52 45 46 3a 20 42 69 6e 64 69 6e CAPI3REF: Bindin
1f510 67 20 56 61 6c 75 65 73 20 54 6f 20 50 72 65 70 g Values To Prep
1f520 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 73 20 ared Statements
1f530 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 33 30 30 {H13500} <S70300
1f540 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
1f550 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 7d 20 host parameter}
1f560 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 {host parameters
1f570 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 } {host paramete
1f580 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 59 57 4f r name}.** KEYWO
1f590 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 61 6d 65 RDS: {SQL parame
1f5a0 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 61 6d 65 ter} {SQL parame
1f5b0 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 74 65 72 ters} {parameter
1f5c0 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a 2a 20 binding}.**.**
1f5d0 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e In the SQL strin
1f5e0 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c gs input to [sql
1f5f0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
1f600 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72 69 61 )] and its varia
1f610 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c 73 nts,.** literals
1f620 20 6d 61 79 20 62 65 20 72 65 70 6c 61 63 65 64 may be replaced
1f630 20 62 79 20 61 20 5b 70 61 72 61 6d 65 74 65 72 by a [parameter
1f640 5d 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 73 ] in one of thes
1f650 65 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 3c e forms:.**.** <
1f660 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a ul>.** <li> ?.*
1f670 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20 * <li> ?NNN.**
1f680 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 3c 6c <li> :VVV.** <l
1f690 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c 6c 69 3e i> @VVV.** <li>
1f6a0 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a $VVV.** </ul>.
1f6b0 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 70 61 72 **.** In the par
1f6c0 61 6d 65 74 65 72 20 66 6f 72 6d 73 20 73 68 6f ameter forms sho
1f6d0 77 6e 20 61 62 6f 76 65 20 4e 4e 4e 20 69 73 20 wn above NNN is
1f6e0 61 6e 20 69 6e 74 65 67 65 72 20 6c 69 74 65 72 an integer liter
1f6f0 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 56 20 69 al,.** and VVV i
1f700 73 20 61 6e 20 61 6c 70 68 61 2d 6e 75 6d 65 72 s an alpha-numer
1f710 69 63 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d ic parameter nam
1f720 65 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66 e. The values of
1f730 20 74 68 65 73 65 0a 2a 2a 20 70 61 72 61 6d 65 these.** parame
1f740 74 65 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c 65 ters (also calle
1f750 64 20 22 68 6f 73 74 20 70 61 72 61 6d 65 74 65 d "host paramete
1f760 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 53 51 4c r names" or "SQL
1f770 20 70 61 72 61 6d 65 74 65 72 73 22 29 0a 2a 2a parameters").**
1f780 20 63 61 6e 20 62 65 20 73 65 74 20 75 73 69 6e can be set usin
1f790 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 g the sqlite3_bi
1f7a0 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 nd_*() routines
1f7b0 64 65 66 69 6e 65 64 20 68 65 72 65 2e 0a 2a 2a defined here..**
1f7c0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 .** The first ar
1f7d0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 gument to the sq
1f7e0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 lite3_bind_*() r
1f7f0 6f 75 74 69 6e 65 73 20 69 73 20 61 6c 77 61 79 outines is alway
1f800 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 s.** a pointer t
1f810 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 o the [sqlite3_s
1f820 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 tmt] object retu
1f830 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 rned from.** [sq
1f840 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1f850 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 61 ()] or its varia
1f860 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 nts..**.** The s
1f870 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
1f880 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
1f890 68 65 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 he SQL parameter
1f8a0 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a 2a 20 54 to be set..** T
1f8b0 68 65 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 he leftmost SQL
1f8c0 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e parameter has an
1f8d0 20 69 6e 64 65 78 20 6f 66 20 31 2e 20 20 57 68 index of 1. Wh
1f8e0 65 6e 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 en the same name
1f8f0 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 d.** SQL paramet
1f900 65 72 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 er is used more
1f910 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65 63 6f 6e than once, secon
1f920 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 d and subsequent
1f930 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63 65 73 20 .** occurrences
1f940 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 69 6e have the same in
1f950 64 65 78 20 61 73 20 74 68 65 20 66 69 72 73 74 dex as the first
1f960 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a 2a 2a 20 occurrence..**
1f970 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20 6e 61 The index for na
1f980 6d 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 63 med parameters c
1f990 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 75 70 20 an be looked up
1f9a0 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 using the.** [sq
1f9b0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
1f9c0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 20 41 50 eter_index()] AP
1f9d0 49 20 69 66 20 64 65 73 69 72 65 64 2e 20 20 54 I if desired. T
1f9e0 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66 6f 72 20 he index.** for
1f9f0 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65 74 65 72 "?NNN" parameter
1fa00 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f s is the value o
1fa10 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20 4e 4e f NNN..** The NN
1fa20 4e 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 N value must be
1fa30 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 74 68 between 1 and th
1fa40 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 e [sqlite3_limit
1fa50 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 ()].** parameter
1fa60 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 [SQLITE_LIMIT_V
1fa70 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 ARIABLE_NUMBER]
1fa80 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 3a 20 (default value:
1fa90 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 999)..**.** The
1faa0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 third argument i
1fab0 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 s the value to b
1fac0 69 6e 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d ind to the param
1fad0 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 eter..**.** In t
1fae0 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68 hose routines th
1faf0 61 74 20 68 61 76 65 20 61 20 66 6f 75 72 74 68 at have a fourth
1fb00 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76 argument, its v
1fb10 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e alue is the.** n
1fb20 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1fb30 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e n the parameter.
1fb40 20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74 To be clear: t
1fb50 68 65 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a he value is the.
1fb60 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e ** number of <u>
1fb70 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65 bytes</u> in the
1fb80 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20 value, not the
1fb90 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
1fba0 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 ters..** If the
1fbb0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
1fbc0 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 is negative, th
1fbd0 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
1fbe0 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 74 68 65 string is.** the
1fbf0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1fc00 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 up to the first
1fc10 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
1fc20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74 ..**.** The fift
1fc30 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 h argument to sq
1fc40 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 lite3_bind_blob(
1fc50 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f ), sqlite3_bind_
1fc60 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73 text(), and.** s
1fc70 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
1fc80 31 36 28 29 20 69 73 20 61 20 64 65 73 74 72 75 16() is a destru
1fc90 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73 ctor used to dis
1fca0 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 pose of the BLOB
1fcb0 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66 or.** string af
1fcc0 74 65 72 20 53 51 4c 69 74 65 20 68 61 73 20 66 ter SQLite has f
1fcd0 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e inished with it.
1fce0 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72 If the fifth ar
1fcf0 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 68 65 gument is.** the
1fd00 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 20 5b special value [
1fd10 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20 SQLITE_STATIC],
1fd20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 73 73 75 then SQLite assu
1fd30 6d 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 mes that the.**
1fd40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 69 information is i
1fd50 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d 61 6e 61 n static, unmana
1fd60 67 65 64 20 73 70 61 63 65 20 61 6e 64 20 64 6f ged space and do
1fd70 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 es not need to b
1fd80 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 74 e freed..** If t
1fd90 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e he fifth argumen
1fda0 74 20 68 61 73 20 74 68 65 20 76 61 6c 75 65 20 t has the value
1fdb0 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e [SQLITE_TRANSIEN
1fdc0 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 T], then.** SQLi
1fdd0 74 65 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e te makes its own
1fde0 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 private copy of
1fdf0 20 74 68 65 20 64 61 74 61 20 69 6d 6d 65 64 69 the data immedi
1fe00 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 0a 2a 2a ately, before.**
1fe10 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e the sqlite3_bin
1fe20 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 20 72 65 d_*() routine re
1fe30 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 turns..**.** The
1fe40 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 sqlite3_bind_ze
1fe50 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74 69 6e 65 roblob() routine
1fe60 20 62 69 6e 64 73 20 61 20 42 4c 4f 42 20 6f 66 binds a BLOB of
1fe70 20 6c 65 6e 67 74 68 20 4e 20 74 68 61 74 0a 2a length N that.*
1fe80 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 * is filled with
1fe90 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a 65 72 6f zeroes. A zero
1fea0 62 6c 6f 62 20 75 73 65 73 20 61 20 66 69 78 65 blob uses a fixe
1feb0 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f d amount of memo
1fec0 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61 6e 20 69 ry.** (just an i
1fed0 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c 64 20 69 nteger to hold i
1fee0 74 73 20 73 69 7a 65 29 20 77 68 69 6c 65 20 69 ts size) while i
1fef0 74 20 69 73 20 62 65 69 6e 67 20 70 72 6f 63 65 t is being proce
1ff00 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62 6c 6f ssed..** Zeroblo
1ff10 62 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 bs are intended
1ff20 74 6f 20 73 65 72 76 65 20 61 73 20 70 6c 61 63 to serve as plac
1ff30 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 42 4c 4f eholders for BLO
1ff40 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63 6f 6e 74 Bs whose.** cont
1ff50 65 6e 74 20 69 73 20 6c 61 74 65 72 20 77 72 69 ent is later wri
1ff60 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 tten using.** [s
1ff70 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
1ff80 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 | incremental B
1ff90 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74 69 6e 65 LOB I/O] routine
1ffa0 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 s..** A negative
1ffb0 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 7a value for the z
1ffc0 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c 74 73 20 eroblob results
1ffd0 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 in a zero-length
1ffe0 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 BLOB..**.** The
1fff0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 sqlite3_bind_*(
20000 29 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 ) routines must
20010 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 0a be called after.
20020 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
20030 61 72 65 5f 76 32 28 29 5d 20 28 61 6e 64 20 69 are_v2()] (and i
20040 74 73 20 76 61 72 69 61 6e 74 73 29 20 6f 72 20 ts variants) or
20050 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
20060 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f 72 65 20 ] and.** before
20070 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
20080 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73 20 61 72 ..** Bindings ar
20090 65 20 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79 e not cleared by
200a0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 the [sqlite3_re
200b0 73 65 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a set()] routine..
200c0 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d ** Unbound param
200d0 65 74 65 72 73 20 61 72 65 20 69 6e 74 65 72 70 eters are interp
200e0 72 65 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a reted as NULL..*
200f0 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
20100 6e 65 73 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 nes return [SQLI
20110 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 TE_OK] on succes
20120 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f s or an error co
20130 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e de if.** anythin
20140 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 5b g goes wrong. [
20150 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20 69 73 SQLITE_RANGE] is
20160 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 returned if the
20170 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e parameter.** in
20180 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 dex is out of ra
20190 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45 5f 4e 4f nge. [SQLITE_NO
201a0 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 6e 65 64 MEM] is returned
201b0 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 if malloc() fai
201c0 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d ls..** [SQLITE_M
201d0 49 53 55 53 45 5d 20 6d 69 67 68 74 20 62 65 20 ISUSE] might be
201e0 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 73 returned if thes
201f0 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 e routines are c
20200 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20 76 69 alled on a.** vi
20210 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 rtual machine th
20220 61 74 20 69 73 20 74 68 65 20 77 72 6f 6e 67 20 at is the wrong
20230 73 74 61 74 65 20 6f 72 20 77 68 69 63 68 20 68 state or which h
20240 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
20250 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 44 65 finalized..** De
20260 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69 73 75 73 tection of misus
20270 65 20 69 73 20 75 6e 72 65 6c 69 61 62 6c 65 2e e is unreliable.
20280 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 Applications s
20290 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70 65 6e 64 hould not depend
202a0 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45 5f 4d 49 .** on SQLITE_MI
202b0 53 55 53 45 20 72 65 74 75 72 6e 73 2e 20 20 53 SUSE returns. S
202c0 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 73 20 QLITE_MISUSE is
202d0 69 6e 74 65 6e 64 65 64 20 74 6f 20 69 6e 64 69 intended to indi
202e0 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c 6f 67 69 cate a.** a logi
202f0 63 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20 61 c error in the a
20300 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 46 75 74 pplication. Fut
20310 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ure versions of
20320 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20 SQLite might.**
20330 70 61 6e 69 63 20 72 61 74 68 65 72 20 74 68 61 panic rather tha
20340 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f n return SQLITE_
20350 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20 53 65 MISUSE..**.** Se
20360 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
20370 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
20380 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 count()],.** [sq
20390 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
203a0 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e eter_name()], an
203b0 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f d [sqlite3_bind_
203c0 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 parameter_index(
203d0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 )]..**.** Requir
203e0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 35 ements:.** [H135
203f0 30 36 5d 20 5b 48 31 33 35 30 39 5d 20 5b 48 31 06] [H13509] [H1
20400 33 35 31 32 5d 20 5b 48 31 33 35 31 35 5d 20 5b 3512] [H13515] [
20410 48 31 33 35 31 38 5d 20 5b 48 31 33 35 32 31 5d H13518] [H13521]
20420 20 5b 48 31 33 35 32 34 5d 20 5b 48 31 33 35 32 [H13524] [H1352
20430 37 5d 0a 2a 2a 20 5b 48 31 33 35 33 30 5d 20 5b 7].** [H13530] [
20440 48 31 33 35 33 33 5d 20 5b 48 31 33 35 33 36 5d H13533] [H13536]
20450 20 5b 48 31 33 35 33 39 5d 20 5b 48 31 33 35 34 [H13539] [H1354
20460 32 5d 20 5b 48 31 33 35 34 35 5d 20 5b 48 31 33 2] [H13545] [H13
20470 35 34 38 5d 20 5b 48 31 33 35 35 31 5d 0a 2a 2a 548] [H13551].**
20480 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
20490 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
204a0 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d blob(sqlite3_stm
204b0 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 t*, int, const v
204c0 6f 69 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 oid*, int n, voi
204d0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
204e0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
204f0 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 ite3_bind_double
20500 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
20510 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 int, double);.SQ
20520 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
20530 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 ite3_bind_int(sq
20540 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
20550 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 , int);.SQLITE_A
20560 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
20570 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 ind_int64(sqlite
20580 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 73 71 3_stmt*, int, sq
20590 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 lite3_int64);.SQ
205a0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
205b0 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 ite3_bind_null(s
205c0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
205d0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 t);.SQLITE_API i
205e0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
205f0 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d text(sqlite3_stm
20600 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 t*, int, const c
20610 68 61 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 har*, int n, voi
20620 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
20630 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
20640 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 ite3_bind_text16
20650 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
20660 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a int, const void*
20670 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 , int, void(*)(v
20680 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 oid*));.SQLITE_A
20690 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
206a0 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 ind_value(sqlite
206b0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
206c0 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 nst sqlite3_valu
206d0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
206e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
206f0 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 _zeroblob(sqlite
20700 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 3_stmt*, int, in
20710 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 t n);../*.** CAP
20720 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 I3REF: Number Of
20730 20 53 51 4c 20 50 61 72 61 6d 65 74 65 72 73 20 SQL Parameters
20740 7b 48 31 33 36 30 30 7d 20 3c 53 37 30 33 30 30 {H13600} <S70300
20750 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 >.**.** This rou
20760 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 tine can be used
20770 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d to find the num
20780 62 65 72 20 6f 66 20 5b 53 51 4c 20 70 61 72 61 ber of [SQL para
20790 6d 65 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61 20 meters].** in a
207a0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
207b0 65 6e 74 5d 2e 20 20 53 51 4c 20 70 61 72 61 6d ent]. SQL param
207c0 65 74 65 72 73 20 61 72 65 20 74 6f 6b 65 6e 73 eters are tokens
207d0 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 of the.** form
207e0 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a 41 "?", "?NNN", ":A
207f0 41 41 22 2c 20 22 24 41 41 41 22 2c 20 6f 72 20 AA", "$AAA", or
20800 22 40 41 41 41 22 20 74 68 61 74 20 73 65 72 76 "@AAA" that serv
20810 65 20 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f 6c e as.** placehol
20820 64 65 72 73 20 66 6f 72 20 76 61 6c 75 65 73 20 ders for values
20830 74 68 61 74 20 61 72 65 20 5b 73 71 6c 69 74 65 that are [sqlite
20840 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 6f 3_bind_blob | bo
20850 75 6e 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 70 und].** to the p
20860 61 72 61 6d 65 74 65 72 73 20 61 74 20 61 20 6c arameters at a l
20870 61 74 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a ater time..**.**
20880 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 This routine ac
20890 74 75 61 6c 6c 79 20 72 65 74 75 72 6e 73 20 74 tually returns t
208a0 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
208b0 6c 61 72 67 65 73 74 20 28 72 69 67 68 74 6d 6f largest (rightmo
208c0 73 74 29 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 st).** parameter
208d0 2e 20 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20 . For all forms
208e0 65 78 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69 except ?NNN, thi
208f0 73 20 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e s will correspon
20900 64 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 d to the.** numb
20910 65 72 20 6f 66 20 75 6e 69 71 75 65 20 70 61 72 er of unique par
20920 61 6d 65 74 65 72 73 2e 20 20 49 66 20 70 61 72 ameters. If par
20930 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f ameters of the ?
20940 4e 4e 4e 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a NNN are used,.**
20950 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 67 61 there may be ga
20960 70 73 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a ps in the list..
20970 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
20980 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
20990 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 ob|sqlite3_bind(
209a0 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f )],.** [sqlite3_
209b0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e bind_parameter_n
209c0 61 6d 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b ame()], and.** [
209d0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
209e0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e ameter_index()].
209f0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
20a00 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 30 31 5d nts:.** [H13601]
20a10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
20a20 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
20a30 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 parameter_count(
20a40 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
20a50 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
20a60 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f 73 74 20 Name Of A Host
20a70 50 61 72 61 6d 65 74 65 72 20 7b 48 31 33 36 32 Parameter {H1362
20a80 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 0} <S70300>.**.*
20a90 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
20aa0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
20ab0 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 to the name of
20ac0 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53 51 4c the n-th.** [SQL
20ad0 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 61 parameter] in a
20ae0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
20af0 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20 70 61 ment]..** SQL pa
20b00 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 rameters of the
20b10 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 form "?NNN" or "
20b20 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 :AAA" or "@AAA"
20b30 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 68 61 76 or "$AAA".** hav
20b40 65 20 61 20 6e 61 6d 65 20 77 68 69 63 68 20 69 e a name which i
20b50 73 20 74 68 65 20 73 74 72 69 6e 67 20 22 3f 4e s the string "?N
20b60 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 NN" or ":AAA" or
20b70 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 "@AAA" or "$AAA
20b80 22 0a 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c ".** respectivel
20b90 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 y..** In other w
20ba0 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74 69 61 ords, the initia
20bb0 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f 72 20 l ":" or "$" or
20bc0 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a 20 69 73 "@" or "?".** is
20bd0 20 69 6e 63 6c 75 64 65 64 20 61 73 20 70 61 72 included as par
20be0 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 2e 0a 2a t of the name..*
20bf0 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 * Parameters of
20c00 74 68 65 20 66 6f 72 6d 20 22 3f 22 20 77 69 74 the form "?" wit
20c10 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77 69 6e 67 hout a following
20c20 20 69 6e 74 65 67 65 72 20 68 61 76 65 20 6e 6f integer have no
20c30 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 61 72 65 name.** and are
20c40 20 61 6c 73 6f 20 72 65 66 65 72 72 65 64 20 74 also referred t
20c50 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f 75 73 20 o as "anonymous
20c60 70 61 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a parameters"..**.
20c70 2a 2a 20 54 68 65 20 66 69 72 73 74 20 68 6f 73 ** The first hos
20c80 74 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 t parameter has
20c90 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2c 20 6e an index of 1, n
20ca0 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ot 0..**.** If t
20cb0 68 65 20 76 61 6c 75 65 20 6e 20 69 73 20 6f 75 he value n is ou
20cc0 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 t of range or if
20cd0 20 74 68 65 20 6e 2d 74 68 20 70 61 72 61 6d 65 the n-th parame
20ce0 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 6c 65 ter is.** namele
20cf0 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 ss, then NULL is
20d00 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 returned. The
20d10 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
20d20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 69 6e 20 is.** always in
20d30 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 65 UTF-8 encoding e
20d40 76 65 6e 20 69 66 20 74 68 65 20 6e 61 6d 65 64 ven if the named
20d50 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 0a 2a parameter was.*
20d60 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 73 70 65 * originally spe
20d70 63 69 66 69 65 64 20 61 73 20 55 54 46 2d 31 36 cified as UTF-16
20d80 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 in [sqlite3_pre
20d90 70 61 72 65 31 36 28 29 5d 20 6f 72 0a 2a 2a 20 pare16()] or.**
20da0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
20db0 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 16_v2()]..**.**
20dc0 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
20dd0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c e3_bind_blob|sql
20de0 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a ite3_bind()],.**
20df0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
20e00 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 arameter_count()
20e10 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 ], and.** [sqlit
20e20 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
20e30 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a r_index()]..**.*
20e40 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
20e50 2a 2a 20 5b 48 31 33 36 32 31 5d 0a 2a 2f 0a 53 ** [H13621].*/.S
20e60 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
20e70 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 char *sqlite3_bi
20e80 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d nd_parameter_nam
20e90 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
20ea0 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 int);../*.** CA
20eb0 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66 PI3REF: Index Of
20ec0 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 74 A Parameter Wit
20ed0 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b h A Given Name {
20ee0 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e H13640} <S70300>
20ef0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
20f00 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 e index of an SQ
20f10 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 65 L parameter give
20f20 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65 n its name. The
20f30 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20 .** index value
20f40 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 74 returned is suit
20f50 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 able for use as
20f60 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 the second.** pa
20f70 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
20f80 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 te3_bind_blob|sq
20f90 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20 lite3_bind()].
20fa0 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 A zero.** is ret
20fb0 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63 urned if no matc
20fc0 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69 hing parameter i
20fd0 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61 s found. The pa
20fe0 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20 rameter.** name
20ff0 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e must be given in
21000 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74 UTF-8 even if t
21010 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 he original stat
21020 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65 ement.** was pre
21030 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31 pared from UTF-1
21040 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71 6 text using [sq
21050 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
21060 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 v2()]..**.** See
21070 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
21080 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 bind_blob|sqlite
21090 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 3_bind()],.** [s
210a0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
210b0 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 meter_count()],
210c0 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f and.** [sqlite3_
210d0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
210e0 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 ndex()]..**.** R
210f0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
21100 5b 48 31 33 36 34 31 5d 0a 2a 2f 0a 53 51 4c 49 [H13641].*/.SQLI
21110 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
21120 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
21130 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f r_index(sqlite3_
21140 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 stmt*, const cha
21150 72 20 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a r *zName);../*.*
21160 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 * CAPI3REF: Rese
21170 74 20 41 6c 6c 20 42 69 6e 64 69 6e 67 73 20 4f t All Bindings O
21180 6e 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61 n A Prepared Sta
21190 74 65 6d 65 6e 74 20 7b 48 31 33 36 36 30 7d 20 tement {H13660}
211a0 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 43 <S70300>.**.** C
211b0 6f 6e 74 72 61 72 79 20 74 6f 20 74 68 65 20 69 ontrary to the i
211c0 6e 74 75 69 74 69 6f 6e 20 6f 66 20 6d 61 6e 79 ntuition of many
211d0 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 , [sqlite3_reset
211e0 28 29 5d 20 64 6f 65 73 20 6e 6f 74 20 72 65 73 ()] does not res
211f0 65 74 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 et.** the [sqlit
21200 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 e3_bind_blob | b
21210 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 61 20 5b 70 indings] on a [p
21220 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
21230 74 5d 2e 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 t]..** Use this
21240 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 73 65 74 routine to reset
21250 20 61 6c 6c 20 68 6f 73 74 20 70 61 72 61 6d 65 all host parame
21260 74 65 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a ters to NULL..**
21270 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
21280 3a 0a 2a 2a 20 5b 48 31 33 36 36 31 5d 0a 2a 2f :.** [H13661].*/
21290 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
212a0 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 sqlite3_clear_bi
212b0 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 ndings(sqlite3_s
212c0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tmt*);../*.** CA
212d0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f PI3REF: Number O
212e0 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 f Columns In A R
212f0 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 esult Set {H1371
21300 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 0} <S10700>.**.*
21310 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
21320 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
21330 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
21340 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
21350 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
21360 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 atement]. This r
21370 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 outine returns 0
21380 20 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 if pStmt is an
21390 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 SQL.** statement
213a0 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 that does not r
213b0 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 eturn data (for
213c0 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 example an [UPDA
213d0 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 TE])..**.** Requ
213e0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
213f0 33 37 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3711].*/.SQLITE_
21400 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
21410 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c column_count(sql
21420 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
21430 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
21440 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73 EF: Column Names
21450 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 In A Result Set
21460 20 7b 48 31 33 37 32 30 7d 20 3c 53 31 30 37 30 {H13720} <S1070
21470 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
21480 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 outines return t
21490 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 he name assigned
214a0 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 to a particular
214b0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 74 68 column.** in th
214c0 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 e result set of
214d0 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 a [SELECT] state
214e0 6d 65 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74 ment. The sqlit
214f0 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 e3_column_name()
21500 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 65 .** interface re
21510 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
21520 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e to a zero-termin
21530 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e ated UTF-8 strin
21540 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 g.** and sqlite3
21550 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 _column_name16()
21560 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
21570 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 er to a zero-ter
21580 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 minated.** UTF-1
21590 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 66 6 string. The f
215a0 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 irst parameter i
215b0 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 s the [prepared
215c0 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68 statement].** th
215d0 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 at implements th
215e0 65 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 e [SELECT] state
215f0 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 ment. The second
21600 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 parameter is th
21610 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 e.** column numb
21620 65 72 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 er. The leftmos
21630 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62 t column is numb
21640 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 0..**.** The
21650 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
21660 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 pointer is valid
21670 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68 until either th
21680 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
21690 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73 ement].** is des
216a0 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 troyed by [sqlit
216b0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f e3_finalize()] o
216c0 72 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 r until the next
216d0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 call to.** sqli
216e0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 te3_column_name(
216f0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c ) or sqlite3_col
21700 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 6f 6e 20 umn_name16() on
21710 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 2e the same column.
21720 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 .**.** If sqlite
21730 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 3_malloc() fails
21740 20 64 75 72 69 6e 67 20 74 68 65 20 70 72 6f 63 during the proc
21750 65 73 73 69 6e 67 20 6f 66 20 65 69 74 68 65 72 essing of either
21760 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 6f 72 routine.** (for
21770 20 65 78 61 6d 70 6c 65 20 64 75 72 69 6e 67 20 example during
21780 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f a conversion fro
21790 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 m UTF-8 to UTF-1
217a0 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 4e 55 4c 6) then a.** NUL
217b0 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 L pointer is ret
217c0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 urned..**.** The
217d0 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 73 75 6c name of a resul
217e0 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 t column is the
217f0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 41 53 value of the "AS
21800 22 20 63 6c 61 75 73 65 20 66 6f 72 0a 2a 2a 20 " clause for.**
21810 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 that column, if
21820 74 68 65 72 65 20 69 73 20 61 6e 20 41 53 20 63 there is an AS c
21830 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 72 65 lause. If there
21840 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 75 73 65 is no AS clause
21850 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e 61 6d .** then the nam
21860 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 e of the column
21870 69 73 20 75 6e 73 70 65 63 69 66 69 65 64 20 61 is unspecified a
21880 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72 nd may change fr
21890 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65 61 73 om.** one releas
218a0 65 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 74 e of SQLite to t
218b0 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 52 he next..**.** R
218c0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
218d0 5b 48 31 33 37 32 31 5d 20 5b 48 31 33 37 32 33 [H13721] [H13723
218e0 5d 20 5b 48 31 33 37 32 34 5d 20 5b 48 31 33 37 ] [H13724] [H137
218f0 32 35 5d 20 5b 48 31 33 37 32 36 5d 20 5b 48 31 25] [H13726] [H1
21900 33 37 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3727].*/.SQLITE_
21910 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
21920 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
21930 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
21940 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 *, int N);.SQLIT
21950 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
21960 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
21970 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f _name16(sqlite3_
21980 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 0a stmt*, int N);..
21990 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
219a0 53 6f 75 72 63 65 20 4f 66 20 44 61 74 61 20 49 Source Of Data I
219b0 6e 20 41 20 51 75 65 72 79 20 52 65 73 75 6c 74 n A Query Result
219c0 20 7b 48 31 33 37 34 30 7d 20 3c 53 31 30 37 30 {H13740} <S1070
219d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
219e0 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 outines provide
219f0 61 20 6d 65 61 6e 73 20 74 6f 20 64 65 74 65 72 a means to deter
21a00 6d 69 6e 65 20 77 68 61 74 20 63 6f 6c 75 6d 6e mine what column
21a10 20 6f 66 20 77 68 61 74 0a 2a 2a 20 74 61 62 6c of what.** tabl
21a20 65 20 69 6e 20 77 68 69 63 68 20 64 61 74 61 62 e in which datab
21a30 61 73 65 20 61 20 72 65 73 75 6c 74 20 6f 66 20 ase a result of
21a40 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 a [SELECT] state
21a50 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 2e ment comes from.
21a60 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 .** The name of
21a70 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
21a80 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 table or column
21a90 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 can be returned
21aa0 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 55 as.** either a U
21ab0 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 73 TF-8 or UTF-16 s
21ac0 74 72 69 6e 67 2e 20 20 54 68 65 20 5f 64 61 74 tring. The _dat
21ad0 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65 73 20 abase_ routines
21ae0 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64 61 return.** the da
21af0 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74 68 65 tabase name, the
21b00 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e 65 _table_ routine
21b10 73 20 72 65 74 75 72 6e 20 74 68 65 20 74 61 62 s return the tab
21b20 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a 20 le name, and.**
21b30 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75 74 the origin_ rout
21b40 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 ines return the
21b50 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a 20 column name..**
21b60 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 The returned str
21b70 69 6e 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74 ing is valid unt
21b80 69 6c 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 il the [prepared
21b90 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64 statement] is d
21ba0 65 73 74 72 6f 79 65 64 0a 2a 2a 20 75 73 69 6e estroyed.** usin
21bb0 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c g [sqlite3_final
21bc0 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 ize()] or until
21bd0 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 the same informa
21be0 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 tion is requeste
21bf0 64 0a 2a 2a 20 61 67 61 69 6e 20 69 6e 20 61 20 d.** again in a
21c00 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 different encodi
21c10 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 ng..**.** The na
21c20 6d 65 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 mes returned are
21c30 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 75 6e the original un
21c40 2d 61 6c 69 61 73 65 64 20 6e 61 6d 65 73 20 6f -aliased names o
21c50 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 f the.** databas
21c60 65 2c 20 74 61 62 6c 65 2c 20 61 6e 64 20 63 6f e, table, and co
21c70 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 lumn..**.** The
21c80 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
21c90 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
21ca0 63 61 6c 6c 73 20 69 73 20 61 20 5b 70 72 65 70 calls is a [prep
21cb0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e ared statement].
21cc0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 .** These functi
21cd0 6f 6e 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 ons return infor
21ce0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 mation about the
21cf0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 Nth column retu
21d00 72 6e 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 rned by.** the s
21d10 74 61 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20 tatement, where
21d20 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 N is the second
21d30 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
21d40 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 t..**.** If the
21d50 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 Nth column retur
21d60 6e 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 ned by the state
21d70 6d 65 6e 74 20 69 73 20 61 6e 20 65 78 70 72 65 ment is an expre
21d80 73 73 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75 62 71 ssion or.** subq
21d90 75 65 72 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 uery and is not
21da0 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 a column value,
21db0 74 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 73 then all of thes
21dc0 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 e functions retu
21dd0 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54 68 65 rn.** NULL. The
21de0 73 65 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 se routine might
21df0 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 4e 55 4c also return NUL
21e00 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c L if a memory al
21e10 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 0a 2a location error.*
21e20 2a 20 6f 63 63 75 72 73 2e 20 20 4f 74 68 65 72 * occurs. Other
21e30 77 69 73 65 2c 20 74 68 65 79 20 72 65 74 75 72 wise, they retur
21e40 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 n the name of th
21e50 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 e attached datab
21e60 61 73 65 2c 20 74 61 62 6c 65 0a 2a 2a 20 61 6e ase, table.** an
21e70 64 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 71 75 d column that qu
21e80 65 72 79 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d ery result colum
21e90 6e 20 77 61 73 20 65 78 74 72 61 63 74 65 64 20 n was extracted
21ea0 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 from..**.** As w
21eb0 69 74 68 20 61 6c 6c 20 6f 74 68 65 72 20 53 51 ith all other SQ
21ec0 4c 69 74 65 20 41 50 49 73 2c 20 74 68 6f 73 65 Lite APIs, those
21ed0 20 70 6f 73 74 66 69 78 65 64 20 77 69 74 68 20 postfixed with
21ee0 22 31 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20 55 "16" return.** U
21ef0 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 73 74 TF-16 encoded st
21f00 72 69 6e 67 73 2c 20 74 68 65 20 6f 74 68 65 72 rings, the other
21f10 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 functions retur
21f20 6e 20 55 54 46 2d 38 2e 20 7b 45 4e 44 7d 0a 2a n UTF-8. {END}.*
21f30 2a 0a 2a 2a 20 54 68 65 73 65 20 41 50 49 73 20 *.** These APIs
21f40 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 are only availab
21f50 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 le if the librar
21f60 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 y was compiled w
21f70 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 ith the.** [SQLI
21f80 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
21f90 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 _METADATA] C-pre
21fa0 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c processor symbol
21fb0 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 defined..**.**
21fc0 7b 41 31 33 37 35 31 7d 0a 2a 2a 20 49 66 20 74 {A13751}.** If t
21fd0 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 wo or more threa
21fe0 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d ds call one or m
21ff0 6f 72 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 ore of these rou
22000 74 69 6e 65 73 20 61 67 61 69 6e 73 74 20 74 68 tines against th
22010 65 20 73 61 6d 65 0a 2a 2a 20 70 72 65 70 61 72 e same.** prepar
22020 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 ed statement and
22030 20 63 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 73 column at the s
22040 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68 ame time then th
22050 65 20 72 65 73 75 6c 74 73 20 61 72 65 0a 2a 2a e results are.**
22060 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
22070 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
22080 2a 2a 20 5b 48 31 33 37 34 31 5d 20 5b 48 31 33 ** [H13741] [H13
22090 37 34 32 5d 20 5b 48 31 33 37 34 33 5d 20 5b 48 742] [H13743] [H
220a0 31 33 37 34 34 5d 20 5b 48 31 33 37 34 35 5d 20 13744] [H13745]
220b0 5b 48 31 33 37 34 36 5d 20 5b 48 31 33 37 34 38 [H13746] [H13748
220c0 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f ].**.** If two o
220d0 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 r more threads c
220e0 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a all one or more.
220f0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** [sqlite3_colu
22100 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 mn_database_name
22110 20 7c 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61 | column metada
22120 74 61 20 69 6e 74 65 72 66 61 63 65 73 5d 0a 2a ta interfaces].*
22130 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b * for the same [
22140 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
22150 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63 nt] and result c
22160 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 20 74 68 65 20 olumn.** at the
22170 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 same time then t
22180 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 he results are u
22190 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c ndefined..*/.SQL
221a0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
221b0 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ar *sqlite3_colu
221c0 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 mn_database_name
221d0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 (sqlite3_stmt*,i
221e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
221f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
22200 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 te3_column_datab
22210 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 ase_name16(sqlit
22220 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 e3_stmt*,int);.S
22230 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
22240 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
22250 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 lumn_table_name(
22260 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
22270 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 t);.SQLITE_API c
22280 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
22290 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f e3_column_table_
222a0 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 name16(sqlite3_s
222b0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 tmt*,int);.SQLIT
222c0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
222d0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
222e0 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c _origin_name(sql
222f0 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b ite3_stmt*,int);
22300 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
22310 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
22320 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 column_origin_na
22330 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d me16(sqlite3_stm
22340 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t*,int);../*.**
22350 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 CAPI3REF: Declar
22360 65 64 20 44 61 74 61 74 79 70 65 20 4f 66 20 41 ed Datatype Of A
22370 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 7b 48 Query Result {H
22380 31 33 37 36 30 7d 20 3c 53 31 30 37 30 30 3e 0a 13760} <S10700>.
22390 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
223a0 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 5b parameter is a [
223b0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
223c0 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 nt]..** If this
223d0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b statement is a [
223e0 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e SELECT] statemen
223f0 74 20 61 6e 64 20 74 68 65 20 4e 74 68 20 63 6f t and the Nth co
22400 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 lumn of the.** r
22410 65 74 75 72 6e 65 64 20 72 65 73 75 6c 74 20 73 eturned result s
22420 65 74 20 6f 66 20 74 68 61 74 20 5b 53 45 4c 45 et of that [SELE
22430 43 54 5d 20 69 73 20 61 20 74 61 62 6c 65 20 63 CT] is a table c
22440 6f 6c 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a 2a 2a olumn (not an.**
22450 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 expression or s
22460 75 62 71 75 65 72 79 29 20 74 68 65 6e 20 74 68 ubquery) then th
22470 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 20 e declared type
22480 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 of the table.**
22490 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e column is return
224a0 65 64 2e 20 20 49 66 20 74 68 65 20 4e 74 68 20 ed. If the Nth
224b0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 column of the re
224c0 73 75 6c 74 20 73 65 74 20 69 73 20 61 6e 0a 2a sult set is an.*
224d0 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 * expression or
224e0 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e 20 61 subquery, then a
224f0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 NULL pointer is
22500 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 returned..** Th
22510 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e e returned strin
22520 67 20 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d g is always UTF-
22530 38 20 65 6e 63 6f 64 65 64 2e 20 7b 45 4e 44 7d 8 encoded. {END}
22540 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 .**.** For examp
22550 6c 65 2c 20 67 69 76 65 6e 20 74 68 65 20 64 61 le, given the da
22560 74 61 62 61 73 65 20 73 63 68 65 6d 61 3a 0a 2a tabase schema:.*
22570 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c *.** CREATE TABL
22580 45 20 74 31 28 63 31 20 56 41 52 49 41 4e 54 29 E t1(c1 VARIANT)
22590 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ;.**.** and the
225a0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d following statem
225b0 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c ent to be compil
225c0 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 43 54 ed:.**.** SELECT
225d0 20 63 31 20 2b 20 31 2c 20 63 31 20 46 52 4f 4d c1 + 1, c1 FROM
225e0 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 t1;.**.** this
225f0 72 6f 75 74 69 6e 65 20 77 6f 75 6c 64 20 72 65 routine would re
22600 74 75 72 6e 20 74 68 65 20 73 74 72 69 6e 67 20 turn the string
22610 22 56 41 52 49 41 4e 54 22 20 66 6f 72 20 74 68 "VARIANT" for th
22620 65 20 73 65 63 6f 6e 64 20 72 65 73 75 6c 74 0a e second result.
22630 2a 2a 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 31 29 ** column (i==1)
22640 2c 20 61 6e 64 20 61 20 4e 55 4c 4c 20 70 6f 69 , and a NULL poi
22650 6e 74 65 72 20 66 6f 72 20 74 68 65 20 66 69 72 nter for the fir
22660 73 74 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e st result column
22670 20 28 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 53 (i==0)..**.** S
22680 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d QLite uses dynam
22690 69 63 20 72 75 6e 2d 74 69 6d 65 20 74 79 70 69 ic run-time typi
226a0 6e 67 2e 20 20 53 6f 20 6a 75 73 74 20 62 65 63 ng. So just bec
226b0 61 75 73 65 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a ause a column.**
226c0 20 69 73 20 64 65 63 6c 61 72 65 64 20 74 6f 20 is declared to
226d0 63 6f 6e 74 61 69 6e 20 61 20 70 61 72 74 69 63 contain a partic
226e0 75 6c 61 72 20 74 79 70 65 20 64 6f 65 73 20 6e ular type does n
226f0 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 ot mean that the
22700 0a 2a 2a 20 64 61 74 61 20 73 74 6f 72 65 64 20 .** data stored
22710 69 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 in that column i
22720 73 20 6f 66 20 74 68 65 20 64 65 63 6c 61 72 65 s of the declare
22730 64 20 74 79 70 65 2e 20 20 53 51 4c 69 74 65 20 d type. SQLite
22740 69 73 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79 20 74 is.** strongly t
22750 79 70 65 64 2c 20 62 75 74 20 74 68 65 20 74 79 yped, but the ty
22760 70 69 6e 67 20 69 73 20 64 79 6e 61 6d 69 63 20 ping is dynamic
22770 6e 6f 74 20 73 74 61 74 69 63 2e 20 20 54 79 70 not static. Typ
22780 65 0a 2a 2a 20 69 73 20 61 73 73 6f 63 69 61 74 e.** is associat
22790 65 64 20 77 69 74 68 20 69 6e 64 69 76 69 64 75 ed with individu
227a0 61 6c 20 76 61 6c 75 65 73 2c 20 6e 6f 74 20 77 al values, not w
227b0 69 74 68 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 ith the containe
227c0 72 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 68 6f rs.** used to ho
227d0 6c 64 20 74 68 6f 73 65 20 76 61 6c 75 65 73 2e ld those values.
227e0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
227f0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 36 31 5d nts:.** [H13761]
22800 20 5b 48 31 33 37 36 32 5d 20 5b 48 31 33 37 36 [H13762] [H1376
22810 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 3].*/.SQLITE_API
22820 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
22830 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c ite3_column_decl
22840 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d type(sqlite3_stm
22850 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
22860 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
22870 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 sqlite3_column_d
22880 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 ecltype16(sqlite
22890 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 3_stmt*,int);../
228a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
228b0 76 61 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53 valuate An SQL S
228c0 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 32 30 30 tatement {H13200
228d0 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a } <S10000>.**.**
228e0 20 41 66 74 65 72 20 61 20 5b 70 72 65 70 61 72 After a [prepar
228f0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 ed statement] ha
22900 73 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20 s been prepared
22910 75 73 69 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20 using either.**
22920 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
22930 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 _v2()] or [sqlit
22940 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 e3_prepare16_v2(
22950 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 )] or one of the
22960 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 legacy.** inter
22970 66 61 63 65 73 20 5b 73 71 6c 69 74 65 33 5f 70 faces [sqlite3_p
22980 72 65 70 61 72 65 28 29 5d 20 6f 72 20 5b 73 71 repare()] or [sq
22990 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 lite3_prepare16(
229a0 29 5d 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f )], this functio
229b0 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 63 61 6c n.** must be cal
229c0 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 led one or more
229d0 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75 61 74 times to evaluat
229e0 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e e the statement.
229f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 .**.** The detai
22a00 6c 73 20 6f 66 20 74 68 65 20 62 65 68 61 76 69 ls of the behavi
22a10 6f 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 or of the sqlite
22a20 33 5f 73 74 65 70 28 29 20 69 6e 74 65 72 66 61 3_step() interfa
22a30 63 65 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 ce depend.** on
22a40 77 68 65 74 68 65 72 20 74 68 65 20 73 74 61 74 whether the stat
22a50 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 ement was prepar
22a60 65 64 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 ed using the new
22a70 65 72 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 er "v2" interfac
22a80 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 e.** [sqlite3_pr
22a90 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
22aa0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
22ab0 31 36 5f 76 32 28 29 5d 20 6f 72 20 74 68 65 20 16_v2()] or the
22ac0 6f 6c 64 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20 older legacy.**
22ad0 69 6e 74 65 72 66 61 63 65 20 5b 73 71 6c 69 74 interface [sqlit
22ae0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e e3_prepare()] an
22af0 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 d [sqlite3_prepa
22b00 72 65 31 36 28 29 5d 2e 20 20 54 68 65 20 75 73 re16()]. The us
22b10 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 e of the.** new
22b20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 "v2" interface i
22b30 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f s recommended fo
22b40 72 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f r new applicatio
22b50 6e 73 20 62 75 74 20 74 68 65 20 6c 65 67 61 63 ns but the legac
22b60 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 77 y.** interface w
22b70 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 ill continue to
22b80 62 65 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a be supported..**
22b90 0a 2a 2a 20 49 6e 20 74 68 65 20 6c 65 67 61 63 .** In the legac
22ba0 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 y interface, the
22bb0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69 return value wi
22bc0 6c 6c 20 62 65 20 65 69 74 68 65 72 20 5b 53 51 ll be either [SQ
22bd0 4c 49 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b LITE_BUSY],.** [
22be0 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 SQLITE_DONE], [S
22bf0 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c QLITE_ROW], [SQL
22c00 49 54 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b ITE_ERROR], or [
22c10 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a SQLITE_MISUSE]..
22c20 2a 2a 20 57 69 74 68 20 74 68 65 20 22 76 32 22 ** With the "v2"
22c30 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20 interface, any
22c40 6f 66 20 74 68 65 20 6f 74 68 65 72 20 5b 72 65 of the other [re
22c50 73 75 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a sult codes] or.*
22c60 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 * [extended resu
22c70 6c 74 20 63 6f 64 65 73 5d 20 6d 69 67 68 74 20 lt codes] might
22c80 62 65 20 72 65 74 75 72 6e 65 64 20 61 73 20 77 be returned as w
22c90 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 ell..**.** [SQLI
22ca0 54 45 5f 42 55 53 59 5d 20 6d 65 61 6e 73 20 74 TE_BUSY] means t
22cb0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
22cc0 20 65 6e 67 69 6e 65 20 77 61 73 20 75 6e 61 62 engine was unab
22cd0 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 le to acquire th
22ce0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6c 6f e.** database lo
22cf0 63 6b 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20 cks it needs to
22d00 64 6f 20 69 74 73 20 6a 6f 62 2e 20 20 49 66 20 do its job. If
22d10 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 the statement is
22d20 20 61 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f a [COMMIT].** o
22d30 72 20 6f 63 63 75 72 73 20 6f 75 74 73 69 64 65 r occurs outside
22d40 20 6f 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 of an explicit
22d50 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
22d60 6e 20 79 6f 75 20 63 61 6e 20 72 65 74 72 79 20 n you can retry
22d70 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 the.** statement
22d80 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d . If the statem
22d90 65 6e 74 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f ent is not a [CO
22da0 4d 4d 49 54 5d 20 61 6e 64 20 6f 63 63 75 72 73 MMIT] and occurs
22db0 20 77 69 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70 within a.** exp
22dc0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f licit transactio
22dd0 6e 20 74 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c n then you shoul
22de0 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 d rollback the t
22df0 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 ransaction befor
22e00 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e e.** continuing.
22e10 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 .**.** [SQLITE_D
22e20 4f 4e 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 ONE] means that
22e30 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 the statement ha
22e40 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 s finished execu
22e50 74 69 6e 67 0a 2a 2a 20 73 75 63 63 65 73 73 66 ting.** successf
22e60 75 6c 6c 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 ully. sqlite3_s
22e70 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 tep() should not
22e80 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e be called again
22e90 20 6f 6e 20 74 68 69 73 20 76 69 72 74 75 61 6c on this virtual
22ea0 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 77 69 74 68 .** machine with
22eb0 6f 75 74 20 66 69 72 73 74 20 63 61 6c 6c 69 6e out first callin
22ec0 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 g [sqlite3_reset
22ed0 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 ()] to reset the
22ee0 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 virtual.** mach
22ef0 69 6e 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 ine back to its
22f00 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a initial state..*
22f10 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 *.** If the SQL
22f20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 statement being
22f30 65 78 65 63 75 74 65 64 20 72 65 74 75 72 6e 73 executed returns
22f40 20 61 6e 79 20 64 61 74 61 2c 20 74 68 65 6e 20 any data, then
22f50 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 [SQLITE_ROW].**
22f60 69 73 20 72 65 74 75 72 6e 65 64 20 65 61 63 68 is returned each
22f70 20 74 69 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 time a new row
22f80 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64 79 of data is ready
22f90 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 for processing
22fa0 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 by the.** caller
22fb0 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6d 61 79 . The values may
22fc0 20 62 65 20 61 63 63 65 73 73 65 64 20 75 73 69 be accessed usi
22fd0 6e 67 20 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 ng the [column a
22fe0 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d ccess functions]
22ff0 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 ..** sqlite3_ste
23000 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 67 p() is called ag
23010 61 69 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 ain to retrieve
23020 74 68 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 the next row of
23030 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c data..**.** [SQL
23040 49 54 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 ITE_ERROR] means
23050 20 74 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 that a run-time
23060 20 65 72 72 6f 72 20 28 73 75 63 68 20 61 73 20 error (such as
23070 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 a constraint.**
23080 76 69 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f violation) has o
23090 63 63 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65 ccurred. sqlite
230a0 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 3_step() should
230b0 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 not be called ag
230c0 61 69 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d ain on.** the VM
230d0 2e 20 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 . More informati
230e0 6f 6e 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 on may be found
230f0 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 by calling [sqli
23100 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a te3_errmsg()]..*
23110 2a 20 57 69 74 68 20 74 68 65 20 6c 65 67 61 63 * With the legac
23120 79 20 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d y interface, a m
23130 6f 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72 ore specific err
23140 6f 72 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61 or code (for exa
23150 6d 70 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 mple,.** [SQLITE
23160 5f 49 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 _INTERRUPT], [SQ
23170 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 LITE_SCHEMA], [S
23180 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 QLITE_CORRUPT],
23190 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a and so forth).**
231a0 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 can be obtained
231b0 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c by calling [sql
231c0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e ite3_reset()] on
231d0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
231e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 d statement]. I
231f0 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 n the "v2" inter
23200 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 face,.** the mor
23210 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 e specific error
23220 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
23230 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 73 71 d directly by sq
23240 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a lite3_step()..**
23250 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 .** [SQLITE_MISU
23260 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 SE] means that t
23270 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 he this routine
23280 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70 was called inapp
23290 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 ropriately..** P
232a0 65 72 68 61 70 73 20 69 74 20 77 61 73 20 63 61 erhaps it was ca
232b0 6c 6c 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61 lled on a [prepa
232c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 red statement] t
232d0 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 hat has.** alrea
232e0 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33 dy been [sqlite3
232f0 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 _finalize | fina
23300 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 lized] or on one
23310 20 74 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65 that had.** pre
23320 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 viously returned
23330 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
23340 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d or [SQLITE_DONE]
23350 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a . Or it could.*
23360 2a 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 * be the case th
23370 61 74 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 at the same data
23380 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
23390 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 is being used by
233a0 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 two or.** more
233b0 74 68 72 65 61 64 73 20 61 74 20 74 68 65 20 73 threads at the s
233c0 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 ame moment in ti
233d0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f me..**.** <b>Goo
233e0 66 79 20 49 6e 74 65 72 66 61 63 65 20 41 6c 65 fy Interface Ale
233f0 72 74 3a 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c rt:</b> In the l
23400 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c egacy interface,
23410 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 the sqlite3_ste
23420 70 28 29 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79 p().** API alway
23430 73 20 72 65 74 75 72 6e 73 20 61 20 67 65 6e 65 s returns a gene
23440 72 69 63 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 ric error code,
23450 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 [SQLITE_ERROR],
23460 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a following any.**
23470 20 65 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61 error other tha
23480 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 n [SQLITE_BUSY]
23490 61 6e 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 and [SQLITE_MISU
234a0 53 45 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 SE]. You must c
234b0 61 6c 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f all.** [sqlite3_
234c0 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c reset()] or [sql
234d0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d ite3_finalize()]
234e0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e in order to fin
234f0 64 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 d one of the.**
23500 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20 specific [error
23510 63 6f 64 65 73 5d 20 74 68 61 74 20 62 65 74 74 codes] that bett
23520 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68 65 er describes the
23530 20 65 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 error..** We ad
23540 6d 69 74 20 74 68 61 74 20 74 68 69 73 20 69 73 mit that this is
23550 20 61 20 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e a goofy design.
23560 20 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 68 61 The problem ha
23570 73 20 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20 s been fixed.**
23580 77 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e with the "v2" in
23590 74 65 72 66 61 63 65 2e 20 20 49 66 20 79 6f 75 terface. If you
235a0 20 70 72 65 70 61 72 65 20 61 6c 6c 20 6f 66 20 prepare all of
235b0 79 6f 75 72 20 53 51 4c 20 73 74 61 74 65 6d 65 your SQL stateme
235c0 6e 74 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74 nts.** using eit
235d0 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 her [sqlite3_pre
235e0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 pare_v2()] or [s
235f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
23600 5f 76 32 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a _v2()] instead.*
23610 2a 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 20 * of the legacy
23620 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
23630 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
23640 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 69 6e _prepare16()] in
23650 74 65 72 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65 terfaces,.** the
23660 6e 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 n the more speci
23670 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 fic [error codes
23680 5d 20 61 72 65 20 72 65 74 75 72 6e 65 64 20 64 ] are returned d
23690 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71 irectly.** by sq
236a0 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 54 lite3_step(). T
236b0 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 22 76 he use of the "v
236c0 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 2" interface is
236d0 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a recommended..**.
236e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
236f0 0a 2a 2a 20 5b 48 31 33 32 30 32 5d 20 5b 48 31 .** [H13202] [H1
23700 35 33 30 34 5d 20 5b 48 31 35 33 30 36 5d 20 5b 5304] [H15306] [
23710 48 31 35 33 30 38 5d 20 5b 48 31 35 33 31 30 5d H15308] [H15310]
23720 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
23730 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 nt sqlite3_step(
23740 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
23750 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
23760 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
23770 6e 73 20 69 6e 20 61 20 72 65 73 75 6c 74 20 73 ns in a result s
23780 65 74 20 7b 48 31 33 37 37 30 7d 20 3c 53 31 30 et {H13770} <S10
23790 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 700>.**.** Retur
237a0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
237b0 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63 values in the c
237c0 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 urrent row of th
237d0 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a e result set..**
237e0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
237f0 3a 0a 2a 2a 20 5b 48 31 33 37 37 31 5d 20 5b 48 :.** [H13771] [H
23800 31 33 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13772].*/.SQLITE
23810 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
23820 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 _data_count(sqli
23830 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
23840 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
23850 46 3a 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 F: Fundamental D
23860 61 74 61 74 79 70 65 73 20 7b 48 31 30 32 36 35 atatypes {H10265
23870 7d 20 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32 } <S10110><S1012
23880 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
23890 53 51 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a SQLITE_TEXT.**.*
238a0 2a 20 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79 * {H10266} Every
238b0 20 76 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 value in SQLite
238c0 20 68 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 has one of five
238d0 20 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 fundamental dat
238e0 61 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 atypes:.**.** <u
238f0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 l>.** <li> 64-bi
23900 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
23910 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 .** <li> 64-bit
23920 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f IEEE floating po
23930 69 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c int number.** <l
23940 69 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 i> string.** <li
23950 3e 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e > BLOB.** <li> N
23960 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e ULL.** </ul> {EN
23970 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 D}.**.** These c
23980 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64 onstants are cod
23990 65 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 es for each of t
239a0 68 6f 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a hose types..**.*
239b0 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * Note that the
239c0 53 51 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 SQLITE_TEXT cons
239d0 74 61 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73 tant was also us
239e0 65 64 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 ed in SQLite ver
239f0 73 69 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 sion 2.** for a
23a00 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65 completely diffe
23a10 72 65 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 rent meaning. S
23a20 6f 66 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e oftware that lin
23a30 6b 73 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a ks against both.
23a40 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f ** SQLite versio
23a50 6e 20 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76 n 2 and SQLite v
23a60 65 72 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 ersion 3 should
23a70 75 73 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 use SQLITE3_TEXT
23a80 2c 20 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f , not.** SQLITE_
23a90 54 45 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 TEXT..*/.#define
23aa0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 SQLITE_INTEGER
23ab0 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
23ac0 45 5f 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 E_FLOAT 2.#de
23ad0 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 fine SQLITE_BLOB
23ae0 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 4.#define S
23af0 51 4c 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 QLITE_NULL 5
23b00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
23b10 45 58 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 EXT.# undef SQLI
23b20 54 45 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 TE_TEXT.#else.#
23b30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
23b40 58 54 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a XT 3.#endif.
23b50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f #define SQLITE3_
23b60 54 45 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a TEXT 3../*.*
23b70 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 * CAPI3REF: Resu
23b80 6c 74 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 lt Values From A
23b90 20 51 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20 Query {H13800}
23ba0 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 <S10700>.** KEYW
23bb0 4f 52 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 ORDS: {column ac
23bc0 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a cess functions}.
23bd0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
23be0 69 6e 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72 ines form the "r
23bf0 65 73 75 6c 74 20 73 65 74 20 71 75 65 72 79 22 esult set query"
23c00 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
23c10 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
23c20 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 return informat
23c30 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67 ion about a sing
23c40 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 le column of the
23c50 20 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 current.** resu
23c60 6c 74 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72 lt row of a quer
23c70 79 2e 20 20 49 6e 20 65 76 65 72 79 20 63 61 73 y. In every cas
23c80 65 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 e the first argu
23c90 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 ment is a pointe
23ca0 72 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 r.** to the [pre
23cb0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
23cc0 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 65 that is being e
23cd0 76 61 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73 valuated (the [s
23ce0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a qlite3_stmt*].**
23cf0 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e that was return
23d00 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
23d10 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f _prepare_v2()] o
23d20 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72 r one of its var
23d30 69 61 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 iants).** and th
23d40 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
23d50 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f t is the index o
23d60 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 f the column for
23d70 20 77 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 which informati
23d80 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 on.** should be
23d90 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c returned. The l
23da0 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f eftmost column o
23db0 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
23dc0 20 68 61 73 20 74 68 65 20 69 6e 64 65 78 20 30 has the index 0
23dd0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 ..**.** If the S
23de0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 6f 65 QL statement doe
23df0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
23e00 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 point to a valid
23e10 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 65 0a row, or if the.
23e20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 ** column index
23e30 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c is out of range,
23e40 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 the result is u
23e50 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 ndefined..** The
23e60 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 79 20 se routines may
23e70 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 only be called w
23e80 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 hen the most rec
23e90 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b ent call to.** [
23ea0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
23eb0 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51 has returned [SQ
23ec0 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 LITE_ROW] and ne
23ed0 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 ither.** [sqlite
23ee0 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 3_reset()] nor [
23ef0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
23f00 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 63 61 ()] have been ca
23f10 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e 74 6c lled subsequentl
23f20 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 y..** If any of
23f30 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 these routines a
23f40 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 re called after
23f50 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
23f60 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ] or.** [sqlite3
23f70 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 _finalize()] or
23f80 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 after [sqlite3_s
23f90 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 tep()] has retur
23fa0 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 ned.** something
23fb0 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c other than [SQL
23fc0 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 ITE_ROW], the re
23fd0 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 sults are undefi
23fe0 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 ned..** If [sqli
23ff0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b te3_step()] or [
24000 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
24010 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e or [sqlite3_fin
24020 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 alize()].** are
24030 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 called from a di
24040 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 77 fferent thread w
24050 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 65 73 hile any of thes
24060 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 e routines.** ar
24070 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 e pending, then
24080 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 the results are
24090 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
240a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c The sqlite3_col
240b0 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 74 69 umn_type() routi
240c0 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a ne returns the.*
240d0 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 * [SQLITE_INTEGE
240e0 52 20 7c 20 64 61 74 61 74 79 70 65 20 63 6f 64 R | datatype cod
240f0 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69 74 69 e] for the initi
24100 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 al data type.**
24110 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f of the result co
24120 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74 75 72 lumn. The retur
24130 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 ned value is one
24140 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 of [SQLITE_INTE
24150 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 GER],.** [SQLITE
24160 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 _FLOAT], [SQLITE
24170 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f _TEXT], [SQLITE_
24180 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 BLOB], or [SQLIT
24190 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 E_NULL]. The va
241a0 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 lue.** returned
241b0 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d by sqlite3_colum
241c0 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 n_type() is only
241d0 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e meaningful if n
241e0 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 o type.** conver
241f0 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63 75 72 sions have occur
24200 72 65 64 20 61 73 20 64 65 73 63 72 69 62 65 64 red as described
24210 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 below. After a
24220 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e type conversion
24230 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72 ,.** the value r
24240 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
24250 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 e3_column_type()
24260 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 is undefined.
24270 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f Future.** versio
24280 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 ns of SQLite may
24290 20 63 68 61 6e 67 65 20 74 68 65 20 62 65 68 61 change the beha
242a0 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f vior of sqlite3_
242b0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a column_type().**
242c0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 following a typ
242d0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a e conversion..**
242e0 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c .** If the resul
242f0 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 t is a BLOB or U
24300 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68 65 6e TF-8 string then
24310 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c the sqlite3_col
24320 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72 umn_bytes().** r
24330 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
24340 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
24350 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 es in that BLOB
24360 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 or string..** If
24370 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 the result is a
24380 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 UTF-16 string,
24390 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c then sqlite3_col
243a0 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 umn_bytes() conv
243b0 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 erts.** the stri
243c0 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 ng to UTF-8 and
243d0 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 then returns the
243e0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
243f0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 ..** If the resu
24400 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20 lt is a numeric
24410 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74 value then sqlit
24420 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 e3_column_bytes(
24430 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 ) uses.** [sqlit
24440 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 e3_snprintf()] t
24450 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 76 o convert that v
24460 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d 38 20 alue to a UTF-8
24470 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 string and retur
24480 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 ns.** the number
24490 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 of bytes in tha
244a0 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 t string..** The
244b0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
244c0 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 does not include
244d0 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e the zero termin
244e0 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e 64 0a ator at the end.
244f0 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 ** of the string
24500 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 . For clarity:
24510 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
24520 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ed is the number
24530 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 of.** bytes in
24540 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 the string, not
24550 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
24560 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 aracters..**.**
24570 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 Strings returned
24580 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 by sqlite3_colu
24590 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71 mn_text() and sq
245a0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
245b0 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 t16(),.** even e
245c0 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20 61 72 mpty strings, ar
245d0 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20 74 65 e always zero te
245e0 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 72 rminated. The r
245f0 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 eturn.** value f
24600 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 rom sqlite3_colu
24610 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 mn_blob() for a
24620 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 zero-length BLOB
24630 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 is an arbitrary
24640 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73 .** pointer, pos
24650 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c sibly even a NUL
24660 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a L pointer..**.**
24670 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c The sqlite3_col
24680 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 72 6f umn_bytes16() ro
24690 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 utine is similar
246a0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
246b0 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 62 75 mn_bytes().** bu
246c0 74 20 6c 65 61 76 65 73 20 74 68 65 20 72 65 73 t leaves the res
246d0 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20 69 6e ult in UTF-16 in
246e0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
246f0 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 er instead of UT
24700 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f F-8..** The zero
24710 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e terminator is n
24720 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 ot included in t
24730 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a his count..**.**
24740 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65 74 75 The object retu
24750 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 rned by [sqlite3
24760 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d _column_value()]
24770 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f is an.** [unpro
24780 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
24790 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41 alue] object. A
247a0 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 n unprotected sq
247b0 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
247c0 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 ct.** may only b
247d0 65 20 75 73 65 64 20 77 69 74 68 20 5b 73 71 6c e used with [sql
247e0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 ite3_bind_value(
247f0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
24800 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e result_value()].
24810 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e 70 72 .** If the [unpr
24820 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
24830 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 72 65 value] object re
24840 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 turned by.** [sq
24850 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c lite3_column_val
24860 75 65 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e ue()] is used in
24870 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79 2c 20 any other way,
24880 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a including calls.
24890 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c ** to routines l
248a0 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c ike [sqlite3_val
248b0 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 ue_int()], [sqli
248c0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 te3_value_text()
248d0 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 ],.** or [sqlite
248e0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 3_value_bytes()]
248f0 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 , then the behav
24900 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
24910 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
24920 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 20 74 utines attempt t
24930 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 o convert the va
24940 6c 75 65 20 77 68 65 72 65 20 61 70 70 72 6f 70 lue where approp
24950 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 riate. For.** e
24960 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 69 xample, if the i
24970 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e nternal represen
24980 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 tation is FLOAT
24990 61 6e 64 20 61 20 74 65 78 74 20 72 65 73 75 6c and a text resul
249a0 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 t.** is requeste
249b0 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 d, [sqlite3_snpr
249c0 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65 64 20 intf()] is used
249d0 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 internally to pe
249e0 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e rform the.** con
249f0 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 version automati
24a00 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c cally. The foll
24a10 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 74 61 owing table deta
24a20 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 73 69 ils the conversi
24a30 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 ons.** that are
24a40 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c applied:.**.** <
24a50 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c blockquote>.** <
24a60 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 table border="1"
24a70 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e >.** <tr><th> In
24a80 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c ternal<br>Type <
24a90 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c 62 72 th> Requested<br
24aa0 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 >Type <th> Conv
24ab0 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 ersion.**.** <tr
24ac0 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c ><td> NULL <
24ad0 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 td> INTEGER <t
24ae0 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 0a 2a d> Result is 0.*
24af0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c * <tr><td> NULL
24b00 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <td> FLOAT
24b10 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 <td> Result i
24b20 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 s 0.0.** <tr><td
24b30 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 > NULL <td>
24b40 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 52 TEXT <td> R
24b50 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f esult is NULL po
24b60 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 inter.** <tr><td
24b70 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 > NULL <td>
24b80 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 BLOB <td> R
24b90 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f esult is NULL po
24ba0 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 inter.** <tr><td
24bb0 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 > INTEGER <td>
24bc0 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 FLOAT <td> C
24bd0 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 onvert from inte
24be0 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 ger to float.**
24bf0 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 <tr><td> INTEGER
24c00 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 <td> TEXT
24c10 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 <td> ASCII rend
24c20 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 ering of the int
24c30 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e eger.** <tr><td>
24c40 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 INTEGER <td>
24c50 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 BLOB <td> Sa
24c60 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 me as INTEGER->T
24c70 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 EXT.** <tr><td>
24c80 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e FLOAT <td> IN
24c90 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e TEGER <td> Con
24ca0 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 vert from float
24cb0 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 to integer.** <t
24cc0 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 r><td> FLOAT
24cd0 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c <td> TEXT <
24ce0 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 td> ASCII render
24cf0 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 ing of the float
24d00 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c .** <tr><td> FL
24d10 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f OAT <td> BLO
24d20 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 B <td> Same a
24d30 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a s FLOAT->TEXT.**
24d40 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 <tr><td> TEXT
24d50 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 <td> INTEGER
24d60 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 69 28 <td> Use atoi(
24d70 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 ).** <tr><td> T
24d80 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f EXT <td> FLO
24d90 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 20 61 AT <td> Use a
24da0 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 tof().** <tr><td
24db0 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 > TEXT <td>
24dc0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e BLOB <td> N
24dd0 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e o change.** <tr>
24de0 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 <td> BLOB <t
24df0 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 d> INTEGER <td
24e00 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 > Convert to TEX
24e10 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 69 28 T then use atoi(
24e20 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 ).** <tr><td> B
24e30 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f LOB <td> FLO
24e40 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 AT <td> Conve
24e50 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20 rt to TEXT then
24e60 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 use atof().** <t
24e70 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 r><td> BLOB
24e80 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c <td> TEXT <
24e90 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f 20 74 td> Add a zero t
24ea0 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 erminator if nee
24eb0 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a ded.** </table>.
24ec0 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e ** </blockquote>
24ed0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 .**.** The table
24ee0 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 above makes ref
24ef0 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 erence to standa
24f00 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e rd C library fun
24f10 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a ctions atoi().**
24f20 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51 and atof(). SQ
24f30 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 Lite does not re
24f40 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20 66 ally use these f
24f50 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 unctions. It ha
24f60 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 s its.** own equ
24f70 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c ivalent internal
24f80 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20 routines. The
24f90 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28 atoi() and atof(
24fa0 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 ) names are.** u
24fb0 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 sed in the table
24fc0 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64 for brevity and
24fd0 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72 because they ar
24fe0 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f e familiar to mo
24ff0 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d st.** C programm
25000 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ers..**.** Note
25010 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 20 63 that when type c
25020 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 onversions occur
25030 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 , pointers retur
25040 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 ned by prior.**
25050 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
25060 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 _column_blob(),
25070 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
25080 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a ext(), and/or.**
25090 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
250a0 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 65 20 text16() may be
250b0 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 invalidated..**
250c0 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 Type conversions
250d0 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 and pointer inv
250e0 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 alidations might
250f0 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 occur.** in the
25100 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 following cases
25110 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
25120 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c <li> The initial
25130 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c content is a BL
25140 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 OB and sqlite3_c
25150 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a olumn_text() or.
25160 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ** sqlite3_
25170 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 column_text16()
25180 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 is called. A ze
25190 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 ro-terminator mi
251a0 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 ght.** need
251b0 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 to be added to
251c0 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e the string.</li>
251d0 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 .** <li> The ini
251e0 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 tial content is
251f0 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64 20 73 UTF-8 text and s
25200 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
25210 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 tes16() or.**
25220 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d sqlite3_colum
25230 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61 n_text16() is ca
25240 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 lled. The conte
25250 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 nt must be conve
25260 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 rted.** to
25270 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 UTF-16.</li>.**
25280 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c <li> The initial
25290 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d content is UTF-
252a0 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69 16 text and sqli
252b0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
252c0 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 () or.** sq
252d0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
252e0 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 t() is called.
252f0 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 The content must
25300 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a be converted.**
25310 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c to UTF-8.<
25320 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a /li>.** </ul>.**
25330 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 .** Conversions
25340 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62 65 between UTF-16be
25350 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 72 and UTF-16le ar
25360 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e e always done in
25370 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a place and do.**
25380 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 not invalidate
25390 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c a prior pointer,
253a0 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 though of cours
253b0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
253c0 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 the buffer.** t
253d0 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70 6f hat the prior po
253e0 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 inter points to
253f0 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 6d will have been m
25400 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72 20 odified. Other
25410 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76 kinds.** of conv
25420 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20 ersion are done
25430 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69 74 in place when it
25440 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62 75 is possible, bu
25450 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 65 79 t sometimes they
25460 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 .** are not poss
25470 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 ible and in thos
25480 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70 6f e cases prior po
25490 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61 6c inters are inval
254a0 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 idated..**.** Th
254b0 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 61 73 e safest and eas
254c0 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 iest to remember
254d0 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 6e policy is to in
254e0 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74 69 voke these routi
254f0 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 nes.** in one of
25500 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 the following w
25510 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a ays:.**.** <ul>.
25520 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f ** <li>sqlite3_
25530 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f column_text() fo
25540 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 llowed by sqlite
25550 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3_column_bytes()
25560 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 </li>.** <li>sq
25570 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f lite3_column_blo
25580 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 b() followed by
25590 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
255a0 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 ytes()</li>.**
255b0 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 <li>sqlite3_colu
255c0 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c 6c mn_text16() foll
255d0 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f owed by sqlite3_
255e0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 column_bytes16()
255f0 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a </li>.** </ul>.*
25600 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f *.** In other wo
25610 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20 rds, you should
25620 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c call sqlite3_col
25630 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73 umn_text(),.** s
25640 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c qlite3_column_bl
25650 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 ob(), or sqlite3
25660 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 _column_text16()
25670 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65 20 first to force
25680 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e the result.** in
25690 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 20 66 to the desired f
256a0 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f ormat, then invo
256b0 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ke sqlite3_colum
256c0 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 n_bytes() or.**
256d0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
256e0 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 6e 64 ytes16() to find
256f0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
25700 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74 result. Do not
25710 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f mix calls.** to
25720 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
25730 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 74 65 text() or sqlite
25740 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 3_column_blob()
25750 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a with calls to.**
25760 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
25770 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 20 64 bytes16(), and d
25780 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20 o not mix calls
25790 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d to sqlite3_colum
257a0 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 77 69 n_text16().** wi
257b0 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 th calls to sqli
257c0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
257d0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f ()..**.** The po
257e0 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 inters returned
257f0 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 are valid until
25800 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f a type conversio
25810 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 n occurs as.** d
25820 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20 escribed above,
25830 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 or until [sqlite
25840 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 3_step()] or [sq
25850 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f lite3_reset()] o
25860 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 r.** [sqlite3_fi
25870 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 61 6c nalize()] is cal
25880 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 led. The memory
25890 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 68 space used to h
258a0 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 old strings.** a
258b0 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72 65 65 nd BLOBs is free
258c0 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e d automatically.
258d0 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 Do <b>not</b>
258e0 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72 pass the pointer
258f0 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 s returned.** [s
25900 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c qlite3_column_bl
25910 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f ob()], [sqlite3_
25920 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 column_text()],
25930 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71 etc. into.** [sq
25940 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a lite3_free()]..*
25950 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 *.** If a memory
25960 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
25970 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 r occurs during
25980 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f the evaluation o
25990 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 f any.** of thes
259a0 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 e routines, a de
259b0 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20 72 fault value is r
259c0 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 64 65 eturned. The de
259d0 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 fault value.** i
259e0 73 20 65 69 74 68 65 72 20 74 68 65 20 69 6e 74 s either the int
259f0 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 eger 0, the floa
25a00 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 ting point numbe
25a10 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c r 0.0, or a NULL
25a20 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 .** pointer. Su
25a30 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
25a40 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f o [sqlite3_errco
25a50 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 de()] will retur
25a60 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d n.** [SQLITE_NOM
25a70 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 EM]..**.** Requi
25a80 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
25a90 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20 5b 48 803] [H13806] [H
25aa0 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32 5d 20 13809] [H13812]
25ab0 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38 31 38 [H13815] [H13818
25ac0 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31 33 38 ] [H13821] [H138
25ad0 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37 5d 20 24].** [H13827]
25ae0 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51 4c 49 [H13830].*/.SQLI
25af0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
25b00 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
25b10 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 n_blob(sqlite3_s
25b20 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
25b30 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
25b40 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
25b50 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ytes(sqlite3_stm
25b60 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
25b70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
25b80 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
25b90 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d es16(sqlite3_stm
25ba0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
25bb0 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 QLITE_API double
25bc0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
25bd0 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 double(sqlite3_s
25be0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
25bf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
25c00 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 sqlite3_column_i
25c10 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a nt(sqlite3_stmt*
25c20 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c , int iCol);.SQL
25c30 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
25c40 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 6f int64 sqlite3_co
25c50 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74 lumn_int64(sqlit
25c60 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 e3_stmt*, int iC
25c70 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ol);.SQLITE_API
25c80 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
25c90 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
25ca0 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33 umn_text(sqlite3
25cb0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
25cc0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
25cd0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
25ce0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
25cf0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
25d00 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 nt iCol);.SQLITE
25d10 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
25d20 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c _column_type(sql
25d30 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
25d40 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 iCol);.SQLITE_AP
25d50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 I sqlite3_value
25d60 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
25d70 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74 value(sqlite3_st
25d80 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a mt*, int iCol);.
25d90 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
25da0 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 70 61 Destroy A Prepa
25db0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 red Statement Ob
25dc0 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 3c 53 ject {H13300} <S
25dd0 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e 0a 2a 70300><S30100>.*
25de0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
25df0 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 6e 63 _finalize() func
25e00 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 tion is called t
25e10 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 65 70 o delete a [prep
25e20 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e ared statement].
25e30 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 .** If the state
25e40 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 74 65 ment was execute
25e50 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f d successfully o
25e60 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 20 61 r not executed a
25e70 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a 20 53 t all, then.** S
25e80 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
25e90 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 74 69 rned. If executi
25ea0 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d on of the statem
25eb0 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 6e 20 ent failed then
25ec0 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 an.** [error cod
25ed0 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 e] or [extended
25ee0 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 error code] is r
25ef0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
25f00 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 his routine can
25f10 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79 be called at any
25f20 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 74 68 point during th
25f30 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 e execution of t
25f40 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 he.** [prepared
25f50 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 66 20 statement]. If
25f60 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
25f70 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 ine has not.** c
25f80 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 69 ompleted executi
25f90 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 on when this rou
25fa0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 tine is called,
25fb0 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a 2a 20 that is like.**
25fc0 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 6e 20 encountering an
25fd0 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 71 6c error or an [sql
25fe0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 7c ite3_interrupt |
25ff0 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a 2a 20 interrupt]..**
26000 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 61 74 Incomplete updat
26010 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 es may be rolled
26020 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e 73 61 back and transa
26030 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 64 2c ctions canceled,
26040 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e .** depending on
26050 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 6e 63 the circumstanc
26060 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b es, and the.** [
26070 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 74 75 error code] retu
26080 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b 53 51 rned will be [SQ
26090 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a LITE_ABORT]..**.
260a0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
260b0 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b 48 31 .** [H11302] [H1
260c0 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 1304].*/.SQLITE_
260d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
260e0 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 finalize(sqlite3
260f0 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a _stmt *pStmt);..
26100 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
26110 52 65 73 65 74 20 41 20 50 72 65 70 61 72 65 64 Reset A Prepared
26120 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 Statement Objec
26130 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37 30 33 t {H13330} <S703
26140 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
26150 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 66 75 lite3_reset() fu
26160 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
26170 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70 72 65 to reset a [pre
26180 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
26190 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63 6b 20 .** object back
261a0 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 to its initial s
261b0 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f 20 62 tate, ready to b
261c0 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e 0a 2a e re-executed..*
261d0 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74 65 6d * Any SQL statem
261e0 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20 74 68 ent variables th
261f0 61 74 20 68 61 64 20 76 61 6c 75 65 73 20 62 6f at had values bo
26200 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73 69 6e und to them usin
26210 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 g.** the [sqlite
26220 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 3_bind_blob | sq
26230 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 41 lite3_bind_*() A
26240 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65 69 72 PI] retain their
26250 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 65 20 values..** Use
26260 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 [sqlite3_clear_b
26270 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 72 65 indings()] to re
26280 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e 67 73 set the bindings
26290 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 32 7d ..**.** {H11332}
262a0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 The [sqlite3_re
262b0 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 set(S)] interfac
262c0 65 20 72 65 73 65 74 73 20 74 68 65 20 5b 70 72 e resets the [pr
262d0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
262e0 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ] S.**
262f0 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67 69 back to the begi
26300 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 72 6f nning of its pro
26310 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 gram..**.** {H11
26320 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f 73 74 334} If the most
26330 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 recent call to
26340 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 [sqlite3_step(S)
26350 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 ] for the.**
26360 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 [prepared
26370 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72 65 74 statement] S ret
26380 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 4f urned [SQLITE_RO
26390 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f W] or [SQLITE_DO
263a0 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 NE],.**
263b0 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f or if [sqlite3_
263c0 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e 65 76 step(S)] has nev
263d0 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 63 er before been c
263e0 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a 20 20 alled on S,.**
263f0 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 then [sq
26400 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 lite3_reset(S)]
26410 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
26420 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 OK]..**.** {H113
26430 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73 74 20 36} If the most
26440 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b recent call to [
26450 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d sqlite3_step(S)]
26460 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 for the.**
26470 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 [prepared s
26480 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 64 69 tatement] S indi
26490 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 cated an error,
264a0 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 then.**
264b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
264c0 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e 20 61 S)] returns an a
264d0 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f ppropriate [erro
264e0 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b r code]..**.** {
264f0 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73 71 6c H11338} The [sql
26500 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 69 ite3_reset(S)] i
26510 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 6e 6f nterface does no
26520 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c t change the val
26530 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ues.**
26540 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 33 5f of any [sqlite3_
26550 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 69 6e bind_blob|bindin
26560 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 65 70 gs] on the [prep
26570 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
26580 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 S..*/.SQLITE_API
26590 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 73 int sqlite3_res
265a0 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 et(sqlite3_stmt
265b0 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *pStmt);../*.**
265c0 43 41 50 49 33 52 45 46 3a 20 43 72 65 61 74 65 CAPI3REF: Create
265d0 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53 51 4c Or Redefine SQL
265e0 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 31 Functions {H161
265f0 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 00} <S20200>.**
26600 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e 63 74 KEYWORDS: {funct
26610 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75 ion creation rou
26620 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 tines}.** KEYWOR
26630 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f 6e DS: {application
26640 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e -defined SQL fun
26650 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57 4f 52 ction}.** KEYWOR
26660 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f 6e DS: {application
26670 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e -defined SQL fun
26680 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 ctions}.**.** Th
26690 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e ese two function
266a0 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c 79 20 s (collectively
266b0 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63 74 69 known as "functi
266c0 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75 74 on creation rout
266d0 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20 75 73 ines").** are us
266e0 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 66 75 ed to add SQL fu
266f0 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65 nctions or aggre
26700 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65 64 65 gates or to rede
26710 66 69 6e 65 20 74 68 65 20 62 65 68 61 76 69 6f fine the behavio
26720 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69 6e 67 r.** of existing
26730 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f SQL functions o
26740 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 54 r aggregates. T
26750 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e he only differen
26760 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 0a 2a ce between the.*
26770 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20 74 68 * two is that th
26780 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
26790 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 er, the name of
267a0 74 68 65 20 28 73 63 61 6c 61 72 29 20 66 75 6e the (scalar) fun
267b0 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 ction or.** aggr
267c0 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f 64 65 egate, is encode
267d0 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 20 73 d in UTF-8 for s
267e0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
267f0 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55 54 46 nction() and UTF
26800 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 -16.** for sqlit
26810 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
26820 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 on16()..**.** Th
26830 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
26840 72 20 69 73 20 74 68 65 20 5b 64 61 74 61 62 61 r is the [databa
26850 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 se connection] t
26860 6f 20 77 68 69 63 68 20 74 68 65 20 53 51 4c 0a o which the SQL.
26870 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 ** function is t
26880 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49 66 20 o be added. If
26890 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72 61 6d a single program
268a0 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 uses more than
268b0 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 one database.**
268c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74 65 72 connection inter
268d0 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51 4c 20 nally, then SQL
268e0 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 20 62 functions must b
268f0 65 20 61 64 64 65 64 20 69 6e 64 69 76 69 64 75 e added individu
26900 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63 68 20 ally to.** each
26910 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
26920 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ion..**.** The s
26930 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
26940 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
26950 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 he SQL function
26960 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 6f 72 to be created or
26970 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20 .** redefined.
26980 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 The length of th
26990 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 74 65 e name is limite
269a0 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 2c 20 d to 255 bytes,
269b0 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a 2a 20 exclusive of.**
269c0 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 the zero-termina
269d0 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 tor. Note that
269e0 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 68 20 the name length
269f0 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 74 65 limit is in byte
26a00 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 61 63 s, not.** charac
26a10 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 65 6d ters. Any attem
26a20 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 20 66 pt to create a f
26a30 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 6c unction with a l
26a40 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 77 69 onger name.** wi
26a50 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b 53 51 ll result in [SQ
26a60 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65 69 6e LITE_ERROR] bein
26a70 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a g returned..**.*
26a80 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 * The third para
26a90 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a 2a 20 meter (nArg).**
26aa0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
26ab0 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 arguments that
26ac0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
26ad0 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 or.** aggregate
26ae0 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69 73 20 takes. If this
26af0 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67 parameter is neg
26b00 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 65 20 ative, then the
26b10 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a SQL function or.
26b20 2a 2a 20 61 67 67 72 65 67 61 74 65 20 6d 61 79 ** aggregate may
26b30 20 74 61 6b 65 20 61 6e 79 20 6e 75 6d 62 65 72 take any number
26b40 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a of arguments..*
26b50 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 *.** The fourth
26b60 70 61 72 61 6d 65 74 65 72 2c 20 65 54 65 78 74 parameter, eText
26b70 52 65 70 2c 20 73 70 65 63 69 66 69 65 73 20 77 Rep, specifies w
26b80 68 61 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 hat.** [SQLITE_U
26b90 54 46 38 20 7c 20 74 65 78 74 20 65 6e 63 6f 64 TF8 | text encod
26ba0 69 6e 67 5d 20 74 68 69 73 20 53 51 4c 20 66 75 ing] this SQL fu
26bb0 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 73 20 66 nction prefers f
26bc0 6f 72 0a 2a 2a 20 69 74 73 20 70 61 72 61 6d 65 or.** its parame
26bd0 74 65 72 73 2e 20 20 41 6e 79 20 53 51 4c 20 66 ters. Any SQL f
26be0 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e unction implemen
26bf0 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 tation should be
26c00 20 61 62 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a able to work.**
26c10 20 77 6f 72 6b 20 77 69 74 68 20 55 54 46 2d 38 work with UTF-8
26c20 2c 20 55 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55 , UTF-16le, or U
26c30 54 46 2d 31 36 62 65 2e 20 20 42 75 74 20 73 6f TF-16be. But so
26c40 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f me implementatio
26c50 6e 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 ns may be.** mor
26c60 65 20 65 66 66 69 63 69 65 6e 74 20 77 69 74 68 e efficient with
26c70 20 6f 6e 65 20 65 6e 63 6f 64 69 6e 67 20 74 68 one encoding th
26c80 61 6e 20 61 6e 6f 74 68 65 72 2e 20 20 49 74 20 an another. It
26c90 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 2a 2a is allowed to.**
26ca0 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
26cb0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
26cc0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 ) or sqlite3_cre
26cd0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 ate_function16()
26ce0 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d multiple.** tim
26cf0 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 es with the same
26d00 20 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 function but wi
26d10 74 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c th different val
26d20 75 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e ues of eTextRep.
26d30 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c .** When multipl
26d40 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
26d50 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 s of the same fu
26d60 6e 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c nction are avail
26d70 61 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 able, SQLite.**
26d80 77 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e will pick the on
26d90 65 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 e that involves
26da0 74 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 the least amount
26db0 20 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73 of data convers
26dc0 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 ion..** If there
26dd0 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c is only a singl
26de0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
26df0 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 which does not
26e00 63 61 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a care what text.*
26e10 2a 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 * encoding is us
26e20 65 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 ed, then the fou
26e30 72 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f rth argument sho
26e40 75 6c 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 uld be [SQLITE_A
26e50 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 NY]..**.** The f
26e60 69 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 ifth parameter i
26e70 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 s an arbitrary p
26e80 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70 ointer. The imp
26e90 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
26ea0 68 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 he.** function c
26eb0 61 6e 20 67 61 69 6e 20 61 63 63 65 73 73 20 74 an gain access t
26ec0 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75 o this pointer u
26ed0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 sing [sqlite3_us
26ee0 65 72 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a er_data()]..**.*
26ef0 2a 20 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65 * The seventh, e
26f00 69 67 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 ighth and ninth
26f10 70 61 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e parameters, xFun
26f20 63 2c 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 c, xStep and xFi
26f30 6e 61 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e nal, are.** poin
26f40 74 65 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 ters to C-langua
26f50 67 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 ge functions tha
26f60 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 t implement the
26f70 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a SQL function or.
26f80 2a 2a 20 61 67 67 72 65 67 61 74 65 2e 20 41 20 ** aggregate. A
26f90 73 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 scalar SQL funct
26fa0 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 ion requires an
26fb0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
26fc0 66 20 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 f the xFunc.** c
26fd0 61 6c 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 allback only, NU
26fe0 4c 4c 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 LL pointers shou
26ff0 6c 64 20 62 65 20 70 61 73 73 65 64 20 61 73 20 ld be passed as
27000 74 68 65 20 78 53 74 65 70 20 61 6e 64 20 78 46 the xStep and xF
27010 69 6e 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 inal.** paramete
27020 72 73 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65 rs. An aggregate
27030 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 SQL function re
27040 71 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d quires an implem
27050 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 entation of xSte
27060 70 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 p.** and xFinal
27070 61 6e 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 and NULL should
27080 62 65 20 70 61 73 73 65 64 20 66 6f 72 20 78 46 be passed for xF
27090 75 6e 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 unc. To delete a
270a0 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 n existing.** SQ
270b0 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 L function or ag
270c0 67 72 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55 gregate, pass NU
270d0 4c 4c 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 LL for all three
270e0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 function callba
270f0 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 cks..**.** It is
27100 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 permitted to re
27110 67 69 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 gister multiple
27120 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
27130 6f 66 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 of the same.** f
27140 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 unctions with th
27150 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 e same name but
27160 77 69 74 68 20 65 69 74 68 65 72 20 64 69 66 66 with either diff
27170 65 72 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 ering numbers of
27180 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 .** arguments or
27190 20 64 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 differing prefe
271a0 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 rred text encodi
271b0 6e 67 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c ngs. SQLite wil
271c0 6c 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 l use.** the imp
271d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 6f 73 74 lementation most
271e0 20 63 6c 6f 73 65 6c 79 20 6d 61 74 63 68 65 73 closely matches
271f0 20 74 68 65 20 77 61 79 20 69 6e 20 77 68 69 63 the way in whic
27200 68 20 74 68 65 0a 2a 2a 20 53 51 4c 20 66 75 6e h the.** SQL fun
27210 63 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20 20 ction is used.
27220 41 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 A function imple
27230 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 20 61 mentation with a
27240 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 0a 2a 2a non-negative.**
27250 20 6e 41 72 67 20 70 61 72 61 6d 65 74 65 72 20 nArg parameter
27260 69 73 20 61 20 62 65 74 74 65 72 20 6d 61 74 63 is a better matc
27270 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f h than a functio
27280 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
27290 20 77 69 74 68 0a 2a 2a 20 61 20 6e 65 67 61 74 with.** a negat
272a0 69 76 65 20 6e 41 72 67 2e 20 20 41 20 66 75 6e ive nArg. A fun
272b0 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 ction where the
272c0 70 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 preferred text e
272d0 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d 61 74 63 68 ncoding.** match
272e0 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 es the database
272f0 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20 62 65 encoding is a be
27300 74 74 65 72 0a 2a 2a 20 6d 61 74 63 68 20 74 68 tter.** match th
27310 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 an a function wh
27320 65 72 65 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 ere the encoding
27330 20 69 73 20 64 69 66 66 65 72 65 6e 74 2e 20 20 is different.
27340 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 .** A function w
27350 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64 69 6e here the encodin
27360 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 g difference is
27370 62 65 74 77 65 65 6e 20 55 54 46 31 36 6c 65 20 between UTF16le
27380 61 6e 64 20 55 54 46 31 36 62 65 0a 2a 2a 20 69 and UTF16be.** i
27390 73 20 61 20 63 6c 6f 73 65 72 20 6d 61 74 63 68 s a closer match
273a0 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e than a function
273b0 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64 where the encod
273c0 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 ing difference i
273d0 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 55 54 46 s.** between UTF
273e0 38 20 61 6e 64 20 55 54 46 31 36 2e 0a 2a 2a 0a 8 and UTF16..**.
273f0 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 66 75 6e 63 ** Built-in func
27400 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 6f 76 65 tions may be ove
27410 72 6c 6f 61 64 65 64 20 62 79 20 6e 65 77 20 61 rloaded by new a
27420 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
27430 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a ed functions..**
27440 20 54 68 65 20 66 69 72 73 74 20 61 70 70 6c 69 The first appli
27450 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
27460 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 67 unction with a g
27470 69 76 65 6e 20 6e 61 6d 65 20 6f 76 65 72 72 69 iven name overri
27480 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 75 69 6c 74 des all.** built
27490 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e -in functions in
274a0 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 the same [datab
274b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
274c0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 with the same na
274d0 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 71 75 65 6e me..** Subsequen
274e0 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 t application-de
274f0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 fined functions
27500 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 of the same name
27510 20 6f 6e 6c 79 20 6f 76 65 72 72 69 64 65 20 0a only override .
27520 2a 2a 20 70 72 69 6f 72 20 61 70 70 6c 69 63 61 ** prior applica
27530 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
27540 63 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 ctions that are
27550 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 66 an exact match f
27560 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 or the.** number
27570 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 61 of parameters a
27580 6e 64 20 70 72 65 66 65 72 72 65 64 20 65 6e 63 nd preferred enc
27590 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 oding..**.** An
275a0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
275b0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ned function is
275c0 70 65 72 6d 69 74 74 65 64 20 74 6f 20 63 61 6c permitted to cal
275d0 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 74 l other.** SQLit
275e0 65 20 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48 e interfaces. H
275f0 6f 77 65 76 65 72 2c 20 73 75 63 68 20 63 61 6c owever, such cal
27600 6c 73 20 6d 75 73 74 20 6e 6f 74 0a 2a 2a 20 63 ls must not.** c
27610 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73 lose the databas
27620 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e 6f 72 e connection nor
27630 20 66 69 6e 61 6c 69 7a 65 20 6f 72 20 72 65 73 finalize or res
27640 65 74 20 74 68 65 20 70 72 65 70 61 72 65 64 0a et the prepared.
27650 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 ** statement in
27660 77 68 69 63 68 20 74 68 65 20 66 75 6e 63 74 69 which the functi
27670 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a on is running..*
27680 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
27690 73 3a 0a 2a 2a 20 5b 48 31 36 31 30 33 5d 20 5b s:.** [H16103] [
276a0 48 31 36 31 30 36 5d 20 5b 48 31 36 31 30 39 5d H16106] [H16109]
276b0 20 5b 48 31 36 31 31 32 5d 20 5b 48 31 36 31 31 [H16112] [H1611
276c0 38 5d 20 5b 48 31 36 31 32 31 5d 20 5b 48 31 36 8] [H16121] [H16
276d0 31 32 34 5d 20 5b 48 31 36 31 32 37 5d 0a 2a 2a 124] [H16127].**
276e0 20 5b 48 31 36 31 33 30 5d 20 5b 48 31 36 31 33 [H16130] [H1613
276f0 33 5d 20 5b 48 31 36 31 33 36 5d 20 5b 48 31 36 3] [H16136] [H16
27700 31 33 39 5d 20 5b 48 31 36 31 34 32 5d 0a 2a 2f 139] [H16142].*/
27710 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
27720 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
27730 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 unction(. sqlit
27740 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 e3 *db,. const
27750 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e char *zFunctionN
27760 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c ame,. int nArg,
27770 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c . int eTextRep,
27780 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 . void *pApp,.
27790 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 void (*xFunc)(s
277a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
277b0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
277c0 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 e**),. void (*x
277d0 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f Step)(sqlite3_co
277e0 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 ntext*,int,sqlit
277f0 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 e3_value**),. v
27800 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 oid (*xFinal)(sq
27810 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a lite3_context*).
27820 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
27830 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
27840 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 73 _function16(. s
27850 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f qlite3 *db,. co
27860 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74 nst void *zFunct
27870 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e ionName,. int n
27880 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 Arg,. int eText
27890 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 Rep,. void *pAp
278a0 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e p,. void (*xFun
278b0 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 c)(sqlite3_conte
278c0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
278d0 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 value**),. void
278e0 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 (*xStep)(sqlite
278f0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
27900 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c qlite3_value**),
27910 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c . void (*xFinal
27920 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
27930 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 t*).);../*.** CA
27940 50 49 33 52 45 46 3a 20 54 65 78 74 20 45 6e 63 PI3REF: Text Enc
27950 6f 64 69 6e 67 73 20 7b 48 31 30 32 36 37 7d 20 odings {H10267}
27960 3c 53 35 30 32 30 30 3e 20 3c 48 31 36 31 30 30 <S50200> <H16100
27970 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f >.**.** These co
27980 6e 73 74 61 6e 74 20 64 65 66 69 6e 65 20 69 6e nstant define in
27990 74 65 67 65 72 20 63 6f 64 65 73 20 74 68 61 74 teger codes that
279a0 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 76 represent the v
279b0 61 72 69 6f 75 73 0a 2a 2a 20 74 65 78 74 20 65 arious.** text e
279c0 6e 63 6f 64 69 6e 67 73 20 73 75 70 70 6f 72 74 ncodings support
279d0 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2f ed by SQLite..*/
279e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
279f0 55 54 46 38 20 20 20 20 20 20 20 20 20 20 20 31 UTF8 1
27a00 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
27a10 55 54 46 31 36 4c 45 20 20 20 20 20 20 20 20 32 UTF16LE 2
27a20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
27a30 55 54 46 31 36 42 45 20 20 20 20 20 20 20 20 33 UTF16BE 3
27a40 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
27a50 55 54 46 31 36 20 20 20 20 20 20 20 20 20 20 34 UTF16 4
27a60 20 20 20 20 2f 2a 20 55 73 65 20 6e 61 74 69 76 /* Use nativ
27a70 65 20 62 79 74 65 20 6f 72 64 65 72 20 2a 2f 0a e byte order */.
27a80 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
27a90 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 35 20 NY 5
27aa0 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72 /* sqlite3_cr
27ab0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 6f 6e eate_function on
27ac0 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ly */.#define SQ
27ad0 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e LITE_UTF16_ALIGN
27ae0 45 44 20 20 38 20 20 20 20 2f 2a 20 73 71 6c 69 ED 8 /* sqli
27af0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
27b00 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a tion only */../*
27b10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 .** CAPI3REF: De
27b20 70 72 65 63 61 74 65 64 20 46 75 6e 63 74 69 6f precated Functio
27b30 6e 73 0a 2a 2a 20 44 45 50 52 45 43 41 54 45 44 ns.** DEPRECATED
27b40 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e .**.** These fun
27b50 63 74 69 6f 6e 73 20 61 72 65 20 5b 64 65 70 72 ctions are [depr
27b60 65 63 61 74 65 64 5d 2e 20 20 49 6e 20 6f 72 64 ecated]. In ord
27b70 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 0a 2a er to maintain.*
27b80 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 * backwards comp
27b90 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f atibility with o
27ba0 6c 64 65 72 20 63 6f 64 65 2c 20 74 68 65 73 65 lder code, these
27bb0 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6e 74 69 functions conti
27bc0 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 65 20 73 75 nue .** to be su
27bd0 70 70 6f 72 74 65 64 2e 20 20 48 6f 77 65 76 65 pported. Howeve
27be0 72 2c 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 r, new applicati
27bf0 6f 6e 73 20 73 68 6f 75 6c 64 20 61 76 6f 69 64 ons should avoid
27c00 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f 66 20 74 .** the use of t
27c10 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 hese functions.
27c20 20 54 6f 20 68 65 6c 70 20 65 6e 63 6f 75 72 61 To help encoura
27c30 67 65 20 70 65 6f 70 6c 65 20 74 6f 20 61 76 6f ge people to avo
27c40 69 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 73 id.** using thes
27c50 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 65 20 e functions, we
27c60 61 72 65 20 6e 6f 74 20 67 6f 69 6e 67 20 74 6f are not going to
27c70 20 74 65 6c 6c 20 79 6f 75 20 77 68 61 74 20 74 tell you what t
27c80 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 hey do..*/.#ifnd
27c90 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
27ca0 45 50 52 45 43 41 54 45 44 0a 53 51 4c 49 54 45 EPRECATED.SQLITE
27cb0 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 _API SQLITE_DEPR
27cc0 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 ECATED int sqlit
27cd0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 e3_aggregate_cou
27ce0 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nt(sqlite3_conte
27cf0 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 xt*);.SQLITE_API
27d00 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 SQLITE_DEPRECAT
27d10 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 ED int sqlite3_e
27d20 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f 73 xpired(sqlite3_s
27d30 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 tmt*);.SQLITE_AP
27d40 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 I SQLITE_DEPRECA
27d50 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TED int sqlite3_
27d60 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 transfer_binding
27d70 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c s(sqlite3_stmt*,
27d80 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b sqlite3_stmt*);
27d90 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
27da0 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e TE_DEPRECATED in
27db0 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c t sqlite3_global
27dc0 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29 3b 0a _recover(void);.
27dd0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
27de0 45 5f 44 45 50 52 45 43 41 54 45 44 20 76 6f 69 E_DEPRECATED voi
27df0 64 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 d sqlite3_thread
27e00 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 29 3b 0a _cleanup(void);.
27e10 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
27e20 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 E_DEPRECATED int
27e30 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f sqlite3_memory_
27e40 61 6c 61 72 6d 28 76 6f 69 64 28 2a 29 28 76 6f alarm(void(*)(vo
27e50 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 id*,sqlite3_int6
27e60 34 2c 69 6e 74 29 2c 76 6f 69 64 2a 2c 73 71 6c 4,int),void*,sql
27e70 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 23 65 6e ite3_int64);.#en
27e80 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 dif../*.** CAPI3
27e90 52 45 46 3a 20 4f 62 74 61 69 6e 69 6e 67 20 53 REF: Obtaining S
27ea0 51 4c 20 46 75 6e 63 74 69 6f 6e 20 50 61 72 61 QL Function Para
27eb0 6d 65 74 65 72 20 56 61 6c 75 65 73 20 7b 48 31 meter Values {H1
27ec0 35 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 5100} <S20200>.*
27ed0 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61 6e 67 75 *.** The C-langu
27ee0 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 age implementati
27ef0 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e 63 74 69 on of SQL functi
27f00 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 ons and aggregat
27f10 65 73 20 75 73 65 73 0a 2a 2a 20 74 68 69 73 20 es uses.** this
27f20 73 65 74 20 6f 66 20 69 6e 74 65 72 66 61 63 65 set of interface
27f30 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 61 63 63 routines to acc
27f40 65 73 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 ess the paramete
27f50 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a 2a 20 74 r values on.** t
27f60 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 he function or a
27f70 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 ggregate..**.**
27f80 54 68 65 20 78 46 75 6e 63 20 28 66 6f 72 20 73 The xFunc (for s
27f90 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 73 29 calar functions)
27fa0 20 6f 72 20 78 53 74 65 70 20 28 66 6f 72 20 61 or xStep (for a
27fb0 67 67 72 65 67 61 74 65 73 29 20 70 61 72 61 6d ggregates) param
27fc0 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b 73 71 6c eters.** to [sql
27fd0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
27fe0 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c tion()] and [sql
27ff0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
28000 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 64 65 66 tion16()].** def
28010 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 ine callbacks th
28020 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 at implement the
28030 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 SQL functions a
28040 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a nd aggregates..*
28050 2a 20 54 68 65 20 34 74 68 20 70 61 72 61 6d 65 * The 4th parame
28060 74 65 72 20 74 6f 20 74 68 65 73 65 20 63 61 6c ter to these cal
28070 6c 62 61 63 6b 73 20 69 73 20 61 6e 20 61 72 72 lbacks is an arr
28080 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 ay of pointers t
28090 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74 65 64 20 o.** [protected
280a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
280b0 62 6a 65 63 74 73 2e 20 20 54 68 65 72 65 20 69 bjects. There i
280c0 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 33 5f 76 s one [sqlite3_v
280d0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72 alue] object for
280e0 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 6d 65 74 .** each paramet
280f0 65 72 20 74 6f 20 74 68 65 20 53 51 4c 20 66 75 er to the SQL fu
28100 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20 72 nction. These r
28110 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
28120 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63 74 20 76 to.** extract v
28130 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 5b alues from the [
28140 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
28150 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 bjects..**.** Th
28160 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 ese routines wor
28170 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b 70 72 6f k only with [pro
28180 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
28190 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a alue] objects..*
281a0 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f * Any attempt to
281b0 20 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 use these routi
281c0 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e 70 72 6f nes on an [unpro
281d0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
281e0 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 alue].** object
281f0 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 results in undef
28200 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a ined behavior..*
28210 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
28220 6e 65 73 20 77 6f 72 6b 20 6a 75 73 74 20 6c 69 nes work just li
28230 6b 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e ke the correspon
28240 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 ding [column acc
28250 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 0a 2a ess functions].*
28260 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 20 74 * except that t
28270 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 61 hese routines ta
28280 6b 65 20 61 20 73 69 6e 67 6c 65 20 5b 70 72 6f ke a single [pro
28290 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
282a0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20 alue] object.**
282b0 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 20 pointer instead
282c0 6f 66 20 61 20 5b 73 71 6c 69 74 65 33 5f 73 74 of a [sqlite3_st
282d0 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 20 61 6e 64 mt*] pointer and
282e0 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6c 75 an integer colu
282f0 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a mn number..**.**
28300 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c The sqlite3_val
28310 75 65 5f 74 65 78 74 31 36 28 29 20 69 6e 74 65 ue_text16() inte
28320 72 66 61 63 65 20 65 78 74 72 61 63 74 73 20 61 rface extracts a
28330 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a UTF-16 string.*
28340 2a 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 * in the native
28350 62 79 74 65 2d 6f 72 64 65 72 20 6f 66 20 74 68 byte-order of th
28360 65 20 68 6f 73 74 20 6d 61 63 68 69 6e 65 2e 20 e host machine.
28370 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f The.** sqlite3_
28380 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 29 value_text16be()
28390 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 61 6c and sqlite3_val
283a0 75 65 5f 74 65 78 74 31 36 6c 65 28 29 20 69 6e ue_text16le() in
283b0 74 65 72 66 61 63 65 73 0a 2a 2a 20 65 78 74 72 terfaces.** extr
283c0 61 63 74 20 55 54 46 2d 31 36 20 73 74 72 69 6e act UTF-16 strin
283d0 67 73 20 61 73 20 62 69 67 2d 65 6e 64 69 61 6e gs as big-endian
283e0 20 61 6e 64 20 6c 69 74 74 6c 65 2d 65 6e 64 69 and little-endi
283f0 61 6e 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e an respectively.
28400 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
28410 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 e3_value_numeric
28420 5f 74 79 70 65 28 29 20 69 6e 74 65 72 66 61 63 _type() interfac
28430 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 61 70 e attempts to ap
28440 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 69 63 20 61 ply.** numeric a
28450 66 66 69 6e 69 74 79 20 74 6f 20 74 68 65 20 76 ffinity to the v
28460 61 6c 75 65 2e 20 20 54 68 69 73 20 6d 65 61 6e alue. This mean
28470 73 20 74 68 61 74 20 61 6e 20 61 74 74 65 6d 70 s that an attemp
28480 74 20 69 73 0a 2a 2a 20 6d 61 64 65 20 74 6f 20 t is.** made to
28490 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c 75 convert the valu
284a0 65 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 e to an integer
284b0 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e or floating poin
284c0 74 2e 20 20 49 66 0a 2a 2a 20 73 75 63 68 20 61 t. If.** such a
284d0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 conversion is p
284e0 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 20 ossible without
284f0 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 loss of informat
28500 69 6f 6e 20 28 69 6e 20 6f 74 68 65 72 0a 2a 2a ion (in other.**
28510 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 76 words, if the v
28520 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 alue is a string
28530 20 74 68 61 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 that looks like
28540 20 61 20 6e 75 6d 62 65 72 29 0a 2a 2a 20 74 68 a number).** th
28550 65 6e 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f en the conversio
28560 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 n is performed.
28570 20 4f 74 68 65 72 77 69 73 65 20 6e 6f 20 63 6f Otherwise no co
28580 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 2e nversion occurs.
28590 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f .** The [SQLITE_
285a0 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61 74 79 INTEGER | dataty
285b0 70 65 5d 20 61 66 74 65 72 20 63 6f 6e 76 65 72 pe] after conver
285c0 73 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 sion is returned
285d0 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73 65 20 70 ..**.** Please p
285e0 61 79 20 70 61 72 74 69 63 75 6c 61 72 20 61 74 ay particular at
285f0 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68 65 20 66 tention to the f
28600 61 63 74 20 74 68 61 74 20 74 68 65 20 70 6f 69 act that the poi
28610 6e 74 65 72 20 72 65 74 75 72 6e 65 64 0a 2a 2a nter returned.**
28620 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 76 from [sqlite3_v
28630 61 6c 75 65 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 alue_blob()], [s
28640 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
28650 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c t()], or.** [sql
28660 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
28670 36 28 29 5d 20 63 61 6e 20 62 65 20 69 6e 76 61 6()] can be inva
28680 6c 69 64 61 74 65 64 20 62 79 20 61 20 73 75 62 lidated by a sub
28690 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a sequent call to.
286a0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 ** [sqlite3_valu
286b0 65 5f 62 79 74 65 73 28 29 5d 2c 20 5b 73 71 6c e_bytes()], [sql
286c0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
286d0 31 36 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 16()], [sqlite3_
286e0 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a value_text()],.*
286f0 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 * or [sqlite3_va
28700 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 2e 0a 2a lue_text16()]..*
28710 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
28720 6e 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c nes must be call
28730 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 ed from the same
28740 20 74 68 72 65 61 64 20 61 73 0a 2a 2a 20 74 68 thread as.** th
28750 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 e SQL function t
28760 68 61 74 20 73 75 70 70 6c 69 65 64 20 74 68 65 hat supplied the
28770 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a [sqlite3_value*
28780 5d 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a ] parameters..**
28790 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
287a0 3a 0a 2a 2a 20 5b 48 31 35 31 30 33 5d 20 5b 48 :.** [H15103] [H
287b0 31 35 31 30 36 5d 20 5b 48 31 35 31 30 39 5d 20 15106] [H15109]
287c0 5b 48 31 35 31 31 32 5d 20 5b 48 31 35 31 31 35 [H15112] [H15115
287d0 5d 20 5b 48 31 35 31 31 38 5d 20 5b 48 31 35 31 ] [H15118] [H151
287e0 32 31 5d 20 5b 48 31 35 31 32 34 5d 0a 2a 2a 20 21] [H15124].**
287f0 5b 48 31 35 31 32 37 5d 20 5b 48 31 35 31 33 30 [H15127] [H15130
28800 5d 20 5b 48 31 35 31 33 33 5d 20 5b 48 31 35 31 ] [H15133] [H151
28810 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 36].*/.SQLITE_AP
28820 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
28830 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
28840 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
28850 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
28860 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
28870 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c ytes(sqlite3_val
28880 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
28890 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
288a0 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 ue_bytes16(sqlit
288b0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.SQLI
288c0 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 TE_API double sq
288d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 lite3_value_doub
288e0 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
288f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
28900 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
28910 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c _int(sqlite3_val
28920 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
28930 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
28940 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
28950 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 64(sqlite3_value
28960 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
28970 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
28980 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 ar *sqlite3_valu
28990 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 e_text(sqlite3_v
289a0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
289b0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
289c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
289d0 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 t16(sqlite3_valu
289e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
289f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
28a00 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
28a10 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
28a20 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
28a30 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
28a40 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 e3_value_text16b
28a50 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a e(sqlite3_value*
28a60 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
28a70 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
28a80 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c type(sqlite3_val
28a90 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
28aa0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
28ab0 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 ue_numeric_type(
28ac0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
28ad0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
28ae0 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67 61 : Obtain Aggrega
28af0 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 te Function Cont
28b00 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53 32 ext {H16210} <S2
28b10 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0200>.**.** The
28b20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
28b30 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 f aggregate SQL
28b40 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68 functions use th
28b50 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61 6c is routine to al
28b60 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72 75 locate.** a stru
28b70 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69 6e cture for storin
28b80 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a 2a g their state..*
28b90 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 *.** The first t
28ba0 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f ime the sqlite3_
28bb0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
28bc0 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63 t() routine is c
28bd0 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20 70 alled for a.** p
28be0 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 67 articular aggreg
28bf0 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c 6f ate, SQLite allo
28c00 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66 20 cates nBytes of
28c10 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20 6f memory, zeroes o
28c20 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f 72 ut that.** memor
28c30 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 y, and returns a
28c40 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
28c50 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 On second and su
28c60 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
28c70 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67 67 o.** sqlite3_agg
28c80 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 regate_context()
28c90 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61 67 for the same ag
28ca0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
28cb0 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20 73 index,.** the s
28cc0 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 65 ame buffer is re
28cd0 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70 6c turned. The impl
28ce0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
28cf0 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e 20 e aggregate can
28d00 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 use.** the retur
28d10 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61 63 ned buffer to ac
28d20 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a 2a cumulate data..*
28d30 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74 6f *.** SQLite auto
28d40 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73 20 matically frees
28d50 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 the allocated bu
28d60 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61 67 ffer when the ag
28d70 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72 79 gregate.** query
28d80 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a concludes..**.*
28d90 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 * The first para
28da0 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 meter should be
28db0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a a copy of the.**
28dc0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 [sqlite3_contex
28dd0 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e t | SQL function
28de0 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 69 context] that i
28df0 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61 s the first para
28e00 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 meter.** to the
28e10 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 callback routine
28e20 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
28e30 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 66 the aggregate f
28e40 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 unction..**.** T
28e50 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 his routine must
28e60 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 be called from
28e70 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
28e80 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 in which.** the
28e90 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 aggregate SQL fu
28ea0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
28eb0 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 g..**.** Require
28ec0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 31 ments:.** [H1621
28ed0 31 5d 20 5b 48 31 36 32 31 33 5d 20 5b 48 31 36 1] [H16213] [H16
28ee0 32 31 35 5d 20 5b 48 31 36 32 31 37 5d 0a 2a 2f 215] [H16217].*/
28ef0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
28f00 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 *sqlite3_aggreg
28f10 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 ate_context(sqli
28f20 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e te3_context*, in
28f30 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a 2a t nBytes);../*.*
28f40 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65 72 * CAPI3REF: User
28f50 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74 69 Data For Functi
28f60 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c 53 32 ons {H16240} <S2
28f70 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0200>.**.** The
28f80 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
28f90 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 a() interface re
28fa0 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a turns a copy of.
28fb0 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 ** the pointer t
28fc0 68 61 74 20 77 61 73 20 74 68 65 20 70 55 73 65 hat was the pUse
28fd0 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 72 20 rData parameter
28fe0 28 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 (the 5th paramet
28ff0 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 er).** of the [s
29000 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
29010 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 nction()].** and
29020 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
29030 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 _function16()] r
29040 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f 72 69 outines that ori
29050 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 ginally.** regis
29060 74 65 72 65 64 20 74 68 65 20 61 70 70 6c 69 63 tered the applic
29070 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 ation defined fu
29080 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a 2a nction. {END}.**
29090 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
290a0 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 must be called
290b0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 from the same th
290c0 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a read in which.**
290d0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
290e0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
290f0 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a n is running..**
29100 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
29110 3a 0a 2a 2a 20 5b 48 31 36 32 34 33 5d 0a 2a 2f :.** [H16243].*/
29120 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
29130 20 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 *sqlite3_user_d
29140 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ata(sqlite3_cont
29150 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ext*);../*.** CA
29160 50 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 65 PI3REF: Database
29170 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 46 6f 72 20 Connection For
29180 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 32 35 Functions {H1625
29190 30 7d 20 3c 53 36 30 36 30 30 3e 3c 53 32 30 32 0} <S60600><S202
291a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
291b0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
291c0 5f 68 61 6e 64 6c 65 28 29 20 69 6e 74 65 72 66 _handle() interf
291d0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f ace returns a co
291e0 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 70 6f 69 py of.** the poi
291f0 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 64 61 74 nter to the [dat
29200 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
29210 5d 20 28 74 68 65 20 31 73 74 20 70 61 72 61 6d ] (the 1st param
29220 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 eter).** of the
29230 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
29240 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 function()].** a
29250 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 nd [sqlite3_crea
29260 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d te_function16()]
29270 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f routines that o
29280 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 riginally.** reg
29290 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c istered the appl
292a0 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 ication defined
292b0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
292c0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
292d0 20 5b 48 31 36 32 35 33 5d 0a 2a 2f 0a 53 51 4c [H16253].*/.SQL
292e0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 ITE_API sqlite3
292f0 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 *sqlite3_context
29300 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 _db_handle(sqlit
29310 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f e3_context*);../
29320 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 *.** CAPI3REF: F
29330 75 6e 63 74 69 6f 6e 20 41 75 78 69 6c 69 61 72 unction Auxiliar
29340 79 20 44 61 74 61 20 7b 48 31 36 32 37 30 7d 20 y Data {H16270}
29350 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20200>.**.** T
29360 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f he following two
29370 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 functions may b
29380 65 20 75 73 65 64 20 62 79 20 73 63 61 6c 61 72 e used by scalar
29390 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 74 SQL functions t
293a0 6f 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 20 6d o.** associate m
293b0 65 74 61 64 61 74 61 20 77 69 74 68 20 61 72 67 etadata with arg
293c0 75 6d 65 6e 74 20 76 61 6c 75 65 73 2e 20 49 66 ument values. If
293d0 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 the same value
293e0 69 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 is passed to.**
293f0 6d 75 6c 74 69 70 6c 65 20 69 6e 76 6f 63 61 74 multiple invocat
29400 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 ions of the same
29410 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 64 75 SQL function du
29420 72 69 6e 67 20 71 75 65 72 79 20 65 78 65 63 75 ring query execu
29430 74 69 6f 6e 2c 20 75 6e 64 65 72 0a 2a 2a 20 73 tion, under.** s
29440 6f 6d 65 20 63 69 72 63 75 6d 73 74 61 6e 63 65 ome circumstance
29450 73 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 s the associated
29460 20 6d 65 74 61 64 61 74 61 20 6d 61 79 20 62 65 metadata may be
29470 20 70 72 65 73 65 72 76 65 64 2e 20 54 68 69 73 preserved. This
29480 20 6d 61 79 0a 2a 2a 20 62 65 20 75 73 65 64 2c may.** be used,
29490 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f for example, to
294a0 20 61 64 64 20 61 20 72 65 67 75 6c 61 72 2d 65 add a regular-e
294b0 78 70 72 65 73 73 69 6f 6e 20 6d 61 74 63 68 69 xpression matchi
294c0 6e 67 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75 6e ng scalar.** fun
294d0 63 74 69 6f 6e 2e 20 54 68 65 20 63 6f 6d 70 69 ction. The compi
294e0 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 led version of t
294f0 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 he regular expre
29500 73 73 69 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 ssion is stored
29510 61 73 0a 2a 2a 20 6d 65 74 61 64 61 74 61 20 61 as.** metadata a
29520 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
29530 68 65 20 53 51 4c 20 76 61 6c 75 65 20 70 61 73 he SQL value pas
29540 73 65 64 20 61 73 20 74 68 65 20 72 65 67 75 6c sed as the regul
29550 61 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a ar expression.**
29560 20 70 61 74 74 65 72 6e 2e 20 20 54 68 65 20 63 pattern. The c
29570 6f 6d 70 69 6c 65 64 20 72 65 67 75 6c 61 72 20 ompiled regular
29580 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 20 62 expression can b
29590 65 20 72 65 75 73 65 64 20 6f 6e 20 6d 75 6c 74 e reused on mult
295a0 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 iple.** invocati
295b0 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ons of the same
295c0 66 75 6e 63 74 69 6f 6e 20 73 6f 20 74 68 61 74 function so that
295d0 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 the original pa
295e0 74 74 65 72 6e 20 73 74 72 69 6e 67 0a 2a 2a 20 ttern string.**
295f0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
29600 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 6f be recompiled o
29610 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f n each invocatio
29620 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c n..**.** The sql
29630 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 ite3_get_auxdata
29640 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
29650 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
29660 6f 20 74 68 65 20 6d 65 74 61 64 61 74 61 0a 2a o the metadata.*
29670 2a 20 61 73 73 6f 63 69 61 74 65 64 20 62 79 20 * associated by
29680 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f the sqlite3_set_
29690 61 75 78 64 61 74 61 28 29 20 66 75 6e 63 74 69 auxdata() functi
296a0 6f 6e 20 77 69 74 68 20 74 68 65 20 4e 74 68 20 on with the Nth
296b0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 76 61 6c 75 argument.** valu
296c0 65 20 74 6f 20 74 68 65 20 61 70 70 6c 69 63 61 e to the applica
296d0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
296e0 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 6d 65 74 ction. If no met
296f0 61 64 61 74 61 20 68 61 73 20 62 65 65 6e 20 65 adata has been e
29700 76 65 72 0a 2a 2a 20 62 65 65 6e 20 73 65 74 20 ver.** been set
29710 66 6f 72 20 74 68 65 20 4e 74 68 20 61 72 67 75 for the Nth argu
29720 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 75 6e 63 ment of the func
29730 74 69 6f 6e 2c 20 6f 72 20 69 66 20 74 68 65 20 tion, or if the
29740 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a corresponding.**
29750 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 function parame
29760 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 ter has changed
29770 73 69 6e 63 65 20 74 68 65 20 6d 65 74 61 2d 64 since the meta-d
29780 61 74 61 20 77 61 73 20 73 65 74 2c 0a 2a 2a 20 ata was set,.**
29790 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 then sqlite3_get
297a0 5f 61 75 78 64 61 74 61 28 29 20 72 65 74 75 72 _auxdata() retur
297b0 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ns a NULL pointe
297c0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c r..**.** The sql
297d0 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
297e0 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 61 76 () interface sav
297f0 65 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 0a es the metadata.
29800 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ** pointed to by
29810 20 69 74 73 20 33 72 64 20 70 61 72 61 6d 65 74 its 3rd paramet
29820 65 72 20 61 73 20 74 68 65 20 6d 65 74 61 64 61 er as the metada
29830 74 61 20 66 6f 72 20 74 68 65 20 4e 2d 74 68 0a ta for the N-th.
29840 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 ** argument of t
29850 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
29860 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e efined function.
29870 20 20 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 Subsequent.**
29880 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
29890 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 6d _get_auxdata() m
298a0 69 67 68 74 20 72 65 74 75 72 6e 20 74 68 69 73 ight return this
298b0 20 64 61 74 61 2c 20 69 66 20 69 74 20 68 61 73 data, if it has
298c0 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20 64 65 73 .** not been des
298d0 74 72 6f 79 65 64 2e 0a 2a 2a 20 49 66 20 69 74 troyed..** If it
298e0 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51 is not NULL, SQ
298f0 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 Lite will invoke
29900 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a the destructor.
29910 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 ** function give
29920 6e 20 62 79 20 74 68 65 20 34 74 68 20 70 61 72 n by the 4th par
29930 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
29940 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 3_set_auxdata()
29950 6f 6e 0a 2a 2a 20 74 68 65 20 6d 65 74 61 64 61 on.** the metada
29960 74 61 20 77 68 65 6e 20 74 68 65 20 63 6f 72 72 ta when the corr
29970 65 73 70 6f 6e 64 69 6e 67 20 66 75 6e 63 74 69 esponding functi
29980 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 63 68 61 on parameter cha
29990 6e 67 65 73 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 nges.** or when
299a0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
299b0 74 20 63 6f 6d 70 6c 65 74 65 73 2c 20 77 68 69 t completes, whi
299c0 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 chever comes fir
299d0 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 st..**.** SQLite
299e0 20 69 73 20 66 72 65 65 20 74 6f 20 63 61 6c 6c is free to call
299f0 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
29a00 61 6e 64 20 64 72 6f 70 20 6d 65 74 61 64 61 74 and drop metadat
29a10 61 20 6f 6e 20 61 6e 79 0a 2a 2a 20 70 61 72 61 a on any.** para
29a20 6d 65 74 65 72 20 6f 66 20 61 6e 79 20 66 75 6e meter of any fun
29a30 63 74 69 6f 6e 20 61 74 20 61 6e 79 20 74 69 6d ction at any tim
29a40 65 2e 20 20 54 68 65 20 6f 6e 6c 79 20 67 75 61 e. The only gua
29a50 72 61 6e 74 65 65 20 69 73 20 74 68 61 74 0a 2a rantee is that.*
29a60 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 * the destructor
29a70 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 will be called
29a80 62 65 66 6f 72 65 20 74 68 65 20 6d 65 74 61 64 before the metad
29a90 61 74 61 20 69 73 20 64 72 6f 70 70 65 64 2e 0a ata is dropped..
29aa0 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63 74 69 63 **.** In practic
29ab0 65 2c 20 6d 65 74 61 64 61 74 61 20 69 73 20 70 e, metadata is p
29ac0 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65 6e reserved between
29ad0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 function calls
29ae0 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f for.** expressio
29af0 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 73 ns that are cons
29b00 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 tant at compile
29b10 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63 6c 75 time. This inclu
29b20 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a 2a 20 76 des literal.** v
29b30 61 6c 75 65 73 20 61 6e 64 20 53 51 4c 20 76 61 alues and SQL va
29b40 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 riables..**.** T
29b50 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 hese routines mu
29b60 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f st be called fro
29b70 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 m the same threa
29b80 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 d in which.** th
29b90 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 e SQL function i
29ba0 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a s running..**.**
29bb0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
29bc0 2a 20 5b 48 31 36 32 37 32 5d 20 5b 48 31 36 32 * [H16272] [H162
29bd0 37 34 5d 20 5b 48 31 36 32 37 36 5d 20 5b 48 31 74] [H16276] [H1
29be0 36 32 37 37 5d 20 5b 48 31 36 32 37 38 5d 20 5b 6277] [H16278] [
29bf0 48 31 36 32 37 39 5d 0a 2a 2f 0a 53 51 4c 49 54 H16279].*/.SQLIT
29c00 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
29c10 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
29c20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
29c30 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 , int N);.SQLITE
29c40 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
29c50 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 71 3_set_auxdata(sq
29c60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
29c70 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f int N, void*, vo
29c80 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a id (*)(void*));.
29c90 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
29ca0 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69 : Constants Defi
29cb0 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73 ning Special Des
29cc0 74 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72 tructor Behavior
29cd0 20 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 30 {H10280} <S3010
29ce0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 0>.**.** These a
29cf0 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 re special value
29d00 73 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 75 s for the destru
29d10 63 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 73 ctor that is pas
29d20 73 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a sed in as the.**
29d30 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 final argument
29d40 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 to routines like
29d50 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
29d60 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 68 _blob()]. If th
29d70 65 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 e destructor.**
29d80 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 argument is SQLI
29d90 54 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d 65 TE_STATIC, it me
29da0 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e ans that the con
29db0 74 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20 tent pointer is
29dc0 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 constant.** and
29dd0 77 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e 67 will never chang
29de0 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 e. It does not
29df0 6e 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 72 need to be destr
29e00 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 51 oyed. The.** SQ
29e10 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76 LITE_TRANSIENT v
29e20 61 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 alue means that
29e30 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c the content will
29e40 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69 likely change i
29e50 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75 n.** the near fu
29e60 74 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 51 ture and that SQ
29e70 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 Lite should make
29e80 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 its own private
29e90 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 copy of.** the
29ea0 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72 content before r
29eb0 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 eturning..**.**
29ec0 54 68 65 20 74 79 70 65 64 65 66 20 69 73 20 6e The typedef is n
29ed0 65 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b ecessary to work
29ee0 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73 around problems
29ef0 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43 in certain.** C
29f00 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53 ++ compilers. S
29f10 65 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e ee ticket #2191.
29f20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 .*/.typedef void
29f30 20 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72 (*sqlite3_destr
29f40 75 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64 uctor_type)(void
29f50 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 *);.#define SQLI
29f60 54 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28 TE_STATIC (
29f70 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 (sqlite3_destruc
29f80 74 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66 tor_type)0).#def
29f90 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 ine SQLITE_TRANS
29fa0 49 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33 IENT ((sqlite3
29fb0 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 _destructor_type
29fc0 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 )-1)../*.** CAPI
29fd0 33 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68 3REF: Setting Th
29fe0 65 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53 e Result Of An S
29ff0 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 36 QL Function {H16
2a000 34 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 400} <S20200>.**
2a010 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
2a020 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74 es are used by t
2a030 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 6e he xFunc or xFin
2a040 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 al callbacks tha
2a050 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 53 t.** implement S
2a060 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 QL functions and
2a070 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 65 aggregates. Se
2a080 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 e.** [sqlite3_cr
2a090 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d eate_function()]
2a0a0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 and [sqlite3_cr
2a0b0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 eate_function16(
2a0c0 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 )].** for additi
2a0d0 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
2a0e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 ..**.** These fu
2a0f0 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 72 nctions work ver
2a100 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20 y much like the
2a110 5b 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 [parameter bindi
2a120 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a ng] family of.**
2a130 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 functions used
2a140 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 74 to bind values t
2a150 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 o host parameter
2a160 73 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 74 s in prepared st
2a170 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 66 atements..** Ref
2a180 65 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 70 er to the [SQL p
2a190 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d 65 arameter] docume
2a1a0 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 ntation for addi
2a1b0 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
2a1c0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 on..**.** The sq
2a1d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f lite3_result_blo
2a1e0 62 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 b() interface se
2a1f0 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 ts the result fr
2a200 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 om.** an applica
2a210 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
2a220 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 ction to be the
2a230 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65 BLOB whose conte
2a240 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a 2a nt is pointed.**
2a250 20 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e to by the secon
2a260 64 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 d parameter and
2a270 77 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73 which is N bytes
2a280 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73 long where N is
2a290 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 61 the.** third pa
2a2a0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 rameter..**.** T
2a2b0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2a2c0 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 74 t_zeroblob() int
2a2d0 65 72 66 61 63 65 73 20 73 65 74 20 74 68 65 20 erfaces set the
2a2e0 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 result of.** the
2a2f0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
2a300 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f ined function to
2a310 20 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61 be a BLOB conta
2a320 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a ining all zero.*
2a330 2a 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79 * bytes and N by
2a340 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65 tes in size, whe
2a350 72 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75 re N is the valu
2a360 65 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72 e of the 2nd par
2a370 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ameter..**.** Th
2a380 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2a390 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66 _double() interf
2a3a0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 ace sets the res
2a3b0 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 ult from.** an a
2a3c0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
2a3d0 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ed function to b
2a3e0 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 e a floating poi
2a3f0 6e 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 nt value specifi
2a400 65 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64 ed.** by its 2nd
2a410 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
2a420 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
2a430 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 ult_error() and
2a440 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2a450 72 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f rror16() functio
2a460 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20 ns.** cause the
2a470 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 implemented SQL
2a480 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f function to thro
2a490 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a w an exception..
2a4a0 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 ** SQLite uses t
2a4b0 68 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 he string pointe
2a4c0 64 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32 d to by the.** 2
2a4d0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 nd parameter of
2a4e0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2a4f0 72 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65 rror() or sqlite
2a500 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
2a510 28 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78 ().** as the tex
2a520 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 t of an error me
2a530 73 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69 ssage. SQLite i
2a540 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 65 72 nterprets the er
2a550 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73 ror.** message s
2a560 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 tring from sqlit
2a570 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
2a580 29 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c 69 ) as UTF-8. SQLi
2a590 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 73 te.** interprets
2a5a0 20 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f 6d the string from
2a5b0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2a5c0 65 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 46 error16() as UTF
2a5d0 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a 2a -16 in native.**
2a5e0 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 66 byte order. If
2a5f0 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d the third param
2a600 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f eter to sqlite3_
2a610 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a result_error().*
2a620 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 * or sqlite3_res
2a630 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73 ult_error16() is
2a640 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 negative then S
2a650 51 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 74 QLite takes as t
2a660 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 he error.** mess
2a670 61 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 20 age all text up
2a680 74 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 through the firs
2a690 74 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 t zero character
2a6a0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 72 ..** If the thir
2a6b0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 d parameter to s
2a6c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2a6d0 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 ror() or.** sqli
2a6e0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2a6f0 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 16() is non-nega
2a700 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 tive then SQLite
2a710 20 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e 79 takes that many
2a720 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63 .** bytes (not c
2a730 68 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20 haracters) from
2a740 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
2a750 72 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d r as the error m
2a760 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 73 essage..** The s
2a770 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2a780 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ror() and sqlite
2a790 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
2a7a0 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6d ().** routines m
2a7b0 61 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f ake a private co
2a7c0 70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 py of the error
2a7d0 6d 65 73 73 61 67 65 20 74 65 78 74 20 62 65 66 message text bef
2a7e0 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 75 ore.** they retu
2a7f0 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 rn. Hence, the
2a800 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
2a810 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20 can deallocate
2a820 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 65 or.** modify the
2a830 20 74 65 78 74 20 61 66 74 65 72 20 74 68 65 79 text after they
2a840 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 return without
2a850 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c harm..** The sql
2a860 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2a870 72 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f r_code() functio
2a880 6e 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 72 n changes the er
2a890 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75 ror code.** retu
2a8a0 72 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 rned by SQLite a
2a8b0 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e s a result of an
2a8c0 20 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e 63 error in a func
2a8d0 74 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 6c tion. By defaul
2a8e0 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 t,.** the error
2a8f0 63 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45 code is SQLITE_E
2a900 52 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 75 RROR. A subsequ
2a910 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ent call to sqli
2a920 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2a930 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 ().** or sqlite3
2a940 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
2a950 29 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72 ) resets the err
2a960 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 or code to SQLIT
2a970 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 E_ERROR..**.** T
2a980 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2a990 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 72 t_toobig() inter
2a9a0 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c 69 face causes SQLi
2a9b0 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 te to throw an e
2a9c0 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69 rror.** indicati
2a9d0 6e 67 20 74 68 61 74 20 61 20 73 74 72 69 6e 67 ng that a string
2a9e0 20 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 6c or BLOB is to l
2a9f0 6f 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74 ong to represent
2aa00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2aa10 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d te3_result_nomem
2aa20 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 () interface cau
2aa30 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 ses SQLite to th
2aa40 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 row an error.**
2aa50 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 indicating that
2aa60 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
2aa70 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a ion failed..**.*
2aa80 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2aa90 73 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72 sult_int() inter
2aaa0 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 face sets the re
2aab0 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 turn value.** of
2aac0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2aad0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
2aae0 6e 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62 n to be the 32-b
2aaf0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 it signed intege
2ab00 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e r.** value given
2ab10 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 in the 2nd argu
2ab20 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 6c ment..** The sql
2ab30 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 ite3_result_int6
2ab40 34 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 4() interface se
2ab50 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ts the return va
2ab60 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 lue.** of the ap
2ab70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2ab80 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
2ab90 20 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e the 64-bit sign
2aba0 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 ed integer.** va
2abb0 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 lue given in the
2abc0 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2nd argument..*
2abd0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2abe0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 _result_null() i
2abf0 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 nterface sets th
2ac00 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
2ac10 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 * of the applica
2ac20 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
2ac30 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c ction to be NULL
2ac40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2ac50 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
2ac60 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c ), sqlite3_resul
2ac70 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 73 t_text16(),.** s
2ac80 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2ac90 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 71 xt16le(), and sq
2aca0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2acb0 74 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 63 t16be() interfac
2acc0 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 65 es.** set the re
2acd0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 turn value of th
2ace0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
2acf0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
2ad00 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 73 o be.** a text s
2ad10 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 72 tring which is r
2ad20 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 54 epresented as UT
2ad30 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 69 F-8, UTF-16 nati
2ad40 76 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a 2a ve byte order,.*
2ad50 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 * UTF-16 little
2ad60 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 endian, or UTF-1
2ad70 36 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 65 6 big endian, re
2ad80 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 53 spectively..** S
2ad90 51 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 20 QLite takes the
2ada0 74 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f 6d text result from
2adb0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2adc0 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e 64 from.** the 2nd
2add0 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 parameter of th
2ade0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2adf0 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 _text* interface
2ae00 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 s..** If the 3rd
2ae10 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
2ae20 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2ae30 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 _text* interface
2ae40 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 65 s.** is negative
2ae50 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 , then SQLite ta
2ae60 6b 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 20 kes result text
2ae70 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 from the 2nd par
2ae80 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 67 ameter.** throug
2ae90 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f h the first zero
2aea0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 character..** I
2aeb0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 f the 3rd parame
2aec0 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 ter to the sqlit
2aed0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 e3_result_text*
2aee0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73 interfaces.** is
2aef0 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 non-negative, t
2af00 68 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 65 hen as many byte
2af10 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 72 s (not character
2af20 73 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a 2a s) of the text.*
2af30 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 * pointed to by
2af40 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
2af50 72 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 74 r are taken as t
2af60 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
2af70 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 69 efined.** functi
2af80 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 on result..** If
2af90 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 the 4th paramet
2afa0 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 er to the sqlite
2afb0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 3_result_text* i
2afc0 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 nterfaces.** or
2afd0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 sqlite3_result_b
2afe0 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c lob is a non-NUL
2aff0 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 L pointer, then
2b000 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 61 SQLite calls tha
2b010 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73 t.** function as
2b020 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
2b030 6f 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 on the text or B
2b040 4c 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e 20 LOB result when
2b050 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 it has.** finish
2b060 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65 ed using that re
2b070 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 sult..** If the
2b080 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 4th parameter to
2b090 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 the sqlite3_res
2b0a0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 ult_text* interf
2b0b0 61 63 65 73 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 aces or.** sqlit
2b0c0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 e3_result_blob i
2b0d0 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f s the special co
2b0e0 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 nstant SQLITE_ST
2b0f0 41 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 ATIC, then SQLit
2b100 65 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 e.** assumes tha
2b110 74 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c t the text or BL
2b120 4f 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 OB result is in
2b130 63 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 constant space a
2b140 6e 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 nd does not.** c
2b150 6f 70 79 20 74 68 65 20 69 74 20 6f 72 20 63 61 opy the it or ca
2b160 6c 6c 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 ll a destructor
2b170 77 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 when it has fini
2b180 73 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 shed using that
2b190 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 result..** If th
2b1a0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 4th parameter
2b1b0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 to the sqlite3_r
2b1c0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 esult_text* inte
2b1d0 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c rfaces.** or sql
2b1e0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
2b1f0 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 is the special
2b200 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f constant SQLITE_
2b210 54 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 TRANSIENT.** the
2b220 6e 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 n SQLite makes a
2b230 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 copy of the res
2b240 75 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f ult into space o
2b250 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
2b260 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 from [sqlite3_ma
2b270 6c 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 lloc()] before i
2b280 74 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a t returns..**.**
2b290 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
2b2a0 75 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 ult_value() inte
2b2b0 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 rface sets the r
2b2c0 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 esult of.** the
2b2d0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
2b2e0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 ned function to
2b2f0 62 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a be a copy the.**
2b300 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 [unprotected sq
2b310 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
2b320 65 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 ect specified by
2b330 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 the 2nd paramet
2b340 65 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 er. The.** sqli
2b350 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
2b360 28 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b () interface mak
2b370 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 es a copy of the
2b380 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d [sqlite3_value]
2b390 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 .** so that the
2b3a0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 [sqlite3_value]
2b3b0 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 specified in the
2b3c0 20 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 parameter may c
2b3d0 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 hange or.** be d
2b3e0 65 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 eallocated after
2b3f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2b400 76 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 value() returns
2b410 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a without harm..**
2b420 20 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 A [protected sq
2b430 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
2b440 65 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 ect may always b
2b450 65 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a e used where an.
2b460 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 ** [unprotected
2b470 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
2b480 62 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 bject is require
2b490 64 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 d, so either.**
2b4a0 6b 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 kind of [sqlite3
2b4b0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 _value] object c
2b4c0 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 an be used with
2b4d0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a this interface..
2b4e0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 **.** If these r
2b4f0 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c outines are call
2b500 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 ed from within t
2b510 68 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 he different thr
2b520 65 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 ead.** than the
2b530 6f 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 one containing t
2b540 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
2b550 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
2b560 74 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a that received.**
2b570 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f the [sqlite3_co
2b580 6e 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 ntext] pointer,
2b590 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 the results are
2b5a0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
2b5b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
2b5c0 2a 20 5b 48 31 36 34 30 33 5d 20 5b 48 31 36 34 * [H16403] [H164
2b5d0 30 36 5d 20 5b 48 31 36 34 30 39 5d 20 5b 48 31 06] [H16409] [H1
2b5e0 36 34 31 32 5d 20 5b 48 31 36 34 31 35 5d 20 5b 6412] [H16415] [
2b5f0 48 31 36 34 31 38 5d 20 5b 48 31 36 34 32 31 5d H16418] [H16421]
2b600 20 5b 48 31 36 34 32 34 5d 0a 2a 2a 20 5b 48 31 [H16424].** [H1
2b610 36 34 32 37 5d 20 5b 48 31 36 34 33 30 5d 20 5b 6427] [H16430] [
2b620 48 31 36 34 33 33 5d 20 5b 48 31 36 34 33 36 5d H16433] [H16436]
2b630 20 5b 48 31 36 34 33 39 5d 20 5b 48 31 36 34 34 [H16439] [H1644
2b640 32 5d 20 5b 48 31 36 34 34 35 5d 20 5b 48 31 36 2] [H16445] [H16
2b650 34 34 38 5d 0a 2a 2a 20 5b 48 31 36 34 35 31 5d 448].** [H16451]
2b660 20 5b 48 31 36 34 35 34 5d 20 5b 48 31 36 34 35 [H16454] [H1645
2b670 37 5d 20 5b 48 31 36 34 36 30 5d 20 5b 48 31 36 7] [H16460] [H16
2b680 34 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 463].*/.SQLITE_A
2b690 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2b6a0 72 65 73 75 6c 74 5f 62 6c 6f 62 28 73 71 6c 69 result_blob(sqli
2b6b0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
2b6c0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 nst void*, int,
2b6d0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
2b6e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2b6f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2b700 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 double(sqlite3_c
2b710 6f 6e 74 65 78 74 2a 2c 20 64 6f 75 62 6c 65 29 ontext*, double)
2b720 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
2b730 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
2b740 5f 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 _error(sqlite3_c
2b750 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 ontext*, const c
2b760 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 har*, int);.SQLI
2b770 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2b780 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2b790 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 16(sqlite3_conte
2b7a0 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a xt*, const void*
2b7b0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 , int);.SQLITE_A
2b7c0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2b7d0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
2b7e0 62 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 big(sqlite3_cont
2b7f0 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ext*);.SQLITE_AP
2b800 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2b810 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 esult_error_nome
2b820 6d 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 m(sqlite3_contex
2b830 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 t*);.SQLITE_API
2b840 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2b850 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 ult_error_code(s
2b860 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2b870 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 int);.SQLITE_AP
2b880 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2b890 65 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 esult_int(sqlite
2b8a0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3_context*, int)
2b8b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
2b8c0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
2b8d0 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63 _int64(sqlite3_c
2b8e0 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 ontext*, sqlite3
2b8f0 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f _int64);.SQLITE_
2b900 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2b910 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c _result_null(sql
2b920 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a ite3_context*);.
2b930 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2b940 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2b950 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ext(sqlite3_cont
2b960 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 ext*, const char
2b970 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 *, int, void(*)(
2b980 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f void*));.SQLITE_
2b990 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2b9a0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 73 _result_text16(s
2b9b0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2b9c0 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
2b9d0 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a t, void(*)(void*
2b9e0 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ));.SQLITE_API v
2b9f0 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
2ba00 6c 74 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 lt_text16le(sqli
2ba10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
2ba20 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 nst void*, int,v
2ba30 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
2ba40 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2ba50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2ba60 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f ext16be(sqlite3_
2ba70 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 context*, const
2ba80 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 void*, int,void(
2ba90 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 *)(void*));.SQLI
2baa0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2bab0 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
2bac0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2bad0 2a 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *, sqlite3_value
2bae0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 *);.SQLITE_API v
2baf0 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
2bb00 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 lt_zeroblob(sqli
2bb10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e te3_context*, in
2bb20 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 t n);../*.** CAP
2bb30 49 33 52 45 46 3a 20 44 65 66 69 6e 65 20 4e 65 I3REF: Define Ne
2bb40 77 20 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 w Collating Sequ
2bb50 65 6e 63 65 73 20 7b 48 31 36 36 30 30 7d 20 3c ences {H16600} <
2bb60 53 32 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20300>.**.** Th
2bb70 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 ese functions ar
2bb80 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 6e 65 e used to add ne
2bb90 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 w collation sequ
2bba0 65 6e 63 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 ences to the.**
2bbb0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
2bbc0 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 20 tion] specified
2bbd0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
2bbe0 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ument..**.** The
2bbf0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 name of the new
2bc00 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
2bc10 6e 63 65 20 69 73 20 73 70 65 63 69 66 69 65 64 nce is specified
2bc20 20 61 73 20 61 20 55 54 46 2d 38 20 73 74 72 69 as a UTF-8 stri
2bc30 6e 67 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65 ng.** for sqlite
2bc40 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 3_create_collati
2bc50 6f 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 on() and sqlite3
2bc60 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2bc70 6e 5f 76 32 28 29 0a 2a 2a 20 61 6e 64 20 61 20 n_v2().** and a
2bc80 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 66 6f UTF-16 string fo
2bc90 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 r sqlite3_create
2bca0 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 20 _collation16().
2bcb0 49 6e 20 61 6c 6c 20 63 61 73 65 73 0a 2a 2a 20 In all cases.**
2bcc0 74 68 65 20 6e 61 6d 65 20 69 73 20 70 61 73 73 the name is pass
2bcd0 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
2bce0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
2bcf0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 nt..**.** The th
2bd00 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 ird argument may
2bd10 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 be one of the c
2bd20 6f 6e 73 74 61 6e 74 73 20 5b 53 51 4c 49 54 45 onstants [SQLITE
2bd30 5f 55 54 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 _UTF8],.** [SQLI
2bd40 54 45 5f 55 54 46 31 36 4c 45 5d 20 6f 72 20 5b TE_UTF16LE] or [
2bd50 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c SQLITE_UTF16BE],
2bd60 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 indicating that
2bd70 20 74 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69 the user-suppli
2bd80 65 64 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78 ed.** routine ex
2bd90 70 65 63 74 73 20 74 6f 20 62 65 20 70 61 73 73 pects to be pass
2bda0 65 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 ed pointers to s
2bdb0 74 72 69 6e 67 73 20 65 6e 63 6f 64 65 64 20 75 trings encoded u
2bdc0 73 69 6e 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55 sing UTF-8,.** U
2bdd0 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 TF-16 little-end
2bde0 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 ian, or UTF-16 b
2bdf0 69 67 2d 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 ig-endian, respe
2be00 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 ctively. The.**
2be10 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d third argument m
2be20 69 67 68 74 20 61 6c 73 6f 20 62 65 20 5b 53 51 ight also be [SQ
2be30 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e LITE_UTF16_ALIGN
2be40 45 44 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 ED] to indicate
2be50 74 68 61 74 0a 2a 2a 20 74 68 65 20 72 6f 75 74 that.** the rout
2be60 69 6e 65 20 65 78 70 65 63 74 73 20 70 6f 69 6e ine expects poin
2be70 74 65 72 73 20 74 6f 20 31 36 2d 62 69 74 20 77 ters to 16-bit w
2be80 6f 72 64 20 61 6c 69 67 6e 65 64 20 73 74 72 69 ord aligned stri
2be90 6e 67 73 0a 2a 2a 20 6f 66 20 55 54 46 2d 31 36 ngs.** of UTF-16
2bea0 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 in the native b
2beb0 79 74 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 yte order of the
2bec0 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a host computer..
2bed0 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 **.** A pointer
2bee0 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 70 70 to the user supp
2bef0 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d 75 73 lied routine mus
2bf00 74 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 t be passed as t
2bf10 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 67 75 he fifth.** argu
2bf20 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 73 20 ment. If it is
2bf30 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 74 68 NULL, this is th
2bf40 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 74 69 e same as deleti
2bf50 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ng the collation
2bf60 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 73 6f .** sequence (so
2bf70 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e that SQLite can
2bf80 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e 79 6d not call it anym
2bf90 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 74 69 ore)..** Each ti
2bfa0 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 me the applicati
2bfb0 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 6e 63 on supplied func
2bfc0 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2c tion is invoked,
2bfd0 20 69 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a it is passed.**
2bfe0 20 61 73 20 69 74 73 20 66 69 72 73 74 20 70 61 as its first pa
2bff0 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 20 6f rameter a copy o
2c000 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 73 73 f the void* pass
2c010 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68 ed as the fourth
2c020 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
2c030 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2c040 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 73 71 ollation() or sq
2c050 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
2c060 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a lation16()..**.*
2c070 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 * The remaining
2c080 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 arguments to the
2c090 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 application-sup
2c0a0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61 72 plied routine ar
2c0b0 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a 2a e two strings,.*
2c0c0 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e 74 * each represent
2c0d0 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68 2c ed by a (length,
2c0e0 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64 20 data) pair and
2c0f0 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 65 encoded in the e
2c100 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74 20 ncoding.** that
2c110 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 68 was passed as th
2c120 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
2c130 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74 when the collat
2c140 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61 73 ion sequence was
2c150 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e 20 .** registered.
2c160 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 6c 69 {END} The appli
2c170 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 63 cation defined c
2c180 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 ollation routine
2c190 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 should.** retur
2c1a0 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f n negative, zero
2c1b0 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 or positive if
2c1c0 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67 the first string
2c1d0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 0a 2a is less than,.*
2c1e0 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 * equal to, or g
2c1f0 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
2c200 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e 20 69 second string. i
2c210 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d 20 53 .e. (STRING1 - S
2c220 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a 20 54 TRING2)..**.** T
2c230 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 he sqlite3_creat
2c240 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 e_collation_v2()
2c250 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 works like sqli
2c260 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
2c270 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 70 74 tion().** except
2c280 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61 that it takes a
2c290 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74 n extra argument
2c2a0 20 77 68 69 63 68 20 69 73 20 61 20 64 65 73 74 which is a dest
2c2b0 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 74 68 ructor for.** th
2c2c0 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 54 68 e collation. Th
2c2d0 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 73 20 e destructor is
2c2e0 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 called when the
2c2f0 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 collation is.**
2c300 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 69 73 destroyed and is
2c310 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f passed a copy o
2c320 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 f the fourth par
2c330 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 6f 69 ameter void* poi
2c340 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 73 nter.** of the s
2c350 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2c360 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a 2a 2a llation_v2()..**
2c370 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 65 20 Collations are
2c380 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 74 destroyed when t
2c390 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 64 64 hey are overridd
2c3a0 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 6c 6c en by later call
2c3b0 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c s to the.** coll
2c3c0 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 66 ation creation f
2c3d0 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 65 6e unctions or when
2c3e0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
2c3f0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63 6c onnection] is cl
2c400 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 osed.** using [s
2c410 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e qlite3_close()].
2c420 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
2c430 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 36 30 33 5d nts:.** [H16603]
2c440 20 5b 48 31 36 36 30 34 5d 20 5b 48 31 36 36 30 [H16604] [H1660
2c450 36 5d 20 5b 48 31 36 36 30 39 5d 20 5b 48 31 36 6] [H16609] [H16
2c460 36 31 32 5d 20 5b 48 31 36 36 31 35 5d 20 5b 48 612] [H16615] [H
2c470 31 36 36 31 38 5d 20 5b 48 31 36 36 32 31 5d 0a 16618] [H16621].
2c480 2a 2a 20 5b 48 31 36 36 32 34 5d 20 5b 48 31 36 ** [H16624] [H16
2c490 36 32 37 5d 20 5b 48 31 36 36 33 30 5d 0a 2a 2f 627] [H16630].*/
2c4a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2c4b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2c4c0 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c 69 ollation(. sqli
2c4d0 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 63 te3*, . const c
2c4e0 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 har *zName, . i
2c4f0 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20 nt eTextRep, .
2c500 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 void*,. int(*xC
2c510 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e ompare)(void*,in
2c520 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e t,const void*,in
2c530 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 t,const void*).)
2c540 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
2c550 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2c560 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a 20 20 collation_v2(.
2c570 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e sqlite3*, . con
2c580 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
2c590 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c . int eTextRep,
2c5a0 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 . void*,. int
2c5b0 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 (*xCompare)(void
2c5c0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2c5d0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2c5e0 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 73 *),. void(*xDes
2c5f0 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29 3b 0a troy)(void*).);.
2c600 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2c610 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2c620 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c llation16(. sql
2c630 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 ite3*, . const
2c640 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 void *zName,. i
2c650 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20 nt eTextRep, .
2c660 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 void*,. int(*xC
2c670 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e ompare)(void*,in
2c680 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e t,const void*,in
2c690 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 t,const void*).)
2c6a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2c6b0 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e 65 65 F: Collation Nee
2c6c0 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 ded Callbacks {H
2c6d0 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 16700} <S20300>.
2c6e0 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 68 **.** To avoid h
2c6f0 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73 74 65 aving to registe
2c700 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f 6e 20 r all collation
2c710 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f 72 65 sequences before
2c720 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 a database.** c
2c730 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20 73 69 an be used, a si
2c740 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 ngle callback fu
2c750 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 72 65 nction may be re
2c760 67 69 73 74 65 72 65 64 20 77 69 74 68 20 74 68 gistered with th
2c770 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
2c780 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 62 65 onnection] to be
2c790 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 65 72 called whenever
2c7a0 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20 63 6f an undefined co
2c7b0 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 llation.** seque
2c7c0 6e 63 65 20 69 73 20 72 65 71 75 69 72 65 64 2e nce is required.
2c7d0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 .**.** If the fu
2c7e0 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74 nction is regist
2c7f0 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 73 ered using the s
2c800 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
2c810 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c 0a 2a _needed() API,.*
2c820 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70 61 73 * then it is pas
2c830 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 sed the names of
2c840 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 undefined colla
2c850 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 61 tion sequences a
2c860 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65 6e 63 s strings.** enc
2c870 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 20 7b oded in UTF-8. {
2c880 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c 69 74 H16703} If sqlit
2c890 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 e3_collation_nee
2c8a0 64 65 64 31 36 28 29 20 69 73 20 75 73 65 64 2c ded16() is used,
2c8b0 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 61 72 .** the names ar
2c8c0 65 20 70 61 73 73 65 64 20 61 73 20 55 54 46 2d e passed as UTF-
2c8d0 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 6e 61 16 in machine na
2c8e0 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e tive byte order.
2c8f0 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 65 69 .** A call to ei
2c900 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 72 65 ther function re
2c910 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69 73 74 places any exist
2c920 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a ing callback..**
2c930 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 61 6c .** When the cal
2c940 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
2c950 2c 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 , the first argu
2c960 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73 20 61 ment passed is a
2c970 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68 65 20 copy.** of the
2c980 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
2c990 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 to sqlite3_colla
2c9a0 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 6f 72 tion_needed() or
2c9b0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c .** sqlite3_coll
2c9c0 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29 ation_needed16()
2c9d0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 . The second ar
2c9e0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 64 61 gument is the da
2c9f0 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
2ca00 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69 72 64 tion. The third
2ca10 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 argument is one
2ca20 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 of [SQLITE_UTF8
2ca30 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 ], [SQLITE_UTF16
2ca40 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 BE],.** or [SQLI
2ca50 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69 6e 64 TE_UTF16LE], ind
2ca60 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f 73 74 icating the most
2ca70 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72 6d 20 desirable form
2ca80 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e of the collation
2ca90 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66 75 6e .** sequence fun
2caa0 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64 2e 20 ction required.
2cab0 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 The fourth para
2cac0 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d meter is the nam
2cad0 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 71 75 e of the.** requ
2cae0 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 ired collation s
2caf0 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 equence..**.** T
2cb00 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
2cb10 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 67 69 tion should regi
2cb20 73 74 65 72 20 74 68 65 20 64 65 73 69 72 65 64 ster the desired
2cb30 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69 6e 67 collation using
2cb40 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 .** [sqlite3_cre
2cb50 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 5d ate_collation()]
2cb60 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 , [sqlite3_creat
2cb70 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 5d e_collation16()]
2cb80 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 , or.** [sqlite3
2cb90 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2cba0 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 n_v2()]..**.** R
2cbb0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
2cbc0 5b 48 31 36 37 30 32 5d 20 5b 48 31 36 37 30 34 [H16702] [H16704
2cbd0 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 2f 0a 53 51 ] [H16706].*/.SQ
2cbe0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2cbf0 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e ite3_collation_n
2cc00 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74 65 33 eeded(. sqlite3
2cc10 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 20 0a 20 20 *, . void*, .
2cc20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 void(*)(void*,sq
2cc30 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 lite3*,int eText
2cc40 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 Rep,const char*)
2cc50 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
2cc60 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 nt sqlite3_colla
2cc70 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20 tion_needed16(.
2cc80 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f sqlite3*, . vo
2cc90 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a 29 28 76 id*,. void(*)(v
2cca0 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e oid*,sqlite3*,in
2ccb0 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 t eTextRep,const
2ccc0 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a void*).);../*.*
2ccd0 2a 20 53 70 65 63 69 66 79 20 74 68 65 20 6b 65 * Specify the ke
2cce0 79 20 66 6f 72 20 61 6e 20 65 6e 63 72 79 70 74 y for an encrypt
2ccf0 65 64 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 ed database. Th
2cd00 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c is routine shoul
2cd10 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 d be.** called r
2cd20 69 67 68 74 20 61 66 74 65 72 20 73 71 6c 69 74 ight after sqlit
2cd30 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a e3_open()..**.**
2cd40 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 The code to imp
2cd50 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 49 20 lement this API
2cd60 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
2cd70 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20 72 in the public r
2cd80 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c elease.** of SQL
2cd90 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ite..*/.SQLITE_A
2cda0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6b PI int sqlite3_k
2cdb0 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 ey(. sqlite3 *d
2cdc0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b,
2cdd0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
2cde0 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64 20 2a to be rekeyed *
2cdf0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
2ce00 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 pKey, int nKey
2ce10 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f /* The key */
2ce20 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 .);../*.** Chang
2ce30 65 20 74 68 65 20 6b 65 79 20 6f 6e 20 61 6e 20 e the key on an
2ce40 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20 20 open database.
2ce50 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 If the current d
2ce60 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a atabase is not.*
2ce70 2a 20 65 6e 63 72 79 70 74 65 64 2c 20 74 68 69 * encrypted, thi
2ce80 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 s routine will e
2ce90 6e 63 72 79 70 74 20 69 74 2e 20 20 49 66 20 70 ncrypt it. If p
2cea0 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d New==0 or nNew==
2ceb0 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 0, the.** databa
2cec0 73 65 20 69 73 20 64 65 63 72 79 70 74 65 64 2e se is decrypted.
2ced0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 .**.** The code
2cee0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 to implement thi
2cef0 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 s API is not ava
2cf00 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 ilable in the pu
2cf10 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 blic release.**
2cf20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 of SQLite..*/.SQ
2cf30 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2cf40 69 74 65 33 5f 72 65 6b 65 79 28 0a 20 20 73 71 ite3_rekey(. sq
2cf50 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
2cf60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2cf70 44 61 74 61 62 61 73 65 20 74 6f 20 62 65 20 72 Database to be r
2cf80 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 ekeyed */. cons
2cf90 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e t void *pKey, in
2cfa0 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 t nKey /* Th
2cfb0 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a e new key */.);.
2cfc0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2cfd0 20 53 75 73 70 65 6e 64 20 45 78 65 63 75 74 69 Suspend Executi
2cfe0 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74 20 54 on For A Short T
2cff0 69 6d 65 20 7b 48 31 30 35 33 30 7d 20 3c 53 34 ime {H10530} <S4
2d000 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0410>.**.** The
2d010 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 29 20 sqlite3_sleep()
2d020 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65 73 20 function causes
2d030 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 the current thre
2d040 61 64 20 74 6f 20 73 75 73 70 65 6e 64 20 65 78 ad to suspend ex
2d050 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 ecution.** for a
2d060 74 20 6c 65 61 73 74 20 61 20 6e 75 6d 62 65 72 t least a number
2d070 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 of milliseconds
2d080 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 69 74 specified in it
2d090 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a s parameter..**.
2d0a0 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72 61 74 ** If the operat
2d0b0 69 6e 67 20 73 79 73 74 65 6d 20 64 6f 65 73 20 ing system does
2d0c0 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 6c 65 65 not support slee
2d0d0 70 20 72 65 71 75 65 73 74 73 20 77 69 74 68 0a p requests with.
2d0e0 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 20 74 ** millisecond t
2d0f0 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 ime resolution,
2d100 74 68 65 6e 20 74 68 65 20 74 69 6d 65 20 77 69 then the time wi
2d110 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64 20 75 70 ll be rounded up
2d120 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 61 72 65 to.** the neare
2d130 73 74 20 73 65 63 6f 6e 64 2e 20 54 68 65 20 6e st second. The n
2d140 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 umber of millise
2d150 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 conds of sleep a
2d160 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 ctually.** reque
2d170 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 70 sted from the op
2d180 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
2d190 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
2d1a0 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 * SQLite impleme
2d1b0 6e 74 73 20 74 68 69 73 20 69 6e 74 65 72 66 61 nts this interfa
2d1c0 63 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 ce by calling th
2d1d0 65 20 78 53 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 e xSleep().** me
2d1e0 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61 thod of the defa
2d1f0 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 ult [sqlite3_vfs
2d200 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 ] object..**.**
2d210 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 Requirements: [H
2d220 31 30 35 33 33 5d 20 5b 48 31 30 35 33 36 5d 0a 10533] [H10536].
2d230 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
2d240 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 t sqlite3_sleep(
2d250 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 int);../*.** CAP
2d260 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 54 I3REF: Name Of T
2d270 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e he Folder Holdin
2d280 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69 6c 65 g Temporary File
2d290 73 20 7b 48 31 30 33 31 30 7d 20 3c 53 32 30 30 s {H10310} <S200
2d2a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 00>.**.** If thi
2d2b0 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c s global variabl
2d2c0 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 e is made to poi
2d2d0 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 nt to a string w
2d2e0 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6e hich is.** the n
2d2f0 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 72 20 ame of a folder
2d300 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 6f 72 (a.k.a. director
2d310 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 6d y), then all tem
2d320 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a 2a 20 porary files.**
2d330 63 72 65 61 74 65 64 20 62 79 20 53 51 4c 69 74 created by SQLit
2d340 65 20 77 69 6c 6c 20 62 65 20 70 6c 61 63 65 64 e will be placed
2d350 20 69 6e 20 74 68 61 74 20 64 69 72 65 63 74 6f in that directo
2d360 72 79 2e 20 20 49 66 20 74 68 69 73 20 76 61 72 ry. If this var
2d370 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 4e 55 iable.** is a NU
2d380 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e LL pointer, then
2d390 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72 6d 73 SQLite performs
2d3a0 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e a search for an
2d3b0 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 appropriate.**
2d3c0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 temporary file d
2d3d0 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 irectory..**.**
2d3e0 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 It is not safe t
2d3f0 6f 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 o modify this va
2d400 72 69 61 62 6c 65 20 6f 6e 63 65 20 61 20 5b 64 riable once a [d
2d410 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
2d420 6f 6e 5d 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 on].** has been
2d430 6f 70 65 6e 65 64 2e 20 20 49 74 20 69 73 20 69 opened. It is i
2d440 6e 74 65 6e 64 65 64 20 74 68 61 74 20 74 68 69 ntended that thi
2d450 73 20 76 61 72 69 61 62 6c 65 20 62 65 20 73 65 s variable be se
2d460 74 20 6f 6e 63 65 0a 2a 2a 20 61 73 20 70 61 72 t once.** as par
2d470 74 20 6f 66 20 70 72 6f 63 65 73 73 20 69 6e 69 t of process ini
2d480 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20 tialization and
2d490 62 65 66 6f 72 65 20 61 6e 79 20 53 51 4c 69 74 before any SQLit
2d4a0 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 72 e interface.** r
2d4b0 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62 65 65 outines have bee
2d4c0 6e 20 63 61 6c 6c 20 61 6e 64 20 72 65 6d 61 69 n call and remai
2d4d0 6e 20 75 6e 63 68 61 6e 67 65 64 20 74 68 65 72 n unchanged ther
2d4e0 65 61 66 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 eafter..*/.SQLIT
2d4f0 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 E_API char *sqli
2d500 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f te3_temp_directo
2d510 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 ry;../*.** CAPI3
2d520 52 45 46 3a 20 54 65 73 74 20 46 6f 72 20 41 75 REF: Test For Au
2d530 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64 65 20 7b to-Commit Mode {
2d540 48 31 32 39 33 30 7d 20 3c 53 36 30 32 30 30 3e H12930} <S60200>
2d550 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 .** KEYWORDS: {a
2d560 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 7d 0a utocommit mode}.
2d570 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2d580 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 3_get_autocommit
2d590 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
2d5a0 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 urns non-zero or
2d5b0 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 65 20 .** zero if the
2d5c0 67 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 63 given database c
2d5d0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 72 20 onnection is or
2d5e0 69 73 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f is not in autoco
2d5f0 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 mmit mode,.** re
2d600 73 70 65 63 74 69 76 65 6c 79 2e 20 20 41 75 74 spectively. Aut
2d610 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 ocommit mode is
2d620 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a on by default..*
2d630 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 * Autocommit mod
2d640 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 e is disabled by
2d650 20 61 20 5b 42 45 47 49 4e 5d 20 73 74 61 74 65 a [BEGIN] state
2d660 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d ment..** Autocom
2d670 6d 69 74 20 6d 6f 64 65 20 69 73 20 72 65 2d 65 mit mode is re-e
2d680 6e 61 62 6c 65 64 20 62 79 20 61 20 5b 43 4f 4d nabled by a [COM
2d690 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42 41 43 MIT] or [ROLLBAC
2d6a0 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 65 72 K]..**.** If cer
2d6b0 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 20 65 72 tain kinds of er
2d6c0 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e 20 61 20 rors occur on a
2d6d0 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e statement within
2d6e0 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 65 6d 65 a multi-stateme
2d6f0 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
2d700 6e 20 28 65 72 72 6f 72 73 20 69 6e 63 6c 75 64 n (errors includ
2d710 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46 55 4c 4c ing [SQLITE_FULL
2d720 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 ], [SQLITE_IOERR
2d730 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f ],.** [SQLITE_NO
2d740 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 MEM], [SQLITE_BU
2d750 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 54 45 SY], and [SQLITE
2d760 5f 49 4e 54 45 52 52 55 50 54 5d 29 20 74 68 65 _INTERRUPT]) the
2d770 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 n the.** transac
2d780 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 72 6f tion might be ro
2d790 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 lled back automa
2d7a0 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 6f 6e tically. The on
2d7b0 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20 66 69 6e ly way to.** fin
2d7c0 64 20 6f 75 74 20 77 68 65 74 68 65 72 20 53 51 d out whether SQ
2d7d0 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c Lite automatical
2d7e0 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 ly rolled back t
2d7f0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 he transaction a
2d800 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 fter.** an error
2d810 20 69 73 20 74 6f 20 75 73 65 20 74 68 69 73 20 is to use this
2d820 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
2d830 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 If another threa
2d840 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61 75 d changes the au
2d850 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73 20 tocommit status
2d860 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a of the database.
2d870 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 ** connection wh
2d880 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ile this routine
2d890 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 is running, the
2d8a0 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c n the return val
2d8b0 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e ue.** is undefin
2d8c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ed..**.** Requir
2d8d0 65 6d 65 6e 74 73 3a 20 5b 48 31 32 39 33 31 5d ements: [H12931]
2d8e0 20 5b 48 31 32 39 33 32 5d 20 5b 48 31 32 39 33 [H12932] [H1293
2d8f0 33 5d 20 5b 48 31 32 39 33 34 5d 0a 2a 2f 0a 53 3] [H12934].*/.S
2d900 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
2d910 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f lite3_get_autoco
2d920 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a mmit(sqlite3*);.
2d930 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2d940 20 46 69 6e 64 20 54 68 65 20 44 61 74 61 62 61 Find The Databa
2d950 73 65 20 48 61 6e 64 6c 65 20 4f 66 20 41 20 50 se Handle Of A P
2d960 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e repared Statemen
2d970 74 20 7b 48 31 33 31 32 30 7d 20 3c 53 36 30 36 t {H13120} <S606
2d980 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
2d990 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 lite3_db_handle
2d9a0 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
2d9b0 73 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 s the [database
2d9c0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 connection] hand
2d9d0 6c 65 0a 2a 2a 20 74 6f 20 77 68 69 63 68 20 61 le.** to which a
2d9e0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
2d9f0 6d 65 6e 74 5d 20 62 65 6c 6f 6e 67 73 2e 20 20 ment] belongs.
2da00 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f The [database co
2da10 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 72 65 74 nnection].** ret
2da20 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
2da30 5f 64 62 5f 68 61 6e 64 6c 65 20 69 73 20 74 68 _db_handle is th
2da40 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 e same [database
2da50 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 connection] tha
2da60 74 20 77 61 73 20 74 68 65 20 66 69 72 73 74 20 t was the first
2da70 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 argument.** to t
2da80 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 he [sqlite3_prep
2da90 61 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 are_v2()] call (
2daa0 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 or its variants)
2dab0 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 that was used t
2dac0 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20 o.** create the
2dad0 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 statement in the
2dae0 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a first place..**
2daf0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
2db00 3a 20 5b 48 31 33 31 32 33 5d 0a 2a 2f 0a 53 51 : [H13123].*/.SQ
2db10 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
2db20 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e *sqlite3_db_han
2db30 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 dle(sqlite3_stmt
2db40 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
2db50 52 45 46 3a 20 46 69 6e 64 20 74 68 65 20 6e 65 REF: Find the ne
2db60 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 xt prepared stat
2db70 65 6d 65 6e 74 20 7b 48 31 33 31 34 30 7d 20 3c ement {H13140} <
2db80 53 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S60600>.**.** Th
2db90 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 is interface ret
2dba0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
2dbb0 6f 20 74 68 65 20 6e 65 78 74 20 5b 70 72 65 70 o the next [prep
2dbc0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
2dbd0 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d 74 20 61 after.** pStmt a
2dbe0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
2dbf0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
2dc00 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e 20 20 49 nection] pDb. I
2dc10 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 0a f pStmt is NULL.
2dc20 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 69 6e 74 ** then this int
2dc30 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
2dc40 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
2dc50 66 69 72 73 74 20 70 72 65 70 61 72 65 64 20 73 first prepared s
2dc60 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 73 73 6f tatement.** asso
2dc70 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
2dc80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2dc90 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 6e 6f 20 ion pDb. If no
2dca0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2dcb0 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69 65 73 20 nt.** satisfies
2dcc0 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6f the conditions o
2dcd0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 f this routine,
2dce0 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e it returns NULL.
2dcf0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64 61 74 61 .**.** The [data
2dd00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
2dd10 20 70 6f 69 6e 74 65 72 20 44 20 69 6e 20 61 20 pointer D in a
2dd20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 call to.** [sqli
2dd30 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c te3_next_stmt(D,
2dd40 53 29 5d 20 6d 75 73 74 20 72 65 66 65 72 20 74 S)] must refer t
2dd50 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 o an open databa
2dd60 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
2dd70 20 61 6e 64 20 69 6e 20 70 61 72 74 69 63 75 6c and in particul
2dd80 61 72 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 ar must not be a
2dd90 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a NULL pointer..*
2dda0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2ddb0 73 3a 20 5b 48 31 33 31 34 33 5d 20 5b 48 31 33 s: [H13143] [H13
2ddc0 31 34 36 5d 20 5b 48 31 33 31 34 39 5d 20 5b 48 146] [H13149] [H
2ddd0 31 33 31 35 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13152].*/.SQLITE
2dde0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d _API sqlite3_stm
2ddf0 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f t *sqlite3_next_
2de00 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 stmt(sqlite3 *pD
2de10 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 b, sqlite3_stmt
2de20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *pStmt);../*.**
2de30 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 CAPI3REF: Commit
2de40 20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f And Rollback No
2de50 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 tification Callb
2de60 61 63 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 acks {H12950} <S
2de70 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 60400>.**.** The
2de80 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f sqlite3_commit_
2de90 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 hook() interface
2dea0 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c registers a cal
2deb0 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f lback.** functio
2dec0 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 n to be invoked
2ded0 77 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 whenever a trans
2dee0 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
2def0 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c ted..** Any call
2df00 62 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 back set by a pr
2df10 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 evious call to s
2df20 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f qlite3_commit_ho
2df30 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 ok().** for the
2df40 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
2df50 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 nnection is over
2df60 72 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 ridden..** The s
2df70 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f qlite3_rollback_
2df80 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 hook() interface
2df90 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c registers a cal
2dfa0 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f lback.** functio
2dfb0 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 n to be invoked
2dfc0 77 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 whenever a trans
2dfd0 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
2dfe0 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c ted..** Any call
2dff0 62 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 back set by a pr
2e000 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 evious call to s
2e010 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f qlite3_commit_ho
2e020 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 ok().** for the
2e030 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
2e040 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 nnection is over
2e050 72 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 ridden..** The p
2e060 41 72 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 Arg argument is
2e070 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 passed through t
2e080 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a o the callback..
2e090 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 ** If the callba
2e0a0 63 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 ck on a commit h
2e0b0 6f 6f 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 ook function ret
2e0c0 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a urns non-zero,.*
2e0d0 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 * then the commi
2e0e0 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 t is converted i
2e0f0 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a nto a rollback..
2e100 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 **.** If another
2e110 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 function was pr
2e120 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 eviously registe
2e130 72 65 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 red, its.** pArg
2e140 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e value is return
2e150 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e ed. Otherwise N
2e160 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e ULL is returned.
2e170 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 .**.** The callb
2e180 61 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ack implementati
2e190 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 on must not do a
2e1a0 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c nything that wil
2e1b0 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 l modify.** the
2e1c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2e1d0 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 ion that invoked
2e1e0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 the callback.
2e1f0 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 Any actions.** t
2e200 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 o modify the dat
2e210 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2e220 20 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65 must be deferre
2e230 64 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 d until after th
2e240 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 e.** completion
2e250 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
2e260 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 step()] call tha
2e270 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20 t triggered the
2e280 63 6f 6d 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c commit.** or rol
2e290 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 lback hook in th
2e2a0 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a e first place..*
2e2b0 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c * Note that [sql
2e2c0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
2e2d0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
2e2e0 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 step()] both mod
2e2f0 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 ify their.** dat
2e300 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2e310 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e s for the meanin
2e320 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e g of "modify" in
2e330 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e this paragraph.
2e340 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 .**.** Registeri
2e350 6e 67 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 ng a NULL functi
2e360 6f 6e 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 on disables the
2e370 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 callback..**.**
2e380 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 For the purposes
2e390 20 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 of this API, a
2e3a0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
2e3b0 61 69 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e aid to have been
2e3c0 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 .** rolled back
2e3d0 69 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 if an explicit "
2e3e0 52 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d ROLLBACK" statem
2e3f0 65 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 2c ent is executed,
2e400 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 or.** an error
2e410 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 or constraint ca
2e420 75 73 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 uses an implicit
2e430 20 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 rollback to occ
2e440 75 72 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 ur..** The rollb
2e450 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ack callback is
2e460 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 not invoked if a
2e470 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a transaction is.
2e480 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ** automatically
2e490 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 rolled back bec
2e4a0 61 75 73 65 20 74 68 65 20 64 61 74 61 62 61 73 ause the databas
2e4b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
2e4c0 63 6c 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 closed..** The r
2e4d0 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b ollback callback
2e4e0 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 is not invoked
2e4f0 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e if a transaction
2e500 20 69 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 is.** rolled ba
2e510 63 6b 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d ck because a com
2e520 6d 69 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 mit callback ret
2e530 75 72 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a urned non-zero..
2e540 2a 2a 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 ** <todo> Check
2e550 6f 6e 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a on this </todo>.
2e560 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
2e570 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 ts:.** [H12951]
2e580 5b 48 31 32 39 35 32 5d 20 5b 48 31 32 39 35 33 [H12952] [H12953
2e590 5d 20 5b 48 31 32 39 35 34 5d 20 5b 48 31 32 39 ] [H12954] [H129
2e5a0 35 35 5d 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 55].** [H12961]
2e5b0 5b 48 31 32 39 36 32 5d 20 5b 48 31 32 39 36 33 [H12962] [H12963
2e5c0 5d 20 5b 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 ] [H12964].*/.SQ
2e5d0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
2e5e0 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f qlite3_commit_ho
2e5f0 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ok(sqlite3*, int
2e600 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 (*)(void*), void
2e610 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 *);.SQLITE_API v
2e620 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c oid *sqlite3_rol
2e630 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 lback_hook(sqlit
2e640 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 e3*, void(*)(voi
2e650 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f d *), void*);../
2e660 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
2e670 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 ata Change Notif
2e680 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b ication Callback
2e690 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 34 s {H12970} <S604
2e6a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
2e6b0 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f lite3_update_hoo
2e6c0 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 k() interface re
2e6d0 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 gisters a callba
2e6e0 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 ck function.** w
2e6f0 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 ith the [databas
2e700 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 e connection] id
2e710 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 entified by the
2e720 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a first argument.*
2e730 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 * to be invoked
2e740 77 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 69 whenever a row i
2e750 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72 s updated, inser
2e760 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a ted or deleted..
2e770 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 ** Any callback
2e780 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 set by a previou
2e790 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 s call to this f
2e7a0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 unction.** for t
2e7b0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
2e7c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f connection is o
2e7d0 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a verridden..**.**
2e7e0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
2e7f0 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 ment is a pointe
2e800 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f r to the functio
2e810 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e n to invoke when
2e820 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 a.** row is upd
2e830 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f ated, inserted o
2e840 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 r deleted..** Th
2e850 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
2e860 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
2e870 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 is a copy of th
2e880 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
2e890 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 .** to sqlite3_u
2e8a0 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a pdate_hook()..**
2e8b0 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c The second call
2e8c0 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 back argument is
2e8d0 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f one of [SQLITE_
2e8e0 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 INSERT], [SQLITE
2e8f0 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 _DELETE],.** or
2e900 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c [SQLITE_UPDATE],
2e910 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 depending on th
2e920 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 e operation that
2e930 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c caused the call
2e940 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e back.** to be in
2e950 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 voked..** The th
2e960 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 ird and fourth a
2e970 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
2e980 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e callback contain
2e990 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 pointers to the
2e9a0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 .** database and
2e9b0 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 table name cont
2e9c0 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63 aining the affec
2e9d0 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 ted row..** The
2e9e0 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 final callback p
2e9f0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 arameter is the
2ea00 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 72 [rowid] of the r
2ea10 6f 77 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 ow..** In the ca
2ea20 73 65 20 6f 66 20 61 6e 20 75 70 64 61 74 65 2c se of an update,
2ea30 20 74 68 69 73 20 69 73 20 74 68 65 20 5b 72 6f this is the [ro
2ea40 77 69 64 5d 20 61 66 74 65 72 20 74 68 65 20 75 wid] after the u
2ea50 70 64 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63 pdate takes plac
2ea60 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 e..**.** The upd
2ea70 61 74 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 ate hook is not
2ea80 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 invoked when int
2ea90 65 72 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62 ernal system tab
2eaa0 6c 65 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 les are.** modif
2eab0 69 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 ied (i.e. sqlite
2eac0 5f 6d 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69 _master and sqli
2ead0 74 65 5f 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a te_sequence)..**
2eae0 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 68 .** The update h
2eaf0 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ook implementati
2eb00 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 on must not do a
2eb10 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c nything that wil
2eb20 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 l modify.** the
2eb30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2eb40 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 ion that invoked
2eb50 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b the update hook
2eb60 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a . Any actions.*
2eb70 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 * to modify the
2eb80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2eb90 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 65 ion must be defe
2eba0 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 72 rred until after
2ebb0 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 the.** completi
2ebc0 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 on of the [sqlit
2ebd0 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 e3_step()] call
2ebe0 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 74 that triggered t
2ebf0 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a he update hook..
2ec00 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 ** Note that [sq
2ec10 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
2ec20 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
2ec30 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f _step()] both mo
2ec40 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 dify their.** da
2ec50 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2ec60 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 ns for the meani
2ec70 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 ng of "modify" i
2ec80 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 n this paragraph
2ec90 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 ..**.** If anoth
2eca0 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 er function was
2ecb0 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 previously regis
2ecc0 74 65 72 65 64 2c 20 69 74 73 20 70 41 72 67 20 tered, its pArg
2ecd0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65 74 75 value.** is retu
2ece0 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 rned. Otherwise
2ecf0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
2ed00 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 d..**.** Require
2ed10 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 37 ments:.** [H1297
2ed20 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48 31 32 1] [H12973] [H12
2ed30 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20 5b 48 975] [H12977] [H
2ed40 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31 5d 20 12979] [H12981]
2ed50 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39 38 36 [H12983] [H12986
2ed60 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
2ed70 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 70 void *sqlite3_up
2ed80 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c date_hook(. sql
2ed90 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 28 2a ite3*, . void(*
2eda0 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 68 )(void *,int ,ch
2edb0 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20 ar const *,char
2edc0 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 33 5f const *,sqlite3_
2edd0 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 2a 0a int64),. void*.
2ede0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
2edf0 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 69 EF: Enable Or Di
2ee00 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 61 67 sable Shared Pag
2ee10 65 72 20 43 61 63 68 65 20 7b 48 31 30 33 33 30 er Cache {H10330
2ee20 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20 4b 45 } <S30900>.** KE
2ee30 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65 64 20 YWORDS: {shared
2ee40 63 61 63 68 65 7d 20 7b 73 68 61 72 65 64 20 63 cache} {shared c
2ee50 61 63 68 65 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a ache mode}.**.**
2ee60 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e This routine en
2ee70 61 62 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 ables or disable
2ee80 73 20 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 s the sharing of
2ee90 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 the database ca
2eea0 63 68 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d che.** and schem
2eeb0 61 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 a data structure
2eec0 73 20 62 65 74 77 65 65 6e 20 5b 64 61 74 61 62 s between [datab
2eed0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c ase connection |
2eee0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a connections].**
2eef0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 to the same dat
2ef00 61 62 61 73 65 2e 20 53 68 61 72 69 6e 67 20 69 abase. Sharing i
2ef10 73 20 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 s enabled if the
2ef20 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 argument is tru
2ef30 65 0a 2a 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 e.** and disable
2ef40 64 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e d if the argumen
2ef50 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a t is false..**.*
2ef60 2a 20 43 61 63 68 65 20 73 68 61 72 69 6e 67 20 * Cache sharing
2ef70 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 is enabled and d
2ef80 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 isabled for an e
2ef90 6e 74 69 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a ntire process..*
2efa0 2a 20 54 68 69 73 20 69 73 20 61 20 63 68 61 6e * This is a chan
2efb0 67 65 20 61 73 20 6f 66 20 53 51 4c 69 74 65 20 ge as of SQLite
2efc0 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 version 3.5.0. I
2efd0 6e 20 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 n prior versions
2efe0 20 6f 66 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 of SQLite,.** s
2eff0 68 61 72 69 6e 67 20 77 61 73 20 65 6e 61 62 6c haring was enabl
2f000 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66 ed or disabled f
2f010 6f 72 20 65 61 63 68 20 74 68 72 65 61 64 20 73 or each thread s
2f020 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a eparately..**.**
2f030 20 54 68 65 20 63 61 63 68 65 20 73 68 61 72 69 The cache shari
2f040 6e 67 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74 ng mode set by t
2f050 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 65 66 his interface ef
2f060 66 65 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71 fects all subseq
2f070 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f uent.** calls to
2f080 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
2f090 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e ], [sqlite3_open
2f0a0 5f 76 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c _v2()], and [sql
2f0b0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a ite3_open16()]..
2f0c0 2a 2a 20 45 78 69 73 74 69 6e 67 20 64 61 74 61 ** Existing data
2f0d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
2f0e0 20 63 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68 continue use th
2f0f0 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a e sharing mode.*
2f100 2a 20 74 68 61 74 20 77 61 73 20 69 6e 20 65 66 * that was in ef
2f110 66 65 63 74 20 61 74 20 74 68 65 20 74 69 6d 65 fect at the time
2f120 20 74 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65 they were opene
2f130 64 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c d..**.** Virtual
2f140 20 74 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 tables cannot b
2f150 65 20 75 73 65 64 20 77 69 74 68 20 61 20 73 68 e used with a sh
2f160 61 72 65 64 20 63 61 63 68 65 2e 20 20 57 68 65 ared cache. Whe
2f170 6e 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 n shared.** cach
2f180 65 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 e is enabled, th
2f190 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 e [sqlite3_creat
2f1a0 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 e_module()] API
2f1b0 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 used to register
2f1c0 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c .** virtual tabl
2f1d0 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 es will always r
2f1e0 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a eturn an error..
2f1f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
2f200 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 ne returns [SQLI
2f210 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 TE_OK] if shared
2f220 20 63 61 63 68 65 20 77 61 73 20 65 6e 61 62 6c cache was enabl
2f230 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a ed or disabled.*
2f240 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 * successfully.
2f250 20 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d An [error code]
2f260 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 74 68 is returned oth
2f270 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 erwise..**.** Sh
2f280 61 72 65 64 20 63 61 63 68 65 20 69 73 20 64 69 ared cache is di
2f290 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c sabled by defaul
2f2a0 74 2e 20 42 75 74 20 74 68 69 73 20 6d 69 67 68 t. But this migh
2f2b0 74 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 t change in.** f
2f2c0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f uture releases o
2f2d0 66 20 53 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 f SQLite. Appli
2f2e0 63 61 74 69 6f 6e 73 20 74 68 61 74 20 63 61 72 cations that car
2f2f0 65 20 61 62 6f 75 74 20 73 68 61 72 65 64 0a 2a e about shared.*
2f300 2a 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 20 * cache setting
2f310 73 68 6f 75 6c 64 20 73 65 74 20 69 74 20 65 78 should set it ex
2f320 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 plicitly..**.**
2f330 53 65 65 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 See Also: [SQLi
2f340 74 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 te Shared-Cache
2f350 4d 6f 64 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 Mode].**.** Requ
2f360 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 irements: [H1033
2f370 31 5d 20 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 1] [H10336] [H10
2f380 33 33 37 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 337] [H10339].*/
2f390 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2f3a0 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 sqlite3_enable_s
2f3b0 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29 hared_cache(int)
2f3c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2f3d0 46 3a 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72 F: Attempt To Fr
2f3e0 65 65 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b ee Heap Memory {
2f3f0 48 31 37 33 34 30 7d 20 3c 53 33 30 32 32 30 3e H17340} <S30220>
2f400 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2f410 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
2f420 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 y() interface at
2f430 74 65 6d 70 74 73 20 74 6f 20 66 72 65 65 20 4e tempts to free N
2f440 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 bytes.** of hea
2f450 70 20 6d 65 6d 6f 72 79 20 62 79 20 64 65 61 6c p memory by deal
2f460 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 locating non-ess
2f470 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c ential memory al
2f480 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c locations.** hel
2f490 64 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73 d by the databas
2f4a0 65 20 6c 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d e library. {END}
2f4b0 20 20 4d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f Memory used to
2f4c0 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65 0a cache database.
2f4d0 2a 2a 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72 ** pages to impr
2f4e0 6f 76 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ove performance
2f4f0 69 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 is an example of
2f500 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d non-essential m
2f510 65 6d 6f 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 emory..** sqlite
2f520 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
2f530 28 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e () returns the n
2f540 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 umber of bytes a
2f550 63 74 75 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a ctually freed,.*
2f560 2a 20 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 * which might be
2f570 20 6d 6f 72 65 20 6f 72 20 6c 65 73 73 20 74 68 more or less th
2f580 61 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 an the amount re
2f590 71 75 65 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 quested..**.** R
2f5a0 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 equirements: [H1
2f5b0 37 33 34 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 7341] [H17342].*
2f5c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
2f5d0 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
2f5e0 5f 6d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f _memory(int);../
2f5f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 *.** CAPI3REF: I
2f600 6d 70 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e mpose A Limit On
2f610 20 48 65 61 70 20 53 69 7a 65 20 7b 48 31 37 33 Heap Size {H173
2f620 35 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 50} <S30220>.**.
2f630 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 ** The sqlite3_s
2f640 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 oft_heap_limit()
2f650 20 69 6e 74 65 72 66 61 63 65 20 70 6c 61 63 65 interface place
2f660 73 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 s a "soft" limit
2f670 0a 2a 2a 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e .** on the amoun
2f680 74 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 t of heap memory
2f690 20 74 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c that may be all
2f6a0 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 ocated by SQLite
2f6b0 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 ..** If an inter
2f6c0 6e 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 nal allocation i
2f6d0 73 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74 s requested that
2f6e0 20 77 6f 75 6c 64 20 65 78 63 65 65 64 20 74 68 would exceed th
2f6f0 65 0a 2a 2a 20 73 6f 66 74 20 68 65 61 70 20 6c e.** soft heap l
2f700 69 6d 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 imit, [sqlite3_r
2f710 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d elease_memory()]
2f720 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 is invoked one
2f730 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 or.** more times
2f740 20 74 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65 to free up some
2f750 20 73 70 61 63 65 20 62 65 66 6f 72 65 20 74 68 space before th
2f760 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 e allocation is
2f770 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a performed..**.**
2f780 20 54 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61 The limit is ca
2f790 6c 6c 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63 lled "soft", bec
2f7a0 61 75 73 65 20 69 66 20 5b 73 71 6c 69 74 65 33 ause if [sqlite3
2f7b0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
2f7c0 29 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 )].** cannot fre
2f7d0 65 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d e sufficient mem
2f7e0 6f 72 79 20 74 6f 20 70 72 65 76 65 6e 74 20 74 ory to prevent t
2f7f0 68 65 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 he limit from be
2f800 69 6e 67 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a ing exceeded,.**
2f810 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 the memory is a
2f820 6c 6c 6f 63 61 74 65 64 20 61 6e 79 77 61 79 20 llocated anyway
2f830 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 and the current
2f840 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 operation procee
2f850 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 ds..**.** A nega
2f860 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c tive or zero val
2f870 75 65 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 ue for N means t
2f880 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 hat there is no
2f890 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 soft heap limit
2f8a0 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f and.** [sqlite3_
2f8b0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
2f8c0 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 ] will only be c
2f8d0 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 alled when memor
2f8e0 79 20 69 73 20 65 78 68 61 75 73 74 65 64 2e 0a y is exhausted..
2f8f0 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 ** The default v
2f900 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73 6f 66 alue for the sof
2f910 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 t heap limit is
2f920 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 zero..**.** SQLi
2f930 74 65 20 6d 61 6b 65 73 20 61 20 62 65 73 74 20 te makes a best
2f940 65 66 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 effort to honor
2f950 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 the soft heap li
2f960 6d 69 74 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 mit..** But if t
2f970 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d he soft heap lim
2f980 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e it cannot be hon
2f990 6f 72 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 ored, execution
2f9a0 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 will.** continue
2f9b0 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f without error o
2f9c0 72 20 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 r notification.
2f9d0 20 54 68 69 73 20 69 73 20 77 68 79 20 74 68 65 This is why the
2f9e0 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c limit is.** cal
2f9f0 6c 65 64 20 61 20 22 73 6f 66 74 22 20 6c 69 6d led a "soft" lim
2fa00 69 74 2e 20 20 49 74 20 69 73 20 61 64 76 69 73 it. It is advis
2fa10 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 ory only..**.**
2fa20 50 72 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 Prior to SQLite
2fa30 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 version 3.5.0, t
2fa40 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 his routine only
2fa50 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65 constrained the
2fa60 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 memory.** alloc
2fa70 61 74 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 ated by a single
2fa80 20 74 68 72 65 61 64 20 2d 20 74 68 65 20 73 61 thread - the sa
2fa90 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 me thread in whi
2faa0 63 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a ch this routine.
2fab0 2a 2a 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e ** runs. Beginn
2fac0 69 6e 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 ing with SQLite
2fad0 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 version 3.5.0, t
2fae0 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d he soft heap lim
2faf0 69 74 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 it is.** applied
2fb00 20 74 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e to all threads.
2fb10 20 54 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 The value speci
2fb20 66 69 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66 fied for the sof
2fb30 74 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 t heap limit.**
2fb40 69 73 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e is an upper boun
2fb50 64 20 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d d on the total m
2fb60 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
2fb70 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73 for all threads
2fb80 2e 20 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 . In.** version
2fb90 33 2e 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e 3.5.0 there is n
2fba0 6f 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 o mechanism for
2fbb0 6c 69 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61 limiting the hea
2fbc0 70 20 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 p usage for.** i
2fbd0 6e 64 69 76 69 64 75 61 6c 20 74 68 72 65 61 64 ndividual thread
2fbe0 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 s..**.** Require
2fbf0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 ments:.** [H1635
2fc00 31 5d 20 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 1] [H16352] [H16
2fc10 33 35 33 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 353] [H16354] [H
2fc20 31 36 33 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 16355] [H16358].
2fc30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
2fc40 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f id sqlite3_soft_
2fc50 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b heap_limit(int);
2fc60 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
2fc70 3a 20 45 78 74 72 61 63 74 20 4d 65 74 61 64 61 : Extract Metada
2fc80 74 61 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d ta About A Colum
2fc90 6e 20 4f 66 20 41 20 54 61 62 6c 65 20 7b 48 31 n Of A Table {H1
2fca0 32 38 35 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2850} <S60300>.*
2fcb0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
2fcc0 65 20 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61 e returns metada
2fcd0 74 61 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 ta about a speci
2fce0 66 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 fic column of a
2fcf0 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 specific.** data
2fd00 62 61 73 65 20 74 61 62 6c 65 20 61 63 63 65 73 base table acces
2fd10 73 69 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 sible using the
2fd20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
2fd30 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 tion] handle.**
2fd40 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
2fd50 72 73 74 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 rst function arg
2fd60 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ument..**.** The
2fd70 20 63 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 column is ident
2fd80 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 ified by the sec
2fd90 6f 6e 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66 ond, third and f
2fda0 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 ourth parameters
2fdb0 20 74 6f 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 to.** this func
2fdc0 74 69 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 tion. The second
2fdd0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 65 69 parameter is ei
2fde0 74 68 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 ther the name of
2fdf0 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
2fe00 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 (i.e. "main", "
2fe10 74 65 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61 temp" or an atta
2fe20 63 68 65 64 20 64 61 74 61 62 61 73 65 29 20 63 ched database) c
2fe30 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70 ontaining the sp
2fe40 65 63 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 ecified.** table
2fe50 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 or NULL. If it
2fe60 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c is NULL, then al
2fe70 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 l attached datab
2fe80 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65 ases are searche
2fe90 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 d.** for the tab
2fea0 6c 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d le using the sam
2feb0 65 20 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 e algorithm used
2fec0 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 by the database
2fed0 20 65 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 engine to.** re
2fee0 73 6f 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 solve unqualifie
2fef0 64 20 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 d table referenc
2ff00 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 es..**.** The th
2ff10 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 ird and fourth p
2ff20 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 arameters to thi
2ff30 73 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 s function are t
2ff40 68 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c he table and col
2ff50 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 umn.** name of t
2ff60 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d he desired colum
2ff70 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e n, respectively.
2ff80 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 Neither of thes
2ff90 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 e parameters.**
2ffa0 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a may be NULL..**.
2ffb0 2a 2a 20 4d 65 74 61 64 61 74 61 20 69 73 20 72 ** Metadata is r
2ffc0 65 74 75 72 6e 65 64 20 62 79 20 77 72 69 74 69 eturned by writi
2ffd0 6e 67 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 ng to the memory
2ffe0 20 6c 6f 63 61 74 69 6f 6e 73 20 70 61 73 73 65 locations passe
2fff0 64 20 61 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 d as the 5th.**
30000 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 70 and subsequent p
30010 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 arameters to thi
30020 73 20 66 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 s function. Any
30030 6f 66 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e of these argumen
30040 74 73 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c ts may be.** NUL
30050 4c 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 L, in which case
30060 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
30070 6e 67 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 ng element of me
30080 74 61 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 tadata is omitte
30090 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 d..**.** <blockq
300a0 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 uote>.** <table
300b0 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c border="1">.** <
300c0 74 72 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 tr><th> Paramete
300d0 72 20 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 r <th> Output<br
300e0 3e 54 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63 >Type <th> Desc
300f0 72 69 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 ription.**.** <t
30100 72 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 r><td> 5th <td>
30110 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e const char* <td>
30120 20 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 Data type.** <t
30130 72 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 r><td> 6th <td>
30140 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e const char* <td>
30150 20 4e 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 Name of default
30160 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
30170 6e 63 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 nce.** <tr><td>
30180 37 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 7th <td> int
30190 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 <td> True i
301a0 66 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e f column has a N
301b0 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 OT NULL constrai
301c0 6e 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 nt.** <tr><td> 8
301d0 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 th <td> int
301e0 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 <td> True if
301f0 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 column is part
30200 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b of the PRIMARY K
30210 45 59 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 EY.** <tr><td> 9
30220 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 th <td> int
30230 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 <td> True if
30240 20 63 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f column is [AUTO
30250 49 4e 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f INCREMENT].** </
30260 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 table>.** </bloc
30270 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 kquote>.**.** Th
30280 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 e memory pointed
30290 20 74 6f 20 62 79 20 74 68 65 20 63 68 61 72 61 to by the chara
302a0 63 74 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65 cter pointers re
302b0 74 75 72 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a turned for the.*
302c0 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 * declaration ty
302d0 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e pe and collation
302e0 20 73 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c sequence is val
302f0 69 64 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 id only until th
30300 65 20 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 e next.** call t
30310 6f 20 61 6e 79 20 53 51 4c 69 74 65 20 41 50 49 o any SQLite API
30320 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
30330 20 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65 If the specifie
30340 64 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 d table is actua
30350 6c 6c 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b lly a view, an [
30360 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 error code] is r
30370 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 eturned..**.** I
30380 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 f the specified
30390 63 6f 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 column is "rowid
303a0 22 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f ", "oid" or "_ro
303b0 77 69 64 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 wid_" and an.**
303c0 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 [INTEGER PRIMARY
303d0 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 KEY] column has
303e0 20 62 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 been explicitly
303f0 20 64 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 declared, then
30400 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 the output.** pa
30410 72 61 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 rameters are set
30420 20 66 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69 for the explici
30430 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c tly declared col
30440 75 6d 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73 umn. If there is
30450 20 6e 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c no.** explicitl
30460 79 20 64 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 y declared [INTE
30470 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d GER PRIMARY KEY]
30480 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 column, then th
30490 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 e output.** para
304a0 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20 61 meters are set a
304b0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
304c0 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 <pre>.** da
304d0 74 61 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45 ta type: "INTEGE
304e0 52 22 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 R".** collat
304f0 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42 ion sequence: "B
30500 49 4e 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f INARY".** no
30510 74 20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 t null: 0.**
30520 20 70 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a primary key: 1.
30530 2a 2a 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 ** auto incr
30540 65 6d 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 ement: 0.** </pr
30550 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 e>.**.** This fu
30560 6e 63 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 nction may load
30570 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 one or more sche
30580 6d 61 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73 mas from databas
30590 65 20 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a e files. If an.*
305a0 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 * error occurs d
305b0 75 72 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65 uring this proce
305c0 73 73 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65 ss, or if the re
305d0 71 75 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72 quested table or
305e0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f column.** canno
305f0 74 20 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b t be found, an [
30600 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 error code] is r
30610 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 eturned and an e
30620 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6c 65 66 rror message lef
30630 74 0a 2a 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 t.** in the [dat
30640 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
30650 5d 20 28 74 6f 20 62 65 20 72 65 74 72 69 65 76 ] (to be retriev
30660 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ed using sqlite3
30670 5f 65 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a _errmsg())..**.*
30680 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e * This API is on
30690 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 ly available if
306a0 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 the library was
306b0 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
306c0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 e.** [SQLITE_ENA
306d0 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 BLE_COLUMN_METAD
306e0 41 54 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 ATA] C-preproces
306f0 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e sor symbol defin
30700 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ed..*/.SQLITE_AP
30710 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 I int sqlite3_ta
30720 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 ble_column_metad
30730 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a ata(. sqlite3 *
30740 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 db,
30750 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e /* Connection
30760 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
30770 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 st char *zDbName
30780 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 , /* Data
30790 62 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c base name or NUL
307a0 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 L */. const cha
307b0 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 r *zTableName,
307c0 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 /* Table name
307d0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
307e0 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 *zColumnName,
307f0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 /* Column name
30800 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 */. char const
30810 20 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 **pzDataType,
30820 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 /* OUTPUT: Dec
30830 6c 61 72 65 64 20 64 61 74 61 20 74 79 70 65 20 lared data type
30840 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 */. char const
30850 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 **pzCollSeq,
30860 20 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c /* OUTPUT: Coll
30870 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e ation sequence n
30880 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e ame */. int *pN
30890 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 otNull,
308a0 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 /* OUTPUT:
308b0 54 72 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c True if NOT NULL
308c0 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 constraint exis
308d0 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 ts */. int *pPr
308e0 69 6d 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 imaryKey,
308f0 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 /* OUTPUT: T
30900 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 rue if column pa
30910 72 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e rt of PK */. in
30920 74 20 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 t *pAutoinc
30930 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
30940 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c PUT: True if col
30950 75 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 umn is auto-incr
30960 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a ement */.);../*.
30970 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 ** CAPI3REF: Loa
30980 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b d An Extension {
30990 48 31 32 36 30 30 7d 20 3c 53 32 30 35 30 30 3e H12600} <S20500>
309a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
309b0 72 66 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 rface loads an S
309c0 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 QLite extension
309d0 6c 69 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65 library from the
309e0 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a named file..**.
309f0 2a 2a 20 7b 48 31 32 36 30 31 7d 20 54 68 65 20 ** {H12601} The
30a00 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 sqlite3_load_ext
30a10 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 ension() interfa
30a20 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c ce attempts to l
30a30 6f 61 64 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 oad an.**
30a40 20 20 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 SQLite extens
30a50 69 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 ion library cont
30a60 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c ained in the fil
30a70 65 20 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b e zFile..**.** {
30a80 48 31 32 36 30 32 7d 20 54 68 65 20 65 6e 74 72 H12602} The entr
30a90 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 y point is zProc
30aa0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d ..**.** {H12603}
30ab0 20 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 30 2c zProc may be 0,
30ac0 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 in which case t
30ad0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 he name of the e
30ae0 6e 74 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 ntry point.**
30af0 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 73 20 defaults
30b00 74 6f 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 to "sqlite3_exte
30b10 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a nsion_init"..**.
30b20 2a 2a 20 7b 48 31 32 36 30 34 7d 20 54 68 65 20 ** {H12604} The
30b30 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 sqlite3_load_ext
30b40 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 ension() interfa
30b50 63 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a ce shall return.
30b60 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c ** [SQL
30b70 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 ITE_OK] on succe
30b80 73 73 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 ss and [SQLITE_E
30b90 52 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 RROR] if somethi
30ba0 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a ng goes wrong..*
30bb0 2a 0a 2a 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 *.** {H12605} If
30bc0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
30bd0 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 and pzErrMsg is
30be0 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 not 0, then the
30bf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
30c00 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e lite3_load_exten
30c10 73 69 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 sion()] interfac
30c20 65 20 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 e shall attempt
30c30 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 to.** f
30c40 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 ill *pzErrMsg wi
30c50 74 68 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 th error message
30c60 20 74 65 78 74 20 73 74 6f 72 65 64 20 69 6e 20 text stored in
30c70 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 memory.**
30c80 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d obtained from
30c90 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
30ca0 28 29 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 ()]. {END} The
30cb0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
30cc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 6f .** sho
30cd0 75 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d 65 uld free this me
30ce0 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 mory by calling
30cf0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
30d00 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d ..**.** {H12606}
30d10 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 Extension loadi
30d20 6e 67 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c ng must be enabl
30d30 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 ed using.**
30d40 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e [sqlite3_en
30d50 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 able_load_extens
30d60 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 ion()] prior to
30d70 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49 calling this API
30d80 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 74 ,.** ot
30d90 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 herwise an error
30da0 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 will be returne
30db0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
30dc0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 int sqlite3_loa
30dd0 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 d_extension(. s
30de0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
30df0 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 /* Load the
30e00 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 extension into
30e10 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f this database co
30e20 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f nnection */. co
30e30 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c nst char *zFile,
30e40 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
30e50 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 he shared librar
30e60 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 y containing ext
30e70 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 ension */. cons
30e80 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 t char *zProc,
30e90 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 /* Entry point
30ea0 2e 20 20 44 65 72 69 76 65 64 20 66 72 6f 6d 20 . Derived from
30eb0 7a 46 69 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 zFile if 0 */.
30ec0 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 char **pzErrMsg
30ed0 20 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 /* Put err
30ee0 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 or message here
30ef0 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a if not 0 */.);..
30f00 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
30f10 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c Enable Or Disabl
30f20 65 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 e Extension Load
30f30 69 6e 67 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 ing {H12620} <S2
30f40 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 0500>.**.** So a
30f50 73 20 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 s not to open se
30f60 63 75 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 curity holes in
30f70 6f 6c 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f older applicatio
30f80 6e 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 ns that are.** u
30f90 6e 70 72 65 70 61 72 65 64 20 74 6f 20 64 65 61 nprepared to dea
30fa0 6c 20 77 69 74 68 20 65 78 74 65 6e 73 69 6f 6e l with extension
30fb0 20 6c 6f 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 loading, and as
30fc0 20 61 20 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 a means of disa
30fd0 62 6c 69 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 bling.** extensi
30fe0 6f 6e 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 on loading while
30ff0 20 65 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72 evaluating user
31000 2d 65 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68 -entered SQL, th
31010 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a e following API.
31020 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20 74 ** is provided t
31030 6f 20 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 o turn the [sqli
31040 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 te3_load_extensi
31050 6f 6e 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 on()] mechanism
31060 6f 6e 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a on and off..**.*
31070 2a 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 * Extension load
31080 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 ing is off by de
31090 66 61 75 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 fault. See ticke
310a0 74 20 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b t #1863..**.** {
310b0 48 31 32 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 H12621} Call the
310c0 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f sqlite3_enable_
310d0 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 load_extension()
310e0 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e routine with on
310f0 6f 66 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 off==1.**
31100 20 20 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e to turn exten
31110 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 sion loading on
31120 61 6e 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68 and call it with
31130 20 6f 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 onoff==0 to tur
31140 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 n.** it
31150 20 62 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e back off again.
31160 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 .**.** {H12622}
31170 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e Extension loadin
31180 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 g is off by defa
31190 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ult..*/.SQLITE_A
311a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 PI int sqlite3_e
311b0 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e nable_load_exten
311c0 73 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 sion(sqlite3 *db
311d0 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f , int onoff);../
311e0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 *.** CAPI3REF: A
311f0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 utomatically Loa
31200 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 d An Extensions
31210 7b 48 31 32 36 34 30 7d 20 3c 53 32 30 35 30 30 {H12640} <S20500
31220 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 >.**.** This API
31230 20 63 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 can be invoked
31240 61 74 20 70 72 6f 67 72 61 6d 20 73 74 61 72 74 at program start
31250 75 70 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 up in order to r
31260 65 67 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f egister.** one o
31270 72 20 6d 6f 72 65 20 73 74 61 74 69 63 61 6c 6c r more staticall
31280 79 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 y linked extensi
31290 6f 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 ons that will be
312a0 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f available.** to
312b0 20 61 6c 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 all new [databa
312c0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e se connections].
312d0 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 {END}.**.** Thi
312e0 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 s routine stores
312f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
31300 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 e extension in a
31310 6e 20 61 72 72 61 79 20 74 68 61 74 20 69 73 0a n array that is.
31320 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ** obtained from
31330 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
31340 28 29 5d 2e 20 20 49 66 20 79 6f 75 20 72 75 6e ()]. If you run
31350 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 a memory leak c
31360 68 65 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 hecker.** on you
31370 72 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 r program and it
31380 20 72 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 reports a leak
31390 62 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 20 because of this
313a0 61 72 72 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a array, invoke.**
313b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f [sqlite3_reset_
313c0 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 auto_extension()
313d0 5d 20 70 72 69 6f 72 20 74 6f 20 73 68 75 74 64 ] prior to shutd
313e0 6f 77 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 own to free the
313f0 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 memory..**.** {H
31400 31 32 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63 12641} This func
31410 74 69 6f 6e 20 72 65 67 69 73 74 65 72 73 20 61 tion registers a
31420 6e 20 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 n extension entr
31430 79 20 70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a y point that is.
31440 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f ** auto
31450 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 matically invoke
31460 64 20 77 68 65 6e 65 76 65 72 20 61 20 6e 65 77 d whenever a new
31470 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
31480 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 ction].**
31490 20 20 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 is opened usi
314a0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e ng [sqlite3_open
314b0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ()], [sqlite3_op
314c0 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 en16()],.**
314d0 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 or [sqlite3
314e0 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a _open_v2()]..**.
314f0 2a 2a 20 7b 48 31 32 36 34 32 7d 20 44 75 70 6c ** {H12642} Dupl
31500 69 63 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 icate extensions
31510 20 61 72 65 20 64 65 74 65 63 74 65 64 20 73 6f are detected so
31520 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f calling this ro
31530 75 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 utine.**
31540 20 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 multiple times
31550 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 65 with the same e
31560 78 74 65 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d xtension is harm
31570 6c 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 less..**.** {H12
31580 36 34 33 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 643} This routin
31590 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 e stores a point
315a0 65 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 er to the extens
315b0 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 0a ion in an array.
315c0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 61 74 ** that
315d0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
315e0 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f m [sqlite3_mallo
315f0 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 c()]..**.** {H12
31600 36 34 34 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 644} Automatic e
31610 78 74 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 xtensions apply
31620 61 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 across all threa
31630 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ds..*/.SQLITE_AP
31640 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 I int sqlite3_au
31650 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 to_extension(voi
31660 64 20 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 d (*xEntryPoint)
31670 28 76 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 (void));../*.**
31680 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 CAPI3REF: Reset
31690 41 75 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 Automatic Extens
316a0 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 ion Loading {H12
316b0 36 36 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 660} <S20500>.**
316c0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
316d0 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 n disables all p
316e0 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 reviously regist
316f0 65 72 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a ered automatic.*
31700 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 * extensions. {E
31710 4e 44 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 ND} It undoes t
31720 68 65 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c he effect of all
31730 20 70 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 prior.** [sqlit
31740 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f e3_auto_extensio
31750 6e 28 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a n()] calls..**.*
31760 2a 20 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 * {H12661} This
31770 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 function disable
31780 73 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 s all previously
31790 20 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 registered.**
317a0 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 automati
317b0 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a c extensions..**
317c0 0a 2a 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 .** {H12662} Thi
317d0 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 s function disab
317e0 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 les automatic ex
317f0 74 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 tensions in all
31800 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 threads..*/.SQLI
31810 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
31820 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 te3_reset_auto_e
31830 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a xtension(void);.
31840 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 ./*.****** EXPER
31850 49 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 IMENTAL - subjec
31860 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 t to change with
31870 6f 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a out notice *****
31880 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
31890 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f The interface to
318a0 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 the virtual-tab
318b0 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 le mechanism is
318c0 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 currently consid
318d0 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 ered.** to be ex
318e0 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 perimental. The
318f0 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 interface might
31900 20 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d change in incom
31910 70 61 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a patible ways..**
31920 20 49 66 20 74 68 69 73 20 69 73 20 61 20 70 72 If this is a pr
31930 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 oblem for you, d
31940 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e o not use the in
31950 74 65 72 66 61 63 65 20 61 74 20 74 68 69 73 20 terface at this
31960 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e time..**.** When
31970 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 the virtual-tab
31980 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 le mechanism sta
31990 62 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c bilizes, we will
319a0 20 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 declare the.**
319b0 69 6e 74 65 72 66 61 63 65 20 66 69 78 65 64 2c interface fixed,
319c0 20 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 support it inde
319d0 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 finitely, and re
319e0 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e move this commen
319f0 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 t..*/../*.** Str
31a00 75 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 uctures used by
31a10 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
31a20 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 e interface.*/.t
31a30 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
31a40 6c 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 lite3_vtab sqlit
31a50 65 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 e3_vtab;.typedef
31a60 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
31a70 69 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 index_info sqlit
31a80 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 e3_index_info;.t
31a90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
31aa0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
31ab0 72 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 r sqlite3_vtab_c
31ac0 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 ursor;.typedef s
31ad0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f truct sqlite3_mo
31ae0 64 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 dule sqlite3_mod
31af0 75 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ule;../*.** CAPI
31b00 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 3REF: Virtual Ta
31b10 62 6c 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 ble Object {H180
31b20 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 00} <S20400>.**
31b30 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 KEYWORDS: sqlite
31b40 33 5f 6d 6f 64 75 6c 65 0a 2a 2a 20 45 58 50 45 3_module.** EXPE
31b50 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 RIMENTAL.**.** A
31b60 20 6d 6f 64 75 6c 65 20 69 73 20 61 20 63 6c 61 module is a cla
31b70 73 73 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 ss of virtual ta
31b80 62 6c 65 73 2e 20 20 45 61 63 68 20 6d 6f 64 75 bles. Each modu
31b90 6c 65 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a le is defined.**
31ba0 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 by an instance
31bb0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
31bc0 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 structure. Thi
31bd0 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 s structure cons
31be0 69 73 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79 20 6f ists.** mostly o
31bf0 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 f methods for th
31c00 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 e module..**.**
31c10 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 This interface i
31c20 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 s experimental a
31c30 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f nd is subject to
31c40 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 change or.** re
31c50 6d 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 65 20 moval in future
31c60 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 releases of SQLi
31c70 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 te..*/.struct sq
31c80 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 lite3_module {.
31c90 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 int iVersion;.
31ca0 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 int (*xCreate)(
31cb0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a sqlite3*, void *
31cc0 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 pAux,.
31cd0 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 int argc, c
31ce0 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 onst char *const
31cf0 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 *argv,.
31d00 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
31d10 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 ab **ppVTab, cha
31d20 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 r**);. int (*xC
31d30 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a onnect)(sqlite3*
31d40 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 , void *pAux,.
31d50 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
31d60 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 argc, const cha
31d70 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 r *const*argv,.
31d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
31d90 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 lite3_vtab **ppV
31da0 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 Tab, char**);.
31db0 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 int (*xBestIndex
31dc0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
31dd0 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 pVTab, sqlite3_i
31de0 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 ndex_info*);. i
31df0 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 nt (*xDisconnect
31e00 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
31e10 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a pVTab);. int (*
31e20 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 xDestroy)(sqlite
31e30 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
31e40 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 int (*xOpen)(s
31e50 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 qlite3_vtab *pVT
31e60 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 ab, sqlite3_vtab
31e70 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 _cursor **ppCurs
31e80 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c or);. int (*xCl
31e90 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 ose)(sqlite3_vta
31ea0 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e b_cursor*);. in
31eb0 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c t (*xFilter)(sql
31ec0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
31ed0 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 *, int idxNum, c
31ee0 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 onst char *idxSt
31ef0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 r,.
31f00 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c int argc, sql
31f10 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
31f20 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 v);. int (*xNex
31f30 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f t)(sqlite3_vtab_
31f40 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 cursor*);. int
31f50 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f (*xEof)(sqlite3_
31f60 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 vtab_cursor*);.
31f70 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 int (*xColumn)(
31f80 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
31f90 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f sor*, sqlite3_co
31fa0 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 ntext*, int);.
31fb0 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 int (*xRowid)(sq
31fc0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
31fd0 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 r*, sqlite3_int6
31fe0 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 4 *pRowid);. in
31ff0 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c t (*xUpdate)(sql
32000 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 ite3_vtab *, int
32010 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
32020 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 **, sqlite3_int6
32030 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 4 *);. int (*xB
32040 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 egin)(sqlite3_vt
32050 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e ab *pVTab);. in
32060 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 t (*xSync)(sqlit
32070 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b e3_vtab *pVTab);
32080 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 . int (*xCommit
32090 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
320a0 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a pVTab);. int (*
320b0 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 xRollback)(sqlit
320c0 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b e3_vtab *pVTab);
320d0 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 . int (*xFindFu
320e0 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f nction)(sqlite3_
320f0 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 vtab *pVtab, int
32100 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 nArg, const cha
32110 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 r *zName,.
32120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
32130 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 void (**pxFunc)
32140 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
32150 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
32160 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 lue**),.
32170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 v
32180 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 oid **ppArg);.
32190 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 int (*xRename)(s
321a0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
321b0 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ab, const char *
321c0 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a zNew);.};../*.**
321d0 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 CAPI3REF: Virtu
321e0 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e al Table Indexin
321f0 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 g Information {H
32200 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18100} <S20400>.
32210 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c ** KEYWORDS: sql
32220 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a ite3_index_info.
32230 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
32240 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
32250 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 3_index_info str
32260 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 ucture and its s
32270 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 ubstructures is
32280 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 used to.** pass
32290 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f information into
322a0 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 and receive the
322b0 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 reply from the
322c0 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 xBestIndex.** me
322d0 74 68 6f 64 20 6f 66 20 61 6e 20 73 71 6c 69 74 thod of an sqlit
322e0 65 33 5f 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 e3_module. The
322f0 66 69 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49 fields under **I
32300 6e 70 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a nputs** are the.
32310 2a 2a 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65 ** inputs to xBe
32320 73 74 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20 stIndex and are
32330 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73 read-only. xBes
32340 74 49 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69 tIndex inserts i
32350 74 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e ts.** results in
32360 74 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73 to the **Outputs
32370 2a 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a ** fields..**.**
32380 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 The aConstraint
32390 5b 5d 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 [] array records
323a0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f WHERE clause co
323b0 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 nstraints of the
323c0 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 form:.**.** <pr
323d0 65 3e 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72 e>column OP expr
323e0 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 </pre>.**.** whe
323f0 72 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b re OP is =, <
32400 2c 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f , <=, >, o
32410 72 20 26 67 74 3b 3d 2e 20 20 54 68 65 20 70 61 r >=. The pa
32420 72 74 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f rticular operato
32430 72 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 r is.** stored i
32440 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e n aConstraint[].
32450 6f 70 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f op. The index o
32460 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 f the column is
32470 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f stored in.** aCo
32480 6e 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 nstraint[].iColu
32490 6d 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74 mn. aConstraint
324a0 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55 [].usable is TRU
324b0 45 20 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 E if the.** expr
324c0 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 on the right-ha
324d0 6e 64 20 73 69 64 65 20 63 61 6e 20 62 65 20 65 nd side can be e
324e0 76 61 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68 valuated (and th
324f0 75 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e us the constrain
32500 74 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20 t.** is usable)
32510 61 6e 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 and false if it
32520 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 cannot..**.** Th
32530 65 20 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f e optimizer auto
32540 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74 matically invert
32550 73 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 s terms of the f
32560 6f 72 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c orm "expr OP col
32570 75 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 umn".** and make
32580 73 20 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69 s other simplifi
32590 63 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 57 cations to the W
325a0 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61 HERE clause in a
325b0 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 n attempt to.**
325c0 67 65 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 get as many WHER
325d0 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 E clause terms i
325e0 6e 74 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f nto the form sho
325f0 77 6e 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 wn above as poss
32600 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f ible..** The aCo
32610 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 nstraint[] array
32620 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48 only reports WH
32630 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 ERE clause terms
32640 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a in the correct.
32650 2a 2a 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66 ** form that ref
32660 65 72 20 74 6f 20 74 68 65 20 70 61 72 74 69 63 er to the partic
32670 75 6c 61 72 20 76 69 72 74 75 61 6c 20 74 61 62 ular virtual tab
32680 6c 65 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 le being queried
32690 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 ..**.** Informat
326a0 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 ion about the OR
326b0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 DER BY clause is
326c0 20 73 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65 stored in aOrde
326d0 72 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74 rBy[]..** Each t
326e0 65 72 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20 erm of aOrderBy
326f0 72 65 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e records a column
32700 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 of the ORDER BY
32710 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 clause..**.** T
32720 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 he xBestIndex me
32730 74 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 thod must fill a
32740 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b ConstraintUsage[
32750 5d 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 ] with informati
32760 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 on.** about what
32770 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 parameters to p
32780 61 73 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 ass to xFilter.
32790 20 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 If argvIndex>0
327a0 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68 then.** the righ
327b0 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 t-hand side of t
327c0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
327d0 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69 aConstraint[] i
327e0 73 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61 s evaluated.** a
327f0 6e 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61 nd becomes the a
32800 72 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72 rgvIndex-th entr
32810 79 20 69 6e 20 61 72 67 76 2e 20 20 49 66 20 61 y in argv. If a
32820 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b ConstraintUsage[
32830 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 75 ].omit.** is tru
32840 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 e, then the cons
32850 74 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d 65 traint is assume
32860 64 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 61 d to be fully ha
32870 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 ndled by the.**
32880 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e virtual table an
32890 64 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 d is not checked
328a0 20 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74 65 again by SQLite
328b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78 4e ..**.** The idxN
328c0 75 6d 20 61 6e 64 20 69 64 78 50 74 72 20 76 61 um and idxPtr va
328d0 6c 75 65 73 20 61 72 65 20 72 65 63 6f 72 64 65 lues are recorde
328e0 64 20 61 6e 64 20 70 61 73 73 65 64 20 69 6e 74 d and passed int
328f0 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 20 73 71 o xFilter..** sq
32900 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 lite3_free() is
32910 75 73 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 used to free idx
32920 50 74 72 20 69 66 20 6e 65 65 64 54 6f 46 72 65 Ptr if needToFre
32930 65 49 64 78 50 74 72 20 69 73 20 74 72 75 65 2e eIdxPtr is true.
32940 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 .**.** The order
32950 42 79 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 ByConsumed means
32960 20 74 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f that output fro
32970 6d 20 78 46 69 6c 74 65 72 20 77 69 6c 6c 20 6f m xFilter will o
32980 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63 ccur in.** the c
32990 6f 72 72 65 63 74 20 6f 72 64 65 72 20 74 6f 20 orrect order to
329a0 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 satisfy the ORDE
329b0 52 20 42 59 20 63 6c 61 75 73 65 20 73 6f 20 74 R BY clause so t
329c0 68 61 74 20 6e 6f 20 73 65 70 61 72 61 74 65 0a hat no separate.
329d0 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 70 20 ** sorting step
329e0 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a is required..**.
329f0 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 ** The estimated
32a00 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20 61 6e Cost value is an
32a10 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 estimate of the
32a20 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 20 74 cost of doing t
32a30 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 he.** particular
32a40 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c lookup. A full
32a50 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62 6c 65 scan of a table
32a60 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65 73 20 with N entries
32a70 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61 should have.** a
32a80 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41 20 62 cost of N. A b
32a90 69 6e 61 72 79 20 73 65 61 72 63 68 20 6f 66 20 inary search of
32aa0 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74 a table of N ent
32ab0 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 ries should have
32ac0 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70 a.** cost of ap
32ad0 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28 proximately log(
32ae0 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 N)..**.** This i
32af0 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 nterface is expe
32b00 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 rimental and is
32b10 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
32b20 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 e or.** removal
32b30 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
32b40 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f es of SQLite..*/
32b50 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
32b60 69 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f index_info {. /
32b70 2a 20 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e * Inputs */. in
32b80 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 t nConstraint;
32b90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
32ba0 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
32bb0 20 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a aConstraint */.
32bc0 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 struct sqlite3
32bd0 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e _index_constrain
32be0 74 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f t {. int iCo
32bf0 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 lumn;
32c00 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 /* Column on
32c10 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f left-hand side o
32c20 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a f constraint */.
32c30 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 unsigned ch
32c40 61 72 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f ar op; /
32c50 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 * Constraint ope
32c60 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e rator */. un
32c70 73 69 67 6e 65 64 20 63 68 61 72 20 75 73 61 62 signed char usab
32c80 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 le; /* True
32c90 69 66 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 if this constrai
32ca0 6e 74 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a nt is usable */.
32cb0 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 int iTermOf
32cc0 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f fset; /
32cd0 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c * Used internall
32ce0 79 20 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73 y - xBestIndex s
32cf0 68 6f 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a hould ignore */.
32d00 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 } *aConstraint
32d10 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
32d20 54 61 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63 Table of WHERE c
32d30 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 lause constraint
32d40 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 s */. int nOrde
32d50 72 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 rBy;
32d60 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
32d70 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 erms in the ORDE
32d80 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 R BY clause */.
32d90 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
32da0 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a index_orderby {.
32db0 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e int iColumn
32dc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
32dd0 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 * Column number
32de0 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 */. unsigned
32df0 20 63 68 61 72 20 64 65 73 63 3b 20 20 20 20 20 char desc;
32e00 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 /* True for DE
32e10 53 43 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 41 SC. False for A
32e20 53 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 SC. */. } *aOrd
32e30 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 erBy;
32e40 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 /* The ORDER
32e50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 BY clause */.
32e60 2f 2a 20 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 /* Outputs */.
32e70 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
32e80 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f ndex_constraint_
32e90 75 73 61 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 usage {. int
32ea0 61 72 67 76 49 6e 64 65 78 3b 20 20 20 20 20 20 argvIndex;
32eb0 20 20 20 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 /* if >0, c
32ec0 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 70 61 72 onstraint is par
32ed0 74 20 6f 66 20 61 72 67 76 20 74 6f 20 78 46 69 t of argv to xFi
32ee0 6c 74 65 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 lter */. unsi
32ef0 67 6e 65 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 gned char omit;
32f00 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 /* Do not c
32f10 6f 64 65 20 61 20 74 65 73 74 20 66 6f 72 20 74 ode a test for t
32f20 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a his constraint *
32f30 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 /. } *aConstrai
32f40 6e 74 55 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 ntUsage;. int i
32f50 64 78 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 dxNum;
32f60 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
32f70 75 73 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 used to identify
32f80 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 the index */.
32f90 63 68 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20 char *idxStr;
32fa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
32fb0 72 69 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f ring, possibly o
32fc0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
32fd0 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 ite3_malloc */.
32fe0 20 69 6e 74 20 6e 65 65 64 54 6f 46 72 65 65 49 int needToFreeI
32ff0 64 78 53 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 dxStr; /* F
33000 72 65 65 20 69 64 78 53 74 72 20 75 73 69 6e 67 ree idxStr using
33010 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 sqlite3_free()
33020 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 if true */. int
33030 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 orderByConsumed
33040 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
33050 69 66 20 6f 75 74 70 75 74 20 69 73 20 61 6c 72 if output is alr
33060 65 61 64 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a eady ordered */.
33070 20 20 64 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 double estimat
33080 65 64 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 edCost; /*
33090 45 73 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f Estimated cost o
330a0 66 20 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 f using this ind
330b0 65 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 ex */.};.#define
330c0 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f SQLITE_INDEX_CO
330d0 4e 53 54 52 41 49 4e 54 5f 45 51 20 20 20 20 32 NSTRAINT_EQ 2
330e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
330f0 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 INDEX_CONSTRAINT
33100 5f 47 54 20 20 20 20 34 0a 23 64 65 66 69 6e 65 _GT 4.#define
33110 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f SQLITE_INDEX_CO
33120 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 NSTRAINT_LE 8
33130 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
33140 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 INDEX_CONSTRAINT
33150 5f 4c 54 20 20 20 20 31 36 0a 23 64 65 66 69 6e _LT 16.#defin
33160 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 e SQLITE_INDEX_C
33170 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 20 20 20 ONSTRAINT_GE
33180 33 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 32.#define SQLIT
33190 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 E_INDEX_CONSTRAI
331a0 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a NT_MATCH 64../*.
331b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 ** CAPI3REF: Reg
331c0 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 ister A Virtual
331d0 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 Table Implementa
331e0 74 69 6f 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 tion {H18200} <S
331f0 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 20400>.** EXPERI
33200 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 MENTAL.**.** Thi
33210 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
33220 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 d to register a
33230 6e 65 77 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 new module name
33240 77 69 74 68 20 61 0a 2a 2a 20 5b 64 61 74 61 62 with a.** [datab
33250 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
33260 20 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d Module names m
33270 75 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 ust be registere
33280 64 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 d before.** crea
33290 74 69 6e 67 20 6e 65 77 20 76 69 72 74 75 61 6c ting new virtual
332a0 20 74 61 62 6c 65 73 20 6f 6e 20 74 68 65 20 6d tables on the m
332b0 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65 odule, or before
332c0 20 75 73 69 6e 67 0a 2a 2a 20 70 72 65 65 78 69 using.** preexi
332d0 73 74 69 6e 67 20 76 69 72 74 75 61 6c 20 74 61 sting virtual ta
332e0 62 6c 65 73 20 6f 66 20 74 68 65 20 6d 6f 64 75 bles of the modu
332f0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 le..**.** This i
33300 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 nterface is expe
33310 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 rimental and is
33320 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
33330 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 e or.** removal
33340 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
33350 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f es of SQLite..*/
33360 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
33370 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
33380 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
33390 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c te_module(. sql
333a0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
333b0 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 /* SQLit
333c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 e connection to
333d0 72 65 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 register module
333e0 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 with */. const
333f0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 char *zName,
33400 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
33410 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 the module */.
33420 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
33430 64 75 6c 65 20 2a 2c 20 20 20 20 2f 2a 20 4d 65 dule *, /* Me
33440 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f thods for the mo
33450 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a dule */. void *
33460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33470 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 /* Client d
33480 61 74 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f ata for xCreate/
33490 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a xConnect */.);..
334a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
334b0 52 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75 Register A Virtu
334c0 61 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 al Table Impleme
334d0 6e 74 61 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d ntation {H18210}
334e0 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 <S20400>.** EXP
334f0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
33500 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
33510 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 identical to the
33520 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
33530 5f 6d 6f 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f _module()] metho
33540 64 20 61 62 6f 76 65 2c 0a 2a 2a 20 65 78 63 65 d above,.** exce
33550 70 74 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 77 pt that it allow
33560 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 s a destructor f
33570 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 73 70 unction to be sp
33580 65 63 69 66 69 65 64 2e 20 49 74 20 69 73 0a 2a ecified. It is.*
33590 2a 20 65 76 65 6e 20 6d 6f 72 65 20 65 78 70 65 * even more expe
335a0 72 69 6d 65 6e 74 61 6c 20 74 68 61 6e 20 74 68 rimental than th
335b0 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 76 69 e rest of the vi
335c0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 41 50 49 rtual tables API
335d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
335e0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
335f0 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TAL int sqlite3_
33600 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 create_module_v2
33610 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
33620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
33630 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74 * SQLite connect
33640 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 ion to register
33650 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a 20 module with */.
33660 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
33670 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e me, /* N
33680 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c ame of the modul
33690 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c e */. const sql
336a0 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20 20 ite3_module *,
336b0 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 /* Methods for
336c0 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 the module */.
336d0 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 void *,
336e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
336f0 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20 78 lient data for x
33700 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 Create/xConnect
33710 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65 73 74 */. void(*xDest
33720 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20 20 20 roy)(void*)
33730 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 /* Module destru
33740 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ctor function */
33750 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
33760 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 REF: Virtual Tab
33770 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62 6a 65 le Instance Obje
33780 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53 32 30 ct {H18010} <S20
33790 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 400>.** KEYWORDS
337a0 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 0a 2a : sqlite3_vtab.*
337b0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
337c0 2a 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75 6c *.** Every modul
337d0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
337e0 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 73 73 uses a subclass
337f0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
33800 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 g structure.** t
33810 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72 o describe a par
33820 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 65 ticular instance
33830 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 of the module.
33840 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 Each subclass w
33850 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 ill.** be tailor
33860 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 ed to the specif
33870 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 ic needs of the
33880 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
33890 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 ation..** The pu
338a0 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 rpose of this su
338b0 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 perclass is to d
338c0 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 efine certain fi
338d0 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a elds that are.**
338e0 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d common to all m
338f0 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 odule implementa
33900 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 tions..**.** Vir
33910 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 tual tables meth
33920 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 ods can set an e
33930 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 rror message by
33940 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 assigning a.** s
33950 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 tring obtained f
33960 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 rom [sqlite3_mpr
33970 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d intf()] to zErrM
33980 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 sg. The method
33990 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 should.** take c
339a0 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 are that any pri
339b0 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 or string is fre
339c0 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 ed by a call to
339d0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
339e0 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 .** prior to ass
339f0 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 igning a new str
33a00 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 ing to zErrMsg.
33a10 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 After the error
33a20 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 message.** is d
33a30 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 elivered up to t
33a40 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 he client applic
33a50 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e ation, the strin
33a60 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 g will be automa
33a70 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 tically.** freed
33a80 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 by sqlite3_free
33a90 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d () and the zErrM
33aa0 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 sg field will be
33ab0 20 7a 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a 2a zeroed. Note.*
33ac0 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 6d * that sqlite3_m
33ad0 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c printf() and sql
33ae0 69 74 65 33 5f 66 72 65 65 28 29 20 61 72 65 20 ite3_free() are
33af0 75 73 65 64 20 6f 6e 20 74 68 65 20 7a 45 72 72 used on the zErr
33b00 4d 73 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69 6e Msg field.** sin
33b10 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 ce virtual table
33b20 73 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 69 s are commonly i
33b30 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c 6f mplemented in lo
33b40 61 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e adable extension
33b50 73 20 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e 6f s which.** do no
33b60 74 20 68 61 76 65 20 61 63 63 65 73 73 20 74 6f t have access to
33b70 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
33b80 29 20 6f 72 20 73 71 6c 69 74 65 33 46 72 65 65 ) or sqlite3Free
33b90 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ()..**.** This i
33ba0 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 nterface is expe
33bb0 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 rimental and is
33bc0 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
33bd0 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 e or.** removal
33be0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
33bf0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f es of SQLite..*/
33c00 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
33c10 76 74 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 vtab {. const s
33c20 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
33c30 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 Module; /* The
33c40 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 module for this
33c50 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f virtual table */
33c60 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
33c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33c80 20 20 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 /* Used inter
33c90 6e 61 6c 6c 79 20 2a 2f 0a 20 20 63 68 61 72 20 nally */. char
33ca0 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 *zErrMsg;
33cb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 /* Er
33cc0 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d ror message from
33cd0 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 sqlite3_mprintf
33ce0 28 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 () */. /* Virtu
33cf0 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 al table impleme
33d00 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 ntations will ty
33d10 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 pically add addi
33d20 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f tional fields */
33d30 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .};../*.** CAPI3
33d40 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 REF: Virtual Tab
33d50 6c 65 20 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 le Cursor Object
33d60 20 20 7b 48 31 38 30 32 30 7d 20 3c 53 32 30 34 {H18020} <S204
33d70 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 00>.** KEYWORDS:
33d80 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 sqlite3_vtab_cu
33d90 72 73 6f 72 0a 2a 2a 20 45 58 50 45 52 49 4d 45 rsor.** EXPERIME
33da0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 NTAL.**.** Every
33db0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
33dc0 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 tation uses a su
33dd0 62 63 6c 61 73 73 20 6f 66 20 74 68 65 20 66 6f bclass of the fo
33de0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
33df0 65 0a 2a 2a 20 74 6f 20 64 65 73 63 72 69 62 65 e.** to describe
33e00 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f cursors that po
33e10 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 76 69 72 int into the vir
33e20 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 61 tual table and a
33e30 72 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f re used.** to lo
33e40 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 76 op through the v
33e50 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 43 irtual table. C
33e60 75 72 73 6f 72 73 20 61 72 65 20 63 72 65 61 74 ursors are creat
33e70 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 ed using the.**
33e80 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20 xOpen method of
33e90 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 the module. Eac
33ea0 68 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 h module impleme
33eb0 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 ntation will def
33ec0 69 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 ine.** the conte
33ed0 6e 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 nt of a cursor s
33ee0 74 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 tructure to suit
33ef0 20 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a its own needs..
33f00 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 **.** This super
33f10 63 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 class exists in
33f20 6f 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 order to define
33f30 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 fields of the cu
33f40 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 rsor that.** are
33f50 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 common to all i
33f60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a mplementations..
33f70 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
33f80 66 61 63 65 20 69 73 20 65 78 70 65 72 69 6d 65 face is experime
33f90 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75 62 6a ntal and is subj
33fa0 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 ect to change or
33fb0 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e 20 66 .** removal in f
33fc0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f uture releases o
33fd0 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 72 f SQLite..*/.str
33fe0 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 uct sqlite3_vtab
33ff0 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 _cursor {. sqli
34000 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
34010 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c /* Virtual
34020 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 table of this c
34030 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56 69 ursor */. /* Vi
34040 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c rtual table impl
34050 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c ementations will
34060 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 typically add a
34070 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 dditional fields
34080 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.};../*.** CA
34090 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 20 PI3REF: Declare
340a0 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41 20 The Schema Of A
340b0 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 Virtual Table {H
340c0 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18280} <S20400>.
340d0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
340e0 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74 **.** The xCreat
340f0 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20 6d e and xConnect m
34100 65 74 68 6f 64 73 20 6f 66 20 61 20 6d 6f 64 75 ethods of a modu
34110 6c 65 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f le use the follo
34120 77 69 6e 67 20 41 50 49 0a 2a 2a 20 74 6f 20 64 wing API.** to d
34130 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61 eclare the forma
34140 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 t (the names and
34150 20 64 61 74 61 74 79 70 65 73 20 6f 66 20 74 68 datatypes of th
34160 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a e columns) of.**
34170 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
34180 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65 les they impleme
34190 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 nt..**.** This i
341a0 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 nterface is expe
341b0 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 rimental and is
341c0 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
341d0 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 e or.** removal
341e0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
341f0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f es of SQLite..*/
34200 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
34210 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
34220 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c int sqlite3_decl
34230 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 are_vtab(sqlite3
34240 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
34250 43 72 65 61 74 65 54 61 62 6c 65 29 3b 0a 0a 2f CreateTable);../
34260 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
34270 76 65 72 6c 6f 61 64 20 41 20 46 75 6e 63 74 69 verload A Functi
34280 6f 6e 20 46 6f 72 20 41 20 56 69 72 74 75 61 6c on For A Virtual
34290 20 54 61 62 6c 65 20 7b 48 31 38 33 30 30 7d 20 Table {H18300}
342a0 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 <S20400>.** EXPE
342b0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 56 RIMENTAL.**.** V
342c0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 irtual tables ca
342d0 6e 20 70 72 6f 76 69 64 65 20 61 6c 74 65 72 6e n provide altern
342e0 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 ative implementa
342f0 74 69 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f tions of functio
34300 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 ns.** using the
34310 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 xFindFunction me
34320 74 68 6f 64 2e 20 20 42 75 74 20 67 6c 6f 62 61 thod. But globa
34330 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 l versions of th
34340 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a ose functions.**
34350 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f must exist in o
34360 72 64 65 72 20 74 6f 20 62 65 20 6f 76 65 72 6c rder to be overl
34370 6f 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 oaded..**.** Thi
34380 73 20 41 50 49 20 6d 61 6b 65 73 20 73 75 72 65 s API makes sure
34390 20 61 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f a global versio
343a0 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 n of a function
343b0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 with a particula
343c0 72 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75 r.** name and nu
343d0 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 mber of paramete
343e0 72 73 20 65 78 69 73 74 73 2e 20 20 49 66 20 6e rs exists. If n
343f0 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 o such function
34400 65 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 exists.** before
34410 20 74 68 69 73 20 41 50 49 20 69 73 20 63 61 6c this API is cal
34420 6c 65 64 2c 20 61 20 6e 65 77 20 66 75 6e 63 74 led, a new funct
34430 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 2e 20 ion is created.
34440 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 The implementat
34450 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 ion.** of the ne
34460 77 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 w function alway
34470 73 20 63 61 75 73 65 73 20 61 6e 20 65 78 63 65 s causes an exce
34480 70 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f ption to be thro
34490 77 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e wn. So.** the n
344a0 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e ew function is n
344b0 6f 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 ot good for anyt
344c0 68 69 6e 67 20 62 79 20 69 74 73 65 6c 66 2e 20 hing by itself.
344d0 20 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 Its only.** pur
344e0 70 6f 73 65 20 69 73 20 74 6f 20 62 65 20 61 20 pose is to be a
344f0 70 6c 61 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 placeholder func
34500 74 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 tion that can be
34510 20 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 overloaded.** b
34520 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 y virtual tables
34530 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 ..**.** This API
34540 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 should be consi
34550 64 65 72 65 64 20 70 61 72 74 20 6f 66 20 74 68 dered part of th
34560 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
34570 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 77 68 interface,.** wh
34580 69 63 68 20 69 73 20 65 78 70 65 72 69 6d 65 6e ich is experimen
34590 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 tal and subject
345a0 74 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 53 51 to change..*/.SQ
345b0 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
345c0 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 EXPERIMENTAL int
345d0 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 sqlite3_overloa
345e0 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 d_function(sqlit
345f0 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 e3*, const char
34600 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 *zFuncName, int
34610 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 nArg);../*.** Th
34620 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 e interface to t
34630 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
34640 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66 69 6e mechanism defin
34650 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b 20 75 ed above (back u
34660 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d 65 6e p.** to a commen
34670 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73 69 6d t remarkably sim
34680 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 ilar to this one
34690 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 ) is currently c
346a0 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 onsidered.** to
346b0 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e be experimental.
346c0 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 The interface
346d0 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 might change in
346e0 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 incompatible way
346f0 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 s..** If this is
34700 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 a problem for y
34710 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 ou, do not use t
34720 68 65 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 he interface at
34730 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a this time..**.**
34740 20 57 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 When the virtua
34750 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 l-table mechanis
34760 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 m stabilizes, we
34770 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 will declare th
34780 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 e.** interface f
34790 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 ixed, support it
347a0 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 indefinitely, a
347b0 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 nd remove this c
347c0 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a omment..**.*****
347d0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d * EXPERIMENTAL -
347e0 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e subject to chan
347f0 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 ge without notic
34800 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e **************
34810 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .*/../*.** CAPI3
34820 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20 54 6f REF: A Handle To
34830 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 An Open BLOB {H
34840 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30 3e 0a 17800} <S30230>.
34850 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 42 4c ** KEYWORDS: {BL
34860 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c 4f 42 OB handle} {BLOB
34870 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a 2a 20 handles}.**.**
34880 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
34890 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 his object repre
348a0 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 42 4c sents an open BL
348b0 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 5b OB on which.** [
348c0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
348d0 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 n | incremental
348e0 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 62 65 BLOB I/O] can be
348f0 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f performed..** O
34900 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 20 74 bjects of this t
34910 79 70 65 20 61 72 65 20 63 72 65 61 74 65 64 20 ype are created
34920 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 by [sqlite3_blob
34930 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 _open()].** and
34940 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 destroyed by [sq
34950 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 lite3_blob_close
34960 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c ()]..** The [sql
34970 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 ite3_blob_read()
34980 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 ] and [sqlite3_b
34990 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69 6e 74 lob_write()] int
349a0 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e 20 62 erfaces.** can b
349b0 65 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f e used to read o
349c0 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20 73 75 r write small su
349d0 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 bsections of the
349e0 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 5b 73 BLOB..** The [s
349f0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 qlite3_blob_byte
34a00 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 s()] interface r
34a10 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 eturns the size
34a20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e 20 62 of the BLOB in b
34a30 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ytes..*/.typedef
34a40 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
34a50 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62 6c 6f blob sqlite3_blo
34a60 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 b;../*.** CAPI3R
34a70 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 42 20 EF: Open A BLOB
34a80 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 For Incremental
34a90 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c 53 33 I/O {H17810} <S3
34aa0 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0230>.**.** This
34ab0 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70 65 6e interfaces open
34ac0 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 s a [BLOB handle
34ad0 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20 74 68 | handle] to th
34ae0 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64 0a 2a e BLOB located.*
34af0 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c 20 63 * in row iRow, c
34b00 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 olumn zColumn, t
34b10 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e 20 64 able zTable in d
34b20 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 atabase zDb;.**
34b30 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 in other words,
34b40 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20 74 68 the same BLOB th
34b50 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 at would be sele
34b60 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c cted by:.**.** <
34b70 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45 4c 45 pre>.** SELE
34b80 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 CT zColumn FROM
34b90 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45 52 45 zDb.zTable WHERE
34ba0 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f 77 3b [rowid] = iRow;
34bb0 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e 44 7d .** </pre> {END}
34bc0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6c .**.** If the fl
34bd0 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 ags parameter is
34be0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 74 non-zero, the t
34bf0 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 he BLOB is opene
34c00 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a 20 61 6e d for read.** an
34c10 64 20 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 d write access.
34c20 49 66 20 69 74 20 69 73 20 7a 65 72 6f 2c 20 74 If it is zero, t
34c30 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 he BLOB is opene
34c40 64 20 66 6f 72 20 72 65 61 64 20 61 63 63 65 73 d for read acces
34c50 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 s..**.** Note th
34c60 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 at the database
34c70 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 65 20 name is not the
34c80 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20 63 6f filename that co
34c90 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 64 61 ntains.** the da
34ca0 74 61 62 61 73 65 20 62 75 74 20 72 61 74 68 65 tabase but rathe
34cb0 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e r the symbolic n
34cc0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
34cd0 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 61 ase that.** is a
34ce0 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74 68 65 ssigned when the
34cf0 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 6e database is con
34d00 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b 41 54 nected using [AT
34d10 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20 74 68 TACH]..** For th
34d20 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
34d30 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 62 61 file, the databa
34d40 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61 69 6e se name is "main
34d50 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50 20 74 "..** For TEMP t
34d60 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74 61 62 ables, the datab
34d70 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74 65 6d ase name is "tem
34d80 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 p"..**.** On suc
34d90 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b cess, [SQLITE_OK
34da0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
34db0 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f 42 20 d the new [BLOB
34dc0 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 74 74 handle] is writt
34dd0 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 en.** to *ppBlob
34de0 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b . Otherwise an [
34df0 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 error code] is r
34e00 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 79 20 eturned and any
34e10 76 61 6c 75 65 20 77 72 69 74 74 65 6e 0a 2a 2a value written.**
34e20 20 74 6f 20 2a 70 70 42 6c 6f 62 20 73 68 6f 75 to *ppBlob shou
34e30 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 20 62 ld not be used b
34e40 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a y the caller..**
34e50 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
34e60 65 74 73 20 74 68 65 20 5b 64 61 74 61 62 61 73 ets the [databas
34e70 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 e connection] er
34e80 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 ror code and mes
34e90 73 61 67 65 0a 2a 2a 20 61 63 63 65 73 73 69 62 sage.** accessib
34ea0 6c 65 20 76 69 61 20 5b 73 71 6c 69 74 65 33 5f le via [sqlite3_
34eb0 65 72 72 63 6f 64 65 28 29 5d 20 61 6e 64 20 5b errcode()] and [
34ec0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
34ed0 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 ]..**.** If the
34ee0 72 6f 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 row that a BLOB
34ef0 68 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f handle points to
34f00 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 is modified by
34f10 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 an.** [UPDATE],
34f20 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 [DELETE], or by
34f30 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 [ON CONFLICT] si
34f40 64 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 de-effects.** th
34f50 65 6e 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 en the BLOB hand
34f60 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 le is marked as
34f70 22 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 "expired"..** Th
34f80 69 73 20 69 73 20 74 72 75 65 20 69 66 20 61 6e is is true if an
34f90 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 y column of the
34fa0 72 6f 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 row is changed,
34fb0 65 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a even a column.**
34fc0 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 other than the
34fd0 6f 6e 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e one the BLOB han
34fe0 64 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a dle is open on..
34ff0 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c ** Calls to [sql
35000 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 ite3_blob_read()
35010 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 ] and [sqlite3_b
35020 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 lob_write()] for
35030 0a 2a 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c .** a expired BL
35040 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 OB handle fail w
35050 69 74 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f ith an return co
35060 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 de of [SQLITE_AB
35070 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 ORT]..** Changes
35080 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 written into a
35090 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68 BLOB prior to th
350a0 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 e BLOB expiring
350b0 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 are not.** rollb
350c0 61 63 6b 20 62 79 20 74 68 65 20 65 78 70 69 72 ack by the expir
350d0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f ation of the BLO
350e0 42 2e 20 20 53 75 63 68 20 63 68 61 6e 67 65 73 B. Such changes
350f0 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 will eventually
35100 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 .** commit if th
35110 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
35120 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c ntinues to compl
35130 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 etion..**.** Req
35140 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
35150 31 37 38 31 33 5d 20 5b 48 31 37 38 31 34 5d 20 17813] [H17814]
35160 5b 48 31 37 38 31 36 5d 20 5b 48 31 37 38 31 39 [H17816] [H17819
35170 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48 31 37 38 ] [H17821] [H178
35180 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 24].*/.SQLITE_AP
35190 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
351a0 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 ob_open(. sqlit
351b0 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 e3*,. const cha
351c0 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20 r *zDb,. const
351d0 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20 char *zTable,.
351e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c const char *zCol
351f0 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 umn,. sqlite3_i
35200 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74 nt64 iRow,. int
35210 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65 flags,. sqlite
35220 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a 3_blob **ppBlob.
35230 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
35240 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42 EF: Close A BLOB
35250 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d Handle {H17830}
35260 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 <S30230>.**.**
35270 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b Closes an open [
35280 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a BLOB handle]..**
35290 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c .** Closing a BL
352a0 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74 OB shall cause t
352b0 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
352c0 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 action to commit
352d0 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 .** if there are
352e0 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c no other BLOBs,
352f0 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 72 65 70 no pending prep
35300 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2c ared statements,
35310 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 and the.** data
35320 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
35330 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69 is in [autocommi
35340 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61 t mode]..** If a
35350 6e 79 20 77 72 69 74 65 73 20 77 65 72 65 20 6d ny writes were m
35360 61 64 65 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c ade to the BLOB,
35370 20 74 68 65 79 20 6d 69 67 68 74 20 62 65 20 68 they might be h
35380 65 6c 64 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 eld in cache.**
35390 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f 73 65 20 until the close
353a0 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 74 68 65 operation if the
353b0 79 20 77 69 6c 6c 20 66 69 74 2e 20 7b 45 4e 44 y will fit. {END
353c0 7d 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 }.**.** Closing
353d0 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 the BLOB often f
353e0 6f 72 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 orces the change
353f0 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b s.** out to disk
35400 20 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 and so if any I
35410 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c /O errors occur,
35420 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c they will likel
35430 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 y occur.** at th
35440 65 20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 e time when the
35450 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 BLOB is closed.
35460 20 7b 48 31 37 38 33 33 7d 20 41 6e 79 20 65 72 {H17833} Any er
35470 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 20 rors that occur
35480 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69 6e during.** closin
35490 67 20 61 72 65 20 72 65 70 6f 72 74 65 64 20 61 g are reported a
354a0 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 s a non-zero ret
354b0 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a urn value..**.**
354c0 20 54 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f The BLOB is clo
354d0 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 sed unconditiona
354e0 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 20 74 68 lly. Even if th
354f0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
35500 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 ns.** an error c
35510 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 ode, the BLOB is
35520 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a 2a still closed..*
35530 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
35540 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d 20 5b s:.** [H17833] [
35550 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 39 5d H17836] [H17839]
35560 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
35570 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
35580 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c close(sqlite3_bl
35590 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ob *);../*.** CA
355a0 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e 20 54 PI3REF: Return T
355b0 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 he Size Of An Op
355c0 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d en BLOB {H17840}
355d0 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 <S30230>.**.**
355e0 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 Returns the size
355f0 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 in bytes of the
35600 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 6c 65 BLOB accessible
35610 20 76 69 61 20 74 68 65 20 6f 70 65 6e 0a 2a 2a via the open.**
35620 20 5b 5d 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 []BLOB handle]
35630 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 in its only argu
35640 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ment..**.** Requ
35650 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
35660 37 38 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 7843].*/.SQLITE_
35670 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
35680 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 blob_bytes(sqlit
35690 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a e3_blob *);../*.
356a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 61 ** CAPI3REF: Rea
356b0 64 20 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c d Data From A BL
356c0 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 OB Incrementally
356d0 20 7b 48 31 37 38 35 30 7d 20 3c 53 33 30 32 33 {H17850} <S3023
356e0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 0>.**.** This fu
356f0 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 nction is used t
35700 6f 20 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d o read data from
35710 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 an open [BLOB h
35720 61 6e 64 6c 65 5d 20 69 6e 74 6f 20 61 0a 2a 2a andle] into a.**
35730 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 caller-supplied
35740 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 buffer. N bytes
35750 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 of data are cop
35760 69 65 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20 ied into buffer
35770 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70 Z.** from the op
35780 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e en BLOB, startin
35790 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 g at offset iOff
357a0 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 66 set..**.** If of
357b0 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 fset iOffset is
357c0 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 less than N byte
357d0 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f s from the end o
357e0 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b f the BLOB,.** [
357f0 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 SQLITE_ERROR] is
35800 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f returned and no
35810 20 64 61 74 61 20 69 73 20 72 65 61 64 2e 20 20 data is read.
35820 49 66 20 4e 20 6f 72 20 69 4f 66 66 73 65 74 20 If N or iOffset
35830 69 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 is.** less than
35840 7a 65 72 6f 2c 20 5b 53 51 4c 49 54 45 5f 45 52 zero, [SQLITE_ER
35850 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ROR] is returned
35860 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 and no data is
35870 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 read..**.** An a
35880 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 66 ttempt to read f
35890 72 6f 6d 20 61 6e 20 65 78 70 69 72 65 64 20 5b rom an expired [
358a0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 BLOB handle] fai
358b0 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 ls with an.** er
358c0 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c ror code of [SQL
358d0 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a ITE_ABORT]..**.*
358e0 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 * On success, SQ
358f0 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
35900 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ned..** Otherwis
35910 65 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 e, an [error cod
35920 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 e] or an [extend
35930 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 ed error code] i
35940 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
35950 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
35960 2a 2a 20 5b 48 31 37 38 35 33 5d 20 5b 48 31 37 ** [H17853] [H17
35970 38 35 36 5d 20 5b 48 31 37 38 35 39 5d 20 5b 48 856] [H17859] [H
35980 31 37 38 36 32 5d 20 5b 48 31 37 38 36 33 5d 20 17862] [H17863]
35990 5b 48 31 37 38 36 35 5d 20 5b 48 31 37 38 36 38 [H17865] [H17868
359a0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
359b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
359c0 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c _read(sqlite3_bl
359d0 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69 ob *, void *Z, i
359e0 6e 74 20 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65 nt N, int iOffse
359f0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
35a00 52 45 46 3a 20 57 72 69 74 65 20 44 61 74 61 20 REF: Write Data
35a10 49 6e 74 6f 20 41 20 42 4c 4f 42 20 49 6e 63 72 Into A BLOB Incr
35a20 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 37 ementally {H1787
35a30 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 0} <S30230>.**.*
35a40 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
35a50 69 73 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 is used to write
35a60 20 64 61 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 data into an op
35a70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d en [BLOB handle]
35a80 20 66 72 6f 6d 20 61 0a 2a 2a 20 63 61 6c 6c 65 from a.** calle
35a90 72 2d 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 r-supplied buffe
35aa0 72 2e 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 r. N bytes of da
35ab0 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 ta are copied fr
35ac0 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 5a 0a om the buffer Z.
35ad0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e ** into the open
35ae0 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 BLOB, starting
35af0 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 at offset iOffse
35b00 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 t..**.** If the
35b10 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 70 61 [BLOB handle] pa
35b20 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 ssed as the firs
35b30 74 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 6e t argument was n
35b40 6f 74 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a ot opened for.**
35b50 20 77 72 69 74 69 6e 67 20 28 74 68 65 20 66 6c writing (the fl
35b60 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f ags parameter to
35b70 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
35b80 70 65 6e 28 29 5d 20 77 61 73 20 7a 65 72 6f 29 pen()] was zero)
35b90 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 ,.** this functi
35ba0 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 on returns [SQLI
35bb0 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a TE_READONLY]..**
35bc0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
35bd0 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66 n may only modif
35be0 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f y the contents o
35bf0 66 20 74 68 65 20 42 4c 4f 42 3b 20 69 74 20 69 f the BLOB; it i
35c00 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c s.** not possibl
35c10 65 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 e to increase th
35c20 65 20 73 69 7a 65 20 6f 66 20 61 20 42 4c 4f 42 e size of a BLOB
35c30 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 2e using this API.
35c40 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f .** If offset iO
35c50 66 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 ffset is less th
35c60 61 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 an N bytes from
35c70 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 the end of the B
35c80 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f LOB,.** [SQLITE_
35c90 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e ERROR] is return
35ca0 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 ed and no data i
35cb0 73 20 77 72 69 74 74 65 6e 2e 20 20 49 66 20 4e s written. If N
35cc0 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e is.** less than
35cd0 20 7a 65 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52 zero [SQLITE_ER
35ce0 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ROR] is returned
35cf0 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 and no data is
35d00 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 41 written..**.** A
35d10 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 n attempt to wri
35d20 74 65 20 74 6f 20 61 6e 20 65 78 70 69 72 65 64 te to an expired
35d30 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 [BLOB handle] f
35d40 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 ails with an.**
35d50 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 error code of [S
35d60 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 QLITE_ABORT]. W
35d70 72 69 74 65 73 20 74 6f 20 74 68 65 20 42 4c 4f rites to the BLO
35d80 42 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 0a B that occurred.
35d90 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 5b 42 ** before the [B
35da0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 LOB handle] expi
35db0 72 65 64 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c red are not roll
35dc0 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 0a 2a ed back by the.*
35dd0 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 * expiration of
35de0 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 the handle, thou
35df0 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74 68 6f gh of course tho
35e00 73 65 20 63 68 61 6e 67 65 73 20 6d 69 67 68 74 se changes might
35e10 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 76 .** have been ov
35e20 65 72 77 72 69 74 74 65 6e 20 62 79 20 74 68 65 erwritten by the
35e30 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 statement that
35e40 65 78 70 69 72 65 64 20 74 68 65 20 42 4c 4f 42 expired the BLOB
35e50 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 handle.** or by
35e60 20 6f 74 68 65 72 20 69 6e 64 65 70 65 6e 64 65 other independe
35e70 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a nt statements..*
35e80 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c *.** On success,
35e90 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
35ea0 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 turned..** Other
35eb0 77 69 73 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 wise, an [error
35ec0 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 code] or an [ex
35ed0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
35ee0 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a e] is returned..
35ef0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
35f00 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 37 33 5d 20 ts:.** [H17873]
35f10 5b 48 31 37 38 37 34 5d 20 5b 48 31 37 38 37 35 [H17874] [H17875
35f20 5d 20 5b 48 31 37 38 37 36 5d 20 5b 48 31 37 38 ] [H17876] [H178
35f30 37 37 5d 20 5b 48 31 37 38 37 39 5d 20 5b 48 31 77] [H17879] [H1
35f40 37 38 38 32 5d 20 5b 48 31 37 38 38 35 5d 0a 2a 7882] [H17885].*
35f50 2a 20 5b 48 31 37 38 38 38 5d 0a 2a 2f 0a 53 51 * [H17888].*/.SQ
35f60 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
35f70 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 ite3_blob_write(
35f80 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 sqlite3_blob *,
35f90 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 const void *z, i
35fa0 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 nt n, int iOffse
35fb0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
35fc0 52 45 46 3a 20 56 69 72 74 75 61 6c 20 46 69 6c REF: Virtual Fil
35fd0 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 63 74 73 e System Objects
35fe0 20 7b 48 31 31 32 30 30 7d 20 3c 53 32 30 31 30 {H11200} <S2010
35ff0 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 0>.**.** A virtu
36000 61 6c 20 66 69 6c 65 73 79 73 74 65 6d 20 28 56 al filesystem (V
36010 46 53 29 20 69 73 20 61 6e 20 5b 73 71 6c 69 74 FS) is an [sqlit
36020 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 0a 2a e3_vfs] object.*
36030 2a 20 74 68 61 74 20 53 51 4c 69 74 65 20 75 73 * that SQLite us
36040 65 73 20 74 6f 20 69 6e 74 65 72 61 63 74 0a 2a es to interact.*
36050 2a 20 77 69 74 68 20 74 68 65 20 75 6e 64 65 72 * with the under
36060 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 lying operating
36070 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74 20 53 51 system. Most SQ
36080 4c 69 74 65 20 62 75 69 6c 64 73 20 63 6f 6d 65 Lite builds come
36090 20 77 69 74 68 20 61 0a 2a 2a 20 73 69 6e 67 6c with a.** singl
360a0 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 74 68 e default VFS th
360b0 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 at is appropriat
360c0 65 20 66 6f 72 20 74 68 65 20 68 6f 73 74 20 63 e for the host c
360d0 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e 65 77 20 omputer..** New
360e0 56 46 53 65 73 20 63 61 6e 20 62 65 20 72 65 67 VFSes can be reg
360f0 69 73 74 65 72 65 64 20 61 6e 64 20 65 78 69 73 istered and exis
36100 74 69 6e 67 20 56 46 53 65 73 20 63 61 6e 20 62 ting VFSes can b
36110 65 20 75 6e 72 65 67 69 73 74 65 72 65 64 2e 0a e unregistered..
36120 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
36130 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 interfaces are
36140 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 provided..**.**
36150 54 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f The sqlite3_vfs_
36160 66 69 6e 64 28 29 20 69 6e 74 65 72 66 61 63 65 find() interface
36170 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
36180 65 72 20 74 6f 20 61 20 56 46 53 20 67 69 76 65 er to a VFS give
36190 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a 2a 20 4e n its name..** N
361a0 61 6d 65 73 20 61 72 65 20 63 61 73 65 20 73 65 ames are case se
361b0 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e 61 6d 65 nsitive..** Name
361c0 73 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 s are zero-termi
361d0 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 nated UTF-8 stri
361e0 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 ngs..** If there
361f0 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 61 20 is no match, a
36200 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 NULL pointer is
36210 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 returned..** If
36220 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e 55 4c 4c zVfsName is NULL
36230 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75 6c then the defaul
36240 74 20 56 46 53 20 69 73 20 72 65 74 75 72 6e 65 t VFS is returne
36250 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 56 46 53 d..**.** New VFS
36260 65 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65 es are registere
36270 64 20 77 69 74 68 20 73 71 6c 69 74 65 33 5f 76 d with sqlite3_v
36280 66 73 5f 72 65 67 69 73 74 65 72 28 29 2e 0a 2a fs_register()..*
36290 2a 20 45 61 63 68 20 6e 65 77 20 56 46 53 20 62 * Each new VFS b
362a0 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75 ecomes the defau
362b0 6c 74 20 56 46 53 20 69 66 20 74 68 65 20 6d 61 lt VFS if the ma
362c0 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 73 20 73 keDflt flag is s
362d0 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 et..** The same
362e0 56 46 53 20 63 61 6e 20 62 65 20 72 65 67 69 73 VFS can be regis
362f0 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 tered multiple t
36300 69 6d 65 73 20 77 69 74 68 6f 75 74 20 69 6e 6a imes without inj
36310 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 6b 65 20 ury..** To make
36320 61 6e 20 65 78 69 73 74 69 6e 67 20 56 46 53 20 an existing VFS
36330 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 into the default
36340 20 56 46 53 2c 20 72 65 67 69 73 74 65 72 20 69 VFS, register i
36350 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 74 68 20 t again.** with
36360 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 the makeDflt fla
36370 67 20 73 65 74 2e 20 20 49 66 20 74 77 6f 20 64 g set. If two d
36380 69 66 66 65 72 65 6e 74 20 56 46 53 65 73 20 77 ifferent VFSes w
36390 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 ith the.** same
363a0 6e 61 6d 65 20 61 72 65 20 72 65 67 69 73 74 65 name are registe
363b0 72 65 64 2c 20 74 68 65 20 62 65 68 61 76 69 6f red, the behavio
363c0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 r is undefined.
363d0 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 69 73 20 If a.** VFS is
363e0 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 registered with
363f0 61 20 6e 61 6d 65 20 74 68 61 74 20 69 73 20 4e a name that is N
36400 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 ULL or an empty
36410 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 65 6e 20 string,.** then
36420 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 the behavior is
36430 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
36440 20 55 6e 72 65 67 69 73 74 65 72 20 61 20 56 46 Unregister a VF
36450 53 20 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 S with the sqlit
36460 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 e3_vfs_unregiste
36470 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a r() interface..*
36480 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 * If the default
36490 20 56 46 53 20 69 73 20 75 6e 72 65 67 69 73 74 VFS is unregist
364a0 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 20 56 46 ered, another VF
364b0 53 20 69 73 20 63 68 6f 73 65 6e 20 61 73 0a 2a S is chosen as.*
364c0 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 * the default.
364d0 54 68 65 20 63 68 6f 69 63 65 20 66 6f 72 20 74 The choice for t
364e0 68 65 20 6e 65 77 20 56 46 53 20 69 73 20 61 72 he new VFS is ar
364f0 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 52 bitrary..**.** R
36500 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
36510 5b 48 31 31 32 30 33 5d 20 5b 48 31 31 32 30 36 [H11203] [H11206
36520 5d 20 5b 48 31 31 32 30 39 5d 20 5b 48 31 31 32 ] [H11209] [H112
36530 31 32 5d 20 5b 48 31 31 32 31 35 5d 20 5b 48 31 12] [H11215] [H1
36540 31 32 31 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 1218].*/.SQLITE_
36550 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66 73 20 API sqlite3_vfs
36560 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e *sqlite3_vfs_fin
36570 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 d(const char *zV
36580 66 73 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f fsName);.SQLITE_
36590 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
365a0 76 66 73 5f 72 65 67 69 73 74 65 72 28 73 71 6c vfs_register(sql
365b0 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6d ite3_vfs*, int m
365c0 61 6b 65 44 66 6c 74 29 3b 0a 53 51 4c 49 54 45 akeDflt);.SQLITE
365d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
365e0 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28 _vfs_unregister(
365f0 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b 0a 0a sqlite3_vfs*);..
36600 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
36610 4d 75 74 65 78 65 73 20 7b 48 31 37 30 30 30 7d Mutexes {H17000}
36620 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20000>.**.**
36630 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 The SQLite core
36640 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74 69 uses these routi
36650 6e 65 73 20 66 6f 72 20 74 68 72 65 61 64 0a 2a nes for thread.*
36660 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f * synchronizatio
36670 6e 2e 20 54 68 6f 75 67 68 20 74 68 65 79 20 61 n. Though they a
36680 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 re intended for
36690 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 internal.** use
366a0 62 79 20 53 51 4c 69 74 65 2c 20 63 6f 64 65 20 by SQLite, code
366b0 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e that links again
366c0 73 74 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 st SQLite is.**
366d0 70 65 72 6d 69 74 74 65 64 20 74 6f 20 75 73 65 permitted to use
366e0 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 6f any of these ro
366f0 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 utines..**.** Th
36700 65 20 53 51 4c 69 74 65 20 73 6f 75 72 63 65 20 e SQLite source
36710 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73 20 6d 75 code contains mu
36720 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 ltiple implement
36730 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 ations.** of the
36740 73 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 se mutex routine
36750 73 2e 20 20 41 6e 20 61 70 70 72 6f 70 72 69 61 s. An appropria
36760 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f te implementatio
36770 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63 74 65 64 n.** is selected
36780 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 automatically a
36790 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 t compile-time.
367a0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a The following.*
367b0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
367c0 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 s are available
367d0 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f in the SQLite co
367e0 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a re:.**.** <ul>.*
367f0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f * <li> SQLITE_
36800 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20 3c 6c 69 MUTEX_OS2.** <li
36810 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 > SQLITE_MUTEX
36820 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c 6c 69 3e _PTHREAD.** <li>
36830 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f SQLITE_MUTEX_
36840 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 W32.** <li> SQ
36850 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a LITE_MUTEX_NOOP.
36860 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
36870 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f he SQLITE_MUTEX_
36880 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 NOOP implementat
36890 69 6f 6e 20 69 73 20 61 20 73 65 74 20 6f 66 20 ion is a set of
368a0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 74 68 61 74 routines.** that
368b0 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c 20 6c 6f does no real lo
368c0 63 6b 69 6e 67 20 61 6e 64 20 69 73 20 61 70 70 cking and is app
368d0 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 ropriate for use
368e0 20 69 6e 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 2d in.** a single-
368f0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 threaded applica
36900 74 69 6f 6e 2e 20 20 54 68 65 20 53 51 4c 49 54 tion. The SQLIT
36910 45 5f 4d 55 54 45 58 5f 4f 53 32 2c 0a 2a 2a 20 E_MUTEX_OS2,.**
36920 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 SQLITE_MUTEX_PTH
36930 52 45 41 44 2c 20 61 6e 64 20 53 51 4c 49 54 45 READ, and SQLITE
36940 5f 4d 55 54 45 58 5f 57 33 32 20 69 6d 70 6c 65 _MUTEX_W32 imple
36950 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 mentations.** ar
36960 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f e appropriate fo
36970 72 20 75 73 65 20 6f 6e 20 4f 53 2f 32 2c 20 55 r use on OS/2, U
36980 6e 69 78 2c 20 61 6e 64 20 57 69 6e 64 6f 77 73 nix, and Windows
36990 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 ..**.** If SQLit
369a0 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 e is compiled wi
369b0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 th the SQLITE_MU
369c0 54 45 58 5f 41 50 50 44 45 46 20 70 72 65 70 72 TEX_APPDEF prepr
369d0 6f 63 65 73 73 6f 72 0a 2a 2a 20 6d 61 63 72 6f ocessor.** macro
369e0 20 64 65 66 69 6e 65 64 20 28 77 69 74 68 20 22 defined (with "
369f0 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 -DSQLITE_MUTEX_A
36a00 50 50 44 45 46 3d 31 22 29 2c 20 74 68 65 6e 20 PPDEF=1"), then
36a10 6e 6f 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c no mutex.** impl
36a20 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 69 6e ementation is in
36a30 63 6c 75 64 65 64 20 77 69 74 68 20 74 68 65 20 cluded with the
36a40 6c 69 62 72 61 72 79 2e 20 49 6e 20 74 68 69 73 library. In this
36a50 20 63 61 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 case the.** app
36a60 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 lication must su
36a70 70 70 6c 79 20 61 20 63 75 73 74 6f 6d 20 6d 75 pply a custom mu
36a80 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
36a90 6f 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 on using the.**
36aa0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
36ab0 55 54 45 58 5d 20 6f 70 74 69 6f 6e 20 6f 66 20 UTEX] option of
36ac0 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 the sqlite3_conf
36ad0 69 67 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a ig() function.**
36ae0 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 before calling
36af0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
36b00 7a 65 28 29 20 6f 72 20 61 6e 79 20 6f 74 68 65 ze() or any othe
36b10 72 20 70 75 62 6c 69 63 20 73 71 6c 69 74 65 33 r public sqlite3
36b20 5f 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 _.** function th
36b30 61 74 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 at calls sqlite3
36b40 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a _initialize()..*
36b50 2a 0a 2a 2a 20 7b 48 31 37 30 31 31 7d 20 54 68 *.** {H17011} Th
36b60 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
36b70 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 alloc() routine
36b80 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a allocates a new.
36b90 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 ** mutex and ret
36ba0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
36bb0 6f 20 69 74 2e 20 7b 48 31 37 30 31 32 7d 20 49 o it. {H17012} I
36bc0 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c f it returns NUL
36bd0 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 L.** that means
36be0 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 that a mutex cou
36bf0 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 ld not be alloca
36c00 74 65 64 2e 20 7b 48 31 37 30 31 33 7d 20 53 51 ted. {H17013} SQ
36c10 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 Lite.** will unw
36c20 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e ind its stack an
36c30 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f d return an erro
36c40 72 2e 20 7b 48 31 37 30 31 34 7d 20 54 68 65 20 r. {H17014} The
36c50 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 argument.** to s
36c60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
36c70 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 oc() is one of t
36c80 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e hese integer con
36c90 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 stants:.**.** <u
36ca0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 l>.** <li> SQLI
36cb0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a TE_MUTEX_FAST.**
36cc0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
36cd0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
36ce0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
36cf0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
36d00 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 R.** <li> SQLIT
36d10 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
36d20 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 EM.** <li> SQLI
36d30 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
36d40 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 MEM2.** <li> SQ
36d50 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
36d60 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 C_PRNG.** <li>
36d70 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
36d80 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 TIC_LRU.** <li>
36d90 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
36da0 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 ATIC_LRU2.** </u
36db0 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 35 l>.**.** {H17015
36dc0 7d 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 } The first two
36dd0 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 constants cause
36de0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
36df0 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a loc() to create.
36e00 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 ** a new mutex.
36e10 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 The new mutex i
36e20 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e s recursive when
36e30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
36e40 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 CURSIVE.** is us
36e50 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 ed but not neces
36e60 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 sarily so when S
36e70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
36e80 20 69 73 20 75 73 65 64 2e 20 7b 45 4e 44 7d 0a is used. {END}.
36e90 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 ** The mutex imp
36ea0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
36eb0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
36ec0 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a e a distinction.
36ed0 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 ** between SQLIT
36ee0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
36ef0 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 E and SQLITE_MUT
36f00 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f EX_FAST if it do
36f10 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 es.** not want t
36f20 6f 2e 20 20 7b 48 31 37 30 31 36 7d 20 42 75 74 o. {H17016} But
36f30 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c SQLite will onl
36f40 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63 75 y request a recu
36f50 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a rsive mutex in.*
36f60 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69 74 * cases where it
36f70 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e really needs on
36f80 65 2e 20 20 7b 45 4e 44 7d 20 49 66 20 61 20 66 e. {END} If a f
36f90 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 aster non-recurs
36fa0 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 ive mutex.** imp
36fb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 lementation is a
36fc0 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 vailable on the
36fd0 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 host platform, t
36fe0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 he mutex subsyst
36ff0 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 em.** might retu
37000 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 rn such a mutex
37010 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 in response to S
37020 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
37030 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 37 7d ..**.** {H17017}
37040 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 The other allow
37050 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f ed parameters to
37060 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
37070 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 lloc() each retu
37080 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 rn.** a pointer
37090 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 to a static pree
370a0 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 7b xisting mutex. {
370b0 45 4e 44 7d 20 20 46 6f 75 72 20 73 74 61 74 69 END} Four stati
370c0 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a c mutexes are.**
370d0 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72 used by the cur
370e0 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 rent version of
370f0 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 SQLite. Future
37100 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
37110 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 te.** may add ad
37120 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 ditional static
37130 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 mutexes. Static
37140 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 mutexes are for
37150 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 internal.** use
37160 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e by SQLite only.
37170 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 Applications t
37180 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d hat use SQLite m
37190 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a utexes should.**
371a0 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 use only the dy
371b0 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 namic mutexes re
371c0 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 turned by SQLITE
371d0 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a _MUTEX_FAST or.*
371e0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 * SQLITE_MUTEX_R
371f0 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 ECURSIVE..**.**
37200 7b 48 31 37 30 31 38 7d 20 4e 6f 74 65 20 74 68 {H17018} Note th
37210 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 at if one of the
37220 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 dynamic mutex p
37230 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 arameters (SQLIT
37240 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 E_MUTEX_FAST.**
37250 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f or SQLITE_MUTEX_
37260 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 RECURSIVE) is us
37270 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f ed then sqlite3_
37280 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a mutex_alloc().**
37290 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 returns a diffe
372a0 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 rent mutex on ev
372b0 65 72 79 20 63 61 6c 6c 2e 20 20 7b 48 31 37 30 ery call. {H170
372c0 33 34 7d 20 42 75 74 20 66 6f 72 20 74 68 65 20 34} But for the
372d0 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 static.** mutex
372e0 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 types, the same
372f0 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 mutex is returne
37300 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 d on every call
37310 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 that has.** the
37320 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 same type number
37330 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 39 7d ..**.** {H17019}
37340 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
37350 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74 69 6e ex_free() routin
37360 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 e deallocates a
37370 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c previously.** al
37380 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69 63 20 located dynamic
37390 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 30 7d 20 mutex. {H17020}
373a0 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
373b0 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
373c0 65 76 65 72 79 0a 2a 2a 20 64 79 6e 61 6d 69 63 every.** dynamic
373d0 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 mutex that it a
373e0 6c 6c 6f 63 61 74 65 73 2e 20 7b 41 31 37 30 32 llocates. {A1702
373f0 31 7d 20 54 68 65 20 64 79 6e 61 6d 69 63 20 6d 1} The dynamic m
37400 75 74 65 78 65 73 20 6d 75 73 74 20 6e 6f 74 20 utexes must not
37410 62 65 20 69 6e 0a 2a 2a 20 75 73 65 20 77 68 65 be in.** use whe
37420 6e 20 74 68 65 79 20 61 72 65 20 64 65 61 6c 6c n they are deall
37430 6f 63 61 74 65 64 2e 20 7b 41 31 37 30 32 32 7d ocated. {A17022}
37440 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 64 Attempting to d
37450 65 61 6c 6c 6f 63 61 74 65 20 61 20 73 74 61 74 eallocate a stat
37460 69 63 0a 2a 2a 20 6d 75 74 65 78 20 72 65 73 75 ic.** mutex resu
37470 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 lts in undefined
37480 20 62 65 68 61 76 69 6f 72 2e 20 7b 48 31 37 30 behavior. {H170
37490 32 33 7d 20 53 51 4c 69 74 65 20 6e 65 76 65 72 23} SQLite never
374a0 20 64 65 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 deallocates.**
374b0 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 2e 20 a static mutex.
374c0 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 {END}.**.** The
374d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
374e0 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ter() and sqlite
374f0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
37500 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a utines attempt.*
37510 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 * to enter a mut
37520 65 78 2e 20 7b 48 31 37 30 32 34 7d 20 49 66 20 ex. {H17024} If
37530 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
37540 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e s already within
37550 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 the mutex,.** s
37560 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
37570 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 er() will block
37580 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
37590 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 x_try() will ret
375a0 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 urn.** SQLITE_BU
375b0 53 59 2e 20 7b 48 31 37 30 32 35 7d 20 20 54 68 SY. {H17025} Th
375c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
375d0 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 try() interface
375e0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
375f0 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 OK].** upon succ
37600 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 7b essful entry. {
37610 48 31 37 30 32 36 7d 20 4d 75 74 65 78 65 73 20 H17026} Mutexes
37620 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a created using.**
37630 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
37640 43 55 52 53 49 56 45 20 63 61 6e 20 62 65 20 65 CURSIVE can be e
37650 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 ntered multiple
37660 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d times by the sam
37670 65 20 74 68 72 65 61 64 2e 0a 2a 2a 20 7b 48 31 e thread..** {H1
37680 37 30 32 37 7d 20 49 6e 20 73 75 63 68 20 63 61 7027} In such ca
37690 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 ses the,.** mute
376a0 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 x must be exited
376b0 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 an equal number
376c0 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 of times before
376d0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a another thread.
376e0 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 7b ** can enter. {
376f0 41 31 37 30 32 38 7d 20 49 66 20 74 68 65 20 73 A17028} If the s
37700 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 ame thread tries
37710 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 to enter any ot
37720 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 6d her.** kind of m
37730 75 74 65 78 20 6d 6f 72 65 20 74 68 61 6e 20 6f utex more than o
37740 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f nce, the behavio
37750 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a r is undefined..
37760 2a 2a 20 7b 48 31 37 30 32 39 7d 20 53 51 4c 69 ** {H17029} SQLi
37770 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 65 78 te will never ex
37780 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 20 62 65 hibit.** such be
37790 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20 6f 77 havior in its ow
377a0 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78 65 73 n use of mutexes
377b0 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 ..**.** Some sys
377c0 74 65 6d 73 20 28 66 6f 72 20 65 78 61 6d 70 6c tems (for exampl
377d0 65 2c 20 57 69 6e 64 6f 77 73 20 39 35 29 20 64 e, Windows 95) d
377e0 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68 o not support th
377f0 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 69 e operation.** i
37800 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 73 71 mplemented by sq
37810 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
37820 29 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79 73 ). On those sys
37830 74 65 6d 73 2c 20 73 71 6c 69 74 65 33 5f 6d 75 tems, sqlite3_mu
37840 74 65 78 5f 74 72 79 28 29 0a 2a 2a 20 77 69 6c tex_try().** wil
37850 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 l always return
37860 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 7b 48 SQLITE_BUSY. {H
37870 31 37 30 33 30 7d 20 54 68 65 20 53 51 4c 69 74 17030} The SQLit
37880 65 20 63 6f 72 65 20 6f 6e 6c 79 20 65 76 65 72 e core only ever
37890 20 75 73 65 73 0a 2a 2a 20 73 71 6c 69 74 65 33 uses.** sqlite3
378a0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 61 73 20 _mutex_try() as
378b0 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 an optimization
378c0 73 6f 20 74 68 69 73 20 69 73 20 61 63 63 65 70 so this is accep
378d0 74 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a table behavior..
378e0 2a 2a 0a 2a 2a 20 7b 48 31 37 30 33 31 7d 20 54 **.** {H17031} T
378f0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
37900 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 _leave() routine
37910 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 exits a mutex t
37920 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 hat was.** previ
37930 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 ously entered by
37940 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
37950 2e 20 20 7b 41 31 37 30 33 32 7d 20 54 68 65 20 . {A17032} The
37960 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 behavior.** is u
37970 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
37980 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 mutex is not cur
37990 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 62 rently entered b
379a0 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 y the.** calling
379b0 20 74 68 72 65 61 64 20 6f 72 20 69 73 20 6e 6f thread or is no
379c0 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f t currently allo
379d0 63 61 74 65 64 2e 20 20 7b 48 31 37 30 33 33 7d cated. {H17033}
379e0 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 SQLite will.**
379f0 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e never do either.
37a00 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 {END}.**.** If
37a10 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 the argument to
37a20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
37a30 74 65 72 28 29 2c 20 73 71 6c 69 74 65 33 5f 6d ter(), sqlite3_m
37a40 75 74 65 78 5f 74 72 79 28 29 2c 20 6f 72 0a 2a utex_try(), or.*
37a50 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f * sqlite3_mutex_
37a60 6c 65 61 76 65 28 29 20 69 73 20 61 20 4e 55 4c leave() is a NUL
37a70 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 L pointer, then
37a80 61 6c 6c 20 74 68 72 65 65 20 72 6f 75 74 69 6e all three routin
37a90 65 73 0a 2a 2a 20 62 65 68 61 76 65 20 61 73 20 es.** behave as
37aa0 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 no-ops..**.** Se
37ab0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
37ac0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 61 _mutex_held()] a
37ad0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 nd [sqlite3_mute
37ae0 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 2e 0a 2a 2f x_notheld()]..*/
37af0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
37b00 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 te3_mutex *sqlit
37b10 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69 e3_mutex_alloc(i
37b20 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
37b30 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
37b40 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f ex_free(sqlite3_
37b50 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f mutex*);.SQLITE_
37b60 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
37b70 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c _mutex_enter(sql
37b80 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 ite3_mutex*);.SQ
37b90 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
37ba0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 73 ite3_mutex_try(s
37bb0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a qlite3_mutex*);.
37bc0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
37bd0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
37be0 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ave(sqlite3_mute
37bf0 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 x*);../*.** CAPI
37c00 33 52 45 46 3a 20 4d 75 74 65 78 20 4d 65 74 68 3REF: Mutex Meth
37c10 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31 37 31 ods Object {H171
37c20 32 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 20 20} <S20130>.**
37c30 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
37c40 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
37c50 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
37c60 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c 6f 77 defines the low
37c70 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 0a -level routines.
37c80 2a 2a 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 ** used to alloc
37c90 61 74 65 20 61 6e 64 20 75 73 65 20 6d 75 74 65 ate and use mute
37ca0 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55 73 75 61 6c xes..**.** Usual
37cb0 6c 79 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20 ly, the default
37cc0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 mutex implementa
37cd0 74 69 6f 6e 73 20 70 72 6f 76 69 64 65 64 20 62 tions provided b
37ce0 79 20 53 51 4c 69 74 65 20 61 72 65 0a 2a 2a 20 y SQLite are.**
37cf0 73 75 66 66 69 63 69 65 6e 74 2c 20 68 6f 77 65 sufficient, howe
37d00 76 65 72 20 74 68 65 20 75 73 65 72 20 68 61 73 ver the user has
37d10 20 74 68 65 20 6f 70 74 69 6f 6e 20 6f 66 20 73 the option of s
37d20 75 62 73 74 69 74 75 74 69 6e 67 20 61 20 63 75 ubstituting a cu
37d30 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e stom.** implemen
37d40 74 61 74 69 6f 6e 20 66 6f 72 20 73 70 65 63 69 tation for speci
37d50 61 6c 69 7a 65 64 20 64 65 70 6c 6f 79 6d 65 6e alized deploymen
37d60 74 73 20 6f 72 20 73 79 73 74 65 6d 73 20 66 6f ts or systems fo
37d70 72 20 77 68 69 63 68 20 53 51 4c 69 74 65 0a 2a r which SQLite.*
37d80 2a 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 * does not provi
37d90 64 65 20 61 20 73 75 69 74 61 62 6c 65 20 69 6d de a suitable im
37da0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 6e plementation. In
37db0 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 this case, the
37dc0 75 73 65 72 0a 2a 2a 20 63 72 65 61 74 65 73 20 user.** creates
37dd0 61 6e 64 20 70 6f 70 75 6c 61 74 65 73 20 61 6e and populates an
37de0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
37df0 73 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 s structure to p
37e00 61 73 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 ass.** to sqlite
37e10 33 5f 63 6f 6e 66 69 67 28 29 20 61 6c 6f 6e 67 3_config() along
37e20 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c 49 54 with the [SQLIT
37e30 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 E_CONFIG_MUTEX]
37e40 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41 64 64 69 74 option..** Addit
37e50 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20 69 6e 73 74 ionally, an inst
37e60 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
37e70 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 ucture can be us
37e80 65 64 20 61 73 20 61 6e 0a 2a 2a 20 6f 75 74 70 ed as an.** outp
37e90 75 74 20 76 61 72 69 61 62 6c 65 20 77 68 65 6e ut variable when
37ea0 20 71 75 65 72 79 69 6e 67 20 74 68 65 20 73 79 querying the sy
37eb0 73 74 65 6d 20 66 6f 72 20 74 68 65 20 63 75 72 stem for the cur
37ec0 72 65 6e 74 20 6d 75 74 65 78 0a 2a 2a 20 69 6d rent mutex.** im
37ed0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 75 73 plementation, us
37ee0 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54 45 5f ing the [SQLITE_
37ef0 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 5d CONFIG_GETMUTEX]
37f00 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 option..**.** T
37f10 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 6d 65 he xMutexInit me
37f20 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62 79 20 thod defined by
37f30 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 this structure i
37f40 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a 2a 20 s invoked as.**
37f50 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d 20 69 part of system i
37f60 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 62 79 nitialization by
37f70 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 the sqlite3_ini
37f80 74 69 61 6c 69 7a 65 28 29 20 66 75 6e 63 74 69 tialize() functi
37f90 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 30 30 31 7d 20 on..** {H17001}
37fa0 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 72 The xMutexInit r
37fb0 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 62 65 20 outine shall be
37fc0 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 called by SQLite
37fd0 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 0a 2a once for each.*
37fe0 2a 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c * effective call
37ff0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 to [sqlite3_ini
38000 74 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a tialize()]..**.*
38010 2a 20 54 68 65 20 78 4d 75 74 65 78 45 6e 64 20 * The xMutexEnd
38020 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62 method defined b
38030 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
38040 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a is invoked as.*
38050 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d * part of system
38060 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74 68 65 shutdown by the
38070 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
38080 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 n() function. Th
38090 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
380a0 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 74 68 ion of this meth
380b0 6f 64 20 69 73 20 65 78 70 65 63 74 65 64 20 74 od is expected t
380c0 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6f 75 o release all ou
380d0 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72 65 73 tstanding.** res
380e0 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65 64 20 ources obtained
380f0 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74 by the mutex met
38100 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 hods implementat
38110 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 0a ion, especially.
38120 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69 6e 65 ** those obtaine
38130 64 20 62 79 20 74 68 65 20 78 4d 75 74 65 78 49 d by the xMutexI
38140 6e 69 74 20 6d 65 74 68 6f 64 2e 20 7b 48 31 37 nit method. {H17
38150 30 30 33 7d 20 54 68 65 20 78 4d 75 74 65 78 45 003} The xMutexE
38160 6e 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 nd().** interfac
38170 65 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 6f 6b e shall be invok
38180 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ed once for each
38190 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
381a0 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a 2a 3_shutdown()]..*
381b0 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 *.** The remaini
381c0 6e 67 20 73 65 76 65 6e 20 6d 65 74 68 6f 64 73 ng seven methods
381d0 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 69 73 defined by this
381e0 20 73 74 72 75 63 74 75 72 65 20 28 78 4d 75 74 structure (xMut
381f0 65 78 41 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d 75 74 exAlloc,.** xMut
38200 65 78 46 72 65 65 2c 20 78 4d 75 74 65 78 45 6e exFree, xMutexEn
38210 74 65 72 2c 20 78 4d 75 74 65 78 54 72 79 2c 20 ter, xMutexTry,
38220 78 4d 75 74 65 78 4c 65 61 76 65 2c 20 78 4d 75 xMutexLeave, xMu
38230 74 65 78 48 65 6c 64 20 61 6e 64 0a 2a 2a 20 78 texHeld and.** x
38240 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 20 69 6d MutexNotheld) im
38250 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c plement the foll
38260 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73 owing interfaces
38270 20 28 72 65 73 70 65 63 74 69 76 65 6c 79 29 3a (respectively):
38280 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 .**.** <ul>.**
38290 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
382a0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 3c mutex_alloc()] <
382b0 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 /li>.** <li>
382c0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 [sqlite3_mutex_f
382d0 72 65 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 ree()] </li>.**
382e0 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 <li> [sqlite3
382f0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 5d 20 _mutex_enter()]
38300 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 </li>.** <li>
38310 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
38320 74 72 79 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 try()] </li>.**
38330 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 <li> [sqlite3
38340 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 5d 20 _mutex_leave()]
38350 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 </li>.** <li>
38360 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
38370 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a held()] </li>.**
38380 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 <li> [sqlite
38390 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
383a0 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c )] </li>.** </ul
383b0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 >.**.** The only
383c0 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 74 difference is t
383d0 68 61 74 20 74 68 65 20 70 75 62 6c 69 63 20 73 hat the public s
383e0 71 6c 69 74 65 33 5f 58 58 58 20 66 75 6e 63 74 qlite3_XXX funct
383f0 69 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65 64 0a ions enumerated.
38400 2a 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e 74 6c ** above silentl
38410 79 20 69 67 6e 6f 72 65 20 61 6e 79 20 69 6e 76 y ignore any inv
38420 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 70 61 ocations that pa
38430 73 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ss a NULL pointe
38440 72 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 r instead.** of
38450 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20 68 61 a valid mutex ha
38460 6e 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c 65 6d ndle. The implem
38470 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
38480 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 methods defined
38490 0a 2a 2a 20 62 79 20 74 68 69 73 20 73 74 72 75 .** by this stru
384a0 63 74 75 72 65 20 61 72 65 20 6e 6f 74 20 72 65 cture are not re
384b0 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 quired to handle
384c0 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 this case, the
384d0 72 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20 70 61 results.** of pa
384e0 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 ssing a NULL poi
384f0 6e 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 nter instead of
38500 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20 68 61 a valid mutex ha
38510 6e 64 6c 65 20 61 72 65 20 75 6e 64 65 66 69 6e ndle are undefin
38520 65 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74 20 69 ed.** (i.e. it i
38530 73 20 61 63 63 65 70 74 61 62 6c 65 20 74 6f 20 s acceptable to
38540 70 72 6f 76 69 64 65 20 61 6e 20 69 6d 70 6c 65 provide an imple
38550 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 73 mentation that s
38560 65 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a 20 69 egfaults if.** i
38570 74 20 69 73 20 70 61 73 73 65 64 20 61 20 4e 55 t is passed a NU
38580 4c 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a 2f 0a LL pointer)..*/.
38590 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
385a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
385b0 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 75 74 hods sqlite3_mut
385c0 65 78 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 ex_methods;.stru
385d0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ct sqlite3_mutex
385e0 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 _methods {. int
385f0 20 28 2a 78 4d 75 74 65 78 49 6e 69 74 29 28 76 (*xMutexInit)(v
38600 6f 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d oid);. int (*xM
38610 75 74 65 78 45 6e 64 29 28 76 6f 69 64 29 3b 0a utexEnd)(void);.
38620 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
38630 2a 28 2a 78 4d 75 74 65 78 41 6c 6c 6f 63 29 28 *(*xMutexAlloc)(
38640 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 int);. void (*x
38650 4d 75 74 65 78 46 72 65 65 29 28 73 71 6c 69 74 MutexFree)(sqlit
38660 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 e3_mutex *);. v
38670 6f 69 64 20 28 2a 78 4d 75 74 65 78 45 6e 74 65 oid (*xMutexEnte
38680 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 r)(sqlite3_mutex
38690 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 *);. int (*xMu
386a0 74 65 78 54 72 79 29 28 73 71 6c 69 74 65 33 5f texTry)(sqlite3_
386b0 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 mutex *);. void
386c0 20 28 2a 78 4d 75 74 65 78 4c 65 61 76 65 29 28 (*xMutexLeave)(
386d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 sqlite3_mutex *)
386e0 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 ;. int (*xMutex
386f0 48 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 Held)(sqlite3_mu
38700 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a tex *);. int (*
38710 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 28 73 xMutexNotheld)(s
38720 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b qlite3_mutex *);
38730 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .};../*.** CAPI3
38740 52 45 46 3a 20 4d 75 74 65 78 20 56 65 72 69 66 REF: Mutex Verif
38750 69 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 ication Routines
38760 20 7b 48 31 37 30 38 30 7d 20 3c 53 32 30 31 33 {H17080} <S2013
38770 30 3e 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 0> <S30800>.**.*
38780 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
38790 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 tex_held() and s
387a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
387b0 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 73 0a held() routines.
387c0 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 ** are intended
387d0 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 for use inside a
387e0 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
387f0 74 73 2e 20 7b 48 31 37 30 38 31 7d 20 54 68 65 ts. {H17081} The
38800 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a 2a 20 SQLite core.**
38810 6e 65 76 65 72 20 75 73 65 73 20 74 68 65 73 65 never uses these
38820 20 72 6f 75 74 69 6e 65 73 20 65 78 63 65 70 74 routines except
38830 20 69 6e 73 69 64 65 20 61 6e 20 61 73 73 65 72 inside an asser
38840 74 28 29 20 61 6e 64 20 61 70 70 6c 69 63 61 74 t() and applicat
38850 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 64 76 69 ions.** are advi
38860 73 65 64 20 74 6f 20 66 6f 6c 6c 6f 77 20 74 68 sed to follow th
38870 65 20 6c 65 61 64 20 6f 66 20 74 68 65 20 63 6f e lead of the co
38880 72 65 2e 20 20 7b 48 31 37 30 38 32 7d 20 54 68 re. {H17082} Th
38890 65 20 63 6f 72 65 20 6f 6e 6c 79 0a 2a 2a 20 70 e core only.** p
388a0 72 6f 76 69 64 65 73 20 69 6d 70 6c 65 6d 65 6e rovides implemen
388b0 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 73 tations for thes
388c0 65 20 72 6f 75 74 69 6e 65 73 20 77 68 65 6e 20 e routines when
388d0 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 0a 2a it is compiled.*
388e0 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 * with the SQLIT
388f0 45 5f 44 45 42 55 47 20 66 6c 61 67 2e 20 20 7b E_DEBUG flag. {
38900 41 31 37 30 38 37 7d 20 45 78 74 65 72 6e 61 6c A17087} External
38910 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 mutex implement
38920 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e ations.** are on
38930 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 ly required to p
38940 72 6f 76 69 64 65 20 74 68 65 73 65 20 72 6f 75 rovide these rou
38950 74 69 6e 65 73 20 69 66 20 53 51 4c 49 54 45 5f tines if SQLITE_
38960 44 45 42 55 47 20 69 73 0a 2a 2a 20 64 65 66 69 DEBUG is.** defi
38970 6e 65 64 20 61 6e 64 20 69 66 20 4e 44 45 42 55 ned and if NDEBU
38980 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 G is not defined
38990 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 33 7d ..**.** {H17083}
389a0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
389b0 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 72 should return tr
389c0 75 65 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 ue if the mutex
389d0 69 6e 20 74 68 65 69 72 20 61 72 67 75 6d 65 6e in their argumen
389e0 74 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 72 20 t.** is held or
389f0 6e 6f 74 20 68 65 6c 64 2c 20 72 65 73 70 65 63 not held, respec
38a00 74 69 76 65 6c 79 2c 20 62 79 20 74 68 65 20 63 tively, by the c
38a10 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 2e 0a 2a alling thread..*
38a20 2a 0a 2a 2a 20 7b 58 31 37 30 38 34 7d 20 54 68 *.** {X17084} Th
38a30 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
38a40 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
38a50 20 74 6f 20 70 72 6f 76 69 64 65 64 20 76 65 72 to provided ver
38a60 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 0a 2a sions of these.*
38a70 2a 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 * routines that
38a80 61 63 74 75 61 6c 6c 79 20 77 6f 72 6b 2e 20 49 actually work. I
38a90 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 f the implementa
38aa0 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 70 72 tion does not pr
38ab0 6f 76 69 64 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a ovide working.**
38ac0 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 versions of the
38ad0 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 69 74 20 se routines, it
38ae0 73 68 6f 75 6c 64 20 61 74 20 6c 65 61 73 74 20 should at least
38af0 70 72 6f 76 69 64 65 20 73 74 75 62 73 20 74 68 provide stubs th
38b00 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 at always.** ret
38b10 75 72 6e 20 74 72 75 65 20 73 6f 20 74 68 61 74 urn true so that
38b20 20 6f 6e 65 20 64 6f 65 73 20 6e 6f 74 20 67 65 one does not ge
38b30 74 20 73 70 75 72 69 6f 75 73 20 61 73 73 65 72 t spurious asser
38b40 74 69 6f 6e 20 66 61 69 6c 75 72 65 73 2e 0a 2a tion failures..*
38b50 2a 0a 2a 2a 20 7b 48 31 37 30 38 35 7d 20 49 66 *.** {H17085} If
38b60 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f the argument to
38b70 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
38b80 65 6c 64 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 eld() is a NULL
38b90 70 6f 69 6e 74 65 72 20 74 68 65 6e 0a 2a 2a 20 pointer then.**
38ba0 74 68 65 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 the routine shou
38bb0 6c 64 20 72 65 74 75 72 6e 20 31 2e 20 20 7b 45 ld return 1. {E
38bc0 4e 44 7d 20 54 68 69 73 20 73 65 65 6d 73 20 63 ND} This seems c
38bd0 6f 75 6e 74 65 72 2d 69 6e 74 75 69 74 69 76 65 ounter-intuitive
38be0 20 73 69 6e 63 65 0a 2a 2a 20 63 6c 65 61 72 6c since.** clearl
38bf0 79 20 74 68 65 20 6d 75 74 65 78 20 63 61 6e 6e y the mutex cann
38c00 6f 74 20 62 65 20 68 65 6c 64 20 69 66 20 69 74 ot be held if it
38c10 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e does not exist.
38c20 20 20 42 75 74 20 74 68 65 0a 2a 2a 20 74 68 65 But the.** the
38c30 20 72 65 61 73 6f 6e 20 74 68 65 20 6d 75 74 65 reason the mute
38c40 78 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 x does not exist
38c50 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65 20 is because the
38c60 62 75 69 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a 20 build is not.**
38c70 75 73 69 6e 67 20 6d 75 74 65 78 65 73 2e 20 20 using mutexes.
38c80 41 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 And we do not wa
38c90 6e 74 20 74 68 65 20 61 73 73 65 72 74 28 29 20 nt the assert()
38ca0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a containing the.*
38cb0 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 * call to sqlite
38cc0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 74 3_mutex_held() t
38cd0 6f 20 66 61 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e o fail, so a non
38ce0 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 69 73 0a -zero return is.
38cf0 2a 2a 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 ** the appropria
38d00 74 65 20 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 te thing to do.
38d10 20 7b 48 31 37 30 38 36 7d 20 54 68 65 20 73 71 {H17086} The sq
38d20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
38d30 65 6c 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 eld().** interfa
38d40 63 65 20 73 68 6f 75 6c 64 20 61 6c 73 6f 20 72 ce should also r
38d50 65 74 75 72 6e 20 31 20 77 68 65 6e 20 67 69 76 eturn 1 when giv
38d60 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 en a NULL pointe
38d70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r..*/.SQLITE_API
38d80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 int sqlite3_mut
38d90 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 5f ex_held(sqlite3_
38da0 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f mutex*);.SQLITE_
38db0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
38dc0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 mutex_notheld(sq
38dd0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a lite3_mutex*);..
38de0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
38df0 4d 75 74 65 78 20 54 79 70 65 73 20 7b 48 31 37 Mutex Types {H17
38e00 30 30 31 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 001} <H17000>.**
38e10 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
38e20 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 _mutex_alloc()]
38e30 69 6e 74 65 72 66 61 63 65 20 74 61 6b 65 73 20 interface takes
38e40 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e a single argumen
38e50 74 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 6f 6e t.** which is on
38e60 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
38e70 65 72 20 63 6f 6e 73 74 61 6e 74 73 2e 0a 2a 2a er constants..**
38e80 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 73 .** The set of s
38e90 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 6d 61 tatic mutexes ma
38ea0 79 20 63 68 61 6e 67 65 20 66 72 6f 6d 20 6f 6e y change from on
38eb0 65 20 53 51 4c 69 74 65 20 72 65 6c 65 61 73 65 e SQLite release
38ec0 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74 2e to the.** next.
38ed0 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 Applications t
38ee0 68 61 74 20 6f 76 65 72 72 69 64 65 20 74 68 65 hat override the
38ef0 20 62 75 69 6c 74 2d 69 6e 20 6d 75 74 65 78 20 built-in mutex
38f00 6c 6f 67 69 63 20 6d 75 73 74 20 62 65 0a 2a 2a logic must be.**
38f10 20 70 72 65 70 61 72 65 64 20 74 6f 20 61 63 63 prepared to acc
38f20 6f 6d 6d 6f 64 61 74 65 20 61 64 64 69 74 69 6f ommodate additio
38f30 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 nal static mutex
38f40 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 es..*/.#define S
38f50 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
38f60 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 0.#
38f70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 define SQLITE_MU
38f80 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 20 20 TEX_RECURSIVE
38f90 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 1.#define S
38fa0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
38fb0 49 43 5f 4d 41 53 54 45 52 20 20 20 20 32 0a 23 IC_MASTER 2.#
38fc0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 define SQLITE_MU
38fd0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 TEX_STATIC_MEM
38fe0 20 20 20 20 20 33 20 20 2f 2a 20 73 71 6c 69 74 3 /* sqlit
38ff0 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 e3_malloc() */.#
39000 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 define SQLITE_MU
39010 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 20 TEX_STATIC_MEM2
39020 20 20 20 20 20 34 20 20 2f 2a 20 4e 4f 54 20 55 4 /* NOT U
39030 53 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 SED */.#define S
39040 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
39050 49 43 5f 4f 50 45 4e 20 20 20 20 20 20 34 20 20 IC_OPEN 4
39060 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f /* sqlite3BtreeO
39070 70 65 6e 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 pen() */.#define
39080 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
39090 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 35 ATIC_PRNG 5
390a0 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72 61 6e /* sqlite3_ran
390b0 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 dom() */.#define
390c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
390d0 41 54 49 43 5f 4c 52 55 20 20 20 20 20 20 20 36 ATIC_LRU 6
390e0 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 /* lru page li
390f0 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 st */.#define SQ
39100 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
39110 43 5f 4c 52 55 32 20 20 20 20 20 20 37 20 20 2f C_LRU2 7 /
39120 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 * lru page list
39130 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 */../*.** CAPI3R
39140 45 46 3a 20 52 65 74 72 69 65 76 65 20 74 68 65 EF: Retrieve the
39150 20 6d 75 74 65 78 20 66 6f 72 20 61 20 64 61 74 mutex for a dat
39160 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
39170 20 7b 48 31 37 30 30 32 7d 20 3c 48 31 37 30 30 {H17002} <H1700
39180 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 0>.**.** This in
39190 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
391a0 61 20 70 6f 69 6e 74 65 72 20 74 68 65 20 5b 73 a pointer the [s
391b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 6f 62 qlite3_mutex] ob
391c0 6a 65 63 74 20 74 68 61 74 20 0a 2a 2a 20 73 65 ject that .** se
391d0 72 69 61 6c 69 7a 65 73 20 61 63 63 65 73 73 20 rializes access
391e0 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 to the [database
391f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 67 69 76 connection] giv
39200 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 en in the argume
39210 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 5b nt.** when the [
39220 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 threading mode]
39230 69 73 20 53 65 72 69 61 6c 69 7a 65 64 2e 0a 2a is Serialized..*
39240 2a 20 49 66 20 74 68 65 20 5b 74 68 72 65 61 64 * If the [thread
39250 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 69 6e ing mode] is Sin
39260 67 6c 65 2d 74 68 72 65 61 64 20 6f 72 20 4d 75 gle-thread or Mu
39270 6c 74 69 2d 74 68 72 65 61 64 20 74 68 65 6e 20 lti-thread then
39280 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
39290 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 returns a NULL p
392a0 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 ointer..*/.SQLIT
392b0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 E_API sqlite3_mu
392c0 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f tex *sqlite3_db_
392d0 6d 75 74 65 78 28 73 71 6c 69 74 65 33 2a 29 3b mutex(sqlite3*);
392e0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
392f0 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43 6f 6e 74 : Low-Level Cont
39300 72 6f 6c 20 4f 66 20 44 61 74 61 62 61 73 65 20 rol Of Database
39310 46 69 6c 65 73 20 7b 48 31 31 33 30 30 7d 20 3c Files {H11300} <
39320 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 S30800>.**.** {H
39330 31 31 33 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 11301} The [sqli
39340 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c te3_file_control
39350 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 6d 61 ()] interface ma
39360 6b 65 73 20 61 20 64 69 72 65 63 74 20 63 61 6c kes a direct cal
39370 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c l to the.** xFil
39380 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 eControl method
39390 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 for the [sqlite3
393a0 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a _io_methods] obj
393b0 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 0a 2a ect associated.*
393c0 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 * with a particu
393d0 6c 61 72 20 64 61 74 61 62 61 73 65 20 69 64 65 lar database ide
393e0 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 ntified by the s
393f0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 econd argument.
39400 7b 48 31 31 33 30 32 7d 20 54 68 65 0a 2a 2a 20 {H11302} The.**
39410 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
39420 62 61 73 65 20 69 73 20 74 68 65 20 6e 61 6d 65 base is the name
39430 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 65 assigned to the
39440 20 64 61 74 61 62 61 73 65 20 62 79 20 74 68 65 database by the
39450 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 6c 61 6e .** <a href="lan
39460 67 5f 61 74 74 61 63 68 2e 68 74 6d 6c 22 3e 41 g_attach.html">A
39470 54 54 41 43 48 3c 2f 61 3e 20 53 51 4c 20 63 6f TTACH</a> SQL co
39480 6d 6d 61 6e 64 20 74 68 61 74 20 6f 70 65 6e 65 mmand that opene
39490 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 d the.** databas
394a0 65 2e 20 7b 48 31 31 33 30 33 7d 20 54 6f 20 63 e. {H11303} To c
394b0 6f 6e 74 72 6f 6c 20 74 68 65 20 6d 61 69 6e 20 ontrol the main
394c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 75 database file, u
394d0 73 65 20 74 68 65 20 6e 61 6d 65 20 22 6d 61 69 se the name "mai
394e0 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e 55 4c 4c 20 n".** or a NULL
394f0 70 6f 69 6e 74 65 72 2e 20 7b 48 31 31 33 30 34 pointer. {H11304
39500 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 } The third and
39510 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
39520 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e s to this routin
39530 65 0a 2a 2a 20 61 72 65 20 70 61 73 73 65 64 20 e.** are passed
39540 64 69 72 65 63 74 6c 79 20 74 68 72 6f 75 67 68 directly through
39550 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 61 to the second a
39560 6e 64 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 nd third paramet
39570 65 72 73 20 6f 66 0a 2a 2a 20 74 68 65 20 78 46 ers of.** the xF
39580 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f ileControl metho
39590 64 2e 20 20 7b 48 31 31 33 30 35 7d 20 54 68 65 d. {H11305} The
395a0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
395b0 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f the xFileContro
395c0 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 63 6f l.** method beco
395d0 6d 65 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 mes the return v
395e0 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 alue of this rou
395f0 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 tine..**.** {H11
39600 33 30 36 7d 20 49 66 20 74 68 65 20 73 65 63 6f 306} If the seco
39610 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 7a 44 nd parameter (zD
39620 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e 6f 74 20 bName) does not
39630 6d 61 74 63 68 20 74 68 65 20 6e 61 6d 65 20 6f match the name o
39640 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e 20 64 61 f any.** open da
39650 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
39660 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 n SQLITE_ERROR i
39670 73 20 72 65 74 75 72 6e 65 64 2e 20 7b 48 31 31 s returned. {H11
39680 33 30 37 7d 20 54 68 69 73 20 65 72 72 6f 72 0a 307} This error.
39690 2a 2a 20 63 6f 64 65 20 69 73 20 6e 6f 74 20 72 ** code is not r
396a0 65 6d 65 6d 62 65 72 65 64 20 61 6e 64 20 77 69 emembered and wi
396b0 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63 61 6c 6c ll not be recall
396c0 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 65 ed by [sqlite3_e
396d0 72 72 63 6f 64 65 28 29 5d 0a 2a 2a 20 6f 72 20 rrcode()].** or
396e0 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 [sqlite3_errmsg(
396f0 29 5d 2e 20 7b 41 31 31 33 30 38 7d 20 54 68 65 )]. {A11308} The
39700 20 75 6e 64 65 72 6c 79 69 6e 67 20 78 46 69 6c underlying xFil
39710 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 eControl method
39720 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 72 65 might.** also re
39730 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
39740 52 2e 20 20 7b 41 31 31 33 30 39 7d 20 54 68 65 R. {A11309} The
39750 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 re is no way to
39760 64 69 73 74 69 6e 67 75 69 73 68 20 62 65 74 77 distinguish betw
39770 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 een.** an incorr
39780 65 63 74 20 7a 44 62 4e 61 6d 65 20 61 6e 64 20 ect zDbName and
39790 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 an SQLITE_ERROR
397a0 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 65 20 return from the
397b0 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 78 46 underlying.** xF
397c0 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f ileControl metho
397d0 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 d. {END}.**.** S
397e0 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 ee also: [SQLITE
397f0 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 _FCNTL_LOCKSTATE
39800 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
39810 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 int sqlite3_file
39820 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 _control(sqlite3
39830 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
39840 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 DbName, int op,
39850 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 void*);../*.** C
39860 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 API3REF: Testing
39870 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 31 34 Interface {H114
39880 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 00} <S30800>.**.
39890 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 ** The sqlite3_t
398a0 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e est_control() in
398b0 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 terface is used
398c0 74 6f 20 72 65 61 64 20 6f 75 74 20 69 6e 74 65 to read out inte
398d0 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 65 20 6f 66 rnal.** state of
398e0 20 53 51 4c 69 74 65 20 61 6e 64 20 74 6f 20 69 SQLite and to i
398f0 6e 6a 65 63 74 20 66 61 75 6c 74 73 20 69 6e 74 nject faults int
39900 6f 20 53 51 4c 69 74 65 20 66 6f 72 20 74 65 73 o SQLite for tes
39910 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 ting.** purposes
39920 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 . The first par
39930 61 6d 65 74 65 72 20 69 73 20 61 6e 20 6f 70 65 ameter is an ope
39940 72 61 74 69 6f 6e 20 63 6f 64 65 20 74 68 61 74 ration code that
39950 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 74 determines.** t
39960 68 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61 6e 69 he number, meani
39970 6e 67 2c 20 61 6e 64 20 6f 70 65 72 61 74 69 6f ng, and operatio
39980 6e 20 6f 66 20 61 6c 6c 20 73 75 62 73 65 71 75 n of all subsequ
39990 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 2e 0a ent parameters..
399a0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
399b0 66 61 63 65 20 69 73 20 6e 6f 74 20 66 6f 72 20 face is not for
399c0 75 73 65 20 62 79 20 61 70 70 6c 69 63 61 74 69 use by applicati
399d0 6f 6e 73 2e 20 20 49 74 20 65 78 69 73 74 73 20 ons. It exists
399e0 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 20 76 65 solely.** for ve
399f0 72 69 66 79 69 6e 67 20 74 68 65 20 63 6f 72 72 rifying the corr
39a00 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 ect operation of
39a10 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
39a20 61 72 79 2e 20 20 44 65 70 65 6e 64 69 6e 67 0a ary. Depending.
39a30 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65 20 53 51 ** on how the SQ
39a40 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69 73 20 Lite library is
39a50 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69 73 20 69 compiled, this i
39a60 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 6e nterface might n
39a70 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 ot exist..**.**
39a80 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 The details of t
39a90 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 he operation cod
39aa0 65 73 2c 20 74 68 65 69 72 20 6d 65 61 6e 69 6e es, their meanin
39ab0 67 73 2c 20 74 68 65 20 70 61 72 61 6d 65 74 65 gs, the paramete
39ac0 72 73 0a 2a 2a 20 74 68 65 79 20 74 61 6b 65 2c rs.** they take,
39ad0 20 61 6e 64 20 77 68 61 74 20 74 68 65 79 20 64 and what they d
39ae0 6f 20 61 72 65 20 61 6c 6c 20 73 75 62 6a 65 63 o are all subjec
39af0 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 t to change with
39b00 6f 75 74 20 6e 6f 74 69 63 65 2e 0a 2a 2a 20 55 out notice..** U
39b10 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 20 74 68 nlike most of th
39b20 65 20 53 51 4c 69 74 65 20 41 50 49 2c 20 74 68 e SQLite API, th
39b30 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e is function is n
39b40 6f 74 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f ot guaranteed to
39b50 0a 2a 2a 20 6f 70 65 72 61 74 65 20 63 6f 6e 73 .** operate cons
39b60 69 73 74 65 6e 74 6c 79 20 66 72 6f 6d 20 6f 6e istently from on
39b70 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65 e release to the
39b80 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 next..*/.SQLITE
39b90 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
39ba0 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e _test_control(in
39bb0 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a t op, ...);../*.
39bc0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 73 ** CAPI3REF: Tes
39bd0 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 4f ting Interface O
39be0 70 65 72 61 74 69 6f 6e 20 43 6f 64 65 73 20 7b peration Codes {
39bf0 48 31 31 34 31 30 7d 20 3c 48 31 31 34 30 30 3e H11410} <H11400>
39c00 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e .**.** These con
39c10 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 76 stants are the v
39c20 61 6c 69 64 20 6f 70 65 72 61 74 69 6f 6e 20 63 alid operation c
39c30 6f 64 65 20 70 61 72 61 6d 65 74 65 72 73 20 75 ode parameters u
39c40 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 sed.** as the fi
39c50 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
39c60 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f [sqlite3_test_co
39c70 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 ntrol()]..**.**
39c80 54 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73 These parameters
39c90 20 61 6e 64 20 74 68 65 69 72 20 6d 65 61 6e 69 and their meani
39ca0 6e 67 73 20 61 72 65 20 73 75 62 6a 65 63 74 20 ngs are subject
39cb0 74 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 77 69 74 to change.** wit
39cc0 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20 20 54 68 hout notice. Th
39cd0 65 73 65 20 76 61 6c 75 65 73 20 61 72 65 20 66 ese values are f
39ce0 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f or testing purpo
39cf0 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70 ses only..** App
39d00 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 lications should
39d10 20 6e 6f 74 20 75 73 65 20 61 6e 79 20 6f 66 20 not use any of
39d20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73 these parameters
39d30 20 6f 72 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 or the.** [sqli
39d40 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c te3_test_control
39d50 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a ()] interface..*
39d60 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
39d70 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53 _TESTCTRL_PRNG_S
39d80 41 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20 AVE
39d90 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 5.#define SQL
39da0 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e ITE_TESTCTRL_PRN
39db0 47 5f 52 45 53 54 4f 52 45 20 20 20 20 20 20 20 G_RESTORE
39dc0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
39dd0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
39de0 50 52 4e 47 5f 52 45 53 45 54 20 20 20 20 20 20 PRNG_RESET
39df0 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 7.#defi
39e00 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
39e10 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 20 20 RL_BITVEC_TEST
39e20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 8.#d
39e30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
39e40 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 4e 53 54 TCTRL_FAULT_INST
39e50 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 39 ALL 9
39e60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
39e70 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f TESTCTRL_BENIGN_
39e80 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20 20 MALLOC_HOOKS
39e90 20 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 10.#define SQLI
39ea0 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 TE_TESTCTRL_PEND
39eb0 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 ING_BYTE
39ec0 20 20 20 20 31 31 0a 0a 2f 2a 0a 2a 2a 20 43 41 11../*.** CA
39ed0 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20 52 PI3REF: SQLite R
39ee0 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b 48 untime Status {H
39ef0 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 17200} <S60200>.
39f00 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
39f10 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
39f20 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 face is used to
39f30 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 retrieve runtime
39f40 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 status informat
39f50 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 ion.** about the
39f60 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66 20 preformance of
39f70 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74 69 SQLite, and opti
39f80 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74 20 onally to reset
39f90 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68 77 various.** highw
39fa0 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68 65 ater marks. The
39fb0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
39fc0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f is an integer co
39fd0 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 de for.** the sp
39fe0 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65 72 ecific parameter
39ff0 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52 65 to measure. Re
3a000 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65 72 cognized integer
3a010 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66 codes.** are of
3a020 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49 54 the form [SQLIT
3a030 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
3a040 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53 54 USED | SQLITE_ST
3a050 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54 68 ATUS_...]..** Th
3a060 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
3a070 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 of the parameter
3a080 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 74 is returned int
3a090 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 20 o *pCurrent..**
3a0a0 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f The highest reco
3a0b0 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72 65 rded value is re
3a0c0 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67 68 turned in *pHigh
3a0d0 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a 2a water. If the.*
3a0e0 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 74 * resetFlag is t
3a0f0 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 rue, then the hi
3a100 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61 6c ghest record val
3a110 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74 65 ue is reset afte
3a120 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 r.** *pHighwater
3a130 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f 6d is written. Som
3a140 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f 20 e parameters do
3a150 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20 68 not record the h
3a160 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e ighest.** value.
3a170 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72 61 For those para
3a180 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e meters.** nothin
3a190 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 g is written int
3a1a0 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 6e o *pHighwater an
3a1b0 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67 20 d the resetFlag
3a1c0 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 4f is ignored..** O
3a1d0 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 ther parameters
3a1e0 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 20 record only the
3a1f0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61 highwater mark a
3a200 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 65 nd not the curre
3a210 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f nt.** value. Fo
3a220 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 70 r these latter p
3a230 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 6e arameters nothin
3a240 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 g is written int
3a250 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 0a o *pCurrent..**.
3a260 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
3a270 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
3a280 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 K on success and
3a290 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 5b a non-zero.** [
3a2a0 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 error code] on f
3a2b0 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ailure..**.** Th
3a2c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
3a2d0 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73 20 readsafe but is
3a2e0 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68 69 not atomic. Thi
3a2f0 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 0a 2a 2a s routine can.**
3a300 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20 6f 74 called while ot
3a310 68 65 72 20 74 68 72 65 61 64 73 20 61 72 65 20 her threads are
3a320 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 61 6d 65 running the same
3a330 20 6f 72 20 64 69 66 66 65 72 65 6e 74 20 53 51 or different SQ
3a340 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 Lite.** interfac
3a350 65 73 2e 20 20 48 6f 77 65 76 65 72 20 74 68 65 es. However the
3a360 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 values returned
3a370 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e in *pCurrent an
3a380 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 d.** *pHighwater
3a390 20 72 65 66 6c 65 63 74 20 74 68 65 20 73 74 61 reflect the sta
3a3a0 74 75 73 20 6f 66 20 53 51 4c 69 74 65 20 61 74 tus of SQLite at
3a3b0 20 64 69 66 66 65 72 65 6e 74 20 70 6f 69 6e 74 different point
3a3c0 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 61 6e 64 s in time.** and
3a3d0 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
3a3e0 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 that another thr
3a3f0 65 61 64 20 6d 69 67 68 74 20 63 68 61 6e 67 65 ead might change
3a400 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a the parameter.*
3a410 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 * in between the
3a420 20 74 69 6d 65 73 20 77 68 65 6e 20 2a 70 43 75 times when *pCu
3a430 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 69 67 68 rrent and *pHigh
3a440 77 61 74 65 72 20 61 72 65 20 77 72 69 74 74 65 water are writte
3a450 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 n..**.** See als
3a460 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 o: [sqlite3_db_s
3a470 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 51 4c 49 tatus()].*/.SQLI
3a480 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
3a490 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 PERIMENTAL int s
3a4a0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e qlite3_status(in
3a4b0 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 t op, int *pCurr
3a4c0 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 ent, int *pHighw
3a4d0 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 ater, int resetF
3a4e0 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 lag);.../*.** CA
3a4f0 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 PI3REF: Status P
3a500 61 72 61 6d 65 74 65 72 73 20 7b 48 31 37 32 35 arameters {H1725
3a510 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 2a 20 45 0} <H17200>.** E
3a520 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
3a530 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20 * These integer
3a540 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 67 6e constants design
3a550 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 6e 2d ate various run-
3a560 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 72 61 time status para
3a570 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 63 meters.** that c
3a580 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 an be returned b
3a590 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 y [sqlite3_statu
3a5a0 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e s()]..**.** <dl>
3a5b0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3a5c0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
3a5d0 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
3a5e0 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 is parameter is
3a5f0 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d 6f 75 the current amou
3a600 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 68 65 nt of memory che
3a610 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73 69 6e cked out.** usin
3a620 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f g [sqlite3_mallo
3a630 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64 69 72 c()], either dir
3a640 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 ectly or indirec
3a650 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66 69 67 tly. The.** fig
3a660 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63 61 6c ure includes cal
3a670 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71 6c 69 ls made to [sqli
3a680 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 79 te3_malloc()] by
3a690 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
3a6a0 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c .** and internal
3a6b0 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 62 79 memory usage by
3a6c0 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
3a6d0 61 72 79 2e 20 20 53 63 72 61 74 63 68 20 6d 65 ary. Scratch me
3a6e0 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 6c mory.** controll
3a6f0 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f ed by [SQLITE_CO
3a700 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61 6e NFIG_SCRATCH] an
3a710 64 20 61 75 78 69 6c 69 61 72 79 20 70 61 67 65 d auxiliary page
3a720 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 -cache.** memory
3a730 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b controlled by [
3a740 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
3a750 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f 74 20 GECACHE] is not
3a760 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a 20 74 included in.** t
3a770 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e 20 20 his parameter.
3a780 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74 75 72 The amount retur
3a790 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d 20 6f ned is the sum o
3a7a0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
3a7b0 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72 65 70 .** sizes as rep
3a7c0 6f 72 74 65 64 20 62 79 20 74 68 65 20 78 53 69 orted by the xSi
3a7d0 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b 73 71 ze method in [sq
3a7e0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
3a7f0 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c s].</dd>.**.** <
3a800 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dt>SQLITE_STATUS
3a810 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64 74 _MALLOC_SIZE</dt
3a820 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 >.** <dd>This pa
3a830 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 rameter records
3a840 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f the largest memo
3a850 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 ry allocation re
3a860 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 quest.** handed
3a870 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c to [sqlite3_mall
3a880 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 oc()] or [sqlite
3a890 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28 6f 72 3_realloc()] (or
3a8a0 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 65 72 6e their.** intern
3a8b0 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 73 29 2e al equivalents).
3a8c0 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 Only the value
3a8d0 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 returned in the
3a8e0 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 .** *pHighwater
3a8f0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
3a900 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 lite3_status()]
3a910 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 is of interest.
3a920 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 .** The value w
3a930 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
3a940 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 *pCurrent parame
3a950 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ter is undefined
3a960 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
3a970 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 >SQLITE_STATUS_P
3a980 41 47 45 43 41 43 48 45 5f 55 53 45 44 3c 2f 64 AGECACHE_USED</d
3a990 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 t>.** <dd>This p
3a9a0 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 arameter returns
3a9b0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
3a9c0 61 67 65 73 20 75 73 65 64 20 6f 75 74 20 6f 66 ages used out of
3a9d0 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 63 61 63 the.** [pagecac
3a9e0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
3a9f0 74 6f 72 5d 20 74 68 61 74 20 77 61 73 20 63 6f tor] that was co
3aa00 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 20 0a nfigured using .
3aa10 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
3aa20 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 20 20 54 G_PAGECACHE]. T
3aa30 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 65 74 75 he.** value retu
3aa40 72 6e 65 64 20 69 73 20 69 6e 20 70 61 67 65 73 rned is in pages
3aa50 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 73 2e 3c , not in bytes.<
3aa60 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
3aa70 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
3aa80 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 3c ECACHE_OVERFLOW<
3aa90 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3aaa0 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 parameter retur
3aab0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
3aac0 20 62 79 74 65 73 20 6f 66 20 70 61 67 65 20 63 bytes of page c
3aad0 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 ache.** allocati
3aae0 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 20 6e on which could n
3aaf0 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 65 64 ot be statisfied
3ab00 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f by the [SQLITE_
3ab10 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
3ab20 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64 20 ].** buffer and
3ab30 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f 20 where forced to
3ab40 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71 6c overflow to [sql
3ab50 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 ite3_malloc()].
3ab60 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 The.** returned
3ab70 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 73 20 value includes
3ab80 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 allocations that
3ab90 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63 61 overflowed beca
3aba0 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 68 65 72 use they.** wher
3abb0 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 74 68 65 e too large (the
3abc0 79 20 77 65 72 65 20 6c 61 72 67 65 72 20 74 68 y were larger th
3abd0 61 6e 20 74 68 65 20 22 73 7a 22 20 70 61 72 61 an the "sz" para
3abe0 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 53 51 4c meter to.** [SQL
3abf0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
3ac00 41 43 48 45 5d 29 20 61 6e 64 20 61 6c 6c 6f 63 ACHE]) and alloc
3ac10 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 ations that over
3ac20 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 0a 2a flowed because.*
3ac30 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 73 20 6c * no space was l
3ac40 65 66 74 20 69 6e 20 74 68 65 20 70 61 67 65 20 eft in the page
3ac50 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a cache.</dd>.**.*
3ac60 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 * <dt>SQLITE_STA
3ac70 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 TUS_PAGECACHE_SI
3ac80 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ZE</dt>.** <dd>T
3ac90 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
3aca0 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 cords the larges
3acb0 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 t memory allocat
3acc0 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 ion request.** h
3acd0 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 65 63 61 anded to [pageca
3ace0 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 che memory alloc
3acf0 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 ator]. Only the
3ad00 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
3ad10 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 in the.** *pHigh
3ad20 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 water parameter
3ad30 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 to [sqlite3_stat
3ad40 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 us()] is of inte
3ad50 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 rest. .** The v
3ad60 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 alue written int
3ad70 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 o the *pCurrent
3ad80 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 parameter is und
3ad90 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a efined.</dd>.**.
3ada0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
3adb0 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 ATUS_SCRATCH_USE
3adc0 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
3add0 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 is parameter ret
3ade0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
3adf0 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 75 of allocations u
3ae00 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a sed out of the.*
3ae10 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 * [scratch memor
3ae20 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e y allocator] con
3ae30 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a figured using.**
3ae40 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
3ae50 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20 76 SCRATCH]. The v
3ae60 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
3ae70 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c in allocations,
3ae80 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 65 73 not.** in bytes
3ae90 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e 67 6c . Since a singl
3aea0 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c e thread may onl
3aeb0 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 61 74 y have one scrat
3aec0 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a ch allocation.**
3aed0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 outstanding at
3aee0 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 61 6d time, this param
3aef0 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f 72 74 eter also report
3af00 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
3af10 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 6e 67 threads.** using
3af20 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 scratch memory
3af30 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
3af40 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
3af50 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 >SQLITE_STATUS_S
3af60 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 3c CRATCH_OVERFLOW<
3af70 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3af80 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 parameter retur
3af90 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
3afa0 20 62 79 74 65 73 20 6f 66 20 73 63 72 61 74 63 bytes of scratc
3afb0 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f h memory.** allo
3afc0 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 cation which cou
3afd0 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73 ld not be statis
3afe0 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c fied by the [SQL
3aff0 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
3b000 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e CH].** buffer an
3b010 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 d where forced t
3b020 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 o overflow to [s
3b030 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
3b040 2e 20 20 54 68 65 20 76 61 6c 75 65 73 0a 2a 2a . The values.**
3b050 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 returned includ
3b060 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65 63 61 e overflows beca
3b070 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65 use the requeste
3b080 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 d allocation was
3b090 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 72 20 28 too.** larger (
3b0a0 74 68 61 74 20 69 73 2c 20 62 65 63 61 75 73 65 that is, because
3b0b0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 61 the requested a
3b0c0 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6c 61 llocation was la
3b0d0 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a rger than the.**
3b0e0 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20 "sz" parameter
3b0f0 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 to [SQLITE_CONFI
3b100 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e 64 20 G_SCRATCH]) and
3b110 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72 61 74 because no scrat
3b120 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 73 6c 6f ch buffer.** slo
3b130 74 73 20 77 65 72 65 20 61 76 61 69 6c 61 62 6c ts were availabl
3b140 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a e..** </dd>.**.*
3b150 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 * <dt>SQLITE_STA
3b160 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 TUS_SCRATCH_SIZE
3b170 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
3b180 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f s parameter reco
3b190 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 rds the largest
3b1a0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
3b1b0 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e n request.** han
3b1c0 64 65 64 20 74 6f 20 5b 73 63 72 61 74 63 68 20 ded to [scratch
3b1d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
3b1e0 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c ]. Only the val
3b1f0 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 ue returned in t
3b200 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 he.** *pHighwate
3b210 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b r parameter to [
3b220 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 sqlite3_status()
3b230 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 ] is of interest
3b240 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 . .** The value
3b250 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
3b260 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 e *pCurrent para
3b270 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e meter is undefin
3b280 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ed.</dd>.**.** <
3b290 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dt>SQLITE_STATUS
3b2a0 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c 2f 64 _PARSER_STACK</d
3b2b0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 t>.** <dd>This p
3b2c0 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 arameter records
3b2d0 20 74 68 65 20 64 65 65 70 65 73 74 20 70 61 72 the deepest par
3b2e0 73 65 72 20 73 74 61 63 6b 2e 20 20 49 74 20 69 ser stack. It i
3b2f0 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 6e s only.** meanin
3b300 67 66 75 6c 20 69 66 20 53 51 4c 69 74 65 20 69 gful if SQLite i
3b310 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 s compiled with
3b320 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b [YYTRACKMAXSTACK
3b330 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a 2a 2a 20 DEPTH].</dd>.**
3b340 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 </dl>.**.** New
3b350 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 status parameter
3b360 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 66 s may be added f
3b370 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d 65 rom time to time
3b380 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
3b390 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
3b3a0 59 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 20 Y_USED
3b3b0 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
3b3c0 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
3b3d0 45 5f 55 53 45 44 20 20 20 20 20 20 20 31 0a 23 E_USED 1.#
3b3e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3b3f0 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f ATUS_PAGECACHE_O
3b400 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 65 66 VERFLOW 2.#def
3b410 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
3b420 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 20 20 S_SCRATCH_USED
3b430 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 3.#define
3b440 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 SQLITE_STATUS_S
3b450 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20 CRATCH_OVERFLOW
3b460 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4.#define SQ
3b470 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c LITE_STATUS_MALL
3b480 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 OC_SIZE
3b490 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 5.#define SQLIT
3b4a0 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f E_STATUS_PARSER_
3b4b0 53 54 41 43 4b 20 20 20 20 20 20 20 20 20 36 0a STACK 6.
3b4c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
3b4d0 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
3b4e0 53 49 5a 45 20 20 20 20 20 20 20 37 0a 23 64 65 SIZE 7.#de
3b4f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
3b500 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 20 US_SCRATCH_SIZE
3b510 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a 0a 2a 2a 8../*.**
3b520 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62 CAPI3REF: Datab
3b530 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 53 ase Connection S
3b540 74 61 74 75 73 20 7b 48 31 37 35 30 30 7d 20 3c tatus {H17500} <
3b550 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S60200>.** EXPER
3b560 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
3b570 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 is interface is
3b580 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 used to retrieve
3b590 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 runtime status
3b5a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20 information .**
3b5b0 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20 5b about a single [
3b5c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3b5d0 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72 73 74 ion]. The first
3b5e0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
3b5f0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
3b600 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 20 74 nection object t
3b610 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 o be interrogate
3b620 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 d. The second a
3b630 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68 rgument.** is th
3b640 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 69 e parameter to i
3b650 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 43 75 72 nterrogate. Cur
3b660 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 rently, the only
3b670 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 0a 2a allowed value.*
3b680 2a 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 * for the second
3b690 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 5b 53 parameter is [S
3b6a0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c QLITE_DBSTATUS_L
3b6b0 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d 2e 0a OOKASIDE_USED]..
3b6c0 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6f 70 ** Additional op
3b6d0 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c tions will likel
3b6e0 79 20 61 70 70 65 61 72 20 69 6e 20 66 75 74 75 y appear in futu
3b6f0 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 re releases of S
3b700 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 QLite..**.** The
3b710 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f current value o
3b720 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
3b730 70 61 72 61 6d 65 74 65 72 20 69 73 20 77 72 69 parameter is wri
3b740 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 0a tten into *pCur.
3b750 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67 68 65 ** and the highe
3b760 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 st instantaneous
3b770 20 76 61 6c 75 65 20 69 73 20 77 72 69 74 74 65 value is writte
3b780 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72 2e 20 n into *pHiwtr.
3b790 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 73 65 74 If.** the reset
3b7a0 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 Flg is true, the
3b7b0 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 69 6e n the highest in
3b7c0 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 stantaneous valu
3b7d0 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 20 62 61 e is.** reset ba
3b7e0 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 63 ck down to the c
3b7f0 75 72 72 65 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a urrent value..**
3b800 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
3b810 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d qlite3_status()]
3b820 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 and [sqlite3_st
3b830 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2f mt_status()]..*/
3b840 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
3b850 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
3b860 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 int sqlite3_db_s
3b870 74 61 74 75 73 28 73 71 6c 69 74 65 33 2a 2c 20 tatus(sqlite3*,
3b880 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 int op, int *pCu
3b890 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 72 2c 20 r, int *pHiwtr,
3b8a0 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a int resetFlg);..
3b8b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3b8c0 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 Status Parameter
3b8d0 73 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 63 s for database c
3b8e0 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 31 37 35 onnections {H175
3b8f0 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 2a 2a 20 20} <H17500>.**
3b900 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
3b910 2a 2a 20 53 74 61 74 75 73 20 76 65 72 62 73 20 ** Status verbs
3b920 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f for [sqlite3_db_
3b930 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a status()]..**.**
3b940 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c <dl>.** <dt>SQL
3b950 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f ITE_DBSTATUS_LOO
3b960 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e KASIDE_USED</dt>
3b970 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 .** <dd>This par
3b980 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 ameter returns t
3b990 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f he number of loo
3b9a0 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c kaside memory sl
3b9b0 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a ots currently.**
3b9c0 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 checked out.</d
3b9d0 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 d>.** </dl>.*/.#
3b9e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 define SQLITE_DB
3b9f0 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 STATUS_LOOKASIDE
3ba00 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f 2a _USED 0.../*
3ba10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 72 .** CAPI3REF: Pr
3ba20 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 epared Statement
3ba30 20 53 74 61 74 75 73 20 7b 48 31 37 35 35 30 7d Status {H17550}
3ba40 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 <S60200>.** EXP
3ba50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
3ba60 45 61 63 68 20 70 72 65 70 61 72 65 64 20 73 74 Each prepared st
3ba70 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69 6e atement maintain
3ba80 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53 51 s various.** [SQ
3ba90 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f LITE_STMTSTATUS_
3baa0 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73 5d SORT | counters]
3bab0 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68 that measure th
3bac0 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 e number.** of t
3bad0 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66 imes it has perf
3bae0 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f ormed specific o
3baf0 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65 73 perations. Thes
3bb00 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a 2a e counters can.*
3bb10 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f 6e * be used to mon
3bb20 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72 6d itor the perform
3bb30 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 69 73 ance characteris
3bb40 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 65 70 tics of the prep
3bb50 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e ared.** statemen
3bb60 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 ts. For example
3bb70 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 , if the number
3bb80 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 20 67 of table steps g
3bb90 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a 2a reatly exceeds.*
3bba0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
3bbb0 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 6f table searches o
3bbc0 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20 74 r result rows, t
3bbd0 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20 74 hat would tend t
3bbe0 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 o indicate.** th
3bbf0 61 74 20 74 68 65 20 70 72 65 70 61 72 65 64 20 at the prepared
3bc00 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73 69 statement is usi
3bc10 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 ng a full table
3bc20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 61 6e scan rather than
3bc30 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 0a .** an index. .
3bc40 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
3bc50 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 face is used to
3bc60 72 65 74 72 69 65 76 65 20 61 6e 64 20 72 65 73 retrieve and res
3bc70 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 et counter value
3bc80 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72 65 s from.** a [pre
3bc90 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
3bca0 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 . The first arg
3bcb0 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72 65 ument is the pre
3bcc0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a pared statement.
3bcd0 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 ** object to be
3bce0 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54 interrogated. T
3bcf0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
3bd00 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 nt.** is an inte
3bd10 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 73 ger code for a s
3bd20 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 5f pecific [SQLITE_
3bd30 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 STMTSTATUS_SORT
3bd40 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f | counter].** to
3bd50 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 be interrogated
3bd60 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e . .** The curren
3bd70 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 t value of the r
3bd80 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72 equested counter
3bd90 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
3bda0 20 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67 If the resetFlg
3bdb0 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
3bdc0 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 he counter is re
3bdd0 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 set to zero afte
3bde0 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 r this.** interf
3bdf0 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 ace call returns
3be00 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
3be10 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 : [sqlite3_statu
3be20 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 s()] and [sqlite
3be30 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a 3_db_status()]..
3be40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
3be50 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
3be60 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 L int sqlite3_st
3be70 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 mt_status(sqlite
3be80 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c 3_stmt*, int op,
3be90 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a int resetFlg);..
3bea0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3beb0 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 Status Parameter
3bec0 73 20 66 6f 72 20 70 72 65 70 61 72 65 64 20 73 s for prepared s
3bed0 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35 37 tatements {H1757
3bee0 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20 45 0} <H17550>.** E
3bef0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
3bf00 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65 * These preproce
3bf10 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69 ssor macros defi
3bf20 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 ne integer codes
3bf30 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74 that name count
3bf40 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73 er.** values ass
3bf50 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
3bf60 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 [sqlite3_stmt_s
3bf70 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 tatus()] interfa
3bf80 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69 ce..** The meani
3bf90 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f ngs of the vario
3bfa0 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20 us counters are
3bfb0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
3bfc0 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
3bfd0 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f LITE_STMTSTATUS_
3bfe0 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f 64 FULLSCAN_STEP</d
3bff0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 t>.** <dd>This i
3c000 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
3c010 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69 74 times that SQLit
3c020 65 20 68 61 73 20 73 74 65 70 70 65 64 20 66 6f e has stepped fo
3c030 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74 61 rward in.** a ta
3c040 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20 61 ble as part of a
3c050 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e full table scan
3c060 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72 73 . Large numbers
3c070 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74 65 for this counte
3c080 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61 74 r.** may indicat
3c090 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 20 e opportunities
3c0a0 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 for performance
3c0b0 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72 6f improvement thro
3c0c0 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c 20 ugh .** careful
3c0d0 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c use of indices.<
3c0e0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
3c0f0 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
3c100 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 _SORT</dt>.** <d
3c110 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 d>This is the nu
3c120 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 65 mber of sort ope
3c130 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 rations that hav
3c140 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 41 e occurred..** A
3c150 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 non-zero value
3c160 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 in this counter
3c170 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e 20 may indicate an
3c180 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a 2a opportunity to.*
3c190 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 65 * improvement pe
3c1a0 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 67 rformance throug
3c1b0 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66 h careful use of
3c1c0 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a indices.</dd>.*
3c1d0 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 *.** </dl>.*/.#d
3c1e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d efine SQLITE_STM
3c1f0 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e TSTATUS_FULLSCAN
3c200 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64 65 66 _STEP 1.#def
3c210 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 ine SQLITE_STMTS
3c220 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20 20 TATUS_SORT
3c230 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 2../*.**
3c240 20 43 41 50 49 33 52 45 46 3a 20 43 75 73 74 6f CAPI3REF: Custo
3c250 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f 62 6a m Page Cache Obj
3c260 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e ect.** EXPERIMEN
3c270 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 TAL.**.** The sq
3c280 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74 79 70 lite3_pcache typ
3c290 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20 49 74 e is opaque. It
3c2a0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
3c2b0 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 67 61 by.** the plugga
3c2c0 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 ble module. The
3c2d0 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68 61 73 SQLite core has
3c2e0 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66 no knowledge of
3c2f0 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f 72 20 .** its size or
3c300 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 internal structu
3c310 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64 65 61 re and never dea
3c320 6c 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 ls with the.** s
3c330 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 6f 62 qlite3_pcache ob
3c340 6a 65 63 74 20 65 78 63 65 70 74 20 62 79 20 68 ject except by h
3c350 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 73 69 olding and passi
3c360 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 74 ng pointers.** t
3c370 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a o the object..**
3c380 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 65 33 .** See [sqlite3
3c390 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d _pcache_methods]
3c3a0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
3c3b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
3c3c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
3c3d0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 73 71 qlite3_pcache sq
3c3e0 6c 69 74 65 33 5f 70 63 61 63 68 65 3b 0a 0a 2f lite3_pcache;../
3c3f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 *.** CAPI3REF: A
3c400 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 69 6e pplication Defin
3c410 65 64 20 50 61 67 65 20 43 61 63 68 65 2e 0a 2a ed Page Cache..*
3c420 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3c430 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 *.** The [sqlite
3c440 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 3_config]([SQLIT
3c450 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d E_CONFIG_PCACHE]
3c460 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 , ...) interface
3c470 20 63 61 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 can.** register
3c480 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 an alternative
3c490 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 page cache imple
3c4a0 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20 70 61 73 mentation by pas
3c4b0 73 69 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 sing in an .** i
3c4c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 nstance of the s
3c4d0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
3c4e0 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 2e thods structure.
3c4f0 20 54 68 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 The majority of
3c500 20 74 68 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 the .** heap me
3c510 6d 6f 72 79 20 75 73 65 64 20 62 79 20 73 71 6c mory used by sql
3c520 69 74 65 20 69 73 20 75 73 65 64 20 62 79 20 74 ite is used by t
3c530 68 65 20 70 61 67 65 20 63 61 63 68 65 20 74 6f he page cache to
3c540 20 63 61 63 68 65 20 64 61 74 61 20 72 65 61 64 cache data read
3c550 20 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 .** from, or re
3c560 61 64 79 20 74 6f 20 62 65 20 77 72 69 74 74 65 ady to be writte
3c570 6e 20 74 6f 2c 20 74 68 65 20 64 61 74 61 62 61 n to, the databa
3c580 73 65 20 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c se file. By impl
3c590 65 6d 65 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 ementing a .** c
3c5a0 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65 ustom page cache
3c5b0 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 2c using this API,
3c5c0 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 an application
3c5d0 63 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 can control more
3c5e0 20 0a 2a 2a 20 70 72 65 63 69 73 65 6c 79 20 74 .** precisely t
3c5f0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d he amount of mem
3c600 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 ory consumed by
3c610 73 71 6c 69 74 65 2c 20 74 68 65 20 77 61 79 20 sqlite, the way
3c620 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20 73 61 69 in which .** sai
3c630 64 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f d memory is allo
3c640 63 61 74 65 64 20 61 6e 64 20 72 65 6c 65 61 73 cated and releas
3c650 65 64 2c 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 ed, and the poli
3c660 63 69 65 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a cies used to .**
3c670 20 64 65 74 65 72 6d 69 6e 65 20 65 78 61 63 74 determine exact
3c680 6c 79 20 77 68 69 63 68 20 70 61 72 74 73 20 6f ly which parts o
3c690 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c f a database fil
3c6a0 65 20 61 72 65 20 63 61 63 68 65 64 20 61 6e 64 e are cached and
3c6b0 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e for .** how lon
3c6c0 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e g..**.** The con
3c6d0 74 65 6e 74 73 20 6f 66 20 74 68 65 20 73 74 72 tents of the str
3c6e0 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 69 65 ucture are copie
3c6f0 64 20 74 6f 20 61 6e 20 69 6e 74 65 72 6e 61 6c d to an internal
3c700 20 62 75 66 66 65 72 20 62 79 20 73 71 6c 69 74 buffer by sqlit
3c710 65 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 e.** within the
3c720 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
3c730 5f 63 6f 6e 66 69 67 5d 2e 0a 2a 2a 0a 2a 2a 20 _config]..**.**
3c740 54 68 65 20 78 49 6e 69 74 28 29 20 6d 65 74 68 The xInit() meth
3c750 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 od is called onc
3c760 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 e for each call
3c770 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 to [sqlite3_init
3c780 69 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 28 75 73 ialize()].** (us
3c790 75 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65 20 ually only once
3c7a0 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 during the lifet
3c7b0 69 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63 65 ime of the proce
3c7c0 73 73 29 2e 20 49 74 20 69 73 20 70 61 73 73 65 ss). It is passe
3c7d0 64 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 d.** a copy of t
3c7e0 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
3c7f0 65 5f 6d 65 74 68 6f 64 73 2e 70 41 72 67 20 76 e_methods.pArg v
3c800 61 6c 75 65 2e 20 49 74 20 63 61 6e 20 62 65 20 alue. It can be
3c810 75 73 65 64 20 74 6f 20 73 65 74 0a 2a 2a 20 75 used to set.** u
3c820 70 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 p global structu
3c830 72 65 73 20 61 6e 64 20 6d 75 74 65 78 65 73 20 res and mutexes
3c840 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 required by the
3c850 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 custom page cach
3c860 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 e .** implementa
3c870 74 69 6f 6e 2e 20 54 68 65 20 78 53 68 75 74 64 tion. The xShutd
3c880 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 20 own() method is
3c890 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 called from with
3c8a0 69 6e 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f in .** [sqlite3_
3c8b0 73 68 75 74 64 6f 77 6e 28 29 5d 2c 20 69 66 20 shutdown()], if
3c8c0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
3c8d0 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 49 invokes this API
3c8e0 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 . It can be used
3c8f0 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 .** to clean up
3c900 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 any outstanding
3c910 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 65 resources before
3c920 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f 77 process shutdow
3c930 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a n, if required..
3c940 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74 **.** The xCreat
3c950 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73 e() method is us
3c960 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 ed to construct
3c970 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e 73 74 a new cache inst
3c980 61 6e 63 65 2e 20 54 68 65 0a 2a 2a 20 66 69 72 ance. The.** fir
3c990 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20 73 7a st parameter, sz
3c9a0 50 61 67 65 2c 20 69 73 20 74 68 65 20 73 69 7a Page, is the siz
3c9b0 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 e in bytes of th
3c9c0 65 20 70 61 67 65 73 20 74 68 61 74 20 6d 75 73 e pages that mus
3c9d0 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f 63 61 74 65 t.** be allocate
3c9e0 64 20 62 79 20 74 68 65 20 63 61 63 68 65 2e 20 d by the cache.
3c9f0 73 7a 50 61 67 65 20 77 69 6c 6c 20 6e 6f 74 20 szPage will not
3ca00 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 be a power of tw
3ca10 6f 2e 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 o. The.** second
3ca20 20 61 72 67 75 6d 65 6e 74 2c 20 62 50 75 72 67 argument, bPurg
3ca30 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65 20 69 eable, is true i
3ca40 66 20 74 68 65 20 63 61 63 68 65 20 62 65 69 6e f the cache bein
3ca50 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c 0a 2a g created will.*
3ca60 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63 61 63 * be used to cac
3ca70 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
3ca80 73 20 72 65 61 64 20 66 72 6f 6d 20 61 20 66 69 s read from a fi
3ca90 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 le stored on dis
3caa0 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69 k, or.** false i
3cab0 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72 f it is used for
3cac0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
3cad0 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68 tabase. The cach
3cae0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
3caf0 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 .** does not hav
3cb00 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 e to do anything
3cb10 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 6f special based o
3cb20 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 62 n the value of b
3cb30 50 75 72 67 65 61 62 6c 65 2c 0a 2a 2a 20 69 74 Purgeable,.** it
3cb40 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69 73 is purely advis
3cb50 6f 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ory. .**.** The
3cb60 78 43 61 63 68 65 73 69 7a 65 28 29 20 6d 65 74 xCachesize() met
3cb70 68 6f 64 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 hod may be calle
3cb80 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 62 79 d at any time by
3cb90 20 53 51 4c 69 74 65 20 74 6f 20 73 65 74 20 74 SQLite to set t
3cba0 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65 64 20 he.** suggested
3cbb0 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 2d 73 69 maximum cache-si
3cbc0 7a 65 20 28 6e 75 6d 62 65 72 20 6f 66 20 70 61 ze (number of pa
3cbd0 67 65 73 20 73 74 6f 72 65 64 20 62 79 29 20 74 ges stored by) t
3cbe0 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 6e 73 74 he cache.** inst
3cbf0 61 6e 63 65 20 70 61 73 73 65 64 20 61 73 20 74 ance passed as t
3cc00 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
3cc10 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 76 t. This is the v
3cc20 61 6c 75 65 20 63 6f 6e 66 69 67 75 72 65 64 20 alue configured
3cc30 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c using.** the SQL
3cc40 69 74 65 20 22 5b 50 52 41 47 4d 41 20 63 61 63 ite "[PRAGMA cac
3cc50 68 65 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e he_size]" comman
3cc60 64 2e 20 41 73 20 77 69 74 68 20 74 68 65 20 62 d. As with the b
3cc70 50 75 72 67 65 61 62 6c 65 20 70 61 72 61 6d 65 Purgeable parame
3cc80 74 65 72 2c 0a 2a 2a 20 74 68 65 20 69 6d 70 6c ter,.** the impl
3cc90 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f ementation is no
3cca0 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 64 6f t required to do
3ccb0 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63 69 61 anything specia
3ccc0 6c 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 76 l with this.** v
3ccd0 61 6c 75 65 2c 20 69 74 20 69 73 20 61 64 76 69 alue, it is advi
3cce0 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a sory only..**.**
3ccf0 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74 28 The xPagecount(
3cd00 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 ) method should
3cd10 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 return the numbe
3cd20 72 20 6f 66 20 70 61 67 65 73 20 63 75 72 72 65 r of pages curre
3cd30 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20 69 ntly.** stored i
3cd40 6e 20 74 68 65 20 63 61 63 68 65 20 73 75 70 70 n the cache supp
3cd50 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d lied as an argum
3cd60 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 ent..** .** The
3cd70 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64 20 xFetch() method
3cd80 69 73 20 75 73 65 64 20 74 6f 20 66 65 74 63 68 is used to fetch
3cd90 20 61 20 70 61 67 65 20 61 6e 64 20 72 65 74 75 a page and retu
3cda0 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
3cdb0 69 74 2e 20 0a 2a 2a 20 41 20 27 70 61 67 65 27 it. .** A 'page'
3cdc0 2c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 , in this contex
3cdd0 74 2c 20 69 73 20 61 20 62 75 66 66 65 72 20 6f t, is a buffer o
3cde0 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 20 61 f szPage bytes a
3cdf0 6c 69 67 6e 65 64 20 61 74 20 61 6e 0a 2a 2a 20 ligned at an.**
3ce00 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 8-byte boundary.
3ce10 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 The page to be
3ce20 66 65 74 63 68 65 64 20 69 73 20 64 65 74 65 72 fetched is deter
3ce30 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b 65 79 mined by the key
3ce40 2e 20 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d . The.** mimimum
3ce50 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 31 2e key value is 1.
3ce60 20 41 66 74 65 72 20 69 74 20 68 61 73 20 62 65 After it has be
3ce70 65 6e 20 72 65 74 72 69 65 76 65 64 20 75 73 69 en retrieved usi
3ce80 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65 20 70 ng xFetch, the p
3ce90 61 67 65 20 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 age .** is consi
3cea0 64 65 72 65 64 20 74 6f 20 62 65 20 70 69 6e 6e dered to be pinn
3ceb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
3cec0 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
3ced0 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 is already in th
3cee0 65 20 70 61 67 65 20 63 61 63 68 65 2c 20 74 68 e page cache, th
3cef0 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a en a pointer to.
3cf00 2a 2a 20 74 68 65 20 63 61 63 68 65 64 20 62 75 ** the cached bu
3cf10 66 66 65 72 20 73 68 6f 75 6c 64 20 62 65 20 72 ffer should be r
3cf20 65 74 75 72 6e 65 64 20 77 69 74 68 20 69 74 73 eturned with its
3cf30 20 63 6f 6e 74 65 6e 74 73 20 69 6e 74 61 63 74 contents intact
3cf40 2e 20 49 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 . If the.** page
3cf50 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
3cf60 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 in the cache, th
3cf70 65 6e 20 74 68 65 20 65 78 70 65 63 74 65 64 20 en the expected
3cf80 62 65 68 61 76 69 6f 75 72 20 6f 66 20 74 68 65 behaviour of the
3cf90 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 64 65 74 .** cache is det
3cfa0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 76 ermined by the v
3cfb0 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61 alue of the crea
3cfc0 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72 teFlag parameter
3cfd0 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 78 46 passed.** to xF
3cfe0 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e 67 20 etch, according
3cff0 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
3d000 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c 74 table:.**.** <t
3d010 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 77 69 able border=1 wi
3d020 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63 65 dth=85% align=ce
3d030 6e 74 65 72 3e 0a 2a 2a 20 20 20 3c 74 72 3e 3c nter>.** <tr><
3d040 74 68 3e 63 72 65 61 74 65 46 6c 61 67 3c 74 68 th>createFlag<th
3d050 3e 45 78 70 65 63 74 65 64 20 42 65 68 61 76 69 >Expected Behavi
3d060 6f 75 72 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 our.** <tr><td
3d070 3e 30 3c 74 64 3e 4e 55 4c 4c 20 73 68 6f 75 6c >0<td>NULL shoul
3d080 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 4e d be returned. N
3d090 6f 20 6e 65 77 20 63 61 63 68 65 20 65 6e 74 72 o new cache entr
3d0a0 79 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a y is created..**
3d0b0 20 20 20 3c 74 72 3e 3c 74 64 3e 31 3c 74 64 3e <tr><td>1<td>
3d0c0 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 If createFlag is
3d0d0 20 73 65 74 20 74 6f 20 31 2c 20 74 68 69 73 20 set to 1, this
3d0e0 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 0a indicates that .
3d0f0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
3d100 20 20 53 51 4c 69 74 65 20 69 73 20 68 6f 6c 64 SQLite is hold
3d110 69 6e 67 20 70 69 6e 6e 65 64 20 70 61 67 65 73 ing pinned pages
3d120 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 6e 70 that can be unp
3d130 69 6e 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 inned.**
3d140 20 20 20 20 20 20 20 20 62 79 20 77 72 69 74 69 by writi
3d150 6e 67 20 74 68 65 69 72 20 63 6f 6e 74 65 6e 74 ng their content
3d160 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 s to the databas
3d170 65 20 66 69 6c 65 20 28 61 0a 2a 2a 20 20 20 20 e file (a.**
3d180 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 61 rela
3d190 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69 76 65 tively expensive
3d1a0 20 6f 70 65 72 61 74 69 6f 6e 29 2e 20 49 6e 20 operation). In
3d1b0 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e 20 74 this situation t
3d1c0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 he.**
3d1d0 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 6c 65 cache imple
3d1e0 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 74 77 mentation has tw
3d1f0 6f 20 63 68 6f 69 63 65 73 3a 20 69 74 20 63 61 o choices: it ca
3d200 6e 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2c 0a 2a n return NULL,.*
3d210 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
3d220 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 in which case S
3d230 51 4c 69 74 65 20 77 69 6c 6c 20 61 74 74 65 6d QLite will attem
3d240 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 pt to unpin one
3d250 6f 72 20 6d 6f 72 65 20 0a 2a 2a 20 20 20 20 20 or more .**
3d260 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 73 pages
3d270 20 62 65 66 6f 72 65 20 72 65 2d 72 65 71 75 65 before re-reque
3d280 73 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 70 sting the same p
3d290 61 67 65 2c 20 6f 72 20 69 74 20 63 61 6e 0a 2a age, or it can.*
3d2a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
3d2b0 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
3d2c0 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 page and return
3d2d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
3d2e0 20 49 66 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 If a new.**
3d2f0 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 page
3d300 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 is allocated, t
3d310 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 73 69 hen the first si
3d320 7a 65 6f 66 28 76 6f 69 64 2a 29 20 62 79 74 65 zeof(void*) byte
3d330 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 s of.**
3d340 20 20 20 20 20 20 20 69 74 20 28 61 74 20 6c 65 it (at le
3d350 61 73 74 29 20 6d 75 73 74 20 62 65 20 7a 65 72 ast) must be zer
3d360 6f 65 64 20 62 65 66 6f 72 65 20 69 74 20 69 73 oed before it is
3d370 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 20 20 returned..**
3d380 3c 74 72 3e 3c 74 64 3e 32 3c 74 64 3e 49 66 20 <tr><td>2<td>If
3d390 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 73 65 createFlag is se
3d3a0 74 20 74 6f 20 32 2c 20 74 68 65 6e 20 53 51 4c t to 2, then SQL
3d3b0 69 74 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69 ite is not holdi
3d3c0 6e 67 20 61 6e 79 0a 2a 2a 20 20 20 20 20 20 20 ng any.**
3d3d0 20 20 20 20 20 20 20 20 20 70 69 6e 6e 65 64 20 pinned
3d3e0 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 pages associated
3d3f0 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 with the specif
3d400 69 63 20 63 61 63 68 65 20 70 61 73 73 65 64 0a ic cache passed.
3d410 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
3d420 20 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 as the first a
3d430 72 67 75 6d 65 6e 74 20 74 6f 20 78 46 65 74 63 rgument to xFetc
3d440 68 28 29 20 74 68 61 74 20 63 61 6e 20 62 65 20 h() that can be
3d450 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65 0a 2a 2a unpinned. The.**
3d460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3d470 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 cache implementa
3d480 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 74 74 65 tion should atte
3d490 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 mpt to allocate
3d4a0 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 a new.**
3d4b0 20 20 20 20 20 20 20 20 63 61 63 68 65 20 65 6e cache en
3d4c0 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 61 try and return a
3d4d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
3d4e0 41 67 61 69 6e 2c 20 74 68 65 20 66 69 72 73 74 Again, the first
3d4f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
3d500 20 20 20 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 sizeof(void*)
3d510 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 bytes of the pa
3d520 67 65 20 73 68 6f 75 6c 64 20 62 65 20 7a 65 72 ge should be zer
3d530 6f 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20 20 oed before .**
3d540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 74 it
3d550 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
3d560 20 74 68 65 20 78 46 65 74 63 68 28 29 20 6d 65 the xFetch() me
3d570 74 68 6f 64 20 72 65 74 75 72 6e 73 20 4e 55 4c thod returns NUL
3d580 4c 20 77 68 65 6e 20 0a 2a 2a 20 20 20 20 20 20 L when .**
3d590 20 20 20 20 20 20 20 20 20 20 63 72 65 61 74 65 create
3d5a0 46 6c 61 67 3d 3d 32 2c 20 53 51 4c 69 74 65 20 Flag==2, SQLite
3d5b0 61 73 73 75 6d 65 73 20 74 68 61 74 20 61 20 6d assumes that a m
3d5c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
3d5d0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 .**
3d5e0 20 20 20 20 66 61 69 6c 65 64 20 61 6e 64 20 72 failed and r
3d5f0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4e 4f eturns SQLITE_NO
3d600 4d 45 4d 20 74 6f 20 74 68 65 20 75 73 65 72 2e MEM to the user.
3d610 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 0a .** </table>.**.
3d620 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20 63 ** xUnpin() is c
3d630 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 alled by SQLite
3d640 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 with a pointer t
3d650 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70 69 o a currently pi
3d660 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73 20 nned page.** as
3d670 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d its second argum
3d680 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69 72 ent. If the thir
3d690 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69 73 d parameter, dis
3d6a0 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 card, is non-zer
3d6b0 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 o,.** then the p
3d6c0 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65 76 age should be ev
3d6d0 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63 icted from the c
3d6e0 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 61 ache. In this ca
3d6f0 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61 73 se SQLite .** as
3d700 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 6e sumes that the n
3d710 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61 67 ext time the pag
3d720 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20 66 e is retrieved f
3d730 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75 73 rom the cache us
3d740 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74 63 ing.** the xFetc
3d750 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20 77 h() method, it w
3d760 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20 49 ill be zeroed. I
3d770 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70 61 f the discard pa
3d780 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a 65 rameter is.** ze
3d790 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 ro, then the pag
3d7a0 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 e is considered
3d7b0 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20 to be unpinned.
3d7c0 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d The cache implem
3d7d0 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20 entation.** may
3d7e0 63 68 6f 6f 73 65 20 74 6f 20 72 65 63 6c 61 69 choose to reclai
3d7f0 6d 20 28 66 72 65 65 20 6f 72 20 72 65 63 79 63 m (free or recyc
3d800 6c 65 29 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 le) unpinned pag
3d810 65 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a es at any time..
3d820 2a 2a 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 ** SQLite assume
3d830 73 20 74 68 61 74 20 6e 65 78 74 20 74 69 6d 65 s that next time
3d840 20 74 68 65 20 70 61 67 65 20 69 73 20 72 65 74 the page is ret
3d850 72 69 65 76 65 64 20 66 72 6f 6d 20 74 68 65 20 rieved from the
3d860 63 61 63 68 65 0a 2a 2a 20 69 74 20 77 69 6c 6c cache.** it will
3d870 20 65 69 74 68 65 72 20 62 65 20 7a 65 72 6f 65 either be zeroe
3d880 64 2c 20 6f 72 20 63 6f 6e 74 61 69 6e 20 74 68 d, or contain th
3d890 65 20 73 61 6d 65 20 64 61 74 61 20 74 68 61 74 e same data that
3d8a0 20 69 74 20 64 69 64 20 77 68 65 6e 20 69 74 0a it did when it.
3d8b0 2a 2a 20 77 61 73 20 75 6e 70 69 6e 6e 65 64 2e ** was unpinned.
3d8c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 .**.** The cache
3d8d0 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
3d8e0 20 74 6f 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 to perform any
3d8f0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 reference counti
3d900 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0a 2a 2a ng. A single .**
3d910 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70 69 6e 28 call to xUnpin(
3d920 29 20 75 6e 70 69 6e 73 20 74 68 65 20 70 61 67 ) unpins the pag
3d930 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 e regardless of
3d940 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 the number of pr
3d950 69 6f 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f ior calls .** to
3d960 20 78 46 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a xFetch()..**.**
3d970 20 54 68 65 20 78 52 65 6b 65 79 28 29 20 6d 65 The xRekey() me
3d980 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 thod is used to
3d990 63 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 76 change the key v
3d9a0 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 alue associated
3d9b0 77 69 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 with the.** page
3d9c0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
3d9d0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 66 econd argument f
3d9e0 72 6f 6d 20 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 rom oldKey to ne
3d9f0 77 4b 65 79 2e 20 49 66 20 74 68 65 20 63 61 63 wKey. If the cac
3da00 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 he.** previously
3da10 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 contains an ent
3da20 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ry associated wi
3da30 74 68 20 6e 65 77 4b 65 79 2c 20 69 74 20 73 68 th newKey, it sh
3da40 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 69 73 63 61 ould be.** disca
3da50 72 64 65 64 2e 20 41 6e 79 20 70 72 69 6f 72 20 rded. Any prior
3da60 63 61 63 68 65 20 65 6e 74 72 79 20 61 73 73 6f cache entry asso
3da70 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b ciated with newK
3da80 65 79 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 ey is guaranteed
3da90 20 6e 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 70 69 not.** to be pi
3daa0 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e nned..**.** When
3dab0 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 SQLite calls th
3dac0 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 6d 65 e xTruncate() me
3dad0 74 68 6f 64 2c 20 74 68 65 20 63 61 63 68 65 20 thod, the cache
3dae0 6d 75 73 74 20 64 69 73 63 61 72 64 20 61 6c 6c must discard all
3daf0 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 63 61 63 .** existing cac
3db00 68 65 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 he entries with
3db10 70 61 67 65 20 6e 75 6d 62 65 72 73 20 28 6b 65 page numbers (ke
3db20 79 73 29 20 67 72 65 61 74 65 72 20 74 68 61 6e ys) greater than
3db30 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 or equal.** to
3db40 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
3db50 20 69 4c 69 6d 69 74 20 70 61 72 61 6d 65 74 65 iLimit paramete
3db60 72 20 70 61 73 73 65 64 20 74 6f 20 78 54 72 75 r passed to xTru
3db70 6e 63 61 74 65 28 29 2e 20 49 66 20 61 6e 79 0a ncate(). If any.
3db80 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61 67 65 ** of these page
3db90 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20 74 68 s are pinned, th
3dba0 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c ey are implicitl
3dbb0 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e y unpinned, mean
3dbc0 69 6e 67 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 ing that.** they
3dbd0 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20 64 can be safely d
3dbe0 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 iscarded..**.**
3dbf0 54 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d The xDestroy() m
3dc00 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f ethod is used to
3dc10 20 64 65 6c 65 74 65 20 61 20 63 61 63 68 65 20 delete a cache
3dc20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 78 43 72 allocated by xCr
3dc30 65 61 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 eate()..** All r
3dc40 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 esources associa
3dc50 74 65 64 20 77 69 74 68 20 74 68 65 20 73 70 65 ted with the spe
3dc60 63 69 66 69 65 64 20 63 61 63 68 65 20 73 68 6f cified cache sho
3dc70 75 6c 64 20 62 65 20 66 72 65 65 64 2e 20 41 66 uld be freed. Af
3dc80 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 ter.** calling t
3dc90 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65 he xDestroy() me
3dca0 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63 6f 6e thod, SQLite con
3dcb0 73 69 64 65 72 73 20 74 68 65 20 5b 73 71 6c 69 siders the [sqli
3dcc0 74 65 33 5f 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 te3_pcache*].**
3dcd0 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64 2c 20 handle invalid,
3dce0 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75 73 65 and will not use
3dcf0 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f 74 68 it with any oth
3dd00 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 er sqlite3_pcach
3dd10 65 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 66 75 6e e_methods.** fun
3dd20 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 ctions..*/.typed
3dd30 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
3dd40 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3_pcache_methods
3dd50 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f sqlite3_pcache_
3dd60 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 methods;.struct
3dd70 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
3dd80 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 ethods {. void
3dd90 2a 70 41 72 67 3b 0a 20 20 69 6e 74 20 28 2a 78 *pArg;. int (*x
3dda0 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 0a 20 20 Init)(void*);.
3ddb0 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e void (*xShutdown
3ddc0 29 28 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 )(void*);. sqli
3ddd0 74 65 33 5f 70 63 61 63 68 65 20 2a 28 2a 78 43 te3_pcache *(*xC
3dde0 72 65 61 74 65 29 28 69 6e 74 20 73 7a 50 61 67 reate)(int szPag
3ddf0 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c e, int bPurgeabl
3de00 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 e);. void (*xCa
3de10 63 68 65 73 69 7a 65 29 28 73 71 6c 69 74 65 33 chesize)(sqlite3
3de20 5f 70 63 61 63 68 65 2a 2c 20 69 6e 74 20 6e 43 _pcache*, int nC
3de30 61 63 68 65 73 69 7a 65 29 3b 0a 20 20 69 6e 74 achesize);. int
3de40 20 28 2a 78 50 61 67 65 63 6f 75 6e 74 29 28 73 (*xPagecount)(s
3de50 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b qlite3_pcache*);
3de60 0a 20 20 76 6f 69 64 20 2a 28 2a 78 46 65 74 63 . void *(*xFetc
3de70 68 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 h)(sqlite3_pcach
3de80 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6b 65 79 e*, unsigned key
3de90 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 , int createFlag
3dea0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 70 );. void (*xUnp
3deb0 69 6e 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 in)(sqlite3_pcac
3dec0 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 he*, void*, int
3ded0 64 69 73 63 61 72 64 29 3b 0a 20 20 76 6f 69 64 discard);. void
3dee0 20 28 2a 78 52 65 6b 65 79 29 28 73 71 6c 69 74 (*xRekey)(sqlit
3def0 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 e3_pcache*, void
3df00 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b *, unsigned oldK
3df10 65 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e 65 77 ey, unsigned new
3df20 4b 65 79 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 Key);. void (*x
3df30 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 Truncate)(sqlite
3df40 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 3_pcache*, unsig
3df50 6e 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20 20 76 ned iLimit);. v
3df60 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 oid (*xDestroy)(
3df70 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 sqlite3_pcache*)
3df80 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ;.};../*.** CAPI
3df90 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 3REF: Online Bac
3dfa0 6b 75 70 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 kup Object.** EX
3dfb0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
3dfc0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 The sqlite3_bac
3dfd0 6b 75 70 20 6f 62 6a 65 63 74 20 72 65 63 6f 72 kup object recor
3dfe0 64 73 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 ds state informa
3dff0 74 69 6f 6e 20 61 62 6f 75 74 20 61 6e 20 6f 6e tion about an on
3e000 67 6f 69 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 going.** online
3e010 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
3e020 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 . The sqlite3_b
3e030 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 69 73 20 ackup object is
3e040 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 created by.** a
3e050 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
3e060 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20 _backup_init()]
3e070 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79 65 64 and is destroyed
3e080 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a by a call to.**
3e090 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 [sqlite3_backup
3e0a0 5f 66 69 6e 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a _finish()]..**.*
3e0b0 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 * See Also: [Usi
3e0c0 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e ng the SQLite On
3e0d0 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d line Backup API]
3e0e0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
3e0f0 63 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ct sqlite3_backu
3e100 70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 p sqlite3_backup
3e110 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
3e120 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 F: Online Backup
3e130 20 41 50 49 2e 0a 2a 2a 20 45 58 50 45 52 49 4d API..** EXPERIM
3e140 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ENTAL.**.** This
3e150 20 41 50 49 20 69 73 20 75 73 65 64 20 74 6f 20 API is used to
3e160 6f 76 65 72 77 72 69 74 65 20 74 68 65 20 63 6f overwrite the co
3e170 6e 74 65 6e 74 73 20 6f 66 20 6f 6e 65 20 64 61 ntents of one da
3e180 74 61 62 61 73 65 20 77 69 74 68 20 74 68 61 74 tabase with that
3e190 0a 2a 2a 20 6f 66 20 61 6e 6f 74 68 65 72 2e 20 .** of another.
3e1a0 49 74 20 69 73 20 75 73 65 66 75 6c 20 65 69 74 It is useful eit
3e1b0 68 65 72 20 66 6f 72 20 63 72 65 61 74 69 6e 67 her for creating
3e1c0 20 62 61 63 6b 75 70 73 20 6f 66 20 64 61 74 61 backups of data
3e1d0 62 61 73 65 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 bases or.** for
3e1e0 63 6f 70 79 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 copying in-memor
3e1f0 79 20 64 61 74 61 62 61 73 65 73 20 74 6f 20 6f y databases to o
3e200 72 20 66 72 6f 6d 20 70 65 72 73 69 73 74 65 6e r from persisten
3e210 74 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 t files. .**.**
3e220 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 See Also: [Using
3e230 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 the SQLite Onli
3e240 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a ne Backup API].*
3e250 2a 0a 2a 2a 20 45 78 63 6c 75 73 69 76 65 20 61 *.** Exclusive a
3e260 63 63 65 73 73 20 69 73 20 72 65 71 75 69 72 65 ccess is require
3e270 64 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 d to the destina
3e280 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 66 6f tion database fo
3e290 72 20 74 68 65 20 0a 2a 2a 20 64 75 72 61 74 69 r the .** durati
3e2a0 6f 6e 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 on of the operat
3e2b0 69 6f 6e 2e 20 48 6f 77 65 76 65 72 20 74 68 65 ion. However the
3e2c0 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
3e2d0 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 is only.** read
3e2e0 2d 6c 6f 63 6b 65 64 20 77 68 69 6c 65 20 69 74 -locked while it
3e2f0 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 is actually bei
3e300 6e 67 20 72 65 61 64 2c 20 69 74 20 69 73 20 6e ng read, it is n
3e310 6f 74 20 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e ot locked.** con
3e320 74 69 6e 75 6f 75 73 6c 79 20 66 6f 72 20 74 68 tinuously for th
3e330 65 20 65 6e 74 69 72 65 20 6f 70 65 72 61 74 69 e entire operati
3e340 6f 6e 2e 20 54 68 75 73 2c 20 74 68 65 20 62 61 on. Thus, the ba
3e350 63 6b 75 70 20 6d 61 79 20 62 65 0a 2a 2a 20 70 ckup may be.** p
3e360 65 72 66 6f 72 6d 65 64 20 6f 6e 20 61 20 6c 69 erformed on a li
3e370 76 65 20 64 61 74 61 62 61 73 65 20 77 69 74 68 ve database with
3e380 6f 75 74 20 70 72 65 76 65 6e 74 69 6e 67 20 6f out preventing o
3e390 74 68 65 72 20 75 73 65 72 73 20 66 72 6f 6d 0a ther users from.
3e3a0 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 ** writing to th
3e3b0 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 61 e database for a
3e3c0 6e 20 65 78 74 65 6e 64 65 64 20 70 65 72 69 6f n extended perio
3e3d0 64 20 6f 66 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a d of time..** .*
3e3e0 2a 20 54 6f 20 70 65 72 66 6f 72 6d 20 61 20 62 * To perform a b
3e3f0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 3a ackup operation:
3e400 20 0a 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 .** <ol>.**
3e410 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 <li><b>sqlite
3e420 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 3_backup_init()<
3e430 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e /b> is called on
3e440 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 ce to initialize
3e450 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
3e460 62 61 63 6b 75 70 2c 20 0a 2a 2a 20 20 20 20 20 backup, .**
3e470 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 <li><b>sqlite3_b
3e480 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e ackup_step()</b>
3e490 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f is called one o
3e4a0 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 r more times to
3e4b0 74 72 61 6e 73 66 65 72 20 0a 2a 2a 20 20 20 20 transfer .**
3e4c0 20 20 20 20 20 74 68 65 20 64 61 74 61 20 62 65 the data be
3e4d0 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 64 61 tween the two da
3e4e0 74 61 62 61 73 65 73 2c 20 61 6e 64 20 66 69 6e tabases, and fin
3e4f0 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e ally.** <li>
3e500 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 <b>sqlite3_backu
3e510 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 20 69 p_finish()</b> i
3e520 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 6c 65 s called to rele
3e530 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 ase all resource
3e540 73 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 73 s .** as
3e550 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
3e560 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 e backup operati
3e570 6f 6e 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a on. .** </ol>.
3e580 2a 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 ** There should
3e590 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 be exactly one c
3e5a0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 all to sqlite3_b
3e5b0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66 ackup_finish() f
3e5c0 6f 72 20 65 61 63 68 0a 2a 2a 20 73 75 63 63 65 or each.** succe
3e5d0 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 73 71 ssful call to sq
3e5e0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 lite3_backup_ini
3e5f0 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 t()..**.** <b>sq
3e600 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 lite3_backup_ini
3e610 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 t()</b>.**.** Th
3e620 65 20 66 69 72 73 74 20 74 77 6f 20 61 72 67 75 e first two argu
3e630 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 ments passed to
3e640 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f [sqlite3_backup_
3e650 69 6e 69 74 28 29 5d 20 61 72 65 20 74 68 65 20 init()] are the
3e660 64 61 74 61 62 61 73 65 0a 2a 2a 20 68 61 6e 64 database.** hand
3e670 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 le associated wi
3e680 74 68 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 th the destinati
3e690 6f 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 on database and
3e6a0 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
3e6b0 65 20 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 74 e .** used to at
3e6c0 74 61 63 68 20 74 68 65 20 64 65 73 74 69 6e 61 tach the destina
3e6d0 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 74 6f tion database to
3e6e0 20 74 68 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 the handle. The
3e6f0 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a database name.*
3e700 2a 20 69 73 20 22 6d 61 69 6e 22 20 66 6f 72 20 * is "main" for
3e710 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
3e720 65 2c 20 22 74 65 6d 70 22 20 66 6f 72 20 74 68 e, "temp" for th
3e730 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 e temporary data
3e740 62 61 73 65 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 base, or.** the
3e750 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64 20 61 name specified a
3e760 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 5b 41 s part of the [A
3e770 54 54 41 43 48 5d 20 73 74 61 74 65 6d 65 6e 74 TTACH] statement
3e780 20 69 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 if the destinat
3e790 69 6f 6e 20 69 73 0a 2a 2a 20 61 6e 20 61 74 74 ion is.** an att
3e7a0 61 63 68 65 64 20 64 61 74 61 62 61 73 65 2e 20 ached database.
3e7b0 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f The third and fo
3e7c0 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 70 urth arguments p
3e7d0 61 73 73 65 64 20 74 6f 20 0a 2a 2a 20 73 71 6c assed to .** sql
3e7e0 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 ite3_backup_init
3e7f0 28 29 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 () identify the
3e800 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
3e810 74 69 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 tion].** and dat
3e820 61 62 61 73 65 20 6e 61 6d 65 20 75 73 65 64 0a abase name used.
3e830 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ** to access the
3e840 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
3e850 2e 20 54 68 65 20 76 61 6c 75 65 73 20 70 61 73 . The values pas
3e860 73 65 64 20 66 6f 72 20 74 68 65 20 73 6f 75 72 sed for the sour
3e870 63 65 20 61 6e 64 20 0a 2a 2a 20 64 65 73 74 69 ce and .** desti
3e880 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 nation [database
3e890 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 61 72 connection] par
3e8a0 61 6d 65 74 65 72 73 20 6d 75 73 74 20 6e 6f 74 ameters must not
3e8b0 20 62 65 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a be the same..**
3e8c0 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
3e8d0 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 73 71 occurs within sq
3e8e0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 lite3_backup_ini
3e8f0 74 28 29 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 t(), then NULL i
3e900 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e s returned.** an
3e910 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 d an error code
3e920 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 and error messag
3e930 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
3e940 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
3e950 6e 65 63 74 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 nection] .** pas
3e960 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
3e970 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 79 20 argument. They
3e980 6d 61 79 20 62 65 20 72 65 74 72 69 65 76 65 64 may be retrieved
3e990 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 using the.** [s
3e9a0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
3e9b0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d ], [sqlite3_errm
3e9c0 73 67 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 sg()], and [sqli
3e9d0 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 te3_errmsg16()]
3e9e0 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 functions..** Ot
3e9f0 68 65 72 77 69 73 65 2c 20 69 66 20 73 75 63 63 herwise, if succ
3ea00 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e 74 65 essful, a pointe
3ea10 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74 65 33 r to an [sqlite3
3ea20 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 _backup] object
3ea30 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 is.** returned.
3ea40 54 68 69 73 20 70 6f 69 6e 74 65 72 20 6d 61 79 This pointer may
3ea50 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 be used with th
3ea60 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 e sqlite3_backup
3ea70 5f 73 74 65 70 28 29 20 61 6e 64 0a 2a 2a 20 73 _step() and.** s
3ea80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
3ea90 6e 69 73 68 28 29 20 66 75 6e 63 74 69 6f 6e 73 nish() functions
3eaa0 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20 to perform the
3eab0 73 70 65 63 69 66 69 65 64 20 62 61 63 6b 75 70 specified backup
3eac0 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 0a .** operation..
3ead0 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 **.** <b>sqlite3
3eae0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f _backup_step()</
3eaf0 62 3e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f b>.**.** Functio
3eb00 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 n [sqlite3_backu
3eb10 70 5f 73 74 65 70 28 29 5d 20 69 73 20 75 73 65 p_step()] is use
3eb20 64 20 74 6f 20 63 6f 70 79 20 75 70 20 74 6f 20 d to copy up to
3eb30 6e 50 61 67 65 20 70 61 67 65 73 20 62 65 74 77 nPage pages betw
3eb40 65 65 6e 20 0a 2a 2a 20 74 68 65 20 73 6f 75 72 een .** the sour
3eb50 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 ce and destinati
3eb60 6f 6e 20 64 61 74 61 62 61 73 65 73 2c 20 77 68 on databases, wh
3eb70 65 72 65 20 6e 50 61 67 65 20 69 73 20 74 68 65 ere nPage is the
3eb80 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 0a 2a value of the .*
3eb90 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 * second paramet
3eba0 65 72 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c er passed to sql
3ebb0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
3ebc0 28 29 2e 20 49 66 20 6e 50 61 67 65 20 69 73 20 (). If nPage is
3ebd0 61 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 76 61 a negative.** va
3ebe0 6c 75 65 2c 20 61 6c 6c 20 72 65 6d 61 69 6e 69 lue, all remaini
3ebf0 6e 67 20 73 6f 75 72 63 65 20 70 61 67 65 73 20 ng source pages
3ec00 61 72 65 20 63 6f 70 69 65 64 2e 20 49 66 20 74 are copied. If t
3ec10 68 65 20 72 65 71 75 69 72 65 64 20 70 61 67 65 he required page
3ec20 73 20 61 72 65 20 0a 2a 2a 20 73 75 63 63 65 73 s are .** succes
3ec30 66 75 6c 6c 79 20 63 6f 70 69 65 64 2c 20 62 75 fully copied, bu
3ec40 74 20 74 68 65 72 65 20 61 72 65 20 73 74 69 6c t there are stil
3ec50 6c 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 20 l more pages to
3ec60 63 6f 70 79 20 62 65 66 6f 72 65 20 74 68 65 20 copy before the
3ec70 0a 2a 2a 20 62 61 63 6b 75 70 20 69 73 20 63 6f .** backup is co
3ec80 6d 70 6c 65 74 65 2c 20 69 74 20 72 65 74 75 72 mplete, it retur
3ec90 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 ns [SQLITE_OK].
3eca0 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 If no error occu
3ecb0 72 65 64 20 61 6e 64 20 74 68 65 72 65 20 0a 2a red and there .*
3ecc0 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61 * are no more pa
3ecd0 67 65 73 20 74 6f 20 63 6f 70 79 2c 20 74 68 65 ges to copy, the
3ece0 6e 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 n [SQLITE_DONE]
3ecf0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
3ed00 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 an error .** occ
3ed10 75 72 73 2c 20 74 68 65 6e 20 61 6e 20 53 51 4c urs, then an SQL
3ed20 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ite error code i
3ed30 73 20 72 65 74 75 72 6e 65 64 2e 20 41 73 20 77 s returned. As w
3ed40 65 6c 6c 20 61 73 20 5b 53 51 4c 49 54 45 5f 4f ell as [SQLITE_O
3ed50 4b 5d 20 61 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 K] and.** [SQLIT
3ed60 45 5f 44 4f 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 E_DONE], a call
3ed70 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
3ed80 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 65 74 p_step() may ret
3ed90 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 urn [SQLITE_READ
3eda0 4f 4e 4c 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 ONLY],.** [SQLIT
3edb0 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 E_NOMEM], [SQLIT
3edc0 45 5f 42 55 53 59 5d 2c 20 5b 53 51 4c 49 54 45 E_BUSY], [SQLITE
3edd0 5f 4c 4f 43 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a _LOCKED], or an.
3ede0 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 ** [SQLITE_IOERR
3edf0 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 _ACCESS | SQLITE
3ee00 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 78 74 65 _IOERR_XXX] exte
3ee10 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 2e nded error code.
3ee20 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 .**.** As well a
3ee30 73 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65 s the case where
3ee40 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
3ee50 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
3ee60 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a as opened for.**
3ee70 20 72 65 61 64 2d 6f 6e 6c 79 20 61 63 63 65 73 read-only acces
3ee80 73 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 s, sqlite3_backu
3ee90 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 65 74 p_step() may ret
3eea0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 urn [SQLITE_READ
3eeb0 4f 4e 4c 59 5d 20 69 66 0a 2a 2a 20 74 68 65 20 ONLY] if.** the
3eec0 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 destination is a
3eed0 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
3eee0 62 61 73 65 20 77 69 74 68 20 61 20 64 69 66 66 base with a diff
3eef0 65 72 65 6e 74 20 70 61 67 65 20 73 69 7a 65 0a erent page size.
3ef00 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 ** from the sour
3ef10 63 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a ce database..**.
3ef20 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 62 61 ** If sqlite3_ba
3ef30 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 6e ckup_step() cann
3ef40 6f 74 20 6f 62 74 61 69 6e 20 61 20 72 65 71 75 ot obtain a requ
3ef50 69 72 65 64 20 66 69 6c 65 2d 73 79 73 74 65 6d ired file-system
3ef60 20 6c 6f 63 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 lock, then.** t
3ef70 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 he [sqlite3_busy
3ef80 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 2d _handler | busy-
3ef90 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e handler function
3efa0 5d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20 ].** is invoked
3efb0 28 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 (if one is speci
3efc0 66 69 65 64 29 2e 20 49 66 20 74 68 65 20 0a 2a fied). If the .*
3efd0 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 72 * busy-handler r
3efe0 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 eturns non-zero
3eff0 62 65 66 6f 72 65 20 74 68 65 20 6c 6f 63 6b 20 before the lock
3f000 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 is available, th
3f010 65 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 en .** [SQLITE_B
3f020 55 53 59 5d 20 69 73 20 72 65 74 75 72 6e 65 64 USY] is returned
3f030 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 to the caller.
3f040 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
3f050 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 call to.** sqli
3f060 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
3f070 29 20 63 61 6e 20 62 65 20 72 65 74 72 69 65 64 ) can be retried
3f080 20 6c 61 74 65 72 2e 20 49 66 20 74 68 65 20 73 later. If the s
3f090 6f 75 72 63 65 0a 2a 2a 20 5b 64 61 74 61 62 61 ource.** [databa
3f0a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a se connection].*
3f0b0 2a 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 * is being used
3f0c0 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 to write to the
3f0d0 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 source database
3f0e0 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 when sqlite3_bac
3f0f0 6b 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20 69 73 kup_step().** is
3f100 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 5b 53 called, then [S
3f110 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 QLITE_LOCKED] is
3f120 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 returned immedi
3f130 61 74 65 6c 79 2e 20 41 67 61 69 6e 2c 20 69 6e ately. Again, in
3f140 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 this.** case th
3f150 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
3f160 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
3f170 63 61 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c can be retried l
3f180 61 74 65 72 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b ater on. If.** [
3f190 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 SQLITE_IOERR_ACC
3f1a0 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 ESS | SQLITE_IOE
3f1b0 52 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45 RR_XXX], [SQLITE
3f1c0 5f 4e 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b _NOMEM], or.** [
3f1d0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d SQLITE_READONLY]
3f1e0 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 is returned, th
3f1f0 65 6e 20 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 en .** there is
3f200 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65 74 72 no point in retr
3f210 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f ying the call to
3f220 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
3f230 73 74 65 70 28 29 2e 20 54 68 65 73 65 20 0a 2a step(). These .*
3f240 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63 6f 6e * errors are con
3f250 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e 20 41 sidered fatal. A
3f260 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 t this point the
3f270 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 application mus
3f280 74 20 61 63 63 65 70 74 20 0a 2a 2a 20 74 68 61 t accept .** tha
3f290 74 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 t the backup ope
3f2a0 72 61 74 69 6f 6e 20 68 61 73 20 66 61 69 6c 65 ration has faile
3f2b0 64 20 61 6e 64 20 70 61 73 73 20 74 68 65 20 62 d and pass the b
3f2c0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 ackup operation
3f2d0 68 61 6e 64 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 handle .** to th
3f2e0 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 e sqlite3_backup
3f2f0 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 72 65 6c _finish() to rel
3f300 65 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 20 ease associated
3f310 72 65 73 6f 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a resources..**.**
3f320 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 66 Following the f
3f330 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c irst call to sql
3f340 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
3f350 28 29 2c 20 61 6e 20 65 78 63 6c 75 73 69 76 65 (), an exclusive
3f360 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 lock is.** obta
3f370 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 65 73 74 ined on the dest
3f380 69 6e 61 74 69 6f 6e 20 66 69 6c 65 2e 20 49 74 ination file. It
3f390 20 69 73 20 6e 6f 74 20 72 65 6c 65 61 73 65 64 is not released
3f3a0 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 0a 2a until either .*
3f3b0 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 * sqlite3_backup
3f3c0 5f 66 69 6e 69 73 68 28 29 20 69 73 20 63 61 6c _finish() is cal
3f3d0 6c 65 64 20 6f 72 20 74 68 65 20 62 61 63 6b 75 led or the backu
3f3e0 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 63 p operation is c
3f3f0 6f 6d 70 6c 65 74 65 20 0a 2a 2a 20 61 6e 64 20 omplete .** and
3f400 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
3f410 74 65 70 28 29 20 72 65 74 75 72 6e 73 20 5b 53 tep() returns [S
3f420 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 QLITE_DONE]. Add
3f430 69 74 69 6f 6e 61 6c 6c 79 2c 20 65 61 63 68 20 itionally, each
3f440 74 69 6d 65 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 time .** a call
3f450 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
3f460 70 5f 73 74 65 70 28 29 20 69 73 20 6d 61 64 65 p_step() is made
3f470 20 61 20 5b 73 68 61 72 65 64 20 6c 6f 63 6b 5d a [shared lock]
3f480 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 0a is obtained on.
3f490 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 ** the source da
3f4a0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 tabase file. Thi
3f4b0 73 20 6c 6f 63 6b 20 69 73 20 72 65 6c 65 61 73 s lock is releas
3f4c0 65 64 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a ed before the.**
3f4d0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
3f4e0 73 74 65 70 28 29 20 63 61 6c 6c 20 72 65 74 75 step() call retu
3f4f0 72 6e 73 2e 20 42 65 63 61 75 73 65 20 74 68 65 rns. Because the
3f500 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
3f510 20 69 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 is not.** locke
3f520 64 20 62 65 74 77 65 65 6e 20 63 61 6c 6c 73 20 d between calls
3f530 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
3f540 70 5f 73 74 65 70 28 29 2c 20 69 74 20 6d 61 79 p_step(), it may
3f550 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 be modified mid
3f560 2d 77 61 79 0a 2a 2a 20 74 68 72 6f 75 67 68 20 -way.** through
3f570 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 the backup proce
3f580 64 75 72 65 2e 20 49 66 20 74 68 65 20 73 6f 75 dure. If the sou
3f590 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
3f5a0 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a modified by an.*
3f5b0 2a 20 65 78 74 65 72 6e 61 6c 20 70 72 6f 63 65 * external proce
3f5c0 73 73 20 6f 72 20 76 69 61 20 61 20 64 61 74 61 ss or via a data
3f5d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
3f5e0 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f other than the o
3f5f0 6e 65 20 62 65 69 6e 67 0a 2a 2a 20 75 73 65 64 ne being.** used
3f600 20 62 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f by the backup o
3f610 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 peration, then t
3f620 68 65 20 62 61 63 6b 75 70 20 77 69 6c 6c 20 62 he backup will b
3f630 65 20 74 72 61 6e 73 70 61 72 65 6e 74 6c 79 0a e transparently.
3f640 2a 2a 20 72 65 73 74 61 72 74 65 64 20 62 79 20 ** restarted by
3f650 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f the next call to
3f660 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
3f670 73 74 65 70 28 29 2e 20 49 66 20 74 68 65 20 73 step(). If the s
3f680 6f 75 72 63 65 20 0a 2a 2a 20 64 61 74 61 62 61 ource .** databa
3f690 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 se is modified b
3f6a0 79 20 74 68 65 20 75 73 69 6e 67 20 74 68 65 20 y the using the
3f6b0 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
3f6c0 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73 20 75 nnection as is u
3f6d0 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 62 61 sed.** by the ba
3f6e0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 ckup operation,
3f6f0 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20 then the backup
3f700 64 61 74 61 62 61 73 65 20 69 73 20 74 72 61 6e database is tran
3f710 73 70 61 72 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 sparently .** up
3f720 64 61 74 65 64 20 61 74 20 74 68 65 20 73 61 6d dated at the sam
3f730 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 e time..**.** <b
3f740 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f >sqlite3_backup_
3f750 66 69 6e 69 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a finish()</b>.**.
3f760 2a 2a 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f ** Once sqlite3_
3f770 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 68 61 backup_step() ha
3f780 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 s returned [SQLI
3f790 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 TE_DONE], or whe
3f7a0 6e 20 74 68 65 20 0a 2a 2a 20 61 70 70 6c 69 63 n the .** applic
3f7b0 61 74 69 6f 6e 20 77 69 73 68 65 73 20 74 6f 20 ation wishes to
3f7c0 61 62 61 6e 64 6f 6e 20 74 68 65 20 62 61 63 6b abandon the back
3f7d0 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 up operation, th
3f7e0 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 e [sqlite3_backu
3f7f0 70 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 73 68 6f p].** object sho
3f800 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 74 6f uld be passed to
3f810 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
3f820 66 69 6e 69 73 68 28 29 2e 20 54 68 69 73 20 72 finish(). This r
3f830 65 6c 65 61 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 eleases all.** r
3f840 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 esources associa
3f850 74 65 64 20 77 69 74 68 20 74 68 65 20 62 61 63 ted with the bac
3f860 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 kup operation. I
3f870 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 f sqlite3_backup
3f880 5f 73 74 65 70 28 29 0a 2a 2a 20 68 61 73 20 6e _step().** has n
3f890 6f 74 20 79 65 74 20 72 65 74 75 72 6e 65 64 20 ot yet returned
3f8a0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 [SQLITE_DONE], t
3f8b0 68 65 6e 20 61 6e 79 20 61 63 74 69 76 65 20 77 hen any active w
3f8c0 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
3f8d0 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 on the.** desti
3f8e0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
3f8f0 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 is rolled back.
3f900 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 The [sqlite3_bac
3f910 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 20 69 kup] object is i
3f920 6e 76 61 6c 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 nvalid.** and ma
3f930 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 66 6f y not be used fo
3f940 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 llowing a call t
3f950 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 o sqlite3_backup
3f960 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a _finish()..**.**
3f970 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 The value retur
3f980 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 ned by sqlite3_b
3f990 61 63 6b 75 70 5f 66 69 6e 69 73 68 20 69 73 20 ackup_finish is
3f9a0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 6e [SQLITE_OK] if n
3f9b0 6f 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 o error.** occur
3f9c0 72 65 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20 red, regardless
3f9d0 6f 72 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f or whether or no
3f9e0 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 t sqlite3_backup
3f9f0 5f 73 74 65 70 28 29 20 77 61 73 20 63 61 6c 6c _step() was call
3fa00 65 64 0a 2a 2a 20 61 20 73 75 66 66 69 63 69 65 ed.** a sufficie
3fa10 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d nt number of tim
3fa20 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 es to complete t
3fa30 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 he backup operat
3fa40 69 6f 6e 2e 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 ion. Or, if.** a
3fa50 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 n out-of-memory
3fa60 63 6f 6e 64 69 74 69 6f 6e 20 6f 72 20 49 4f 20 condition or IO
3fa70 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 64 75 error occured du
3fa80 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 0a 2a ring a call to.*
3fa90 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 * sqlite3_backup
3faa0 5f 73 74 65 70 28 29 20 74 68 65 6e 20 5b 53 51 _step() then [SQ
3fab0 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 LITE_NOMEM] or a
3fac0 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 n.** [SQLITE_IOE
3fad0 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49 RR_ACCESS | SQLI
3fae0 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 72 TE_IOERR_XXX] er
3faf0 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20 72 ror code.** is r
3fb00 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 eturned. In this
3fb10 20 63 61 73 65 20 74 68 65 20 65 72 72 6f 72 20 case the error
3fb20 63 6f 64 65 20 61 6e 64 20 61 6e 20 65 72 72 6f code and an erro
3fb30 72 20 6d 65 73 73 61 67 65 20 61 72 65 0a 2a 2a r message are.**
3fb40 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
3fb50 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 destination [dat
3fb60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3fb70 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 74 75 72 ]..**.** A retur
3fb80 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42 55 53 n of [SQLITE_BUS
3fb90 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f Y] or [SQLITE_LO
3fba0 43 4b 45 44 5d 20 66 72 6f 6d 20 73 71 6c 69 74 CKED] from sqlit
3fbb0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
3fbc0 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 65 72 is.** not a per
3fbd0 6d 61 6e 65 6e 74 20 65 72 72 6f 72 20 61 6e 64 manent error and
3fbe0 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74 does not affect
3fbf0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
3fc00 65 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f e of.** sqlite3_
3fc10 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e backup_finish().
3fc20 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 .**.** <b>sqlite
3fc30 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 3_backup_remaini
3fc40 6e 67 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 61 ng(), sqlite3_ba
3fc50 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 ckup_pagecount()
3fc60 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 </b>.**.** Each
3fc70 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
3fc80 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 73 65 backup_step() se
3fc90 74 73 20 74 77 6f 20 76 61 6c 75 65 73 20 73 74 ts two values st
3fca0 6f 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a ored internally.
3fcb0 2a 2a 20 62 79 20 61 6e 20 5b 73 71 6c 69 74 65 ** by an [sqlite
3fcc0 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 3_backup] object
3fcd0 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 . The number of
3fce0 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f 20 62 pages still to b
3fcf0 65 20 62 61 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 e backed.** up,
3fd00 77 68 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 which may be que
3fd10 72 69 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f ried by sqlite3_
3fd20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 backup_remaining
3fd30 28 29 2c 20 61 6e 64 20 74 68 65 20 74 6f 74 61 (), and the tota
3fd40 6c 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 l.** number of p
3fd50 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f 75 72 ages in the sour
3fd60 63 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ce database file
3fd70 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 71 , which may be q
3fd80 75 65 72 69 65 64 20 62 79 0a 2a 2a 20 73 71 6c ueried by.** sql
3fd90 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 ite3_backup_page
3fda0 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 count()..**.** T
3fdb0 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e he values return
3fdc0 65 64 20 62 79 20 74 68 65 73 65 20 66 75 6e 63 ed by these func
3fdd0 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20 75 tions are only u
3fde0 70 64 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c pdated by.** sql
3fdf0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
3fe00 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 (). If the sourc
3fe10 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f e database is mo
3fe20 64 69 66 69 65 64 20 64 75 72 69 6e 67 20 61 20 dified during a
3fe30 62 61 63 6b 75 70 0a 2a 2a 20 6f 70 65 72 61 74 backup.** operat
3fe40 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 76 61 ion, then the va
3fe50 6c 75 65 73 20 61 72 65 20 6e 6f 74 20 75 70 64 lues are not upd
3fe60 61 74 65 64 20 74 6f 20 61 63 63 6f 75 6e 74 20 ated to account
3fe70 66 6f 72 20 61 6e 79 20 65 78 74 72 61 0a 2a 2a for any extra.**
3fe80 20 70 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 pages that need
3fe90 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 6f to be updated o
3fea0 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 r the size of th
3feb0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
3fec0 65 20 66 69 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 e file.** changi
3fed0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e ng..**.** <b>Con
3fee0 63 75 72 72 65 6e 74 20 55 73 61 67 65 20 6f 66 current Usage of
3fef0 20 44 61 74 61 62 61 73 65 20 48 61 6e 64 6c 65 Database Handle
3ff00 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 s</b>.**.** The
3ff10 73 6f 75 72 63 65 20 5b 64 61 74 61 62 61 73 65 source [database
3ff20 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 connection] may
3ff30 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20 be used by the
3ff40 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 6f 72 20 application for
3ff50 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65 other.** purpose
3ff60 73 20 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70 s while a backup
3ff70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 6e operation is un
3ff80 64 65 72 77 61 79 20 6f 72 20 62 65 69 6e 67 20 derway or being
3ff90 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 initialized..**
3ffa0 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d If SQLite is com
3ffb0 70 69 6c 65 64 20 61 6e 64 20 63 6f 6e 66 69 67 piled and config
3ffc0 75 72 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 ured to support
3ffd0 74 68 72 65 61 64 73 61 66 65 20 64 61 74 61 62 threadsafe datab
3ffe0 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f ase.** connectio
3fff0 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 6f 75 ns, then the sou
40000 72 63 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e rce database con
40010 6e 65 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 nection may be u
40020 73 65 64 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 sed concurrently
40030 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 .** from within
40040 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e 0a 2a other threads..*
40050 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 *.** However, th
40060 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 e application mu
40070 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 st guarantee tha
40080 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f t the destinatio
40090 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f n database.** co
400a0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 nnection handle
400b0 69 73 20 6e 6f 74 20 70 61 73 73 65 64 20 74 6f is not passed to
400c0 20 61 6e 79 20 6f 74 68 65 72 20 41 50 49 20 28 any other API (
400d0 62 79 20 61 6e 79 20 74 68 72 65 61 64 29 20 61 by any thread) a
400e0 66 74 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 fter .** sqlite3
400f0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 _backup_init() i
40100 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 62 65 66 s called and bef
40110 6f 72 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f ore the correspo
40120 6e 64 69 6e 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a nding call to.**
40130 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
40140 66 69 6e 69 73 68 28 29 2e 20 55 6e 66 6f 72 74 finish(). Unfort
40150 75 6e 61 74 65 6c 79 20 53 51 4c 69 74 65 20 64 unately SQLite d
40160 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c oes not currentl
40170 79 20 63 68 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 y check.** for t
40180 68 69 73 2c 20 69 66 20 74 68 65 20 61 70 70 6c his, if the appl
40190 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 75 73 65 ication does use
401a0 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
401b0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
401c0 63 74 69 6f 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f ction].** for so
401d0 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 me other purpose
401e0 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 during a backup
401f0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 6e operation, thin
40200 67 73 20 6d 61 79 20 61 70 70 65 61 72 20 74 6f gs may appear to
40210 0a 2a 2a 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 .** work correct
40220 6c 79 20 62 75 74 20 69 6e 20 66 61 63 74 20 62 ly but in fact b
40230 65 20 73 75 62 74 6c 79 20 6d 61 6c 66 75 6e 63 e subtly malfunc
40240 74 69 6f 6e 69 6e 67 2e 20 20 55 73 65 20 6f 66 tioning. Use of
40250 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 the.** destinat
40260 69 6f 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e ion database con
40270 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 nection while a
40280 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70 72 6f backup is in pro
40290 67 72 65 73 73 20 6d 69 67 68 74 0a 2a 2a 20 61 gress might.** a
402a0 6c 73 6f 20 63 61 75 73 65 20 61 20 6d 75 74 65 lso cause a mute
402b0 78 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a x deadlock..**.*
402c0 2a 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 69 * Furthermore, i
402d0 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 f running in [sh
402e0 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d ared cache mode]
402f0 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f , the applicatio
40300 6e 20 6d 75 73 74 0a 2a 2a 20 67 75 61 72 61 6e n must.** guaran
40310 74 65 65 20 74 68 61 74 20 74 68 65 20 73 68 61 tee that the sha
40320 72 65 64 20 63 61 63 68 65 20 75 73 65 64 20 62 red cache used b
40330 79 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f y the destinatio
40340 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 n database.** is
40350 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 77 68 not accessed wh
40360 69 6c 65 20 74 68 65 20 62 61 63 6b 75 70 20 69 ile the backup i
40370 73 20 72 75 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 s running. In pr
40380 61 63 74 69 63 65 20 74 68 69 73 20 6d 65 61 6e actice this mean
40390 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 61 70 s.** that the ap
403a0 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 plication must g
403b0 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 uarantee that th
403c0 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 e file-system fi
403d0 6c 65 20 62 65 69 6e 67 20 0a 2a 2a 20 62 61 63 le being .** bac
403e0 6b 65 64 20 75 70 20 74 6f 20 69 73 20 6e 6f 74 ked up to is not
403f0 20 61 63 63 65 73 73 65 64 20 62 79 20 61 6e 79 accessed by any
40400 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 connection with
40410 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 2c 0a in the process,.
40420 2a 2a 20 6e 6f 74 20 6a 75 73 74 20 74 68 65 20 ** not just the
40430 73 70 65 63 69 66 69 63 20 63 6f 6e 6e 65 63 74 specific connect
40440 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 61 73 ion that was pas
40450 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 sed to sqlite3_b
40460 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a ackup_init()..**
40470 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
40480 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 _backup] object
40490 69 74 73 65 6c 66 20 69 73 20 70 61 72 74 69 61 itself is partia
404a0 6c 6c 79 20 74 68 72 65 61 64 73 61 66 65 2e 20 lly threadsafe.
404b0 4d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 68 72 Multiple .** thr
404c0 65 61 64 73 20 6d 61 79 20 73 61 66 65 6c 79 20 eads may safely
404d0 6d 61 6b 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f make multiple co
404e0 6e 63 75 72 72 65 6e 74 20 63 61 6c 6c 73 20 74 ncurrent calls t
404f0 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 o sqlite3_backup
40500 5f 73 74 65 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 _step()..** Howe
40510 76 65 72 2c 20 74 68 65 20 73 71 6c 69 74 65 33 ver, the sqlite3
40520 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e _backup_remainin
40530 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f g() and sqlite3_
40540 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 backup_pagecount
40550 28 29 0a 2a 2a 20 41 50 49 73 20 61 72 65 20 6e ().** APIs are n
40560 6f 74 20 73 74 72 69 63 74 6c 79 20 73 70 65 61 ot strictly spea
40570 6b 69 6e 67 20 74 68 72 65 61 64 73 61 66 65 2e king threadsafe.
40580 20 49 66 20 74 68 65 79 20 61 72 65 20 69 6e 76 If they are inv
40590 6f 6b 65 64 20 61 74 20 74 68 65 0a 2a 2a 20 73 oked at the.** s
405a0 61 6d 65 20 74 69 6d 65 20 61 73 20 61 6e 6f 74 ame time as anot
405b0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 69 6e her thread is in
405c0 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 62 voking sqlite3_b
405d0 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 74 20 ackup_step() it
405e0 69 73 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 is.** possible t
405f0 68 61 74 20 74 68 65 79 20 72 65 74 75 72 6e 20 hat they return
40600 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 73 2e 0a invalid values..
40610 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
40620 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 lite3_backup *sq
40630 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 lite3_backup_ini
40640 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 44 t(. sqlite3 *pD
40650 65 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 est,
40660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
40670 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
40680 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
40690 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 65 73 const char *zDes
406a0 74 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 tName,
406b0 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e /* Destin
406c0 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 6e ation database n
406d0 61 6d 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ame */. sqlite3
406e0 20 2a 70 53 6f 75 72 63 65 2c 20 20 20 20 20 20 *pSource,
406f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
40700 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 /* Source databa
40710 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 se handle */. c
40720 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 6f 75 72 onst char *zSour
40730 63 65 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 ceName
40740 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
40750 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f database name */
40760 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
40770 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 nt sqlite3_backu
40780 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 62 p_step(sqlite3_b
40790 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 ackup *p, int nP
407a0 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 age);.SQLITE_API
407b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 int sqlite3_bac
407c0 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c 69 74 kup_finish(sqlit
407d0 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 e3_backup *p);.S
407e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
407f0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d lite3_backup_rem
40800 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 aining(sqlite3_b
40810 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 ackup *p);.SQLIT
40820 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
40830 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 3_backup_pagecou
40840 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 nt(sqlite3_backu
40850 70 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 p *p);../*.** CA
40860 50 49 33 52 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e PI3REF: Unlock N
40870 6f 74 69 66 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 otification.** E
40880 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
40890 2a 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 * When running i
408a0 6e 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d n shared-cache m
408b0 6f 64 65 2c 20 61 20 64 61 74 61 62 61 73 65 20 ode, a database
408c0 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20 66 61 operation may fa
408d0 69 6c 20 77 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 il with.** an [S
408e0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 QLITE_LOCKED] er
408f0 72 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 69 ror if the requi
40900 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 red locks on the
40910 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6f 72 shared-cache or
40920 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 .** individual t
40930 61 62 6c 65 73 20 77 69 74 68 69 6e 20 74 68 65 ables within the
40940 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 61 shared-cache ca
40950 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 nnot be obtained
40960 2e 20 53 65 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 . See.** [SQLite
40970 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f Shared-Cache Mo
40980 64 65 5d 20 66 6f 72 20 61 20 64 65 73 63 72 69 de] for a descri
40990 70 74 69 6f 6e 20 6f 66 20 73 68 61 72 65 64 2d ption of shared-
409a0 63 61 63 68 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a cache locking. .
409b0 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61 79 20 ** This API may
409c0 62 65 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 be used to regis
409d0 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 ter a callback t
409e0 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 hat SQLite will
409f0 69 6e 76 6f 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 invoke .** when
40a00 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 the connection c
40a10 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 urrently holding
40a20 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f the required lo
40a30 63 6b 20 72 65 6c 69 6e 71 75 69 73 68 65 73 20 ck relinquishes
40a40 69 74 2e 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 it..** This API
40a50 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
40a60 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 e if the library
40a70 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 was compiled wi
40a80 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 th the.** [SQLIT
40a90 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f E_ENABLE_UNLOCK_
40aa0 4e 4f 54 49 46 59 5d 20 43 2d 70 72 65 70 72 6f NOTIFY] C-prepro
40ab0 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 cessor symbol de
40ac0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 fined..**.** See
40ad0 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 Also: [Using th
40ae0 65 20 53 51 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 e SQLite Unlock
40af0 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 46 65 61 Notification Fea
40b00 74 75 72 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 ture]..**.** Sha
40b10 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b 73 20 red-cache locks
40b20 61 72 65 20 72 65 6c 65 61 73 65 64 20 77 68 65 are released whe
40b30 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e n a database con
40b40 6e 65 63 74 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 nection conclude
40b50 73 0a 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 s.** its current
40b60 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 65 69 transaction, ei
40b70 74 68 65 72 20 62 79 20 63 6f 6d 6d 69 74 74 69 ther by committi
40b80 6e 67 20 69 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 ng it or rolling
40b90 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a it back. .**.**
40ba0 20 57 68 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69 When a connecti
40bb0 6f 6e 20 28 6b 6e 6f 77 6e 20 61 73 20 74 68 65 on (known as the
40bc0 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 blocked connect
40bd0 69 6f 6e 29 20 66 61 69 6c 73 20 74 6f 20 6f 62 ion) fails to ob
40be0 74 61 69 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 tain a.** shared
40bf0 2d 63 61 63 68 65 20 6c 6f 63 6b 20 61 6e 64 20 -cache lock and
40c00 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 SQLITE_LOCKED is
40c10 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
40c20 20 63 61 6c 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 caller, the.**
40c30 69 64 65 6e 74 69 74 79 20 6f 66 20 74 68 65 20 identity of the
40c40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
40c50 69 6f 6e 20 28 74 68 65 20 62 6c 6f 63 6b 69 6e ion (the blockin
40c60 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 g connection) th
40c70 61 74 0a 2a 2a 20 68 61 73 20 6c 6f 63 6b 65 64 at.** has locked
40c80 20 74 68 65 20 72 65 71 75 69 72 65 64 20 72 65 the required re
40c90 73 6f 75 72 63 65 20 69 73 20 73 74 6f 72 65 64 source is stored
40ca0 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 41 66 74 internally. Aft
40cb0 65 72 20 61 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 er an .** applic
40cc0 61 74 69 6f 6e 20 72 65 63 65 69 76 65 73 20 61 ation receives a
40cd0 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 n SQLITE_LOCKED
40ce0 65 72 72 6f 72 2c 20 69 74 20 6d 61 79 20 63 61 error, it may ca
40cf0 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 ll the.** sqlite
40d00 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 3_unlock_notify(
40d10 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20 74 68 ) method with th
40d20 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 e blocked connec
40d30 74 69 6f 6e 20 68 61 6e 64 6c 65 20 61 73 20 0a tion handle as .
40d40 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 67 ** the first arg
40d50 75 6d 65 6e 74 20 74 6f 20 72 65 67 69 73 74 65 ument to registe
40d60 72 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b r for a callback
40d70 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e that will be in
40d80 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 voked.** when th
40d90 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 e blocking conne
40da0 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 20 74 ctions current t
40db0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
40dc0 6e 63 6c 75 64 65 64 2e 20 54 68 65 0a 2a 2a 20 ncluded. The.**
40dd0 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f callback is invo
40de0 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 ked from within
40df0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 the [sqlite3_ste
40e00 70 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 p] or [sqlite3_c
40e10 6c 6f 73 65 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 lose].** call th
40e20 61 74 20 63 6f 6e 63 6c 75 64 65 73 20 74 68 65 at concludes the
40e30 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 blocking connec
40e40 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f tions transactio
40e50 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 n..**.** If sqli
40e60 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
40e70 79 28 29 20 69 73 20 63 61 6c 6c 65 64 20 69 6e y() is called in
40e80 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 a multi-threade
40e90 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a d application,.*
40ea0 2a 20 74 68 65 72 65 20 69 73 20 61 20 63 68 61 * there is a cha
40eb0 6e 63 65 20 74 68 61 74 20 74 68 65 20 62 6c 6f nce that the blo
40ec0 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e cking connection
40ed0 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 will have alrea
40ee0 64 79 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65 64 20 dy.** concluded
40ef0 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 its transaction
40f00 62 79 20 74 68 65 20 74 69 6d 65 20 73 71 6c 69 by the time sqli
40f10 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
40f20 79 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a y() is invoked..
40f30 2a 2a 20 49 66 20 74 68 69 73 20 68 61 70 70 65 ** If this happe
40f40 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 70 65 ns, then the spe
40f50 63 69 66 69 65 64 20 63 61 6c 6c 62 61 63 6b 20 cified callback
40f60 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 is invoked immed
40f70 69 61 74 65 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 iately,.** from
40f80 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 within the call
40f90 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 to sqlite3_unloc
40fa0 6b 5f 6e 6f 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a k_notify()..**.*
40fb0 2a 20 49 66 20 74 68 65 20 62 6c 6f 63 6b 65 64 * If the blocked
40fc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 connection is a
40fd0 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 ttempting to obt
40fe0 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ain a write-lock
40ff0 20 6f 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d on a.** shared-
41000 63 61 63 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 cache table, and
41010 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f more than one o
41020 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ther connection
41030 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 73 0a currently holds.
41040 2a 2a 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f ** a read-lock o
41050 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 n the same table
41060 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 72 , then SQLite ar
41070 62 69 74 72 61 72 69 6c 79 20 73 65 6c 65 63 74 bitrarily select
41080 73 20 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 s one of .** the
41090 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
410a0 6e 73 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 ns to use as the
410b0 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 blocking connec
410c0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 tion..**.** Ther
410d0 65 20 6d 61 79 20 62 65 20 61 74 20 6d 6f 73 74 e may be at most
410e0 20 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 one unlock-noti
410f0 66 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 67 69 fy callback regi
41100 73 74 65 72 65 64 20 62 79 20 61 20 0a 2a 2a 20 stered by a .**
41110 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 blocked connecti
41120 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75 on. If sqlite3_u
41130 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 nlock_notify() i
41140 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 s called when th
41150 65 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e e.** blocked con
41160 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 nection already
41170 68 61 73 20 61 20 72 65 67 69 73 74 65 72 65 64 has a registered
41180 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 unlock-notify c
41190 61 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e allback,.** then
411a0 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62 61 63 the new callbac
411b0 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f k replaces the o
411c0 6c 64 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75 ld. If sqlite3_u
411d0 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 nlock_notify() i
411e0 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 s.** called with
411f0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
41200 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 as its second ar
41210 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 61 6e 79 gument, then any
41220 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c existing.** unl
41230 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
41240 61 63 6b 20 69 73 20 63 61 6e 63 65 6c 6c 65 64 ack is cancelled
41250 2e 20 54 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f . The blocked co
41260 6e 6e 65 63 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e nnections .** un
41270 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
41280 62 61 63 6b 20 6d 61 79 20 61 6c 73 6f 20 62 65 back may also be
41290 20 63 61 6e 63 65 6c 65 64 20 62 79 20 63 6c 6f canceled by clo
412a0 73 69 6e 67 20 74 68 65 20 62 6c 6f 63 6b 65 64 sing the blocked
412b0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 .** connection u
412c0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c sing [sqlite3_cl
412d0 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 ose()]..**.** Th
412e0 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 e unlock-notify
412f0 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 callback is not
41300 72 65 65 6e 74 72 61 6e 74 2e 20 49 66 20 61 6e reentrant. If an
41310 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 application inv
41320 6f 6b 65 73 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 okes.** any sqli
41330 74 65 33 5f 78 78 78 20 41 50 49 20 66 75 6e 63 te3_xxx API func
41340 74 69 6f 6e 73 20 66 72 6f 6d 20 77 69 74 68 69 tions from withi
41350 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 n an unlock-noti
41360 66 79 20 63 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a fy callback, a.*
41370 2a 20 63 72 61 73 68 20 6f 72 20 64 65 61 64 6c * crash or deadl
41380 6f 63 6b 20 6d 61 79 20 62 65 20 74 68 65 20 72 ock may be the r
41390 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c esult..**.** Unl
413a0 65 73 73 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 ess deadlock is
413b0 64 65 74 65 63 74 65 64 20 28 73 65 65 20 62 65 detected (see be
413c0 6c 6f 77 29 2c 20 73 71 6c 69 74 65 33 5f 75 6e low), sqlite3_un
413d0 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 61 6c lock_notify() al
413e0 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 73 20 ways.** returns
413f0 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a SQLITE_OK..**.**
41400 20 3c 62 3e 43 61 6c 6c 62 61 63 6b 20 49 6e 76 <b>Callback Inv
41410 6f 63 61 74 69 6f 6e 20 44 65 74 61 69 6c 73 3c ocation Details<
41420 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 /b>.**.** When a
41430 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 n unlock-notify
41440 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69 callback is regi
41450 73 74 65 72 65 64 2c 20 74 68 65 20 61 70 70 6c stered, the appl
41460 69 63 61 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 ication provides
41470 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 76 6f a .** single vo
41480 69 64 2a 20 70 6f 69 6e 74 65 72 20 74 68 61 74 id* pointer that
41490 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 is passed to th
414a0 65 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 e callback when
414b0 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a it is invoked..*
414c0 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 73 * However, the s
414d0 69 67 6e 61 74 75 72 65 20 6f 66 20 74 68 65 20 ignature of the
414e0 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
414f0 6e 20 61 6c 6c 6f 77 73 20 53 51 4c 69 74 65 20 n allows SQLite
41500 74 6f 20 70 61 73 73 0a 2a 2a 20 69 74 20 61 6e to pass.** it an
41510 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 array of void*
41520 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 context pointers
41530 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 . The first argu
41540 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 0a 2a ment passed to.*
41550 2a 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 * an unlock-noti
41560 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 fy callback is a
41570 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 pointer to an a
41580 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 70 6f rray of void* po
41590 69 6e 74 65 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 inters,.** and t
415a0 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 he second is the
415b0 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 number of entri
415c0 65 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e es in the array.
415d0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 62 6c .**.** When a bl
415e0 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f ocking connectio
415f0 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ns transaction i
41600 73 20 63 6f 6e 63 6c 75 64 65 64 2c 20 74 68 65 s concluded, the
41610 72 65 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 re may be.** mor
41620 65 20 74 68 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b e than one block
41630 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 ed connection th
41640 61 74 20 68 61 73 20 72 65 67 69 73 74 65 72 65 at has registere
41650 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d d for an unlock-
41660 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 notify.** callba
41670 63 6b 2e 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f ck. If two or mo
41680 72 65 20 73 75 63 68 20 62 6c 6f 63 6b 65 64 20 re such blocked
41690 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 61 76 65 connections have
416a0 20 73 70 65 63 69 66 69 65 64 20 74 68 65 0a 2a specified the.*
416b0 2a 20 73 61 6d 65 20 63 61 6c 6c 62 61 63 6b 20 * same callback
416c0 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 69 function, then i
416d0 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 nstead of invoki
416e0 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 ng the callback
416f0 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 function.** mult
41700 69 70 6c 65 20 74 69 6d 65 73 2c 20 69 74 20 69 iple times, it i
41710 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 77 s invoked once w
41720 69 74 68 20 74 68 65 20 73 65 74 20 6f 66 20 76 ith the set of v
41730 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 oid* context poi
41740 6e 74 65 72 73 0a 2a 2a 20 73 70 65 63 69 66 69 nters.** specifi
41750 65 64 20 62 79 20 74 68 65 20 62 6c 6f 63 6b 65 ed by the blocke
41760 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 62 75 d connections bu
41770 6e 64 6c 65 64 20 74 6f 67 65 74 68 65 72 20 69 ndled together i
41780 6e 74 6f 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2a nto an array..**
41790 20 54 68 69 73 20 67 69 76 65 73 20 74 68 65 20 This gives the
417a0 61 70 70 6c 69 63 61 74 69 6f 6e 20 61 6e 20 6f application an o
417b0 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 70 72 pportunity to pr
417c0 69 6f 72 69 74 69 7a 65 20 61 6e 79 20 61 63 74 ioritize any act
417d0 69 6f 6e 73 20 0a 2a 2a 20 72 65 6c 61 74 65 64 ions .** related
417e0 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66 20 75 to the set of u
417f0 6e 62 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73 nblocked databas
41800 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a e connections..*
41810 2a 0a 2a 2a 20 3c 62 3e 44 65 61 64 6c 6f 63 6b *.** <b>Deadlock
41820 20 44 65 74 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a Detection</b>.*
41830 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 *.** Assuming th
41840 61 74 20 61 66 74 65 72 20 72 65 67 69 73 74 65 at after registe
41850 72 69 6e 67 20 66 6f 72 20 61 6e 20 75 6e 6c 6f ring for an unlo
41860 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 ck-notify callba
41870 63 6b 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 ck a .** databas
41880 65 20 77 61 69 74 73 20 66 6f 72 20 74 68 65 20 e waits for the
41890 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 callback to be i
418a0 73 73 75 65 64 20 62 65 66 6f 72 65 20 74 61 6b ssued before tak
418b0 69 6e 67 20 61 6e 79 20 66 75 72 74 68 65 72 0a ing any further.
418c0 2a 2a 20 61 63 74 69 6f 6e 20 28 61 20 72 65 61 ** action (a rea
418d0 73 6f 6e 61 62 6c 65 20 61 73 73 75 6d 70 74 69 sonable assumpti
418e0 6f 6e 29 2c 20 74 68 65 6e 20 75 73 69 6e 67 20 on), then using
418f0 74 68 69 73 20 41 50 49 20 6d 61 79 20 63 61 75 this API may cau
41900 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 se the.** applic
41910 61 74 69 6f 6e 20 74 6f 20 64 65 61 64 6c 6f 63 ation to deadloc
41920 6b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 k. For example,
41930 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 if connection X
41940 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 0a 2a is waiting for.*
41950 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 59 27 73 * connection Y's
41960 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 transaction to
41970 62 65 20 63 6f 6e 63 6c 75 64 65 64 2c 20 61 6e be concluded, an
41980 64 20 73 69 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e d similarly conn
41990 65 63 74 69 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 ection.** Y is w
419a0 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 aiting on connec
419b0 74 69 6f 6e 20 58 27 73 20 74 72 61 6e 73 61 63 tion X's transac
419c0 74 69 6f 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 tion, then neith
419d0 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a er connection.**
419e0 20 77 69 6c 6c 20 70 72 6f 63 65 65 64 20 61 6e will proceed an
419f0 64 20 74 68 65 20 73 79 73 74 65 6d 20 6d 61 79 d the system may
41a00 20 72 65 6d 61 69 6e 20 64 65 61 64 6c 6f 63 6b remain deadlock
41a10 65 64 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2e ed indefinitely.
41a20 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 .**.** To avoid
41a30 74 68 69 73 20 73 63 65 6e 61 72 69 6f 2c 20 74 this scenario, t
41a40 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 he sqlite3_unloc
41a50 6b 5f 6e 6f 74 69 66 79 28 29 20 70 65 72 66 6f k_notify() perfo
41a60 72 6d 73 20 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 rms deadlock.**
41a70 64 65 74 65 63 74 69 6f 6e 2e 20 49 66 20 61 20 detection. If a
41a80 67 69 76 65 6e 20 63 61 6c 6c 20 74 6f 20 73 71 given call to sq
41a90 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
41aa0 69 66 79 28 29 20 77 6f 75 6c 64 20 70 75 74 20 ify() would put
41ab0 74 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20 69 6e the.** system in
41ac0 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 a deadlocked st
41ad0 61 74 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 ate, then SQLITE
41ae0 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72 _LOCKED is retur
41af0 6e 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e ned and no.** un
41b00 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
41b10 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 72 back is register
41b20 65 64 2e 20 54 68 65 20 73 79 73 74 65 6d 20 69 ed. The system i
41b30 73 20 73 61 69 64 20 74 6f 20 62 65 20 69 6e 0a s said to be in.
41b40 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 ** a deadlocked
41b50 73 74 61 74 65 20 69 66 20 63 6f 6e 6e 65 63 74 state if connect
41b60 69 6f 6e 20 41 20 68 61 73 20 72 65 67 69 73 74 ion A has regist
41b70 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f ered for an unlo
41b80 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c ck-notify.** cal
41b90 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e lback on the con
41ba0 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 clusion of conne
41bb0 63 74 69 6f 6e 20 42 27 73 20 74 72 61 6e 73 61 ction B's transa
41bc0 63 74 69 6f 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 ction, and conne
41bd0 63 74 69 6f 6e 0a 2a 2a 20 42 20 68 61 73 20 69 ction.** B has i
41be0 74 73 65 6c 66 20 72 65 67 69 73 74 65 72 65 64 tself registered
41bf0 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e for an unlock-n
41c00 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 77 otify callback w
41c10 68 65 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a hen connection.*
41c20 2a 20 41 27 73 20 74 72 61 6e 73 61 63 74 69 6f * A's transactio
41c30 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 n is concluded.
41c40 49 6e 64 69 72 65 63 74 20 64 65 61 64 6c 6f 63 Indirect deadloc
41c50 6b 20 69 73 20 61 6c 73 6f 20 64 65 74 65 63 74 k is also detect
41c60 65 64 2c 20 73 6f 0a 2a 2a 20 74 68 65 20 73 79 ed, so.** the sy
41c70 73 74 65 6d 20 69 73 20 61 6c 73 6f 20 63 6f 6e stem is also con
41c80 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 64 65 sidered to be de
41c90 61 64 6c 6f 63 6b 65 64 20 69 66 20 63 6f 6e 6e adlocked if conn
41ca0 65 63 74 69 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 ection B has.**
41cb0 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 registered for a
41cc0 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 n unlock-notify
41cd0 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 callback on the
41ce0 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f conclusion of co
41cf0 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 nnection.** C's
41d00 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 77 68 65 transaction, whe
41d10 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 re connection C
41d20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f is waiting on co
41d30 6e 6e 65 63 74 69 6f 6e 20 41 2e 20 41 6e 79 0a nnection A. Any.
41d40 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 ** number of lev
41d50 65 6c 73 20 6f 66 20 69 6e 64 69 72 65 63 74 69 els of indirecti
41d60 6f 6e 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a on are allowed..
41d70 2a 2a 0a 2a 2a 20 3c 62 3e 54 68 65 20 22 44 52 **.** <b>The "DR
41d80 4f 50 20 54 41 42 4c 45 22 20 45 78 63 65 70 74 OP TABLE" Except
41d90 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 ion</b>.**.** Wh
41da0 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 en a call to [sq
41db0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65 lite3_step()] re
41dc0 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4c 4f 43 turns SQLITE_LOC
41dd0 4b 45 44 2c 20 69 74 20 69 73 20 61 6c 6d 6f 73 KED, it is almos
41de0 74 20 0a 2a 2a 20 61 6c 77 61 79 73 20 61 70 70 t .** always app
41df0 72 6f 70 72 69 61 74 65 20 74 6f 20 63 61 6c 6c ropriate to call
41e00 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f sqlite3_unlock_
41e10 6e 6f 74 69 66 79 28 29 2e 20 54 68 65 72 65 20 notify(). There
41e20 69 73 20 68 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f is however,.** o
41e30 6e 65 20 65 78 63 65 70 74 69 6f 6e 2e 20 57 68 ne exception. Wh
41e40 65 6e 20 65 78 65 63 75 74 69 6e 67 20 61 20 22 en executing a "
41e50 44 52 4f 50 20 54 41 42 4c 45 22 20 6f 72 20 22 DROP TABLE" or "
41e60 44 52 4f 50 20 49 4e 44 45 58 22 20 73 74 61 74 DROP INDEX" stat
41e70 65 6d 65 6e 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 ement,.** SQLite
41e80 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
41e90 20 61 72 65 20 61 6e 79 20 63 75 72 72 65 6e 74 are any current
41ea0 6c 79 20 65 78 65 63 75 74 69 6e 67 20 53 45 4c ly executing SEL
41eb0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a ECT statements.*
41ec0 2a 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74 6f * that belong to
41ed0 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 the same connec
41ee0 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 61 tion. If there a
41ef0 72 65 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 re, SQLITE_LOCKE
41f00 44 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 D is.** returned
41f10 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
41f20 68 65 72 65 20 69 73 20 6e 6f 20 22 62 6c 6f 63 here is no "bloc
41f30 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 king connection"
41f40 2c 20 73 6f 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a , so invoking.**
41f50 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f sqlite3_unlock_
41f60 6e 6f 74 69 66 79 28 29 20 72 65 73 75 6c 74 73 notify() results
41f70 20 69 6e 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e in the unlock-n
41f80 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 62 otify callback b
41f90 65 69 6e 67 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 eing.** invoked
41fa0 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 66 20 immediately. If
41fb0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
41fc0 74 68 65 6e 20 72 65 2d 61 74 74 65 6d 70 74 73 then re-attempts
41fd0 20 74 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45 the "DROP TABLE
41fe0 22 0a 2a 2a 20 6f 72 20 22 44 52 4f 50 20 49 4e ".** or "DROP IN
41ff0 44 45 58 22 20 71 75 65 72 79 2c 20 61 6e 20 69 DEX" query, an i
42000 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 20 6d 69 67 nfinite loop mig
42010 68 74 20 62 65 20 74 68 65 20 72 65 73 75 6c 74 ht be the result
42020 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 ..**.** One way
42030 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 around this prob
42040 6c 65 6d 20 69 73 20 74 6f 20 63 68 65 63 6b 20 lem is to check
42050 74 68 65 20 65 78 74 65 6e 64 65 64 20 65 72 72 the extended err
42060 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 or code returned
42070 0a 2a 2a 20 62 79 20 61 6e 20 73 71 6c 69 74 65 .** by an sqlite
42080 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 2e 20 49 3_step() call. I
42090 66 20 74 68 65 72 65 20 69 73 20 61 20 62 6c 6f f there is a blo
420a0 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e cking connection
420b0 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 78 , then the.** ex
420c0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
420d0 65 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 e is set to SQLI
420e0 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
420f0 43 41 43 48 45 2e 20 4f 74 68 65 72 77 69 73 65 CACHE. Otherwise
42100 2c 20 69 6e 0a 2a 2a 20 74 68 65 20 73 70 65 63 , in.** the spec
42110 69 61 6c 20 22 44 52 4f 50 20 54 41 42 4c 45 2f ial "DROP TABLE/
42120 49 4e 44 45 58 22 20 63 61 73 65 2c 20 74 68 65 INDEX" case, the
42130 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 extended error
42140 63 6f 64 65 20 69 73 20 6a 75 73 74 20 0a 2a 2a code is just .**
42150 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a SQLITE_LOCKED..
42160 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
42170 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b t sqlite3_unlock
42180 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c 69 74 _notify(. sqlit
42190 65 33 20 2a 70 42 6c 6f 63 6b 65 64 2c 20 20 20 e3 *pBlocked,
421a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
421b0 20 20 20 20 20 20 20 2f 2a 20 57 61 69 74 69 6e /* Waitin
421c0 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a g connection */.
421d0 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 void (*xNotify
421e0 29 28 76 6f 69 64 20 2a 2a 61 70 41 72 67 2c 20 )(void **apArg,
421f0 69 6e 74 20 6e 41 72 67 29 2c 20 20 20 20 2f 2a int nArg), /*
42200 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 Callback functi
42210 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a on to invoke */.
42220 20 20 76 6f 69 64 20 2a 70 4e 6f 74 69 66 79 41 void *pNotifyA
42230 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 rg
42240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
42250 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 Argument to pas
42260 73 20 74 6f 20 78 4e 6f 74 69 66 79 20 2a 2f 0a s to xNotify */.
42270 29 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 74 );../*.** Undo t
42280 68 65 20 68 61 63 6b 20 74 68 61 74 20 63 6f 6e he hack that con
42290 76 65 72 74 73 20 66 6c 6f 61 74 69 6e 67 20 70 verts floating p
422a0 6f 69 6e 74 20 74 79 70 65 73 20 74 6f 20 69 6e oint types to in
422b0 74 65 67 65 72 20 66 6f 72 0a 2a 2a 20 62 75 69 teger for.** bui
422c0 6c 64 73 20 6f 6e 20 70 72 6f 63 65 73 73 6f 72 lds on processor
422d0 73 20 77 69 74 68 6f 75 74 20 66 6c 6f 61 74 69 s without floati
422e0 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 ng point support
422f0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
42300 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
42310 5f 50 4f 49 4e 54 0a 23 20 75 6e 64 65 66 20 64 _POINT.# undef d
42320 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 0a 23 69 ouble.#endif..#i
42330 66 20 30 0a 7d 20 20 2f 2a 20 45 6e 64 20 6f 66 f 0.} /* End of
42340 20 74 68 65 20 27 65 78 74 65 72 6e 20 22 43 22 the 'extern "C"
42350 27 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65 6e 64 69 ' block */.#endi
42360 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a f.#endif../*****
42370 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
42380 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a sqlite3.h *****
42390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
423a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
423b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
423c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
423d0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
423e0 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
423f0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
42400 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
42410 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
42420 65 20 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20 e hash.h in the
42430 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
42440 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
42450 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
42460 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
42470 66 69 6c 65 20 68 61 73 68 2e 68 20 2a 2a 2a 2a file hash.h ****
42480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
424a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
424b0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 2001 September 2
424c0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
424d0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
424e0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
424f0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
42500 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
42510 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
42520 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
42530 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
42540 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
42550 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
42560 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
42570 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
42580 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
42590 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
425a0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
425b0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
425c0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
425d0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
425e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
425f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42620 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
42630 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 header file for
42640 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 73 68 the generic hash
42650 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 61 -table implemena
42660 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e 20 tion.** used in
42670 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 SQLite..**.** $I
42680 64 3a 20 68 61 73 68 2e 68 2c 76 20 31 2e 31 32 d: hash.h,v 1.12
42690 20 32 30 30 38 2f 31 30 2f 31 30 20 31 37 3a 34 2008/10/10 17:4
426a0 31 3a 32 39 20 64 72 68 20 45 78 70 20 24 0a 2a 1:29 drh Exp $.*
426b0 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 /.#ifndef _SQLIT
426c0 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e E_HASH_H_.#defin
426d0 65 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 e _SQLITE_HASH_H
426e0 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 _../* Forward de
426f0 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 clarations of st
42700 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 ructures. */.typ
42710 65 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68 edef struct Hash
42720 20 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 Hash;.typedef s
42730 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 48 truct HashElem H
42740 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 ashElem;../* A c
42750 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 omplete hash tab
42760 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 le is an instanc
42770 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
42780 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
42790 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f The internals o
427a0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
427b0 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f are intended to
427c0 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c be opaque -- cl
427d0 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f ient.** code sho
427e0 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 uld not attempt
427f0 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 to access or mod
42800 69 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f ify the fields o
42810 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
42820 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43 .** directly. C
42830 68 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63 hange this struc
42840 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 ture only by usi
42850 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 ng the routines
42860 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 below..** Howeve
42870 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68 65 20 22 r, many of the "
42880 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 procedures" and
42890 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 "functions" for
428a0 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a modifying and.**
428b0 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 accessing this
428c0 73 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 structure are re
428d0 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 ally macros, so
428e0 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 we can't really
428f0 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 make.** this str
42900 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a ucture opaque..*
42910 2f 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0a /.struct Hash {.
42920 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 unsigned int c
42930 6f 70 79 4b 65 79 3a 20 31 3b 20 20 2f 2a 20 54 opyKey: 1; /* T
42940 72 75 65 20 69 66 20 63 6f 70 79 20 6f 66 20 6b rue if copy of k
42950 65 79 20 6d 61 64 65 20 6f 6e 20 69 6e 73 65 72 ey made on inser
42960 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 t */. unsigned
42970 69 6e 74 20 68 74 73 69 7a 65 20 3a 20 33 31 3b int htsize : 31;
42980 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 /* Number of bu
42990 63 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 73 ckets in the has
429a0 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 h table */. uns
429b0 69 67 6e 65 64 20 69 6e 74 20 63 6f 75 6e 74 3b igned int count;
429c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
429d0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 of entries in t
429e0 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 his table */. H
429f0 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 ashElem *first;
42a00 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
42a10 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 first element of
42a20 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 the array */.
42a30 73 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20 20 struct _ht {
42a40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 /* the
42a50 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 hash table */.
42a60 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 int count;
42a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
42a80 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
42a90 65 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73 es with this has
42aa0 68 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c 65 h */. HashEle
42ab0 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 20 m *chain;
42ac0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
42ad0 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 77 69 o first entry wi
42ae0 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a th this hash */.
42af0 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 } *ht;.};../*
42b00 45 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 Each element in
42b10 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 the hash table i
42b20 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
42b30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a the following .
42b40 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 ** structure. A
42b50 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 ll elements are
42b60 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 stored on a sing
42b70 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 le doubly-linked
42b80 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 list..**.** Aga
42b90 69 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74 75 in, this structu
42ba0 72 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 re is intended t
42bb0 6f 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75 74 o be opaque, but
42bc0 20 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 it can't really
42bd0 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62 65 .** be opaque be
42be0 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64 cause it is used
42bf0 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 by macros..*/.s
42c00 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 7b truct HashElem {
42c10 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 . HashElem *nex
42c20 74 2c 20 2a 70 72 65 76 3b 20 20 20 2f 2a 20 4e t, *prev; /* N
42c30 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 ext and previous
42c40 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 elements in the
42c50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 table */. void
42c60 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 *data;
42c70 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73 73 /* Data ass
42c80 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 ociated with thi
42c90 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 76 s element */. v
42ca0 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e oid *pKey; int n
42cb0 4b 65 79 3b 20 20 20 20 2f 2a 20 4b 65 79 20 61 Key; /* Key a
42cc0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
42cd0 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d his element */.}
42ce0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 ;../*.** Access
42cf0 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64 65 routines. To de
42d00 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20 4e lete, insert a N
42d10 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a ULL pointer..*/.
42d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
42d30 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 oid sqlite3HashI
42d40 6e 69 74 28 48 61 73 68 2a 2c 20 69 6e 74 20 63 nit(Hash*, int c
42d50 6f 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f opyKey);.SQLITE_
42d60 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
42d70 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 lite3HashInsert(
42d80 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 Hash*, const voi
42d90 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 d *pKey, int nKe
42da0 79 2c 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b y, void *pData);
42db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
42dc0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 void *sqlite3Has
42dd0 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 hFind(const Hash
42de0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 *, const void *p
42df0 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a Key, int nKey);.
42e00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 48 SQLITE_PRIVATE H
42e10 61 73 68 45 6c 65 6d 20 2a 73 71 6c 69 74 65 33 ashElem *sqlite3
42e20 48 61 73 68 46 69 6e 64 45 6c 65 6d 28 63 6f 6e HashFindElem(con
42e30 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 st Hash*, const
42e40 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 void *pKey, int
42e50 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nKey);.SQLITE_PR
42e60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
42e70 65 33 48 61 73 68 43 6c 65 61 72 28 48 61 73 68 e3HashClear(Hash
42e80 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f *);../*.** Macro
42e90 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 s for looping ov
42ea0 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 er all elements
42eb0 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e of a hash table.
42ec0 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a The idiom is.*
42ed0 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a * like this:.**.
42ee0 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a 2a 2a 20 ** Hash h;.**
42ef0 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a HashElem *p;.*
42f00 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 * ....** for
42f10 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 (p=sqliteHashFir
42f20 73 74 28 26 68 29 3b 20 70 3b 20 70 3d 73 71 6c st(&h); p; p=sql
42f30 69 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b iteHashNext(p)){
42f40 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72 75 .** SomeStru
42f50 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20 73 cture *pData = s
42f60 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29 qliteHashData(p)
42f70 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 ;.** // do s
42f80 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 44 omething with pD
42f90 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 ata.** }.*/.#d
42fa0 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 efine sqliteHash
42fb0 46 69 72 73 74 28 48 29 20 20 28 28 48 29 2d 3e First(H) ((H)->
42fc0 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65 20 73 first).#define s
42fd0 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 45 29 qliteHashNext(E)
42fe0 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 ((E)->next).#
42ff0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 define sqliteHas
43000 68 44 61 74 61 28 45 29 20 20 20 28 28 45 29 2d hData(E) ((E)-
43010 3e 64 61 74 61 29 0a 23 64 65 66 69 6e 65 20 73 >data).#define s
43020 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45 29 20 qliteHashKey(E)
43030 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 0a 23 ((E)->pKey).#
43040 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 define sqliteHas
43050 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28 45 29 hKeysize(E) ((E)
43060 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a 2a 20 4e ->nKey)../*.** N
43070 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
43080 20 69 6e 20 61 20 68 61 73 68 20 74 61 62 6c 65 in a hash table
43090 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 .*/.#define sqli
430a0 74 65 48 61 73 68 43 6f 75 6e 74 28 48 29 20 20 teHashCount(H)
430b0 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a 0a 23 65 ((H)->count)..#e
430c0 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f ndif /* _SQLITE_
430d0 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a HASH_H_ */../***
430e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
430f0 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a of hash.h ******
43100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
43130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
43140 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
43150 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
43160 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
43170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
43180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
43190 75 64 65 20 70 61 72 73 65 2e 68 20 69 6e 20 74 ude parse.h in t
431a0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
431b0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
431c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
431d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
431e0 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 68 20 2a n file parse.h *
431f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 **********/.#def
43220 69 6e 65 20 54 4b 5f 53 45 4d 49 20 20 20 20 20 ine TK_SEMI
43230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43240 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
43250 20 54 4b 5f 45 58 50 4c 41 49 4e 20 20 20 20 20 TK_EXPLAIN
43260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43270 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 54 4b 2.#define TK
43280 5f 51 55 45 52 59 20 20 20 20 20 20 20 20 20 20 _QUERY
43290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
432a0 20 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 3.#define TK_PL
432b0 41 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AN
432c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 4.
432d0 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 47 49 4e #define TK_BEGIN
432e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
432f0 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 5.#de
43300 66 69 6e 65 20 54 4b 5f 54 52 41 4e 53 41 43 54 fine TK_TRANSACT
43310 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 ION
43320 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 6.#defin
43330 65 20 54 4b 5f 44 45 46 45 52 52 45 44 20 20 20 e TK_DEFERRED
43340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43350 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 54 7.#define T
43360 4b 5f 49 4d 4d 45 44 49 41 54 45 20 20 20 20 20 K_IMMEDIATE
43370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43380 20 20 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 8.#define TK_E
43390 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 XCLUSIVE
433a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
433b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d .#define TK_COMM
433c0 49 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IT
433d0 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 10.#d
433e0 65 66 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20 efine TK_END
433f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43400 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 11.#defi
43410 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 ne TK_ROLLBACK
43420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43430 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 12.#define
43440 54 4b 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20 TK_SAVEPOINT
43450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43460 20 20 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 13.#define TK_
43470 52 45 4c 45 41 53 45 20 20 20 20 20 20 20 20 20 RELEASE
43480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
43490 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20 4.#define TK_TO
434a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
434b0 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 15.#
434c0 64 65 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 define TK_TABLE
434d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
434e0 20 20 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 16.#def
434f0 69 6e 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20 ine TK_CREATE
43500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43510 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 17.#define
43520 20 54 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20 TK_IF
43530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43540 20 20 20 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 18.#define TK
43550 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 _NOT
43560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43570 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 19.#define TK_EX
43580 49 53 54 53 20 20 20 20 20 20 20 20 20 20 20 20 ISTS
43590 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 20.
435a0 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20 #define TK_TEMP
435b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
435c0 20 20 20 20 20 20 20 20 20 20 32 31 0a 23 64 65 21.#de
435d0 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20 fine TK_LP
435e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
435f0 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 22.#defin
43600 65 20 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20 e TK_RP
43610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43620 20 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 54 23.#define T
43630 4b 5f 41 53 20 20 20 20 20 20 20 20 20 20 20 20 K_AS
43640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43650 20 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 24.#define TK_C
43660 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 OMMA
43670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35 25
43680 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20 .#define TK_ID
43690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
436a0 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 26.#d
436b0 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 efine TK_INDEXED
436c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
436d0 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 27.#defi
436e0 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 ne TK_ABORT
436f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43700 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 28.#define
43710 54 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 20 TK_AFTER
43720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43730 20 20 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 29.#define TK_
43740 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 ANALYZE
43750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
43760 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43 0.#define TK_ASC
43770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43780 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 31.#
43790 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 48 define TK_ATTACH
437a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
437b0 20 20 20 20 20 20 20 20 20 33 32 0a 23 64 65 66 32.#def
437c0 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 20 ine TK_BEFORE
437d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
437e0 20 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 33.#define
437f0 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 20 TK_BY
43800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43810 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 34.#define TK
43820 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 20 20 _CASCADE
43830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43840 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 35.#define TK_CA
43850 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ST
43860 20 20 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 36.
43870 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d #define TK_COLUM
43880 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 NKW
43890 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 37.#de
438a0 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 fine TK_CONFLICT
438b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
438c0 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 38.#defin
438d0 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20 20 e TK_DATABASE
438e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
438f0 20 20 20 20 33 39 0a 23 64 65 66 69 6e 65 20 54 39.#define T
43900 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20 20 K_DESC
43910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43920 20 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 40.#define TK_D
43930 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 ETACH
43940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31 41
43950 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43 48 .#define TK_EACH
43960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43970 20 20 20 20 20 20 20 20 20 20 20 34 32 0a 23 64 42.#d
43980 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20 20 efine TK_FAIL
43990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
439a0 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 43.#defi
439b0 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20 20 ne TK_FOR
439c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
439d0 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 44.#define
439e0 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 20 TK_IGNORE
439f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43a00 20 20 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45.#define TK_
43a10 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20 INITIALLY
43a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
43a30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53 6.#define TK_INS
43a40 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20 TEAD
43a50 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a 23 47.#
43a60 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b define TK_LIKE_K
43a70 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 W
43a80 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 48.#def
43a90 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20 20 ine TK_MATCH
43aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43ab0 20 20 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65 49.#define
43ac0 20 54 4b 5f 4b 45 59 20 20 20 20 20 20 20 20 20 TK_KEY
43ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43ae0 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 50.#define TK
43af0 5f 4f 46 20 20 20 20 20 20 20 20 20 20 20 20 20 _OF
43b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43b10 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 51.#define TK_OF
43b20 46 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20 FSET
43b30 20 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 52.
43b40 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d #define TK_PRAGM
43b50 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 A
43b60 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 53.#de
43b70 66 69 6e 65 20 54 4b 5f 52 41 49 53 45 20 20 20 fine TK_RAISE
43b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43b90 20 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 54.#defin
43ba0 65 20 54 4b 5f 52 45 50 4c 41 43 45 20 20 20 20 e TK_REPLACE
43bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43bc0 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 20 54 55.#define T
43bd0 4b 5f 52 45 53 54 52 49 43 54 20 20 20 20 20 20 K_RESTRICT
43be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43bf0 20 35 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 56.#define TK_R
43c00 4f 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OW
43c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37 57
43c20 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 49 47 .#define TK_TRIG
43c30 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 GER
43c40 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 58.#d
43c50 65 66 69 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20 efine TK_VACUUM
43c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43c70 20 20 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 59.#defi
43c80 6e 65 20 54 4b 5f 56 49 45 57 20 20 20 20 20 20 ne TK_VIEW
43c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43ca0 20 20 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 60.#define
43cb0 54 4b 5f 56 49 52 54 55 41 4c 20 20 20 20 20 20 TK_VIRTUAL
43cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43cd0 20 20 36 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 61.#define TK_
43ce0 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 REINDEX
43cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
43d00 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e 2.#define TK_REN
43d10 41 4d 45 20 20 20 20 20 20 20 20 20 20 20 20 20 AME
43d20 20 20 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 63.#
43d30 64 65 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f define TK_CTIME_
43d40 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 KW
43d50 20 20 20 20 20 20 20 20 20 36 34 0a 23 64 65 66 64.#def
43d60 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20 20 20 20 ine TK_ANY
43d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43d80 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 65.#define
43d90 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 20 20 20 TK_OR
43da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43db0 20 20 20 36 36 0a 23 64 65 66 69 6e 65 20 54 4b 66.#define TK
43dc0 5f 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 _AND
43dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43de0 36 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 67.#define TK_IS
43df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e00 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 0a 68.
43e10 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 54 57 45 #define TK_BETWE
43e20 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EN
43e30 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 64 65 69.#de
43e40 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20 20 20 20 fine TK_IN
43e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e60 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e 70.#defin
43e70 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 e TK_ISNULL
43e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e90 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 20 54 71.#define T
43ea0 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 K_NOTNULL
43eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43ec0 20 37 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 72.#define TK_N
43ed0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
43ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 33 73
43ef0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 51 20 20 .#define TK_EQ
43f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43f10 20 20 20 20 20 20 20 20 20 20 20 37 34 0a 23 64 74.#d
43f20 65 66 69 6e 65 20 54 4b 5f 47 54 20 20 20 20 20 efine TK_GT
43f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43f40 20 20 20 20 20 20 20 20 37 35 0a 23 64 65 66 69 75.#defi
43f50 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 ne TK_LE
43f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43f70 20 20 20 20 20 37 36 0a 23 64 65 66 69 6e 65 20 76.#define
43f80 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20 20 TK_LT
43f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43fa0 20 20 37 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 77.#define TK_
43fb0 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 GE
43fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
43fd0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 53 43 8.#define TK_ESC
43fe0 41 50 45 20 20 20 20 20 20 20 20 20 20 20 20 20 APE
43ff0 20 20 20 20 20 20 20 20 20 20 20 20 37 39 0a 23 79.#
44000 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44 define TK_BITAND
44010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44020 20 20 20 20 20 20 20 20 20 38 30 0a 23 64 65 66 80.#def
44030 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 ine TK_BITOR
44040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44050 20 20 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 81.#define
44060 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 20 20 20 TK_LSHIFT
44070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44080 20 20 20 38 32 0a 23 64 65 66 69 6e 65 20 54 4b 82.#define TK
44090 5f 52 53 48 49 46 54 20 20 20 20 20 20 20 20 20 _RSHIFT
440a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
440b0 38 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 83.#define TK_PL
440c0 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 US
440d0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 34 0a 84.
440e0 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 #define TK_MINUS
440f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44100 20 20 20 20 20 20 20 20 20 20 38 35 0a 23 64 65 85.#de
44110 66 69 6e 65 20 54 4b 5f 53 54 41 52 20 20 20 20 fine TK_STAR
44120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44130 20 20 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e 86.#defin
44140 65 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 20 20 e TK_SLASH
44150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44160 20 20 20 20 38 37 0a 23 64 65 66 69 6e 65 20 54 87.#define T
44170 4b 5f 52 45 4d 20 20 20 20 20 20 20 20 20 20 20 K_REM
44180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44190 20 38 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 88.#define TK_C
441a0 4f 4e 43 41 54 20 20 20 20 20 20 20 20 20 20 20 ONCAT
441b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 39 89
441c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c .#define TK_COLL
441d0 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ATE
441e0 20 20 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 90.#d
441f0 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20 efine TK_UMINUS
44200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44210 20 20 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 91.#defi
44220 6e 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20 ne TK_UPLUS
44230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44240 20 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 92.#define
44250 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20 TK_BITNOT
44260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44270 20 20 39 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 93.#define TK_
44280 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 20 STRING
44290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
442a0 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4.#define TK_JOI
442b0 4e 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 N_KW
442c0 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a 23 95.#
442d0 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 define TK_CONSTR
442e0 41 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 AINT
442f0 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 96.#def
44300 69 6e 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 20 ine TK_DEFAULT
44310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44320 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 97.#define
44330 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20 TK_NULL
44340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44350 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 54 4b 98.#define TK
44360 5f 50 52 49 4d 41 52 59 20 20 20 20 20 20 20 20 _PRIMARY
44370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44380 39 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 99.#define TK_UN
44390 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20 20 IQUE
443a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 30 100
443b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43 .#define TK_CHEC
443c0 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K
443d0 20 20 20 20 20 20 20 20 20 20 20 31 30 31 0a 23 101.#
443e0 64 65 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45 define TK_REFERE
443f0 4e 43 45 53 20 20 20 20 20 20 20 20 20 20 20 20 NCES
44400 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 102.#de
44410 66 69 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52 fine TK_AUTOINCR
44420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44430 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 103.#defi
44440 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20 ne TK_ON
44450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44460 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 104.#define
44470 20 54 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20 TK_DELETE
44480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44490 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65 20 54 105.#define T
444a0 4b 5f 55 50 44 41 54 45 20 20 20 20 20 20 20 20 K_UPDATE
444b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
444c0 20 31 30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 106.#define TK_
444d0 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 INSERT
444e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
444f0 30 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 07.#define TK_SE
44500 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
44510 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38 108
44520 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 .#define TK_DEFE
44530 52 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 RRABLE
44540 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 109.#
44550 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 define TK_FOREIG
44560 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
44570 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64 65 110.#de
44580 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 fine TK_DROP
44590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
445a0 20 20 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 111.#defi
445b0 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 ne TK_UNION
445c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
445d0 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 112.#define
445e0 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 TK_ALL
445f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44600 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 54 113.#define T
44610 4b 5f 45 58 43 45 50 54 20 20 20 20 20 20 20 20 K_EXCEPT
44620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44630 20 31 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 114.#define TK_
44640 49 4e 54 45 52 53 45 43 54 20 20 20 20 20 20 20 INTERSECT
44650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
44660 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 15.#define TK_SE
44670 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20 LECT
44680 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36 116
44690 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 .#define TK_DIST
446a0 49 4e 43 54 20 20 20 20 20 20 20 20 20 20 20 20 INCT
446b0 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 117.#
446c0 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 define TK_DOT
446d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
446e0 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64 65 118.#de
446f0 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 fine TK_FROM
44700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44710 20 20 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 119.#defi
44720 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 ne TK_JOIN
44730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44740 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 120.#define
44750 20 54 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20 TK_USING
44760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44770 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 54 121.#define T
44780 4b 5f 4f 52 44 45 52 20 20 20 20 20 20 20 20 20 K_ORDER
44790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
447a0 20 31 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 122.#define TK_
447b0 47 52 4f 55 50 20 20 20 20 20 20 20 20 20 20 20 GROUP
447c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
447d0 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41 23.#define TK_HA
447e0 56 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 VING
447f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 34 124
44800 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 .#define TK_LIMI
44810 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
44820 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 125.#
44830 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20 define TK_WHERE
44840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44850 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 64 65 126.#de
44860 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20 fine TK_INTO
44870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44880 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 66 69 127.#defi
44890 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20 ne TK_VALUES
448a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
448b0 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 128.#define
448c0 20 54 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20 TK_INTEGER
448d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
448e0 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 54 129.#define T
448f0 4b 5f 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20 K_FLOAT
44900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44910 20 31 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 130.#define TK_
44920 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 BLOB
44930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
44940 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 31.#define TK_RE
44950 47 49 53 54 45 52 20 20 20 20 20 20 20 20 20 20 GISTER
44960 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 32 132
44970 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49 .#define TK_VARI
44980 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 ABLE
44990 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 133.#
449a0 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20 define TK_CASE
449b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
449c0 20 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 134.#de
449d0 66 69 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20 fine TK_WHEN
449e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
449f0 20 20 20 20 20 20 20 31 33 35 0a 23 64 65 66 69 135.#defi
44a00 6e 65 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20 ne TK_THEN
44a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a20 20 20 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 136.#define
44a30 20 54 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20 TK_ELSE
44a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a50 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 54 137.#define T
44a60 4b 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 K_INDEX
44a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a80 20 31 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 138.#define TK_
44a90 41 4c 54 45 52 20 20 20 20 20 20 20 20 20 20 20 ALTER
44aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
44ab0 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 39.#define TK_AD
44ac0 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D
44ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 30 140
44ae0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 .#define TK_TO_T
44af0 45 58 54 20 20 20 20 20 20 20 20 20 20 20 20 20 EXT
44b00 20 20 20 20 20 20 20 20 20 20 20 31 34 31 0a 23 141.#
44b10 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f define TK_TO_BLO
44b20 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 B
44b30 20 20 20 20 20 20 20 20 20 31 34 32 0a 23 64 65 142.#de
44b40 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 fine TK_TO_NUMER
44b50 49 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IC
44b60 20 20 20 20 20 20 20 31 34 33 0a 23 64 65 66 69 143.#defi
44b70 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 ne TK_TO_INT
44b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44b90 20 20 20 20 20 31 34 34 0a 23 64 65 66 69 6e 65 144.#define
44ba0 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20 TK_TO_REAL
44bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44bc0 20 20 20 31 34 35 0a 23 64 65 66 69 6e 65 20 54 145.#define T
44bd0 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 20 20 K_END_OF_FILE
44be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44bf0 20 31 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 146.#define TK_
44c00 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20 20 20 ILLEGAL
44c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
44c20 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 50 47.#define TK_SP
44c30 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ACE
44c40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 38 148
44c50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 43 4c .#define TK_UNCL
44c60 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20 20 20 OSED_STRING
44c70 20 20 20 20 20 20 20 20 20 20 20 31 34 39 0a 23 149.#
44c80 64 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43 54 49 define TK_FUNCTI
44c90 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ON
44ca0 20 20 20 20 20 20 20 20 20 31 35 30 0a 23 64 65 150.#de
44cb0 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 fine TK_COLUMN
44cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44cd0 20 20 20 20 20 20 20 31 35 31 0a 23 64 65 66 69 151.#defi
44ce0 6e 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 ne TK_AGG_FUNCTI
44cf0 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ON
44d00 20 20 20 20 20 31 35 32 0a 23 64 65 66 69 6e 65 152.#define
44d10 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 TK_AGG_COLUMN
44d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44d30 20 20 20 31 35 33 0a 23 64 65 66 69 6e 65 20 54 153.#define T
44d40 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20 20 20 K_CONST_FUNC
44d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44d60 20 31 35 34 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 154../*********
44d70 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 ***** End of par
44d80 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a se.h ***********
44d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44db0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
44dc0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
44dd0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
44de0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
44df0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
44e00 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c ****/.#include <
44e10 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 stdio.h>.#includ
44e20 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e e <stdlib.h>.#in
44e30 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e clude <string.h>
44e40 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 .#include <asser
44e50 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 t.h>.#include <s
44e60 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 tddef.h>../*.**
44e70 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 If compiling for
44e80 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 a processor tha
44e90 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 t lacks floating
44ea0 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a point support,.
44eb0 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e ** substitute in
44ec0 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 teger for floati
44ed0 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 ng-point.*/.#ifd
44ee0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
44ef0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 LOATING_POINT.#
44f00 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 define double sq
44f10 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 64 65 66 lite_int64.# def
44f20 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 ine LONGDOUBLE_T
44f30 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 YPE sqlite_int64
44f40 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 .# ifndef SQLITE
44f50 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20 64 65 66 _BIG_DBL.# def
44f60 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 ine SQLITE_BIG_D
44f70 42 4c 20 28 30 78 37 66 66 66 66 66 66 66 66 66 BL (0x7fffffffff
44f80 66 66 66 66 66 66 29 0a 23 20 65 6e 64 69 66 0a ffffff).# endif.
44f90 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
44fa0 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 OMIT_DATETIME_FU
44fb0 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65 20 53 NCS 1.# define S
44fc0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
44fd0 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 1.# undef SQLIT
44fe0 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 E_MIXED_ENDIAN_6
44ff0 34 42 49 54 5f 46 4c 4f 41 54 0a 23 65 6e 64 69 4BIT_FLOAT.#endi
45000 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
45010 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69 6e _BIG_DBL.# defin
45020 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c e SQLITE_BIG_DBL
45030 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a 0a (1e99).#endif..
45040 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50 44 /*.** OMIT_TEMPD
45050 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 69 66 B is set to 1 if
45060 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d SQLITE_OMIT_TEM
45070 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c 20 PDB is defined,
45080 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61 72 or 0.** afterwar
45090 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20 6d d. Having this m
450a0 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20 74 acro allows us t
450b0 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 63 6f o cause the C co
450c0 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d mpiler .** to om
450d0 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 79 20 it code used by
450e0 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74 68 TEMP tables with
450f0 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64 65 out messy #ifnde
45100 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f f statements..*/
45110 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
45120 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66 69 MIT_TEMPDB.#defi
45130 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 31 ne OMIT_TEMPDB 1
45140 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
45150 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65 6e MIT_TEMPDB 0.#en
45160 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 dif../*.** If th
45170 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
45180 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 74 o is set to 1, t
45190 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 hen NULL values
451a0 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a are considered.*
451b0 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e 20 * distinct when
451c0 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 74 determining whet
451d0 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20 65 her or not two e
451e0 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20 73 ntries are the s
451f0 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49 51 ame.** in a UNIQ
45200 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20 UE index. This
45210 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 74 67 is the way Postg
45220 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20 44 reSQL, Oracle, D
45230 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43 B2, MySQL,.** OC
45240 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62 69 ELOT, and Firebi
45250 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54 68 rd all work. Th
45260 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 78 70 e SQL92 spec exp
45270 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 68 69 licitly says thi
45280 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79 20 s.** is the way
45290 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 70 6f things are suppo
452a0 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a se to work..**.*
452b0 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * If the followi
452c0 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20 ng macro is set
452d0 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73 20 to 0, the NULLs
452e0 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20 66 are indistinct f
452f0 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 69 or.** a UNIQUE i
45300 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20 6d ndex. In this m
45310 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e 6c ode, you can onl
45320 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 y have a single
45330 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66 6f NULL entry.** fo
45340 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 r a column decla
45350 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68 69 red UNIQUE. Thi
45360 73 20 69 73 20 74 68 65 20 77 61 79 20 49 6e 66 s is the way Inf
45370 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53 65 ormix and SQL Se
45380 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f rver.** work..*/
45390 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44 49 .#define NULL_DI
453a0 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51 55 STINCT_FOR_UNIQU
453b0 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 E 1../*.** The "
453c0 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75 6d file format" num
453d0 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ber is an intege
453e0 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d r that is increm
453f0 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a 2a ented whenever.*
45400 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65 6c * the VDBE-level
45410 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68 61 file format cha
45420 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f nges. The follo
45430 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69 wing macros defi
45440 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64 65 ne the.** the de
45450 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d 61 fault file forma
45460 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62 61 t for new databa
45470 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78 69 ses and the maxi
45480 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 0a mum file format.
45490 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62 72 ** that the libr
454a0 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a 2f ary can read..*/
454b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
454c0 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 MAX_FILE_FORMAT
454d0 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 4.#ifndef SQLITE
454e0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f _DEFAULT_FILE_FO
454f0 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53 51 RMAT.# define SQ
45500 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c LITE_DEFAULT_FIL
45510 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64 69 E_FORMAT 1.#endi
45520 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 65 f../*.** Provide
45530 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 a default value
45540 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d 50 for SQLITE_TEMP
45550 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 69 _STORE in case i
45560 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 t is not specifi
45570 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d ed.** on the com
45580 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66 mand-line.*/.#if
45590 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 ndef SQLITE_TEMP
455a0 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 20 _STORE.# define
455b0 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 SQLITE_TEMP_STOR
455c0 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a E 1.#endif../*.*
455d0 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 64 * GCC does not d
455e0 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 74 efine the offset
455f0 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 65 of() macro so we
45600 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 69 'll have to do i
45610 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e 0a t.** ourselves..
45620 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 65 */.#ifndef offse
45630 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 73 tof.#define offs
45640 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c 46 etof(STRUCTURE,F
45650 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 68 IELD) ((int)((ch
45660 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 45 ar*)&((STRUCTURE
45670 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 65 *)0)->FIELD)).#e
45680 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 ndif../*.** Chec
45690 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73 k to see if this
456a0 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 42 machine uses EB
456b0 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 6c CDIC. (Yes, bel
456c0 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f ieve it or.** no
456d0 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 69 t, there are sti
456e0 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 20 ll machines out
456f0 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 45 there that use E
45700 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 27 BCDIC.).*/.#if '
45710 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 64 A' == '\301'.# d
45720 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 43 efine SQLITE_EBC
45730 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 DIC 1.#else.# de
45740 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 49 fine SQLITE_ASCI
45750 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a I 1.#endif../*.*
45760 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b 6e * Integers of kn
45770 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 73 own sizes. Thes
45780 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 74 e typedefs might
45790 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 68 change for arch
457a0 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 65 itectures.** whe
457b0 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 72 re the sizes ver
457c0 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f 72 y. Preprocessor
457d0 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 69 macros are avai
457e0 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 68 lable so that th
457f0 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 62 e.** types can b
45800 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72 e conveniently r
45810 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 edefined at comp
45820 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20 ile-type. Like
45830 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 this:.**.**
45840 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 54 cc '-DUINTPT
45850 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 R_TYPE=long long
45860 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 int' ....*/.#if
45870 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 ndef UINT32_TYPE
45880 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 .# ifdef HAVE_UI
45890 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e 65 NT32_T.# define
458a0 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 6e UINT32_TYPE uin
458b0 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 t32_t.# else.#
458c0 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 define UINT32_TY
458d0 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a PE unsigned int.
458e0 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 # endif.#endif.#
458f0 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 59 ifndef UINT16_TY
45900 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f PE.# ifdef HAVE_
45910 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69 UINT16_T.# defi
45920 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 ne UINT16_TYPE u
45930 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 int16_t.# else.#
45940 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f define UINT16_
45950 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68 TYPE unsigned sh
45960 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a ort int.# endif.
45970 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49 #endif.#ifndef I
45980 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65 NT16_TYPE.# ifde
45990 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a 23 f HAVE_INT16_T.#
459a0 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 define INT16_T
459b0 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 6c YPE int16_t.# el
459c0 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 se.# define INT
459d0 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 6e 16_TYPE short in
459e0 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 t.# endif.#endif
459f0 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f 54 .#ifndef UINT8_T
45a00 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
45a10 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 _UINT8_T.# defi
45a20 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 69 ne UINT8_TYPE ui
45a30 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 nt8_t.# else.#
45a40 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 define UINT8_TYP
45a50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a E unsigned char.
45a60 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 # endif.#endif.#
45a70 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45 ifndef INT8_TYPE
45a80 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e .# ifdef HAVE_IN
45a90 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49 T8_T.# define I
45aa0 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 0a NT8_TYPE int8_t.
45ab0 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 # else.# define
45ac0 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e 65 INT8_TYPE signe
45ad0 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23 d char.# endif.#
45ae0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f endif.#ifndef LO
45af0 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20 NGDOUBLE_TYPE.#
45b00 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c define LONGDOUBL
45b10 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62 E_TYPE long doub
45b20 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65 le.#endif.typede
45b30 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 f sqlite_int64 i
45b40 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64; /*
45b50 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 8-byte signed in
45b60 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 teger */.typedef
45b70 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75 sqlite_uint64 u
45b80 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38 64; /* 8
45b90 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 -byte unsigned i
45ba0 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 nteger */.typede
45bb0 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33 f UINT32_TYPE u3
45bc0 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2; /*
45bd0 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 4-byte unsigned
45be0 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 integer */.typed
45bf0 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 ef UINT16_TYPE u
45c00 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 16; /*
45c10 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 2-byte unsigned
45c20 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
45c30 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69 def INT16_TYPE i
45c40 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 16; /
45c50 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 2-byte signed
45c60 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 integer */.typed
45c70 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38 ef UINT8_TYPE u8
45c80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
45c90 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 1-byte unsigned
45ca0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
45cb0 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 38 def INT8_TYPE i8
45cc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
45cd0 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 1-byte signed
45ce0 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a integer */../*.*
45cf0 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 * Macros to dete
45d00 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68 rmine whether th
45d10 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67 e machine is big
45d20 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 or little endia
45d30 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 n,.** evaluated
45d40 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 at runtime..*/.#
45d50 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 ifdef SQLITE_AMA
45d60 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 LGAMATION.SQLITE
45d70 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 _PRIVATE const i
45d80 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 nt sqlite3one =
45d90 31 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 1;.#else.SQLITE_
45da0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e PRIVATE const in
45db0 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 t sqlite3one;.#e
45dc0 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 ndif.#if defined
45dd0 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
45de0 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
45df0 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c efined(_M_IX86)\
45e00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
45e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
45e20 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 defined(__x86_6
45e30 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4) || defined(__
45e40 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 x86_64__).# defi
45e50 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 ne SQLITE_BIGEND
45e60 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 6e IAN 0.# defin
45e70 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 e SQLITE_LITTLEE
45e80 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 NDIAN 1.# define
45e90 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
45ea0 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31 IVE SQLITE_UTF1
45eb0 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6LE.#else.# defi
45ec0 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 ne SQLITE_BIGEND
45ed0 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a IAN (*(char *
45ee0 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d )(&sqlite3one)==
45ef0 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 0).# define SQLI
45f00 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 TE_LITTLEENDIAN
45f10 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 (*(char *)(&sqli
45f20 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 te3one)==1).# de
45f30 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 fine SQLITE_UTF1
45f40 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 5f 6NATIVE (SQLITE_
45f50 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 BIGENDIAN?SQLITE
45f60 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 5f _UTF16BE:SQLITE_
45f70 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a UTF16LE).#endif.
45f80 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 ./*.** Constants
45f90 20 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 74 for the largest
45fa0 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f and smallest po
45fb0 73 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 69 ssible 64-bit si
45fc0 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a gned integers..*
45fd0 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 61 * These macros a
45fe0 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77 re designed to w
45ff0 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e ork correctly on
46000 20 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e 64 both 32-bit and
46010 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 64-bit.** compi
46020 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 lers..*/.#define
46030 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 20 LARGEST_INT64
46040 28 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 69 (0xffffffff|(((i
46050 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 64)0x7fffffff)<<
46060 33 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 32)).#define SMA
46070 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 69 LLEST_INT64 (((i
46080 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 64)-1) - LARGEST
46090 5f 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 _INT64)../* .**
460a0 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 Round up a numbe
460b0 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 r to the next la
460c0 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 rger multiple of
460d0 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 8. This is use
460e0 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d d.** to force 8-
460f0 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f byte alignment o
46100 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65 n 64-bit archite
46110 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 ctures..*/.#defi
46120 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 20 ne ROUND8(x)
46130 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f (((x)+7)&~7)../
46140 2a 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 *.** Round down
46150 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 6d to the nearest m
46160 75 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a ultiple of 8.*/.
46170 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 #define ROUNDDOW
46180 4e 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a 0a N8(x) ((x)&~7)..
46190 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
461a0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
461b0 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ng structure is
461c0 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
461d0 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a e busy-handler.*
461e0 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 * callback for a
461f0 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61 given sqlite ha
46200 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 ndle. .**.** The
46210 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 sqlite.busyHand
46220 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 ler member of th
46230 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 20 e sqlite struct
46240 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73 contains the bus
46250 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f y.** callback fo
46260 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 r the database h
46270 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65 andle. Each page
46280 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65 r opened via the
46290 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c sqlite.** handl
462a0 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 6f e is passed a po
462b0 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e inter to sqlite.
462c0 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65 busyHandler. The
462d0 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a busy-handler.**
462e0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72 callback is cur
462f0 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f rently invoked o
46300 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 nly from within
46310 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 pager.c..*/.type
46320 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 48 def struct BusyH
46330 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c andler BusyHandl
46340 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48 er;.struct BusyH
46350 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28 andler {. int (
46360 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69 *xFunc)(void *,i
46370 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73 nt); /* The bus
46380 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 y callback */.
46390 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20 void *pArg;
463a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
463b0 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 20 rst arg to busy
463c0 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e callback */. in
463d0 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20 t nBusy;
463e0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 /* Incr
463f0 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 emented with eac
46400 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d h busy call */.}
46410 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 ;../*.** Name of
46420 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 the master data
46430 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 65 base table. The
46440 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 master database
46450 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 73 table.** is a s
46460 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 61 pecial table tha
46470 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 t holds the name
46480 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 73 s and attributes
46490 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 20 of all.** user
464a0 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 tables and indic
464b0 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d es..*/.#define M
464c0 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 20 ASTER_NAME
464d0 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 "sqlite_master"
464e0 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41 .#define TEMP_MA
464f0 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69 STER_NAME "sqli
46500 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0a te_temp_master".
46510 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 2d ./*.** The root-
46520 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 74 page of the mast
46530 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c er database tabl
46540 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 e..*/.#define MA
46550 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 20 STER_ROOT
46560 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 1../*.** The nam
46570 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 e of the schema
46580 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e table..*/.#defin
46590 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 78 e SCHEMA_TABLE(x
465a0 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 44 ) ((!OMIT_TEMPD
465b0 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 5f B)&&(x==1)?TEMP_
465c0 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 54 MASTER_NAME:MAST
465d0 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 ER_NAME)../*.**
465e0 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d 61 A convenience ma
465f0 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e 73 cro that returns
46600 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
46610 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e lements in.** an
46620 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 69 array..*/.#defi
46630 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 20 ne ArraySize(X)
46640 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 ((int)(sizeof
46650 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29 (X)/sizeof(X[0])
46660 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f ))../*.** The fo
46670 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61 73 llowing value as
46680 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d 65 a destructor me
46690 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69 74 ans to use sqlit
466a0 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20 54 e3DbFree()..** T
466b0 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e his is an intern
466c0 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f 20 al extension to
466d0 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 61 6e SQLITE_STATIC an
466e0 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 d SQLITE_TRANSIE
466f0 4e 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 NT..*/.#define S
46700 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20 20 QLITE_DYNAMIC
46710 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 ((sqlite3_destru
46720 63 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74 65 ctor_type)sqlite
46730 33 44 62 46 72 65 65 29 0a 0a 2f 2a 0a 2a 2a 20 3DbFree)../*.**
46740 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 When SQLITE_OMIT
46750 5f 57 53 44 20 69 73 20 64 65 66 69 6e 65 64 2c _WSD is defined,
46760 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 it means that t
46770 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 66 6f he target platfo
46780 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73 rm does.** not s
46790 75 70 70 6f 72 74 20 57 72 69 74 61 62 6c 65 20 upport Writable
467a0 53 74 61 74 69 63 20 44 61 74 61 20 28 57 53 44 Static Data (WSD
467b0 29 20 73 75 63 68 20 61 73 20 67 6c 6f 62 61 6c ) such as global
467c0 20 61 6e 64 20 73 74 61 74 69 63 20 76 61 72 69 and static vari
467d0 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c 20 76 61 ables..** All va
467e0 72 69 61 62 6c 65 73 20 6d 75 73 74 20 65 69 74 riables must eit
467f0 68 65 72 20 62 65 20 6f 6e 20 74 68 65 20 73 74 her be on the st
46800 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 63 61 6c ack or dynamical
46810 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f ly allocated fro
46820 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 2e 20 20 m.** the heap.
46830 57 68 65 6e 20 57 53 44 20 69 73 20 75 6e 73 75 When WSD is unsu
46840 70 70 6f 72 74 65 64 2c 20 74 68 65 20 76 61 72 pported, the var
46850 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f iable declaratio
46860 6e 73 20 73 63 61 74 74 65 72 65 64 0a 2a 2a 20 ns scattered.**
46870 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 53 throughout the S
46880 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 73 74 20 QLite code must
46890 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 6e 74 73 become constants
468a0 20 69 6e 73 74 65 61 64 2e 20 20 54 68 65 20 53 instead. The S
468b0 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 6d 61 63 QLITE_WSD.** mac
468c0 72 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 ro is used for t
468d0 68 69 73 20 70 75 72 70 6f 73 65 2e 20 20 41 6e his purpose. An
468e0 64 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 66 d instead of ref
468f0 65 72 65 6e 63 69 6e 67 20 74 68 65 20 76 61 72 erencing the var
46900 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 63 74 6c iable.** directl
46910 79 2c 20 77 65 20 75 73 65 20 69 74 73 20 63 6f y, we use its co
46920 6e 73 74 61 6e 74 20 61 73 20 61 20 6b 65 79 20 nstant as a key
46930 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 72 75 to lookup the ru
46940 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 n-time allocated
46950 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74 20 .** buffer that
46960 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 72 69 61 holds real varia
46970 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e 73 74 61 ble. The consta
46980 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20 69 nt is also the i
46990 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20 66 6f nitializer.** fo
469a0 72 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 r the run-time a
469b0 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e llocated buffer.
469c0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 75 73 .**.** In the us
469d0 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 57 ual case where W
469e0 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c SD is supported,
469f0 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44 20 the SQLITE_WSD
46a00 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 and GLOBAL.** ma
46a10 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f cros become no-o
46a20 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65 72 6f ps and have zero
46a30 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70 performance imp
46a40 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 act..*/.#ifdef S
46a50 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 QLITE_OMIT_WSD.
46a60 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f #define SQLITE_
46a70 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 64 65 66 WSD const. #def
46a80 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 ine GLOBAL(t,v)
46a90 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 5f 77 73 (*(t*)sqlite3_ws
46aa0 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a 29 26 28 d_find((void*)&(
46ab0 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 29 29 0a v), sizeof(v))).
46ac0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
46ad0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 47 4c 3GlobalConfig GL
46ae0 4f 42 41 4c 28 73 74 72 75 63 74 20 53 71 6c 69 OBAL(struct Sqli
46af0 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 6c 69 74 te3Config, sqlit
46b00 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c 49 54 45 e3Config).SQLITE
46b10 5f 41 50 49 20 20 20 69 6e 74 20 73 71 6c 69 74 _API int sqlit
46b20 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 6e 74 20 e3_wsd_init(int
46b30 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 4c 49 54 N, int J);.SQLIT
46b40 45 5f 41 50 49 20 20 20 76 6f 69 64 20 2a 73 71 E_API void *sq
46b50 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 76 lite3_wsd_find(v
46b60 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c 29 3b 0a oid *K, int L);.
46b70 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
46b80 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 20 23 64 SQLITE_WSD . #d
46b90 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 efine GLOBAL(t,v
46ba0 29 20 76 0a 20 20 23 64 65 66 69 6e 65 20 73 71 ) v. #define sq
46bb0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
46bc0 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 0a g sqlite3Config.
46bd0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
46be0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
46bf0 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 73 os are used to s
46c00 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65 72 uppress compiler
46c10 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74 6f warnings and to
46c20 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c 65 61 .** make it clea
46c30 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61 64 65 r to human reade
46c40 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63 74 69 rs when a functi
46c50 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 on parameter is
46c60 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0a 2a 2a deliberately .**
46c70 20 6c 65 66 74 20 75 6e 75 73 65 64 20 77 69 74 left unused wit
46c80 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 hin the body of
46c90 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 a function. This
46ca0 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 usually happens
46cb0 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 6e 63 74 when.** a funct
46cc0 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 76 69 ion is called vi
46cd0 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 a a function poi
46ce0 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 6d 70 6c nter. For exampl
46cf0 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d e the .** implem
46d00 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 entation of an S
46d10 51 4c 20 61 67 67 72 65 67 61 74 65 20 73 74 65 QL aggregate ste
46d20 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 6e p callback may n
46d30 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a 20 70 61 ot use the.** pa
46d40 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 69 rameter indicati
46d50 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ng the number of
46d60 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 arguments passe
46d70 64 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 61 d to the aggrega
46d80 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 6b 6e 6f te,.** if it kno
46d90 77 73 20 74 68 61 74 20 74 68 69 73 20 69 73 20 ws that this is
46da0 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 77 68 65 enforced elsewhe
46db0 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 re..**.** When a
46dc0 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 function parame
46dd0 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20 ter is not used
46de0 61 74 20 61 6c 6c 20 77 69 74 68 69 6e 20 74 68 at all within th
46df0 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 e body of a func
46e00 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 73 20 67 tion,.** it is g
46e10 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 64 20 22 enerally named "
46e20 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 4e 6f 74 NotUsed" or "Not
46e30 55 73 65 64 32 22 20 74 6f 20 6d 61 6b 65 20 74 Used2" to make t
46e40 68 69 6e 67 73 20 65 76 65 6e 20 63 6c 65 61 72 hings even clear
46e50 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 er..** However,
46e60 74 68 65 73 65 20 6d 61 63 72 6f 73 20 6d 61 79 these macros may
46e70 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 6f also be used to
46e80 20 73 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e suppress warnin
46e90 67 73 20 72 65 6c 61 74 65 64 20 74 6f 0a 2a 2a gs related to.**
46ea0 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 parameters that
46eb0 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 may or may not
46ec0 62 65 20 75 73 65 64 20 64 65 70 65 6e 64 69 6e be used dependin
46ed0 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 69 6f 6e g on compilation
46ee0 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 46 6f 72 options..** For
46ef0 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 65 20 70 example those p
46f00 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c 79 20 75 arameters only u
46f10 73 65 64 20 69 6e 20 61 73 73 65 72 74 28 29 20 sed in assert()
46f20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 6e 20 74 statements. In t
46f30 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 20 74 68 hese.** cases th
46f40 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 e parameters are
46f50 20 6e 61 6d 65 64 20 61 73 20 70 65 72 20 74 68 named as per th
46f60 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74 69 e usual conventi
46f70 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ons..*/.#define
46f80 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
46f90 28 78 29 20 28 76 6f 69 64 29 28 78 29 0a 23 64 (x) (void)(x).#d
46fa0 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52 efine UNUSED_PAR
46fb0 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e 55 AMETER2(x,y) UNU
46fc0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29 SED_PARAMETER(x)
46fd0 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 ,UNUSED_PARAMETE
46fe0 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 R(y)../*.** Forw
46ff0 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 ard references t
47000 6f 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2f 0a o structures.*/.
47010 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41 typedef struct A
47020 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 6f 3b 0a ggInfo AggInfo;.
47030 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41 typedef struct A
47040 75 74 68 43 6f 6e 74 65 78 74 20 41 75 74 68 43 uthContext AuthC
47050 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20 ontext;.typedef
47060 73 74 72 75 63 74 20 42 69 74 76 65 63 20 42 69 struct Bitvec Bi
47070 74 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73 74 tvec;.typedef st
47080 72 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 53 ruct RowSet RowS
47090 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 et;.typedef stru
470a0 63 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 ct CollSeq CollS
470b0 65 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 eq;.typedef stru
470c0 63 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e ct Column Column
470d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
470e0 20 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 Db Db;.typedef
470f0 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 struct Schema Sc
47100 68 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 hema;.typedef st
47110 72 75 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a ruct Expr Expr;.
47120 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 typedef struct E
47130 78 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 xprList ExprList
47140 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
47150 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 FKey FKey;.type
47160 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 def struct FuncD
47170 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 ef FuncDef;.type
47180 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 def struct FuncD
47190 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 61 efHash FuncDefHa
471a0 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 sh;.typedef stru
471b0 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74 ct IdList IdList
471c0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
471d0 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 79 Index Index;.ty
471e0 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 pedef struct Key
471f0 43 6c 61 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a Class KeyClass;.
47200 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4b typedef struct K
47210 65 79 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a eyInfo KeyInfo;.
47220 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c typedef struct L
47230 6f 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 ookaside Lookasi
47240 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 de;.typedef stru
47250 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 ct LookasideSlot
47260 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a LookasideSlot;.
47270 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d typedef struct M
47280 6f 64 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 odule Module;.ty
47290 70 65 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d pedef struct Nam
472a0 65 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e eContext NameCon
472b0 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 text;.typedef st
472c0 72 75 63 74 20 50 61 72 73 65 20 50 61 72 73 65 ruct Parse Parse
472d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
472e0 20 53 61 76 65 70 6f 69 6e 74 20 53 61 76 65 70 Savepoint Savep
472f0 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 oint;.typedef st
47300 72 75 63 74 20 53 65 6c 65 63 74 20 53 65 6c 65 ruct Select Sele
47310 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ct;.typedef stru
47320 63 74 20 53 72 63 4c 69 73 74 20 53 72 63 4c 69 ct SrcList SrcLi
47330 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 st;.typedef stru
47340 63 74 20 53 74 72 41 63 63 75 6d 20 53 74 72 41 ct StrAccum StrA
47350 63 63 75 6d 3b 0a 74 79 70 65 64 65 66 20 73 74 ccum;.typedef st
47360 72 75 63 74 20 54 61 62 6c 65 20 54 61 62 6c 65 ruct Table Table
47370 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
47380 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61 62 6c 65 TableLock Table
47390 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 Lock;.typedef st
473a0 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f 6b 65 6e ruct Token Token
473b0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
473c0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 54 72 TriggerStack Tr
473d0 69 67 67 65 72 53 74 61 63 6b 3b 0a 74 79 70 65 iggerStack;.type
473e0 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 def struct Trigg
473f0 65 72 53 74 65 70 20 54 72 69 67 67 65 72 53 74 erStep TriggerSt
47400 65 70 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ep;.typedef stru
47410 63 74 20 54 72 69 67 67 65 72 20 54 72 69 67 67 ct Trigger Trigg
47420 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 er;.typedef stru
47430 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 ct UnpackedRecor
47440 64 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 d UnpackedRecord
47450 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
47460 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 72 3b 0a Walker Walker;.
47470 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 typedef struct W
47480 68 65 72 65 50 6c 61 6e 20 57 68 65 72 65 50 6c herePlan WherePl
47490 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 an;.typedef stru
474a0 63 74 20 57 68 65 72 65 49 6e 66 6f 20 57 68 65 ct WhereInfo Whe
474b0 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 reInfo;.typedef
474c0 73 74 72 75 63 74 20 57 68 65 72 65 4c 65 76 65 struct WhereLeve
474d0 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b 0a 0a 2f l WhereLevel;../
474e0 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f 75 72 63 *.** Defer sourc
474f0 69 6e 67 20 76 64 62 65 2e 68 20 61 6e 64 20 62 ing vdbe.h and b
47500 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 61 66 74 tree.h until aft
47510 65 72 20 74 68 65 20 22 75 38 22 20 61 6e 64 20 er the "u8" and
47520 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 6c 65 72 .** "BusyHandler
47530 22 20 74 79 70 65 64 65 66 73 2e 20 76 64 62 65 " typedefs. vdbe
47540 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73 .h also requires
47550 20 61 20 66 65 77 20 6f 66 20 74 68 65 20 6f 70 a few of the op
47560 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 aque.** pointer
47570 74 79 70 65 73 20 28 69 2e 65 2e 20 46 75 6e 63 types (i.e. Func
47580 44 65 66 29 20 64 65 66 69 6e 65 64 20 61 62 6f Def) defined abo
47590 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ve..*/./********
475a0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 ****** Include b
475b0 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 tree.h in the mi
475c0 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
475d0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
475e0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
475f0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
47600 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a e btree.h ******
47610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47630 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
47640 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
47650 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
47660 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
47670 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
47680 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
47690 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
476a0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
476b0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
476c0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
476d0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
476e0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
476f0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
47700 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
47710 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
47720 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
47730 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
47740 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
47750 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
47760 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
47770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
477a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
477b0 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c This header fil
477c0 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e e defines the in
477d0 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 terface that the
477e0 20 73 71 6c 69 74 65 20 42 2d 54 72 65 65 20 66 sqlite B-Tree f
477f0 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d ile.** subsystem
47800 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 . See comments
47810 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f in the source co
47820 64 65 20 66 6f 72 20 61 20 64 65 74 61 69 6c 65 de for a detaile
47830 64 20 64 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a d description.**
47840 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 69 6e of what each in
47850 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 20 terface routine
47860 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 does..**.** @(#)
47870 20 24 49 64 3a 20 62 74 72 65 65 2e 68 2c 76 20 $Id: btree.h,v
47880 31 2e 31 31 31 20 32 30 30 39 2f 30 33 2f 31 38 1.111 2009/03/18
47890 20 31 30 3a 33 33 3a 30 31 20 64 61 6e 69 65 6c 10:33:01 daniel
478a0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 k1977 Exp $.*/.#
478b0 69 66 6e 64 65 66 20 5f 42 54 52 45 45 5f 48 5f ifndef _BTREE_H_
478c0 0a 23 64 65 66 69 6e 65 20 5f 42 54 52 45 45 5f .#define _BTREE_
478d0 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 69 H_../* TODO: Thi
478e0 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 73 20 s definition is
478f0 6a 75 73 74 20 69 6e 63 6c 75 64 65 64 20 73 6f just included so
47900 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 63 other modules c
47910 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e 65 ompile. It.** ne
47920 65 64 73 20 74 6f 20 62 65 20 72 65 76 69 73 69 eds to be revisi
47930 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ted..*/.#define
47940 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d SQLITE_N_BTREE_M
47950 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 ETA 10../*.** If
47960 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f 6e 2d defined as non-
47970 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 75 75 zero, auto-vacuu
47980 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79 20 m is enabled by
47990 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 77 69 default. Otherwi
479a0 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 62 65 se.** it must be
479b0 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 20 65 turned on for e
479c0 61 63 68 20 64 61 74 61 62 61 73 65 20 75 73 69 ach database usi
479d0 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 6f 5f ng "PRAGMA auto_
479e0 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f 0a vacuum = 1"..*/.
479f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
47a00 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 EFAULT_AUTOVACUU
47a10 4d 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 M. #define SQLI
47a20 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 TE_DEFAULT_AUTOV
47a30 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a ACUUM 0.#endif..
47a40 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 #define BTREE_AU
47a50 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 20 30 20 TOVACUUM_NONE 0
47a60 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 /* Do not
47a70 20 64 6f 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 do auto-vacuum
47a80 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 */.#define BTREE
47a90 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c _AUTOVACUUM_FULL
47aa0 20 31 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 1 /* Do
47ab0 66 75 6c 6c 20 61 75 74 6f 2d 76 61 63 75 75 6d full auto-vacuum
47ac0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
47ad0 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 E_AUTOVACUUM_INC
47ae0 52 20 32 20 20 20 20 20 20 20 20 2f 2a 20 49 6e R 2 /* In
47af0 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
47b00 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 */../*.** Forwa
47b10 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 rd declarations
47b20 6f 66 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a of structure.*/.
47b30 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 typedef struct B
47b40 74 72 65 65 20 42 74 72 65 65 3b 0a 74 79 70 65 tree Btree;.type
47b50 64 65 66 20 73 74 72 75 63 74 20 42 74 43 75 72 def struct BtCur
47b60 73 6f 72 20 42 74 43 75 72 73 6f 72 3b 0a 74 79 sor BtCursor;.ty
47b70 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 53 pedef struct BtS
47b80 68 61 72 65 64 20 42 74 53 68 61 72 65 64 3b 0a hared BtShared;.
47b90 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 typedef struct B
47ba0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 42 treeMutexArray B
47bb0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3b 0a treeMutexArray;.
47bc0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 ./*.** This stru
47bd0 63 74 75 72 65 20 72 65 63 6f 72 64 73 20 61 6c cture records al
47be0 6c 20 6f 66 20 74 68 65 20 42 74 72 65 65 73 20 l of the Btrees
47bf0 74 68 61 74 20 6e 65 65 64 20 74 6f 20 68 6f 6c that need to hol
47c00 64 0a 2a 2a 20 61 20 6d 75 74 65 78 20 62 65 66 d.** a mutex bef
47c10 6f 72 65 20 77 65 20 65 6e 74 65 72 20 73 71 6c ore we enter sql
47c20 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 ite3VdbeExec().
47c30 20 54 68 65 20 42 74 72 65 65 73 20 61 72 65 0a The Btrees are.
47c40 2a 2a 20 61 72 65 20 70 6c 61 63 65 64 20 69 6e ** are placed in
47c50 20 61 42 74 72 65 65 5b 5d 20 69 6e 20 6f 72 64 aBtree[] in ord
47c60 65 72 20 6f 66 20 61 42 74 72 65 65 5b 5d 2d 3e er of aBtree[]->
47c70 70 42 74 2e 20 20 54 68 61 74 20 77 61 79 2c 0a pBt. That way,.
47c80 2a 2a 20 77 65 20 63 61 6e 20 61 6c 77 61 79 73 ** we can always
47c90 20 6c 6f 63 6b 20 61 6e 64 20 75 6e 6c 6f 63 6b lock and unlock
47ca0 20 74 68 65 6d 20 61 6c 6c 20 71 75 69 63 6b 6c them all quickl
47cb0 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 y..*/.struct Btr
47cc0 65 65 4d 75 74 65 78 41 72 72 61 79 20 7b 0a 20 eeMutexArray {.
47cd0 20 69 6e 74 20 6e 4d 75 74 65 78 3b 0a 20 20 42 int nMutex;. B
47ce0 74 72 65 65 20 2a 61 42 74 72 65 65 5b 53 51 4c tree *aBtree[SQL
47cf0 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 ITE_MAX_ATTACHED
47d00 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 54 45 +1];.};...SQLITE
47d10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
47d20 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 ite3BtreeOpen(.
47d30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
47d40 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d lename, /* Nam
47d50 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 e of database fi
47d60 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 le to open */.
47d70 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
47d80 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f /* Asso
47d90 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 ciated database
47da0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
47db0 42 74 72 65 65 20 2a 2a 2c 20 20 20 20 20 20 20 Btree **,
47dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
47dd0 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a 20 68 rn open Btree* h
47de0 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ere */. int fla
47df0 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
47e00 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 /* Flags */.
47e10 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 int vfsFlags
47e20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
47e30 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 s passed through
47e40 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a 2f 0a to VFS open */.
47e50 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 );../* The flags
47e60 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
47e70 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 20 63 lite3BtreeOpen c
47e80 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73 an be the bitwis
47e90 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 e or of the.** f
47ea0 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e ollowing values.
47eb0 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54 68 .**.** NOTE: Th
47ec0 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 74 20 ese values must
47ed0 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65 73 match the corres
47ee0 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f 20 76 ponding PAGER_ v
47ef0 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61 67 65 alues in.** page
47f00 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 r.h..*/.#define
47f10 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e BTREE_OMIT_JOURN
47f20 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e 6f 74 AL 1 /* Do not
47f30 20 75 73 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e use journal. N
47f40 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 o argument */.#d
47f50 65 66 69 6e 65 20 42 54 52 45 45 5f 4e 4f 5f 52 efine BTREE_NO_R
47f60 45 41 44 4c 4f 43 4b 20 20 20 32 20 20 2f 2a 20 EADLOCK 2 /*
47f70 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f Omit readlocks o
47f80 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 n readonly files
47f90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
47fa0 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 20 20 E_MEMORY
47fb0 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 4 /* In-memory
47fc0 44 42 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 DB. No argument
47fd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
47fe0 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 E_READONLY
47ff0 38 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64 8 /* Open the d
48000 61 74 61 62 61 73 65 20 69 6e 20 72 65 61 64 2d atabase in read-
48010 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 only mode */.#de
48020 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44 57 fine BTREE_READW
48030 52 49 54 45 20 20 20 20 31 36 20 20 2f 2a 20 4f RITE 16 /* O
48040 70 65 6e 20 66 6f 72 20 62 6f 74 68 20 72 65 61 pen for both rea
48050 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 ding and writing
48060 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
48070 45 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 33 E_CREATE 3
48080 32 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 2 /* Create the
48090 20 64 61 74 61 62 61 73 65 20 69 66 20 69 74 20 database if it
480a0 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 2a does not exist *
480b0 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 /..SQLITE_PRIVAT
480c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
480d0 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a 29 3b eeClose(Btree*);
480e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
480f0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
48100 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74 72 SetCacheSize(Btr
48110 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ee*,int);.SQLITE
48120 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
48130 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 ite3BtreeSetSafe
48140 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a 2c 69 tyLevel(Btree*,i
48150 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
48160 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
48170 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 te3BtreeSyncDisa
48180 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a 53 51 bled(Btree*);.SQ
48190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
481a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
481b0 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a 2c PageSize(Btree*,
481c0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
481d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
481e0 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
481f0 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 Size(Btree*);.SQ
48200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
48210 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 sqlite3BtreeMax
48220 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 2a PageCount(Btree*
48230 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
48240 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48250 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 3BtreeGetReserve
48260 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
48270 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
48280 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74 6f ite3BtreeSetAuto
48290 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 2c 20 Vacuum(Btree *,
482a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
482b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
482c0 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 BtreeGetAutoVacu
482d0 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c um(Btree *);.SQL
482e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
482f0 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
48300 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 2c 69 6e nTrans(Btree*,in
48310 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
48320 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
48330 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e reeCommitPhaseOn
48340 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e 73 74 20 e(Btree*, const
48350 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 3b 0a char *zMaster);.
48360 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
48370 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
48380 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 ommitPhaseTwo(Bt
48390 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
483a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
483b0 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 3BtreeCommit(Btr
483c0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
483d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
483e0 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 BtreeRollback(Bt
483f0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
48400 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48410 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 3BtreeBeginStmt(
48420 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c Btree*,int);.SQL
48430 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
48440 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 sqlite3BtreeCrea
48450 74 65 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 teTable(Btree*,
48460 69 6e 74 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 int*, int flags)
48470 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
48480 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
48490 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 eIsInTrans(Btree
484a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
484b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
484c0 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 reeIsInReadTrans
484d0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
484e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
484f0 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 ite3BtreeIsInBac
48500 6b 75 70 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c kup(Btree*);.SQL
48510 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
48520 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 *sqlite3BtreeSc
48530 68 65 6d 61 28 42 74 72 65 65 20 2a 2c 20 69 6e hema(Btree *, in
48540 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 t, void(*)(void
48550 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *));.SQLITE_PRIV
48560 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
48570 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 treeSchemaLocked
48580 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 (Btree *);.SQLIT
48590 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
485a0 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 lite3BtreeLockTa
485b0 62 6c 65 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 ble(Btree *, int
485c0 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 , u8);.SQLITE_PR
485d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
485e0 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 3BtreeSavepoint(
485f0 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 69 6e Btree *, int, in
48600 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 t);..SQLITE_PRIV
48610 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
48620 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 sqlite3BtreeGetF
48630 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 ilename(Btree *)
48640 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
48650 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
48660 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 ite3BtreeGetJour
48670 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 nalname(Btree *)
48680 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
48690 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
486a0 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 eCopyFile(Btree
486b0 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51 *, Btree *);..SQ
486c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
486d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 sqlite3BtreeInc
486e0 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 rVacuum(Btree *)
486f0 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 ;../* The flags
48700 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
48710 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 ite3BtreeCreateT
48720 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 20 able can be the
48730 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66 bitwise OR.** of
48740 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
48750 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 lags:.*/.#define
48760 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 BTREE_INTKEY
48770 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 1 /* Table
48780 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 has only 64-bit
48790 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b signed integer k
487a0 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 eys */.#define B
487b0 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 TREE_ZERODATA
487c0 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 2 /* Table ha
487d0 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f s keys only - no
487e0 20 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 data */.#define
487f0 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 20 BTREE_LEAFDATA
48800 20 20 34 20 20 20 20 2f 2a 20 44 61 74 61 20 73 4 /* Data s
48810 74 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 73 20 tored in leaves
48820 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 20 49 only. Implies I
48830 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45 NTKEY */..SQLITE
48840 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
48850 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 ite3BtreeDropTab
48860 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 le(Btree*, int,
48870 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
48880 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48890 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 3BtreeClearTable
488a0 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e (Btree*, int, in
488b0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
488c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
488d0 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 treeGetMeta(Btre
488e0 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 e*, int idx, u32
488f0 20 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 *pValue);.SQLIT
48900 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
48910 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
48920 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 Meta(Btree*, int
48930 20 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 idx, u32 value)
48940 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
48950 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
48960 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 eeTripAllCursors
48970 28 42 74 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a (Btree*, int);..
48980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
48990 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
489a0 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c ursor(. Btree*,
489b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
489c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
489d0 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e BTree containin
489e0 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 g table to open
489f0 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c */. int iTable,
48a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
48a20 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 ex of root page
48a30 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c */. int wrFlag,
48a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 /* 1 f
48a60 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20 66 or writing. 0 f
48a70 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a or read-only */.
48a80 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f struct KeyInfo
48a90 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *,
48aa0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
48ab0 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 argument to comp
48ac0 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a are function */.
48ad0 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
48ae0 73 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 sor
48af0 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
48b00 74 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 to write cursor
48b10 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a structure */.);.
48b20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
48b30 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
48b40 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b ursorSize(void);
48b50 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
48b60 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
48b70 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 eCloseCursor(BtC
48b80 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ursor*);.SQLITE_
48b90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
48ba0 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 0a te3BtreeMoveto(.
48bb0 20 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 63 BtCursor*,. c
48bc0 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c onst void *pKey,
48bd0 0a 20 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 69 . i64 nKey,. i
48be0 6e 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a nt bias,. int *
48bf0 70 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 pRes.);.SQLITE_P
48c00 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
48c10 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
48c20 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f acked(. BtCurso
48c30 72 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 r*,. UnpackedRe
48c40 63 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20 cord *pUnKey,.
48c50 69 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 69 6e i64 intKey,. in
48c60 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 t bias,. int *p
48c70 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Res.);.SQLITE_PR
48c80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48c90 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 3BtreeCursorHasM
48ca0 6f 76 65 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 oved(BtCursor*,
48cb0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
48cc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48cd0 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 3BtreeDelete(BtC
48ce0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ursor*);.SQLITE_
48cf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
48d00 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 42 te3BtreeInsert(B
48d10 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 tCursor*, const
48d20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 void *pKey, i64
48d30 6e 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 nKey,.
48d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48d50 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f const vo
48d60 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e id *pData, int n
48d70 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 Data,.
48d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48d90 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 int nZer
48da0 6f 2c 20 69 6e 74 20 62 69 61 73 29 3b 0a 53 51 o, int bias);.SQ
48db0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
48dc0 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 sqlite3BtreeFir
48dd0 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e st(BtCursor*, in
48de0 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 t *pRes);.SQLITE
48df0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
48e00 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 ite3BtreeLast(Bt
48e10 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 Cursor*, int *pR
48e20 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 es);.SQLITE_PRIV
48e30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
48e40 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f treeNext(BtCurso
48e50 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a r*, int *pRes);.
48e60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
48e70 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 nt sqlite3BtreeE
48e80 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 of(BtCursor*);.S
48e90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
48ea0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c t sqlite3BtreeFl
48eb0 61 67 73 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a ags(BtCursor*);.
48ec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
48ed0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 nt sqlite3BtreeP
48ee0 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 revious(BtCursor
48ef0 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 *, int *pRes);.S
48f00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
48f10 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 t sqlite3BtreeKe
48f20 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c ySize(BtCursor*,
48f30 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 i64 *pSize);.SQ
48f40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
48f50 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
48f60 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 (BtCursor*, u32
48f70 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c offset, u32 amt,
48f80 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f void*);.SQLITE_
48f90 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 PRIVATE sqlite3
48fa0 2a 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 *sqlite3BtreeCur
48fb0 73 6f 72 44 62 28 63 6f 6e 73 74 20 42 74 43 75 sorDb(const BtCu
48fc0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
48fd0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 RIVATE const voi
48fe0 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b d *sqlite3BtreeK
48ff0 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 eyFetch(BtCursor
49000 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 *, int *pAmt);.S
49010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
49020 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
49030 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 3BtreeDataFetch(
49040 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
49050 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pAmt);.SQLITE_PR
49060 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
49070 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42 3BtreeDataSize(B
49080 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70 tCursor*, u32 *p
49090 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Size);.SQLITE_PR
490a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
490b0 33 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72 3BtreeData(BtCur
490c0 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 sor*, u32 offset
490d0 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a , u32 amt, void*
490e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
490f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
49100 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 reeSetCachedRowi
49110 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 73 71 6c d(BtCursor*, sql
49120 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c ite3_int64);.SQL
49130 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
49140 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
49150 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52 3BtreeGetCachedR
49160 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a 29 3b owid(BtCursor*);
49170 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
49180 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
49190 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 reeIntegrityChec
491a0 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61 k(Btree*, int *a
491b0 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c Root, int nRoot,
491c0 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c int, int*);.SQL
491d0 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 72 75 ITE_PRIVATE stru
491e0 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 ct Pager *sqlite
491f0 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 65 3BtreePager(Btre
49200 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 e*);..SQLITE_PRI
49210 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
49220 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43 BtreePutData(BtC
49230 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 ursor*, u32 offs
49240 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 et, u32 amt, voi
49250 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d*);.SQLITE_PRIV
49260 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
49270 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c BtreeCacheOverfl
49280 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a ow(BtCursor *);.
49290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
492a0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
492b0 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 ClearCursor(BtCu
492c0 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 6e 64 65 rsor *);..#ifnde
492d0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 f SQLITE_OMIT_BT
492e0 52 45 45 43 4f 55 4e 54 0a 53 51 4c 49 54 45 5f REECOUNT.SQLITE_
492f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
49300 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 te3BtreeCount(Bt
49310 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 Cursor *, i64 *)
49320 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
49330 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
49340 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
49350 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
49360 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 2a orInfo(BtCursor*
49370 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 , int*, int);.SQ
49380 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
49390 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 d sqlite3BtreeCu
493a0 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 2a 29 rsorList(Btree*)
493b0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
493c0 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 75 73 If we are not us
493d0 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 68 65 ing shared cache
493e0 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 , then there is
493f0 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 75 73 no need to.** us
49400 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 63 63 e mutexes to acc
49410 65 73 73 20 74 68 65 20 42 74 53 68 61 72 65 64 ess the BtShared
49420 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 53 6f structures. So
49430 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 6e 74 make the.** Ent
49440 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 72 6f er and Leave pro
49450 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 2e 0a cedures no-ops..
49460 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
49470 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
49480 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c ED_CACHE) && SQL
49490 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53 ITE_THREADSAFE.S
494a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
494b0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
494c0 65 45 6e 74 65 72 28 42 74 72 65 65 2a 29 3b 0a eEnter(Btree*);.
494d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
494e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
494f0 65 65 4c 65 61 76 65 28 42 74 72 65 65 2a 29 3b eeLeave(Btree*);
49500 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
49510 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e /* This routin
49520 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 e is used inside
49530 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
49540 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 51 ents only. */.SQ
49550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
49560 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 nt sqlite3BtreeH
49570 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 2a oldsMutex(Btree*
49580 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 );.#endif.SQLITE
49590 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
495a0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
495b0 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 rCursor(BtCursor
495c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
495d0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
495e0 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 3BtreeLeaveCurso
495f0 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 r(BtCursor*);.SQ
49600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
49610 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
49620 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 EnterAll(sqlite3
49630 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
49640 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
49650 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 3BtreeLeaveAll(s
49660 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 qlite3*);.#ifnde
49670 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 f NDEBUG. /* Th
49680 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
49690 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ed inside assert
496a0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e () statements on
496b0 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly. */.SQLITE_PR
496c0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
496d0 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c te3BtreeHoldsAll
496e0 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a Mutexes(sqlite3*
496f0 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 );.#endif.SQLITE
49700 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
49710 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
49720 78 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 xArrayEnter(Btre
49730 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 eMutexArray*);.S
49740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
49750 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
49760 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
49770 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 (BtreeMutexArray
49780 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
49790 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
497a0 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3BtreeMutexArray
497b0 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 Insert(BtreeMute
497c0 78 41 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29 xArray*, Btree*)
497d0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
497e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
497f0 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 er(X).# define s
49800 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
49810 28 58 29 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 (X).#ifndef NDEB
49820 55 47 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 UG. /* This rou
49830 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 tine is used ins
49840 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
49850 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f tements only. */
49860 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
49870 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 3BtreeHoldsMutex
49880 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 23 20 64 (X) 1.#endif.# d
49890 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 efine sqlite3Btr
498a0 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29 eeEnterCursor(X)
498b0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
498c0 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 3BtreeLeaveCurso
498d0 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 r(X).# define sq
498e0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
498f0 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 ll(X).# define s
49900 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
49910 41 6c 6c 28 58 29 0a 23 69 66 6e 64 65 66 20 4e All(X).#ifndef N
49920 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 69 73 20 DEBUG. /* This
49930 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
49940 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
49950 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e statements only.
49960 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 71 6c */.# define sql
49970 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c ite3BtreeHoldsAl
49980 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23 65 lMutexes(X) 1.#e
49990 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 73 71 ndif.# define sq
499a0 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
499b0 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20 64 rrayEnter(X).# d
499c0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 efine sqlite3Btr
499d0 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 eeMutexArrayLeav
499e0 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 e(X).# define sq
499f0 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
49a00 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29 0a rrayInsert(X,Y).
49a10 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 20 #endif...#endif
49a20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f 0a /* _BTREE_H_ */.
49a30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
49a40 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 20 End of btree.h
49a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
49a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
49a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
49a80 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
49a90 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
49aa0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
49ab0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
49ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
49ad0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
49ae0 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 20 Include vdbe.h
49af0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
49b00 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
49b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
49b20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
49b30 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 Begin file vdbe
49b40 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
49b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
49b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
49b70 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
49b80 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
49b90 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
49ba0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
49bb0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
49bc0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
49bd0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
49be0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
49bf0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
49c00 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
49c10 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
49c20 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
49c30 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
49c40 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
49c50 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
49c60 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
49c70 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
49c80 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
49c90 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
49ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
49cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
49cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
49cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
49ce0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 65 *******.** Heade
49cf0 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 56 r file for the V
49d00 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 65 20 irtual DataBase
49d10 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a 2a Engine (VDBE).**
49d20 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 .** This header
49d30 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 defines the inte
49d40 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 72 rface to the vir
49d50 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e tual database en
49d60 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 2e gine.** or VDBE.
49d70 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c 65 The VDBE imple
49d80 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61 63 ments an abstrac
49d90 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 72 t machine that r
49da0 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 20 uns a.** simple
49db0 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 73 program to acces
49dc0 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 s and modify the
49dd0 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 underlying data
49de0 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a base..**.** $Id:
49df0 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 34 30 20 vdbe.h,v 1.140
49e00 32 30 30 39 2f 30 32 2f 31 39 20 31 34 3a 33 39 2009/02/19 14:39
49e10 3a 32 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :25 danielk1977
49e20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
49e30 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f _SQLITE_VDBE_H_
49e40 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 .#define _SQLITE
49e50 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 _VDBE_H_../*.**
49e60 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69 73 A single VDBE is
49e70 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 an opaque struc
49e80 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62 65 ture named "Vdbe
49e90 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e 65 ". Only routine
49ea0 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 72 s.** in the sour
49eb0 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56 64 ce file sqliteVd
49ec0 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 64 be.c are allowed
49ed0 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73 69 to see the insi
49ee0 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 des.** of this s
49ef0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 tructure..*/.typ
49f00 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
49f10 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 Vdbe;../*.** Th
49f20 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66 e names of the f
49f30 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64 ollowing types d
49f40 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49 eclared in vdbeI
49f50 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72 65 nt.h are require
49f60 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 62 d.** for the Vdb
49f70 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a eOp definition..
49f80 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
49f90 74 20 56 64 62 65 46 75 6e 63 20 56 64 62 65 46 t VdbeFunc VdbeF
49fa0 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 unc;.typedef str
49fb0 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 0a 2f 2a uct Mem Mem;../*
49fc0 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73 .** A single ins
49fd0 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 truction of the
49fe0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 virtual machine
49ff0 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a has an opcode.**
4a000 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20 and as many as
4a010 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20 three operands.
4a020 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e The instruction
4a030 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a 20 is recorded.**
4a040 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f as an instance o
4a050 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
4a060 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 74 structure:.*/.st
4a070 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 20 ruct VdbeOp {.
4a080 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 u8 opcode;
4a090 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 /* What oper
4a0a0 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d ation to perform
4a0b0 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 */. signed cha
4a0c0 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e 65 r p4type; /* One
4a0d0 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20 63 of the P4_xxx c
4a0e0 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34 20 onstants for p4
4a0f0 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 3b */. u8 opflags;
4a100 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 /* Not
4a110 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a currently used *
4a120 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20 20 /. u8 p5;
4a130 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74 68 /* Fifth
4a140 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e parameter is an
4a150 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 unsigned charac
4a160 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b ter */. int p1;
4a170 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4a180 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f First operand */
4a190 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 . int p2;
4a1a0 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 /* Second
4a1b0 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 parameter (ofte
4a1c0 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 n the jump desti
4a1d0 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e 74 nation) */. int
4a1e0 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 p3;
4a1f0 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 /* The third pa
4a200 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e 69 rameter */. uni
4a210 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 on {
4a220 20 2f 2a 20 66 6f 72 74 68 20 70 61 72 61 6d 65 /* forth parame
4a230 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ter */. int i
4a240 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4a250 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c /* Integer val
4a260 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d 50 34 ue if p4type==P4
4a270 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76 6f _INT32 */. vo
4a280 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 id *p;
4a290 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 63 20 /* Generic
4a2a0 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 63 pointer */. c
4a2b0 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 har *z;
4a2c0 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
4a2d0 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73 74 72 to data for str
4a2e0 69 6e 67 20 28 63 68 61 72 20 61 72 72 61 79 29 ing (char array)
4a2f0 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20 69 36 types */. i6
4a300 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20 20 20 4 *pI64;
4a310 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
4a320 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 n p4type is P4_I
4a330 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75 62 NT64 */. doub
4a340 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 le *pReal;
4a350 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
4a360 70 34 74 79 70 65 20 69 73 20 50 34 5f 52 45 41 p4type is P4_REA
4a370 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 L */. FuncDef
4a380 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 *pFunc;
4a390 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 /* Used when p4t
4a3a0 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43 44 45 ype is P4_FUNCDE
4a3b0 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75 6e F */. VdbeFun
4a3c0 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 20 c *pVdbeFunc;
4a3d0 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 /* Used when p4t
4a3e0 79 70 65 20 69 73 20 50 34 5f 56 44 42 45 46 55 ype is P4_VDBEFU
4a3f0 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 NC */. CollSe
4a400 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 q *pColl;
4a410 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
4a420 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c 53 type is P4_COLLS
4a430 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a 70 EQ */. Mem *p
4a440 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
4a450 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
4a460 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d 20 2a type is P4_MEM *
4a470 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 /. sqlite3_vt
4a480 61 62 20 2a 70 56 74 61 62 3b 20 20 20 2f 2a 20 ab *pVtab; /*
4a490 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 Used when p4type
4a4a0 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a 20 is P4_VTAB */.
4a4b0 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 KeyInfo *pKey
4a4c0 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 Info; /* Use
4a4d0 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4a4e0 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a 20 P4_KEYINFO */.
4a4f0 20 20 20 69 6e 74 20 2a 61 69 3b 20 20 20 20 20 int *ai;
4a500 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
4a510 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4a520 20 50 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f 0a P4_INTARRAY */.
4a530 20 20 7d 20 70 34 3b 0a 23 69 66 64 65 66 20 53 } p4;.#ifdef S
4a540 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 63 68 QLITE_DEBUG. ch
4a550 61 72 20 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 ar *zComment;
4a560 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e /* Commen
4a570 74 20 74 6f 20 69 6d 70 72 6f 76 65 20 72 65 61 t to improve rea
4a580 64 61 62 69 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 dability */.#end
4a590 69 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 if.#ifdef VDBE_P
4a5a0 52 4f 46 49 4c 45 0a 20 20 69 6e 74 20 63 6e 74 ROFILE. int cnt
4a5b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4a5c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
4a5d0 69 6d 65 73 20 74 68 69 73 20 69 6e 73 74 72 75 imes this instru
4a5e0 63 74 69 6f 6e 20 77 61 73 20 65 78 65 63 75 74 ction was execut
4a5f0 65 64 20 2a 2f 0a 20 20 75 36 34 20 63 79 63 6c ed */. u64 cycl
4a600 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 es;
4a610 20 2f 2a 20 54 6f 74 61 6c 20 74 69 6d 65 20 73 /* Total time s
4a620 70 65 6e 74 20 65 78 65 63 75 74 69 6e 67 20 74 pent executing t
4a630 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
4a640 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 */.#endif.};.typ
4a650 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
4a660 4f 70 20 56 64 62 65 4f 70 3b 0a 0a 2f 2a 0a 2a Op VdbeOp;../*.*
4a670 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 73 * A smaller vers
4a680 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 73 ion of VdbeOp us
4a690 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65 41 ed for the VdbeA
4a6a0 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 74 ddOpList() funct
4a6b0 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 ion because.** i
4a6c0 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73 20 t takes up less
4a6d0 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 space..*/.struct
4a6e0 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20 VdbeOpList {.
4a6f0 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 u8 opcode;
4a700 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 /* What oper
4a710 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d ation to perform
4a720 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 */. signed cha
4a730 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 72 r p1; /* Fir
4a740 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 st operand */.
4a750 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b 20 signed char p2;
4a760 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 /* Second pa
4a770 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74 rameter (often t
4a780 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 he jump destinat
4a790 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 64 ion) */. signed
4a7a0 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f 2a char p3; /*
4a7b0 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 Third parameter
4a7c0 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 */.};.typedef s
4a7d0 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 truct VdbeOpList
4a7e0 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a VdbeOpList;../*
4a7f0 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
4a800 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 33 74 es of VdbeOp.p3t
4a810 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ype.*/.#define P
4a820 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20 20 4_NOTUSED 0
4a830 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61 6d /* The P4 param
4a840 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 eter is not used
4a850 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 44 */.#define P4_D
4a860 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a YNAMIC (-1) /*
4a870 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 Pointer to a st
4a880 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 ring obtained fr
4a890 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
4a8a0 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f ) */.#define P4_
4a8b0 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20 2f STATIC (-2) /
4a8c0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 * Pointer to a s
4a8d0 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a tatic string */.
4a8e0 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53 #define P4_COLLS
4a8f0 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 20 EQ (-4) /* P4
4a900 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
4a910 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 a CollSeq struct
4a920 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
4a930 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29 20 4_FUNCDEF (-5)
4a940 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
4a950 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65 66 ter to a FuncDef
4a960 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
4a970 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f efine P4_KEYINFO
4a980 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 73 (-6) /* P4 is
4a990 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4a9a0 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
4a9b0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
4a9c0 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20 2f VDBEFUNC (-7) /
4a9d0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
4a9e0 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63 20 r to a VdbeFunc
4a9f0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 structure */.#de
4aa00 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 20 fine P4_MEM
4aa10 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 20 (-8) /* P4 is
4aa20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d a pointer to a M
4aa30 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72 65 em* structure
4aa40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 54 */.#define P4_T
4aa50 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f 2a RANSIENT (-9) /*
4aa60 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
4aa70 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 to a transient
4aa80 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
4aa90 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28 2d e P4_VTAB (-
4aaa0 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20 70 10) /* P4 is a p
4aab0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c ointer to an sql
4aac0 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 ite3_vtab struct
4aad0 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
4aae0 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 29 4_MPRINTF (-11)
4aaf0 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72 69 /* P4 is a stri
4ab00 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ng obtained from
4ab10 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 sqlite3_mprintf
4ab20 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 () */.#define P4
4ab30 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29 20 _REAL (-12)
4ab40 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 /* P4 is a 64-bi
4ab50 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 t floating point
4ab60 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e value */.#defin
4ab70 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28 2d e P4_INT64 (-
4ab80 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 13) /* P4 is a 6
4ab90 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 4-bit signed int
4aba0 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 eger */.#define
4abb0 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 34 P4_INT32 (-14
4abc0 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32 2d ) /* P4 is a 32-
4abd0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
4abe0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 er */.#define P4
4abf0 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29 20 _INTARRAY (-15)
4ac00 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74 6f /* P4 is a vecto
4ac10 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 65 r of 32-bit inte
4ac20 67 65 72 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e gers */../* When
4ac30 20 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 adding a P4 arg
4ac40 75 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b ument using P4_K
4ac50 45 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f EYINFO, a copy o
4ac60 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 f the KeyInfo st
4ac70 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 ructure.** is ma
4ac80 64 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 de. That copy i
4ac90 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 s freed when the
4aca0 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a Vdbe is finaliz
4acb0 65 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a ed. But if the.
4acc0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 ** argument is P
4acd0 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 4_KEYINFO_HANDOF
4ace0 46 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e F, the passed in
4acf0 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 pointer is used
4ad00 2e 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 . It still.** g
4ad10 65 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 ets freed when t
4ad20 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c he Vdbe is final
4ad30 69 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c ized so it still
4ad40 20 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 should be obtai
4ad50 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 ned.** from a si
4ad60 6e 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f ngle sqliteMallo
4ad70 63 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 c(). But no cop
4ad80 79 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 y is made and th
4ad90 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e e calling.** fun
4ada0 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f ction should *no
4adb0 74 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 t* try to free t
4adc0 68 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 he KeyInfo..*/.#
4add0 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 define P4_KEYINF
4ade0 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a O_HANDOFF (-16).
4adf0 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e #define P4_KEYIN
4ae00 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 FO_STATIC (-17)
4ae10 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 ../*.** The Vdbe
4ae20 2e 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 .aColName array
4ae30 63 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 contains 5n Mem
4ae40 73 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 structures, wher
4ae50 65 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e e n is the .** n
4ae60 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
4ae70 20 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 of data returne
4ae80 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 d by the stateme
4ae90 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 nt..*/.#define C
4aea0 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 OLNAME_NAME
4aeb0 30 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 0.#define COLNAM
4aec0 45 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 E_DECLTYPE 1.#de
4aed0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 fine COLNAME_DAT
4aee0 41 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 ABASE 2.#define
4aef0 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 COLNAME_TABLE
4af00 20 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 3.#define COLNA
4af10 4d 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 ME_COLUMN 4.#i
4af20 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
4af30 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 LE_COLUMN_METADA
4af40 54 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e TA.# define COLN
4af50 41 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 AME_N 5
4af60 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
4af70 20 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d COLNAME_xxx sym
4af80 62 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 bols */.#else.#
4af90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
4afa0 54 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 T_DECLTYPE.# d
4afb0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
4afc0 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 1 /* S
4afd0 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 tore only the na
4afe0 6d 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 me */.# else.#
4aff0 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f define COLNAME_
4b000 4e 20 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a N 2 /*
4b010 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 Store the name
4b020 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a and decltype */.
4b030 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a # endif.#endif..
4b040 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
4b050 69 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 ing macro conver
4b060 74 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 ts a relative ad
4b070 64 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 dress in the p2
4b080 66 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 field.** of a Vd
4b090 62 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 beOp structure i
4b0a0 6e 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e nto a negative n
4b0b0 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a umber so that .*
4b0c0 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 * sqlite3VdbeAdd
4b0d0 4f 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 OpList() knows t
4b0e0 68 61 74 20 74 68 65 20 61 64 64 72 65 73 73 20 hat the address
4b0f0 69 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 is relative. Ca
4b100 6c 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 lling.** the mac
4b110 72 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 ro again restore
4b120 73 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a s the address..*
4b130 2f 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 /.#define ADDR(X
4b140 29 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a ) (-1-(X))../*.
4b150 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 ** The makefile
4b160 73 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 scans the vdbe.c
4b170 20 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 source file and
4b180 20 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 creates the "op
4b190 63 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 codes.h".** head
4b1a0 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 er file that def
4b1b0 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f ines a number fo
4b1c0 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 r each opcode us
4b1d0 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a ed by the VDBE..
4b1e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4b1f0 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 ** Include opcod
4b200 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 es.h in the midd
4b210 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a le of vdbe.h ***
4b220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b230 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4b240 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 ** Begin file op
4b250 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a codes.h ********
4b260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b280 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 */./* Automatica
4b290 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 lly generated.
4b2a0 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f Do not edit */./
4b2b0 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f * See the mkopco
4b2c0 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 deh.awk script f
4b2d0 6f 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 or details */.#d
4b2e0 65 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 efine OP_VNext
4b2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 1.
4b310 23 64 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e #define OP_Affin
4b320 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20 20 ity
4b330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b340 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 2.#define OP_Col
4b350 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 umn
4b360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b370 20 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 3.#define OP_S
4b380 65 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 etCookie
4b390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b3a0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 4.#define OP
4b3b0 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 _Seek
4b3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b3d0 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 5.#define
4b3e0 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 OP_Real
4b3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b400 20 20 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 130 /* s
4b410 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 ame as TK_FLOAT
4b420 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4b430 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20 20 20 _Sequence
4b440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b450 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
4b460 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 OP_Savepoint
4b470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b480 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 7.#defin
4b490 65 20 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 e OP_Ge
4b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b4b0 20 20 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a 78 /*
4b4c0 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 same as TK_GE
4b4d0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4b4e0 4f 50 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20 OP_RowKey
4b4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b500 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 8.#defin
4b510 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20 e OP_SCopy
4b520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b530 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 9.#def
4b540 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20 ine OP_Eq
4b550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b560 20 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20 74
4b570 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 /* same as TK_EQ
4b580 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
4b590 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 e OP_OpenWrite
4b5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b5b0 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 10.#def
4b5c0 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 ine OP_NotNull
4b5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b5e0 20 20 20 20 20 20 20 20 20 20 20 37 32 20 20 20 72
4b5f0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f /* same as TK_NO
4b600 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e TNULL */.#defin
4b610 65 20 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 e OP_If
4b620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b630 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 11.#def
4b640 69 6e 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 ine OP_ToInt
4b650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b660 20 20 20 20 20 20 20 20 20 20 31 34 34 20 20 20 144
4b670 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
4b680 5f 49 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e _INT */.#defin
4b690 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 e OP_String8
4b6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b6b0 20 20 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 94 /*
4b6c0 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 same as TK_STRI
4b6d0 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NG */.#define
4b6e0 4f 50 5f 56 52 6f 77 69 64 20 20 20 20 20 20 20 OP_VRowid
4b6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b700 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 12.#defin
4b710 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 20 e OP_CollSeq
4b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b730 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 13.#def
4b740 69 6e 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 ine OP_OpenRead
4b750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b760 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 14.#d
4b770 65 66 69 6e 65 20 4f 50 5f 45 78 70 69 72 65 20 efine OP_Expire
4b780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b790 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 15.
4b7a0 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 74 6f 43 #define OP_AutoC
4b7b0 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 ommit
4b7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4b7d0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 74 20 6.#define OP_Gt
4b7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b800 20 37 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 75 /* same as
4b810 20 54 4b 5f 47 54 20 20 20 20 20 20 20 2a 2f 0a TK_GT */.
4b820 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 63 #define OP_Pagec
4b830 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 ount
4b840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4b850 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 7.#define OP_Int
4b860 65 67 72 69 74 79 43 6b 20 20 20 20 20 20 20 20 egrityCk
4b870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b880 20 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 18.#define OP_S
4b890 6f 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ort
4b8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b8b0 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 20.#define OP
4b8c0 5f 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 20 _Copy
4b8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b8e0 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 21.#define
4b8f0 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20 20 20 OP_Trace
4b900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b910 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 22.#defin
4b920 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20 e OP_Function
4b930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b940 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 23.#def
4b950 69 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20 20 20 ine OP_IfNeg
4b960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b970 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 24.#d
4b980 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20 efine OP_And
4b990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 37 20 67
4b9b0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4b9c0 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 AND */.#def
4b9d0 69 6e 65 20 4f 50 5f 53 75 62 74 72 61 63 74 20 ine OP_Subtract
4b9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b9f0 20 20 20 20 20 20 20 20 20 20 20 38 35 20 20 20 85
4ba00 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 /* same as TK_MI
4ba10 4e 55 53 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e NUS */.#defin
4ba20 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20 20 20 20 e OP_Noop
4ba30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ba40 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 25.#def
4ba50 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 ine OP_Return
4ba60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ba70 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 26.#d
4ba80 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 efine OP_Remaind
4ba90 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 er
4baa0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 20 88
4bab0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4bac0 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 REM */.#def
4bad0 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 ine OP_NewRowid
4bae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4baf0 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 27.#d
4bb00 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c efine OP_Multipl
4bb10 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4bb20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36 20 86
4bb30 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4bb40 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 STAR */.#def
4bb50 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 ine OP_Variable
4bb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bb70 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 28.#d
4bb80 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20 efine OP_String
4bb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bba0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 29.
4bbb0 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 #define OP_RealA
4bbc0 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 ffinity
4bbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
4bbe0 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 0.#define OP_VRe
4bbf0 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 name
4bc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bc10 20 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 31.#define OP_P
4bc20 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20 arseSchema
4bc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bc40 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 32.#define OP
4bc50 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20 _VOpen
4bc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bc70 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 33.#define
4bc80 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20 OP_Close
4bc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bca0 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 34.#defin
4bcb0 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 e OP_CreateIndex
4bcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bcd0 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 35.#def
4bce0 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 ine OP_IsUnique
4bcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bd00 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 36.#d
4bd10 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e efine OP_NotFoun
4bd20 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
4bd30 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 37.
4bd40 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34 #define OP_Int64
4bd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
4bd70 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73 8.#define OP_Mus
4bd80 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 20 tBeInt
4bd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bda0 20 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 39.#define OP_H
4bdb0 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 alt
4bdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bdd0 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 40.#define OP
4bde0 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 _Rowid
4bdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4be00 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 41.#define
4be10 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 OP_IdxLT
4be20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4be30 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 42.#defin
4be40 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 e OP_AddImm
4be50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4be60 20 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 43.#def
4be70 69 6e 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74 ine OP_Statement
4be80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4be90 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 44.#d
4bea0 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61 efine OP_RowData
4beb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 45.
4bed0 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 #define OP_MemMa
4bee0 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x
4bef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
4bf00 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 72 20 6.#define OP_Or
4bf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bf30 20 36 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 66 /* same as
4bf40 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 2a 2f 0a TK_OR */.
4bf50 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 #define OP_NotEx
4bf60 69 73 74 73 20 20 20 20 20 20 20 20 20 20 20 20 ists
4bf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
4bf80 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 7.#define OP_Gos
4bf90 75 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ub
4bfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bfb0 20 34 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 48.#define OP_D
4bfc0 69 76 69 64 65 20 20 20 20 20 20 20 20 20 20 20 ivide
4bfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bfe0 20 20 20 38 37 20 20 20 2f 2a 20 73 61 6d 65 20 87 /* same
4bff0 61 73 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a as TK_SLASH *
4c000 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 /.#define OP_Int
4c010 65 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 eger
4c020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c030 20 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 49.#define OP_T
4c040 6f 4e 75 6d 65 72 69 63 20 20 20 20 20 20 20 20 oNumeric
4c050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c060 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20 143 /* same
4c070 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 as TK_TO_NUMERIC
4c080 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 */.#define OP_Pr
4c090 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ev
4c0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c0b0 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50.#define OP_
4c0c0 52 6f 77 53 65 74 52 65 61 64 20 20 20 20 20 20 RowSetRead
4c0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c0e0 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 4f 51.#define O
4c0f0 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20 P_Concat
4c100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c110 20 20 20 20 20 20 38 39 20 20 20 2f 2a 20 73 61 89 /* sa
4c120 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 me as TK_CONCAT
4c130 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4c140 52 6f 77 53 65 74 41 64 64 20 20 20 20 20 20 20 RowSetAdd
4c150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c160 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 52.#define O
4c170 50 5f 42 69 74 41 6e 64 20 20 20 20 20 20 20 20 P_BitAnd
4c180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c190 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61 80 /* sa
4c1a0 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 20 me as TK_BITAND
4c1b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4c1c0 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 VColumn
4c1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c1e0 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 53.#define O
4c1f0 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 P_CreateTable
4c200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c210 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 54.#define
4c220 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 20 20 20 OP_Last
4c230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c240 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 55.#defi
4c250 6e 65 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 ne OP_SeekLe
4c260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c270 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 56.#de
4c280 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 fine OP_IsNull
4c290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c2a0 20 20 20 20 20 20 20 20 20 20 20 20 37 31 20 20 71
4c2b0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 /* same as TK_I
4c2c0 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 SNULL */.#defi
4c2d0 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d ne OP_IncrVacuum
4c2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c2f0 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 57.#de
4c300 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 fine OP_IdxRowid
4c310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c320 20 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 58.#
4c330 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 52 define OP_ShiftR
4c340 69 67 68 74 20 20 20 20 20 20 20 20 20 20 20 20 ight
4c350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33 83
4c360 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4c370 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 _RSHIFT */.#de
4c380 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 fine OP_ResetCou
4c390 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nt
4c3a0 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 59.#
4c3b0 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 74 65 78 define OP_Contex
4c3c0 74 50 75 73 68 20 20 20 20 20 20 20 20 20 20 20 tPush
4c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30 60
4c3e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59 69 65 6c .#define OP_Yiel
4c3f0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
4c400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c410 36 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 61.#define OP_Dr
4c420 6f 70 54 72 69 67 67 65 72 20 20 20 20 20 20 20 opTrigger
4c430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c440 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 62.#define OP_
4c450 44 72 6f 70 49 6e 64 65 78 20 20 20 20 20 20 20 DropIndex
4c460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c470 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f 63.#define O
4c480 50 5f 49 64 78 47 45 20 20 20 20 20 20 20 20 20 P_IdxGE
4c490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c4a0 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 64.#define
4c4b0 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20 OP_IdxDelete
4c4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c4d0 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 65.#defi
4c4e0 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 20 ne OP_Vacuum
4c4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c500 20 20 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 68.#de
4c510 66 69 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 fine OP_IfNot
4c520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c530 20 20 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 69.#
4c540 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 define OP_DropTa
4c550 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ble
4c560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30 70
4c570 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b .#define OP_Seek
4c580 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Lt
4c590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c5a0 37 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 61 79.#define OP_Ma
4c5b0 6b 65 52 65 63 6f 72 64 20 20 20 20 20 20 20 20 keRecord
4c5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c5d0 20 20 39 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 90.#define OP_
4c5e0 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 ToBlob
4c5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c600 20 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 142 /* same
4c610 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 as TK_TO_BLOB
4c620 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 */.#define OP_Re
4c630 73 75 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20 sultRow
4c640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c650 20 20 39 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 91.#define OP_
4c660 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 Delete
4c670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c680 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 92.#define O
4c690 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20 P_AggFinal
4c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c6b0 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 95.#define
4c6c0 20 4f 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 OP_Compare
4c6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c6e0 20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 96.#defi
4c6f0 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 ne OP_ShiftLeft
4c700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c710 20 20 20 20 20 20 20 20 20 20 38 32 20 20 20 2f 82 /
4c720 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 * same as TK_LSH
4c730 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 IFT */.#define
4c740 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20 OP_Goto
4c750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c760 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 97.#defi
4c770 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 ne OP_TableLock
4c780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c790 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 98.#de
4c7a0 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 fine OP_Clear
4c7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c7c0 20 20 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 99.#
4c7d0 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20 define OP_Le
4c7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 76
4c800 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4c810 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _LE */.#de
4c820 66 69 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 6f fine OP_VerifyCo
4c830 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 okie
4c840 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 100.#
4c850 64 65 66 69 6e 65 20 4f 50 5f 41 67 67 53 74 65 define OP_AggSte
4c860 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p
4c870 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 31 101
4c880 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 .#define OP_ToTe
4c890 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xt
4c8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4c8b0 34 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 41 /* same as
4c8c0 54 4b 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 TK_TO_TEXT */.#
4c8d0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 define OP_Not
4c8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39 19
4c900 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4c910 5f 4e 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 _NOT */.#de
4c920 66 69 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20 fine OP_ToReal
4c930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c940 20 20 20 20 20 20 20 20 20 20 20 31 34 35 20 20 145
4c950 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
4c960 4f 5f 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69 O_REAL */.#defi
4c970 6e 65 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 ne OP_SetNumColu
4c980 6d 6e 73 20 20 20 20 20 20 20 20 20 20 20 20 20 mns
4c990 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 102.#de
4c9a0 66 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 fine OP_Transact
4c9b0 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ion
4c9c0 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 103.#
4c9d0 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 65 define OP_VFilte
4c9e0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
4c9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34 104
4ca00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20 .#define OP_Ne
4ca10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ca20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ca30 37 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 73 /* same as
4ca40 54 4b 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23 TK_NE */.#
4ca50 64 65 66 69 6e 65 20 4f 50 5f 56 44 65 73 74 72 define OP_VDestr
4ca60 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 oy
4ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35 105
4ca80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 74 .#define OP_Cont
4ca90 65 78 74 50 6f 70 20 20 20 20 20 20 20 20 20 20 extPop
4caa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4cab0 30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 06.#define OP_Bi
4cac0 74 4f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 tOr
4cad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cae0 20 20 38 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 81 /* same a
4caf0 73 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 2a 2f s TK_BITOR */
4cb00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74 .#define OP_Next
4cb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4cb30 30 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 07.#define OP_Co
4cb40 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 unt
4cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb60 20 31 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 108.#define OP_
4cb70 49 64 78 49 6e 73 65 72 74 20 20 20 20 20 20 20 IdxInsert
4cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb90 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 4f 109.#define O
4cba0 50 5f 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 P_Lt
4cbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cbc0 20 20 20 20 20 20 37 37 20 20 20 2f 2a 20 73 61 77 /* sa
4cbd0 6d 65 20 61 73 20 54 4b 5f 4c 54 20 20 20 20 20 me as TK_LT
4cbe0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4cbf0 53 65 65 6b 47 65 20 20 20 20 20 20 20 20 20 20 SeekGe
4cc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cc10 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 110.#define O
4cc20 50 5f 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 P_Insert
4cc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cc40 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 111.#define
4cc50 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20 20 OP_Destroy
4cc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cc70 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 112.#defi
4cc80 6e 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 ne OP_ReadCookie
4cc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cca0 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 113.#de
4ccb0 66 69 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c fine OP_LoadAnal
4ccc0 79 73 69 73 20 20 20 20 20 20 20 20 20 20 20 20 ysis
4ccd0 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 114.#
4cce0 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 define OP_Explai
4ccf0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
4cd00 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 35 115
4cd10 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 .#define OP_Halt
4cd20 49 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 IfNull
4cd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4cd40 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 16.#define OP_Op
4cd50 65 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 enPseudo
4cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cd70 20 31 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 117.#define OP_
4cd80 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 OpenEphemeral
4cd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cda0 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f 118.#define O
4cdb0 50 5f 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 P_Null
4cdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cdd0 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 119.#define
4cde0 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 OP_Move
4cdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce00 20 20 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 120.#defi
4ce10 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 ne OP_Blob
4ce20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce30 20 20 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 121.#de
4ce40 66 69 6e 65 20 4f 50 5f 41 64 64 20 20 20 20 20 fine OP_Add
4ce50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce60 20 20 20 20 20 20 20 20 20 20 20 20 38 34 20 20 84
4ce70 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 /* same as TK_P
4ce80 4c 55 53 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 LUS */.#defi
4ce90 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 ne OP_Rewind
4cea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ceb0 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 122.#de
4cec0 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 fine OP_SeekGt
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cee0 20 20 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 123.#
4cef0 64 65 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e define OP_VBegin
4cf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cf10 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 34 124
4cf20 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 .#define OP_VUpd
4cf30 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ate
4cf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4cf50 32 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 25.#define OP_If
4cf60 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 Zero
4cf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cf80 20 31 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 126.#define OP_
4cf90 42 69 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20 BitNot
4cfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cfb0 20 20 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 93 /* same
4cfc0 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 as TK_BITNOT
4cfd0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 */.#define OP_VC
4cfe0 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20 reate
4cff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d000 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 127.#define OP_
4d010 46 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 Found
4d020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d030 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 128.#define O
4d040 50 5f 49 66 50 6f 73 20 20 20 20 20 20 20 20 20 P_IfPos
4d050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d060 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 129.#define
4d070 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 OP_NullRow
4d080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d090 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 131.#defi
4d0a0 6e 65 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 ne OP_Jump
4d0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d0c0 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 132.#de
4d0d0 66 69 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 fine OP_Permutat
4d0e0 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ion
4d0f0 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 0a 133..
4d100 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 /* The following
4d110 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20 61 opcode values a
4d120 72 65 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f re never used */
4d130 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
4d140 73 65 64 5f 31 33 34 20 20 20 20 20 20 20 20 20 sed_134
4d150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4d160 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 34.#define OP_No
4d170 74 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 tUsed_135
4d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d190 20 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 135.#define OP_
4d1a0 4e 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 NotUsed_136
4d1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d1c0 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 136.#define O
4d1d0 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 P_NotUsed_137
4d1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d1f0 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 137.#define
4d200 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 OP_NotUsed_138
4d210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d220 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 138.#defi
4d230 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 ne OP_NotUsed_13
4d240 39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 9
4d250 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 139.#de
4d260 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
4d270 31 34 30 20 20 20 20 20 20 20 20 20 20 20 20 20 140
4d280 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 140..
4d290 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 ./* Properties s
4d2a0 75 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 uch as "out2" or
4d2b0 20 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 "jump" that are
4d2c0 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a specified in.**
4d2d0 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 comments follow
4d2e0 69 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 66 ing the "case" f
4d2f0 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 or each opcode i
4d300 6e 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 n the vdbe.c.**
4d310 61 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f are encoded into
4d320 20 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 bitvectors as f
4d330 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 ollows:.*/.#defi
4d340 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 ne OPFLG_JUMP
4d350 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 0x0001
4d360 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f /* jump: P2 ho
4d370 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a lds jmp target *
4d380 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
4d390 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 OUT2_PRERELEASE
4d3a0 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 0x0002 /* out2-
4d3b0 70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 prerelease: */.#
4d3c0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 define OPFLG_IN1
4d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
4d3e0 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 004 /* in1: P
4d3f0 31 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 1 is an input */
4d400 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 .#define OPFLG_I
4d410 4e 32 20 20 20 20 20 20 20 20 20 20 20 20 20 30 N2 0
4d420 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 x0008 /* in2:
4d430 20 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 P2 is an input
4d440 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 */.#define OPFLG
4d450 5f 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 _IN3
4d460 20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 0x0010 /* in3:
4d470 20 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 P3 is an inpu
4d480 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 t */.#define OPF
4d490 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20 LG_OUT3
4d4a0 20 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 0x0020 /* ou
4d4b0 74 33 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75 t3: P3 is an ou
4d4c0 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tput */.#define
4d4d0 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 OPFLG_INITIALIZE
4d4e0 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 R {\./* 0 */ 0
4d4f0 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x00, 0x01, 0x00,
4d500 20 30 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 0x00, 0x10, 0x0
4d510 38 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 8, 0x02, 0x00,\.
4d520 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20 /* 8 */ 0x00,
4d530 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35 0x04, 0x00, 0x05
4d540 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 , 0x02, 0x00, 0x
4d550 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31 00, 0x00,\./* 1
4d560 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 6 */ 0x00, 0x02,
4d570 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 0x00, 0x04, 0x0
4d580 31 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 1, 0x04, 0x00, 0
4d590 78 30 30 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 x00,\./* 24 */
4d5a0 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 34 0x05, 0x00, 0x04
4d5b0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 , 0x02, 0x00, 0x
4d5c0 30 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 5c 02, 0x04, 0x00,\
4d5d0 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c ./* 32 */ 0x00,
4d5e0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
4d5f0 32 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 2, 0x11, 0x11, 0
4d600 78 30 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 x02, 0x05,\./*
4d610 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 40 */ 0x00, 0x02
4d620 2c 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78 , 0x11, 0x04, 0x
4d630 30 30 2c 20 30 78 30 30 2c 20 30 78 30 63 2c 20 00, 0x00, 0x0c,
4d640 30 78 31 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 0x11,\./* 48 */
4d650 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 0x01, 0x02, 0x0
4d660 31 2c 20 30 78 32 31 2c 20 30 78 30 38 2c 20 30 1, 0x21, 0x08, 0
4d670 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c x00, 0x02, 0x01,
4d680 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 78 31 31 \./* 56 */ 0x11
4d690 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
4d6a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 00, 0x00, 0x04,
4d6b0 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 0x00, 0x00,\./*
4d6c0 20 36 34 20 2a 2f 20 30 78 31 31 2c 20 30 78 30 64 */ 0x11, 0x0
4d6d0 30 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 0, 0x2c, 0x2c, 0
4d6e0 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 30 2c x00, 0x05, 0x00,
4d6f0 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 37 32 20 2a 0x05,\./* 72 *
4d700 2f 20 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 / 0x05, 0x15, 0x
4d710 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 15, 0x15, 0x15,
4d720 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 31 0x15, 0x15, 0x11
4d730 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 32 ,\./* 80 */ 0x2
4d740 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 c, 0x2c, 0x2c, 0
4d750 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c x2c, 0x2c, 0x2c,
4d760 20 30 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 0x2c, 0x2c,\./*
4d770 20 20 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78 88 */ 0x2c, 0x
4d780 32 63 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 2c, 0x00, 0x00,
4d790 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 0x00, 0x04, 0x02
4d7a0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 , 0x00,\./* 96
4d7b0 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 */ 0x00, 0x01, 0
4d7c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
4d7d0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
4d7e0 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 0,\./* 104 */ 0x
4d7f0 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 01, 0x00, 0x00,
4d800 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 38 0x01, 0x02, 0x08
4d810 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 5c 0a 2f , 0x11, 0x00,\./
4d820 2a 20 31 31 32 20 2a 2f 20 30 78 30 32 2c 20 30 * 112 */ 0x02, 0
4d830 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x02, 0x00, 0x00,
4d840 20 30 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 0x10, 0x00, 0x0
4d850 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30 0, 0x02,\./* 120
4d860 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20 */ 0x00, 0x02,
4d870 30 78 30 31 2c 20 30 78 31 31 2c 20 30 78 30 30 0x01, 0x11, 0x00
4d880 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 , 0x00, 0x05, 0x
4d890 30 30 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 00,\./* 128 */ 0
4d8a0 78 31 31 2c 20 30 78 30 35 2c 20 30 78 30 32 2c x11, 0x05, 0x02,
4d8b0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
4d8c0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 0, 0x00, 0x00,\.
4d8d0 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 /* 136 */ 0x00,
4d8e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
4d8f0 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 , 0x00, 0x04, 0x
4d900 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 04, 0x04,\./* 14
4d910 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 4 */ 0x04, 0x04,
4d920 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
4d930 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 ** End of opcode
4d940 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.h ************
4d950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4d960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4d970 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4d980 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
4d990 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
4d9a0 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a in vdbe.h ******
4d9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4d9c0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 */../*.** Protot
4d9d0 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 ypes for the VDB
4d9e0 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 E interface. Se
4d9f0 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 e comments on th
4da00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
4da10 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 .** for a descri
4da20 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 ption of what ea
4da30 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 ch of these rout
4da40 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 ines does..*/.SQ
4da50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 LITE_PRIVATE Vdb
4da60 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 e *sqlite3VdbeCr
4da70 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a eate(sqlite3*);.
4da80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4da90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
4daa0 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b dOp0(Vdbe*,int);
4dab0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4dac0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
4dad0 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c ddOp1(Vdbe*,int,
4dae0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4daf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4db00 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a VdbeAddOp2(Vdbe*
4db10 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
4db20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4db30 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
4db40 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e Op3(Vdbe*,int,in
4db50 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 t,int,int);.SQLI
4db60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4db70 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
4db80 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 (Vdbe*,int,int,i
4db90 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 nt,int,const cha
4dba0 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c r *zP4,int);.SQL
4dbb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4dbc0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
4dbd0 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 List(Vdbe*, int
4dbe0 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 nOp, VdbeOpList
4dbf0 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c const *aOp);.SQL
4dc00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4dc10 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
4dc20 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 geP1(Vdbe*, int
4dc30 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 addr, int P1);.S
4dc40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4dc50 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
4dc60 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e angeP2(Vdbe*, in
4dc70 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b t addr, int P2);
4dc80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4dc90 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
4dca0 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 ChangeP3(Vdbe*,
4dcb0 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 int addr, int P3
4dcc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4dcd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
4dce0 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a beChangeP5(Vdbe*
4dcf0 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 , u8 P5);.SQLITE
4dd00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
4dd10 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
4dd20 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 e(Vdbe*, int add
4dd30 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 r);.SQLITE_PRIVA
4dd40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
4dd50 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
4dd60 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c Vdbe*, int addr,
4dd70 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
4dd80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4dd90 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 ite3VdbeChangeP4
4dda0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 (Vdbe*, int addr
4ddb0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 , const char *zP
4ddc0 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 4, int N);.SQLIT
4ddd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
4dde0 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 qlite3VdbeUsesBt
4ddf0 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b ree(Vdbe*, int);
4de00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4de10 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 VdbeOp *sqlite3V
4de20 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 dbeGetOp(Vdbe*,
4de30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4de40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4de50 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 VdbeMakeLabel(Vd
4de60 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
4de70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
4de80 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 3VdbeDelete(Vdbe
4de90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4dea0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
4deb0 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 dbeMakeReady(Vdb
4dec0 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 e*,int,int,int,i
4ded0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
4dee0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
4def0 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 dbeFinalize(Vdbe
4df00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4df10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
4df20 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
4df30 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Vdbe*, int);.SQL
4df40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4df50 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
4df60 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 ntAddr(Vdbe*);.#
4df70 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
4df80 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
4df90 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
4dfa0 56 64 62 65 54 72 61 63 65 28 56 64 62 65 2a 2c VdbeTrace(Vdbe*,
4dfb0 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 FILE*);.#endif.S
4dfc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4dfd0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 id sqlite3VdbeRe
4dfe0 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 64 setStepResult(Vd
4dff0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
4e000 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4e010 56 64 62 65 52 65 73 65 74 28 56 64 62 65 2a 29 VdbeReset(Vdbe*)
4e020 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4e030 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
4e040 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 eSetNumCols(Vdbe
4e050 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
4e060 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4e070 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 e3VdbeSetColName
4e080 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 (Vdbe*, int, int
4e090 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
4e0a0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
4e0b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4e0c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
4e0d0 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62 CountChanges(Vdb
4e0e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
4e0f0 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c ATE sqlite3 *sql
4e100 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65 2a ite3VdbeDb(Vdbe*
4e110 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4e120 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
4e130 62 65 53 65 74 53 71 6c 28 56 64 62 65 2a 2c 20 beSetSql(Vdbe*,
4e140 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
4e150 6e 74 20 6e 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 nt n, int);.SQLI
4e160 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
4e170 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 28 sqlite3VdbeSwap(
4e180 56 64 62 65 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23 Vdbe*,Vdbe*);..#
4e190 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
4e1a0 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 BLE_MEMORY_MANAG
4e1b0 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 EMENT.SQLITE_PRI
4e1c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4e1d0 56 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 VdbeReleaseMemor
4e1e0 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 53 y(int);.#endif.S
4e1f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e QLITE_PRIVATE Un
4e200 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71 packedRecord *sq
4e210 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 lite3VdbeRecordU
4e220 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 npack(KeyInfo*,i
4e230 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 0a nt,const void*,.
4e240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4e260 20 20 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 Unpacked
4e270 52 65 63 6f 72 64 2a 2c 69 6e 74 29 3b 0a 53 51 Record*,int);.SQ
4e280 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4e290 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c d sqlite3VdbeDel
4e2a0 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
4e2b0 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 d(UnpackedRecord
4e2c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4e2d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
4e2e0 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
4e2f0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
4e300 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 UnpackedRecord*)
4e310 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 ;...#ifndef NDEB
4e320 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
4e330 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
4e340 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 VdbeComment(Vdbe
4e350 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
4e360 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 ...);.# define V
4e370 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 dbeComment(X) s
4e380 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e qlite3VdbeCommen
4e390 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t X.SQLITE_PRIVA
4e3a0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
4e3b0 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 3VdbeNoopComment
4e3c0 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 (Vdbe*, const ch
4e3d0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 ar*, ...);.# def
4e3e0 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ine VdbeNoopComm
4e3f0 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 33 56 ent(X) sqlite3V
4e400 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 dbeNoopComment X
4e410 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
4e420 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 VdbeComment(X).#
4e430 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 define VdbeNoop
4e440 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 Comment(X).#endi
4e450 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a f..#endif../****
4e460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
4e470 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a f vdbe.h *******
4e480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
4e4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
4e4c0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
4e4d0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 eft off in sqlit
4e4e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
4e4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
4e500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
4e510 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 74 68 de pager.h in th
4e520 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
4e530 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
4e540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
4e550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
4e560 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a file pager.h **
4e570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
4e5a0 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
4e5b0 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
4e5c0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
4e5d0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
4e5e0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
4e5f0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
4e600 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
4e610 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
4e620 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
4e630 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
4e640 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
4e650 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
4e660 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
4e670 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
4e680 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
4e690 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
4e6a0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
4e6b0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
4e6c0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
4e6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e710 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 *.** This header
4e720 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 file defines th
4e730 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 e interface that
4e740 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 the sqlite page
4e750 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 cache.** subsys
4e760 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 20 63 tem. The page c
4e770 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 72 ache subsystem r
4e780 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 eads and writes
4e790 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a a file a page.**
4e7a0 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 70 at a time and p
4e7b0 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 rovides a journa
4e7c0 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a l for rollback..
4e7d0 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
4e7e0 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30 30 20 pager.h,v 1.100
4e7f0 32 30 30 39 2f 30 32 2f 30 33 20 31 36 3a 35 31 2009/02/03 16:51
4e800 3a 32 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :25 danielk1977
4e810 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 Exp $.*/..#ifnde
4e820 66 20 5f 50 41 47 45 52 5f 48 5f 0a 23 64 65 66 f _PAGER_H_.#def
4e830 69 6e 65 20 5f 50 41 47 45 52 5f 48 5f 0a 0a 2f ine _PAGER_H_../
4e840 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 6d 61 78 *.** Default max
4e850 69 6d 75 6d 20 73 69 7a 65 20 66 6f 72 20 70 65 imum size for pe
4e860 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c rsistent journal
4e870 20 66 69 6c 65 73 2e 20 41 20 6e 65 67 61 74 69 files. A negati
4e880 76 65 20 0a 2a 2a 20 76 61 6c 75 65 20 6d 65 61 ve .** value mea
4e890 6e 73 20 6e 6f 20 6c 69 6d 69 74 2e 20 54 68 69 ns no limit. Thi
4e8a0 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 6f s value may be o
4e8b0 76 65 72 72 69 64 64 65 6e 20 75 73 69 6e 67 20 verridden using
4e8c0 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 the .** sqlite3P
4e8d0 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c agerJournalSizeL
4e8e0 69 6d 69 74 28 29 20 41 50 49 2e 20 53 65 65 20 imit() API. See
4e8f0 61 6c 73 6f 20 22 50 52 41 47 4d 41 20 6a 6f 75 also "PRAGMA jou
4e900 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 rnal_size_limit"
4e910 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
4e920 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 ITE_DEFAULT_JOUR
4e930 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 NAL_SIZE_LIMIT.
4e940 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f #define SQLITE_
4e950 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f DEFAULT_JOURNAL_
4e960 53 49 5a 45 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 SIZE_LIMIT -1.#e
4e970 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
4e980 74 79 70 65 20 75 73 65 64 20 74 6f 20 72 65 70 type used to rep
4e990 72 65 73 65 6e 74 20 61 20 70 61 67 65 20 6e 75 resent a page nu
4e9a0 6d 62 65 72 2e 20 20 54 68 65 20 66 69 72 73 74 mber. The first
4e9b0 20 70 61 67 65 20 69 6e 20 61 20 66 69 6c 65 0a page in a file.
4e9c0 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 ** is called pag
4e9d0 65 20 31 2e 20 20 30 20 69 73 20 75 73 65 64 20 e 1. 0 is used
4e9e0 74 6f 20 72 65 70 72 65 73 65 6e 74 20 22 6e 6f to represent "no
4e9f0 74 20 61 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 t a page"..*/.ty
4ea00 70 65 64 65 66 20 75 33 32 20 50 67 6e 6f 3b 0a pedef u32 Pgno;.
4ea10 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e ./*.** Each open
4ea20 20 66 69 6c 65 20 69 73 20 6d 61 6e 61 67 65 64 file is managed
4ea30 20 62 79 20 61 20 73 65 70 61 72 61 74 65 20 69 by a separate i
4ea40 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 22 nstance of the "
4ea50 50 61 67 65 72 22 20 73 74 72 75 63 74 75 72 65 Pager" structure
4ea60 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
4ea70 75 63 74 20 50 61 67 65 72 20 50 61 67 65 72 3b uct Pager Pager;
4ea80 0a 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 ../*.** Handle t
4ea90 79 70 65 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a ype for pages..*
4eaa0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
4eab0 20 50 67 48 64 72 20 44 62 50 61 67 65 3b 0a 0a PgHdr DbPage;..
4eac0 2f 2a 0a 2a 2a 20 50 61 67 65 20 6e 75 6d 62 65 /*.** Page numbe
4ead0 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 20 r PAGER_MJ_PGNO
4eae0 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 69 6e is never used in
4eaf0 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 an SQLite datab
4eb00 61 73 65 20 28 69 74 20 69 73 0a 2a 2a 20 72 65 ase (it is.** re
4eb10 73 65 72 76 65 64 20 66 6f 72 20 77 6f 72 6b 69 served for worki
4eb20 6e 67 20 61 72 6f 75 6e 64 20 61 20 77 69 6e 64 ng around a wind
4eb30 6f 77 73 2f 70 6f 73 69 78 20 69 6e 63 6f 6d 70 ows/posix incomp
4eb40 61 74 69 62 69 6c 69 74 79 29 2e 20 49 74 20 69 atibility). It i
4eb50 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74 68 65 s.** used in the
4eb60 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 69 67 6e journal to sign
4eb70 69 66 79 20 74 68 61 74 20 74 68 65 20 72 65 6d ify that the rem
4eb80 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6a 6f ainder of the jo
4eb90 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 69 urnal file .** i
4eba0 73 20 64 65 76 6f 74 65 64 20 74 6f 20 73 74 6f s devoted to sto
4ebb0 72 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6a 6f ring a master jo
4ebc0 75 72 6e 61 6c 20 6e 61 6d 65 20 2d 20 74 68 65 urnal name - the
4ebd0 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 re are no more p
4ebe0 61 67 65 73 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 ages to.** roll
4ebf0 62 61 63 6b 2e 20 53 65 65 20 63 6f 6d 6d 65 6e back. See commen
4ec00 74 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 ts for function
4ec10 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e writeMasterJourn
4ec20 61 6c 28 29 20 69 6e 20 70 61 67 65 72 2e 63 20 al() in pager.c
4ec30 0a 2a 2a 20 66 6f 72 20 64 65 74 61 69 6c 73 2e .** for details.
4ec40 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 .*/.#define PAGE
4ec50 52 5f 4d 4a 5f 50 47 4e 4f 28 78 29 20 28 28 50 R_MJ_PGNO(x) ((P
4ec60 67 6e 6f 29 28 28 50 45 4e 44 49 4e 47 5f 42 59 gno)((PENDING_BY
4ec70 54 45 2f 28 28 78 29 2d 3e 70 61 67 65 53 69 7a TE/((x)->pageSiz
4ec80 65 29 29 2b 31 29 29 0a 0a 2f 2a 0a 2a 2a 20 41 e))+1))../*.** A
4ec90 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f llowed values fo
4eca0 72 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 r the flags para
4ecb0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
4ecc0 50 61 67 65 72 4f 70 65 6e 28 29 2e 0a 2a 2a 0a PagerOpen()..**.
4ecd0 2a 2a 20 4e 4f 54 45 3a 20 54 68 65 73 65 20 76 ** NOTE: These v
4ece0 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68 alues must match
4ecf0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
4ed00 6e 67 20 42 54 52 45 45 5f 20 76 61 6c 75 65 73 ng BTREE_ values
4ed10 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a 2a 2f 0a in btree.h..*/.
4ed20 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4f 4d #define PAGER_OM
4ed30 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30 78 30 30 IT_JOURNAL 0x00
4ed40 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 01 /* Do not
4ed50 75 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a use a rollback j
4ed60 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e ournal */.#defin
4ed70 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c e PAGER_NO_READL
4ed80 4f 43 4b 20 20 20 30 78 30 30 30 32 20 20 20 20 OCK 0x0002
4ed90 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b /* Omit readlock
4eda0 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 s on readonly fi
4edb0 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 les */../*.** Va
4edc0 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 lid values for t
4edd0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
4ede0 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 nt to sqlite3Pag
4edf0 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 29 2e erLockingMode().
4ee00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 .*/.#define PAGE
4ee10 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 R_LOCKINGMODE_QU
4ee20 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 ERY -1.#def
4ee30 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e ine PAGER_LOCKIN
4ee40 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 20 20 20 GMODE_NORMAL
4ee50 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45 0.#define PAGE
4ee60 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
4ee70 43 4c 55 53 49 56 45 20 20 20 31 0a 0a 2f 2a 0a CLUSIVE 1../*.
4ee80 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 73 20 ** Valid values
4ee90 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 61 for the second a
4eea0 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
4eeb0 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f e3PagerJournalMo
4eec0 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 de()..*/.#define
4eed0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
4eee0 44 45 5f 51 55 45 52 59 20 20 20 20 20 20 2d 31 DE_QUERY -1
4eef0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a .#define PAGER_J
4ef00 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 OURNALMODE_DELET
4ef10 45 20 20 20 20 20 20 30 20 20 20 2f 2a 20 43 6f E 0 /* Co
4ef20 6d 6d 69 74 20 62 79 20 64 65 6c 65 74 69 6e 67 mmit by deleting
4ef30 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f journal file */
4ef40 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a .#define PAGER_J
4ef50 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 OURNALMODE_PERSI
4ef60 53 54 20 20 20 20 20 31 20 20 20 2f 2a 20 43 6f ST 1 /* Co
4ef70 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69 6e 67 20 mmit by zeroing
4ef80 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a journal header *
4ef90 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
4efa0 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 JOURNALMODE_OFF
4efb0 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 4a 2 /* J
4efc0 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65 64 2e 20 ournal omitted.
4efd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
4efe0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 R_JOURNALMODE_TR
4eff0 55 4e 43 41 54 45 20 20 20 20 33 20 20 20 2f 2a UNCATE 3 /*
4f000 20 43 6f 6d 6d 69 74 20 62 79 20 74 72 75 6e 63 Commit by trunc
4f010 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 2a 2f ating journal */
4f020 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a .#define PAGER_J
4f030 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
4f040 59 20 20 20 20 20 20 34 20 20 20 2f 2a 20 49 6e Y 4 /* In
4f050 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 -memory journal
4f060 66 69 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 file */../*.** T
4f070 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 he remainder of
4f080 74 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 this file contai
4f090 6e 73 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 ns the declarati
4f0a0 6f 6e 73 20 6f 66 20 74 68 65 20 66 75 6e 63 74 ons of the funct
4f0b0 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b ions.** that mak
4f0c0 65 20 75 70 20 74 68 65 20 50 61 67 65 72 20 73 e up the Pager s
4f0d0 75 62 2d 73 79 73 74 65 6d 20 41 50 49 2e 20 53 ub-system API. S
4f0e0 65 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 63 ee source code c
4f0f0 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 0a 2a 2a 20 omments for .**
4f100 61 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 a detailed descr
4f110 69 70 74 69 6f 6e 20 6f 66 20 65 61 63 68 20 72 iption of each r
4f120 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 0a 2f 2a 20 4f outine..*/../* O
4f130 70 65 6e 20 61 6e 64 20 63 6c 6f 73 65 20 61 20 pen and close a
4f140 50 61 67 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e Pager connection
4f150 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 . */ .SQLITE_PRI
4f160 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4f170 50 61 67 65 72 4f 70 65 6e 28 73 71 6c 69 74 65 PagerOpen(sqlite
4f180 33 5f 76 66 73 20 2a 2c 20 50 61 67 65 72 20 2a 3_vfs *, Pager *
4f190 2a 70 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 *ppPager, const
4f1a0 63 68 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c 69 char*, int,int,i
4f1b0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
4f1c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
4f1d0 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65 72 20 agerClose(Pager
4f1e0 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 *pPager);.SQLITE
4f1f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4f200 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c ite3PagerReadFil
4f210 65 68 65 61 64 65 72 28 50 61 67 65 72 2a 2c 20 eheader(Pager*,
4f220 69 6e 74 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 int, unsigned ch
4f230 61 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 ar*);../* Functi
4f240 6f 6e 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 ons used to conf
4f250 69 67 75 72 65 20 61 20 50 61 67 65 72 20 6f 62 igure a Pager ob
4f260 6a 65 63 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f ject. */.SQLITE_
4f270 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4f280 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 ite3PagerSetBusy
4f290 68 61 6e 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 handler(Pager*,
4f2a0 69 6e 74 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 int(*)(void *),
4f2b0 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f void *);.SQLITE_
4f2c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4f2d0 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69 6e ite3PagerSetRein
4f2e0 69 74 65 72 28 50 61 67 65 72 2a 2c 20 76 6f 69 iter(Pager*, voi
4f2f0 64 28 2a 29 28 44 62 50 61 67 65 2a 29 29 3b 0a d(*)(DbPage*));.
4f300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4f310 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
4f320 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 etPagesize(Pager
4f330 2a 2c 20 75 31 36 2a 29 3b 0a 53 51 4c 49 54 45 *, u16*);.SQLITE
4f340 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4f350 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 ite3PagerMaxPage
4f360 43 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e Count(Pager*, in
4f370 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4f380 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
4f390 61 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 agerSetCachesize
4f3a0 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 (Pager*, int);.S
4f3b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4f3c0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 id sqlite3PagerS
4f3d0 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 etSafetyLevel(Pa
4f3e0 67 65 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ger*,int,int);.S
4f3f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4f400 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f t sqlite3PagerLo
4f410 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 20 ckingMode(Pager
4f420 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
4f430 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4f440 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d te3PagerJournalM
4f450 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 ode(Pager *, int
4f460 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4f470 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 61 67 E i64 sqlite3Pag
4f480 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d erJournalSizeLim
4f490 69 74 28 50 61 67 65 72 20 2a 2c 20 69 36 34 29 it(Pager *, i64)
4f4a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4f4b0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
4f4c0 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 **sqlite3PagerBa
4f4d0 63 6b 75 70 50 74 72 28 50 61 67 65 72 2a 29 3b ckupPtr(Pager*);
4f4e0 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 ../* Functions u
4f4f0 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e sed to obtain an
4f500 64 20 72 65 6c 65 61 73 65 20 70 61 67 65 20 72 d release page r
4f510 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 20 0a 53 eferences. */ .S
4f520 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4f530 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 t sqlite3PagerAc
4f540 71 75 69 72 65 28 50 61 67 65 72 20 2a 70 50 61 quire(Pager *pPa
4f550 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 ger, Pgno pgno,
4f560 44 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c DbPage **ppPage,
4f570 20 69 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 int clrFlag);.#
4f580 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 define sqlite3Pa
4f590 67 65 72 47 65 74 28 41 2c 42 2c 43 29 20 73 71 gerGet(A,B,C) sq
4f5a0 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 lite3PagerAcquir
4f5b0 65 28 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 e(A,B,C,0).SQLIT
4f5c0 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65 E_PRIVATE DbPage
4f5d0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f *sqlite3PagerLo
4f5e0 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 okup(Pager *pPag
4f5f0 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a er, Pgno pgno);.
4f600 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4f610 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
4f620 52 65 66 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 Ref(DbPage*);.SQ
4f630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4f640 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e d sqlite3PagerUn
4f650 72 65 66 28 44 62 50 61 67 65 2a 29 3b 0a 0a 2f ref(DbPage*);../
4f660 2a 20 4f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 * Operations on
4f670 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 2e page references.
4f680 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
4f690 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
4f6a0 67 65 72 57 72 69 74 65 28 44 62 50 61 67 65 2a gerWrite(DbPage*
4f6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4f6c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
4f6d0 67 65 72 44 6f 6e 74 57 72 69 74 65 28 44 62 50 gerDontWrite(DbP
4f6e0 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 age*);.SQLITE_PR
4f6f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4f700 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 3PagerMovepage(P
4f710 61 67 65 72 2a 2c 44 62 50 61 67 65 2a 2c 50 67 ager*,DbPage*,Pg
4f720 6e 6f 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f no,int);.SQLITE_
4f730 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4f740 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
4f750 6f 75 6e 74 28 44 62 50 61 67 65 2a 29 3b 0a 53 ount(DbPage*);.S
4f760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4f770 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
4f780 47 65 74 44 61 74 61 28 44 62 50 61 67 65 20 2a GetData(DbPage *
4f790 29 3b 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ); .SQLITE_PRIVA
4f7a0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
4f7b0 50 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62 PagerGetExtra(Db
4f7c0 50 61 67 65 20 2a 29 3b 20 0a 0a 2f 2a 20 46 75 Page *); ../* Fu
4f7d0 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 nctions used to
4f7e0 6d 61 6e 61 67 65 20 70 61 67 65 72 20 74 72 61 manage pager tra
4f7f0 6e 73 61 63 74 69 6f 6e 73 20 61 6e 64 20 73 61 nsactions and sa
4f800 76 65 70 6f 69 6e 74 73 2e 20 2a 2f 0a 53 51 4c vepoints. */.SQL
4f810 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4f820 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
4f830 63 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e count(Pager*, in
4f840 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
4f850 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
4f860 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 2a agerBegin(Pager*
4f870 2c 20 69 6e 74 20 65 78 46 6c 61 67 29 3b 0a 53 , int exFlag);.S
4f880 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4f890 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
4f8a0 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 mmitPhaseOne(Pag
4f8b0 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a er*,const char *
4f8c0 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 zMaster, int);.S
4f8d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4f8e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 t sqlite3PagerSy
4f8f0 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nc(Pager *pPager
4f900 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4f910 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
4f920 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
4f930 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
4f940 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4f950 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
4f960 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 k(Pager*);.SQLIT
4f970 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4f980 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 lite3PagerOpenSa
4f990 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
4f9a0 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 Pager, int n);.S
4f9b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4f9c0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 t sqlite3PagerSa
4f9d0 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
4f9e0 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 Pager, int op, i
4f9f0 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a nt iSavepoint);.
4fa00 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 ./* Functions us
4fa10 65 64 20 74 6f 20 71 75 65 72 79 20 70 61 67 65 ed to query page
4fa20 72 20 73 74 61 74 65 20 61 6e 64 20 63 6f 6e 66 r state and conf
4fa30 69 67 75 72 61 74 69 6f 6e 2e 20 2a 2f 0a 53 51 iguration. */.SQ
4fa40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 LITE_PRIVATE u8
4fa50 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 sqlite3PagerIsre
4fa60 61 64 6f 6e 6c 79 28 50 61 67 65 72 2a 29 3b 0a adonly(Pager*);.
4fa70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4fa80 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 nt sqlite3PagerR
4fa90 65 66 63 6f 75 6e 74 28 50 61 67 65 72 2a 29 3b efcount(Pager*);
4faa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4fab0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
4fac0 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 te3PagerFilename
4fad0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
4fae0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
4faf0 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 qlite3_vfs *sqli
4fb00 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65 te3PagerVfs(Page
4fb10 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
4fb20 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ATE sqlite3_file
4fb30 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
4fb40 6c 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 le(Pager*);.SQLI
4fb50 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
4fb60 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 char *sqlite3Pa
4fb70 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 gerJournalname(P
4fb80 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
4fb90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4fba0 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 e3PagerNosync(Pa
4fbb0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
4fbc0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
4fbd0 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 te3PagerTempSpac
4fbe0 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 e(Pager*);.SQLIT
4fbf0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4fc00 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 lite3PagerIsMemd
4fc10 62 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 b(Pager*);../* F
4fc20 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f unctions used to
4fc30 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 64 61 truncate the da
4fc40 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a tabase file. */.
4fc50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4fc60 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
4fc70 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 TruncateImage(Pa
4fc80 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 ger*,Pgno);../*
4fc90 55 73 65 64 20 62 79 20 65 6e 63 72 79 70 74 69 Used by encrypti
4fca0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 2a on extensions. *
4fcb0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
4fcc0 48 41 53 5f 43 4f 44 45 43 0a 53 51 4c 49 54 45 HAS_CODEC.SQLITE
4fcd0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4fce0 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 sqlite3PagerSetC
4fcf0 6f 64 65 63 28 50 61 67 65 72 2a 2c 76 6f 69 64 odec(Pager*,void
4fd00 2a 28 2a 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a *(*)(void*,void*
4fd10 2c 50 67 6e 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a ,Pgno,int),void*
4fd20 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 75 );.#endif../* Fu
4fd30 6e 63 74 69 6f 6e 73 20 74 6f 20 73 75 70 70 6f nctions to suppo
4fd40 72 74 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 rt testing and d
4fd50 65 62 75 67 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 ebugging. */.#if
4fd60 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
4fd70 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
4fd80 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 ITE_TEST).SQLITE
4fd90 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20 _PRIVATE Pgno
4fda0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
4fdb0 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b number(DbPage*);
4fdc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4fdd0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 int sqlite3Pag
4fde0 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 erIswriteable(Db
4fdf0 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 Page*);.#endif.#
4fe00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
4fe10 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
4fe20 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 int *sqlite3P
4fe30 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 2a agerStats(Pager*
4fe40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4fe50 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
4fe60 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67 PagerRefdump(Pag
4fe70 65 72 2a 29 3b 0a 20 20 76 6f 69 64 20 64 69 73 er*);. void dis
4fe80 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
4fe90 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a o_errors(void);.
4fea0 20 20 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 void enable_si
4feb0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
4fec0 73 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23 s(void);.#else.#
4fed0 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f define disable_
4fee0 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
4fef0 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 ors().# define e
4ff00 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
4ff10 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 io_errors().#end
4ff20 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 if..#endif /* _P
4ff30 41 47 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a AGER_H_ */../***
4ff40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
4ff50 6f 66 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a of pager.h *****
4ff60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4ff70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4ff80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
4ff90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
4ffa0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
4ffb0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
4ffc0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
4ffd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
4ffe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
4fff0 75 64 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20 ude pcache.h in
50000 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 the middle of sq
50010 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
50020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
50030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
50040 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20 n file pcache.h
50050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
50080 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35 * 2008 August 05
50090 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
500a0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
500b0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
500c0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
500d0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
500e0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
500f0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
50100 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
50110 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
50120 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
50130 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
50140 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
50150 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
50160 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
50170 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
50180 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
50190 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
501a0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
501b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
501c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
501d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
501e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
501f0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 ** This header f
50200 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 ile defines the
50210 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 interface that t
50220 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 he sqlite page c
50230 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 ache.** subsyste
50240 6d 2e 20 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 m. .**.** @(#) $
50250 49 64 3a 20 70 63 61 63 68 65 2e 68 2c 76 20 31 Id: pcache.h,v 1
50260 2e 31 39 20 32 30 30 39 2f 30 31 2f 32 30 20 31 .19 2009/01/20 1
50270 37 3a 30 36 3a 32 37 20 64 61 6e 69 65 6c 6b 31 7:06:27 danielk1
50280 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 977 Exp $.*/..#i
50290 66 6e 64 65 66 20 5f 50 43 41 43 48 45 5f 48 5f fndef _PCACHE_H_
502a0 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ..typedef struct
502b0 20 50 67 48 64 72 20 50 67 48 64 72 3b 0a 74 79 PgHdr PgHdr;.ty
502c0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61 pedef struct PCa
502d0 63 68 65 20 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a che PCache;../*.
502e0 2a 2a 20 45 76 65 72 79 20 70 61 67 65 20 69 6e ** Every page in
502f0 20 74 68 65 20 63 61 63 68 65 20 69 73 20 63 6f the cache is co
50300 6e 74 72 6f 6c 6c 65 64 20 62 79 20 61 6e 20 69 ntrolled by an i
50310 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
50320 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 ollowing.** stru
50330 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 cture..*/.struct
50340 20 50 67 48 64 72 20 7b 0a 20 20 76 6f 69 64 20 PgHdr {. void
50350 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 *pData;
50360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e /* Con
50370 74 65 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67 tent of this pag
50380 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 78 e */. void *pEx
50390 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 tra;
503a0 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 63 /* Extra c
503b0 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 50 67 48 64 ontent */. PgHd
503c0 72 20 2a 70 44 69 72 74 79 3b 20 20 20 20 20 20 r *pDirty;
503d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
503e0 61 6e 73 69 65 6e 74 20 6c 69 73 74 20 6f 66 20 ansient list of
503f0 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 dirty pages */.
50400 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 Pgno pgno;
50410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50420 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 /* Page number f
50430 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a or this page */.
50440 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b Pager *pPager;
50450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50460 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 74 68 /* The pager th
50470 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20 is page is part
50480 6f 66 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c of */.#ifdef SQL
50490 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
504a0 20 20 75 33 32 20 70 61 67 65 48 61 73 68 3b 20 u32 pageHash;
504b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504c0 20 2f 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65 /* Hash of page
504d0 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 content */.#end
504e0 69 66 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 if. u16 flags;
504f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50500 20 20 20 20 2f 2a 20 50 47 48 44 52 20 66 6c 61 /* PGHDR fla
50510 67 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 gs defined below
50520 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a */.. /********
50530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 **************.
50570 20 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f ** Elements abo
50580 76 65 20 61 72 65 20 70 75 62 6c 69 63 2e 20 20 ve are public.
50590 41 6c 6c 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 All that follows
505a0 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 70 is private to p
505b0 63 61 63 68 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 cache.c. ** and
505c0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 should not be a
505d0 63 63 65 73 73 65 64 20 62 79 20 6f 74 68 65 72 ccessed by other
505e0 20 6d 6f 64 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 modules.. */.
505f0 20 69 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20 i16 nRef;
50600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50610 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 /* Number of use
50620 72 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 rs of this page
50630 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 */. PCache *pCa
50640 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 che;
50650 20 20 20 20 2f 2a 20 43 61 63 68 65 20 74 68 61 /* Cache tha
50660 74 20 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65 t owns this page
50670 20 2a 2f 0a 0a 20 20 50 67 48 64 72 20 2a 70 44 */.. PgHdr *pD
50680 69 72 74 79 4e 65 78 74 3b 20 20 20 20 20 20 20 irtyNext;
50690 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6c /* Next el
506a0 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 ement in list of
506b0 20 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a dirty pages */.
506c0 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 50 PgHdr *pDirtyP
506d0 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 rev;
506e0 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 65 6c 65 /* Previous ele
506f0 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 ment in list of
50700 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 7d dirty pages */.}
50710 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65 73 ;../* Bit values
50720 20 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67 73 for PgHdr.flags
50730 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 */.#define PGHD
50740 52 5f 44 49 52 54 59 20 20 20 20 20 20 20 20 20 R_DIRTY
50750 20 20 20 20 30 78 30 30 32 20 20 2f 2a 20 50 61 0x002 /* Pa
50760 67 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a ge has changed *
50770 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f /.#define PGHDR_
50780 4e 45 45 44 5f 53 59 4e 43 20 20 20 20 20 20 20 NEED_SYNC
50790 20 20 30 78 30 30 34 20 20 2f 2a 20 46 73 79 6e 0x004 /* Fsyn
507a0 63 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a c the rollback j
507b0 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 0a 20 20 ournal before.
507c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507e0 20 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 ** writing
507f0 74 68 69 73 20 70 61 67 65 20 74 6f 20 74 68 65 this page to the
50800 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 database */.#de
50810 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f fine PGHDR_NEED_
50820 52 45 41 44 20 20 20 20 20 20 20 20 20 30 78 30 READ 0x0
50830 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 08 /* Content i
50840 73 20 75 6e 72 65 61 64 20 2a 2f 0a 23 64 65 66 s unread */.#def
50850 69 6e 65 20 50 47 48 44 52 5f 52 45 55 53 45 5f ine PGHDR_REUSE_
50860 55 4e 4c 49 4b 45 4c 59 20 20 20 20 30 78 30 31 UNLIKELY 0x01
50870 30 20 20 2f 2a 20 41 20 68 69 6e 74 20 74 68 61 0 /* A hint tha
50880 74 20 72 65 75 73 65 20 69 73 20 75 6e 6c 69 6b t reuse is unlik
50890 65 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ely */.#define P
508a0 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 GHDR_DONT_WRITE
508b0 20 20 20 20 20 20 20 30 78 30 32 30 20 20 2f 2a 0x020 /*
508c0 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f Do not write co
508d0 6e 74 65 6e 74 20 74 6f 20 64 69 73 6b 20 2a 2f ntent to disk */
508e0 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ../* Initialize
508f0 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65 and shutdown the
50900 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 page cache subs
50910 79 73 74 65 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f ystem */.SQLITE_
50920 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
50930 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c te3PcacheInitial
50940 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ize(void);.SQLIT
50950 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
50960 71 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 qlite3PcacheShut
50970 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 down(void);../*
50980 50 61 67 65 20 63 61 63 68 65 20 62 75 66 66 65 Page cache buffe
50990 72 20 6d 61 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a r management:.**
509a0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
509b0 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 49 54 45 implement SQLITE
509c0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
509d0 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 E..*/.SQLITE_PRI
509e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
509f0 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74 3PCacheBufferSet
50a00 75 70 28 76 6f 69 64 20 2a 2c 20 69 6e 74 20 73 up(void *, int s
50a10 7a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 z, int n);../* C
50a20 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 reate a new page
50a30 72 20 63 61 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 r cache..** Unde
50a40 72 20 6d 65 6d 6f 72 79 20 73 74 72 65 73 73 2c r memory stress,
50a50 20 69 6e 76 6f 6b 65 20 78 53 74 72 65 73 73 20 invoke xStress
50a60 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 to try to make p
50a70 61 67 65 73 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f ages clean..** O
50a80 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64 20 75 6e nly clean and un
50a90 70 69 6e 6e 65 64 20 70 61 67 65 73 20 63 61 6e pinned pages can
50aa0 20 62 65 20 72 65 63 6c 61 69 6d 65 64 2e 0a 2a be reclaimed..*
50ab0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
50ac0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
50ad0 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 cheOpen(. int s
50ae0 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 zPage,
50af0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
50b00 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 e of every page
50b10 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 */. int szExtra
50b20 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
50b30 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 /* Extra spa
50b40 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ce associated wi
50b50 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a th each page */.
50b60 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 int bPurgeable
50b70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
50b80 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 /* True if page
50b90 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 s are on backing
50ba0 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 store */. int
50bb0 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a (*xStress)(void*
50bc0 2c 20 50 67 48 64 72 2a 29 2c 20 2f 2a 20 43 61 , PgHdr*), /* Ca
50bd0 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b ll to try to mak
50be0 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f e pages clean */
50bf0 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 . void *pStress
50c00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
50c10 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
50c20 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 xStress */. PC
50c30 61 63 68 65 20 2a 70 54 6f 49 6e 69 74 20 20 20 ache *pToInit
50c40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
50c50 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 Preallocated spa
50c60 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 68 ce for the PCach
50c70 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 e */.);../* Modi
50c80 66 79 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 fy the page-size
50c90 20 61 66 74 65 72 20 74 68 65 20 63 61 63 68 65 after the cache
50ca0 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 has been create
50cb0 64 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d. */.SQLITE_PRI
50cc0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
50cd0 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 3PcacheSetPageSi
50ce0 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 ze(PCache *, int
50cf0 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 );../* Return th
50d00 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 e size in bytes
50d10 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 of a PCache obje
50d20 63 74 2e 20 20 55 73 65 64 20 74 6f 20 70 72 65 ct. Used to pre
50d30 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 74 6f 72 allocate.** stor
50d40 61 67 65 20 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 age space..*/.SQ
50d50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
50d60 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 sqlite3PcacheSi
50d70 7a 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e ze(void);../* On
50d80 65 20 72 65 6c 65 61 73 65 20 70 65 72 20 73 75 e release per su
50d90 63 63 65 73 73 66 75 6c 20 66 65 74 63 68 2e 20 ccessful fetch.
50da0 20 50 61 67 65 20 69 73 20 70 69 6e 6e 65 64 20 Page is pinned
50db0 75 6e 74 69 6c 20 72 65 6c 65 61 73 65 64 2e 0a until released..
50dc0 2a 2a 20 52 65 66 65 72 65 6e 63 65 20 63 6f 75 ** Reference cou
50dd0 6e 74 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 nted. .*/.SQLITE
50de0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
50df0 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
50e00 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 PCache*, Pgno, i
50e10 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 50 nt createFlag, P
50e20 67 48 64 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f gHdr**);.SQLITE_
50e30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
50e40 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
50e50 65 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 e(PgHdr*);..SQLI
50e60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
50e70 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f sqlite3PcacheDro
50e80 70 28 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20 p(PgHdr*);
50e90 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 70 61 67 /* Remove pag
50ea0 65 20 66 72 6f 6d 20 63 61 63 68 65 20 2a 2f 0a e from cache */.
50eb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
50ec0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
50ed0 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 eMakeDirty(PgHdr
50ee0 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 *); /* Make s
50ef0 75 72 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b ure page is mark
50f00 65 64 20 64 69 72 74 79 20 2a 2f 0a 53 51 4c 49 ed dirty */.SQLI
50f10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
50f20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
50f30 65 43 6c 65 61 6e 28 50 67 48 64 72 2a 29 3b 20 eClean(PgHdr*);
50f40 20 20 20 2f 2a 20 4d 61 72 6b 20 61 20 73 69 6e /* Mark a sin
50f50 67 6c 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 gle page as clea
50f60 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 n */.SQLITE_PRIV
50f70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
50f80 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 PcacheCleanAll(P
50f90 43 61 63 68 65 2a 29 3b 20 20 20 20 2f 2a 20 4d Cache*); /* M
50fa0 61 72 6b 20 61 6c 6c 20 64 69 72 74 79 20 6c 69 ark all dirty li
50fb0 73 74 20 70 61 67 65 73 20 61 73 20 63 6c 65 61 st pages as clea
50fc0 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 n */../* Change
50fd0 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 a page number.
50fe0 55 73 65 64 20 62 79 20 69 6e 63 72 2d 76 61 63 Used by incr-vac
50ff0 75 75 6d 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 uum. */.SQLITE_P
51000 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
51010 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 50 67 te3PcacheMove(Pg
51020 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a Hdr*, Pgno);../*
51030 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70 61 67 65 Remove all page
51040 73 20 77 69 74 68 20 70 67 6e 6f 3e 78 2e 20 20 s with pgno>x.
51050 52 65 73 65 74 20 74 68 65 20 63 61 63 68 65 20 Reset the cache
51060 69 66 20 78 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 if x==0 */.SQLIT
51070 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
51080 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e qlite3PcacheTrun
51090 63 61 74 65 28 50 43 61 63 68 65 2a 2c 20 50 67 cate(PCache*, Pg
510a0 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 no x);../* Get a
510b0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 list of all dir
510c0 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ty pages in the
510d0 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 cache, sorted by
510e0 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
510f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 SQLITE_PRIVATE P
51100 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 gHdr *sqlite3Pca
51110 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 cheDirtyList(PCa
51120 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 che*);../* Reset
51130 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 63 and close the c
51140 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 ache object */.S
51150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
51160 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
51170 43 6c 6f 73 65 28 50 43 61 63 68 65 2a 29 3b 0a Close(PCache*);.
51180 0a 2f 2a 20 43 6c 65 61 72 20 66 6c 61 67 73 20 ./* Clear flags
51190 66 72 6f 6d 20 70 61 67 65 73 20 6f 66 20 74 68 from pages of th
511a0 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a e page cache */.
511b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
511c0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
511d0 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 eClearSyncFlags(
511e0 50 43 61 63 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 PCache *);../* D
511f0 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 iscard the conte
51200 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 nts of the cache
51210 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
51220 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
51230 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68 cacheClear(PCach
51240 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 e*);../* Return
51250 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
51260 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 of outstanding
51270 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 page references
51280 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
51290 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
512a0 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 cheRefCount(PCac
512b0 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d he*);../* Increm
512c0 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 ent the referenc
512d0 65 20 63 6f 75 6e 74 20 6f 66 20 61 6e 20 65 78 e count of an ex
512e0 69 73 74 69 6e 67 20 70 61 67 65 20 2a 2f 0a 53 isting page */.S
512f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
51300 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
51310 52 65 66 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 Ref(PgHdr*);..SQ
51320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
51330 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 sqlite3PcachePa
51340 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72 geRefcount(PgHdr
51350 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 *);../* Return t
51360 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
51370 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64 20 of pages stored
51380 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a in the cache */.
51390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
513a0 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
513b0 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 Pagecount(PCache
513c0 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 *);..#ifdef SQLI
513d0 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f TE_CHECK_PAGES./
513e0 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f 75 67 * Iterate throug
513f0 68 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 h all dirty page
51400 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 s currently stor
51410 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e ed in the cache.
51420 20 54 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 This.** interfa
51430 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ce is only avail
51440 61 62 6c 65 20 69 66 20 53 51 4c 49 54 45 5f 43 able if SQLITE_C
51450 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 HECK_PAGES is de
51460 66 69 6e 65 64 20 77 68 65 6e 20 74 68 65 20 0a fined when the .
51470 2a 2a 20 6c 69 62 72 61 72 79 20 69 73 20 62 75 ** library is bu
51480 69 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ilt..*/.SQLITE_P
51490 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
514a0 74 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 te3PcacheIterate
514b0 44 69 72 74 79 28 50 43 61 63 68 65 20 2a 70 43 Dirty(PCache *pC
514c0 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49 74 ache, void (*xIt
514d0 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0a 23 er)(PgHdr *));.#
514e0 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 61 6e endif../* Set an
514f0 64 20 67 65 74 20 74 68 65 20 73 75 67 67 65 73 d get the sugges
51500 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 66 ted cache-size f
51510 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 or the specified
51520 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 2a 2a pager-cache..**
51530 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 61 6c .** If no global
51540 20 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f 6e 66 maximum is conf
51550 69 67 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65 igured, then the
51560 20 73 79 73 74 65 6d 20 61 74 74 65 6d 70 74 73 system attempts
51570 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 68 65 to limit.** the
51580 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
51590 20 70 61 67 65 73 20 63 61 63 68 65 64 20 62 79 pages cached by
515a0 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 72 purgeable pager
515b0 2d 63 61 63 68 65 73 20 74 6f 20 74 68 65 20 73 -caches to the s
515c0 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 67 um.** of the sug
515d0 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a gested cache-siz
515e0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
515f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
51600 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 e3PcacheSetCache
51610 73 69 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 size(PCache *, i
51620 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 nt);.#ifdef SQLI
51630 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 TE_TEST.SQLITE_P
51640 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
51650 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 e3PcacheGetCache
51660 73 69 7a 65 28 50 43 61 63 68 65 20 2a 29 3b 0a size(PCache *);.
51670 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
51680 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
51690 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f ORY_MANAGEMENT./
516a0 2a 20 54 72 79 20 74 6f 20 72 65 74 75 72 6e 20 * Try to return
516b0 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 74 memory used by t
516c0 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 he pcache module
516d0 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 6d 65 6d to the main mem
516e0 6f 72 79 20 68 65 61 70 20 2a 2f 0a 53 51 4c 49 ory heap */.SQLI
516f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
51700 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 qlite3PcacheRele
51710 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a aseMemory(int);.
51720 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
51730 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
51740 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
51750 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74 qlite3PcacheStat
51760 73 28 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a s(int*,int*,int*
51770 2c 69 6e 74 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a ,int*);.#endif..
51780 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
51790 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 oid sqlite3PCach
517a0 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 eSetDefault(void
517b0 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 );..#endif /* _P
517c0 43 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a CACHE_H_ */../**
517d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
517e0 20 6f 66 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a of pcache.h ***
517f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
51820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
51830 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
51840 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c left off in sql
51850 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
51860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
51870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
51880 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20 74 68 clude os.h in th
51890 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
518a0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
518b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
518c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
518d0 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20 2a 2a gin file os.h **
518e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
518f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
51910 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
51920 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 16.**.** The
51930 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
51940 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
51950 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
51960 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
51970 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
51980 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
51990 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
519a0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
519b0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
519c0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
519d0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
519e0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
519f0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
51a00 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
51a10 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
51a20 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
51a30 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
51a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
51a90 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 This header file
51aa0 20 28 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 (together with
51ab0 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 20 73 is companion C s
51ac0 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 6c 65 0a ource-code file.
51ad0 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 65 6d ** "os.c") attem
51ae0 70 74 20 74 6f 20 61 62 73 74 72 61 63 74 20 74 pt to abstract t
51af0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 he underlying op
51b00 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 erating system s
51b10 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 53 51 o that.** the SQ
51b20 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c Lite library wil
51b30 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68 20 50 l work on both P
51b40 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 6f 77 73 OSIX and windows
51b50 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 systems..**.**
51b60 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 This header file
51b70 20 69 73 20 23 69 6e 63 6c 75 64 65 2d 65 64 20 is #include-ed
51b80 62 79 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 61 by sqliteInt.h a
51b90 6e 64 20 74 68 75 73 20 65 6e 64 73 20 75 70 0a nd thus ends up.
51ba0 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75 64 65 ** being include
51bb0 64 20 62 79 20 65 76 65 72 79 20 73 6f 75 72 63 d by every sourc
51bc0 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 e file..**.** $I
51bd0 64 3a 20 6f 73 2e 68 2c 76 20 31 2e 31 30 38 20 d: os.h,v 1.108
51be0 32 30 30 39 2f 30 32 2f 30 35 20 31 36 3a 33 31 2009/02/05 16:31
51bf0 3a 34 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :46 drh Exp $.*/
51c00 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 .#ifndef _SQLITE
51c10 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f _OS_H_.#define _
51c20 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a SQLITE_OS_H_../*
51c30 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 .** Figure out i
51c40 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 f we are dealing
51c50 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e 64 with Unix, Wind
51c60 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74 68 ows, or some oth
51c70 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 er.** operating
51c80 73 79 73 74 65 6d 2e 20 20 41 66 74 65 72 20 74 system. After t
51c90 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
51ca0 63 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 73 73 ck of preprocess
51cb0 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 macros,.** all
51cc0 6f 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 of SQLITE_OS_UNI
51cd0 58 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e X, SQLITE_OS_WIN
51ce0 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c , SQLITE_OS_OS2,
51cf0 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 5f 4f and SQLITE_OS_O
51d00 54 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 64 65 THER .** will de
51d10 66 69 6e 65 64 20 74 6f 20 65 69 74 68 65 72 20 fined to either
51d20 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66 20 1 or 0. One of
51d30 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62 65 the four will be
51d40 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20 0a 1. The other .
51d50 2a 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 62 65 ** three will be
51d60 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 0..*/.#if defin
51d70 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 ed(SQLITE_OS_OTH
51d80 45 52 29 0a 23 20 69 66 20 53 51 4c 49 54 45 5f ER).# if SQLITE_
51d90 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20 20 OS_OTHER==1.#
51da0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f undef SQLITE_OS_
51db0 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65 20 UNIX.# define
51dc0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 SQLITE_OS_UNIX 0
51dd0 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 .# undef SQLIT
51de0 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 65 66 E_OS_WIN.# def
51df0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
51e00 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51 N 0.# undef SQ
51e10 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 LITE_OS_OS2.#
51e20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
51e30 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23 20 _OS2 0.# else.#
51e40 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f undef SQLITE_O
51e50 53 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 66 0a S_OTHER.# endif.
51e60 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 69 #endif.#if !defi
51e70 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 55 4e ned(SQLITE_OS_UN
51e80 49 58 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 IX) && !defined(
51e90 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 SQLITE_OS_OTHER)
51ea0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
51eb0 5f 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69 66 _OS_OTHER 0.# if
51ec0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 ndef SQLITE_OS_W
51ed0 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e 65 IN.# if define
51ee0 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 d(_WIN32) || def
51ef0 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20 64 ined(WIN32) || d
51f00 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f efined(__CYGWIN_
51f10 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f _) || defined(__
51f20 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64 65 MINGW32__) || de
51f30 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 fined(__BORLANDC
51f40 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65 __).# define
51f50 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 31 SQLITE_OS_WIN 1
51f60 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 .# define SQ
51f70 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 LITE_OS_UNIX 0.#
51f80 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
51f90 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 20 TE_OS_OS2 0.#
51fa0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 45 elif defined(__E
51fb0 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 MX__) || defined
51fc0 28 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 (_OS2) || define
51fd0 64 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 d(OS2) || define
51fe0 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66 69 d(_OS2_) || defi
51ff0 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 ned(__OS2__).#
52000 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
52010 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 _OS_WIN 0.#
52020 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
52030 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65 _UNIX 0.# de
52040 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f fine SQLITE_OS_O
52050 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23 20 S2 1.# else.#
52060 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
52070 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 E_OS_WIN 0.#
52080 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
52090 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 20 64 S_UNIX 1.# d
520a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
520b0 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 OS2 0.# endif.#
520c0 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 else.# define
520d0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 SQLITE_OS_UNIX 0
520e0 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 .# define SQLIT
520f0 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e 64 E_OS_OS2 0.# end
52100 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64 65 if.#else.# ifnde
52110 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a f SQLITE_OS_WIN.
52120 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 # define SQLITE
52130 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64 69 _OS_WIN 0.# endi
52140 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 f.#endif../*.**
52150 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 Determine if we
52160 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
52170 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 WindowsCE - whi
52180 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a ch has a much.**
52190 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f reduced API..*/
521a0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 .#if defined(_WI
521b0 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e N32_WCE).# defin
521c0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 e SQLITE_OS_WINC
521d0 45 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 E 1.#else.# defi
521e0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e ne SQLITE_OS_WIN
521f0 43 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a CE 0.#endif.../*
52200 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 6d .** Define the m
52210 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 aximum size of a
52220 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 6e temporary filen
52230 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ame.*/.#if SQLIT
52240 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 E_OS_WIN.# inclu
52250 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 de <windows.h>.#
52260 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 define SQLITE_T
52270 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 4d 41 EMPNAME_SIZE (MA
52280 58 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c 69 66 X_PATH+50).#elif
52290 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 SQLITE_OS_OS2.#
522a0 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 if (__GNUC__ >
522b0 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 3 || __GNUC__ ==
522c0 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 3 && __GNUC_MIN
522d0 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64 65 OR__ >= 3) && de
522e0 66 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f 4d fined(OS2_HIGH_M
522f0 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75 64 EMORY).# includ
52300 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f 2a e <os2safe.h> /*
52310 20 68 61 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 has to be inclu
52320 64 65 64 20 62 65 66 6f 72 65 20 6f 73 32 2e 68 ded before os2.h
52330 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 for linking to
52340 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66 0a work */.# endif.
52350 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f # define INCL_DO
52360 53 44 41 54 45 54 49 4d 45 0a 23 20 64 65 66 69 SDATETIME.# defi
52370 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d ne INCL_DOSFILEM
52380 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c GR.# define INCL
52390 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 65 66 _DOSERRORS.# def
523a0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 ine INCL_DOSMISC
523b0 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 .# define INCL_D
523c0 4f 53 50 52 4f 43 45 53 53 0a 23 20 64 65 66 69 OSPROCESS.# defi
523d0 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c ne INCL_DOSMODUL
523e0 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e EMGR.# define IN
523f0 43 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 45 53 CL_DOSSEMAPHORES
52400 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 2e .# include <os2.
52410 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 63 h>.# include <uc
52420 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 onv.h>.# define
52430 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f SQLITE_TEMPNAME_
52440 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54 48 SIZE (CCHMAXPATH
52450 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 COMP).#else.# de
52460 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 fine SQLITE_TEMP
52470 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23 65 NAME_SIZE 200.#e
52480 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65 20 ndif../* If the
52490 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 63 SET_FULLSYNC mac
524a0 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 ro is not define
524b0 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d 61 d above, then ma
524c0 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 ke it.** a no-op
524d0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 54 5f .*/.#ifndef SET_
524e0 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69 6e FULLSYNC.# defin
524f0 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28 78 e SET_FULLSYNC(x
52500 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ,y).#endif../*.*
52510 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69 * The default si
52520 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63 ze of a disk sec
52530 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 tor.*/.#ifndef S
52540 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
52550 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66 69 CTOR_SIZE.# defi
52560 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ne SQLITE_DEFAUL
52570 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31 T_SECTOR_SIZE 51
52580 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 2.#endif../*.**
52590 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 Temporary files
525a0 61 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74 69 are named starti
525b0 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72 65 ng with this pre
525c0 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 fix followed by
525d0 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 16 random.** alp
525e0 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63 hanumeric charac
525f0 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 6c ters, and no fil
52600 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 65 e extension. The
52610 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 y are stored in
52620 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61 6e the.** OS's stan
52630 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20 66 dard temporary f
52640 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20 61 ile directory, a
52650 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64 20 70 nd are deleted p
52660 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a 2a rior to exit..**
52670 20 49 66 20 73 71 6c 69 74 65 20 69 73 20 62 65 If sqlite is be
52680 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69 6e 20 ing embedded in
52690 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 6d 2c another program,
526a0 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20 74 6f you may wish to
526b0 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 70 change the.** p
526c0 72 65 66 69 78 20 74 6f 20 72 65 66 6c 65 63 74 refix to reflect
526d0 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 73 20 your program's
526e0 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 69 66 name, so that if
526f0 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 65 78 your program ex
52700 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72 65 its.** premature
52710 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61 72 ly, old temporar
52720 79 20 66 69 6c 65 73 20 63 61 6e 20 62 65 20 65 y files can be e
52730 61 73 69 6c 79 20 69 64 65 6e 74 69 66 69 65 64 asily identified
52740 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 6f . This can be do
52750 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 53 51 ne.** using -DSQ
52760 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 LITE_TEMP_FILE_P
52770 52 45 46 49 58 3d 6d 79 70 72 65 66 69 78 5f 20 REFIX=myprefix_
52780 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 on the compiler
52790 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a command line..**
527a0 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 3a 20 .** 2006-10-31:
527b0 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 72 65 The default pre
527c0 66 69 78 20 75 73 65 64 20 74 6f 20 62 65 20 22 fix used to be "
527d0 73 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 20 74 sqlite_". But t
527e0 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 73 74 hen.** Mcafee st
527f0 61 72 74 65 64 20 75 73 69 6e 67 20 53 51 4c 69 arted using SQLi
52800 74 65 20 69 6e 20 74 68 65 69 72 20 61 6e 74 69 te in their anti
52810 2d 76 69 72 75 73 20 70 72 6f 64 75 63 74 20 61 -virus product a
52820 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 65 64 nd it.** started
52830 20 70 75 74 74 69 6e 67 20 66 69 6c 65 73 20 77 putting files w
52840 69 74 68 20 74 68 65 20 22 73 71 6c 69 74 65 22 ith the "sqlite"
52850 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 3a 2f name in the c:/
52860 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20 temp folder..**
52870 54 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d 61 6e This annoyed man
52880 79 20 77 69 6e 64 6f 77 73 20 75 73 65 72 73 2e y windows users.
52890 20 20 54 68 6f 73 65 20 75 73 65 72 73 20 77 6f Those users wo
528a0 75 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 0a 2a uld then do a .*
528b0 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 68 20 * Google search
528c0 66 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 66 69 for "sqlite", fi
528d0 6e 64 20 74 68 65 20 74 65 6c 65 70 68 6f 6e 65 nd the telephone
528e0 20 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 65 0a numbers of the.
528f0 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 61 6e ** developers an
52900 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 20 74 d call to wake t
52910 68 65 6d 20 75 70 20 61 74 20 6e 69 67 68 74 20 hem up at night
52920 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a and complain..**
52930 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e For this reason
52940 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61 , the default na
52950 6d 65 20 70 72 65 66 69 78 20 69 73 20 63 68 61 me prefix is cha
52960 6e 67 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 nged to be "sqli
52970 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 te" .** spelled
52980 62 61 63 6b 77 61 72 64 73 2e 20 20 53 6f 20 74 backwards. So t
52990 68 65 20 74 65 6d 70 20 66 69 6c 65 73 20 61 72 he temp files ar
529a0 65 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 66 69 e still identifi
529b0 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 62 6f ed, but.** anybo
529c0 64 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68 20 dy smart enough
529d0 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 to figure out th
529e0 65 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20 6c e code is also l
529f0 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a 20 65 ikely smart.** e
52a00 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74 68 nough to know th
52a10 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 at calling the d
52a20 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e 6f eveloper will no
52a30 74 20 68 65 6c 70 20 67 65 74 20 72 69 64 0a 2a t help get rid.*
52a40 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a * of the file..*
52a50 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
52a60 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 _TEMP_FILE_PREFI
52a70 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 X.# define SQLIT
52a80 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
52a90 49 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 65 6e IX "etilqs_".#en
52aa0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
52ab0 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 20 ollowing values
52ac0 6d 61 79 20 62 65 20 70 61 73 73 65 64 20 61 73 may be passed as
52ad0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
52ae0 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 ment to.** sqlit
52af0 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 65 20 e3OsLock(). The
52b00 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 65 78 various locks ex
52b10 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c 6f 77 hibit the follow
52b20 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a 0a 2a ing semantics:.*
52b30 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 20 20 *.** SHARED:
52b40 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 Any number of pr
52b50 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 ocesses may hold
52b60 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 73 a SHARED lock s
52b70 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a imultaneously..*
52b80 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41 20 73 * RESERVED: A s
52b90 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 ingle process ma
52ba0 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 y hold a RESERVE
52bb0 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 D lock on a file
52bc0 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 at.**
52bd0 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 68 65 any time. Othe
52be0 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 r processes may
52bf0 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 6e 20 hold and obtain
52c00 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 new SHARED locks
52c10 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20 20 ..** PENDING:
52c20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 A single process
52c30 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 4e 44 may hold a PEND
52c40 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 ING lock on a fi
52c50 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 le at.**
52c60 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d 65 any one time
52c70 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41 52 45 . Existing SHARE
52c80 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72 73 D locks may pers
52c90 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77 0a ist, but no new.
52ca0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 53 48 ** SH
52cb0 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 62 ARED locks may b
52cc0 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f 74 e obtained by ot
52cd0 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a her processes..*
52ce0 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e 20 * EXCLUSIVE: An
52cf0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 70 EXCLUSIVE lock p
52d00 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68 recludes all oth
52d10 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 er locks..**.**
52d20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79 PENDING_LOCK may
52d30 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64 20 64 not be passed d
52d40 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c 69 74 irectly to sqlit
52d50 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 73 74 e3OsLock(). Inst
52d60 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 65 73 ead, a.** proces
52d70 73 20 74 68 61 74 20 72 65 71 75 65 73 74 73 20 s that requests
52d80 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
52d90 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 6f k may actually o
52da0 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 0a btain a PENDING.
52db0 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 63 61 ** lock. This ca
52dc0 6e 20 62 65 20 75 70 67 72 61 64 65 64 20 74 6f n be upgraded to
52dd0 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
52de0 63 6b 20 62 79 20 61 20 73 75 62 73 65 71 75 65 ck by a subseque
52df0 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 nt call to.** sq
52e00 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a lite3OsLock()..*
52e10 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43 /.#define NO_LOC
52e20 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 K 0.#def
52e30 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ine SHARED_LOCK
52e40 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 52 45 1.#define RE
52e50 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 32 0a SERVED_LOCK 2.
52e60 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f #define PENDING_
52e70 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 69 6e LOCK 3.#defin
52e80 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b e EXCLUSIVE_LOCK
52e90 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4../*.** File
52ea0 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20 20 Locking Notes:
52eb0 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77 69 (Mostly about wi
52ec0 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20 73 ndows but also s
52ed0 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e 69 ome info for Uni
52ee0 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e x).**.** We cann
52ef0 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65 45 ot use LockFileE
52f00 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c x() or UnlockFil
52f10 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f 39 eEx() on Win95/9
52f20 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a 2a 20 8/ME because.**
52f30 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 those functions
52f40 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c are not availabl
52f50 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20 6f 6e e. So we use on
52f60 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 61 6e ly LockFile() an
52f70 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 d.** UnlockFile(
52f80 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c )..**.** LockFil
52f90 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e 6f 74 e() prevents not
52fa0 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20 62 75 just writing bu
52fb0 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 20 62 t also reading b
52fc0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 y other processe
52fd0 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 5f 4c s..** A SHARED_L
52fe0 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 OCK is obtained
52ff0 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 69 6e by locking a sin
53000 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f gle randomly-cho
53010 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f 75 74 sen .** byte out
53020 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 20 72 of a specific r
53030 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e 20 54 ange of bytes. T
53040 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 73 20 he lock byte is
53050 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a 2a 20 obtained at .**
53060 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 73 65 random so two se
53070 70 61 72 61 74 65 20 72 65 61 64 65 72 73 20 63 parate readers c
53080 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 63 65 an probably acce
53090 73 73 20 74 68 65 20 66 69 6c 65 20 61 74 20 74 ss the file at t
530a0 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65 he .** same time
530b0 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20 61 72 , unless they ar
530c0 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 63 68 e unlucky and ch
530d0 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20 6c 6f oose the same lo
530e0 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e 20 45 ck byte..** An E
530f0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 69 73 XCLUSIVE_LOCK is
53100 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 obtained by loc
53110 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 20 69 king all bytes i
53120 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2a 20 n the range..**
53130 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 There can only b
53140 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 20 41 e one writer. A
53150 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 69 RESERVED_LOCK i
53160 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f s obtained by lo
53170 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e 67 6c cking.** a singl
53180 65 20 62 79 74 65 20 6f 66 20 74 68 65 20 66 69 e byte of the fi
53190 6c 65 20 74 68 61 74 20 69 73 20 64 65 73 69 67 le that is desig
531a0 6e 61 74 65 64 20 61 73 20 74 68 65 20 72 65 73 nated as the res
531b0 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 65 2e erved lock byte.
531c0 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f 4c 4f .** A PENDING_LO
531d0 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 CK is obtained b
531e0 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 73 69 y locking a desi
531f0 67 6e 61 74 65 64 20 62 79 74 65 20 64 69 66 66 gnated byte diff
53200 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 erent from.** th
53210 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 e RESERVED_LOCK
53220 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 byte..**.** On W
53230 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74 65 inNT/2K/XP syste
53240 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 ms, LockFileEx()
53250 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 and UnlockFileE
53260 78 28 29 20 61 72 65 20 61 76 61 69 6c 61 62 6c x() are availabl
53270 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e e,.** which mean
53280 73 20 77 65 20 63 61 6e 20 75 73 65 20 72 65 61 s we can use rea
53290 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 der/writer locks
532a0 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f 77 . When reader/w
532b0 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61 riter locks.** a
532c0 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c 6f 63 re used, the loc
532d0 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e 20 74 k is placed on t
532e0 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20 6f 66 he same range of
532f0 20 62 79 74 65 73 20 74 68 61 74 20 69 73 20 75 bytes that is u
53300 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f 62 61 sed.** for proba
53310 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 6e 67 bilistic locking
53320 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e in Win95/98/ME.
53330 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f 63 Hence, the loc
53340 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a 20 77 king scheme.** w
53350 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 6f 20 ill support two
53360 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 72 65 or more Win95 re
53370 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f 72 20 aders or two or
53380 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 64 65 more WinNT reade
53390 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 69 6e rs..** But a sin
533a0 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 gle Win95 reader
533b0 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 will lock out a
533c0 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73 ll WinNT readers
533d0 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a 2a 2a and a single.**
533e0 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20 77 69 WinNT reader wi
533f0 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 ll lock out all
53400 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65 61 64 other Win95 read
53410 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ers..**.** The f
53420 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 6e 65 ollowing #define
53430 73 20 73 70 65 63 69 66 79 20 74 68 65 20 72 61 s specify the ra
53440 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75 73 65 nge of bytes use
53450 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a d for locking..*
53460 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73 * SHARED_SIZE is
53470 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
53480 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 69 ytes available i
53490 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f 6d 20 n the pool from
534a0 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e 64 6f which.** a rando
534b0 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65 63 74 m byte is select
534c0 65 64 20 66 6f 72 20 61 20 73 68 61 72 65 64 20 ed for a shared
534d0 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f 6c 20 lock. The pool
534e0 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a 2a 20 of bytes for.**
534f0 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 65 67 shared locks beg
53500 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f 46 49 ins at SHARED_FI
53510 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 RST. .**.** The
53520 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 same locking str
53530 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79 74 ategy and.** byt
53540 65 20 72 61 6e 67 65 73 20 61 72 65 20 75 73 65 e ranges are use
53550 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69 d for Unix. Thi
53560 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68 s leaves open th
53570 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20 e possiblity of
53580 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 having.** client
53590 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e s on win95, winN
535a0 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20 T, and unix all
535b0 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 talking to the s
535c0 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0a ame shared file.
535d0 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 ** and all locki
535e0 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 ng correctly. T
535f0 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65 o do so would re
53600 71 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61 quire that samba
53610 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a 2a (or whatever.**
53620 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20 75 tool is being u
53630 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68 61 sed for file sha
53640 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74 73 ring) implements
53650 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c 79 locks correctly
53660 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 between.** wind
53670 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20 49 ows and unix. I
53680 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61 74 'm guessing that
53690 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74 6f isn't likely to
536a0 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79 0a happen, but by.
536b0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d ** using the sam
536c0 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 e locking range
536d0 77 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 we are at least
536e0 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73 73 open to the poss
536f0 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c ibility..**.** L
53700 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f 77 ocking in window
53710 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e 20 s is manditory.
53720 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e For this reason
53730 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 72 , we cannot stor
53740 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74 61 e.** actual data
53750 20 69 6e 20 74 68 65 20 62 79 74 65 73 20 75 73 in the bytes us
53760 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 ed for locking.
53770 20 54 68 65 20 70 61 67 65 72 20 6e 65 76 65 72 The pager never
53780 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74 68 allocates.** th
53790 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 e pages involved
537a0 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 72 in locking ther
537b0 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f 53 efore. SHARED_S
537c0 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64 20 IZE is selected
537d0 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 6c so.** that all l
537e0 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f 6e ocks will fit on
537f0 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 65 a single page e
53800 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69 6d ven at the minim
53810 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a um page size..**
53820 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64 65 PENDING_BYTE de
53830 66 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e 6e fines the beginn
53840 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73 ing of the locks
53850 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20 50 45 . By default PE
53860 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69 73 NDING_BYTE.** is
53870 20 73 65 74 20 68 69 67 68 20 73 6f 20 74 68 61 set high so tha
53880 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20 t we don't have
53890 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 75 to allocate an u
538a0 6e 75 73 65 64 20 70 61 67 65 20 65 78 63 65 70 nused page excep
538b0 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c 61 t.** for very la
538c0 72 67 65 20 64 61 74 61 62 61 73 65 73 2e 20 20 rge databases.
538d0 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20 74 But one should t
538e0 65 73 74 20 74 68 65 20 70 61 67 65 20 73 6b 69 est the page ski
538f0 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 pping logic .**
53900 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44 49 by setting PENDI
53910 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64 20 NG_BYTE low and
53920 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69 running the enti
53930 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73 75 re regression su
53940 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 ite..**.** Chang
53950 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ing the value of
53960 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65 PENDING_BYTE re
53970 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c sults in a subtl
53980 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a 2a y incompatible.*
53990 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20 * file format.
539a0 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 Depending on how
539b0 20 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20 it is changed,
539c0 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f you might not no
539d0 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63 6f tice.** the inco
539e0 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68 mpatibility righ
539f0 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e t away, even run
53a00 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72 ning a full regr
53a10 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 ession test..**
53a20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 61 The default loca
53a30 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47 5f tion of PENDING_
53a40 42 59 54 45 20 69 73 20 74 68 65 20 66 69 72 73 BYTE is the firs
53a50 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a t byte past the.
53a60 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2e ** 1GB boundary.
53a70 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 .**.*/.#define P
53a80 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 ENDING_BYTE
53a90 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 sqlite3PendingB
53aa0 79 74 65 0a 23 64 65 66 69 6e 65 20 52 45 53 45 yte.#define RESE
53ab0 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 28 50 RVED_BYTE (P
53ac0 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 0a 23 ENDING_BYTE+1).#
53ad0 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 46 49 define SHARED_FI
53ae0 52 53 54 20 20 20 20 20 20 28 50 45 4e 44 49 4e RST (PENDIN
53af0 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 69 6e G_BYTE+2).#defin
53b00 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20 20 20 e SHARED_SIZE
53b10 20 20 20 20 35 31 30 0a 0a 2f 2a 20 0a 2a 2a 20 510../* .**
53b20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 Functions for ac
53b30 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f cessing sqlite3_
53b40 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 2a 2f file methods .*/
53b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53b60 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f int sqlite3OsClo
53b70 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a se(sqlite3_file*
53b80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
53b90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 E int sqlite3OsR
53ba0 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ead(sqlite3_file
53bb0 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d *, void*, int am
53bc0 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a t, i64 offset);.
53bd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53be0 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 nt sqlite3OsWrit
53bf0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c e(sqlite3_file*,
53c00 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
53c10 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 t amt, i64 offse
53c20 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
53c30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
53c40 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 Truncate(sqlite3
53c50 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a 65 _file*, i64 size
53c60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
53c70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 E int sqlite3OsS
53c80 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ync(sqlite3_file
53c90 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
53ca0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
53cb0 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 te3OsFileSize(sq
53cc0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 lite3_file*, i64
53cd0 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 *pSize);.SQLITE
53ce0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53cf0 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74 ite3OsLock(sqlit
53d00 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
53d10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53d20 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f nt sqlite3OsUnlo
53d30 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ck(sqlite3_file*
53d40 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
53d50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
53d60 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
53d70 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 dLock(sqlite3_fi
53d80 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 le *id, int *pRe
53d90 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sOut);.SQLITE_PR
53da0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
53db0 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 3OsFileControl(s
53dc0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 qlite3_file*,int
53dd0 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 ,void*);.#define
53de0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 SQLITE_FCNTL_DB
53df0 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63 61 30 _UNCHANGED 0xca0
53e00 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50 52 49 93fa0.SQLITE_PRI
53e10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53e20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c OsSectorSize(sql
53e30 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a ite3_file *id);.
53e40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53e50 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 nt sqlite3OsDevi
53e60 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
53e70 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a s(sqlite3_file *
53e80 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e id);../* .** Fun
53e90 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 ctions for acces
53ea0 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76 66 73 sing sqlite3_vfs
53eb0 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c methods .*/.SQL
53ec0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53ed0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 73 71 sqlite3OsOpen(sq
53ee0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
53ef0 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 st char *, sqlit
53f00 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c 20 69 e3_file*, int, i
53f10 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt *);.SQLITE_PR
53f20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
53f30 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 3OsDelete(sqlite
53f40 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 3_vfs *, const c
53f50 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c har *, int);.SQL
53f60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53f70 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
53f80 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 sqlite3_vfs *, c
53f90 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
53fa0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b , int *pResOut);
53fb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53fc0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c int sqlite3OsFul
53fd0 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65 lPathname(sqlite
53fe0 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 3_vfs *, const c
53ff0 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 har *, int, char
54000 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c *);.#ifndef SQL
54010 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
54020 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 TENSION.SQLITE_P
54030 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
54040 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c ite3OsDlOpen(sql
54050 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 ite3_vfs *, cons
54060 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 t char *);.SQLIT
54070 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
54080 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 qlite3OsDlError(
54090 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 sqlite3_vfs *, i
540a0 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nt, char *);.SQL
540b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
540c0 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 (*sqlite3OsDlSy
540d0 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c m(sqlite3_vfs *,
540e0 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63 void *, const c
540f0 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b 0a 53 har *))(void);.S
54100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
54110 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c id sqlite3OsDlCl
54120 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ose(sqlite3_vfs
54130 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65 6e 64 *, void *);.#end
54140 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
54150 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
54160 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
54170 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
54180 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 Randomness(sqlit
54190 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63 e3_vfs *, int, c
541a0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har *);.SQLITE_P
541b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
541c0 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65 e3OsSleep(sqlite
541d0 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b 0a 53 3_vfs *, int);.S
541e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
541f0 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 t sqlite3OsCurre
54200 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 ntTime(sqlite3_v
54210 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a fs *, double*);.
54220 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e ./*.** Convenien
54230 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 ce functions for
54240 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f opening and clo
54250 73 69 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67 sing files using
54260 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c .** sqlite3_mal
54270 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 loc() to obtain
54280 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 66 69 space for the fi
54290 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74 le-handle struct
542a0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ure..*/.SQLITE_P
542b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
542c0 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 e3OsOpenMalloc(s
542d0 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f qlite3_vfs *, co
542e0 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 nst char *, sqli
542f0 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 te3_file **, int
54300 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ,int*);.SQLITE_P
54310 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54320 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71 e3OsCloseFree(sq
54330 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a lite3_file *);..
54340 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 #endif /* _SQLIT
54350 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a E_OS_H_ */../***
54360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
54370 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a of os.h ********
54380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
543a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
543b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
543c0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
543d0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
543e0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
543f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
54400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
54410 75 64 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74 ude mutex.h in t
54420 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
54430 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
54440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
54450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
54460 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a n file mutex.h *
54470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
544a0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 * 2007 August 28
544b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
544c0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
544d0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
544e0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
544f0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
54500 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
54510 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
54520 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
54530 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
54540 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
54550 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
54560 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
54570 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
54580 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
54590 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
545a0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
545b0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
545c0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
545d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
545e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
545f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
54610 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
54620 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d contains the com
54630 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f 72 20 61 mon header for a
54640 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 ll mutex impleme
54650 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 ntations..** The
54660 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 68 65 61 sqliteInt.h hea
54670 64 65 72 20 23 69 6e 63 6c 75 64 65 73 20 74 68 der #includes th
54680 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 is file so that
54690 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a it is available.
546a0 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65 ** to all source
546b0 20 66 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61 files. We brea
546c0 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65 k it out in an e
546d0 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68 ffort to keep th
546e0 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 74 65 72 e code.** better
546f0 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a organized..**.*
54700 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65 20 * NOTE: source
54710 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e 6f files should *no
54720 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 t* #include this
54730 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 69 72 header file dir
54740 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 ectly..** Source
54750 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 23 69 files should #i
54760 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c 69 74 nclude the sqlit
54770 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e 64 20 eInt.h file and
54780 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0a 2a 2a let that file.**
54790 20 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6f 6e include this on
547a0 65 20 69 6e 64 69 72 65 63 74 6c 79 2e 0a 2a 2a e indirectly..**
547b0 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 68 .** $Id: mutex.h
547c0 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 30 2f 30 ,v 1.9 2008/10/0
547d0 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45 7 15:25:48 drh E
547e0 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 xp $.*/.../*.**
547f0 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74 20 Figure out what
54800 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 version of the c
54810 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68 65 ode to use. The
54820 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a 0a choices are.**.
54830 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 ** SQLITE_MUTE
54840 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 20 4e X_OMIT N
54850 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 20 o mutex logic.
54860 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 2e 20 Not even stubs.
54870 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 The.**
54880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54890 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d 70 6c mutexes impl
548a0 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 emention cannot
548b0 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a 2a be overridden.**
548c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
548d0 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20 at
548e0 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a start-time..**.*
548f0 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 * SQLITE_MUTEX
54900 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 46 6f _NOOP Fo
54910 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 r single-threade
54920 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 d applications.
54930 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 No.**
54940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54950 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 mutual exclus
54960 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 2e ion is provided.
54970 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 20 But this.**
54980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54990 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c 65 6d implem
549a0 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 entation can be
549b0 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a 2a 2a overridden at.**
549c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
549d0 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 sta
549e0 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 rt-time..**.**
549f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 SQLITE_MUTEX_PT
54a00 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20 6d HREADS For m
54a10 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 ulti-threaded ap
54a20 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 6e plications on Un
54a30 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 ix..**.** SQLI
54a40 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20 TE_MUTEX_W32
54a50 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d For multi-
54a60 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 threaded applica
54a70 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0a tions on Win32..
54a80 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
54a90 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20 UTEX_OS2
54aa0 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 For multi-thre
54ab0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e aded application
54ac0 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 s on OS/2..*/.#i
54ad0 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 41 44 f !SQLITE_THREAD
54ae0 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SAFE.# define SQ
54af0 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a LITE_MUTEX_OMIT.
54b00 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
54b10 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
54b20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
54b30 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 MUTEX_NOOP).# i
54b40 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 f SQLITE_OS_UNIX
54b50 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c .# define SQL
54b60 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
54b70 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 DS.# elif SQLIT
54b80 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 65 E_OS_WIN.# de
54b90 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
54ba0 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 53 51 X_W32.# elif SQ
54bb0 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 LITE_OS_OS2.#
54bc0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
54bd0 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 65 UTEX_OS2.# else
54be0 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c .# define SQL
54bf0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 ITE_MUTEX_NOOP.#
54c00 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a endif.#endif..
54c10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 #ifdef SQLITE_MU
54c20 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 TEX_OMIT./*.** I
54c30 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f f this is a no-o
54c40 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e p implementation
54c50 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 72 , implement ever
54c60 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 6f 73 ything as macros
54c70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c ..*/.#define sql
54c80 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
54c90 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 65 33 (X) ((sqlite3
54ca0 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69 _mutex*)8).#defi
54cb0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
54cc0 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 6e 65 _free(X).#define
54cd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
54ce0 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e 65 20 nter(X).#define
54cf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
54d00 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 54 45 y(X) SQLITE
54d10 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 _OK.#define sqli
54d20 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
54d30 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 X).#define sqlit
54d40 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 29 e3_mutex_held(X)
54d50 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 73 1.#define s
54d60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
54d70 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 66 69 held(X) 1.#defi
54d80 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ne sqlite3MutexA
54d90 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 28 73 lloc(X) ((s
54da0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 qlite3_mutex*)8)
54db0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
54dc0 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 20 20 MutexInit()
54dd0 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 SQLITE_OK.#de
54de0 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 fine sqlite3Mute
54df0 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f 2a xEnd().#endif /*
54e00 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
54e10 4f 4d 49 54 5f 4d 55 54 45 58 29 20 2a 2f 0a 0a OMIT_MUTEX) */..
54e20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
54e30 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 2a End of mutex.h *
54e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
54e70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
54e80 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
54e90 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
54ea0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
54eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
54ec0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 ../*.** Each dat
54ed0 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62 65 abase file to be
54ee0 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65 accessed by the
54ef0 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69 6e system is an in
54f00 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 stance.** of the
54f10 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
54f20 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 ture. There are
54f30 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 normally two of
54f40 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 these structure
54f50 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 s.** in the sqli
54f60 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 te.aDb[] array.
54f70 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20 6d aDb[0] is the m
54f80 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
54f90 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 e and.** aDb[1]
54fa0 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 is the database
54fb0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f 6c file used to hol
54fc0 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c d temporary tabl
54fd0 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a es. Additional.
54fe0 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61 79 ** databases may
54ff0 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a 2f be attached..*/
55000 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20 63 .struct Db {. c
55010 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
55020 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
55030 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a his database */.
55040 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 Btree *pBt;
55050 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 2a /* The B*
55060 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 66 Tree structure f
55070 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 or this database
55080 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e file */. u8 in
55090 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 Trans;
550a0 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 62 /* 0: not writab
550b0 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 74 le. 1: Transact
550c0 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 6f ion. 2: Checkpo
550d0 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 65 int */. u8 safe
550e0 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f 2a ty_level; /*
550f0 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65 20 How aggressive
55100 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 61 20 at syncing data
55110 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 76 6f 69 to disk */. voi
55120 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 d *pAux;
55130 20 20 20 20 20 20 20 2f 2a 20 41 75 78 69 6c 69 /* Auxili
55140 61 72 79 20 64 61 74 61 2e 20 20 55 73 75 61 6c ary data. Usual
55150 6c 79 20 4e 55 4c 4c 20 2a 2f 0a 20 20 76 6f 69 ly NULL */. voi
55160 64 20 28 2a 78 46 72 65 65 41 75 78 29 28 76 6f d (*xFreeAux)(vo
55170 69 64 2a 29 3b 20 20 2f 2a 20 52 6f 75 74 69 6e id*); /* Routin
55180 65 20 74 6f 20 66 72 65 65 20 70 41 75 78 20 2a e to free pAux *
55190 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 /. Schema *pSch
551a0 65 6d 61 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e ema; /* Poin
551b0 74 65 72 20 74 6f 20 64 61 74 61 62 61 73 65 20 ter to database
551c0 73 63 68 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 schema (possibly
551d0 20 73 68 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a shared) */.};..
551e0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
551f0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
55200 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 74 6f ng structure sto
55210 72 65 73 20 61 20 64 61 74 61 62 61 73 65 20 73 res a database s
55220 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 chema..**.** If
55230 74 68 65 72 65 20 61 72 65 20 6e 6f 20 76 69 72 there are no vir
55240 74 75 61 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 tual tables conf
55250 69 67 75 72 65 64 20 69 6e 20 74 68 69 73 20 73 igured in this s
55260 63 68 65 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 chema, the.** Sc
55270 68 65 6d 61 2e 64 62 20 76 61 72 69 61 62 6c 65 hema.db variable
55280 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e is set to NULL.
55290 20 41 66 74 65 72 20 74 68 65 20 66 69 72 73 74 After the first
552a0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a virtual table.*
552b0 2a 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 * has been added
552c0 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 70 , it is set to p
552d0 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61 oint to the data
552e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
552f0 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 61 .** used to crea
55300 74 65 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f te the connectio
55310 6e 2e 20 4f 6e 63 65 20 61 20 76 69 72 74 75 61 n. Once a virtua
55320 6c 20 74 61 62 6c 65 20 68 61 73 20 62 65 65 6e l table has been
55330 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65 .** added to the
55340 20 53 63 68 65 6d 61 20 73 74 72 75 63 74 75 72 Schema structur
55350 65 20 61 6e 64 20 74 68 65 20 53 63 68 65 6d 61 e and the Schema
55360 2e 64 62 20 76 61 72 69 61 62 6c 65 20 70 6f 70 .db variable pop
55370 75 6c 61 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 ulated, .** only
55380 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63 that database c
55390 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 onnection may us
553a0 65 20 74 68 65 20 53 63 68 65 6d 61 20 74 6f 20 e the Schema to
553b0 70 72 65 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 prepare .** stat
553c0 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 ements..*/.struc
553d0 74 20 53 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 t Schema {. int
553e0 20 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 schema_cookie;
553f0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73 63 /* Database sc
55400 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d hema version num
55410 62 65 72 20 66 6f 72 20 74 68 69 73 20 66 69 6c ber for this fil
55420 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 e */. Hash tblH
55430 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ash; /* A
55440 6c 6c 20 74 61 62 6c 65 73 20 69 6e 64 65 78 65 ll tables indexe
55450 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 d by name */. H
55460 61 73 68 20 69 64 78 48 61 73 68 3b 20 20 20 20 ash idxHash;
55470 20 20 20 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 /* All (name
55480 64 29 20 69 6e 64 69 63 65 73 20 69 6e 64 65 78 d) indices index
55490 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 ed by name */.
554a0 48 61 73 68 20 74 72 69 67 48 61 73 68 3b 20 20 Hash trigHash;
554b0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 /* All trig
554c0 67 65 72 73 20 69 6e 64 65 78 65 64 20 62 79 20 gers indexed by
554d0 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 61 name */. Hash a
554e0 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 2f FKey; /
554f0 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 * Foreign keys i
55500 6e 64 65 78 65 64 20 62 79 20 74 6f 2d 74 61 62 ndexed by to-tab
55510 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 le */. Table *p
55520 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 SeqTab; /*
55530 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 The sqlite_seque
55540 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 nce table used b
55550 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 y AUTOINCREMENT
55560 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 */. u8 file_for
55570 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 mat; /* Sch
55580 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69 ema format versi
55590 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 on for this file
555a0 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 */. u8 enc;
555b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
555c0 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 xt encoding used
555d0 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73 by this databas
555e0 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 e */. u16 flags
555f0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 ; /* F
55600 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 lags associated
55610 77 69 74 68 20 74 68 69 73 20 73 63 68 65 6d 61 with this schema
55620 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 5f */. int cache_
55630 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 size; /* Nu
55640 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f mber of pages to
55650 20 75 73 65 20 69 6e 20 74 68 65 20 63 61 63 68 use in the cach
55660 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c e */.#ifndef SQL
55670 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
55680 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 TABLE. sqlite3
55690 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 *db; /*
556a0 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 "Owner" connecti
556b0 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 on. See comment
556c0 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a above */.#endif.
556d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 };../*.** These
556e0 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 macros can be us
556f0 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c ed to test, set,
55700 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 or clear bits i
55710 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 n the .** Db.fla
55720 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 gs field..*/.#de
55730 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70 65 72 fine DbHasProper
55740 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 28 ty(D,I,P) ((
55750 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 (D)->aDb[I].pSch
55760 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d ema->flags&(P))=
55770 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 44 62 =(P)).#define Db
55780 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 44 HasAnyProperty(D
55790 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e 61 44 ,I,P) (((D)->aD
557a0 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c b[I].pSchema->fl
557b0 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65 ags&(P))!=0).#de
557c0 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70 65 72 fine DbSetProper
557d0 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 44 ty(D,I,P) (D
557e0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d )->aDb[I].pSchem
557f0 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 a->flags|=(P).#d
55800 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50 72 6f efine DbClearPro
55810 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 28 perty(D,I,P) (
55820 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 D)->aDb[I].pSche
55830 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a ma->flags&=~(P).
55840 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
55850 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 44 42 alues for the DB
55860 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a .flags field..**
55870 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d .** The DB_Schem
55880 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20 aLoaded flag is
55890 73 65 74 20 61 66 74 65 72 20 74 68 65 20 64 61 set after the da
558a0 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 tabase schema ha
558b0 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 s been.** read i
558c0 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 nto internal has
558d0 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 h tables..**.**
558e0 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 DB_UnresetViews
558f0 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f means that one o
55900 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76 r more views hav
55910 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 e column names t
55920 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e hat.** have been
55930 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 filled out. If
55940 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e the schema chan
55950 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d ges, these colum
55960 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a n names might.**
55970 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 changes and so
55980 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 the view will ne
55990 65 64 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a ed to be reset..
559a0 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63 */.#define DB_Sc
559b0 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78 hemaLoaded 0x
559c0 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68 0001 /* The sch
559d0 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 ema has been loa
559e0 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 ded */.#define D
559f0 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 20 B_UnresetViews
55a00 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 0x0002 /* Som
55a10 65 20 76 69 65 77 73 20 68 61 76 65 20 64 65 66 e views have def
55a20 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 ined column name
55a30 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f s */.#define DB_
55a40 45 6d 70 74 79 20 20 20 20 20 20 20 20 20 20 20 Empty
55a50 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66 0x0004 /* The f
55a60 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65 ile is empty (le
55a70 6e 67 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f ngth 0 bytes) */
55a80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 ../*.** The numb
55a90 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 er of different
55aa0 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20 kinds of things
55ab0 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 that can be limi
55ac0 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 ted.** using the
55ad0 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 sqlite3_limit()
55ae0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 interface..*/.#
55af0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f define SQLITE_N_
55b00 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 LIMIT (SQLITE_LI
55b10 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
55b20 42 45 52 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f BER+1)../*.** Lo
55b30 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 okaside malloc i
55b40 73 20 61 20 73 65 74 20 6f 66 20 66 69 78 65 64 s a set of fixed
55b50 2d 73 69 7a 65 20 62 75 66 66 65 72 73 20 74 68 -size buffers th
55b60 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a at can be used.*
55b70 2a 20 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61 * to satisfy sma
55b80 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d ll transient mem
55b90 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
55ba0 65 71 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 equests for obje
55bb0 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 cts.** associate
55bc0 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 d with a particu
55bd0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e lar database con
55be0 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 nection. The us
55bf0 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 e of.** lookasid
55c00 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 e malloc provide
55c10 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 s a significant
55c20 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 performance enha
55c30 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 ncement.** (appr
55c40 6f 78 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 ox 10%) by avoid
55c50 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c ing numerous mal
55c60 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 loc/free request
55c70 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a s while parsing.
55c80 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ** SQL statement
55c90 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f s..**.** The Loo
55ca0 6b 61 73 69 64 65 20 73 74 72 75 63 74 75 72 65 kaside structure
55cb0 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 holds configura
55cc0 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tion information
55cd0 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f about the.** lo
55ce0 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 okaside malloc s
55cf0 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 ubsystem. Each
55d00 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 available memory
55d10 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a allocation in.*
55d20 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 * the lookaside
55d30 73 75 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f subsystem is sto
55d40 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 red on a linked
55d50 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 list of Lookasid
55d60 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 eSlot.** objects
55d70 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 ..**.** Lookasid
55d80 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 e allocations ar
55d90 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 e only allowed f
55da0 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 or objects that
55db0 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a are associated.*
55dc0 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 * with a particu
55dd0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e lar database con
55de0 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c nection. Hence,
55df0 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 schema informat
55e00 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 ion cannot.** be
55e10 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 stored in looka
55e20 73 69 64 65 20 62 65 63 61 75 73 65 20 69 6e 20 side because in
55e30 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 shared cache mod
55e40 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 e the schema inf
55e50 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 ormation.** is s
55e60 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c hared by multipl
55e70 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
55e80 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f ctions. Therefo
55e90 72 65 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e re, while parsin
55ea0 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f g.** schema info
55eb0 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f rmation, the Loo
55ec0 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 kaside.bEnabled
55ed0 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 flag is cleared
55ee0 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 so that.** looka
55ef0 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 side allocations
55f00 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f are not used to
55f10 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 construct the s
55f20 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a chema objects..*
55f30 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 /.struct Lookasi
55f40 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 de {. u16 sz;
55f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
55f60 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62 * Size of each b
55f70 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a uffer in bytes *
55f80 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b /. u8 bEnabled;
55f90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
55fa0 61 6c 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 alse to disable
55fb0 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c new lookaside al
55fc0 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 locations */. u
55fd0 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 8 bMalloced;
55fe0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
55ff0 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 f pStart obtaine
56000 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
56010 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 alloc() */. int
56020 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 nOut;
56030 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
56040 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65 6e f buffers curren
56050 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 tly checked out
56060 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 */. int mxOut;
56070 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
56080 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 Highwater mark f
56090 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f or nOut */. Loo
560a0 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 kasideSlot *pFre
560b0 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 e; /* List of
560c0 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 65 72 available buffer
560d0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 s */. void *pSt
560e0 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f art; /
560f0 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
56100 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 available memory
56110 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 space */. void
56120 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 *pEnd;
56130 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
56140 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76 e past end of av
56150 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f ailable space */
56160 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 .};.struct Looka
56170 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f sideSlot {. Loo
56180 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78 kasideSlot *pNex
56190 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 75 t; /* Next bu
561a0 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 ffer in the list
561b0 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 72 73 of free buffers
561c0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 */.};../*.** A
561d0 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 hash table for f
561e0 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 unction definiti
561f0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20 ons..**.** Hash
56200 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 74 72 each FuncDef str
56210 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20 ucture into one
56220 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 48 61 of the FuncDefHa
56230 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a sh.a[] slots..**
56240 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20 Collisions are
56250 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 2e 70 on the FuncDef.p
56260 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 Hash chain..*/.s
56270 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 truct FuncDefHas
56280 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 h {. FuncDef *a
56290 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48 [23]; /* H
562a0 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 ash table for fu
562b0 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f nctions */.};../
562c0 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 *.** Each databa
562d0 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 se is an instanc
562e0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
562f0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
56300 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c .** The sqlite.l
56310 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73 astRowid records
56320 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 the last insert
56330 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64 rowid generated
56340 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 by an.** insert
56350 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 statement. Ins
56360 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f erts on views do
56370 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73 20 not affect its
56380 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 value. Each.**
56390 74 72 69 67 67 65 72 20 68 61 73 20 69 74 73 20 trigger has its
563a0 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 own context, so
563b0 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63 that lastRowid c
563c0 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 69 6e an be updated in
563d0 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 side.** triggers
563e0 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20 as usual. The
563f0 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77 previous value w
56400 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 ill be restored
56410 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 72 once the trigger
56420 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e .** exits. Upon
56430 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f entering a befo
56440 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 re or instead of
56450 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f trigger, lastRo
56460 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e wid is no.** lon
56470 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 65 72 ger (since after
56480 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 version 2.8.12)
56490 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a reset to -1..**
564a0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e .** The sqlite.n
564b0 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 Change does not
564c0 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69 count changes wi
564d0 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e thin triggers an
564e0 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f d keeps no.** co
564f0 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 72 65 ntext. It is re
56500 73 65 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 set at start of
56510 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a sqlite3_exec..**
56520 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 The sqlite.lsCh
56530 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73 20 ange represents
56540 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
56550 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 anges made by th
56560 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 e last.** insert
56570 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c , update, or del
56580 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ete statement.
56590 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 It remains const
565a0 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 ant throughout t
565b0 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 he.** length of
565c0 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 a statement and
565d0 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 64 20 is then updated
565e0 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e by OP_SetCounts.
565f0 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 It keeps a.**
56600 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75 context stack ju
56610 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 st like lastRowi
56620 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f d so that the co
56630 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a unt of changes.*
56640 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 * within a trigg
56650 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f er is not seen o
56660 75 74 73 69 64 65 20 74 68 65 20 74 72 69 67 67 utside the trigg
56670 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20 er. Changes to
56680 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 views do not.**
56690 61 66 66 65 63 74 20 74 68 65 20 76 61 6c 75 65 affect the value
566a0 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a of lsChange..**
566b0 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68 The sqlite.csCh
566c0 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b ange keeps track
566d0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f of the number o
566e0 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65 f current change
566f0 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 s (since.** the
56700 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20 last statement)
56710 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 75 and is used to u
56720 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 pdate sqlite_lsC
56730 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 hange..**.** The
56740 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 member variable
56750 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 s sqlite.errCode
56760 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 , sqlite.zErrMsg
56770 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 and sqlite.zErr
56780 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 Msg16.** store t
56790 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 he most recent e
567a0 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 rror code and, i
567b0 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 f applicable, st
567c0 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 ring. The.** int
567d0 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 ernal function s
567e0 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69 73 qlite3Error() is
567f0 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 65 used to set the
56800 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 se variables.**
56810 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f consistently..*/
56820 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 .struct sqlite3
56830 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
56840 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 *pVfs;
56850 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 /* OS Interfac
56860 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 e */. int nDb;
56870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
56880 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
56890 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65 f backends curre
568a0 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 ntly in use */.
568b0 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20 Db *aDb;
568c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
568d0 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a * All backends *
568e0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 /. int flags;
568f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
56900 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f /* Miscellaneo
56910 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 us flags. See be
56920 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 low */. int ope
56930 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 nFlags;
56940 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
56950 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
56960 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
56970 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 . int errCode;
56980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
56990 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
569a0 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 error code (SQLI
569b0 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 TE_*) */. int e
569c0 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 rrMask;
569d0 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65 /* & re
569e0 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20 sult codes with
569f0 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74 75 this before retu
56a00 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 rning */. u8 au
56a10 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 toCommit;
56a20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
56a30 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 auto-commit flag
56a40 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 . */. u8 temp_s
56a50 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 tore;
56a60 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 /* 1: file
56a70 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 2: memory 0: def
56a80 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c ault */. u8 mal
56a90 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 locFailed;
56aa0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
56ab0 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 if we have seen
56ac0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 a malloc failure
56ad0 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 */. u8 dfltLoc
56ae0 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 kMode;
56af0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c /* Default l
56b00 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 ocking-mode for
56b10 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a attached dbs */.
56b20 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c u8 dfltJournal
56b30 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 Mode;
56b40 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e /* Default journ
56b50 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 al mode for atta
56b60 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 ched dbs */. si
56b70 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41 75 gned char nextAu
56b80 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 tovac; /* A
56b90 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61 utovac setting a
56ba0 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e fter VACUUM if >
56bb0 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 =0 */. int next
56bc0 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20 Pagesize;
56bd0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a /* Pagesiz
56be0 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 e after VACUUM i
56bf0 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 f >0 */. int nT
56c00 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 able;
56c10 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
56c20 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 r of tables in t
56c30 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
56c40 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 CollSeq *pDfltC
56c50 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f oll; /
56c60 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f * The default co
56c70 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
56c80 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 (BINARY) */. i
56c90 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 64 lastRowid;
56ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
56cb0 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 ROWID of most re
56cc0 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65 cent insert (see
56cd0 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69 36 34 above) */. i64
56ce0 20 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 3b 20 priorNewRowid;
56cf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
56d00 73 74 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65 st randomly gene
56d10 72 61 74 65 64 20 52 4f 57 49 44 20 2a 2f 0a 20 rated ROWID */.
56d20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 u32 magic;
56d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
56d40 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 * Magic number f
56d50 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72 or detect librar
56d60 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e y misuse */. in
56d70 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 t nChange;
56d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
56d90 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
56da0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
56db0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 () */. int nTot
56dc0 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 alChange;
56dd0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 /* Value r
56de0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
56df0 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
56e00 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f () */. sqlite3_
56e10 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 mutex *mutex;
56e20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 /* Connect
56e30 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 ion mutex */. i
56e40 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 nt aLimit[SQLITE
56e50 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 _N_LIMIT]; /*
56e60 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 Limits */. stru
56e70 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e ct sqlite3InitIn
56e80 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 fo { /* Inf
56e90 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75 ormation used du
56ea0 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
56eb0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ion */. int i
56ec0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
56ed0 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 /* When b
56ee0 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69 ack is being ini
56ef0 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 tialized */.
56f00 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 int newTnum;
56f10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
56f20 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ootpage of table
56f30 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a being initializ
56f40 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 ed */. u8 bus
56f50 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
56f60 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 /* TRUE if
56f70 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 currently initi
56f80 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d 20 69 alizing */. } i
56f90 6e 69 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 nit;. int nExte
56fa0 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 nsion;
56fb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
56fc0 66 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 f loaded extensi
56fd0 6f 6e 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a ons */. void **
56fe0 61 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 aExtension;
56ff0 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 /* Array
57000 6f 66 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 of shared librar
57010 79 20 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 y handles */. s
57020 74 72 75 63 74 20 56 64 62 65 20 2a 70 56 64 62 truct Vdbe *pVdb
57030 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
57040 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 76 List of active v
57050 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 irtual machines
57060 2a 2f 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56 */. int activeV
57070 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 dbeCnt;
57080 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
57090 56 44 42 45 73 20 63 75 72 72 65 6e 74 6c 79 20 VDBEs currently
570a0 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 executing */. i
570b0 6e 74 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b nt writeVdbeCnt;
570c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
570d0 4e 75 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65 Number of active
570e0 20 56 44 42 45 73 20 74 68 61 74 20 61 72 65 20 VDBEs that are
570f0 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 writing */. voi
57100 64 20 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 d (*xTrace)(void
57110 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 *,const char*);
57120 20 20 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20 /* Trace
57130 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f function */. vo
57140 69 64 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20 id *pTraceArg;
57150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57160 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
57170 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61 63 65 ent to the trace
57180 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
57190 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 oid (*xProfile)(
571a0 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 void*,const char
571b0 2a 2c 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 *,u64); /* Prof
571c0 69 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a iling function *
571d0 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 /. void *pProfi
571e0 6c 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 leArg;
571f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
57200 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f Argument to pro
57210 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f file function */
57220 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 . void *pCommit
57230 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Arg;
57240 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
57250 20 74 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 to xCommitCallb
57260 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e ack() */ . in
57270 74 20 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 t (*xCommitCallb
57280 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 ack)(void*);
57290 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 /* Invoked at ev
572a0 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 ery commit. */.
572b0 20 76 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b void *pRollback
572c0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Arg;
572d0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
572e0 6f 20 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 o xRollbackCallb
572f0 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f ack() */ . vo
57300 69 64 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 id (*xRollbackCa
57310 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 llback)(void*);
57320 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 /* Invoked at ev
57330 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 ery commit. */.
57340 20 76 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72 void *pUpdateAr
57350 67 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64 g;. void (*xUpd
57360 61 74 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 ateCallback)(voi
57370 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 d*,int, const ch
57380 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
57390 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 sqlite_int64);.
573a0 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 void(*xCollNeed
573b0 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 ed)(void*,sqlite
573c0 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 3*,int eTextRep,
573d0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 const char*);.
573e0 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 void(*xCollNeede
573f0 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 d16)(void*,sqlit
57400 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 e3*,int eTextRep
57410 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 ,const void*);.
57420 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 void *pCollNeed
57430 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 edArg;. sqlite3
57440 5f 76 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 _value *pErr;
57450 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 /* Most r
57460 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 ecent error mess
57470 61 67 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a age */. char *z
57480 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 ErrMsg;
57490 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 /* Most r
574a0 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 ecent error mess
574b0 61 67 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 age (UTF-8 encod
574c0 65 64 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ed) */. char *z
574d0 45 72 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20 ErrMsg16;
574e0 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 /* Most r
574f0 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 ecent error mess
57500 61 67 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f age (UTF-16 enco
57510 64 65 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 ded) */. union
57520 7b 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 {. volatile i
57530 6e 74 20 69 73 49 6e 74 65 72 72 75 70 74 65 64 nt isInterrupted
57540 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c ; /* True if sql
57550 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68 ite3_interrupt h
57560 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a as been called *
57570 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 /. double not
57580 55 73 65 64 31 3b 20 20 20 20 20 20 20 20 20 20 Used1;
57590 20 20 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 /* Spacer */.
575a0 20 7d 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 } u1;. Lookasi
575b0 64 65 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 de lookaside;
575c0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 /* Lookas
575d0 69 64 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 ide malloc confi
575e0 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e guration */.#ifn
575f0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
57600 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 AUTHORIZATION.
57610 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 int (*xAuth)(voi
57620 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 d*,int,const cha
57630 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 r*,const char*,c
57640 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
57650 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 char*);.
57660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57670 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65 /* Acce
57680 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e ss authorization
57690 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
576a0 6f 69 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20 oid *pAuthArg;
576b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
576c0 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 1st argument to
576d0 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68 20 the access auth
576e0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 function */.#end
576f0 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 if.#ifndef SQLIT
57700 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f E_OMIT_PROGRESS_
57710 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 CALLBACK. int (
57720 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 *xProgress)(void
57730 20 2a 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 *); /* The
57740 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
57750 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 k */. void *pPr
57760 6f 67 72 65 73 73 41 72 67 3b 20 20 20 20 20 20 ogressArg;
57770 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
57780 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 to the progress
57790 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 callback */. i
577a0 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b nt nProgressOps;
577b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
577c0 4e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 Number of opcode
577d0 73 20 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63 s for progress c
577e0 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 allback */.#endi
577f0 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
57800 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
57810 4c 45 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c LE. Hash aModul
57820 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
57830 20 20 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 /* populated
57840 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 by sqlite3_creat
57850 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 e_module() */.
57860 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 Table *pVTab;
57870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
57880 20 76 74 61 62 20 77 69 74 68 20 61 63 74 69 76 vtab with activ
57890 65 20 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 e Connect/Create
578a0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 71 6c method */. sql
578b0 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 ite3_vtab **aVTr
578c0 61 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 56 69 ans; /* Vi
578d0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 rtual tables wit
578e0 68 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 h open transacti
578f0 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 ons */. int nVT
57900 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 rans;
57910 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 /* Alloca
57920 74 65 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 ted size of aVTr
57930 61 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ans */.#endif.
57940 46 75 6e 63 44 65 66 48 61 73 68 20 61 46 75 6e FuncDefHash aFun
57950 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a c; /*
57960 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 63 Hash table of c
57970 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69 onnection functi
57980 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20 61 43 ons */. Hash aC
57990 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 20 20 ollSeq;
579a0 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 6f /* All co
579b0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
579c0 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c s */. BusyHandl
579d0 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72 3b 20 er busyHandler;
579e0 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63 61 6c /* Busy cal
579f0 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 62 lback */. int b
57a00 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 20 usyTimeout;
57a10 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 73 79 /* Busy
57a20 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 74 handler timeout
57a30 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20 44 , in msec */. D
57a40 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b 20 b aDbStatic[2];
57a50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
57a60 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f 72 Static space for
57a70 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20 62 the 2 default b
57a80 61 63 6b 65 6e 64 73 20 2a 2f 0a 23 69 66 64 65 ackends */.#ifde
57a90 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 73 f SQLITE_SSE. s
57aa0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 65 qlite3_stmt *pFe
57ab0 74 63 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 tch; /*
57ac0 55 73 65 64 20 62 79 20 53 53 45 20 74 6f 20 66 Used by SSE to f
57ad0 65 74 63 68 20 73 74 6f 72 65 64 20 73 74 61 74 etch stored stat
57ae0 65 6d 65 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 ements */.#endif
57af0 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 . Savepoint *pS
57b00 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 avepoint;
57b10 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 /* List of acti
57b20 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f ve savepoints */
57b30 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e . int nSavepoin
57b40 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
57b50 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f /* Number of no
57b60 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 n-transaction sa
57b70 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e vepoints */. in
57b80 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 t nStatement;
57b90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
57ba0 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 umber of nested
57bb0 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 statement-transa
57bc0 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20 ctions */. u8
57bd0 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 isTransactionSav
57be0 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 epoint; /* Tr
57bf0 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d ue if the outerm
57c00 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73 ost savepoint is
57c10 20 61 20 54 53 20 2a 2f 0a 0a 23 69 66 64 65 66 a TS */..#ifdef
57c20 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 SQLITE_ENABLE_U
57c30 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f NLOCK_NOTIFY. /
57c40 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
57c50 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c variables are al
57c60 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 l protected by t
57c70 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 he STATIC_MASTER
57c80 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f . ** mutex, no
57c90 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74 t by sqlite3.mut
57ca0 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 73 65 ex. They are use
57cb0 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74 d by code in not
57cc0 69 66 79 2e 63 2e 20 0a 20 20 2a 2f 0a 20 20 73 ify.c. . */. s
57cd0 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e qlite3 *pBlockin
57ce0 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 gConnection; /*
57cf0 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 Connection that
57d00 63 61 75 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f caused SQLITE_LO
57d10 43 4b 45 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 CKED */. sqlite
57d20 33 20 2a 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 3 *pUnlockConnec
57d30 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 tion;
57d40 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f /* Connection to
57d50 20 77 61 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 watch for unloc
57d60 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e k */. void *pUn
57d70 6c 6f 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 lockArg;
57d80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
57d90 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c Argument to xUnl
57da0 6f 63 6b 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 ockNotify */. v
57db0 6f 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 oid (*xUnlockNot
57dc0 69 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e ify)(void **, in
57dd0 74 29 3b 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e t); /* Unlock n
57de0 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a otify callback *
57df0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 /. sqlite3 *pNe
57e00 78 74 42 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 xtBlocked;
57e10 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 /* Next in lis
57e20 74 20 6f 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 t of all blocked
57e30 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a connections */.
57e40 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
57e50 20 41 20 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 A macro to disc
57e60 6f 76 65 72 20 74 68 65 20 65 6e 63 6f 64 69 6e over the encodin
57e70 67 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e g of a database.
57e80 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 .*/.#define ENC(
57e90 64 62 29 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 db) ((db)->aDb[0
57ea0 5d 2e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a ].pSchema->enc).
57eb0 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 ./*.** Possible
57ec0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 values for the s
57ed0 71 6c 69 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 qlite.flags and
57ee0 6f 72 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c or Db.flags fiel
57ef0 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c ds..**.** On sql
57f00 69 74 65 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 ite.flags, the S
57f10 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 QLITE_InTrans va
57f20 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 77 lue means that w
57f30 65 20 68 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 e have.** execut
57f40 65 64 20 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 ed a BEGIN. On
57f50 44 62 2e 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 Db.flags, SQLITE
57f60 5f 49 6e 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 _InTrans means a
57f70 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 statement.** tr
57f80 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
57f90 69 76 65 20 6f 6e 20 74 68 61 74 20 70 61 72 74 ive on that part
57fa0 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 icular database
57fb0 66 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 file..*/.#define
57fc0 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 SQLITE_VdbeTrac
57fd0 65 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30 e 0x0000000
57fe0 31 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 1 /* True to tr
57ff0 61 63 65 20 56 44 42 45 20 65 78 65 63 75 74 69 ace VDBE executi
58000 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 on */.#define SQ
58010 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 LITE_InTrans
58020 20 20 20 20 30 78 30 30 30 30 30 30 30 38 20 20 0x00000008
58030 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 /* True if in a
58040 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 transaction */.#
58050 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e define SQLITE_In
58060 74 65 72 6e 43 68 61 6e 67 65 73 20 20 30 78 30 ternChanges 0x0
58070 30 30 30 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 0000010 /* Unco
58080 6d 6d 69 74 74 65 64 20 48 61 73 68 20 74 61 62 mmitted Hash tab
58090 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 le changes */.#d
580a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c efine SQLITE_Ful
580b0 6c 43 6f 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 lColNames 0x00
580c0 30 30 30 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 000020 /* Show
580d0 66 75 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 full column name
580e0 73 20 6f 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 s on SELECT */.#
580f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 define SQLITE_Sh
58100 6f 72 74 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 ortColNames 0x0
58110 30 30 30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 0000040 /* Show
58120 20 73 68 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e short columns n
58130 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ames */.#define
58140 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 SQLITE_CountRows
58150 20 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 0x00000080
58160 20 20 2f 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 /* Count rows
58170 63 68 61 6e 67 65 64 20 62 79 20 49 4e 53 45 52 changed by INSER
58180 54 2c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 T, */.
58190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
581a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
581b0 2f 2a 20 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 /* DELETE, or
581c0 55 50 44 41 54 45 20 61 6e 64 20 72 65 74 75 72 UPDATE and retur
581d0 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 n */.
581e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
581f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
58200 2a 20 20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 * the count us
58210 69 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 ing a callback.
58220 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
58230 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 E_NullCallback
58240 20 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 0x00000100 /*
58250 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 Invoke the callb
58260 61 63 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20 ack once if the
58270 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 */.
58280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58290 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
582a0 20 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 result set is
582b0 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 empty */.#define
582c0 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 SQLITE_SqlTrace
582d0 20 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30 0x0000020
582e0 30 20 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 0 /* Debug prin
582f0 74 20 53 51 4c 20 61 73 20 69 74 20 65 78 65 63 t SQL as it exec
58300 75 74 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 utes */.#define
58310 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 SQLITE_VdbeListi
58320 6e 67 20 20 20 20 30 78 30 30 30 30 30 34 30 30 ng 0x00000400
58330 20 20 2f 2a 20 44 65 62 75 67 20 6c 69 73 74 69 /* Debug listi
58340 6e 67 73 20 6f 66 20 56 44 42 45 20 70 72 6f 67 ngs of VDBE prog
58350 72 61 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 rams */.#define
58360 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 SQLITE_WriteSche
58370 6d 61 20 20 20 20 30 78 30 30 30 30 30 38 30 30 ma 0x00000800
58380 20 20 2f 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 /* OK to updat
58390 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 e SQLITE_MASTER
583a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
583b0 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 E_NoReadlock
583c0 20 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 0x00001000 /*
583d0 52 65 61 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d Readlocks are om
583e0 69 74 74 65 64 20 77 68 65 6e 20 0a 20 20 20 20 itted when .
583f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58410 20 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 69 ** accessi
58420 6e 67 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 ng read-only dat
58430 61 62 61 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e abases */.#defin
58440 65 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 e SQLITE_IgnoreC
58450 68 65 63 6b 73 20 20 20 30 78 30 30 30 30 32 30 hecks 0x000020
58460 30 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 00 /* Do not en
58470 66 6f 72 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 force check cons
58480 74 72 61 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 traints */.#defi
58490 6e 65 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e ne SQLITE_ReadUn
584a0 63 6f 6d 6d 69 74 74 65 64 20 30 78 30 30 30 30 committed 0x0000
584b0 34 30 30 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 4000 /* For shar
584c0 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f ed-cache mode */
584d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
584e0 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 20 20 30 LegacyFileFmt 0
584f0 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 43 72 x00008000 /* Cr
58500 65 61 74 65 20 6e 65 77 20 64 61 74 61 62 61 73 eate new databas
58510 65 73 20 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a es in format 1 *
58520 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
58530 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 _FullFSync
58540 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 55 0x00010000 /* U
58550 73 65 20 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e se full fsync on
58560 20 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a the backend */.
58570 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
58580 6f 61 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 oadExtension 0x
58590 30 30 30 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 00020000 /* Ena
585a0 62 6c 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 ble load_extensi
585b0 6f 6e 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 on */..#define S
585c0 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f QLITE_RecoveryMo
585d0 64 65 20 20 20 30 78 30 30 30 34 30 30 30 30 20 de 0x00040000
585e0 20 2f 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d /* Ignore schem
585f0 61 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 a errors */.#def
58600 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 61 72 65 ine SQLITE_Share
58610 64 43 61 63 68 65 20 20 20 20 30 78 30 30 30 38 dCache 0x0008
58620 30 30 30 30 20 20 2f 2a 20 43 61 63 68 65 20 73 0000 /* Cache s
58630 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 haring is enable
58640 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
58650 49 54 45 5f 56 74 61 62 20 20 20 20 20 20 20 20 ITE_Vtab
58660 20 20 20 30 78 30 30 31 30 30 30 30 30 20 20 2f 0x00100000 /
58670 2a 20 54 68 65 72 65 20 65 78 69 73 74 73 20 61 * There exists a
58680 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a virtual table *
58690 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
586a0 5f 43 6f 6d 6d 69 74 42 75 73 79 20 20 20 20 20 _CommitBusy
586b0 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 49 0x00200000 /* I
586c0 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 n the process of
586d0 20 63 6f 6d 6d 69 74 74 69 6e 67 20 2a 2f 0a 23 committing */.#
586e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 define SQLITE_Re
586f0 76 65 72 73 65 4f 72 64 65 72 20 20 20 30 78 30 verseOrder 0x0
58700 30 34 30 30 30 30 30 20 20 2f 2a 20 52 65 76 65 0400000 /* Reve
58710 72 73 65 20 75 6e 6f 72 64 65 72 65 64 20 53 45 rse unordered SE
58720 4c 45 43 54 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 LECTs */../*.**
58730 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 Possible values
58740 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d for the sqlite.m
58750 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 agic field..** T
58760 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f he numbers are o
58770 62 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f btained at rando
58780 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 m and have no sp
58790 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f ecial meaning, o
587a0 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69 ther.** than bei
587b0 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d ng distinct from
587c0 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f one another..*/
587d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
587e0 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30 MAGIC_OPEN 0
587f0 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61 xa029a697 /* Da
58800 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a tabase is open *
58810 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
58820 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20 _MAGIC_CLOSED
58830 30 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44 0x9f3c2d33 /* D
58840 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 atabase is close
58850 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
58860 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 20 ITE_MAGIC_SICK
58870 20 20 20 30 78 34 62 37 37 31 32 39 30 20 20 2f 0x4b771290 /
58880 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 77 61 69 * Error and awai
58890 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 ting close */.#d
588a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 efine SQLITE_MAG
588b0 49 43 5f 42 55 53 59 20 20 20 20 20 30 78 66 30 IC_BUSY 0xf0
588c0 33 62 37 39 30 36 20 20 2f 2a 20 44 61 74 61 62 3b7906 /* Datab
588d0 61 73 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e ase currently in
588e0 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 use */.#define
588f0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 SQLITE_MAGIC_ERR
58900 4f 52 20 20 20 20 30 78 62 35 33 35 37 39 33 30 OR 0xb5357930
58910 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4d /* An SQLITE_M
58920 49 53 55 53 45 20 65 72 72 6f 72 20 6f 63 63 75 ISUSE error occu
58930 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 rred */../*.** E
58940 61 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ach SQL function
58950 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 is defined by a
58960 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
58970 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 e following.** s
58980 74 72 75 63 74 75 72 65 2e 20 20 41 20 70 6f 69 tructure. A poi
58990 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 nter to this str
589a0 75 63 74 75 72 65 20 69 73 20 73 74 6f 72 65 64 ucture is stored
589b0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 in the sqlite.a
589c0 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 62 Func.** hash tab
589d0 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 70 le. When multip
589e0 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 le functions hav
589f0 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c e the same name,
58a00 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a the hash table.
58a10 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6c ** points to a l
58a20 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 inked list of th
58a30 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a ese structures..
58a40 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 */.struct FuncDe
58a50 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b 20 f {. i16 nArg;
58a60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
58a70 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
58a80 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c s. -1 means unl
58a90 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 imited */. u8 i
58aa0 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20 PrefEnc;
58ab0 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 /* Preferred te
58ac0 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c xt encoding (SQL
58ad0 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20 ITE_UTF8, 16LE,
58ae0 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 6c 16BE) */. u8 fl
58af0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
58b00 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 /* Some combinat
58b10 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 55 ion of SQLITE_FU
58b20 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a NC_* */. void *
58b30 70 55 73 65 72 44 61 74 61 3b 20 20 20 20 20 2f pUserData; /
58b40 2a 20 55 73 65 72 20 64 61 74 61 20 70 61 72 61 * User data para
58b50 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 44 meter */. FuncD
58b60 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 ef *pNext;
58b70 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69 6f 6e /* Next function
58b80 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 20 with same name
58b90 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e */. void (*xFun
58ba0 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 c)(sqlite3_conte
58bb0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
58bc0 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 value**); /* Reg
58bd0 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ular function */
58be0 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 . void (*xStep)
58bf0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
58c00 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
58c10 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 65 lue**); /* Aggre
58c20 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 76 gate step */. v
58c30 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 29 oid (*xFinalize)
58c40 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
58c50 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 *);
58c60 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 /* Aggregate
58c70 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 finalizer */. c
58c80 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
58c90 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 /* SQL name
58ca0 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e of the function.
58cb0 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 */. FuncDef *p
58cc0 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e 65 Hash; /* Ne
58cd0 78 74 20 77 69 74 68 20 61 20 64 69 66 66 65 72 xt with a differ
58ce0 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68 65 ent name but the
58cf0 20 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d 3b same hash */.};
58d00 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 ../*.** Possible
58d10 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63 values for Func
58d20 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 Def.flags.*/.#de
58d30 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 fine SQLITE_FUNC
58d40 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20 2f _LIKE 0x01 /
58d50 2a 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72 20 * Candidate for
58d60 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a the LIKE optimiz
58d70 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 ation */.#define
58d80 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 SQLITE_FUNC_CAS
58d90 45 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43 61 E 0x02 /* Ca
58da0 73 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 4b se-sensitive LIK
58db0 45 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e 20 E-type function
58dc0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
58dd0 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 20 E_FUNC_EPHEM
58de0 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 65 72 61 0x04 /* Ephemera
58df0 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68 20 l. Delete with
58e00 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 VDBE */.#define
58e10 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 SQLITE_FUNC_NEED
58e20 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 6c COLL 0x08 /* sql
58e30 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 ite3GetFuncCollS
58e40 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63 61 eq() might be ca
58e50 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 lled */.#define
58e60 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49 56 SQLITE_FUNC_PRIV
58e70 41 54 45 20 20 30 78 31 30 20 2f 2a 20 41 6c 6c ATE 0x10 /* All
58e80 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 owed for interna
58e90 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 l use only */.#d
58ea0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
58eb0 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 32 30 20 C_COUNT 0x20
58ec0 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 6e /* Built-in coun
58ed0 74 28 2a 29 20 61 67 67 72 65 67 61 74 65 20 2a t(*) aggregate *
58ee0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c /../*.** The fol
58ef0 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 6d 61 63 lowing three mac
58f00 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e 28 29 2c ros, FUNCTION(),
58f10 20 4c 49 4b 45 46 55 4e 43 28 29 20 61 6e 64 20 LIKEFUNC() and
58f20 41 47 47 52 45 47 41 54 45 28 29 20 61 72 65 0a AGGREGATE() are.
58f30 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 ** used to creat
58f40 65 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 e the initialize
58f50 72 73 20 66 6f 72 20 74 68 65 20 46 75 6e 63 44 rs for the FuncD
58f60 65 66 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a ef structures..*
58f70 2a 0a 2a 2a 20 20 20 46 55 4e 43 54 49 4f 4e 28 *.** FUNCTION(
58f80 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 zName, nArg, iAr
58f90 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 0a 2a g, bNC, xFunc).*
58fa0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 * Used to cr
58fb0 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 eate a scalar fu
58fc0 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f nction definitio
58fd0 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 n of a function
58fe0 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 69 6d zName .** im
58ff0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 43 20 66 plemented by C f
59000 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63 20 74 68 unction xFunc th
59010 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 at accepts nArg
59020 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 0a 2a arguments. The.*
59030 2a 20 20 20 20 20 76 61 6c 75 65 20 70 61 73 73 * value pass
59040 65 64 20 61 73 20 69 41 72 67 20 69 73 20 63 61 ed as iArg is ca
59050 73 74 20 74 6f 20 61 20 28 76 6f 69 64 2a 29 20 st to a (void*)
59060 61 6e 64 20 6d 61 64 65 20 61 76 61 69 6c 61 62 and made availab
59070 6c 65 0a 2a 2a 20 20 20 20 20 61 73 20 74 68 65 le.** as the
59080 20 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 user-data (sqli
59090 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 te3_user_data())
590a0 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f for the functio
590b0 6e 2e 20 49 66 20 0a 2a 2a 20 20 20 20 20 61 72 n. If .** ar
590c0 67 75 6d 65 6e 74 20 62 4e 43 20 69 73 20 74 72 gument bNC is tr
590d0 75 65 2c 20 74 68 65 6e 20 74 68 65 20 46 75 6e ue, then the Fun
590e0 63 44 65 66 2e 6e 65 65 64 43 6f 6c 6c 61 74 65 cDef.needCollate
590f0 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a flag is set..**
59100 0a 2a 2a 20 20 20 41 47 47 52 45 47 41 54 45 28 .** AGGREGATE(
59110 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 zName, nArg, iAr
59120 67 2c 20 62 4e 43 2c 20 78 53 74 65 70 2c 20 78 g, bNC, xStep, x
59130 46 69 6e 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73 Final).** Us
59140 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 ed to create an
59150 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
59160 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d on definition im
59170 70 6c 65 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 plemented by.**
59180 20 20 20 20 74 68 65 20 43 20 66 75 6e 63 74 69 the C functi
59190 6f 6e 73 20 78 53 74 65 70 20 61 6e 64 20 78 46 ons xStep and xF
591a0 69 6e 61 6c 2e 20 54 68 65 20 66 69 72 73 74 20 inal. The first
591b0 66 6f 75 72 20 70 61 72 61 6d 65 74 65 72 73 0a four parameters.
591c0 2a 2a 20 20 20 20 20 61 72 65 20 69 6e 74 65 72 ** are inter
591d0 70 72 65 74 65 64 20 69 6e 20 74 68 65 20 73 61 preted in the sa
591e0 6d 65 20 77 61 79 20 61 73 20 74 68 65 20 66 69 me way as the fi
591f0 72 73 74 20 34 20 70 61 72 61 6d 65 74 65 72 73 rst 4 parameters
59200 20 74 6f 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54 to.** FUNCT
59210 49 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c ION()..**.** L
59220 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e IKEFUNC(zName, n
59230 41 72 67 2c 20 70 41 72 67 2c 20 66 6c 61 67 73 Arg, pArg, flags
59240 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f ).** Used to
59250 20 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72 create a scalar
59260 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 function defini
59270 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 tion of a functi
59280 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 on zName .**
59290 20 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41 that accepts nA
592a0 72 67 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 rg arguments and
592b0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
592c0 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a by a call to C .
592d0 2a 2a 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 ** function
592e0 6c 69 6b 65 46 75 6e 63 2e 20 41 72 67 75 6d 65 likeFunc. Argume
592f0 6e 74 20 70 41 72 67 20 69 73 20 63 61 73 74 20 nt pArg is cast
59300 74 6f 20 61 20 28 76 6f 69 64 20 2a 29 20 61 6e to a (void *) an
59310 64 20 6d 61 64 65 0a 2a 2a 20 20 20 20 20 61 76 d made.** av
59320 61 69 6c 61 62 6c 65 20 61 73 20 74 68 65 20 66 ailable as the f
59330 75 6e 63 74 69 6f 6e 20 75 73 65 72 2d 64 61 74 unction user-dat
59340 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f a (sqlite3_user_
59350 64 61 74 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20 data()). The.**
59360 20 20 20 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 FuncDef.flag
59370 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 s variable is se
59380 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 t to the value p
59390 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6c 61 assed as the fla
593a0 67 73 0a 2a 2a 20 20 20 20 20 70 61 72 61 6d 65 gs.** parame
593b0 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ter..*/.#define
593c0 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 FUNCTION(zName,
593d0 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c nArg, iArg, bNC,
593e0 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 xFunc) \. {nAr
593f0 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 g, SQLITE_UTF8,
59400 62 4e 43 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e bNC*8, SQLITE_IN
59410 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 T_TO_PTR(iArg),
59420 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 0, xFunc, 0, 0,
59430 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 #zName, 0}.#defi
59440 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 ne STR_FUNCTION(
59450 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 zName, nArg, pAr
59460 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c g, bNC, xFunc) \
59470 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 . {nArg, SQLITE
59480 5f 55 54 46 38 2c 20 62 4e 43 2a 38 2c 20 70 41 _UTF8, bNC*8, pA
59490 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c rg, 0, xFunc, 0,
594a0 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 0, #zName, 0}.#
594b0 64 65 66 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 define LIKEFUNC(
594c0 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 zName, nArg, arg
594d0 2c 20 66 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 , flags) \. {nA
594e0 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c rg, SQLITE_UTF8,
594f0 20 66 6c 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 flags, (void *)
59500 61 72 67 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 arg, 0, likeFunc
59510 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 , 0, 0, #zName,
59520 30 7d 0a 23 64 65 66 69 6e 65 20 41 47 47 52 45 0}.#define AGGRE
59530 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 GATE(zName, nArg
59540 2c 20 61 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 , arg, nc, xStep
59550 2c 20 78 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e , xFinal) \. {n
59560 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Arg, SQLITE_UTF8
59570 2c 20 6e 63 2a 38 2c 20 53 51 4c 49 54 45 5f 49 , nc*8, SQLITE_I
59580 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 NT_TO_PTR(arg),
59590 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 0, 0, xStep,xFin
595a0 61 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a al,#zName,0}../*
595b0 0a 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 .** All current
595c0 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 savepoints are s
595d0 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 tored in a linke
595e0 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 d list starting
595f0 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 at.** sqlite3.pS
59600 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 avepoint. The fi
59610 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 rst element in t
59620 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d he list is the m
59630 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 ost recently.**
59640 6f 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 opened savepoint
59650 2e 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65 . Savepoints are
59660 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69 added to the li
59670 73 74 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a st by the vdbe.*
59680 2a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 * OP_Savepoint i
59690 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 nstruction..*/.s
596a0 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 truct Savepoint
596b0 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b {. char *zName;
596c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
596d0 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70 /* Savep
596e0 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 oint name (nul-t
596f0 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 erminated) */.
59700 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 Savepoint *pNext
59710 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
59720 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 /* Parent sa
59730 76 65 70 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 vepoint (if any)
59740 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
59750 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
59760 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f used as the seco
59770 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
59780 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 sqlite3Savepoint
59790 28 29 2c 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68 (),.** and as th
597a0 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f e P1 argument to
597b0 20 74 68 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e the OP_Savepoin
597c0 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
597d0 2f 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f /.#define SAVEPO
597e0 49 4e 54 5f 42 45 47 49 4e 20 20 20 20 20 20 30 INT_BEGIN 0
597f0 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 .#define SAVEPOI
59800 4e 54 5f 52 45 4c 45 41 53 45 20 20 20 20 31 0a NT_RELEASE 1.
59810 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e #define SAVEPOIN
59820 54 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a T_ROLLBACK 2..
59830 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69 ./*.** Each SQLi
59840 74 65 20 6d 6f 64 75 6c 65 20 28 76 69 72 74 75 te module (virtu
59850 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 al table definit
59860 69 6f 6e 29 20 69 73 20 64 65 66 69 6e 65 64 20 ion) is defined
59870 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 by an.** instanc
59880 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
59890 6e 67 20 73 74 72 75 63 74 75 72 65 2c 20 73 74 ng structure, st
598a0 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 ored in the sqli
598b0 74 65 33 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 te3.aModule.** h
598c0 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 ash table..*/.st
598d0 72 75 63 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 ruct Module {.
598e0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
598f0 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 dule *pModule;
59900 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b /* Callback
59910 20 70 6f 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63 pointers */. c
59920 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
59930 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
59940 20 20 20 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73 /* Name pass
59950 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 ed to create_mod
59960 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 ule() */. void
59970 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 *pAux;
59980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59990 2f 2a 20 70 41 75 78 20 70 61 73 73 65 64 20 74 /* pAux passed t
599a0 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 o create_module(
599b0 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 ) */. void (*xD
599c0 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b estroy)(void *);
599d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
599e0 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 odule destructor
599f0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a function */.};.
59a00 0a 2f 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 ./*.** informati
59a10 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f on about each co
59a20 6c 75 6d 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 lumn of an SQL t
59a30 61 62 6c 65 20 69 73 20 68 65 6c 64 20 69 6e 20 able is held in
59a40 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f an instance.** o
59a50 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
59a60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 ..*/.struct Colu
59a70 6d 6e 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 mn {. char *zNa
59a80 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 me; /* Name
59a90 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a of this column *
59aa0 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b /. Expr *pDflt;
59ab0 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 /* Default
59ac0 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f value of this co
59ad0 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a lumn */. char *
59ae0 7a 54 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 zType; /* Da
59af0 74 61 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 ta type for this
59b00 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 column */. cha
59b10 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a r *zColl; /*
59b20 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 Collating seque
59b30 6e 63 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 nce. If NULL, u
59b40 73 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a se the default *
59b50 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 /. u8 notNull;
59b60 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
59b70 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e there is a NOT N
59b80 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a ULL constraint *
59b90 2f 0a 20 20 75 38 20 69 73 50 72 69 6d 4b 65 79 /. u8 isPrimKey
59ba0 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 ; /* True if
59bb0 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70 this column is p
59bc0 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 art of the PRIMA
59bd0 52 59 20 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72 RY KEY */. char
59be0 20 61 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 affinity; /*
59bf0 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 One of the SQLIT
59c00 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 E_AFF_... values
59c10 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
59c20 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
59c30 41 42 4c 45 0a 20 20 75 38 20 69 73 48 69 64 64 ABLE. u8 isHidd
59c40 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 en; /* True
59c50 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 if this column i
59c60 73 20 27 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65 s 'hidden' */.#e
59c70 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 ndif.};../*.** A
59c80 20 22 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 "Collating Sequ
59c90 65 6e 63 65 22 20 69 73 20 64 65 66 69 6e 65 64 ence" is defined
59ca0 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 by an instance
59cb0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
59cc0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 43 .** structure. C
59cd0 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20 61 20 63 onceptually, a c
59ce0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
59cf0 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 e consists of a
59d00 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f name and.** a co
59d10 6d 70 61 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65 mparison routine
59d20 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
59d30 65 20 6f 72 64 65 72 20 6f 66 20 74 68 61 74 20 e order of that
59d40 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 sequence..**.**
59d50 54 68 65 72 65 20 6d 61 79 20 74 77 6f 20 73 65 There may two se
59d60 70 61 72 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 parate implement
59d70 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f ations of the co
59d80 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e llation function
59d90 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72 , one.** that pr
59da0 6f 63 65 73 73 65 73 20 74 65 78 74 20 69 6e 20 ocesses text in
59db0 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28 UTF-8 encoding (
59dc0 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e CollSeq.xCmp) an
59dd0 64 20 61 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a d another that.*
59de0 2a 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 74 * processes text
59df0 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d encoded in UTF-
59e00 31 36 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 16 (CollSeq.xCmp
59e10 31 36 29 2c 20 75 73 69 6e 67 20 74 68 65 20 6d 16), using the m
59e20 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 achine.** native
59e30 20 62 79 74 65 20 6f 72 64 65 72 2e 20 57 68 65 byte order. Whe
59e40 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 n a collation se
59e50 71 75 65 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65 quence is invoke
59e60 64 2c 20 53 51 4c 69 74 65 20 73 65 6c 65 63 74 d, SQLite select
59e70 73 0a 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e s.** the version
59e80 20 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 that will requi
59e90 72 65 20 74 68 65 20 6c 65 61 73 74 20 65 78 70 re the least exp
59ea0 65 6e 73 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a ensive encoding.
59eb0 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c ** translations,
59ec0 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 if any..**.** T
59ed0 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 he CollSeq.pUser
59ee0 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 member variable
59ef0 20 69 73 20 61 6e 20 65 78 74 72 61 20 70 61 72 is an extra par
59f00 61 6d 65 74 65 72 20 74 68 61 74 20 70 61 73 73 ameter that pass
59f10 65 64 20 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20 ed in.** as the
59f20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
59f30 6f 20 74 68 65 20 55 54 46 2d 38 20 63 6f 6d 70 o the UTF-8 comp
59f40 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c arison function,
59f50 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 xCmp..** CollSe
59f60 71 2e 70 55 73 65 72 31 36 20 69 73 20 74 68 65 q.pUser16 is the
59f70 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 20 equivalent for
59f80 74 68 65 20 55 54 46 2d 31 36 20 63 6f 6d 70 61 the UTF-16 compa
59f90 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a rison function,.
59fa0 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a ** xCmp16..**.**
59fb0 20 49 66 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71 If both CollSeq
59fc0 2e 78 43 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65 .xCmp and CollSe
59fd0 71 2e 78 43 6d 70 31 36 20 61 72 65 20 4e 55 4c q.xCmp16 are NUL
59fe0 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 L, it means that
59ff0 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e the.** collatin
5a000 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 75 6e g sequence is un
5a010 64 65 66 69 6e 65 64 2e 20 20 49 6e 64 69 63 65 defined. Indice
5a020 73 20 62 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e s built on an un
5a030 64 65 66 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 defined.** colla
5a040 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6d 61 ting sequence ma
5a050 79 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 y not be read or
5a060 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 written..*/.str
5a070 75 63 74 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 uct CollSeq {.
5a080 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
5a090 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
5a0a0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 the collating s
5a0b0 65 71 75 65 6e 63 65 2c 20 55 54 46 2d 38 20 65 equence, UTF-8 e
5a0c0 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20 65 ncoded */. u8 e
5a0d0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nc;
5a0e0 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 /* Text encodi
5a0f0 6e 67 20 68 61 6e 64 6c 65 64 20 62 79 20 78 43 ng handled by xC
5a100 6d 70 28 29 20 2a 2f 0a 20 20 75 38 20 74 79 70 mp() */. u8 typ
5a110 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
5a120 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 /* One of the SQ
5a130 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 LITE_COLL_... va
5a140 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 lues below */.
5a150 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20 20 void *pUser;
5a160 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
5a170 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28 rgument to xCmp(
5a180 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d ) */. int (*xCm
5a190 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f p)(void*,int, co
5a1a0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 nst void*, int,
5a1b0 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 const void*);.
5a1c0 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
5a1d0 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 d*); /* Destruc
5a1e0 74 6f 72 20 66 6f 72 20 70 55 73 65 72 20 2a 2f tor for pUser */
5a1f0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 .};../*.** Allow
5a200 65 64 20 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c ed values of Col
5a210 6c 53 65 71 2e 74 79 70 65 3a 0a 2a 2f 0a 23 64 lSeq.type:.*/.#d
5a220 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c efine SQLITE_COL
5a230 4c 5f 42 49 4e 41 52 59 20 20 31 20 20 2f 2a 20 L_BINARY 1 /*
5a240 54 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 63 The default memc
5a250 6d 70 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73 mp() collating s
5a260 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 equence */.#defi
5a270 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e ne SQLITE_COLL_N
5a280 4f 43 41 53 45 20 20 32 20 20 2f 2a 20 54 68 65 OCASE 2 /* The
5a290 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 built-in NOCASE
5a2a0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5a2b0 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 nce */.#define S
5a2c0 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 QLITE_COLL_REVER
5a2d0 53 45 20 33 20 20 2f 2a 20 54 68 65 20 62 75 69 SE 3 /* The bui
5a2e0 6c 74 2d 69 6e 20 52 45 56 45 52 53 45 20 63 6f lt-in REVERSE co
5a2f0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
5a300 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5a310 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20 TE_COLL_USER
5a320 30 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 0 /* Any other
5a330 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c user-defined col
5a340 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
5a350 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 */../*.** A sort
5a360 20 6f 72 64 65 72 20 63 61 6e 20 62 65 20 65 69 order can be ei
5a370 74 68 65 72 20 41 53 43 20 6f 72 20 44 45 53 43 ther ASC or DESC
5a380 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5a390 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 20 ITE_SO_ASC
5a3a0 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 0 /* Sort in a
5a3b0 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a scending order *
5a3c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5a3d0 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31 20 _SO_DESC 1
5a3e0 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65 /* Sort in asce
5a3f0 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a nding order */..
5a400 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 /*.** Column aff
5a410 69 6e 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a inity types..**.
5a420 2a 2a 20 54 68 65 73 65 20 75 73 65 64 20 74 6f ** These used to
5a430 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e have mnemonic n
5a440 61 6d 65 20 6c 69 6b 65 20 27 69 27 20 66 6f 72 ame like 'i' for
5a450 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
5a460 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66 GER and.** 't' f
5a470 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 or SQLITE_AFF_TE
5a480 58 54 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 XT. But we can
5a490 73 61 76 65 20 61 20 6c 69 74 74 6c 65 20 73 70 save a little sp
5a4a0 61 63 65 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a ace and improve.
5a4b0 2a 2a 20 74 68 65 20 73 70 65 65 64 20 61 20 6c ** the speed a l
5a4c0 69 74 74 6c 65 20 62 79 20 6e 75 6d 62 65 72 69 ittle by numberi
5a4d0 6e 67 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f ng the values co
5a4e0 6e 73 65 63 75 74 69 76 65 6c 79 2e 20 20 0a 2a nsecutively. .*
5a4f0 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68 65 72 20 *.** But rather
5a500 74 68 61 6e 20 73 74 61 72 74 20 77 69 74 68 20 than start with
5a510 30 20 6f 72 20 31 2c 20 77 65 20 62 65 67 69 6e 0 or 1, we begin
5a520 20 77 69 74 68 20 27 61 27 2e 20 20 54 68 61 74 with 'a'. That
5a530 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75 way,.** when mu
5a540 6c 74 69 70 6c 65 20 61 66 66 69 6e 69 74 79 20 ltiple affinity
5a550 74 79 70 65 73 20 61 72 65 20 63 6f 6e 63 61 74 types are concat
5a560 65 6e 61 74 65 64 20 69 6e 74 6f 20 61 20 73 74 enated into a st
5a570 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75 73 65 64 ring and.** used
5a580 20 61 73 20 74 68 65 20 50 34 20 6f 70 65 72 61 as the P4 opera
5a590 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c 20 62 65 nd, they will be
5a5a0 20 6d 6f 72 65 20 72 65 61 64 61 62 6c 65 2e 0a more readable..
5a5b0 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 **.** Note also
5a5c0 74 68 61 74 20 74 68 65 20 6e 75 6d 65 72 69 63 that the numeric
5a5d0 20 74 79 70 65 73 20 61 72 65 20 67 72 6f 75 70 types are group
5a5e0 65 64 20 74 6f 67 65 74 68 65 72 20 73 6f 20 74 ed together so t
5a5f0 68 61 74 20 74 65 73 74 69 6e 67 0a 2a 2a 20 66 hat testing.** f
5a600 6f 72 20 61 20 6e 75 6d 65 72 69 63 20 74 79 70 or a numeric typ
5a610 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 63 6f e is a single co
5a620 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 mparison..*/.#de
5a630 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5a640 54 45 58 54 20 20 20 20 20 27 61 27 0a 23 64 65 TEXT 'a'.#de
5a650 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5a660 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a 23 64 65 NONE 'b'.#de
5a670 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5a680 4e 55 4d 45 52 49 43 20 20 27 63 27 0a 23 64 65 NUMERIC 'c'.#de
5a690 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5a6a0 49 4e 54 45 47 45 52 20 20 27 64 27 0a 23 64 65 INTEGER 'd'.#de
5a6b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5a6c0 52 45 41 4c 20 20 20 20 20 27 65 27 0a 0a 23 64 REAL 'e'..#d
5a6d0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 4e efine sqlite3IsN
5a6e0 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 58 umericAffinity(X
5a6f0 29 20 20 28 28 58 29 3e 3d 53 51 4c 49 54 45 5f ) ((X)>=SQLITE_
5a700 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a AFF_NUMERIC)../*
5a710 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41 .** The SQLITE_A
5a720 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65 73 20 6d FF_MASK values m
5a730 61 73 6b 73 20 6f 66 66 20 74 68 65 20 73 69 67 asks off the sig
5a740 6e 69 66 69 63 61 6e 74 20 62 69 74 73 20 6f 66 nificant bits of
5a750 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 an.** affinity
5a760 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64 65 66 69 value. .*/.#defi
5a770 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 ne SQLITE_AFF_MA
5a780 53 4b 20 20 20 20 20 30 78 36 37 0a 0a 2f 2a 0a SK 0x67../*.
5a790 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 62 69 ** Additional bi
5a7a0 74 20 76 61 6c 75 65 73 20 74 68 61 74 20 63 61 t values that ca
5a7b0 6e 20 62 65 20 4f 52 65 64 20 77 69 74 68 20 61 n be ORed with a
5a7c0 6e 20 61 66 66 69 6e 69 74 79 20 77 69 74 68 6f n affinity witho
5a7d0 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 ut.** changing t
5a7e0 68 65 20 61 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a he affinity..*/.
5a7f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4a #define SQLITE_J
5a800 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30 78 30 38 UMPIFNULL 0x08
5a810 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66 20 65 69 /* jumps if ei
5a820 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
5a830 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NULL */.#define
5a840 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 20 SQLITE_STOREP2
5a850 20 20 20 20 30 78 31 30 20 20 2f 2a 20 53 74 6f 0x10 /* Sto
5a860 72 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 re result in reg
5a870 5b 50 32 5d 20 72 61 74 68 65 72 20 74 68 61 6e [P2] rather than
5a880 20 6a 75 6d 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 jump */../*.**
5a890 45 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69 Each SQL table i
5a8a0 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e s represented in
5a8b0 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e memory by an in
5a8c0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a stance of the.**
5a8d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
5a8e0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c ture..**.** Tabl
5a8f0 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e e.zName is the n
5a900 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
5a910 2e 20 20 54 68 65 20 63 61 73 65 20 6f 66 20 74 . The case of t
5a920 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 he original.** C
5a930 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
5a940 65 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c ement is stored,
5a950 20 62 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74 but case is not
5a960 20 73 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 significant for
5a970 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e .** comparisons.
5a980 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f .**.** Table.nCo
5a990 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 l is the number
5a9a0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
5a9b0 69 73 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 is table. Table
5a9c0 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f .aCol is a.** po
5a9d0 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 inter to an arra
5a9e0 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75 y of Column stru
5a9f0 63 74 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20 ctures, one for
5aa00 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a each column..**.
5aa10 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 ** If the table
5aa20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 has an INTEGER P
5aa30 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e RIMARY KEY, then
5aa40 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 Table.iPKey is
5aa50 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 the index of.**
5aa60 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 the column that
5aa70 69 73 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f is that key. O
5aa80 74 68 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69 therwise Table.i
5aa90 50 4b 65 79 20 69 73 20 6e 65 67 61 74 69 76 65 PKey is negative
5aaa0 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20 . Note.** that
5aab0 74 68 65 20 64 61 74 61 74 79 70 65 20 6f 66 20 the datatype of
5aac0 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 the PRIMARY KEY
5aad0 6d 75 73 74 20 62 65 20 49 4e 54 45 47 45 52 20 must be INTEGER
5aae0 66 6f 72 20 74 68 69 73 20 66 69 65 6c 64 20 74 for this field t
5aaf0 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e o.** be set. An
5ab00 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
5ab10 20 4b 45 59 20 69 73 20 75 73 65 64 20 61 73 20 KEY is used as
5ab20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61 the rowid for ea
5ab30 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 ch row of.** the
5ab40 20 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61 table. If a ta
5ab50 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 ble has no INTEG
5ab60 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY,
5ab70 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f then a random ro
5ab80 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61 wid.** is genera
5ab90 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 ted for each row
5aba0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 of the table.
5abb0 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 TF_HasPrimaryKey
5abc0 20 69 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 is set if.** th
5abd0 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20 e table has any
5abe0 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54 PRIMARY KEY, INT
5abf0 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73 EGER or otherwis
5ac00 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 e..**.** Table.t
5ac10 6e 75 6d 20 69 73 20 74 68 65 20 70 61 67 65 20 num is the page
5ac20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 number for the r
5ac30 6f 6f 74 20 42 54 72 65 65 20 70 61 67 65 20 6f oot BTree page o
5ac40 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 f the table in t
5ac50 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 he.** database f
5ac60 69 6c 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69 ile. If Table.i
5ac70 44 62 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 Db is the index
5ac80 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
5ac90 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a table backend.**
5aca0 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d in sqlite.aDb[]
5acb0 2e 20 20 30 20 69 73 20 66 6f 72 20 74 68 65 20 . 0 is for the
5acc0 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e main database an
5acd0 64 20 31 20 69 73 20 66 6f 72 20 74 68 65 20 66 d 1 is for the f
5ace0 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 ile that.** hold
5acf0 73 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c s temporary tabl
5ad00 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20 es and indices.
5ad10 20 49 66 20 54 46 5f 45 70 68 65 6d 65 72 61 6c If TF_Ephemeral
5ad20 20 69 73 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20 is set.** then
5ad30 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 74 6f the table is sto
5ad40 72 65 64 20 69 6e 20 61 20 66 69 6c 65 20 74 68 red in a file th
5ad50 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 at is automatica
5ad60 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77 lly deleted.** w
5ad70 68 65 6e 20 74 68 65 20 56 44 42 45 20 63 75 72 hen the VDBE cur
5ad80 73 6f 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 sor to the table
5ad90 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20 is closed. In
5ada0 74 68 69 73 20 63 61 73 65 20 54 61 62 6c 65 2e this case Table.
5adb0 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20 tnum .** refers
5adc0 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 VDBE cursor numb
5add0 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 er that holds th
5ade0 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f e table open, no
5adf0 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a t to the root.**
5ae00 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 page number. T
5ae10 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 73 20 ransient tables
5ae20 61 72 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 are used to hold
5ae30 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 the results of
5ae40 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72 79 20 74 a.** sub-query t
5ae50 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 73 74 hat appears inst
5ae60 65 61 64 20 6f 66 20 61 20 72 65 61 6c 20 74 61 ead of a real ta
5ae70 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 ble name in the
5ae80 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20 FROM clause .**
5ae90 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 of a SELECT stat
5aea0 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 ement..*/.struct
5aeb0 20 54 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 Table {. sqlit
5aec0 65 33 20 2a 64 62 4d 65 6d 3b 20 20 20 20 20 20 e3 *dbMem;
5aed0 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 74 69 6f 6e /* DB connection
5aee0 20 75 73 65 64 20 66 6f 72 20 6c 6f 6f 6b 61 73 used for lookas
5aef0 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e ide allocations.
5af00 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d */. char *zNam
5af10 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 e; /* Na
5af20 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
5af30 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 or view */. int
5af40 20 69 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20 iPKey;
5af50 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61 /* If not nega
5af60 74 69 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69 tive, use aCol[i
5af70 50 4b 65 79 5d 20 61 73 20 74 68 65 20 70 72 69 PKey] as the pri
5af80 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e mary key */. in
5af90 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 t nCol;
5afa0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5afb0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 columns in this
5afc0 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d table */. Colum
5afd0 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 n *aCol;
5afe0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
5aff0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e bout each column
5b000 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e */. Index *pIn
5b010 64 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 dex; /* Li
5b020 73 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 st of SQL indexe
5b030 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e s on this table.
5b040 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 */. int tnum;
5b050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
5b060 6f 74 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f ot BTree node fo
5b070 72 20 74 68 69 73 20 74 61 62 6c 65 20 28 73 65 r this table (se
5b080 65 20 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f e note above) */
5b090 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
5b0a0 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 ct; /* NULL
5b0b0 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69 for tables. Poi
5b0c0 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f nts to definitio
5b0d0 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a n if a view. */.
5b0e0 20 20 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20 u16 nRef;
5b0f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5b100 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
5b110 74 68 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20 this Table */.
5b120 75 38 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20 u8 tabFlags;
5b130 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 /* Mask of
5b140 54 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 TF_* values */.
5b150 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 u8 keyConf;
5b160 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f /* What to
5b170 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75 do in case of u
5b180 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 niqueness confli
5b190 63 74 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 ct on iPKey */.
5b1a0 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 FKey *pFKey;
5b1b0 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 /* Linked
5b1c0 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65 list of all fore
5b1d0 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73 ign keys in this
5b1e0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 table */. char
5b1f0 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20 *zColAff;
5b200 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e /* String defin
5b210 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 ing the affinity
5b220 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 of each column
5b230 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
5b240 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 E_OMIT_CHECK. E
5b250 78 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 xpr *pCheck;
5b260 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f /* The AND o
5b270 66 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 f all CHECK cons
5b280 74 72 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 traints */.#endi
5b290 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
5b2a0 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 _OMIT_ALTERTABLE
5b2b0 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66 . int addColOff
5b2c0 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 set; /* Offse
5b2d0 74 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c t in CREATE TABL
5b2e0 45 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20 E stmt to add a
5b2f0 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 new column */.#e
5b300 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
5b310 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
5b320 54 41 42 4c 45 0a 20 20 4d 6f 64 75 6c 65 20 2a TABLE. Module *
5b330 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 pMod; /*
5b340 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69 Pointer to the i
5b350 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
5b360 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 the module */.
5b370 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
5b380 56 74 61 62 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 Vtab; /* Pointer
5b390 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 to the module i
5b3a0 6e 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 nstance */. int
5b3b0 20 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 nModuleArg;
5b3c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
5b3d0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
5b3e0 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72 module */. char
5b3f0 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 **azModuleArg;
5b400 20 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20 /* Text of all
5b410 6d 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d module args. [0]
5b420 20 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 is module name
5b430 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 67 */.#endif. Trig
5b440 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 ger *pTrigger;
5b450 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 /* List of trig
5b460 67 65 72 73 20 73 74 6f 72 65 64 20 69 6e 20 70 gers stored in p
5b470 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 65 Schema */. Sche
5b480 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 ma *pSchema;
5b490 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20 /* Schema that
5b4a0 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61 contains this ta
5b4b0 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ble */. Table *
5b4c0 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a pNextZombie; /*
5b4d0 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72 Next on the Par
5b4e0 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69 se.pZombieTab li
5b4f0 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 st */.};../*.**
5b500 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 Allowed values f
5b510 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73 or Tabe.tabFlags
5b520 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f ..*/.#define TF_
5b530 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20 Readonly
5b540 30 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d 0x01 /* Read-
5b550 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c only system tabl
5b560 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f e */.#define TF_
5b570 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 Ephemeral
5b580 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70 0x02 /* An ep
5b590 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f hemeral table */
5b5a0 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50 .#define TF_HasP
5b5b0 72 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34 rimaryKey 0x04
5b5c0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 /* Table has
5b5d0 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a a primary key *
5b5e0 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74 /.#define TF_Aut
5b5f0 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30 oincrement 0x0
5b600 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 8 /* Integer
5b610 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61 primary key is a
5b620 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a utoincrement */.
5b630 23 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75 #define TF_Virtu
5b640 61 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 20 al 0x10
5b650 20 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75 /* Is a virtu
5b660 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 al table */.#def
5b670 69 6e 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64 ine TF_NeedMetad
5b680 61 74 61 20 20 20 20 30 78 32 30 20 20 20 20 2f ata 0x20 /
5b690 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 * aCol[].zType a
5b6a0 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 nd aCol[].pColl
5b6b0 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a missing */..../*
5b6c0 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 .** Test to see
5b6d0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 whether or not a
5b6e0 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 table is a virt
5b6f0 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 ual table. This
5b700 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61 is.** done as a
5b710 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 macro so that i
5b720 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69 t will be optimi
5b730 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72 zed out when vir
5b740 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75 tual.** table su
5b750 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64 pport is omitted
5b760 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e from the build.
5b770 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
5b780 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
5b790 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49 ABLE.# define I
5b7a0 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 sVirtual(X)
5b7b0 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73 (((X)->tabFlags
5b7c0 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d & TF_Virtual)!=
5b7d0 30 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 0).# define IsH
5b7e0 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 iddenColumn(X) (
5b7f0 28 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23 (X)->isHidden).#
5b800 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 else.# define I
5b810 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 sVirtual(X)
5b820 20 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 0.# define IsH
5b830 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 iddenColumn(X) 0
5b840 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 .#endif../*.** E
5b850 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 ach foreign key
5b860 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e constraint is an
5b870 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
5b880 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
5b890 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f ture..**.** A fo
5b8a0 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73 reign key is ass
5b8b0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f ociated with two
5b8c0 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66 tables. The "f
5b8d0 72 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a rom" table is.**
5b8e0 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 the table that
5b8f0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46 contains the REF
5b900 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74 ERENCES clause t
5b910 68 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20 hat creates the
5b920 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 foreign.** key.
5b930 20 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20 The "to" table
5b940 69 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 is the table tha
5b950 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 t is named in th
5b960 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 e REFERENCES cla
5b970 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 use..** Consider
5b980 20 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a this example:.*
5b990 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 *.** CREATE
5b9a0 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20 TABLE ex1(.**
5b9b0 20 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52 a INTEGER PR
5b9c0 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 IMARY KEY,.**
5b9d0 20 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f b INTEGER CO
5b9e0 4e 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46 NSTRAINT fk1 REF
5b9f0 45 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a ERENCES ex2(x).*
5ba00 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 * );.**.** F
5ba10 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22 or foreign key "
5ba20 66 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74 fk1", the from-t
5ba30 61 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e able is "ex1" an
5ba40 64 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 d the to-table i
5ba50 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 s "ex2"..**.** E
5ba60 61 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63 ach REFERENCES c
5ba70 6c 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20 lause generates
5ba80 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
5ba90 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
5baa0 75 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20 ucture.** which
5bab0 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 is attached to t
5bac0 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 he from-table.
5bad0 54 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65 The to-table nee
5bae0 64 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e d not exist when
5baf0 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 .** the from-tab
5bb00 6c 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 le is created.
5bb10 54 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 The existence of
5bb20 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 the to-table is
5bb30 20 6e 6f 74 20 63 68 65 63 6b 65 64 0a 2a 2a 20 not checked.**
5bb40 75 6e 74 69 6c 20 61 6e 20 61 74 74 65 6d 70 74 until an attempt
5bb50 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65 is made to inse
5bb60 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 rt data into the
5bb70 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a from-table..**.
5bb80 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 61 46 ** The sqlite.aF
5bb90 4b 65 79 20 68 61 73 68 20 74 61 62 6c 65 20 73 Key hash table s
5bba0 74 6f 72 65 73 20 70 6f 69 6e 74 65 72 73 20 74 tores pointers t
5bbb0 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 o this structure
5bbc0 0a 2a 2a 20 67 69 76 65 6e 20 74 68 65 20 6e 61 .** given the na
5bbd0 6d 65 20 6f 66 20 61 20 74 6f 2d 74 61 62 6c 65 me of a to-table
5bbe0 2e 20 20 46 6f 72 20 65 61 63 68 20 74 6f 2d 74 . For each to-t
5bbf0 61 62 6c 65 2c 20 61 6c 6c 20 66 6f 72 65 69 67 able, all foreig
5bc00 6e 20 6b 65 79 73 0a 2a 2a 20 61 73 73 6f 63 69 n keys.** associ
5bc10 61 74 65 64 20 77 69 74 68 20 74 68 61 74 20 74 ated with that t
5bc20 61 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 6c 69 able are on a li
5bc30 6e 6b 65 64 20 6c 69 73 74 20 75 73 69 6e 67 20 nked list using
5bc40 74 68 65 20 46 4b 65 79 2e 70 4e 65 78 74 54 6f the FKey.pNextTo
5bc50 0a 2a 2a 20 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 .** field..*/.st
5bc60 72 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54 61 ruct FKey {. Ta
5bc70 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 20 ble *pFrom;
5bc80 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 /* The table tha
5bc90 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 t contains the R
5bca0 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 EFERENCES clause
5bcb0 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 */. FKey *pNex
5bcc0 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 tFrom; /* Next
5bcd0 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 foreign key in p
5bce0 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a From */. char *
5bcf0 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e zTo; /* N
5bd00 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 ame of table tha
5bd10 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 t the key points
5bd20 20 74 6f 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 to */. FKey *p
5bd30 4e 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 NextTo; /* Ne
5bd40 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 xt foreign key t
5bd50 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 7a 54 hat points to zT
5bd60 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b o */. int nCol;
5bd70 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5bd80 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
5bd90 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 73 this key */. s
5bda0 74 72 75 63 74 20 73 43 6f 6c 4d 61 70 20 7b 20 truct sColMap {
5bdb0 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20 63 /* Mapping of c
5bdc0 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f 6d 20 olumns in pFrom
5bdd0 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a 54 to columns in zT
5bde0 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 46 72 o */. int iFr
5bdf0 6f 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 om; /* I
5be00 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 ndex of column i
5be10 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 n pFrom */. c
5be20 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 har *zCol;
5be30 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c /* Name of col
5be40 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49 66 20 umn in zTo. If
5be50 30 20 75 73 65 20 50 52 49 4d 41 52 59 20 4b 45 0 use PRIMARY KE
5be60 59 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 20 Y */. } *aCol;
5be70 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
5be80 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f entry for each o
5be90 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 20 f nCol column s
5bea0 2a 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72 */. u8 isDeferr
5beb0 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 ed; /* True i
5bec0 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 f constraint che
5bed0 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 cking is deferre
5bee0 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f d till COMMIT */
5bef0 0a 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e 66 . u8 updateConf
5bf00 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 ; /* How to r
5bf10 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 esolve conflicts
5bf20 20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 that occur on U
5bf30 50 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 PDATE */. u8 de
5bf40 6c 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 leteConf; /*
5bf50 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 How to resolve c
5bf60 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f 63 onflicts that oc
5bf70 63 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f cur on DELETE */
5bf80 0a 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 . u8 insertConf
5bf90 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 ; /* How to r
5bfa0 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 esolve conflicts
5bfb0 20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 that occur on I
5bfc0 4e 53 45 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a NSERT */.};../*.
5bfd0 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 ** SQLite suppor
5bfe0 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e ts many differen
5bff0 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 t ways to resolv
5c000 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a e a constraint.*
5c010 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 * error. ROLLBA
5c020 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 CK processing me
5c030 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 ans that a const
5c040 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a raint violation.
5c050 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 ** causes the op
5c060 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 eration in proce
5c070 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 ss to fail and f
5c080 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 or the current t
5c090 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f ransaction.** to
5c0a0 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e be rolled back.
5c0b0 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 ABORT processi
5c0c0 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 ng means the ope
5c0d0 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 ration in proces
5c0e0 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 s.** fails and a
5c0f0 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 ny prior changes
5c100 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f from that one o
5c110 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 peration are bac
5c120 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 ked out,.** but
5c130 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
5c140 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 is not rolled ba
5c150 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 ck. FAIL proces
5c160 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a sing means that.
5c170 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e ** the operation
5c180 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f in progress sto
5c190 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 ps and returns a
5c1a0 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 n error code. B
5c1b0 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e ut prior.** chan
5c1c0 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 ges due to the s
5c1d0 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 ame operation ar
5c1e0 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 e not backed out
5c1f0 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b and no rollback
5c200 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e .** occurs. IGN
5c210 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 ORE means that t
5c220 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f he particular ro
5c230 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 w that caused th
5c240 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 e constraint.**
5c250 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 error is not ins
5c260 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 erted or updated
5c270 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f . Processing co
5c280 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 ntinues and no e
5c290 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 rror.** is retur
5c2a0 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 ned. REPLACE me
5c2b0 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73 ans that preexis
5c2c0 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f ting database ro
5c2d0 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a ws that caused.*
5c2e0 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 * a UNIQUE const
5c2f0 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 raint violation
5c300 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 are removed so t
5c310 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 hat the new inse
5c320 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 rt or.** update
5c330 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 can proceed. Pr
5c340 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 ocessing continu
5c350 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 es and no error
5c360 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a is reported..**.
5c370 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 ** RESTRICT, SET
5c380 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 NULL, and CASCAD
5c390 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 E actions apply
5c3a0 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 only to foreign
5c3b0 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 keys..** RESTRIC
5c3c0 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 T is the same as
5c3d0 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 ABORT for IMMED
5c3e0 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 IATE foreign key
5c3f0 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d s and the.** sam
5c400 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f e as ROLLBACK fo
5c410 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e r DEFERRED keys.
5c420 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 SETNULL means
5c430 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e that the foreign
5c440 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 .** key is set t
5c450 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 o NULL. CASCADE
5c460 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 means that a DE
5c470 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f LETE or UPDATE o
5c480 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e f the.** referen
5c490 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 ced table row is
5c4a0 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f propagated into
5c4b0 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f the row that ho
5c4c0 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 lds the.** forei
5c4d0 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 gn key..** .** T
5c4e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d he following sym
5c4f0 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 bolic values are
5c500 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 used to record
5c510 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 which type.** of
5c520 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e action to take.
5c530 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e .*/.#define OE_N
5c540 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 one 0 /* T
5c550 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 here is no const
5c560 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a raint to check *
5c570 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c /.#define OE_Rol
5c580 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 lback 1 /* Fai
5c590 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 l the operation
5c5a0 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 and rollback the
5c5b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a transaction */.
5c5c0 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 #define OE_Abort
5c5d0 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 2 /* Back
5c5e0 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 out changes but
5c5f0 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 do no rollback t
5c600 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 ransaction */.#d
5c610 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 efine OE_Fail
5c620 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 3 /* Stop th
5c630 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 e operation but
5c640 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 leave all prior
5c650 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 changes */.#defi
5c660 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 ne OE_Ignore 4
5c670 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 /* Ignore the
5c680 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 error. Do not d
5c690 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 o the INSERT or
5c6a0 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e UPDATE */.#defin
5c6b0 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 e OE_Replace 5
5c6c0 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 /* Delete exis
5c6d0 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 ting record, the
5c6e0 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 n do INSERT or U
5c6f0 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e PDATE */..#defin
5c700 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 e OE_Restrict 6
5c710 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f /* OE_Abort fo
5c720 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f r IMMEDIATE, OE_
5c730 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 Rollback for DEF
5c740 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 ERRED */.#define
5c750 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 OE_SetNull 7
5c760 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 /* Set the fore
5c770 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f ign key value to
5c780 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 NULL */.#define
5c790 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 OE_SetDflt 8
5c7a0 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 /* Set the fore
5c7b0 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f ign key value to
5c7c0 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a its default */.
5c7d0 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 #define OE_Casca
5c7e0 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 de 9 /* Casca
5c7f0 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a de the changes *
5c800 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 /..#define OE_De
5c810 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f fault 99 /* Do
5c820 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65 whatever the de
5c830 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 fault action is
5c840 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e */.../*.** An in
5c850 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
5c860 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
5c870 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 e is passed as t
5c880 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 he first.** argu
5c890 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 ment to sqlite3V
5c8a0 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e dbeKeyCompare an
5c8b0 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e d is used to con
5c8c0 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d trol the .** com
5c8d0 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 parison of the t
5c8e0 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a wo index keys..*
5c8f0 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f /.struct KeyInfo
5c900 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
5c910 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
5c920 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
5c930 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b ion */. u8 enc;
5c940 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5c950 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 Text encoding -
5c960 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f one of the TEXT_
5c970 55 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 Utf* values */.
5c980 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 u16 nField;
5c990 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5c9a0 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f f entries in aCo
5c9b0 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 ll[] */. u8 *aS
5c9c0 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a ortOrder; /*
5c9d0 20 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61 If defined an a
5c9e0 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 SortOrder[i] is
5c9f0 74 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20 true, sort DESC
5ca00 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 */. CollSeq *aC
5ca10 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c oll[1]; /* Coll
5ca20 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 ating sequence f
5ca30 6f 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 or each term of
5ca40 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f the key */.};../
5ca50 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
5ca60 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
5ca70 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 g structure hold
5ca80 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 s information ab
5ca90 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 out a.** single
5caa0 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 61 index record tha
5cab0 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 t has already be
5cac0 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e en parsed out in
5cad0 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a to individual.**
5cae0 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 values..**.** A
5caf0 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 record is an ob
5cb00 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69 ject that contai
5cb10 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 ns one or more f
5cb20 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a ields of data..*
5cb30 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 73 * Records are us
5cb40 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ed to store the
5cb50 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62 content of a tab
5cb60 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 le row and to st
5cb70 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f ore.** the key o
5cb80 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 f an index. A b
5cb90 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 lob encoding of
5cba0 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 61 a record is crea
5cbb0 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 ted by.** the OP
5cbc0 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f _MakeRecord opco
5cbd0 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 de of the VDBE a
5cbe0 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c nd is disassembl
5cbf0 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f ed by the.** OP_
5cc00 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a Column opcode..*
5cc10 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 *.** This struct
5cc20 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f ure holds a reco
5cc30 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 rd that has alre
5cc40 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 ady been disasse
5cc50 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 mbled.** into it
5cc60 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69 s constituent fi
5cc70 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 elds..*/.struct
5cc80 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b UnpackedRecord {
5cc90 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 . KeyInfo *pKey
5cca0 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 Info; /* Collat
5ccb0 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 ion and sort-ord
5ccc0 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a er information *
5ccd0 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 /. u16 nField;
5cce0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5ccf0 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
5cd00 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 apMem[] */. u16
5cd10 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
5cd20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 /* Boolean sett
5cd30 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f ings. UNPACKED_
5cd40 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 4d ... below */. M
5cd50 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 em *aMem;
5cd60 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a /* Values */.
5cd70 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 };../*.** Allowe
5cd80 64 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 d values of Unpa
5cd90 63 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 ckedRecord.flags
5cda0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 .*/.#define UNPA
5cdb0 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 CKED_NEED_FREE
5cdc0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 0x0001 /* Me
5cdd0 6d 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c mory is from sql
5cde0 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a ite3Malloc() */.
5cdf0 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 #define UNPACKED
5ce00 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 _NEED_DESTROY 0
5ce10 78 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b x0002 /* apMem[
5ce20 5d 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 ]s should all be
5ce30 20 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 destroyed */.#d
5ce40 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 efine UNPACKED_I
5ce50 47 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 GNORE_ROWID 0x0
5ce60 30 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 004 /* Ignore t
5ce70 72 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e railing rowid on
5ce80 20 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 key1 */.#define
5ce90 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 UNPACKED_INCRKE
5cea0 59 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 Y 0x0008
5ceb0 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 /* Make this key
5cec0 20 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 an epsilon larg
5ced0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e er */.#define UN
5cee0 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 PACKED_PREFIX_MA
5cef0 54 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 TCH 0x0010 /*
5cf00 41 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 A prefix match i
5cf10 73 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 s considered OK
5cf20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 */../*.** Each S
5cf30 51 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 QL index is repr
5cf40 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 esented in memor
5cf50 79 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 y by an.** insta
5cf60 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
5cf70 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
5cf80 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e **.** The column
5cf90 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 s of the table t
5cfa0 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e hat are to be in
5cfb0 64 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 dexed are descri
5cfc0 62 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 bed.** by the ai
5cfd0 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f Column[] field o
5cfe0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
5cff0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
5d000 73 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 suppose.** we ha
5d010 76 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ve the following
5d020 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 table and index
5d030 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 :.**.** CREA
5d040 54 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 TE TABLE Ex1(c1
5d050 69 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 int, c2 int, c3
5d060 74 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 text);.** CR
5d070 45 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f EATE INDEX Ex2 O
5d080 4e 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a N Ex1(c3,c1);.**
5d090 0a 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 .** In the Table
5d0a0 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 structure descr
5d0b0 69 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d ibing Ex1, nCol=
5d0c0 3d 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 =3 because there
5d0d0 20 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f are.** three co
5d0e0 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 lumns in the tab
5d0f0 6c 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 le. In the Inde
5d100 78 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 x structure desc
5d110 72 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e ribing.** Ex2, n
5d120 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 Column==2 since
5d130 32 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 2 of the 3 colum
5d140 6e 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e ns of Ex1 are in
5d150 64 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 dexed..** The va
5d160 6c 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 lue of aiColumn
5d170 69 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f is {2, 0}. aiCo
5d180 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 lumn[0]==2 becau
5d190 73 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 se the .** first
5d1a0 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e column to be in
5d1b0 64 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 dexed (c3) has a
5d1c0 6e 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 n index of 2 in
5d1d0 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 Ex1.aCol[]..** T
5d1e0 68 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e he second column
5d1f0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 to be indexed (
5d200 63 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 c1) has an index
5d210 20 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e of 0 in.** Ex1.
5d220 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 aCol[], hence Ex
5d230 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2.aiColumn[1]==0
5d240 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 ..**.** The Inde
5d250 78 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 x.onError field
5d260 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 determines wheth
5d270 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e er or not the in
5d280 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a dexed columns.**
5d290 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 must be unique
5d2a0 61 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 and what to do i
5d2b0 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 f they are not.
5d2c0 20 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 When Index.onEr
5d2d0 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 ror=OE_None,.**
5d2e0 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 it means this is
5d2f0 20 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e not a unique in
5d300 64 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 dex. Otherwise
5d310 69 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 it is a unique i
5d320 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ndex.** and the
5d330 76 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f value of Index.o
5d340 6e 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 nError indicate
5d350 74 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 the which confli
5d360 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a ct resolution .*
5d370 2a 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 * algorithm to e
5d380 6d 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 mploy whenever a
5d390 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 n attempt is mad
5d3a0 65 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f e to insert a no
5d3b0 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d n-unique.** elem
5d3c0 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 ent..*/.struct I
5d3d0 6e 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a ndex {. char *z
5d3e0 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d Name; /* Nam
5d3f0 65 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 e of this index
5d400 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e */. int nColumn
5d410 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ; /* Number
5d420 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
5d430 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 e table used by
5d440 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 this index */.
5d450 69 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 int *aiColumn;
5d460 20 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e /* Which column
5d470 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 s are used by th
5d480 69 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 is index. 1st i
5d490 73 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 s 0 */. unsigne
5d4a0 64 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 d *aiRowEst; /*
5d4b0 52 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a Result of ANALYZ
5d4c0 45 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c E: Est. rows sel
5d4d0 65 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f ected by each co
5d4e0 6c 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 lumn */. Table
5d4f0 2a 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 *pTable; /* Th
5d500 65 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e e SQL table bein
5d510 67 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 g indexed */. i
5d520 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 nt tnum;
5d530 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 /* Page containi
5d540 6e 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 ng root of this
5d550 69 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 index in databas
5d560 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f e file */. u8 o
5d570 6e 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 nError; /*
5d580 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e OE_Abort, OE_Ign
5d590 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c ore, OE_Replace,
5d5a0 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 or OE_None */.
5d5b0 20 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 u8 autoIndex;
5d5c0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 /* True if is
5d5d0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 automatically cr
5d5e0 65 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e eated (ex: by UN
5d5f0 49 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 IQUE) */. char
5d600 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 *zColAff; /* S
5d610 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 tring defining t
5d620 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 he affinity of e
5d630 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 ach column */.
5d640 49 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 Index *pNext;
5d650 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 /* The next ind
5d660 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ex associated wi
5d670 74 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c th the same tabl
5d680 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 e */. Schema *p
5d690 53 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d Schema; /* Schem
5d6a0 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 a containing thi
5d6b0 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 s index */. u8
5d6c0 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a *aSortOrder; /*
5d6d0 20 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 Array of size I
5d6e0 6e 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 ndex.nColumn. Tr
5d6f0 75 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d ue==DESC, False=
5d700 3d 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a =ASC */. char *
5d710 2a 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 *azColl; /* Ar
5d720 72 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e ray of collation
5d730 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 sequence names
5d740 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a for index */.};.
5d750 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 ./*.** Each toke
5d760 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 n coming out of
5d770 74 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 the lexer is an
5d780 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 instance of.** t
5d790 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 his structure.
5d7a0 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 Tokens are also
5d7b0 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 used as part of
5d7c0 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a an expression..*
5d7d0 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b *.** Note if Tok
5d7e0 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b en.z==0 then Tok
5d7f0 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e en.dyn and Token
5d800 2e 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 .n are undefined
5d810 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 and.** may cont
5d820 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 ain random value
5d830 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 s. Do not make
5d840 61 6e 79 20 61 73 73 75 6d 70 74 69 6f 6e 73 20 any assumptions
5d850 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a about Token.dyn.
5d860 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 ** and Token.n w
5d870 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a hen Token.z==0..
5d880 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 */.struct Token
5d890 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e {. const unsign
5d8a0 65 64 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 ed char *z; /* T
5d8b0 65 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e ext of the token
5d8c0 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d . Not NULL-term
5d8d0 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 inated! */. uns
5d8e0 69 67 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 igned dyn : 1;
5d8f0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
5d900 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 malloced memory
5d910 2c 20 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 , false for stat
5d920 69 63 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ic */. unsigned
5d930 20 6e 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 n : 31;
5d940 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 /* Number of cha
5d950 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 racters in this
5d960 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a token */.};../*.
5d970 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
5d980 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
5d990 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d contains inform
5d9a0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 ation needed to
5d9b0 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 generate.** code
5d9c0 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 for a SELECT th
5d9d0 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 at contains aggr
5d9e0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e egate functions.
5d9f0 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f .**.** If Expr.o
5da00 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
5da10 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 or TK_AGG_FUNCT
5da20 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 ION then Expr.pA
5da30 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 ggInfo is a.** p
5da40 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 ointer to this s
5da50 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 tructure. The E
5da60 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c xpr.iColumn fiel
5da70 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 d is the index i
5da80 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f n.** AggInfo.aCo
5da90 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 l[] or AggInfo.a
5daa0 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d Func[] of inform
5dab0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 ation needed to
5dac0 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 generate.** code
5dad0 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a for that node..
5dae0 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 **.** AggInfo.pG
5daf0 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e roupBy and AggIn
5db00 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 fo.aFunc.pExpr p
5db10 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 oint to fields w
5db20 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 ithin the.** ori
5db30 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 ginal Select str
5db40 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 ucture that desc
5db50 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 ribes the SELECT
5db60 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 statement. The
5db70 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 se.** fields do
5db80 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 not need to be f
5db90 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f reed when deallo
5dba0 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e cating the AggIn
5dbb0 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f fo structure..*/
5dbc0 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 .struct AggInfo
5dbd0 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 {. u8 directMod
5dbe0 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 e; /* D
5dbf0 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 irect rendering
5dc00 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 mode means take
5dc10 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 data directly.
5dc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5dc30 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 ** from
5dc40 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 source tables ra
5dc50 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 ther than from a
5dc60 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 ccumulators */.
5dc70 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 u8 useSortingId
5dc80 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 x; /* In d
5dc90 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 irect mode, refe
5dca0 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e rence the sortin
5dcb0 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 g index rather.
5dcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5dcd0 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e ** than
5dce0 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c the source tabl
5dcf0 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 e */. int sorti
5dd00 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f ngIdx; /
5dd10 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 * Cursor number
5dd20 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 of the sorting i
5dd30 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 ndex */. ExprLi
5dd40 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 st *pGroupBy;
5dd50 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 /* The group b
5dd60 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e y clause */. in
5dd70 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e t nSortingColumn
5dd80 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ; /* Number
5dd90 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
5dda0 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 e sorting index
5ddb0 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 */. struct AggI
5ddc0 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 nfo_col { /*
5ddd0 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 For each column
5dde0 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 used in source t
5ddf0 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 ables */. Tab
5de00 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 le *pTab;
5de10 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
5de20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 table */. int
5de30 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 iTable;
5de40 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
5de50 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f number of the so
5de60 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 urce table */.
5de70 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 int iColumn;
5de80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
5de90 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 lumn number with
5dea0 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 in the source ta
5deb0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ble */. int i
5dec0 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 SorterColumn;
5ded0 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 /* Column nu
5dee0 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 mber in the sort
5def0 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 ing index */.
5df00 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 int iMem;
5df10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
5df20 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 ory location tha
5df30 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 t acts as accumu
5df40 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 lator */. Exp
5df50 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 r *pExpr;
5df60 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 /* The ori
5df70 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e ginal expression
5df80 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 */. } *aCol;.
5df90 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 int nColumn;
5dfa0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5dfb0 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 er of used entri
5dfc0 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a es in aCol[] */.
5dfd0 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c int nColumnAll
5dfe0 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d oc; /* Num
5dff0 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c ber of slots all
5e000 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b ocated for aCol[
5e010 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 ] */. int nAccu
5e020 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f mulator; /
5e030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
5e040 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 mns that show th
5e050 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 rough to the out
5e060 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 put..
5e070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
5e080 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c * Additional col
5e090 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e umns are used on
5e0a0 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 ly as parameters
5e0b0 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 to.
5e0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
5e0d0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
5e0e0 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 ions */. struct
5e0f0 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 AggInfo_func {
5e100 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 /* For each ag
5e110 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
5e120 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 */. Expr *pE
5e130 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 xpr;
5e140 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 /* Expression e
5e150 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 ncoding the func
5e160 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 tion */. Func
5e170 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 Def *pFunc;
5e180 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 /* The aggr
5e190 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 egate function i
5e1a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f mplementation */
5e1b0 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 . int iMem;
5e1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5e1d0 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e Memory location
5e1e0 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 that acts as ac
5e1f0 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 cumulator */.
5e200 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 int iDistinct;
5e210 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 /* Eph
5e220 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 emeral table use
5e230 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 d to enforce DIS
5e240 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 TINCT */. } *aF
5e250 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 unc;. int nFunc
5e260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
5e270 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
5e280 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a ies in aFunc[] *
5e290 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c /. int nFuncAll
5e2a0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e oc; /* N
5e2b0 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 umber of slots a
5e2c0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 llocated for aFu
5e2d0 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a nc[] */.};../*.*
5e2e0 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 * Each node of a
5e2f0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 n expression in
5e300 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 the parse tree i
5e310 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a s an instance.**
5e320 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
5e330 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f re..**.** Expr.o
5e340 70 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e p is the opcode.
5e350 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 The integer par
5e360 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20 ser token codes
5e370 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73 are reused.** as
5e380 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 46 opcodes here. F
5e390 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 or example, the
5e3a0 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 54 parser defines T
5e3b0 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 6e K_GE to be an in
5e3c0 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 65 teger.** code re
5e3d0 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22 presenting the "
5e3e0 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 >=" operator. Th
5e3f0 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20 is same integer
5e400 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 0a 2a code is reused.*
5e410 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 74 * to represent t
5e420 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 6e 2d he greater-than-
5e430 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 65 72 or-equal-to oper
5e440 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 70 72 ator in the expr
5e450 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a ession.** tree..
5e460 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 **.** If the exp
5e470 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 ression is an SQ
5e480 4c 20 6c 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e L literal (TK_IN
5e490 54 45 47 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c TEGER, TK_FLOAT,
5e4a0 20 54 4b 5f 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 TK_BLOB, .** or
5e4b0 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20 74 68 65 TK_STRING), the
5e4c0 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e n Expr.token con
5e4d0 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f tains the text o
5e4e0 66 20 74 68 65 20 53 51 4c 20 6c 69 74 65 72 61 f the SQL litera
5e4f0 6c 2e 20 49 66 0a 2a 2a 20 74 68 65 20 65 78 70 l. If.** the exp
5e500 72 65 73 73 69 6f 6e 20 69 73 20 61 20 76 61 72 ression is a var
5e510 69 61 62 6c 65 20 28 54 4b 5f 56 41 52 49 41 42 iable (TK_VARIAB
5e520 4c 45 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 LE), then Expr.t
5e530 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 oken contains th
5e540 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e e .** variable n
5e550 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c 20 69 66 ame. Finally, if
5e560 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
5e570 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 is an SQL functi
5e580 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 on (TK_FUNCTION)
5e590 2c 0a 2a 2a 20 74 68 65 6e 20 45 78 70 72 2e 74 ,.** then Expr.t
5e5a0 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 oken contains th
5e5b0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 e name of the fu
5e5c0 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 nction..**.** Ex
5e5d0 70 72 2e 70 52 69 67 68 74 20 61 6e 64 20 45 78 pr.pRight and Ex
5e5e0 70 72 2e 70 4c 65 66 74 20 61 72 65 20 74 68 65 pr.pLeft are the
5e5f0 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 left and right
5e600 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 6f subexpressions o
5e610 66 20 61 0a 2a 2a 20 62 69 6e 61 72 79 20 6f 70 f a.** binary op
5e620 65 72 61 74 6f 72 2e 20 45 69 74 68 65 72 20 6f erator. Either o
5e630 72 20 62 6f 74 68 20 6d 61 79 20 62 65 20 4e 55 r both may be NU
5e640 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 LL..**.** Expr.x
5e650 2e 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 .pList is a list
5e660 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 69 66 of arguments if
5e670 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
5e680 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 is an SQL functi
5e690 6f 6e 2c 0a 2a 2a 20 61 20 43 41 53 45 20 65 78 on,.** a CASE ex
5e6a0 70 72 65 73 73 69 6f 6e 20 6f 72 20 61 6e 20 49 pression or an I
5e6b0 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 N expression of
5e6c0 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 the form "<lhs>
5e6d0 49 4e 20 28 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 IN (<y>, <z>...)
5e6e0 22 2e 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 53 65 "..** Expr.x.pSe
5e6f0 6c 65 63 74 20 69 73 20 75 73 65 64 20 69 66 20 lect is used if
5e700 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
5e710 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f s a sub-select o
5e720 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 r an expression
5e730 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22 of.** the form "
5e740 3c 6c 68 73 3e 20 49 4e 20 28 53 45 4c 45 43 54 <lhs> IN (SELECT
5e750 20 2e 2e 2e 29 22 2e 20 49 66 20 74 68 65 20 45 ...)". If the E
5e760 50 5f 78 49 73 53 65 6c 65 63 74 20 62 69 74 20 P_xIsSelect bit
5e770 69 73 20 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a is set in the.**
5e780 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b Expr.flags mask
5e790 2c 20 74 68 65 6e 20 45 78 70 72 2e 78 2e 70 53 , then Expr.x.pS
5e7a0 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64 2e 20 elect is valid.
5e7b0 4f 74 68 65 72 77 69 73 65 2c 20 45 78 70 72 2e Otherwise, Expr.
5e7c0 78 2e 70 4c 69 73 74 20 69 73 20 0a 2a 2a 20 76 x.pList is .** v
5e7d0 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 alid..**.** An e
5e7e0 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 xpression of the
5e7f0 20 66 6f 72 6d 20 49 44 20 6f 72 20 49 44 2e 49 form ID or ID.I
5e800 44 20 72 65 66 65 72 73 20 74 6f 20 61 20 63 6f D refers to a co
5e810 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e lumn in a table.
5e820 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20 65 78 70 .** For such exp
5e830 72 65 73 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f ressions, Expr.o
5e840 70 20 69 73 20 73 65 74 20 74 6f 20 54 4b 5f 43 p is set to TK_C
5e850 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69 OLUMN and Expr.i
5e860 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 Table is.** the
5e870 69 6e 74 65 67 65 72 20 63 75 72 73 6f 72 20 6e integer cursor n
5e880 75 6d 62 65 72 20 6f 66 20 61 20 56 44 42 45 20 umber of a VDBE
5e890 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 cursor pointing
5e8a0 74 6f 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e to that table an
5e8b0 64 0a 2a 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d d.** Expr.iColum
5e8c0 6e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 n is the column
5e8d0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73 number for the s
5e8e0 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 pecific column.
5e8f0 20 49 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 If the.** expre
5e900 73 73 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 ssion is used as
5e910 20 61 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 a result in an
5e920 61 67 67 72 65 67 61 74 65 20 53 45 4c 45 43 54 aggregate SELECT
5e930 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 , then the.** va
5e940 6c 75 65 20 69 73 20 61 6c 73 6f 20 73 74 6f 72 lue is also stor
5e950 65 64 20 69 6e 20 74 68 65 20 45 78 70 72 2e 69 ed in the Expr.i
5e960 41 67 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 Agg column in th
5e970 65 20 61 67 67 72 65 67 61 74 65 20 73 6f 20 74 e aggregate so t
5e980 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65 hat.** it can be
5e990 20 61 63 63 65 73 73 65 64 20 61 66 74 65 72 20 accessed after
5e9a0 61 6c 6c 20 61 67 67 72 65 67 61 74 65 73 20 61 all aggregates a
5e9b0 72 65 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a re computed..**.
5e9c0 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 ** If the expres
5e9d0 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 sion is an unbou
5e9e0 6e 64 20 76 61 72 69 61 62 6c 65 20 6d 61 72 6b nd variable mark
5e9f0 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e 20 6d er (a question m
5ea00 61 72 6b 20 0a 2a 2a 20 63 68 61 72 61 63 74 65 ark .** characte
5ea10 72 20 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 r '?' in the ori
5ea20 67 69 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 ginal SQL) then
5ea30 74 68 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 the Expr.iTable
5ea40 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 20 holds the index
5ea50 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 .** number for t
5ea60 68 61 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a hat variable..**
5ea70 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 .** If the expre
5ea80 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 71 75 ssion is a subqu
5ea90 65 72 79 20 74 68 65 6e 20 45 78 70 72 2e 69 43 ery then Expr.iC
5eaa0 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 olumn holds an i
5eab0 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 nteger.** regist
5eac0 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 er number contai
5ead0 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20 ning the result
5eae0 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e of the subquery.
5eaf0 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 If the.** subq
5eb00 75 65 72 79 20 67 69 76 65 73 20 61 20 63 6f 6e uery gives a con
5eb10 73 74 61 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 stant result, th
5eb20 65 6e 20 69 54 61 62 6c 65 20 69 73 20 2d 31 2e en iTable is -1.
5eb30 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72 If the subquer
5eb40 79 0a 2a 2a 20 67 69 76 65 73 20 61 20 64 69 66 y.** gives a dif
5eb50 66 65 72 65 6e 74 20 61 6e 73 77 65 72 20 61 74 ferent answer at
5eb60 20 64 69 66 66 65 72 65 6e 74 20 74 69 6d 65 73 different times
5eb70 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e during statemen
5eb80 74 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 t processing.**
5eb90 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 74 then iTable is t
5eba0 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20 he address of a
5ebb0 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 subroutine that
5ebc0 63 6f 6d 70 75 74 65 73 20 74 68 65 20 73 75 62 computes the sub
5ebd0 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 query..**.** If
5ebe0 74 68 65 20 45 78 70 72 20 69 73 20 6f 66 20 74 the Expr is of t
5ebf0 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 ype OP_Column, a
5ec00 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 74 20 nd the table it
5ec10 69 73 20 73 65 6c 65 63 74 69 6e 67 20 66 72 6f is selecting fro
5ec20 6d 0a 2a 2a 20 69 73 20 61 20 64 69 73 6b 20 74 m.** is a disk t
5ec30 61 62 6c 65 20 6f 72 20 74 68 65 20 22 6f 6c 64 able or the "old
5ec40 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 .*" pseudo-table
5ec50 2c 20 74 68 65 6e 20 70 54 61 62 20 70 6f 69 6e , then pTab poin
5ec60 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 ts to the.** cor
5ec70 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 responding table
5ec80 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a definition..**.
5ec90 2a 2a 20 41 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f ** ALLOCATION NO
5eca0 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 TES:.**.** Expr
5ecb0 73 74 72 75 63 74 75 72 65 73 20 6d 61 79 20 62 structures may b
5ecc0 65 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 e stored as part
5ecd0 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 of the in-memor
5ece0 79 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d y database schem
5ecf0 61 2c 0a 2a 2a 20 66 6f 72 20 65 78 61 6d 70 6c a,.** for exampl
5ed00 65 20 61 73 20 70 61 72 74 20 6f 66 20 74 72 69 e as part of tri
5ed10 67 67 65 72 2c 20 76 69 65 77 20 6f 72 20 74 61 gger, view or ta
5ed20 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e ble definitions.
5ed30 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a In this case,.*
5ed40 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 * the amount of
5ed50 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 memory consumed
5ed60 62 79 20 63 6f 6d 70 6c 65 78 20 65 78 70 72 65 by complex expre
5ed70 73 73 69 6f 6e 73 20 6d 61 79 20 62 65 20 73 69 ssions may be si
5ed80 67 6e 69 66 69 63 61 6e 74 2e 0a 2a 2a 20 46 6f gnificant..** Fo
5ed90 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 6c r this reason, l
5eda0 65 73 73 20 74 68 61 6e 20 73 69 7a 65 6f 66 28 ess than sizeof(
5edb0 45 78 70 72 29 20 62 79 74 65 73 20 6d 61 79 20 Expr) bytes may
5edc0 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 be allocated for
5edd0 20 73 6f 6d 65 20 0a 2a 2a 20 45 78 70 72 20 73 some .** Expr s
5ede0 74 72 75 63 74 73 20 73 74 6f 72 65 64 20 61 73 tructs stored as
5edf0 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 2d part of the in-
5ee00 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 memory database
5ee10 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 schema..**.** If
5ee20 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 the EP_Reduced
5ee30 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 45 flag is set in E
5ee40 78 70 72 2e 66 6c 61 67 73 2c 20 74 68 65 6e 20 xpr.flags, then
5ee50 6f 6e 6c 79 20 45 58 50 52 5f 52 45 44 55 43 45 only EXPR_REDUCE
5ee60 44 53 49 5a 45 0a 2a 2a 20 62 79 74 65 73 20 6f DSIZE.** bytes o
5ee70 66 20 73 70 61 63 65 20 61 72 65 20 61 6c 6c 6f f space are allo
5ee80 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 65 78 cated for the ex
5ee90 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 pression structu
5eea0 72 65 2e 20 54 68 69 73 20 69 73 20 65 6e 6f 75 re. This is enou
5eeb0 67 68 0a 2a 2a 20 73 70 61 63 65 20 74 6f 20 73 gh.** space to s
5eec0 74 6f 72 65 20 61 6c 6c 20 66 69 65 6c 64 73 20 tore all fields
5eed0 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 up to and includ
5eee0 69 6e 67 20 74 68 65 20 22 54 6f 6b 65 6e 20 73 ing the "Token s
5eef0 70 61 6e 3b 22 20 66 69 65 6c 64 2e 0a 2a 2a 0a pan;" field..**.
5ef00 2a 2a 20 49 66 20 74 68 65 20 45 50 5f 54 6f 6b ** If the EP_Tok
5ef10 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 73 enOnly flag is s
5ef20 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 73 et in Expr.flags
5ef30 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 45 58 50 52 , then only EXPR
5ef40 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 0a 2a _TOKENONLYSIZE.*
5ef50 2a 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 * bytes of space
5ef60 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 are allocated f
5ef70 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f or the expressio
5ef80 6e 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 69 n structure. Thi
5ef90 73 20 69 73 20 65 6e 6f 75 67 68 0a 2a 2a 20 73 s is enough.** s
5efa0 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 61 6c pace to store al
5efb0 6c 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 61 l fields up to a
5efc0 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 nd including the
5efd0 20 22 54 6f 6b 65 6e 20 74 6f 6b 65 6e 3b 22 20 "Token token;"
5efe0 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 field..*/.struct
5eff0 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b Expr {. u8 op;
5f000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5f010 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65 /* Operation pe
5f020 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20 rformed by this
5f030 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 node */. char a
5f040 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 ffinity;
5f050 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 /* The affinity
5f060 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f of the column o
5f070 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c r 0 if not a col
5f080 75 6d 6e 20 2a 2f 0a 20 20 56 56 41 5f 4f 4e 4c umn */. VVA_ONL
5f090 59 28 75 38 20 76 76 61 46 6c 61 67 73 3b 29 20 Y(u8 vvaFlags;)
5f0a0 2f 2a 20 46 6c 61 67 73 20 75 73 65 64 20 66 6f /* Flags used fo
5f0b0 72 20 56 56 26 41 20 6f 6e 6c 79 2e 20 20 45 56 r VV&A only. EV
5f0c0 56 41 5f 2a 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20 VA_* below. */.
5f0d0 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 u16 flags;
5f0e0 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f /* Vario
5f0f0 75 73 20 66 6c 61 67 73 2e 20 20 45 50 5f 2a 20 us flags. EP_*
5f100 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 54 See below */. T
5f110 6f 6b 65 6e 20 74 6f 6b 65 6e 3b 20 20 20 20 20 oken token;
5f120 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 72 /* An oper
5f130 61 6e 64 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 and token */..
5f140 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 54 6f 6b /* If the EP_Tok
5f150 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 73 enOnly flag is s
5f160 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 et in the Expr.f
5f170 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 lags mask, then
5f180 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73 no. ** space is
5f190 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 allocated for t
5f1a0 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20 he fields below
5f1b0 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61 this point. An a
5f1c0 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61 ttempt to. ** a
5f1d0 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20 ccess them will
5f1e0 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66 result in a segf
5f1f0 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74 ault or malfunct
5f200 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a ion. . ********
5f210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
5f250 20 20 54 6f 6b 65 6e 20 73 70 61 6e 3b 20 20 20 Token span;
5f260 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 /* Comp
5f270 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 lete text of the
5f280 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 0a expression */..
5f290 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 53 /* If the EP_S
5f2a0 70 61 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 panOnly flag is
5f2b0 73 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e set in the Expr.
5f2c0 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e flags mask, then
5f2d0 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 no. ** space i
5f2e0 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
5f2f0 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 the fields below
5f300 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 this point. An
5f310 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 attempt to. **
5f320 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c access them will
5f330 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 result in a seg
5f340 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 fault or malfunc
5f350 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a tion. . *******
5f360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
5f3a0 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 . Expr *pLeft;
5f3b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 /* Lef
5f3c0 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 45 t subnode */. E
5f3d0 78 70 72 20 2a 70 52 69 67 68 74 3b 20 20 20 20 xpr *pRight;
5f3e0 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 73 /* Right s
5f3f0 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 75 6e 69 6f ubnode */. unio
5f400 6e 20 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 n {. ExprList
5f410 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 *pList; /*
5f420 46 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e Function argumen
5f430 74 73 20 6f 72 20 69 6e 20 22 3c 65 78 70 72 3e ts or in "<expr>
5f440 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73 74 29 IN (<expr-list)
5f450 22 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 " */. Select
5f460 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a *pSelect; /*
5f470 20 55 73 65 64 20 66 6f 72 20 73 75 62 2d 73 65 Used for sub-se
5f480 6c 65 63 74 73 20 61 6e 64 20 22 3c 65 78 70 72 lects and "<expr
5f490 3e 20 49 4e 20 28 3c 73 65 6c 65 63 74 3e 29 22 > IN (<select>)"
5f4a0 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20 43 6f 6c */. } x;. Col
5f4b0 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 lSeq *pColl;
5f4c0 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 /* The colla
5f4d0 74 69 6f 6e 20 74 79 70 65 20 6f 66 20 74 68 65 tion type of the
5f4e0 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 2a 2f 0a column or 0 */.
5f4f0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f . /* If the EP_
5f500 52 65 64 75 63 65 64 20 66 6c 61 67 20 69 73 20 Reduced flag is
5f510 73 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e set in the Expr.
5f520 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e flags mask, then
5f530 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 no. ** space i
5f540 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
5f550 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 the fields below
5f560 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 this point. An
5f570 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 attempt to. **
5f580 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c access them will
5f590 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 result in a seg
5f5a0 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 fault or malfunc
5f5b0 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a tion.. ********
5f5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
5f600 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 43 int iTable, iC
5f610 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 65 6e olumn; /* When
5f620 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 op==TK_COLUMN,
5f630 74 68 65 6e 20 74 68 69 73 20 65 78 70 72 20 6e then this expr n
5f640 6f 64 65 20 6d 65 61 6e 73 20 74 68 65 0a 20 20 ode means the.
5f650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5f660 20 20 20 20 20 20 20 2a 2a 20 69 43 6f 6c 75 6d ** iColum
5f670 6e 2d 74 68 20 66 69 65 6c 64 20 6f 66 20 74 68 n-th field of th
5f680 65 20 69 54 61 62 6c 65 2d 74 68 20 74 61 62 6c e iTable-th tabl
5f690 65 2e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 e. */. AggInfo
5f6a0 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 20 20 2f *pAggInfo; /
5f6b0 2a 20 55 73 65 64 20 62 79 20 54 4b 5f 41 47 47 * Used by TK_AGG
5f6c0 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 4b 5f 41 _COLUMN and TK_A
5f6d0 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a 2f 0a 20 GG_FUNCTION */.
5f6e0 20 69 6e 74 20 69 41 67 67 3b 20 20 20 20 20 20 int iAgg;
5f6f0 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 /* Which
5f700 20 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e entry in pAggIn
5f710 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 6f 72 20 2d 3e fo->aCol[] or ->
5f720 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 69 6e 74 aFunc[] */. int
5f730 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 iRightJoinTable
5f740 3b 20 20 20 2f 2a 20 49 66 20 45 50 5f 46 72 6f ; /* If EP_Fro
5f750 6d 4a 6f 69 6e 2c 20 74 68 65 20 72 69 67 68 74 mJoin, the right
5f760 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f table of the jo
5f770 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 in */. Table *p
5f780 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f Tab; /
5f790 2a 20 54 61 62 6c 65 20 66 6f 72 20 54 4b 5f 43 * Table for TK_C
5f7a0 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e OLUMN expression
5f7b0 73 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s. */.#if SQLITE
5f7c0 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e _MAX_EXPR_DEPTH>
5f7d0 30 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 3b 0. int nHeight;
5f7e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 /* He
5f7f0 69 67 68 74 20 6f 66 20 74 68 65 20 74 72 65 65 ight of the tree
5f800 20 68 65 61 64 65 64 20 62 79 20 74 68 69 73 20 headed by this
5f810 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d node */.#endif.}
5f820 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c ;../*.** The fol
5f830 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20 6d lowing are the m
5f840 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 20 eanings of bits
5f850 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 in the Expr.flag
5f860 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 s field..*/.#def
5f870 69 6e 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 ine EP_FromJoin
5f880 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 72 69 0x0001 /* Ori
5f890 67 69 6e 61 74 65 64 20 69 6e 20 4f 4e 20 6f 72 ginated in ON or
5f8a0 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 USING clause of
5f8b0 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 a join */.#defi
5f8c0 6e 65 20 45 50 5f 41 67 67 20 20 20 20 20 20 20 ne EP_Agg
5f8d0 20 30 78 30 30 30 32 20 20 2f 2a 20 43 6f 6e 74 0x0002 /* Cont
5f8e0 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 ains one or more
5f8f0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
5f900 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ions */.#define
5f910 45 50 5f 52 65 73 6f 6c 76 65 64 20 20 20 30 78 EP_Resolved 0x
5f920 30 30 30 34 20 20 2f 2a 20 49 44 73 20 68 61 76 0004 /* IDs hav
5f930 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 e been resolved
5f940 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 64 to COLUMNs */.#d
5f950 65 66 69 6e 65 20 45 50 5f 45 72 72 6f 72 20 20 efine EP_Error
5f960 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 45 0x0008 /* E
5f970 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 xpression contai
5f980 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 65 ns one or more e
5f990 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 rrors */.#define
5f9a0 20 45 50 5f 44 69 73 74 69 6e 63 74 20 20 20 30 EP_Distinct 0
5f9b0 78 30 30 31 30 20 20 2f 2a 20 41 67 67 72 65 67 x0010 /* Aggreg
5f9c0 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 ate function wit
5f9d0 68 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f h DISTINCT keywo
5f9e0 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 rd */.#define EP
5f9f0 5f 56 61 72 53 65 6c 65 63 74 20 20 30 78 30 30 _VarSelect 0x00
5fa00 32 30 20 20 2f 2a 20 70 53 65 6c 65 63 74 20 69 20 /* pSelect i
5fa10 73 20 63 6f 72 72 65 6c 61 74 65 64 2c 20 6e 6f s correlated, no
5fa20 74 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 23 64 t constant */.#d
5fa30 65 66 69 6e 65 20 45 50 5f 44 65 71 75 6f 74 65 efine EP_Dequote
5fa40 64 20 20 20 30 78 30 30 34 30 20 20 2f 2a 20 54 d 0x0040 /* T
5fa50 72 75 65 20 69 66 20 74 68 65 20 73 74 72 69 6e rue if the strin
5fa60 67 20 68 61 73 20 62 65 65 6e 20 64 65 71 75 6f g has been dequo
5fa70 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ted */.#define E
5fa80 50 5f 49 6e 66 69 78 46 75 6e 63 20 20 30 78 30 P_InfixFunc 0x0
5fa90 30 38 30 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 080 /* True for
5faa0 20 61 6e 20 69 6e 66 69 78 20 66 75 6e 63 74 69 an infix functi
5fab0 6f 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 on: LIKE, GLOB,
5fac0 65 74 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 etc */.#define E
5fad0 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 30 78 30 P_ExpCollate 0x0
5fae0 31 30 30 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 100 /* Collatin
5faf0 67 20 73 65 71 75 65 6e 63 65 20 73 70 65 63 69 g sequence speci
5fb00 66 69 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 fied explicitly
5fb10 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 6e */.#define EP_An
5fb20 79 41 66 66 20 20 20 20 20 30 78 30 32 30 30 20 yAff 0x0200
5fb30 20 2f 2a 20 43 61 6e 20 74 61 6b 65 20 61 20 63 /* Can take a c
5fb40 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 ached column of
5fb50 61 6e 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a any affinity */.
5fb60 23 64 65 66 69 6e 65 20 45 50 5f 46 69 78 65 64 #define EP_Fixed
5fb70 44 65 73 74 20 20 30 78 30 34 30 30 20 20 2f 2a Dest 0x0400 /*
5fb80 20 52 65 73 75 6c 74 20 6e 65 65 64 65 64 20 69 Result needed i
5fb90 6e 20 61 20 73 70 65 63 69 66 69 63 20 72 65 67 n a specific reg
5fba0 69 73 74 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 ister */.#define
5fbb0 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 20 20 30 EP_IntValue 0
5fbc0 78 30 38 30 30 20 20 2f 2a 20 49 6e 74 65 67 65 x0800 /* Intege
5fbd0 72 20 76 61 6c 75 65 20 63 6f 6e 74 61 69 6e 65 r value containe
5fbe0 64 20 69 6e 20 69 54 61 62 6c 65 20 2a 2f 0a 23 d in iTable */.#
5fbf0 64 65 66 69 6e 65 20 45 50 5f 78 49 73 53 65 6c define EP_xIsSel
5fc00 65 63 74 20 20 30 78 31 30 30 30 20 20 2f 2a 20 ect 0x1000 /*
5fc10 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76 61 6c x.pSelect is val
5fc20 69 64 20 28 6f 74 68 65 72 77 69 73 65 20 78 2e id (otherwise x.
5fc30 70 4c 69 73 74 20 69 73 29 20 2a 2f 0a 0a 23 64 pList is) */..#d
5fc40 65 66 69 6e 65 20 45 50 5f 52 65 64 75 63 65 64 efine EP_Reduced
5fc50 20 20 20 20 30 78 32 30 30 30 20 20 2f 2a 20 45 0x2000 /* E
5fc60 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45 58 xpr struct is EX
5fc70 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 62 PR_REDUCEDSIZE b
5fc80 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 ytes only */.#de
5fc90 66 69 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c fine EP_TokenOnl
5fca0 79 20 20 30 78 34 30 30 30 20 20 2f 2a 20 45 78 y 0x4000 /* Ex
5fcb0 70 72 20 73 74 72 75 63 74 20 69 73 20 45 58 50 pr struct is EXP
5fcc0 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 R_TOKENONLYSIZE
5fcd0 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 bytes only */.#d
5fce0 65 66 69 6e 65 20 45 50 5f 53 70 61 6e 4f 6e 6c efine EP_SpanOnl
5fcf0 79 20 20 20 30 78 38 30 30 30 20 20 2f 2a 20 45 y 0x8000 /* E
5fd00 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45 58 xpr struct is EX
5fd10 50 52 5f 53 50 41 4e 4f 4e 4c 59 53 49 5a 45 20 PR_SPANONLYSIZE
5fd20 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f bytes only */../
5fd30 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
5fd40 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 ng are the meani
5fd50 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 ngs of bits in t
5fd60 68 65 20 45 78 70 72 2e 76 76 61 46 6c 61 67 73 he Expr.vvaFlags
5fd70 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 69 73 20 field..** This
5fd80 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6f information is o
5fd90 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 53 51 nly used when SQ
5fda0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
5fdb0 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f with.** SQLITE_
5fdc0 44 45 42 55 47 20 64 65 66 69 6e 65 64 2e 0a 2a DEBUG defined..*
5fdd0 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 /.#ifndef NDEBUG
5fde0 0a 23 64 65 66 69 6e 65 20 45 56 56 41 5f 52 65 .#define EVVA_Re
5fdf0 61 64 4f 6e 6c 79 54 6f 6b 65 6e 20 20 30 78 30 adOnlyToken 0x0
5fe00 31 20 20 2f 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 1 /* Expr.token
5fe10 2e 7a 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 20 .z is read-only
5fe20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a */.#endif../*.**
5fe30 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 These macros ca
5fe40 6e 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 n be used to tes
5fe50 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 t, set, or clear
5fe60 20 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a bits in the .**
5fe70 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65 6c Expr.flags fiel
5fe80 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 78 d..*/.#define Ex
5fe90 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 45 2c prHasProperty(E,
5fea0 50 29 20 20 20 20 20 28 28 28 45 29 2d 3e 66 6c P) (((E)->fl
5feb0 61 67 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 ags&(P))==(P)).#
5fec0 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 41 6e define ExprHasAn
5fed0 79 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 yProperty(E,P)
5fee0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 (((E)->flags&(P)
5fef0 29 21 3d 30 29 0a 23 64 65 66 69 6e 65 20 45 78 )!=0).#define Ex
5ff00 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 45 2c prSetProperty(E,
5ff10 50 29 20 20 20 20 20 28 45 29 2d 3e 66 6c 61 67 P) (E)->flag
5ff20 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 45 s|=(P).#define E
5ff30 78 70 72 43 6c 65 61 72 50 72 6f 70 65 72 74 79 xprClearProperty
5ff40 28 45 2c 50 29 20 20 20 28 45 29 2d 3e 66 6c 61 (E,P) (E)->fla
5ff50 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 gs&=~(P)../*.**
5ff60 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d Macros to determ
5ff70 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ine the number o
5ff80 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
5ff90 20 62 79 20 61 20 6e 6f 72 6d 61 6c 20 45 78 70 by a normal Exp
5ffa0 72 20 0a 2a 2a 20 73 74 72 75 63 74 2c 20 61 6e r .** struct, an
5ffb0 20 45 78 70 72 20 73 74 72 75 63 74 20 77 69 74 Expr struct wit
5ffc0 68 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 h the EP_Reduced
5ffd0 20 66 6c 61 67 20 73 65 74 20 69 6e 20 45 78 70 flag set in Exp
5ffe0 72 2e 66 6c 61 67 73 20 0a 2a 2a 20 61 6e 64 20 r.flags .** and
5fff0 61 6e 20 45 78 70 72 20 73 74 72 75 63 74 20 77 an Expr struct w
60000 69 74 68 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e ith the EP_Token
60010 4f 6e 6c 79 20 66 6c 61 67 20 73 65 74 2e 0a 2a Only flag set..*
60020 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 46 /.#define EXPR_F
60030 55 4c 4c 53 49 5a 45 20 20 20 20 20 20 20 20 20 ULLSIZE
60040 20 20 73 69 7a 65 6f 66 28 45 78 70 72 29 0a 23 sizeof(Expr).#
60050 64 65 66 69 6e 65 20 45 58 50 52 5f 52 45 44 55 define EXPR_REDU
60060 43 45 44 53 49 5a 45 20 20 20 20 20 20 20 20 6f CEDSIZE o
60070 66 66 73 65 74 6f 66 28 45 78 70 72 2c 69 54 61 ffsetof(Expr,iTa
60080 62 6c 65 29 0a 23 64 65 66 69 6e 65 20 45 58 50 ble).#define EXP
60090 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 R_TOKENONLYSIZE
600a0 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 offsetof(Ex
600b0 70 72 2c 73 70 61 6e 29 0a 23 64 65 66 69 6e 65 pr,span).#define
600c0 20 45 58 50 52 5f 53 50 41 4e 4f 4e 4c 59 53 49 EXPR_SPANONLYSI
600d0 5a 45 20 20 20 20 20 20 20 6f 66 66 73 65 74 6f ZE offseto
600e0 66 28 45 78 70 72 2c 70 4c 65 66 74 29 0a 0a 2f f(Expr,pLeft)../
600f0 2a 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 *.** Flags passe
60100 64 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 d to the sqlite3
60110 45 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 ExprDup() functi
60120 6f 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 on. See the head
60130 65 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 er comment .** a
60140 62 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 bove sqlite3Expr
60150 44 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c Dup() for detail
60160 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 s..*/.#define EX
60170 50 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 PRDUP_REDUCE
60180 20 20 20 20 20 30 78 30 30 30 31 0a 23 64 65 66 0x0001.#def
60190 69 6e 65 20 45 58 50 52 44 55 50 5f 53 50 41 4e ine EXPRDUP_SPAN
601a0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 0x000
601b0 32 0a 23 64 65 66 69 6e 65 20 45 58 50 52 44 55 2.#define EXPRDU
601c0 50 5f 44 49 53 54 49 4e 43 54 53 50 41 4e 20 20 P_DISTINCTSPAN
601d0 20 30 78 30 30 30 34 0a 0a 2f 2a 0a 2a 2a 20 41 0x0004../*.** A
601e0 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
601f0 69 6f 6e 73 2e 20 20 45 61 63 68 20 65 78 70 72 ions. Each expr
60200 65 73 73 69 6f 6e 20 6d 61 79 20 6f 70 74 69 6f ession may optio
60210 6e 61 6c 6c 79 20 68 61 76 65 20 61 0a 2a 2a 20 nally have a.**
60220 6e 61 6d 65 2e 20 20 41 6e 20 65 78 70 72 2f 6e name. An expr/n
60230 61 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 ame combination
60240 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 73 can be used in s
60250 65 76 65 72 61 6c 20 77 61 79 73 2c 20 73 75 63 everal ways, suc
60260 68 0a 2a 2a 20 61 73 20 74 68 65 20 6c 69 73 74 h.** as the list
60270 20 6f 66 20 22 65 78 70 72 20 41 53 20 49 44 22 of "expr AS ID"
60280 20 66 69 65 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e fields followin
60290 67 20 61 20 22 53 45 4c 45 43 54 22 20 6f 72 20 g a "SELECT" or
602a0 69 6e 20 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f in the.** list o
602b0 66 20 22 49 44 20 3d 20 65 78 70 72 22 20 69 74 f "ID = expr" it
602c0 65 6d 73 20 69 6e 20 61 6e 20 55 50 44 41 54 45 ems in an UPDATE
602d0 2e 20 20 41 20 6c 69 73 74 20 6f 66 20 65 78 70 . A list of exp
602e0 72 65 73 73 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20 ressions can.**
602f0 61 6c 73 6f 20 62 65 20 75 73 65 64 20 61 73 20 also be used as
60300 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 the argument to
60310 61 20 66 75 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 a function, in w
60320 68 69 63 68 20 63 61 73 65 20 74 68 65 20 61 2e hich case the a.
60330 7a 4e 61 6d 65 0a 2a 2a 20 66 69 65 6c 64 20 69 zName.** field i
60340 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 s not used..*/.s
60350 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 20 7b truct ExprList {
60360 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 . int nExpr;
60370 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
60380 62 65 72 20 6f 66 20 65 78 70 72 65 73 73 69 6f ber of expressio
60390 6e 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a ns on the list *
603a0 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 /. int nAlloc;
603b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
603c0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
603d0 61 6c 6c 6f 63 61 74 65 64 20 62 65 6c 6f 77 20 allocated below
603e0 2a 2f 0a 20 20 69 6e 74 20 69 45 43 75 72 73 6f */. int iECurso
603f0 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 r; /* V
60400 44 42 45 20 43 75 72 73 6f 72 20 61 73 73 6f 63 DBE Cursor assoc
60410 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 iated with this
60420 45 78 70 72 4c 69 73 74 20 2a 2f 0a 20 20 73 74 ExprList */. st
60430 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
60440 65 6d 20 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 em {. Expr *p
60450 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
60460 2f 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20 65 /* The list of e
60470 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 xpressions */.
60480 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
60490 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 /* Toke
604a0 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 n associated wit
604b0 68 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f h this expressio
604c0 6e 20 2a 2f 0a 20 20 20 20 75 38 20 73 6f 72 74 n */. u8 sort
604d0 4f 72 64 65 72 3b 20 20 20 20 20 20 20 20 20 20 Order;
604e0 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 20 6f 72 /* 1 for DESC or
604f0 20 30 20 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20 0 for ASC */.
60500 20 20 75 38 20 64 6f 6e 65 3b 20 20 20 20 20 20 u8 done;
60510 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 66 6c /* A fl
60520 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77 ag to indicate w
60530 68 65 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69 hen processing i
60540 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 s finished */.
60550 20 20 75 31 36 20 69 43 6f 6c 3b 20 20 20 20 20 u16 iCol;
60560 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
60570 4f 52 44 45 52 20 42 59 2c 20 63 6f 6c 75 6d 6e ORDER BY, column
60580 20 6e 75 6d 62 65 72 20 69 6e 20 72 65 73 75 6c number in resul
60590 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 75 31 36 t set */. u16
605a0 20 69 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 iAlias;
605b0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 /* Index int
605c0 6f 20 50 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d o Parse.aAlias[]
605d0 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 for zName */.
605e0 7d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20 } *a;
605f0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e /* One en
60600 74 72 79 20 66 6f 72 20 65 61 63 68 20 65 78 70 try for each exp
60610 72 65 73 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f ression */.};../
60620 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
60630 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
60640 72 65 20 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 re can hold a si
60650 6d 70 6c 65 20 6c 69 73 74 20 6f 66 20 69 64 65 mple list of ide
60660 6e 74 69 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63 ntifiers,.** suc
60670 68 20 61 73 20 74 68 65 20 6c 69 73 74 20 22 61 h as the list "a
60680 2c 62 2c 63 22 20 69 6e 20 74 68 65 20 66 6f 6c ,b,c" in the fol
60690 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 lowing statement
606a0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e s:.**.** IN
606b0 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c SERT INTO t(a,b,
606c0 63 29 20 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a c) VALUES ...;.*
606d0 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 49 4e * CREATE IN
606e0 44 45 58 20 69 64 78 20 4f 4e 20 74 28 61 2c 62 DEX idx ON t(a,b
606f0 2c 63 29 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 ,c);.** CRE
60700 41 54 45 20 54 52 49 47 47 45 52 20 74 72 69 67 ATE TRIGGER trig
60710 20 42 45 46 4f 52 45 20 55 50 44 41 54 45 20 4f BEFORE UPDATE O
60720 4e 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a N t(a,b,c) ...;.
60730 2a 2a 0a 2a 2a 20 54 68 65 20 49 64 4c 69 73 74 **.** The IdList
60740 2e 61 2e 69 64 78 20 66 69 65 6c 64 20 69 73 20 .a.idx field is
60750 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 49 64 used when the Id
60760 4c 69 73 74 20 72 65 70 72 65 73 65 6e 74 73 20 List represents
60770 74 68 65 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 the list of.** c
60780 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65 olumn names afte
60790 72 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 r a table name i
607a0 6e 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 n an INSERT stat
607b0 65 6d 65 6e 74 2e 20 20 49 6e 20 74 68 65 20 73 ement. In the s
607c0 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 tatement.**.**
607d0 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 INSERT INTO t
607e0 28 61 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a (a,b,c) ....**.*
607f0 2a 20 49 66 20 22 61 22 20 69 73 20 74 68 65 20 * If "a" is the
60800 6b 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 k-th column of t
60810 61 62 6c 65 20 22 74 22 2c 20 74 68 65 6e 20 49 able "t", then I
60820 64 4c 69 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d dList.a[0].idx==
60830 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c k..*/.struct IdL
60840 69 73 74 20 7b 0a 20 20 73 74 72 75 63 74 20 49 ist {. struct I
60850 64 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 dList_item {.
60860 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
60870 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
60880 65 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a e identifier */.
60890 20 20 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 int idx;
608a0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
608b0 6e 20 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f n some Table.aCo
608c0 6c 5b 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 l[] of a column
608d0 6e 61 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 named zName */.
608e0 20 7d 20 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64 } *a;. int nId
608f0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
60900 62 65 72 20 6f 66 20 69 64 65 6e 74 69 66 69 65 ber of identifie
60910 72 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a rs on the list *
60920 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 /. int nAlloc;
60930 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
60940 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 f entries alloca
60950 74 65 64 20 66 6f 72 20 61 5b 5d 20 62 65 6c 6f ted for a[] belo
60960 77 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 w */.};../*.** T
60970 68 65 20 62 69 74 6d 61 73 6b 20 64 61 74 61 74 he bitmask datat
60980 79 70 65 20 64 65 66 69 6e 65 64 20 62 65 6c 6f ype defined belo
60990 77 20 69 73 20 75 73 65 64 20 66 6f 72 20 76 61 w is used for va
609a0 72 69 6f 75 73 20 6f 70 74 69 6d 69 7a 61 74 69 rious optimizati
609b0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 ons..**.** Chang
609c0 69 6e 67 20 74 68 69 73 20 66 72 6f 6d 20 61 20 ing this from a
609d0 36 34 2d 62 69 74 20 74 6f 20 61 20 33 32 2d 62 64-bit to a 32-b
609e0 69 74 20 74 79 70 65 20 6c 69 6d 69 74 73 20 74 it type limits t
609f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
60a00 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e tables in a join
60a10 20 74 6f 20 33 32 20 69 6e 73 74 65 61 64 20 6f to 32 instead o
60a20 66 20 36 34 2e 20 20 42 75 74 20 69 74 20 61 6c f 64. But it al
60a30 73 6f 20 72 65 64 75 63 65 73 20 74 68 65 20 73 so reduces the s
60a40 69 7a 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 69 ize.** of the li
60a50 62 72 61 72 79 20 62 79 20 37 33 38 20 62 79 74 brary by 738 byt
60a60 65 73 20 6f 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74 es on ix86..*/.t
60a70 79 70 65 64 65 66 20 75 36 34 20 42 69 74 6d 61 ypedef u64 Bitma
60a80 73 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e sk;../*.** The n
60a90 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e umber of bits in
60aa0 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 22 42 4d a Bitmask. "BM
60ab0 53 22 20 6d 65 61 6e 73 20 22 42 69 74 4d 61 73 S" means "BitMas
60ac0 6b 20 53 69 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66 k Size"..*/.#def
60ad0 69 6e 65 20 42 4d 53 20 20 28 28 69 6e 74 29 28 ine BMS ((int)(
60ae0 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a sizeof(Bitmask)*
60af0 38 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 8))../*.** The f
60b00 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
60b10 72 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 re describes the
60b20 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 FROM clause of
60b30 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
60b40 6e 74 2e 0a 2a 2a 20 45 61 63 68 20 74 61 62 6c nt..** Each tabl
60b50 65 20 6f 72 20 73 75 62 71 75 65 72 79 20 69 6e e or subquery in
60b60 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
60b70 20 69 73 20 61 20 73 65 70 61 72 61 74 65 20 65 is a separate e
60b80 6c 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 lement of.** the
60b90 20 53 72 63 4c 69 73 74 2e 61 5b 5d 20 61 72 72 SrcList.a[] arr
60ba0 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 ay..**.** With t
60bb0 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 6d he addition of m
60bc0 75 6c 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 ultiple database
60bd0 20 73 75 70 70 6f 72 74 2c 20 74 68 65 20 66 6f support, the fo
60be0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
60bf0 65 0a 2a 2a 20 63 61 6e 20 61 6c 73 6f 20 62 65 e.** can also be
60c00 20 75 73 65 64 20 74 6f 20 64 65 73 63 72 69 62 used to describ
60c10 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 e a particular t
60c20 61 62 6c 65 20 73 75 63 68 20 61 73 20 74 68 65 able such as the
60c30 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 69 table that.** i
60c40 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e s modified by an
60c50 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c INSERT, DELETE,
60c60 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 or UPDATE state
60c70 6d 65 6e 74 2e 20 20 49 6e 20 73 74 61 6e 64 61 ment. In standa
60c80 72 64 20 53 51 4c 2c 0a 2a 2a 20 73 75 63 68 20 rd SQL,.** such
60c90 61 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 a table must be
60ca0 61 20 73 69 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 a simple name: I
60cb0 44 2e 20 20 42 75 74 20 69 6e 20 53 51 4c 69 74 D. But in SQLit
60cc0 65 2c 20 74 68 65 20 74 61 62 6c 65 20 63 61 6e e, the table can
60cd0 0a 2a 2a 20 6e 6f 77 20 62 65 20 69 64 65 6e 74 .** now be ident
60ce0 69 66 69 65 64 20 62 79 20 61 20 64 61 74 61 62 ified by a datab
60cf0 61 73 65 20 6e 61 6d 65 2c 20 61 20 64 6f 74 2c ase name, a dot,
60d00 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 then the table
60d10 6e 61 6d 65 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a name: ID.ID..**.
60d20 2a 2a 20 54 68 65 20 6a 6f 69 6e 74 79 70 65 20 ** The jointype
60d30 73 74 61 72 74 73 20 6f 75 74 20 73 68 6f 77 69 starts out showi
60d40 6e 67 20 74 68 65 20 6a 6f 69 6e 20 74 79 70 65 ng the join type
60d50 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 75 72 between the cur
60d60 72 65 6e 74 20 74 61 62 6c 65 0a 2a 2a 20 61 6e rent table.** an
60d70 64 20 74 68 65 20 6e 65 78 74 20 74 61 62 6c 65 d the next table
60d80 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 20 54 on the list. T
60d90 68 65 20 70 61 72 73 65 72 20 62 75 69 6c 64 73 he parser builds
60da0 20 74 68 65 20 6c 69 73 74 20 74 68 69 73 20 77 the list this w
60db0 61 79 2e 0a 2a 2a 20 42 75 74 20 73 71 6c 69 74 ay..** But sqlit
60dc0 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f e3SrcListShiftJo
60dd0 69 6e 54 79 70 65 28 29 20 6c 61 74 65 72 20 73 inType() later s
60de0 68 69 66 74 73 20 74 68 65 20 6a 6f 69 6e 74 79 hifts the jointy
60df0 70 65 73 20 73 6f 20 74 68 61 74 20 65 61 63 68 pes so that each
60e00 0a 2a 2a 20 6a 6f 69 6e 74 79 70 65 20 65 78 70 .** jointype exp
60e10 72 65 73 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 resses the join
60e20 62 65 74 77 65 65 6e 20 74 68 65 20 74 61 62 6c between the tabl
60e30 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f e and the previo
60e40 75 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 us table..*/.str
60e50 75 63 74 20 53 72 63 4c 69 73 74 20 7b 0a 20 20 uct SrcList {.
60e60 69 31 36 20 6e 53 72 63 3b 20 20 20 20 20 20 20 i16 nSrc;
60e70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 /* Number of ta
60e80 62 6c 65 73 20 6f 72 20 73 75 62 71 75 65 72 69 bles or subqueri
60e90 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 es in the FROM c
60ea0 6c 61 75 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e lause */. i16 n
60eb0 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e Alloc; /* N
60ec0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
60ed0 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b allocated in a[
60ee0 5d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 ] below */. str
60ef0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
60f00 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 61 {. char *zDa
60f10 74 61 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 tabase; /* Name
60f20 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c of database hol
60f30 64 69 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 ding this table
60f40 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 */. char *zNa
60f50 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 me; /* Name
60f60 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f of the table */
60f70 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 6c 69 61 . char *zAlia
60f80 73 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 22 42 s; /* The "B
60f90 22 20 70 61 72 74 20 6f 66 20 61 20 22 41 20 41 " part of a "A A
60fa0 53 20 42 22 20 70 68 72 61 73 65 2e 20 20 7a 4e S B" phrase. zN
60fb0 61 6d 65 20 69 73 20 74 68 65 20 22 41 22 20 2a ame is the "A" *
60fc0 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 /. Table *pTa
60fd0 62 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 b; /* An SQ
60fe0 4c 20 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f L table correspo
60ff0 6e 64 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a nding to zName *
61000 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 /. Select *pS
61010 65 6c 65 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c elect; /* A SEL
61020 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 75 73 ECT statement us
61030 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 ed in place of a
61040 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 table name */.
61050 20 20 20 75 38 20 69 73 50 6f 70 75 6c 61 74 65 u8 isPopulate
61060 64 3b 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 d; /* Temporar
61070 79 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 y table associat
61080 65 64 20 77 69 74 68 20 53 45 4c 45 43 54 20 69 ed with SELECT i
61090 73 20 70 6f 70 75 6c 61 74 65 64 20 2a 2f 0a 20 s populated */.
610a0 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 3b 20 u8 jointype;
610b0 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 /* Type of
610c0 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 68 69 join between thi
610d0 73 20 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70 s able and the p
610e0 72 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20 20 75 revious */. u
610f0 38 20 6e 6f 74 49 6e 64 65 78 65 64 3b 20 20 20 8 notIndexed;
61100 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 /* True if ther
61110 65 20 69 73 20 61 20 4e 4f 54 20 49 4e 44 45 58 e is a NOT INDEX
61120 45 44 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 ED clause */.
61130 20 69 6e 74 20 69 43 75 72 73 6f 72 3b 20 20 20 int iCursor;
61140 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 /* The VDBE c
61150 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 ursor number use
61160 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 d to access this
61170 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 table */. Ex
61180 70 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20 pr *pOn;
61190 2f 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 /* The ON clause
611a0 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 of a join */.
611b0 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 IdList *pUsing
611c0 3b 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 ; /* The USING
611d0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 clause of a joi
611e0 6e 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b n */. Bitmask
611f0 20 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 colUsed; /* Bi
61200 74 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 t N (1<<N) set i
61210 66 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 f column N of pT
61220 61 62 20 69 73 20 75 73 65 64 20 2a 2f 0a 20 20 ab is used */.
61230 20 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 char *zIndex;
61240 20 20 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 /* Identifie
61250 72 20 66 72 6f 6d 20 22 49 4e 44 45 58 45 44 20 r from "INDEXED
61260 42 59 20 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61 BY <zIndex>" cla
61270 75 73 65 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 use */. Index
61280 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 *pIndex; /*
61290 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 Index structure
612a0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
612b0 20 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e 79 20 zIndex, if any
612c0 2a 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20 */. } a[1];
612d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
612e0 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 69 entry for each i
612f0 64 65 6e 74 69 66 69 65 72 20 6f 6e 20 74 68 65 dentifier on the
61300 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a list */.};../*.
61310 2a 2a 20 50 65 72 6d 69 74 74 65 64 20 76 61 6c ** Permitted val
61320 75 65 73 20 6f 66 20 74 68 65 20 53 72 63 4c 69 ues of the SrcLi
61330 73 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20 66 69 st.a.jointype fi
61340 65 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a eld.*/.#define J
61350 54 5f 49 4e 4e 45 52 20 20 20 20 20 30 78 30 30 T_INNER 0x00
61360 30 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 01 /* Any kin
61370 64 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20 63 72 d of inner or cr
61380 6f 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 oss join */.#def
61390 69 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20 20 20 ine JT_CROSS
613a0 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20 45 78 0x0002 /* Ex
613b0 70 6c 69 63 69 74 20 75 73 65 20 6f 66 20 74 68 plicit use of th
613c0 65 20 43 52 4f 53 53 20 6b 65 79 77 6f 72 64 20 e CROSS keyword
613d0 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4e 41 */.#define JT_NA
613e0 54 55 52 41 4c 20 20 20 30 78 30 30 30 34 20 20 TURAL 0x0004
613f0 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
61400 22 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e 20 2a "natural" join *
61410 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c 45 46 /.#define JT_LEF
61420 54 20 20 20 20 20 20 30 78 30 30 30 38 20 20 20 T 0x0008
61430 20 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72 20 6a /* Left outer j
61440 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a oin */.#define J
61450 54 5f 52 49 47 48 54 20 20 20 20 20 30 78 30 30 T_RIGHT 0x00
61460 31 30 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 10 /* Right o
61470 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 uter join */.#de
61480 66 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20 20 20 fine JT_OUTER
61490 20 20 30 78 30 30 32 30 20 20 20 20 2f 2a 20 54 0x0020 /* T
614a0 68 65 20 22 4f 55 54 45 52 22 20 6b 65 79 77 6f he "OUTER" keywo
614b0 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f rd is present */
614c0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 45 52 52 4f .#define JT_ERRO
614d0 52 20 20 20 20 20 30 78 30 30 34 30 20 20 20 20 R 0x0040
614e0 2f 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e /* unknown or un
614f0 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 supported join t
61500 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 ype */.../*.** A
61510 20 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63 WherePlan objec
61520 74 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 t holds informat
61530 69 6f 6e 20 74 68 61 74 20 64 65 73 63 72 69 62 ion that describ
61540 65 73 20 61 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 es a lookup.** s
61550 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 trategy..**.** T
61560 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 69 6e his object is in
61570 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 tended to be opa
61580 71 75 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 que outside of t
61590 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c he where.c modul
615a0 65 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e 63 6c e..** It is incl
615b0 75 64 65 64 20 68 65 72 65 20 6f 6e 6c 79 20 73 uded here only s
615c0 6f 20 74 68 61 74 20 74 68 61 74 20 63 6f 6d 70 o that that comp
615d0 69 6c 65 72 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 iler will know h
615e0 6f 77 20 62 69 67 20 69 74 0a 2a 2a 20 69 73 2e ow big it.** is.
615f0 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 None of the fi
61600 65 6c 64 73 20 69 6e 20 74 68 69 73 20 6f 62 6a elds in this obj
61610 65 63 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 ect should be us
61620 65 64 20 6f 75 74 73 69 64 65 20 6f 66 0a 2a 2a ed outside of.**
61630 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 the where.c mod
61640 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 ule..**.** Withi
61650 6e 20 74 68 65 20 75 6e 69 6f 6e 2c 20 70 49 64 n the union, pId
61660 78 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 x is only used w
61670 68 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52 hen wsFlags&WHER
61680 45 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 E_INDEXED is tru
61690 65 2e 0a 2a 2a 20 70 54 65 72 6d 20 69 73 20 6f e..** pTerm is o
616a0 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 nly used when ws
616b0 46 6c 61 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 Flags&WHERE_MULT
616c0 49 5f 4f 52 20 69 73 20 74 72 75 65 2e 20 20 41 I_OR is true. A
616d0 6e 64 20 70 56 74 61 62 49 64 78 0a 2a 2a 20 69 nd pVtabIdx.** i
616e0 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e s only used when
616f0 20 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 56 wsFlags&WHERE_V
61700 49 52 54 55 41 4c 54 41 42 4c 45 20 69 73 20 74 IRTUALTABLE is t
61710 72 75 65 2e 20 20 49 74 20 69 73 20 6e 65 76 65 rue. It is neve
61720 72 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 r the.** case th
61730 61 74 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 at more than one
61740 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 of these condit
61750 69 6f 6e 73 20 69 73 20 74 72 75 65 2e 0a 2a 2f ions is true..*/
61760 0a 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 .struct WherePla
61770 6e 20 7b 0a 20 20 75 33 32 20 77 73 46 6c 61 67 n {. u32 wsFlag
61780 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
61790 20 20 20 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20 /* WHERE_*
617a0 66 6c 61 67 73 20 74 68 61 74 20 64 65 73 63 72 flags that descr
617b0 69 62 65 20 74 68 65 20 73 74 72 61 74 65 67 79 ibe the strategy
617c0 20 2a 2f 0a 20 20 75 33 32 20 6e 45 71 3b 20 20 */. u32 nEq;
617d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
617e0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
617f0 66 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 f == constraints
61800 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 */. union {.
61810 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 Index *pIdx;
61820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
61830 20 2f 2a 20 49 6e 64 65 78 20 77 68 65 6e 20 57 /* Index when W
61840 48 45 52 45 5f 49 4e 44 45 58 45 44 20 69 73 20 HERE_INDEXED is
61850 74 72 75 65 20 2a 2f 0a 20 20 20 20 73 74 72 75 true */. stru
61860 63 74 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 ct WhereTerm *pT
61870 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20 57 48 erm; /* WH
61880 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20 ERE clause term
61890 66 6f 72 20 4f 52 2d 73 65 61 72 63 68 20 2a 2f for OR-search */
618a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 . sqlite3_ind
618b0 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 ex_info *pVtabId
618c0 78 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 x; /* Virtual t
618d0 61 62 6c 65 20 69 6e 64 65 78 20 74 6f 20 75 73 able index to us
618e0 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a e */. } u;.};..
618f0 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 6e /*.** For each n
61900 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20 61 20 ested loop in a
61910 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 6d 70 WHERE clause imp
61920 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 68 65 lementation, the
61930 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20 73 74 WhereInfo.** st
61940 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
61950 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e a single instan
61960 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ce of this struc
61970 74 75 72 65 2e 20 20 54 68 69 73 20 73 74 72 75 ture. This stru
61980 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e 74 65 cture.** is inte
61990 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69 76 61 nded to be priva
619a0 74 65 20 74 68 65 20 74 68 65 20 77 68 65 72 65 te the the where
619b0 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 73 68 .c module and sh
619c0 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 ould not be.** a
619d0 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 69 65 ccess or modifie
619e0 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c d by other modul
619f0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 49 es..**.** The pI
61a00 64 78 49 6e 66 6f 20 66 69 65 6c 64 20 69 73 20 dxInfo field is
61a10 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69 63 used to help pic
61a20 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 k the best index
61a30 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c on a.** virtual
61a40 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49 64 table. The pId
61a50 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63 6f xInfo pointer co
61a60 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67 0a ntains indexing.
61a70 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ** information f
61a80 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c or the i-th tabl
61a90 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c e in the FROM cl
61aa0 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f 72 ause before reor
61ab0 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74 dering..** All t
61ac0 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e he pIdxInfo poin
61ad0 74 65 72 73 20 61 72 65 20 66 72 65 65 64 20 62 ters are freed b
61ae0 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28 y whereInfoFree(
61af0 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a 2a ) in where.c..**
61b00 20 41 6c 6c 20 6f 74 68 65 72 20 69 6e 66 6f 72 All other infor
61b10 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 69 2d mation in the i-
61b20 74 68 20 57 68 65 72 65 4c 65 76 65 6c 20 6f 62 th WhereLevel ob
61b30 6a 65 63 74 20 66 6f 72 20 74 68 65 20 69 2d 74 ject for the i-t
61b40 68 20 74 61 62 6c 65 0a 2a 2a 20 61 66 74 65 72 h table.** after
61b50 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 72 64 FROM clause ord
61b60 65 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 ering..*/.struct
61b70 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a 20 20 WhereLevel {.
61b80 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 WherePlan plan;
61b90 20 20 20 20 20 20 2f 2a 20 71 75 65 72 79 20 70 /* query p
61ba0 6c 61 6e 20 66 6f 72 20 74 68 69 73 20 65 6c 65 lan for this ele
61bb0 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d ment of the FROM
61bc0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 clause */. int
61bd0 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20 20 20 iLeftJoin;
61be0 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c /* Memory cel
61bf0 6c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d l used to implem
61c00 65 6e 74 20 4c 45 46 54 20 4f 55 54 45 52 20 4a ent LEFT OUTER J
61c10 4f 49 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 OIN */. int iTa
61c20 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f bCur; /
61c30 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f * The VDBE curso
61c40 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 r used to access
61c50 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
61c60 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 int iIdxCur;
61c70 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 /* The VDB
61c80 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f E cursor used to
61c90 20 61 63 63 65 73 73 20 70 49 64 78 20 2a 2f 0a access pIdx */.
61ca0 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b 20 20 int addrBrk;
61cb0 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
61cc0 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 here to break ou
61cd0 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f t of the loop */
61ce0 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 . int addrNxt;
61cf0 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 /* Jump
61d00 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74 here to start t
61d10 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69 he next IN combi
61d20 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nation */. int
61d30 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 20 20 addrCont;
61d40 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
61d50 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 o continue with
61d60 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79 the next loop cy
61d70 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 cle */. int add
61d80 72 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 2f rFirst; /
61d90 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74 * First instruct
61da0 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 ion of interior
61db0 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 of the loop */.
61dc0 20 75 38 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 u8 iFrom;
61dd0 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 /* Which
61de0 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52 4f entry in the FRO
61df0 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 75 38 M clause */. u8
61e00 20 6f 70 2c 20 70 35 3b 20 20 20 20 20 20 20 20 op, p5;
61e10 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 61 6e /* Opcode an
61e20 64 20 50 35 20 6f 66 20 74 68 65 20 6f 70 63 6f d P5 of the opco
61e30 64 65 20 74 68 61 74 20 65 6e 64 73 20 74 68 65 de that ends the
61e40 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 70 loop */. int p
61e50 31 2c 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 1, p2;
61e60 20 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6f 66 20 /* Operands of
61e70 74 68 65 20 6f 70 63 6f 64 65 20 75 73 65 64 20 the opcode used
61e80 74 6f 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 to ends the loop
61e90 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 */. union {
61ea0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
61eb0 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 nformation that
61ec0 64 65 70 65 6e 64 73 20 6f 6e 20 70 6c 61 6e 2e depends on plan.
61ed0 77 73 46 6c 61 67 73 20 2a 2f 0a 20 20 20 20 73 wsFlags */. s
61ee0 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20 69 6e truct {. in
61ef0 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20 t nIn;
61f00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
61f10 20 65 6e 74 72 69 65 73 20 69 6e 20 61 49 6e 4c entries in aInL
61f20 6f 6f 70 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 73 oop[] */. s
61f30 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20 truct InLoop {.
61f40 20 20 20 20 20 20 20 69 6e 74 20 69 43 75 72 3b int iCur;
61f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
61f60 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 The VDBE cursor
61f70 20 75 73 65 64 20 62 79 20 74 68 69 73 20 49 4e used by this IN
61f80 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 operator */.
61f90 20 20 20 20 20 69 6e 74 20 61 64 64 72 49 6e 54 int addrInT
61fa0 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 op; /* T
61fb0 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f op of the IN loo
61fc0 70 20 2a 2f 0a 20 20 20 20 20 20 7d 20 2a 61 49 p */. } *aI
61fd0 6e 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 nLoop;
61fe0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
61ff0 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 65 about each neste
62000 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f d IN operator */
62010 0a 20 20 20 20 7d 20 69 6e 3b 20 20 20 20 20 20 . } in;
62020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
62030 65 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77 73 46 ed when plan.wsF
62040 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 5f 41 42 lags&WHERE_IN_AB
62050 4c 45 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 LE */. } u;..
62060 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 /* The following
62070 20 66 69 65 6c 64 20 69 73 20 72 65 61 6c 6c 79 field is really
62080 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 not part of the
62090 20 63 75 72 72 65 6e 74 20 6c 65 76 65 6c 2e 20 current level.
620a0 20 42 75 74 0a 20 20 2a 2a 20 77 65 20 6e 65 65 But. ** we nee
620b0 64 20 61 20 70 6c 61 63 65 20 74 6f 20 63 61 63 d a place to cac
620c0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
620d0 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 index informati
620e0 6f 6e 20 66 6f 72 20 65 61 63 68 0a 20 20 2a 2a on for each. **
620f0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 virtual table i
62100 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
62110 65 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 4c e and the WhereL
62120 65 76 65 6c 20 73 74 72 75 63 74 75 72 65 20 69 evel structure i
62130 73 0a 20 20 2a 2a 20 61 20 63 6f 6e 76 65 6e 69 s. ** a conveni
62140 65 6e 74 20 70 6c 61 63 65 20 73 69 6e 63 65 20 ent place since
62150 74 68 65 72 65 20 69 73 20 6f 6e 65 20 57 68 65 there is one Whe
62160 72 65 4c 65 76 65 6c 20 66 6f 72 20 65 61 63 68 reLevel for each
62170 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a FROM clause. *
62180 2a 20 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a * element.. */.
62190 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f sqlite3_index_
621a0 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 info *pIdxInfo;
621b0 20 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 /* Index info f
621c0 6f 72 20 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 or n-th source t
621d0 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a able */.};../*.*
621e0 2a 20 46 6c 61 67 73 20 61 70 70 72 6f 70 72 69 * Flags appropri
621f0 61 74 65 20 66 6f 72 20 74 68 65 20 77 63 74 72 ate for the wctr
62200 6c 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 lFlags parameter
62210 20 6f 66 20 73 71 6c 69 74 65 33 57 68 65 72 65 of sqlite3Where
62220 42 65 67 69 6e 28 29 2e 0a 2a 2f 0a 23 64 65 66 Begin()..*/.#def
62230 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 ine WHERE_ORDERB
62240 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30 Y_NORMAL 0x000
62250 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64 0 /* No-op */.#d
62260 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 efine WHERE_ORDE
62270 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30 RBY_MIN 0x0
62280 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 001 /* ORDER BY
62290 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d processing for m
622a0 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 in() func */.#de
622b0 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 fine WHERE_ORDER
622c0 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30 BY_MAX 0x00
622d0 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 02 /* ORDER BY p
622e0 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61 rocessing for ma
622f0 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 x() func */.#def
62300 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 ine WHERE_ONEPAS
62310 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30 S_DESIRED 0x000
62320 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20 4 /* Want to do
62330 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f one-pass UPDATE/
62340 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e DELETE */.#defin
62350 65 20 57 48 45 52 45 5f 46 49 4c 4c 5f 52 4f 57 e WHERE_FILL_ROW
62360 53 45 54 20 20 20 20 20 20 30 78 30 30 30 38 20 SET 0x0008
62370 20 2f 2a 20 53 61 76 65 20 72 65 73 75 6c 74 73 /* Save results
62380 20 69 6e 20 61 20 52 6f 77 53 65 74 20 6f 62 6a in a RowSet obj
62390 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 ect */.#define W
623a0 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 20 HERE_OMIT_OPEN
623b0 20 20 20 20 20 20 30 78 30 30 31 30 20 20 2f 2a 0x0010 /*
623c0 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 72 Table cursor ar
623d0 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a e already open *
623e0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
623f0 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 20 OMIT_CLOSE
62400 20 30 78 30 30 32 30 20 20 2f 2a 20 4f 6d 69 74 0x0020 /* Omit
62410 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20 close of table
62420 26 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 & index cursors
62430 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 */../*.** The WH
62440 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 ERE clause proce
62450 73 73 69 6e 67 20 72 6f 75 74 69 6e 65 20 68 61 ssing routine ha
62460 73 20 74 77 6f 20 68 61 6c 76 65 73 2e 20 20 54 s two halves. T
62470 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 74 he.** first part
62480 20 64 6f 65 73 20 74 68 65 20 73 74 61 72 74 20 does the start
62490 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f of the WHERE loo
624a0 70 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 p and the second
624b0 0a 2a 2a 20 68 61 6c 66 20 64 6f 65 73 20 74 68 .** half does th
624c0 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 57 48 e tail of the WH
624d0 45 52 45 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e ERE loop. An in
624e0 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 stance of.** thi
624f0 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 s structure is r
62500 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 eturned by the f
62510 69 72 73 74 20 68 61 6c 66 20 61 6e 64 20 70 61 irst half and pa
62520 73 73 65 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 ssed.** into the
62530 20 73 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 second half to
62540 67 69 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e give some contin
62550 75 69 74 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 uity..*/.struct
62560 57 68 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 WhereInfo {. Pa
62570 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 rse *pParse;
62580 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e /* Parsing an
62590 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e d code generatin
625a0 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75 g context */. u
625b0 31 36 20 77 63 74 72 6c 46 6c 61 67 73 3b 20 20 16 wctrlFlags;
625c0 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 /* Flags ori
625d0 67 69 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 74 ginally passed t
625e0 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 o sqlite3WhereBe
625f0 67 69 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b gin() */. u8 ok
62600 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 OnePass;
62610 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 /* Ok to use one
62620 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 -pass algorithm
62630 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45 for UPDATE or DE
62640 4c 45 54 45 20 2a 2f 0a 20 20 69 6e 74 20 72 65 LETE */. int re
62650 67 52 6f 77 53 65 74 3b 20 20 20 20 20 20 20 20 gRowSet;
62660 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 /* Stor
62670 65 20 72 6f 77 69 64 73 20 69 6e 20 74 68 69 73 e rowids in this
62680 20 72 6f 77 73 65 74 20 69 66 20 3e 3d 30 20 2a rowset if >=0 *
62690 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 /. SrcList *pTa
626a0 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 bList;
626b0 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 /* List of ta
626c0 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e bles in the join
626d0 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b 20 */. int iTop;
626e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
626f0 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72 79 /* The very
62700 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
62710 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f 0a e WHERE loop */.
62720 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b int iContinue;
62730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62740 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f /* Jump here to
62750 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e continue with n
62760 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 ext record */.
62770 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20 20 int iBreak;
62780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
62790 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 * Jump here to b
627a0 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 reak out of the
627b0 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c loop */. int nL
627c0 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 evel;
627d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
627e0 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f 6f er of nested loo
627f0 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 57 68 p */. struct Wh
62800 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 ereClause *pWC;
62810 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f /* Decompo
62820 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 48 sition of the WH
62830 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ERE clause */.
62840 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d 3b WhereLevel a[1];
62850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
62860 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 * Information ab
62870 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 6c 6f out each nest lo
62880 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a 7d op in WHERE */.}
62890 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65 43 ;../*.** A NameC
628a0 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 20 61 ontext defines a
628b0 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 context in whic
628c0 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 h to resolve tab
628d0 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a le and column.**
628e0 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f 6e names. The con
628f0 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 text consists of
62900 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 a list of table
62910 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 74 29 s (the pSrcList)
62920 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61 20 field and.** a
62930 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 list of named ex
62940 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73 74 pression (pEList
62950 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65 78 ). The named ex
62960 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d 61 pression list ma
62970 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 54 y.** be NULL. T
62980 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70 6f he pSrc correspo
62990 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 nds to the FROM
629a0 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 clause of a SELE
629b0 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65 20 CT or.** to the
629c0 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 65 72 table being oper
629d0 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 45 52 ated on by INSER
629e0 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 T, UPDATE, or DE
629f0 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70 45 LETE. The.** pE
62a00 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 List corresponds
62a10 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 to the result s
62a20 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 et of a SELECT a
62a30 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a 2a nd is NULL for.*
62a40 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e * other statemen
62a50 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43 6f ts..**.** NameCo
62a60 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 6e 65 ntexts can be ne
62a70 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 73 6f sted. When reso
62a80 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68 65 lving names, the
62a90 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a 20 inner-most .**
62aa0 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 72 63 context is searc
62ab0 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 20 6e hed first. If n
62ac0 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 o match is found
62ad0 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 65 72 , the next outer
62ae0 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 63 .** context is c
62af0 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 65 72 hecked. If ther
62b00 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d 61 e is still no ma
62b10 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 63 6f tch, the next co
62b20 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 65 63 ntext.** is chec
62b30 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f 63 65 ked. This proce
62b40 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e 74 ss continues unt
62b50 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 74 63 il either a matc
62b60 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f 72 h is found.** or
62b70 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61 72 all contexts ar
62b80 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e 20 61 e check. When a
62b90 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c match is found,
62ba0 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 65 72 the nRef member
62bb0 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 of.** the conte
62bc0 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 xt containing th
62bd0 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 72 65 e match is incre
62be0 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 45 mented. .**.** E
62bf0 61 63 68 20 73 75 62 71 75 65 72 79 20 67 65 74 ach subquery get
62c00 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e 74 s a new NameCont
62c10 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 74 20 ext. The pNext
62c20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 field points to
62c30 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 the.** NameConte
62c40 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 xt in the parent
62c50 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 74 68 query. Thus th
62c60 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 63 61 e process of sca
62c70 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61 6d nning the.** Nam
62c80 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63 6f eContext list co
62c90 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65 61 rresponds to sea
62ca0 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20 73 rching through s
62cb0 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74 65 uccessively oute
62cc0 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20 r.** subqueries
62cd0 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d 61 looking for a ma
62ce0 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4e tch..*/.struct N
62cf0 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20 50 ameContext {. P
62d00 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
62d10 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 /* The parse
62d20 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a r */. SrcList *
62d30 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20 4f pSrcList; /* O
62d40 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c 65 ne or more table
62d50 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 s used to resolv
62d60 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78 70 e names */. Exp
62d70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 rList *pEList;
62d80 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c 69 /* Optional li
62d90 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 72 st of named expr
62da0 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 essions */. int
62db0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 nRef;
62dc0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e /* Number of n
62dd0 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 62 79 ames resolved by
62de0 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 2a 2f this context */
62df0 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 . int nErr;
62e00 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
62e10 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f r of errors enco
62e20 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 72 65 untered while re
62e30 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a 2f solving names */
62e40 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b 20 . u8 allowAgg;
62e50 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 /* Aggre
62e60 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 gate functions a
62e70 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a 20 llowed here */.
62e80 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 20 20 u8 hasAgg;
62e90 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
62ea0 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20 aggregates are
62eb0 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73 43 seen */. u8 isC
62ec0 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f heck; /
62ed0 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f 6c 76 * True if resolv
62ee0 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 20 43 ing names in a C
62ef0 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 HECK constraint
62f00 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 68 3b */. int nDepth;
62f10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 /* Dep
62f20 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 20 72 th of subquery r
62f30 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72 20 ecursion. 1 for
62f40 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a no recursion */.
62f50 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 AggInfo *pAggI
62f60 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d nfo; /* Inform
62f70 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 67 72 ation about aggr
62f80 65 67 61 74 65 73 20 61 74 20 74 68 69 73 20 6c egates at this l
62f90 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f evel */. NameCo
62fa0 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 20 2f ntext *pNext; /
62fb0 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e 61 6d * Next outer nam
62fc0 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c 4c e context. NULL
62fd0 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20 2a for outermost *
62fe0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 /.};../*.** An i
62ff0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
63000 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
63010 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 re contains all
63020 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6e information.** n
63030 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 61 74 eeded to generat
63040 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e e code for a sin
63050 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 gle SELECT state
63060 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69 6d ment..**.** nLim
63070 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20 it is set to -1
63080 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4c if there is no L
63090 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 6e 4f IMIT clause. nO
630a0 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 ffset is set to
630b0 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 0..** If there i
630c0 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 s a LIMIT clause
630d0 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 65 74 , the parser set
630e0 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65 20 s nLimit to the
630f0 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 value of the.**
63100 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 limit and nOffse
63110 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f t to the value o
63120 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 6f 72 f the offset (or
63130 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 0 if there is n
63140 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20 20 ot.** offset).
63150 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e 4c But later on, nL
63160 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 74 imit and nOffset
63170 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d 6f become the memo
63180 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 ry locations.**
63190 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 61 74 in the VDBE that
631a0 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 6d 69 record the limi
631b0 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 6f 75 t and offset cou
631c0 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64 64 nters..**.** add
631d0 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74 72 rOpenEphm[] entr
631e0 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 ies contain the
631f0 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 address of OP_Op
63200 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f enEphemeral opco
63210 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61 64 des..** These ad
63220 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 65 20 dresses must be
63230 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 20 77 stored so that w
63240 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 61 6e e can go back an
63250 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68 65 d fill in.** the
63260 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64 20 P4_KEYINFO and
63270 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 6c 61 P2 parameters la
63280 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 74 68 ter. Neither th
63290 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a 2a e KeyInfo nor.**
632a0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
632b0 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 61 6e olumns in P2 can
632c0 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74 20 be computed at
632d0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a 2a the same time.**
632e0 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 as the OP_OpenE
632f0 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 phm instruction
63300 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 73 65 is coded because
63310 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 69 not.** enough i
63320 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
63330 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 the compound qu
63340 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 ery is known at
63350 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20 54 that point..** T
63360 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 he KeyInfo for a
63370 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 61 ddrOpenTran[0] a
63380 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 20 nd [1] contains
63390 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
633a0 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 ces.** for the r
633b0 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65 20 esult set. The
633c0 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 72 KeyInfo for addr
633d0 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e 74 OpenTran[2] cont
633e0 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a ains collating.*
633f0 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72 20 * sequences for
63400 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
63410 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 use..*/.struct S
63420 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 4c 69 elect {. ExprLi
63430 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 st *pEList;
63440 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 20 6f /* The fields o
63450 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a f the result */.
63460 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 u8 op;
63470 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
63480 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b 5f of: TK_UNION TK_
63490 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 ALL TK_INTERSECT
634a0 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20 20 TK_EXCEPT */.
634b0 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 char affinity;
634c0 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52 65 /* MakeRe
634d0 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 20 61 cord with this a
634e0 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 54 5f ffinity for SRT_
634f0 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 65 6c Set */. u16 sel
63500 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 Flags;
63510 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a 20 /* Various SF_*
63520 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63 4c values */. SrcL
63530 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 ist *pSrc;
63540 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 /* The FROM c
63550 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 lause */. Expr
63560 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 *pWhere;
63570 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 /* The WHERE c
63580 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c lause */. ExprL
63590 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 ist *pGroupBy;
635a0 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 /* The GROUP B
635b0 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 Y clause */. Ex
635c0 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20 pr *pHaving;
635d0 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49 /* The HAVI
635e0 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 NG clause */. E
635f0 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 xprList *pOrderB
63600 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 y; /* The ORD
63610 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a ER BY clause */.
63620 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 Select *pPrior
63630 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 6f ; /* Prio
63640 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f r select in a co
63650 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 mpound select st
63660 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c atement */. Sel
63670 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 ect *pNext;
63680 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c 65 /* Next sele
63690 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20 69 ct to the left i
636a0 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a n a compound */.
636b0 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68 74 Select *pRight
636c0 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67 68 most; /* Righ
636d0 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e t-most select in
636e0 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 a compound sele
636f0 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a ct statement */.
63700 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20 Expr *pLimit;
63710 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49 /* LIMI
63720 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 T expression. NU
63730 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 LL means not use
63740 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f d. */. Expr *pO
63750 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f ffset; /
63760 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 73 73 * OFFSET express
63770 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 ion. NULL means
63780 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 69 not used. */. i
63790 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 nt iLimit, iOffs
637a0 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 et; /* Memory
637b0 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e registers holdin
637c0 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 45 54 g LIMIT & OFFSET
637d0 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 counters */. i
637e0 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b nt addrOpenEphm[
637f0 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 3]; /* OP_Open
63800 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 72 65 Ephem opcodes re
63810 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 73 65 lated to this se
63820 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a lect */.};../*.*
63830 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 * Allowed values
63840 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c 46 for Select.selF
63850 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 22 20 lags. The "SF"
63860 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 66 6f prefix stands fo
63870 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c 61 r.** "Select Fla
63880 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 g"..*/.#define S
63890 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20 20 F_Distinct
638a0 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 74 0x0001 /* Out
638b0 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44 49 put should be DI
638c0 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 6e STINCT */.#defin
638d0 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 20 e SF_Resolved
638e0 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 0x0002 /*
638f0 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 76 65 Identifiers have
63900 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 2a been resolved *
63910 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67 67 /.#define SF_Agg
63920 72 65 67 61 74 65 20 20 20 20 20 20 20 30 78 30 regate 0x0
63930 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 004 /* Contains
63940 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
63950 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ions */.#define
63960 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c SF_UsesEphemeral
63970 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55 73 0x0008 /* Us
63980 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d es the OpenEphem
63990 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 eral opcode */.#
639a0 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e 64 define SF_Expand
639b0 65 64 20 20 20 20 20 20 20 20 30 78 30 30 31 30 ed 0x0010
639c0 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 /* sqlite3Sele
639d0 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c 65 ctExpand() calle
639e0 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 65 d on this */.#de
639f0 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65 49 fine SF_HasTypeI
63a00 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 20 20 nfo 0x0020
63a10 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72 69 /* FROM subqueri
63a20 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d 65 es have Table me
63a30 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a 2a tadata */.../*.*
63a40 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f 66 * The results of
63a50 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 62 65 a select can be
63a60 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 distributed in
63a70 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 20 54 several ways. T
63a80 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 65 66 he.** "SRT" pref
63a90 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 43 54 ix means "SELECT
63aa0 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e 0a 2a Result Type"..*
63ab0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 55 6e /.#define SRT_Un
63ac0 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 2f 2a ion 1 /*
63ad0 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73 Store result as
63ae0 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 keys in an inde
63af0 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 x */.#define SRT
63b00 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 32 20 _Except 2
63b10 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 75 6c /* Remove resul
63b20 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20 69 t from a UNION i
63b30 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ndex */.#define
63b40 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20 20 SRT_Exists
63b50 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 20 69 3 /* Store 1 i
63b60 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 f the result is
63b70 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 not empty */.#de
63b80 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72 64 fine SRT_Discard
63b90 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f 20 6e 4 /* Do n
63ba0 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 73 75 ot save the resu
63bb0 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f 0a lts anywhere */.
63bc0 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 ./* The ORDER BY
63bd0 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f 72 clause is ignor
63be0 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 ed for all of th
63bf0 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66 69 e above */.#defi
63c00 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 ne IgnorableOrde
63c10 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 65 73 rby(X) ((X->eDes
63c20 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 64 29 t)<=SRT_Discard)
63c30 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f 75 ..#define SRT_Ou
63c40 74 70 75 74 20 20 20 20 20 20 20 35 20 20 2f 2a tput 5 /*
63c50 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f 77 Output each row
63c60 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23 64 of result */.#d
63c70 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20 20 efine SRT_Mem
63c80 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74 6f 6 /* Sto
63c90 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20 6d re result in a m
63ca0 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23 64 emory cell */.#d
63cb0 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20 20 efine SRT_Set
63cc0 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74 6f 7 /* Sto
63cd0 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b 65 re results as ke
63ce0 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a ys in an index *
63cf0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54 61 /.#define SRT_Ta
63d00 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f 2a ble 8 /*
63d10 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73 Store result as
63d20 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61 75 data with an au
63d30 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a 2f tomatic rowid */
63d40 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70 68 .#define SRT_Eph
63d50 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a 20 emTab 9 /*
63d60 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e 74 Create transient
63d70 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20 6c tab and store l
63d80 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a 2f ike SRT_Table */
63d90 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 6f 72 .#define SRT_Cor
63da0 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f 2a 20 outine 10 /*
63db0 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67 6c Generate a singl
63dc0 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 e row of result
63dd0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 */../*.** A stru
63de0 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 63 75 cture used to cu
63df0 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 68 61 stomize the beha
63e00 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 53 vior of sqlite3S
63e10 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a 20 elect(). See.**
63e20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 73 comments above s
63e30 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 66 qlite3Select() f
63e40 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 74 or details..*/.t
63e50 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65 ypedef struct Se
63e60 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 74 44 lectDest SelectD
63e70 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 6c 65 est;.struct Sele
63e80 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65 44 ctDest {. u8 eD
63e90 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 est; /*
63ea0 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f How to dispose o
63eb0 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a 2f f the results */
63ec0 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 3b 20 . u8 affinity;
63ed0 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 /* Affinity
63ee0 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 73 74 used when eDest
63ef0 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 69 ==SRT_Set */. i
63f00 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 20 20 nt iParm;
63f10 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 72 20 /* A parameter
63f20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 65 73 used by the eDes
63f30 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f t disposal metho
63f40 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 3b d */. int iMem;
63f50 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 /* Base
63f60 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 20 register where
63f70 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 69 74 results are writ
63f80 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 ten */. int nMe
63f90 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 m; /* Nu
63fa0 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 mber of register
63fb0 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 7d s allocated */.}
63fc0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 ;../*.** An SQL
63fd0 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 parser context.
63fe0 20 41 20 63 6f 70 79 20 6f 66 20 74 68 69 73 20 A copy of this
63ff0 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73 structure is pas
64000 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 sed through.** t
64010 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 64 6f he parser and do
64020 77 6e 20 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20 wn into all the
64030 70 61 72 73 65 72 20 61 63 74 69 6f 6e 20 72 6f parser action ro
64040 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 utine in order t
64050 6f 0a 2a 2a 20 63 61 72 72 79 20 61 72 6f 75 6e o.** carry aroun
64060 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 d information th
64070 61 74 20 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20 at is global to
64080 74 68 65 20 65 6e 74 69 72 65 20 70 61 72 73 65 the entire parse
64090 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75 ..**.** The stru
640a0 63 74 75 72 65 20 69 73 20 64 69 76 69 64 65 64 cture is divided
640b0 20 69 6e 74 6f 20 74 77 6f 20 70 61 72 74 73 2e into two parts.
640c0 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 When the parse
640d0 72 20 61 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65 r and code.** ge
640e0 6e 65 72 61 74 65 20 63 61 6c 6c 20 74 68 65 6d nerate call them
640f0 73 65 6c 76 65 73 20 72 65 63 75 72 73 69 76 65 selves recursive
64100 6c 79 2c 20 74 68 65 20 66 69 72 73 74 20 70 61 ly, the first pa
64110 72 74 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 rt of the struct
64120 75 72 65 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61 ure.** is consta
64130 6e 74 20 62 75 74 20 74 68 65 20 73 65 63 6f 6e nt but the secon
64140 64 20 70 61 72 74 20 69 73 20 72 65 73 65 74 20 d part is reset
64150 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
64160 20 61 6e 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 and end of.** e
64170 61 63 68 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a ach recursion..*
64180 2a 0a 2a 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c *.** The nTableL
64190 6f 63 6b 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f ock and aTableLo
641a0 63 6b 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 ck variables are
641b0 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 only used if th
641c0 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 0a e shared-cache .
641d0 2a 2a 20 66 65 61 74 75 72 65 20 69 73 20 65 6e ** feature is en
641e0 61 62 6c 65 64 20 28 69 66 20 73 71 6c 69 74 65 abled (if sqlite
641f0 33 54 73 64 28 29 2d 3e 75 73 65 53 68 61 72 65 3Tsd()->useShare
64200 64 44 61 74 61 20 69 73 20 74 72 75 65 29 2e 20 dData is true).
64210 54 68 65 79 20 61 72 65 0a 2a 2a 20 75 73 65 64 They are.** used
64220 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 to store the se
64230 74 20 6f 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 t of table-locks
64240 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 required by the
64250 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 statement being
64260 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75 .** compiled. Fu
64270 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 54 61 nction sqlite3Ta
64280 62 6c 65 4c 6f 63 6b 28 29 20 69 73 20 75 73 65 bleLock() is use
64290 64 20 74 6f 20 61 64 64 20 65 6e 74 72 69 65 73 d to add entries
642a0 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e to the.** list.
642b0 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 72 73 65 .*/.struct Parse
642c0 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
642d0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
642e0 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 main database s
642f0 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e tructure */. in
64300 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
64310 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
64320 65 20 66 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e e from execution
64330 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 */. char *zErr
64340 4d 73 67 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e Msg; /* An
64350 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a error message *
64360 2f 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b /. Vdbe *pVdbe;
64370 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 /* An e
64380 6e 67 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74 ngine for execut
64390 69 6e 67 20 64 61 74 61 62 61 73 65 20 62 79 74 ing database byt
643a0 65 63 6f 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f ecode */. u8 co
643b0 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20 20 20 20 lNamesSet;
643c0 2f 2a 20 54 52 55 45 20 61 66 74 65 72 20 4f 50 /* TRUE after OP
643d0 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20 _ColumnName has
643e0 62 65 65 6e 20 69 73 73 75 65 64 20 74 6f 20 70 been issued to p
643f0 56 64 62 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d Vdbe */. u8 nam
64400 65 43 6c 61 73 68 3b 20 20 20 20 20 20 20 20 2f eClash; /
64410 2a 20 41 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 * A permanent ta
64420 62 6c 65 20 6e 61 6d 65 20 63 6c 61 73 68 65 73 ble name clashes
64430 20 77 69 74 68 20 74 65 6d 70 20 74 61 62 6c 65 with temp table
64440 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68 name */. u8 ch
64450 65 63 6b 53 63 68 65 6d 61 3b 20 20 20 20 20 20 eckSchema;
64460 2f 2a 20 43 61 75 73 65 73 20 73 63 68 65 6d 61 /* Causes schema
64470 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20 61 66 cookie check af
64480 74 65 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a ter an error */.
64490 20 20 75 38 20 6e 65 73 74 65 64 3b 20 20 20 20 u8 nested;
644a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
644b0 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 of nested calls
644c0 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 2f 63 to the parser/c
644d0 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f ode generator */
644e0 0a 20 20 75 38 20 70 61 72 73 65 45 72 72 6f 72 . u8 parseError
644f0 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
64500 61 66 74 65 72 20 61 20 70 61 72 73 69 6e 67 20 after a parsing
64510 65 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 error. Ticket #
64520 31 37 39 34 20 2a 2f 0a 20 20 75 38 20 6e 54 65 1794 */. u8 nTe
64530 6d 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f mpReg; /
64540 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 6d 70 * Number of temp
64550 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 orary registers
64560 69 6e 20 61 54 65 6d 70 52 65 67 5b 5d 20 2a 2f in aTempReg[] */
64570 0a 20 20 75 38 20 6e 54 65 6d 70 49 6e 55 73 65 . u8 nTempInUse
64580 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
64590 72 20 6f 66 20 61 54 65 6d 70 52 65 67 5b 5d 20 r of aTempReg[]
645a0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 currently checke
645b0 64 20 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61 d out */. int a
645c0 54 65 6d 70 52 65 67 5b 38 5d 3b 20 20 20 20 20 TempReg[8];
645d0 2f 2a 20 48 6f 6c 64 69 6e 67 20 61 72 65 61 20 /* Holding area
645e0 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 72 65 for temporary re
645f0 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 gisters */. int
64600 20 6e 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20 nRangeReg;
64610 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
64620 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 temporary regis
64630 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 ter block */. i
64640 6e 74 20 69 52 61 6e 67 65 52 65 67 3b 20 20 20 nt iRangeReg;
64650 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 /* First reg
64660 69 73 74 65 72 20 69 6e 20 74 65 6d 70 6f 72 61 ister in tempora
64670 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 ry register bloc
64680 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b k */. int nErr;
64690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
646a0 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
646b0 73 65 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 seen */. int nT
646c0 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ab; /
646d0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 72 65 76 * Number of prev
646e0 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 iously allocated
646f0 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 2a 2f VDBE cursors */
64700 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 . int nMem;
64710 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
64720 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c r of memory cell
64730 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f s used so far */
64740 0a 20 20 69 6e 74 20 6e 53 65 74 3b 20 20 20 20 . int nSet;
64750 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
64760 72 20 6f 66 20 73 65 74 73 20 75 73 65 64 20 73 r of sets used s
64770 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 o far */. int c
64780 6b 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 kBase;
64790 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 /* Base register
647a0 20 6f 66 20 64 61 74 61 20 64 75 72 69 6e 67 20 of data during
647b0 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 check constraint
647c0 73 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 61 62 s */. int disab
647d0 6c 65 43 6f 6c 43 61 63 68 65 3b 20 2f 2a 20 54 leColCache; /* T
647e0 72 75 65 20 74 6f 20 64 69 73 61 62 6c 65 20 61 rue to disable a
647f0 64 64 69 6e 67 20 74 6f 20 63 6f 6c 75 6d 6e 20 dding to column
64800 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 6e cache */. int n
64810 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20 ColCache;
64820 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
64830 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 6c 75 ries in the colu
64840 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e mn cache */. in
64850 74 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 t iColCache;
64860 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79 /* Next entry
64870 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 74 6f of the cache to
64880 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 73 74 replace */. st
64890 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 7b ruct yColCache {
648a0 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65 3b . int iTable;
648b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 /* Ta
648c0 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 ble cursor numbe
648d0 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f r */. int iCo
648e0 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f lumn; /
648f0 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e * Table column n
64900 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 63 68 61 umber */. cha
64910 72 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20 r affChange;
64920 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
64930 69 73 20 72 65 67 69 73 74 65 72 20 68 61 73 20 is register has
64940 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 79 20 had an affinity
64950 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 69 6e change */. in
64960 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 t iReg;
64970 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 /* Register
64980 68 6f 6c 64 69 6e 67 20 76 61 6c 75 65 20 6f 66 holding value of
64990 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a this column */.
649a0 20 20 7d 20 61 43 6f 6c 43 61 63 68 65 5b 31 30 } aColCache[10
649b0 5d 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 66 6f ]; /* One fo
649c0 72 20 65 61 63 68 20 76 61 6c 69 64 20 63 6f 6c r each valid col
649d0 75 6d 6e 20 63 61 63 68 65 20 65 6e 74 72 79 20 umn cache entry
649e0 2a 2f 0a 20 20 75 33 32 20 77 72 69 74 65 4d 61 */. u32 writeMa
649f0 73 6b 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 sk; /* Sta
64a00 72 74 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 rt a write trans
64a10 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20 action on these
64a20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 databases */. u
64a30 33 32 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 32 cookieMask;
64a40 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f /* Bitmask o
64a50 66 20 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 f schema verifie
64a60 64 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 d databases */.
64a70 20 69 6e 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b int cookieGoto;
64a80 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
64a90 20 6f 66 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 of OP_Goto to c
64aa0 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 73 ookie verifier s
64ab0 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 ubroutine */. i
64ac0 6e 74 20 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 nt cookieValue[S
64ad0 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 QLITE_MAX_ATTACH
64ae0 45 44 2b 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 ED+2]; /* Value
64af0 73 20 6f 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20 s of cookies to
64b00 76 65 72 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65 verify */.#ifnde
64b10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
64b20 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 ARED_CACHE. int
64b30 20 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 nTableLock;
64b40 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
64b50 20 6c 6f 63 6b 73 20 69 6e 20 61 54 61 62 6c 65 locks in aTable
64b60 4c 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c Lock */. TableL
64b70 6f 63 6b 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b ock *aTableLock;
64b80 20 2f 2a 20 52 65 71 75 69 72 65 64 20 74 61 62 /* Required tab
64b90 6c 65 20 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61 le locks for sha
64ba0 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a red-cache mode *
64bb0 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 72 /.#endif. int r
64bc0 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 egRowid;
64bd0 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 /* Register hold
64be0 69 6e 67 20 72 6f 77 69 64 20 6f 66 20 43 52 45 ing rowid of CRE
64bf0 41 54 45 20 54 41 42 4c 45 20 65 6e 74 72 79 20 ATE TABLE entry
64c00 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 6f 74 */. int regRoot
64c10 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 ; /* Reg
64c20 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f ister holding ro
64c30 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 ot page number f
64c40 6f 72 20 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a or new objects *
64c50 2f 0a 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69 73 /.. /* Above is
64c60 20 63 6f 6e 73 74 61 6e 74 20 62 65 74 77 65 65 constant betwee
64c70 6e 20 72 65 63 75 72 73 69 6f 6e 73 2e 20 20 42 n recursions. B
64c80 65 6c 6f 77 20 69 73 20 72 65 73 65 74 20 62 65 elow is reset be
64c90 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 0a 20 fore and after.
64ca0 20 2a 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 ** each recursi
64cb0 6f 6e 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 56 61 on */.. int nVa
64cc0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r; /*
64cd0 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 76 Number of '?' v
64ce0 61 72 69 61 62 6c 65 73 20 73 65 65 6e 20 69 6e ariables seen in
64cf0 20 74 68 65 20 53 51 4c 20 73 6f 20 66 61 72 20 the SQL so far
64d00 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 */. int nVarExp
64d10 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d r; /* Num
64d20 62 65 72 20 6f 66 20 75 73 65 64 20 73 6c 6f 74 ber of used slot
64d30 73 20 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d s in apVarExpr[]
64d40 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 */. int nVarEx
64d50 70 72 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 4e 75 prAlloc; /* Nu
64d60 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 65 mber of allocate
64d70 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 d slots in apVar
64d80 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 70 72 Expr[] */. Expr
64d90 20 2a 2a 61 70 56 61 72 45 78 70 72 3b 20 20 20 **apVarExpr;
64da0 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 /* Pointers to
64db0 3a 61 61 61 20 61 6e 64 20 24 61 61 61 61 20 77 :aaa and $aaaa w
64dc0 69 6c 64 63 61 72 64 20 65 78 70 72 65 73 73 69 ildcard expressi
64dd0 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c ons */. int nAl
64de0 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ias; /*
64df0 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 73 Number of alias
64e00 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f ed result set co
64e10 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e lumns */. int n
64e20 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 20 AliasAlloc;
64e30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c /* Number of all
64e40 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 66 6f 72 ocated slots for
64e50 20 61 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 aAlias[] */. i
64e60 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 nt *aAlias;
64e70 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 /* Register
64e80 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 69 used to hold ali
64e90 61 73 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a 20 ased result */.
64ea0 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 u8 explain;
64eb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
64ec0 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 the EXPLAIN fla
64ed0 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 g is found on th
64ee0 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b e query */. Tok
64ef0 65 6e 20 73 45 72 72 54 6f 6b 65 6e 3b 20 20 20 en sErrToken;
64f00 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 61 /* The token a
64f10 74 20 77 68 69 63 68 20 74 68 65 20 65 72 72 6f t which the erro
64f20 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 20 20 r occurred */.
64f30 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e Token sNameToken
64f40 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 ; /* Token wi
64f50 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20 73 th unqualified s
64f60 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d chema object nam
64f70 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 e */. Token sLa
64f80 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 stToken; /* T
64f90 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 61 he last token pa
64fa0 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rsed */. const
64fb0 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 2f char *zSql; /
64fc0 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 2a * All SQL text *
64fd0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
64fe0 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c 20 zTail; /* All
64ff0 53 51 4c 20 74 65 78 74 20 70 61 73 74 20 74 68 SQL text past th
65000 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c 6f 6e e last semicolon
65010 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54 61 62 parsed */. Tab
65020 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b 20 20 le *pNewTable;
65030 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62 65 69 /* A table bei
65040 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 ng constructed b
65050 79 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 2a y CREATE TABLE *
65060 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e 65 /. Trigger *pNe
65070 77 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f 2a wTrigger; /*
65080 20 54 72 69 67 67 65 72 20 75 6e 64 65 72 20 63 Trigger under c
65090 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20 43 52 onstruct by a CR
650a0 45 41 54 45 20 54 52 49 47 47 45 52 20 2a 2f 0a EATE TRIGGER */.
650b0 20 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a TriggerStack *
650c0 74 72 69 67 53 74 61 63 6b 3b 20 20 2f 2a 20 54 trigStack; /* T
650d0 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73 20 62 rigger actions b
650e0 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 eing coded */.
650f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 75 74 const char *zAut
65100 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20 54 68 65 hContext; /* The
65110 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6th parameter t
65120 6f 20 64 62 2d 3e 78 41 75 74 68 20 63 61 6c 6c o db->xAuth call
65130 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e 64 65 66 backs */.#ifndef
65140 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
65150 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 6f 6b 65 TUALTABLE. Toke
65160 6e 20 73 41 72 67 3b 20 20 20 20 20 20 20 20 20 n sArg;
65170 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 /* Comple
65180 74 65 20 74 65 78 74 20 6f 66 20 61 20 6d 6f 64 te text of a mod
65190 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a ule argument */.
651a0 20 20 75 38 20 64 65 63 6c 61 72 65 56 74 61 62 u8 declareVtab
651b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
651c0 54 72 75 65 20 69 66 20 69 6e 73 69 64 65 20 73 True if inside s
651d0 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 qlite3_declare_v
651e0 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e tab() */. int n
651f0 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20 VtabLock;
65200 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
65210 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 of virtual table
65220 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 54 s to lock */. T
65230 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 4c 6f 63 able **apVtabLoc
65240 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 k; /* Poi
65250 6e 74 65 72 20 74 6f 20 76 69 72 74 75 61 6c 20 nter to virtual
65260 74 61 62 6c 65 73 20 6e 65 65 64 69 6e 67 20 6c tables needing l
65270 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 ocking */.#endif
65280 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 3b 20 . int nHeight;
65290 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
652a0 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65 pression tree he
652b0 69 67 68 74 20 6f 66 20 63 75 72 72 65 6e 74 20 ight of current
652c0 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f 0a 20 20 sub-select */.
652d0 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69 65 54 61 Table *pZombieTa
652e0 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 b; /* List
652f0 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 of Table objects
65300 20 74 6f 20 64 65 6c 65 74 65 20 61 66 74 65 72 to delete after
65310 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a 7d 3b 0a code gen */.};.
65320 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
65330 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
65340 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 . #define IN_DE
65350 43 4c 41 52 45 5f 56 54 41 42 20 30 0a 23 65 6c CLARE_VTAB 0.#el
65360 73 65 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f se. #define IN_
65370 44 45 43 4c 41 52 45 5f 56 54 41 42 20 28 70 50 DECLARE_VTAB (pP
65380 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 arse->declareVta
65390 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a b).#endif../*.**
653a0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
653b0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
653c0 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 64 ructure can be d
653d0 65 63 6c 61 72 65 64 20 6f 6e 20 61 20 73 74 61 eclared on a sta
653e0 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a 2a 20 74 ck and used.** t
653f0 6f 20 73 61 76 65 20 74 68 65 20 50 61 72 73 65 o save the Parse
65400 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 76 61 .zAuthContext va
65410 6c 75 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 lue so that it c
65420 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 6c an be restored l
65430 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ater..*/.struct
65440 41 75 74 68 43 6f 6e 74 65 78 74 20 7b 0a 20 20 AuthContext {.
65450 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 75 74 const char *zAut
65460 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f 2a 20 50 hContext; /* P
65470 75 74 20 73 61 76 65 64 20 50 61 72 73 65 2e 7a ut saved Parse.z
65480 41 75 74 68 43 6f 6e 74 65 78 74 20 68 65 72 65 AuthContext here
65490 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 */. Parse *pPa
654a0 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 rse;
654b0 20 20 2f 2a 20 54 68 65 20 50 61 72 73 65 20 73 /* The Parse s
654c0 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d 3b 0a 0a tructure */.};..
654d0 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c 64 20 66 /*.** Bitfield f
654e0 6c 61 67 73 20 66 6f 72 20 50 32 20 76 61 6c 75 lags for P2 valu
654f0 65 20 69 6e 20 4f 50 5f 49 6e 73 65 72 74 20 61 e in OP_Insert a
65500 6e 64 20 4f 50 5f 44 65 6c 65 74 65 0a 2a 2f 0a nd OP_Delete.*/.
65510 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4e #define OPFLAG_N
65520 43 48 41 4e 47 45 20 20 20 31 20 20 20 20 2f 2a CHANGE 1 /*
65530 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 Set to update d
65540 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 b->nChange */.#d
65550 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 efine OPFLAG_LAS
65560 54 52 4f 57 49 44 20 32 20 20 20 20 2f 2a 20 53 TROWID 2 /* S
65570 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d et to update db-
65580 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f 0a 23 64 >lastRowid */.#d
65590 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 49 53 55 efine OPFLAG_ISU
655a0 50 44 41 54 45 20 20 34 20 20 20 20 2f 2a 20 54 PDATE 4 /* T
655b0 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 his OP_Insert is
655c0 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a an sql UPDATE *
655d0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 /.#define OPFLAG
655e0 5f 41 50 50 45 4e 44 20 20 20 20 38 20 20 20 20 _APPEND 8
655f0 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65 6c /* This is likel
65600 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e y to be an appen
65610 64 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61 63 68 d */../*. * Each
65620 20 74 72 69 67 67 65 72 20 70 72 65 73 65 6e 74 trigger present
65630 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
65640 20 73 63 68 65 6d 61 20 69 73 20 73 74 6f 72 65 schema is store
65650 64 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 d as an instance
65660 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 20 54 72 of. * struct Tr
65670 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a 20 50 6f igger. . *. * Po
65680 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 74 61 6e inters to instan
65690 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 72 ces of struct Tr
656a0 69 67 67 65 72 20 61 72 65 20 73 74 6f 72 65 64 igger are stored
656b0 20 69 6e 20 74 77 6f 20 77 61 79 73 2e 0a 20 2a in two ways.. *
656c0 20 31 2e 20 49 6e 20 74 68 65 20 22 74 72 69 67 1. In the "trig
656d0 48 61 73 68 22 20 68 61 73 68 20 74 61 62 6c 65 Hash" hash table
656e0 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 73 71 (part of the sq
656f0 6c 69 74 65 33 2a 20 74 68 61 74 20 72 65 70 72 lite3* that repr
65700 65 73 65 6e 74 73 20 74 68 65 20 0a 20 2a 20 20 esents the . *
65710 20 20 64 61 74 61 62 61 73 65 29 2e 20 54 68 69 database). Thi
65720 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 67 65 72 s allows Trigger
65730 20 73 74 72 75 63 74 75 72 65 73 20 74 6f 20 62 structures to b
65740 65 20 72 65 74 72 69 65 76 65 64 20 62 79 20 6e e retrieved by n
65750 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c 20 74 ame.. * 2. All t
65760 72 69 67 67 65 72 73 20 61 73 73 6f 63 69 61 74 riggers associat
65770 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 ed with a single
65780 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 20 6c 69 table form a li
65790 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 69 6e 67 nked list, using
657a0 20 74 68 65 0a 20 2a 20 20 20 20 70 4e 65 78 74 the. * pNext
657b0 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 72 75 63 member of struc
657c0 74 20 54 72 69 67 67 65 72 2e 20 41 20 70 6f 69 t Trigger. A poi
657d0 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 nter to the firs
657e0 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 t element of the
657f0 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 20 6c 69 . * linked li
65800 73 74 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 st is stored as
65810 74 68 65 20 22 70 54 72 69 67 67 65 72 22 20 6d the "pTrigger" m
65820 65 6d 62 65 72 20 6f 66 20 74 68 65 20 61 73 73 ember of the ass
65830 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 20 73 74 ociated. * st
65840 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 2a 0a 20 ruct Table.. *.
65850 2a 20 54 68 65 20 22 73 74 65 70 5f 6c 69 73 74 * The "step_list
65860 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73 20 " member points
65870 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 to the first ele
65880 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e 6b 65 64 ment of a linked
65890 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 61 69 6e list. * contain
658a0 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74 61 74 ing the SQL stat
658b0 65 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 64 ements specified
658c0 20 61 73 20 74 68 65 20 74 72 69 67 67 65 72 20 as the trigger
658d0 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73 74 72 program.. */.str
658e0 75 63 74 20 54 72 69 67 67 65 72 20 7b 0a 20 20 uct Trigger {.
658f0 63 68 61 72 20 2a 6e 61 6d 65 3b 20 20 20 20 20 char *name;
65900 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
65910 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67 67 ame of the trigg
65920 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 er
65930 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 63 */. c
65940 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 20 20 20 har *table;
65950 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 /* The ta
65960 62 6c 65 20 6f 72 20 76 69 65 77 20 74 6f 20 77 ble or view to w
65970 68 69 63 68 20 74 68 65 20 74 72 69 67 67 65 72 hich the trigger
65980 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 20 75 38 applies */. u8
65990 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 op;
659a0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
659b0 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 TK_DELETE, TK_UP
659c0 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20 DATE, TK_INSERT
659d0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 75 38 20 */. u8
659e0 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 20 20 20 tr_tm;
659f0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 /* One of T
65a00 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 54 RIGGER_BEFORE, T
65a10 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a RIGGER_AFTER */.
65a20 20 20 45 78 70 72 20 2a 70 57 68 65 6e 3b 20 20 Expr *pWhen;
65a30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
65a40 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 WHEN clause of
65a50 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 28 the expression (
65a60 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a may be NULL) */.
65a70 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d IdList *pColum
65a80 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49 66 20 ns; /* If
65a90 74 68 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 this is an UPDAT
65aa0 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 E OF <column-lis
65ab0 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 t> trigger,.
65ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65ad0 20 20 20 20 20 20 20 20 20 74 68 65 20 3c 63 6f the <co
65ae0 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 73 74 lumn-list> is st
65af0 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 54 ored here */. T
65b00 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e 3b 20 oken nameToken;
65b10 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 /* Token
65b20 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 4e 61 6d 65 containing zName
65b30 2e 20 55 73 65 20 64 75 72 69 6e 67 20 70 61 72 . Use during par
65b40 73 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20 20 53 sing only */. S
65b50 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 chema *pSchema;
65b60 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 /* Schema
65b70 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
65b80 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 63 68 trigger */. Sch
65b90 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d 61 3b ema *pTabSchema;
65ba0 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63 /* Schema c
65bb0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 ontaining the ta
65bc0 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 ble */. Trigger
65bd0 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73 74 3b Step *step_list;
65be0 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 6f 66 /* Link list of
65bf0 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d trigger program
65c00 20 73 74 65 70 73 20 20 20 20 20 20 20 20 20 20 steps
65c10 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 */. Trigger
65c20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 *pNext;
65c30 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72 20 /* Next trigger
65c40 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
65c50 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a the table */.};.
65c60 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67 65 72 ./*.** A trigger
65c70 20 69 73 20 65 69 74 68 65 72 20 61 20 42 45 46 is either a BEF
65c80 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45 52 20 ORE or an AFTER
65c90 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 66 6f trigger. The fo
65ca0 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 llowing constant
65cb0 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 77 s.** determine w
65cc0 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 hich. .**.** If
65cd0 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74 69 70 there are multip
65ce0 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79 6f 75 le triggers, you
65cf0 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65 20 42 might of some B
65d00 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65 20 41 EFORE and some A
65d10 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 FTER..** In that
65d20 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f 6e 73 cases, the cons
65d30 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61 6e 20 tants below can
65d40 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68 65 72 be ORed together
65d50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 49 ..*/.#define TRI
65d60 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31 0a 23 GGER_BEFORE 1.#
65d70 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 5f 41 define TRIGGER_A
65d80 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 2a 20 FTER 2../*. *
65d90 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73 An instance of s
65da0 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 truct TriggerSte
65db0 70 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f p is used to sto
65dc0 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 re a single SQL
65dd0 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74 68 61 statement. * tha
65de0 74 20 69 73 20 61 20 70 61 72 74 20 6f 66 20 61 t is a part of a
65df0 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d trigger-program
65e00 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 6e 63 . . *. * Instanc
65e10 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 es of struct Tri
65e20 67 67 65 72 53 74 65 70 20 61 72 65 20 73 74 6f ggerStep are sto
65e30 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 79 20 red in a singly
65e40 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c 69 6e linked list (lin
65e50 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74 68 65 ked. * using the
65e60 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 72 29 "pNext" member)
65e70 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 referenced by t
65e80 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 20 6d he "step_list" m
65e90 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a 20 2a ember of the . *
65ea0 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 72 75 associated stru
65eb0 63 74 20 54 72 69 67 67 65 72 20 69 6e 73 74 61 ct Trigger insta
65ec0 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74 20 65 nce. The first e
65ed0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6c 69 lement of the li
65ee0 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20 2a 20 nked list is. *
65ef0 74 68 65 20 66 69 72 73 74 20 73 74 65 70 20 6f the first step o
65f00 66 20 74 68 65 20 74 72 69 67 67 65 72 2d 70 72 f the trigger-pr
65f10 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 54 68 ogram.. * . * Th
65f20 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20 69 6e e "op" member in
65f30 64 69 63 61 74 65 73 20 77 68 65 74 68 65 72 20 dicates whether
65f40 74 68 69 73 20 69 73 20 61 20 22 44 45 4c 45 54 this is a "DELET
65f50 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20 22 55 E", "INSERT", "U
65f60 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22 53 45 PDATE" or. * "SE
65f70 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e 74 2e LECT" statement.
65f80 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 The meanings of
65f90 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d 62 65 the other membe
65fa0 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 rs is determined
65fb0 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61 6c 75 by the . * valu
65fc0 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66 6f 6c e of "op" as fol
65fd0 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f 70 20 lows:. *. * (op
65fe0 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a 20 2a == TK_INSERT). *
65ff0 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20 73 74 orconf -> st
66000 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f 4e 46 ores the ON CONF
66010 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 0a 20 LICT algorithm.
66020 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e 20 49 * pSelect -> I
66030 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 f this is an INS
66040 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c ERT INTO ... SEL
66050 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e ECT ... statemen
66060 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 t, then. *
66070 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74 6f this sto
66080 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f res a pointer to
66090 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 the SELECT stat
660a0 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69 73 65 ement. Otherwise
660b0 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 65 74 NULL.. * target
660c0 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 -> A token h
660d0 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 olding the name
660e0 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 of the table to
660f0 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 insert into.. *
66100 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 pExprList -> If
66110 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 this is an INSER
66120 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 T INTO ... VALUE
66130 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c S ... statement,
66140 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 then. *
66150 20 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 this store
66160 73 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 s values to be i
66170 6e 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 nserted. Otherwi
66180 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c se NULL.. * pIdL
66190 69 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 ist -> If this
661a0 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e is an INSERT IN
661b0 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d TO ... (<column-
661c0 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e names>) VALUES .
661d0 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 .. . *
661e0 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 statement, t
661f0 68 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 hen this stores
66200 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 the column-names
66210 20 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 to be. *
66220 20 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 inserted
66230 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 into.. *. * (op
66240 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a == TK_DELETE). *
66250 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 target -> A
66260 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 token holding th
66270 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
66280 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72 ble to delete fr
66290 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20 om.. * pWhere
662a0 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c -> The WHERE cl
662b0 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45 ause of the DELE
662c0 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 TE statement if
662d0 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 one is specified
662e0 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 .. *
662f0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c Otherwise NULL
66300 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20 .. * . * (op ==
66310 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61 TK_UPDATE). * ta
66320 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b rget -> A tok
66330 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e en holding the n
66340 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
66350 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77 73 20 to update rows
66360 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20 of.. * pWhere
66370 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c -> The WHERE cl
66380 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41 ause of the UPDA
66390 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 TE statement if
663a0 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 one is specified
663b0 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 .. *
663c0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c Otherwise NULL
663d0 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d .. * pExprList -
663e0 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68 65 20 > A list of the
663f0 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 columns to updat
66400 65 20 61 6e 64 20 74 68 65 20 65 78 70 72 65 73 e and the expres
66410 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74 65 0a sions to update.
66420 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
66430 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c them to. See sql
66440 69 74 65 33 55 70 64 61 74 65 28 29 20 64 6f 63 ite3Update() doc
66450 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 umentation of "p
66460 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 Changes". *
66470 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e argumen
66480 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 t.. * . */.struc
66490 74 20 54 72 69 67 67 65 72 53 74 65 70 20 7b 0a t TriggerStep {.
664a0 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 int op;
664b0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
664c0 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 TK_DELETE, TK_U
664d0 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 PDATE, TK_INSERT
664e0 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 , TK_SELECT */.
664f0 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 int orconf;
66500 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c /* OE_Roll
66510 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 back etc. */. T
66520 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 20 20 rigger *pTrig;
66530 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 /* The trigg
66540 65 72 20 74 68 61 74 20 74 68 69 73 20 73 74 65 er that this ste
66550 70 20 69 73 20 61 20 70 61 72 74 20 6f 66 20 2a p is a part of *
66560 2f 0a 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 /.. Select *pSe
66570 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c lect; /* Val
66580 69 64 20 66 6f 72 20 53 45 4c 45 43 54 20 61 6e id for SELECT an
66590 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a 20 20 20 d sometimes .
665a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
665b0 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 73 74 INSERT st
665c0 65 70 73 20 28 77 68 65 6e 20 70 45 78 70 72 4c eps (when pExprL
665d0 69 73 74 20 3d 3d 20 30 29 20 2a 2f 0a 20 20 54 ist == 0) */. T
665e0 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20 20 20 oken target;
665f0 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 /* Valid for
66600 20 44 45 4c 45 54 45 2c 20 55 50 44 41 54 45 2c DELETE, UPDATE,
66610 20 49 4e 53 45 52 54 20 73 74 65 70 73 20 2a 2f INSERT steps */
66620 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b . Expr *pWhere;
66630 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 /* Valid
66640 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 for DELETE, UPD
66650 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 ATE steps */. E
66660 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c 69 xprList *pExprLi
66670 73 74 3b 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 st; /* Valid for
66680 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e UPDATE statemen
66690 74 73 20 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 ts and sometimes
666a0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
666b0 20 20 20 20 20 20 20 20 20 20 20 20 20 49 4e 53 INS
666c0 45 52 54 20 73 74 65 70 73 20 28 77 68 65 6e 20 ERT steps (when
666d0 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 20 20 20 pSelect == 0)
666e0 20 20 20 20 20 20 2a 2f 0a 20 20 49 64 4c 69 73 */. IdLis
666f0 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 20 t *pIdList;
66700 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 49 4e 53 /* Valid for INS
66710 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73 20 6f ERT statements o
66720 6e 6c 79 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 nly */. Trigger
66730 53 74 65 70 20 2a 70 4e 65 78 74 3b 20 20 2f 2a Step *pNext; /*
66740 20 4e 65 78 74 20 69 6e 20 74 68 65 20 6c 69 6e Next in the lin
66750 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 54 72 69 67 k-list */. Trig
66760 67 65 72 53 74 65 70 20 2a 70 4c 61 73 74 3b 20 gerStep *pLast;
66770 20 2f 2a 20 4c 61 73 74 20 65 6c 65 6d 65 6e 74 /* Last element
66780 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e 20 56 in link-list. V
66790 61 6c 69 64 20 66 6f 72 20 31 73 74 20 65 6c 65 alid for 1st ele
667a0 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a m only */.};../*
667b0 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 . * An instance
667c0 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 of struct Trigge
667d0 72 53 74 61 63 6b 20 73 74 6f 72 65 73 20 69 6e rStack stores in
667e0 66 6f 72 6d 61 74 69 6f 6e 20 72 65 71 75 69 72 formation requir
667f0 65 64 20 64 75 72 69 6e 67 20 63 6f 64 65 0a 20 ed during code.
66800 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 * generation of
66810 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 a single trigger
66820 20 70 72 6f 67 72 61 6d 2e 20 57 68 69 6c 65 20 program. While
66830 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 the trigger prog
66840 72 61 6d 20 69 73 20 62 65 69 6e 67 0a 20 2a 20 ram is being. *
66850 63 6f 64 65 64 2c 20 69 74 73 20 61 73 73 6f 63 coded, its assoc
66860 69 61 74 65 64 20 54 72 69 67 67 65 72 53 74 61 iated TriggerSta
66870 63 6b 20 69 6e 73 74 61 6e 63 65 20 69 73 20 70 ck instance is p
66880 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 ointed to by the
66890 0a 20 2a 20 22 70 54 72 69 67 67 65 72 53 74 61 . * "pTriggerSta
668a0 63 6b 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 ck" member of th
668b0 65 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 e Parse structur
668c0 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 70 54 61 e.. *. * The pTa
668d0 62 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73 20 b member points
668e0 74 6f 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 to the table tha
668f0 74 20 74 72 69 67 67 65 72 73 20 61 72 65 20 62 t triggers are b
66900 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e 2e 20 54 eing coded on. T
66910 68 65 20 0a 20 2a 20 6e 65 77 49 64 78 20 6d 65 he . * newIdx me
66920 6d 62 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 68 mber contains th
66930 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 e index of the v
66940 64 62 65 20 63 75 72 73 6f 72 20 74 68 61 74 20 dbe cursor that
66950 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 74 65 points at the te
66960 6d 70 0a 20 2a 20 74 61 62 6c 65 20 74 68 61 74 mp. * table that
66970 20 73 74 6f 72 65 73 20 74 68 65 20 6e 65 77 2e stores the new.
66980 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20 49 66 * references. If
66990 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 new.* reference
669a0 73 20 61 72 65 20 6e 6f 74 20 76 61 6c 69 64 0a s are not valid.
669b0 20 2a 20 66 6f 72 20 74 68 65 20 74 72 69 67 67 * for the trigg
669c0 65 72 20 62 65 69 6e 67 20 63 6f 64 65 64 20 28 er being coded (
669d0 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 4f for example an O
669e0 4e 20 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 N DELETE trigger
669f0 29 2c 20 74 68 65 6e 20 6e 65 77 49 64 78 0a 20 ), then newIdx.
66a00 2a 20 69 73 20 73 65 74 20 74 6f 20 2d 31 2e 20 * is set to -1.
66a10 54 68 65 20 6f 6c 64 49 64 78 20 6d 65 6d 62 65 The oldIdx membe
66a20 72 20 69 73 20 61 6e 61 6c 6f 67 6f 75 73 20 74 r is analogous t
66a30 6f 20 6e 65 77 49 64 78 2c 20 66 6f 72 20 6f 6c o newIdx, for ol
66a40 64 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0a d.* references..
66a50 20 2a 0a 20 2a 20 54 68 65 20 4f 4e 20 43 4f 4e *. * The ON CON
66a60 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 6f 20 FLICT policy to
66a70 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 be used for the
66a80 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 trigger program
66a90 73 74 65 70 73 20 69 73 20 73 74 6f 72 65 64 20 steps is stored
66aa0 0a 20 2a 20 61 73 20 74 68 65 20 6f 72 63 6f 6e . * as the orcon
66ab0 66 20 6d 65 6d 62 65 72 2e 20 49 66 20 74 68 69 f member. If thi
66ac0 73 20 69 73 20 4f 45 5f 44 65 66 61 75 6c 74 2c s is OE_Default,
66ad0 20 74 68 65 6e 20 74 68 65 20 4f 4e 20 43 4f 4e then the ON CON
66ae0 46 4c 49 43 54 20 63 6c 61 75 73 65 20 0a 20 2a FLICT clause . *
66af0 20 73 70 65 63 69 66 69 65 64 20 66 6f 72 20 69 specified for i
66b00 6e 64 69 76 69 64 75 61 6c 20 74 72 69 67 67 65 ndividual trigge
66b10 72 73 20 73 74 65 70 73 20 69 73 20 75 73 65 64 rs steps is used
66b20 2e 0a 20 2a 0a 20 2a 20 73 74 72 75 63 74 20 54 .. *. * struct T
66b30 72 69 67 67 65 72 53 74 61 63 6b 20 68 61 73 20 riggerStack has
66b40 61 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 72 a "pNext" member
66b50 2c 20 74 6f 20 61 6c 6c 6f 77 20 6c 69 6e 6b 65 , to allow linke
66b60 64 20 6c 69 73 74 73 20 74 6f 20 62 65 0a 20 2a d lists to be. *
66b70 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 57 68 constructed. Wh
66b80 65 6e 20 63 6f 64 69 6e 67 20 6e 65 73 74 65 64 en coding nested
66b90 20 74 72 69 67 67 65 72 73 20 28 74 72 69 67 67 triggers (trigg
66ba0 65 72 73 20 66 69 72 65 64 20 62 79 20 6f 74 68 ers fired by oth
66bb0 65 72 20 74 72 69 67 67 65 72 73 29 0a 20 2a 20 er triggers). *
66bc0 65 61 63 68 20 6e 65 73 74 65 64 20 74 72 69 67 each nested trig
66bd0 67 65 72 20 73 74 6f 72 65 73 20 69 74 73 20 70 ger stores its p
66be0 61 72 65 6e 74 20 74 72 69 67 67 65 72 27 73 20 arent trigger's
66bf0 54 72 69 67 67 65 72 53 74 61 63 6b 20 61 73 20 TriggerStack as
66c00 74 68 65 20 22 70 4e 65 78 74 22 20 0a 20 2a 20 the "pNext" . *
66c10 70 6f 69 6e 74 65 72 2e 20 4f 6e 63 65 20 74 68 pointer. Once th
66c20 65 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 e nested trigger
66c30 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64 2c has been coded,
66c40 20 74 68 65 20 70 4e 65 78 74 20 76 61 6c 75 65 the pNext value
66c50 20 69 73 20 72 65 73 74 6f 72 65 64 0a 20 2a 20 is restored. *
66c60 74 6f 20 74 68 65 20 70 54 72 69 67 67 65 72 53 to the pTriggerS
66c70 74 61 63 6b 20 6d 65 6d 62 65 72 20 6f 66 20 74 tack member of t
66c80 68 65 20 50 61 72 73 65 20 73 74 75 63 74 75 72 he Parse stuctur
66c90 65 20 61 6e 64 20 63 6f 64 69 6e 67 20 6f 66 20 e and coding of
66ca0 74 68 65 20 70 61 72 65 6e 74 0a 20 2a 20 74 72 the parent. * tr
66cb0 69 67 67 65 72 20 63 6f 6e 74 69 6e 75 65 73 2e igger continues.
66cc0 0a 20 2a 0a 20 2a 20 42 65 66 6f 72 65 20 61 20 . *. * Before a
66cd0 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 20 69 nested trigger i
66ce0 73 20 63 6f 64 65 64 2c 20 74 68 65 20 6c 69 6e s coded, the lin
66cf0 6b 65 64 20 6c 69 73 74 20 70 6f 69 6e 74 65 64 ked list pointed
66d00 20 74 6f 20 62 79 20 74 68 65 20 0a 20 2a 20 70 to by the . * p
66d10 54 72 69 67 67 65 72 53 74 61 63 6b 20 69 73 20 TriggerStack is
66d20 73 63 61 6e 6e 65 64 20 74 6f 20 65 6e 73 75 72 scanned to ensur
66d30 65 20 74 68 61 74 20 74 68 65 20 74 72 69 67 67 e that the trigg
66d40 65 72 20 69 73 20 6e 6f 74 20 61 62 6f 75 74 20 er is not about
66d50 74 6f 20 62 65 20 63 6f 64 65 64 0a 20 2a 20 72 to be coded. * r
66d60 65 63 75 72 73 69 76 65 6c 79 2e 20 49 66 20 74 ecursively. If t
66d70 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 his condition is
66d80 20 64 65 74 65 63 74 65 64 2c 20 74 68 65 20 6e detected, the n
66d90 65 73 74 65 64 20 74 72 69 67 67 65 72 20 69 73 ested trigger is
66da0 20 6e 6f 74 20 63 6f 64 65 64 2e 0a 20 2a 2f 0a not coded.. */.
66db0 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 struct TriggerSt
66dc0 61 63 6b 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 ack {. Table *p
66dd0 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Tab; /*
66de0 54 61 62 6c 65 20 74 68 61 74 20 74 72 69 67 67 Table that trigg
66df0 65 72 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c ers are currentl
66e00 79 20 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e y being coded on
66e10 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 */. int newIdx
66e20 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e ; /* In
66e30 64 65 78 20 6f 66 20 76 64 62 65 20 63 75 72 73 dex of vdbe curs
66e40 6f 72 20 74 6f 20 22 6e 65 77 22 20 74 65 6d 70 or to "new" temp
66e50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
66e60 6f 6c 64 49 64 78 3b 20 20 20 20 20 20 20 20 20 oldIdx;
66e70 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76 64 62 /* Index of vdb
66e80 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6f 6c 64 e cursor to "old
66e90 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a " temp table */.
66ea0 20 20 75 33 32 20 6e 65 77 43 6f 6c 4d 61 73 6b u32 newColMask
66eb0 3b 0a 20 20 75 33 32 20 6f 6c 64 43 6f 6c 4d 61 ;. u32 oldColMa
66ec0 73 6b 3b 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 sk;. int orconf
66ed0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 ; /* Cu
66ee0 72 72 65 6e 74 20 6f 72 63 6f 6e 66 20 70 6f 6c rrent orconf pol
66ef0 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e icy */. int ign
66f00 6f 72 65 4a 75 6d 70 3b 20 20 20 20 20 20 2f 2a oreJump; /*
66f10 20 77 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74 where to jump t
66f20 6f 20 66 6f 72 20 61 20 52 41 49 53 45 28 49 47 o for a RAISE(IG
66f30 4e 4f 52 45 29 20 2a 2f 0a 20 20 54 72 69 67 67 NORE) */. Trigg
66f40 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 er *pTrigger;
66f50 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 63 /* The trigger c
66f60 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 urrently being c
66f70 6f 64 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 oded */. Trigge
66f80 72 53 74 61 63 6b 20 2a 70 4e 65 78 74 3b 20 2f rStack *pNext; /
66f90 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72 20 64 * Next trigger d
66fa0 6f 77 6e 20 6f 6e 20 74 68 65 20 74 72 69 67 67 own on the trigg
66fb0 65 72 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b 0a 0a er stack */.};..
66fc0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
66fd0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f ing structure co
66fe0 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 ntains informati
66ff0 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 73 on used by the s
67000 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a 20 72 qliteFix....** r
67010 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65 79 20 outines as they
67020 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 walk the parse t
67030 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61 74 61 ree to make data
67040 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 0a base references.
67050 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20 0a 2a ** explicit. .*
67060 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
67070 20 44 62 46 69 78 65 72 20 44 62 46 69 78 65 72 DbFixer DbFixer
67080 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78 65 72 ;.struct DbFixer
67090 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 {. Parse *pPar
670a0 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 se; /* The
670b0 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e parsing context.
670c0 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 Error messages
670d0 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
670e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
670f0 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 Db; /* Make s
67100 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 73 20 ure all objects
67110 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e are contained in
67120 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a this database *
67130 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
67140 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 65 20 zType; /* Type
67150 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 of the container
67160 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f - used for erro
67170 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 r messages */.
67180 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 const Token *pNa
67190 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 me; /* Name of t
671a0 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20 75 he container - u
671b0 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 sed for error me
671c0 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a ssages */.};../*
671d0 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65 64 20 .** An objected
671e0 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75 6c 61 used to accumula
671f0 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 te the text of a
67200 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 77 65 string where we
67210 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 .** do not neces
67220 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f 77 20 sarily know how
67230 62 69 67 20 74 68 65 20 73 74 72 69 6e 67 20 77 big the string w
67240 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20 65 6e ill be in the en
67250 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74 72 d..*/.struct Str
67260 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69 74 65 Accum {. sqlite
67270 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 3 *db; /
67280 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 61 62 * Optional datab
67290 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 ase for lookasid
672a0 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c 4c 20 e. Can be NULL
672b0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61 73 65 */. char *zBase
672c0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 ; /* A b
672d0 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 ase allocation.
672e0 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 Not from malloc
672f0 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 65 . */. char *zTe
67300 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 xt; /* T
67310 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c 65 63 he string collec
67320 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 ted so far */.
67330 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20 20 20 int nChar;
67340 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f /* Length o
67350 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 6f 20 f the string so
67360 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 41 far */. int nA
67370 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a lloc; /*
67380 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 Amount of space
67390 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 7a 54 allocated in zT
673a0 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20 6d 78 ext */. int mx
673b0 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a Alloc; /*
673c0 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 Maximum allowed
673d0 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 2a string length *
673e0 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f 63 46 /. u8 mallocF
673f0 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65 63 6f ailed; /* Beco
67400 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 mes true if any
67410 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
67420 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75 38 20 n fails */. u8
67430 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 20 20 useMalloc;
67440 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 54 65 /* True if zTe
67450 78 74 20 69 73 20 65 6e 6c 61 72 67 65 61 62 6c xt is enlargeabl
67460 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 20 e using realloc
67470 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42 69 67 */. u8 tooBig
67480 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 63 ; /* Bec
67490 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73 74 72 omes true if str
674a0 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65 64 73 ing size exceeds
674b0 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f limits */.};../
674c0 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 *.** A pointer t
674d0 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 o this structure
674e0 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d is used to comm
674f0 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d 61 74 unicate informat
67500 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 ion.** from sqli
67510 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f 50 te3Init and OP_P
67520 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 6f 20 arseSchema into
67530 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 74 43 the sqlite3InitC
67540 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70 65 allback..*/.type
67550 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 20 73 def struct {. s
67560 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
67570 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
67580 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c se being initial
67590 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 ized */. int iD
675a0 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b; /*
675b0 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 74 61 0 for main data
675c0 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 45 4d base. 1 for TEM
675d0 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 41 43 P, 2.. for ATTAC
675e0 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a Hed */. char **
675f0 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a 20 pzErrMsg; /*
67600 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 Error message st
67610 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 69 ored here */. i
67620 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
67630 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 /* Result cod
67640 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f e stored here */
67650 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f 2a .} InitData;../*
67660 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 63 6f .** Structure co
67670 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c 20 ntaining global
67680 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 64 61 configuration da
67690 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c 69 74 ta for the SQLit
676a0 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a e library..**.**
676b0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
676c0 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73 6f also contains so
676d0 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 me state informa
676e0 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 tion..*/.struct
676f0 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 7b 0a Sqlite3Config {.
67700 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74 3b 20 int bMemstat;
67710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67720 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 /* True to e
67730 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 74 61 nable memory sta
67740 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62 43 6f tus */. int bCo
67750 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 reMutex;
67760 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
67770 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63 6f 72 ue to enable cor
67780 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 e mutexing */.
67790 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78 3b 20 int bFullMutex;
677a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
677b0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
677c0 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78 69 6e ble full mutexin
677d0 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 74 72 g */. int mxStr
677e0 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 len;
677f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
67800 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 mum string lengt
67810 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c 6f 6f h */. int szLoo
67820 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 kaside;
67830 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 /* Defa
67840 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 ult lookaside bu
67850 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20 69 ffer size */. i
67860 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 nt nLookaside;
67870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67880 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b /* Default look
67890 61 73 69 64 65 20 62 75 66 66 65 72 20 63 6f 75 aside buffer cou
678a0 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f nt */. sqlite3_
678b0 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 20 20 mem_methods m;
678c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 /* Low
678d0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
678e0 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72 66 61 location interfa
678f0 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ce */. sqlite3_
67900 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 6d 75 mutex_methods mu
67910 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 77 tex; /* Low
67920 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69 6e 74 -level mutex int
67930 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 erface */. sqli
67940 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
67950 64 73 20 70 63 61 63 68 65 3b 20 20 20 20 2f 2a ds pcache; /*
67960 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 67 65 2d Low-level page-
67970 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65 20 cache interface
67980 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 65 61 70 */. void *pHeap
67990 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
679a0 20 20 20 20 20 20 20 2f 2a 20 48 65 61 70 20 73 /* Heap s
679b0 74 6f 72 61 67 65 20 73 70 61 63 65 20 2a 2f 0a torage space */.
679c0 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 20 20 20 int nHeap;
679d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
679e0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 /* Size of p
679f0 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 Heap[] */. int
67a00 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b 20 20 20 mnReq, mxReq;
67a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
67a20 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 68 65 61 Min and max hea
67a30 70 20 72 65 71 75 65 73 74 73 20 73 69 7a 65 73 p requests sizes
67a40 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 72 */. void *pScr
67a50 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
67a60 20 20 20 20 20 20 20 20 2f 2a 20 53 63 72 61 74 /* Scrat
67a70 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 ch memory */. i
67a80 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20 nt szScratch;
67a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67aa0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 /* Size of each
67ab0 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 scratch buffer
67ac0 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 61 74 63 */. int nScratc
67ad0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
67ae0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
67af0 20 6f 66 20 73 63 72 61 74 63 68 20 62 75 66 66 of scratch buff
67b00 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ers */. void *p
67b10 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
67b20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
67b30 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 ge cache memory
67b40 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b */. int szPage;
67b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67b60 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
67b70 66 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 70 f each page in p
67b80 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 Page[] */. int
67b90 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 nPage;
67ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
67bb0 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
67bc0 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 in pPage[] */.
67bd0 20 69 6e 74 20 6d 78 50 61 72 73 65 72 53 74 61 int mxParserSta
67be0 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ck;
67bf0 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d 20 64 65 /* maximum de
67c00 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65 pth of the parse
67c10 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 r stack */. int
67c20 20 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 sharedCacheEnab
67c30 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f led; /
67c40 2a 20 74 72 75 65 20 69 66 20 73 68 61 72 65 64 * true if shared
67c50 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 6e 61 62 -cache mode enab
67c60 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 led */. /* The
67c70 61 62 6f 76 65 20 6d 69 67 68 74 20 62 65 20 69 above might be i
67c80 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 6e 6f nitialized to no
67c90 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20 66 6f 6c n-zero. The fol
67ca0 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74 6f 20 61 lowing need to a
67cb0 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e 69 74 69 lways. ** initi
67cc0 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c 20 68 6f ally be zero, ho
67cd0 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 wever. */. int
67ce0 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 isInit;
67cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
67d00 20 54 72 75 65 20 61 66 74 65 72 20 69 6e 69 74 True after init
67d10 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 73 20 66 ialization has f
67d20 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 69 6e 74 inished */. int
67d30 20 69 6e 50 72 6f 67 72 65 73 73 3b 20 20 20 20 inProgress;
67d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
67d50 2a 20 54 72 75 65 20 77 68 69 6c 65 20 69 6e 69 * True while ini
67d60 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e 20 70 tialization in p
67d70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 rogress */. int
67d80 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 isMallocInit;
67d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
67da0 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c * True after mal
67db0 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a loc is initializ
67dc0 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ed */. sqlite3_
67dd0 6d 75 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 mutex *pInitMute
67de0 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 x; /* Mut
67df0 65 78 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 ex used by sqlit
67e00 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
67e10 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 */. int nRefIni
67e20 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 tMutex;
67e30 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
67e40 20 6f 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e of users of pIn
67e50 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f itMutex */.};../
67e60 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 *.** Context poi
67e70 6e 74 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e nter passed down
67e80 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 through the tre
67e90 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 e-walk..*/.struc
67ea0 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 t Walker {. int
67eb0 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b (*xExprCallback
67ec0 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a )(Walker*, Expr*
67ed0 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 ); /* Callba
67ee0 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f ck for expressio
67ef0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 ns */. int (*xS
67f00 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 electCallback)(W
67f10 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b alker*,Select*);
67f20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f /* Callback fo
67f30 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 r SELECTs */. P
67f40 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
67f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67f60 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
67f70 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a er context. */.
67f80 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 union {
67f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
67fb0 78 74 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 xtra data for ca
67fc0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 llback */. Na
67fd0 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 meContext *pNC;
67fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67ff0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 /* Nami
68000 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
68010 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
68020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68030 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
68040 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f Integer value */
68050 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 . } u;.};../* F
68060 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
68070 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ons */.SQLITE_PR
68080 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
68090 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 3WalkExpr(Walker
680a0 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
680b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
680c0 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
680d0 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c t(Walker*, ExprL
680e0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
680f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
68100 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 3WalkSelect(Walk
68110 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 er*, Select*);.S
68120 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
68130 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c t sqlite3WalkSel
68140 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c ectExpr(Walker*,
68150 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 Select*);.SQLIT
68160 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
68170 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 lite3WalkSelectF
68180 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c rom(Walker*, Sel
68190 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 ect*);../*.** Re
681a0 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 turn code from t
681b0 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 he parse-tree wa
681c0 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 lking primitives
681d0 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 and their.** ca
681e0 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 llbacks..*/.#def
681f0 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 ine WRC_Continue
68200 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 0 /* Conti
68210 6e 75 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 nue down into ch
68220 69 6c 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e ildren */.#defin
68230 65 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 e WRC_Prune
68240 20 20 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 1 /* Omit ch
68250 69 6c 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 ildren but conti
68260 6e 75 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c nue walking sibl
68270 69 6e 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ings */.#define
68280 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 WRC_Abort
68290 32 20 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 2 /* Abandon t
682a0 68 65 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a he tree walk */.
682b0 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 ./*.** Assuming
682c0 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 zIn points to th
682d0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
682e0 61 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 a UTF-8 characte
682f0 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 r,.** advance zI
68300 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 n to point to th
68310 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
68320 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 the next UTF-8 c
68330 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 haracter..*/.#de
68340 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 fine SQLITE_SKIP
68350 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 _UTF8(zIn) {
68360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68370 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a \. if( (*(z
68380 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 In++))>=0xc0 ){
68390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
683a0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
683b0 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 while( (*zIn
683c0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b & 0xc0)==0x80 ){
683d0 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 zIn++; }
683e0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
683f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
68420 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 .}../*.** The SQ
68430 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
68440 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65 T macro can be e
68450 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 ither a constant
68460 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e (for production
68470 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61 .** builds) or a
68480 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 function call (
68490 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20 for debugging).
684a0 20 49 66 20 69 74 20 69 73 20 61 20 66 75 6e 63 If it is a func
684b0 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 tion call,.** it
684c0 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 allows the oper
684d0 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72 ator to set a br
684e0 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20 eakpoint at the
684f0 73 70 6f 74 20 77 68 65 72 65 20 64 61 74 61 62 spot where datab
68500 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f ase.** corruptio
68510 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63 n is first detec
68520 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ted..*/.#ifdef S
68530 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
68540 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
68550 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 sqlite3Corrupt(
68560 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 void);.# define
68570 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
68580 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75 KPT sqlite3Corru
68590 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 pt().#else.# def
685a0 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 ine SQLITE_CORRU
685b0 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 PT_BKPT SQLITE_C
685c0 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f ORRUPT.#endif../
685d0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
685e0 6e 67 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 ng macros mimic
685f0 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 the standard lib
68600 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 rary functions t
68610 6f 75 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 oupper(),.** iss
68620 70 61 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 pace(), isalnum(
68630 29 2c 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 ), isdigit() and
68640 20 69 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 isxdigit(), res
68650 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a pectively. The.*
68660 2a 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e * sqlite version
68670 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 s only work for
68680 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 73 ASCII characters
68690 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 , regardless of
686a0 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 locale..*/.#ifde
686b0 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 f SQLITE_ASCII.#
686c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 define sqlite3T
686d0 6f 75 70 70 65 72 28 78 29 20 20 28 28 78 29 26 oupper(x) ((x)&
686e0 7e 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 ~(sqlite3CtypeMa
686f0 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 p[(unsigned char
68700 29 28 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64 )(x)]&0x20)).# d
68710 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 efine sqlite3Iss
68720 70 61 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 pace(x) (sqlit
68730 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 e3CtypeMap[(unsi
68740 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 gned char)(x)]&0
68750 78 30 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 x01).# define sq
68760 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 lite3Isalnum(x)
68770 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d (sqlite3CtypeM
68780 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 ap[(unsigned cha
68790 72 29 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 r)(x)]&0x06).# d
687a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 efine sqlite3Isa
687b0 6c 70 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 lpha(x) (sqlit
687c0 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 e3CtypeMap[(unsi
687d0 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 gned char)(x)]&0
687e0 78 30 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 x02).# define sq
687f0 6c 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 lite3Isdigit(x)
68800 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d (sqlite3CtypeM
68810 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 ap[(unsigned cha
68820 72 29 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 r)(x)]&0x04).# d
68830 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 efine sqlite3Isx
68840 64 69 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 digit(x) (sqlit
68850 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 e3CtypeMap[(unsi
68860 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 gned char)(x)]&0
68870 78 30 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 x08).# define sq
68880 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 lite3Tolower(x)
68890 20 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 (sqlite3UpperT
688a0 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 oLower[(unsigned
688b0 20 63 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 char)(x)]).#els
688c0 65 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79 e.# include <cty
688d0 70 65 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 73 pe.h>.# define s
688e0 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 qlite3Toupper(x)
688f0 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69 toupper((unsi
68900 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 gned char)(x)).#
68910 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
68920 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 70 sspace(x) issp
68930 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 ace((unsigned ch
68940 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 ar)(x)).# define
68950 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 sqlite3Isalnum(
68960 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e x) isalnum((un
68970 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 signed char)(x))
68980 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
68990 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 73 3Isalpha(x) is
689a0 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20 alpha((unsigned
689b0 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 char)(x)).# defi
689c0 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 ne sqlite3Isdigi
689d0 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 28 t(x) isdigit((
689e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
689f0 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 )).# define sqli
68a00 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20 te3Isxdigit(x)
68a10 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e isxdigit((unsign
68a20 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
68a30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c efine sqlite3Tol
68a40 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 ower(x) tolowe
68a50 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 r((unsigned char
68a60 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a )(x)).#endif../*
68a70 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e .** Internal fun
68a80 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 ction prototypes
68a90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
68aa0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
68ab0 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 rICmp(const char
68ac0 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
68ad0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
68ae0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 E int sqlite3Str
68af0 4e 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 NICmp(const char
68b00 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
68b10 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
68b20 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
68b30 65 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 e3IsNumber(const
68b40 20 63 68 61 72 2a 2c 20 69 6e 74 2a 2c 20 75 38 char*, int*, u8
68b50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
68b60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 E int sqlite3Str
68b70 6c 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f len(sqlite3*, co
68b80 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
68b90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
68ba0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 qlite3Strlen30(c
68bb0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a 53 51 onst char*);..SQ
68bc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
68bd0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e sqlite3MallocIn
68be0 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 it(void);.SQLITE
68bf0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
68c00 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76 lite3MallocEnd(v
68c10 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
68c20 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
68c30 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 e3Malloc(int);.S
68c40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
68c50 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f id *sqlite3Mallo
68c60 63 5a 65 72 6f 28 69 6e 74 29 3b 0a 53 51 4c 49 cZero(int);.SQLI
68c70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
68c80 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 *sqlite3DbMalloc
68c90 5a 65 72 6f 28 73 71 6c 69 74 65 33 2a 2c 20 69 Zero(sqlite3*, i
68ca0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
68cb0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
68cc0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 3DbMallocRaw(sql
68cd0 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ite3*, int);.SQL
68ce0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
68cf0 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 *sqlite3DbStrDu
68d00 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 p(sqlite3*,const
68d10 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
68d20 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
68d30 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 lite3DbStrNDup(s
68d40 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 qlite3*,const ch
68d50 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 ar*, int);.SQLIT
68d60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
68d70 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 sqlite3Realloc(v
68d80 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 oid*, int);.SQLI
68d90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
68da0 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f *sqlite3DbReallo
68db0 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33 20 cOrFree(sqlite3
68dc0 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b *, void *, int);
68dd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
68de0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 void *sqlite3DbR
68df0 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a ealloc(sqlite3 *
68e00 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b 0a , void *, int);.
68e10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
68e20 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 65 oid sqlite3DbFre
68e30 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 e(sqlite3*, void
68e40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
68e50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 TE int sqlite3Ma
68e60 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 2a 29 3b llocSize(void*);
68e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
68e80 69 6e 74 20 73 71 6c 69 74 65 33 44 62 4d 61 6c int sqlite3DbMal
68e90 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74 65 33 2a locSize(sqlite3*
68ea0 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 , void*);.SQLITE
68eb0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
68ec0 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c qlite3ScratchMal
68ed0 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 loc(int);.SQLITE
68ee0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
68ef0 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 lite3ScratchFree
68f00 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f (void*);.SQLITE_
68f10 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
68f20 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 lite3PageMalloc(
68f30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
68f40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
68f50 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 2a 29 3PageFree(void*)
68f60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
68f70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
68f80 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 SetDefault(void)
68f90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
68fa0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 6e void sqlite3Ben
68fb0 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 76 ignMallocHooks(v
68fc0 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 2c 20 76 oid (*)(void), v
68fd0 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 29 3b 0a oid (*)(void));.
68fe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
68ff0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 nt sqlite3Memory
69000 41 6c 61 72 6d 28 76 6f 69 64 20 28 2a 29 28 76 Alarm(void (*)(v
69010 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e oid*, sqlite3_in
69020 74 36 34 2c 20 69 6e 74 29 2c 20 76 6f 69 64 2a t64, int), void*
69030 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 , sqlite3_int64)
69040 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
69050 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 0a _ENABLE_MEMSYS3.
69060 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
69070 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d onst sqlite3_mem
69080 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 _methods *sqlite
69090 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 3MemGetMemsys3(v
690a0 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 oid);.#endif.#if
690b0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
690c0 45 5f 4d 45 4d 53 59 53 35 0a 53 51 4c 49 54 45 E_MEMSYS5.SQLITE
690d0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
690e0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
690f0 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 ds *sqlite3MemGe
69100 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 3b 0a tMemsys5(void);.
69110 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 #endif...#ifndef
69120 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d SQLITE_MUTEX_OM
69130 49 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 IT.SQLITE_PRIVAT
69140 45 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E sqlite3_mute
69150 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 x_methods *sqlit
69160 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 e3DefaultMutex(v
69170 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
69180 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 5f 6d VATE sqlite3_m
69190 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d 75 74 utex *sqlite3Mut
691a0 65 78 41 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 exAlloc(int);.SQ
691b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
691c0 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 nt sqlite3MutexI
691d0 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 nit(void);.SQLIT
691e0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
691f0 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 sqlite3MutexEnd(
69200 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 void);.#endif..S
69210 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
69220 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 t sqlite3StatusV
69230 61 6c 75 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54 alue(int);.SQLIT
69240 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
69250 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
69260 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
69270 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
69280 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 qlite3StatusSet(
69290 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 int, int);..SQLI
692a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
692b0 71 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 qlite3IsNaN(doub
692c0 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 le);..SQLITE_PRI
692d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
692e0 33 56 58 50 72 69 6e 74 66 28 53 74 72 41 63 63 3VXPrintf(StrAcc
692f0 75 6d 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 um*, int, const
69300 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b char*, va_list);
69310 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
69320 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 char *sqlite3MPr
69330 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c 63 6f intf(sqlite3*,co
69340 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
69350 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
69360 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 char *sqlite3VMP
69370 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c 63 rintf(sqlite3*,c
69380 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c onst char*, va_l
69390 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ist);.SQLITE_PRI
693a0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
693b0 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c 69 74 e3MAppendf(sqlit
693c0 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 e3*,char*,const
693d0 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 69 66 20 char*,...);.#if
693e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
693f0 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 EST) || defined(
69400 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 53 51 SQLITE_DEBUG).SQ
69410 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
69420 6f 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 oid sqlite3Debug
69430 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 Printf(const cha
69440 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e 64 69 66 r*, ...);.#endif
69450 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
69460 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 ITE_TEST).SQLITE
69470 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
69480 2a 73 71 6c 69 74 65 33 54 65 73 74 54 65 78 74 *sqlite3TestText
69490 54 6f 50 74 72 28 63 6f 6e 73 74 20 63 68 61 72 ToPtr(const char
694a0 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 *);.#endif.SQLIT
694b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
694c0 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
694d0 63 68 61 72 20 2a 2a 2c 20 73 71 6c 69 74 65 33 char **, sqlite3
694e0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
694f0 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ...);.SQLITE_PRI
69500 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
69510 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 2a 3ErrorMsg(Parse*
69520 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e , const char*, .
69530 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..);.SQLITE_PRIV
69540 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
69550 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65 ErrorClear(Parse
69560 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
69570 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
69580 65 71 75 6f 74 65 28 63 68 61 72 2a 29 3b 0a 53 equote(char*);.S
69590 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
695a0 69 64 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 id sqlite3Dequot
695b0 65 45 78 70 72 28 45 78 70 72 2a 29 3b 0a 53 51 eExpr(Expr*);.SQ
695c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
695d0 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 sqlite3KeywordC
695e0 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e ode(const unsign
695f0 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a ed char*, int);.
69600 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
69610 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 nt sqlite3RunPar
69620 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 ser(Parse*, cons
69630 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20 2a 2a t char*, char **
69640 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
69650 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69 E void sqlite3Fi
69660 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 73 65 nishCoding(Parse
69670 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
69680 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
69690 74 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a 29 tTempReg(Parse*)
696a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
696b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c void sqlite3Rel
696c0 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72 73 easeTempReg(Pars
696d0 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
696e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
696f0 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 te3GetTempRange(
69700 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c Parse*,int);.SQL
69710 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
69720 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
69730 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c empRange(Parse*,
69740 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
69750 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
69760 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69 74 qlite3Expr(sqlit
69770 65 33 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c e3*, int, Expr*,
69780 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f Expr*, const To
69790 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
697a0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
697b0 74 65 33 50 45 78 70 72 28 50 61 72 73 65 2a 2c te3PExpr(Parse*,
697c0 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 int, Expr*, Exp
697d0 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a r*, const Token*
697e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
697f0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 52 E Expr *sqlite3R
69800 65 67 69 73 74 65 72 45 78 70 72 28 50 61 72 73 egisterExpr(Pars
69810 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 e*,Token*);.SQLI
69820 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
69830 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 *sqlite3ExprAnd(
69840 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 sqlite3*,Expr*,
69850 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
69860 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
69870 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 70 72 te3ExprSpan(Expr
69880 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 29 *,Token*,Token*)
69890 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
698a0 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
698b0 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 73 65 prFunction(Parse
698c0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b *,ExprList*, Tok
698d0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
698e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
698f0 33 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75 3ExprAssignVarNu
69900 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45 78 70 mber(Parse*, Exp
69910 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
69920 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
69930 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69 74 65 ExprClear(sqlite
69940 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 3*, Expr*);.SQLI
69950 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
69960 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
69970 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 e(sqlite3*, Expr
69980 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
69990 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c TE ExprList *sql
699a0 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 ite3ExprListAppe
699b0 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 nd(Parse*,ExprLi
699c0 73 74 2a 2c 45 78 70 72 2a 2c 54 6f 6b 65 6e 2a st*,Expr*,Token*
699d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
699e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
699f0 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c prListDelete(sql
69a00 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a ite3*, ExprList*
69a10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
69a20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 E int sqlite3Ini
69a30 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 t(sqlite3*, char
69a40 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
69a50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
69a60 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 nitCallback(void
69a70 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 *, int, char**,
69a80 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f char**);.SQLITE_
69a90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
69aa0 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65 ite3Pragma(Parse
69ab0 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c *,Token*,Token*,
69ac0 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c Token*,int);.SQL
69ad0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
69ae0 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 sqlite3ResetInt
69af0 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 ernalSchema(sqli
69b00 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 te3*, int);.SQLI
69b10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
69b20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 sqlite3BeginPars
69b30 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 e(Parse*,int);.S
69b40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
69b50 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 id sqlite3Commit
69b60 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 InternalChanges(
69b70 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
69b80 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 E_PRIVATE Table
69b90 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 *sqlite3ResultSe
69ba0 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 2a tOfSelect(Parse*
69bb0 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 ,Select*);.SQLIT
69bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
69bd0 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 qlite3OpenMaster
69be0 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 Table(Parse *, i
69bf0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
69c00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
69c10 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 73 65 StartTable(Parse
69c20 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c *,Token*,Token*,
69c30 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 int,int,int,int)
69c40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
69c50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
69c60 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f Column(Parse*,To
69c70 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
69c80 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
69c90 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 e3AddNotNull(Par
69ca0 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 se*, int);.SQLIT
69cb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
69cc0 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 qlite3AddPrimary
69cd0 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 Key(Parse*, Expr
69ce0 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c List*, int, int,
69cf0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
69d00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
69d10 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 e3AddCheckConstr
69d20 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78 70 aint(Parse*, Exp
69d30 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
69d40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
69d50 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 AddColumnType(Pa
69d60 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 rse*,Token*);.SQ
69d70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
69d80 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 d sqlite3AddDefa
69d90 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a 2c ultValue(Parse*,
69da0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
69db0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
69dc0 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 te3AddCollateTyp
69dd0 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a e(Parse*, Token*
69de0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
69df0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e E void sqlite3En
69e00 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f dTable(Parse*,To
69e10 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 ken*,Token*,Sele
69e20 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 ct*);..SQLITE_PR
69e30 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 71 IVATE Bitvec *sq
69e40 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
69e50 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 e(u32);.SQLITE_P
69e60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
69e70 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69 74 e3BitvecTest(Bit
69e80 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 vec*, u32);.SQLI
69e90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
69ea0 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
69eb0 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 Bitvec*, u32);.S
69ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
69ed0 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 id sqlite3Bitvec
69ee0 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20 75 Clear(Bitvec*, u
69ef0 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 32);.SQLITE_PRIV
69f00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
69f10 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 BitvecDestroy(Bi
69f20 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tvec*);.SQLITE_P
69f30 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
69f40 65 33 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 e3BitvecSize(Bit
69f50 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 vec*);.SQLITE_PR
69f60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
69f70 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 3BitvecBuiltinTe
69f80 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 st(int,int*);..S
69f90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 52 6f QLITE_PRIVATE Ro
69fa0 77 53 65 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 wSet *sqlite3Row
69fb0 53 65 74 49 6e 69 74 28 73 71 6c 69 74 65 33 2a SetInit(sqlite3*
69fc0 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 , void*, unsigne
69fd0 64 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 d int);.SQLITE_P
69fe0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
69ff0 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 52 te3RowSetClear(R
6a000 6f 77 53 65 74 2a 29 3b 0a 53 51 4c 49 54 45 5f owSet*);.SQLITE_
6a010 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6a020 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 ite3RowSetInsert
6a030 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 29 3b 0a (RowSet*, i64);.
6a040 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6a050 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 nt sqlite3RowSet
6a060 4e 65 78 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 Next(RowSet*, i6
6a070 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 4*);..SQLITE_PRI
6a080 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6a090 33 43 72 65 61 74 65 56 69 65 77 28 50 61 72 73 3CreateView(Pars
6a0a0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a e*,Token*,Token*
6a0b0 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c ,Token*,Select*,
6a0c0 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 21 int,int);..#if !
6a0d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
6a0e0 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 MIT_VIEW) || !de
6a0f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
6a100 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a T_VIRTUALTABLE).
6a110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6a120 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77 int sqlite3View
6a130 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 GetColumnNames(P
6a140 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 arse*,Table*);.#
6a150 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 else.# define sq
6a160 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 lite3ViewGetColu
6a170 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0a 23 mnNames(A,B) 0.#
6a180 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
6a190 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6a1a0 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73 e3DropTable(Pars
6a1b0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e e*, SrcList*, in
6a1c0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6a1d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6a1e0 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 ite3DeleteTable(
6a1f0 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Table*);.SQLITE_
6a200 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6a210 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 65 ite3Insert(Parse
6a220 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 *, SrcList*, Exp
6a230 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c rList*, Select*,
6a240 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a IdList*, int);.
6a250 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6a260 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 oid *sqlite3Arra
6a270 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 yAllocate(sqlite
6a280 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 3*,void*,int,int
6a290 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 ,int*,int*,int*)
6a2a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6a2b0 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 IdList *sqlite3
6a2c0 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c IdListAppend(sql
6a2d0 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 ite3*, IdList*,
6a2e0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6a2f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6a300 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 te3IdListIndex(I
6a310 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 dList*,const cha
6a320 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6a330 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
6a340 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 ite3SrcListEnlar
6a350 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 ge(sqlite3*, Src
6a360 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 List*, int, int)
6a370 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6a380 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
6a390 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73 3SrcListAppend(s
6a3a0 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 qlite3*, SrcList
6a3b0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
6a3c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6a3d0 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 TE SrcList *sqli
6a3e0 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 te3SrcListAppend
6a3f0 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c FromTerm(Parse*,
6a400 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e SrcList*, Token
6a410 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 *, Token*,.
6a420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6a440 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a Token*, Select*
6a450 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a , Expr*, IdList*
6a460 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6a470 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
6a480 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 cListIndexedBy(P
6a490 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 arse *, SrcList
6a4a0 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c *, Token *);.SQL
6a4b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6a4c0 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 sqlite3IndexedBy
6a4d0 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 Lookup(Parse *,
6a4e0 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
6a4f0 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tem *);.SQLITE_P
6a500 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6a510 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a te3SrcListShiftJ
6a520 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a oinType(SrcList*
6a530 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6a540 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
6a550 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f cListAssignCurso
6a560 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 rs(Parse*, SrcLi
6a570 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
6a580 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6a590 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 3IdListDelete(sq
6a5a0 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 lite3*, IdList*)
6a5b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6a5c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 void sqlite3Src
6a5d0 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 ListDelete(sqlit
6a5e0 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a e3*, SrcList*);.
6a5f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6a600 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74 oid sqlite3Creat
6a610 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f eIndex(Parse*,To
6a620 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c ken*,Token*,SrcL
6a630 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 ist*,ExprList*,i
6a640 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 nt,Token*,.
6a650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6a660 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20 Token*, int,
6a670 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6a680 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6a690 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 3DropIndex(Parse
6a6a0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 *, SrcList*, int
6a6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6a6c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c E int sqlite3Sel
6a6d0 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 ect(Parse*, Sele
6a6e0 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a ct*, SelectDest*
6a6f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6a700 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 E Select *sqlite
6a710 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65 3SelectNew(Parse
6a720 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c *,ExprList*,SrcL
6a730 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c ist*,Expr*,ExprL
6a740 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 ist*,.
6a750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
6a760 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 xpr*,ExprList*,i
6a770 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b nt,Expr*,Expr*);
6a780 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6a790 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 void sqlite3Sele
6a7a0 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 ctDelete(sqlite3
6a7b0 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
6a7c0 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
6a7d0 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 e *sqlite3SrcLis
6a7e0 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20 tLookup(Parse*,
6a7f0 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 SrcList*);.SQLIT
6a800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6a810 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 lite3IsReadOnly(
6a820 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
6a830 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6a840 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6a850 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65 3OpenTable(Parse
6a860 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74 *, int iCur, int
6a870 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e iDb, Table*, in
6a880 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 t);.#if defined(
6a890 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 SQLITE_ENABLE_UP
6a8a0 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 DATE_DELETE_LIMI
6a8b0 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 T) && !defined(S
6a8c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
6a8d0 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 ERY).SQLITE_PRIV
6a8e0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
6a8f0 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73 3LimitWhere(Pars
6a900 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 e *, SrcList *,
6a910 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74 Expr *, ExprList
6a920 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 *, Expr *, Expr
6a930 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e *, char *);.#en
6a940 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
6a950 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
6a960 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a eleteFrom(Parse*
6a970 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 , SrcList*, Expr
6a980 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6a990 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
6a9a0 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72 pdate(Parse*, Sr
6a9b0 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 cList*, ExprList
6a9c0 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a *, Expr*, int);.
6a9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 SQLITE_PRIVATE W
6a9e0 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 hereInfo *sqlite
6a9f0 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73 3WhereBegin(Pars
6aa00 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 e*, SrcList*, Ex
6aa10 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c pr*, ExprList**,
6aa20 20 75 38 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 u8, int);.SQLIT
6aa30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6aa40 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 qlite3WhereEnd(W
6aa50 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 hereInfo*);.SQLI
6aa60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6aa70 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 qlite3ExprCodeGe
6aa80 74 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20 tColumn(Parse*,
6aa90 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Table*, int, int
6aaa0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
6aab0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6aac0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
6aad0 4d 6f 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 Move(Parse*, int
6aae0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
6aaf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6ab00 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
6ab10 43 6f 70 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 Copy(Parse*, int
6ab20 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
6ab30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6ab40 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 sqlite3ExprClea
6ab50 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 rColumnCache(Par
6ab60 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 se*, int);.SQLIT
6ab70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6ab80 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 qlite3ExprCacheA
6ab90 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 ffinityChange(Pa
6aba0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b rse*, int, int);
6abb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6abc0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
6abd0 57 72 69 74 61 62 6c 65 52 65 67 69 73 74 65 72 WritableRegister
6abe0 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 (Parse*,int);.SQ
6abf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6ac00 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 d sqlite3ExprHar
6ac10 64 43 6f 70 79 28 50 61 72 73 65 2a 2c 69 6e 74 dCopy(Parse*,int
6ac20 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
6ac30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6ac40 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 2a 3ExprCode(Parse*
6ac50 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 , Expr*, int);.S
6ac60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6ac70 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
6ac80 65 54 65 6d 70 28 50 61 72 73 65 2a 2c 20 45 78 eTemp(Parse*, Ex
6ac90 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 pr*, int*);.SQLI
6aca0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6acb0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
6acc0 72 67 65 74 28 50 61 72 73 65 2a 2c 20 45 78 70 rget(Parse*, Exp
6acd0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
6ace0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6acf0 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 ite3ExprCodeAndC
6ad00 61 63 68 65 28 50 61 72 73 65 2a 2c 20 45 78 70 ache(Parse*, Exp
6ad10 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
6ad20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6ad30 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e lite3ExprCodeCon
6ad40 73 74 61 6e 74 73 28 50 61 72 73 65 2a 2c 20 45 stants(Parse*, E
6ad50 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6ad60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6ad70 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 3ExprCodeExprLis
6ad80 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 t(Parse*, ExprLi
6ad90 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a st*, int, int);.
6ada0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6adb0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
6adc0 66 54 72 75 65 28 50 61 72 73 65 2a 2c 20 45 78 fTrue(Parse*, Ex
6add0 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a pr*, int, int);.
6ade0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6adf0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
6ae00 66 46 61 6c 73 65 28 50 61 72 73 65 2a 2c 20 45 fFalse(Parse*, E
6ae10 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b xpr*, int, int);
6ae20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ae30 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 69 Table *sqlite3Fi
6ae40 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a ndTable(sqlite3*
6ae50 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f ,const char*, co
6ae60 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
6ae70 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 TE_PRIVATE Table
6ae80 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 *sqlite3LocateT
6ae90 61 62 6c 65 28 50 61 72 73 65 2a 2c 69 6e 74 20 able(Parse*,int
6aea0 69 73 56 69 65 77 2c 63 6f 6e 73 74 20 63 68 61 isView,const cha
6aeb0 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 r*, const char*)
6aec0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6aed0 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46 Index *sqlite3F
6aee0 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33 indIndex(sqlite3
6aef0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 *,const char*, c
6af00 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
6af10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6af20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e sqlite3UnlinkAn
6af30 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71 6c dDeleteTable(sql
6af40 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 ite3*,int,const
6af50 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
6af60 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6af70 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 te3UnlinkAndDele
6af80 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33 2a teIndex(sqlite3*
6af90 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a ,int,const char*
6afa0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6afb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 E void sqlite3Va
6afc0 63 75 75 6d 28 50 61 72 73 65 2a 29 3b 0a 53 51 cuum(Parse*);.SQ
6afd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6afe0 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 sqlite3RunVacuu
6aff0 6d 28 63 68 61 72 2a 2a 2c 20 73 71 6c 69 74 65 m(char**, sqlite
6b000 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 3*);.SQLITE_PRIV
6b010 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
6b020 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 3NameFromToken(s
6b030 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 qlite3*, Token*)
6b040 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b050 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
6b060 43 6f 6d 70 61 72 65 28 45 78 70 72 2a 2c 20 45 Compare(Expr*, E
6b070 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6b080 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6b090 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 e3ExprAnalyzeAgg
6b0a0 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f 6e 74 regates(NameCont
6b0b0 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 ext*, Expr*);.SQ
6b0c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6b0d0 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 d sqlite3ExprAna
6b0e0 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 6d 65 lyzeAggList(Name
6b0f0 43 6f 6e 74 65 78 74 2a 2c 45 78 70 72 4c 69 73 Context*,ExprLis
6b100 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6b110 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 ATE Vdbe *sqlite
6b120 33 47 65 74 56 64 62 65 28 50 61 72 73 65 2a 29 3GetVdbe(Parse*)
6b130 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b140 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72 Expr *sqlite3Cr
6b150 65 61 74 65 49 64 45 78 70 72 28 50 61 72 73 65 eateIdExpr(Parse
6b160 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 *, const char*)
6b170 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b180 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e void sqlite3Prn
6b190 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29 gSaveState(void)
6b1a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b1b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e void sqlite3Prn
6b1c0 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 76 6f gRestoreState(vo
6b1d0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
6b1e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6b1f0 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 76 PrngResetState(v
6b200 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
6b210 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6b220 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c 3RollbackAll(sql
6b230 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
6b240 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6b250 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 te3CodeVerifySch
6b260 65 6d 61 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 ema(Parse*, int)
6b270 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b280 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 void sqlite3Beg
6b290 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 inTransaction(Pa
6b2a0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 rse*, int);.SQLI
6b2b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6b2c0 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 sqlite3CommitTra
6b2d0 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 nsaction(Parse*)
6b2e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b2f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c void sqlite3Rol
6b300 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e lbackTransaction
6b310 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
6b320 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6b330 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 50 lite3Savepoint(P
6b340 61 72 73 65 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 arse*, int, Toke
6b350 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
6b360 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6b370 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 CloseSavepoints(
6b380 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 sqlite3 *);.SQLI
6b390 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6b3a0 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
6b3b0 74 61 6e 74 28 45 78 70 72 2a 29 3b 0a 53 51 4c tant(Expr*);.SQL
6b3c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6b3d0 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
6b3e0 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 stantNotJoin(Exp
6b3f0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6b400 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
6b410 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 xprIsConstantOrF
6b420 75 6e 63 74 69 6f 6e 28 45 78 70 72 2a 29 3b 0a unction(Expr*);.
6b430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6b440 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 nt sqlite3ExprIs
6b450 49 6e 74 65 67 65 72 28 45 78 70 72 2a 2c 20 69 Integer(Expr*, i
6b460 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
6b470 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6b480 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68 IsRowid(const ch
6b490 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
6b4a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6b4b0 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 3GenerateRowDele
6b4c0 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 te(Parse*, Table
6b4d0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
6b4e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6b4f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 E void sqlite3Ge
6b500 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 nerateRowIndexDe
6b510 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62 lete(Parse*, Tab
6b520 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b le*, int, int*);
6b530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6b540 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65 72 int sqlite3Gener
6b550 61 74 65 49 6e 64 65 78 4b 65 79 28 50 61 72 73 ateIndexKey(Pars
6b560 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e 74 2c e*, Index*, int,
6b570 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
6b580 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6b590 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 sqlite3GenerateC
6b5a0 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 onstraintChecks(
6b5b0 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c 69 6e Parse*,Table*,in
6b5c0 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 t,int,.
6b5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b5e0 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 2a int*
6b5f0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
6b600 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6b610 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
6b620 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 mpleteInsertion(
6b630 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
6b640 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 int, int, int*,
6b650 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a int, int, int);.
6b660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6b670 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 nt sqlite3OpenTa
6b680 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61 bleAndIndices(Pa
6b690 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e rse*, Table*, in
6b6a0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6b6b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6b6c0 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 ite3BeginWriteOp
6b6d0 65 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 eration(Parse*,
6b6e0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
6b6f0 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
6b700 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 sqlite3ExprDup(s
6b710 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e qlite3*,Expr*,in
6b720 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6b730 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 TE void sqlite3T
6b740 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 okenCopy(sqlite3
6b750 2a 2c 54 6f 6b 65 6e 2a 2c 63 6f 6e 73 74 20 54 *,Token*,const T
6b760 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6b770 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 RIVATE ExprList
6b780 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 *sqlite3ExprList
6b790 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 Dup(sqlite3*,Exp
6b7a0 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c rList*,int);.SQL
6b7b0 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c ITE_PRIVATE SrcL
6b7c0 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c ist *sqlite3SrcL
6b7d0 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c istDup(sqlite3*,
6b7e0 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 SrcList*,int);.S
6b7f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 QLITE_PRIVATE Id
6b800 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c List *sqlite3IdL
6b810 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c istDup(sqlite3*,
6b820 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 IdList*);.SQLITE
6b830 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 _PRIVATE Select
6b840 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 *sqlite3SelectDu
6b850 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 p(sqlite3*,Selec
6b860 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
6b870 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6b880 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 ite3FuncDefInser
6b890 74 28 46 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 t(FuncDefHash*,
6b8a0 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 FuncDef*);.SQLIT
6b8b0 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 E_PRIVATE FuncDe
6b8c0 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75 f *sqlite3FindFu
6b8d0 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c nction(sqlite3*,
6b8e0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c const char*,int,
6b8f0 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c int,u8,int);.SQL
6b900 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6b910 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 sqlite3Register
6b920 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 BuiltinFunctions
6b930 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
6b940 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6b950 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 sqlite3RegisterD
6b960 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 ateTimeFunctions
6b970 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 (void);.SQLITE_P
6b980 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6b990 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 te3RegisterGloba
6b9a0 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 lFunctions(void)
6b9b0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
6b9c0 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
6b9d0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6b9e0 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 e3SafetyOn(sqlit
6b9f0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
6ba00 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6ba10 65 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 e3SafetyOff(sqli
6ba20 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 te3*);.#else.# d
6ba30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 efine sqlite3Saf
6ba40 65 74 79 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 etyOn(A) 0.# def
6ba50 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 ine sqlite3Safet
6ba60 79 4f 66 66 28 41 29 20 30 0a 23 65 6e 64 69 66 yOff(A) 0.#endif
6ba70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ba80 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 int sqlite3Safet
6ba90 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 yCheckOk(sqlite3
6baa0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6bab0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 TE int sqlite3Sa
6bac0 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f fetyCheckSickOrO
6bad0 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c k(sqlite3*);.SQL
6bae0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6baf0 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f sqlite3ChangeCo
6bb00 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 okie(Parse*, int
6bb10 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 );..#if !defined
6bb20 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 (SQLITE_OMIT_VIE
6bb30 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 W) && !defined(S
6bb40 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
6bb50 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ER).SQLITE_PRIVA
6bb60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
6bb70 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 50 aterializeView(P
6bb80 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 arse*, Table*, E
6bb90 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 xpr*, int);.#end
6bba0 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
6bbb0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
6bbc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6bbd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 void sqlite3Beg
6bbe0 69 6e 54 72 69 67 67 65 72 28 50 61 72 73 65 2a inTrigger(Parse*
6bbf0 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c , Token*,Token*,
6bc00 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c int,int,IdList*,
6bc10 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 SrcList*,.
6bc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6bc30 20 20 20 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 Expr*,int,
6bc40 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6bc50 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
6bc60 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 te3FinishTrigger
6bc70 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 (Parse*, Trigger
6bc80 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a Step*, Token*);.
6bc90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6bca0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f void sqlite3Dro
6bcb0 70 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c pTrigger(Parse*,
6bcc0 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b SrcList*, int);
6bcd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6bce0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 void sqlite3Dr
6bcf0 6f 70 54 72 69 67 67 65 72 50 74 72 28 50 61 72 opTriggerPtr(Par
6bd00 73 65 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a se*, Trigger*);.
6bd10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6bd20 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 Trigger *sqlite
6bd30 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 50 3TriggersExist(P
6bd40 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 arse *, Table*,
6bd50 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 int, ExprList*,
6bd60 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c int *pMask);.SQL
6bd70 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 ITE_PRIVATE Tr
6bd80 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 igger *sqlite3Tr
6bd90 69 67 67 65 72 4c 69 73 74 28 50 61 72 73 65 20 iggerList(Parse
6bda0 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c *, Table *);.SQL
6bdb0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
6bdc0 74 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 t sqlite3CodeRow
6bdd0 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 Trigger(Parse*,
6bde0 54 72 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 Trigger *, int,
6bdf0 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 ExprList*, int,
6be00 54 61 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 Table *,.
6be10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6be20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 int, int, i
6be30 6e 74 2c 20 69 6e 74 2c 20 75 33 32 2a 2c 20 75 nt, int, u32*, u
6be40 33 32 2a 29 3b 0a 20 20 76 6f 69 64 20 73 71 6c 32*);. void sql
6be50 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73 28 iteViewTriggers(
6be60 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
6be70 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72 Expr*, int, Expr
6be80 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 List*);.SQLITE_P
6be90 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
6bea0 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 lite3DeleteTrigg
6beb0 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c erStep(sqlite3*,
6bec0 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a TriggerStep*);.
6bed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6bee0 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 TriggerStep *sq
6bef0 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 lite3TriggerSele
6bf00 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c ctStep(sqlite3*,
6bf10 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 Select*);.SQLITE
6bf20 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
6bf30 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 erStep *sqlite3T
6bf40 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 riggerInsertStep
6bf50 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a (sqlite3*,Token*
6bf60 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20 , IdList*,.
6bf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6bf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6bf90 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c ExprList*,Sel
6bfa0 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ect*,int);.SQLIT
6bfb0 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 E_PRIVATE Trig
6bfc0 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 gerStep *sqlite3
6bfd0 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 TriggerUpdateSte
6bfe0 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e p(sqlite3*,Token
6bff0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 *,ExprList*, Exp
6c000 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
6c010 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
6c020 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 erStep *sqlite3T
6c030 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 riggerDeleteStep
6c040 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a (sqlite3*,Token*
6c050 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6c060 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
6c070 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 sqlite3DeleteTri
6c080 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 gger(sqlite3*, T
6c090 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 rigger*);.SQLITE
6c0a0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
6c0b0 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 sqlite3UnlinkAnd
6c0c0 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 DeleteTrigger(sq
6c0d0 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 lite3*,int,const
6c0e0 20 63 68 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23 char*);.#else.#
6c0f0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 define sqlite3T
6c100 72 69 67 67 65 72 73 45 78 69 73 74 28 42 2c 43 riggersExist(B,C
6c110 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 ,D,E,F) 0.# defi
6c120 6e 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 ne sqlite3Delete
6c130 54 72 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64 Trigger(A,B).# d
6c140 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f efine sqlite3Dro
6c150 70 54 72 69 67 67 65 72 50 74 72 28 41 2c 42 29 pTriggerPtr(A,B)
6c160 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6c170 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
6c180 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 Trigger(A,B,C).#
6c190 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 define sqlite3C
6c1a0 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c odeRowTrigger(A,
6c1b0 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c B,C,D,E,F,G,H,I,
6c1c0 4a 2c 4b 2c 4c 29 20 30 0a 23 65 6e 64 69 66 0a J,K,L) 0.#endif.
6c1d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6c1e0 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 int sqlite3JoinT
6c1f0 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 ype(Parse*, Toke
6c200 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 n*, Token*, Toke
6c210 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
6c220 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6c230 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 CreateForeignKey
6c240 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 (Parse*, ExprLis
6c250 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 t*, Token*, Expr
6c260 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c List*, int);.SQL
6c270 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6c280 20 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 sqlite3DeferFor
6c290 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 eignKey(Parse*,
6c2a0 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 int);.#ifndef SQ
6c2b0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
6c2c0 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 IZATION.SQLITE_P
6c2d0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
6c2e0 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 50 61 lite3AuthRead(Pa
6c2f0 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d rse*,Expr*,Schem
6c300 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 a*,SrcList*);.SQ
6c310 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
6c320 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 nt sqlite3AuthCh
6c330 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 eck(Parse*,int,
6c340 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e const char*, con
6c350 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 st char*, const
6c360 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
6c370 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
6c380 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 lite3AuthContext
6c390 50 75 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74 Push(Parse*, Aut
6c3a0 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 hContext*, const
6c3b0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
6c3c0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
6c3d0 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 qlite3AuthContex
6c3e0 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 tPop(AuthContext
6c3f0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 *);.#else.# defi
6c400 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 ne sqlite3AuthRe
6c410 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 65 ad(a,b,c,d).# de
6c420 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 fine sqlite3Auth
6c430 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 Check(a,b,c,d,e)
6c440 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 SQLITE_OK.#
6c450 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 define sqlite3Au
6c460 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 61 2c thContextPush(a,
6c470 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 73 71 b,c).# define sq
6c480 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 lite3AuthContext
6c490 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64 29 28 Pop(a) ((void)(
6c4a0 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 a)).#endif.SQLIT
6c4b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6c4c0 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72 qlite3Attach(Par
6c4d0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 se*, Expr*, Expr
6c4e0 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
6c4f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6c500 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72 qlite3Detach(Par
6c510 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c se*, Expr*);.SQL
6c520 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6c530 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 sqlite3BtreeFact
6c540 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 ory(const sqlite
6c550 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
6c560 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 r *zFilename,.
6c570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c580 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 int omitJou
6c590 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63 68 65 rnal, int nCache
6c5a0 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 42 74 72 , int flags, Btr
6c5b0 65 65 20 2a 2a 70 70 42 74 72 65 65 29 3b 0a 53 ee **ppBtree);.S
6c5c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6c5d0 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 t sqlite3FixInit
6c5e0 28 44 62 46 69 78 65 72 2a 2c 20 50 61 72 73 65 (DbFixer*, Parse
6c5f0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 *, int, const ch
6c600 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e ar*, const Token
6c610 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6c620 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
6c630 78 53 72 63 4c 69 73 74 28 44 62 46 69 78 65 72 xSrcList(DbFixer
6c640 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 *, SrcList*);.SQ
6c650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6c660 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 sqlite3FixSelec
6c670 74 28 44 62 46 69 78 65 72 2a 2c 20 53 65 6c 65 t(DbFixer*, Sele
6c680 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ct*);.SQLITE_PRI
6c690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6c6a0 46 69 78 45 78 70 72 28 44 62 46 69 78 65 72 2a FixExpr(DbFixer*
6c6b0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6c6c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6c6d0 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 ite3FixExprList(
6c6e0 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 4c 69 DbFixer*, ExprLi
6c6f0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
6c700 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6c710 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 44 FixTriggerStep(D
6c720 62 46 69 78 65 72 2a 2c 20 54 72 69 67 67 65 72 bFixer*, Trigger
6c730 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Step*);.SQLITE_P
6c740 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6c750 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 e3AtoF(const cha
6c760 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a r *z, double*);.
6c770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6c780 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 nt sqlite3GetInt
6c790 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 32(const char *,
6c7a0 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int*);.SQLITE_P
6c7b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6c7c0 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63 e3FitsIn64Bits(c
6c7d0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
6c7e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c7f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 E int sqlite3Utf
6c800 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 16ByteLen(const
6c810 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 void *pData, int
6c820 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f nChar);.SQLITE_
6c830 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6c840 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 te3Utf8CharLen(c
6c850 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 onst char *pData
6c860 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51 , int nByte);.SQ
6c870 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6c880 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
6c890 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73 (const u8*, cons
6c8a0 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 2a t u8*, const u8*
6c8b0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 *);../*.** Routi
6c8c0 6e 65 73 20 74 6f 20 72 65 61 64 20 61 6e 64 20 nes to read and
6c8d0 77 72 69 74 65 20 76 61 72 69 61 62 6c 65 2d 6c write variable-l
6c8e0 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20 ength integers.
6c8f0 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 0a 2a These used to.*
6c900 2a 20 62 65 20 64 65 66 69 6e 65 64 20 6c 6f 63 * be defined loc
6c910 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77 65 ally, but now we
6c920 20 75 73 65 20 74 68 65 20 76 61 72 69 6e 74 20 use the varint
6c930 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 routines in the
6c940 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 util.c.** file.
6c950 20 43 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 Code should use
6c960 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 73 the MACRO forms
6c970 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 56 below, as the V
6c980 61 72 69 6e 74 33 32 20 76 65 72 73 69 6f 6e 73 arint32 versions
6c990 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64 20 74 6f .** are coded to
6c9a0 20 61 73 73 75 6d 65 20 74 68 65 20 73 69 6e 67 assume the sing
6c9b0 6c 65 20 62 79 74 65 20 63 61 73 65 20 69 73 20 le byte case is
6c9c0 61 6c 72 65 61 64 79 20 68 61 6e 64 6c 65 64 20 already handled
6c9d0 28 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 4d (which .** the M
6c9e0 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29 2e ACRO form does).
6c9f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
6ca00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 TE int sqlite3Pu
6ca10 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 tVarint(unsigned
6ca20 20 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53 51 char*, u64);.SQ
6ca30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ca40 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e sqlite3PutVarin
6ca50 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 t32(unsigned cha
6ca60 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 r*, u32);.SQLITE
6ca70 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 _PRIVATE u8 sqli
6ca80 74 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e te3GetVarint(con
6ca90 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
6caa0 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 51 4c 49 *, u64 *);.SQLI
6cab0 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 TE_PRIVATE u8 sq
6cac0 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 lite3GetVarint32
6cad0 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
6cae0 63 68 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a char *, u32 *);.
6caf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6cb00 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 nt sqlite3Varint
6cb10 4c 65 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a Len(u64 v);../*.
6cb20 2a 2a 20 54 68 65 20 68 65 61 64 65 72 20 6f 66 ** The header of
6cb30 20 61 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 a record consis
6cb40 74 73 20 6f 66 20 61 20 73 65 71 75 65 6e 63 65 ts of a sequence
6cb50 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 variable-length
6cb60 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 integers..** Th
6cb70 65 73 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 ese integers are
6cb80 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 73 almost always s
6cb90 6d 61 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e 63 mall and are enc
6cba0 6f 64 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 oded as a single
6cbb0 20 62 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 6f byte..** The fo
6cbc0 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 74 llowing macros t
6cbd0 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 74 68 ake advantage th
6cbe0 69 73 20 66 61 63 74 20 74 6f 20 70 72 6f 76 69 is fact to provi
6cbf0 64 65 20 61 20 66 61 73 74 20 65 6e 63 6f 64 65 de a fast encode
6cc00 0a 2a 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 6f .** and decode o
6cc10 66 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 69 f the integers i
6cc20 6e 20 61 20 72 65 63 6f 72 64 20 68 65 61 64 65 n a record heade
6cc30 72 2e 20 20 49 74 20 69 73 20 66 61 73 74 65 72 r. It is faster
6cc40 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a for the common.
6cc50 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 74 68 ** case where th
6cc60 65 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 73 e integer is a s
6cc70 69 6e 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20 ingle byte. It
6cc80 69 73 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 is a little slow
6cc90 65 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 er when the.** i
6cca0 6e 74 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72 nteger is two or
6ccb0 20 6d 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75 more bytes. Bu
6ccc0 74 20 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20 t overall it is
6ccd0 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 faster..**.** Th
6cce0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 e following expr
6ccf0 65 73 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69 essions are equi
6cd00 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 valent:.**.**
6cd10 20 20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 x = sqlite3Get
6cd20 56 61 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20 Varint32( A, &B
6cd30 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 );.** x = sq
6cd40 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 lite3PutVarint32
6cd50 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 ( A, B );.**.**
6cd60 20 20 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e x = getVarin
6cd70 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 t32( A, B );.**
6cd80 20 20 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e x = putVarin
6cd90 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a t32( A, B );.**.
6cda0 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 */.#define getVa
6cdb0 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38 rint32(A,B) (u8
6cdc0 29 28 28 2a 28 41 29 3c 28 75 38 29 30 78 38 30 )((*(A)<(u8)0x80
6cdd0 29 20 3f 20 28 28 42 29 20 3d 20 28 75 33 32 29 ) ? ((B) = (u32)
6cde0 2a 28 41 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 *(A)),1 : sqlite
6cdf0 33 47 65 74 56 61 72 69 6e 74 33 32 28 28 41 29 3GetVarint32((A)
6ce00 2c 20 28 75 33 32 20 2a 29 26 28 42 29 29 29 0a , (u32 *)&(B))).
6ce10 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e #define putVarin
6ce20 74 33 32 28 41 2c 42 29 20 20 28 75 38 29 28 28 t32(A,B) (u8)((
6ce30 28 75 33 32 29 28 42 29 3c 28 75 33 32 29 30 78 (u32)(B)<(u32)0x
6ce40 38 30 29 20 3f 20 28 2a 28 41 29 20 3d 20 28 75 80) ? (*(A) = (u
6ce50 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 42 29 nsigned char)(B)
6ce60 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 50 75 74 ),1 : sqlite3Put
6ce70 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28 42 Varint32((A), (B
6ce80 29 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 56 ))).#define getV
6ce90 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 arint sqlite3
6cea0 47 65 74 56 61 72 69 6e 74 0a 23 64 65 66 69 6e GetVarint.#defin
6ceb0 65 20 70 75 74 56 61 72 69 6e 74 20 20 20 20 73 e putVarint s
6cec0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 0a qlite3PutVarint.
6ced0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
6cee0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 64 void sqlite3Ind
6cef0 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 exAffinityStr(Vd
6cf00 62 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a be *, Index *);.
6cf10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6cf20 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 oid sqlite3Table
6cf30 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 AffinityStr(Vdbe
6cf40 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 *, Table *);.SQ
6cf50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
6cf60 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 r sqlite3Compare
6cf70 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 Affinity(Expr *p
6cf80 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 Expr, char aff2)
6cf90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6cfa0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 int sqlite3Inde
6cfb0 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 xAffinityOk(Expr
6cfc0 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 *pExpr, char id
6cfd0 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c x_affinity);.SQL
6cfe0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
6cff0 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 sqlite3ExprAffi
6d000 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 nity(Expr *pExpr
6d010 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6d020 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f E int sqlite3Ato
6d030 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c i64(const char*,
6d040 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 i64*);.SQLITE_P
6d050 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6d060 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 te3Error(sqlite3
6d070 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 *, int, const ch
6d080 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 ar*,...);.SQLITE
6d090 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
6d0a0 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 qlite3HexToBlob(
6d0b0 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 sqlite3*, const
6d0c0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b char *z, int n);
6d0d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6d0e0 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 int sqlite3TwoPa
6d0f0 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 rtName(Parse *,
6d100 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a Token *, Token *
6d110 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c , Token **);.SQL
6d120 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
6d130 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 t char *sqlite3E
6d140 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49 rrStr(int);.SQLI
6d150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6d160 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 qlite3ReadSchema
6d170 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b (Parse *pParse);
6d180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6d190 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
6d1a0 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69 FindCollSeq(sqli
6d1b0 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e te3*,u8 enc, con
6d1c0 73 74 20 63 68 61 72 20 2a 2c 69 6e 74 2c 69 6e st char *,int,in
6d1d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6d1e0 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 TE CollSeq *sqli
6d1f0 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 te3LocateCollSeq
6d200 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
6d210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
6d220 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 3b 0a 53 e, int nName);.S
6d230 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f QLITE_PRIVATE Co
6d240 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 llSeq *sqlite3Ex
6d250 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 prCollSeq(Parse
6d260 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
6d270 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Expr);.SQLITE_PR
6d280 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
6d290 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 te3ExprSetColl(P
6d2a0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
6d2b0 70 72 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a pr *, Token *);.
6d2c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6d2d0 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 nt sqlite3CheckC
6d2e0 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 ollSeq(Parse *,
6d2f0 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49 CollSeq *);.SQLI
6d300 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6d310 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 qlite3CheckObjec
6d320 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63 tName(Parse *, c
6d330 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 onst char *);.SQ
6d340 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6d350 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 d sqlite3VdbeSet
6d360 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 Changes(sqlite3
6d370 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 *, int);..SQLITE
6d380 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 _PRIVATE const v
6d390 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 oid *sqlite3Valu
6d3a0 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 eText(sqlite3_va
6d3b0 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 lue*, u8);.SQLIT
6d3c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6d3d0 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 lite3ValueBytes(
6d3e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 sqlite3_value*,
6d3f0 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 u8);.SQLITE_PRIV
6d400 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6d410 56 61 6c 75 65 53 65 74 53 74 72 28 73 71 6c 69 ValueSetStr(sqli
6d420 74 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 2c te3_value*, int,
6d430 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 38 const void *,u8
6d440 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
6d450 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 28 void(
6d460 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 *)(void*));.SQLI
6d470 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6d480 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 sqlite3ValueFree
6d490 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
6d4a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6d4b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
6d4c0 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 sqlite3ValueNew(
6d4d0 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 sqlite3 *);.SQLI
6d4e0 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
6d4f0 2a 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 *sqlite3Utf16to8
6d500 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 (sqlite3 *, cons
6d510 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 t void*, int);.S
6d520 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6d530 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 t sqlite3ValueFr
6d540 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a omExpr(sqlite3 *
6d550 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75 38 , Expr *, u8, u8
6d560 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
6d570 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
6d580 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6d590 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 ValueApplyAffini
6d5a0 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ty(sqlite3_value
6d5b0 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69 66 *, u8, u8);.#if
6d5c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c ndef SQLITE_AMAL
6d5d0 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f GAMATION.SQLITE_
6d5e0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e PRIVATE const un
6d5f0 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 signed char sqli
6d600 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b te3UpperToLower[
6d610 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ];.SQLITE_PRIVAT
6d620 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 E const unsigned
6d630 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79 char sqlite3Cty
6d640 70 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f peMap[];.SQLITE_
6d650 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 PRIVATE SQLITE_W
6d660 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 SD struct Sqlite
6d670 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 3Config sqlite3C
6d680 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f 50 52 onfig;.SQLITE_PR
6d690 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 IVATE SQLITE_WSD
6d6a0 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c FuncDefHash sql
6d6b0 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 ite3GlobalFuncti
6d6c0 6f 6e 73 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ons;.SQLITE_PRIV
6d6d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
6d6e0 65 6e 64 69 6e 67 42 79 74 65 3b 0a 23 65 6e 64 endingByte;.#end
6d6f0 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
6d700 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f E void sqlite3Ro
6d710 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62 2a 2c otPageMoved(Db*,
6d720 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
6d730 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6d740 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 sqlite3Reindex(P
6d750 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 arse*, Token*, T
6d760 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6d770 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6d780 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e te3AlterFunction
6d790 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c s(sqlite3*);.SQL
6d7a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6d7b0 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e sqlite3AlterRen
6d7c0 61 6d 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c ameTable(Parse*,
6d7d0 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e SrcList*, Token
6d7e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6d7f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
6d800 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 tToken(const uns
6d810 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 69 6e igned char *, in
6d820 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t *);.SQLITE_PRI
6d830 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6d840 33 4e 65 73 74 65 64 50 61 72 73 65 28 50 61 72 3NestedParse(Par
6d850 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a se*, const char*
6d860 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 , ...);.SQLITE_P
6d870 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6d880 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 te3ExpirePrepare
6d890 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 dStatements(sqli
6d8a0 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
6d8b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6d8c0 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 e3CodeSubselect(
6d8d0 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c Parse *, Expr *,
6d8e0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
6d8f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6d900 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 sqlite3SelectPre
6d910 70 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 p(Parse*, Select
6d920 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 *, NameContext*)
6d930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6d940 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f int sqlite3Reso
6d950 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 4e 61 6d lveExprNames(Nam
6d960 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a eContext*, Expr*
6d970 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6d980 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
6d990 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 solveSelectNames
6d9a0 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a (Parse*, Select*
6d9b0 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b , NameContext*);
6d9c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6d9d0 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c int sqlite3Resol
6d9e0 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 50 veOrderGroupBy(P
6d9f0 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 arse*, Select*,
6da00 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 ExprList*, const
6da10 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
6da20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6da30 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c ite3ColumnDefaul
6da40 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20 t(Vdbe *, Table
6da50 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
6da60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6da70 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 ite3AlterFinishA
6da80 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a ddColumn(Parse *
6da90 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 , Token *);.SQLI
6daa0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6dab0 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 sqlite3AlterBegi
6dac0 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 nAddColumn(Parse
6dad0 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 29 3b 0a *, SrcList *);.
6dae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
6daf0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 ollSeq *sqlite3G
6db00 65 74 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 etCollSeq(sqlite
6db10 33 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 3*, CollSeq *, c
6db20 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
6db30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6db40 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 E char sqlite3Af
6db50 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 finityType(const
6db60 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
6db70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6db80 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 61 72 lite3Analyze(Par
6db90 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b se*, Token*, Tok
6dba0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
6dbb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6dbc0 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 InvokeBusyHandle
6dbd0 72 28 42 75 73 79 48 61 6e 64 6c 65 72 2a 29 3b r(BusyHandler*);
6dbe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6dbf0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 int sqlite3FindD
6dc00 62 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 b(sqlite3*, Toke
6dc10 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
6dc20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
6dc30 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69 74 65 indDbName(sqlite
6dc40 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 3 *, const char
6dc50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6dc60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e TE int sqlite3An
6dc70 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 alysisLoad(sqlit
6dc80 65 33 2a 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51 e3*,int iDB);.SQ
6dc90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6dca0 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 d sqlite3Default
6dcb0 52 6f 77 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a RowEst(Index*);.
6dcc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6dcd0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 oid sqlite3Regis
6dce0 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 terLikeFunctions
6dcf0 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b (sqlite3*, int);
6dd00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6dd10 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b int sqlite3IsLik
6dd20 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 eFunction(sqlite
6dd30 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 3*,Expr*,int*,ch
6dd40 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
6dd50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6dd60 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 3MinimumFileForm
6dd70 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 at(Parse*, int,
6dd80 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6dd90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6dda0 33 53 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64 3SchemaFree(void
6ddb0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
6ddc0 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 ATE Schema *sqli
6ddd0 74 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c te3SchemaGet(sql
6dde0 69 74 65 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 ite3 *, Btree *)
6ddf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6de00 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 int sqlite3Sche
6de10 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 maToIndex(sqlite
6de20 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3 *db, Schema *)
6de30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6de40 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 KeyInfo *sqlite
6de50 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 3IndexKeyinfo(Pa
6de60 72 73 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b rse *, Index *);
6de70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6de80 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74 int sqlite3Creat
6de90 65 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c eFunc(sqlite3 *,
6dea0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 const char *, i
6deb0 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c nt, int, void *,
6dec0 20 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c . void (*)(sql
6ded0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
6dee0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 t,sqlite3_value
6def0 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 **),. void (*)(
6df00 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
6df10 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
6df20 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 ue **), void (*)
6df30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
6df40 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *));.SQLITE_PRIV
6df50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 ATE int sqlite3A
6df60 70 69 45 78 69 74 28 73 71 6c 69 74 65 33 20 2a piExit(sqlite3 *
6df70 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 db, int);.SQLITE
6df80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6df90 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 ite3OpenTempData
6dfa0 62 61 73 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a base(Parse *);..
6dfb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6dfc0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 oid sqlite3StrAc
6dfd0 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d cumInit(StrAccum
6dfe0 2a 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 *, char*, int, i
6dff0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6e000 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6e010 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 StrAccumAppend(S
6e020 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 trAccum*,const c
6e030 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 har*,int);.SQLIT
6e040 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
6e050 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
6e060 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d 2a 29 inish(StrAccum*)
6e070 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
6e090 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 AccumReset(StrAc
6e0a0 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 cum*);.SQLITE_PR
6e0b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6e0c0 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 e3SelectDestInit
6e0d0 28 53 65 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74 (SelectDest*,int
6e0e0 2c 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 ,int);..SQLITE_P
6e0f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6e100 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 te3BackupRestart
6e110 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
6e120 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e130 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
6e140 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69 ackupUpdate(sqli
6e150 74 65 33 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67 te3_backup *, Pg
6e160 6e 6f 2c 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b no, const u8 *);
6e170 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ../*.** The inte
6e180 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45 4d rface to the LEM
6e190 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61 72 ON-generated par
6e1a0 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ser.*/.SQLITE_PR
6e1b0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6e1c0 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76 te3ParserAlloc(v
6e1d0 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29 oid*(*)(size_t))
6e1e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e1f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 void sqlite3Par
6e200 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c 20 76 serFree(void*, v
6e210 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
6e220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6e230 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 oid sqlite3Parse
6e240 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f r(void*, int, To
6e250 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 23 69 ken, Parse*);.#i
6e260 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 fdef YYTRACKMAXS
6e270 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 TACKDEPTH.SQLITE
6e280 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
6e290 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63 qlite3ParserStac
6e2a0 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 65 kPeak(void*);.#e
6e2b0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 ndif..SQLITE_PRI
6e2c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6e2d0 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f AutoLoadExtensio
6e2e0 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 69 ns(sqlite3*);.#i
6e2f0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
6e300 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
6e310 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e320 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c void sqlite3Cl
6e330 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 oseExtensions(sq
6e340 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 lite3*);.#else.#
6e350 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 define sqlite3C
6e360 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 58 loseExtensions(X
6e370 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 ).#endif..#ifnde
6e380 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
6e390 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 ARED_CACHE.SQLIT
6e3a0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
6e3b0 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 sqlite3TableLoc
6e3c0 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c 20 k(Parse *, int,
6e3d0 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20 63 int, u8, const c
6e3e0 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 har *);.#else.
6e3f0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 #define sqlite3T
6e400 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 ableLock(v,w,x,y
6e410 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 ,z).#endif..#ifd
6e420 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
6e430 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6e440 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 54 int sqlite3Utf8T
6e450 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 o8(unsigned char
6e460 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 *);.#endif..#ifd
6e470 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
6e480 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 IRTUALTABLE.# d
6e490 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 efine sqlite3Vta
6e4a0 62 43 6c 65 61 72 28 58 29 0a 23 20 20 64 65 66 bClear(X).# def
6e4b0 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 53 ine sqlite3VtabS
6e4c0 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 54 45 5f ync(X,Y) SQLITE_
6e4d0 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c OK.# define sql
6e4e0 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b ite3VtabRollback
6e4f0 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 (X).# define sq
6e500 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 lite3VtabCommit(
6e510 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c X).# define sql
6e520 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 ite3VtabInSync(d
6e530 62 29 20 30 0a 23 65 6c 73 65 0a 53 51 4c 49 54 b) 0.#else.SQLIT
6e540 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 E_PRIVATE voi
6e550 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 d sqlite3VtabCle
6e560 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 ar(Table*);.SQLI
6e570 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e TE_PRIVATE in
6e580 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e t sqlite3VtabSyn
6e590 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 c(sqlite3 *db, c
6e5a0 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f har **);.SQLITE_
6e5b0 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 PRIVATE int s
6e5c0 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 qlite3VtabRollba
6e5d0 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b ck(sqlite3 *db);
6e5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e5f0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 int sqlite3Vt
6e600 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 abCommit(sqlite3
6e610 20 2a 64 62 29 3b 0a 23 20 20 64 65 66 69 6e 65 *db);.# define
6e620 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 sqlite3VtabInSy
6e630 6e 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e 56 nc(db) ((db)->nV
6e640 54 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29 2d Trans>0 && (db)-
6e650 3e 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65 6e >aVTrans==0).#en
6e660 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
6e670 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
6e680 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 tabMakeWritable(
6e690 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a Parse*,Table*);.
6e6a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6e6b0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4c oid sqlite3VtabL
6e6c0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61 62 ock(sqlite3_vtab
6e6d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e6e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
6e6f0 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 tabUnlock(sqlite
6e700 33 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 3*, sqlite3_vtab
6e710 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e720 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
6e730 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 50 61 tabBeginParse(Pa
6e740 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f rse*, Token*, To
6e750 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 ken*, Token*);.S
6e760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6e770 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 69 id sqlite3VtabFi
6e780 6e 69 73 68 50 61 72 73 65 28 50 61 72 73 65 2a nishParse(Parse*
6e790 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
6e7a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6e7b0 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69 qlite3VtabArgIni
6e7c0 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 t(Parse*);.SQLIT
6e7d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6e7e0 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78 74 qlite3VtabArgExt
6e7f0 65 6e 64 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 end(Parse*, Toke
6e800 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
6e810 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
6e820 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73 71 tabCallCreate(sq
6e830 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e lite3*, int, con
6e840 73 74 20 63 68 61 72 20 2a 2c 20 63 68 61 72 20 st char *, char
6e850 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
6e860 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
6e870 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 tabCallConnect(P
6e880 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a arse*, Table*);.
6e890 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6e8a0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 nt sqlite3VtabCa
6e8b0 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 llDestroy(sqlite
6e8c0 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 3*, int, const c
6e8d0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har *);.SQLITE_P
6e8e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6e8f0 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c 69 e3VtabBegin(sqli
6e900 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 5f 76 te3 *, sqlite3_v
6e910 74 61 62 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tab *);.SQLITE_P
6e920 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a RIVATE FuncDef *
6e930 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c sqlite3VtabOverl
6e940 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 oadFunction(sqli
6e950 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 te3 *,FuncDef*,
6e960 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 2a 29 int nArg, Expr*)
6e970 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e980 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76 void sqlite3Inv
6e990 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c alidFunction(sql
6e9a0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
6e9b0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
6e9c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e9d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 TE int sqlite3Tr
6e9e0 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73 ansferBindings(s
6e9f0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73 qlite3_stmt *, s
6ea00 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a qlite3_stmt *);.
6ea10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6ea20 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 nt sqlite3Reprep
6ea30 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 are(Vdbe*);.SQLI
6ea40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6ea50 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 sqlite3ExprListC
6ea60 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 65 heckLength(Parse
6ea70 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f *, ExprList*, co
6ea80 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
6ea90 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
6eaa0 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 eq *sqlite3Binar
6eab0 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 yCompareCollSeq(
6eac0 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c Parse *, Expr *,
6ead0 20 45 78 70 72 20 2a 29 3b 0a 0a 0a 2f 2a 0a 2a Expr *);.../*.*
6eae0 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 75 6c * Available faul
6eaf0 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 53 68 t injectors. Sh
6eb00 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 65 64 ould be numbered
6eb10 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
6eb20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 0..*/.#define SQ
6eb30 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 LITE_FAULTINJECT
6eb40 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 30 0a OR_MALLOC 0.
6eb50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
6eb60 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 AULTINJECTOR_COU
6eb70 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a NT 1../*.**
6eb80 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 The interface t
6eb90 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 66 61 o the code in fa
6eba0 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 20 69 ult.c used for i
6ebb0 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 6e 69 dentifying "beni
6ebc0 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 gn".** malloc fa
6ebd0 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 73 20 ilures. This is
6ebe0 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 66 20 only present if
6ebf0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
6ec00 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 20 6e TIN_TEST.** is n
6ec10 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 ot defined..*/.#
6ec20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
6ec30 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a IT_BUILTIN_TEST.
6ec40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6ec50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 void sqlite3Beg
6ec60 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 inBenignMalloc(v
6ec70 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
6ec80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
6ec90 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
6eca0 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a oc(void);.#else.
6ecb0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
6ecc0 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
6ecd0 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 20 73 oc(). #define s
6ece0 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
6ecf0 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 0a 0a alloc().#endif..
6ed00 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 #define IN_INDEX
6ed10 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 20 20 _ROWID
6ed20 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 1.#define IN_IN
6ed30 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 20 20 DEX_EPH
6ed40 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 49 4e 2.#define IN
6ed50 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 20 20 _INDEX_INDEX
6ed60 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 45 5f 3.SQLITE_
6ed70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6ed80 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 te3FindInIndex(P
6ed90 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 arse *, Expr *,
6eda0 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 int*);..#ifdef S
6edb0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
6edc0 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 54 45 MIC_WRITE.SQLITE
6edd0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
6ede0 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 qlite3JournalOpe
6edf0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c n(sqlite3_vfs *,
6ee00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 const char *, s
6ee10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20 69 qlite3_file *, i
6ee20 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
6ee30 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
6ee40 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a qlite3JournalSiz
6ee50 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 29 e(sqlite3_vfs *)
6ee60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ee70 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f int sqlite3Jo
6ee80 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69 urnalCreate(sqli
6ee90 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 65 6c te3_file *);.#el
6eea0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c se. #define sql
6eeb0 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 ite3JournalSize(
6eec0 70 56 66 73 29 20 28 28 70 56 66 73 29 2d 3e 73 pVfs) ((pVfs)->s
6eed0 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 66 0a zOsFile).#endif.
6eee0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6eef0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a void sqlite3MemJ
6ef00 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 ournalOpen(sqlit
6ef10 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 4c 49 e3_file *);.SQLI
6ef20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6ef30 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
6ef40 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 Size(void);.SQLI
6ef50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6ef60 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
6ef70 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 al(sqlite3_file
6ef80 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f *);..#if SQLITE_
6ef90 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 MAX_EXPR_DEPTH>0
6efa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6efb0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 void sqlite3Ex
6efc0 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72 73 prSetHeight(Pars
6efd0 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
6efe0 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *p);.SQLITE_PRIV
6eff0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
6f000 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 3SelectExprHeigh
6f010 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 51 4c t(Select *);.SQL
6f020 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
6f030 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 t sqlite3ExprChe
6f040 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 2a 2c ckHeight(Parse*,
6f050 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20 23 int);.#else. #
6f060 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 define sqlite3Ex
6f070 70 72 53 65 74 48 65 69 67 68 74 28 78 2c 79 29 prSetHeight(x,y)
6f080 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
6f090 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 e3SelectExprHeig
6f0a0 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 69 6e ht(x) 0. #defin
6f0b0 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 e sqlite3ExprChe
6f0c0 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a 23 65 ckHeight(x,y).#e
6f0d0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 ndif..SQLITE_PRI
6f0e0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
6f0f0 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75 Get4byte(const u
6f100 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 8*);.SQLITE_PRIV
6f110 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f120 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 75 33 Put4byte(u8*, u3
6f130 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 2);..#ifdef SQLI
6f140 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b TE_ENABLE_UNLOCK
6f150 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 54 45 5f 50 _NOTIFY.SQLITE_P
6f160 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
6f170 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
6f180 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a locked(sqlite3 *
6f190 2c 20 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 , sqlite3 *);.SQ
6f1a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
6f1b0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 oid sqlite3Conne
6f1c0 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71 ctionUnlocked(sq
6f1d0 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 lite3 *db);.SQLI
6f1e0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
6f1f0 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 d sqlite3Connect
6f200 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74 65 ionClosed(sqlite
6f210 33 20 2a 64 62 29 3b 0a 23 65 6c 73 65 0a 20 20 3 *db);.#else.
6f220 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 #define sqlite3C
6f230 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 onnectionBlocked
6f240 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 20 (x,y). #define
6f250 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
6f260 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 0a 20 20 23 nUnlocked(x). #
6f270 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f define sqlite3Co
6f280 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 78 nnectionClosed(x
6f290 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65 ).#endif...#ifde
6f2a0 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 23 69 6e f SQLITE_SSE.#in
6f2b0 63 6c 75 64 65 20 22 73 73 65 49 6e 74 2e 68 22 clude "sseInt.h"
6f2c0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
6f2d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
6f2e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
6f2f0 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 id sqlite3Parser
6f300 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 63 68 61 Trace(FILE*, cha
6f310 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a r *);.#endif../*
6f320 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 .** If the SQLIT
6f330 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 41 43 45 E_ENABLE IOTRACE
6f340 20 65 78 69 73 74 73 20 74 68 65 6e 20 74 68 65 exists then the
6f350 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
6f360 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f 54 72 61 .** sqlite3IoTra
6f370 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 ce is a pointer
6f380 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b 65 to a printf-like
6f390 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 6f routine used to
6f3a0 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74 72 .** print I/O tr
6f3b0 61 63 69 6e 67 20 6d 65 73 73 61 67 65 73 2e 20 acing messages.
6f3c0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
6f3d0 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 E_ENABLE_IOTRACE
6f3e0 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41 43 .# define IOTRAC
6f3f0 45 28 41 29 20 20 69 66 28 20 73 71 6c 69 74 65 E(A) if( sqlite
6f400 33 49 6f 54 72 61 63 65 20 29 7b 20 73 71 6c 69 3IoTrace ){ sqli
6f410 74 65 33 49 6f 54 72 61 63 65 20 41 3b 20 7d 0a te3IoTrace A; }.
6f420 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6f430 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
6f440 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 eIOTraceSql(Vdbe
6f450 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f460 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 TE void (*sqlite
6f470 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20 3IoTrace)(const
6f480 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c 73 char*,...);.#els
6f490 65 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41 e.# define IOTRA
6f4a0 43 45 28 41 29 0a 23 20 64 65 66 69 6e 65 20 73 CE(A).# define s
6f4b0 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 qlite3VdbeIOTrac
6f4c0 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a eSql(X).#endif..
6f4d0 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
6f4e0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 ******* End of s
6f4f0 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
6f500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6f510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6f520 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
6f530 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
6f540 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a le global.c ****
6f550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6f560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6f570 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
6f580 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a 2a 08 June 13.**.**
6f590 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
6f5a0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
6f5b0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
6f5c0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
6f5d0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
6f5e0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
6f5f0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
6f600 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
6f610 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
6f620 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
6f630 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
6f640 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
6f650 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
6f660 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
6f670 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
6f680 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
6f690 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
6f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6f6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6f6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6f6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6f6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
6f6f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
6f700 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f ns definitions o
6f710 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c f global variabl
6f720 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73 2e es and contants.
6f730 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 67 6c 6f 62 .**.** $Id: glob
6f740 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 39 al.c,v 1.12 2009
6f750 2f 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 /02/05 16:31:46
6f760 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f drh Exp $.*/.../
6f770 2a 20 41 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 * An array to ma
6f780 70 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 p all upper-case
6f790 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f characters into
6f7a0 20 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e their correspon
6f7b0 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 ding.** lower-ca
6f7c0 73 65 20 63 68 61 72 61 63 74 65 72 2e 20 0a 2a se character. .*
6f7d0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 *.** SQLite only
6f7e0 20 63 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 53 considers US-AS
6f7f0 43 49 49 20 28 6f 72 20 45 42 43 44 49 43 29 20 CII (or EBCDIC)
6f800 63 68 61 72 61 63 74 65 72 73 2e 20 20 57 65 20 characters. We
6f810 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 do not.** handle
6f820 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e case conversion
6f830 73 20 66 6f 72 20 74 68 65 20 55 54 46 20 63 68 s for the UTF ch
6f840 61 72 61 63 74 65 72 20 73 65 74 20 73 69 6e 63 aracter set sinc
6f850 65 20 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 e the tables.**
6f860 69 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 involved are nea
6f870 72 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62 69 rly as big or bi
6f880 67 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65 gger than SQLite
6f890 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 itself..*/.SQLI
6f8a0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
6f8b0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 unsigned char s
6f8c0 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 qlite3UpperToLow
6f8d0 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 er[] = {.#ifdef
6f8e0 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20 SQLITE_ASCII.
6f8f0 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 0, 1, 2, 3
6f900 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 , 4, 5, 6, 7
6f910 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 , 8, 9, 10, 11
6f920 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 , 12, 13, 14, 15
6f930 2c 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 , 16, 17,. 1
6f940 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 8, 19, 20, 21, 2
6f950 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 2, 23, 24, 25, 2
6f960 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 6, 27, 28, 29, 3
6f970 30 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 0, 31, 32, 33, 3
6f980 34 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 4, 35,. 36,
6f990 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 37, 38, 39, 40,
6f9a0 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 41, 42, 43, 44,
6f9b0 34 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 45, 46, 47, 48,
6f9c0 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 49, 50, 51, 52,
6f9d0 35 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 53,. 54, 55,
6f9e0 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 56, 57, 58, 59,
6f9f0 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 60, 61, 62, 63,
6fa00 20 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 64, 97, 98, 99,
6fa10 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 100,101,102,103,
6fa20 0a 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36 . 104,105,106
6fa30 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 ,107,108,109,110
6fa40 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 ,111,112,113,114
6fa50 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 ,115,116,117,118
6fa60 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 ,119,120,121,.
6fa70 20 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 122, 91, 92, 9
6fa80 33 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 3, 94, 95, 96, 9
6fa90 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 7, 98, 99,100,10
6faa0 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 1,102,103,104,10
6fab0 35 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 5,106,107,. 1
6fac0 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 08,109,110,111,1
6fad0 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 12,113,114,115,1
6fae0 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 16,117,118,119,1
6faf0 32 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 20,121,122,123,1
6fb00 32 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 24,125,. 126,
6fb10 31 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 127,128,129,130,
6fb20 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 131,132,133,134,
6fb30 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 135,136,137,138,
6fb40 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 139,140,141,142,
6fb50 31 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 143,. 144,145
6fb60 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 ,146,147,148,149
6fb70 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 ,150,151,152,153
6fb80 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 ,154,155,156,157
6fb90 2c 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 ,158,159,160,161
6fba0 2c 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36 ,. 162,163,16
6fbb0 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 4,165,166,167,16
6fbc0 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 8,169,170,171,17
6fbd0 32 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 2,173,174,175,17
6fbe0 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 6,177,178,179,.
6fbf0 20 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 180,181,182,1
6fc00 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 83,184,185,186,1
6fc10 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 87,188,189,190,1
6fc20 39 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 91,192,193,194,1
6fc30 39 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 95,196,197,.
6fc40 31 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 198,199,200,201,
6fc50 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 202,203,204,205,
6fc60 32 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 206,207,208,209,
6fc70 32 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 210,211,212,213,
6fc80 32 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 214,215,. 216
6fc90 2c 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 ,217,218,219,220
6fca0 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 ,221,222,223,224
6fcb0 2c 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 ,225,226,227,228
6fcc0 2c 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 ,229,230,231,232
6fcd0 2c 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 ,233,. 234,23
6fce0 35 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 5,236,237,238,23
6fcf0 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 9,240,241,242,24
6fd00 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 3,244,245,246,24
6fd10 37 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 7,248,249,250,25
6fd20 31 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32 1,. 252,253,2
6fd30 35 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 54,255.#endif.#i
6fd40 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 fdef SQLITE_EBCD
6fd50 49 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 IC. 0, 1,
6fd60 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 2, 3, 4, 5,
6fd70 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 6, 7, 8, 9,
6fd80 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 10, 11, 12, 13,
6fd90 31 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 14, 15, /* 0x */
6fda0 0a 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38 . 16, 17, 18
6fdb0 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 , 19, 20, 21, 22
6fdc0 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 , 23, 24, 25, 26
6fdd0 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 , 27, 28, 29, 30
6fde0 2c 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 , 31, /* 1x */.
6fdf0 20 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 32, 33, 34,
6fe00 33 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 35, 36, 37, 38,
6fe10 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 39, 40, 41, 42,
6fe20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 43, 44, 45, 46,
6fe30 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 47, /* 2x */.
6fe40 20 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 48, 49, 50, 51
6fe50 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 , 52, 53, 54, 55
6fe60 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 , 56, 57, 58, 59
6fe70 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 , 60, 61, 62, 63
6fe80 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 , /* 3x */.
6fe90 36 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 64, 65, 66, 67,
6fea0 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 68, 69, 70, 71,
6feb0 37 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 72, 73, 74, 75,
6fec0 37 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 76, 77, 78, 79,
6fed0 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 /* 4x */. 80
6fee0 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 , 81, 82, 83, 84
6fef0 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 , 85, 86, 87, 88
6ff00 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 , 89, 90, 91, 92
6ff10 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a , 93, 94, 95, /*
6ff20 20 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 5x */. 96,
6ff30 39 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 97, 66, 67, 68,
6ff40 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 69, 70, 71, 72,
6ff50 37 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 73,106,107,108,1
6ff60 30 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 09,110,111, /* 6
6ff70 78 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 x */. 112, 81
6ff80 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 , 82, 83, 84, 85
6ff90 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 , 86, 87, 88, 89
6ffa0 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 ,122,123,124,125
6ffb0 2c 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 ,126,127, /* 7x
6ffc0 2a 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31 */. 128,129,1
6ffd0 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 30,131,132,133,1
6ffe0 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 34,135,136,137,1
6fff0 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 38,139,140,141,1
70000 34 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 42,143, /* 8x */
70010 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 . 144,145,146
70020 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 ,147,148,149,150
70030 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 ,151,152,153,154
70040 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 ,155,156,157,156
70050 2c 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 ,159, /* 9x */.
70060 20 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 160,161,162,1
70070 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 63,164,165,166,1
70080 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 67,168,169,170,1
70090 37 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 71,140,141,142,1
700a0 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 75, /* Ax */.
700b0 20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 176,177,178,179
700c0 2c 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 ,180,181,182,183
700d0 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 ,184,185,186,187
700e0 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 ,188,189,190,191
700f0 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 , /* Bx */. 1
70100 39 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 92,129,130,131,1
70110 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 32,133,134,135,1
70120 33 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 36,137,202,203,2
70130 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 04,205,206,207,
70140 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 /* Cx */. 208
70150 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 ,145,146,147,148
70160 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 ,149,150,151,152
70170 2c 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 ,153,218,219,220
70180 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a ,221,222,223, /*
70190 20 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 Dx */. 224,2
701a0 32 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 25,162,163,164,1
701b0 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 65,166,167,168,1
701c0 36 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 69,232,203,204,2
701d0 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 05,206,207, /* E
701e0 78 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 x */. 239,240
701f0 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 ,241,242,243,244
70200 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 ,245,246,247,248
70210 2c 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 ,249,219,220,221
70220 2c 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 ,222,255, /* Fx
70230 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a */.#endif.};../*
70240 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
70250 67 20 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 g 256 byte looku
70260 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 p table is used
70270 74 6f 20 73 75 70 70 6f 72 74 20 53 51 4c 69 74 to support SQLit
70280 65 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 es built-in.** e
70290 71 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 quivalents to th
702a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e e following stan
702b0 64 61 72 64 20 6c 69 62 72 61 72 79 20 66 75 6e dard library fun
702c0 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ctions:.**.**
702d0 69 73 73 70 61 63 65 28 29 20 20 20 20 20 20 20 isspace()
702e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
702f0 20 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 0x01.** isalp
70300 68 61 28 29 20 20 20 20 20 20 20 20 20 20 20 20 ha()
70310 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 32 0x02
70320 0a 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29 20 .** isdigit()
70330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70340 20 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 0x04.**
70350 20 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 isalnum()
70360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70370 20 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 0x06.** isxd
70380 69 67 69 74 28 29 20 20 20 20 20 20 20 20 20 20 igit()
70390 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
703a0 38 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 29 8.** toupper()
703b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
703c0 20 20 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0a 0x20.**.
703d0 2a 2a 20 42 69 74 20 30 78 32 30 20 69 73 20 73 ** Bit 0x20 is s
703e0 65 74 20 69 66 20 74 68 65 20 6d 61 70 70 65 64 et if the mapped
703f0 20 63 68 61 72 61 63 74 65 72 20 72 65 71 75 69 character requi
70400 72 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 res translation
70410 74 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 to upper.** case
70420 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63 68 . i.e. if the ch
70430 61 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f 77 aracter is a low
70440 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 er-case ASCII ch
70450 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 aracter..** If x
70460 20 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 is a lower-case
70470 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 ASCII character
70480 2c 20 74 68 65 6e 20 69 74 73 20 75 70 70 65 72 , then its upper
70490 2d 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74 -case equivalent
704a0 0a 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 32 30 .** is (x - 0x20
704b0 29 2e 20 54 68 65 72 65 66 6f 72 65 20 74 6f 75 ). Therefore tou
704c0 70 70 65 72 28 29 20 63 61 6e 20 62 65 20 69 6d pper() can be im
704d0 70 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a plemented as:.**
704e0 0a 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 70 .** (x & ~(map
704f0 5b 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a [x]&0x20)).**.**
70500 20 53 74 61 6e 64 61 72 64 20 66 75 6e 63 74 69 Standard functi
70510 6f 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 on tolower() is
70520 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e implemented usin
70530 67 20 74 68 65 20 73 71 6c 69 74 65 33 55 70 70 g the sqlite3Upp
70540 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 erToLower[].** a
70550 72 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 rray. tolower()
70560 69 73 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 is used more oft
70570 65 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 72 28 en than toupper(
70580 29 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a ) by SQLite..**.
70590 2a 2a 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 ** SQLite's vers
705a0 69 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 ions are identic
705b0 61 6c 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 al to the standa
705c0 72 64 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 rd versions assu
705d0 6d 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 ming a.** locale
705e0 20 6f 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 of "C". They ar
705f0 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 e implemented as
70600 20 6d 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 macros in sqlit
70610 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 eInt.h..*/.#ifde
70620 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 f SQLITE_ASCII.S
70630 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
70640 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
70650 72 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 r sqlite3CtypeMa
70660 70 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 p[256] = {. 0x0
70670 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
70680 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70690 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
706a0 20 30 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 00..07 .....
706b0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
706c0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c x01, 0x01, 0x01,
706d0 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 0x01, 0x01, 0x0
706e0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 0, 0x00, /* 08.
706f0 2e 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .0f ........
70700 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c */. 0x00, 0x00,
70710 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70720 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
70730 78 30 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 x00, /* 10..17
70740 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
70750 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70760 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
70770 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70780 20 20 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e /* 18..1f .
70790 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
707a0 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 1, 0x00, 0x00, 0
707b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
707c0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
707d0 20 32 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 20..27 !"#$
707e0 25 26 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 %&' */. 0x00, 0
707f0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70800 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70810 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 0, 0x00, /* 28.
70820 2e 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 .2f ()*+,-./
70830 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c */. 0x0c, 0x0c,
70840 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 0x0c, 0x0c, 0x0
70850 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 c, 0x0c, 0x0c, 0
70860 78 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 x0c, /* 30..37
70870 20 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 01234567 */.
70880 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 0x0c, 0x0c, 0x0
70890 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
708a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
708b0 20 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 /* 38..3f 8
708c0 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 9:;<=>? */.. 0x
708d0 30 30 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 00, 0x0a, 0x0a,
708e0 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 0x0a, 0x0a, 0x0a
708f0 2c 20 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f , 0x0a, 0x02, /
70900 2a 20 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 * 40..47 @ABC
70910 44 45 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 DEFG */. 0x02,
70920 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
70930 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
70940 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 02, 0x02, /* 48
70950 2e 2e 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f ..4f HIJKLMNO
70960 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 */. 0x02, 0x02
70970 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
70980 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
70990 30 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 0x02, /* 50..57
709a0 20 20 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a PQRSTUVW */.
709b0 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 0x02, 0x02, 0x
709c0 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 02, 0x00, 0x00,
709d0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
709e0 2c 20 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 , /* 58..5f
709f0 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 XYZ[\]^_ */. 0x
70a00 30 30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 00, 0x2a, 0x2a,
70a10 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 0x2a, 0x2a, 0x2a
70a20 2c 20 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f , 0x2a, 0x22, /
70a30 2a 20 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 * 60..67 `abc
70a40 64 65 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 defg */. 0x22,
70a50 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
70a60 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
70a70 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 22, 0x22, /* 68
70a80 2e 2e 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f ..6f hijklmno
70a90 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 */. 0x22, 0x22
70aa0 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
70ab0 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
70ac0 30 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 0x22, /* 70..77
70ad0 20 20 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a pqrstuvw */.
70ae0 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 0x22, 0x22, 0x
70af0 32 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 22, 0x00, 0x00,
70b00 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
70b10 2c 20 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 , /* 78..7f
70b20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 xyz{|}~. */.. 0
70b30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70b40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70b50 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
70b60 2f 2a 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e /* 80..87 ...
70b70 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
70b80 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70b90 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
70ba0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38 x00, 0x00, /* 8
70bb0 38 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..8f .......
70bc0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
70bd0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
70be0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70bf0 20 30 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 0x00, /* 90..9
70c00 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
70c10 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
70c20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70c30 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70c40 30 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 0, /* 98..9f
70c50 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
70c60 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70c70 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70c80 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
70c90 2f 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e /* a0..a7 ...
70ca0 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
70cb0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70cc0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
70cd0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61 x00, 0x00, /* a
70ce0 38 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..af .......
70cf0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
70d00 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
70d10 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70d20 20 30 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 0x00, /* b0..b
70d30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
70d40 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
70d50 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
70d60 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
70d70 30 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 0, /* b8..bf
70d80 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 ........ */..
70d90 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
70da0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
70db0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
70dc0 20 2f 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e /* c0..c7 ..
70dd0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
70de0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
70df0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
70e00 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
70e10 63 38 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e c8..cf ......
70e20 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
70e30 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
70e40 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
70e50 2c 20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 2e , 0x00, /* d0..
70e60 64 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a d7 ........ *
70e70 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
70e80 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
70e90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
70ea0 30 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 00, /* d8..df
70eb0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
70ec0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
70ed0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
70ee0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
70ef0 20 2f 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e /* e0..e7 ..
70f00 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
70f10 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
70f20 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
70f30 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
70f40 65 38 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e e8..ef ......
70f50 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
70f60 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
70f70 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
70f80 2c 20 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 2e , 0x00, /* f0..
70f90 66 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a f7 ........ *
70fa0 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
70fb0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
70fc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
70fd0 30 30 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 00 /* f8..ff
70fe0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b ........ */.};
70ff0 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a .#endif..../*.**
71000 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 The following s
71010 69 6e 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e ingleton contain
71020 73 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e s the global con
71030 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a figuration for.*
71040 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 * the SQLite lib
71050 72 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rary..*/.SQLITE_
71060 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 PRIVATE SQLITE_W
71070 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 SD struct Sqlite
71080 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 3Config sqlite3C
71090 6f 6e 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c onfig = {. SQL
710a0 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 ITE_DEFAULT_MEMS
710b0 54 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 TATUS, /* bMems
710c0 74 61 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 tat */. 1,
710d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
710e0 20 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 /* bCoreMut
710f0 65 78 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f ex */. SQLITE_
71100 54 48 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 THREADSAFE==1,
71110 20 20 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 /* bFullMute
71120 78 20 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 x */. 0x7fffff
71130 66 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 fe,
71140 20 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a /* mxStrlen *
71150 2f 0a 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 /. 100,
71160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71170 2f 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a /* szLookaside *
71180 2f 0a 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 /. 500,
71190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
711a0 2f 2a 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f /* nLookaside */
711b0 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 . {0,0,0,0,0,0
711c0 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f ,0,0}, /
711d0 2a 20 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 * m */. {0,0,0
711e0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 ,0,0,0,0,0,0},
711f0 20 20 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f /* mutex */
71200 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 . {0,0,0,0,0,0
71210 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f ,0,0,0,0,0}, /
71220 2a 20 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 * pcache */. (
71230 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 void*)0,
71240 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 /* pHe
71250 61 70 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ap */. 0,
71260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71270 20 20 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a /* nHeap */.
71280 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 0, 0,
71290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
712a0 20 6d 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 mnHeap, mxHeap
712b0 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 */. (void*)0,
712c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
712d0 20 2f 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a /* pScratch */.
712e0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
712f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
71300 20 73 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 szScratch */.
71310 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
71320 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e /* n
71330 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 Scratch */. (v
71340 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 oid*)0,
71350 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 /* pPag
71360 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 e */. 0,
71370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71380 20 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a /* szPage */.
71390 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
713a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
713b0 20 6e 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 nPage */. 0,
713c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
713d0 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 /* mxPar
713e0 73 65 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 serStack */. 0
713f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
71400 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 /* sha
71410 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 redCacheEnabled
71420 2a 2f 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 */. /* All the
71430 20 72 65 73 74 20 6e 65 65 64 20 74 6f 20 61 6c rest need to al
71440 77 61 79 73 20 62 65 20 7a 65 72 6f 20 2a 2f 0a ways be zero */.
71450 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
71460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
71470 20 69 73 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c isInit */. 0,
71480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71490 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 50 72 /* inPr
714a0 6f 67 72 65 73 73 20 2a 2f 0a 20 20 20 30 2c 20 ogress */. 0,
714b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
714c0 20 20 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 6c /* isMal
714d0 6c 6f 63 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c locInit */. 0,
714e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
714f0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 /* pIni
71500 74 4d 75 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 tMutex */. 0,
71510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71520 20 20 20 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 /* nRefI
71530 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a nitMutex */.};..
71540 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c ./*.** Hash tabl
71550 65 20 66 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e e for global fun
71560 63 74 69 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f ctions - functio
71570 6e 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c ns common to all
71580 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
71590 6e 65 63 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 nections. After
715a0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c initialization,
715b0 20 74 68 69 73 20 74 61 62 6c 65 20 69 73 0a 2a this table is.*
715c0 2a 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a * read-only..*/.
715d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
715e0 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 QLITE_WSD FuncDe
715f0 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f fHash sqlite3Glo
71600 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f balFunctions;../
71610 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f *.** The value o
71620 66 20 74 68 65 20 22 70 65 6e 64 69 6e 67 22 20 f the "pending"
71630 62 79 74 65 20 6d 75 73 74 20 62 65 20 30 78 34 byte must be 0x4
71640 30 30 30 30 30 30 30 20 28 31 20 62 79 74 65 20 0000000 (1 byte
71650 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 past the.** 1-gi
71660 62 61 62 79 74 65 20 62 6f 75 6e 64 61 72 79 29 babyte boundary)
71670 20 69 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 in a compatible
71680 20 64 61 74 61 62 61 73 65 2e 20 20 53 51 4c 69 database. SQLi
71690 74 65 20 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a te never uses.**
716a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 the database pa
716b0 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ge that contains
716c0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 the pending byt
716d0 65 2e 20 20 49 74 20 6e 65 76 65 72 20 61 74 74 e. It never att
716e0 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 empts.** to read
716f0 20 6f 72 20 77 72 69 74 65 20 74 68 61 74 20 70 or write that p
71700 61 67 65 2e 20 20 54 68 65 20 70 65 6e 64 69 6e age. The pendin
71710 67 20 62 79 74 65 20 70 61 67 65 20 69 73 20 73 g byte page is s
71720 65 74 20 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 et assign.** for
71730 20 75 73 65 20 62 79 20 74 68 65 20 56 46 53 20 use by the VFS
71740 6c 61 79 65 72 73 20 61 73 20 73 70 61 63 65 20 layers as space
71750 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c for managing fil
71760 65 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 e locks..**.** D
71770 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 uring testing, i
71780 74 20 69 73 20 6f 66 74 65 6e 20 64 65 73 69 72 t is often desir
71790 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 able to move the
717a0 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f pending byte to
717b0 0a 2a 2a 20 61 20 64 69 66 66 65 72 65 6e 74 20 .** a different
717c0 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 position in the
717d0 66 69 6c 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f file. This allo
717e0 77 73 20 63 6f 64 65 20 74 68 61 74 20 68 61 73 ws code that has
717f0 20 74 6f 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 to.** deal with
71800 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 the pending byt
71810 65 20 74 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 e to run on file
71820 73 20 74 68 61 74 20 61 72 65 20 6d 75 63 68 20 s that are much
71830 73 6d 61 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 smaller.** than
71840 31 20 47 69 42 2e 20 20 54 68 65 20 73 71 6c 69 1 GiB. The sqli
71850 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c te3_test_control
71860 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e () interface can
71870 20 62 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d be used to.** m
71880 6f 76 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 ove the pending
71890 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f byte..**.** IMPO
718a0 52 54 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 RTANT: Changing
718b0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 the pending byt
718c0 65 20 74 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f e to any value o
718d0 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 ther than.** 0x4
718e0 30 30 30 30 30 30 30 20 72 65 73 75 6c 74 73 20 0000000 results
718f0 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 in an incompatib
71900 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 le database file
71910 20 66 6f 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e format!.** Chan
71920 67 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 ging the pending
71930 20 62 79 74 65 20 64 75 72 69 6e 67 20 6f 70 65 byte during ope
71940 72 61 74 69 6e 67 20 72 65 73 75 6c 74 73 20 69 rating results i
71950 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 n undefined.** a
71960 6e 64 20 64 69 6c 65 74 65 72 69 6f 75 73 20 62 nd dileterious b
71970 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 ehavior..*/.SQLI
71980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
71990 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 qlite3PendingByt
719a0 65 20 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a e = 0x40000000;.
719b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
719c0 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 End of global.c
719d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
719e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
719f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
71a00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
71a10 20 42 65 67 69 6e 20 66 69 6c 65 20 73 74 61 74 Begin file stat
71a20 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a us.c ***********
71a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
71a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
71a50 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 ./*.** 2008 June
71a60 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 18.**.** The au
71a70 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
71a80 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
71a90 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
71aa0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
71ab0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
71ac0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
71ad0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
71ae0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
71af0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
71b00 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
71b10 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
71b20 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
71b30 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
71b40 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
71b50 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
71b60 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
71b70 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
71b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
71b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
71ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
71bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
71bc0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f **.**.** This mo
71bd0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 dule implements
71be0 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74 the sqlite3_stat
71bf0 75 73 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 us() interface a
71c00 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 nd related.** fu
71c10 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a nctionality..**.
71c20 2a 2a 20 24 49 64 3a 20 73 74 61 74 75 73 2e 63 ** $Id: status.c
71c30 2c 76 20 31 2e 39 20 32 30 30 38 2f 30 39 2f 30 ,v 1.9 2008/09/0
71c40 32 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 2 00:52:52 drh E
71c50 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 xp $.*/../*.** V
71c60 61 72 69 61 62 6c 65 73 20 69 6e 20 77 68 69 63 ariables in whic
71c70 68 20 74 6f 20 72 65 63 6f 72 64 20 73 74 61 74 h to record stat
71c80 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a us information..
71c90 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
71ca0 74 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 t sqlite3StatTyp
71cb0 65 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 e sqlite3StatTyp
71cc0 65 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 e;.static SQLITE
71cd0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 _WSD struct sqli
71ce0 74 65 33 53 74 61 74 54 79 70 65 20 7b 0a 20 20 te3StatType {.
71cf0 69 6e 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b int nowValue[9];
71d00 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 /* Curr
71d10 65 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 ent value */. i
71d20 6e 74 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 nt mxValue[9];
71d30 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
71d40 75 6d 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 um value */.} sq
71d50 6c 69 74 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 lite3Stat = { {0
71d60 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a ,}, {0,} };.../*
71d70 20 54 68 65 20 22 77 73 64 53 74 61 74 22 20 6d The "wsdStat" m
71d80 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 acro will resolv
71d90 65 20 74 6f 20 74 68 65 20 73 74 61 74 75 73 20 e to the status
71da0 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 information.** s
71db0 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66 tate vector. If
71dc0 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 writable static
71dd0 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f data is unsuppo
71de0 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 rted on the targ
71df0 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 et,.** we have t
71e00 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 o locate the sta
71e10 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e te vector at run
71e20 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d -time. In the m
71e30 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 ore common.** ca
71e40 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c se where writabl
71e50 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 e static data is
71e60 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53 supported, wsdS
71e70 74 61 74 20 63 61 6e 20 72 65 66 65 72 20 64 69 tat can refer di
71e80 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 rectly.** to the
71e90 20 22 73 71 6c 69 74 65 33 53 74 61 74 22 20 73 "sqlite3Stat" s
71ea0 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c tate vector decl
71eb0 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 ared above..*/.#
71ec0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
71ed0 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 T_WSD.# define w
71ee0 73 64 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69 sdStatInit sqli
71ef0 74 65 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d te3StatType *x =
71f00 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 &GLOBAL(sqlite3
71f10 53 74 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33 StatType,sqlite3
71f20 53 74 61 74 29 0a 23 20 64 65 66 69 6e 65 20 77 Stat).# define w
71f30 73 64 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 sdStat x[0].#els
71f40 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 e.# define wsdSt
71f50 61 74 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 atInit.# define
71f60 77 73 64 53 74 61 74 20 73 71 6c 69 74 65 33 53 wsdStat sqlite3S
71f70 74 61 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a tat.#endif../*.*
71f80 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 * Return the cur
71f90 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 20 rent value of a
71fa0 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 status parameter
71fb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
71fc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
71fd0 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 20 6f tatusValue(int o
71fe0 70 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 p){. wsdStatIni
71ff0 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e t;. assert( op>
72000 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 =0 && op<ArraySi
72010 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 ze(wsdStat.nowVa
72020 6c 75 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e lue) );. return
72030 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
72040 65 5b 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 e[op];.}../*.**
72050 41 64 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c Add N to the val
72060 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 72 ue of a status r
72070 65 63 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73 ecord. It is as
72080 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 0a 2a sumed that the.*
72090 2a 20 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 * caller holds a
720a0 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 ppropriate locks
720b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
720c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
720d0 53 74 61 74 75 73 41 64 64 28 69 6e 74 20 6f 70 StatusAdd(int op
720e0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 , int N){. wsdS
720f0 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 tatInit;. asser
72100 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 t( op>=0 && op<A
72110 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 rraySize(wsdStat
72120 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 .nowValue) );.
72130 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
72140 5b 6f 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 [op] += N;. if(
72150 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
72160 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 e[op]>wsdStat.mx
72170 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 Value[op] ){.
72180 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 wsdStat.mxValue
72190 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e [op] = wsdStat.n
721a0 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d owValue[op];. }
721b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
721c0 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 e value of a sta
721d0 74 75 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c tus to X..*/.SQL
721e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
721f0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 sqlite3StatusSe
72200 74 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 t(int op, int X)
72210 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b {. wsdStatInit;
72220 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 . assert( op>=0
72230 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 && op<ArraySize
72240 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 (wsdStat.nowValu
72250 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e e) );. wsdStat.
72260 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 nowValue[op] = X
72270 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61 74 2e ;. if( wsdStat.
72280 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 nowValue[op]>wsd
72290 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d Stat.mxValue[op]
722a0 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e ){. wsdStat.
722b0 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 mxValue[op] = ws
722c0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f dStat.nowValue[o
722d0 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p];. }.}../*.**
722e0 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69 6e Query status in
722f0 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a formation..**.**
72300 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 This implementa
72310 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
72320 74 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 t reading or wri
72330 74 69 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a ting an aligned.
72340 2a 2a 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 ** 32-bit intege
72350 72 20 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f r is an atomic o
72360 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 peration. If th
72370 61 74 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 at assumption is
72380 20 6e 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 not true,.** th
72390 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
723a0 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 is not threadsaf
723b0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
723c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 int sqlite3_sta
723d0 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 tus(int op, int
723e0 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a *pCurrent, int *
723f0 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 pHighwater, int
72400 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 resetFlag){. ws
72410 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 dStatInit;. if(
72420 20 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 op<0 || op>=Arr
72430 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e aySize(wsdStat.n
72440 6f 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 owValue) ){.
72450 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
72460 53 55 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 SUSE;. }. *pCu
72470 72 72 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e rrent = wsdStat.
72480 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 nowValue[op];.
72490 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 *pHighwater = ws
724a0 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 dStat.mxValue[op
724b0 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c ];. if( resetFl
724c0 61 67 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 ag ){. wsdSta
724d0 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 t.mxValue[op] =
724e0 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
724f0 5b 6f 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 [op];. }. retu
72500 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
72510 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 ./*.** Query sta
72520 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 tus information
72530 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 for a single dat
72540 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
72550 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
72560 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 nt sqlite3_db_st
72570 61 74 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 atus(. sqlite3
72580 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a *db, /*
72590 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
725a0 6e 6e 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 nnection whose s
725b0 74 61 74 75 73 20 69 73 20 64 65 73 69 72 65 64 tatus is desired
725c0 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 */. int op,
725d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
725e0 74 61 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 tatus verb */.
725f0 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 int *pCurrent,
72600 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 /* Write c
72610 75 72 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 urrent value her
72620 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 e */. int *pHig
72630 68 77 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 hwater, /*
72640 57 72 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 Write high-water
72650 20 6d 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 mark here */.
72660 69 6e 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 int resetFlag
72670 20 20 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 /* Reset h
72680 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 igh-water mark i
72690 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 f true */.){. s
726a0 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 witch( op ){.
726b0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 case SQLITE_DBS
726c0 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f TATUS_LOOKASIDE_
726d0 55 53 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 USED: {. *p
726e0 43 75 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f Current = db->lo
726f0 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 okaside.nOut;.
72700 20 20 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 *pHighwater
72710 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e = db->lookaside.
72720 6d 78 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 28 mxOut;. if(
72730 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 resetFlag ){.
72740 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 db->lookas
72750 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e ide.mxOut = db->
72760 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a lookaside.nOut;.
72770 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
72780 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
72790 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
727a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
727b0 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ROR;. }. }.
727c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
727d0 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a K;.}../*********
727e0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 ***** End of sta
727f0 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tus.c **********
72800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72820 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
72830 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
72840 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a date.c ********
72850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72870 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 ****/./*.** 2003
72880 20 4f 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a October 31.**.*
72890 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
728a0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
728b0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
728c0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
728d0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
728e0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
728f0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
72900 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
72910 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
72920 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
72930 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
72940 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
72950 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
72960 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
72970 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
72980 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
72990 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
729a0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
729b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
729f0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
72a00 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
72a10 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
72a20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a date and time.**
72a30 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 functions for S
72a40 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 QLite. .**.** T
72a50 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 here is only one
72a60 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c exported symbol
72a70 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 in this file -
72a80 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 the function.**
72a90 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 sqlite3RegisterD
72aa0 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 ateTimeFunctions
72ab0 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 () found at the
72ac0 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 bottom of the fi
72ad0 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 le..** All other
72ae0 20 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 code has file s
72af0 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a cope..**.** $Id:
72b00 20 64 61 74 65 2e 63 2c 76 20 31 2e 31 30 33 20 date.c,v 1.103
72b10 32 30 30 39 2f 30 32 2f 30 34 20 30 33 3a 35 39 2009/02/04 03:59
72b20 3a 32 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a :25 shane Exp $.
72b30 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f **.** SQLite pro
72b40 63 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 cesses all times
72b50 20 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a 75 and dates as Ju
72b60 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 lian Day numbers
72b70 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 . The.** dates
72b80 61 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73 74 and times are st
72b90 6f 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d 62 ored as the numb
72ba0 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 er of days since
72bb0 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 noon.** in Gree
72bc0 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 nwich on Novembe
72bd0 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 r 24, 4714 B.C.
72be0 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 according to the
72bf0 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 Gregorian.** ca
72c00 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 0a lendar system. .
72c10 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 **.** 1970-01-01
72c20 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 00:00:00 is JD
72c30 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 2440587.5.** 200
72c40 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 0-01-01 00:00:00
72c50 20 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e 35 is JD 2451544.5
72c60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c .**.** This impl
72c70 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 emention require
72c80 73 20 79 65 61 72 73 20 74 6f 20 62 65 20 65 78 s years to be ex
72c90 70 72 65 73 73 65 64 20 61 73 20 61 20 34 2d 64 pressed as a 4-d
72ca0 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 igit number.** w
72cb0 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 hich means that
72cc0 6f 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77 65 only dates betwe
72cd0 65 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61 6e en 0000-01-01 an
72ce0 64 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61 6e d 9999-12-31 can
72cf0 0a 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e 74 .** be represent
72d00 65 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 ed, even though
72d10 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 julian day numbe
72d20 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 rs allow a much
72d30 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f wider.** range o
72d40 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 f dates..**.** T
72d50 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c he Gregorian cal
72d60 65 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73 20 endar system is
72d70 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 used for all dat
72d80 65 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a es and times,.**
72d90 20 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61 74 even those that
72da0 20 70 72 65 64 61 74 65 20 74 68 65 20 47 72 65 predate the Gre
72db0 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e gorian calendar.
72dc0 20 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73 75 Historians usu
72dd0 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 ally.** use the
72de0 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 Julian calendar
72df0 66 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72 20 for dates prior
72e00 74 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61 6e to 1582-10-15 an
72e10 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 d for some.** da
72e20 74 65 73 20 61 66 74 65 72 77 61 72 64 73 2c 20 tes afterwards,
72e30 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 depending on loc
72e40 61 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66 20 ale. Beware of
72e50 74 68 69 73 20 64 69 66 66 65 72 65 6e 63 65 2e this difference.
72e60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 .**.** The conve
72e70 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 rsion algorithms
72e80 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 are implemented
72e90 20 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72 69 based on descri
72ea0 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 ptions.** in the
72eb0 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a following text:
72ec0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e .**.** Jean
72ed0 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 Meeus.** A
72ee0 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f stronomical Algo
72ef0 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 rithms, 2nd Edit
72f00 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 ion, 1998.**
72f10 20 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36 2d ISBM 0-943396-
72f20 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 61-1.** Wil
72f30 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a lmann-Bell, Inc.
72f40 2a 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 ** Richmond
72f50 2c 20 56 69 72 67 69 6e 69 61 20 28 55 53 41 29 , Virginia (USA)
72f60 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 .*/.#include <ti
72f70 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 me.h>..#ifndef S
72f80 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 QLITE_OMIT_DATET
72f90 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a IME_FUNCS../*.**
72fa0 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 6f On recent Windo
72fb0 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 68 ws platforms, th
72fc0 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 20 e localtime_s()
72fd0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 69 function is avai
72fe0 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 74 lable.** as part
72ff0 20 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 20 of the "Secure
73000 43 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 65 CRT". It is esse
73010 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c 65 ntially equivale
73020 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 nt to .** localt
73030 69 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 6c ime_r() availabl
73040 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f 53 e under most POS
73050 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 78 IX platforms, ex
73060 63 65 70 74 20 74 68 61 74 20 74 68 65 20 0a 2a cept that the .*
73070 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 70 * order of the p
73080 61 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 arameters is rev
73090 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 ersed..**.** See
730a0 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 http://msdn.mic
730b0 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 rosoft.com/en-us
730c0 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79 /library/a442x3y
730d0 65 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a 2a e(VS.80).aspx..*
730e0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 72 *.** If the user
730f0 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 74 has not indicat
73100 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c 74 ed to use localt
73110 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 6c ime_r() or local
73120 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 time_s().** alre
73130 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 ady, check for a
73140 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e 76 n MSVC build env
73150 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 72 ironment that pr
73160 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c ovides .** local
73170 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 time_s()..*/.#if
73180 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c !defined(HAVE_L
73190 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 21 OCALTIME_R) && !
731a0 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 defined(HAVE_LOC
731b0 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a 20 ALTIME_S) && \.
731c0 20 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 defined(_MSC
731d0 5f 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 64 _VER) && defined
731e0 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f 44 (_CRT_INSECURE_D
731f0 45 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 6e EPRECATE).#defin
73200 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 e HAVE_LOCALTIME
73210 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a _S 1.#endif../*.
73220 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 66 ** A structure f
73230 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e or holding a sin
73240 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d gle date and tim
73250 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 e..*/.typedef st
73260 72 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 61 ruct DateTime Da
73270 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 teTime;.struct D
73280 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69 ateTime {. sqli
73290 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f te3_int64 iJD; /
732a0 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 * The julian day
732b0 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 36 number times 86
732c0 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20 400000 */. int
732d0 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f Y, M, D; /
732e0 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 * Year, month, a
732f0 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20 nd day */. int
73300 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 2f h, m; /
73310 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 * Hour and minut
73320 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 es */. int tz;
73330 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 /* Ti
73340 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e mezone offset in
73350 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f minutes */. do
73360 75 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 20 uble s;
73370 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 /* Seconds */.
73380 20 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b 20 char validYMD;
73390 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 /* True (1)
733a0 69 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c if Y,M,D are val
733b0 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c id */. char val
733c0 69 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54 72 idHMS; /* Tr
733d0 75 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73 20 ue (1) if h,m,s
733e0 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 are valid */. c
733f0 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 20 har validJD;
73400 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 /* True (1) if
73410 20 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a 2f iJD is valid */
73420 0a 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a 3b . char validTZ;
73430 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 /* True (1
73440 29 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 64 ) if tz is valid
73450 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 */.};.../*.** C
73460 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e 74 onvert zDate int
73470 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e o one or more in
73480 74 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 6f tegers. Additio
73490 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a nal arguments.**
734a0 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 come in groups
734b0 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a of 5 as follows:
734c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 .**.** N
734d0 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 number of d
734e0 69 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e 74 igits in the int
734f0 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 eger.** mi
73500 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c n minimum al
73510 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 lowed value of t
73520 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 he integer.**
73530 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 69 max maxi
73540 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 mum allowed valu
73550 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 e of the integer
73560 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 20 .** nextC
73570 20 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 first characte
73580 72 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 65 r after the inte
73590 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 ger.** pVa
735a0 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 72 l where to wr
735b0 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 73 ite the integers
735c0 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f value..**.** Co
735d0 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e nversions contin
735e0 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 ue until one wit
735f0 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 6e h nextC==0 is en
73600 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 countered..** Th
73610 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 e function retur
73620 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
73630 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 successful conv
73640 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 ersions..*/.stat
73650 69 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 73 ic int getDigits
73660 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 (const char *zDa
73670 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c te, ...){. va_l
73680 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 ist ap;. int va
73690 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e l;. int N;. in
736a0 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 t min;. int max
736b0 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 ;. int nextC;.
736c0 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e int *pVal;. in
736d0 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f t cnt = 0;. va_
736e0 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 29 start(ap, zDate)
736f0 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 ;. do{. N =
73700 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b va_arg(ap, int);
73710 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 . min = va_ar
73720 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 g(ap, int);.
73730 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c max = va_arg(ap,
73740 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 int);. nextC
73750 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e = va_arg(ap, in
73760 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 t);. pVal = v
73770 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b a_arg(ap, int*);
73780 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 . val = 0;.
73790 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a while( N-- ){.
737a0 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 if( !sqlit
737b0 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 e3Isdigit(*zDate
737c0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 ) ){. got
737d0 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b o end_getDigits;
737e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 . }. v
737f0 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a al = val*10 + *z
73800 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 Date - '0';.
73810 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d zDate++;. }
73820 0a 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e . if( val<min
73830 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 || val>max || (
73840 6e 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74 nextC!=0 && next
73850 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 C!=*zDate) ){.
73860 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 goto end_get
73870 44 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 Digits;. }.
73880 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 *pVal = val;.
73890 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
738a0 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 cnt++;. }while(
738b0 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 nextC );.end_ge
738c0 74 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e tDigits:. va_en
738d0 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 d(ap);. return
738e0 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 cnt;.}../*.** Re
738f0 61 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d ad text from z[]
73900 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74 and convert int
73910 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 o a floating poi
73920 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 nt number. Retu
73930 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 rn.** the number
73940 20 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65 of digits conve
73950 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rted..*/.#define
73960 20 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65 getValue sqlite
73970 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 3AtoF../*.** Par
73980 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 se a timezone ex
73990 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 tension on the e
739a0 6e 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d nd of a date-tim
739b0 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 e..** The extens
739c0 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f ion is of the fo
739d0 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 rm:.**.**
739e0 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a (+/-)HH:MM.**.*
739f0 2a 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 * Or the "zulu"
73a00 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 notation:.**.**
73a10 20 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 Z.**.** I
73a20 66 20 74 68 65 20 70 61 72 73 65 20 69 73 20 73 f the parse is s
73a30 75 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 uccessful, write
73a40 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d the number of m
73a50 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 inutes.** of cha
73a60 6e 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 nge in p->tz and
73a70 20 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 return 0. If a
73a80 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 parser error oc
73a90 63 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 curs,.** return
73aa0 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 non-zero..**.**
73ab0 41 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 A missing specif
73ac0 69 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 ier is not consi
73ad0 64 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a dered an error..
73ae0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
73af0 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 rseTimezone(cons
73b00 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 t char *zDate, D
73b10 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 ateTime *p){. i
73b20 6e 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e nt sgn = 0;. in
73b30 74 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e t nHr, nMn;. in
73b40 74 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 t c;. while( sq
73b50 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 lite3Isspace(*zD
73b60 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b ate) ){ zDate++;
73b70 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a }. p->tz = 0;.
73b80 20 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 c = *zDate;.
73b90 69 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 if( c=='-' ){.
73ba0 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 sgn = -1;. }e
73bb0 6c 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 29 lse if( c=='+' )
73bc0 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a {. sgn = +1;.
73bd0 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 }else if( c=='
73be0 5a 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a Z' || c=='z' ){.
73bf0 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 zDate++;.
73c00 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b goto zulu_time;
73c10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
73c20 74 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 turn c!=0;. }.
73c30 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 20 zDate++;. if(
73c40 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c getDigits(zDate,
73c50 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c 20 2, 0, 14, ':',
73c60 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c 20 &nHr, 2, 0, 59,
73c70 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 0, &nMn)!=2 ){.
73c80 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
73c90 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 . zDate += 5;.
73ca0 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d p->tz = sgn*(nM
73cb0 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c n + nHr*60);.zul
73cc0 75 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 28 u_time:. while(
73cd0 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
73ce0 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 *zDate) ){ zDate
73cf0 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a ++; }. return *
73d00 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a zDate!=0;.}../*.
73d10 2a 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f ** Parse times o
73d20 66 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d f the form HH:MM
73d30 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 or HH:MM:SS or
73d40 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a HH:MM:SS.FFFF..*
73d50 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e * The HH, MM, an
73d60 64 20 53 53 20 6d 75 73 74 20 65 61 63 68 20 62 d SS must each b
73d70 65 20 65 78 61 63 74 6c 79 20 32 20 64 69 67 69 e exactly 2 digi
73d80 74 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63 ts. The.** frac
73d90 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46 tional seconds F
73da0 46 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f FFF can be one o
73db0 72 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a r more digits..*
73dc0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 *.** Return 1 if
73dd0 20 74 68 65 72 65 20 69 73 20 61 20 70 61 72 73 there is a pars
73de0 69 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20 ing error and 0
73df0 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 on success..*/.s
73e00 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 48 tatic int parseH
73e10 68 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72 hMmSs(const char
73e20 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d *zDate, DateTim
73e30 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20 e *p){. int h,
73e40 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d m, s;. double m
73e50 73 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67 s = 0.0;. if( g
73e60 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 etDigits(zDate,
73e70 32 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26 2, 0, 24, ':', &
73e80 68 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 h, 2, 0, 59, 0,
73e90 26 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 &m)!=2 ){. re
73ea0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 turn 1;. }. zD
73eb0 61 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20 ate += 5;. if(
73ec0 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 *zDate==':' ){.
73ed0 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
73ee0 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 if( getDigits(zD
73ef0 61 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 ate, 2, 0, 59, 0
73f00 2c 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20 , &s)!=1 ){.
73f10 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
73f20 7d 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32 }. zDate += 2
73f30 3b 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65 ;. if( *zDate
73f40 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65 33 =='.' && sqlite3
73f50 49 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31 5d Isdigit(zDate[1]
73f60 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c ) ){. doubl
73f70 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b 0a e rScale = 1.0;.
73f80 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 zDate++;.
73f90 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 while( sqli
73fa0 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 te3Isdigit(*zDat
73fb0 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 e) ){. ms
73fc0 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 = ms*10.0 + *zD
73fd0 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 ate - '0';.
73fe0 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e rScale *= 10.
73ff0 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 0;. zDate
74000 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
74010 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a ms /= rScale;.
74020 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
74030 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 s = 0;. }.
74040 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a p->validJD = 0;.
74050 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 p->validHMS =
74060 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 1;. p->h = h;.
74070 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e p->m = m;. p->
74080 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 s = s + ms;. if
74090 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 ( parseTimezone(
740a0 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 zDate, p) ) retu
740b0 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 rn 1;. p->valid
740c0 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f TZ = (p->tz!=0)?
740d0 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 1:0;. return 0;
740e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
740f0 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 t from YYYY-MM-D
74100 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 D HH:MM:SS to ju
74110 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c lian day. We al
74120 77 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 ways assume.** t
74130 68 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d hat the YYYY-MM-
74140 44 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 DD is according
74150 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e to the Gregorian
74160 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a calendar..**.**
74170 20 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 Reference: Mee
74180 75 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 us page 61.*/.st
74190 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 atic void comput
741a0 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 eJD(DateTime *p)
741b0 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c {. int Y, M, D,
741c0 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a A, B, X1, X2;..
741d0 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 if( p->validJD
741e0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
741f0 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a p->validYMD ){.
74200 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 Y = p->Y;.
74210 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 M = p->M;.
74220 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 D = p->D;. }els
74230 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b e{. Y = 2000;
74240 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 /* If no YMD s
74250 70 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 pecified, assume
74260 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 2000-Jan-01 */.
74270 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 M = 1;. D
74280 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 1;. }. if(
74290 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b M<=2 ){. Y--;
742a0 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 . M += 12;.
742b0 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 }. A = Y/100;.
742c0 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f B = 2 - A + (A/
742d0 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35 4);. X1 = 36525
742e0 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20 *(Y+4716)/100;.
742f0 20 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b X2 = 306001*(M+
74300 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69 1)/10000;. p->i
74310 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e JD = (sqlite3_in
74320 74 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20 t64)((X1 + X2 +
74330 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29 D + B - 1524.5 )
74340 20 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 * 86400000);.
74350 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a p->validJD = 1;.
74360 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d if( p->validHM
74370 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 S ){. p->iJD
74380 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 += p->h*3600000
74390 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28 + p->m*60000 + (
743a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70 sqlite3_int64)(p
743b0 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69 ->s*1000);. i
743c0 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b f( p->validTZ ){
743d0 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d . p->iJD -=
743e0 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20 p->tz*60000;.
743f0 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 p->validYMD
74400 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 = 0;. p->va
74410 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20 lidHMS = 0;.
74420 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 p->validTZ = 0
74430 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
74440 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73 *.** Parse dates
74450 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a of the form.**.
74460 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 ** YYYY-MM-D
74470 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a D HH:MM:SS.FFF.*
74480 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 * YYYY-MM-DD
74490 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 HH:MM:SS.**
744a0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d YYYY-MM-DD HH:M
744b0 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d M.** YYYY-MM
744c0 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 -DD.**.** Write
744d0 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 the result into
744e0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 the DateTime str
744f0 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 ucture and retur
74500 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 n 0.** on succes
74510 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 69 s and 1 if the i
74520 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e nput string is n
74530 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 ot a well-formed
74540 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 .** date..*/.sta
74550 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 tic int parseYyy
74560 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 yMmDd(const char
74570 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d *zDate, DateTim
74580 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 e *p){. int Y,
74590 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 M, D, neg;.. if
745a0 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 ( zDate[0]=='-'
745b0 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a ){. zDate++;.
745c0 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d neg = 1;. }
745d0 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 else{. neg =
745e0 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 0;. }. if( get
745f0 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30 Digits(zDate,4,0
74600 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 ,9999,'-',&Y,2,1
74610 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 ,12,'-',&M,2,1,3
74620 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20 1,0,&D)!=3 ){.
74630 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
74640 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20 zDate += 10;.
74650 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
74660 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 7c sspace(*zDate) |
74670 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 | 'T'==*(u8*)zDa
74680 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d te ){ zDate++; }
74690 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d . if( parseHhMm
746a0 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 Ss(zDate, p)==0
746b0 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 ){. /* We got
746c0 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d the time */. }
746d0 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d else if( *zDate=
746e0 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c =0 ){. p->val
746f0 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c idHMS = 0;. }el
74700 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 se{. return 1
74710 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 ;. }. p->valid
74720 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c JD = 0;. p->val
74730 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e idYMD = 1;. p->
74740 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 Y = neg ? -Y : Y
74750 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 ;. p->M = M;.
74760 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 p->D = D;. if(
74770 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 p->validTZ ){.
74780 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
74790 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
747a0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
747b0 20 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 time to the cur
747c0 72 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 rent time report
747d0 65 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f ed by the VFS.*/
747e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
747f0 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e DateTimeToCurren
74800 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 t(sqlite3_contex
74810 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65 t *context, Date
74820 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62 Time *p){. doub
74830 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 le r;. sqlite3
74840 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f *db = sqlite3_co
74850 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
74860 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 context);. sqli
74870 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
74880 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a (db->pVfs, &r);.
74890 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 p->iJD = (sqli
748a0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 te3_int64)(r*864
748b0 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 00000.0 + 0.5);.
748c0 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 p->validJD = 1
748d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d ;.}../*.** Attem
748e0 70 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 pt to parse the
748f0 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 given string int
74900 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e o a Julian Day N
74910 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a umber. Return.*
74920 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
74930 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 errors..**.** Th
74940 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
74950 61 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 acceptable forms
74960 20 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 for the input s
74970 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 tring:.**.**
74980 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a YYYY-MM-DD HH:
74990 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 MM:SS.FFF +/-HH
749a0 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 :MM.** DDDD
749b0 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 .DD .** now
749c0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 .**.** In the fi
749d0 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f rst form, the +/
749e0 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 -HH:MM is always
749f0 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 optional. The
74a00 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 fractional.** se
74a10 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 conds extension
74a20 28 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 (the ".FFF") is
74a30 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 optional. The s
74a40 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a econds portion.*
74a50 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 * (":SS.FFF") is
74a60 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 option. The ye
74a70 61 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 ar and date can
74a80 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f be omitted as lo
74a90 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 ng.** as there i
74aa0 73 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e s a time string.
74ab0 20 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e The time strin
74ac0 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 g can be omitted
74ad0 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 as long.** as t
74ae0 68 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61 here is a year a
74af0 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 nd date..*/.stat
74b00 69 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 ic int parseDate
74b10 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65 OrTime(. sqlite
74b20 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
74b30 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 xt, . const cha
74b40 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74 r *zDate, . Dat
74b50 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 66 eTime *p.){. if
74b60 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 ( parseYyyyMmDd(
74b70 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 zDate,p)==0 ){.
74b80 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
74b90 65 6c 73 65 20 69 66 28 20 70 61 72 73 65 48 68 else if( parseHh
74ba0 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d MmSs(zDate, p)==
74bb0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
74bc0 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 0;. }else if( s
74bd0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 qlite3StrICmp(zD
74be0 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a ate,"now")==0){.
74bf0 20 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 setDateTimeT
74c00 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 oCurrent(context
74c10 2c 20 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e , p);. return
74c20 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
74c30 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 sqlite3IsNumber(
74c40 7a 44 61 74 65 2c 20 30 2c 20 53 51 4c 49 54 45 zDate, 0, SQLITE
74c50 5f 55 54 46 38 29 20 29 7b 0a 20 20 20 20 64 6f _UTF8) ){. do
74c60 75 62 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 56 uble r;. getV
74c70 61 6c 75 65 28 7a 44 61 74 65 2c 20 26 72 29 3b alue(zDate, &r);
74c80 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 . p->iJD = (s
74c90 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a qlite3_int64)(r*
74ca0 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 86400000.0 + 0.5
74cb0 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a );. p->validJ
74cc0 44 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 D = 1;. retur
74cd0 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n 0;. }. retur
74ce0 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f n 1;.}../*.** Co
74cf0 6d 70 75 74 65 20 74 68 65 20 59 65 61 72 2c 20 mpute the Year,
74d00 4d 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66 Month, and Day f
74d10 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 rom the julian d
74d20 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 ay number..*/.st
74d30 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 atic void comput
74d40 65 59 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70 eYMD(DateTime *p
74d50 29 7b 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 ){. int Z, A, B
74d60 2c 20 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 , C, D, E, X1;.
74d70 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 if( p->validYMD
74d80 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
74d90 20 21 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a !p->validJD ){.
74da0 20 20 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b p->Y = 2000;
74db0 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 . p->M = 1;.
74dc0 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d p->D = 1;. }
74dd0 65 6c 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 else{. Z = (i
74de0 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 nt)((p->iJD + 43
74df0 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 200000)/86400000
74e00 29 3b 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 29 );. A = (int)
74e10 28 28 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35 ((Z - 1867216.25
74e20 29 2f 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 )/36524.25);.
74e30 20 41 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d A = Z + 1 + A -
74e40 20 28 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 (A/4);. B =
74e50 41 20 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20 A + 1524;. C
74e60 3d 20 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32 = (int)((B - 122
74e70 2e 31 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 .1)/365.25);.
74e80 20 44 20 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 D = (36525*C)/1
74e90 30 30 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 00;. E = (int
74ea0 29 28 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 )((B-D)/30.6001)
74eb0 3b 0a 20 20 20 20 58 31 20 3d 20 28 69 6e 74 29 ;. X1 = (int)
74ec0 28 33 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 (30.6001*E);.
74ed0 20 70 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 p->D = B - D -
74ee0 58 31 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 X1;. p->M = E
74ef0 3c 31 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 <14 ? E-1 : E-13
74f00 3b 0a 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e ;. p->Y = p->
74f10 4d 3e 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a M>2 ? C - 4716 :
74f20 20 43 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 C - 4715;. }.
74f30 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 p->validYMD = 1
74f40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 ;.}../*.** Compu
74f50 74 65 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e te the Hour, Min
74f60 75 74 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 ute, and Seconds
74f70 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e from the julian
74f80 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a day number..*/.
74f90 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 static void comp
74fa0 75 74 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 uteHMS(DateTime
74fb0 2a 70 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 *p){. int s;.
74fc0 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 if( p->validHMS
74fd0 29 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 ) return;. comp
74fe0 75 74 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 uteJD(p);. s =
74ff0 28 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 (int)((p->iJD +
75000 34 33 32 30 30 30 30 30 29 20 25 20 38 36 34 30 43200000) % 8640
75010 30 30 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 0000);. p->s =
75020 73 2f 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 s/1000.0;. s =
75030 28 69 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e (int)p->s;. p->
75040 73 20 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d s -= s;. p->h =
75050 20 73 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 s/3600;. s -=
75060 70 2d 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e p->h*3600;. p->
75070 6d 20 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 m = s/60;. p->s
75080 20 2b 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b += s - p->m*60;
75090 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d . p->validHMS =
750a0 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 1;.}../*.** Com
750b0 70 75 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e pute both YMD an
750c0 64 20 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 d HMS.*/.static
750d0 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f void computeYMD_
750e0 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 HMS(DateTime *p)
750f0 7b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 {. computeYMD(p
75100 29 3b 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 );. computeHMS(
75110 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 p);.}../*.** Cle
75120 61 72 20 74 68 65 20 59 4d 44 20 61 6e 64 20 48 ar the YMD and H
75130 4d 53 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f MS and the TZ.*/
75140 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 .static void cle
75150 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 arYMD_HMS_TZ(Dat
75160 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e eTime *p){. p->
75170 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 validYMD = 0;.
75180 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b p->validHMS = 0;
75190 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 . p->validTZ =
751a0 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 0;.}..#ifndef SQ
751b0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 LITE_OMIT_LOCALT
751c0 49 4d 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 IME./*.** Comput
751d0 65 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 e the difference
751e0 20 28 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 (in millisecond
751f0 73 29 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f s).** between lo
75200 63 61 6c 74 69 6d 65 20 61 6e 64 20 55 54 43 20 caltime and UTC
75210 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 (a.k.a. GMT).**
75220 66 6f 72 20 74 68 65 20 74 69 6d 65 20 76 61 6c for the time val
75230 75 65 20 70 20 77 68 65 72 65 20 70 20 69 73 20 ue p where p is
75240 69 6e 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 in UTC..*/.stati
75250 63 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 c sqlite3_int64
75260 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 localtimeOffset(
75270 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 DateTime *p){.
75280 44 61 74 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 DateTime x, y;.
75290 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d time_t t;. x =
752a0 20 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d *p;. computeYM
752b0 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 D_HMS(&x);. if(
752c0 20 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 x.Y<1971 || x.Y
752d0 3e 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 78 2e >=2038 ){. x.
752e0 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 2e Y = 2000;. x.
752f0 4d 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d M = 1;. x.D =
75300 20 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 1;. x.h = 0;
75310 0a 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 . x.m = 0;.
75320 20 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d x.s = 0.0;. }
75330 20 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 else {. int
75340 73 20 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 s = (int)(x.s +
75350 30 2e 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 0.5);. x.s =
75360 73 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 s;. }. x.tz =
75370 30 3b 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 0;. x.validJD =
75380 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 0;. computeJD(
75390 26 78 29 3b 0a 20 20 74 20 3d 20 78 2e 69 4a 44 &x);. t = x.iJD
753a0 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 /1000 - 21086676
753b0 2a 28 69 36 34 29 31 30 30 30 30 3b 0a 23 69 66 *(i64)10000;.#if
753c0 64 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 def HAVE_LOCALTI
753d0 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 ME_R. {. str
753e0 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 uct tm sLocal;.
753f0 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 26 localtime_r(&
75400 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 20 20 t, &sLocal);.
75410 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.Y = sLocal.tm
75420 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 _year + 1900;.
75430 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.M = sLocal.t
75440 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 m_mon + 1;. y
75450 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d .D = sLocal.tm_m
75460 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 day;. y.h = s
75470 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 Local.tm_hour;.
75480 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e y.m = sLocal.
75490 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 tm_min;. y.s
754a0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b = sLocal.tm_sec;
754b0 0a 20 20 7d 0a 23 65 6c 69 66 20 64 65 66 69 6e . }.#elif defin
754c0 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ed(HAVE_LOCALTIM
754d0 45 5f 53 29 0a 20 20 7b 0a 20 20 20 20 73 74 72 E_S). {. str
754e0 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 uct tm sLocal;.
754f0 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 localtime_s(&
75500 73 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 sLocal, &t);.
75510 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.Y = sLocal.tm
75520 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 _year + 1900;.
75530 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.M = sLocal.t
75540 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 m_mon + 1;. y
75550 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d .D = sLocal.tm_m
75560 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 day;. y.h = s
75570 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 Local.tm_hour;.
75580 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e y.m = sLocal.
75590 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 tm_min;. y.s
755a0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b = sLocal.tm_sec;
755b0 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 . }.#else. {.
755c0 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 struct tm *pT
755d0 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d m;. sqlite3_m
755e0 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 utex_enter(sqlit
755f0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
75600 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
75610 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 _MASTER));. p
75620 54 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 Tm = localtime(&
75630 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 t);. y.Y = pT
75640 6d 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 m->tm_year + 190
75650 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 0;. y.M = pTm
75660 2d 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 ->tm_mon + 1;.
75670 20 20 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f y.D = pTm->tm_
75680 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 mday;. y.h =
75690 70 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 pTm->tm_hour;.
756a0 20 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f y.m = pTm->tm_
756b0 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 min;. y.s = p
756c0 54 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 Tm->tm_sec;.
756d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
756e0 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ave(sqlite3Mutex
756f0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
75700 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
75710 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ));. }.#endif.
75720 20 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b y.validYMD = 1;
75730 0a 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 . y.validHMS =
75740 31 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 1;. y.validJD =
75750 20 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 0;. y.validTZ
75760 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 = 0;. computeJD
75770 28 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 (&y);. return y
75780 2e 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a .iJD - x.iJD;.}.
75790 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
757a0 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 _OMIT_LOCALTIME
757b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 */../*.** Proces
757c0 73 20 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 s a modifier to
757d0 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d a date-time stam
757e0 70 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 p. The modifier
757f0 73 20 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c s are.** as foll
75800 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e ows:.**.** N
75810 4e 4e 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e NN days.** N
75820 4e 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 NN hours.**
75830 4e 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 NNN minutes.**
75840 20 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f NNN.NNNN seco
75850 6e 64 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d nds.** NNN m
75860 6f 6e 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e onths.** NNN
75870 20 79 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 years.** st
75880 61 72 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 art of month.**
75890 20 20 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 start of yea
758a0 72 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f r.** start o
758b0 66 20 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 f week.** st
758c0 61 72 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 art of day.**
758d0 20 20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 weekday N.**
758e0 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 unixepoch.**
758f0 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a localtime.**
75900 20 20 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 utc.**.** R
75910 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 eturn 0 on succe
75920 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 ss and 1 if ther
75930 65 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 e is any kind of
75940 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 error..*/.stati
75950 63 20 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 c int parseModif
75960 69 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ier(const char *
75970 7a 4d 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a zMod, DateTime *
75980 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 p){. int rc = 1
75990 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 ;. int n;. dou
759a0 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a ble r;. char *z
759b0 2c 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 , zBuf[30];. z
759c0 3d 20 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d = zBuf;. for(n=
759d0 30 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 0; n<ArraySize(z
759e0 42 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e Buf)-1 && zMod[n
759f0 5d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e ]; n++){. z[n
75a00 5d 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 ] = (char)sqlite
75a10 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 3UpperToLower[(u
75a20 38 29 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 8)zMod[n]];. }.
75a30 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 z[n] = 0;. sw
75a40 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 itch( z[0] ){.#i
75a50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
75a60 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 T_LOCALTIME.
75a70 63 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 case 'l': {.
75a80 20 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d /* localtim
75a90 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 e. **.
75aa0 20 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 ** Assuming the
75ab0 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 current time va
75ac0 6c 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e lue is UTC (a.k.
75ad0 61 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 a. GMT), shift i
75ae0 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 t to. ** sh
75af0 6f 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 ow local time..
75b00 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
75b10 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 ( strcmp(z, "loc
75b20 61 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 altime")==0 ){.
75b30 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
75b40 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
75b50 69 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 iJD += localtime
75b60 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 Offset(p);.
75b70 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f clearYMD_HMS_
75b80 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 TZ(p);. r
75b90 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 0;. }.
75ba0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
75bb0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 }.#endif. cas
75bc0 65 20 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f e 'u': {. /
75bd0 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e *. ** un
75be0 69 78 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a ixepoch. **
75bf0 0a 20 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 . ** Treat
75c00 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 the current valu
75c10 65 20 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 e of p->iJD as t
75c20 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 he number of.
75c30 20 20 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 ** seconds si
75c40 6e 63 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 nce 1970. Conve
75c50 72 74 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c rt to a real jul
75c60 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a ian day number..
75c70 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
75c80 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e f( strcmp(z, "un
75c90 69 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 ixepoch")==0 &&
75ca0 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 p->validJD ){.
75cb0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 70 p->iJD = p
75cc0 2d 3e 69 4a 44 2f 38 36 34 30 30 20 2b 20 32 31 ->iJD/86400 + 21
75cd0 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 086676*(i64)1000
75ce0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c 0000;. cl
75cf0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 earYMD_HMS_TZ(p)
75d00 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 ;. rc = 0
75d10 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 ;. }.#ifnde
75d20 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
75d30 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 6c CALTIME. el
75d40 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c se if( strcmp(z,
75d50 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20 "utc")==0 ){.
75d60 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e sqlite3_in
75d70 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20 20 t64 c1;.
75d80 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
75d90 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c c1 = local
75da0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 timeOffset(p);.
75db0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d p->iJD -=
75dc0 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 c1;. cle
75dd0 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
75de0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
75df0 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d += c1 - localtim
75e00 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 eOffset(p);.
75e10 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
75e20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
75e30 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
75e40 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 case 'w': {.
75e50 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 /*. **
75e60 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20 weekday N.
75e70 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d **. ** M
75e80 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20 ove the date to
75e90 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e the same time on
75ea0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72 the next occurr
75eb0 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a ence of. **
75ec0 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65 weekday N where
75ed0 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d 0==Sunday, 1==M
75ee0 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f onday, and so fo
75ef0 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20 rth. If the.
75f00 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c ** date is al
75f10 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70 ready on the app
75f20 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79 ropriate weekday
75f30 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f , this is a no-o
75f40 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 p.. */.
75f50 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c if( strncmp(z,
75f60 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d "weekday ", 8)=
75f70 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26 =0 && getValue(&
75f80 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20 z[8],&r)>0.
75f90 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 && (
75fa0 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20 n=(int)r)==r &&
75fb0 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a 20 n>=0 && r<7 ){.
75fc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
75fd0 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 20 nt64 Z;.
75fe0 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 computeYMD_HMS(p
75ff0 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 );. p->va
76000 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 lidTZ = 0;.
76010 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 p->validJD =
76020 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 0;. compu
76030 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 teJD(p);.
76040 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b 20 Z = ((p->iJD +
76050 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 129600000)/86400
76060 30 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20 000) % 7;.
76070 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d 3d if( Z>n ) Z -=
76080 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 7;. p->i
76090 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 36 JD += (n - Z)*86
760a0 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 400000;.
760b0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 clearYMD_HMS_TZ(
760c0 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d p);. rc =
760d0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
760e0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
760f0 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 case 's': {.
76100 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a /*. **
76110 20 20 20 20 73 74 61 72 74 20 6f 66 20 54 54 54 start of TTT
76120 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 TT. **.
76130 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 ** Move the da
76140 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20 te backwards to
76150 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
76160 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 79 the current day
76170 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f ,. ** or mo
76180 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20 nth or year..
76190 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
761a0 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72 strncmp(z, "star
761b0 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20 t of ", 9)!=0 )
761c0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b break;. z +
761d0 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 = 9;. compu
761e0 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 teYMD(p);.
761f0 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b p->validHMS = 1;
76200 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d . p->h = p-
76210 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d >m = 0;. p-
76220 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 >s = 0.0;.
76230 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
76240 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 p->validJD
76250 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 = 0;. if(
76260 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 strcmp(z,"month"
76270 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
76280 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 p->D = 1;.
76290 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 rc = 0;.
762a0 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 }else if( strcmp
762b0 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b (z,"year")==0 ){
762c0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
762d0 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 YMD(p);.
762e0 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 p->M = 1;.
762f0 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 p->D = 1;.
76300 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
76310 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 }else if( strc
76320 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 mp(z,"day")==0 )
76330 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 {. rc = 0
76340 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
76350 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
76360 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63 case '+':. c
76370 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73 ase '-':. cas
76380 65 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20 e '0':. case
76390 27 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32 '1':. case '2
763a0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a ':. case '3':
763b0 0a 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20 . case '4':.
763c0 20 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20 case '5':.
763d0 20 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63 case '6':. c
763e0 61 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73 ase '7':. cas
763f0 65 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20 e '8':. case
76400 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75 '9': {. dou
76410 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b 0a 20 20 ble rRounder;.
76420 20 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75 65 n = getValue
76430 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 61 (z, &r);. a
76440 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 ssert( n>=1 );.
76450 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27 if( z[n]=='
76460 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a :' ){. /*
76470 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 74 A modifier of t
76480 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a he form (+|-)HH:
76490 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 28 MM:SS.FFF adds (
764a0 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74 68 or subtracts) th
764b0 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 65 e. ** spe
764c0 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66 cified number of
764d0 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c hours, minutes,
764e0 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72 seconds, and fr
764f0 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 actional seconds
76500 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 . ** to t
76510 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e he time. The ".
76520 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 74 FFF" may be omit
76530 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e 46 ted. The ":SS.F
76540 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 20 FF" may be.
76550 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20 ** omitted..
76560 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
76570 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 const char *z2
76580 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 61 = z;. Da
76590 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 20 teTime tx;.
765a0 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 sqlite3_int64
765b0 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 66 day;. if
765c0 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 ( !sqlite3Isdigi
765d0 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a 20 t(*z2) ) z2++;.
765e0 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 74 memset(&t
765f0 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78 29 x, 0, sizeof(tx)
76600 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
76610 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 26 arseHhMmSs(z2, &
76620 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 tx) ) break;.
76630 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 computeJD(&
76640 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78 2e tx);. tx.
76650 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 3b iJD -= 43200000;
76660 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20 74 . day = t
76670 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b 0a x.iJD/86400000;.
76680 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d tx.iJD -
76690 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b 0a = day*86400000;.
766a0 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d if( z[0]
766b0 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20 3d =='-' ) tx.iJD =
766c0 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 -tx.iJD;.
766d0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
766e0 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 clearYMD
766f0 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 _HMS_TZ(p);.
76700 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74 78 p->iJD += tx
76710 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72 63 .iJD;. rc
76720 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 = 0;. br
76730 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
76740 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 z += n;.
76750 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
76760 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b sspace(*z) ) z++
76770 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 ;. n = sqli
76780 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a te3Strlen30(z);.
76790 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c if( n>10 |
767a0 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20 | n<3 ) break;.
767b0 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d if( z[n-1]=
767c0 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d ='s' ){ z[n-1] =
767d0 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20 0; n--; }.
767e0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
767f0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
76800 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72 3c rRounder = r<
76810 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35 3b 0 ? -0.5 : +0.5;
76820 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 20 . if( n==3
76830 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 && strcmp(z,"day
76840 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
76850 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
76860 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 te3_int64)(r*864
76870 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 00000.0 + rRound
76880 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 er);. }else
76890 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 if( n==4 && str
768a0 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30 cmp(z,"hour")==0
768b0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
768c0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 JD += (sqlite3_i
768d0 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 nt64)(r*(8640000
768e0 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f 75 0.0/24.0) + rRou
768f0 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
76900 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 se if( n==6 && s
76910 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 22 trcmp(z,"minute"
76920 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
76930 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
76940 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 e3_int64)(r*(864
76950 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 00000.0/(24.0*60
76960 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 .0)) + rRounder)
76970 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
76980 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 ( n==6 && strcmp
76990 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20 (z,"second")==0
769a0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a ){. p->iJ
769b0 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e D += (sqlite3_in
769c0 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 t64)(r*(86400000
769d0 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30 .0/(24.0*60.0*60
769e0 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 .0)) + rRounder)
769f0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
76a00 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d 70 ( n==5 && strcmp
76a10 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 (z,"month")==0 )
76a20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 2c {. int x,
76a30 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 y;. comp
76a40 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 uteYMD_HMS(p);.
76a50 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 28 p->M += (
76a60 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 78 int)r;. x
76a70 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e = p->M>0 ? (p->
76a80 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d M-1)/12 : (p->M-
76a90 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20 12)/12;.
76aa0 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20 p->Y += x;.
76ab0 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b p->M -= x*12;
76ac0 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 . p->vali
76ad0 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dJD = 0;.
76ae0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
76af0 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74 29 y = (int)
76b00 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 r;. if( y
76b10 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=r ){.
76b20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
76b30 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 te3_int64)((r -
76b40 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 30 y)*30.0*86400000
76b50 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a .0 + rRounder);.
76b60 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
76b70 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 }else if( n==4 &
76b80 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 & strcmp(z,"year
76b90 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
76ba0 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72 3b int y = (int)r;
76bb0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
76bc0 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 YMD_HMS(p);.
76bd0 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a 20 p->Y += y;.
76be0 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a p->validJ
76bf0 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 D = 0;. c
76c00 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 omputeJD(p);.
76c10 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b if( y!=r ){
76c20 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a . p->iJ
76c30 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e D += (sqlite3_in
76c40 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36 35 t64)((r - y)*365
76c50 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 .0*86400000.0 +
76c60 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
76c70 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
76c80 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 {. rc = 1
76c90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
76ca0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 clearYMD_HMS_TZ(
76cb0 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b p);. break;
76cc0 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
76cd0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61 lt: {. brea
76ce0 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 k;. }. }. r
76cf0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
76d00 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 20 ** Process time
76d10 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
76d20 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 ts. argv[0] is
76d30 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d a date-time stam
76d40 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 6e p..** argv[1] an
76d50 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 d following are
76d60 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 73 modifiers. Pars
76d70 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 77 e them all and w
76d80 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75 rite.** the resu
76d90 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f 20 lting time into
76da0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 the DateTime str
76db0 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 72 ucture p. Retur
76dc0 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 n 0.** on succes
76dd0 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 s and 1 if there
76de0 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 2e are any errors.
76df0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
76e00 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 74 are zero paramet
76e10 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 67 ers (if even arg
76e20 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e 65 v[0] is undefine
76e30 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 6d d).** then assum
76e40 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 e a default valu
76e50 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 61 e of "now" for a
76e60 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69 rgv[0]..*/.stati
76e70 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 20 c int isDate(.
76e80 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
76e90 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 *context, . int
76ea0 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65 argc, . sqlite
76eb0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c 20 3_value **argv,
76ec0 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29 . DateTime *p.)
76ed0 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e {. int i;. con
76ee0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
76ef0 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70 65 *z;. int eType
76f00 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c ;. memset(p, 0,
76f10 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 sizeof(*p));.
76f20 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a 20 if( argc==0 ){.
76f30 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f setDateTimeTo
76f40 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c Current(context,
76f50 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 p);. }else if(
76f60 20 28 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 (eType = sqlite
76f70 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
76f80 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 45 5f 46 v[0]))==SQLITE_F
76f90 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 20 20 20 LOAT.
76fa0 20 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 || eType
76fb0 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 ==SQLITE_INTEGER
76fc0 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d ){. p->iJD =
76fd0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
76fe0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 (sqlite3_value_d
76ff0 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 2a 38 ouble(argv[0])*8
77000 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 6400000.0 + 0.5)
77010 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 ;. p->validJD
77020 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
77030 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 z = sqlite3_v
77040 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
77050 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 7c ]);. if( !z |
77060 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d | parseDateOrTim
77070 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 e(context, (char
77080 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 *)z, p) ){.
77090 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
770a0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 . }. for(i=1;
770b0 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 i<argc; i++){.
770c0 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69 74 if( (z = sqlit
770d0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
770e0 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70 61 gv[i]))==0 || pa
770f0 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68 61 rseModifier((cha
77100 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 r*)z, p) ){.
77110 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
77120 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
77130 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ;.}.../*.** The
77140 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
77150 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 es implement the
77160 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61 6e various date an
77170 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 73 d time functions
77180 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a .** of SQLite..*
77190 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c 69 /../*.** juli
771a0 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 4e anday( TIMESTRIN
771b0 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e G, MOD, MOD, ...
771c0 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ).**.** Return t
771d0 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 he julian day nu
771e0 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74 65 mber of the date
771f0 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
77200 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 73 e arguments.*/.s
77210 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69 61 tatic void julia
77220 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c 69 ndayFunc(. sqli
77230 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
77240 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
77250 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
77260 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 e **argv.){. Da
77270 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 teTime x;. if(
77280 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 isDate(context,
77290 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d argc, argv, &x)=
772a0 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74 =0 ){. comput
772b0 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c eJD(&x);. sql
772c0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 ite3_result_doub
772d0 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 2e 69 4a le(context, x.iJ
772e0 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 D/86400000.0);.
772f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64 }.}../*.** d
77300 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54 52 atetime( TIMESTR
77310 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e ING, MOD, MOD, .
77320 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ..).**.** Return
77330 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d YYYY-MM-DD HH:M
77340 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 M:SS.*/.static v
77350 6f 69 64 20 64 61 74 65 74 69 6d 65 46 75 6e 63 oid datetimeFunc
77360 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
77370 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
77380 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
77390 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
773a0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
773b0 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 ;. if( isDate(c
773c0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 ontext, argc, ar
773d0 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 gv, &x)==0 ){.
773e0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
773f0 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 ;. computeYMD
77400 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71 _HMS(&x);. sq
77410 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
77420 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
77430 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 f, "%04d-%02d-%0
77440 32 64 20 25 30 32 64 3a 25 30 32 64 3a 25 30 32 2d %02d:%02d:%02
77450 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 d",.
77460 20 20 20 20 20 20 20 20 20 78 2e 59 2c 20 78 2e x.Y, x.
77470 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e 6d M, x.D, x.h, x.m
77480 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 3b 0a 20 , (int)(x.s));.
77490 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
774a0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
774b0 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 zBuf, -1, SQLITE
774c0 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d _TRANSIENT);. }
774d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 74 69 6d .}../*.** tim
774e0 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d e( TIMESTRING, M
774f0 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a OD, MOD, ...).**
77500 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 3a 4d 4d .** Return HH:MM
77510 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f :SS.*/.static vo
77520 69 64 20 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 id timeFunc(. s
77530 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
77540 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
77550 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
77560 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
77570 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 DateTime x;. i
77580 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 f( isDate(contex
77590 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 t, argc, argv, &
775a0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 x)==0 ){. cha
775b0 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 r zBuf[100];.
775c0 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29 3b computeHMS(&x);
775d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
775e0 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 rintf(sizeof(zBu
775f0 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64 3a f), zBuf, "%02d:
77600 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68 2c %02d:%02d", x.h,
77610 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29 3b x.m, (int)x.s);
77620 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
77630 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
77640 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 , zBuf, -1, SQLI
77650 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
77660 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64 }.}../*.** d
77670 61 74 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c ate( TIMESTRING,
77680 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a MOD, MOD, ...).
77690 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 **.** Return YYY
776a0 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 61 74 69 Y-MM-DD.*/.stati
776b0 63 20 76 6f 69 64 20 64 61 74 65 46 75 6e 63 28 c void dateFunc(
776c0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
776d0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
776e0 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
776f0 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
77700 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b ){. DateTime x;
77710 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f . if( isDate(co
77720 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 ntext, argc, arg
77730 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 v, &x)==0 ){.
77740 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b char zBuf[100];
77750 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 . computeYMD(
77760 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 &x);. sqlite3
77770 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
77780 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 (zBuf), zBuf, "%
77790 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c 20 04d-%02d-%02d",
777a0 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b 0a x.Y, x.M, x.D);.
777b0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
777c0 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
777d0 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 zBuf, -1, SQLIT
777e0 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 E_TRANSIENT);.
777f0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73 74 }.}../*.** st
77800 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c 20 rftime( FORMAT,
77810 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c TIMESTRING, MOD,
77820 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a MOD, ...).**.**
77830 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 Return a string
77840 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46 4f described by FO
77850 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69 6f RMAT. Conversio
77860 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a ns as follows:.*
77870 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20 6f *.** %d day o
77880 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66 20 f month.** %f
77890 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 ** fractional s
778a0 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a 2a econds SS.SSS.*
778b0 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30 2d * %H hour 00-
778c0 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79 20 24.** %j day
778d0 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36 0a of year 000-366.
778e0 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c 69 ** %J ** Juli
778f0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a 2a an day number.**
77900 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31 2d %m month 01-
77910 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e 75 12.** %M minu
77920 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 73 te 00-59.** %s
77930 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 seconds since
77940 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20 20 1970-01-01.**
77950 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d 35 %S seconds 00-5
77960 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20 6f 9.** %w day o
77970 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e 64 f week 0-6 sund
77980 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20 77 ay==0.** %W w
77990 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d 35 eek of year 00-5
779a0 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72 20 3.** %Y year
779b0 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20 25 0000-9999.** %
779c0 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20 76 % %.*/.static v
779d0 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e 63 oid strftimeFunc
779e0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
779f0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
77a00 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
77a10 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
77a20 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
77a30 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 73 69 7a ;. u64 n;. siz
77a40 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 61 72 20 e_t i,j;. char
77a50 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 *z;. sqlite3 *d
77a60 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 b;. const char
77a70 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 63 *zFmt = (const c
77a80 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
77a90 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
77aa0 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 ;. char zBuf[10
77ab0 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 3d 0];. if( zFmt==
77ac0 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e 74 0 || isDate(cont
77ad0 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72 67 ext, argc-1, arg
77ae0 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75 72 v+1, &x) ) retur
77af0 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 n;. db = sqlite
77b00 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
77b10 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
77b20 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46 for(i=0, n=1; zF
77b30 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 mt[i]; i++, n++)
77b40 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 {. if( zFmt[i
77b50 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 ]=='%' ){.
77b60 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31 switch( zFmt[i+1
77b70 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 ] ){. cas
77b80 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 63 e 'd':. c
77b90 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 20 ase 'H':.
77ba0 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 20 case 'm':.
77bb0 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 20 case 'M':.
77bc0 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a 20 case 'S':.
77bd0 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 3a case 'W':
77be0 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a . n++;.
77bf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c /* fal
77c00 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 l thru */.
77c10 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 20 case 'w':.
77c20 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 20 case '%':.
77c30 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
77c40 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a case 'f':
77c50 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 . n +=
77c60 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 8;. bre
77c70 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
77c80 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'j':.
77c90 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 n += 3;.
77ca0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
77cb0 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 20 case 'Y':.
77cc0 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 n += 8;.
77cd0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
77ce0 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 0a case 's':.
77cf0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27 case 'J'
77d00 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d :. n +=
77d10 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 50;. b
77d20 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 reak;. de
77d30 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 20 fault:.
77d40 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 52 return; /* ERR
77d50 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e 55 OR. return a NU
77d60 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 LL */. }.
77d70 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20 i++;. }.
77d80 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e }. testcase( n
77d90 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31 ==sizeof(zBuf)-1
77da0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
77db0 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 n==sizeof(zBuf)
77dc0 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e );. testcase( n
77dd0 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 ==(u64)db->aLimi
77de0 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
77df0 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 74 65 ENGTH]+1 );. te
77e00 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29 stcase( n==(u64)
77e10 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
77e20 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
77e30 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f );. if( n<sizeo
77e40 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 7a f(zBuf) ){. z
77e50 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 65 = zBuf;. }else
77e60 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d 3e if( n>(u64)db->
77e70 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
77e80 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
77e90 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
77ea0 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 t_error_toobig(c
77eb0 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74 ontext);. ret
77ec0 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 urn;. }else{.
77ed0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 4d z = sqlite3DbM
77ee0 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e allocRaw(db, (in
77ef0 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d t)n);. if( z=
77f00 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
77f10 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
77f20 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b _nomem(context);
77f30 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
77f40 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75 }. }. compu
77f50 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70 teJD(&x);. comp
77f60 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a uteYMD_HMS(&x);.
77f70 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d for(i=j=0; zFm
77f80 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 t[i]; i++){.
77f90 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 if( zFmt[i]!='%'
77fa0 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d ){. z[j++]
77fb0 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20 = zFmt[i];.
77fc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b }else{. i++
77fd0 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 ;. switch(
77fe0 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 zFmt[i] ){.
77ff0 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73 71 case 'd': sq
78000 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
78010 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 , &z[j],"%02d",x
78020 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b .D); j+=2; break
78030 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
78040 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 f': {.
78050 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a double s = x.s;.
78060 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 3e if( s>
78070 35 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e 59.999 ) s = 59.
78080 39 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73 999;. s
78090 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
780a0 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66 7, &z[j],"%06.3f
780b0 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20 ", s);.
780c0 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 j += sqlite3Str
780d0 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 len30(&z[j]);.
780e0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
780f0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
78100 20 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c 69 case 'H': sqli
78110 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 te3_snprintf(3,
78120 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 68 &z[j],"%02d",x.h
78130 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a ); j+=2; break;.
78140 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 case 'W'
78150 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 2a : /* Fall thru *
78160 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 /. case '
78170 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 j': {.
78180 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20 20 int nDay;
78190 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
781a0 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 31 73 of days since 1s
781b0 74 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a 2f t day of year */
781c0 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 65 54 . DateT
781d0 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20 20 ime y = x;.
781e0 20 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d y.validJD =
781f0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 0;. y.
78200 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 M = 1;.
78210 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 y.D = 1;.
78220 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 computeJD(&y
78230 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 61 );. nDa
78240 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69 4a 44 y = (int)((x.iJD
78250 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29 -y.iJD+43200000)
78260 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 /86400000);.
78270 20 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 if( zFmt[i
78280 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 20 ]=='W' ){.
78290 20 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 20 int wd;
782a0 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54 /* 0=Monday, 1=T
782b0 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75 uesday, ... 6=Su
782c0 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 nday */.
782d0 20 20 20 20 77 64 20 3d 20 28 69 6e 74 29 28 28 wd = (int)((
782e0 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29 (x.iJD+43200000)
782f0 2f 38 36 34 30 30 30 30 30 29 25 37 29 3b 0a 20 /86400000)%7);.
78300 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
78310 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 e3_snprintf(3, &
78320 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44 61 z[j],"%02d",(nDa
78330 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20 20 y+7-wd)/7);.
78340 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b 0a j += 2;.
78350 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
78360 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
78370 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 34 2c ite3_snprintf(4,
78380 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e 44 &z[j],"%03d",nD
78390 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 ay+1);.
783a0 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 j += 3;.
783b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
783c0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
783d0 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 }. case '
783e0 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 J': {.
783f0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
78400 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 36 (20, &z[j],"%.16
78410 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 g",x.iJD/8640000
78420 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 0.0);.
78430 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e j+=sqlite3Strlen
78440 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 30(&z[j]);.
78450 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
78460 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 }. ca
78470 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 33 se 'm': sqlite3
78480 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b _snprintf(3, &z[
78490 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b 20 j],"%02d",x.M);
784a0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 j+=2; break;.
784b0 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 20 case 'M':
784c0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
784d0 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 (3, &z[j],"%02d"
784e0 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 ,x.m); j+=2; bre
784f0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
78500 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 's': {.
78510 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
78520 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 64 22 tf(30,&z[j],"%d"
78530 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
78540 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e (in
78550 74 29 28 78 2e 69 4a 44 2f 31 30 30 30 2e 30 20 t)(x.iJD/1000.0
78560 2d 20 32 31 30 38 36 36 37 36 30 30 30 30 2e 30 - 210866760000.0
78570 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 ));. j
78580 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
78590 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 30(&z[j]);.
785a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
785b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 }. ca
785c0 73 65 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33 se 'S': sqlite3
785d0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a _snprintf(3,&z[j
785e0 5d 2c 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e ],"%02d",(int)x.
785f0 73 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b s); j+=2; break;
78600 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 77 . case 'w
78610 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 7a ': {. z
78620 5b 6a 2b 2b 5d 20 3d 20 28 63 68 61 72 29 28 28 [j++] = (char)((
78630 28 78 2e 69 4a 44 2b 31 32 39 36 30 30 30 30 30 (x.iJD+129600000
78640 29 2f 38 36 34 30 30 30 30 30 29 20 25 20 37 29 )/86400000) % 7)
78650 20 2b 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 + '0';.
78660 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
78670 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 }. case
78680 27 59 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 'Y': {.
78690 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
786a0 66 28 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 f(5,&z[j],"%04d"
786b0 2c 78 2e 59 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 ,x.Y); j+=sqlite
786c0 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3Strlen30(&z[j])
786d0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
786e0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
786f0 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 default:
78700 7a 5b 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72 z[j++] = '%'; br
78710 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
78720 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 }. }. z[j] =
78730 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 0;. sqlite3_res
78740 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
78750 2c 20 7a 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20 , z, -1,.
78760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
78770 3d 3d 7a 42 75 66 20 3f 20 53 51 4c 49 54 45 5f ==zBuf ? SQLITE_
78780 54 52 41 4e 53 49 45 4e 54 20 3a 20 53 51 4c 49 TRANSIENT : SQLI
78790 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a TE_DYNAMIC);.}..
787a0 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 /*.** current_ti
787b0 6d 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 me().**.** This
787c0 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
787d0 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 the same value
787e0 61 73 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a as time('now')..
787f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
78800 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 timeFunc(. sqli
78810 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
78820 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 text,. int NotU
78830 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 sed,. sqlite3_v
78840 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a alue **NotUsed2.
78850 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
78860 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 METER2(NotUsed,
78870 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 74 69 6d NotUsed2);. tim
78880 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 eFunc(context, 0
78890 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 , 0);.}../*.** c
788a0 75 72 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a 2a urrent_date().**
788b0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
788c0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 n returns the sa
788d0 6d 65 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 me value as date
788e0 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 ('now')..*/.stat
788f0 69 63 20 76 6f 69 64 20 63 64 61 74 65 46 75 6e ic void cdateFun
78900 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
78910 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
78920 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 int NotUsed,.
78930 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
78940 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e NotUsed2.){. UN
78950 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 USED_PARAMETER2(
78960 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 NotUsed, NotUsed
78970 32 29 3b 0a 20 20 64 61 74 65 46 75 6e 63 28 63 2);. dateFunc(c
78980 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d ontext, 0, 0);.}
78990 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f ../*.** current_
789a0 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a timestamp().**.*
789b0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
789c0 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 returns the same
789d0 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 74 69 value as dateti
789e0 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 me('now')..*/.st
789f0 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73 atic void ctimes
78a00 74 61 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69 tampFunc(. sqli
78a10 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
78a20 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 text,. int NotU
78a30 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 sed,. sqlite3_v
78a40 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a alue **NotUsed2.
78a50 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
78a60 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 METER2(NotUsed,
78a70 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 NotUsed2);. dat
78a80 65 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 etimeFunc(contex
78a90 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 t, 0, 0);.}.#end
78aa0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 if /* !defined(S
78ab0 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 QLITE_OMIT_DATET
78ac0 49 4d 45 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 IME_FUNCS) */..#
78ad0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
78ae0 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 T_DATETIME_FUNCS
78af0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 ./*.** If the li
78b00 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 brary is compile
78b10 64 20 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 d to omit the fu
78b20 6c 6c 2d 73 63 61 6c 65 20 64 61 74 65 20 61 6e ll-scale date an
78b30 64 20 74 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 d time.** handli
78b40 6e 67 20 28 74 6f 20 67 65 74 20 61 20 73 6d 61 ng (to get a sma
78b50 6c 6c 65 72 20 62 69 6e 61 72 79 29 2c 20 74 68 ller binary), th
78b60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 e following mini
78b70 6d 61 6c 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f mal version.** o
78b80 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 f the functions
78b90 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 current_time(),
78ba0 63 75 72 72 65 6e 74 5f 64 61 74 65 28 29 20 61 current_date() a
78bb0 6e 64 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 nd current_times
78bc0 74 61 6d 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e tamp().** are in
78bd0 63 6c 75 64 65 64 20 69 6e 73 74 65 61 64 2e 20 cluded instead.
78be0 54 68 69 73 20 69 73 20 74 6f 20 73 75 70 70 6f This is to suppo
78bf0 72 74 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 rt column declar
78c00 61 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 ations that.** i
78c10 6e 63 6c 75 64 65 20 22 44 45 46 41 55 4c 54 20 nclude "DEFAULT
78c20 43 55 52 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 CURRENT_TIME" et
78c30 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 c..**.** This fu
78c40 6e 63 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 nction uses the
78c50 43 2d 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 C-library functi
78c60 6f 6e 73 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 ons time(), gmti
78c70 6d 65 28 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 me().** and strf
78c80 74 69 6d 65 28 29 2e 20 54 68 65 20 66 6f 72 6d time(). The form
78c90 61 74 20 73 74 72 69 6e 67 20 74 6f 20 70 61 73 at string to pas
78ca0 73 20 74 6f 20 73 74 72 66 74 69 6d 65 28 29 20 s to strftime()
78cb0 69 73 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 is supplied.** a
78cc0 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 s the user-data
78cd0 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e for the function
78ce0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
78cf0 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 currentTimeFunc
78d00 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
78d10 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
78d20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
78d30 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
78d40 0a 29 7b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a .){. time_t t;.
78d50 20 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 char *zFormat
78d60 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
78d70 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 3_user_data(cont
78d80 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 ext);. sqlite3
78d90 2a 64 62 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 *db;. double rT
78da0 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 ;. char zBuf[20
78db0 5d 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 ];.. UNUSED_PAR
78dc0 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 AMETER(argc);.
78dd0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
78de0 28 61 72 67 76 29 3b 0a 0a 20 20 64 62 20 3d 20 (argv);.. db =
78df0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f sqlite3_context_
78e00 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 db_handle(contex
78e10 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 t);. sqlite3OsC
78e20 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 urrentTime(db->p
78e30 56 66 73 2c 20 26 72 54 29 3b 0a 23 69 66 6e 64 Vfs, &rT);.#ifnd
78e40 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
78e50 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 LOATING_POINT.
78e60 74 20 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20 t = 86400.0*(rT
78e70 2d 20 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30 - 2440587.5) + 0
78e80 2e 35 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 77 .5;.#else. /* w
78e90 69 74 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 ithout floating
78ea0 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 20 72 point support, r
78eb0 54 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a T will have. **
78ec0 20 61 6c 72 65 61 64 79 20 6c 6f 73 74 20 66 72 already lost fr
78ed0 61 63 74 69 6f 6e 61 6c 20 64 61 79 20 70 72 65 actional day pre
78ee0 63 69 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 74 cision.. */. t
78ef0 20 3d 20 38 36 34 30 30 20 2a 20 28 72 54 20 2d = 86400 * (rT -
78f00 20 32 34 34 30 35 38 37 29 20 2d 20 34 33 32 30 2440587) - 4320
78f10 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 0;.#endif.#ifdef
78f20 20 48 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 HAVE_GMTIME_R.
78f30 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d {. struct tm
78f40 20 73 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d sNow;. gmtim
78f50 65 5f 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a e_r(&t, &sNow);.
78f60 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 strftime(zBu
78f70 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 f, 20, zFormat,
78f80 26 73 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 &sNow);. }.#els
78f90 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 e. {. struct
78fa0 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 tm *pTm;. sq
78fb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
78fc0 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c r(sqlite3MutexAl
78fd0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
78fe0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 _STATIC_MASTER))
78ff0 3b 0a 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 ;. pTm = gmti
79000 6d 65 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 me(&t);. strf
79010 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a time(zBuf, 20, z
79020 46 6f 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 Format, pTm);.
79030 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
79040 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 leave(sqlite3Mut
79050 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
79060 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
79070 45 52 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 ER));. }.#endif
79080 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 .. sqlite3_resu
79090 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
790a0 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 zBuf, -1, SQLIT
790b0 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a E_TRANSIENT);.}.
790c0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
790d0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 is function regi
790e0 73 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 stered all of th
790f0 65 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 e above C functi
79100 6f 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 ons as SQL.** fu
79110 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 nctions. This s
79120 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c hould be the onl
79130 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 y routine in thi
79140 73 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 s file with.** e
79150 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e xternal linkage.
79160 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
79170 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
79180 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 egisterDateTimeF
79190 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a unctions(void){.
791a0 20 20 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f static SQLITE_
791b0 57 53 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 WSD FuncDef aDat
791c0 65 54 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b eTimeFuncs[] = {
791d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
791e0 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 OMIT_DATETIME_FU
791f0 4e 43 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e NCS. FUNCTION
79200 28 6a 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 (julianday,
79210 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c -1, 0, 0, jul
79220 69 61 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 iandayFunc ),.
79230 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c FUNCTION(date,
79240 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c -1,
79250 20 30 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 0, 0, dateFunc
79260 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
79270 54 49 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 TION(time,
79280 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c -1, 0, 0,
79290 20 74 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 timeFunc )
792a0 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 ,. FUNCTION(d
792b0 61 74 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 atetime,
792c0 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 -1, 0, 0, datet
792d0 69 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 imeFunc ),.
792e0 46 55 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d FUNCTION(strftim
792f0 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 e, -1, 0
79300 2c 20 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e , 0, strftimeFun
79310 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 c ),. FUNCTI
79320 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c ON(current_time,
79330 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 0, 0, 0, c
79340 74 69 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a timeFunc ),.
79350 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 FUNCTION(cur
79360 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 rent_timestamp,
79370 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 0, 0, 0, ctimest
79380 61 6d 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 ampFunc),. FU
79390 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 NCTION(current_d
793a0 61 74 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 ate, 0, 0,
793b0 30 2c 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 0, cdateFunc
793c0 20 29 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 ),.#else. ST
793d0 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 R_FUNCTION(curre
793e0 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c nt_time, 0,
793f0 20 22 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 "%H:%M:%S",
79400 20 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 0, current
79410 54 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 TimeFunc),. S
79420 54 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 TR_FUNCTION(curr
79430 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 ent_timestamp, 0
79440 2c 20 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 , "%Y-%m-%d",
79450 20 20 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 0, curren
79460 74 54 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 tTimeFunc),.
79470 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 STR_FUNCTION(cur
79480 72 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 rent_date,
79490 30 2c 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 0, "%Y-%m-%d %H:
794a0 25 4d 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 %M:%S", 0, curre
794b0 6e 74 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e ntTimeFunc),.#en
794c0 64 69 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 dif. };. int i
794d0 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 ;. FuncDefHash
794e0 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c *pHash = &GLOBAL
794f0 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 (FuncDefHash, sq
79500 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 lite3GlobalFunct
79510 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 ions);. FuncDef
79520 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 *aFunc = (FuncD
79530 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 ef*)&GLOBAL(Func
79540 44 65 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 Def, aDateTimeFu
79550 6e 63 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 ncs);.. for(i=0
79560 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 ; i<ArraySize(aD
79570 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 ateTimeFuncs); i
79580 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
79590 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 FuncDefInsert(pH
795a0 61 73 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b ash, &aFunc[i]);
795b0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a . }.}../*******
795c0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 ******* End of d
795d0 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ate.c **********
795e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
795f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79600 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
79610 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
79620 6c 65 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a le os.c ********
79630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79650 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
79660 30 35 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 05 November 29.*
79670 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
79680 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
79690 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
796a0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
796b0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
796c0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
796d0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
796e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
796f0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
79700 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
79710 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
79720 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
79730 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
79740 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
79750 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
79760 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
79770 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
79780 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
79790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
797a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
797b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
797c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
797d0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
797e0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 le contains OS i
797f0 6e 74 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 nterface code th
79800 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 at is common to
79810 61 6c 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 all.** architect
79820 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a ures..**.** $Id:
79830 20 6f 73 2e 63 2c 76 20 31 2e 31 32 36 20 32 30 os.c,v 1.126 20
79840 30 39 2f 30 33 2f 32 35 20 31 34 3a 32 34 3a 34 09/03/25 14:24:4
79850 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 2 drh Exp $.*/.#
79860 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f define _SQLITE_O
79870 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 S_C_ 1.#undef _S
79880 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a QLITE_OS_C_../*.
79890 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 53 ** The default S
798a0 51 4c 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66 QLite sqlite3_vf
798b0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
798c0 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 s do not allocat
798d0 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 e.** memory (act
798e0 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 ually, os_unix.c
798f0 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 allocates a sma
79900 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d ll amount of mem
79910 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 ory.** from with
79920 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 in OsOpen()), bu
79930 74 20 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 t some third-par
79940 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ty implementatio
79950 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 ns may..** So we
79960 20 74 65 73 74 20 74 68 65 20 65 66 66 65 63 74 test the effect
79970 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 s of a malloc()
79980 66 61 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 failing and the
79990 73 71 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a sqlite3OsXXX().*
799a0 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 * function retur
799b0 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 ning SQLITE_IOER
799c0 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 R_NOMEM using th
799d0 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 e DO_OS_MALLOC_T
799e0 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a EST macro..**.**
799f0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 The following f
79a00 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 unctions are ins
79a10 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 trumented for ma
79a20 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a lloc() failure .
79a30 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a ** testing:.**.*
79a40 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f * sqlite3OsO
79a50 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c pen().** sql
79a60 69 74 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 ite3OsRead().**
79a70 20 20 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69 sqlite3OsWri
79a80 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 te().** sqli
79a90 74 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 te3OsSync().**
79aa0 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b sqlite3OsLock
79ab0 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 ().**.*/.#if def
79ac0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
79ad0 29 20 26 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f ) && (SQLITE_OS_
79ae0 57 49 4e 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e WIN==0). #defin
79af0 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 e DO_OS_MALLOC_T
79b00 45 53 54 20 69 66 20 28 31 29 20 7b 20 20 20 20 EST if (1) {
79b10 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 76 6f \. vo
79b20 69 64 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 id *pTstAlloc =
79b30 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 sqlite3Malloc(10
79b40 29 3b 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 ); \. i
79b50 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 f (!pTstAlloc) r
79b60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
79b70 52 52 5f 4e 4f 4d 45 4d 3b 20 20 5c 0a 20 20 20 RR_NOMEM; \.
79b80 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 sqlite3_free(pT
79b90 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 stAlloc);
79ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
79bb0 20 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 }.#else. #defi
79bc0 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f ne DO_OS_MALLOC_
79bd0 54 45 53 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a TEST.#endif../*.
79be0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
79bf0 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f routines are co
79c00 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 nvenience wrappe
79c10 72 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 rs around method
79c20 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 s.** of the sqli
79c30 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e te3_file object.
79c40 20 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 This is mostly
79c50 20 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 20 just syntactic
79c60 73 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 sugar. All.** of
79c70 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63 this would be c
79c80 6f 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 ompletely automa
79c90 74 69 63 20 69 66 20 53 51 4c 69 74 65 20 77 65 tic if SQLite we
79ca0 72 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a re coded using.*
79cb0 2a 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 * C++ instead of
79cc0 20 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f plain old C..*/
79cd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
79ce0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f int sqlite3OsClo
79cf0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
79d00 2a 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 *pId){. int rc
79d10 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
79d20 66 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 f( pId->pMethods
79d30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64 ){. rc = pId
79d40 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f ->pMethods->xClo
79d50 73 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64 se(pId);. pId
79d60 2d 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a ->pMethods = 0;.
79d70 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
79d80 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
79d90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 E int sqlite3OsR
79da0 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ead(sqlite3_file
79db0 20 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 *id, void *pBuf
79dc0 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f , int amt, i64 o
79dd0 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f ffset){. DO_OS_
79de0 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 MALLOC_TEST;. r
79df0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
79e00 64 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42 ds->xRead(id, pB
79e10 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 uf, amt, offset)
79e20 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
79e30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
79e40 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 Write(sqlite3_fi
79e50 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f le *id, const vo
79e60 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d id *pBuf, int am
79e70 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a t, i64 offset){.
79e80 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
79e90 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 EST;. return id
79ea0 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 ->pMethods->xWri
79eb0 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 te(id, pBuf, amt
79ec0 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c , offset);.}.SQL
79ed0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
79ee0 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
79ef0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
79f00 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 id, i64 size){.
79f10 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
79f20 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 hods->xTruncate(
79f30 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c id, size);.}.SQL
79f40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
79f50 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 sqlite3OsSync(sq
79f60 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
79f70 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f int flags){. DO
79f80 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b _OS_MALLOC_TEST;
79f90 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
79fa0 65 74 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 ethods->xSync(id
79fb0 2c 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 , flags);.}.SQLI
79fc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
79fd0 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
79fe0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
79ff0 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a d, i64 *pSize){.
7a000 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
7a010 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 EST;. return id
7a020 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c ->pMethods->xFil
7a030 65 53 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 eSize(id, pSize)
7a040 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
7a050 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
7a060 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
7a070 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 e *id, int lockT
7a080 79 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 ype){. DO_OS_MA
7a090 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 LLOC_TEST;. ret
7a0a0 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 urn id->pMethods
7a0b0 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b ->xLock(id, lock
7a0c0 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f Type);.}.SQLITE_
7a0d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7a0e0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 te3OsUnlock(sqli
7a0f0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
7a100 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 t lockType){. r
7a110 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
7a120 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 ds->xUnlock(id,
7a130 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c lockType);.}.SQL
7a140 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7a150 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 sqlite3OsCheckRe
7a160 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
7a170 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
7a180 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f *pResOut){. DO
7a190 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b _OS_MALLOC_TEST;
7a1a0 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
7a1b0 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 ethods->xCheckRe
7a1c0 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 servedLock(id, p
7a1d0 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 ResOut);.}.SQLIT
7a1e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7a1f0 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 lite3OsFileContr
7a200 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ol(sqlite3_file
7a210 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 *id, int op, voi
7a220 64 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75 d *pArg){. retu
7a230 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
7a240 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 >xFileControl(id
7a250 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 , op, pArg);.}.S
7a260 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
7a270 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f t sqlite3OsSecto
7a280 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 rSize(sqlite3_fi
7a290 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 le *id){. int (
7a2a0 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 *xSectorSize)(sq
7a2b0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 lite3_file*) = i
7a2c0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 d->pMethods->xSe
7a2d0 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 ctorSize;. retu
7a2e0 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20 rn (xSectorSize
7a2f0 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64 ? xSectorSize(id
7a300 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ) : SQLITE_DEFAU
7a310 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b LT_SECTOR_SIZE);
7a320 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
7a330 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 E int sqlite3OsD
7a340 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
7a350 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c tics(sqlite3_fil
7a360 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e e *id){. return
7a370 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
7a380 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
7a390 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a stics(id);.}../*
7a3a0 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f .** The next gro
7a3b0 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 up of routines a
7a3c0 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 re convenience w
7a3d0 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 rappers around t
7a3e0 68 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 he.** VFS method
7a3f0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
7a400 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
7a410 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 OsOpen(. sqlite
7a420 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 3_vfs *pVfs, .
7a430 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
7a440 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 h, . sqlite3_fi
7a450 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e le *pFile, . in
7a460 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 t flags, . int
7a470 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 *pFlagsOut.){.
7a480 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f int rc;. DO_OS_
7a490 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 MALLOC_TEST;. r
7a4a0 63 20 3d 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 c = pVfs->xOpen(
7a4b0 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 pVfs, zPath, pFi
7a4c0 6c 65 2c 20 66 6c 61 67 73 2c 20 70 46 6c 61 67 le, flags, pFlag
7a4d0 73 4f 75 74 29 3b 0a 20 20 61 73 73 65 72 74 28 sOut);. assert(
7a4e0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc==SQLITE_OK |
7a4f0 7c 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 | pFile->pMethod
7a500 73 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e s==0 );. return
7a510 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 rc;.}.SQLITE_PR
7a520 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7a530 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 3OsDelete(sqlite
7a540 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 3_vfs *pVfs, con
7a550 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
7a560 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 int dirSync){.
7a570 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 return pVfs->xDe
7a580 6c 65 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68 lete(pVfs, zPath
7a590 2c 20 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 , dirSync);.}.SQ
7a5a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
7a5b0 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
7a5c0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
7a5d0 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 *pVfs, . const
7a5e0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 char *zPath, .
7a5f0 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e int flags, . in
7a600 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 t *pResOut.){.
7a610 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
7a620 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 T;. return pVfs
7a630 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 2c 20 ->xAccess(pVfs,
7a640 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 52 zPath, flags, pR
7a650 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 esOut);.}.SQLITE
7a660 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
7a670 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 ite3OsFullPathna
7a680 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 me(. sqlite3_vf
7a690 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 s *pVfs, . cons
7a6a0 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a t char *zPath, .
7a6b0 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20 int nPathOut,
7a6c0 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f 75 . char *zPathOu
7a6d0 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 t.){. return pV
7a6e0 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d fs->xFullPathnam
7a6f0 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e e(pVfs, zPath, n
7a700 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 PathOut, zPathOu
7a710 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 t);.}.#ifndef SQ
7a720 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
7a730 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f XTENSION.SQLITE_
7a740 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
7a750 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 lite3OsDlOpen(sq
7a760 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
7a770 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
7a780 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 th){. return pV
7a790 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 fs->xDlOpen(pVfs
7a7a0 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49 , zPath);.}.SQLI
7a7b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
7a7c0 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 sqlite3OsDlError
7a7d0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
7a7e0 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 fs, int nByte, c
7a7f0 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 har *zBufOut){.
7a800 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 pVfs->xDlError(
7a810 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 pVfs, nByte, zBu
7a820 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f fOut);.}.SQLITE_
7a830 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 PRIVATE void (*s
7a840 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 qlite3OsDlSym(sq
7a850 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
7a860 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f void *pHdle, co
7a870 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29 nst char *zSym))
7a880 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e (void){. return
7a890 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56 pVfs->xDlSym(pV
7a8a0 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29 fs, pHdle, zSym)
7a8b0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
7a8c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f TE void sqlite3O
7a8d0 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 sDlClose(sqlite3
7a8e0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 _vfs *pVfs, void
7a8f0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56 *pHandle){. pV
7a900 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66 fs->xDlClose(pVf
7a910 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 s, pHandle);.}.#
7a920 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
7a930 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
7a940 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ION */.SQLITE_PR
7a950 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7a960 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 3OsRandomness(sq
7a970 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
7a980 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 int nByte, char
7a990 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 *zBufOut){. re
7a9a0 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 turn pVfs->xRand
7a9b0 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79 omness(pVfs, nBy
7a9c0 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a te, zBufOut);.}.
7a9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
7a9e0 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 nt sqlite3OsSlee
7a9f0 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 p(sqlite3_vfs *p
7aa00 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 Vfs, int nMicro)
7aa10 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
7aa20 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d >xSleep(pVfs, nM
7aa30 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f icro);.}.SQLITE_
7aa40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7aa50 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
7aa60 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
7aa70 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d fs, double *pTim
7aa80 65 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 eOut){. return
7aa90 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 pVfs->xCurrentTi
7aaa0 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 me(pVfs, pTimeOu
7aab0 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 t);.}..SQLITE_PR
7aac0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7aad0 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 3OsOpenMalloc(.
7aae0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
7aaf0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 fs, . const cha
7ab00 72 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c r *zFile, . sql
7ab10 69 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 ite3_file **ppFi
7ab20 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 le, . int flags
7ab30 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 ,. int *pOutFla
7ab40 67 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d gs.){. int rc =
7ab50 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
7ab60 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
7ab70 46 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 File;. pFile =
7ab80 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
7ab90 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 sqlite3Malloc(pV
7aba0 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 fs->szOsFile);.
7abb0 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 if( pFile ){.
7abc0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
7abd0 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 Open(pVfs, zFile
7abe0 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 , pFile, flags,
7abf0 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 pOutFlags);.
7ac00 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
7ac10 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
7ac20 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a e3_free(pFile);.
7ac30 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
7ac40 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 *ppFile = pFile
7ac50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
7ac60 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
7ac70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7ac80 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 lite3OsCloseFree
7ac90 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
7aca0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 File){. int rc
7acb0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 = SQLITE_OK;. a
7acc0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
7acd0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
7ace0 43 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 Close(pFile);.
7acf0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 sqlite3_free(pFi
7ad00 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 le);. return rc
7ad10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c ;.}../*.** The l
7ad20 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73 ist of all regis
7ad30 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d tered VFS implem
7ad40 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 entations..*/.st
7ad50 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 atic sqlite3_vfs
7ad60 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76 66 * SQLITE_WSD vf
7ad70 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 69 sList = 0;.#defi
7ad80 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 41 ne vfsList GLOBA
7ad90 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c L(sqlite3_vfs *,
7ada0 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a 2a vfsList)../*.**
7adb0 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62 79 Locate a VFS by
7adc0 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e 61 name. If no na
7add0 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69 6d me is given, sim
7ade0 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a 2a ply return the.*
7adf0 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20 74 * first VFS on t
7ae00 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 he list..*/.SQLI
7ae10 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 TE_API sqlite3_v
7ae20 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f fs *sqlite3_vfs_
7ae30 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 find(const char
7ae40 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65 *zVfs){. sqlite
7ae50 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b 3_vfs *pVfs = 0;
7ae60 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
7ae70 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 ADSAFE. sqlite3
7ae80 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23 _mutex *mutex;.#
7ae90 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 endif.#ifndef SQ
7aea0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
7aeb0 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 IT. int rc = sq
7aec0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
7aed0 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 ();. if( rc ) r
7aee0 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a eturn 0;.#endif.
7aef0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
7af00 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20 DSAFE. mutex =
7af10 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
7af20 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
7af30 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 TATIC_MASTER);.#
7af40 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f endif. sqlite3_
7af50 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
7af60 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d x);. for(pVfs =
7af70 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20 vfsList; pVfs;
7af80 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74 pVfs=pVfs->pNext
7af90 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d ){. if( zVfs=
7afa0 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
7afb0 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c if( strcmp(zVfs,
7afc0 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 pVfs->zName)==0
7afd0 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 ) break;. }.
7afe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
7aff0 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 ave(mutex);. re
7b000 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a turn pVfs;.}../*
7b010 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53 .** Unlink a VFS
7b020 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64 from the linked
7b030 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20 list.*/.static
7b040 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73 void vfsUnlink(s
7b050 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
7b060 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
7b070 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
7b080 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
7b090 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
7b0a0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 TATIC_MASTER)) )
7b0b0 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 ;. if( pVfs==0
7b0c0 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 ){. /* No-op
7b0d0 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 */. }else if( v
7b0e0 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a fsList==pVfs ){.
7b0f0 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 vfsList = pV
7b100 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c fs->pNext;. }el
7b110 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 29 se if( vfsList )
7b120 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 {. sqlite3_vf
7b130 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a s *p = vfsList;.
7b140 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e while( p->pN
7b150 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 ext && p->pNext!
7b160 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 =pVfs ){. p
7b170 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 = p->pNext;.
7b180 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e }. if( p->pN
7b190 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 ext==pVfs ){.
7b1a0 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 p->pNext = pV
7b1b0 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d fs->pNext;. }
7b1c0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
7b1d0 67 69 73 74 65 72 20 61 20 56 46 53 20 77 69 74 gister a VFS wit
7b1e0 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49 h the system. I
7b1f0 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f t is harmless to
7b200 20 72 65 67 69 73 74 65 72 20 74 68 65 20 73 61 register the sa
7b210 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70 me.** VFS multip
7b220 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e le times. The n
7b230 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 ew VFS becomes t
7b240 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61 he default if ma
7b250 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75 keDflt is.** tru
7b260 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
7b270 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 int sqlite3_vfs
7b280 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 _register(sqlite
7b290 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
7b2a0 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73 71 makeDflt){. sq
7b2b0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
7b2c0 65 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 ex = 0;.#ifndef
7b2d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
7b2e0 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 INIT. int rc =
7b2f0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
7b300 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 ze();. if( rc )
7b310 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 return rc;.#end
7b320 69 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c if. mutex = sql
7b330 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
7b340 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
7b350 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 IC_MASTER);. sq
7b360 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
7b370 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 r(mutex);. vfsU
7b380 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 69 nlink(pVfs);. i
7b390 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 76 f( makeDflt || v
7b3a0 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 fsList==0 ){.
7b3b0 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 pVfs->pNext = v
7b3c0 66 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 4c fsList;. vfsL
7b3d0 69 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 65 ist = pVfs;. }e
7b3e0 6c 73 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 lse{. pVfs->p
7b3f0 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 2d 3e Next = vfsList->
7b400 70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 4c 69 pNext;. vfsLi
7b410 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 st->pNext = pVfs
7b420 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 76 ;. }. assert(v
7b430 66 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 fsList);. sqlit
7b440 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
7b450 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
7b460 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
7b470 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 .** Unregister a
7b480 20 56 46 53 20 73 6f 20 74 68 61 74 20 69 74 20 VFS so that it
7b490 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 63 is no longer acc
7b4a0 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 essible..*/.SQLI
7b4b0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
7b4c0 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 e3_vfs_unregiste
7b4d0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 r(sqlite3_vfs *p
7b4e0 56 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 Vfs){.#if SQLITE
7b4f0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 _THREADSAFE. sq
7b500 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
7b510 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
7b520 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
7b530 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
7b540 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c R);.#endif. sql
7b550 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
7b560 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e (mutex);. vfsUn
7b570 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 71 link(pVfs);. sq
7b580 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
7b590 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 e(mutex);. retu
7b5a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
7b5b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7b5c0 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a 2a End of os.c ***
7b5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7b600 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7b610 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61 75 6c Begin file faul
7b620 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.c ************
7b630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7b650 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e 20 ./*.** 2008 Jan
7b660 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 22.**.** The aut
7b670 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
7b680 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
7b690 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
7b6a0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
7b6b0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
7b6c0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
7b6d0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
7b6e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
7b6f0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
7b700 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
7b710 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
7b720 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
7b730 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
7b740 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
7b750 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
7b760 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
7b770 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
7b780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b7c0 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 61 75 *.**.** $Id: fau
7b7d0 6c 74 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38 lt.c,v 1.11 2008
7b7e0 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 20 /09/02 00:52:52
7b7f0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
7b800 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
7b810 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 73 ntains code to s
7b820 75 70 70 6f 72 74 20 74 68 65 20 63 6f 6e 63 65 upport the conce
7b830 70 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 20 0a pt of "benign" .
7b840 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ** malloc failur
7b850 65 73 20 28 77 68 65 6e 20 74 68 65 20 78 4d 61 es (when the xMa
7b860 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 6c 6c lloc() or xReall
7b870 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 oc() method of t
7b880 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 he.** sqlite3_me
7b890 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 m_methods struct
7b8a0 75 72 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c ure fails to all
7b8b0 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 ocate a block of
7b8c0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 memory.** and r
7b8d0 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a eturns 0). .**.*
7b8e0 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 61 * Most malloc fa
7b8f0 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 ilures are non-b
7b900 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 68 65 enign. After the
7b910 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 65 0a y occur, SQLite.
7b920 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 65 20 ** abandons the
7b930 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f current operatio
7b940 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e n and returns an
7b950 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75 error code (usu
7b960 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e ally.** SQLITE_N
7b970 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 73 65 OMEM) to the use
7b980 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 r. However, some
7b990 74 69 6d 65 73 20 61 20 66 61 75 6c 74 20 69 73 times a fault is
7b9a0 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 not necessarily
7b9b0 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20 65 .** fatal. For e
7b9c0 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61 6c xample, if a mal
7b9d0 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 loc fails while
7b9e0 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73 68 20 resizing a hash
7b9f0 74 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a 20 table, this .**
7ba00 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 65 is completely re
7ba10 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c 79 coverable simply
7ba20 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e 67 by not carrying
7ba30 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65 2e out the resize.
7ba40 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 74 61 The .** hash ta
7ba50 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 ble will continu
7ba60 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f e to function no
7ba70 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61 rmally. So a ma
7ba80 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a 2a lloc failure .**
7ba90 20 64 75 72 69 6e 67 20 61 20 68 61 73 68 20 74 during a hash t
7baa0 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20 61 able resize is a
7bab0 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a 2a benign fault..*
7bac0 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 /...#ifndef SQLI
7bad0 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
7bae0 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 TEST../*.** Glob
7baf0 61 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2f al variables..*/
7bb00 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
7bb10 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b BenignMallocHook
7bb20 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f s BenignMallocHo
7bb30 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 oks;.static SQLI
7bb40 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 42 65 TE_WSD struct Be
7bb50 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 nignMallocHooks
7bb60 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 {. void (*xBeni
7bb70 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 3b 0a gnBegin)(void);.
7bb80 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e void (*xBenign
7bb90 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73 71 End)(void);.} sq
7bba0 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 lite3Hooks = { 0
7bbb0 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 , 0 };../* The "
7bbc0 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 wsdHooks" macro
7bbd0 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 will resolve to
7bbe0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
7bbf0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b BenignMallocHook
7bc00 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 s.** structure.
7bc10 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 If writable sta
7bc20 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 tic data is unsu
7bc30 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 pported on the t
7bc40 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 arget,.** we hav
7bc50 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 e to locate the
7bc60 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 state vector at
7bc70 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 run-time. In th
7bc80 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a e more common.**
7bc90 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69 74 case where writ
7bca0 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 able static data
7bcb0 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 is supported, w
7bcc0 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65 sdHooks can refe
7bcd0 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f r directly.** to
7bce0 20 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f 6f the "sqlite3Hoo
7bcf0 6b 73 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 ks" state vector
7bd00 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e declared above.
7bd10 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
7bd20 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 E_OMIT_WSD.# def
7bd30 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 ine wsdHooksInit
7bd40 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f \. BenignMallo
7bd50 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c 4f cHooks *x = &GLO
7bd60 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 BAL(BenignMalloc
7bd70 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f 6f Hooks,sqlite3Hoo
7bd80 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 ks).# define wsd
7bd90 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 65 Hooks x[0].#else
7bda0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f .# define wsdHoo
7bdb0 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 ksInit.# define
7bdc0 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65 33 wsdHooks sqlite3
7bdd0 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f Hooks.#endif.../
7bde0 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f *.** Register ho
7bdf0 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e oks to call when
7be00 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
7be10 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0a ignMalloc() and.
7be20 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e ** sqlite3EndBen
7be30 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20 ignMalloc() are
7be40 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 69 called, respecti
7be50 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f vely..*/.SQLITE_
7be60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
7be70 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 ite3BenignMalloc
7be80 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28 2a Hooks(. void (*
7be90 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f xBenignBegin)(vo
7bea0 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 42 id),. void (*xB
7beb0 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 0a enignEnd)(void).
7bec0 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 ){. wsdHooksIni
7bed0 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 t;. wsdHooks.xB
7bee0 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 42 65 enignBegin = xBe
7bef0 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73 64 nignBegin;. wsd
7bf00 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 Hooks.xBenignEnd
7bf10 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d = xBenignEnd;.}
7bf20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73 71 ../*.** This (sq
7bf30 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
7bf40 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 6c 6c 65 lloc()) is calle
7bf50 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f 64 65 d by SQLite code
7bf60 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 to indicate tha
7bf70 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 t.** subsequent
7bf80 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 malloc failures
7bf90 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 63 61 are benign. A ca
7bfa0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 6e 64 ll to sqlite3End
7bfb0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a BenignMalloc().*
7bfc0 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 * indicates that
7bfd0 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c subsequent mall
7bfe0 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 oc failures are
7bff0 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 non-benign..*/.S
7c000 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7c010 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 id sqlite3BeginB
7c020 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 enignMalloc(void
7c030 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 ){. wsdHooksIni
7c040 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b t;. if( wsdHook
7c050 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 29 s.xBenignBegin )
7c060 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 {. wsdHooks.x
7c070 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a 20 BenignBegin();.
7c080 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 }.}.SQLITE_PRIV
7c090 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
7c0a0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
7c0b0 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b void){. wsdHook
7c0c0 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 sInit;. if( wsd
7c0d0 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 Hooks.xBenignEnd
7c0e0 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 ){. wsdHooks
7c0f0 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 .xBenignEnd();.
7c100 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20 2f }.}..#endif /
7c110 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 * #ifndef SQLITE
7c120 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 _OMIT_BUILTIN_TE
7c130 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ST */../********
7c140 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61 ****** End of fa
7c150 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ult.c **********
7c160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c180 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
7c190 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
7c1a0 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a e mem0.c *******
7c1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c1d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
7c1e0 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 8 October 28.**.
7c1f0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
7c200 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
7c210 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
7c220 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
7c230 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
7c240 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
7c250 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
7c260 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
7c270 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
7c280 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
7c290 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
7c2a0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
7c2b0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
7c2c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
7c2d0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
7c2e0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
7c2f0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
7c300 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
7c310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
7c350 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
7c360 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d ains a no-op mem
7c370 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 ory allocation d
7c380 72 69 76 65 72 73 20 66 6f 72 20 75 73 65 20 77 rivers for use w
7c390 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 hen.** SQLITE_ZE
7c3a0 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 RO_MALLOC is def
7c3b0 69 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 ined. The alloc
7c3c0 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 69 6d ation drivers im
7c3d0 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 72 plemented.** her
7c3e0 65 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 e always fail.
7c3f0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 SQLite will not
7c400 6f 70 65 72 61 74 65 20 77 69 74 68 20 74 68 65 operate with the
7c410 73 65 20 64 72 69 76 65 72 73 2e 20 20 54 68 65 se drivers. The
7c420 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79 se.** are merely
7c430 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 20 placeholders.
7c440 52 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75 73 Real drivers mus
7c450 74 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 t be substituted
7c460 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 using.** sqlite
7c470 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f 72 3_config() befor
7c480 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70 e SQLite will op
7c490 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 erate..**.** $Id
7c4a0 3a 20 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 20 32 : mem0.c,v 1.1 2
7c4b0 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 3a 008/10/28 18:58:
7c4c0 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 20 drh Exp $.*/.
7c4d0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 ./*.** This vers
7c4e0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
7c4f0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 y allocator is t
7c500 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20 he default. It
7c510 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 is.** used when
7c520 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 no other memory
7c530 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65 allocator is spe
7c540 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d cified using com
7c550 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 pile-time.** mac
7c560 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ros..*/.#ifdef S
7c570 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f QLITE_ZERO_MALLO
7c580 43 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 76 C../*.** No-op v
7c590 65 72 73 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 6d ersions of all m
7c5a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
7c5b0 20 72 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 74 61 routines.*/.sta
7c5c0 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 tic void *sqlite
7c5d0 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 3MemMalloc(int n
7c5e0 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b Byte){ return 0;
7c5f0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 }.static void s
7c600 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f qlite3MemFree(vo
7c610 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 id *pPrior){ ret
7c620 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f urn; }.static vo
7c630 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 id *sqlite3MemRe
7c640 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 alloc(void *pPri
7c650 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 20 or, int nByte){
7c660 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 return 0; }.stat
7c670 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
7c680 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 mSize(void *pPri
7c690 6f 72 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d or){ return 0; }
7c6a0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
7c6b0 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e te3MemRoundup(in
7c6c0 74 20 6e 29 7b 20 72 65 74 75 72 6e 20 6e 3b 20 t n){ return n;
7c6d0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c }.static int sql
7c6e0 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 ite3MemInit(void
7c6f0 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75 *NotUsed){ retu
7c700 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
7c710 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
7c720 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 te3MemShutdown(v
7c730 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 oid *NotUsed){ r
7c740 65 74 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 eturn; }../*.**
7c750 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
7c760 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 the only routine
7c770 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 in this file wi
7c780 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b th external link
7c790 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c age..**.** Popul
7c7a0 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 ate the low-leve
7c7b0 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
7c7c0 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 ion function poi
7c7d0 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 nters in.** sqli
7c7e0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
7c7f0 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 m with pointers
7c800 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 to the routines
7c810 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f in this file..*/
7c820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7c830 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 void sqlite3MemS
7c840 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b etDefault(void){
7c850 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
7c860 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
7c870 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f ods defaultMetho
7c880 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 ds = {. sqli
7c890 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 te3MemMalloc,.
7c8a0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 sqlite3MemFre
7c8b0 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d e,. sqlite3M
7c8c0 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 emRealloc,.
7c8d0 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a sqlite3MemSize,.
7c8e0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 sqlite3MemR
7c8f0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c oundup,. sql
7c900 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 ite3MemInit,.
7c910 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 sqlite3MemShut
7c920 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d down,. 0. }
7c930 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ;. sqlite3_conf
7c940 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
7c950 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c _MALLOC, &defaul
7c960 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 tMethods);.}..#e
7c970 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 5a ndif /* SQLITE_Z
7c980 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f ERO_MALLOC */../
7c990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
7c9a0 6e 64 20 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a 2a nd of mem0.c ***
7c9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
7c9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
7c9f0 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 31 2e 63 egin file mem1.c
7ca00 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
7ca10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ca20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
7ca30 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 *.** 2007 August
7ca40 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 14.**.** The au
7ca50 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
7ca60 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
7ca70 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
7ca80 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
7ca90 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
7caa0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
7cab0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
7cac0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
7cad0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
7cae0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
7caf0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
7cb00 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
7cb10 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
7cb20 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
7cb30 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
7cb40 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
7cb50 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
7cb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7cb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7cb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7cb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7cba0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
7cbb0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d le contains low-
7cbc0 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
7cbd0 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 ocation drivers
7cbe0 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 for when.** SQLi
7cbf0 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 te will use the
7cc00 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 standard C-libra
7cc10 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f ry malloc/reallo
7cc20 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63 65 c/free interface
7cc30 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 .** to obtain th
7cc40 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 e memory it need
7cc50 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 s..**.** This fi
7cc60 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c le contains impl
7cc70 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 ementations of t
7cc80 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d he low-level mem
7cc90 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a ory allocation.*
7cca0 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63 69 * routines speci
7ccb0 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 fied in the sqli
7ccc0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
7ccd0 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 object..**.** $I
7cce0 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 33 30 d: mem1.c,v 1.30
7ccf0 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 2009/03/23 04:3
7cd00 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 3:33 danielk1977
7cd10 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
7cd20 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
7cd30 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
7cd40 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 cator is the def
7cd50 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 ault. It is.**
7cd60 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 used when no oth
7cd70 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 er memory alloca
7cd80 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64 tor is specified
7cd90 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 using compile-t
7cda0 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a ime.** macros..*
7cdb0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
7cdc0 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f SYSTEM_MALLOC../
7cdd0 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f 63 *.** Like malloc
7cde0 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 72 (), but remember
7cdf0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
7ce00 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 allocation.** s
7ce10 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66 69 o that we can fi
7ce20 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 69 6e nd it later usin
7ce30 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 g sqlite3MemSize
7ce40 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ()..**.** For th
7ce50 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 is low-level rou
7ce60 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 75 61 tine, we are gua
7ce70 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 42 79 ranteed that nBy
7ce80 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a 20 te>0 because.**
7ce90 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d cases of nByte<=
7cea0 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63 0 will be interc
7ceb0 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 epted and dealt
7cec0 77 69 74 68 20 62 79 20 68 69 67 68 65 72 20 6c with by higher l
7ced0 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 evel.** routines
7cee0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
7cef0 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c *sqlite3MemMall
7cf00 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 oc(int nByte){.
7cf10 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
7cf20 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 p;. assert( nBy
7cf30 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 te>0 );. nByte
7cf40 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b = ROUND8(nByte);
7cf50 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e . p = malloc( n
7cf60 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 Byte+8 );. if(
7cf70 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 p ){. p[0] =
7cf80 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a nByte;. p++;.
7cf90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f }. return (vo
7cfa0 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a id *)p;.}../*.**
7cfb0 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62 75 74 Like free() but
7cfc0 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f 63 works for alloc
7cfd0 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 20 ations obtained
7cfe0 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d 4d from sqlite3MemM
7cff0 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 71 alloc().** or sq
7d000 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 lite3MemRealloc(
7d010 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 )..**.** For thi
7d020 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 s low-level rout
7d030 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 79 20 ine, we already
7d040 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 know that pPrior
7d050 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 73 !=0 since.** cas
7d060 65 73 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d es where pPrior=
7d070 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 =0 will have bee
7d080 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64 n intecepted and
7d090 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 62 dealt with.** b
7d0a0 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 y higher-level r
7d0b0 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 outines..*/.stat
7d0c0 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d ic void sqlite3M
7d0d0 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 emFree(void *pPr
7d0e0 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f ior){. sqlite3_
7d0f0 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 int64 *p = (sqli
7d100 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f te3_int64*)pPrio
7d110 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 r;. assert( pPr
7d120 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b ior!=0 );. p--;
7d130 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f . free(p);.}../
7d140 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f *.** Like reallo
7d150 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20 c(). Resize an
7d160 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 allocation previ
7d170 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 ously obtained f
7d180 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65 rom.** sqlite3Me
7d190 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a mMalloc()..**.**
7d1a0 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 For this low-le
7d1b0 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77 vel interface, w
7d1c0 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 e know that pPri
7d1d0 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68 or!=0. Cases wh
7d1e0 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 ere.** pPrior==0
7d1f0 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e while have been
7d200 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 20 intercepted by
7d210 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 higher-level rou
7d220 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69 tine and.** redi
7d230 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f rected to xMallo
7d240 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 c. Similarly, w
7d250 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 e know that nByt
7d260 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20 e>0 becauses.**
7d270 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 cases where nByt
7d280 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 e<=0 will have b
7d290 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 een intercepted
7d2a0 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a by higher-level.
7d2b0 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 ** routines and
7d2c0 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 redirected to xF
7d2d0 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ree..*/.static v
7d2e0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 oid *sqlite3MemR
7d2f0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
7d300 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b ior, int nByte){
7d310 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
7d320 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 *p = (sqlite3_i
7d330 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 nt64*)pPrior;.
7d340 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d assert( pPrior!=
7d350 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0 && nByte>0 );.
7d360 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 nByte = ROUND8
7d370 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 28 (nByte);. p = (
7d380 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 sqlite3_int64*)p
7d390 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 Prior;. p--;.
7d3a0 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e p = realloc(p, n
7d3b0 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 Byte+8 );. if(
7d3c0 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 p ){. p[0] =
7d3d0 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a nByte;. p++;.
7d3e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f }. return (vo
7d3f0 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 id*)p;.}../*.**
7d400 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 Report the alloc
7d410 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 ated size of a p
7d420 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d rior return from
7d430 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 xMalloc().** or
7d440 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a xRealloc()..*/.
7d450 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
7d460 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a e3MemSize(void *
7d470 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 pPrior){. sqlit
7d480 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69 e3_int64 *p;. i
7d490 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 72 f( pPrior==0 ) r
7d4a0 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 28 eturn 0;. p = (
7d4b0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 sqlite3_int64*)p
7d4c0 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 Prior;. p--;.
7d4d0 72 65 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 5d return (int)p[0]
7d4e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 ;.}../*.** Round
7d4f0 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69 up a request si
7d500 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 ze to the next v
7d510 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 alid allocation
7d520 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 size..*/.static
7d530 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f int sqlite3MemRo
7d540 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 undup(int n){.
7d550 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29 return ROUND8(n)
7d560 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
7d570 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c alize this modul
7d580 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
7d590 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 sqlite3MemInit(
7d5a0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
7d5b0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
7d5c0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
7d5d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
7d5e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
7d5f0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 ialize this modu
7d600 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
7d610 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 id sqlite3MemShu
7d620 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
7d630 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
7d640 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
7d650 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a );. return;.}..
7d660 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
7d670 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 ne is the only r
7d680 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 outine in this f
7d690 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 ile with externa
7d6a0 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a l linkage..**.**
7d6b0 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f Populate the lo
7d6c0 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
7d6d0 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
7d6e0 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a on pointers in.*
7d6f0 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
7d700 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 onfig.m with poi
7d710 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 nters to the rou
7d720 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
7d730 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
7d740 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
7d750 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
7d760 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
7d770 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
7d780 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c m_methods defaul
7d790 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 tMethods = {.
7d7a0 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c sqlite3MemMall
7d7b0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
7d7c0 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 MemFree,. sq
7d7d0 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c lite3MemRealloc,
7d7e0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
7d7f0 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 Size,. sqlit
7d800 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 e3MemRoundup,.
7d810 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 sqlite3MemIni
7d820 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d t,. sqlite3M
7d830 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 emShutdown,.
7d840 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 0. };. sqlite
7d850 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
7d860 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 CONFIG_MALLOC, &
7d870 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b defaultMethods);
7d880 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
7d890 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c LITE_SYSTEM_MALL
7d8a0 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a OC */../********
7d8b0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
7d8c0 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m1.c ***********
7d8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d8f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
7d900 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
7d910 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a e mem2.c *******
7d920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d940 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
7d950 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 7 August 15.**.*
7d960 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
7d970 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
7d980 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
7d990 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
7d9a0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
7d9b0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
7d9c0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
7d9d0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
7d9e0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
7d9f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
7da00 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
7da10 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
7da20 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
7da30 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
7da40 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
7da50 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
7da60 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
7da70 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
7da80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7da90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7daa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7dab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
7dac0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
7dad0 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 ins low-level me
7dae0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
7daf0 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e drivers for when
7db00 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
7db10 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 use the standard
7db20 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f C-library mallo
7db30 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 c/realloc/free i
7db40 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f nterface.** to o
7db50 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 btain the memory
7db60 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20 it needs while
7db70 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61 adding lots of a
7db80 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67 dditional debugg
7db90 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 ing.** informati
7dba0 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63 on to each alloc
7dbb0 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74 ation in order t
7dbc0 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e o help detect an
7dbd0 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20 d fix memory.**
7dbe0 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79 leaks and memory
7dbf0 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a usage errors..*
7dc00 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
7dc10 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e ontains implemen
7dc20 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c tations of the l
7dc30 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
7dc40 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f allocation.** ro
7dc50 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 utines specified
7dc60 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f in the sqlite3_
7dc70 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 mem_methods obje
7dc80 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d ct..**.** $Id: m
7dc90 65 6d 32 2e 63 2c 76 20 31 2e 34 35 20 32 30 30 em2.c,v 1.45 200
7dca0 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 33 9/03/23 04:33:33
7dcb0 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
7dcc0 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 $.*/../*.** Thi
7dcd0 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 s version of the
7dce0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
7dcf0 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 69 r is used only i
7dd00 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f f the.** SQLITE_
7dd10 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f 20 69 MEMDEBUG macro i
7dd20 73 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69 66 s defined.*/.#if
7dd30 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 def SQLITE_MEMDE
7dd40 42 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 BUG../*.** The b
7dd50 61 63 6b 74 72 61 63 65 20 66 75 6e 63 74 69 6f acktrace functio
7dd60 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20 61 nality is only a
7dd70 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20 47 4c vailable with GL
7dd80 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f IBC.*/.#ifdef __
7dd90 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 72 6e GLIBC__. extern
7dda0 20 69 6e 74 20 62 61 63 6b 74 72 61 63 65 28 76 int backtrace(v
7ddb0 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65 78 oid**,int);. ex
7ddc0 74 65 72 6e 20 76 6f 69 64 20 62 61 63 6b 74 72 tern void backtr
7ddd0 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 76 ace_symbols_fd(v
7dde0 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c 69 oid*const*,int,i
7ddf0 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 nt);.#else.# def
7de00 69 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41 2c ine backtrace(A,
7de10 42 29 20 31 0a 23 20 64 65 66 69 6e 65 20 62 61 B) 1.# define ba
7de20 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f cktrace_symbols_
7de30 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 fd(A,B,C).#endif
7de40 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 6d ../*.** Each mem
7de50 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c ory allocation l
7de60 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a ooks like this:.
7de70 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d **.** ---------
7de80 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7de90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7dea0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7deb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
7dec0 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 62 ** | Title | b
7ded0 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 acktrace pointer
7dee0 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 s | MemBlockHdr
7def0 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 7c | allocation |
7df00 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a 20 EndGuard |.**
7df10 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
7df20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7df30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7df40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7df50 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 ---------.**.**
7df60 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 The application
7df70 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 61 code sees only a
7df80 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
7df90 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 20 allocation. We
7dfa0 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b 20 have.** to back
7dfb0 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c 6f up from the allo
7dfc0 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74 cation pointer t
7dfd0 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c o find the MemBl
7dfe0 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a 20 ockHdr. The.**
7dff0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c 6c MemBlockHdr tell
7e000 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f 66 s us the size of
7e010 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
7e020 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f and the number o
7e030 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 70 f.** backtrace p
7e040 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65 20 ointers. There
7e050 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64 20 is also a guard
7e060 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64 20 word at the end
7e070 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f of the.** MemBlo
7e080 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 ckHdr..*/.struct
7e090 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a 20 MemBlockHdr {.
7e0a0 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20 20 i64 iSize;
7e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e0c0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
7e0d0 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 this allocation
7e0e0 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 */. struct MemB
7e0f0 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20 lockHdr *pNext,
7e100 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b *pPrev; /* Link
7e110 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75 ed list of all u
7e120 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f nfreed memory */
7e130 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 . char nBacktra
7e140 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ce;
7e150 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
7e160 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20 6f of backtraces o
7e170 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0a n this alloc */.
7e180 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 char nBacktrac
7e190 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20 eSlots;
7e1a0 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 /* Availab
7e1b0 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f le backtrace slo
7e1c0 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e 54 ts */. short nT
7e1d0 69 74 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 itle;
7e1e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
7e1f0 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69 ytes of title; i
7e200 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0a ncludes '\0' */.
7e210 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72 64 int iForeGuard
7e220 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
7e230 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20 77 /* Guard w
7e240 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20 2a ord for sanity *
7e250 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 72 /.};../*.** Guar
7e260 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 69 d words.*/.#defi
7e270 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 78 38 ne FOREGUARD 0x8
7e280 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65 20 0F5E153.#define
7e290 52 45 41 52 47 55 41 52 44 20 30 78 45 34 36 37 REARGUARD 0xE467
7e2a0 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 6B53../*.** Numb
7e2b0 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 7a er of malloc siz
7e2c0 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f 20 e increments to
7e2d0 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e track..*/.#defin
7e2e0 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a 0a e NCSIZE 1000..
7e2f0 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 /*.** All of the
7e300 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 static variable
7e310 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d s used by this m
7e320 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 odule are collec
7e330 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 ted.** into a si
7e340 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e ngle structure n
7e350 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 69 amed "mem". Thi
7e360 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 s is to keep the
7e370 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 .** static varia
7e380 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 bles organized a
7e390 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d nd to reduce nam
7e3a0 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e espace pollution
7e3b0 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f .** when this mo
7e3c0 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 dule is combined
7e3d0 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 with other in t
7e3e0 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e he amalgamation.
7e3f0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
7e400 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a t {. . /*. **
7e410 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f Mutex to contro
7e420 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 l access to the
7e430 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
7e440 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a n subsystem.. *
7e450 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 /. sqlite3_mute
7e460 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a x *mutex;.. /*.
7e470 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 61 ** Head and ta
7e480 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c il of a linked l
7e490 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 74 ist of all outst
7e4a0 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f anding allocatio
7e4b0 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 74 ns. */. struct
7e4c0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 46 MemBlockHdr *pF
7e4d0 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20 4d irst;. struct M
7e4e0 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 73 emBlockHdr *pLas
7e4f0 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 t;. . /*. **
7e500 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 The number of le
7e510 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 63 vels of backtrac
7e520 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 77 e to save in new
7e530 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 allocations..
7e540 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 72 */. int nBacktr
7e550 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 ace;. void (*xB
7e560 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 acktrace)(int, i
7e570 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a 20 nt, void **);..
7e580 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 74 /*. ** Title t
7e590 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69 6e ext to insert in
7e5a0 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 62 front of each b
7e5b0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 lock. */. int
7e5c0 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 2f nTitle; /
7e5d0 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74 6c * Bytes of zTitl
7e5e0 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 6c e to save. Incl
7e5f0 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70 61 udes '\0' and pa
7e600 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 dding */. char
7e610 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f 2a zTitle[100]; /*
7e620 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74 20 The title text
7e630 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 73 */.. /* . ** s
7e640 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61 qlite3MallocDisa
7e650 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e 74 llow() increment
7e660 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 s the following
7e670 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 71 counter.. ** sq
7e680 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 lite3MallocAllow
7e690 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69 74 () decrements it
7e6a0 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 .. */. int dis
7e6b0 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f 74 allow; /* Do not
7e6c0 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 6c allow memory al
7e6d0 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f location */.. /
7e6e0 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73 74 *. ** Gather st
7e6f0 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65 20 atistics on the
7e700 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 sizes of memory
7e710 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a allocations.. *
7e720 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 74 * nAlloc[i] is t
7e730 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c he number of all
7e740 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 ocation attempts
7e750 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 74 of i*8. ** byt
7e760 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 69 es. i==NCSIZE i
7e770 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
7e780 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d allocation attem
7e790 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 7a pts for. ** siz
7e7a0 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 53 es more than NCS
7e7b0 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 2a IZE*8 bytes.. *
7e7c0 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b 4e /. int nAlloc[N
7e7d0 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a 20 CSIZE]; /*
7e7e0 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
7e7f0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 allocations */.
7e800 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e 43 int nCurrent[NC
7e810 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 72 SIZE]; /* Cur
7e820 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 rent number of a
7e830 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 llocations */.
7e840 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e 43 int mxCurrent[NC
7e850 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 68 SIZE]; /* High
7e860 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e water mark for n
7e870 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 Current */..} me
7e880 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 m;.../*.** Adjus
7e890 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 73 t memory usage s
7e8a0 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 61 tatistics.*/.sta
7e8b0 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74 53 tic void adjustS
7e8c0 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c 20 tats(int iSize,
7e8d0 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b 0a int increment){.
7e8e0 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 38 int i = ROUND8
7e8f0 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 28 (iSize)/8;. if(
7e900 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a 20 i>NCSIZE-1 ){.
7e910 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d 20 i = NCSIZE -
7e920 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 63 1;. }. if( inc
7e930 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 20 rement>0 ){.
7e940 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b mem.nAlloc[i]++;
7e950 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e . mem.nCurren
7e960 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 t[i]++;. if(
7e970 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e mem.nCurrent[i]>
7e980 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d mem.mxCurrent[i]
7e990 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d 78 ){. mem.mx
7e9a0 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d Current[i] = mem
7e9b0 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 20 .nCurrent[i];.
7e9c0 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
7e9d0 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d mem.nCurrent[i]
7e9e0 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 --;. assert(
7e9f0 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e mem.nCurrent[i]>
7ea00 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a =0 );. }.}../*.
7ea10 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c 6f ** Given an allo
7ea20 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 65 cation, find the
7ea30 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f 72 MemBlockHdr for
7ea40 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f 6e that allocation
7ea50 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
7ea60 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65 20 tine checks the
7ea70 67 75 61 72 64 73 20 61 74 20 65 69 74 68 65 72 guards at either
7ea80 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c 6f end of the allo
7ea90 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66 cation and.** if
7eaa0 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72 72 they are incorr
7eab0 65 63 74 20 69 74 20 61 73 73 65 72 74 73 2e 0a ect it asserts..
7eac0 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 */.static struct
7ead0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 71 MemBlockHdr *sq
7eae0 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 lite3MemsysGetHe
7eaf0 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c 6f ader(void *pAllo
7eb00 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 63 cation){. struc
7eb10 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
7eb20 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 ;. int *pInt;.
7eb30 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 20 u8 *pU8;. int
7eb40 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 3d nReserve;.. p =
7eb50 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 (struct MemBloc
7eb60 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 6f kHdr*)pAllocatio
7eb70 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 65 n;. p--;. asse
7eb80 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 72 rt( p->iForeGuar
7eb90 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 52 d==(int)FOREGUAR
7eba0 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 D );. nReserve
7ebb0 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 7a = ROUND8(p->iSiz
7ebc0 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 6e e);. pInt = (in
7ebd0 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a t*)pAllocation;.
7ebe0 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41 6c pU8 = (u8*)pAl
7ebf0 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 65 location;. asse
7ec00 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 rt( pInt[nReserv
7ec10 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d 3d e/sizeof(int)]==
7ec20 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 20 29 (int)REARGUARD )
7ec30 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65 63 ;. /* This chec
7ec40 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22 65 ks any of the "e
7ec50 78 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c 6f xtra" bytes allo
7ec60 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 74 cated due. ** t
7ec70 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 6f o rounding up to
7ec80 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 an 8 byte bound
7ec90 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a 20 ary to ensure .
7eca0 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27 74 ** they haven't
7ecb0 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65 been overwritte
7ecc0 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 n.. */. while(
7ecd0 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 2d nReserve-- > p-
7ece0 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 74 28 >iSize ) assert(
7ecf0 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d 3d pU8[nReserve]==
7ed00 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 6e 0x65 );. return
7ed10 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 p;.}../*.** Ret
7ed20 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
7ed30 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c f bytes currentl
7ed40 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 y allocated at a
7ed50 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 61 ddress p..*/.sta
7ed60 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d tic int sqlite3M
7ed70 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b emSize(void *p){
7ed80 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
7ed90 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 69 ckHdr *pHdr;. i
7eda0 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 f( !p ){. ret
7edb0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 64 urn 0;. }. pHd
7edc0 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 r = sqlite3Memsy
7edd0 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20 sGetHeader(p);.
7ede0 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 53 return pHdr->iS
7edf0 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e ize;.}../*.** In
7ee00 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
7ee10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
7ee20 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 ubsystem..*/.sta
7ee30 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d tic int sqlite3M
7ee40 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 emInit(void *Not
7ee50 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
7ee60 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
7ee70 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 d);. assert( (s
7ee80 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65 6d izeof(struct Mem
7ee90 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d 20 BlockHdr)&7) ==
7eea0 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 0 );. if( !sqli
7eeb0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
7eec0 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 bMemstat ){.
7eed0 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 61 /* If memory sta
7eee0 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 tus is enabled,
7eef0 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 2e then the malloc.
7ef00 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20 61 c wrapper will a
7ef10 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 6f lready. ** ho
7ef20 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 ld the STATIC_ME
7ef30 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 65 M mutex when the
7ef40 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20 61 routines here a
7ef50 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a 20 re invoked. */.
7ef60 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 73 mem.mutex = s
7ef70 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
7ef80 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
7ef90 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 ATIC_MEM);. }.
7efa0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
7efb0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e K;.}../*.** Dein
7efc0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
7efd0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
7efe0 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 ubsystem..*/.sta
7eff0 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
7f000 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 MemShutdown(void
7f010 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
7f020 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
7f030 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e 6d otUsed);. mem.m
7f040 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a utex = 0;.}../*.
7f050 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 ** Round up a re
7f060 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 quest size to th
7f070 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c e next valid all
7f080 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f ocation size..*/
7f090 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
7f0a0 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e te3MemRoundup(in
7f0b0 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 t n){. return R
7f0c0 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a OUND8(n);.}../*.
7f0d0 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 ** Allocate nByt
7f0e0 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 e bytes of memor
7f0f0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 y..*/.static voi
7f100 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c d *sqlite3MemMal
7f110 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a loc(int nByte){.
7f120 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
7f130 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f kHdr *pHdr;. vo
7f140 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 id **pBt;. char
7f150 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 *z;. int *pInt
7f160 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b ;. void *p = 0;
7f170 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 . int totalSize
7f180 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 ;. int nReserve
7f190 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
7f1a0 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 x_enter(mem.mute
7f1b0 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 x);. assert( me
7f1c0 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b m.disallow==0 );
7f1d0 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f . nReserve = RO
7f1e0 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 74 UND8(nByte);. t
7f1f0 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65 otalSize = nRese
7f200 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 rve + sizeof(*pH
7f210 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 dr) + sizeof(int
7f220 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ) +.
7f230 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 mem.nBacktrac
7f240 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 e*sizeof(void*)
7f250 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 + mem.nTitle;.
7f260 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c p = malloc(total
7f270 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 29 Size);. if( p )
7f280 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20 {. z = p;.
7f290 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26 pBt = (void**)&
7f2a0 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20 z[mem.nTitle];.
7f2b0 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75 63 pHdr = (struc
7f2c0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 26 t MemBlockHdr*)&
7f2d0 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 pBt[mem.nBacktra
7f2e0 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 ce];. pHdr->p
7f2f0 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 48 Next = 0;. pH
7f300 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d 2e dr->pPrev = mem.
7f310 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20 6d pLast;. if( m
7f320 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 em.pLast ){.
7f330 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e 65 mem.pLast->pNe
7f340 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d xt = pHdr;. }
7f350 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e else{. mem.
7f360 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20 pFirst = pHdr;.
7f370 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 }. mem.pLa
7f380 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 70 st = pHdr;. p
7f390 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20 Hdr->iForeGuard
7f3a0 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 20 = FOREGUARD;.
7f3b0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
7f3c0 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 61 eSlots = mem.nBa
7f3d0 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48 64 cktrace;. pHd
7f3e0 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d 2e r->nTitle = mem.
7f3f0 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 20 nTitle;. if(
7f400 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 29 mem.nBacktrace )
7f410 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61 41 {. void *aA
7f420 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 70 ddr[40];. p
7f430 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 Hdr->nBacktrace
7f440 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 64 64 = backtrace(aAdd
7f450 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 r, mem.nBacktrac
7f460 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d 65 e+1)-1;. me
7f470 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 72 mcpy(pBt, &aAddr
7f480 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b [1], pHdr->nBack
7f490 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 trace*sizeof(voi
7f4a0 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 d*));. asse
7f4b0 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 20 rt(pBt[0]);.
7f4c0 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74 if( mem.xBackt
7f4d0 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 race ){.
7f4e0 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e mem.xBacktrace(n
7f4f0 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 Byte, pHdr->nBac
7f500 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72 ktrace-1, &aAddr
7f510 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 [1]);. }.
7f520 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
7f530 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 Hdr->nBacktrace
7f540 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 0;. }. i
7f550 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b f( mem.nTitle ){
7f560 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c . memcpy(z,
7f570 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d mem.zTitle, mem
7f580 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a .nTitle);. }.
7f590 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 pHdr->iSize
7f5a0 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a = nByte;. adj
7f5b0 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20 ustStats(nByte,
7f5c0 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20 +1);. pInt =
7f5d0 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a (int*)&pHdr[1];.
7f5e0 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 pInt[nReserv
7f5f0 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d e/sizeof(int)] =
7f600 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20 REARGUARD;.
7f610 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 36 memset(pInt, 0x6
7f620 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 5, nReserve);.
7f630 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e p = (void*)pIn
7f640 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 t;. }. sqlite3
7f650 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
7f660 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 .mutex);. retur
7f670 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 n p; .}../*.** F
7f680 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 ree memory..*/.s
7f690 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
7f6a0 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a e3MemFree(void *
7f6b0 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63 pPrior){. struc
7f6c0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
7f6d0 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 Hdr;. void **pB
7f6e0 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 t;. char *z;.
7f6f0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 assert( sqlite3G
7f700 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
7f710 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 65 stat || mem.mute
7f720 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 3d x!=0 );. pHdr =
7f730 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 sqlite3MemsysGe
7f740 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b tHeader(pPrior);
7f750 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a . pBt = (void**
7f760 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d 20 )pHdr;. pBt -=
7f770 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
7f780 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 33 Slots;. sqlite3
7f790 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
7f7a0 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 .mutex);. if( p
7f7b0 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 Hdr->pPrev ){.
7f7c0 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e assert( pHdr->
7f7d0 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48 pPrev->pNext==pH
7f7e0 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e dr );. pHdr->
7f7f0 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 pPrev->pNext = p
7f800 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 Hdr->pNext;. }e
7f810 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
7f820 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64 mem.pFirst==pHd
7f830 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69 r );. mem.pFi
7f840 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 rst = pHdr->pNex
7f850 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64 t;. }. if( pHd
7f860 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 r->pNext ){.
7f870 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e assert( pHdr->pN
7f880 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72 ext->pPrev==pHdr
7f890 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e );. pHdr->pN
7f8a0 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64 ext->pPrev = pHd
7f8b0 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73 r->pPrev;. }els
7f8c0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d e{. assert( m
7f8d0 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29 em.pLast==pHdr )
7f8e0 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 ;. mem.pLast
7f8f0 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 = pHdr->pPrev;.
7f900 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 }. z = (char*)
7f910 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72 pBt;. z -= pHdr
7f920 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a 75 ->nTitle;. adju
7f930 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53 stStats(pHdr->iS
7f940 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d 73 ize, -1);. mems
7f950 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65 et(z, 0x2b, size
7f960 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e of(void*)*pHdr->
7f970 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 nBacktraceSlots
7f980 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 + sizeof(*pHdr)
7f990 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
7f9a0 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 pHdr->iSize
7f9b0 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20 + sizeof(int) +
7f9c0 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20 pHdr->nTitle);.
7f9d0 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69 free(z);. sqli
7f9e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
7f9f0 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a mem.mutex); .}.
7fa00 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
7fa10 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 e size of an exi
7fa20 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c sting memory all
7fa30 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 ocation..**.** F
7fa40 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69 6e or this debuggin
7fa50 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e g implementation
7fa60 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61 , we *always* ma
7fa70 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ke a copy of the
7fa80 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 .** allocation i
7fa90 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20 nto a new place
7faa0 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74 in memory. In t
7fab0 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 20 his way, if the
7fac0 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c .** higher level
7fad0 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70 code is using p
7fae0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c ointer to the ol
7faf0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 d allocation, it
7fb00 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72 is .** much mor
7fb10 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61 e likely to brea
7fb20 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63 k and we are muc
7fb30 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f h more liking to
7fb40 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72 find.** the err
7fb50 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f or..*/.static vo
7fb60 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 id *sqlite3MemRe
7fb70 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 alloc(void *pPri
7fb80 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a or, int nByte){.
7fb90 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
7fba0 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a 20 kHdr *pOldHdr;.
7fbb0 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 61 void *pNew;. a
7fbc0 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c ssert( mem.disal
7fbd0 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c 64 low==0 );. pOld
7fbe0 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d Hdr = sqlite3Mem
7fbf0 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50 72 sysGetHeader(pPr
7fc00 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 ior);. pNew = s
7fc10 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 qlite3MemMalloc(
7fc20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 4e nByte);. if( pN
7fc30 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 ew ){. memcpy
7fc40 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 6e (pNew, pPrior, n
7fc50 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 53 Byte<pOldHdr->iS
7fc60 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 4f ize ? nByte : pO
7fc70 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 ldHdr->iSize);.
7fc80 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c if( nByte>pOl
7fc90 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 dHdr->iSize ){.
7fca0 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 memset(&((c
7fcb0 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 har*)pNew)[pOldH
7fcc0 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 62 dr->iSize], 0x2b
7fcd0 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 64 , nByte - pOldHd
7fce0 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 7d r->iSize);. }
7fcf0 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 . sqlite3MemF
7fd00 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d ree(pPrior);. }
7fd10 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
7fd20 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 }../*.** Populat
7fd30 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 e the low-level
7fd40 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
7fd50 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 n function point
7fd60 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 ers in.** sqlite
7fd70 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3GlobalConfig.m
7fd80 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f with pointers to
7fd90 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e the routines in
7fda0 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 this file..*/.S
7fdb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7fdc0 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 id sqlite3MemSet
7fdd0 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 Default(void){.
7fde0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
7fdf0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
7fe00 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 s defaultMethods
7fe10 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 = {. sqlite
7fe20 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 3MemMalloc,.
7fe30 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c sqlite3MemFree,
7fe40 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
7fe50 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 Realloc,. sq
7fe60 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 lite3MemSize,.
7fe70 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 sqlite3MemRou
7fe80 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 ndup,. sqlit
7fe90 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 e3MemInit,.
7fea0 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f sqlite3MemShutdo
7feb0 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a wn,. 0. };.
7fec0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 sqlite3_config
7fed0 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d (SQLITE_CONFIG_M
7fee0 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d ALLOC, &defaultM
7fef0 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ethods);.}../*.*
7ff00 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 * Set the number
7ff10 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 of backtrace le
7ff20 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 vels kept for ea
7ff30 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a ch allocation..*
7ff40 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 * A value of zer
7ff50 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b o turns off back
7ff60 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 tracing. The nu
7ff70 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 mber is always r
7ff80 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 ounded.** up to
7ff90 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e a multiple of 2.
7ffa0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
7ffb0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
7ffc0 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 emdebugBacktrace
7ffd0 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 (int depth){. i
7ffe0 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 f( depth<0 ){ de
7fff0 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 pth = 0; }. if(
80000 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 depth>20 ){ dep
80010 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 th = 20; }. dep
80020 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 th = (depth+1)&0
80030 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b xfe;. mem.nBack
80040 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d trace = depth;.}
80050 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
80060 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
80070 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 debugBacktraceCa
80080 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 llback(void (*xB
80090 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 acktrace)(int, i
800a0 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 nt, void **)){.
800b0 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 mem.xBacktrace
800c0 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a = xBacktrace;.}.
800d0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 ./*.** Set the t
800e0 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 itle string for
800f0 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 subsequent alloc
80100 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ations..*/.SQLIT
80110 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
80120 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 qlite3MemdebugSe
80130 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 ttitle(const cha
80140 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 6e r *zTitle){. un
80150 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 73 signed int n = s
80160 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
80170 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 Title) + 1;. sq
80180 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
80190 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 r(mem.mutex);.
801a0 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 if( n>=sizeof(me
801b0 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 m.zTitle) ) n =
801c0 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c sizeof(mem.zTitl
801d0 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d e)-1;. memcpy(m
801e0 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c em.zTitle, zTitl
801f0 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 e, n);. mem.zTi
80200 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 tle[n] = 0;. me
80210 6d 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44 m.nTitle = ROUND
80220 38 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 8(n);. sqlite3_
80230 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e mutex_leave(mem.
80240 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 mutex);.}..SQLIT
80250 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
80260 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 qlite3MemdebugSy
80270 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d nc(){. struct M
80280 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 emBlockHdr *pHdr
80290 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d ;. for(pHdr=mem
802a0 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 .pFirst; pHdr; p
802b0 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 Hdr=pHdr->pNext)
802c0 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 {. void **pBt
802d0 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b = (void**)pHdr;
802e0 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 . pBt -= pHdr
802f0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 ->nBacktraceSlot
80300 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b s;. mem.xBack
80310 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a trace(pHdr->iSiz
80320 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 e, pHdr->nBacktr
80330 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b ace-1, &pBt[1]);
80340 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 . }.}../*.** Op
80350 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 en the file indi
80360 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 cated and write
80370 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 a log of all unf
80380 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 reed memory .**
80390 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f allocations into
803a0 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 that log..*/.SQ
803b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
803c0 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 d sqlite3Memdebu
803d0 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 gDump(const char
803e0 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 *zFilename){.
803f0 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 FILE *out;. str
80400 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
80410 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a *pHdr;. void **
80420 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 pBt;. int i;.
80430 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c out = fopen(zFil
80440 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 ename, "w");. i
80450 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 f( out==0 ){.
80460 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
80470 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f "** Unable to o
80480 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 utput memory deb
80490 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 ug output log: %
804a0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 s **\n",.
804b0 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 zFi
804c0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 lename);. ret
804d0 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 urn;. }. for(p
804e0 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 Hdr=mem.pFirst;
804f0 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d pHdr; pHdr=pHdr-
80500 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 >pNext){. cha
80510 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 r *z = (char*)pH
80520 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 dr;. z -= pHd
80530 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f r->nBacktraceSlo
80540 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 ts*sizeof(void*)
80550 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b + pHdr->nTitle;
80560 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
80570 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 , "**** %lld byt
80580 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 es at %p from %s
80590 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 ****\n", .
805a0 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 pHdr->iSi
805b0 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 ze, &pHdr[1], pH
805c0 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a dr->nTitle ? z :
805d0 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 "???");. if(
805e0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
805f0 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 e ){. fflus
80600 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 h(out);. pB
80610 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 t = (void**)pHdr
80620 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 ;. pBt -= p
80630 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 Hdr->nBacktraceS
80640 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b lots;. back
80650 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 trace_symbols_fd
80660 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 (pBt, pHdr->nBac
80670 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f ktrace, fileno(o
80680 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 ut));. fpri
80690 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a ntf(out, "\n");.
806a0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 }. }. fpri
806b0 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 ntf(out, "COUNTS
806c0 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 :\n");. for(i=0
806d0 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b ; i<NCSIZE-1; i+
806e0 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e +){. if( mem.
806f0 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 nAlloc[i] ){.
80700 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
80710 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 " %5d: %10d %1
80720 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 0d %10d\n", .
80730 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 i*8, me
80740 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d m.nAlloc[i], mem
80750 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 .nCurrent[i], me
80760 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b m.mxCurrent[i]);
80770 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
80780 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 mem.nAlloc[NCSI
80790 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 ZE-1] ){. fpr
807a0 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 intf(out, " %5
807b0 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 d: %10d %10d %10
807c0 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
807d0 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d NCSIZE*8-8, m
807e0 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 em.nAlloc[NCSIZE
807f0 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 -1],.
80800 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e mem.nCurrent[N
80810 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 CSIZE-1], mem.mx
80820 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 Current[NCSIZE-1
80830 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 ]);. }. fclose
80840 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (out);.}../*.**
80850 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
80860 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 r of times sqlit
80870 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 e3MemMalloc() ha
80880 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a s been called..*
80890 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
808a0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 int sqlite3Memd
808b0 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 ebugMallocCount(
808c0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e ){. int i;. in
808d0 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 t nTotal = 0;.
808e0 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a for(i=0; i<NCSIZ
808f0 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f E; i++){. nTo
80900 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f tal += mem.nAllo
80910 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 c[i];. }. retu
80920 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 rn nTotal;.}...#
80930 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
80940 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a MEMDEBUG */../**
80950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
80960 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a of mem2.c *****
80970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
809a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
809b0 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a in file mem3.c *
809c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
809d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
809e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
809f0 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 ** 2007 October
80a00 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 14.**.** The aut
80a10 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
80a20 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
80a30 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
80a40 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
80a50 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
80a60 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
80a70 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
80a80 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
80a90 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
80aa0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
80ab0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
80ac0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
80ad0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
80ae0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
80af0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
80b00 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
80b10 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
80b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80b60 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
80b70 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 ontains the C fu
80b80 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 nctions that imp
80b90 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a lement a memory.
80ba0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ** allocation su
80bb0 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 bsystem for use
80bc0 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a by SQLite. .**.*
80bd0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
80be0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
80bf0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
80c00 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 m omits all.** u
80c10 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 se of malloc().
80c20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 The SQLite user
80c30 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b supplies a block
80c40 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 of memory.** be
80c50 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c fore calling sql
80c60 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
80c70 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c ) from which all
80c80 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 ocations.** are
80c90 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 made and returne
80ca0 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 d by the xMalloc
80cb0 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 () and xRealloc(
80cc0 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ) .** implementa
80cd0 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 tions. Once sqli
80ce0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
80cf0 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
80d00 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 ,.** the amount
80d10 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 of memory availa
80d20 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 ble to SQLite is
80d30 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f fixed and canno
80d40 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e t.** be changed.
80d50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 .**.** This vers
80d60 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
80d70 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
80d80 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 system is includ
80d90 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 ed.** in the bui
80da0 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 ld only if SQLIT
80db0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 E_ENABLE_MEMSYS3
80dc0 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a is defined..**.
80dd0 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76 ** $Id: mem3.c,v
80de0 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f 31 39 1.25 2008/11/19
80df0 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 16:52:44 daniel
80e00 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
80e10 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
80e20 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
80e30 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e allocator is on
80e40 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 ly built into th
80e50 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c e library.** SQL
80e60 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
80e70 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44 S3 is defined. D
80e80 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d efining this sym
80e90 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 bol does not.**
80ea0 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 mean that the li
80eb0 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61 brary will use a
80ec0 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 memory-pool by
80ed0 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68 default, just th
80ee0 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69 at.** it is avai
80ef0 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f lable. The mempo
80f00 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ol allocator is
80f10 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 6c activated by cal
80f20 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ling.** sqlite3_
80f30 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 config()..*/.#if
80f40 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
80f50 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a E_MEMSYS3../*.**
80f60 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69 Maximum size (i
80f70 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 n Mem3Blocks) of
80f80 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b a "small" chunk
80f90 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
80fa0 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a SMALL 10.../*.**
80fb0 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c Number of freel
80fc0 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a ist hash slots.*
80fd0 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48 /.#define N_HASH
80fe0 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 61../*.** A me
80ff0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
81000 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22 (also called a "
81010 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73 chunk") consists
81020 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d of two or .** m
81030 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65 ore blocks where
81040 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38 each block is 8
81050 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 72 bytes. The fir
81060 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 0a st 8 bytes are .
81070 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 74 ** a header that
81080 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 is not returned
81090 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a to the user..**
810a0 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74 .** A chunk is t
810b0 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b wo or more block
810c0 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 72 s that is either
810d0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a checked out or.
810e0 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 69 ** free. The fi
810f0 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f rst block has fo
81100 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68 rmat u.hdr. u.h
81110 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74 dr.size4x is 4 t
81120 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 imes the.** size
81130 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 of the allocati
81140 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 on in blocks if
81150 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
81160 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75 s free..** The u
81170 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69 .hdr.size4x&1 bi
81180 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 t is true if the
81190 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
811a0 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c d out and.** fal
811b0 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 se if the chunk
811c0 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 is on the freeli
811d0 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 st. The u.hdr.s
811e0 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69 ize4x&2 bit.** i
811f0 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72 s true if the pr
81200 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 evious chunk is
81210 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 checked out and
81220 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20 false if the.**
81230 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 previous chunk i
81240 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68 s free. The u.h
81250 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c dr.prevSize fiel
81260 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 d is the size of
81270 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 .** the previous
81280 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 chunk in blocks
81290 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 if the previous
812a0 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 chunk is on the
812b0 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66 .** freelist. If
812c0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 the previous ch
812d0 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f unk is checked o
812e0 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 ut, then.** u.hd
812f0 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62 r.prevSize can b
81300 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 e part of the da
81310 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e ta for that chun
81320 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 k and should.**
81330 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 not be read or w
81340 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 ritten..**.** We
81350 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20 often identify
81360 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69 a chunk by its i
81370 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f ndex in mem3.aPo
81380 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 ol[]. When.** t
81390 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65 his is done, the
813a0 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66 chunk index ref
813b0 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e ers to the secon
813c0 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 d block of.** th
813d0 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69 e chunk. In thi
813e0 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 74 s way, the first
813f0 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e chunk has an in
81400 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 dex of 1..** A c
81410 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20 hunk index of 0
81420 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63 means "no such c
81430 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65 hunk" and is the
81440 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f equivalent.** o
81450 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 f a NULL pointer
81460 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ..**.** The seco
81470 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65 nd block of free
81480 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68 chunks is of th
81490 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20 e form u.list.
814a0 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 The.** two field
814b0 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d s form a double-
814c0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 linked list of c
814d0 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 hunks of related
814e0 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 sizes..** Point
814f0 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 ers to the head
81500 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 of the list are
81510 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 stored in mem3.a
81520 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 iSmall[] .** for
81530 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 smaller chunks
81540 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b and mem3.aiHash[
81550 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 ] for larger chu
81560 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 nks..**.** The s
81570 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61 econd block of a
81580 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64 chunk is user d
81590 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b ata if the chunk
815a0 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20 is checked .**
815b0 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b out. If a chunk
815c0 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c is checked out,
815d0 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 6d the user data m
815e0 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a ay extend into.*
815f0 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76 * the u.hdr.prev
81600 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 Size value of th
81610 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e e following chun
81620 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 k..*/.typedef st
81630 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d ruct Mem3Block M
81640 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 em3Block;.struct
81650 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 Mem3Block {. u
81660 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 nion {. struc
81670 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72 t {. u32 pr
81680 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a evSize; /* Siz
81690 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68 e of previous ch
816a0 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b unk in Mem3Block
816b0 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 elements */.
816c0 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20 u32 size4x;
816d0 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a /* 4x the siz
816e0 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75 e of current chu
816f0 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 nk in Mem3Block
81700 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 elements */.
81710 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63 } hdr;. struc
81720 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65 t {. u32 ne
81730 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 xt; /* Ind
81740 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c ex in mem3.aPool
81750 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 [] of next free
81760 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75 chunk */. u
81770 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 32 prev; /
81780 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e * Index in mem3.
81790 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 aPool[] of previ
817a0 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a ous free chunk *
817b0 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 /. } list;.
817c0 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 } u;.};../*.** A
817d0 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 ll of the static
817e0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 variables used
817f0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 by this module a
81800 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 re collected.**
81810 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 into a single st
81820 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d ructure named "m
81830 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 74 em3". This is t
81840 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 o keep the.** st
81850 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f atic variables o
81860 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 rganized and to
81870 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 reduce namespace
81880 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 pollution.** wh
81890 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 en this module i
818a0 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 s combined with
818b0 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 other in the ama
818c0 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 lgamation..*/.st
818d0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
818e0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 struct Mem3Globa
818f0 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 l {. /*. ** Me
81900 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 mory available f
81910 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e or allocation. n
81920 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65 Pool is the size
81930 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 20 of the array.
81940 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b ** (in Mem3Block
81950 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 s) pointed to by
81960 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 aPool less 2..
81970 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b */. u32 nPool;
81980 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 . Mem3Block *aP
81990 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ool;.. /*. **
819a0 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 65 True if we are e
819b0 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74 valuating an out
819c0 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 -of-memory callb
819d0 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 ack.. */. int
819e0 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 alarmBusy;. .
819f0 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f /*. ** Mutex to
81a00 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 control access
81a10 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c to the memory al
81a20 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
81a30 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 em.. */. sqlit
81a40 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b e3_mutex *mutex;
81a50 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 . . /*. ** Th
81a60 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 e minimum amount
81a70 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 74 of free space t
81a80 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 6e hat we have seen
81a90 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d .. */. u32 mnM
81aa0 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a aster;.. /*. *
81ab0 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 65 * iMaster is the
81ac0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 index of the ma
81ad0 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 ster chunk. Mos
81ae0 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e t new allocation
81af0 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 s. ** occur off
81b00 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20 of this chunk.
81b10 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 szMaster is the
81b20 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c size (in Mem3Bl
81b30 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68 ocks). ** of th
81b40 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72 e current master
81b50 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 20 . iMaster is 0
81b60 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 if there is not
81b70 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20 master chunk..
81b80 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 68 ** The master ch
81b90 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 unk is not in ei
81ba0 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 5b ther the aiHash[
81bb0 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a ] or aiSmall[]..
81bc0 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74 */. u32 iMast
81bd0 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74 er;. u32 szMast
81be0 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 er;.. /*. ** A
81bf0 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66 rray of lists of
81c00 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63 free blocks acc
81c10 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c ording to the bl
81c20 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66 ock size . ** f
81c30 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b or smaller chunk
81c40 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20 s, or a hash on
81c50 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66 the block size f
81c60 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63 or larger. ** c
81c70 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 hunks.. */. u3
81c80 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 2 aiSmall[MX_SMA
81c90 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 LL-1]; /* For
81ca0 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 sizes 2 through
81cb0 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 MX_SMALL, inclus
81cc0 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48 ive */. u32 aiH
81cd0 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 ash[N_HASH];
81ce0 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 /* For sizes
81cf0 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 MX_SMALL+1 and
81d00 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 larger */.} mem3
81d10 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b = { 97535575 };
81d20 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47 ..#define mem3 G
81d30 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d LOBAL(struct Mem
81d40 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 3Global, mem3)..
81d50 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 /*.** Unlink the
81d60 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 chunk at mem3.a
81d70 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 Pool[i] from lis
81d80 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c t it is currentl
81d90 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 y.** on. *pRoot
81da0 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 61 is the list tha
81db0 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20 t i is a member
81dc0 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f of..*/.static vo
81dd0 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b id memsys3Unlink
81de0 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20 FromList(u32 i,
81df0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 u32 *pRoot){. u
81e00 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 32 next = mem3.a
81e10 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[i].u.list.n
81e20 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20 ext;. u32 prev
81e30 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e = mem3.aPool[i].
81e40 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61 u.list.prev;. a
81e50 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
81e60 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
81e70 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
81e80 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 rev==0 ){. *p
81e90 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d Root = next;. }
81ea0 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 else{. mem3.a
81eb0 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 Pool[prev].u.lis
81ec0 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 t.next = next;.
81ed0 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b }. if( next ){
81ee0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
81ef0 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 next].u.list.pre
81f00 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20 v = prev;. }.
81f10 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
81f20 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 list.next = 0;.
81f30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 mem3.aPool[i].u
81f40 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a .list.prev = 0;.
81f50 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 }../*.** Unlink
81f60 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 the chunk at ind
81f70 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 ex i from .** wh
81f80 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63 atever list is c
81f90 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65 urrently a membe
81fa0 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 r of..*/.static
81fb0 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 void memsys3Unli
81fc0 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 nk(u32 i){. u32
81fd0 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 size, hash;. a
81fe0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
81ff0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
82000 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
82010 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
82020 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
82030 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 & 1)==0 );. as
82040 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 sert( i>=1 );.
82050 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
82060 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
82070 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 e4x/4;. assert(
82080 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f size==mem3.aPoo
82090 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 l[i+size-1].u.hd
820a0 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 r.prevSize );.
820b0 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 assert( size>=2
820c0 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d );. if( size <=
820d0 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 MX_SMALL ){.
820e0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
820f0 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e omList(i, &mem3.
82100 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 aiSmall[size-2])
82110 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 ;. }else{. h
82120 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 ash = size % N_H
82130 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 ASH;. memsys3
82140 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 UnlinkFromList(i
82150 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
82160 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ash]);. }.}../*
82170 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 .** Link the chu
82180 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c nk at mem3.aPool
82190 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f [i] so that is o
821a0 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 n the list roote
821b0 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a d.** at *pRoot..
821c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
821d0 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 emsys3LinkIntoLi
821e0 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 st(u32 i, u32 *p
821f0 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28 Root){. assert(
82200 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
82210 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
82220 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b );. mem3.aPool[
82230 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d i].u.list.next =
82240 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e *pRoot;. mem3.
82250 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
82260 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20 prev = 0;. if(
82270 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65 *pRoot ){. me
82280 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d m3.aPool[*pRoot]
82290 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 .u.list.prev = i
822a0 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d ;. }. *pRoot =
822b0 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e i;.}../*.** Lin
822c0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 k the chunk at i
822d0 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68 ndex i into eith
822e0 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 er the appropria
822f0 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e te.** small chun
82300 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20 k list, or into
82310 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 the large chunk
82320 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 hash table..*/.s
82330 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
82340 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 s3Link(u32 i){.
82350 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b u32 size, hash;
82360 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
82370 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
82380 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 m3.mutex) );. a
82390 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 ssert( i>=1 );.
823a0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 assert( (mem3.a
823b0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
823c0 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 size4x & 1)==0 )
823d0 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e ;. size = mem3.
823e0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
823f0 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 .size4x/4;. ass
82400 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e ert( size==mem3.
82410 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e aPool[i+size-1].
82420 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 u.hdr.prevSize )
82430 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
82440 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a >=2 );. if( siz
82450 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b e <= MX_SMALL ){
82460 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b . memsys3Link
82470 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d IntoList(i, &mem
82480 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 3.aiSmall[size-2
82490 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ]);. }else{.
824a0 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e hash = size % N
824b0 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 _HASH;. memsy
824c0 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 s3LinkIntoList(i
824d0 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
824e0 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ash]);. }.}../*
824f0 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 .** If the STATI
82500 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e C_MEM mutex is n
82510 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c ot already held,
82520 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 obtain it now.
82530 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c The mutex.** wil
82540 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c l already be hel
82550 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 d (obtained by c
82560 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 ode in malloc.c)
82570 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c if.** sqlite3Gl
82580 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 obalConfig.bMemS
82590 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a tat is true..*/.
825a0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
825b0 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a ys3Enter(void){.
825c0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
825d0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
825e0 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75 at==0 && mem3.mu
825f0 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 tex==0 ){. me
82600 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 m3.mutex = sqlit
82610 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
82620 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
82630 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c _MEM);. }. sql
82640 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
82650 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a (mem3.mutex);.}.
82660 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
82670 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a ys3Leave(void){.
82680 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
82690 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 leave(mem3.mutex
826a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c );.}../*.** Call
826b0 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 ed when we are u
826c0 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 nable to satisfy
826d0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f an allocation o
826e0 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 f nBytes..*/.sta
826f0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
82700 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 OutOfMemory(int
82710 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d nByte){. if( !m
82720 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b em3.alarmBusy ){
82730 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 . mem3.alarmB
82740 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 usy = 1;. ass
82750 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
82760 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
82770 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ex) );. sqlit
82780 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
82790 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em3.mutex);.
827a0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
827b0 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20 memory(nByte);.
827c0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
827d0 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 _enter(mem3.mute
827e0 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 x);. mem3.ala
827f0 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a rmBusy = 0;. }.
82800 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 }.../*.** Chunk
82810 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 6e i is a free chun
82820 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 k that has been
82830 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 unlinked. Adjus
82840 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70 t its .** size p
82850 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 arameters for ch
82860 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 eck-out and retu
82870 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
82880 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72 the .** user por
82890 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e tion of the chun
828a0 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 k..*/.static voi
828b0 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f d *memsys3Checko
828c0 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42 ut(u32 i, u32 nB
828d0 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a lock){. u32 x;.
828e0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
828f0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
82900 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
82910 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 sert( i>=1 );.
82920 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f assert( mem3.aPo
82930 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
82940 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 ze4x/4==nBlock )
82950 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 ;. assert( mem3
82960 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
82970 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
82980 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 e==nBlock );. x
82990 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
829a0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 1].u.hdr.size4x;
829b0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d . mem3.aPool[i-
829c0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
829d0 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c = nBlock*4 | 1 |
829e0 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 (x&2);. mem3.a
829f0 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d Pool[i+nBlock-1]
82a00 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
82a10 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 = nBlock;. mem3
82a20 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
82a30 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
82a40 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 |= 2;. return &
82a50 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d mem3.aPool[i];.}
82a60 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 ../*.** Carve a
82a70 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 piece off of the
82a80 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 end of the mem3
82a90 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 .iMaster free ch
82aa0 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 unk..** Return a
82ab0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
82ac0 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 new allocation.
82ad0 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74 Or, if the mast
82ae0 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e er chunk.** is n
82af0 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c ot large enough,
82b00 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 return 0..*/.st
82b10 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 atic void *memsy
82b20 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32 s3FromMaster(u32
82b30 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 nBlock){. asse
82b40 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
82b50 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
82b60 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
82b70 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e mem3.szMaster>=n
82b80 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e Block );. if( n
82b90 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 Block>=mem3.szMa
82ba0 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a ster-1 ){. /*
82bb0 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 Use the entire
82bc0 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f master */. vo
82bd0 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43 id *p = memsys3C
82be0 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 heckout(mem3.iMa
82bf0 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 ster, mem3.szMas
82c00 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 ter);. mem3.i
82c10 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
82c20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 mem3.szMaster =
82c30 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 0;. mem3.mnMa
82c40 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 ster = 0;. re
82c50 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b turn p;. }else{
82c60 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 . /* Split th
82c70 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 e master block.
82c80 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c Return the tail
82c90 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77 . */. u32 new
82ca0 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d i, x;. newi =
82cb0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20 mem3.iMaster +
82cc0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20 mem3.szMaster -
82cd0 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 nBlock;. asse
82ce0 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e rt( newi > mem3.
82cf0 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20 iMaster+1 );.
82d00 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
82d10 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
82d20 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
82d30 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 prevSize = nBloc
82d40 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f k;. mem3.aPoo
82d50 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d l[mem3.iMaster+m
82d60 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e em3.szMaster-1].
82d70 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 u.hdr.size4x |=
82d80 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 2;. mem3.aPoo
82d90 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e l[newi-1].u.hdr.
82da0 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a size4x = nBlock*
82db0 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 4 + 1;. mem3.
82dc0 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f szMaster -= nBlo
82dd0 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ck;. mem3.aPo
82de0 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 ol[newi-1].u.hdr
82df0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 .prevSize = mem3
82e00 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78 .szMaster;. x
82e10 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 = mem3.aPool[me
82e20 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e m3.iMaster-1].u.
82e30 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a hdr.size4x & 2;.
82e40 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
82e50 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
82e60 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 .hdr.size4x = me
82e70 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 m3.szMaster*4 |
82e80 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e x;. if( mem3.
82e90 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e szMaster < mem3.
82ea0 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 mnMaster ){.
82eb0 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 mem3.mnMaster
82ec0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b = mem3.szMaster;
82ed0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
82ee0 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 n (void*)&mem3.a
82ef0 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a Pool[newi];. }.
82f00 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 }../*.** *pRoot
82f10 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 is the head of a
82f20 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 68 list of free ch
82f30 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65 unks of the same
82f40 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 size.** or same
82f50 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 size hash. In
82f60 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 other words, *pR
82f70 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20 oot is an entry
82f80 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d in either.** mem
82f90 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 3.aiSmall[] or m
82fa0 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a em3.aiHash[]. .
82fb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
82fc0 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20 ne examines all
82fd0 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67 entries on the g
82fe0 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72 iven list and tr
82ff0 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 ies.** to coales
83000 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 20 ce each entries
83010 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72 with adjacent fr
83020 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a ee chunks. .**.
83030 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 20 ** If it sees a
83040 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61 chunk that is la
83050 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69 rger than mem3.i
83060 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61 Master, it repla
83070 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72 ces .** the curr
83080 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ent mem3.iMaster
83090 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61 with the new la
830a0 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20 rger chunk. In
830b0 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69 order for.** thi
830c0 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72 s mem3.iMaster r
830d0 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f eplacement to wo
830e0 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63 rk, the master c
830f0 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20 hunk must be.**
83100 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 linked into the
83110 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68 hash tables. Th
83120 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f at is not the no
83130 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a rmal state of.**
83140 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75 affairs, of cou
83150 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e rse. The callin
83160 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c g routine must l
83170 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a ink the master.*
83180 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 * chunk before i
83190 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 nvoking this rou
831a0 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 tine, then must
831b0 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 unlink the (poss
831c0 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29 ibly.** changed)
831d0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e master chunk on
831e0 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ce this routine
831f0 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f has finished..*/
83200 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
83210 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70 sys3Merge(u32 *p
83220 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65 Root){. u32 iNe
83230 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20 xt, prev, size,
83240 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28 i, x;.. assert(
83250 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
83260 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
83270 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f );. for(i=*pRoo
83280 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29 t; i>0; i=iNext)
83290 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 {. iNext = me
832a0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 m3.aPool[i].u.li
832b0 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a st.next;. siz
832c0 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 e = mem3.aPool[i
832d0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
832e0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73 ;. assert( (s
832f0 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20 ize&1)==0 );.
83300 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30 if( (size&2)==0
83310 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 ){. memsys
83320 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 3UnlinkFromList(
83330 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 i, pRoot);.
83340 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d assert( i > mem
83350 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
83360 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 dr.prevSize );.
83370 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20 prev = i -
83380 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
83390 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a u.hdr.prevSize;.
833a0 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d if( prev==
833b0 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 iNext ){.
833c0 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 iNext = mem3.aP
833d0 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 ool[prev].u.list
833e0 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 .next;. }.
833f0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
83400 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 nk(prev);.
83410 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f size = i + size/
83420 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20 4 - prev;.
83430 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 x = mem3.aPool[p
83440 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a rev-1].u.hdr.siz
83450 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d e4x & 2;. m
83460 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 em3.aPool[prev-1
83470 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d ].u.hdr.size4x =
83480 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20 size*4 | x;.
83490 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 mem3.aPool[pr
834a0 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 ev+size-1].u.hdr
834b0 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 .prevSize = size
834c0 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c ;. memsys3L
834d0 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 ink(prev);.
834e0 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d i = prev;. }
834f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 else{. size
83500 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 /= 4;. }.
83510 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 if( size>mem3.s
83520 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 zMaster ){.
83530 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 mem3.iMaster =
83540 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a i;. mem3.sz
83550 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20 Master = size;.
83560 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
83570 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b * Return a block
83580 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 of memory of at
83590 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e least nBytes in
835a0 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e size..** Return
835b0 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e NULL if unable.
835c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
835d0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
835e0 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 t the necessary
835f0 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c mutexes, if any,
83600 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 are.** already
83610 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c held by the call
83620 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 er. Hence "Unsaf
83630 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e"..*/.static vo
83640 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f id *memsys3Mallo
83650 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 cUnsafe(int nByt
83660 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75 e){. u32 i;. u
83670 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32 32 nBlock;. u32
83680 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65 toFree;.. asse
83690 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
836a0 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
836b0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
836c0 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b sizeof(Mem3Block
836d0 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42 )==8 );. if( nB
836e0 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e yte<=12 ){. n
836f0 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c Block = 2;. }el
83700 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d se{. nBlock =
83710 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b (nByte + 11)/8;
83720 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e . }. assert( n
83730 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f Block>=2 );.. /
83740 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c * STEP 1:. ** L
83750 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 ook for an entry
83760 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 of the correct
83770 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74 size in either t
83780 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 he small. ** ch
83790 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20 unk table or in
837a0 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 the large chunk
837b0 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 hash table. Thi
837c0 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73 s is. ** succes
837d0 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65 sful most of the
837e0 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74 time (about 9 t
837f0 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e imes out of 10).
83800 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f . */. if( nBlo
83810 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 ck <= MX_SMALL )
83820 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61 {. i = mem3.a
83830 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d iSmall[nBlock-2]
83840 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b ;. if( i>0 ){
83850 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e . memsys3Un
83860 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 linkFromList(i,
83870 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 &mem3.aiSmall[nB
83880 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20 lock-2]);.
83890 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 return memsys3Ch
838a0 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b eckout(i, nBlock
838b0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
838c0 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d {. int hash =
838d0 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48 nBlock % N_HASH
838e0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33 ;. for(i=mem3
838f0 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69 .aiHash[hash]; i
83900 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c >0; i=mem3.aPool
83910 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 [i].u.list.next)
83920 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 {. if( mem3
83930 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
83940 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f r.size4x/4==nBlo
83950 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 ck ){. me
83960 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c msys3UnlinkFromL
83970 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 ist(i, &mem3.aiH
83980 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 ash[hash]);.
83990 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 return memsy
839a0 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 s3Checkout(i, nB
839b0 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 lock);. }.
839c0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 }. }.. /* S
839d0 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 TEP 2:. ** Try
839e0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61 to satisfy the a
839f0 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 llocation by car
83a00 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 ving a piece off
83a10 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a of the end. **
83a20 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 of the master c
83a30 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 hunk. This step
83a40 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 usually works i
83a50 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a f step 1 fails..
83a60 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e */. if( mem3.
83a70 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b szMaster>=nBlock
83a80 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d ){. return m
83a90 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 emsys3FromMaster
83aa0 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a (nBlock);. }...
83ab0 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 /* STEP 3: .
83ac0 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 ** Loop through
83ad0 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f the entire memo
83ae0 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 ry pool. Coales
83af0 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 ce adjacent free
83b00 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 . ** chunks. R
83b10 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73 ecompute the mas
83b20 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65 ter chunk as the
83b30 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 68 largest free ch
83b40 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 unk.. ** Then t
83b50 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69 ry again to sati
83b60 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 sfy the allocati
83b70 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 20 on by carving a
83b80 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f piece off. ** o
83b90 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
83ba0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 master chunk.
83bb0 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 6e This step happen
83bc0 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 s very. ** rare
83bd0 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20 ly (we hope!).
83be0 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d */. for(toFree=
83bf0 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 nBlock*16; toFre
83c00 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 e<(mem3.nPool*16
83c10 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b ); toFree *= 2){
83c20 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f . memsys3OutO
83c30 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b fMemory(toFree);
83c40 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d . if( mem3.iM
83c50 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
83c60 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e emsys3Link(mem3.
83c70 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 iMaster);.
83c80 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 mem3.iMaster = 0
83c90 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d ;. mem3.szM
83ca0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d aster = 0;. }
83cb0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
83cc0 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 N_HASH; i++){.
83cd0 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 memsys3Merge
83ce0 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d (&mem3.aiHash[i]
83cf0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
83d00 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c (i=0; i<MX_SMALL
83d10 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 -1; i++){.
83d20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 memsys3Merge(&me
83d30 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a m3.aiSmall[i]);.
83d40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 }. if( me
83d50 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 m3.szMaster ){.
83d60 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
83d70 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 nk(mem3.iMaster)
83d80 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 ;. if( mem3
83d90 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 .szMaster>=nBloc
83da0 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 k ){. ret
83db0 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d urn memsys3FromM
83dc0 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 aster(nBlock);.
83dd0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
83de0 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f .. /* If none o
83df0 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b f the above work
83e00 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c ed, then we fail
83e10 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b . */. return 0;
83e20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
83e30 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 n outstanding me
83e40 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e mory allocation.
83e50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
83e60 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
83e70 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 t the necessary
83e80 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c mutexes, if any,
83e90 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 are.** already
83ea0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c held by the call
83eb0 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 er. Hence "Unsaf
83ec0 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 e"..*/.void mems
83ed0 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f ys3FreeUnsafe(vo
83ee0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d id *pOld){. Mem
83ef0 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 3Block *p = (Mem
83f00 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 3Block*)pOld;.
83f10 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a int i;. u32 siz
83f20 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 e, x;. assert(
83f30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
83f40 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 ld(mem3.mutex) )
83f50 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65 ;. assert( p>me
83f60 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d m3.aPool && p<&m
83f70 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e em3.aPool[mem3.n
83f80 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70 Pool] );. i = p
83f90 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 - mem3.aPool;.
83fa0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 assert( (mem3.a
83fb0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
83fc0 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a size4x&1)==1 );.
83fd0 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 size = mem3.aP
83fe0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
83ff0 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 ize4x/4;. asser
84000 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e t( i+size<=mem3.
84010 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d nPool+1 );. mem
84020 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
84030 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b dr.size4x &= ~1;
84040 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b . mem3.aPool[i+
84050 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 size-1].u.hdr.pr
84060 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 evSize = size;.
84070 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 mem3.aPool[i+si
84080 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 ze-1].u.hdr.size
84090 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 4x &= ~2;. mems
840a0 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f ys3Link(i);.. /
840b0 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20 * Try to expand
840c0 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67 the master using
840d0 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 the newly freed
840e0 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 chunk */. if(
840f0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a mem3.iMaster ){.
84100 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 while( (mem3
84110 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
84120 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
84130 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 e4x&2)==0 ){.
84140 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 size = mem3.a
84150 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
84160 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 r-1].u.hdr.prevS
84170 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e ize;. mem3.
84180 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b iMaster -= size;
84190 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 . mem3.szMa
841a0 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 ster += size;.
841b0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
841c0 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b k(mem3.iMaster);
841d0 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e . x = mem3.
841e0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
841f0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
84200 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 4x & 2;. me
84210 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
84220 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
84230 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d ize4x = mem3.szM
84240 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 aster*4 | x;.
84250 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 mem3.aPool[me
84260 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e m3.iMaster+mem3.
84270 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 szMaster-1].u.hd
84280 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d r.prevSize = mem
84290 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 3.szMaster;.
842a0 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 }. x = mem3.a
842b0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
842c0 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
842d0 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65 x & 2;. while
842e0 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 ( (mem3.aPool[me
842f0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e m3.iMaster+mem3.
84300 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 szMaster-1].u.hd
84310 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29 r.size4x&1)==0 )
84320 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 {. memsys3U
84330 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 nlink(mem3.iMast
84340 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 er+mem3.szMaster
84350 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a );. mem3.sz
84360 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 Master += mem3.a
84370 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
84380 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d r+mem3.szMaster-
84390 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
843a0 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 4;. mem3.aP
843b0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
843c0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
843d0 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = mem3.szMaster
843e0 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 *4 | x;. me
843f0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
84400 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 aster+mem3.szMas
84410 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ter-1].u.hdr.pre
84420 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d vSize = mem3.szM
84430 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d aster;. }. }
84440 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
84450 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 the size of an
84460 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f outstanding allo
84470 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 cation, in bytes
84480 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 . The.** size r
84490 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 eturned omits th
844a0 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 e 8-byte header
844b0 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 overhead. This
844c0 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f only.** works fo
844d0 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 r chunks that ar
844e0 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 e currently chec
844f0 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 ked out..*/.stat
84500 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69 ic int memsys3Si
84510 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d ze(void *p){. M
84520 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b em3Block *pBlock
84530 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
84540 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 eturn 0;. pBloc
84550 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 k = (Mem3Block*)
84560 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 p;. assert( (pB
84570 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 lock[-1].u.hdr.s
84580 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20 ize4x&1)!=0 );.
84590 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b return (pBlock[
845a0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
845b0 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f &~3)*2 - 4;.}../
845c0 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 *.** Round up a
845d0 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 request size to
845e0 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 the next valid a
845f0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a llocation size..
84600 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
84610 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74 msys3Roundup(int
84620 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32 n){. if( n<=12
84630 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ){. return 1
84640 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2;. }else{.
84650 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e return ((n+11)&~
84660 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 7) - 4;. }.}../
84670 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 *.** Allocate nB
84680 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a ytes of memory..
84690 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
846a0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e memsys3Malloc(in
846b0 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c t nBytes){. sql
846c0 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 ite3_int64 *p;.
846d0 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e assert( nBytes>
846e0 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 0 ); /*
846f0 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72 malloc.c filter
84700 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65 71 s out 0 byte req
84710 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 uests */. memsy
84720 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d s3Enter();. p =
84730 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e memsys3MallocUn
84740 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 safe(nBytes);.
84750 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a memsys3Leave();.
84760 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
84770 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p; .}../*.** Fre
84780 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 e memory..*/.voi
84790 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f d memsys3Free(vo
847a0 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 id *pPrior){. a
847b0 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b ssert( pPrior );
847c0 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 . memsys3Enter(
847d0 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 );. memsys3Free
847e0 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a Unsafe(pPrior);.
847f0 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 memsys3Leave()
84800 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
84810 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e e the size of an
84820 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 existing memory
84830 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 allocation.*/.v
84840 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c oid *memsys3Real
84850 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 loc(void *pPrior
84860 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 , int nBytes){.
84870 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 int nOld;. voi
84880 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 d *p;. if( pPri
84890 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 or==0 ){. ret
848a0 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c urn sqlite3_mall
848b0 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a oc(nBytes);. }.
848c0 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 if( nBytes<=0
848d0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
848e0 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20 ree(pPrior);.
848f0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
84900 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53 nOld = memsys3S
84910 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 ize(pPrior);. i
84920 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 f( nBytes<=nOld
84930 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d && nBytes>=nOld-
84940 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72 128 ){. retur
84950 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 n pPrior;. }.
84960 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a memsys3Enter();.
84970 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c p = memsys3Mal
84980 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 locUnsafe(nBytes
84990 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 );. if( p ){.
849a0 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 if( nOld<nByte
849b0 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 s ){. memcp
849c0 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c y(p, pPrior, nOl
849d0 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 d);. }else{.
849e0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 memcpy(p, p
849f0 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a Prior, nBytes);.
84a00 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73 }. memsys
84a10 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 3FreeUnsafe(pPri
84a20 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 or);. }. memsy
84a30 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 s3Leave();. ret
84a40 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
84a50 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 Initialize this
84a60 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 module..*/.stati
84a70 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69 c int memsys3Ini
84a80 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
84a90 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
84aa0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
84ab0 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f if( !sqlite3Glo
84ac0 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20 balConfig.pHeap
84ad0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
84ae0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
84af0 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 6f . /* Store a po
84b00 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d inter to the mem
84b10 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f ory block in glo
84b20 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d 65 bal structure me
84b30 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 m3. */. assert(
84b40 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 sizeof(Mem3Bloc
84b50 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 2e k)==8 );. mem3.
84b60 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f aPool = (Mem3Blo
84b70 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 ck *)sqlite3Glob
84b80 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a alConfig.pHeap;.
84b90 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28 mem3.nPool = (
84ba0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
84bb0 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a 65 fig.nHeap / size
84bc0 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 2d of(Mem3Block)) -
84bd0 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 2;.. /* Initia
84be0 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 20 lize the master
84bf0 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33 block. */. mem3
84c00 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 .szMaster = mem3
84c10 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d .nPool;. mem3.m
84c20 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73 nMaster = mem3.s
84c30 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 2e zMaster;. mem3.
84c40 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 6d iMaster = 1;. m
84c50 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 em3.aPool[0].u.h
84c60 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 6d dr.size4x = (mem
84c70 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 2b 3.szMaster<<2) +
84c80 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 2;. mem3.aPool
84c90 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 [mem3.nPool].u.h
84ca0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 dr.prevSize = me
84cb0 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 m3.nPool;. mem3
84cc0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f .aPool[mem3.nPoo
84cd0 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 l].u.hdr.size4x
84ce0 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 = 1;.. return S
84cf0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
84d00 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 ** Deinitialize
84d10 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a this module..*/.
84d20 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
84d30 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 ys3Shutdown(void
84d40 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
84d50 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
84d60 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
84d70 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 n;.}..../*.** Op
84d80 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 en the file indi
84d90 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 cated and write
84da0 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 a log of all unf
84db0 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 reed memory .**
84dc0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f allocations into
84dd0 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 that log..*/.SQ
84de0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
84df0 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 33 d sqlite3Memsys3
84e00 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 Dump(const char
84e10 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 69 66 *zFilename){.#if
84e20 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
84e30 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 . FILE *out;.
84e40 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 33 32 20 u32 i, j;. u32
84e50 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 46 69 6c size;. if( zFil
84e60 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c ename==0 || zFil
84e70 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 ename[0]==0 ){.
84e80 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b out = stdout;
84e90 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 . }else{. ou
84ea0 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e t = fopen(zFilen
84eb0 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 ame, "w");. i
84ec0 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 f( out==0 ){.
84ed0 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
84ee0 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f r, "** Unable to
84ef0 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 output memory d
84f00 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a ebug output log:
84f10 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 %s **\n",.
84f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
84f30 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 zFilename);.
84f40 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
84f50 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e . }. memsys3En
84f60 74 65 72 28 29 3b 0a 20 20 66 70 72 69 6e 74 66 ter();. fprintf
84f70 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e (out, "CHUNKS:\n
84f80 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 ");. for(i=1; i
84f90 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b <=mem3.nPool; i+
84fa0 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69 =size/4){. si
84fb0 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
84fc0 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
84fd0 78 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f x;. if( size/
84fe0 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70 4<=1 ){. fp
84ff0 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73 rintf(out, "%p s
85000 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d ize error\n", &m
85010 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 em3.aPool[i]);.
85020 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 assert( 0 )
85030 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
85040 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 73 69 }. if( (si
85050 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d 33 ze&1)==0 && mem3
85060 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d .aPool[i+size/4-
85070 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
85080 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 20 20 e!=size/4 ){.
85090 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
850a0 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20 64 6f "%p tail size do
850b0 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e 22 2c es not match\n",
850c0 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 &mem3.aPool[i])
850d0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
850e0 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
850f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
85100 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 ((mem3.aPool[i+s
85110 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 73 ize/4-1].u.hdr.s
85120 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 3d 28 73 ize4x&2)>>1)!=(s
85130 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 20 20 20 ize&1) ){.
85140 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 fprintf(out, "%p
85150 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 74 20 62 tail checkout b
85160 69 74 20 69 73 20 69 6e 63 6f 72 72 65 63 74 5c it is incorrect\
85170 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b n", &mem3.aPool[
85180 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 i]);. asser
85190 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 t( 0 );. br
851a0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
851b0 66 28 20 73 69 7a 65 26 31 20 29 7b 0a 20 20 20 f( size&1 ){.
851c0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
851d0 22 25 70 20 25 36 64 20 62 79 74 65 73 20 63 68 "%p %6d bytes ch
851e0 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 26 6d ecked out\n", &m
851f0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 em3.aPool[i], (s
85200 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 ize/4)*8-8);.
85210 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70 }else{. fp
85220 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 rintf(out, "%p %
85230 36 64 20 62 79 74 65 73 20 66 72 65 65 25 73 5c 6d bytes free%s\
85240 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b n", &mem3.aPool[
85250 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 i], (size/4)*8-8
85260 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
85270 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 i==mem3.iMas
85280 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72 ter ? " **master
85290 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 20 20 7d **" : "");. }
852a0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
852b0 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b i<MX_SMALL-1; i+
852c0 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 +){. if( mem3
852d0 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29 .aiSmall[i]==0 )
852e0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 continue;. f
852f0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 73 6d 61 printf(out, "sma
85300 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 ll(%2d):", i);.
85310 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e for(j = mem3.
85320 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b aiSmall[i]; j>0;
85330 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d j=mem3.aPool[j]
85340 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 .u.list.next){.
85350 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
85360 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65 , " %p(%d)", &me
85370 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 m3.aPool[j],.
85380 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33 (mem3
85390 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 .aPool[j-1].u.hd
853a0 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 r.size4x/4)*8-8)
853b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 ;. }. fpri
853c0 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 ntf(out, "\n");
853d0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
853e0 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a i<N_HASH; i++){.
853f0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 48 if( mem3.aiH
85400 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 ash[i]==0 ) cont
85410 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 inue;. fprint
85420 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64 f(out, "hash(%2d
85430 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 ):", i);. for
85440 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68 (j = mem3.aiHash
85450 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 [i]; j>0; j=mem3
85460 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 .aPool[j].u.list
85470 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 .next){. fp
85480 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 rintf(out, " %p(
85490 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f %d)", &mem3.aPoo
854a0 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 l[j],.
854b0 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b (mem3.aPool[
854c0 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 j-1].u.hdr.size4
854d0 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d x/4)*8-8);. }
854e0 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
854f0 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 , "\n"); . }.
85500 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 61 fprintf(out, "ma
85510 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 ster=%d\n", mem3
85520 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 66 70 72 .iMaster);. fpr
85530 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f 77 55 73 intf(out, "nowUs
85540 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e ed=%d\n", mem3.n
85550 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a Pool*8 - mem3.sz
85560 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72 Master*8);. fpr
85570 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65 intf(out, "mxUse
85580 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 d=%d\n", mem3.nP
85590 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d ool*8 - mem3.mnM
855a0 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 71 6c 69 aster*8);. sqli
855b0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
855c0 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 69 mem3.mutex);. i
855d0 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 f( out==stdout )
855e0 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 {. fflush(std
855f0 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 out);. }else{.
85600 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a fclose(out);.
85610 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 }.#else. UNUS
85620 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69 ED_PARAMETER(zFi
85630 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a lename);.#endif.
85640 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
85650 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c utine is the onl
85660 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 y routine in thi
85670 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 s file with exte
85680 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 rnal .** linkage
85690 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 ..**.** Populate
856a0 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d the low-level m
856b0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
856c0 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 function pointe
856d0 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 rs in.** sqlite3
856e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 GlobalConfig.m w
856f0 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ith pointers to
85700 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 the routines in
85710 74 68 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a this file. The.*
85720 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 * arguments spec
85730 69 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 ify the block of
85740 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 memory to manag
85750 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
85760 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 utine is only ca
85770 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f lled by sqlite3_
85780 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68 config(), and th
85790 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f erefore.** is no
857a0 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 t required to be
857b0 20 74 68 72 65 61 64 73 61 66 65 20 28 69 74 20 threadsafe (it
857c0 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 is not)..*/.SQLI
857d0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
857e0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
857f0 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d hods *sqlite3Mem
85800 47 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 GetMemsys3(void)
85810 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
85820 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
85830 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 hods mempoolMeth
85840 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d ods = {. mem
85850 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 sys3Malloc,.
85860 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20 memsys3Free,.
85870 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f memsys3Reallo
85880 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 c,. memsys3S
85890 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 ize,. memsys
858a0 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 3Roundup,. m
858b0 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20 emsys3Init,.
858c0 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e memsys3Shutdown
858d0 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 ,. 0. };.
858e0 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d return &mempoolM
858f0 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 ethods;.}..#endi
85900 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 f /* SQLITE_ENAB
85910 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f LE_MEMSYS3 */../
85920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
85930 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a nd of mem3.c ***
85940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
85970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
85980 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 egin file mem5.c
85990 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
859a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
859b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
859c0 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 *.** 2007 Octobe
859d0 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 14.**.** The a
859e0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
859f0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
85a00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
85a10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
85a20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
85a30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
85a40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
85a50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
85a60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
85a70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
85a80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
85a90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
85aa0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
85ab0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
85ac0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
85ad0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
85ae0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
85af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85b30 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
85b40 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
85b50 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
85b60 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 mplement a memor
85b70 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y.** allocation
85b80 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 subsystem for us
85b90 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a e by SQLite. .**
85ba0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
85bb0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
85bc0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
85bd0 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a tem omits all.**
85be0 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 use of malloc()
85bf0 2e 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 . The SQLite use
85c00 72 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f r supplies a blo
85c10 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ck of memory.**
85c20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 before calling s
85c30 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
85c40 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 e() from which a
85c50 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 llocations.** ar
85c60 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 e made and retur
85c70 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c ned by the xMall
85c80 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f oc() and xReallo
85c90 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e c() .** implemen
85ca0 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 tations. Once sq
85cb0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
85cc0 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c () has been call
85cd0 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e ed,.** the amoun
85ce0 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 t of memory avai
85cf0 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 lable to SQLite
85d00 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e is fixed and can
85d10 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 not.** be change
85d20 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 d..**.** This ve
85d30 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d rsion of the mem
85d40 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
85d50 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c ubsystem is incl
85d60 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 uded.** in the b
85d70 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c uild only if SQL
85d80 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
85d90 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a S5 is defined..*
85da0 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 35 2e 63 *.** $Id: mem5.c
85db0 2c 76 20 31 2e 31 39 20 32 30 30 38 2f 31 31 2f ,v 1.19 2008/11/
85dc0 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 19 16:52:44 dani
85dd0 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
85de0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
85df0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
85e00 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ry allocator is
85e10 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a used only when .
85e20 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ** SQLITE_ENABLE
85e30 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 _MEMSYS5 is defi
85e40 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ned..*/.#ifdef S
85e50 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
85e60 53 59 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 SYS5../*.** A mi
85e70 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e nimum allocation
85e80 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
85e90 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
85ea0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c structure..** L
85eb0 61 72 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e arger allocation
85ec0 73 20 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f s are an array o
85ed0 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
85ee0 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 es where the.**
85ef0 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 size of the arra
85f00 79 20 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20 y is a power of
85f10 32 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 2..*/.typedef st
85f20 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 ruct Mem5Link Me
85f30 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d m5Link;.struct M
85f40 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 em5Link {. int
85f50 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 next; /* I
85f60 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65 ndex of next fre
85f70 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 e chunk */. int
85f80 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 prev; /*
85f90 49 6e 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75 Index of previou
85fa0 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a s free chunk */.
85fb0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 };../*.** Maximu
85fc0 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c m size of any al
85fd0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c location is ((1<
85fe0 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 6e 41 <LOGMAX)*mem5.nA
85ff0 74 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d tom). Since.** m
86000 65 6d 35 2e 6e 41 74 6f 6d 20 69 73 20 61 6c 77 em5.nAtom is alw
86010 61 79 73 20 61 74 20 6c 65 61 73 74 20 38 2c 20 ays at least 8,
86020 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c this is not real
86030 6c 79 20 61 20 70 72 61 63 74 69 63 61 6c 0a 2a ly a practical.*
86040 2a 20 6c 69 6d 69 74 61 74 69 6f 6e 2e 0a 2a 2f * limitation..*/
86050 0a 23 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 .#define LOGMAX
86060 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 30../*.** Masks
86070 75 73 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 used for mem5.aC
86080 74 72 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a trl[] elements..
86090 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f */.#define CTRL_
860a0 4c 4f 47 53 49 5a 45 20 20 30 78 31 66 20 20 20 LOGSIZE 0x1f
860b0 20 2f 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 /* Log2 Size of
860c0 20 74 68 69 73 20 62 6c 6f 63 6b 20 72 65 6c 61 this block rela
860d0 74 69 76 65 20 74 6f 20 50 4f 57 32 5f 4d 49 4e tive to POW2_MIN
860e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c */.#define CTRL
860f0 5f 46 52 45 45 20 20 20 20 20 30 78 32 30 20 20 _FREE 0x20
86100 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 /* True if not
86110 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
86120 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ./*.** All of th
86130 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c e static variabl
86140 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 es used by this
86150 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 module are colle
86160 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 cted.** into a s
86170 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 ingle structure
86180 6e 61 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 named "mem5". T
86190 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 his is to keep t
861a0 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 he.** static var
861b0 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 iables organized
861c0 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e and to reduce n
861d0 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 amespace polluti
861e0 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 on.** when this
861f0 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e module is combin
86200 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e ed with other in
86210 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f the amalgamatio
86220 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c n..*/.static SQL
86230 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d ITE_WSD struct M
86240 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a em5Global {. /*
86250 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 . ** Memory ava
86260 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 ilable for alloc
86270 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 ation. */. int
86280 20 6e 41 74 6f 6d 3b 20 20 20 20 20 20 20 2f 2a nAtom; /*
86290 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 Smallest possib
862a0 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e le allocation in
862b0 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 bytes */. int
862c0 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 nBlock; /*
862d0 4e 75 6d 62 65 72 20 6f 66 20 6e 41 74 6f 6d 20 Number of nAtom
862e0 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20 sized blocks in
862f0 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a zPool */. u8 *z
86300 50 6f 6f 6c 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 Pool;. . /*.
86310 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 ** Mutex to cont
86320 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 rol access to th
86330 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
86340 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 ion subsystem..
86350 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
86360 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f tex *mutex;.. /
86370 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e *. ** Performan
86380 63 65 20 73 74 61 74 69 73 74 69 63 73 0a 20 20 ce statistics.
86390 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b */. u64 nAlloc;
863a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
863b0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c l number of call
863c0 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 s to malloc */.
863d0 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b u64 totalAlloc;
863e0 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 /* Total of
863f0 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c all malloc call
86400 73 20 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74 s - includes int
86410 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20 ernal frag */.
86420 75 36 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b u64 totalExcess;
86430 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 /* Total int
86440 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 ernal fragmentat
86450 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 ion */. u32 cur
86460 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20 rentOut; /*
86470 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 Current checkout
86480 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 , including inte
86490 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 rnal fragmentati
864a0 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 on */. u32 curr
864b0 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 entCount; /* C
864c0 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 urrent number of
864d0 20 64 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f distinct checko
864e0 75 74 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 uts */. u32 max
864f0 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Out; /*
86500 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 Maximum instanta
86510 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74 neous currentOut
86520 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75 */. u32 maxCou
86530 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 nt; /* Max
86540 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f imum instantaneo
86550 75 73 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 us currentCount
86560 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75 */. u32 maxRequ
86570 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67 est; /* Larg
86580 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 est allocation (
86590 65 78 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74 exclusive of int
865a0 65 72 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20 ernal frag) */.
865b0 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 . /*. ** List
865c0 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 s of free blocks
865d0 20 6f 66 20 76 61 72 69 6f 75 73 20 73 69 7a 65 of various size
865e0 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 s.. */. int ai
865f0 46 72 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b Freelist[LOGMAX+
86600 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 1];.. /*. ** S
86610 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e pace for trackin
86620 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 g which blocks a
86630 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 re checked out a
86640 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a nd the size. **
86650 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 of each block.
86660 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c One byte per bl
86670 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a ock.. */. u8 *
86680 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d aCtrl;..} mem5 =
86690 20 7b 20 31 39 38 30 34 31 36 37 20 7d 3b 0a 0a { 19804167 };..
866a0 23 64 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c 4f #define mem5 GLO
866b0 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 47 BAL(struct Mem5G
866c0 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 23 64 lobal, mem5)..#d
866d0 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 efine MEM5LINK(i
866e0 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a dx) ((Mem5Link *
866f0 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 )(&mem5.zPool[(i
86700 64 78 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 29 dx)*mem5.nAtom])
86710 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 )../*.** Unlink
86720 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d the chunk at mem
86730 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 5.aPool[i] from
86740 6c 69 73 74 20 69 74 20 69 73 20 63 75 72 72 65 list it is curre
86750 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 ntly.** on. It
86760 73 68 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 should be found
86770 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 on mem5.aiFreeli
86780 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f st[iLogsize]..*/
86790 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
867a0 73 79 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 sys5Unlink(int i
867b0 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b , int iLogsize){
867c0 0a 20 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 . int next, pre
867d0 76 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d v;. assert( i>=
867e0 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 0 && i<mem5.nBlo
867f0 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ck );. assert(
86800 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 iLogsize>=0 && i
86810 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 Logsize<=LOGMAX
86820 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 );. assert( (me
86830 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 m5.aCtrl[i] & CT
86840 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f RL_LOGSIZE)==iLo
86850 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74 gsize );.. next
86860 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e = MEM5LINK(i)->
86870 6e 65 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d next;. prev = M
86880 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 EM5LINK(i)->prev
86890 3b 0a 20 20 69 66 28 20 70 72 65 76 3c 30 20 29 ;. if( prev<0 )
868a0 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 {. mem5.aiFre
868b0 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 elist[iLogsize]
868c0 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b = next;. }else{
868d0 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72 . MEM5LINK(pr
868e0 65 76 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 ev)->next = next
868f0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 ;. }. if( next
86900 3e 3d 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c >=0 ){. MEM5L
86910 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 20 INK(next)->prev
86920 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f = prev;. }.}../
86930 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 *.** Link the ch
86940 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f unk at mem5.aPoo
86950 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 l[i] so that is
86960 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a on the iLogsize.
86970 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f ** free list..*/
86980 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
86990 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20 sys5Link(int i,
869a0 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 int iLogsize){.
869b0 20 69 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 74 int x;. assert
869c0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
869d0 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29 held(mem5.mutex)
869e0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e );. assert( i>
869f0 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c =0 && i<mem5.nBl
86a00 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ock );. assert(
86a10 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 iLogsize>=0 &&
86a20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 iLogsize<=LOGMAX
86a30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d );. assert( (m
86a40 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 em5.aCtrl[i] & C
86a50 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c TRL_LOGSIZE)==iL
86a60 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d ogsize );.. x =
86a70 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 MEM5LINK(i)->ne
86a80 78 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 xt = mem5.aiFree
86a90 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a list[iLogsize];.
86aa0 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 MEM5LINK(i)->p
86ab0 72 65 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 rev = -1;. if(
86ac0 78 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 x>=0 ){. asse
86ad0 72 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 rt( x<mem5.nBloc
86ae0 6b 20 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e k );. MEM5LIN
86af0 4b 28 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a K(x)->prev = i;.
86b00 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65 }. mem5.aiFre
86b10 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 elist[iLogsize]
86b20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 = i;.}../*.** If
86b30 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 the STATIC_MEM
86b40 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 mutex is not alr
86b50 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 eady held, obtai
86b60 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 n it now. The mu
86b70 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 tex.** will alre
86b80 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 ady be held (obt
86b90 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e ained by code in
86ba0 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a malloc.c) if.**
86bb0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
86bc0 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 nfig.bMemStat is
86bd0 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 true..*/.static
86be0 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 void memsys5Ent
86bf0 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 er(void){. if(
86c00 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
86c10 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 fig.bMemstat==0
86c20 26 26 20 6d 65 6d 35 2e 6d 75 74 65 78 3d 3d 30 && mem5.mutex==0
86c30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74 ){. mem5.mut
86c40 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
86c50 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
86c60 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b TEX_STATIC_MEM);
86c70 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
86c80 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e utex_enter(mem5.
86c90 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 mutex);.}.static
86ca0 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 65 61 void memsys5Lea
86cb0 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 ve(void){. sqli
86cc0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
86cd0 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a mem5.mutex);.}..
86ce0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
86cf0 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 size of an outs
86d00 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 tanding allocati
86d10 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 on, in bytes. T
86d20 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 he.** size retur
86d30 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d ned omits the 8-
86d40 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 byte header over
86d50 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 head. This only
86d60 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 .** works for ch
86d70 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 unks that are cu
86d80 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 rrently checked
86d90 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 out..*/.static i
86da0 6e 74 20 6d 65 6d 73 79 73 35 53 69 7a 65 28 76 nt memsys5Size(v
86db0 6f 69 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 oid *p){. int i
86dc0 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 Size = 0;. if(
86dd0 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d p ){. int i =
86de0 20 28 28 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a ((u8 *)p-mem5.z
86df0 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d Pool)/mem5.nAtom
86e00 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e ;. assert( i>
86e10 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c =0 && i<mem5.nBl
86e20 6f 63 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 ock );. iSize
86e30 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 2a 20 = mem5.nAtom *
86e40 28 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 (1 << (mem5.aCtr
86e50 6c 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a l[i]&CTRL_LOGSIZ
86e60 45 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 E));. }. retur
86e70 6e 20 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a n iSize;.}../*.*
86e80 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 * Find the first
86e90 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 entry on the fr
86ea0 65 65 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e eelist iLogsize.
86eb0 20 20 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a Unlink that.**
86ec0 20 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 entry and retur
86ed0 6e 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f n its index. .*/
86ee0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 .static int mems
86ef0 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 ys5UnlinkFirst(i
86f00 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 nt iLogsize){.
86f10 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 int i;. int iFi
86f20 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rst;.. assert(
86f30 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 iLogsize>=0 && i
86f40 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 Logsize<=LOGMAX
86f50 29 3b 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 );. i = iFirst
86f60 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 = mem5.aiFreelis
86f70 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 t[iLogsize];. a
86f80 73 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 ssert( iFirst>=0
86f90 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 );. while( i>0
86fa0 20 29 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 ){. if( i<iF
86fb0 69 72 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 irst ) iFirst =
86fc0 69 3b 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c i;. i = MEM5L
86fd0 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 INK(i)->next;.
86fe0 7d 0a 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e }. memsys5Unlin
86ff0 6b 28 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 k(iFirst, iLogsi
87000 7a 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 ze);. return iF
87010 69 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 irst;.}../*.** R
87020 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 eturn a block of
87030 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 memory of at le
87040 61 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 ast nBytes in si
87050 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 ze..** Return NU
87060 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2f LL if unable..*/
87070 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
87080 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 msys5MallocUnsaf
87090 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 e(int nByte){.
870a0 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
870b0 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d /* Index of a m
870c0 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 em5.aPool[] slot
870d0 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20 */. int iBin;
870e0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
870f0 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65 into mem5.aiFree
87100 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 list[] */. int
87110 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 iFullSz; /*
87120 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69 Size of allocati
87130 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f on rounded up to
87140 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20 power of 2 */.
87150 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20 int iLogsize;
87160 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75 /* Log2 of iFu
87170 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f llSz/POW2_MIN */
87180 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 74 72 61 63 .. /* Keep trac
87190 6b 20 6f 66 20 74 68 65 20 6d 61 78 69 6d 75 6d k of the maximum
871a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 allocation requ
871b0 65 73 74 2e 20 20 45 76 65 6e 20 75 6e 66 75 6c est. Even unful
871c0 66 69 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71 75 filled. ** requ
871d0 65 73 74 73 20 61 72 65 20 63 6f 75 6e 74 65 64 ests are counted
871e0 20 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 29 6e */. if( (u32)n
871f0 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 65 71 Byte>mem5.maxReq
87200 75 65 73 74 20 29 7b 0a 20 20 20 20 6d 65 6d 35 uest ){. mem5
87210 2e 6d 61 78 52 65 71 75 65 73 74 20 3d 20 6e 42 .maxRequest = nB
87220 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 yte;. }.. /* R
87230 6f 75 6e 64 20 6e 42 79 74 65 20 75 70 20 74 6f ound nByte up to
87240 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 the next valid
87250 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a power of two */.
87260 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 for(iFullSz=me
87270 6d 35 2e 6e 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 m5.nAtom, iLogsi
87280 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 ze=0; iFullSz<nB
87290 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 yte; iFullSz *=
872a0 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 2, iLogsize++){}
872b0 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
872c0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
872d0 5b 69 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 [iLogsize] conta
872e0 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 ins at least one
872f0 20 66 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b free. ** block
87300 2e 20 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 . If not, then
87310 73 70 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 split a block of
87320 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 the next larger
87330 20 70 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 power of. ** t
87340 77 6f 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 wo in order to c
87350 72 65 61 74 65 20 61 20 6e 65 77 20 66 72 65 65 reate a new free
87360 20 62 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 block of size i
87370 4c 6f 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 Logsize.. */.
87380 66 6f 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a for(iBin=iLogsiz
87390 65 3b 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 e; mem5.aiFreeli
873a0 73 74 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 st[iBin]<0 && iB
873b0 69 6e 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e in<=LOGMAX; iBin
873c0 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e ++){}. if( iBin
873d0 3e 4c 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e >LOGMAX ) return
873e0 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 0;. i = memsys
873f0 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 5UnlinkFirst(iBi
87400 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69 n);. while( iBi
87410 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 n>iLogsize ){.
87420 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a int newSize;..
87430 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 iBin--;.
87440 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 newSize = 1 << i
87450 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 Bin;. mem5.aC
87460 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d trl[i+newSize] =
87470 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 CTRL_FREE | iBi
87480 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 n;. memsys5Li
87490 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 nk(i+newSize, iB
874a0 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e in);. }. mem5.
874b0 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 aCtrl[i] = iLogs
874c0 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 ize;.. /* Updat
874d0 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 e allocator perf
874e0 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 ormance statisti
874f0 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 cs. */. mem5.nA
87500 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 lloc++;. mem5.t
87510 6f 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 otalAlloc += iFu
87520 6c 6c 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 llSz;. mem5.tot
87530 61 6c 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c alExcess += iFul
87540 6c 53 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d lSz - nByte;. m
87550 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
87560 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 ++;. mem5.curre
87570 6e 74 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a ntOut += iFullSz
87580 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 ;. if( mem5.max
87590 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 Count<mem5.curre
875a0 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d ntCount ) mem5.m
875b0 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 axCount = mem5.c
875c0 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 urrentCount;. i
875d0 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d f( mem5.maxOut<m
875e0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 em5.currentOut )
875f0 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d mem5.maxOut = m
87600 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a em5.currentOut;.
87610 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 . /* Return a p
87620 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c ointer to the al
87630 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 located memory.
87640 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 */. return (voi
87650 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 d*)&mem5.zPool[i
87660 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 7d 0a *mem5.nAtom];.}.
87670 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f ./*.** Free an o
87680 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 utstanding memor
87690 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f y allocation..*/
876a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
876b0 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 sys5FreeUnsafe(v
876c0 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 oid *pOld){. u3
876d0 32 20 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 2 size, iLogsize
876e0 3b 0a 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 20 ;. int iBlock;
876f0 20 20 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20 ..
87700 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 74 6f /* Set iBlock to
87710 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
87720 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 20 e block pointed
87730 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a 20 to by pOld in .
87740 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 6f 66 ** the array of
87750 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 62 79 74 65 mem5.nAtom byte
87760 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 64 20 blocks pointed
87770 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c to by mem5.zPool
87780 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20 .. */. iBlock
87790 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 = ((u8 *)pOld-me
877a0 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e m5.zPool)/mem5.n
877b0 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 Atom;.. /* Chec
877c0 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 k that the point
877d0 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74 er pOld points t
877e0 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66 o a valid, non-f
877f0 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 ree block. */.
87800 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d assert( iBlock>=
87810 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 0 && iBlock<mem5
87820 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 .nBlock );. ass
87830 65 72 74 28 20 28 28 75 38 20 2a 29 70 4f 6c 64 ert( ((u8 *)pOld
87840 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d -mem5.zPool)%mem
87850 35 2e 6e 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 5.nAtom==0 );.
87860 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 assert( (mem5.aC
87870 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 trl[iBlock] & CT
87880 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a RL_FREE)==0 );..
87890 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d iLogsize = mem
878a0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
878b0 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a & CTRL_LOGSIZE;.
878c0 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 size = 1<<iLog
878d0 73 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 size;. assert(
878e0 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 iBlock+size-1<(u
878f0 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 32)mem5.nBlock )
87900 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b ;.. mem5.aCtrl[
87910 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f iBlock] |= CTRL_
87920 46 52 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 FREE;. mem5.aCt
87930 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 rl[iBlock+size-1
87940 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a ] |= CTRL_FREE;.
87950 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 assert( mem5.c
87960 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b urrentCount>0 );
87970 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e . assert( mem5.
87980 63 75 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a currentOut>=(siz
87990 65 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 29 20 29 3b e*mem5.nAtom) );
879a0 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 . mem5.currentC
879b0 6f 75 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 ount--;. mem5.c
879c0 75 72 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a urrentOut -= siz
879d0 65 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 e*mem5.nAtom;.
879e0 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 assert( mem5.cur
879f0 72 65 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d rentOut>0 || mem
87a00 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 5.currentCount==
87a10 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 0 );. assert( m
87a20 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
87a30 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 >0 || mem5.curre
87a40 6e 74 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d ntOut==0 );.. m
87a50 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b em5.aCtrl[iBlock
87a60 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 ] = CTRL_FREE |
87a70 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c iLogsize;. whil
87a80 65 28 20 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d e( iLogsize<LOGM
87a90 41 58 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 AX ){. int iB
87aa0 75 64 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 uddy;. if( (i
87ab0 42 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 Block>>iLogsize)
87ac0 20 26 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 & 1 ){. iB
87ad0 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 uddy = iBlock -
87ae0 73 69 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b size;. }else{
87af0 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 . iBuddy =
87b00 69 42 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 iBlock + size;.
87b10 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
87b20 20 69 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 iBuddy>=0 );.
87b30 20 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 if( (iBuddy+(1
87b40 3c 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d <<iLogsize))>mem
87b50 35 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 5.nBlock ) break
87b60 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 ;. if( mem5.a
87b70 43 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 Ctrl[iBuddy]!=(C
87b80 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 TRL_FREE | iLogs
87b90 69 7a 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 ize) ) break;.
87ba0 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 memsys5Unlink(
87bb0 69 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 iBuddy, iLogsize
87bc0 29 3b 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b );. iLogsize+
87bd0 2b 3b 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 +;. if( iBudd
87be0 79 3c 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 y<iBlock ){.
87bf0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 mem5.aCtrl[iBu
87c00 64 64 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 ddy] = CTRL_FREE
87c10 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 | iLogsize;.
87c20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 mem5.aCtrl[iB
87c30 6c 6f 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 lock] = 0;.
87c40 20 69 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 iBlock = iBuddy
87c50 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
87c60 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 mem5.aCtrl[iB
87c70 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 lock] = CTRL_FRE
87c80 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 E | iLogsize;.
87c90 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 mem5.aCtrl[i
87ca0 42 75 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 Buddy] = 0;.
87cb0 7d 0a 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b }. size *= 2;
87cc0 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 . }. memsys5Li
87cd0 6e 6b 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 nk(iBlock, iLogs
87ce0 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ize);.}../*.** A
87cf0 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f llocate nBytes o
87d00 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 f memory.*/.stat
87d10 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 ic void *memsys5
87d20 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 Malloc(int nByte
87d30 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e s){. sqlite3_in
87d40 74 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 t64 *p = 0;. if
87d50 28 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 ( nBytes>0 ){.
87d60 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 memsys5Enter()
87d70 3b 0a 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 ;. p = memsys
87d80 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 5MallocUnsafe(nB
87d90 79 74 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 ytes);. memsy
87da0 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 s5Leave();. }.
87db0 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 return (void*)p
87dc0 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 ; .}../*.** Free
87dd0 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 memory..*/.stat
87de0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 ic void memsys5F
87df0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
87e00 29 7b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d ){. if( pPrior=
87e10 3d 30 20 29 7b 0a 61 73 73 65 72 74 28 30 29 3b =0 ){.assert(0);
87e20 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
87e30 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 . memsys5Enter(
87e40 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65 );. memsys5Free
87e50 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a Unsafe(pPrior);.
87e60 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 memsys5Leave()
87e70 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 ; .}../*.** Cha
87e80 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 nge the size of
87e90 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f an existing memo
87ea0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f ry allocation.*/
87eb0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
87ec0 6d 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 msys5Realloc(voi
87ed0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e d *pPrior, int n
87ee0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f Bytes){. int nO
87ef0 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 ld;. void *p;.
87f00 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 if( pPrior==0 )
87f10 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d {. return mem
87f20 73 79 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 sys5Malloc(nByte
87f30 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 s);. }. if( nB
87f40 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 6d ytes<=0 ){. m
87f50 65 6d 73 79 73 35 46 72 65 65 28 70 50 72 69 6f emsys5Free(pPrio
87f60 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 r);. return 0
87f70 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d ;. }. nOld = m
87f80 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f emsys5Size(pPrio
87f90 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 r);. if( nBytes
87fa0 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 <=nOld ){. re
87fb0 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d turn pPrior;. }
87fc0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 . memsys5Enter(
87fd0 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 );. p = memsys5
87fe0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 MallocUnsafe(nBy
87ff0 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b tes);. if( p ){
88000 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 . memcpy(p, p
88010 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 Prior, nOld);.
88020 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 memsys5FreeUns
88030 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d afe(pPrior);. }
88040 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
88050 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d );. return p;.}
88060 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 ../*.** Round up
88070 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 a request size
88080 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 to the next vali
88090 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a d allocation siz
880a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
880b0 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 memsys5Roundup(
880c0 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 int n){. int iF
880d0 75 6c 6c 53 7a 3b 0a 20 20 66 6f 72 28 69 46 75 ullSz;. for(iFu
880e0 6c 6c 53 7a 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 3b llSz=mem5.nAtom;
880f0 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c iFullSz<n; iFul
88100 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74 lSz *= 2);. ret
88110 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a urn iFullSz;.}..
88120 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 static int memsy
88130 73 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 s5Log(int iValue
88140 29 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 ){. int iLog;.
88150 20 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c for(iLog=0; (1<
88160 3c 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 <iLog)<iValue; i
88170 4c 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e Log++);. return
88180 20 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iLog;.}../*.**
88190 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 Initialize this
881a0 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 module..*/.stati
881b0 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69 c int memsys5Ini
881c0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
881d0 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e {. int ii;. in
881e0 74 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 t nByte = sqlite
881f0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 3GlobalConfig.nH
88200 65 61 70 3b 0a 20 20 75 38 20 2a 7a 42 79 74 65 eap;. u8 *zByte
88210 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 = (u8 *)sqlite3
88220 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 GlobalConfig.pHe
88230 61 70 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f ap;. int nMinLo
88240 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
88250 20 20 20 2f 2a 20 4c 6f 67 20 6f 66 20 6d 69 6e /* Log of min
88260 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 imum allocation
88270 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2a 2f 0a size in bytes*/.
88280 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 0a int iOffset;..
88290 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
882a0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 ER(NotUsed);..
882b0 69 66 28 20 21 7a 42 79 74 65 20 29 7b 0a 20 20 if( !zByte ){.
882c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
882d0 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 6e 4d ERROR;. }.. nM
882e0 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c inLog = memsys5L
882f0 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c og(sqlite3Global
88300 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 Config.mnReq);.
88310 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3d 20 28 31 mem5.nAtom = (1
88320 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68 <<nMinLog);. wh
88330 69 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 ile( (int)sizeof
88340 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e (Mem5Link)>mem5.
88350 6e 41 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 6d nAtom ){. mem
88360 35 2e 6e 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 6e 5.nAtom = mem5.n
88370 41 74 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a Atom << 1;. }..
88380 20 20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 mem5.nBlock =
88390 28 6e 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 6e (nByte / (mem5.n
883a0 41 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 Atom+sizeof(u8))
883b0 29 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 );. mem5.zPool
883c0 3d 20 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e = zByte;. mem5.
883d0 61 43 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d aCtrl = (u8 *)&m
883e0 65 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e em5.zPool[mem5.n
883f0 42 6c 6f 63 6b 2a 6d 65 6d 35 2e 6e 41 74 6f 6d Block*mem5.nAtom
88400 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 ];.. for(ii=0;
88410 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b ii<=LOGMAX; ii++
88420 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 ){. mem5.aiFr
88430 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b eelist[ii] = -1;
88440 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 . }.. iOffset
88450 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f = 0;. for(ii=LO
88460 47 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d GMAX; ii>=0; ii-
88470 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c -){. int nAll
88480 6f 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 oc = (1<<ii);.
88490 20 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e if( (iOffset+n
884a0 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c Alloc)<=mem5.nBl
884b0 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ock ){. mem
884c0 35 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 5.aCtrl[iOffset]
884d0 20 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 = ii | CTRL_FRE
884e0 45 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 E;. memsys5
884f0 4c 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 Link(iOffset, ii
88500 29 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 );. iOffset
88510 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 += nAlloc;.
88520 7d 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f }. assert((iO
88530 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 ffset+nAlloc)>me
88540 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a m5.nBlock);. }.
88550 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
88560 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 _OK;.}../*.** De
88570 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 initialize this
88580 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 module..*/.stati
88590 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 53 68 c void memsys5Sh
885a0 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 utdown(void *Not
885b0 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
885c0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
885d0 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a d);. return;.}.
885e0 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 ./*.** Open the
885f0 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 file indicated a
88600 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f nd write a log o
88610 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 f all unfreed me
88620 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 mory .** allocat
88630 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c ions into that l
88640 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 og..*/.SQLITE_PR
88650 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
88660 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 6f e3Memsys5Dump(co
88670 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
88680 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c ame){.#ifdef SQL
88690 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 ITE_DEBUG. FILE
886a0 20 2a 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 *out;. int i,
886b0 6a 2c 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e j, n;. int nMin
886c0 4c 6f 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c Log;.. if( zFil
886d0 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c ename==0 || zFil
886e0 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 ename[0]==0 ){.
886f0 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b out = stdout;
88700 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 . }else{. ou
88710 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e t = fopen(zFilen
88720 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 ame, "w");. i
88730 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 f( out==0 ){.
88740 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
88750 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f r, "** Unable to
88760 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 output memory d
88770 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a ebug output log:
88780 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 %s **\n",.
88790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
887a0 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 zFilename);.
887b0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
887c0 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e . }. memsys5En
887d0 74 65 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 ter();. nMinLog
887e0 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 = memsys5Log(me
887f0 6d 35 2e 6e 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 m5.nAtom);. for
88800 28 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 (i=0; i<=LOGMAX
88810 26 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b && i+nMinLog<32;
88820 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e i++){. for(n
88830 3d 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 =0, j=mem5.aiFre
88840 65 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 elist[i]; j>=0;
88850 6a 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d j = MEM5LINK(j)-
88860 3e 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 >next, n++){}.
88870 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
88880 66 72 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f freelist items o
88890 66 20 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 f size %d: %d\n"
888a0 2c 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c 3c 20 , mem5.nAtom <<
888b0 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 i, n);. }. fpr
888c0 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e intf(out, "mem5.
888d0 6e 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 nAlloc = %
888e0 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c llu\n", mem5.nAl
888f0 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 loc);. fprintf(
88900 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c out, "mem5.total
88910 41 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e Alloc = %llu\n
88920 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c ", mem5.totalAll
88930 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f oc);. fprintf(o
88940 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 ut, "mem5.totalE
88950 78 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 xcess = %llu\n"
88960 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 , mem5.totalExce
88970 73 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f ss);. fprintf(o
88980 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e ut, "mem5.curren
88990 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 tOut = %u\n",
889a0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 mem5.currentOut)
889b0 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c ;. fprintf(out,
889c0 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f "mem5.currentCo
889d0 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d unt = %u\n", mem
889e0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 5.currentCount);
889f0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
88a00 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 "mem5.maxOut
88a10 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 = %u\n", mem5
88a20 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 .maxOut);. fpri
88a30 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d ntf(out, "mem5.m
88a40 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 axCount = %u
88a50 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 \n", mem5.maxCou
88a60 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f nt);. fprintf(o
88a70 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 ut, "mem5.maxReq
88a80 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 uest = %u\n",
88a90 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 mem5.maxRequest)
88aa0 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 ;. memsys5Leave
88ab0 28 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 ();. if( out==s
88ac0 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c tdout ){. ffl
88ad0 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d ush(stdout);. }
88ae0 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 else{. fclose
88af0 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 (out);. }.#else
88b00 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
88b10 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a TER(zFilename);.
88b20 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
88b30 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
88b40 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 the only routine
88b50 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 in this file wi
88b60 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 th external .**
88b70 6c 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75 linkage. It retu
88b80 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
88b90 20 61 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 a static sqlite
88ba0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a 3_mem_methods.**
88bb0 20 73 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65 struct populate
88bc0 64 20 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79 d with the memsy
88bd0 73 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 s5 methods..*/.S
88be0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
88bf0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
88c00 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
88c10 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f MemGetMemsys5(vo
88c20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f id){. static co
88c30 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
88c40 6d 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d methods memsys5M
88c50 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 ethods = {.
88c60 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20 memsys5Malloc,.
88c70 20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c memsys5Free,
88c80 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61 . memsys5Rea
88c90 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 lloc,. memsy
88ca0 73 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d s5Size,. mem
88cb0 73 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20 sys5Roundup,.
88cc0 20 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20 memsys5Init,.
88cd0 20 20 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64 memsys5Shutd
88ce0 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
88cf0 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79 . return &memsy
88d00 73 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 s5Methods;.}..#e
88d10 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
88d20 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f NABLE_MEMSYS5 */
88d30 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
88d40 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20 * End of mem5.c
88d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88d80 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
88d90 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 * Begin file mut
88da0 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.c ***********
88db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88dd0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 /./*.** 2007 Aug
88de0 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 14.**.** The
88df0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
88e00 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
88e10 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
88e20 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
88e30 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
88e40 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
88e50 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
88e60 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
88e70 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
88e80 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
88e90 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
88ea0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
88eb0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
88ec0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
88ed0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
88ee0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
88ef0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
88f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88f40 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
88f50 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
88f60 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
88f70 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
88f80 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 es..**.** This f
88f90 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
88fa0 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e e that is common
88fb0 20 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65 across all mute
88fc0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
88fd0 73 2e 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d s...**.** $Id: m
88fe0 75 74 65 78 2e 63 2c 76 20 31 2e 33 30 20 32 30 utex.c,v 1.30 20
88ff0 30 39 2f 30 32 2f 31 37 20 31 36 3a 32 39 3a 31 09/02/17 16:29:1
89000 31 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 1 danielk1977 Ex
89010 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 p $.*/..#ifndef
89020 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 SQLITE_MUTEX_OMI
89030 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 T./*.** Initiali
89040 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 79 73 ze the mutex sys
89050 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 tem..*/.SQLITE_P
89060 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
89070 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 e3MutexInit(void
89080 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ){ . int rc = S
89090 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
890a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
890b0 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 fig.bCoreMutex )
890c0 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 {. if( !sqlit
890d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
890e0 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 utex.xMutexAlloc
890f0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
89100 74 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 the xMutexAlloc
89110 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 method has not b
89120 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 74 68 een set, then th
89130 65 20 75 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 e user did not.
89140 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 ** install
89150 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e a mutex implemen
89160 74 61 74 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 tation via sqlit
89170 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 72 69 6f e3_config() prio
89180 72 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 r to . ** s
89190 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
891a0 65 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 e() being called
891b0 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 . This block cop
891c0 69 65 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a ies pointers to.
891d0 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 ** the def
891e0 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ault implementat
891f0 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73 71 6c ion into the sql
89200 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
89210 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 structure..
89220 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 **. ** Th
89230 65 20 64 61 6e 67 65 72 20 69 73 20 74 68 61 74 e danger is that
89240 20 61 6c 74 68 6f 75 67 68 20 73 71 6c 69 74 65 although sqlite
89250 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 6e 6f 3_config() is no
89260 74 20 61 20 74 68 72 65 61 64 73 61 66 65 0a 20 t a threadsafe.
89270 20 20 20 20 20 2a 2a 20 41 50 49 2c 20 73 71 6c ** API, sql
89280 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
89290 29 20 69 73 2c 20 61 6e 64 20 73 6f 20 6d 75 6c ) is, and so mul
892a0 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 6d 61 tiple threads ma
892b0 79 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 61 74 y be. ** at
892c0 74 65 6d 70 74 69 6e 67 20 74 6f 20 72 75 6e 20 tempting to run
892d0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 69 this function si
892e0 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 20 54 6f multaneously. To
892f0 20 67 75 61 72 64 20 77 72 69 74 65 0a 20 20 20 guard write.
89300 20 20 20 2a 2a 20 61 63 63 65 73 73 20 74 6f 20 ** access to
89310 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 the sqlite3Globa
89320 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75 72 lConfig structur
89330 65 2c 20 74 68 65 20 27 4d 41 53 54 45 52 27 20 e, the 'MASTER'
89340 73 74 61 74 69 63 20 6d 75 74 65 78 0a 20 20 20 static mutex.
89350 20 20 20 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 ** is obtaine
89360 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 d before modifyi
89370 6e 67 20 69 74 2e 0a 20 20 20 20 20 20 2a 2f 0a ng it.. */.
89380 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
89390 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 20 3d tex_methods *p =
893a0 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d sqlite3DefaultM
893b0 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 73 71 utex();. sq
893c0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 lite3_mutex *pMa
893d0 73 74 65 72 20 3d 20 30 3b 0a 20 20 0a 20 20 20 ster = 0;. .
893e0 20 20 20 72 63 20 3d 20 70 2d 3e 78 4d 75 74 65 rc = p->xMute
893f0 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 20 20 69 xInit();. i
89400 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
89410 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 61 73 ){. pMas
89420 74 65 72 20 3d 20 70 2d 3e 78 4d 75 74 65 78 41 ter = p->xMutexA
89430 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
89440 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
89450 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
89460 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 (pMaster);.
89470 20 20 20 70 2d 3e 78 4d 75 74 65 78 45 6e 74 65 p->xMutexEnte
89480 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 r(pMaster);.
89490 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
894a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
894b0 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f mutex.xMutexAllo
894c0 63 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 c==0 .
894d0 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f || sqlite3Glo
894e0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
894f0 78 4d 75 74 65 78 41 6c 6c 6f 63 3d 3d 70 2d 3e xMutexAlloc==p->
89500 78 4d 75 74 65 78 41 6c 6c 6f 63 0a 20 20 20 20 xMutexAlloc.
89510 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 );. i
89520 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 f( !sqlite3Globa
89530 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
89540 75 74 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 utexAlloc ){.
89550 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c sqlite3Gl
89560 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
89570 20 3d 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 7d = *p;. }
89580 0a 20 20 20 20 20 20 20 20 70 2d 3e 78 4d 75 74 . p->xMut
89590 65 78 4c 65 61 76 65 28 70 4d 61 73 74 65 72 29 exLeave(pMaster)
895a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
895b0 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
895c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
895d0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
895e0 49 6e 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20 20 Init();. }.
895f0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
89600 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 }../*.** Shutdow
89610 6e 20 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 n the mutex syst
89620 65 6d 2e 20 54 68 69 73 20 63 61 6c 6c 20 66 72 em. This call fr
89630 65 65 73 20 72 65 73 6f 75 72 63 65 73 20 61 6c ees resources al
89640 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 located by.** sq
89650 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 lite3MutexInit()
89660 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
89670 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
89680 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 0a 20 utexEnd(void){.
89690 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
896a0 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 _OK;. if( sqlit
896b0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
896c0 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 64 20 29 utex.xMutexEnd )
896d0 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
896e0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
896f0 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 64 28 29 utex.xMutexEnd()
89700 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
89710 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 c;.}../*.** Retr
89720 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 ieve a pointer t
89730 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 o a static mutex
89740 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e or allocate a n
89750 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a ew dynamic one..
89760 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
89770 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c lite3_mutex *sql
89780 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
89790 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65 (int id){.#ifnde
897a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
897b0 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c TOINIT. if( sql
897c0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
897d0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 ) ) return 0;.#e
897e0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 ndif. return sq
897f0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
89800 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c g.mutex.xMutexAl
89810 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49 loc(id);.}..SQLI
89820 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
89830 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 e3_mutex *sqlite
89840 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 3MutexAlloc(int
89850 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 id){. if( !sqli
89860 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
89870 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 bCoreMutex ){.
89880 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
89890 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
898a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
898b0 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 ex.xMutexAlloc(i
898c0 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 d);.}../*.** Fre
898d0 65 20 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 e a dynamic mute
898e0 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 x..*/.SQLITE_API
898f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
89900 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 tex_free(sqlite3
89910 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 _mutex *p){. if
89920 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( p ){. sqlit
89930 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
89940 75 74 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 utex.xMutexFree(
89950 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
89960 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 Obtain the mute
89970 78 20 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 x p. If some oth
89980 65 72 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 er thread alread
89990 79 20 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c y has the mutex,
899a0 20 62 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 block.** until
899b0 69 74 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e it can be obtain
899c0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ed..*/.SQLITE_AP
899d0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d I void sqlite3_m
899e0 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 utex_enter(sqlit
899f0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
89a00 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c if( p ){. sql
89a10 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
89a20 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 .mutex.xMutexEnt
89a30 65 72 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a er(p);. }.}../*
89a40 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d .** Obtain the m
89a50 75 74 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 utex p. If succe
89a60 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 ssful, return SQ
89a70 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 LITE_OK. Otherwi
89a80 73 65 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a se, if another.*
89a90 2a 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 * thread holds t
89aa0 68 65 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 he mutex and it
89ab0 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e cannot be obtain
89ac0 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ed, return SQLIT
89ad0 45 5f 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 E_BUSY..*/.SQLIT
89ae0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
89af0 33 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 3_mutex_try(sqli
89b00 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
89b10 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
89b20 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a _OK;. if( p ){.
89b30 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
89b40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
89b50 75 74 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 utex.xMutexTry(p
89b60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
89b70 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
89b80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
89b90 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 eave() routine e
89ba0 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 xits a mutex tha
89bb0 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 t was previously
89bc0 0a 2a 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 .** entered by t
89bd0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
89be0 20 54 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 The behavior is
89bf0 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
89c00 65 20 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e e mutex .** is n
89c10 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
89c20 65 72 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 ered. If a NULL
89c30 70 6f 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 pointer is passe
89c40 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 d as an argument
89c50 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
89c60 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f n is a no-op..*/
89c70 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
89c80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
89c90 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
89ca0 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 ex *p){. if( p
89cb0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
89cc0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
89cd0 2e 78 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b .xMutexLeave(p);
89ce0 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 . }.}..#ifndef
89cf0 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 NDEBUG./*.** The
89d00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
89d10 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 eld() and sqlite
89d20 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
89d30 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a ) routine are.**
89d40 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 intended for us
89d50 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 e inside assert(
89d60 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f ) statements..*/
89d70 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
89d80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
89d90 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
89da0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
89db0 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c ==0 || sqlite3Gl
89dc0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
89dd0 2e 78 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a .xMutexHeld(p);.
89de0 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
89df0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
89e00 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
89e10 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
89e20 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 rn p==0 || sqlit
89e30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
89e40 75 74 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 utex.xMutexNothe
89e50 6c 64 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a ld(p);.}.#endif.
89e60 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
89e70 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a E_OMIT_MUTEX */.
89e80 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
89e90 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 End of mutex.c
89ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
89ed0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
89ee0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
89ef0 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a x_noop.c *******
89f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
89f20 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f ./*.** 2008 Octo
89f30 62 65 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 07.**.** The
89f40 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
89f50 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
89f60 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
89f70 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
89f80 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
89f90 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
89fa0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
89fb0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
89fc0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
89fd0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
89fe0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
89ff0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
8a000 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
8a010 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
8a020 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
8a030 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
8a040 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
8a050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8a060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8a070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8a080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8a090 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
8a0a0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
8a0b0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
8a0c0 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
8a0d0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 es..**.** This i
8a0e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e mplementation in
8a0f0 20 74 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 this file does
8a100 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 not provide any
8a110 6d 75 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 mutual.** exclus
8a120 69 6f 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 ion and is thus
8a130 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 suitable for use
8a140 20 6f 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 only in applica
8a150 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 tions.** that us
8a160 65 20 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 e SQLite in a si
8a170 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 ngle thread. Th
8a180 65 20 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e e routines defin
8a190 65 64 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 ed.** here are p
8a1a0 6c 61 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 lace-holders. A
8a1b0 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 pplications can
8a1c0 73 75 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 substitute worki
8a1d0 6e 67 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 ng.** mutex rout
8a1e0 69 6e 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 ines at start-ti
8a1f0 6d 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a me using the.**.
8a200 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
8a210 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e onfig(SQLITE_CON
8a220 46 49 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a FIG_MUTEX,...).*
8a230 2a 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a *.** interface..
8a240 2a 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 **.** If compile
8a250 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 d with SQLITE_DE
8a260 42 55 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 BUG, then additi
8a270 6f 6e 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e onal logic is in
8a280 73 65 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 serted.** that d
8a290 6f 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 oes error checki
8a2a0 6e 67 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f ng on mutexes to
8a2b0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 make sure they
8a2c0 61 72 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c are being.** cal
8a2d0 6c 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a led correctly..*
8a2e0 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f *.** $Id: mutex_
8a2f0 6e 6f 6f 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 noop.c,v 1.3 200
8a300 38 2f 31 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 8/12/05 17:17:08
8a310 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
8a320 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
8a330 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 TE_MUTEX_NOOP) &
8a340 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
8a350 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 E_DEBUG)./*.** S
8a360 74 75 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 tub routines for
8a370 20 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f all mutex metho
8a380 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ds..**.** This r
8a390 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 outines provide
8a3a0 6e 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 no mutual exclus
8a3b0 69 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 ion or error che
8a3c0 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 cking..*/.static
8a3d0 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 int noopMutexHe
8a3e0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
8a3f0 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 *p){ return 1;
8a400 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f }.static int noo
8a410 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 pMutexNotheld(sq
8a420 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
8a430 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 return 1; }.sta
8a440 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 tic int noopMute
8a450 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 xInit(void){ ret
8a460 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
8a470 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 .static int noop
8a480 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 MutexEnd(void){
8a490 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
8a4a0 3b 20 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 ; }.static sqlit
8a4b0 65 33 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 e3_mutex *noopMu
8a4c0 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 texAlloc(int id)
8a4d0 7b 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 { return (sqlite
8a4e0 33 5f 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 3_mutex*)8; }.st
8a4f0 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 atic void noopMu
8a500 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
8a510 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 mutex *p){ retur
8a520 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 n; }.static void
8a530 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 noopMutexEnter(
8a540 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
8a550 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 ){ return; }.sta
8a560 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 tic int noopMute
8a570 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 xTry(sqlite3_mut
8a580 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 ex *p){ return S
8a590 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 QLITE_OK; }.stat
8a5a0 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 ic void noopMute
8a5b0 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d xLeave(sqlite3_m
8a5c0 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e utex *p){ return
8a5d0 3b 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ; }..SQLITE_PRIV
8a5e0 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ATE sqlite3_mute
8a5f0 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 x_methods *sqlit
8a600 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 e3DefaultMutex(v
8a610 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 oid){. static s
8a620 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
8a630 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a hods sMutex = {.
8a640 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 noopMutexIni
8a650 74 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 t,. noopMutex
8a660 45 6e 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 End,. noopMut
8a670 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f exAlloc,. noo
8a680 70 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 pMutexFree,.
8a690 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a noopMutexEnter,.
8a6a0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 noopMutexTry
8a6b0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c ,. noopMutexL
8a6c0 65 61 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d eave,.. noopM
8a6d0 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f utexHeld,. no
8a6e0 6f 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 opMutexNotheld.
8a6f0 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 };.. return &s
8a700 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 Mutex;.}.#endif
8a710 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 /* defined(SQLIT
8a720 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 E_MUTEX_NOOP) &&
8a730 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
8a740 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 _DEBUG) */..#if
8a750 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
8a760 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 UTEX_NOOP) && de
8a770 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
8a780 55 47 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 UG)./*.** In thi
8a790 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
8a7a0 2c 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 , error checking
8a7b0 20 69 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 is provided for
8a7c0 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 testing.** and
8a7d0 64 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 debugging purpos
8a7e0 65 73 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 es. The mutexes
8a7f0 20 73 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 still do not pr
8a800 6f 76 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 ovide any.** mut
8a810 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a ual exclusion..*
8a820 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 /../*.** The mut
8a830 65 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 ex object.*/.str
8a840 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 uct sqlite3_mute
8a850 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 x {. int id;
8a860 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 /* The mutex t
8a870 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 ype */. int cnt
8a880 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f ; /* Number o
8a890 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 f entries withou
8a8a0 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 t a matching lea
8a8b0 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ve */.};../*.**
8a8c0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
8a8d0 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c x_held() and sql
8a8e0 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 ite3_mutex_nothe
8a8f0 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 ld() routine are
8a900 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 .** intended for
8a910 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 use inside asse
8a920 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
8a930 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
8a940 65 62 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 ebugMutexHeld(sq
8a950 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
8a960 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c . return p==0 |
8a970 7c 20 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 | p->cnt>0;.}.st
8a980 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 atic int debugMu
8a990 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 texNotheld(sqlit
8a9a0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
8a9b0 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 return p==0 || p
8a9c0 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a ->cnt==0;.}../*.
8a9d0 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e ** Initialize an
8a9e0 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 d deinitialize t
8a9f0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 he mutex subsyst
8aa00 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e em..*/.static in
8aa10 74 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 t debugMutexInit
8aa20 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 (void){ return S
8aa30 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 QLITE_OK; }.stat
8aa40 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
8aa50 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 xEnd(void){ retu
8aa60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
8aa70 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
8aa80 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
8aa90 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 routine allocat
8aaa0 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 es a new.** mute
8aab0 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 x and returns a
8aac0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 pointer to it.
8aad0 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 If it returns NU
8aae0 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 LL.** that means
8aaf0 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f that a mutex co
8ab00 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 uld not be alloc
8ab10 61 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 ated. .*/.static
8ab20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
8ab30 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 debugMutexAlloc(
8ab40 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 int id){. stati
8ab50 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
8ab60 61 53 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 aStatic[6];. sq
8ab70 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 lite3_mutex *pNe
8ab80 77 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 w = 0;. switch(
8ab90 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 id ){. case
8aba0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
8abb0 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 T:. case SQLI
8abc0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
8abd0 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 VE: {. pNew
8abe0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
8abf0 28 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b (sizeof(*pNew));
8ac00 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 . if( pNew
8ac10 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d ){. pNew-
8ac20 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 >id = id;.
8ac30 20 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b pNew->cnt = 0;
8ac40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
8ac50 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
8ac60 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
8ac70 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d assert( id-2 >=
8ac80 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 0 );. asse
8ac90 72 74 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 rt( id-2 < (int)
8aca0 28 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 (sizeof(aStatic)
8acb0 2f 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b /sizeof(aStatic[
8acc0 30 5d 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 0])) );. pN
8acd0 65 77 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 ew = &aStatic[id
8ace0 2d 32 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d -2];. pNew-
8acf0 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 >id = id;.
8ad00 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
8ad10 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
8ad20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
8ad30 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 utine deallocate
8ad40 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 s a previously a
8ad50 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a llocated mutex..
8ad60 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 */.static void d
8ad70 65 62 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 ebugMutexFree(sq
8ad80 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
8ad90 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e . assert( p->cn
8ada0 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t==0 );. assert
8adb0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f ( p->id==SQLITE_
8adc0 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d MUTEX_FAST || p-
8add0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
8ade0 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 X_RECURSIVE );.
8adf0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
8ae00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
8ae10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
8ae20 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 er() and sqlite3
8ae30 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 _mutex_try() rou
8ae40 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a tines attempt.**
8ae50 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 to enter a mute
8ae60 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 x. If another t
8ae70 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 hread is already
8ae80 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 within the mute
8ae90 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 x,.** sqlite3_mu
8aea0 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c tex_enter() will
8aeb0 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 block and sqlit
8aec0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 e3_mutex_try() w
8aed0 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 ill return.** SQ
8aee0 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 LITE_BUSY. The
8aef0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
8af00 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 y() interface re
8af10 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a turns SQLITE_OK.
8af20 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 ** upon successf
8af30 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 ul entry. Mutex
8af40 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 es created using
8af50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
8af60 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 CURSIVE can.** b
8af70 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 e entered multip
8af80 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 le times by the
8af90 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e same thread. In
8afa0 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c such cases the,
8afb0 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 .** mutex must b
8afc0 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 e exited an equa
8afd0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 l number of time
8afe0 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 s before another
8aff0 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 thread.** can e
8b000 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 nter. If the sa
8b010 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 me thread tries
8b020 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 to enter any oth
8b030 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 er kind of mutex
8b040 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e .** more than on
8b050 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 ce, the behavior
8b060 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
8b070 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 /.static void de
8b080 62 75 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 bugMutexEnter(sq
8b090 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
8b0a0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
8b0b0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
8b0c0 45 43 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 ECURSIVE || debu
8b0d0 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 gMutexNotheld(p)
8b0e0 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a );. p->cnt++;.
8b0f0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 }.static int deb
8b100 75 67 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 ugMutexTry(sqlit
8b110 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
8b120 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 assert( p->id==S
8b130 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
8b140 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 RSIVE || debugMu
8b150 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
8b160 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 . p->cnt++;. r
8b170 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
8b180 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
8b190 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
8b1a0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 e() routine exit
8b1b0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 s a mutex that w
8b1c0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 as.** previously
8b1d0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 entered by the
8b1e0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 same thread. Th
8b1f0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 e behavior.** is
8b200 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
8b210 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 e mutex is not c
8b220 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 urrently entered
8b230 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 or.** is not cu
8b240 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 rrently allocate
8b250 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 d. SQLite will
8b260 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e never do either.
8b270 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8b280 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 debugMutexLeave(
8b290 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
8b2a0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 ){. assert( deb
8b2b0 75 67 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 ugMutexHeld(p) )
8b2c0 3b 0a 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 ;. p->cnt--;.
8b2d0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 assert( p->id==S
8b2e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
8b2f0 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 RSIVE || debugMu
8b300 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
8b310 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}..SQLITE_PRIVA
8b320 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 TE sqlite3_mutex
8b330 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 _methods *sqlite
8b340 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 3DefaultMutex(vo
8b350 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 id){. static sq
8b360 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
8b370 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 ods sMutex = {.
8b380 20 20 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 debugMutexIni
8b390 74 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 t,. debugMute
8b3a0 78 45 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d xEnd,. debugM
8b3b0 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 utexAlloc,. d
8b3c0 65 62 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 ebugMutexFree,.
8b3d0 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 debugMutexEnt
8b3e0 65 72 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 er,. debugMut
8b3f0 65 78 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 exTry,. debug
8b400 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 MutexLeave,..
8b410 20 64 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c debugMutexHeld,
8b420 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e . debugMutexN
8b430 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 otheld. };.. r
8b440 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d eturn &sMutex;.}
8b450 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e .#endif /* defin
8b460 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ed(SQLITE_MUTEX_
8b470 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 NOOP) && defined
8b480 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a (SQLITE_DEBUG) *
8b490 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
8b4a0 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f ** End of mutex_
8b4b0 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a noop.c *********
8b4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b4e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
8b4f0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 ** Begin file mu
8b500 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a tex_os2.c ******
8b510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b530 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 */./*.** 2007 Au
8b540 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 gust 28.**.** Th
8b550 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
8b560 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
8b570 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
8b580 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
8b590 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
8b5a0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
8b5b0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
8b5c0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
8b5d0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
8b5e0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
8b5f0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
8b600 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
8b610 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
8b620 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
8b630 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
8b640 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
8b650 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
8b660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b6a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
8b6b0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
8b6c0 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 C functions tha
8b6d0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 t implement mute
8b6e0 78 65 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a xes for OS/2.**.
8b6f0 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 ** $Id: mutex_os
8b700 32 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 2.c,v 1.11 2008/
8b710 31 31 2f 32 32 20 31 39 3a 35 30 3a 35 34 20 70 11/22 19:50:54 p
8b720 77 65 69 6c 62 61 63 68 65 72 20 45 78 70 20 24 weilbacher Exp $
8b730 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 .*/../*.** The c
8b740 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ode in this file
8b750 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 is only used if
8b760 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 SQLITE_MUTEX_OS
8b770 32 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 2 is defined..**
8b780 20 53 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 See the mutex.h
8b790 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c file for detail
8b7a0 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c s..*/.#ifdef SQL
8b7b0 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f ITE_MUTEX_OS2../
8b7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8b7d0 2a 2a 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 ****** OS/2 Mute
8b7e0 78 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x Implementation
8b7f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
8b800 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
8b810 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
8b820 6e 20 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 n of mutexes is
8b830 62 75 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 built using the
8b840 4f 53 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a OS/2 API..*/../*
8b850 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 .** The mutex ob
8b860 6a 65 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 ject.** Each rec
8b870 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 ursive mutex is
8b880 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
8b890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
8b8a0 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 ucture..*/.struc
8b8b0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 t sqlite3_mutex
8b8c0 7b 0a 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 {. HMTX mutex;
8b8d0 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 /* Mutex c
8b8e0 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c ontrolling the l
8b8f0 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 ock */. int id
8b900 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 ; /* Mu
8b910 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e tex type */. in
8b920 74 20 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 t nRef;
8b930 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 /* Number of ref
8b940 65 72 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 erences */. TID
8b950 20 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f owner; /
8b960 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 * Thread holding
8b970 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d this mutex */.}
8b980 3b 0a 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d ;..#define OS2_M
8b990 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
8b9a0 20 20 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 0,0,0,0../*.*
8b9b0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 * Initialize and
8b9c0 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 deinitialize th
8b9d0 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
8b9e0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 m..*/.static int
8b9f0 20 6f 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f os2MutexInit(vo
8ba00 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
8ba10 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 TE_OK; }.static
8ba20 69 6e 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 int os2MutexEnd(
8ba30 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 void){ return SQ
8ba40 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a LITE_OK; }../*.*
8ba50 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
8ba60 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 tex_alloc() rout
8ba70 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 ine allocates a
8ba80 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 new.** mutex and
8ba90 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
8baa0 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 er to it. If it
8bab0 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a returns NULL.**
8bac0 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
8bad0 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e a mutex could n
8bae0 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e ot be allocated.
8baf0 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c .** SQLite will
8bb00 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 unwind its stac
8bb10 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 k and return an
8bb20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 error. The argu
8bb30 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 ment.** to sqlit
8bb40 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
8bb50 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 is one of these
8bb60 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
8bb70 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a ts:.**.** <ul>.*
8bb80 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
8bb90 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 UTEX_FAST
8bba0 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 0.** <li
8bbb0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
8bbc0 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 RECURSIVE
8bbd0 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 1.** <li> SQ
8bbe0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
8bbf0 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a C_MASTER 2.
8bc00 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
8bc10 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
8bc20 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 3.** <l
8bc30 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
8bc40 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 _STATIC_PRNG
8bc50 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 4.** </ul>.*
8bc60 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 *.** The first t
8bc70 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 wo constants cau
8bc80 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 se sqlite3_mutex
8bc90 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 _alloc() to crea
8bca0 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 te.** a new mute
8bcb0 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 x. The new mute
8bcc0 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77 x is recursive w
8bcd0 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 hen SQLITE_MUTEX
8bce0 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 _RECURSIVE.** is
8bcf0 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 used but not ne
8bd00 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 cessarily so whe
8bd10 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 n SQLITE_MUTEX_F
8bd20 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 AST is used..**
8bd30 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d The mutex implem
8bd40 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f entation does no
8bd50 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 t need to make a
8bd60 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 distinction.**
8bd70 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d between SQLITE_M
8bd80 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 UTEX_RECURSIVE a
8bd90 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f nd SQLITE_MUTEX_
8bda0 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a FAST if it does.
8bdb0 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 ** not want to.
8bdc0 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c But SQLite will
8bdd0 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 only request a
8bde0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 recursive mutex
8bdf0 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 in.** cases wher
8be00 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 e it really need
8be10 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 s one. If a fas
8be20 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 ter non-recursiv
8be30 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 e mutex.** imple
8be40 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 mentation is ava
8be50 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f ilable on the ho
8be60 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 st platform, the
8be70 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d mutex subsystem
8be80 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e .** might return
8be90 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e such a mutex in
8bea0 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c response to SQL
8beb0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a ITE_MUTEX_FAST..
8bec0 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 **.** The other
8bed0 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 allowed paramete
8bee0 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 rs to sqlite3_mu
8bef0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 tex_alloc() each
8bf00 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 return.** a poi
8bf10 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 nter to a static
8bf20 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 preexisting mut
8bf30 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 ex. Three stati
8bf40 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a c mutexes are.**
8bf50 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72 used by the cur
8bf60 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 rent version of
8bf70 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 SQLite. Future
8bf80 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
8bf90 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 te.** may add ad
8bfa0 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 ditional static
8bfb0 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 mutexes. Static
8bfc0 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 mutexes are for
8bfd0 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 internal.** use
8bfe0 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e by SQLite only.
8bff0 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 Applications t
8c000 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d hat use SQLite m
8c010 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a utexes should.**
8c020 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 use only the dy
8c030 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 namic mutexes re
8c040 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 turned by SQLITE
8c050 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a _MUTEX_FAST or.*
8c060 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 * SQLITE_MUTEX_R
8c070 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 ECURSIVE..**.**
8c080 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 Note that if one
8c090 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 of the dynamic
8c0a0 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 mutex parameters
8c0b0 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 (SQLITE_MUTEX_F
8c0c0 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 AST.** or SQLITE
8c0d0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
8c0e0 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 ) is used then s
8c0f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
8c100 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 oc().** returns
8c110 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 a different mute
8c120 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e x on every call.
8c130 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 But for the st
8c140 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 atic.** mutex ty
8c150 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 pes, the same mu
8c160 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 tex is returned
8c170 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 on every call th
8c180 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 at has.** the sa
8c190 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a me type number..
8c1a0 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 */.static sqlite
8c1b0 33 5f 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 3_mutex *os2Mute
8c1c0 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 xAlloc(int iType
8c1d0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ){. sqlite3_mut
8c1e0 65 78 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 ex *p = NULL;.
8c1f0 73 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b switch( iType ){
8c200 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
8c210 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 _MUTEX_FAST:.
8c220 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 case SQLITE_MUT
8c230 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a EX_RECURSIVE: {.
8c240 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 p = sqlite
8c250 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 3MallocZero( siz
8c260 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 eof(*p) );.
8c270 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 if( p ){.
8c280 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b p->id = iType;
8c290 0a 20 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 . if( Dos
8c2a0 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 CreateMutexSem(
8c2b0 30 2c 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 0, &p->mutex, 0,
8c2c0 20 46 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 FALSE ) != NO_E
8c2d0 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 RROR ){.
8c2e0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 sqlite3_free(
8c2f0 70 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 p );. p
8c300 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 = NULL;.
8c310 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
8c320 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
8c330 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
8c340 20 20 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 static volati
8c350 6c 65 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 le int isInit =
8c360 30 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 0;. static
8c370 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 sqlite3_mutex st
8c380 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 aticMutexes[] =
8c390 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f {. { OS2_
8c3a0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
8c3b0 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 R, },. {
8c3c0 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 OS2_MUTEX_INITIA
8c3d0 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 LIZER, },.
8c3e0 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e { OS2_MUTEX_IN
8c3f0 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 ITIALIZER, },.
8c400 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 { OS2_MUTE
8c410 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d X_INITIALIZER, }
8c420 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f ,. { OS2_
8c430 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
8c440 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 R, },. {
8c450 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 OS2_MUTEX_INITIA
8c460 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 LIZER, },.
8c470 7d 3b 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 };. if ( !i
8c480 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 sInit ){.
8c490 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 APIRET rc;.
8c4a0 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 PTIB ptib;.
8c4b0 20 20 20 20 20 20 20 50 50 49 42 20 70 70 69 62 PPIB ppib
8c4c0 3b 0a 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d ;. HMTX m
8c4d0 75 74 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 utex;. ch
8c4e0 61 72 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 ar name[32];.
8c4f0 20 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 DosGetInfoB
8c500 6c 6f 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 locks( &ptib, &p
8c510 70 69 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 pib );. s
8c520 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
8c530 20 73 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e sizeof(name), n
8c540 61 6d 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 ame, "\\SEM32\\S
8c550 51 4c 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 QLITE%04x",.
8c560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8c570 20 20 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f ppib->pib_
8c580 75 6c 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 ulpid );.
8c590 20 77 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 while( !isInit
8c5a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 ){. mut
8c5b0 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ex = 0;.
8c5c0 20 20 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 rc = DosCreate
8c5d0 4d 75 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 MutexSem( name,
8c5e0 26 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 &mutex, 0, FALSE
8c5f0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
8c600 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc == NO_ERROR
8c610 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 ){. u
8c620 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 nsigned int i;.
8c630 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 if( !
8c640 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 isInit ){.
8c650 20 20 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d for( i =
8c660 20 30 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 0; i < sizeof(s
8c670 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 taticMutexes)/si
8c680 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 zeof(staticMutex
8c690 65 73 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 es[0]); i++ ){.
8c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 D
8c6b0 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d osCreateMutexSem
8c6c0 28 20 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 ( 0, &staticMute
8c6d0 78 65 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c xes[i].mutex, 0,
8c6e0 20 46 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 FALSE );.
8c6f0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
8c700 20 20 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d isInit =
8c710 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1;.
8c720 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 6f }. Do
8c730 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 sCloseMutexSem(
8c740 6d 75 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 mutex );.
8c750 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 }else if( rc
8c760 3d 3d 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 == ERROR_DUPLICA
8c770 54 45 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 TE_NAME ){.
8c780 20 20 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 DosSleep(
8c790 20 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 1 );.
8c7a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
8c7b0 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 return p;.
8c7c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
8c7d0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
8c7e0 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 assert( iType-2
8c7f0 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 >= 0 );. a
8c800 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c ssert( iType-2 <
8c810 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 sizeof(staticMu
8c820 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 texes)/sizeof(st
8c830 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 aticMutexes[0])
8c840 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 );. p = &st
8c850 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 aticMutexes[iTyp
8c860 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 e-2];. p->i
8c870 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 d = iType;.
8c880 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
8c890 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
8c8a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
8c8b0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 tine deallocates
8c8c0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c a previously al
8c8d0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a located mutex..*
8c8e0 2a 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 * SQLite is care
8c8f0 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 ful to deallocat
8c900 65 20 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 e every mutex th
8c910 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e at it allocates.
8c920 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8c930 6f 73 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c os2MutexFree(sql
8c940 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
8c950 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
8c960 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
8c970 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 ->nRef==0 );. a
8c980 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
8c990 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
8c9a0 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
8c9b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
8c9c0 20 29 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 );. DosCloseMu
8c9d0 74 65 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 texSem( p->mutex
8c9e0 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 );. sqlite3_fr
8c9f0 65 65 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ee( p );.}../*.*
8ca00 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
8ca10 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 tex_enter() and
8ca20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
8ca30 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 y() routines att
8ca40 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 empt.** to enter
8ca50 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e a mutex. If an
8ca60 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 other thread is
8ca70 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 already within t
8ca80 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c he mutex,.** sql
8ca90 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
8caa0 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e () will block an
8cab0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
8cac0 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 try() will retur
8cad0 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 n.** SQLITE_BUSY
8cae0 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d . The sqlite3_m
8caf0 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 utex_try() inter
8cb00 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c face returns SQL
8cb10 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 ITE_OK.** upon s
8cb20 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e uccessful entry.
8cb30 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 Mutexes create
8cb40 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d d using SQLITE_M
8cb50 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 UTEX_RECURSIVE c
8cb60 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 an.** be entered
8cb70 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 multiple times
8cb80 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
8cb90 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 ad. In such cas
8cba0 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 es the,.** mutex
8cbb0 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 must be exited
8cbc0 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 an equal number
8cbd0 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 of times before
8cbe0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a another thread.*
8cbf0 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 * can enter. If
8cc00 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
8cc10 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 tries to enter
8cc20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f any other kind o
8cc30 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 f mutex.** more
8cc40 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 than once, the b
8cc50 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
8cc60 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ined..*/.static
8cc70 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 void os2MutexEnt
8cc80 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
8cc90 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b *p){. TID tid;
8cca0 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a . PID holder1;.
8ccb0 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b ULONG holder2;
8ccc0 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
8ccd0 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 turn;. assert(
8cce0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
8ccf0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
8cd00 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 os2MutexNotheld
8cd10 28 70 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 (p) );. DosRequ
8cd20 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d estMutexSem(p->m
8cd30 75 74 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 utex, SEM_INDEFI
8cd40 4e 49 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f NITE_WAIT);. Do
8cd50 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
8cd60 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 ->mutex, &holder
8cd70 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 1, &tid, &holder
8cd80 32 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 2);. p->owner =
8cd90 20 74 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b tid;. p->nRef+
8cda0 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 +;.}.static int
8cdb0 6f 73 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 os2MutexTry(sqli
8cdc0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
8cdd0 20 69 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 int rc;. TID t
8cde0 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 id;. PID holder
8cdf0 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 1;. ULONG holde
8ce00 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 r2;. if( p==0 )
8ce10 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
8ce20 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e K;. assert( p->
8ce30 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
8ce40 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 _RECURSIVE || os
8ce50 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 2MutexNotheld(p)
8ce60 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 );. if( DosReq
8ce70 75 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e uestMutexSem(p->
8ce80 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 mutex, SEM_IMMED
8ce90 49 41 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 IATE_RETURN) ==
8cea0 4e 4f 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 NO_ERROR) {.
8ceb0 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d DosQueryMutexSem
8cec0 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 (p->mutex, &hold
8ced0 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 er1, &tid, &hold
8cee0 65 72 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e er2);. p->own
8cef0 65 72 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d er = tid;. p-
8cf00 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 >nRef++;. rc
8cf10 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
8cf20 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d else {. rc =
8cf30 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
8cf40 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
8cf50 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
8cf60 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
8cf70 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
8cf80 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
8cf90 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
8cfa0 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
8cfb0 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
8cfc0 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 behavior.** is
8cfd0 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
8cfe0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 mutex is not cu
8cff0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
8d000 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 or.** is not cur
8d010 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
8d020 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
8d030 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a ever do either..
8d040 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f */.static void o
8d050 73 32 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c s2MutexLeave(sql
8d060 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
8d070 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 TID tid;. PID
8d080 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e holder1;. ULON
8d090 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 G holder2;. if(
8d0a0 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a p==0 ) return;.
8d0b0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
8d0c0 66 3e 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 f>0 );. DosQuer
8d0d0 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 yMutexSem(p->mut
8d0e0 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 ex, &holder1, &t
8d0f0 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 id, &holder2);.
8d100 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 assert( p->owne
8d110 72 3d 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e r==tid );. p->n
8d120 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 Ref--;. assert(
8d130 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 p->nRef==0 || p
8d140 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
8d150 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a EX_RECURSIVE );.
8d160 20 20 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 DosReleaseMute
8d170 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a xSem(p->mutex);.
8d180 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
8d190 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 _DEBUG./*.** The
8d1a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
8d1b0 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 eld() and sqlite
8d1c0 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
8d1d0 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a ) routine are.**
8d1e0 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 intended for us
8d1f0 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 e inside assert(
8d200 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f ) statements..*/
8d210 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d .static int os2M
8d220 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 utexHeld(sqlite3
8d230 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 _mutex *p){. TI
8d240 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 D tid;. PID pid
8d250 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e ;. ULONG ulCoun
8d260 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a t;. PTIB ptib;.
8d270 20 20 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 if( p!=0 ) {.
8d280 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 DosQueryMutex
8d290 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 Sem(p->mutex, &p
8d2a0 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 id, &tid, &ulCou
8d2b0 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a nt);. } else {.
8d2c0 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
8d2d0 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c ocks(&ptib, NULL
8d2e0 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 );. tid = pti
8d2f0 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 b->tib_ptib2->ti
8d300 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 b2_ultid;. }.
8d310 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 return p==0 || (
8d320 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d p->nRef!=0 && p-
8d330 3e 6f 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a >owner==tid);.}.
8d340 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 static int os2Mu
8d350 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 texNotheld(sqlit
8d360 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
8d370 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 TID tid;. PID p
8d380 69 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f id;. ULONG ulCo
8d390 75 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 unt;. PTIB ptib
8d3a0 3b 0a 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 ;. if( p!= 0 )
8d3b0 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 {. DosQueryMu
8d3c0 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c texSem(p->mutex,
8d3d0 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c &pid, &tid, &ul
8d3e0 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 Count);. } else
8d3f0 20 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 {. DosGetInf
8d400 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e oBlocks(&ptib, N
8d410 55 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 ULL);. tid =
8d420 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d ptib->tib_ptib2-
8d430 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d >tib2_ultid;. }
8d440 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c . return p==0 |
8d450 7c 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 | p->nRef==0 ||
8d460 70 2d 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d p->owner!=tid;.}
8d470 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f .#endif..SQLITE_
8d480 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
8d490 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 mutex_methods *s
8d4a0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 qlite3DefaultMut
8d4b0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ex(void){. stat
8d4c0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
8d4d0 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 _methods sMutex
8d4e0 3d 20 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 = {. os2Mutex
8d4f0 49 6e 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 Init,. os2Mut
8d500 65 78 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 exEnd,. os2Mu
8d510 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 texAlloc,. os
8d520 32 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 2MutexFree,.
8d530 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 os2MutexEnter,.
8d540 20 20 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a os2MutexTry,.
8d550 20 20 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 os2MutexLeav
8d560 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 e,.#ifdef SQLITE
8d570 5f 44 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 _DEBUG. os2Mu
8d580 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 texHeld,. os2
8d590 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e MutexNotheld.#en
8d5a0 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 dif. };.. retu
8d5b0 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 rn &sMutex;.}.#e
8d5c0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
8d5d0 55 54 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a UTEX_OS2 */../**
8d5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
8d5f0 20 6f 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 of mutex_os2.c
8d600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
8d630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
8d640 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e in file mutex_un
8d650 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ix.c ***********
8d660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
8d680 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 ** 2007 August 2
8d690 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 8.**.** The auth
8d6a0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
8d6b0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
8d6c0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
8d6d0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
8d6e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
8d6f0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
8d700 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
8d710 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
8d720 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
8d730 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
8d740 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
8d750 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
8d760 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
8d770 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
8d780 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
8d790 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
8d7a0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
8d7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d7f0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
8d800 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
8d810 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
8d820 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f ement mutexes fo
8d830 72 20 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a r pthreads.**.**
8d840 20 24 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 $Id: mutex_unix
8d850 2e 63 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 .c,v 1.16 2008/1
8d860 32 2f 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72 2/08 18:19:18 dr
8d870 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a h Exp $.*/../*.*
8d880 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 * The code in th
8d890 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 is file is only
8d8a0 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 used if we are c
8d8b0 6f 6d 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 ompiling threads
8d8c0 61 66 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 afe.** under uni
8d8d0 78 20 77 69 74 68 20 70 74 68 72 65 61 64 73 2e x with pthreads.
8d8e0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
8d8f0 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 this implementa
8d900 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 tion requires a
8d910 76 65 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 version of pthre
8d920 61 64 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 ads that.** supp
8d930 6f 72 74 73 20 72 65 63 75 72 73 69 76 65 20 6d orts recursive m
8d940 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 utexes..*/.#ifde
8d950 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 f SQLITE_MUTEX_P
8d960 54 48 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 THREADS..#includ
8d970 65 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a e <pthread.h>...
8d980 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 /*.** Each recur
8d990 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e sive mutex is an
8d9a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
8d9b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
8d9c0 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ture..*/.struct
8d9d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a sqlite3_mutex {.
8d9e0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
8d9f0 74 20 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 t mutex; /*
8da00 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e Mutex controllin
8da10 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 g the lock */.
8da20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 int id;
8da30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 /* Mu
8da40 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e tex type */. in
8da50 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
8da60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
8da70 65 72 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 er of entrances
8da80 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f */. pthread_t o
8da90 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 wner;
8daa0 2f 2a 20 54 68 72 65 61 64 20 74 68 61 74 20 69 /* Thread that i
8dab0 73 20 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 s within this mu
8dac0 74 65 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 tex */.#ifdef SQ
8dad0 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 LITE_DEBUG. int
8dae0 20 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 trace;
8daf0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
8db00 74 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 to trace changes
8db10 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 */.#endif.};.#i
8db20 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
8db30 47 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 G.#define SQLITE
8db40 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
8db50 5a 45 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 ZER { PTHREAD_MU
8db60 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
8db70 20 30 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 0, 0, (pthread_
8db80 74 29 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 t)0, 0 }.#else.#
8db90 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d define SQLITE3_M
8dba0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
8dbb0 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 { PTHREAD_MUTEX
8dbc0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c _INITIALIZER, 0,
8dbd0 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 0, (pthread_t)0
8dbe0 20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a }.#endif../*.**
8dbf0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
8dc00 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 ex_held() and sq
8dc10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
8dc20 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 eld() routine ar
8dc30 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f e.** intended fo
8dc40 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 r use only insid
8dc50 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 e assert() state
8dc60 6d 65 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 ments. On some
8dc70 70 6c 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 platforms,.** th
8dc80 65 72 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 ere might be rac
8dc90 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 e conditions tha
8dca0 74 20 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 t can cause thes
8dcb0 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a e routines to.**
8dcc0 20 64 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 deliver incorre
8dcd0 63 74 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 ct results. In
8dce0 70 61 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 particular, if p
8dcf0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 thread_equal() i
8dd00 73 0a 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d s.** not an atom
8dd10 69 63 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 ic operation, th
8dd20 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 en these routine
8dd30 73 20 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 s might delivery
8dd40 0a 2a 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 .** incorrect re
8dd50 73 75 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 sults. On most
8dd60 70 6c 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 platforms, pthre
8dd70 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 ad_equal() is a
8dd80 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f .** comparison o
8dd90 66 20 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 f two integers a
8dda0 6e 64 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 nd is therefore
8ddb0 61 74 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 atomic. But we
8ddc0 61 72 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 are.** told that
8ddd0 20 48 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 HPUX is not suc
8dde0 68 20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 h a platform. I
8ddf0 66 20 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 f so, then these
8de00 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c routines.** wil
8de10 6c 20 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 l not always wor
8de20 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 k correctly on H
8de30 50 55 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 PUX..**.** On th
8de40 6f 73 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 ose platforms wh
8de50 65 72 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 ere pthread_equa
8de60 6c 28 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 l() is not atomi
8de70 63 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f c, SQLite.** sho
8de80 75 6c 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 uld be compiled
8de90 77 69 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 without -DSQLITE
8dea0 5f 44 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 _DEBUG and with
8deb0 2d 44 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d -DNDEBUG to.** m
8dec0 61 6b 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 ake sure no asse
8ded0 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
8dee0 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e are evaluated an
8def0 64 20 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a d hence these.**
8df00 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 routines are ne
8df10 76 65 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 ver called..*/.#
8df20 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
8df30 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 UG) || defined(S
8df40 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 QLITE_DEBUG).sta
8df50 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d tic int pthreadM
8df60 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 utexHeld(sqlite3
8df70 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
8df80 74 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 turn (p->nRef!=0
8df90 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 && pthread_equa
8dfa0 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 l(p->owner, pthr
8dfb0 65 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a ead_self()));.}.
8dfc0 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 static int pthre
8dfd0 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 adMutexNotheld(s
8dfe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
8dff0 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 {. return p->nR
8e000 65 66 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 ef==0 || pthread
8e010 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c _equal(p->owner,
8e020 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 pthread_self())
8e030 3d 3d 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ==0;.}.#endif../
8e040 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
8e050 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 and deinitialize
8e060 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 the mutex subsy
8e070 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 stem..*/.static
8e080 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 int pthreadMutex
8e090 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 Init(void){ retu
8e0a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
8e0b0 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 static int pthre
8e0c0 61 64 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 adMutexEnd(void)
8e0d0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
8e0e0 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 OK; }../*.** The
8e0f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
8e100 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 lloc() routine a
8e110 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a llocates a new.*
8e120 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 * mutex and retu
8e130 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
8e140 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 it. If it retu
8e150 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 rns NULL.** that
8e160 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 means that a mu
8e170 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 tex could not be
8e180 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c allocated. SQL
8e190 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 ite.** will unwi
8e1a0 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 nd its stack and
8e1b0 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 return an error
8e1c0 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a . The argument.
8e1d0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 ** to sqlite3_mu
8e1e0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f tex_alloc() is o
8e1f0 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 ne of these inte
8e200 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a ger constants:.*
8e210 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
8e220 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
8e230 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 FAST.** <li> SQ
8e240 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
8e250 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 SIVE.** <li> SQ
8e260 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
8e270 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e C_MASTER.** <li>
8e280 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
8e290 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 TATIC_MEM.** <li
8e2a0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
8e2b0 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c STATIC_MEM2.** <
8e2c0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
8e2d0 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a X_STATIC_PRNG.**
8e2e0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
8e2f0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a TEX_STATIC_LRU.*
8e300 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
8e310 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 e first two cons
8e320 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 tants cause sqli
8e330 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
8e340 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 ) to create.** a
8e350 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 new mutex. The
8e360 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 new mutex is re
8e370 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c cursive when SQL
8e380 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
8e390 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 IVE.** is used b
8e3a0 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 ut not necessari
8e3b0 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 ly so when SQLIT
8e3c0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 E_MUTEX_FAST is
8e3d0 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 used..** The mut
8e3e0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ex implementatio
8e3f0 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
8e400 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e to make a distin
8e410 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e ction.** between
8e420 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
8e430 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 CURSIVE and SQLI
8e440 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 TE_MUTEX_FAST if
8e450 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
8e460 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 want to. But SQ
8e470 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 Lite will only r
8e480 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 equest a recursi
8e490 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 ve mutex in.** c
8e4a0 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 ases where it re
8e4b0 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 ally needs one.
8e4c0 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e If a faster non
8e4d0 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 -recursive mutex
8e4e0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
8e4f0 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 on is available
8e500 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 on the host plat
8e510 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 form, the mutex
8e520 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 subsystem.** mig
8e530 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 ht return such a
8e540 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e mutex in respon
8e550 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 se to SQLITE_MUT
8e560 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 EX_FAST..**.** T
8e570 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 he other allowed
8e580 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 parameters to s
8e590 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
8e5a0 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e oc() each return
8e5b0 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f .** a pointer to
8e5c0 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 a static preexi
8e5d0 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 sting mutex. Th
8e5e0 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 ree static mutex
8e5f0 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 es are.** used b
8e600 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 y the current ve
8e610 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e rsion of SQLite.
8e620 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e Future version
8e630 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d s of SQLite.** m
8e640 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 ay add additiona
8e650 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 l static mutexes
8e660 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 . Static mutexe
8e670 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e s are for intern
8e680 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c al.** use by SQL
8e690 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 ite only. Appli
8e6a0 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 cations that use
8e6b0 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 SQLite mutexes
8e6c0 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e should.** use on
8e6d0 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d ly the dynamic m
8e6e0 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 utexes returned
8e6f0 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f by SQLITE_MUTEX_
8e700 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 FAST or.** SQLIT
8e710 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
8e720 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 E..**.** Note th
8e730 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 at if one of the
8e740 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 dynamic mutex p
8e750 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 arameters (SQLIT
8e760 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 E_MUTEX_FAST.**
8e770 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f or SQLITE_MUTEX_
8e780 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 RECURSIVE) is us
8e790 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f ed then sqlite3_
8e7a0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a mutex_alloc().**
8e7b0 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 returns a diffe
8e7c0 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 rent mutex on ev
8e7d0 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 ery call. But f
8e7e0 6f 72 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a or the static .*
8e7f0 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 * mutex types, t
8e800 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 he same mutex is
8e810 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 returned on eve
8e820 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 ry call that has
8e830 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 .** the same typ
8e840 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
8e850 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tic sqlite3_mute
8e860 78 20 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 x *pthreadMutexA
8e870 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b lloc(int iType){
8e880 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
8e890 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 3_mutex staticMu
8e8a0 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 texes[] = {.
8e8b0 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e SQLITE3_MUTEX_IN
8e8c0 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 ITIALIZER,. S
8e8d0 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 QLITE3_MUTEX_INI
8e8e0 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 TIALIZER,. SQ
8e8f0 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 LITE3_MUTEX_INIT
8e900 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c IALIZER,. SQL
8e910 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
8e920 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 ALIZER,. SQLI
8e930 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
8e940 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 LIZER,. SQLIT
8e950 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
8e960 49 5a 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 IZER. };. sqli
8e970 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 te3_mutex *p;.
8e980 73 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b switch( iType ){
8e990 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
8e9a0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
8e9b0 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 : {. p = sq
8e9c0 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 lite3MallocZero(
8e9d0 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 sizeof(*p) );.
8e9e0 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 if( p ){.#i
8e9f0 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 fdef SQLITE_HOME
8ea00 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f GROWN_RECURSIVE_
8ea10 4d 55 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a MUTEX. /*
8ea20 20 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 If recursive mu
8ea30 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 texes are not av
8ea40 61 69 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c ailable, we will
8ea50 20 68 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 have to.
8ea60 20 2a 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 ** build our ow
8ea70 6e 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a n. See below. *
8ea80 2f 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 /. pthrea
8ea90 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d d_mutex_init(&p-
8eaa0 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 >mutex, 0);.#els
8eab0 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 e. /* Use
8eac0 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 a recursive mut
8ead0 65 78 20 69 66 20 69 74 20 69 73 20 61 76 61 69 ex if it is avai
8eae0 6c 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 lable */.
8eaf0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 pthread_mutexat
8eb00 74 72 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 tr_t recursiveAt
8eb10 74 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 tr;. pthr
8eb20 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e ead_mutexattr_in
8eb30 69 74 28 26 72 65 63 75 72 73 69 76 65 41 74 74 it(&recursiveAtt
8eb40 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 r);. pthr
8eb50 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 ead_mutexattr_se
8eb60 74 74 79 70 65 28 26 72 65 63 75 72 73 69 76 65 ttype(&recursive
8eb70 41 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 Attr, PTHREAD_MU
8eb80 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a TEX_RECURSIVE);.
8eb90 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f pthread_
8eba0 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d mutex_init(&p->m
8ebb0 75 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 utex, &recursive
8ebc0 41 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 Attr);. p
8ebd0 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 thread_mutexattr
8ebe0 5f 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 _destroy(&recurs
8ebf0 69 76 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 iveAttr);.#endif
8ec00 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d . p->id =
8ec10 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a iType;. }.
8ec20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
8ec30 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 }. case SQLI
8ec40 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b TE_MUTEX_FAST: {
8ec50 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 . p = sqlit
8ec60 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 e3MallocZero( si
8ec70 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 zeof(*p) );.
8ec80 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 if( p ){.
8ec90 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 p->id = iType
8eca0 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 ;. pthrea
8ecb0 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d d_mutex_init(&p-
8ecc0 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 >mutex, 0);.
8ecd0 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
8ece0 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
8ecf0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 lt: {. asse
8ed00 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 rt( iType-2 >= 0
8ed10 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
8ed20 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 ( iType-2 < Arra
8ed30 79 53 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65 ySize(staticMute
8ed40 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 xes) );. p
8ed50 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 = &staticMutexes
8ed60 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 [iType-2];.
8ed70 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
8ed80 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
8ed90 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
8eda0 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 p;.}.../*.** Thi
8edb0 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f s routine deallo
8edc0 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 cates a previous
8edd0 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 ly.** allocated
8ede0 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 mutex. SQLite i
8edf0 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 s careful to dea
8ee00 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a llocate every.**
8ee10 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 mutex that it a
8ee20 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 llocates..*/.sta
8ee30 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 tic void pthread
8ee40 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 MutexFree(sqlite
8ee50 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 3_mutex *p){. a
8ee60 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
8ee70 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
8ee80 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
8ee90 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 EX_FAST || p->id
8eea0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
8eeb0 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 ECURSIVE );. pt
8eec0 68 72 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 hread_mutex_dest
8eed0 72 6f 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a roy(&p->mutex);.
8eee0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
8eef0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 );.}../*.** The
8ef00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
8ef10 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ter() and sqlite
8ef20 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
8ef30 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a utines attempt.*
8ef40 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 * to enter a mut
8ef50 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 ex. If another
8ef60 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 thread is alread
8ef70 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 y within the mut
8ef80 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ex,.** sqlite3_m
8ef90 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c utex_enter() wil
8efa0 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 l block and sqli
8efb0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
8efc0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 will return.** S
8efd0 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 QLITE_BUSY. The
8efe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
8eff0 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ry() interface r
8f000 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
8f010 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 .** upon success
8f020 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 ful entry. Mute
8f030 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e xes created usin
8f040 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 g SQLITE_MUTEX_R
8f050 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 ECURSIVE can.**
8f060 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 be entered multi
8f070 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 ple times by the
8f080 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 same thread. I
8f090 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 n such cases the
8f0a0 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 ,.** mutex must
8f0b0 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 be exited an equ
8f0c0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d al number of tim
8f0d0 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 es before anothe
8f0e0 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 r thread.** can
8f0f0 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 enter. If the s
8f100 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 ame thread tries
8f110 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 to enter any ot
8f120 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 her kind of mute
8f130 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f x.** more than o
8f140 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f nce, the behavio
8f150 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a r is undefined..
8f160 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
8f170 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 threadMutexEnter
8f180 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
8f190 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
8f1a0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
8f1b0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 X_RECURSIVE || p
8f1c0 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 threadMutexNothe
8f1d0 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 ld(p) );..#ifdef
8f1e0 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 SQLITE_HOMEGROW
8f1f0 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 N_RECURSIVE_MUTE
8f200 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 X. /* If recurs
8f210 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 ive mutexes are
8f220 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 not available, t
8f230 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 hen we have to g
8f240 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e row. ** our own
8f250 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e . This implemen
8f260 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 tation assumes t
8f270 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 hat pthread_equa
8f280 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d l(). ** is atom
8f290 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e ic - that it can
8f2a0 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20 not be deceived
8f2b0 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 into thinking se
8f2c0 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f lf. ** and p->o
8f2d0 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 wner are equal i
8f2e0 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 f p->owner chang
8f2f0 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 es between two v
8f300 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 alues. ** that
8f310 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f are not equal to
8f320 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 self while the
8f330 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 comparison is ta
8f340 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a king place.. **
8f350 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 This implementa
8f360 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 tion also assume
8f370 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 s a coherent cac
8f380 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 he - that . **
8f390 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 separate process
8f3a0 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 es cannot read d
8f3b0 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 ifferent values
8f3c0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 from the same.
8f3d0 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68 ** address at th
8f3e0 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 e same time. If
8f3f0 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 either of these
8f400 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a two conditions.
8f410 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 ** are not met
8f420 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 , then the mutex
8f430 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 es will fail and
8f440 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 problems will r
8f450 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a esult.. */. {.
8f460 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 pthread_t se
8f470 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c lf = pthread_sel
8f480 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e f();. if( p->
8f490 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 nRef>0 && pthrea
8f4a0 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 d_equal(p->owner
8f4b0 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 , self) ){.
8f4c0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
8f4d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 }else{. pth
8f4e0 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 read_mutex_lock(
8f4f0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 &p->mutex);.
8f500 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
8f510 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d f==0 );. p-
8f520 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 >owner = self;.
8f530 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 p->nRef = 1
8f540 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 ;. }. }.#els
8f550 65 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 e. /* Use the b
8f560 75 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 uilt-in recursiv
8f570 65 20 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 e mutexes if the
8f580 79 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e y are available.
8f590 0a 20 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f . */. pthread_
8f5a0 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d mutex_lock(&p->m
8f5b0 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 utex);. p->owne
8f5c0 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 r = pthread_self
8f5d0 28 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b ();. p->nRef++;
8f5e0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
8f5f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 SQLITE_DEBUG. i
8f600 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 f( p->trace ){.
8f610 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 printf("enter
8f620 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 77 mutex %p (%d) w
8f630 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 ith nRef=%d\n",
8f640 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e p, p->trace, p->
8f650 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 nRef);. }.#endi
8f660 66 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 f.}.static int p
8f670 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 28 73 threadMutexTry(s
8f680 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
8f690 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 {. int rc;. as
8f6a0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
8f6b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
8f6c0 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 IVE || pthreadMu
8f6d0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
8f6e0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
8f6f0 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 HOMEGROWN_RECURS
8f700 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 IVE_MUTEX. /* I
8f710 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 f recursive mute
8f720 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 xes are not avai
8f730 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 lable, then we h
8f740 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a ave to grow. **
8f750 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 our own. This
8f760 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 implementation a
8f770 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 ssumes that pthr
8f780 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a ead_equal(). **
8f790 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 is atomic - tha
8f7a0 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 t it cannot be d
8f7b0 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 eceived into thi
8f7c0 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 nking self. **
8f7d0 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 and p->owner are
8f7e0 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e equal if p->own
8f7f0 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 er changes betwe
8f800 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 en two values.
8f810 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 ** that are not
8f820 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 equal to self wh
8f830 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 ile the comparis
8f840 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 on is taking pla
8f850 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d ce.. ** This im
8f860 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 plementation als
8f870 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 o assumes a cohe
8f880 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 rent cache - tha
8f890 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 t . ** separate
8f8a0 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f processes canno
8f8b0 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 t read different
8f8c0 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 values from the
8f8d0 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 same. ** addre
8f8e0 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ss at the same t
8f8f0 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 ime. If either
8f900 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e of these two con
8f910 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 ditions. ** are
8f920 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 not met, then t
8f930 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 he mutexes will
8f940 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d fail and problem
8f950 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 s will result..
8f960 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 */. {. pthr
8f970 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 ead_t self = pth
8f980 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 read_self();.
8f990 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 if( p->nRef>0 &
8f9a0 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 & pthread_equal(
8f9b0 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 p->owner, self)
8f9c0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 ){. p->nRef
8f9d0 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ++;. rc = S
8f9e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 QLITE_OK;. }e
8f9f0 6c 73 65 20 69 66 28 20 70 74 68 72 65 61 64 5f lse if( pthread_
8fa00 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 mutex_trylock(&p
8fa10 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 ->mutex)==0 ){.
8fa20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
8fa30 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 nRef==0 );.
8fa40 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 p->owner = self
8fa50 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 ;. p->nRef
8fa60 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 1;. rc =
8fa70 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
8fa80 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
8fa90 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
8faa0 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 }. }.#else.
8fab0 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 /* Use the built
8fac0 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 -in recursive mu
8fad0 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61 72 texes if they ar
8fae0 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a e available.. *
8faf0 2f 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f /. if( pthread_
8fb00 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 mutex_trylock(&p
8fb10 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 ->mutex)==0 ){.
8fb20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 p->owner = pt
8fb30 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 hread_self();.
8fb40 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 p->nRef++;.
8fb50 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
8fb60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
8fb70 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
8fb80 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 }.#endif..#ifd
8fb90 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
8fba0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
8fbb0 5f 4f 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 _OK && p->trace
8fbc0 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 ){. printf("e
8fbd0 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 nter mutex %p (%
8fbe0 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c d) with nRef=%d\
8fbf0 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c n", p, p->trace,
8fc00 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 p->nRef);. }.#
8fc10 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 endif. return r
8fc20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 c;.}../*.** The
8fc30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
8fc40 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 ave() routine ex
8fc50 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 its a mutex that
8fc60 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 was.** previous
8fc70 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 ly entered by th
8fc80 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 e same thread.
8fc90 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 The behavior.**
8fca0 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 is undefined if
8fcb0 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 the mutex is not
8fcc0 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
8fcd0 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 ed or.** is not
8fce0 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 currently alloca
8fcf0 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c ted. SQLite wil
8fd00 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 l never do eithe
8fd10 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
8fd20 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 d pthreadMutexLe
8fd30 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ave(sqlite3_mute
8fd40 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 x *p){. assert(
8fd50 20 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c pthreadMutexHel
8fd60 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 d(p) );. p->nRe
8fd70 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 f--;. assert( p
8fd80 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
8fd90 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
8fda0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 _RECURSIVE );..#
8fdb0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d ifdef SQLITE_HOM
8fdc0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 EGROWN_RECURSIVE
8fdd0 5f 4d 55 54 45 58 0a 20 20 69 66 28 20 70 2d 3e _MUTEX. if( p->
8fde0 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 nRef==0 ){. p
8fdf0 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c thread_mutex_unl
8fe00 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a ock(&p->mutex);.
8fe10 20 20 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 }.#else. pthr
8fe20 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b ead_mutex_unlock
8fe30 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e (&p->mutex);.#en
8fe40 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
8fe50 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 TE_DEBUG. if( p
8fe60 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 ->trace ){. p
8fe70 72 69 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 74 rintf("leave mut
8fe80 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 ex %p (%d) with
8fe90 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 nRef=%d\n", p, p
8fea0 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 ->trace, p->nRef
8feb0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a );. }.#endif.}.
8fec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8fed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
8fee0 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 thods *sqlite3De
8fef0 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 faultMutex(void)
8ff00 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
8ff10 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
8ff20 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 sMutex = {.
8ff30 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 pthreadMutexInit
8ff40 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 ,. pthreadMut
8ff50 65 78 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 exEnd,. pthre
8ff60 61 64 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 adMutexAlloc,.
8ff70 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 pthreadMutexFr
8ff80 65 65 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d ee,. pthreadM
8ff90 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 utexEnter,. p
8ffa0 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 2c 0a threadMutexTry,.
8ffb0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 pthreadMutex
8ffc0 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51 Leave,.#ifdef SQ
8ffd0 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 LITE_DEBUG. p
8ffe0 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 2c threadMutexHeld,
8fff0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 . pthreadMute
90000 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 xNotheld.#else.
90010 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 0,. 0.#end
90020 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 if. };.. retur
90030 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 n &sMutex;.}..#e
90040 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
90050 55 54 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a UTEX_PTHREAD */.
90060 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
90070 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e End of mutex_un
90080 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ix.c ***********
90090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
900a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
900b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
900c0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
900d0 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a x_w32.c ********
900e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
900f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
90100 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
90110 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 14.**.** The
90120 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
90130 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
90140 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
90150 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
90160 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
90170 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
90180 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
90190 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
901a0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
901b0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
901c0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
901d0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
901e0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
901f0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
90200 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
90210 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
90220 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
90230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90270 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
90280 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 e contains the C
90290 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
902a0 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 implement mutexe
902b0 73 20 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a s for win32.**.*
902c0 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 * $Id: mutex_w32
902d0 2e 63 2c 76 20 31 2e 31 35 20 32 30 30 39 2f 30 .c,v 1.15 2009/0
902e0 31 2f 33 30 20 31 36 3a 30 39 3a 32 33 20 73 68 1/30 16:09:23 sh
902f0 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a ane Exp $.*/../*
90300 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 .** The code in
90310 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c this file is onl
90320 79 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65 y used if we are
90330 20 63 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 compiling multi
90340 74 68 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 threaded.** on a
90350 20 77 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a win32 system..*
90360 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
90370 4d 55 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a MUTEX_W32../*.**
90380 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 Each recursive
90390 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 mutex is an inst
903a0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
903b0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
903c0 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 .*/.struct sqlit
903d0 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 e3_mutex {. CRI
903e0 54 49 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 TICAL_SECTION mu
903f0 74 65 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 tex; /* Mutex
90400 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 controlling the
90410 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 lock */. int i
90420 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
90430 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 /* Mutex t
90440 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 ype */. int nRe
90450 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
90460 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
90470 20 65 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 enterances */.
90480 20 44 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 DWORD owner;
90490 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
904a0 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 hread holding th
904b0 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a is mutex */.};..
904c0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
904d0 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 e (non-zero) if
904e0 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 we are running u
904f0 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 nder WinNT, Win2
90500 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 K, WinXP,.** or
90510 57 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 WinCE. Return f
90520 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 alse (zero) for
90530 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 Win95, Win98, or
90540 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 WinME..**.** He
90550 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 re is an interes
90560 74 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e ting observation
90570 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c : Win95, Win98,
90580 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a and WinME lack.
90590 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 ** the LockFileE
905a0 78 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 x() API. But we
905b0 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 can still stati
905c0 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e cally link again
905d0 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 st that.** API a
905e0 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e s long as we don
905f0 27 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 't call it win r
90600 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f unning Win95/98/
90610 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a ME. A call to.*
90620 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * this routine i
90630 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
90640 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 ine if the host
90650 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f is Win95/98/ME o
90660 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 r.** WinNT/2K/XP
90670 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c so that we will
90680 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 know whether or
90690 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 not we can safe
906a0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c ly call.** the L
906b0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
906c0 0a 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 .**.** mutexIsNT
906d0 28 29 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 () is only used
906e0 66 6f 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 for the TryEnter
906f0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
90700 29 20 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 ) API call,.** w
90710 68 69 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 hich is only ava
90720 69 6c 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 ilable if your a
90730 70 70 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 pplication was c
90740 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a ompiled with .**
90750 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 _WIN32_WINNT de
90760 66 69 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 fined to a value
90770 20 3e 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 >= 0x0400. Cur
90780 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 rently, the only
90790 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 .** call to TryE
907a0 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 nterCriticalSect
907b0 69 6f 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 ion() is #ifdef'
907c0 65 64 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 ed out, so #ifde
907d0 66 20 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 f .** this out a
907e0 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 s well..*/.#if 0
907f0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
90800 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 INCE.# define mu
90810 74 65 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 texIsNT() (1).#
90820 65 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e else. static in
90830 74 20 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 t mutexIsNT(void
90840 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e ){. static in
90850 74 20 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 t osType = 0;.
90860 20 20 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 if( osType==0
90870 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 ){. OSVERSI
90880 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 ONINFO sInfo;.
90890 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 sInfo.dwOSVe
908a0 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 rsionInfoSize =
908b0 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 sizeof(sInfo);.
908c0 20 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 GetVersionE
908d0 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 x(&sInfo);.
908e0 20 6f 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e osType = sInfo.
908f0 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 dwPlatformId==VE
90900 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 R_PLATFORM_WIN32
90910 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 _NT ? 2 : 1;.
90920 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 }. return os
90930 54 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e Type==2;. }.#en
90940 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 dif /* SQLITE_OS
90950 5f 57 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 _WINCE */.#endif
90960 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
90970 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 DEBUG./*.** The
90980 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
90990 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 ld() and sqlite3
909a0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
909b0 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 routine are.**
909c0 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 intended for use
909d0 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 only inside ass
909e0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
909f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
90a00 77 69 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c winMutexHeld(sql
90a10 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
90a20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 return p->nRef
90a30 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d !=0 && p->owner=
90a40 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 =GetCurrentThrea
90a50 64 49 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 dId();.}.static
90a60 69 6e 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 int winMutexNoth
90a70 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 eld(sqlite3_mute
90a80 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 x *p){. return
90a90 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d p->nRef==0 || p-
90aa0 3e 6f 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 >owner!=GetCurre
90ab0 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a ntThreadId();.}.
90ac0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 #endif.../*.** I
90ad0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 nitialize and de
90ae0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d initialize the m
90af0 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a utex subsystem..
90b00 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
90b10 6e 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 nMutexInit(void)
90b20 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
90b30 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 OK; }.static int
90b40 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69 winMutexEnd(voi
90b50 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
90b60 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK; }../*.** T
90b70 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
90b80 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 _alloc() routine
90b90 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 allocates a new
90ba0 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 .** mutex and re
90bb0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
90bc0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 to it. If it re
90bd0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 turns NULL.** th
90be0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 at means that a
90bf0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 mutex could not
90c00 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 be allocated. S
90c10 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e QLite.** will un
90c20 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 wind its stack a
90c30 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 nd return an err
90c40 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e or. The argumen
90c50 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f t.** to sqlite3_
90c60 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 mutex_alloc() is
90c70 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e one of these in
90c80 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a teger constants:
90c90 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
90ca0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
90cb0 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20 X_FAST
90cc0 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 0.** <li>
90cd0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
90ce0 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20 20 URSIVE
90cf0 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 1.** <li> SQLIT
90d00 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
90d10 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a 20 ASTER 2.**
90d20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
90d30 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 EX_STATIC_MEM
90d40 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 3.** <li>
90d50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
90d60 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 20 ATIC_PRNG
90d70 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 4.** </ul>.**.*
90d80 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 * The first two
90d90 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 constants cause
90da0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
90db0 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a loc() to create.
90dc0 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 ** a new mutex.
90dd0 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 The new mutex i
90de0 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e s recursive when
90df0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
90e00 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 CURSIVE.** is us
90e10 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 ed but not neces
90e20 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 sarily so when S
90e30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
90e40 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 is used..** The
90e50 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 mutex implement
90e60 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e ation does not n
90e70 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 eed to make a di
90e80 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 stinction.** bet
90e90 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 ween SQLITE_MUTE
90ea0 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 X_RECURSIVE and
90eb0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
90ec0 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 T if it does.**
90ed0 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 not want to. Bu
90ee0 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e t SQLite will on
90ef0 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63 ly request a rec
90f00 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a ursive mutex in.
90f10 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69 ** cases where i
90f20 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f t really needs o
90f30 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 72 ne. If a faster
90f40 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d non-recursive m
90f50 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e utex.** implemen
90f60 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 tation is availa
90f70 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 ble on the host
90f80 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 platform, the mu
90f90 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a tex subsystem.**
90fa0 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 might return su
90fb0 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 ch a mutex in re
90fc0 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 sponse to SQLITE
90fd0 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a _MUTEX_FAST..**.
90fe0 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c ** The other all
90ff0 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 owed parameters
91000 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
91010 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 _alloc() each re
91020 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 turn.** a pointe
91030 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 r to a static pr
91040 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e eexisting mutex.
91050 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20 6d Three static m
91060 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 utexes are.** us
91070 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e ed by the curren
91080 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c t version of SQL
91090 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 ite. Future ver
910a0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a sions of SQLite.
910b0 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 ** may add addit
910c0 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 ional static mut
910d0 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 exes. Static mu
910e0 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e texes are for in
910f0 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 ternal.** use by
91100 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 SQLite only. A
91110 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
91120 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 use SQLite mute
91130 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 xes should.** us
91140 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d e only the dynam
91150 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 ic mutexes retur
91160 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 ned by SQLITE_MU
91170 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 TEX_FAST or.** S
91180 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
91190 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 RSIVE..**.** Not
911a0 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 e that if one of
911b0 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 the dynamic mut
911c0 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 ex parameters (S
911d0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
911e0 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 .** or SQLITE_MU
911f0 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 TEX_RECURSIVE) i
91200 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 s used then sqli
91210 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
91220 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 ).** returns a d
91230 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f ifferent mutex o
91240 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 n every call. B
91250 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 ut for the stati
91260 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 c .** mutex type
91270 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 s, the same mute
91280 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e x is returned on
91290 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 every call that
912a0 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 has.** the same
912b0 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f type number..*/
912c0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f .static sqlite3_
912d0 6d 75 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41 mutex *winMutexA
912e0 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b lloc(int iType){
912f0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
91300 20 2a 70 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 *p;.. switch(
91310 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 iType ){. cas
91320 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 e SQLITE_MUTEX_F
91330 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 AST:. case SQ
91340 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
91350 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 SIVE: {. p
91360 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a = sqlite3MallocZ
91370 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 ero( sizeof(*p)
91380 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 );. if( p )
91390 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 {. p->id
913a0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 = iType;.
913b0 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69 InitializeCriti
913c0 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d calSection(&p->m
913d0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 utex);. }.
913e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
913f0 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
91400 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71 . static sq
91410 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74 lite3_mutex stat
91420 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b 0a 20 20 icMutexes[6];.
91430 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 69 static int i
91440 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 sInit = 0;.
91450 20 77 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 while( !isInit
91460 29 7b 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 ){. stati
91470 63 20 6c 6f 6e 67 20 6c 6f 63 6b 20 3d 20 30 3b c long lock = 0;
91480 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 6e 74 . if( Int
91490 65 72 6c 6f 63 6b 65 64 49 6e 63 72 65 6d 65 6e erlockedIncremen
914a0 74 28 26 6c 6f 63 6b 29 3d 3d 31 20 29 7b 0a 20 t(&lock)==1 ){.
914b0 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a int i;.
914c0 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d for(i=
914d0 30 3b 20 69 3c 73 69 7a 65 6f 66 28 73 74 61 74 0; i<sizeof(stat
914e0 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f icMutexes)/sizeo
914f0 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b f(staticMutexes[
91500 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 0]); i++){.
91510 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a Initializ
91520 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e eCriticalSection
91530 28 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b (&staticMutexes[
91540 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 i].mutex);.
91550 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
91560 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 isInit = 1;.
91570 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
91580 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a Sleep(1);.
91590 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
915a0 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
915b0 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a iType-2 >= 0 );.
915c0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 assert( iT
915d0 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73 ype-2 < sizeof(s
915e0 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 taticMutexes)/si
915f0 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 zeof(staticMutex
91600 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 es[0]) );.
91610 70 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 p = &staticMutex
91620 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 es[iType-2];.
91630 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 p->id = iType
91640 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
91650 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
91660 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 n p;.}.../*.** T
91670 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c his routine deal
91680 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f locates a previo
91690 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 usly.** allocate
916a0 64 20 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 d mutex. SQLite
916b0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 is careful to d
916c0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a eallocate every.
916d0 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 ** mutex that it
916e0 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 allocates..*/.s
916f0 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 tatic void winMu
91700 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
91710 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
91720 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 ert( p );. asse
91730 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 rt( p->nRef==0 )
91740 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
91750 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
91760 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 FAST || p->id==S
91770 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
91780 52 53 49 56 45 20 29 3b 0a 20 20 44 65 6c 65 74 RSIVE );. Delet
91790 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e eCriticalSection
917a0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 (&p->mutex);. s
917b0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
917c0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
917d0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
917e0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
917f0 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 utex_try() routi
91800 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 nes attempt.** t
91810 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e o enter a mutex.
91820 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 If another thr
91830 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
91840 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
91850 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
91860 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
91870 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
91880 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
91890 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
918a0 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 TE_BUSY. The sq
918b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
918c0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
918d0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a rns SQLITE_OK.**
918e0 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c upon successful
918f0 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 entry. Mutexes
91900 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 created using S
91910 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
91920 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 RSIVE can.** be
91930 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 entered multiple
91940 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 times by the sa
91950 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 me thread. In s
91960 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
91970 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
91980 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
91990 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
919a0 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
919b0 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
919c0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 er. If the same
919d0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f thread tries to
919e0 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 enter any other
919f0 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a kind of mutex.*
91a00 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 * more than once
91a10 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
91a20 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
91a30 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d static void winM
91a40 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 utexEnter(sqlite
91a50 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 3_mutex *p){. a
91a60 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
91a70 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
91a80 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 SIVE || winMutex
91a90 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
91aa0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
91ab0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b tion(&p->mutex);
91ac0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 . p->owner = Ge
91ad0 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 tCurrentThreadId
91ae0 28 29 3b 20 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b (); . p->nRef++
91af0 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 ;.}.static int w
91b00 69 6e 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 inMutexTry(sqlit
91b10 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
91b20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
91b30 42 55 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20 BUSY;. assert(
91b40 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
91b50 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
91b60 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 winMutexNotheld
91b70 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a (p) );. /*. **
91b80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
91b90 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 ex_try() routine
91ba0 20 69 73 20 76 65 72 79 20 72 61 72 65 6c 79 20 is very rarely
91bb0 75 73 65 64 2c 20 61 6e 64 20 77 68 65 6e 20 69 used, and when i
91bc0 74 0a 20 20 2a 2a 20 69 73 20 75 73 65 64 20 69 t. ** is used i
91bd0 74 20 69 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f t is merely an o
91be0 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f ptimization. So
91bf0 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 69 74 it is OK for it
91c00 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 to always. **
91c10 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a fail. . **. *
91c20 2a 20 54 68 65 20 54 72 79 45 6e 74 65 72 43 72 * The TryEnterCr
91c30 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 iticalSection()
91c40 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c interface is onl
91c50 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57 y available on W
91c60 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73 inNT.. ** And s
91c70 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70 ome windows comp
91c80 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69 ilers complain i
91c90 66 20 79 6f 75 20 74 72 79 20 74 6f 20 75 73 65 f you try to use
91ca0 20 69 74 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a it without. **
91cb0 20 66 69 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d first doing som
91cc0 65 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20 e #defines that
91cd0 70 72 65 76 65 6e 74 20 53 51 4c 69 74 65 20 66 prevent SQLite f
91ce0 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 rom building on
91cf0 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20 Win98.. ** For
91d00 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 that reason, we
91d10 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f will omit this o
91d20 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 ptimization for
91d30 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74 now. See. ** t
91d40 69 63 6b 65 74 20 23 32 36 38 35 2e 0a 20 20 2a icket #2685.. *
91d50 2f 0a 23 69 66 20 30 0a 20 20 69 66 28 20 6d 75 /.#if 0. if( mu
91d60 74 65 78 49 73 4e 54 28 29 20 26 26 20 54 72 79 texIsNT() && Try
91d70 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
91d80 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20 tion(&p->mutex)
91d90 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 ){. p->owner
91da0 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 = GetCurrentThre
91db0 61 64 49 64 28 29 3b 0a 20 20 20 20 70 2d 3e 6e adId();. p->n
91dc0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 Ref++;. rc =
91dd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 SQLITE_OK;. }.#
91de0 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 else. UNUSED_PA
91df0 52 41 4d 45 54 45 52 28 70 29 3b 0a 23 65 6e 64 RAMETER(p);.#end
91e00 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a if. return rc;.
91e10 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
91e20 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
91e30 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
91e40 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
91e50 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
91e60 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
91e70 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
91e80 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 behavior.** is
91e90 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
91ea0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 mutex is not cu
91eb0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
91ec0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 or.** is not cur
91ed0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
91ee0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
91ef0 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a ever do either..
91f00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
91f10 69 6e 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c inMutexLeave(sql
91f20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
91f30 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
91f40 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 f>0 );. assert(
91f50 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 p->owner==GetCu
91f60 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 20 rrentThreadId()
91f70 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a );. p->nRef--;.
91f80 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
91f90 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 f==0 || p->id==S
91fa0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
91fb0 52 53 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65 RSIVE );. Leave
91fc0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
91fd0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 &p->mutex);.}..S
91fe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
91ff0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
92000 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 ods *sqlite3Defa
92010 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a ultMutex(void){.
92020 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
92030 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 _mutex_methods s
92040 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 77 69 Mutex = {. wi
92050 6e 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 nMutexInit,.
92060 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 winMutexEnd,.
92070 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a winMutexAlloc,.
92080 20 20 20 20 77 69 6e 4d 75 74 65 78 46 72 65 65 winMutexFree
92090 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e ,. winMutexEn
920a0 74 65 72 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 ter,. winMute
920b0 78 54 72 79 2c 0a 20 20 20 20 77 69 6e 4d 75 74 xTry,. winMut
920c0 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 exLeave,.#ifdef
920d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
920e0 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 2c 0a 20 winMutexHeld,.
920f0 20 20 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 winMutexNothe
92100 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a ld.#else. 0,.
92110 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 0.#endif. }
92120 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 ;.. return &sMu
92130 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a tex;.}.#endif /*
92140 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
92150 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2 */../*********
92160 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 ***** End of mut
92170 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a ex_w32.c *******
92180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
921a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
921b0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
921c0 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a malloc.c ******
921d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
921e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
921f0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
92200 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
92210 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
92220 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
92230 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
92240 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
92250 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
92260 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
92270 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
92280 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
92290 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
922a0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
922b0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
922c0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
922d0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
922e0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
922f0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
92300 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
92310 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
92320 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
92330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
92370 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ** Memory alloca
92380 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 tion functions u
92390 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 sed throughout s
923a0 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 qlite..**.** $Id
923b0 3a 20 6d 61 6c 6c 6f 63 2e 63 2c 76 20 31 2e 36 : malloc.c,v 1.6
923c0 31 20 32 30 30 39 2f 30 33 2f 32 34 20 31 35 3a 1 2009/03/24 15:
923d0 30 38 3a 31 30 20 64 72 68 20 45 78 70 20 24 0a 08:10 drh Exp $.
923e0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 */../*.** This r
923f0 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e outine runs when
92400 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
92410 63 61 74 6f 72 20 73 65 65 73 20 74 68 61 74 20 cator sees that
92420 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d the.** total mem
92430 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 ory allocation i
92440 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65 s about to excee
92450 64 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a d the soft heap.
92460 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 ** limit..*/.sta
92470 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61 tic void softHea
92480 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a pLimitEnforcer(.
92490 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c void *NotUsed,
924a0 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 . sqlite3_int6
924b0 34 20 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 4 NotUsed2,. in
924c0 74 20 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 t allocSize.){.
924d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
924e0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
924f0 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 sed2);. sqlite3
92500 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
92510 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f allocSize);.}../
92520 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 *.** Set the sof
92530 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 t heap-size limi
92540 74 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 t for the librar
92550 79 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 y. Passing a zer
92560 6f 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 o or .** negativ
92570 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 e value indicate
92580 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 s no limit..*/.S
92590 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
925a0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 qlite3_soft_heap
925b0 5f 6c 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 _limit(int n){.
925c0 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 sqlite3_uint64
925d0 69 4c 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 iLimit;. int ov
925e0 65 72 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 erage;. if( n<0
925f0 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d ){. iLimit =
92600 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
92610 20 69 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d iLimit = n;. }
92620 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 . sqlite3_initi
92630 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 69 alize();. if( i
92640 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 73 Limit>0 ){. s
92650 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 qlite3MemoryAlar
92660 6d 28 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 m(softHeapLimitE
92670 6e 66 6f 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d nforcer, 0, iLim
92680 69 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 it);. }else{.
92690 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 sqlite3MemoryA
926a0 6c 61 72 6d 28 30 2c 20 30 2c 20 30 29 3b 0a 20 larm(0, 0, 0);.
926b0 20 7d 0a 20 20 6f 76 65 72 61 67 65 20 3d 20 28 }. overage = (
926c0 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 6d 65 6d int)(sqlite3_mem
926d0 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 28 69 36 ory_used() - (i6
926e0 34 29 6e 29 3b 0a 20 20 69 66 28 20 6f 76 65 72 4)n);. if( over
926f0 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c age>0 ){. sql
92700 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
92710 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 20 ory(overage);.
92720 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d }.}../*.** Attem
92730 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 75 70 pt to release up
92740 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 6e to n bytes of n
92750 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
92760 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a ory currently.**
92770 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65 2e held by SQLite.
92780 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e An example of n
92790 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
927a0 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 73 ory is memory us
927b0 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 64 ed to.** cache d
927c0 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68 atabase pages th
927d0 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 at are not curre
927e0 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a ntly in use..*/.
927f0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
92800 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
92810 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69 emory(int n){.#i
92820 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
92830 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 LE_MEMORY_MANAGE
92840 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20 MENT. int nRet
92850 3d 20 30 3b 0a 23 69 66 20 30 0a 20 20 6e 52 65 = 0;.#if 0. nRe
92860 74 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 t += sqlite3Vdbe
92870 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29 ReleaseMemory(n)
92880 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 52 65 74 20 ;.#endif. nRet
92890 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 += sqlite3Pcache
928a0 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d ReleaseMemory(n-
928b0 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 nRet);. return
928c0 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 4e nRet;.#else. UN
928d0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e USED_PARAMETER(n
928e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
928f0 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a TE_OK;.#endif.}.
92900 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66 ./*.** State inf
92910 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 ormation local t
92920 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c o the memory all
92930 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
92940 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c m..*/.static SQL
92950 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d ITE_WSD struct M
92960 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a em0Global {. /*
92970 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 Number of free
92980 70 61 67 65 73 20 66 6f 72 20 73 63 72 61 74 63 pages for scratc
92990 68 20 61 6e 64 20 70 61 67 65 2d 63 61 63 68 65 h and page-cache
929a0 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 32 memory */. u32
929b0 20 6e 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 nScratchFree;.
929c0 20 75 33 32 20 6e 50 61 67 65 46 72 65 65 3b 0a u32 nPageFree;.
929d0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
929e0 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 *mutex;
929f0 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 72 /* Mutex to ser
92a00 69 61 6c 69 7a 65 20 61 63 63 65 73 73 20 2a 2f ialize access */
92a10 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 .. /*. ** The
92a20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61 alarm callback a
92a30 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 nd its arguments
92a40 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65 . The mem0.mute
92a50 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a x lock will. **
92a60 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 74 be held while t
92a70 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 he callback is r
92a80 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 69 unning. Recursi
92a90 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 ve calls into.
92aa0 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 ** the memory su
92ab0 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f bsystem are allo
92ac0 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20 wed, but no new
92ad0 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62 callbacks will b
92ae0 65 0a 20 20 2a 2a 20 69 73 73 75 65 64 2e 20 20 e. ** issued.
92af0 54 68 65 20 61 6c 61 72 6d 42 75 73 79 20 76 61 The alarmBusy va
92b00 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f riable is set to
92b10 20 70 72 65 76 65 6e 74 20 72 65 63 75 72 73 69 prevent recursi
92b20 76 65 0a 20 20 2a 2a 20 63 61 6c 6c 62 61 63 6b ve. ** callback
92b30 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 s.. */. sqlite
92b40 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 72 3_int64 alarmThr
92b50 65 73 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28 eshold;. void (
92b60 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28 *alarmCallback)(
92b70 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 void*, sqlite3_i
92b80 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 nt64,int);. voi
92b90 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0a 20 20 69 d *alarmArg;. i
92ba0 6e 74 20 61 6c 61 72 6d 42 75 73 79 3b 0a 0a 20 nt alarmBusy;..
92bb0 20 2f 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72 /*. ** Pointer
92bc0 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 s to the end of
92bd0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
92be0 66 69 67 2e 70 53 63 72 61 74 63 68 20 61 6e 64 fig.pScratch and
92bf0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f . ** sqlite3Glo
92c00 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 balConfig.pPage
92c10 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 to a block of me
92c20 6d 6f 72 79 20 74 68 61 74 20 72 65 63 6f 72 64 mory that record
92c30 73 0a 20 20 2a 2a 20 77 68 69 63 68 20 70 61 67 s. ** which pag
92c40 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 es are available
92c50 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 .. */. u32 *aS
92c60 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 cratchFree;. u3
92c70 32 20 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 20 2 *aPageFree;.}
92c80 6d 65 6d 30 20 3d 20 7b 20 36 32 35 36 30 39 35 mem0 = { 6256095
92c90 35 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 5, 0, 0, 0, 0, 0
92ca0 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 , 0, 0, 0 };..#d
92cb0 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41 efine mem0 GLOBA
92cc0 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f L(struct Mem0Glo
92cd0 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a bal, mem0)../*.*
92ce0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
92cf0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
92d00 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f on subsystem..*/
92d10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
92d20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f int sqlite3Mallo
92d30 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 69 cInit(void){. i
92d40 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
92d50 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 Config.m.xMalloc
92d60 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
92d70 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
92d80 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 );. }. memset(
92d90 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 &mem0, 0, sizeof
92da0 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 73 (mem0));. if( s
92db0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
92dc0 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b ig.bCoreMutex ){
92dd0 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 20 . mem0.mutex
92de0 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
92df0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
92e00 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 _STATIC_MEM);.
92e10 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 }. if( sqlite3G
92e20 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 lobalConfig.pScr
92e30 61 74 63 68 20 26 26 20 73 71 6c 69 74 65 33 47 atch && sqlite3G
92e40 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 lobalConfig.szSc
92e50 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 20 ratch>=100.
92e60 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 && sqlite3Globa
92e70 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 lConfig.nScratch
92e80 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 >=0 ){. int i
92e90 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f ;. sqlite3Glo
92ea0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 balConfig.szScra
92eb0 74 63 68 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 tch = ROUNDDOWN8
92ec0 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
92ed0 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2d 34 nfig.szScratch-4
92ee0 29 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 );. mem0.aScr
92ef0 61 74 63 68 46 72 65 65 20 3d 20 28 75 33 32 2a atchFree = (u32*
92f00 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 )&((char*)sqlite
92f10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 3GlobalConfig.pS
92f20 63 72 61 74 63 68 29 0a 20 20 20 20 20 20 20 20 cratch).
92f30 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
92f40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
92f50 7a 53 63 72 61 74 63 68 2a 73 71 6c 69 74 65 33 zScratch*sqlite3
92f60 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 GlobalConfig.nSc
92f70 72 61 74 63 68 5d 3b 0a 20 20 20 20 66 6f 72 28 ratch];. for(
92f80 69 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c i=0; i<sqlite3Gl
92f90 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
92fa0 74 63 68 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e tch; i++){ mem0.
92fb0 61 53 63 72 61 74 63 68 46 72 65 65 5b 69 5d 20 aScratchFree[i]
92fc0 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e = i; }. mem0.
92fd0 6e 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 73 nScratchFree = s
92fe0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
92ff0 69 67 2e 6e 53 63 72 61 74 63 68 3b 0a 20 20 7d ig.nScratch;. }
93000 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
93010 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 3GlobalConfig.pS
93020 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 cratch = 0;.
93030 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
93040 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 fig.szScratch =
93050 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 0;. }. if( sql
93060 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
93070 2e 70 50 61 67 65 20 26 26 20 73 71 6c 69 74 65 .pPage && sqlite
93080 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
93090 50 61 67 65 3e 3d 35 31 32 0a 20 20 20 20 20 20 Page>=512.
930a0 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c && sqlite3Global
930b0 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3e 3d 31 20 Config.nPage>=1
930c0 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
930d0 20 20 69 6e 74 20 6f 76 65 72 68 65 61 64 3b 0a int overhead;.
930e0 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 52 4f 55 int sz = ROU
930f0 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 NDDOWN8(sqlite3G
93100 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 lobalConfig.szPa
93110 67 65 29 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d ge);. int n =
93120 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
93130 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 nfig.nPage;.
93140 6f 76 65 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 overhead = (4*n
93150 2b 20 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 + sz - 1)/sz;.
93160 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
93170 6f 6e 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f onfig.nPage -= o
93180 76 65 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d verhead;. mem
93190 30 2e 61 50 61 67 65 46 72 65 65 20 3d 20 28 75 0.aPageFree = (u
931a0 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 32*)&((char*)sql
931b0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
931c0 2e 70 50 61 67 65 29 0a 20 20 20 20 20 20 20 20 .pPage).
931d0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
931e0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
931f0 7a 50 61 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f zPage*sqlite3Glo
93200 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d balConfig.nPage]
93210 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
93220 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f <sqlite3GlobalCo
93230 6e 66 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 nfig.nPage; i++)
93240 7b 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 { mem0.aPageFree
93250 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d [i] = i; }. m
93260 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 em0.nPageFree =
93270 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
93280 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c fig.nPage;. }el
93290 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 se{. sqlite3G
932a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 lobalConfig.pPag
932b0 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 e = 0;. sqlit
932c0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
932d0 7a 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 zPage = 0;. }.
932e0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 return sqlite3G
932f0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 lobalConfig.m.xI
93300 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 nit(sqlite3Globa
93310 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 lConfig.m.pAppDa
93320 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 ta);.}../*.** De
93330 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d initialize the m
93340 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
93350 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 subsystem..*/.S
93360 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
93370 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 id sqlite3Malloc
93380 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 End(void){. if(
93390 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
933a0 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e nfig.m.xShutdown
933b0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ){. sqlite3G
933c0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 lobalConfig.m.xS
933d0 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 hutdown(sqlite3G
933e0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 lobalConfig.m.pA
933f0 70 70 44 61 74 61 29 3b 0a 20 20 7d 0a 20 20 6d ppData);. }. m
93400 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 emset(&mem0, 0,
93410 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d sizeof(mem0));.}
93420 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
93430 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d he amount of mem
93440 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68 ory currently ch
93450 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 ecked out..*/.SQ
93460 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
93470 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d _int64 sqlite3_m
93480 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 emory_used(void)
93490 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 {. int n, mx;.
934a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 sqlite3_int64 r
934b0 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 es;. sqlite3_st
934c0 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54 atus(SQLITE_STAT
934d0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 US_MEMORY_USED,
934e0 26 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 &n, &mx, 0);. r
934f0 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e es = (sqlite3_in
93500 74 36 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 t64)n; /* Work
93510 61 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f around bug in Bo
93520 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 rland C. Ticket
93530 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 #3216 */. retur
93540 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n res;.}../*.**
93550 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d Return the maxim
93560 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d um amount of mem
93570 6f 72 79 20 74 68 61 74 20 68 61 73 20 65 76 65 ory that has eve
93580 72 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 r been.** checke
93590 64 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 d out since eith
935a0 65 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 er the beginning
935b0 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 of this process
935c0 0a 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 .** or since the
935d0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 most recent res
935e0 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 et..*/.SQLITE_AP
935f0 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 I sqlite3_int64
93600 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 sqlite3_memory_h
93610 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 ighwater(int res
93620 65 74 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e etFlag){. int n
93630 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f , mx;. sqlite3_
93640 69 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c int64 res;. sql
93650 69 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 ite3_status(SQLI
93660 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 TE_STATUS_MEMORY
93670 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 _USED, &n, &mx,
93680 72 65 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 resetFlag);. re
93690 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 s = (sqlite3_int
936a0 36 34 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 64)mx; /* Work
936b0 61 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f around bug in Bo
936c0 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 rland C. Ticket
936d0 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 #3216 */. retur
936e0 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n res;.}../*.**
936f0 43 68 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d Change the alarm
93700 20 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c callback.*/.SQL
93710 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
93720 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 sqlite3MemoryAla
93730 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c rm(. void(*xCal
93740 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 lback)(void *pAr
93750 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 g, sqlite3_int64
93760 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 used,int N),.
93770 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 void *pArg,. sq
93780 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 lite3_int64 iThr
93790 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 eshold.){. sqli
937a0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
937b0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d mem0.mutex);. m
937c0 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
937d0 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 k = xCallback;.
937e0 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d mem0.alarmArg =
937f0 20 70 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c pArg;. mem0.al
93800 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 armThreshold = i
93810 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c Threshold;. sql
93820 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
93830 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
93840 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
93850 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
93860 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 ITE_OMIT_DEPRECA
93870 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 TED./*.** Deprec
93880 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e ated external in
93890 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e terface. Intern
938a0 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 al/core SQLite c
938b0 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 ode.** should ca
938c0 6c 6c 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 ll sqlite3Memory
938d0 41 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Alarm..*/.SQLITE
938e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
938f0 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 _memory_alarm(.
93900 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b void(*xCallback
93910 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 )(void *pArg, sq
93920 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 lite3_int64 used
93930 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 ,int N),. void
93940 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 *pArg,. sqlite3
93950 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c _int64 iThreshol
93960 64 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 d.){. return sq
93970 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d lite3MemoryAlarm
93980 28 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 (xCallback, pArg
93990 2c 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d , iThreshold);.}
939a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
939b0 72 69 67 67 65 72 20 74 68 65 20 61 6c 61 72 6d rigger the alarm
939c0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 .*/.static void
939d0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c sqlite3MallocAl
939e0 61 72 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a arm(int nByte){.
939f0 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 void (*xCallba
93a00 63 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 ck)(void*,sqlite
93a10 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 3_int64,int);.
93a20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f sqlite3_int64 no
93a30 77 55 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 wUsed;. void *p
93a40 41 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e Arg;. if( mem0.
93a50 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 alarmCallback==0
93a60 20 7c 7c 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 || mem0.alarmBu
93a70 73 79 20 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 sy ) return;.
93a80 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d mem0.alarmBusy =
93a90 20 31 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 1;. xCallback
93aa0 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c = mem0.alarmCall
93ab0 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 back;. nowUsed
93ac0 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 = sqlite3StatusV
93ad0 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 alue(SQLITE_STAT
93ae0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b US_MEMORY_USED);
93af0 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 . pArg = mem0.a
93b00 6c 61 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 larmArg;. sqlit
93b10 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
93b20 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 em0.mutex);. xC
93b30 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f allback(pArg, no
93b40 77 55 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 wUsed, nByte);.
93b50 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
93b60 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
93b70 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 ;. mem0.alarmBu
93b80 73 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a sy = 0;.}../*.**
93b90 20 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c Do a memory all
93ba0 6f 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61 ocation with sta
93bb0 74 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72 tistics and alar
93bc0 6d 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a ms. Assume the.
93bd0 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 ** lock is alrea
93be0 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 dy held..*/.stat
93bf0 69 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 ic int mallocWit
93c00 68 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f hAlarm(int n, vo
93c10 69 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 id **pp){. int
93c20 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 nFull;. void *p
93c30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
93c40 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
93c50 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em0.mutex) );.
93c60 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 nFull = sqlite3G
93c70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
93c80 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c oundup(n);. sql
93c90 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 ite3StatusSet(SQ
93ca0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c LITE_STATUS_MALL
93cb0 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 OC_SIZE, n);. i
93cc0 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c f( mem0.alarmCal
93cd0 6c 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 lback!=0 ){.
93ce0 69 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 int nUsed = sqli
93cf0 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 te3StatusValue(S
93d00 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
93d10 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 ORY_USED);. i
93d20 66 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e f( nUsed+nFull >
93d30 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 = mem0.alarmThre
93d40 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 shold ){. s
93d50 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 qlite3MallocAlar
93d60 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a m(nFull);. }.
93d70 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 }. p = sqlite
93d80 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
93d90 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a xMalloc(nFull);.
93da0 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 if( p==0 && me
93db0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b m0.alarmCallback
93dc0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d ){. sqlite3M
93dd0 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c allocAlarm(nFull
93de0 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 );. p = sqlit
93df0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
93e00 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b .xMalloc(nFull);
93e10 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a . }. if( p ){.
93e20 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 nFull = sqli
93e30 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
93e40 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
93e50 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
93e60 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
93e70 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 , nFull);. }.
93e80 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 *pp = p;. retur
93e90 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a n nFull;.}../*.*
93ea0 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
93eb0 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 y. This routine
93ec0 20 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 is like sqlite3
93ed0 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 _malloc() except
93ee0 20 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 that it.** assu
93ef0 6d 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 mes the memory s
93f00 75 62 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72 ubsystem has alr
93f10 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 eady been initia
93f20 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 lized..*/.SQLITE
93f30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
93f40 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 qlite3Malloc(int
93f50 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a n){. void *p;.
93f60 20 20 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 4e 45 if( n<=0 || NE
93f70 56 45 52 28 6e 3e 3d 30 78 37 66 66 66 66 66 30 VER(n>=0x7fffff0
93f80 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 0) ){. /* The
93f90 20 4e 45 56 45 52 28 6e 3e 3d 30 78 37 66 66 66 NEVER(n>=0x7fff
93fa0 66 66 30 30 29 20 74 65 72 6d 20 69 73 20 61 64 ff00) term is ad
93fb0 64 65 64 20 6f 75 74 20 6f 66 20 70 61 72 61 6e ded out of paran
93fc0 6f 69 61 2e 20 20 57 65 20 77 61 6e 74 20 74 6f oia. We want to
93fd0 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 61 62 73 make. ** abs
93fe0 6f 6c 75 74 65 6c 79 20 73 75 72 65 20 74 68 61 olutely sure tha
93ff0 74 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 t there is nothi
94000 6e 67 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 ng within SQLite
94010 20 74 68 61 74 20 63 61 6e 20 63 61 75 73 65 20 that can cause
94020 61 0a 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 a. ** memory
94030 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 allocation of a
94040 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
94050 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74 68 which is near th
94060 65 20 6d 61 78 69 6d 75 6d 20 73 69 67 6e 65 64 e maximum signed
94070 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 . ** integer
94080 76 61 6c 75 65 20 61 6e 64 20 74 68 75 73 20 63 value and thus c
94090 61 75 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 ause an integer
940a0 6f 76 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 overflow inside
940b0 6f 66 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 of the xMalloc()
940c0 0a 20 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e . ** implemen
940d0 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 6e 3e 3d tation. The n>=
940e0 30 78 37 66 66 66 66 66 30 30 20 67 69 76 65 73 0x7fffff00 gives
940f0 20 75 73 20 32 35 35 20 62 79 74 65 73 20 6f 66 us 255 bytes of
94100 20 68 65 61 64 72 6f 6f 6d 2e 20 20 54 68 65 0a headroom. The.
94110 20 20 20 20 2a 2a 20 74 65 73 74 20 73 68 6f 75 ** test shou
94120 6c 64 20 6e 65 76 65 72 20 62 65 20 74 72 75 65 ld never be true
94130 20 62 65 63 61 75 73 65 20 53 51 4c 49 54 45 5f because SQLITE_
94140 4d 41 58 5f 4c 45 4e 47 54 48 20 73 68 6f 75 6c MAX_LENGTH shoul
94150 64 20 62 65 20 6d 75 63 68 0a 20 20 20 20 2a 2a d be much. **
94160 20 6c 65 73 73 20 74 68 61 6e 20 30 78 37 66 66 less than 0x7ff
94170 66 66 66 30 30 20 61 6e 64 20 69 74 20 73 68 6f fff00 and it sho
94180 75 6c 64 20 63 61 74 63 68 20 6c 61 72 67 65 20 uld catch large
94190 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
941a0 6e 73 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 ns. ** before
941b0 20 74 68 65 79 20 72 65 61 63 68 20 74 68 69 73 they reach this
941c0 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 70 point. */. p
941d0 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
941e0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
941f0 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
94200 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
94210 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
94220 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f utex);. mallo
94230 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 cWithAlarm(n, &p
94240 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
94250 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
94260 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b mutex);. }else{
94270 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
94280 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
94290 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 Malloc(n);. }.
942a0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
942b0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
942c0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
942d0 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 llocation is for
942e0 20 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c use by the appl
942f0 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 ication..** Firs
94300 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 t make sure the
94310 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d memory subsystem
94320 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c is initialized,
94330 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 then do the.**
94340 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 allocation..*/.S
94350 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
94360 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 sqlite3_malloc(i
94370 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 nt n){.#ifndef S
94380 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
94390 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 NIT. if( sqlite
943a0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 3_initialize() )
943b0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
943c0 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 f. return sqlit
943d0 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a e3Malloc(n);.}..
943e0 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61 /*.** Each threa
943f0 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 d may only have
94400 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e a single outstan
94410 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 ding allocation
94420 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 from.** xScratch
94430 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 Malloc(). We ve
94440 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 rify this constr
94450 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67 aint in the sing
94460 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 le-threaded.** c
94470 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 20 73 ase by setting s
94480 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 cratchAllocOut t
94490 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f o 1 when an allo
944a0 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 cation.** is out
944b0 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e standing clearin
944c0 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c g it when the al
944d0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
944e0 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 d..*/.#if SQLITE
944f0 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 _THREADSAFE==0 &
94500 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 & !defined(NDEBU
94510 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 G).static int sc
94520 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 ratchAllocOut =
94530 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 0;.#endif.../*.*
94540 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
94550 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 y that is to be
94560 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 used and release
94570 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a d right away..**
94580 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
94590 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f similar to allo
945a0 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 74 20 ca() in that it
945b0 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a is not intended.
945c0 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e ** for situation
945d0 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f s where the memo
945e0 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 ry might be held
945f0 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 long-term. Thi
94600 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 s.** routine is
94610 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20 intended to get
94620 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 memory to old la
94630 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61 rge transient da
94640 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 ta.** structures
94650 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 that would not
94660 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 normally fit on
94670 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a the stack of an.
94680 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63 ** embedded proc
94690 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 essor..*/.SQLITE
946a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
946b0 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c qlite3ScratchMal
946c0 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f loc(int n){. vo
946d0 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 id *p;. assert(
946e0 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c n>0 );..#if SQL
946f0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
94700 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 0 && !defined(ND
94710 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 EBUG). /* Verif
94720 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 y that no more t
94730 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 han one scratch
94740 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 allocation per t
94750 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 hread. ** is ou
94760 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 tstanding at one
94770 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 time. (This is
94780 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e only checked in
94790 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 the. ** single
947a0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73 -threaded case s
947b0 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e ince checking in
947c0 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 the multi-threa
947d0 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f ded case. ** wo
947e0 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 uld be much more
947f0 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a complicated.) *
94800 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 72 61 /. assert( scra
94810 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 tchAllocOut==0 )
94820 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 ;.#endif.. if(
94830 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
94840 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 fig.szScratch<n
94850 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61 ){. goto scra
94860 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 tch_overflow;.
94870 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c }else{ . sql
94880 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
94890 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
948a0 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 if( mem0.nScra
948b0 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 tchFree==0 ){.
948c0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
948d0 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
948e0 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ex);. goto
948f0 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 scratch_overflow
94900 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
94910 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
94920 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 i = mem0.aScratc
94930 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 hFree[--mem0.nSc
94940 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 ratchFree];.
94950 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c i *= sqlite3Gl
94960 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
94970 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 atch;. sqli
94980 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
94990 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
949a0 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 CH_USED, 1);.
949b0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
949c0 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Set(SQLITE_STATU
949d0 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 S_SCRATCH_SIZE,
949e0 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 n);. sqlite
949f0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
94a00 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
94a10 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 p = (void*)&((c
94a20 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 har*)sqlite3Glob
94a30 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
94a40 68 29 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 h)[i];. ass
94a50 65 72 74 28 20 20 28 28 28 75 38 2a 29 70 20 2d ert( (((u8*)p -
94a60 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 (u8*)0) & 7)==0
94a70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 );. }. }.#i
94a80 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
94a90 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e AFE==0 && !defin
94aa0 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 ed(NDEBUG). scr
94ab0 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 atchAllocOut = p
94ac0 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 !=0;.#endif.. r
94ad0 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63 eturn p;..scratc
94ae0 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66 h_overflow:. if
94af0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
94b00 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
94b10 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
94b20 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
94b30 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 utex);. sqlit
94b40 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 e3StatusSet(SQLI
94b50 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
94b60 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 H_SIZE, n);.
94b70 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c n = mallocWithAl
94b80 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 arm(n, &p);.
94b90 69 66 28 20 70 20 29 20 73 71 6c 69 74 65 33 53 if( p ) sqlite3S
94ba0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
94bb0 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f STATUS_SCRATCH_O
94bc0 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 VERFLOW, n);.
94bd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
94be0 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 eave(mem0.mutex)
94bf0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
94c00 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
94c10 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 Config.m.xMalloc
94c20 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c (n);. }.#if SQL
94c30 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
94c40 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 0 && !defined(ND
94c50 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 68 41 EBUG). scratchA
94c60 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a llocOut = p!=0;.
94c70 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
94c80 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f p; .}.SQLITE_
94c90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
94ca0 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 ite3ScratchFree(
94cb0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 void *p){. if(
94cc0 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 p ){..#if SQLITE
94cd0 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 _THREADSAFE==0 &
94ce0 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 & !defined(NDEBU
94cf0 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 G). /* Verify
94d00 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 that no more th
94d10 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 an one scratch a
94d20 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 llocation per th
94d30 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f read. ** is o
94d40 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e utstanding at on
94d50 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 e time. (This i
94d60 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 s only checked i
94d70 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e n the. ** sin
94d80 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 gle-threaded cas
94d90 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 e since checking
94da0 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 in the multi-th
94db0 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 20 20 readed case.
94dc0 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 ** would be much
94dd0 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 more complicate
94de0 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 d.) */. asser
94df0 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f t( scratchAllocO
94e00 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 73 63 72 ut==1 );. scr
94e10 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 atchAllocOut = 0
94e20 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 ;.#endif.. if
94e30 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
94e40 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 3d 3d onfig.pScratch==
94e50 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 0. ||
94e60 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 p<sqlite3GlobalC
94e70 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 0a 20 onfig.pScratch.
94e80 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d || p>=
94e90 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72 (void*)mem0.aScr
94ea0 61 74 63 68 46 72 65 65 20 29 7b 0a 20 20 20 20 atchFree ){.
94eb0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
94ec0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
94ed0 61 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e at ){. in
94ee0 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 t iSize = sqlite
94ef0 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 3MallocSize(p);.
94f00 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
94f10 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
94f20 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 .mutex);.
94f30 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
94f40 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
94f50 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 SCRATCH_OVERFLOW
94f60 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 , -iSize);.
94f70 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
94f80 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
94f90 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d S_MEMORY_USED, -
94fa0 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 iSize);.
94fb0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
94fc0 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
94fd0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
94fe0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
94ff0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d .mutex);. }
95000 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
95010 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
95020 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 g.m.xFree(p);.
95030 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
95040 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
95050 20 20 20 20 69 20 3d 20 28 69 6e 74 29 28 28 75 i = (int)((u
95060 38 2a 29 70 20 2d 20 28 75 38 2a 29 73 71 6c 69 8*)p - (u8*)sqli
95070 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
95080 70 53 63 72 61 74 63 68 29 3b 0a 20 20 20 20 20 pScratch);.
95090 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f i /= sqlite3Glo
950a0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 balConfig.szScra
950b0 74 63 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 tch;. asser
950c0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c t( i>=0 && i<sql
950d0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
950e0 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 .nScratch );.
950f0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
95100 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
95110 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 x);. assert
95120 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 ( mem0.nScratchF
95130 72 65 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33 ree<(u32)sqlite3
95140 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 GlobalConfig.nSc
95150 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d ratch );. m
95160 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 em0.aScratchFree
95170 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 [mem0.nScratchFr
95180 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 ee++] = i;.
95190 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
951a0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
951b0 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 SCRATCH_USED, -1
951c0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
951d0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
951e0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 0.mutex);. }.
951f0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 }.}../*.** TRU
95200 45 20 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b E if p is a look
95210 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c aside memory all
95220 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a ocation from db.
95230 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
95240 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 E_OMIT_LOOKASIDE
95250 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f .static int isLo
95260 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 okaside(sqlite3
95270 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 *db, void *p){.
95280 20 72 65 74 75 72 6e 20 64 62 20 26 26 20 70 20 return db && p
95290 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 && p>=db->lookas
952a0 69 64 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c ide.pStart && p<
952b0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 db->lookaside.pE
952c0 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 nd;.}.#else.#def
952d0 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 ine isLookaside(
952e0 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f A,B) 0.#endif../
952f0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
95300 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 size of a memory
95310 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 allocation prev
95320 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 iously obtained
95330 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d from.** sqlite3M
95340 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 alloc() or sqlit
95350 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a e3_malloc()..*/.
95360 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
95370 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 nt sqlite3Malloc
95380 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 Size(void *p){.
95390 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 return sqlite3G
953a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 lobalConfig.m.xS
953b0 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 ize(p);.}.SQLITE
953c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
953d0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 ite3DbMallocSize
953e0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f (sqlite3 *db, vo
953f0 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 id *p){. assert
95400 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ( db==0 || sqlit
95410 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
95420 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
95430 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ( p==0 ){. re
95440 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 turn 0;. }else
95450 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 if( isLookaside(
95460 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 db, p) ){. re
95470 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 turn db->lookasi
95480 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a de.sz;. }else{.
95490 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
954a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
954b0 2e 78 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d .xSize(p);. }.}
954c0 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d ../*.** Free mem
954d0 6f 72 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f ory previously o
954e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
954f0 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f ite3Malloc()..*/
95500 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
95510 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f sqlite3_free(vo
95520 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d id *p){. if( p=
95530 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 =0 ) return;. i
95540 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
95550 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 Config.bMemstat
95560 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
95570 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e utex_enter(mem0.
95580 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 mutex);. sqli
95590 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
955a0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
955b0 59 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33 Y_USED, -sqlite3
955c0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a MallocSize(p));.
955d0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
955e0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 lConfig.m.xFree(
955f0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f p);. sqlite3_
95600 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
95610 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 .mutex);. }else
95620 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f {. sqlite3Glo
95630 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 balConfig.m.xFre
95640 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a e(p);. }.}../*.
95650 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74 ** Free memory t
95660 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 73 73 hat might be ass
95670 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70 ociated with a p
95680 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 articular databa
95690 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
956a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
956b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
956c0 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a DbFree(sqlite3 *
956d0 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 db, void *p){.
956e0 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c assert( db==0 ||
956f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
95700 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
95710 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 ;. if( isLookas
95720 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 ide(db, p) ){.
95730 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 LookasideSlot
95740 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 *pBuf = (Lookasi
95750 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 deSlot*)p;. p
95760 42 75 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d Buf->pNext = db-
95770 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 >lookaside.pFree
95780 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 ;. db->lookas
95790 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 ide.pFree = pBuf
957a0 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 ;. db->lookas
957b0 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 ide.nOut--;. }e
957c0 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
957d0 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a _free(p);. }.}.
957e0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
957f0 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 e size of an exi
95800 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c sting memory all
95810 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 ocation.*/.SQLIT
95820 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
95830 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 sqlite3Realloc(v
95840 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e oid *pOld, int n
95850 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f Bytes){. int nO
95860 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 ld, nNew;. void
95870 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f *pNew;. if( pO
95880 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ld==0 ){. ret
95890 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f urn sqlite3Mallo
958a0 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 c(nBytes);. }.
958b0 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 7c if( nBytes<=0 |
958c0 7c 20 4e 45 56 45 52 28 6e 42 79 74 65 73 3e 3d | NEVER(nBytes>=
958d0 30 78 37 66 66 66 66 66 30 30 29 20 29 7b 0a 20 0x7fffff00) ){.
958e0 20 20 20 2f 2a 20 54 68 65 20 4e 45 56 45 52 28 /* The NEVER(
958f0 2e 2e 2e 29 20 74 65 72 6d 20 69 73 20 65 78 70 ...) term is exp
95900 6c 61 69 6e 65 64 20 69 6e 20 63 6f 6d 6d 65 6e lained in commen
95910 74 73 20 6f 6e 20 73 71 6c 69 74 65 33 4d 61 6c ts on sqlite3Mal
95920 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 73 71 6c loc() */. sql
95930 69 74 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b ite3_free(pOld);
95940 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
95950 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c 69 }. nOld = sqli
95960 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4f te3MallocSize(pO
95970 6c 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ld);. if( sqlit
95980 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 e3GlobalConfig.b
95990 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 Memstat ){. s
959a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
959b0 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
959c0 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
959d0 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 sSet(SQLITE_STAT
959e0 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 US_MALLOC_SIZE,
959f0 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 6e 4e 65 nBytes);. nNe
95a00 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 w = sqlite3Globa
95a10 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 lConfig.m.xRound
95a20 75 70 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 up(nBytes);.
95a30 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 if( nOld==nNew )
95a40 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 {. pNew = p
95a50 4f 6c 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a Old;. }else{.
95a60 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
95a70 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 3StatusValue(SQL
95a80 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
95a90 59 5f 55 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c Y_USED)+nNew-nOl
95aa0 64 20 3e 3d 20 0a 20 20 20 20 20 20 20 20 20 20 d >= .
95ab0 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 mem0.alarmThre
95ac0 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 shold ){.
95ad0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c sqlite3MallocAl
95ae0 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a arm(nNew-nOld);.
95af0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e }. pN
95b00 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ew = sqlite3Glob
95b10 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c alConfig.m.xReal
95b20 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b loc(pOld, nNew);
95b30 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d . if( pNew=
95b40 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d =0 && mem0.alarm
95b50 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 Callback ){.
95b60 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f sqlite3Mallo
95b70 63 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a cAlarm(nBytes);.
95b80 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 pNew = s
95b90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
95ba0 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f ig.m.xRealloc(pO
95bb0 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 ld, nNew);.
95bc0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 }. if( pNe
95bd0 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 4e 65 w ){. nNe
95be0 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f w = sqlite3Mallo
95bf0 63 53 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20 cSize(pNew);.
95c00 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
95c10 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
95c20 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c TUS_MEMORY_USED,
95c30 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 nNew-nOld);.
95c40 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
95c50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
95c60 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a ve(mem0.mutex);.
95c70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 }else{. pNe
95c80 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 w = sqlite3Globa
95c90 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c lConfig.m.xReall
95ca0 6f 63 28 70 4f 6c 64 2c 20 6e 42 79 74 65 73 29 oc(pOld, nBytes)
95cb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
95cc0 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 New;.}../*.** Th
95cd0 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 e public interfa
95ce0 63 65 20 74 6f 20 73 71 6c 69 74 65 33 52 65 61 ce to sqlite3Rea
95cf0 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 lloc. Make sure
95d00 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 that the memory
95d10 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 20 69 73 .** subsystem is
95d20 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69 initialized pri
95d30 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 or to invoking s
95d40 71 6c 69 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f qliteRealloc..*/
95d50 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
95d60 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f *sqlite3_reallo
95d70 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e c(void *pOld, in
95d80 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 t n){.#ifndef SQ
95d90 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
95da0 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 IT. if( sqlite3
95db0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 _initialize() )
95dc0 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 return 0;.#endif
95dd0 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
95de0 33 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 3Realloc(pOld, n
95df0 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c );.}.../*.** All
95e00 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d ocate and zero m
95e10 65 6d 6f 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 emory..*/ .SQLIT
95e20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
95e30 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
95e40 6f 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 o(int n){. void
95e50 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c *p = sqlite3Mal
95e60 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 loc(n);. if( p
95e70 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c ){. memset(p,
95e80 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 0, n);. }. re
95e90 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
95ea0 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 Allocate and ze
95eb0 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 ro memory. If t
95ec0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 he allocation fa
95ed0 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 ils, make.** the
95ee0 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c mallocFailed fl
95ef0 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 ag in the connec
95f00 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f tion pointer..*/
95f10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
95f20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d void *sqlite3DbM
95f30 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 allocZero(sqlite
95f40 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 3 *db, int n){.
95f50 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 void *p = sqlit
95f60 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
95f70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b , n);. if( p ){
95f80 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 . memset(p, 0
95f90 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , n);. }. retu
95fa0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 rn p;.}../*.** A
95fb0 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f llocate and zero
95fc0 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 memory. If the
95fd0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
95fe0 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d s, make.** the m
95ff0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 allocFailed flag
96000 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 in the connecti
96010 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a on pointer..**.*
96020 2a 20 49 66 20 64 62 21 3d 30 20 61 6e 64 20 64 * If db!=0 and d
96030 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
96040 69 73 20 74 72 75 65 20 28 69 6e 64 69 63 61 74 is true (indicat
96050 69 6e 67 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c ing a prior mall
96060 6f 63 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e oc.** failure on
96070 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
96080 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 se connection) t
96090 68 65 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 hen always retur
960a0 6e 20 30 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f n 0..** Hence fo
960b0 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 r a particular d
960c0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
960d0 6f 6e 2c 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 on, once malloc
960e0 73 74 61 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e starts.** failin
960f0 67 2c 20 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 g, it fails cons
96100 69 73 74 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d istently until m
96110 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 72 allocFailed is r
96120 65 73 65 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 eset..** This is
96130 20 61 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 an important as
96140 73 75 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 sumption. There
96150 20 61 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 are many places
96160 20 69 6e 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 in the.** code
96170 74 68 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c that do things l
96180 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
96190 20 20 20 20 20 20 20 20 69 6e 74 20 2a 61 20 3d int *a =
961a0 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 (int*)sqlite3Db
961b0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 MallocRaw(db, 10
961c0 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 0);.** i
961d0 6e 74 20 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 nt *b = (int*)sq
961e0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
961f0 28 64 62 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 (db, 200);.**
96200 20 20 20 20 20 20 69 66 28 20 62 20 29 20 61 5b if( b ) a[
96210 31 30 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 10] = 9;.**.** I
96220 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 n other words, i
96230 66 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 6d f a subsequent m
96240 61 6c 6c 6f 63 20 28 65 78 3a 20 22 62 22 29 20 alloc (ex: "b")
96250 77 6f 72 6b 65 64 2c 20 69 74 20 69 73 20 61 73 worked, it is as
96260 73 75 6d 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c sumed.** that al
96270 6c 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 l prior mallocs
96280 28 65 78 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 (ex: "a") worked
96290 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f too..*/.SQLITE_
962a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
962b0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
962c0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e (sqlite3 *db, in
962d0 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b t n){. void *p;
962e0 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 . assert( db==0
962f0 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
96300 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 x_held(db->mutex
96310 29 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c ) );.#ifndef SQL
96320 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 ITE_OMIT_LOOKASI
96330 44 45 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20 DE. if( db ){.
96340 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 LookasideSlot
96350 20 2a 70 42 75 66 3b 0a 20 20 20 20 69 66 28 20 *pBuf;. if(
96360 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
96370 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
96380 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0;. }. if
96390 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e ( db->lookaside.
963a0 62 45 6e 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64 bEnabled && n<=d
963b0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a b->lookaside.sz.
963c0 20 20 20 20 20 20 20 20 20 26 26 20 28 70 42 75 && (pBu
963d0 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 f = db->lookasid
963e0 65 2e 70 46 72 65 65 29 21 3d 30 20 29 7b 0a 20 e.pFree)!=0 ){.
963f0 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 db->lookasi
96400 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 2d de.pFree = pBuf-
96410 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 64 62 >pNext;. db
96420 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 ->lookaside.nOut
96430 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 ++;. if( db
96440 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 ->lookaside.nOut
96450 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d >db->lookaside.m
96460 78 4f 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 xOut ){.
96470 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 db->lookaside.mx
96480 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 Out = db->lookas
96490 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 ide.nOut;.
964a0 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 }. return (
964b0 76 6f 69 64 2a 29 70 42 75 66 3b 0a 20 20 20 20 void*)pBuf;.
964c0 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 }. }.#else. if
964d0 28 20 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c ( db && db->mall
964e0 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
964f0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 return 0;. }.#e
96500 6e 64 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 ndif. p = sqlit
96510 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 e3Malloc(n);. i
96520 66 28 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20 f( !p && db ){.
96530 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
96540 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 led = 1;. }. r
96550 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
96560 2a 20 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f * Resize the blo
96570 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 ck of memory poi
96580 6e 74 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20 nted to by p to
96590 6e 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a n bytes. If the.
965a0 2a 2a 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c ** resize fails,
965b0 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 set the mallocF
965c0 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 ailed flag in th
965d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a e connection obj
965e0 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ect..*/.SQLITE_P
965f0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
96600 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 ite3DbRealloc(sq
96610 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 lite3 *db, void
96620 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f *p, int n){. vo
96630 69 64 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 id *pNew = 0;.
96640 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b assert( db!=0 );
96650 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
96660 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
96670 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
96680 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
96690 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 ed==0 ){. if(
966a0 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 p==0 ){. r
966b0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 44 62 4d eturn sqlite3DbM
966c0 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b allocRaw(db, n);
966d0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 . }. if( i
966e0 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 sLookaside(db, p
966f0 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e ) ){. if( n
96700 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e <=db->lookaside.
96710 73 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 sz ){. re
96720 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a turn p;. }.
96730 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c pNew = sql
96740 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
96750 64 62 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 db, n);. if
96760 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 ( pNew ){.
96770 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 memcpy(pNew, p
96780 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e , db->lookaside.
96790 73 7a 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c sz);. sql
967a0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
967b0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
967c0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 else{. pNew
967d0 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c = sqlite3_reall
967e0 6f 63 28 70 2c 20 6e 29 3b 0a 20 20 20 20 20 20 oc(p, n);.
967f0 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 if( !pNew ){.
96800 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 db->mallocF
96810 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 ailed = 1;.
96820 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
96830 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
96840 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
96850 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49 reallocate p. I
96860 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 61 74 69 f the reallocati
96870 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 66 on fails, then f
96880 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74 ree p.** and set
96890 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 the mallocFaile
968a0 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 64 61 d flag in the da
968b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
968c0 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
968d0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
968e0 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 e3DbReallocOrFre
968f0 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 e(sqlite3 *db, v
96900 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a oid *p, int n){.
96910 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 void *pNew;.
96920 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
96930 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e Realloc(db, p, n
96940 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 );. if( !pNew )
96950 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
96960 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a ree(db, p);. }.
96970 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
96980 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 ../*.** Make a c
96990 6f 70 79 20 6f 66 20 61 20 73 74 72 69 6e 67 20 opy of a string
969a0 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e in memory obtain
969b0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 ed from sqliteMa
969c0 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65 20 0a 2a lloc(). These .*
969d0 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c * functions call
969e0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 sqlite3MallocRa
969f0 77 28 29 20 64 69 72 65 63 74 6c 79 20 69 6e 73 w() directly ins
96a00 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 tead of sqliteMa
96a10 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 lloc(). This.**
96a20 69 73 20 62 65 63 61 75 73 65 20 77 68 65 6e 20 is because when
96a30 6d 65 6d 6f 72 79 20 64 65 62 75 67 67 69 6e 67 memory debugging
96a40 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 is turned on, t
96a50 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f hese two functio
96a60 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 ns are .** calle
96a70 64 20 76 69 61 20 6d 61 63 72 6f 73 20 74 68 61 d via macros tha
96a80 74 20 72 65 63 6f 72 64 20 74 68 65 20 63 75 72 t record the cur
96a90 72 65 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 rent file and li
96aa0 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 ne number in the
96ab0 0a 2a 2a 20 54 68 72 65 61 64 44 61 74 61 20 73 .** ThreadData s
96ac0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c tructure..*/.SQL
96ad0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
96ae0 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 *sqlite3DbStrDu
96af0 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 p(sqlite3 *db, c
96b00 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 onst char *z){.
96b10 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 char *zNew;. s
96b20 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a ize_t n;. if( z
96b30 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
96b40 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 28 n 0;. }. n = (
96b50 64 62 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c db ? sqlite3Strl
96b60 65 6e 28 64 62 2c 20 7a 29 20 3a 20 73 71 6c 69 en(db, z) : sqli
96b70 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 29 2b te3Strlen30(z))+
96b80 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 1;. assert( (n&
96b90 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 0x7fffffff)==n )
96ba0 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 ;. zNew = sqlit
96bb0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
96bc0 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28 , (int)n);. if(
96bd0 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d zNew ){. mem
96be0 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b cpy(zNew, z, n);
96bf0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e . }. return zN
96c00 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 ew;.}.SQLITE_PRI
96c10 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
96c20 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 e3DbStrNDup(sqli
96c30 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 te3 *db, const c
96c40 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a har *z, int n){.
96c50 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 char *zNew;.
96c60 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 if( z==0 ){.
96c70 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
96c80 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 assert( (n&0x7ff
96c90 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a fffff)==n );. z
96ca0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
96cb0 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31 allocRaw(db, n+1
96cc0 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b );. if( zNew ){
96cd0 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 . memcpy(zNew
96ce0 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 , z, n);. zNe
96cf0 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 w[n] = 0;. }.
96d00 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a return zNew;.}..
96d10 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73 /*.** Create a s
96d20 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a tring from the z
96d30 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 Fromat argument
96d40 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20 and the va_list
96d50 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a that follows..**
96d60 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e Store the strin
96d70 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 g in memory obta
96d80 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
96d90 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b Malloc() and mak
96da0 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74 e *pz.** point t
96db0 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a o that string..*
96dc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
96dd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 void sqlite3Set
96de0 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a String(char **pz
96df0 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 , sqlite3 *db, c
96e00 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
96e10 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
96e20 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a ist ap;. char *
96e30 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61 z;.. va_start(a
96e40 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a p, zFormat);. z
96e50 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
96e60 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 tf(db, zFormat,
96e70 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
96e80 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
96e90 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a ee(db, *pz);. *
96ea0 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a pz = z;.}.../*.*
96eb0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
96ec0 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 must be called b
96ed0 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e efore exiting an
96ee0 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 y API function (
96ef0 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69 i.e. .** returni
96f00 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 ng control to th
96f10 65 20 75 73 65 72 29 20 74 68 61 74 20 68 61 73 e user) that has
96f20 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f called sqlite3_
96f30 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c malloc or.** sql
96f40 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a ite3_realloc..**
96f50 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 .** The returned
96f60 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c value is normal
96f70 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ly a copy of the
96f80 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
96f90 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 to this.** func
96fa0 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69 tion. However, i
96fb0 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
96fc0 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 72 65 lure has occurre
96fd0 64 20 73 69 6e 63 65 20 74 68 65 20 70 72 65 76 d since the prev
96fe0 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 ious.** invocati
96ff0 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 on SQLITE_NOMEM
97000 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 is returned inst
97010 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ead. .**.** If t
97020 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
97030 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 t, db, is not NU
97040 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 LL and a malloc(
97050 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 ) error has occu
97060 72 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 rred,.** then th
97070 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 e connection err
97080 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c or-code (the val
97090 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
970a0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
970b0 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 ).** is set to S
970c0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a QLITE_NOMEM..*/.
970d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
970e0 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 nt sqlite3ApiExi
970f0 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 t(sqlite3* db, i
97100 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 nt rc){. /* If
97110 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73 the db handle is
97120 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
97130 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 we must hold the
97140 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 connection hand
97150 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 le. ** mutex he
97160 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 re. Otherwise th
97170 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73 e read (and poss
97180 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64 ible write) of d
97190 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
971a0 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c . ** is unsafe,
971b0 20 61 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 as is the call
971c0 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 to sqlite3Error(
971d0 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ).. */. assert
971e0 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 ( !db || sqlite3
971f0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
97200 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
97210 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f db && (db->mallo
97220 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 cFailed || rc==S
97230 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 QLITE_IOERR_NOME
97240 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 M) ){. sqlite
97250 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 3Error(db, SQLIT
97260 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 E_NOMEM, 0);.
97270 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
97280 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 d = 0;. rc =
97290 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
972a0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 }. return rc &
972b0 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 (db ? db->errMas
972c0 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a k : 0xff);.}../*
972d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
972e0 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a d of malloc.c **
972f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
97320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
97330 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e gin file printf.
97340 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
97350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
97370 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22 .** The "printf"
97380 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f code that follo
97390 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68 ws dates from th
973a0 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 69 73 e 1980's. It is
973b0 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 in.** the publi
973c0 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f c domain. The o
973d0 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 riginal comments
973e0 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65 are included he
973f0 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 re for.** comple
97400 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 teness. They ar
97410 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 e very out-of-da
97420 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 te but might be
97430 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 useful as.** an
97440 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 historical refer
97450 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 ence. Most of t
97460 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 he "enhancements
97470 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b " have been back
97480 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 ed.** out so tha
97490 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c t the functional
974a0 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73 ity is now the s
974b0 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 20 ame as standard
974c0 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 printf()..**.**
974d0 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20 $Id: printf.c,v
974e0 31 2e 39 39 20 32 30 30 38 2f 31 32 2f 31 30 20 1.99 2008/12/10
974f0 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78 70 19:26:24 drh Exp
97500 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a $.**.**********
97510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97550 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
97560 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 wing modules is
97570 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c an enhanced repl
97580 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 acement for the
97590 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 "printf" subrout
975a0 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e ines.** found in
975b0 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 the standard C
975c0 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f library. The fo
975d0 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d llowing enhancem
975e0 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 ents are.** supp
975f0 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 orted:.**.**
97600 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 + Additional
97610 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 functions. The
97620 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 standard set of
97630 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f "printf" functio
97640 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e ns.** in
97650 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 cludes printf, f
97660 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c printf, sprintf,
97670 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e vprintf, vfprin
97680 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 tf, and.**
97690 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 vsprintf. Th
976a0 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 is module adds t
976b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a he following:.**
976c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 .** *
976d0 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 snprintf -- Wor
976e0 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c ks like sprintf,
976f0 20 62 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 but has an extr
97700 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 a argument.**
97710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
97720 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 which is
97730 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
97740 62 75 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 buffer written t
97750 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 o..**.**
97760 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d * mprintf --
97770 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 Similar to spr
97780 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 intf. Writes ou
97790 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a tput to memory.*
977a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
977b0 20 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 obtai
977c0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e ned from malloc.
977d0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
977e0 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 * xprintf --
977f0 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e Calls a function
97800 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f to dispose of o
97810 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 utput..**.**
97820 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 * nprint
97830 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c f -- No output,
97840 20 62 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 but returns the
97850 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 number of chara
97860 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 cters.**
97870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
97880 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 that would hav
97890 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 e been output by
978a0 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 printf..**.**
978b0 20 20 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d * A v-
978c0 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 version (ex: vs
978d0 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 nprintf) of ever
978e0 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c y function is al
978f0 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 so.**
97900 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a supplied..**.
97910 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 ** + A few
97920 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 extensions to t
97930 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f he formatting no
97940 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f tation are suppo
97950 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 rted:.**.**
97960 20 20 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 * The "="
97970 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 flag (similar t
97980 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 o "-") causes th
97990 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a e output to be.*
979a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 * b
979b0 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 e centered in th
979c0 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 e appropriately
979d0 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a sized field..**.
979e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 ** *
979f0 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 The %b field out
97a00 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 puts an integer
97a10 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 in binary notati
97a20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 on..**.**
97a30 20 20 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 * The %c fi
97a40 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 eld now accepts
97a50 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 a precision. Th
97a60 65 20 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 e character outp
97a70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ut.**
97a80 20 20 20 69 73 20 72 65 70 65 61 74 65 64 20 62 is repeated b
97a90 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 y the number of
97aa0 74 69 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 times the precis
97ab0 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a ion specifies..*
97ac0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
97ad0 20 20 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 The %' field w
97ae0 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 orks like %c, bu
97af0 74 20 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 t takes as its c
97b00 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 haracter the.**
97b10 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 nex
97b20 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 t character of t
97b30 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 he format string
97b40 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 , instead of the
97b50 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 next.**
97b60 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 argument.
97b70 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 For example, p
97b80 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 rintf("%.78'-")
97b90 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 prints 78 minus
97ba0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
97bb0 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 signs, the same
97bc0 20 61 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 as printf("%.7
97bd0 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 8c",'-')..**.**
97be0 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d + When com
97bf0 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 piled using GCC
97c00 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 on a SPARC, this
97c10 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e version of prin
97c20 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 tf is.**
97c30 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 faster than the
97c40 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 library printf
97c50 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a for SUN OS 4.1..
97c60 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c **.** + Al
97c70 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 l functions are
97c80 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e fully reentrant.
97c90 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f .**.*/../*.** Co
97ca0 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 nversion types f
97cb0 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 all into various
97cc0 20 63 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 categories as d
97cd0 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a efined by the.**
97ce0 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 following enume
97cf0 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 ration..*/.#defi
97d00 6e 65 20 65 74 52 41 44 49 58 20 20 20 20 20 20 ne etRADIX
97d10 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 1 /* Integer ty
97d20 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f pes. %d, %x, %o
97d30 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a , and so forth *
97d40 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 /.#define etFLOA
97d50 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f T 2 /* Flo
97d60 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 ating point. %f
97d70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 */.#define etEX
97d80 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 P 3 /* E
97d90 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 xponentional not
97da0 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 ation. %e and %E
97db0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 */.#define etGE
97dc0 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 NERIC 4 /* F
97dd0 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e loating or expon
97de0 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e ential, dependin
97df0 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 g on exponent. %
97e00 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 g */.#define etS
97e10 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 IZE 5 /*
97e20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 Return number of
97e30 20 63 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 characters proc
97e40 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e essed so far. %n
97e50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 */.#define etST
97e60 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 RING 6 /* S
97e70 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 trings. %s */.#d
97e80 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e efine etDYNSTRIN
97e90 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 G 7 /* Dynamic
97ea0 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 ally allocated s
97eb0 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 trings. %z */.#d
97ec0 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 efine etPERCENT
97ed0 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 8 /* Percent
97ee0 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 symbol. %% */.#
97ef0 64 65 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 define etCHARX
97f00 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 9 /* Charac
97f10 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 ters. %c */./* T
97f20 68 65 20 72 65 73 74 20 61 72 65 20 65 78 74 65 he rest are exte
97f30 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d nsions, not norm
97f40 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 ally found in pr
97f50 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e intf() */.#defin
97f60 65 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 e etSQLESCAPE 1
97f70 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 0 /* Strings wit
97f80 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 h '\'' doubled.
97f90 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %q */.#define e
97fa0 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f tSQLESCAPE2 11 /
97fb0 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 * Strings with '
97fc0 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 \'' doubled and
97fd0 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a enclosed in '',.
97fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
97ff0 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 NULL p
98000 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 ointers replaced
98010 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 by SQL NULL. %
98020 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 Q */.#define etT
98030 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 OKEN 12 /*
98040 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 a pointer to a T
98050 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a oken structure *
98060 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c /.#define etSRCL
98070 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 IST 13 /* a p
98080 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c ointer to a SrcL
98090 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 ist */.#define e
980a0 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f tPOINTER 14 /
980b0 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 * The %p convers
980c0 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 ion */.#define e
980d0 74 53 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f tSQLESCAPE3 15 /
980e0 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 * %w -> Strings
980f0 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 with '\"' double
98100 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f d */.#define etO
98110 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 RDINAL 16 /*
98120 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 %r -> 1st, 2nd,
98130 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 3rd, 4th, etc.
98140 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a English only */.
98150 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 ../*.** An "etBy
98160 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 te" is an 8-bit
98170 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a unsigned value..
98180 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 */.typedef unsig
98190 6e 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b ned char etByte;
981a0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 ../*.** Each bui
981b0 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ltin conversion
981c0 63 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 character (ex: t
981d0 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 he 'd' in "%d")
981e0 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 is described.**
981f0 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
98200 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
98210 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 structure.*/.typ
98220 65 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 edef struct et_i
98230 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 nfo { /* Infor
98240 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 mation about eac
98250 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a h format field *
98260 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 /. char fmttype
98270 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
98280 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 The format field
98290 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a code letter */.
982a0 20 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 etByte base;
982b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
982c0 65 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 e base for radix
982d0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 conversion */.
982e0 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 etByte flags;
982f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
98300 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 or more of FLAG
98310 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f _ constants belo
98320 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 w */. etByte ty
98330 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pe;
98340 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 /* Conversion pa
98350 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 radigm */. etBy
98360 74 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 te charset;
98370 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
98380 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 nto aDigits[] of
98390 20 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69 the digits stri
983a0 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 ng */. etByte p
983b0 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 refix;
983c0 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
983d0 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 aPrefix[] of the
983e0 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a prefix string *
983f0 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a /.} et_info;../*
98400 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
98410 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 es for et_info.f
98420 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 lags.*/.#define
98430 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 FLAG_SIGNED 1
98440 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
98450 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 e value to conve
98460 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a rt is signed */.
98470 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 #define FLAG_INT
98480 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 ERN 2 /* Tr
98490 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e ue if for intern
984a0 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 al use only */.#
984b0 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 define FLAG_STRI
984c0 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c NG 4 /* All
984d0 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 ow infinity prec
984e0 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a ision */.../*.**
984f0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
98500 61 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 able is searched
98510 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 linearly, so it
98520 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 is good to put
98530 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 the.** most freq
98540 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 uently used conv
98550 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 ersion types fir
98560 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f st..*/.static co
98570 6e 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73 nst char aDigits
98580 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 [] = "0123456789
98590 41 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39 ABCDEF0123456789
985a0 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 abcdef";.static
985b0 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 const char aPref
985c0 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 ix[] = "-x0\000X
985d0 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 0";.static const
985e0 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f et_info fmtinfo
985f0 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c [] = {. { 'd',
98600 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 10, 1, etRADIX,
98610 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
98620 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 { 's', 0, 4,
98630 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c etSTRING, 0,
98640 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 0 },. { 'g',
98650 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 0, 1, etGENERI
98660 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 C, 30, 0 },.
98670 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 { 'z', 0, 4,
98680 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c etDYNSTRING, 0,
98690 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 0 },. { 'q',
986a0 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 0, 4, etSQLESC
986b0 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 APE, 0, 0 },.
986c0 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 { 'Q', 0, 4,
986d0 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c etSQLESCAPE2, 0,
986e0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 0 },. { 'w',
986f0 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 0, 4, etSQLESC
98700 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 APE3, 0, 0 },.
98710 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 { 'c', 0, 0,
98720 65 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c etCHARX, 0,
98730 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 0 },. { 'o',
98740 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 8, 0, etRADIX,
98750 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 0, 2 },.
98760 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 { 'u', 10, 0,
98770 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
98780 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 0 },. { 'x',
98790 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 16, 0, etRADIX,
987a0 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 16, 1 },.
987b0 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 { 'X', 16, 0,
987c0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
987d0 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 4 },.#ifndef S
987e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
987f0 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 ING_POINT. { '
98800 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f f', 0, 1, etFLO
98810 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d AT, 0, 0 }
98820 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 ,. { 'e', 0,
98830 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 1, etEXP,
98840 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 30, 0 },. { '
98850 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 E', 0, 1, etEXP
98860 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d , 14, 0 }
98870 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 ,. { 'G', 0,
98880 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 1, etGENERIC,
98890 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 14, 0 },.#endif
988a0 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 . { 'i', 10, 1
988b0 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 , etRADIX,
988c0 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 0, 0 },. { 'n
988d0 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 ', 0, 0, etSIZE
988e0 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c , 0, 0 },
988f0 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 . { '%', 0, 0
98900 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 , etPERCENT,
98910 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 0, 0 },. { 'p
98920 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e ', 16, 0, etPOIN
98930 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c TER, 0, 1 },
98940 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 . { 'T', 0, 2
98950 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 , etTOKEN,
98960 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 0, 0 },. { 'S
98970 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c ', 0, 2, etSRCL
98980 49 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c IST, 0, 0 },
98990 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 . { 'r', 10, 3
989a0 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 , etORDINAL,
989b0 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 0, 0 },.};../*.
989c0 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ** If SQLITE_OMI
989d0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
989e0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 is defined, the
989f0 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c n none of the fl
98a00 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 oating point.**
98a10 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c conversions will
98a20 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 work..*/.#ifnde
98a30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
98a40 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a OATING_POINT./*.
98a50 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64 ** "*val" is a d
98a60 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20 ouble such that
98a70 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30 0.1 <= *val < 10
98a80 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 .0.** Return the
98a90 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20 ascii code for
98aa0 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69 the leading digi
98ab0 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a t of *val, then.
98ac0 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61 ** multiply "*va
98ad0 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65 l" by 10.0 to re
98ae0 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a normalize..**.**
98af0 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20 Example:.**
98b00 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c input: *val
98b10 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20 = 3.14159.**
98b20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61 output: *va
98b30 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75 l = 1.4159 fu
98b40 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 nction return =
98b50 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f '3'.**.** The co
98b60 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e unter *cnt is in
98b70 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 cremented each t
98b80 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e ime. After coun
98b90 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31 ter exceeds.** 1
98ba0 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 6 (the number of
98bb0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 significant dig
98bc0 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20 its in a 64-bit
98bd0 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a float) '0' is.**
98be0 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64 always returned
98bf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
98c00 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e et_getdigit(LON
98c10 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61 GDOUBLE_TYPE *va
98c20 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 l, int *cnt){.
98c30 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e int digit;. LON
98c40 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a GDOUBLE_TYPE d;.
98c50 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e if( (*cnt)++ >
98c60 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30 = 16 ) return '0
98c70 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e ';. digit = (in
98c80 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69 t)*val;. d = di
98c90 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20 git;. digit +=
98ca0 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a '0';. *val = (*
98cb0 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 val - d)*10.0;.
98cc0 20 72 65 74 75 72 6e 20 28 63 68 61 72 29 64 69 return (char)di
98cd0 67 69 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a git;.}.#endif /*
98ce0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
98cf0 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a ATING_POINT */..
98d00 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 /*.** Append N s
98d10 70 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20 pace characters
98d20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 to the given str
98d30 69 6e 67 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 ing buffer..*/.s
98d40 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e tatic void appen
98d50 64 53 70 61 63 65 28 53 74 72 41 63 63 75 6d 20 dSpace(StrAccum
98d60 2a 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b *pAccum, int N){
98d70 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
98d80 63 68 61 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d char zSpaces[] =
98d90 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
98da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
98db0 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69 ;. while( N>=(i
98dc0 6e 74 29 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 nt)sizeof(zSpace
98dd0 73 29 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 s)-1 ){. sqli
98de0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
98df0 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65 d(pAccum, zSpace
98e00 73 2c 20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 s, sizeof(zSpace
98e10 73 29 2d 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 s)-1);. N -=
98e20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d sizeof(zSpaces)-
98e30 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30 1;. }. if( N>0
98e40 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
98e50 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
98e60 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e ccum, zSpaces, N
98e70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
98e80 4f 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74 68 On machines with
98e90 20 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73 a small stack s
98ea0 69 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65 64 ize, you can red
98eb0 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c efine the.** SQL
98ec0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 ITE_PRINT_BUF_SI
98ed0 5a 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68 ZE to be less th
98ee0 61 6e 20 33 35 30 2e 20 20 42 75 74 20 62 65 77 an 350. But bew
98ef0 61 72 65 20 2d 20 66 6f 72 0a 2a 2a 20 73 6d 61 are - for.** sma
98f00 6c 6c 65 72 20 76 61 6c 75 65 73 20 73 6f 6d 65 ller values some
98f10 20 25 66 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 %f conversions
98f20 6d 61 79 20 67 6f 20 69 6e 74 6f 20 61 6e 20 69 may go into an i
98f30 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 2e 0a 2a 2f nfinite loop..*/
98f40 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
98f50 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a 23 PRINT_BUF_SIZE.#
98f60 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 define SQLITE_P
98f70 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33 35 RINT_BUF_SIZE 35
98f80 30 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 0.#endif.#define
98f90 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 54 etBUFSIZE SQLIT
98fa0 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 E_PRINT_BUF_SIZE
98fb0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
98fc0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a output buffer *
98fd0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f /../*.** The roo
98fe0 74 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 t program. All
98ff0 76 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20 variations call
99000 74 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a this core..**.**
99010 20 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75 INPUTS:.** fu
99020 6e 63 20 20 20 54 68 69 73 20 69 73 20 61 20 70 nc This is a p
99030 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 ointer to a func
99040 74 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72 65 tion taking thre
99050 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 e arguments.**
99060 20 20 20 20 20 20 20 20 20 20 31 2e 20 41 20 70 1. A p
99070 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 ointer to anythi
99080 6e 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68 65 ng. Same as the
99090 20 22 61 72 67 22 20 70 61 72 61 6d 65 74 65 72 "arg" parameter
990a0 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 ..**
990b0 32 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 2. A pointer to
990c0 74 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61 72 the list of char
990d0 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 acters to be out
990e0 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 put.**
990f0 20 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69 73 (Note, this
99100 20 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c list is NOT nul
99110 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a l terminated.).*
99120 2a 20 20 20 20 20 20 20 20 20 20 20 20 33 2e 20 * 3.
99130 41 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 An integer numbe
99140 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 r of characters
99150 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a to be output..**
99160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
99170 4e 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62 65 Note: This numbe
99180 72 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f 2e r might be zero.
99190 29 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20 ).**.** arg
991a0 20 54 68 69 73 20 69 73 20 74 68 65 20 70 6f 69 This is the poi
991b0 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 nter to anything
991c0 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 70 which will be p
991d0 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 assed as the.**
991e0 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 61 first a
991f0 72 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63 rgument to "func
99200 22 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20 77 ". Use it for w
99210 68 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b 65 hatever you like
99220 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20 ..**.** fmt
99230 20 54 68 69 73 20 69 73 20 74 68 65 20 66 6f 72 This is the for
99240 6d 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20 69 mat string, as i
99250 6e 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e n the usual prin
99260 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20 t..**.** ap
99270 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e This is a poin
99280 74 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f 66 ter to a list of
99290 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d arguments. Sam
992a0 65 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 e as in.**
992b0 20 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a vfprint..**.
992c0 2a 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20 ** OUTPUTS:.**
992d0 20 20 20 20 20 20 20 20 54 68 65 20 72 65 74 75 The retu
992e0 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 rn value is the
992f0 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
99300 63 68 61 72 61 63 74 65 72 73 20 73 65 6e 74 20 characters sent
99310 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 to.** t
99320 68 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e he function "fun
99330 63 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31 20 c". Returns -1
99340 6f 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a on a error..**.*
99350 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * Note that the
99360 6f 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 61 order in which a
99370 75 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62 6c utomatic variabl
99380 65 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20 es are declared
99390 62 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74 below.** seems t
993a0 6f 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69 66 o make a big dif
993b0 66 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65 72 ference in deter
993c0 6d 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74 20 mining how fast
993d0 74 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77 69 this beast.** wi
993e0 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ll run..*/.SQLIT
993f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
99400 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 0a qlite3VXPrintf(.
99410 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 StrAccum *pAcc
99420 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 um,
99430 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 /* Accumula
99440 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 te results here
99450 2a 2f 0a 20 20 69 6e 74 20 75 73 65 45 78 74 65 */. int useExte
99460 6e 64 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 nded,
99470 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 /* Allow
99480 20 65 78 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76 extended %-conv
99490 65 72 73 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e ersions */. con
994a0 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20 st char *fmt,
994b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
994c0 2f 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 /* Format string
994d0 20 2a 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 */. va_list ap
994e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
994f0 20 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75 /* argu
99500 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e ments */.){. in
99510 74 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 t c;
99520 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
99530 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 character in th
99540 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 e format string
99550 2a 2f 0a 20 20 63 68 61 72 20 2a 62 75 66 70 74 */. char *bufpt
99560 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
99570 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
99580 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 e conversion buf
99590 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 fer */. int pre
995a0 63 69 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 cision;
995b0 20 20 20 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e /* Precision
995c0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
995d0 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c field */. int l
995e0 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20 ength;
995f0 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
99600 6f 66 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a of the field */.
99610 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 int idx;
99620 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
99630 41 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 A general purpos
99640 65 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a e loop counter *
99650 2f 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20 /. int width;
99660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
99670 2a 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63 * Width of the c
99680 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a urrent field */.
99690 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65 etByte flag_le
996a0 66 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20 ftjustify; /*
996b0 54 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67 True if "-" flag
996c0 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 is present */.
996d0 20 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75 etByte flag_plu
996e0 73 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54 ssign; /* T
996f0 72 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20 rue if "+" flag
99700 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 is present */.
99710 65 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e etByte flag_blan
99720 6b 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72 ksign; /* Tr
99730 75 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69 ue if " " flag i
99740 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 s present */. e
99750 74 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72 tByte flag_alter
99760 6e 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 nateform; /* Tru
99770 65 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73 e if "#" flag is
99780 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 present */. et
99790 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72 Byte flag_altfor
997a0 6d 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 m2; /* True
997b0 20 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20 if "!" flag is
997c0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
997d0 79 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 yte flag_zeropad
997e0 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
997f0 69 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63 if field width c
99800 6f 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77 onstant starts w
99810 69 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 ith zero */. et
99820 42 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 Byte flag_long;
99830 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
99840 20 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20 if "l" flag is
99850 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
99860 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e yte flag_longlon
99870 67 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 g; /* True
99880 69 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 if the "ll" flag
99890 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 is present */.
998a0 20 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 etByte done;
998b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
998c0 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 oop termination
998d0 66 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 flag */. sqlite
998e0 5f 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75 _uint64 longvalu
998f0 65 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f e; /* Value fo
99900 72 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20 r integer types
99910 2a 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f */. LONGDOUBLE_
99920 54 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20 TYPE realvalue;
99930 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61 /* Value for rea
99940 6c 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e l types */. con
99950 73 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f st et_info *info
99960 70 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 p; /* Point
99970 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 er to the approp
99980 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63 riate info struc
99990 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62 ture */. char b
999a0 75 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20 uf[etBUFSIZE];
999b0 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 /* Conversi
999c0 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 on buffer */. c
999d0 68 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20 har prefix;
999e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 /* Pre
999f0 66 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20 fix character.
99a00 22 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 "+" or "-" or "
99a10 22 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 " or '\0'. */.
99a20 65 74 42 79 74 65 20 78 74 79 70 65 20 3d 20 30 etByte xtype = 0
99a30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ; /* Co
99a40 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 nversion paradig
99a50 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 m */. char *zEx
99a60 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 tra;
99a70 20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72 /* Extra memor
99a80 79 20 75 73 65 64 20 66 6f 72 20 65 74 54 43 4c y used for etTCL
99a90 45 53 43 41 50 45 20 63 6f 6e 76 65 72 73 69 6f ESCAPE conversio
99aa0 6e 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ns */.#ifndef SQ
99ab0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
99ac0 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20 NG_POINT. int
99ad0 65 78 70 2c 20 65 32 3b 20 20 20 20 20 20 20 20 exp, e2;
99ae0 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e /* exponen
99af0 74 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62 65 72 t of real number
99b00 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f s */. double ro
99b10 75 6e 64 65 72 3b 20 20 20 20 20 20 20 20 20 20 under;
99b20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 72 6f /* Used for ro
99b30 75 6e 64 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 unding floating
99b40 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a point values */.
99b50 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70 etByte flag_dp
99b60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
99b70 54 72 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20 True if decimal
99b80 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 point should be
99b90 73 68 6f 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 shown */. etByt
99ba0 65 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 e flag_rtz;
99bb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
99bc0 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 trailing zeros
99bd0 73 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 should be remove
99be0 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c d */. etByte fl
99bf0 61 67 5f 65 78 70 3b 20 20 20 20 20 20 20 20 20 ag_exp;
99c00 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 6f 72 /* True to for
99c10 63 65 20 64 69 73 70 6c 61 79 20 6f 66 20 74 68 ce display of th
99c20 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 e exponent */.
99c30 69 6e 74 20 6e 73 64 3b 20 20 20 20 20 20 20 20 int nsd;
99c40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
99c50 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 mber of signific
99c60 61 6e 74 20 64 69 67 69 74 73 20 72 65 74 75 72 ant digits retur
99c70 6e 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 ned */.#endif..
99c80 20 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62 length = 0;. b
99c90 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 ufpt = 0;. for(
99ca0 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b ; (c=(*fmt))!=0;
99cb0 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 66 28 ++fmt){. if(
99cc0 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 c!='%' ){.
99cd0 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 20 20 int amt;.
99ce0 62 75 66 70 74 20 3d 20 28 63 68 61 72 20 2a 29 bufpt = (char *)
99cf0 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d fmt;. amt =
99d00 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 1;. while(
99d10 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 (c=(*++fmt))!='
99d20 25 27 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74 %' && c!=0 ) amt
99d30 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ++;. sqlite
99d40 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
99d50 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61 pAccum, bufpt, a
99d60 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 mt);. if( c
99d70 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ==0 ) break;.
99d80 20 7d 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a }. if( (c=(*
99d90 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20 ++fmt))==0 ){.
99da0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
99db0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
99dc0 2c 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 20 20 , "%", 1);.
99dd0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
99de0 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 /* Find out wh
99df0 61 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65 at flags are pre
99e00 73 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 sent */. flag
99e10 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66 _leftjustify = f
99e20 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 lag_plussign = f
99e30 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 lag_blanksign =
99e40 0a 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 . flag_alter
99e50 6e 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f nateform = flag_
99e60 61 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f altform2 = flag_
99e70 7a 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 zeropad = 0;.
99e80 20 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 done = 0;. d
99e90 6f 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 o{. switch(
99ea0 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 c ){. ca
99eb0 73 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c se '-': flag_l
99ec0 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 eftjustify = 1;
99ed0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
99ee0 20 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66 case '+': f
99ef0 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 lag_plussign = 1
99f00 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a ; break;.
99f10 20 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27 case ' '
99f20 3a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 : flag_blanksi
99f30 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72 gn = 1; br
99f40 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
99f50 65 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c e '#': flag_al
99f60 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b ternateform = 1;
99f70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
99f80 20 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c case '!': fl
99f90 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b ag_altform2 = 1;
99fa0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
99fb0 20 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a case '0':
99fc0 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 flag_zeropad
99fd0 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65 = 1; bre
99fe0 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61 ak;. defa
99ff0 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 ult: done = 1
9a000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
9a010 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
9a020 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f . }while( !do
9a030 6e 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 ne && (c=(*++fmt
9a040 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 ))!=0 );. /*
9a050 47 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69 Get the field wi
9a060 64 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 dth */. width
9a070 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d = 0;. if( c=
9a080 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69 ='*' ){. wi
9a090 64 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c dth = va_arg(ap,
9a0a0 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 int);. if(
9a0b0 77 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 width<0 ){.
9a0c0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
9a0d0 69 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ify = 1;.
9a0e0 20 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b width = -width;
9a0f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 . }. c
9a100 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d = *++fmt;. }
9a110 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c else{. whil
9a120 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d e( c>='0' && c<=
9a130 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 '9' ){. w
9a140 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20 idth = width*10
9a150 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 + c - '0';.
9a160 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
9a170 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
9a180 20 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42 if( width > etB
9a190 55 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 UFSIZE-10 ){.
9a1a0 20 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46 width = etBUF
9a1b0 53 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 SIZE-10;. }.
9a1c0 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72 /* Get the pr
9a1d0 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 ecision */. i
9a1e0 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 f( c=='.' ){.
9a1f0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 precision = 0
9a200 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 ;. c = *++f
9a210 6d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d mt;. if( c=
9a220 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='*' ){.
9a230 70 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 precision = va_a
9a240 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 rg(ap,int);.
9a250 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
9a260 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 n<0 ) precision
9a270 3d 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 = -precision;.
9a280 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 c = *++fmt
9a290 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
9a2a0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e while( c>
9a2b0 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 ='0' && c<='9' )
9a2c0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 {. prec
9a2d0 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f ision = precisio
9a2e0 6e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a n*10 + c - '0';.
9a2f0 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b c = *+
9a300 2b 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a +fmt;. }.
9a310 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
9a320 65 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 e{. precisi
9a330 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 on = -1;. }.
9a340 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f /* Get the co
9a350 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f nversion type mo
9a360 64 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 difier */. if
9a370 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 ( c=='l' ){.
9a380 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b flag_long = 1;
9a390 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d . c = *++fm
9a3a0 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d t;. if( c==
9a3b0 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66 'l' ){. f
9a3c0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 lag_longlong = 1
9a3d0 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b ;. c = *+
9a3e0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 +fmt;. }els
9a3f0 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f e{. flag_
9a400 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 longlong = 0;.
9a410 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
9a420 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 . flag_long
9a430 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 = flag_longlong
9a440 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
9a450 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66 /* Fetch the inf
9a460 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 o entry for the
9a470 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 field */. inf
9a480 6f 70 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 op = 0;. for(
9a490 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 79 idx=0; idx<Array
9a4a0 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 69 Size(fmtinfo); i
9a4b0 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 dx++){. if(
9a4c0 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d c==fmtinfo[idx]
9a4d0 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 20 .fmttype ){.
9a4e0 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 infop = &fmt
9a4f0 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20 info[idx];.
9a500 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e 64 if( useExtend
9a510 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c ed || (infop->fl
9a520 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 52 ags & FLAG_INTER
9a530 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 N)==0 ){.
9a540 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f 70 xtype = infop
9a550 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20 20 ->type;.
9a560 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
9a570 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 return;.
9a580 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b }. break
9a590 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
9a5a0 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a zExtra = 0;.
9a5b0 20 20 20 20 69 66 28 20 69 6e 66 6f 70 3d 3d 30 if( infop==0
9a5c0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
9a5d0 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a ;. }... /*
9a5e0 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69 Limit the preci
9a5f0 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 sion to prevent
9a600 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b overflowing buf[
9a610 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 ] during convers
9a620 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 ion */. if( p
9a630 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 recision>etBUFSI
9a640 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d ZE-40 && (infop-
9a650 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54 >flags & FLAG_ST
9a660 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 RING)==0 ){.
9a670 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 precision = et
9a680 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 BUFSIZE-40;.
9a690 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a }.. /*. **
9a6a0 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 At this point,
9a6b0 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e variables are in
9a6c0 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c itialized as fol
9a6d0 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 lows:. **.
9a6e0 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 ** flag_alter
9a6f0 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 nateform
9a700 20 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20 TRUE if a '#'
9a710 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 is present..
9a720 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 ** flag_altfor
9a730 6d 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m2
9a740 20 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69 TRUE if a '!' i
9a750 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a s present.. *
9a760 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 * flag_plussig
9a770 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
9a780 54 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 TRUE if a '+' is
9a790 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a present.. **
9a7a0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
9a7b0 69 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54 ify T
9a7c0 52 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 RUE if a '-' is
9a7d0 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 present or if th
9a7e0 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 e. **
9a7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a800 20 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 field wid
9a810 74 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e th was negative.
9a820 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a . ** flag_z
9a830 65 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 eropad
9a840 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 TRUE if th
9a850 65 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 e width began wi
9a860 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 th 0.. ** f
9a870 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 lag_long
9a880 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 TRUE
9a890 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c if the letter 'l
9a8a0 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 ' (ell) prefixed
9a8b0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
9a8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a8d0 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 the conver
9a8e0 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a sion character..
9a8f0 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f ** flag_lo
9a900 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 nglong
9a910 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 TRUE if the
9a920 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c letter 'll' (el
9a930 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a l ell) prefixed.
9a940 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
9a950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a960 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 the convers
9a970 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 ion character..
9a980 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 ** flag_bla
9a990 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 nksign
9a9a0 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20 TRUE if a '
9a9b0 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 ' is present..
9a9c0 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 ** width
9a9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a9e0 20 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 The specified
9a9f0 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 field width. T
9aa00 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 his is. **
9aa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9aa20 20 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 alwa
9aa30 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e ys non-negative.
9aa40 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 Zero is the de
9aa50 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 fault.. **
9aa60 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 precision
9aa70 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
9aa80 73 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 specified precis
9aa90 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c ion. The defaul
9aaa0 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 t. **
9aab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9aac0 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 is -1..
9aad0 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 ** xtype
9aae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9aaf0 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 The class of
9ab00 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a the conversion..
9ab10 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 ** infop
9ab20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ab30 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 Pointer to
9ab40 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
9ab50 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 info struct..
9ab60 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 */. switch(
9ab70 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 xtype ){. c
9ab80 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 ase etPOINTER:.
9ab90 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 flag_long
9aba0 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 long = sizeof(ch
9abb0 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 ar*)==sizeof(i64
9abc0 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f );. flag_
9abd0 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 long = sizeof(ch
9abe0 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e ar*)==sizeof(lon
9abf0 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 g int);.
9ac00 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 /* Fall through
9ac10 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 into the next ca
9ac20 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 se */. case
9ac30 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 etORDINAL:.
9ac40 20 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a case etRADIX:.
9ac50 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f if( info
9ac60 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f p->flags & FLAG_
9ac70 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 SIGNED ){.
9ac80 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 i64 v;.
9ac90 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f if( flag_lo
9aca0 6e 67 6c 6f 6e 67 20 29 20 20 20 76 20 3d 20 76 nglong ) v = v
9acb0 61 5f 61 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 a_arg(ap,i64);.
9acc0 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 else if
9acd0 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 20 20 76 ( flag_long ) v
9ace0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e = va_arg(ap,lon
9acf0 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 g int);.
9ad00 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 else
9ad10 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 v = va_a
9ad20 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 rg(ap,int);.
9ad30 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b if( v<0 ){
9ad40 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e . lon
9ad50 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 gvalue = -v;.
9ad60 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 prefix
9ad70 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 = '-';.
9ad80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
9ad90 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 longvalue =
9ada0 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 v;. i
9adb0 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e f( flag_plussign
9adc0 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78 ) prefix
9add0 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 = '+';.
9ade0 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 else if( fla
9adf0 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 g_blanksign ) p
9ae00 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 refix = ' ';.
9ae10 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 else
9ae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ae30 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a prefix = 0;.
9ae40 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
9ae50 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9ae60 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f if( flag_lo
9ae70 6e 67 6c 6f 6e 67 20 29 20 20 20 6c 6f 6e 67 76 nglong ) longv
9ae80 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 alue = va_arg(ap
9ae90 2c 75 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 ,u64);.
9aea0 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c else if( flag_l
9aeb0 6f 6e 67 20 29 20 20 6c 6f 6e 67 76 61 6c 75 65 ong ) longvalue
9aec0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 = va_arg(ap,uns
9aed0 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b igned long int);
9aee0 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 . else
9aef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9af00 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f longvalue = va_
9af10 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 arg(ap,unsigned
9af20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 int);.
9af30 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 prefix = 0;.
9af40 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
9af50 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29 ( longvalue==0 )
9af60 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 flag_alternatef
9af70 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 orm = 0;.
9af80 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 if( flag_zeropa
9af90 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c 77 d && precision<w
9afa0 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 idth-(prefix!=0)
9afb0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 ){. pr
9afc0 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d ecision = width-
9afd0 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20 20 (prefix!=0);.
9afe0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 }. b
9aff0 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 42 55 ufpt = &buf[etBU
9b000 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 20 FSIZE-1];.
9b010 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 4f if( xtype==etO
9b020 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20 RDINAL ){.
9b030 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
9b040 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 char zOrd[] = "
9b050 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 20 thstndrd";.
9b060 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 6e int x = (in
9b070 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31 t)(longvalue % 1
9b080 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 0);. if
9b090 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76 ( x>=4 || (longv
9b0a0 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29 alue/10)%10==1 )
9b0b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20 {. x
9b0c0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d = 0;. }
9b0d0 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b 65 . buf[e
9b0e0 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 7a 4f tBUFSIZE-3] = zO
9b0f0 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 20 20 rd[x*2];.
9b100 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45 buf[etBUFSIZE
9b110 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b 31 -2] = zOrd[x*2+1
9b120 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 ];. buf
9b130 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20 pt -= 2;.
9b140 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20 }. {.
9b150 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20 register
9b160 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65 74 const char *cset
9b170 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72 65 ; /* Use re
9b180 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65 65 gisters for spee
9b190 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 d */. r
9b1a0 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73 65 egister int base
9b1b0 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65 74 ;. cset
9b1c0 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66 6f = &aDigits[info
9b1d0 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 p->charset];.
9b1e0 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69 6e base = in
9b1f0 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20 fop->base;.
9b200 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20 20 do{
9b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b230 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f /* Convert to
9b240 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20 20 ascii */.
9b250 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 *(--bufpt)
9b260 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75 = cset[longvalu
9b270 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20 20 e%base];.
9b280 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d longvalue =
9b290 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b longvalue/base;
9b2a0 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c . }whil
9b2b0 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29 e( longvalue>0 )
9b2c0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
9b2d0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e length = (in
9b2e0 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a t)(&buf[etBUFSIZ
9b2f0 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 E-1]-bufpt);.
9b300 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 for(idx=pre
9b310 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69 cision-length; i
9b320 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20 dx>0; idx--){.
9b330 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 *(--bufp
9b340 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20 t) = '0';
9b350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b360 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61 /* Zero pa
9b370 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 d */. }.
9b380 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 if( prefi
9b390 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d x ) *(--bufpt) =
9b3a0 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 prefix;
9b3b0 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69 /* Add si
9b3c0 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 gn */. if
9b3d0 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 ( flag_alternate
9b3e0 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70 form && infop->p
9b3f0 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a refix ){ /*
9b400 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 22 Add "0" or "0x"
9b410 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f */. co
9b420 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20 nst char *pre;.
9b430 20 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b char x;
9b440 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d . pre =
9b450 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d &aPrefix[infop-
9b460 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20 >prefix];.
9b470 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70 for(; (x=(*p
9b480 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20 re))!=0; pre++)
9b490 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a *(--bufpt) = x;.
9b4a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9b4b0 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 length = (int)
9b4c0 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d (&buf[etBUFSIZE-
9b4d0 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 20 20 1]-bufpt);.
9b4e0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
9b4f0 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20 20 case etFLOAT:.
9b500 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a 0a case etEXP:.
9b510 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45 4e case etGEN
9b520 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72 65 ERIC:. re
9b530 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 alvalue = va_arg
9b540 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69 66 (ap,double);.#if
9b550 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
9b560 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
9b570 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63 if( prec
9b580 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73 ision<0 ) precis
9b590 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20 ion = 6;
9b5a0 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20 /* Set default
9b5b0 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 precision */.
9b5c0 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 if( precisi
9b5d0 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 on>etBUFSIZE/2-1
9b5e0 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 0 ) precision =
9b5f0 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b 0a etBUFSIZE/2-10;.
9b600 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c if( real
9b610 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20 20 value<0.0 ){.
9b620 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 realvalue
9b630 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a 20 = -realvalue;.
9b640 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 prefix
9b650 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 7d = '-';. }
9b660 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
9b670 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 if( flag_plussig
9b680 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72 65 n ) pre
9b690 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 fix = '+';.
9b6a0 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c else if( fl
9b6b0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 ag_blanksign )
9b6c0 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a prefix = ' ';.
9b6d0 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 else
9b6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b6f0 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 prefix =
9b700 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
9b710 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d if( xtype==
9b720 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72 65 etGENERIC && pre
9b730 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63 69 cision>0 ) preci
9b740 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20 20 sion--;.#if 0.
9b750 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69 6e /* Roundin
9b760 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53 44 g works like BSD
9b770 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 61 when the consta
9b780 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73 65 nt 0.4999 is use
9b790 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20 20 d. Wierd! */.
9b7a0 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 for(idx=pr
9b7b0 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 ecision, rounder
9b7c0 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b 20 =0.4999; idx>0;
9b7d0 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d idx--, rounder*=
9b7e0 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 0.1);.#else.
9b7f0 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73 20 /* It makes
9b800 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75 73 more sense to us
9b810 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20 20 e 0.5 */.
9b820 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 for(idx=precisi
9b830 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35 3b on, rounder=0.5;
9b840 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 idx>0; idx--, r
9b850 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a 23 ounder*=0.1){}.#
9b860 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 endif. if
9b870 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 54 ( xtype==etFLOAT
9b880 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20 ) realvalue +=
9b890 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20 rounder;.
9b8a0 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 65 /* Normalize re
9b8b0 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 69 alvalue to withi
9b8c0 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 6c n 10.0 > realval
9b8d0 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20 20 ue >= 1.0 */.
9b8e0 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20 20 exp = 0;.
9b8f0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
9b900 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 29 72 3IsNaN((double)r
9b910 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 ealvalue) ){.
9b920 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 bufpt = "
9b930 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20 NaN";.
9b940 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20 length = 3;.
9b950 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
9b960 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
9b970 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30 f( realvalue>0.0
9b980 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 ){. wh
9b990 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d ile( realvalue>=
9b9a0 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30 1e32 && exp<=350
9b9b0 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d ){ realvalue *=
9b9c0 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b 1e-32; exp+=32;
9b9d0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 }. whi
9b9e0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 le( realvalue>=1
9b9f0 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 e8 && exp<=350 )
9ba00 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 { realvalue *= 1
9ba10 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20 e-8; exp+=8; }.
9ba20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
9ba30 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 realvalue>=10.0
9ba40 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 && exp<=350 ){ r
9ba50 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b ealvalue *= 0.1;
9ba60 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 exp++; }.
9ba70 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 while( realv
9ba80 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61 alue<1e-8 ){ rea
9ba90 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65 lvalue *= 1e8; e
9baa0 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 xp-=8; }.
9bab0 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 while( realva
9bac0 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76 lue<1.0 ){ realv
9bad0 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78 alue *= 10.0; ex
9bae0 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 p--; }.
9baf0 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a if( exp>350 ){.
9bb00 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
9bb10 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20 prefix=='-' ){.
9bb20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 buf
9bb30 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20 pt = "-Inf";.
9bb40 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }else i
9bb50 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29 f( prefix=='+' )
9bb60 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
9bb70 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a bufpt = "+Inf";.
9bb80 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }els
9bb90 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e{.
9bba0 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a bufpt = "Inf";.
9bbb0 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
9bbc0 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
9bbd0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
9bbe0 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 30(bufpt);.
9bbf0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
9bc00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9bc10 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 }. bufp
9bc20 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 t = buf;.
9bc30 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 /*. ** I
9bc40 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70 65 f the field type
9bc50 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20 74 is etGENERIC, t
9bc60 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20 65 hen convert to e
9bc70 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20 20 ither etEXP.
9bc80 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f 41 ** or etFLOA
9bc90 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 T, as appropriat
9bca0 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
9bcb0 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20 3d flag_exp =
9bcc0 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a 20 xtype==etEXP;.
9bcd0 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 if( xtype
9bce0 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20 20 !=etFLOAT ){.
9bcf0 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 realvalue
9bd00 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 += rounder;.
9bd10 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76 if( realv
9bd20 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 65 alue>=10.0 ){ re
9bd30 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20 alvalue *= 0.1;
9bd40 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 exp++; }.
9bd50 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 }. if( x
9bd60 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 type==etGENERIC
9bd70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 ){. fla
9bd80 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61 6c g_rtz = !flag_al
9bd90 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20 20 ternateform;.
9bda0 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c 2d if( exp<-
9bdb0 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73 69 4 || exp>precisi
9bdc0 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 on ){.
9bdd0 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50 3b xtype = etEXP;
9bde0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
9bdf0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 {. pr
9be00 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 ecision = precis
9be10 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20 20 ion - exp;.
9be20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 xtype = e
9be30 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20 20 tFLOAT;.
9be40 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
9be50 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 e{. fla
9be60 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20 20 g_rtz = 0;.
9be70 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
9be80 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b xtype==etEXP ){
9be90 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d 20 . e2 =
9bea0 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0;. }else
9beb0 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d {. e2 =
9bec0 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a exp;. }.
9bed0 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 3b nsd = 0;
9bee0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 70 . flag_dp
9bef0 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 20 = (precision>0
9bf00 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c 74 ?1:0) | flag_alt
9bf10 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c 61 ernateform | fla
9bf20 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 20 g_altform2;.
9bf30 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e 20 /* The sign
9bf40 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 20 in front of the
9bf50 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 number */.
9bf60 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b 0a if( prefix ){.
9bf70 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
9bf80 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a 20 t++) = prefix;.
9bf90 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9bfa0 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f 72 /* Digits prior
9bfb0 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c 20 to the decimal
9bfc0 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 point */.
9bfd0 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20 20 if( e2<0 ){.
9bfe0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
9bff0 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 ) = '0';.
9c000 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
9c010 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20 65 for(; e2>=0; e
9c020 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2--){.
9c030 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 *(bufpt++) = e
9c040 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c t_getdigit(&real
9c050 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20 value,&nsd);.
9c060 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9c070 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 }. /* Th
9c080 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 e decimal point
9c090 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 */. if( f
9c0a0 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 lag_dp ){.
9c0b0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
9c0c0 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a '.';. }.
9c0d0 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20 64 /* "0" d
9c0e0 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20 igits after the
9c0f0 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62 75 decimal point bu
9c100 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 t before the fir
9c110 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69 st. ** si
9c120 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 20 gnificant digit
9c130 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f of the number */
9c140 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32 2b . for(e2+
9c150 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73 69 +; e2<0; precisi
9c160 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 20 on--, e2++){.
9c170 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
9c180 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20 20 recision>0 );.
9c190 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
9c1a0 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 +) = '0';.
9c1b0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 }. /* S
9c1c0 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 ignificant digit
9c1d0 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 s after the deci
9c1e0 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 mal point */.
9c1f0 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 65 while( (pre
9c200 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a 20 cision--)>0 ){.
9c210 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
9c220 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69 ++) = et_getdigi
9c230 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 t(&realvalue,&ns
9c240 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 d);. }.
9c250 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 /* Remove
9c260 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 61 trailing zeros a
9c270 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e 6f nd the "." if no
9c280 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 74 digits follow t
9c290 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 20 he "." */.
9c2a0 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 26 if( flag_rtz &
9c2b0 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 & flag_dp ){.
9c2c0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 75 while( bu
9c2d0 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 2a fpt[-1]=='0' ) *
9c2e0 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a 20 (--bufpt) = 0;.
9c2f0 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
9c300 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 20 bufpt>buf );.
9c310 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 if( bufp
9c320 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 t[-1]=='.' ){.
9c330 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c if( fl
9c340 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a 20 ag_altform2 ){.
9c350 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 *(b
9c360 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 ufpt++) = '0';.
9c370 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }else
9c380 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
9c390 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a *(--bufpt) = 0;.
9c3a0 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
9c3b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9c3c0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 }. /* A
9c3d0 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 75 dd the "eNNN" su
9c3e0 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 20 ffix */.
9c3f0 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c 20 if( flag_exp ||
9c400 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b 0a xtype==etEXP ){.
9c410 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
9c420 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69 t++) = aDigits[i
9c430 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a nfop->charset];.
9c440 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 if( ex
9c450 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 p<0 ){.
9c460 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
9c470 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b '-'; exp = -exp;
9c480 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
9c490 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 {. *(
9c4a0 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b 0a bufpt++) = '+';.
9c4b0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
9c4c0 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d 31 if( exp>=1
9c4d0 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 00 ){.
9c4e0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 *(bufpt++) = (
9c4f0 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 2b char)((exp/100)+
9c500 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 '0'); /*
9c510 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 100's digit */.
9c520 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 25 exp %
9c530 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 20 = 100;.
9c540 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 }. *(b
9c550 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 ufpt++) = (char)
9c560 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 20 (exp/10+'0');
9c570 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 27 /* 10'
9c580 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 s digit */.
9c590 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
9c5a0 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 2b = (char)(exp%10+
9c5b0 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20 '0');
9c5c0 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 2a /* 1's digit *
9c5d0 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 /. }.
9c5e0 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b 0a *bufpt = 0;.
9c5f0 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
9c600 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 72 converted number
9c610 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64 is in buf[] and
9c620 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 zero terminated
9c630 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 20 . Output it..
9c640 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 ** Note tha
9c650 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 t the number is
9c660 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64 in the usual ord
9c670 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64 er, not reversed
9c680 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 20 as with.
9c690 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76 ** integer conv
9c6a0 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 ersions. */.
9c6b0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e length = (in
9c6c0 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b 0a 20 t)(bufpt-buf);.
9c6d0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
9c6e0 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 uf;.. /*
9c6f0 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 41 Special case: A
9c700 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 dd leading zeros
9c710 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65 72 if the flag_zer
9c720 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20 20 opad flag is.
9c730 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64 20 ** set and
9c740 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74 20 we are not left
9c750 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20 20 justified */.
9c760 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 if( flag_ze
9c770 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f 6c ropad && !flag_l
9c780 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c 65 eftjustify && le
9c790 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a 20 ngth < width){.
9c7a0 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a int i;.
9c7b0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 50 int nP
9c7c0 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65 6e ad = width - len
9c7d0 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20 66 gth;. f
9c7e0 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d 6e or(i=width; i>=n
9c7f0 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 Pad; i--){.
9c800 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d 20 bufpt[i]
9c810 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d 3b = bufpt[i-nPad];
9c820 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
9c830 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66 69 i = prefi
9c840 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 20 x!=0;.
9c850 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29 20 while( nPad-- )
9c860 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30 27 bufpt[i++] = '0'
9c870 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 ;. leng
9c880 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20 20 th = width;.
9c890 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
9c8a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
9c8b0 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a 20 case etSIZE:.
9c8c0 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67 28 *(va_arg(
9c8d0 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63 63 ap,int*)) = pAcc
9c8e0 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 um->nChar;.
9c8f0 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 length = widt
9c900 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 h = 0;. b
9c910 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
9c920 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 20 20 etPERCENT:.
9c930 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 25 27 buf[0] = '%'
9c940 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 ;. bufpt
9c950 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 6c = buf;. l
9c960 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 20 20 ength = 1;.
9c970 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
9c980 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20 20 case etCHARX:.
9c990 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 72 67 c = va_arg
9c9a0 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 (ap,int);.
9c9b0 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 61 72 buf[0] = (char
9c9c0 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 )c;. if(
9c9d0 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a precision>=0 ){.
9c9e0 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 for(id
9c9f0 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 73 69 x=1; idx<precisi
9ca00 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 5b 69 on; idx++) buf[i
9ca10 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 dx] = (char)c;.
9ca20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 length
9ca30 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 = precision;.
9ca40 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
9ca50 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b length =1;
9ca60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
9ca70 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a bufpt = buf;.
9ca80 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9ca90 20 20 20 20 20 63 61 73 65 20 65 74 53 54 52 49 case etSTRI
9caa0 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 NG:. case e
9cab0 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20 tDYNSTRING:.
9cac0 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f 61 bufpt = va_a
9cad0 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 rg(ap,char*);.
9cae0 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d if( bufpt=
9caf0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
9cb00 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20 20 bufpt = "";.
9cb10 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78 74 }else if( xt
9cb20 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47 ype==etDYNSTRING
9cb30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45 ){. zE
9cb40 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20 20 xtra = bufpt;.
9cb50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9cb60 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 if( precision>=0
9cb70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f ){. fo
9cb80 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 6e 67 r(length=0; leng
9cb90 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 26 20 th<precision &&
9cba0 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 6c bufpt[length]; l
9cbb0 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 20 ength++){}.
9cbc0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
9cbd0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c length = sql
9cbe0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66 ite3Strlen30(buf
9cbf0 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 pt);. }.
9cc00 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
9cc10 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53 case etSQLES
9cc20 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73 65 CAPE:. case
9cc30 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a 20 etSQLESCAPE2:.
9cc40 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 case etSQLE
9cc50 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20 20 SCAPE3: {.
9cc60 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 69 int i, j, n, i
9cc70 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 snull;. i
9cc80 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 nt needQuote;.
9cc90 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a 20 char ch;.
9cca0 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20 char q =
9ccb0 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 ((xtype==etSQLES
9ccc0 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 CAPE3)?'"':'\'')
9ccd0 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 ; /* Quote cha
9cce0 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 racter */.
9ccf0 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d char *escarg =
9cd00 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a va_arg(ap,char*
9cd10 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c );. isnul
9cd20 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 l = escarg==0;.
9cd30 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c if( isnul
9cd40 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74 l ) escarg = (xt
9cd50 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 ype==etSQLESCAPE
9cd60 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 2 ? "NULL" : "(N
9cd70 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20 ULL)");.
9cd80 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 for(i=n=0; (ch=e
9cd90 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b scarg[i])!=0; i+
9cda0 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
9cdb0 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a ( ch==q ) n++;.
9cdc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9cdd0 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69 needQuote = !i
9cde0 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d snull && xtype==
9cdf0 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 etSQLESCAPE2;.
9ce00 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 n += i + 1
9ce10 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a + needQuote*2;.
9ce20 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74 if( n>et
9ce30 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 BUFSIZE ){.
9ce40 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78 bufpt = zEx
9ce50 74 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c tra = sqlite3Mal
9ce60 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 loc( n );.
9ce70 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 if( bufpt==0
9ce80 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
9ce90 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 pAccum->mallocFa
9cea0 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 iled = 1;.
9ceb0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
9cec0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9ced0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
9cee0 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a bufpt = buf;.
9cef0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9cf00 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 j = 0;.
9cf10 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 if( needQuote )
9cf20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b bufpt[j++] = q;
9cf30 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 . for(i=0
9cf40 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 ; (ch=escarg[i])
9cf50 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 !=0; i++){.
9cf60 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 bufpt[j++]
9cf70 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20 = ch;.
9cf80 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 70 if( ch==q ) bufp
9cf90 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 t[j++] = ch;.
9cfa0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
9cfb0 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62 f( needQuote ) b
9cfc0 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 ufpt[j++] = q;.
9cfd0 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d 20 bufpt[j]
9cfe0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e = 0;. len
9cff0 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 gth = j;.
9d000 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 6f /* The precisio
9d010 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20 n is ignored on
9d020 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20 %q and %Q */.
9d030 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 63 /* if( prec
9d040 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 ision>=0 && prec
9d050 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c ision<length ) l
9d060 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f ength = precisio
9d070 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 n; */. br
9d080 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
9d090 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a case etTOKEN:
9d0a0 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e {. Token
9d0b0 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 *pToken = va_ar
9d0c0 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 g(ap, Token*);.
9d0d0 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 if( pToke
9d0e0 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 n ){. s
9d0f0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
9d100 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f pend(pAccum, (co
9d110 6e 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e nst char*)pToken
9d120 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b ->z, pToken->n);
9d130 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
9d140 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 length = widt
9d150 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 h = 0;. b
9d160 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
9d170 20 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49 case etSRCLI
9d180 53 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 ST: {. Sr
9d190 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 cList *pSrc = va
9d1a0 5f 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 _arg(ap, SrcList
9d1b0 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 *);. int
9d1c0 6b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 k = va_arg(ap, i
9d1d0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72 nt);. str
9d1e0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
9d1f0 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d *pItem = &pSrc-
9d200 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 >a[k];. a
9d210 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b ssert( k>=0 && k
9d220 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 <pSrc->nSrc );.
9d230 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d if( pItem
9d240 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20 ->zDatabase ){.
9d250 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
9d260 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
9d270 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 Accum, pItem->zD
9d280 61 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 atabase, -1);.
9d290 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
9d2a0 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
9d2b0 63 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 ccum, ".", 1);.
9d2c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9d2d0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
9d2e0 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 Append(pAccum, p
9d2f0 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 Item->zName, -1)
9d300 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 ;. length
9d310 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 = width = 0;.
9d320 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
9d330 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 }. }/* End
9d340 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 68 65 switch over the
9d350 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a format type */.
9d360 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 /*. ** Th
9d370 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f e text of the co
9d380 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e nversion is poin
9d390 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 70 74 ted to by "bufpt
9d3a0 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 " and is. **
9d3b0 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63 74 "length" charact
9d3c0 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 ers long. The f
9d3d0 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 22 77 ield width is "w
9d3e0 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a idth". Do. *
9d3f0 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 * the output..
9d400 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 6c */. if( !fl
9d410 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 29 ag_leftjustify )
9d420 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65 72 {. register
9d430 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 int nspace;.
9d440 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74 nspace = widt
9d450 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 h-length;.
9d460 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a if( nspace>0 ){.
9d470 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53 70 appendSp
9d480 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 ace(pAccum, nspa
9d490 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ce);. }.
9d4a0 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 }. if( lengt
9d4b0 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c h>0 ){. sql
9d4c0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
9d4d0 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 nd(pAccum, bufpt
9d4e0 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d , length);. }
9d4f0 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 . if( flag_le
9d500 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 ftjustify ){.
9d510 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 register int
9d520 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 nspace;. ns
9d530 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e pace = width-len
9d540 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e gth;. if( n
9d550 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 space>0 ){.
9d560 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 70 appendSpace(p
9d570 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a Accum, nspace);.
9d580 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
9d590 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a if( zExtra ){.
9d5a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
9d5b0 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 ee(zExtra);.
9d5c0 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 }. }/* End for
9d5d0 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f loop over the fo
9d5e0 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d rmat string */.}
9d5f0 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 /* End of funct
9d600 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 ion */../*.** Ap
9d610 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 20 pend N bytes of
9d620 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 text from z to t
9d630 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 he StrAccum obje
9d640 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ct..*/.SQLITE_PR
9d650 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
9d660 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
9d670 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f (StrAccum *p, co
9d680 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
9d690 20 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f N){. if( p->to
9d6a0 6f 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 oBig | p->malloc
9d6b0 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 Failed ){. re
9d6c0 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 turn;. }. if(
9d6d0 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 N<0 ){. N = s
9d6e0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
9d6f0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d );. }. if( N==
9d700 30 20 7c 7c 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 0 || z==0 ){.
9d710 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
9d720 66 28 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d f( p->nChar+N >=
9d730 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 p->nAlloc ){.
9d740 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 char *zNew;.
9d750 20 20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c if( !p->useMal
9d760 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e loc ){. p->
9d770 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 tooBig = 1;.
9d780 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 N = p->nAlloc
9d790 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0a - p->nChar - 1;.
9d7a0 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 29 if( N<=0 )
9d7b0 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
9d7c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
9d7d0 6c 73 65 7b 0a 20 20 20 20 20 20 69 36 34 20 73 lse{. i64 s
9d7e0 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b zNew = p->nChar;
9d7f0 0a 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20 . szNew +=
9d800 4e 20 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 28 N + 1;. if(
9d810 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c szNew > p->mxAl
9d820 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 loc ){. s
9d830 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
9d840 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 set(p);.
9d850 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 p->tooBig = 1;.
9d860 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
9d870 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
9d880 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 p->nAlloc =
9d890 28 69 6e 74 29 73 7a 4e 65 77 3b 0a 20 20 20 20 (int)szNew;.
9d8a0 20 20 7d 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d }. zNew =
9d8b0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
9d8c0 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 41 Raw(p->db, p->nA
9d8d0 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 20 20 69 66 lloc );. if
9d8e0 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 ( zNew ){.
9d8f0 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 70 memcpy(zNew, p
9d900 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68 61 ->zText, p->nCha
9d910 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 r);. sqli
9d920 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 te3StrAccumReset
9d930 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
9d940 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0a 20 20 zText = zNew;.
9d950 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9d960 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c p->mallocFail
9d970 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ed = 1;.
9d980 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 sqlite3StrAccumR
9d990 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 eset(p);.
9d9a0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
9d9b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d . }. }. mem
9d9c0 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d cpy(&p->zText[p-
9d9d0 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a >nChar], z, N);.
9d9e0 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b p->nChar += N;
9d9f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68 .}../*.** Finish
9da00 20 6f 66 66 20 61 20 73 74 72 69 6e 67 20 62 79 off a string by
9da10 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 69 74 20 making sure it
9da20 69 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 is zero-terminat
9da30 65 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ed..** Return a
9da40 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
9da50 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e esulting string.
9da60 20 20 52 65 74 75 72 6e 20 61 20 4e 55 4c 4c 0a Return a NULL.
9da70 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 61 6e ** pointer if an
9da80 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 y kind of error
9da90 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e was encountered.
9daa0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9dab0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
9dac0 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53 StrAccumFinish(S
9dad0 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 trAccum *p){. i
9dae0 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 f( p->zText ){.
9daf0 20 20 20 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e p->zText[p->n
9db00 43 68 61 72 5d 20 3d 20 30 3b 0a 20 20 20 20 69 Char] = 0;. i
9db10 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 f( p->useMalloc
9db20 26 26 20 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e && p->zText==p->
9db30 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20 70 zBase ){. p
9db40 2d 3e 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65 ->zText = sqlite
9db50 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 3DbMallocRaw(p->
9db60 64 62 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 20 29 db, p->nChar+1 )
9db70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a ;. if( p->z
9db80 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Text ){.
9db90 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 2c memcpy(p->zText,
9dba0 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e 43 p->zBase, p->nC
9dbb0 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 har+1);. }e
9dbc0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e lse{. p->
9dbd0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
9dbe0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
9dbf0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e }. return p->
9dc00 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 zText;.}../*.**
9dc10 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75 Reset an StrAccu
9dc20 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61 m string. Recla
9dc30 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20 im all malloced
9dc40 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 memory..*/.SQLIT
9dc50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
9dc60 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
9dc70 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 29 set(StrAccum *p)
9dc80 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 {. if( p->zText
9dc90 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20 !=p->zBase ){.
9dca0 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
9dcb0 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78 74 29 p->db, p->zText)
9dcc0 3b 0a 20 20 7d 0a 20 20 70 2d 3e 7a 54 65 78 74 ;. }. p->zText
9dcd0 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 = 0;.}../*.** I
9dce0 6e 69 74 69 61 6c 69 7a 65 20 61 20 73 74 72 69 nitialize a stri
9dcf0 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 2a ng accumulator.*
9dd00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9dd10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
9dd20 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63 AccumInit(StrAcc
9dd30 75 6d 20 2a 70 2c 20 63 68 61 72 20 2a 7a 42 61 um *p, char *zBa
9dd40 73 65 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6d se, int n, int m
9dd50 78 29 7b 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d x){. p->zText =
9dd60 20 70 2d 3e 7a 42 61 73 65 20 3d 20 7a 42 61 73 p->zBase = zBas
9dd70 65 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b 0a e;. p->db = 0;.
9dd80 20 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b 0a p->nChar = 0;.
9dd90 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b p->nAlloc = n;
9dda0 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 . p->mxAlloc =
9ddb0 6d 78 3b 0a 20 20 70 2d 3e 75 73 65 4d 61 6c 6c mx;. p->useMall
9ddc0 6f 63 20 3d 20 31 3b 0a 20 20 70 2d 3e 74 6f 6f oc = 1;. p->too
9ddd0 42 69 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 6d 61 Big = 0;. p->ma
9dde0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a llocFailed = 0;.
9ddf0 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
9de00 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
9de10 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d ned from sqliteM
9de20 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 alloc(). Use th
9de30 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d e internal.** %-
9de40 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e conversion exten
9de50 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 sions..*/.SQLITE
9de60 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
9de70 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 qlite3VMPrintf(s
9de80 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
9de90 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
9dea0 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 va_list ap){.
9deb0 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 char *z;. char
9dec0 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 zBase[SQLITE_PRI
9ded0 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 NT_BUF_SIZE];.
9dee0 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 StrAccum acc;.
9def0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 sqlite3StrAccumI
9df00 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c nit(&acc, zBase,
9df10 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a sizeof(zBase),.
9df20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9df30 20 20 20 20 20 20 64 62 20 3f 20 64 62 2d 3e 61 db ? db->a
9df40 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
9df50 49 54 5f 4c 45 4e 47 54 48 5d 20 3a 20 53 51 4c IT_LENGTH] : SQL
9df60 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b ITE_MAX_LENGTH);
9df70 0a 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b 0a . acc.db = db;.
9df80 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 sqlite3VXPrint
9df90 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d f(&acc, 1, zForm
9dfa0 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 at, ap);. z = s
9dfb0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
9dfc0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 66 nish(&acc);. if
9dfd0 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c ( acc.mallocFail
9dfe0 65 64 20 26 26 20 64 62 20 29 7b 0a 20 20 20 20 ed && db ){.
9dff0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
9e000 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
9e010 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 rn z;.}../*.** P
9e020 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
9e030 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
9e040 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 qliteMalloc().
9e050 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c Use the internal
9e060 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e .** %-conversion
9e070 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a extensions..*/.
9e080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
9e090 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 har *sqlite3MPri
9e0a0 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ntf(sqlite3 *db,
9e0b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
9e0c0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
9e0d0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
9e0e0 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 *z;. va_start(
9e0f0 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
9e100 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 z = sqlite3VMPri
9e110 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c ntf(db, zFormat,
9e120 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
9e130 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a p);. return z;.
9e140 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 }../*.** Like sq
9e150 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 lite3MPrintf(),
9e160 62 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 but call sqlite3
9e170 44 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 DbFree() on zStr
9e180 20 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e after formattin
9e190 67 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 g.** the string
9e1a0 61 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 and before retur
9e1b0 6e 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 nning. This rou
9e1c0 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 tine is intended
9e1d0 20 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 to be used.** t
9e1e0 6f 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 o modify an exis
9e1f0 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f ting string. Fo
9e200 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a r example:.**.**
9e210 20 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 x = sqlit
9e220 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c e3MPrintf(db, x,
9e230 20 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 "prefix %s suff
9e240 69 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 ix", x);.**.*/.S
9e250 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
9e260 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 ar *sqlite3MAppe
9e270 6e 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ndf(sqlite3 *db,
9e280 20 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e char *zStr, con
9e290 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
9e2a0 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 , ...){. va_lis
9e2b0 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b t ap;. char *z;
9e2c0 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 . va_start(ap,
9e2d0 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 zFormat);. z =
9e2e0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
9e2f0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 db, zFormat, ap)
9e300 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
9e310 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
9e320 64 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 db, zStr);. ret
9e330 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn z;.}../*.**
9e340 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 Print into memor
9e350 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 y obtained from
9e360 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
9e370 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 . Omit the inte
9e380 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 rnal.** %-conver
9e390 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e sion extensions.
9e3a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
9e3b0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 har *sqlite3_vmp
9e3c0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 rintf(const char
9e3d0 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 *zFormat, va_li
9e3e0 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a st ap){. char *
9e3f0 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b z;. char zBase[
9e400 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
9e410 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 _SIZE];. StrAcc
9e420 75 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 um acc;.#ifndef
9e430 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
9e440 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 INIT. if( sqlit
9e450 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
9e460 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 ) return 0;.#end
9e470 69 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 if. sqlite3StrA
9e480 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a ccumInit(&acc, z
9e490 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 Base, sizeof(zBa
9e4a0 73 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f se), SQLITE_MAX_
9e4b0 4c 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 LENGTH);. sqlit
9e4c0 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c e3VXPrintf(&acc,
9e4d0 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 0, zFormat, ap)
9e4e0 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 ;. z = sqlite3S
9e4f0 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 trAccumFinish(&a
9e500 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b cc);. return z;
9e510 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 .}../*.** Print
9e520 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
9e530 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
9e540 33 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 3_malloc()(). O
9e550 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c mit the internal
9e560 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e .** %-conversion
9e570 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a extensions..*/.
9e580 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
9e590 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 *sqlite3_mprintf
9e5a0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f (const char *zFo
9e5b0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
9e5c0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
9e5d0 20 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c *z;.#ifndef SQL
9e5e0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 ITE_OMIT_AUTOINI
9e5f0 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f T. if( sqlite3_
9e600 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 initialize() ) r
9e610 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a eturn 0;.#endif.
9e620 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
9e630 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 Format);. z = s
9e640 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 qlite3_vmprintf(
9e650 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
9e660 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 va_end(ap);. re
9e670 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn z;.}../*.**
9e680 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
9e690 66 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 f() works like s
9e6a0 6e 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 nprintf() except
9e6b0 20 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 that it ignores
9e6c0 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 the.** current
9e6d0 6c 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e locale settings.
9e6e0 20 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 This is import
9e6f0 61 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 ant for SQLite b
9e700 65 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 ecause we.** are
9e710 20 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 not able to use
9e720 20 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 a "," as the de
9e730 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 cimal point in p
9e740 6c 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a lace of "." as.*
9e750 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73 * specified by s
9e760 6f 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a ome locales..*/.
9e770 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
9e780 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 *sqlite3_snprint
9e790 66 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a f(int n, char *z
9e7a0 42 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Buf, const char
9e7b0 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
9e7c0 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f char *z;. va_
9e7d0 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 list ap;. StrAc
9e7e0 63 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 cum acc;.. if(
9e7f0 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 n<=0 ){. retu
9e800 72 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 rn zBuf;. }. s
9e810 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e qlite3StrAccumIn
9e820 69 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e it(&acc, zBuf, n
9e830 2c 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d , 0);. acc.useM
9e840 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f alloc = 0;. va_
9e850 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 start(ap,zFormat
9e860 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 );. sqlite3VXPr
9e870 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 intf(&acc, 0, zF
9e880 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 ormat, ap);. va
9e890 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 _end(ap);. z =
9e8a0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
9e8b0 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 inish(&acc);. r
9e8c0 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 eturn z;.}..#if
9e8d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
9e8e0 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 EBUG)./*.** A ve
9e8f0 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 rsion of printf(
9e900 29 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e ) that understan
9e910 64 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 ds %lld. Used f
9e920 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a or debugging..**
9e930 20 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 The printf() bu
9e940 69 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 ilt into some ve
9e950 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 rsions of window
9e960 73 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 s does not under
9e970 73 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e stand %lld.** an
9e980 64 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 d segfaults if y
9e990 6f 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e ou give it a lon
9e9a0 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 g long int..*/.S
9e9b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9e9c0 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 id sqlite3DebugP
9e9d0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 rintf(const char
9e9e0 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
9e9f0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
9ea00 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 StrAccum acc;.
9ea10 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b char zBuf[500];
9ea20 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 . sqlite3StrAcc
9ea30 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 umInit(&acc, zBu
9ea40 66 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c f, sizeof(zBuf),
9ea50 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 0);. acc.useMa
9ea60 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 lloc = 0;. va_s
9ea70 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 tart(ap,zFormat)
9ea80 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 ;. sqlite3VXPri
9ea90 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f ntf(&acc, 0, zFo
9eaa0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
9eab0 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 end(ap);. sqlit
9eac0 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 e3StrAccumFinish
9ead0 28 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 (&acc);. fprint
9eae0 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a f(stdout,"%s", z
9eaf0 42 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 Buf);. fflush(s
9eb00 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 tdout);.}.#endif
9eb10 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
9eb20 2a 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e * End of printf.
9eb30 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
9eb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9eb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9eb60 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
9eb70 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e * Begin file ran
9eb80 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dom.c **********
9eb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9eba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9ebb0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
9ebc0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
9ebd0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
9ebe0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
9ebf0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
9ec00 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
9ec10 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
9ec20 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
9ec30 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
9ec40 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
9ec50 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
9ec60 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
9ec70 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
9ec80 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
9ec90 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
9eca0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
9ecb0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
9ecc0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
9ecd0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
9ece0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9ecf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9ed00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9ed10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9ed20 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
9ed30 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
9ed40 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 ode to implement
9ed50 20 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d a pseudo-random
9ed60 20 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 number.** gener
9ed70 61 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 ator (PRNG) for
9ed80 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 SQLite..**.** Ra
9ed90 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 ndom numbers are
9eda0 20 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 used by some of
9edb0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 the database ba
9edc0 63 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a ckends in order.
9edd0 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 ** to generate r
9ede0 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 andom integer ke
9edf0 79 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 ys for tables or
9ee00 20 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 random filename
9ee10 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 s..**.** $Id: ra
9ee20 6e 64 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30 ndom.c,v 1.29 20
9ee30 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 08/12/10 19:26:2
9ee40 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 4 drh Exp $.*/..
9ee50 0a 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 ./* All threads
9ee60 73 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 share a single r
9ee70 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e andom number gen
9ee80 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 erator..** This
9ee90 73 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65 structure is the
9eea0 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f current state o
9eeb0 66 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e f the generator.
9eec0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
9eed0 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c E_WSD struct sql
9eee0 69 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 ite3PrngType {.
9eef0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 unsigned char i
9ef00 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 sInit;
9ef10 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 /* True if initi
9ef20 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 alized */. unsi
9ef30 67 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 gned char i, j;
9ef40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
9ef50 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f ate variables */
9ef60 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
9ef70 20 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 s[256];
9ef80 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 /* State varia
9ef90 62 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 bles */.} sqlite
9efa0 33 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 3Prng;../*.** Ge
9efb0 74 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 t a single 8-bit
9efc0 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 random value fr
9efd0 6f 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e om the RC4 PRNG.
9efe0 20 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d The Mutex.** m
9eff0 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c ust be held whil
9f000 65 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 e executing this
9f010 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
9f020 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 Why not just use
9f030 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f a library rando
9f040 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 m generator like
9f050 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 lrand48() for t
9f060 68 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 his?.** Because
9f070 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 the OP_NewRowid
9f080 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 opcode in the VD
9f090 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 BE depends on ha
9f0a0 76 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 ving a very.** g
9f0b0 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 ood source of ra
9f0c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 ndom numbers. T
9f0d0 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 he lrand48() lib
9f0e0 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 rary function ma
9f0f0 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f y.** well be goo
9f100 64 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d d enough. But m
9f110 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 aybe not. Or ma
9f120 79 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 ybe lrand48() ha
9f130 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 s some.** subtle
9f140 20 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d problems on som
9f150 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 e systems that c
9f160 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c ould cause probl
9f170 65 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 ems. It is hard
9f180 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f .** to know. To
9f190 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 minimize the ri
9f1a0 73 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 sk of problems d
9f1b0 75 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 ue to bad lrand4
9f1c0 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 8().** implement
9f1d0 61 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 ations, SQLite u
9f1e0 73 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 ses this random
9f1f0 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
9f200 20 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 based.** on RC4
9f210 2c 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 , which we know
9f220 77 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e works very well.
9f230 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 .**.** (Later):
9f240 20 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 Actually, OP_Ne
9f250 77 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 wRowid does not
9f260 64 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 depend on a good
9f270 20 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 source of.** ra
9f280 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 ndomness any mor
9f290 65 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 e. But we will
9f2a0 6c 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 leave this code
9f2b0 69 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e in all the same.
9f2c0 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 .*/.static u8 ra
9f2d0 6e 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a ndomByte(void){.
9f2e0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
9f2f0 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 t;... /* The "w
9f300 73 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 sdPrng" macro wi
9f310 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 ll resolve to th
9f320 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 e pseudo-random
9f330 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
9f340 0a 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 . ** state vect
9f350 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 or. If writable
9f360 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 static data is
9f370 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 unsupported on t
9f380 68 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 he target,. **
9f390 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 we have to locat
9f3a0 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 e the state vect
9f3b0 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 or at run-time.
9f3c0 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d In the more com
9f3d0 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 mon. ** case wh
9f3e0 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 ere writable sta
9f3f0 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 tic data is supp
9f400 6f 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 orted, wsdPrng c
9f410 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c an refer directl
9f420 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 y. ** to the "s
9f430 71 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 qlite3Prng" stat
9f440 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 e vector declare
9f450 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 d above.. */.#i
9f460 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
9f470 5f 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 _WSD. struct sq
9f480 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 lite3PrngType *p
9f490 20 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 = &GLOBAL(struc
9f4a0 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 t sqlite3PrngTyp
9f4b0 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b e, sqlite3Prng);
9f4c0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e .# define wsdPrn
9f4d0 67 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 g p[0].#else.# d
9f4e0 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 efine wsdPrng sq
9f4f0 6c 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 lite3Prng.#endif
9f500 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 ... /* Initiali
9f510 7a 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 ze the state of
9f520 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 the random numbe
9f530 72 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 r generator once
9f540 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 ,. ** the first
9f550 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 time this routi
9f560 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 ne is called. T
9f570 68 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f he seed value do
9f580 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 es. ** not need
9f590 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f to contain a lo
9f5a0 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 t of randomness
9f5b0 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 since we are not
9f5c0 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 . ** trying to
9f5d0 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 do secure encryp
9f5e0 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 tion or anything
9f5f0 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 like that....
9f600 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 **. ** Nothing
9f610 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 in this file or
9f620 61 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e anywhere else in
9f630 20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 SQLite does any
9f640 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e kind of. ** en
9f650 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 cryption. The R
9f660 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 C4 algorithm is
9f670 62 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 being used as a
9f680 50 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e PRNG (pseudo-ran
9f690 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 dom. ** number
9f6a0 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 generator) not a
9f6b0 73 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 s an encryption
9f6c0 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 device.. */. i
9f6d0 66 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e f( !wsdPrng.isIn
9f6e0 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b it ){. int i;
9f6f0 0a 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d . char k[256]
9f700 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 ;. wsdPrng.j
9f710 3d 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 = 0;. wsdPrng
9f720 2e 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 .i = 0;. sqli
9f730 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 te3OsRandomness(
9f740 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 sqlite3_vfs_find
9f750 28 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 (0), 256, k);.
9f760 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 for(i=0; i<256
9f770 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 ; i++){. ws
9f780 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 dPrng.s[i] = (u8
9f790 29 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f )i;. }. fo
9f7a0 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b r(i=0; i<256; i+
9f7b0 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e +){. wsdPrn
9f7c0 67 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 g.j += wsdPrng.s
9f7d0 5b 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 [i] + k[i];.
9f7e0 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b t = wsdPrng.s[
9f7f0 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 wsdPrng.j];.
9f800 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 wsdPrng.s[wsdP
9f810 72 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 rng.j] = wsdPrng
9f820 2e 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 .s[i];. wsd
9f830 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 Prng.s[i] = t;.
9f840 20 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 }. wsdPrng
9f850 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d .isInit = 1;. }
9f860 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 .. /* Generate
9f870 61 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c and return singl
9f880 65 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 e random byte.
9f890 2a 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b */. wsdPrng.i++
9f8a0 3b 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e ;. t = wsdPrng.
9f8b0 73 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 s[wsdPrng.i];.
9f8c0 77 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a wsdPrng.j += t;.
9f8d0 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 wsdPrng.s[wsdP
9f8e0 72 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 rng.i] = wsdPrng
9f8f0 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 .s[wsdPrng.j];.
9f900 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
9f910 6e 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b ng.j] = t;. t +
9f920 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 = wsdPrng.s[wsdP
9f930 72 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e rng.i];. return
9f940 20 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d wsdPrng.s[t];.}
9f950 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e ../*.** Return N
9f960 20 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a random bytes..*
9f970 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
9f980 64 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d d sqlite3_random
9f990 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 ness(int N, void
9f9a0 20 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 *pBuf){. unsig
9f9b0 6e 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d ned char *zBuf =
9f9c0 20 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 pBuf;.#if SQLIT
9f9d0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 E_THREADSAFE. s
9f9e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
9f9f0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
9fa00 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
9fa10 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 UTEX_STATIC_PRNG
9fa20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 );.#endif. sqli
9fa30 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9fa40 6d 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 mutex);. while(
9fa50 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 N-- ){. *(zB
9fa60 75 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 uf++) = randomBy
9fa70 74 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 te();. }. sqli
9fa80 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9fa90 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 mutex);.}..#ifnd
9faa0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 ef SQLITE_OMIT_B
9fab0 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a UILTIN_TEST./*.*
9fac0 2a 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 * For testing pu
9fad0 72 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 rposes, we somet
9fae0 69 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 imes want to pre
9faf0 73 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 serve the state
9fb00 6f 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 of.** PRNG and r
9fb10 65 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 estore the PRNG
9fb20 74 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 to its saved sta
9fb30 74 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 te at a later ti
9fb40 6d 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 me, or.** to res
9fb50 65 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 et the PRNG to i
9fb60 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 ts initial state
9fb70 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
9fb80 73 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 s accomplish.**
9fb90 74 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a those tasks..**.
9fba0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 ** The sqlite3_t
9fbb0 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e est_control() in
9fbc0 74 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 terface calls th
9fbd0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a ese routines to.
9fbe0 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 ** control the P
9fbf0 52 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 RNG..*/.static S
9fc00 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
9fc10 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
9fc20 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e sqlite3SavedPrn
9fc30 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 g;.SQLITE_PRIVAT
9fc40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 E void sqlite3Pr
9fc50 6e 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 ngSaveState(void
9fc60 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 ){. memcpy(.
9fc70 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 &GLOBAL(struct
9fc80 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c sqlite3PrngType,
9fc90 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e sqlite3SavedPrn
9fca0 67 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 g),. &GLOBAL(
9fcb0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
9fcc0 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 ngType, sqlite3P
9fcd0 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 rng),. sizeof
9fce0 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 (sqlite3Prng).
9fcf0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
9fd00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
9fd10 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 PrngRestoreState
9fd20 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 (void){. memcpy
9fd30 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 (. &GLOBAL(st
9fd40 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
9fd50 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e Type, sqlite3Prn
9fd60 67 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 g),. &GLOBAL(
9fd70 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
9fd80 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 ngType, sqlite3S
9fd90 61 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 avedPrng),. s
9fda0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e izeof(sqlite3Prn
9fdb0 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 g). );.}.SQLITE
9fdc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
9fdd0 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 lite3PrngResetSt
9fde0 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f ate(void){. GLO
9fdf0 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 BAL(struct sqlit
9fe00 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 e3PrngType, sqli
9fe10 74 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 te3Prng).isInit
9fe20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a = 0;.}.#endif /*
9fe30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 SQLITE_OMIT_BUI
9fe40 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a LTIN_TEST */../*
9fe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
9fe60 64 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a d of random.c **
9fe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fe90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
9fea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
9feb0 67 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a gin file utf.c *
9fec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
9fef0 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 .** 2004 April 1
9ff00 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 3.**.** The auth
9ff10 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
9ff20 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
9ff30 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
9ff40 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
9ff50 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
9ff60 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
9ff70 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
9ff80 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
9ff90 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
9ffa0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
9ffb0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
9ffc0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
9ffd0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
9ffe0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
9fff0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
a0000 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
a0010 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
a0020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0060 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
a0070 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 ntains routines
a0080 75 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 used to translat
a0090 65 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c e between UTF-8,
a00a0 20 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 .** UTF-16, UTF
a00b0 2d 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 -16BE, and UTF-1
a00c0 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6LE..**.** $Id:
a00d0 75 74 66 2e 63 2c 76 20 31 2e 37 31 20 32 30 30 utf.c,v 1.71 200
a00e0 39 2f 30 33 2f 33 31 20 30 33 3a 34 31 3a 35 37 9/03/31 03:41:57
a00f0 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2a 0a shane Exp $.**.
a0100 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d ** Notes on UTF-
a0110 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 8:.**.** Byte-
a0120 30 20 20 20 20 42 79 74 65 2d 31 20 20 20 20 42 0 Byte-1 B
a0130 79 74 65 2d 32 20 20 20 20 42 79 74 65 2d 33 20 yte-2 Byte-3
a0140 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 Value.** 0xx
a0150 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 xxxxx
a0160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0170 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30 00000000 0
a0180 30 30 30 30 30 30 30 20 30 78 78 78 78 78 78 78 0000000 0xxxxxxx
a0190 0a 2a 2a 20 20 31 31 30 79 79 79 79 79 20 20 31 .** 110yyyyy 1
a01a0 30 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 0xxxxxx
a01b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 00
a01c0 30 30 30 30 30 30 20 30 30 30 30 30 79 79 79 20 000000 00000yyy
a01d0 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 yyxxxxxx.** 111
a01e0 30 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 0zzzz 10yyyyyy
a01f0 20 31 30 78 78 78 78 78 78 20 20 20 20 20 20 20 10xxxxxx
a0200 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 00000000 z
a0210 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 zzzyyyy yyxxxxxx
a0220 0a 2a 2a 20 20 31 31 31 31 30 75 75 75 20 20 31 .** 11110uuu 1
a0230 30 75 75 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 0uuzzzz 10yyyyy
a0240 79 20 20 31 30 78 78 78 78 78 78 20 20 20 30 30 y 10xxxxxx 00
a0250 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 0uuuuu zzzzyyyy
a0260 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a yyxxxxxx.**.**.*
a0270 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 * Notes on UTF-1
a0280 36 3a 20 20 28 77 69 74 68 20 77 77 77 77 2b 31 6: (with wwww+1
a0290 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 ==uuuuu).**.**
a02a0 20 20 20 20 57 6f 72 64 2d 30 20 20 20 20 20 20 Word-0
a02b0 20 20 20 20 20 20 20 20 20 57 6f 72 64 2d 31 20 Word-1
a02c0 20 20 20 20 20 20 20 20 20 56 61 6c 75 65 0a 2a Value.*
a02d0 2a 20 20 31 31 30 31 31 30 77 77 20 77 77 7a 7a * 110110ww wwzz
a02e0 7a 7a 79 79 20 20 20 31 31 30 31 31 31 79 79 20 zzyy 110111yy
a02f0 79 79 78 78 78 78 78 78 20 20 20 20 30 30 30 75 yyxxxxxx 000u
a0300 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 uuuu zzzzyyyy yy
a0310 78 78 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 xxxxxx.** zzzzy
a0320 79 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20 yyy yyxxxxxx
a0330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0340 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 00000000 zzz
a0350 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a zyyyy yyxxxxxx.*
a0360 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 *.**.** BOM or B
a0370 79 74 65 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a yte Order Mark:.
a0380 2a 2a 20 20 20 20 20 30 78 66 66 20 30 78 66 65 ** 0xff 0xfe
a0390 20 20 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e little-endian
a03a0 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a utf-16 follows.
a03b0 2a 2a 20 20 20 20 20 30 78 66 65 20 30 78 66 66 ** 0xfe 0xff
a03c0 20 20 20 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 big-endian ut
a03d0 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a f-16 follows.**.
a03e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
a03f0 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49 ** Include vdbeI
a0400 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 nt.h in the midd
a0410 6c 65 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a le of utf.c ****
a0420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0430 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
a0440 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 ** Begin file vd
a0450 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a beInt.h ********
a0460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0480 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 */./*.** 2003 Se
a0490 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 ptember 6.**.**
a04a0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
a04b0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
a04c0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
a04d0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
a04e0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
a04f0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
a0500 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
a0510 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
a0520 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
a0530 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
a0540 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
a0550 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
a0560 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
a0570 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
a0580 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
a0590 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
a05a0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
a05b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a05c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a05d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a05e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a05f0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
a0600 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 is the header f
a0610 69 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 ile for informat
a0620 69 6f 6e 20 74 68 61 74 20 69 73 20 70 72 69 76 ion that is priv
a0630 61 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 ate to the.** VD
a0640 42 45 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d BE. This inform
a0650 61 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c ation used to al
a0660 6c 20 62 65 20 61 74 20 74 68 65 20 74 6f 70 20 l be at the top
a0670 6f 66 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a of the single.**
a0680 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c source code fil
a0690 65 20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65 e "vdbe.c". Whe
a06a0 6e 20 74 68 61 74 20 66 69 6c 65 20 62 65 63 61 n that file beca
a06b0 6d 65 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 me too big (over
a06c0 0a 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c .** 6000 lines l
a06d0 6f 6e 67 29 20 69 74 20 77 61 73 20 73 70 6c 69 ong) it was spli
a06e0 74 20 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61 t up into severa
a06f0 6c 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 l smaller files
a0700 61 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 and.** this head
a0710 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 er information w
a0720 61 73 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e as factored out.
a0730 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
a0740 49 6e 74 2e 68 2c 76 20 31 2e 31 36 36 20 32 30 Int.h,v 1.166 20
a0750 30 39 2f 30 33 2f 31 38 20 31 30 3a 33 33 3a 30 09/03/18 10:33:0
a0760 32 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 2 danielk1977 Ex
a0770 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f p $.*/.#ifndef _
a0780 56 44 42 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 VDBEINT_H_.#defi
a0790 6e 65 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 0a ne _VDBEINT_H_..
a07a0 2f 2a 0a 2a 2a 20 69 6e 74 54 6f 4b 65 79 28 29 /*.** intToKey()
a07b0 20 61 6e 64 20 6b 65 79 54 6f 49 6e 74 28 29 20 and keyToInt()
a07c0 75 73 65 64 20 74 6f 20 74 72 61 6e 73 66 6f 72 used to transfor
a07d0 6d 20 74 68 65 20 72 6f 77 69 64 2e 20 20 42 75 m the rowid. Bu
a07e0 74 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 6c 61 t with.** the la
a07f0 74 65 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 test versions of
a0800 20 74 68 65 20 64 65 73 69 67 6e 20 74 68 65 79 the design they
a0810 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a are no-ops..*/.
a0820 23 64 65 66 69 6e 65 20 6b 65 79 54 6f 49 6e 74 #define keyToInt
a0830 28 58 29 20 20 20 28 58 29 0a 23 64 65 66 69 6e (X) (X).#defin
a0840 65 20 69 6e 74 54 6f 4b 65 79 28 58 29 20 20 20 e intToKey(X)
a0850 28 58 29 0a 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 (X).../*.** SQL
a0860 69 73 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e is translated in
a0870 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 to a sequence of
a0880 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 6f instructions to
a0890 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 be.** executed
a08a0 62 79 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 by a virtual mac
a08b0 68 69 6e 65 2e 20 20 45 61 63 68 20 69 6e 73 74 hine. Each inst
a08c0 72 75 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e ruction is an in
a08d0 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 stance.** of the
a08e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
a08f0 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ture..*/.typedef
a0900 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 4f struct VdbeOp O
a0910 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 p;../*.** Boolea
a0920 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 n values.*/.type
a0930 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 def unsigned cha
a0940 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 r Bool;../*.** A
a0950 20 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 cursor is a poi
a0960 6e 74 65 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 nter into a sing
a0970 6c 65 20 42 54 72 65 65 20 77 69 74 68 69 6e 20 le BTree within
a0980 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e a database file.
a0990 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 .** The cursor c
a09a0 61 6e 20 73 65 65 6b 20 74 6f 20 61 20 42 54 72 an seek to a BTr
a09b0 65 65 20 65 6e 74 72 79 20 77 69 74 68 20 61 20 ee entry with a
a09c0 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 particular key,
a09d0 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 or.** loop over
a09e0 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f 66 20 74 all entries of t
a09f0 68 65 20 42 74 72 65 65 2e 20 20 59 6f 75 20 63 he Btree. You c
a0a00 61 6e 20 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e an also insert n
a0a10 65 77 20 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 ew BTree.** entr
a0a20 69 65 73 20 6f 72 20 72 65 74 72 69 65 76 65 20 ies or retrieve
a0a30 74 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 the key or data
a0a40 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 from the entry t
a0a50 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a hat the cursor.*
a0a60 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 * is currently p
a0a70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a ointing to..** .
a0a80 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 ** Every cursor
a0a90 74 68 61 74 20 74 68 65 20 76 69 72 74 75 61 6c that the virtual
a0aa0 20 6d 61 63 68 69 6e 65 20 68 61 73 20 6f 70 65 machine has ope
a0ab0 6e 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 n is represented
a0ac0 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e by an.** instan
a0ad0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
a0ae0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
a0af0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65 *.** If the Vdbe
a0b00 43 75 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 Cursor.isTrigger
a0b10 52 6f 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 Row flag is set
a0b20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 it means that th
a0b30 69 73 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 is cursor is.**
a0b40 72 65 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 really a single
a0b50 72 6f 77 20 74 68 61 74 20 72 65 70 72 65 73 65 row that represe
a0b60 6e 74 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f nts the NEW or O
a0b70 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 LD pseudo-table
a0b80 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 of.** a row trig
a0b90 67 65 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 ger. The data f
a0ba0 6f 72 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 or the row is st
a0bb0 6f 72 65 64 20 69 6e 20 56 64 62 65 43 75 72 73 ored in VdbeCurs
a0bc0 6f 72 2e 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 or.pData and.**
a0bd0 74 68 65 20 72 6f 77 69 64 20 69 73 20 69 6e 20 the rowid is in
a0be0 56 64 62 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e VdbeCursor.iKey.
a0bf0 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 43 .*/.struct VdbeC
a0c00 75 72 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 ursor {. BtCurs
a0c10 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 or *pCursor;
a0c20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 73 74 /* The cursor st
a0c30 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20 62 ructure of the b
a0c40 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 ackend */. int
a0c50 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 iDb;
a0c60 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 /* Index of cu
a0c70 72 73 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e rsor database in
a0c80 20 64 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d db->aDb[] (or -
a0c90 31 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 1) */. i64 last
a0ca0 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a Rowid; /*
a0cb0 20 4c 61 73 74 20 72 6f 77 69 64 20 66 72 6f 6d Last rowid from
a0cc0 20 61 20 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 a Next or NextI
a0cd0 64 78 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a dx operation */.
a0ce0 20 20 42 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 Bool zeroed;
a0cf0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
a0d00 69 66 20 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e if zeroed out an
a0d10 64 20 72 65 61 64 79 20 66 6f 72 20 72 65 75 73 d ready for reus
a0d20 65 20 2a 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 e */. Bool rowi
a0d30 64 49 73 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 dIsValid; /*
a0d40 54 72 75 65 20 69 66 20 6c 61 73 74 52 6f 77 69 True if lastRowi
a0d50 64 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 d is valid */.
a0d60 42 6f 6f 6c 20 61 74 46 69 72 73 74 3b 20 20 20 Bool atFirst;
a0d70 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a0d80 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 pointing to fir
a0d90 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f st entry */. Bo
a0da0 6f 6c 20 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 ol useRandomRowi
a0db0 64 3b 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 d; /* Generate
a0dc0 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 new record numbe
a0dd0 72 73 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 rs semi-randomly
a0de0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 */. Bool nullR
a0df0 6f 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ow; /* T
a0e00 72 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 rue if pointing
a0e10 74 6f 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f to a row with no
a0e20 20 64 61 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 data */. Bool
a0e30 70 73 65 75 64 6f 54 61 62 6c 65 3b 20 20 20 20 pseudoTable;
a0e40 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 4e 45 /* This is a NE
a0e50 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d W or OLD pseudo-
a0e60 74 61 62 6c 65 73 20 6f 66 20 61 20 74 72 69 67 tables of a trig
a0e70 67 65 72 20 2a 2f 0a 20 20 42 6f 6f 6c 20 65 70 ger */. Bool ep
a0e80 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 3b 0a hemPseudoTable;.
a0e90 20 20 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d Bool deferredM
a0ea0 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c oveto; /* A cal
a0eb0 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 l to sqlite3Btre
a0ec0 65 4d 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 65 eMoveto() is nee
a0ed0 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 ded */. Bool is
a0ee0 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f Table; /
a0ef0 2a 20 54 72 75 65 20 69 66 20 61 20 74 61 62 6c * True if a tabl
a0f00 65 20 72 65 71 75 69 72 69 6e 67 20 69 6e 74 65 e requiring inte
a0f10 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f ger keys */. Bo
a0f20 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20 20 20 20 ol isIndex;
a0f30 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
a0f40 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 n index containi
a0f50 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e ng keys only - n
a0f60 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 o data */. i64
a0f70 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 20 20 20 movetoTarget;
a0f80 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
a0f90 20 74 68 65 20 64 65 66 65 72 72 65 64 20 73 71 the deferred sq
a0fa0 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f lite3BtreeMoveto
a0fb0 28 29 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 () */. Btree *p
a0fc0 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a Bt; /*
a0fd0 20 53 65 70 61 72 61 74 65 20 66 69 6c 65 20 68 Separate file h
a0fe0 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 olding temporary
a0ff0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
a1000 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 nData;
a1010 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
a1020 79 74 65 73 20 69 6e 20 70 44 61 74 61 20 2a 2f ytes in pData */
a1030 0a 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 . char *pData;
a1040 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
a1050 20 66 6f 72 20 61 20 4e 45 57 20 6f 72 20 4f 4c for a NEW or OL
a1060 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a D pseudo-table *
a1070 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 /. i64 iKey;
a1080 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 /* Key
a1090 20 66 6f 72 20 74 68 65 20 4e 45 57 20 6f 72 20 for the NEW or
a10a0 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 OLD pseudo-table
a10b0 20 72 6f 77 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 row */. KeyInf
a10c0 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 o *pKeyInfo;
a10d0 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 20 69 6e /* Info about in
a10e0 64 65 78 20 6b 65 79 73 20 6e 65 65 64 65 64 20 dex keys needed
a10f0 62 79 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 by index cursors
a1100 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 */. int nField
a1110 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
a1120 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 umber of fields
a1130 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f in the header */
a1140 0a 20 20 69 36 34 20 73 65 71 43 6f 75 6e 74 3b . i64 seqCount;
a1150 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 71 75 /* Sequ
a1160 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a ence counter */.
a1170 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 sqlite3_vtab_c
a1180 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 ursor *pVtabCurs
a1190 6f 72 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73 or; /* The curs
a11a0 6f 72 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c or for a virtual
a11b0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 table */. cons
a11c0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
a11d0 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f *pModule; /
a11e0 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 * Module for cur
a11f0 73 6f 72 20 70 56 74 61 62 43 75 72 73 6f 72 20 sor pVtabCursor
a1200 2a 2f 0a 0a 20 20 2f 2a 20 43 61 63 68 65 64 20 */.. /* Cached
a1210 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
a1220 74 20 74 68 65 20 68 65 61 64 65 72 20 66 6f 72 t the header for
a1230 20 74 68 65 20 64 61 74 61 20 72 65 63 6f 72 64 the data record
a1240 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 63 that the. ** c
a1250 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 ursor is current
a1260 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 ly pointing to.
a1270 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 63 Only valid if c
a1280 61 63 68 65 56 61 6c 69 64 20 69 73 20 74 72 75 acheValid is tru
a1290 65 2e 0a 20 20 2a 2a 20 61 52 6f 77 20 6d 69 67 e.. ** aRow mig
a12a0 68 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 70 68 ht point to (eph
a12b0 65 6d 65 72 61 6c 29 20 64 61 74 61 20 66 6f 72 emeral) data for
a12c0 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
a12d0 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 0a 20 20 , or it might.
a12e0 2a 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f ** be NULL.. */
a12f0 0a 20 20 69 6e 74 20 63 61 63 68 65 53 74 61 74 . int cacheStat
a1300 75 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68 us; /* Cach
a1310 65 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68 e is valid if th
a1320 69 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e is matches Vdbe.
a1330 63 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 69 6e cacheCtr */. in
a1340 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 t payloadSize;
a1350 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
a1360 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
a1370 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
a1380 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 u32 *aType;
a1390 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76 61 /* Type va
a13a0 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e 74 lues for all ent
a13b0 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f ries in the reco
a13c0 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 rd */. u32 *aOf
a13d0 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a fset; /*
a13e0 20 43 61 63 68 65 64 20 6f 66 66 73 65 74 73 20 Cached offsets
a13f0 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 to the start of
a1400 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 61 74 each columns dat
a1410 61 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f 77 3b a */. u8 *aRow;
a1420 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a1430 44 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 Data for the cur
a1440 72 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 6c 6c rent row, if all
a1450 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a on one page */.
a1460 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 };.typedef struc
a1470 74 20 56 64 62 65 43 75 72 73 6f 72 20 56 64 62 t VdbeCursor Vdb
a1480 65 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 eCursor;../*.**
a1490 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64 62 65 A value for Vdbe
a14a0 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61 6c 69 Cursor.cacheVali
a14b0 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 d that means the
a14c0 20 63 61 63 68 65 20 69 73 20 61 6c 77 61 79 73 cache is always
a14d0 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 invalid..*/.#de
a14e0 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41 4c 45 fine CACHE_STALE
a14f0 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 0../*.** Intern
a1500 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 20 6d ally, the vdbe m
a1510 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 72 6c anipulates nearl
a1520 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73 y all SQL values
a1530 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 as Mem.** struc
a1540 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65 6d 20 tures. Each Mem
a1550 73 74 72 75 63 74 20 6d 61 79 20 63 61 63 68 65 struct may cache
a1560 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 65 73 multiple repres
a1570 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 69 6e entations (strin
a1580 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65 74 g,.** integer et
a1590 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d 65 20 c.) of the same
a15a0 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 65 20 value. A value
a15b0 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 4d (and therefore M
a15c0 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a 2a 2a em structure).**
a15d0 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 has the followi
a15e0 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a ng properties:.*
a15f0 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 65 20 *.** Each value
a1600 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74 20 74 has a manifest t
a1610 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 65 73 ype. The manifes
a1620 74 20 74 79 70 65 20 6f 66 20 74 68 65 20 76 61 t type of the va
a1630 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e lue stored.** in
a1640 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20 69 73 a Mem struct is
a1650 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
a1660 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d MemType(Mem*) m
a1670 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65 20 69 acro. The type i
a1680 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 s.** one of SQLI
a1690 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f TE_NULL, SQLITE_
a16a0 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 45 5f INTEGER, SQLITE_
a16b0 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 REAL, SQLITE_TEX
a16c0 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 T or.** SQLITE_B
a16d0 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d LOB..*/.struct M
a16e0 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 em {. union {.
a16f0 20 20 20 69 36 34 20 69 3b 20 20 20 20 20 20 20 i64 i;
a1700 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
a1710 72 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 r value. */.
a1720 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 int nZero;
a1730 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
a1740 20 62 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 bit MEM_Zero is
a1750 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f set in flags */
a1760 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 . FuncDef *pD
a1770 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 64 ef; /* Used
a1780 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 only when flags
a1790 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 ==MEM_Agg */.
a17a0 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74 RowSet *pRowSet
a17b0 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c ; /* Used onl
a17c0 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 y when flags==ME
a17d0 4d 5f 52 6f 77 53 65 74 20 2a 2f 0a 20 20 7d 20 M_RowSet */. }
a17e0 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 u;. double r;
a17f0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c /* Real
a1800 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69 value */. sqli
a1810 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
a1820 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 /* The associate
a1830 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 d database conne
a1840 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 ction */. char
a1850 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f *z; /
a1860 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 * String or BLOB
a1870 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 value */. int
a1880 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
a1890 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 /* Number of cha
a18a0 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e racters in strin
a18b0 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69 g value, excludi
a18c0 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36 ng '\0' */. u16
a18d0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
a18e0 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 /* Some combina
a18f0 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c tion of MEM_Null
a1900 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 , MEM_Str, MEM_D
a1910 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 yn, etc. */. u8
a1920 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 type;
a1930 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49 /* One of SQLI
a1940 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f TE_NULL, SQLITE_
a1950 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54 TEXT, SQLITE_INT
a1960 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75 EGER, etc */. u
a1970 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 8 enc;
a1980 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 /* SQLITE_UTF
a1990 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 8, SQLITE_UTF16B
a19a0 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c E, SQLITE_UTF16L
a19b0 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 E */. void (*xD
a19c0 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a el)(void *); /*
a19d0 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 If not null, ca
a19e0 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ll this function
a19f0 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a to delete Mem.z
a1a00 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c */. char *zMal
a1a10 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e loc; /* Dyn
a1a20 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f amic buffer allo
a1a30 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 cated by sqlite3
a1a40 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a _malloc() */.};.
a1a50 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 ./* One or more
a1a60 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
a1a70 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 74 flags are set t
a1a80 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 76 o indicate the v
a1a90 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 alidOK.** repres
a1aa0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
a1ab0 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
a1ac0 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e the Mem struct.
a1ad0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 .**.** If the ME
a1ae0 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73 M_Null flag is s
a1af0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c et, then the val
a1b00 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c ue is an SQL NUL
a1b10 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f L value..** No o
a1b20 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62 ther flags may b
a1b30 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61 e set in this ca
a1b40 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
a1b50 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73 MEM_Str flag is
a1b60 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 set then Mem.z
a1b70 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 69 points at a stri
a1b80 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ng representatio
a1b90 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68 n..** Usually th
a1ba0 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e is is encoded in
a1bb0 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64 the same unicod
a1bc0 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 e encoding as th
a1bd0 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 e main.** databa
a1be0 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f se (see below fo
a1bf0 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49 r exceptions). I
a1c00 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 f the MEM_Term f
a1c10 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 lag is also.** s
a1c20 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 et, then the str
a1c30 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 ing is nul termi
a1c40 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49 nated. The MEM_I
a1c50 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 nt and MEM_Real
a1c60 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f .** flags may co
a1c70 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 4d exist with the M
a1c80 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a EM_Str flag..**.
a1c90 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74 ** Multiple of t
a1ca0 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 hese values can
a1cb0 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c appear in Mem.fl
a1cc0 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f ags. But only o
a1cd0 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 ne.** at a time
a1ce0 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65 can appear in Me
a1cf0 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 m.type..*/.#defi
a1d00 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 ne MEM_Null
a1d10 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c 0x0001 /* Val
a1d20 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 ue is NULL */.#d
a1d30 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20 efine MEM_Str
a1d40 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20 0x0002 /*
a1d50 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e Value is a strin
a1d60 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d g */.#define MEM
a1d70 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30 _Int 0x000
a1d80 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 4 /* Value is
a1d90 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 an integer */.#d
a1da0 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20 efine MEM_Real
a1db0 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20 0x0008 /*
a1dc0 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20 Value is a real
a1dd0 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e number */.#defin
a1de0 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20 e MEM_Blob
a1df0 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75 0x0010 /* Valu
a1e00 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 e is a BLOB */.#
a1e10 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 define MEM_RowSe
a1e20 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a t 0x0020 /*
a1e30 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53 Value is a RowS
a1e40 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65 et object */.#de
a1e50 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61 73 fine MEM_TypeMas
a1e60 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a 20 4d k 0x00ff /* M
a1e70 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74 73 ask of type bits
a1e80 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 65 72 */../* Whenever
a1e90 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 61 20 Mem contains a
a1ea0 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f 72 20 valid string or
a1eb0 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 61 74 blob representat
a1ec0 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 ion, one of.** t
a1ed0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 he following fla
a1ee0 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 20 74 gs must be set t
a1ef0 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
a1f00 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e memory managemen
a1f10 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f 72 20 t.** policy for
a1f20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 4d 5f Mem.z. The MEM_
a1f30 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c 73 20 Term flag tells
a1f40 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f us whether or no
a1f50 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 t the.** string
a1f60 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 30 30 is \000 or \u000
a1f70 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 0 terminated.*/.
a1f80 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 72 6d #define MEM_Term
a1f90 20 20 20 20 20 20 30 78 30 32 30 30 20 20 20 2f 0x0200 /
a1fa0 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 73 20 * String rep is
a1fb0 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 2a nul terminated *
a1fc0 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44 79 /.#define MEM_Dy
a1fd0 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 20 20 n 0x0400
a1fe0 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c /* Need to call
a1ff0 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 6f 6e sqliteFree() on
a2000 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e Mem.z */.#defin
a2010 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20 20 e MEM_Static
a2020 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 0x0800 /* Mem.
a2030 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 z points to a st
a2040 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 atic string */.#
a2050 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65 6d define MEM_Ephem
a2060 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f 2a 0x1000 /*
a2070 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f Mem.z points to
a2080 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 an ephemeral st
a2090 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ring */.#define
a20a0 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30 78 MEM_Agg 0x
a20b0 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 2000 /* Mem.z
a20c0 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67 67 points to an agg
a20d0 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 function contex
a20e0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d t */.#define MEM
a20f0 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 30 30 _Zero 0x400
a2100 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 0 /* Mem.i con
a2110 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 20 30 tains count of 0
a2120 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 62 6c s appended to bl
a2130 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 ob */..#ifdef SQ
a2140 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
a2150 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d 5f OB. #undef MEM_
a2160 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20 4d Zero. #define M
a2170 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a 23 EM_Zero 0x0000.#
a2180 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c endif.../*.** Cl
a2190 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e 67 ear any existing
a21a0 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f 6d type flags from
a21b0 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c 61 a Mem and repla
a21c0 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a 2a ce them with f.*
a21d0 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65 74 /.#define MemSet
a21e0 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20 5c TypeFlag(p, f) \
a21f0 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 20 . ((p)->flags
a2200 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e 28 = ((p)->flags&~(
a2210 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45 4d MEM_TypeMask|MEM
a2220 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20 _Zero))|f).../*
a2230 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a 75 A VdbeFunc is ju
a2240 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64 65 st a FuncDef (de
a2250 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 fined in sqliteI
a2260 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74 61 nt.h) that conta
a2270 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 ins.** additiona
a2280 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 l information ab
a2290 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69 6e out auxiliary in
a22a0 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64 20 formation bound
a22b0 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 to arguments.**
a22c0 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e of the function.
a22d0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 This is used t
a22e0 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
a22f0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
a2300 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71 6c ata().** and sql
a2310 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
a2320 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22 61 () APIs. The "a
a2330 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65 20 uxdata" is some
a2340 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a 2a auxiliary data.*
a2350 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61 73 * that can be as
a2360 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
a2370 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e constant argumen
a2380 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e t to a function.
a2390 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 This.** allows
a23a0 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68 20 functions such
a23b0 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20 63 as "regexp" to c
a23c0 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f 6e ompile their con
a23d0 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a 2a stant regular.**
a23e0 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67 75 expression argu
a23f0 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 ment once and re
a2400 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 used the compile
a2410 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74 69 d code for multi
a2420 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f ple.** invocatio
a2430 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 ns..*/.struct Vd
a2440 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63 44 beFunc {. FuncD
a2450 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 ef *pFunc;
a2460 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
a2470 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 definition of th
a2480 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 e function */.
a2490 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20 20 int nAux;
a24a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a24b0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
a24c0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 es allocated for
a24d0 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 apAux[] */. st
a24e0 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a 20 ruct AuxData {.
a24f0 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 void *pAux;
a2500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2510 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f 72 /* Aux data for
a2520 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d 65 the i-th argume
a2530 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28 nt */. void (
a2540 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20 2a *xDelete)(void *
a2550 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 ); /* Destr
a2560 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61 75 uctor for the au
a2570 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61 70 x data */. } ap
a2580 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 Aux[1];
a2590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
a25a0 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20 66 slot for each f
a25b0 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 unction argument
a25c0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
a25d0 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67 75 e "context" argu
a25e0 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74 61 ment for a insta
a25f0 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e 2e llable function.
a2600 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 A pointer to a
a2610 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 n.** instance of
a2620 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
a2630 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 is the first arg
a2640 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f 75 ument to the rou
a2650 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69 6d tines used.** im
a2660 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 plement the SQL
a2670 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a functions..**.**
a2680 20 54 68 65 72 65 20 69 73 20 61 20 74 79 70 65 There is a type
a2690 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 74 72 def for this str
a26a0 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74 65 ucture in sqlite
a26b0 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75 74 .h. So all rout
a26c0 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 ines,.** even th
a26d0 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 e public interfa
a26e0 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63 61 ce to SQLite, ca
a26f0 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72 20 n use a pointer
a2700 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
a2710 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20 66 e..** But this f
a2720 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 ile is the only
a2730 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 65 20 place where the
a2740 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c 73 internal details
a2750 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 75 of this.** stru
a2760 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e 2e cture are known.
a2770 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 .**.** This stru
a2780 63 74 75 72 65 20 69 73 20 64 65 66 69 6e 65 64 cture is defined
a2790 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65 49 inside of vdbeI
a27a0 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 74 20 nt.h because it
a27b0 75 73 65 73 20 73 75 62 73 74 72 75 63 74 75 72 uses substructur
a27c0 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69 63 es.** (Mem) whic
a27d0 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69 6e h are only defin
a27e0 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 ed there..*/.str
a27f0 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 uct sqlite3_cont
a2800 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 ext {. FuncDef
a2810 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f 2a *pFunc; /*
a2820 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e 63 Pointer to func
a2830 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tion information
a2840 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 . MUST BE FIRST
a2850 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20 2a */. VdbeFunc *
a2860 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20 41 pVdbeFunc; /* A
a2870 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69 66 uxilary data, if
a2880 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20 4d created. */. M
a2890 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20 20 em s;
a28a0 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 /* The retu
a28b0 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 rn value is stor
a28c0 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 6d ed here */. Mem
a28d0 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 *pMem;
a28e0 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c /* Memory cel
a28f0 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 l used to store
a2900 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78 aggregate contex
a2910 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 t */. int isErr
a2920 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 or; /*
a2930 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 Error code retur
a2940 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e 63 74 ned by the funct
a2950 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 ion. */. CollSe
a2960 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 q *pColl;
a2970 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 /* Collating seq
a2980 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a uence */.};../*.
a2990 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 74 75 ** A Set structu
a29a0 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 71 re is used for q
a29b0 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f 20 uick testing to
a29c0 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a 2a see if a value.*
a29d0 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 73 * is part of a s
a29e0 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73 20 mall set. Sets
a29f0 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c are used to impl
a2a00 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65 0a ement code like.
a2a10 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20 20 ** this:.**
a2a20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28 27 x.y IN ('
a2a30 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27 29 hi','hoo','hum')
a2a40 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
a2a50 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72 75 ct Set Set;.stru
a2a60 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68 20 ct Set {. Hash
a2a70 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 hash;
a2a80 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a 75 /* A set is ju
a2a90 73 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 st a hash table
a2aa0 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 */. HashElem *p
a2ab0 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 rev; /* P
a2ac0 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73 73 reviously access
a2ad0 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20 2a ed hash elemen *
a2ae0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f /.};../*.** A Co
a2af0 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 ntext stores the
a2b00 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 last insert row
a2b10 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 id, the last sta
a2b20 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f tement change co
a2b30 75 6e 74 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 unt,.** and the
a2b40 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e current statemen
a2b50 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 28 t change count (
a2b60 69 2e 65 2e 20 63 68 61 6e 67 65 73 20 73 69 6e i.e. changes sin
a2b70 63 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e ce last statemen
a2b80 74 29 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 t)..** The curre
a2b90 6e 74 20 6b 65 79 6c 69 73 74 20 69 73 20 61 6c nt keylist is al
a2ba0 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 so stored in the
a2bb0 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 context..** Ele
a2bc0 6d 65 6e 74 73 20 6f 66 20 43 6f 6e 74 65 78 74 ments of Context
a2bd0 20 73 74 72 75 63 74 75 72 65 20 74 79 70 65 20 structure type
a2be0 6d 61 6b 65 20 75 70 20 74 68 65 20 43 6f 6e 74 make up the Cont
a2bf0 65 78 74 53 74 61 63 6b 2c 20 77 68 69 63 68 20 extStack, which
a2c00 69 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 62 79 is.** updated by
a2c10 20 74 68 65 20 43 6f 6e 74 65 78 74 50 75 73 68 the ContextPush
a2c20 20 61 6e 64 20 43 6f 6e 74 65 78 74 50 6f 70 20 and ContextPop
a2c30 6f 70 63 6f 64 65 73 20 28 75 73 65 64 20 62 79 opcodes (used by
a2c40 20 74 72 69 67 67 65 72 73 29 2e 0a 2a 2a 20 54 triggers)..** T
a2c50 68 65 20 63 6f 6e 74 65 78 74 20 69 73 20 70 75 he context is pu
a2c60 73 68 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 shed before exec
a2c70 75 74 69 6e 67 20 61 20 74 72 69 67 67 65 72 20 uting a trigger
a2c80 61 20 70 6f 70 70 65 64 20 77 68 65 6e 20 74 68 a popped when th
a2c90 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69 6e e.** trigger fin
a2ca0 69 73 68 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 ishes..*/.typede
a2cb0 66 20 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74 f struct Context
a2cc0 20 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 Context;.struct
a2cd0 20 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 36 34 Context {. i64
a2ce0 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 2f lastRowid; /
a2cf0 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f * Last insert ro
a2d00 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 wid (sqlite3.las
a2d10 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 tRowid) */. int
a2d20 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f nChange; /
a2d30 2a 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e * Statement chan
a2d40 67 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 ges (Vdbe.nChang
a2d50 65 73 29 20 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f es) */.};../
a2d60 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
a2d70 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 of the virtual
a2d80 6d 61 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73 machine. This s
a2d90 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
a2da0 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a s the complete.*
a2db0 2a 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 76 * state of the v
a2dc0 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a irtual machine..
a2dd0 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 **.** The "sqlit
a2de0 65 33 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75 e3_stmt" structu
a2df0 72 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 re pointer that
a2e00 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 is returned by s
a2e10 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 qlite3_compile()
a2e20 0a 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 .** is really a
a2e30 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e pointer to an in
a2e40 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
a2e50 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
a2e60 54 68 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d The Vdbe.inVtabM
a2e70 65 74 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69 ethod variable i
a2e80 73 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 s set to non-zer
a2e90 6f 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 o for the durati
a2ea0 6f 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 on of.** any vir
a2eb0 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f tual table metho
a2ec0 64 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 d invocations ma
a2ed0 64 65 20 62 79 20 74 68 65 20 76 64 62 65 20 70 de by the vdbe p
a2ee0 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a rogram. It is.**
a2ef0 20 73 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44 set to 2 for xD
a2f00 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 estroy method ca
a2f10 6c 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c lls and 1 for al
a2f20 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e l other methods.
a2f30 20 54 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c This.** variabl
a2f40 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 77 e is used for tw
a2f50 6f 20 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 o purposes: to a
a2f60 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65 llow xDestroy me
a2f70 74 68 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65 thods to execute
a2f80 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22 .** "DROP TABLE"
a2f90 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 statements and
a2fa0 74 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 to prevent some
a2fb0 6e 61 73 74 79 20 73 69 64 65 20 65 66 66 65 63 nasty side effec
a2fc0 74 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 ts of.** malloc
a2fd0 66 61 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c failure when SQL
a2fe0 69 74 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 ite is invoked r
a2ff0 65 63 75 72 73 69 76 65 6c 79 20 62 79 20 61 20 ecursively by a
a3000 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a virtual table .*
a3010 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f * method functio
a3020 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 n..*/.struct Vdb
a3030 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 e {. sqlite3 *d
a3040 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 b; /* The
a3050 20 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20 whole database
a3060 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 76 */. Vdbe *pPrev
a3070 2c 2a 70 4e 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b ,*pNext; /* Link
a3080 65 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 ed list of VDBEs
a3090 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 with the same V
a30a0 64 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 dbe.db */. int
a30b0 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 nOp;
a30c0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 /* Number of ins
a30d0 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 tructions in the
a30e0 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e program */. in
a30f0 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 t nOpAlloc;
a3100 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
a3110 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 lots allocated f
a3120 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 or aOp[] */. Op
a3130 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20 *aOp;
a3140 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f /* Space to ho
a3150 6c 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d ld the virtual m
a3160 61 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61 6d achine's program
a3170 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c */. int nLabel
a3180 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
a3190 62 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 ber of labels us
a31a0 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 ed */. int nLab
a31b0 65 6c 41 6c 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e elAlloc; /* N
a31c0 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 umber of slots a
a31d0 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c 61 62 llocated in aLab
a31e0 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 el[] */. int *a
a31f0 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a Label; /*
a3200 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
a3210 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d he labels */. M
a3220 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20 em **apArg;
a3230 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 /* Arguments
a3240 74 6f 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 to currently exe
a3250 63 75 74 69 6e 67 20 75 73 65 72 20 66 75 6e 63 cuting user func
a3260 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 tion */. Mem *a
a3270 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a ColName; /*
a3280 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f Column names to
a3290 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 return */. int
a32a0 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 nCursor;
a32b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c /* Number of sl
a32c0 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a ots in apCsr[] *
a32d0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
a32e0 2a 61 70 43 73 72 3b 20 2f 2a 20 4f 6e 65 20 65 *apCsr; /* One e
a32f0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61 lement of this a
a3300 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70 rray for each op
a3310 65 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69 en cursor */. i
a3320 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 nt nVar;
a3330 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
a3340 65 6e 74 72 69 65 73 20 69 6e 20 61 56 61 72 5b entries in aVar[
a3350 5d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 ] */. Mem *aVar
a3360 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 ; /* Va
a3370 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f 50 5f lues for the OP_
a3380 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e Variable opcode.
a3390 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56 */. char **azV
a33a0 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d ar; /* Nam
a33b0 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a e of variables *
a33c0 2f 0a 20 20 69 6e 74 20 6f 6b 56 61 72 3b 20 20 /. int okVar;
a33d0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
a33e0 69 66 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 if azVar[] has b
a33f0 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 een initialized
a3400 2a 2f 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 */. u32 magic;
a3410 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a3420 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 Magic number for
a3430 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 sanity checking
a3440 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 */. int nMem;
a3450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a3460 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 Number of memor
a3470 79 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 y locations curr
a3480 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ently allocated
a3490 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 */. Mem *aMem;
a34a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a34b0 54 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 The memory locat
a34c0 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 63 61 ions */. int ca
a34d0 63 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 cheCtr;
a34e0 20 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 /* VdbeCursor
a34f0 72 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 row cache genera
a3500 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a tion counter */.
a3510 20 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 61 int contextSta
a3520 63 6b 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e 64 ckTop; /* Ind
a3530 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e ex of top elemen
a3540 74 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 t in the context
a3550 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 stack */. int
a3560 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 contextStackDept
a3570 68 3b 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 h; /* The size
a3580 6f 66 20 74 68 65 20 22 63 6f 6e 74 65 78 74 22 of the "context"
a3590 20 73 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74 stack */. Cont
a35a0 65 78 74 20 2a 63 6f 6e 74 65 78 74 53 74 61 63 ext *contextStac
a35b0 6b 3b 20 20 2f 2a 20 53 74 61 63 6b 20 75 73 65 k; /* Stack use
a35c0 64 20 62 79 20 6f 70 63 6f 64 65 73 20 43 6f 6e d by opcodes Con
a35d0 74 65 78 74 50 75 73 68 20 26 20 43 6f 6e 74 65 textPush & Conte
a35e0 78 74 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70 63 xtPop*/. int pc
a35f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a3600 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d /* The program
a3610 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
a3620 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
a3630 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 /* Value t
a3640 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e o return */. in
a3650 74 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 t errorAction;
a3660 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72 /* Recover
a3670 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69 y action to do i
a3680 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72 n case of an err
a3690 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 or */. int nRes
a36a0 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 Column;
a36b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c /* Number of col
a36c0 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f 77 20 umns in one row
a36d0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
a36e0 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a t */. char **az
a36f0 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f ResColumn; /
a3700 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 6f 6e 65 * Values for one
a3710 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a row of result *
a3720 2f 20 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d / . char *zErrM
a3730 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 sg; /*
a3740 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 Error message wr
a3750 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 itten here */.
a3760 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65 74 3b Mem *pResultSet;
a3770 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
a3780 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f er to an array o
a3790 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 75 f results */. u
a37a0 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 8 explain;
a37b0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a37c0 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e f EXPLAIN presen
a37d0 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 t on SQL command
a37e0 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43 */. u8 changeC
a37f0 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a ntOn; /*
a3800 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20 True to update
a3810 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
a3820 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72 er */. u8 expir
a3830 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
a3840 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56 /* True if the V
a3850 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 M needs to be re
a3860 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38 compiled */. u8
a3870 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 minWriteFileFor
a3880 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d mat; /* Minimum
a3890 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 file format for
a38a0 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61 writable databa
a38b0 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38 se files */. u8
a38c0 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20 inVtabMethod;
a38d0 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d /* See com
a38e0 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20 ments above */.
a38f0 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 u8 usesStmtJour
a3900 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 nal; /* True
a3910 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 65 if uses a state
a3920 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a ment journal */.
a3930 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 u8 readOnly;
a3940 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
a3950 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 e for read-only
a3960 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 statements */.
a3970 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b 20 u8 isPrepareV2;
a3980 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
a3990 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 68 if prepared with
a39a0 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a 2f prepare_v2() */
a39b0 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 . int nChange;
a39c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
a39d0 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67 mber of db chang
a39e0 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61 es made since la
a39f0 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 36 st reset */. i6
a3a00 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 4 startTime;
a3a10 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 /* Time wh
a3a20 65 6e 20 71 75 65 72 79 20 73 74 61 72 74 65 64 en query started
a3a30 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 - used for prof
a3a40 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 iling */. int b
a3a50 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 treeMask;
a3a60 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 /* Bitmask of
a3a70 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 db->aDb[] entri
a3a80 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f es referenced */
a3a90 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 . BtreeMutexArr
a3aa0 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e ay aMutex; /* An
a3ab0 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20 array of Btree
a3ac0 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65 used here and ne
a3ad0 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 eding locks */.
a3ae0 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d int aCounter[2]
a3af0 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e ; /* Coun
a3b00 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c ters used by sql
a3b10 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 ite3_stmt_status
a3b20 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 () */. char *zS
a3b30 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ql; /*
a3b40 20 54 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c Text of the SQL
a3b50 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 statement that
a3b60 67 65 6e 65 72 61 74 65 64 20 74 68 69 73 20 2a generated this *
a3b70 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 65 65 3b /. void *pFree;
a3b80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
a3b90 72 65 65 20 74 68 69 73 20 77 68 65 6e 20 64 65 ree this when de
a3ba0 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62 65 20 leting the vdbe
a3bb0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
a3bc0 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 _DEBUG. FILE *t
a3bd0 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 2f race; /
a3be0 2a 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 75 * Write an execu
a3bf0 74 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 2c tion trace here,
a3c00 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a if not NULL */.
a3c10 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 53 74 #endif. int iSt
a3c20 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 atement;
a3c30 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 /* Statement nu
a3c40 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20 68 61 mber (or 0 if ha
a3c50 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d s not opened stm
a3c60 74 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c t) */.#ifdef SQL
a3c70 49 54 45 5f 53 53 45 0a 20 20 69 6e 74 20 66 65 ITE_SSE. int fe
a3c80 74 63 68 49 64 3b 20 20 20 20 20 20 20 20 20 20 tchId;
a3c90 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d /* Statement num
a3ca0 62 65 72 20 75 73 65 64 20 62 79 20 73 71 6c 69 ber used by sqli
a3cb0 74 65 33 5f 66 65 74 63 68 5f 73 74 61 74 65 6d te3_fetch_statem
a3cc0 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 72 75 ent */. int lru
a3cd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
a3ce0 2a 20 43 6f 75 6e 74 65 72 20 75 73 65 64 20 66 * Counter used f
a3cf0 6f 72 20 4c 52 55 20 63 61 63 68 65 20 72 65 70 or LRU cache rep
a3d00 6c 61 63 65 6d 65 6e 74 20 2a 2f 0a 23 65 6e 64 lacement */.#end
a3d10 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 if.#ifdef SQLITE
a3d20 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
a3d30 41 4e 41 47 45 4d 45 4e 54 0a 20 20 56 64 62 65 ANAGEMENT. Vdbe
a3d40 20 2a 70 4c 72 75 50 72 65 76 3b 0a 20 20 56 64 *pLruPrev;. Vd
a3d50 62 65 20 2a 70 4c 72 75 4e 65 78 74 3b 0a 23 65 be *pLruNext;.#e
a3d60 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 ndif.};../*.** T
a3d70 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
a3d80 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 allowed values
a3d90 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a for Vdbe.magic.*
a3da0 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d /.#define VDBE_M
a3db0 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78 AGIC_INIT 0x
a3dc0 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42 26bceaa5 /* B
a3dd0 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70 uilding a VDBE p
a3de0 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e rogram */.#defin
a3df0 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e e VDBE_MAGIC_RUN
a3e00 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33 0xbdf20da3
a3e10 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72 /* VDBE is r
a3e20 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20 eady to execute
a3e30 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f */.#define VDBE_
a3e40 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30 MAGIC_HALT 0
a3e50 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20 x519c2973 /*
a3e60 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74 VDBE has complet
a3e70 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a ed execution */.
a3e80 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 #define VDBE_MAG
a3e90 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36 IC_DEAD 0xb6
a3ea0 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65 06c3c8 /* The
a3eb0 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64 VDBE has been d
a3ec0 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f eallocated */../
a3ed0 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72 *.** Function pr
a3ee0 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49 ototypes.*/.SQLI
a3ef0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a3f00 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 sqlite3VdbeFreeC
a3f10 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 64 ursor(Vdbe *, Vd
a3f20 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 64 beCursor*);.void
a3f30 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 74 sqliteVdbePopSt
a3f40 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a ack(Vdbe*,int);.
a3f50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a3f60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 nt sqlite3VdbeCu
a3f70 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 rsorMoveto(VdbeC
a3f80 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66 ursor*);.#if def
a3f90 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
a3fa0 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 G) || defined(VD
a3fb0 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49 BE_PROFILE).SQLI
a3fc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a3fd0 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 sqlite3VdbePrint
a3fe0 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f Op(FILE*, int, O
a3ff0 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 p*);.#endif.SQLI
a4000 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a4010 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
a4020 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51 TypeLen(u32);.SQ
a4030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
a4040 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
a4050 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74 alType(Mem*, int
a4060 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
a4070 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
a4080 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 67 eSerialPut(unsig
a4090 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 ned char*, int,
a40a0 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 Mem*, int);.SQLI
a40b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a40c0 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
a40d0 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e Get(const unsign
a40e0 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d ed char*, u32, M
a40f0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
a4100 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a4110 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 3VdbeDeleteAuxDa
a4120 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e ta(VdbeFunc*, in
a4130 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32 t);..int sqlite2
a4140 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 28 BtreeKeyCompare(
a4150 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 BtCursor *, cons
a4160 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69 t void *, int, i
a4170 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 nt, int *);.SQLI
a4180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a4190 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 qlite3VdbeIdxKey
a41a0 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 73 Compare(VdbeCurs
a41b0 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f or*,UnpackedReco
a41c0 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 rd*,int*);.SQLIT
a41d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
a41e0 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 lite3VdbeIdxRowi
a41f0 64 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 d(BtCursor *, i6
a4200 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 4 *);.SQLITE_PRI
a4210 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
a4220 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 MemCompare(const
a4230 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d Mem*, const Mem
a4240 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 *, const CollSeq
a4250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
a4260 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
a4270 62 65 45 78 65 63 28 56 64 62 65 2a 29 3b 0a 53 beExec(Vdbe*);.S
a4280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
a4290 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 t sqlite3VdbeLis
a42a0 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 t(Vdbe*);.SQLITE
a42b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a42c0 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 ite3VdbeHalt(Vdb
a42d0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
a42e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
a42f0 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
a4300 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 g(Mem *, int);.S
a4310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
a4320 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
a4330 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 TooBig(Mem*);.SQ
a4340 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
a4350 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 sqlite3VdbeMemC
a4360 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 opy(Mem*, const
a4370 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
a4380 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a4390 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
a43a0 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 Copy(Mem*, const
a43b0 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Mem*, int);.SQL
a43c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a43d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
a43e0 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b ove(Mem*, Mem*);
a43f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a4400 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
a4410 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d emNulTerminate(M
a4420 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
a4430 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
a4440 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65 VdbeMemSetStr(Me
a4450 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c m*, const char*,
a4460 20 69 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a int, u8, void(*
a4470 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
a4480 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a4490 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
a44a0 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29 Int64(Mem*, i64)
a44b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a44c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
a44d0 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 eMemSetDouble(Me
a44e0 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c m*, double);.SQL
a44f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a4500 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
a4510 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 etNull(Mem*);.SQ
a4520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a4530 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
a4540 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a SetZeroBlob(Mem*
a4550 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
a4560 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a4570 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 e3VdbeMemSetRowS
a4580 65 74 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 et(Mem*);.SQLITE
a4590 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a45a0 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 ite3VdbeMemMakeW
a45b0 72 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a riteable(Mem*);.
a45c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a45d0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
a45e0 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c mStringify(Mem*,
a45f0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
a4600 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 IVATE i64 sqlite
a4610 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 3VdbeIntValue(Me
a4620 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
a4630 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
a4640 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
a4650 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 (Mem*);.SQLITE_P
a4660 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 RIVATE double sq
a4670 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
a4680 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ue(Mem*);.SQLITE
a4690 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a46a0 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 lite3VdbeInteger
a46b0 41 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a Affinity(Mem*);.
a46c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a46d0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
a46e0 6d 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a mRealify(Mem*);.
a46f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a4700 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
a4710 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b mNumerify(Mem*);
a4720 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a4730 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
a4740 65 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75 emFromBtree(BtCu
a4750 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e rsor*,int,int,in
a4760 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f t,Mem*);.SQLITE_
a4770 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a4780 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
a4790 73 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 se(Mem *p);.SQLI
a47a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a47b0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
a47c0 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 leaseExternal(Me
a47d0 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 m *p);.SQLITE_PR
a47e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
a47f0 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 3VdbeMemFinalize
a4800 28 4d 65 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29 (Mem*, FuncDef*)
a4810 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a4820 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
a4830 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 ite3OpcodeName(i
a4840 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
a4850 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
a4860 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 dbeOpcodeHasProp
a4870 65 72 74 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a erty(int, int);.
a4880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a4890 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
a48a0 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c mGrow(Mem *pMem,
a48b0 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 int n, int pres
a48c0 65 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 erve);.SQLITE_PR
a48d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
a48e0 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 3VdbeCloseStatem
a48f0 65 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 ent(Vdbe *, int)
a4900 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
a4910 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
a4920 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f NAGEMENT.SQLITE_
a4930 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a4940 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 te3VdbeReleaseBu
a4950 66 66 65 72 73 28 56 64 62 65 20 2a 70 29 3b 0a ffers(Vdbe *p);.
a4960 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
a4970 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
a4980 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 e3VdbeMemTransla
a4990 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 te(Mem*, u8);.#i
a49a0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
a49b0 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
a49c0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 void sqlite3V
a49d0 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 dbePrintSql(Vdbe
a49e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
a49f0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
a4a00 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 3VdbeMemPrettyPr
a4a10 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 int(Mem *pMem, c
a4a20 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 har *zBuf);.#end
a4a30 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
a4a40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
a4a50 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 eMemHandleBom(Me
a4a60 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 m *pMem);..#ifnd
a4a70 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
a4a80 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 NCRBLOB.SQLITE_P
a4a90 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
a4aa0 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e ite3VdbeMemExpan
a4ab0 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 dBlob(Mem *);.#e
a4ac0 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
a4ad0 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 lite3VdbeMemExpa
a4ae0 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 ndBlob(x) SQLITE
a4af0 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 _OK.#endif..#end
a4b00 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
a4b10 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a VDBEINT_H_) */..
a4b20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a4b30 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 End of vdbeInt.h
a4b40 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
a4b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a4b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a4b70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a4b80 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
a4b90 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
a4ba0 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a utf.c **********
a4bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a4bc0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a4bd0 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a AMALGAMATION./*.
a4be0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
a4bf0 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 constant value
a4c00 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 53 is used by the S
a4c10 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 QLITE_BIGENDIAN
a4c20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 and.** SQLITE_LI
a4c30 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f TTLEENDIAN macro
a4c40 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
a4c50 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 VATE const int s
a4c60 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 qlite3one = 1;.#
a4c70 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
a4c80 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a AMALGAMATION */.
a4c90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b ./*.** This look
a4ca0 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 up table is used
a4cb0 20 74 6f 20 68 65 6c 70 20 64 65 63 6f 64 65 20 to help decode
a4cc0 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f the first byte o
a4cd0 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74 f.** a multi-byt
a4ce0 65 20 55 54 46 38 20 63 68 61 72 61 63 74 65 72 e UTF8 character
a4cf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
a4d00 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
a4d10 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 sqlite3Utf8Trans
a4d20 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 1[] = {. 0x00,
a4d30 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 0x01, 0x02, 0x03
a4d40 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 , 0x04, 0x05, 0x
a4d50 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 06, 0x07,. 0x08
a4d60 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 , 0x09, 0x0a, 0x
a4d70 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 0b, 0x0c, 0x0d,
a4d80 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 0x0e, 0x0f,. 0x
a4d90 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32 2c 20 10, 0x11, 0x12,
a4da0 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78 31 35 0x13, 0x14, 0x15
a4db0 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 20 20 , 0x16, 0x17,.
a4dc0 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78 31 61 0x18, 0x19, 0x1a
a4dd0 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20 30 78 , 0x1b, 0x1c, 0x
a4de0 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66 2c 0a 1d, 0x1e, 0x1f,.
a4df0 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 0x00, 0x01, 0x
a4e00 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 02, 0x03, 0x04,
a4e10 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 0x05, 0x06, 0x07
a4e20 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 ,. 0x08, 0x09,
a4e30 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 0x0a, 0x0b, 0x0c
a4e40 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 , 0x0d, 0x0e, 0x
a4e50 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 0f,. 0x00, 0x01
a4e60 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 , 0x02, 0x03, 0x
a4e70 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 04, 0x05, 0x06,
a4e80 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 30 78 0x07,. 0x00, 0x
a4e90 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 01, 0x02, 0x03,
a4ea0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 0x00, 0x01, 0x00
a4eb0 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 , 0x00,.};...#de
a4ec0 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 38 28 fine WRITE_UTF8(
a4ed0 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 zOut, c) {
a4ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4ef0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 30 78 \. if( c<0x
a4f00 30 30 30 38 30 20 29 7b 20 20 20 20 20 20 20 20 00080 ){
a4f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4f20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a4f30 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
a4f40 29 28 63 26 30 78 46 46 29 3b 20 20 20 20 20 20 )(c&0xFF);
a4f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4f60 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
a4f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a4fa0 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 . else if( c<0x
a4fb0 30 30 38 30 30 20 29 7b 20 20 20 20 20 20 20 20 00800 ){
a4fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4fd0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
a4fe0 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 Out++ = 0xC0 + (
a4ff0 75 38 29 28 28 63 3e 3e 36 29 26 30 78 31 46 29 u8)((c>>6)&0x1F)
a5000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a5010 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
a5020 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 0x80 + (u8)(c &
a5030 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 0x3F);
a5040 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 \. }
a5050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5080 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 \. else if(
a5090 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20 20 20 c<0x10000 ){
a50a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a50b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a50c0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 *zOut++ = 0xE0
a50d0 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 26 + (u8)((c>>12)&
a50e0 30 78 30 46 29 3b 20 20 20 20 20 20 20 20 20 20 0x0F);
a50f0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
a5100 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 ++ = 0x80 + (u8)
a5110 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b ((c>>6) & 0x3F);
a5120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a5130 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
a5140 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 80 + (u8)(c & 0x
a5150 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3F);
a5160 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 \. }else
a5170 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
a5180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a51a0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
a51b0 30 78 46 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 0xF0 + (u8)((c>>
a51c0 31 38 29 20 26 20 30 78 30 37 29 3b 20 20 20 20 18) & 0x07);
a51d0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
a51e0 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
a51f0 28 75 38 29 28 28 63 3e 3e 31 32 29 20 26 20 30 (u8)((c>>12) & 0
a5200 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x3F);
a5210 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
a5220 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 = 0x80 + (u8)((c
a5230 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 >>6) & 0x3F);
a5240 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a5250 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 *zOut++ = 0x80
a5260 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29 + (u8)(c & 0x3F)
a5270 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a5280 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 \. }
a5290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a52a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a52b0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
a52c0 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f ..#define WRITE_
a52d0 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29 UTF16LE(zOut, c)
a52e0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
a52f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5300 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c \. if( c<
a5310 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 =0xFFFF ){
a5320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5340 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a5350 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
a5360 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 (c&0x00FF);
a5370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5390 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
a53a0 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78 = (u8)((c>>8)&0x
a53b0 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 00FF);
a53c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a53d0 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 \. }els
a53e0 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e{
a53f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a5420 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
a5430 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 8)(((c>>10)&0x00
a5440 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 3F) + (((c-0x100
a5450 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 00)>>10)&0x00C0)
a5460 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b ); \. *zOut+
a5470 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 20 + = (u8)(0x00D8
a5480 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e + (((c-0x10000)>
a5490 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 >18)&0x03));
a54a0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
a54b0 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 *zOut++ = (u8)(c
a54c0 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 &0x00FF);
a54d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a54e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a54f0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
a5500 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28 28 (u8)(0x00DC + ((
a5510 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 c>>8)&0x03));
a5520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5530 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
a5540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5570 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a \.}.
a5580 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 .#define WRITE_U
a5590 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20 TF16BE(zOut, c)
a55a0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
a55b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a55c0 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d \. if( c<=
a55d0 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 0xFFFF ){
a55e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a55f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5600 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a5610 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
a5620 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 (c>>8)&0x00FF);
a5630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5650 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
a5660 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b (u8)(c&0x00FF);
a5670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5690 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 \. }else
a56a0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
a56b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a56c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a56d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a56e0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
a56f0 29 28 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d )(0x00D8 + (((c-
a5700 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 0x10000)>>18)&0x
a5710 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 03));
a5720 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
a5730 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 = (u8)(((c>>10)
a5740 26 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d &0x003F) + (((c-
a5750 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 0x10000)>>10)&0x
a5760 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 00C0)); \. *
a5770 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 zOut++ = (u8)(0x
a5780 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 00DC + ((c>>8)&0
a5790 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 x03));
a57a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a57b0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
a57c0 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 u8)(c&0x00FF);
a57d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a57e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a57f0 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 \. }
a5800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5830 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a \.}..
a5840 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 #define READ_UTF
a5850 31 36 4c 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 16LE(zIn, c){
a5860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5880 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a \. c = (*
a5890 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 zIn++);
a58a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a58b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a58c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a58d0 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 c += ((*zIn++)
a58e0 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 <<8);
a58f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5910 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e \. if( c>
a5920 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 =0xD800 && c<0xE
a5930 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 000 ){
a5940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5950 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a5960 20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 int c2 = (*zI
a5970 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
a5980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a59a0 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d \. c2 +=
a59b0 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 ((*zIn++)<<8);
a59c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a59d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a59e0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a59f0 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 c = (c2&0x03F
a5a00 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 F) + ((c&0x003F)
a5a10 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 <<10) + (((c&0x0
a5a20 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 3C0)+0x0040)<<10
a5a30 29 3b 20 20 20 5c 0a 20 20 20 20 69 66 28 20 28 ); \. if( (
a5a40 63 20 26 20 30 78 46 46 46 46 30 30 30 30 29 3d c & 0xFFFF0000)=
a5a50 3d 30 20 29 20 63 20 3d 20 30 78 46 46 46 44 3b =0 ) c = 0xFFFD;
a5a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5a70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a5a80 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
a5a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5ac0 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e \.}..#defin
a5ad0 65 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a e READ_UTF16BE(z
a5ae0 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 In, c){
a5af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5b10 5c 0a 20 20 63 20 3d 20 28 28 2a 7a 49 6e 2b 2b \. c = ((*zIn++
a5b20 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 )<<8);
a5b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5b50 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d \. c +=
a5b60 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 (*zIn++);
a5b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5ba0 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30 \. if( c>=0xD80
a5bb0 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 29 7b 0 && c<0xE000 ){
a5bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5be0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 \. int
a5bf0 20 63 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c c2 = ((*zIn++)<
a5c00 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 <8);
a5c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a5c30 0a 20 20 20 20 63 32 20 2b 3d 20 28 2a 7a 49 6e . c2 += (*zIn
a5c40 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 ++);
a5c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5c70 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d \. c =
a5c80 20 28 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 (c2&0x03FF) + (
a5c90 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 (c&0x003F)<<10)
a5ca0 2b 20 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 + (((c&0x03C0)+0
a5cb0 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c x0040)<<10); \
a5cc0 0a 20 20 20 20 69 66 28 20 28 63 20 26 20 30 78 . if( (c & 0x
a5cd0 46 46 46 46 30 30 30 30 29 3d 3d 30 20 29 20 63 FFFF0000)==0 ) c
a5ce0 20 3d 20 30 78 46 46 46 44 3b 20 20 20 20 20 20 = 0xFFFD;
a5cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5d00 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 \. }
a5d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a5d50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c .}../*.** Transl
a5d60 61 74 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46 ate a single UTF
a5d70 2d 38 20 63 68 61 72 61 63 74 65 72 2e 20 20 52 -8 character. R
a5d80 65 74 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 eturn the unicod
a5d90 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 e value..**.** D
a5da0 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f uring translatio
a5db0 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 n, assume that t
a5dc0 68 65 20 62 79 74 65 20 74 68 61 74 20 7a 54 65 he byte that zTe
a5dd0 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 rm points.** is
a5de0 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 a 0x00..**.** Wr
a5df0 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ite a pointer to
a5e00 20 74 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64 the next unread
a5e10 20 62 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 byte back into
a5e20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e *pzNext..**.** N
a5e30 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 otes On Invalid
a5e40 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 UTF-8:.**.** *
a5e50 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 This routine ne
a5e60 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 ver allows a 7-b
a5e70 69 74 20 63 68 61 72 61 63 74 65 72 20 28 30 78 it character (0x
a5e80 30 30 20 74 68 72 6f 75 67 68 20 30 78 37 66 29 00 through 0x7f)
a5e90 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e to.** be en
a5ea0 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69 coded as a multi
a5eb0 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 2e -byte character.
a5ec0 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 Any multi-byte
a5ed0 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 0a character that.
a5ee0 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 74 73 20 ** attempts
a5ef0 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 to encode a valu
a5f00 65 20 62 65 74 77 65 65 6e 20 30 78 30 30 20 61 e between 0x00 a
a5f10 6e 64 20 30 78 37 66 20 69 73 20 72 65 6e 64 65 nd 0x7f is rende
a5f20 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a red as 0xfffd..*
a5f30 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f *.** * This ro
a5f40 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f utine never allo
a5f50 77 73 20 61 20 55 54 46 31 36 20 73 75 72 72 6f ws a UTF16 surro
a5f60 67 61 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65 gate value to be
a5f70 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 encoded..**
a5f80 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 If a multi-byte
a5f90 20 63 68 61 72 61 63 74 65 72 20 61 74 74 65 6d character attem
a5fa0 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 pts to encode a
a5fb0 76 61 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a value between.**
a5fc0 20 20 20 20 20 30 78 64 38 30 30 20 61 6e 64 20 0xd800 and
a5fd0 30 78 65 30 30 30 20 74 68 65 6e 20 69 74 20 69 0xe000 then it i
a5fe0 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 s rendered as 0x
a5ff0 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 fffd..**.** *
a6000 42 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e Bytes in the ran
a6010 67 65 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75 ge of 0x80 throu
a6020 67 68 20 30 78 62 66 20 77 68 69 63 68 20 6f 63 gh 0xbf which oc
a6030 63 75 72 20 61 73 20 74 68 65 20 66 69 72 73 74 cur as the first
a6040 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20 .** byte of
a6050 61 20 63 68 61 72 61 63 74 65 72 20 61 72 65 20 a character are
a6060 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 73 interpreted as s
a6070 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 ingle-byte chara
a6080 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 cters.** and
a6090 20 72 65 6e 64 65 72 65 64 20 61 73 20 74 68 65 rendered as the
a60a0 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f mselves even tho
a60b0 75 67 68 20 74 68 65 79 20 61 72 65 20 74 65 63 ugh they are tec
a60c0 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 hnically.**
a60d0 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 65 invalid characte
a60e0 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 rs..**.** * Th
a60f0 69 73 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70 is routine accep
a6100 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e ts an infinite n
a6110 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 umber of differe
a6120 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 nt UTF8 encoding
a6130 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 s.** for uni
a6140 63 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38 30 code values 0x80
a6150 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49 and greater. I
a6160 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 t do not change
a6170 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 over-length.**
a6180 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 encodings to
a6190 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73 0xfffd as some s
a61a0 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 ystems recommend
a61b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 ..*/.#define REA
a61c0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 D_UTF8(zIn, zTer
a61d0 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20 20 20 m, c)
a61e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a61f0 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 \. c = *(zIn++)
a6200 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a6210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6220 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a6230 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 20 if( c>=0xc0 ){
a6240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6260 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
a6270 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 c = sqlite3Utf8T
a6280 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20 rans1[c-0xc0];
a6290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a62a0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 \. whi
a62b0 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26 le( zIn!=zTerm &
a62c0 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d & (*zIn & 0xc0)=
a62d0 3d 30 78 38 30 20 29 7b 20 20 20 20 20 20 20 20 =0x80 ){
a62e0 20 20 20 20 5c 0a 20 20 20 20 20 20 63 20 3d 20 \. c =
a62f0 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 (c<<6) + (0x3f &
a6300 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20 *(zIn++));
a6310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6320 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20 \. }
a6330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a6360 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 20 20 if( c<0x80
a6370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6390 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a63a0 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 || (c&0xFFF
a63b0 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30 20 20 FF800)==0xD800
a63c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a63d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 \.
a63e0 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 || (c&0xFFFFFF
a63f0 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 FE)==0xFFFE ){
a6400 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 20 c = 0xFFFD; }
a6410 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54 \. }.SQLIT
a6420 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
a6430 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 0a 20 lite3Utf8Read(.
a6440 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
a6450 63 68 61 72 20 2a 7a 2c 20 20 20 20 20 20 20 20 char *z,
a6460 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f /* First byte o
a6470 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 f UTF-8 characte
a6480 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 r */. const uns
a6490 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 igned char *zTer
a64a0 6d 2c 20 20 20 20 20 2f 2a 20 50 72 65 74 65 6e m, /* Preten
a64b0 64 20 74 68 69 73 20 62 79 74 65 20 69 73 20 30 d this byte is 0
a64c0 78 30 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 x00 */. const u
a64d0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 nsigned char **p
a64e0 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 69 74 zNext /* Writ
a64f0 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 e first byte pas
a6500 74 20 55 54 46 2d 38 20 63 68 61 72 20 68 65 72 t UTF-8 char her
a6510 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b e */.){. int c;
a6520 0a 20 20 52 45 41 44 5f 55 54 46 38 28 7a 2c 20 . READ_UTF8(z,
a6530 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 2a 70 7a zTerm, c);. *pz
a6540 4e 65 78 74 20 3d 20 7a 3b 0a 20 20 72 65 74 75 Next = z;. retu
a6550 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a 2a rn c;.}...../*.*
a6560 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 4c 41 * If the TRANSLA
a6570 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f 20 69 TE_TRACE macro i
a6580 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 20 76 s defined, the v
a6590 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d 65 6d alue of each Mem
a65a0 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 20 6f is.** printed o
a65b0 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 65 20 n stderr on the
a65c0 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f 75 74 way into and out
a65d0 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 4d of sqlite3VdbeM
a65e0 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e 0a 2a emTranslate()..*
a65f0 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 54 52 / ./* #define TR
a6600 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 31 20 ANSLATE_TRACE 1
a6610 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
a6620 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a TE_OMIT_UTF16./*
a6630 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
a6640 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 65 20 transforms the
a6650 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 65 6e internal text en
a6660 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 70 coding used by p
a6670 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 72 65 Mem to.** desire
a6680 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e 20 65 dEnc. It is an e
a6690 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 72 69 rror if the stri
a66a0 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 6f 66 ng is already of
a66b0 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 the desired.**
a66c0 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 66 20 encoding, or if
a66d0 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 20 63 *pMem does not c
a66e0 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e 67 20 ontain a string
a66f0 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 value..*/.SQLITE
a6700 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a6710 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 ite3VdbeMemTrans
a6720 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 late(Mem *pMem,
a6730 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a u8 desiredEnc){.
a6740 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 int len;
a6750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a6760 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 Maximum length
a6770 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67 of output string
a6780 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 in bytes */. u
a6790 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f nsigned char *zO
a67a0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
a67b0 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 /* Output b
a67c0 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 uffer */. unsig
a67d0 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 ned char *zIn;
a67e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a67f0 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 61 74 /* Input iterat
a6800 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 or */. unsigned
a6810 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 char *zTerm;
a6820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a6830 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a 2f End of input */
a6840 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
a6850 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 *z;
a6860 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
a6870 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 ut iterator */.
a6880 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 3b unsigned int c;
a6890 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d .. assert( pMem
a68a0 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
a68b0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d e3_mutex_held(pM
a68c0 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 em->db->mutex) )
a68d0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d ;. assert( pMem
a68e0 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 ->flags&MEM_Str
a68f0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
a6900 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 m->enc!=desiredE
a6910 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 nc );. assert(
a6920 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b 0a pMem->enc!=0 );.
a6930 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
a6940 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 65 66 n>=0 );..#if def
a6950 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 5f 54 ined(TRANSLATE_T
a6960 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 64 RACE) && defined
a6970 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 20 (SQLITE_DEBUG).
a6980 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 {. char zBuf
a6990 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 [100];. sqlit
a69a0 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 e3VdbeMemPrettyP
a69b0 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29 rint(pMem, zBuf)
a69c0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 ;. fprintf(st
a69d0 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20 25 derr, "INPUT: %
a69e0 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d s\n", zBuf);. }
a69f0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 .#endif.. /* If
a6a00 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e the translation
a6a10 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d is between UTF-
a6a20 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 62 69 16 little and bi
a6a30 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e 20 0a g endian, then .
a6a40 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 73 ** all that is
a6a50 20 72 65 71 75 69 72 65 64 20 69 73 20 74 6f 20 required is to
a6a60 73 77 61 70 20 74 68 65 20 62 79 74 65 20 6f 72 swap the byte or
a6a70 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 20 69 der. This case i
a6a80 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a 20 64 s handled. ** d
a6a90 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 ifferently from
a6aa0 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 2a 2f the others.. */
a6ab0 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 . if( pMem->enc
a6ac0 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 !=SQLITE_UTF8 &&
a6ad0 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 51 4c desiredEnc!=SQL
a6ae0 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 ITE_UTF8 ){.
a6af0 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 6e 74 u8 temp;. int
a6b00 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 rc;. rc = sq
a6b10 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 lite3VdbeMemMake
a6b20 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b Writeable(pMem);
a6b30 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
a6b40 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
a6b50 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
a6b60 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
a6b70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
a6b80 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 NOMEM;. }.
a6b90 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d zIn = (u8*)pMem
a6ba0 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d 20 3d ->z;. zTerm =
a6bb0 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e 31 &zIn[pMem->n&~1
a6bc0 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49 ];. while( zI
a6bd0 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 n<zTerm ){.
a6be0 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20 20 temp = *zIn;.
a6bf0 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49 6e *zIn = *(zIn
a6c00 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b 2b +1);. zIn++
a6c10 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20 3d ;. *zIn++ =
a6c20 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 temp;. }.
a6c30 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 pMem->enc = des
a6c40 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f 74 iredEnc;. got
a6c50 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3b o translate_out;
a6c60 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 6c . }.. /* Set l
a6c70 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 en to the maximu
a6c80 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 m number of byte
a6c90 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 s required in th
a6ca0 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e e output buffer.
a6cb0 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72 65 */. if( desire
a6cc0 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
a6cd0 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 8 ){. /* When
a6ce0 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d converting from
a6cf0 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61 78 UTF-16, the max
a6d00 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73 75 imum growth resu
a6d10 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 lts from. **
a6d20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32 2d translating a 2-
a6d30 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74 byte character t
a6d40 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d 38 o a 4-byte UTF-8
a6d50 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 character..
a6d60 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74 65 ** A single byte
a6d70 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 is required for
a6d80 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72 69 the output stri
a6d90 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 ng. ** nul-te
a6da0 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f rminator.. */
a6db0 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d 20 . pMem->n &=
a6dc0 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d ~1;. len = pM
a6dd0 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a 20 em->n * 2 + 1;.
a6de0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 }else{. /* W
a6df0 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 hen converting f
a6e00 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 rom UTF-8 to UTF
a6e10 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 -16 the maximum
a6e20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65 64 growth is caused
a6e30 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20 31 . ** when a 1
a6e40 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61 72 -byte UTF-8 char
a6e50 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c 61 acter is transla
a6e60 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79 74 ted into a 2-byt
a6e70 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a 20 e UTF-16. **
a6e80 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20 62 character. Two b
a6e90 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72 65 ytes are require
a6ea0 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 d in the output
a6eb0 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a 20 buffer for the.
a6ec0 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e ** nul-termin
a6ed0 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ator.. */.
a6ee0 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a len = pMem->n *
a6ef0 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2 + 2;. }.. /
a6f00 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69 * Set zIn to poi
a6f10 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20 nt at the start
a6f20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 of the input buf
a6f30 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f fer and zTerm to
a6f40 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62 79 point 1. ** by
a6f50 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 2e te past the end.
a6f60 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61 . **. ** Varia
a6f70 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74 20 ble zOut is set
a6f80 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 to point at the
a6f90 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 73 output buffer, s
a6fa0 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20 20 pace obtained.
a6fb0 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ** from sqlite3_
a6fc0 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20 malloc().. */.
a6fd0 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d zIn = (u8*)pMem
a6fe0 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26 ->z;. zTerm = &
a6ff0 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 zIn[pMem->n];.
a7000 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44 62 zOut = sqlite3Db
a7010 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e MallocRaw(pMem->
a7020 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28 20 db, len);. if(
a7030 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65 74 !zOut ){. ret
a7040 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
a7050 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74 ;. }. z = zOut
a7060 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 ;.. if( pMem->e
a7070 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc==SQLITE_UTF8
a7080 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69 72 ){. if( desir
a7090 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 edEnc==SQLITE_UT
a70a0 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f F16LE ){. /
a70b0 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 * UTF-8 -> UTF-1
a70c0 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 6 Little-endian
a70d0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 */. while(
a70e0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 zIn<zTerm ){.
a70f0 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69 /* c = sqli
a7100 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c te3Utf8Read(zIn,
a7110 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 zTerm, (const u
a7120 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 8**)&zIn); */.
a7130 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28 READ_UTF8(
a7140 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a zIn, zTerm, c);.
a7150 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
a7160 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 F16LE(z, c);.
a7170 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
a7180 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 65 assert( de
a7190 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
a71a0 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20 20 _UTF16BE );.
a71b0 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 /* UTF-8 -> UT
a71c0 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20 F-16 Big-endian
a71d0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 */. while(
a71e0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 zIn<zTerm ){.
a71f0 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69 /* c = sqli
a7200 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c te3Utf8Read(zIn,
a7210 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 zTerm, (const u
a7220 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 8**)&zIn); */.
a7230 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28 READ_UTF8(
a7240 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a zIn, zTerm, c);.
a7250 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
a7260 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 F16BE(z, c);.
a7270 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 }. }. p
a7280 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a Mem->n = (int)(z
a7290 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2a 7a - zOut);. *z
a72a0 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b ++ = 0;. }else{
a72b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65 73 . assert( des
a72c0 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
a72d0 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28 20 UTF8 );. if(
a72e0 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 pMem->enc==SQLIT
a72f0 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 E_UTF16LE ){.
a7300 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 74 /* UTF-16 Lit
a7310 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 tle-endian -> UT
a7320 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 F-8 */. whi
a7330 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b le( zIn<zTerm ){
a7340 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 . READ_UT
a7350 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b 20 0a F16LE(zIn, c); .
a7360 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
a7370 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 F8(z, c);.
a7380 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
a7390 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69 67 /* UTF-16 Big
a73a0 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38 -endian -> UTF-8
a73b0 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 */. while(
a73c0 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 zIn<zTerm ){.
a73d0 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 READ_UTF16
a73e0 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20 20 BE(zIn, c); .
a73f0 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 WRITE_UTF8(
a7400 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 z, c);. }.
a7410 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e }. pMem->n
a7420 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 = (int)(z - zOu
a7430 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 t);. }. *z = 0
a7440 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 ;. assert( (pMe
a7450 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45 6e 63 m->n+(desiredEnc
a7460 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a ==SQLITE_UTF8?1:
a7470 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 2))<=len );.. s
a7480 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
a7490 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d ease(pMem);. pM
a74a0 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d em->flags &= ~(M
a74b0 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 EM_Static|MEM_Dy
a74c0 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 n|MEM_Ephem);.
a74d0 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 pMem->enc = desi
a74e0 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e redEnc;. pMem->
a74f0 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 flags |= (MEM_Te
a7500 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 70 rm|MEM_Dyn);. p
a7510 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 Mem->z = (char*)
a7520 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d zOut;. pMem->zM
a7530 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b alloc = pMem->z;
a7540 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a ..translate_out:
a7550 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 .#if defined(TRA
a7560 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 NSLATE_TRACE) &&
a7570 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
a7580 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 DEBUG). {. c
a7590 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 har zBuf[100];.
a75a0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
a75b0 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 mPrettyPrint(pMe
a75c0 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 m, zBuf);. fp
a75d0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 4f rintf(stderr, "O
a75e0 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 UTPUT: %s\n", zB
a75f0 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a uf);. }.#endif.
a7600 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
a7610 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 OK;.}../*.** Thi
a7620 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
a7630 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72 64 65 for a byte-orde
a7640 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20 62 65 r mark at the be
a7650 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a ginning of the .
a7660 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 ** UTF-16 string
a7670 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d stored in *pMem
a7680 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72 65 73 . If one is pres
a7690 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d 6f 76 ent, it is remov
a76a0 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e ed and.** the en
a76b0 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 coding of the Me
a76c0 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68 69 73 m adjusted. This
a76d0 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f routine does no
a76e0 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65 t do any.** byte
a76f0 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 6a 75 -swapping, it ju
a7700 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20 st sets Mem.enc
a7710 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a appropriately..*
a7720 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 *.** The allocat
a7730 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64 79 6e ion (static, dyn
a7740 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64 20 65 amic etc.) and e
a7750 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d ncoding of the M
a7760 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61 em may be.** cha
a7770 6e 67 65 64 20 62 79 20 74 68 69 73 20 66 75 6e nged by this fun
a7780 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
a7790 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a77a0 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c ite3VdbeMemHandl
a77b0 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b eBom(Mem *pMem){
a77c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
a77d0 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 TE_OK;. u8 bom
a77e0 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d = 0;.. if( pMem
a77f0 2d 3e 6e 3c 30 20 7c 7c 20 70 4d 65 6d 2d 3e 6e ->n<0 || pMem->n
a7800 3e 31 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 >1 ){. u8 b1
a7810 3d 20 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a = *(u8 *)pMem->z
a7820 3b 0a 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 ;. u8 b2 = *(
a7830 28 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 ((u8 *)pMem->z)
a7840 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 + 1);. if( b1
a7850 3d 3d 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 ==0xFE && b2==0x
a7860 46 46 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 FF ){. bom
a7870 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 = SQLITE_UTF16BE
a7880 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
a7890 62 31 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d b1==0xFF && b2==
a78a0 30 78 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 0xFE ){. bo
a78b0 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 m = SQLITE_UTF16
a78c0 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 LE;. }. }.
a78d0 0a 20 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 . if( bom ){.
a78e0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
a78f0 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
a7900 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 le(pMem);. if
a7910 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
a7920 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e ){. pMem->n
a7930 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d -= 2;. mem
a7940 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 move(pMem->z, &p
a7950 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d Mem->z[2], pMem-
a7960 3e 6e 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d >n);. pMem-
a7970 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c >z[pMem->n] = '\
a7980 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 0';. pMem->
a7990 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 z[pMem->n+1] = '
a79a0 5c 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d \0';. pMem-
a79b0 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 >flags |= MEM_Te
a79c0 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e rm;. pMem->
a79d0 65 6e 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d enc = bom;. }
a79e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
a79f0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
a7a00 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 LITE_OMIT_UTF16
a7a10 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 */../*.** pZ is
a7a20 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 a UTF-8 encoded
a7a30 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 unicode string.
a7a40 49 66 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 If nByte is less
a7a50 20 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 than zero,.** r
a7a60 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
a7a70 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 of unicode char
a7a80 61 63 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 acters in pZ up
a7a90 74 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c to (but not incl
a7aa0 75 64 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 uding).** the fi
a7ab0 72 73 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 rst 0x00 byte. I
a7ac0 66 20 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c f nByte is not l
a7ad0 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 ess than zero, r
a7ae0 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d eturn the.** num
a7af0 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 ber of unicode c
a7b00 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 haracters in the
a7b10 20 66 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 first nByte of
a7b20 70 5a 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a pZ (or up to .**
a7b30 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30 2c the first 0x00,
a7b40 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 whichever comes
a7b50 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 first)..*/.SQLI
a7b60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a7b70 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 qlite3Utf8CharLe
a7b80 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 n(const char *zI
a7b90 6e 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 n, int nByte){.
a7ba0 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f int r = 0;. co
a7bb0 6e 73 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e nst u8 *z = (con
a7bc0 73 74 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f st u8*)zIn;. co
a7bd0 6e 73 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 nst u8 *zTerm;.
a7be0 20 69 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b if( nByte>=0 ){
a7bf0 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b . zTerm = &z[
a7c00 6e 42 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b nByte];. }else{
a7c10 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f . zTerm = (co
a7c20 6e 73 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 nst u8*)(-1);.
a7c30 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a }. assert( z<=z
a7c40 54 65 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 Term );. while(
a7c50 20 2a 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 *z!=0 && z<zTer
a7c60 6d 20 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f m ){. SQLITE_
a7c70 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 SKIP_UTF8(z);.
a7c80 20 20 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 r++;. }. ret
a7c90 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 urn r;.}../* Thi
a7ca0 73 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 s test function
a7cb0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
a7cc0 20 75 73 65 64 20 62 79 20 74 68 65 20 61 75 74 used by the aut
a7cd0 6f 6d 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 omated test-suit
a7ce0 65 2e 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 e. .** Hence it
a7cf0 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
a7d00 65 20 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 e in debug build
a7d10 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 s..*/.#if define
a7d20 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 d(SQLITE_TEST) &
a7d30 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
a7d40 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 _DEBUG)./*.** Tr
a7d50 61 6e 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f anslate UTF-8 to
a7d60 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 UTF-8..**.** Th
a7d70 69 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63 is has the effec
a7d80 74 20 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 t of making sure
a7d90 20 74 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 that the string
a7da0 20 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a is well-formed.
a7db0 2a 2a 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f ** UTF-8. Misco
a7dc0 64 65 64 20 63 68 61 72 61 63 74 65 72 73 20 61 ded characters a
a7dd0 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a re removed..**.*
a7de0 2a 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f * The translatio
a7df0 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 n is done in-pla
a7e00 63 65 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 ce (since it is
a7e10 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 impossible for t
a7e20 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 he.** correct UT
a7e30 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 F-8 encoding to
a7e40 62 65 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 be longer than a
a7e50 20 6d 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 malformed encod
a7e60 69 6e 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ing)..*/.SQLITE_
a7e70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a7e80 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 te3Utf8To8(unsig
a7e90 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a ned char *zIn){.
a7ea0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
a7eb0 2a 7a 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 *zOut = zIn;. u
a7ec0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 nsigned char *zS
a7ed0 74 61 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e tart = zIn;. un
a7ee0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 signed char *zTe
a7ef0 72 6d 20 3d 20 26 7a 49 6e 5b 73 71 6c 69 74 65 rm = &zIn[sqlite
a7f00 33 53 74 72 6c 65 6e 33 30 28 28 63 68 61 72 20 3Strlen30((char
a7f10 2a 29 7a 49 6e 29 5d 3b 0a 20 20 75 33 32 20 63 *)zIn)];. u32 c
a7f20 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b ;.. while( zIn[
a7f30 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 0] ){. c = sq
a7f40 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 lite3Utf8Read(zI
a7f50 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 n, zTerm, (const
a7f60 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20 20 20 u8**)&zIn);.
a7f70 20 69 66 28 20 63 21 3d 30 78 66 66 66 64 20 29 if( c!=0xfffd )
a7f80 7b 0a 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 {. WRITE_UT
a7f90 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a 20 20 20 F8(zOut, c);.
a7fa0 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 74 20 3d }. }. *zOut =
a7fb0 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 0;. return (in
a7fc0 74 29 28 7a 4f 75 74 20 2d 20 7a 53 74 61 72 74 t)(zOut - zStart
a7fd0 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 );.}.#endif..#if
a7fe0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
a7ff0 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f 6e _UTF16./*.** Con
a8000 76 65 72 74 20 61 20 55 54 46 2d 31 36 20 73 74 vert a UTF-16 st
a8010 72 69 6e 67 20 69 6e 20 74 68 65 20 6e 61 74 69 ring in the nati
a8020 76 65 20 65 6e 63 6f 64 69 6e 67 20 69 6e 74 6f ve encoding into
a8030 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e a UTF-8 string.
a8040 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f .** Memory to ho
a8050 6c 64 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 ld the UTF-8 str
a8060 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ing is obtained
a8070 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
a8080 6c 6f 63 20 61 6e 64 20 6d 75 73 74 0a 2a 2a 20 loc and must.**
a8090 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 be freed by the
a80a0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
a80b0 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 ..**.** NULL is
a80c0 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 72 returned if ther
a80d0 65 20 69 73 20 61 6e 20 61 6c 6c 6f 63 61 74 69 e is an allocati
a80e0 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c on error..*/.SQL
a80f0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
a8100 20 2a 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f *sqlite3Utf16to
a8110 38 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 8(sqlite3 *db, c
a8120 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e onst void *z, in
a8130 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20 t nByte){. Mem
a8140 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 m;. memset(&m,
a8150 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 0, sizeof(m));.
a8160 20 6d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 m.db = db;. sq
a8170 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
a8180 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 79 74 65 2c tr(&m, z, nByte,
a8190 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
a81a0 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 IVE, SQLITE_STAT
a81b0 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 IC);. sqlite3Vd
a81c0 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
a81d0 28 26 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 (&m, SQLITE_UTF8
a81e0 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c );. if( db->mal
a81f0 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
a8200 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
a8210 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 20 20 elease(&m);.
a8220 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 m.z = 0;. }. a
a8230 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 ssert( (m.flags
a8240 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 7c & MEM_Term)!=0 |
a8250 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
a8260 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ed );. assert(
a8270 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 (m.flags & MEM_S
a8280 74 72 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 tr)!=0 || db->ma
a8290 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
a82a0 72 65 74 75 72 6e 20 28 6d 2e 66 6c 61 67 73 20 return (m.flags
a82b0 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 3f 20 & MEM_Dyn)!=0 ?
a82c0 6d 2e 7a 20 3a 20 73 71 6c 69 74 65 33 44 62 53 m.z : sqlite3DbS
a82d0 74 72 44 75 70 28 64 62 2c 20 6d 2e 7a 29 3b 0a trDup(db, m.z);.
a82e0 7d 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 }../*.** pZ is a
a82f0 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 UTF-16 encoded
a8300 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 unicode string.
a8310 49 66 20 6e 43 68 61 72 20 69 73 20 6c 65 73 73 If nChar is less
a8320 20 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 than zero,.** r
a8330 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
a8340 20 6f 66 20 62 79 74 65 73 20 75 70 20 74 6f 20 of bytes up to
a8350 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 (but not includi
a8360 6e 67 29 2c 20 74 68 65 20 66 69 72 73 74 20 70 ng), the first p
a8370 61 69 72 0a 2a 2a 20 6f 66 20 63 6f 6e 73 65 63 air.** of consec
a8380 75 74 69 76 65 20 30 78 30 30 20 62 79 74 65 73 utive 0x00 bytes
a8390 20 69 6e 20 70 5a 2e 20 49 66 20 6e 43 68 61 72 in pZ. If nChar
a83a0 20 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 is not less tha
a83b0 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 n zero,.** then
a83c0 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 return the numbe
a83d0 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
a83e0 65 20 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e e first nChar un
a83f0 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 icode characters
a8400 0a 2a 2a 20 69 6e 20 70 5a 20 28 6f 72 20 75 70 .** in pZ (or up
a8410 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 until the first
a8420 20 70 61 69 72 20 6f 66 20 30 78 30 30 20 62 79 pair of 0x00 by
a8430 74 65 73 2c 20 77 68 69 63 68 65 76 65 72 20 63 tes, whichever c
a8440 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a omes first)..*/.
a8450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a8460 6e 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 nt sqlite3Utf16B
a8470 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 yteLen(const voi
a8480 64 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 d *zIn, int nCha
a8490 72 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 r){. unsigned i
a84a0 6e 74 20 63 20 3d 20 31 3b 0a 20 20 63 68 61 72 nt c = 1;. char
a84b0 20 63 6f 6e 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b const *z = zIn;
a84c0 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 . int n = 0;.
a84d0 69 66 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36 if( SQLITE_UTF16
a84e0 4e 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 NATIVE==SQLITE_U
a84f0 54 46 31 36 42 45 20 29 7b 0a 20 20 20 20 2f 2a TF16BE ){. /*
a8500 20 55 73 69 6e 67 20 61 6e 20 22 69 66 20 28 53 Using an "if (S
a8510 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 QLITE_UTF16NATIV
a8520 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 E==SQLITE_UTF16B
a8530 45 29 22 20 63 6f 6e 73 74 72 75 63 74 20 68 65 E)" construct he
a8540 72 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 6e re. ** and in
a8550 20 6f 74 68 65 72 20 70 61 72 74 73 20 6f 66 20 other parts of
a8560 74 68 69 73 20 66 69 6c 65 20 6d 65 61 6e 73 20 this file means
a8570 74 68 61 74 20 61 74 20 6f 6e 65 20 62 72 61 6e that at one bran
a8580 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e ch will. ** n
a8590 6f 74 20 62 65 20 63 6f 76 65 72 65 64 20 62 79 ot be covered by
a85a0 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e coverage testin
a85b0 67 20 6f 6e 20 61 6e 79 20 73 69 6e 67 6c 65 20 g on any single
a85c0 68 6f 73 74 2e 20 42 75 74 20 63 6f 76 65 72 61 host. But covera
a85d0 67 65 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 ge. ** will b
a85e0 65 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 74 68 e complete if th
a85f0 65 20 74 65 73 74 73 20 61 72 65 20 72 75 6e 20 e tests are run
a8600 6f 6e 20 62 6f 74 68 20 61 20 6c 69 74 74 6c 65 on both a little
a8610 2d 65 6e 64 69 61 6e 20 61 6e 64 20 0a 20 20 20 -endian and .
a8620 20 2a 2a 20 62 69 67 2d 65 6e 64 69 61 6e 20 68 ** big-endian h
a8630 6f 73 74 2e 20 42 65 63 61 75 73 65 20 62 6f 74 ost. Because bot
a8640 68 20 74 68 65 20 55 54 46 31 36 4e 41 54 49 56 h the UTF16NATIV
a8650 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 55 54 46 E and SQLITE_UTF
a8660 31 36 42 45 0a 20 20 20 20 2a 2a 20 6d 61 63 72 16BE. ** macr
a8670 6f 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 os are constant
a8680 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 at compile time
a8690 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e the compiler can
a86a0 20 64 65 74 65 72 6d 69 6e 65 0a 20 20 20 20 2a determine. *
a86b0 2a 20 77 68 69 63 68 20 62 72 61 6e 63 68 20 77 * which branch w
a86c0 69 6c 6c 20 62 65 20 66 6f 6c 6c 6f 77 65 64 2e ill be followed.
a86d0 20 49 74 20 69 73 20 74 68 65 72 65 66 6f 72 65 It is therefore
a86e0 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 6e 6f assumed that no
a86f0 20 72 75 6e 74 69 6d 65 0a 20 20 20 20 2a 2a 20 runtime. **
a8700 70 65 6e 61 6c 74 79 20 69 73 20 70 61 69 64 20 penalty is paid
a8710 66 6f 72 20 74 68 69 73 20 22 69 66 22 20 73 74 for this "if" st
a8720 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a atement.. */.
a8730 20 20 20 20 77 68 69 6c 65 28 20 63 20 26 26 20 while( c &&
a8740 28 28 6e 43 68 61 72 3c 30 29 20 7c 7c 20 6e 3c ((nChar<0) || n<
a8750 6e 43 68 61 72 29 20 29 7b 0a 20 20 20 20 20 20 nChar) ){.
a8760 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 READ_UTF16BE(z,
a8770 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 c);. n++;.
a8780 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
a8790 20 20 77 68 69 6c 65 28 20 63 20 26 26 20 28 28 while( c && ((
a87a0 6e 43 68 61 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 nChar<0) || n<nC
a87b0 68 61 72 29 20 29 7b 0a 20 20 20 20 20 20 52 45 har) ){. RE
a87c0 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 AD_UTF16LE(z, c)
a87d0 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 ;. n++;.
a87e0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
a87f0 28 69 6e 74 29 28 7a 2d 28 63 68 61 72 20 63 6f (int)(z-(char co
a8800 6e 73 74 20 2a 29 7a 49 6e 29 2d 28 28 63 3d 3d nst *)zIn)-((c==
a8810 30 29 3f 32 3a 30 29 3b 0a 7d 0a 0a 23 69 66 20 0)?2:0);.}..#if
a8820 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
a8830 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 EST)./*.** This
a8840 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
a8850 64 20 66 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 d from the TCL t
a8860 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 est function "tr
a8870 61 6e 73 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 anslate_selftest
a8880 22 2e 0a 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 "..** It checks
a8890 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 that the primiti
a88a0 76 65 73 20 66 6f 72 20 73 65 72 69 61 6c 69 7a ves for serializ
a88b0 69 6e 67 20 61 6e 64 20 64 65 73 65 72 69 61 6c ing and deserial
a88c0 69 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 izing.** charact
a88d0 65 72 73 20 69 6e 20 65 61 63 68 20 65 6e 63 6f ers in each enco
a88e0 64 69 6e 67 20 61 72 65 20 69 6e 76 65 72 73 65 ding are inverse
a88f0 73 20 6f 66 20 65 61 63 68 20 6f 74 68 65 72 2e s of each other.
a8900 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a8910 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
a8920 74 66 53 65 6c 66 54 65 73 74 28 76 6f 69 64 29 tfSelfTest(void)
a8930 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 {. unsigned int
a8940 20 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 i, t;. unsigne
a8950 64 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b d char zBuf[20];
a8960 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
a8970 20 2a 7a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 *z;. unsigned
a8980 63 68 61 72 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 char *zTerm;. i
a8990 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 nt n;. unsigned
a89a0 20 69 6e 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69 int c;.. for(i
a89b0 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 =0; i<0x00110000
a89c0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 ; i++){. z =
a89d0 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f zBuf;. WRITE_
a89e0 55 54 46 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20 UTF8(z, i);.
a89f0 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 n = (int)(z-zBuf
a8a00 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e );. assert( n
a8a10 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 >0 && n<=4 );.
a8a20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 z[0] = 0;.
a8a30 7a 54 65 72 6d 20 3d 20 7a 3b 0a 20 20 20 20 7a zTerm = z;. z
a8a40 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d = zBuf;. c =
a8a50 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
a8a60 28 7a 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 (z, zTerm, (cons
a8a70 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 t u8**)&z);.
a8a80 74 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 t = i;. if( i
a8a90 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 >=0xD800 && i<=0
a8aa0 78 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 xDFFF ) t = 0xFF
a8ab0 46 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 FD;. if( (i&0
a8ac0 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 xFFFFFFFE)==0xFF
a8ad0 46 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b FE ) t = 0xFFFD;
a8ae0 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d . assert( c==
a8af0 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
a8b00 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a (z-zBuf)==n );.
a8b10 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
a8b20 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b <0x00110000; i++
a8b30 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 ){. if( i>=0x
a8b40 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 D800 && i<0xE000
a8b50 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
a8b60 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 z = zBuf;. W
a8b70 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 RITE_UTF16LE(z,
a8b80 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 i);. n = (int
a8b90 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 )(z-zBuf);. a
a8ba0 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c ssert( n>0 && n<
a8bb0 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d =4 );. z[0] =
a8bc0 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 0;. z = zBuf
a8bd0 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 ;. READ_UTF16
a8be0 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73 LE(z, c);. as
a8bf0 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 sert( c==i );.
a8c00 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 assert( (z-zBu
a8c10 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 f)==n );. }. f
a8c20 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 or(i=0; i<0x0011
a8c30 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 0000; i++){.
a8c40 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 if( i>=0xD800 &&
a8c50 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74 i<0xE000 ) cont
a8c60 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 inue;. z = zB
a8c70 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 uf;. WRITE_UT
a8c80 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20 F16BE(z, i);.
a8c90 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 n = (int)(z-zBu
a8ca0 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 f);. assert(
a8cb0 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 n>0 && n<=4 );.
a8cc0 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 z[0] = 0;.
a8cd0 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 z = zBuf;. R
a8ce0 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 EAD_UTF16BE(z, c
a8cf0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 );. assert( c
a8d00 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==i );. asser
a8d10 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 t( (z-zBuf)==n )
a8d20 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f ;. }.}.#endif /
a8d30 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f * SQLITE_TEST */
a8d40 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
a8d50 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
a8d60 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a8d70 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a End of utf.c **
a8d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a8db0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a8dc0 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c Begin file util
a8dd0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
a8de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a8e00 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
a8e10 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
a8e20 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
a8e30 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
a8e40 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
a8e50 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
a8e60 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
a8e70 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
a8e80 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
a8e90 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
a8ea0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
a8eb0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
a8ec0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
a8ed0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
a8ee0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
a8ef0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
a8f00 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
a8f10 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
a8f20 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
a8f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8f70 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 *******.** Utili
a8f80 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 ty functions use
a8f90 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c d throughout sql
a8fa0 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ite..**.** This
a8fb0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 file contains fu
a8fc0 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f nctions for allo
a8fd0 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 cating memory, c
a8fe0 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 omparing.** stri
a8ff0 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c ngs, and stuff l
a9000 69 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 ike that..**.**
a9010 24 49 64 3a 20 75 74 69 6c 2e 63 2c 76 20 31 2e $Id: util.c,v 1.
a9020 32 34 39 20 32 30 30 39 2f 30 33 2f 30 31 20 32 249 2009/03/01 2
a9030 32 3a 32 39 3a 32 30 20 64 72 68 20 45 78 70 20 2:29:20 drh Exp
a9040 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 $.*/../*.** Rout
a9050 69 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 ine needed to su
a9060 70 70 6f 72 74 20 74 68 65 20 74 65 73 74 63 61 pport the testca
a9070 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 se() macro..*/.#
a9080 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 ifdef SQLITE_COV
a9090 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 ERAGE_TEST.SQLIT
a90a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a90b0 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 qlite3Coverage(i
a90c0 6e 74 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20 nt x){. static
a90d0 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 int dummy = 0;.
a90e0 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 dummy += x;.}.#
a90f0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 endif../*.** Rou
a9100 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 tine needed to s
a9110 75 70 70 6f 72 74 20 74 68 65 20 41 4c 57 41 59 upport the ALWAY
a9120 53 28 29 20 61 6e 64 20 4e 45 56 45 52 28 29 20 S() and NEVER()
a9130 6d 61 63 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 macros..**.** Th
a9140 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c e argument to AL
a9150 57 41 59 53 28 29 20 73 68 6f 75 6c 64 20 61 6c WAYS() should al
a9160 77 61 79 73 20 62 65 20 74 72 75 65 20 61 6e 64 ways be true and
a9170 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a the argument.**
a9180 20 74 6f 20 4e 45 56 45 52 28 29 20 73 68 6f 75 to NEVER() shou
a9190 6c 64 20 61 6c 77 61 79 73 20 62 65 20 66 61 6c ld always be fal
a91a0 73 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 69 se. If either i
a91b0 73 20 6e 6f 74 20 74 68 65 20 63 61 73 65 0a 2a s not the case.*
a91c0 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 * then this rout
a91d0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e ine is called in
a91e0 20 6f 72 64 65 72 20 74 6f 20 74 68 72 6f 77 20 order to throw
a91f0 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 an error..**.**
a9200 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c This routine onl
a9210 79 20 65 78 69 73 74 73 20 69 66 20 61 73 73 65 y exists if asse
a9220 72 74 28 29 20 69 73 20 6f 70 65 72 61 74 69 6f rt() is operatio
a9230 6e 61 6c 2e 20 20 49 74 20 61 6c 77 61 79 73 0a nal. It always.
a9240 2a 2a 20 74 68 72 6f 77 73 20 61 6e 20 61 73 73 ** throws an ass
a9250 65 72 74 20 6f 6e 20 69 74 73 20 66 69 72 73 74 ert on its first
a9260 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 invocation. Th
a9270 65 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 61 e variable has a
a9280 20 6c 6f 6e 67 0a 2a 2a 20 6e 61 6d 65 20 74 6f long.** name to
a9290 20 68 65 6c 70 20 74 68 65 20 61 73 73 65 72 74 help the assert
a92a0 28 29 20 6d 65 73 73 61 67 65 20 62 65 20 6d 6f () message be mo
a92b0 72 65 20 72 65 61 64 61 62 6c 65 2e 20 20 54 68 re readable. Th
a92c0 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 e variable.** is
a92d0 20 75 73 65 64 20 74 6f 20 70 72 65 76 65 6e 74 used to prevent
a92e0 20 61 20 74 6f 6f 2d 63 6c 65 76 65 72 20 6f 70 a too-clever op
a92f0 74 69 6d 69 7a 65 72 20 66 72 6f 6d 20 6f 70 74 timizer from opt
a9300 69 6d 69 7a 69 6e 67 20 6f 75 74 20 74 68 65 0a imizing out the.
a9310 2a 2a 20 65 6e 74 69 72 65 20 63 61 6c 6c 2e 0a ** entire call..
a9320 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 */.#ifndef NDEBU
a9330 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
a9340 20 69 6e 74 20 73 71 6c 69 74 65 33 41 73 73 65 int sqlite3Asse
a9350 72 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 rt(void){. stat
a9360 69 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 ic volatile int
a9370 41 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c 73 65 ALWAYS_was_false
a9380 5f 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72 _or_NEVER_was_tr
a9390 75 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 ue = 0;. assert
a93a0 28 20 41 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c ( ALWAYS_was_fal
a93b0 73 65 5f 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f se_or_NEVER_was_
a93c0 74 72 75 65 20 29 3b 20 20 20 20 20 20 2f 2a 20 true ); /*
a93d0 41 6c 77 61 79 73 20 66 61 69 6c 73 20 2a 2f 0a Always fails */.
a93e0 20 20 72 65 74 75 72 6e 20 41 4c 57 41 59 53 5f return ALWAYS_
a93f0 77 61 73 5f 66 61 6c 73 65 5f 6f 72 5f 4e 45 56 was_false_or_NEV
a9400 45 52 5f 77 61 73 5f 74 72 75 65 2b 2b 3b 20 20 ER_was_true++;
a9410 20 20 20 20 20 2f 2a 20 4e 6f 74 20 52 65 61 63 /* Not Reac
a9420 68 65 64 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 0a hed */.}.#endif.
a9430 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
a9440 75 65 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 ue if the floati
a9450 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 ng point value i
a9460 73 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 s Not a Number (
a9470 4e 61 4e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NaN)..*/.SQLITE_
a9480 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a9490 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 te3IsNaN(double
a94a0 78 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20 4e 61 x){. /* This Na
a94b0 4e 20 74 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 N test sometimes
a94c0 20 66 61 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c fails if compil
a94d0 65 64 20 6f 6e 20 47 43 43 20 77 69 74 68 20 2d ed on GCC with -
a94e0 66 66 61 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a ffast-math.. **
a94f0 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 On the other ha
a9500 6e 64 2c 20 74 68 65 20 75 73 65 20 6f 66 20 2d nd, the use of -
a9510 66 66 61 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 ffast-math comes
a9520 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 with the follow
a9530 69 6e 67 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 ing. ** warning
a9540 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 :. **. **
a9550 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 This option [-f
a9560 66 61 73 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c fast-math] shoul
a9570 64 20 6e 65 76 65 72 20 62 65 20 74 75 72 6e 65 d never be turne
a9580 64 20 6f 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a d on by any. **
a9590 20 20 20 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 -O option
a95a0 73 69 6e 63 65 20 69 74 20 63 61 6e 20 72 65 73 since it can res
a95b0 75 6c 74 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 ult in incorrect
a95c0 20 6f 75 74 70 75 74 20 66 6f 72 20 70 72 6f 67 output for prog
a95d0 72 61 6d 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 rams. ** w
a95e0 68 69 63 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 hich depend on a
a95f0 6e 20 65 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e n exact implemen
a9600 74 61 74 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f tation of IEEE o
a9610 72 20 49 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 r ISO . **
a9620 20 72 75 6c 65 73 2f 73 70 65 63 69 66 69 63 61 rules/specifica
a9630 74 69 6f 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 tions for math f
a9640 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 unctions.. **.
a9650 20 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 ** Under MSVC,
a9660 74 68 69 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 this NaN test ma
a9670 79 20 66 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c y fail if compil
a9680 65 64 20 77 69 74 68 20 61 20 66 6c 6f 61 74 69 ed with a floati
a9690 6e 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 ng-. ** point p
a96a0 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 recision mode ot
a96b0 68 65 72 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 her than /fp:pre
a96c0 63 69 73 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 cise. From the
a96d0 4d 53 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d MSDN . ** docum
a96e0 65 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 entation:. **.
a96f0 20 2a 2a 20 20 20 20 20 20 54 68 65 20 63 6f 6d ** The com
a9700 70 69 6c 65 72 20 5b 77 69 74 68 20 2f 66 70 3a piler [with /fp:
a9710 70 72 65 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 precise] will pr
a9720 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f operly handle co
a9730 6d 70 61 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 mparisons . **
a9740 20 20 20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e involving N
a9750 61 4e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c aN. For example,
a9760 20 78 20 21 3d 20 78 20 65 76 61 6c 75 61 74 65 x != x evaluate
a9770 73 20 74 6f 20 74 72 75 65 20 69 66 20 78 20 69 s to true if x i
a9780 73 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 s NaN . **
a9790 20 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 .... */.#ifdef
a97a0 20 5f 5f 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 __FAST_MATH__.#
a97b0 20 65 72 72 6f 72 20 53 51 4c 69 74 65 20 77 69 error SQLite wi
a97c0 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 ll not work corr
a97d0 65 63 74 6c 79 20 77 69 74 68 20 74 68 65 20 2d ectly with the -
a97e0 66 66 61 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f ffast-math optio
a97f0 6e 20 6f 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 n of GCC..#endif
a9800 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 . volatile doub
a9810 6c 65 20 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 le y = x;. vola
a9820 74 69 6c 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 tile double z =
a9830 79 3b 0a 20 20 72 65 74 75 72 6e 20 79 21 3d 7a y;. return y!=z
a9840 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 ;.}../*.** Compu
a9850 74 65 20 61 20 73 74 72 69 6e 67 20 6c 65 6e 67 te a string leng
a9860 74 68 20 74 68 61 74 20 69 73 20 6c 69 6d 69 74 th that is limit
a9870 65 64 20 74 6f 20 77 68 61 74 20 63 61 6e 20 62 ed to what can b
a9880 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 6c e stored in.** l
a9890 6f 77 65 72 20 33 30 20 62 69 74 73 20 6f 66 20 ower 30 bits of
a98a0 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 a 32-bit signed
a98b0 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 integer..*/.SQLI
a98c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a98d0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 qlite3Strlen30(c
a98e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 onst char *z){.
a98f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 const char *z2
a9900 3d 20 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a = z;. while( *z
a9910 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 2 ){ z2++; }. r
a9920 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 66 66 eturn 0x3fffffff
a9930 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 & (int)(z2 - z)
a9940 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
a9950 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 n the length of
a9960 61 20 73 74 72 69 6e 67 2c 20 65 78 63 65 70 74 a string, except
a9970 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 do not allow th
a9980 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 0a e string length.
a9990 2a 2a 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 ** to exceed the
a99a0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 SQLITE_LIMIT_LE
a99b0 4e 47 54 48 20 73 65 74 74 69 6e 67 2e 0a 2a 2f NGTH setting..*/
a99c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a99d0 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 int sqlite3Strle
a99e0 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 n(sqlite3 *db, c
a99f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 onst char *z){.
a9a00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 const char *z2
a9a10 3d 20 7a 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a = z;. int len;.
a9a20 20 20 69 6e 74 20 78 3b 0a 20 20 77 68 69 6c 65 int x;. while
a9a30 28 20 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d ( *z2 ){ z2++; }
a9a40 0a 20 20 78 20 3d 20 28 69 6e 74 29 28 7a 32 20 . x = (int)(z2
a9a50 2d 20 7a 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 78 - z);. len = 0x
a9a60 37 66 66 66 66 66 66 66 20 26 20 78 3b 0a 20 20 7fffffff & x;.
a9a70 69 66 28 20 6c 65 6e 21 3d 78 20 7c 7c 20 6c 65 if( len!=x || le
a9a80 6e 20 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 n > db->aLimit[S
a9a90 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
a9aa0 54 48 5d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 TH] ){. retur
a9ab0 6e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c n db->aLimit[SQL
a9ac0 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
a9ad0 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
a9ae0 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a return len;. }.
a9af0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
a9b00 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 most recent err
a9b10 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f or code and erro
a9b20 72 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 r string for the
a9b30 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c sqlite.** handl
a9b40 65 20 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f e "db". The erro
a9b50 72 20 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f r code is set to
a9b60 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a "err_code"..**.
a9b70 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 ** If it is not
a9b80 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f NULL, string zFo
a9b90 72 6d 61 74 20 73 70 65 63 69 66 69 65 73 20 74 rmat specifies t
a9ba0 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 he format of the
a9bb0 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67 .** error string
a9bc0 20 69 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66 in the style of
a9bd0 20 74 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63 the printf func
a9be0 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f tions: The follo
a9bf0 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 wing.** format c
a9c00 68 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c haracters are al
a9c10 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 lowed:.**.**
a9c20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 %s Insert
a9c30 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 a string.**
a9c40 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 %z A stri
a9c50 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 ng that should b
a9c60 65 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73 e freed after us
a9c70 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 e.** %d
a9c80 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 Insert an inte
a9c90 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 ger.** %T
a9ca0 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b Insert a tok
a9cb0 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 en.** %S
a9cc0 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 Insert the fi
a9cd0 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 rst element of a
a9ce0 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a SrcList.**.** z
a9cf0 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 Format and any s
a9d00 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 tring tokens tha
a9d10 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 t follow it are
a9d20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a assumed to be.**
a9d30 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d encoded in UTF-
a9d40 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 8..**.** To clea
a9d50 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e r the most recen
a9d60 74 20 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 t error for sqli
a9d70 74 65 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 te handle "db",
a9d80 73 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 sqlite3Error.**
a9d90 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 should be called
a9da0 20 77 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73 with err_code s
a9db0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 et to SQLITE_OK
a9dc0 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a and zFormat set.
a9dd0 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 ** to NULL..*/.S
a9de0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
a9df0 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 id sqlite3Error(
a9e00 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
a9e10 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 err_code, const
a9e20 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
a9e30 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26 ...){. if( db &
a9e40 26 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 & (db->pErr || (
a9e50 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 db->pErr = sqlit
a9e60 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21 e3ValueNew(db))!
a9e70 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 =0) ){. db->e
a9e80 72 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 rrCode = err_cod
a9e90 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d e;. if( zForm
a9ea0 61 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 at ){. char
a9eb0 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 *z;. va_li
a9ec0 73 74 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f st ap;. va_
a9ed0 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 start(ap, zForma
a9ee0 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 t);. z = sq
a9ef0 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
a9f00 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a9f10 20 20 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 va_end(ap)
a9f20 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
a9f30 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 alueSetStr(db->p
a9f40 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 Err, -1, z, SQLI
a9f50 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
a9f60 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 DYNAMIC);. }e
a9f70 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
a9f80 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 e3ValueSetStr(db
a9f90 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 ->pErr, 0, 0, SQ
a9fa0 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
a9fb0 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d E_STATIC);. }
a9fc0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 . }.}../*.** Ad
a9fd0 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 d an error messa
a9fe0 67 65 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 ge to pParse->zE
a9ff0 72 72 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d rrMsg and increm
aa000 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 ent pParse->nErr
aa010 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ..** The followi
aa020 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 ng formatting ch
aa030 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c aracters are all
aa040 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 owed:.**.**
aa050 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 %s Insert
aa060 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 a string.**
aa070 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e %z A strin
aa080 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 g that should be
aa090 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 freed after use
aa0a0 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 .** %d
aa0b0 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 Insert an integ
aa0c0 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 er.** %T
aa0d0 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 Insert a toke
aa0e0 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 n.** %S
aa0f0 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 Insert the fir
aa100 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 st element of a
aa110 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 SrcList.**.** Th
aa120 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 is function shou
aa130 6c 64 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 ld be used to re
aa140 70 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 port any error t
aa150 68 61 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 hat occurs whils
aa160 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 t.** compiling a
aa170 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
aa180 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c (i.e. within sql
aa190 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e ite3_prepare()).
aa1a0 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 The.** last thi
aa1b0 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 ng the sqlite3_p
aa1c0 72 65 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f repare() functio
aa1d0 6e 20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 n does is copy t
aa1e0 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 he error.** stor
aa1f0 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 ed by this funct
aa200 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ion into the dat
aa210 61 62 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 abase handle usi
aa220 6e 67 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 ng sqlite3Error(
aa230 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 )..** Function s
aa240 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 qlite3Error() sh
aa250 6f 75 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 ould be used dur
aa260 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 ing statement ex
aa270 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 ecution.** (sqli
aa280 74 65 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 te3_step() etc.)
aa290 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
aa2a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
aa2b0 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a ErrorMsg(Parse *
aa2c0 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 pParse, const ch
aa2d0 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
aa2e0 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
aa2f0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
aa300 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 pParse->db;. p
aa310 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 Parse->nErr++;.
aa320 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
aa330 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d b, pParse->zErrM
aa340 73 67 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 sg);. va_start(
aa350 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
aa360 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 pParse->zErrMsg
aa370 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
aa380 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 f(db, zFormat, a
aa390 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 p);. va_end(ap)
aa3a0 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
aa3b0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
aa3c0 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 . pParse->rc
aa3d0 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
aa3e0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 }.}../*.** Cle
aa3f0 61 72 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 ar the error mes
aa400 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 sage in pParse,
aa410 69 66 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 if any.*/.SQLITE
aa420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
aa430 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 lite3ErrorClear(
aa440 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a Parse *pParse){.
aa450 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
aa460 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 pParse->db, pPar
aa470 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 se->zErrMsg);.
aa480 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 pParse->zErrMsg
aa490 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e = 0;. pParse->n
aa4a0 45 72 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Err = 0;.}../*.*
aa4b0 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c * Convert an SQL
aa4c0 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 -style quoted st
aa4d0 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d ring into a norm
aa4e0 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d al string by rem
aa4f0 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f oving.** the quo
aa500 74 65 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 te characters.
aa510 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 The conversion i
aa520 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e s done in-place.
aa530 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 If the.** inpu
aa540 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e t does not begin
aa550 20 77 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 with a quote ch
aa560 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 aracter, then th
aa570 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 is routine.** is
aa580 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
aa590 32 30 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 2002-Feb-14: Thi
aa5a0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 s routine is ext
aa5b0 65 6e 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 ended to remove
aa5c0 4d 53 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a MS-Access style.
aa5d0 2a 2a 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d ** brackets from
aa5e0 20 61 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 around identife
aa5f0 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 rs. For example
aa600 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 : "[a-b-c]" bec
aa610 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e omes.** "a-b-c".
aa620 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
aa630 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
aa640 65 71 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b equote(char *z){
aa650 0a 20 20 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 . char quote;.
aa660 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 int i, j;. if(
aa670 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a z==0 ) return;.
aa680 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a quote = z[0];.
aa690 20 20 73 77 69 74 63 68 28 20 71 75 6f 74 65 20 switch( quote
aa6a0 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 ){. case '\''
aa6b0 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 : break;. ca
aa6c0 73 65 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b se '"': break;
aa6d0 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 . case '`':
aa6e0 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 break;
aa6f0 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 /* For My
aa700 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 SQL compatibilit
aa710 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b y */. case '[
aa720 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 ': quote = ']'
aa730 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f ; break; /* Fo
aa740 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63 r MS SqlServer c
aa750 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a ompatibility */.
aa760 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 default:
aa770 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f return;. }. fo
aa780 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d r(i=1, j=0; z[i]
aa790 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
aa7a0 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 z[i]==quote ){.
aa7b0 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d if( z[i+1]=
aa7c0 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 =quote ){.
aa7d0 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 z[j++] = quote
aa7e0 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 ;. i++;.
aa7f0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
aa800 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a z[j++] = 0;.
aa810 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
aa820 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
aa830 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d {. z[j++] =
aa840 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d z[i];. }. }
aa850 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 6e .}../* Convenien
aa860 74 20 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f 0a t short-hand */.
aa870 23 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c #define UpperToL
aa880 6f 77 65 72 20 73 71 6c 69 74 65 33 55 70 70 65 ower sqlite3Uppe
aa890 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 rToLower../*.**
aa8a0 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76 Some systems hav
aa8b0 65 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74 e stricmp(). Ot
aa8c0 68 65 72 73 20 68 61 76 65 20 73 74 72 63 61 73 hers have strcas
aa8d0 65 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65 ecmp(). Because
aa8e0 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 .** there is no
aa8f0 63 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20 consistency, we
aa900 77 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20 will define our
aa910 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 own..*/.SQLITE_P
aa920 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
aa930 65 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 e3StrICmp(const
aa940 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e char *zLeft, con
aa950 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 st char *zRight)
aa960 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 {. register uns
aa970 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a igned char *a, *
aa980 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e b;. a = (unsign
aa990 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b ed char *)zLeft;
aa9a0 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 . b = (unsigned
aa9b0 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a char *)zRight;.
aa9c0 20 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26 while( *a!=0 &
aa9d0 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a & UpperToLower[*
aa9e0 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 a]==UpperToLower
aa9f0 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b [*b]){ a++; b++;
aaa00 20 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65 }. return Uppe
aaa10 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 rToLower[*a] - U
aaa20 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b pperToLower[*b];
aaa30 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
aaa40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 E int sqlite3Str
aaa50 4e 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 NICmp(const char
aaa60 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 *zLeft, const c
aaa70 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74 har *zRight, int
aaa80 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20 N){. register
aaa90 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 unsigned char *a
aaaa0 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 , *b;. a = (uns
aaab0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 igned char *)zLe
aaac0 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 ft;. b = (unsig
aaad0 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68 ned char *)zRigh
aaae0 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 t;. while( N--
aaaf0 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26 26 20 > 0 && *a!=0 &&
aab00 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d UpperToLower[*a]
aab10 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a ==UpperToLower[*
aab20 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d b]){ a++; b++; }
aab30 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20 . return N<0 ?
aab40 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 0 : UpperToLower
aab50 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f [*a] - UpperToLo
aab60 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a wer[*b];.}../*.*
aab70 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
aab80 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e 75 6d z is a pure num
aab90 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20 52 65 eric string. Re
aaba0 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68 turn FALSE if th
aabb0 65 0a 2a 2a 20 73 74 72 69 6e 67 20 63 6f 6e 74 e.** string cont
aabc0 61 69 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 ains any charact
aabd0 65 72 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 er which is not
aabe0 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62 65 72 part of a number
aabf0 2e 20 49 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 . If.** the stri
aac00 6e 67 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e ng is numeric an
aac10 64 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 d contains the '
aac20 2e 27 20 63 68 61 72 61 63 74 65 72 2c 20 73 65 .' character, se
aac30 74 20 2a 72 65 61 6c 6e 75 6d 0a 2a 2a 20 74 6f t *realnum.** to
aac40 20 54 52 55 45 20 28 6f 74 68 65 72 77 69 73 65 TRUE (otherwise
aac50 20 46 41 4c 53 45 29 2e 0a 2a 2a 0a 2a 2a 20 41 FALSE)..**.** A
aac60 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 69 n empty string i
aac70 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e s considered non
aac80 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c -numeric..*/.SQL
aac90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
aaca0 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 sqlite3IsNumber(
aacb0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
aacc0 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20 nt *realnum, u8
aacd0 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72 enc){. int incr
aace0 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f = (enc==SQLITE_
aacf0 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28 UTF8?1:2);. if(
aad00 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 enc==SQLITE_UTF
aad10 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 16BE ) z++;. if
aad20 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d ( *z=='-' || *z=
aad30 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 ='+' ) z += incr
aad40 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 ;. if( !sqlite3
aad50 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 Isdigit(*z) ){.
aad60 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
aad70 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 . z += incr;.
aad80 69 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 if( realnum ) *r
aad90 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68 ealnum = 0;. wh
aada0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
aadb0 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 git(*z) ){ z +=
aadc0 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a 7a incr; }. if( *z
aadd0 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 2b =='.' ){. z +
aade0 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 = incr;. if(
aadf0 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 !sqlite3Isdigit(
aae00 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a *z) ) return 0;.
aae10 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
aae20 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b e3Isdigit(*z) ){
aae30 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 z += incr; }.
aae40 20 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 if( realnum )
aae50 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 *realnum = 1;.
aae60 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 }. if( *z=='e'
aae70 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 || *z=='E' ){.
aae80 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 z += incr;.
aae90 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 if( *z=='+' ||
aaea0 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69 *z=='-' ) z += i
aaeb0 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 73 71 ncr;. if( !sq
aaec0 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 lite3Isdigit(*z)
aaed0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
aaee0 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
aaef0 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 sdigit(*z) ){ z
aaf00 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 69 += incr; }. i
aaf10 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65 f( realnum ) *re
aaf20 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 alnum = 1;. }.
aaf30 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d return *z==0;.}
aaf40 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 ../*.** The stri
aaf50 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20 61 73 63 ng z[] is an asc
aaf60 69 69 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ii representatio
aaf70 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 n of a real numb
aaf80 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 er..** Convert t
aaf90 68 69 73 20 73 74 72 69 6e 67 20 74 6f 20 61 20 his string to a
aafa0 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 double..**.** Th
aafb0 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d is routine assum
aafc0 65 73 20 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c es that z[] real
aafd0 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 6e 75 ly is a valid nu
aafe0 6d 62 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 mber. If it.**
aaff0 69 73 20 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 is not, the resu
ab000 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e lt is undefined.
ab010 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
ab020 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 74 ine is used inst
ab030 65 61 64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 ead of the libra
ab040 72 79 20 61 74 6f 66 28 29 20 66 75 6e 63 74 69 ry atof() functi
ab050 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 on because.** th
ab060 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29 e library atof()
ab070 20 6d 69 67 68 74 20 77 61 6e 74 20 74 6f 20 75 might want to u
ab080 73 65 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 se "," as the de
ab090 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 cimal point inst
ab0a0 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 ead.** of "." de
ab0b0 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c pending on how l
ab0c0 6f 63 61 6c 65 20 69 73 20 73 65 74 2e 20 20 42 ocale is set. B
ab0d0 75 74 20 74 68 61 74 20 77 6f 75 6c 64 20 63 61 ut that would ca
ab0e0 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 use problems.**
ab0f0 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 for SQL. So thi
ab100 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 s routine always
ab110 20 75 73 65 73 20 22 2e 22 20 72 65 67 61 72 64 uses "." regard
ab120 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a less of locale..
ab130 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
ab140 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f E int sqlite3Ato
ab150 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c F(const char *z,
ab160 20 64 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 double *pResult
ab170 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ){.#ifndef SQLIT
ab180 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
ab190 50 4f 49 4e 54 0a 20 20 69 6e 74 20 73 69 67 6e POINT. int sign
ab1a0 20 3d 20 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 = 1;. const ch
ab1b0 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a ar *zBegin = z;.
ab1c0 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 LONGDOUBLE_TYP
ab1d0 45 20 76 31 20 3d 20 30 2e 30 3b 0a 20 20 69 6e E v1 = 0.0;. in
ab1e0 74 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 20 3d t nSignificant =
ab1f0 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 0;. while( sql
ab200 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 ite3Isspace(*z)
ab210 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d ) z++;. if( *z=
ab220 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e ='-' ){. sign
ab230 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a = -1;. z++;.
ab240 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d }else if( *z==
ab250 27 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a '+' ){. z++;.
ab260 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30 }. while( z[0
ab270 5d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 2b ]=='0' ){. z+
ab280 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 +;. }. while(
ab290 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
ab2a0 7a 29 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 76 z) ){. v1 = v
ab2b0 31 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27 1*10.0 + (*z - '
ab2c0 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 0');. z++;.
ab2d0 20 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b nSignificant++
ab2e0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d ;. }. if( *z==
ab2f0 27 2e 27 20 29 7b 0a 20 20 20 20 4c 4f 4e 47 44 '.' ){. LONGD
ab300 4f 55 42 4c 45 5f 54 59 50 45 20 64 69 76 69 73 OUBLE_TYPE divis
ab310 6f 72 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b or = 1.0;. z+
ab320 2b 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 67 6e +;. if( nSign
ab330 69 66 69 63 61 6e 74 3d 3d 30 20 29 7b 0a 20 20 ificant==0 ){.
ab340 20 20 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d while( z[0]=
ab350 3d 27 30 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='0' ){.
ab360 64 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b divisor *= 10.0;
ab370 0a 20 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 . z++;.
ab380 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
ab390 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
ab3a0 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 digit(*z) ){.
ab3b0 20 20 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63 if( nSignific
ab3c0 61 6e 74 3c 31 38 20 29 7b 0a 20 20 20 20 20 20 ant<18 ){.
ab3d0 20 20 76 31 20 3d 20 76 31 2a 31 30 2e 30 20 2b v1 = v1*10.0 +
ab3e0 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 (*z - '0');.
ab3f0 20 20 20 20 20 64 69 76 69 73 6f 72 20 2a 3d 20 divisor *=
ab400 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 6e 53 10.0;. nS
ab410 69 67 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 ignificant++;.
ab420 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 2b 2b 3b }. z++;
ab430 0a 20 20 20 20 7d 0a 20 20 20 20 76 31 20 2f 3d . }. v1 /=
ab440 20 64 69 76 69 73 6f 72 3b 0a 20 20 7d 0a 20 20 divisor;. }.
ab450 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a if( *z=='e' || *
ab460 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 69 6e z=='E' ){. in
ab470 74 20 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 20 t esign = 1;.
ab480 20 69 6e 74 20 65 76 61 6c 20 3d 20 30 3b 0a 20 int eval = 0;.
ab490 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 LONGDOUBLE_TY
ab4a0 50 45 20 73 63 61 6c 65 20 3d 20 31 2e 30 3b 0a PE scale = 1.0;.
ab4b0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 z++;. if(
ab4c0 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 *z=='-' ){.
ab4d0 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 esign = -1;.
ab4e0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c z++;. }el
ab4f0 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 se if( *z=='+' )
ab500 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 {. z++;.
ab510 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 }. while( sq
ab520 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 lite3Isdigit(*z)
ab530 20 29 7b 0a 20 20 20 20 20 20 65 76 61 6c 20 3d ){. eval =
ab540 20 65 76 61 6c 2a 31 30 20 2b 20 2a 7a 20 2d 20 eval*10 + *z -
ab550 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a '0';. z++;.
ab560 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 }. while(
ab570 20 65 76 61 6c 3e 3d 36 34 20 29 7b 20 73 63 61 eval>=64 ){ sca
ab580 6c 65 20 2a 3d 20 31 2e 30 65 2b 36 34 3b 20 65 le *= 1.0e+64; e
ab590 76 61 6c 20 2d 3d 20 36 34 3b 20 7d 0a 20 20 20 val -= 64; }.
ab5a0 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31 36 while( eval>=16
ab5b0 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 ){ scale *= 1.0
ab5c0 65 2b 31 36 3b 20 65 76 61 6c 20 2d 3d 20 31 36 e+16; eval -= 16
ab5d0 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 ; }. while( e
ab5e0 76 61 6c 3e 3d 34 20 29 7b 20 73 63 61 6c 65 20 val>=4 ){ scale
ab5f0 2a 3d 20 31 2e 30 65 2b 34 3b 20 65 76 61 6c 20 *= 1.0e+4; eval
ab600 2d 3d 20 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c -= 4; }. whil
ab610 65 28 20 65 76 61 6c 3e 3d 31 20 29 7b 20 73 63 e( eval>=1 ){ sc
ab620 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 ale *= 1.0e+1; e
ab630 76 61 6c 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 val -= 1; }.
ab640 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 if( esign<0 ){.
ab650 20 20 20 20 20 76 31 20 2f 3d 20 73 63 61 6c 65 v1 /= scale
ab660 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
ab670 20 20 20 76 31 20 2a 3d 20 73 63 61 6c 65 3b 0a v1 *= scale;.
ab680 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 }. }. *pRe
ab690 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65 29 28 sult = (double)(
ab6a0 73 69 67 6e 3c 30 20 3f 20 2d 76 31 20 3a 20 76 sign<0 ? -v1 : v
ab6b0 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 1);. return (in
ab6c0 74 29 28 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a t)(z - zBegin);.
ab6d0 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 #else. return s
ab6e0 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 qlite3Atoi64(z,
ab6f0 70 52 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 pResult);.#endif
ab700 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
ab710 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a FLOATING_POINT *
ab720 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 /.}../*.** Compa
ab730 72 65 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 re the 19-charac
ab740 74 65 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 ter string zNum
ab750 61 67 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 against the text
ab760 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a representation.
ab770 2a 2a 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 ** value 2^63:
ab780 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 9223372036854775
ab790 38 30 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 808. Return neg
ab7a0 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 ative, zero, or
ab7b0 70 6f 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a positive.** if z
ab7c0 4e 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e Num is less than
ab7d0 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 , equal to, or g
ab7e0 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
ab7f0 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e string..**.** Un
ab800 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 like memcmp() th
ab810 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 is routine is gu
ab820 61 72 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 aranteed to retu
ab830 72 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 rn the differenc
ab840 65 0a 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 e.** in the valu
ab850 65 73 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 es of the last d
ab860 69 67 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 igit if the only
ab870 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 difference is i
ab880 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 n the.** last di
ab890 67 69 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 git. So, for ex
ab8a0 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 ample,.**.**
ab8b0 20 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 compare2pow63(
ab8c0 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 "922337203685477
ab8d0 35 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 5800").**.** wil
ab8e0 6c 20 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a l return -8..*/.
ab8f0 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 static int compa
ab900 72 65 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 re2pow63(const c
ab910 68 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e har *zNum){. in
ab920 74 20 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d t c;. c = memcm
ab930 70 28 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 p(zNum,"92233720
ab940 33 36 38 35 34 37 37 35 38 30 22 2c 31 38 29 3b 3685477580",18);
ab950 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 . if( c==0 ){.
ab960 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 c = zNum[18]
ab970 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74 - '8';. }. ret
ab980 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn c;.}.../*.**
ab990 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
ab9a0 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74 zNum is a 64-bit
ab9b0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
ab9c0 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 and write.** the
ab9d0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e value of the in
ab9e0 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d teger into *pNum
ab9f0 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f . If zNum is no
aba00 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 t an integer.**
aba10 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 or is an integer
aba20 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 that is too lar
aba30 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 ge to be express
aba40 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c ed with 64 bits,
aba50 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 .** then return
aba60 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 false..**.** Whe
aba70 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 n this routine w
aba80 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72 as originally wr
aba90 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77 itten it dealt w
abaa0 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 ith only.** 32-b
abab0 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20 it numbers. At
abac0 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61 that time, it wa
abad0 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 s much faster th
abae0 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29 an the.** atoi()
abaf0 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65 library routine
abb00 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a in RedHat 7.2..
abb10 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
abb20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f E int sqlite3Ato
abb30 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a i64(const char *
abb40 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29 zNum, i64 *pNum)
abb50 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 {. i64 v = 0;.
abb60 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20 int neg;. int
abb70 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 i, c;. const ch
abb80 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68 ar *zStart;. wh
abb90 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 ile( sqlite3Issp
abba0 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75 ace(*zNum) ) zNu
abbb0 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d m++;. if( *zNum
abbc0 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 =='-' ){. neg
abbd0 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b = 1;. zNum++
abbe0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a ;. }else if( *z
abbf0 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 Num=='+' ){.
abc00 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 neg = 0;. zNu
abc10 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 m++;. }else{.
abc20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 neg = 0;. }.
abc30 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a zStart = zNum;.
abc40 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d while( zNum[0]
abc50 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b =='0' ){ zNum++;
abc60 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 } /* Skip over
abc70 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 leading zeros. T
abc80 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 icket #2454 */.
abc90 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 for(i=0; (c=zNu
abca0 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c m[i])>='0' && c<
abcb0 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 ='9'; i++){.
abcc0 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 v = v*10 + c - '
abcd0 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 0';. }. *pNum
abce0 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a = neg ? -v : v;.
abcf0 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 69 if( c!=0 || (i
abd00 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a ==0 && zStart==z
abd10 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a Num) || i>19 ){.
abd20 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 /* zNum is e
abd30 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73 mpty or contains
abd40 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78 non-numeric tex
abd50 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20 t or is longer.
abd60 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69 ** than 19 di
abd70 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72 61 gits (thus guara
abd80 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 73 nting that it is
abd90 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20 too large) */.
abda0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
abdb0 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b else if( i<19 ){
abdc0 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61 . /* Less tha
abdd0 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20 n 19 digits, so
abde0 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20 we know that it
abdf0 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 20 fits in 64 bits
abe00 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b */. return 1;
abe10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
abe20 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 19-digit number
abe30 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72 s must be no lar
abe40 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32 ger than 9223372
abe50 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66 20 036854775807 if
abe60 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20 positive. **
abe70 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35 34 or 9223372036854
abe80 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 69 775808 if negati
abe90 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39 ve. Note that 9
abea0 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35 38 2233720368546658
abeb0 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36 08. ** is 2^6
abec0 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3. */. return
abed0 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a compare2pow63(z
abee0 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a Num)<neg;. }.}.
abef0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e ./*.** The strin
abf00 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 g zNum represent
abf10 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 54 s an integer. T
abf20 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 73 6f here might be so
abf30 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f me other.** info
abf40 72 6d 61 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e rmation followin
abf50 67 20 74 68 65 20 69 6e 74 65 67 65 72 20 74 6f g the integer to
abf60 6f 2c 20 62 75 74 20 74 68 61 74 20 70 61 72 74 o, but that part
abf70 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 is ignored..**
abf80 49 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 74 If the integer t
abf90 68 61 74 20 74 68 65 20 70 72 65 66 69 78 20 6f hat the prefix o
abfa0 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 f zNum represent
abfb0 73 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a s will fit in a.
abfc0 2a 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 ** 64-bit signed
abfd0 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e integer, return
abfe0 20 54 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 TRUE. Otherwis
abff0 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a e return FALSE..
ac000 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
ac010 6e 65 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45 ne returns FALSE
ac020 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 for the string
ac030 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 -922337203685477
ac040 35 38 30 38 20 65 76 65 6e 20 74 68 61 74 0a 2a 5808 even that.*
ac050 2a 20 74 68 61 74 20 6e 75 6d 62 65 72 20 77 69 * that number wi
ac060 6c 6c 2c 20 69 6e 20 74 68 65 6f 72 79 20 66 69 ll, in theory fi
ac070 74 20 69 6e 20 61 20 36 34 2d 62 69 74 20 69 6e t in a 64-bit in
ac080 74 65 67 65 72 2e 20 20 50 6f 73 69 74 69 76 65 teger. Positive
ac090 0a 2a 2a 20 39 32 32 33 33 37 33 30 33 36 38 35 .** 922337303685
ac0a0 34 37 37 35 38 30 38 20 77 69 6c 6c 20 6e 6f 74 4775808 will not
ac0b0 20 66 69 74 20 69 6e 20 36 34 20 62 69 74 73 2e fit in 64 bits.
ac0c0 20 20 53 6f 20 69 74 20 73 65 65 6d 73 20 73 61 So it seems sa
ac0d0 66 65 72 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a fer to return.**
ac0e0 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 false..*/.SQLIT
ac0f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ac100 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 lite3FitsIn64Bit
ac110 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e s(const char *zN
ac120 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 um, int negFlag)
ac130 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 {. int i, c;.
ac140 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 int neg = 0;. i
ac150 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b f( *zNum=='-' ){
ac160 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 . neg = 1;.
ac170 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 zNum++;. }els
ac180 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 e if( *zNum=='+'
ac190 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a ){. zNum++;.
ac1a0 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 46 6c 61 }. if( negFla
ac1b0 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67 3b g ) neg = 1-neg;
ac1c0 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d 3d . while( *zNum=
ac1d0 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d ='0' ){. zNum
ac1e0 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c 65 ++; /* Skip le
ac1f0 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54 69 ading zeros. Ti
ac200 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 cket #2454 */.
ac210 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d }. for(i=0; (c=
ac220 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 zNum[i])>='0' &&
ac230 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 7d 0a c<='9'; i++){}.
ac240 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 if( i<19 ){.
ac250 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20 /* Guaranteed
ac260 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20 74 to fit if less t
ac270 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f han 19 digits */
ac280 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
ac290 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39 20 }else if( i>19
ac2a0 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e ){. /* Guaran
ac2b0 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 62 teed to be too b
ac2c0 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74 68 ig if greater th
ac2d0 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a an 19 digits */.
ac2e0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
ac2f0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f }else{. /* Co
ac300 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32 5e mpare against 2^
ac310 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 63. */. retur
ac320 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 n compare2pow63(
ac330 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d zNum)<neg;. }.}
ac340 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 ../*.** If zNum
ac350 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e represents an in
ac360 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c 20 teger that will
ac370 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c 20 fit in 32-bits,
ac380 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61 then set.** *pVa
ac390 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74 65 lue to that inte
ac3a0 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 ger and return t
ac3b0 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 rue. Otherwise
ac3c0 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a return false..**
ac3d0 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 .** Any non-nume
ac3e0 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20 74 ric characters t
ac3f0 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e hat following zN
ac400 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a um are ignored..
ac410 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66 65 ** This is diffe
ac420 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 rent from sqlite
ac430 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68 20 3Atoi64() which
ac440 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20 requires the.**
ac450 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f 20 input number to
ac460 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 be zero-terminat
ac470 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
ac480 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ac490 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 3GetInt32(const
ac4a0 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 char *zNum, int
ac4b0 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69 *pValue){. sqli
ac4c0 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a te_int64 v = 0;.
ac4d0 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e int i, c;. in
ac4e0 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28 t neg = 0;. if(
ac4f0 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b zNum[0]=='-' ){
ac500 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 . neg = 1;.
ac510 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 zNum++;. }els
ac520 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 e if( zNum[0]=='
ac530 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b +' ){. zNum++
ac540 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a ;. }. while( z
ac550 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e Num[0]=='0' ) zN
ac560 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b um++;. for(i=0;
ac570 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a 4e i<11 && (c = zN
ac580 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20 um[i] - '0')>=0
ac590 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20 && c<=9; i++){.
ac5a0 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b v = v*10 + c;
ac5b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c . }.. /* The l
ac5c0 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20 72 ongest decimal r
ac5d0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
ac5e0 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67 65 a 32 bit intege
ac5f0 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a 0a r is 10 digits:.
ac600 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 **. **
ac610 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39 30 1234567890
ac620 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d . ** 2^31 -
ac630 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20 2a > 2147483648. *
ac640 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b 0a /. if( i>10 ){.
ac650 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
ac660 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 31 }. if( v-neg>21
ac670 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20 20 47483647 ){.
ac680 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
ac690 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 76 if( neg ){. v
ac6a0 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 = -v;. }. *pV
ac6b0 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20 alue = (int)v;.
ac6c0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a return 1;.}../*
ac6d0 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c 65 .** The variable
ac6e0 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 -length integer
ac6f0 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20 66 encoding is as f
ac700 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 ollows:.**.** KE
ac710 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20 Y:.** A
ac720 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 20 = 0xxxxxxx 7
ac730 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 bits of data and
ac740 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a one flag bit.**
ac750 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78 B = 1xx
ac760 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20 xxxxx 7 bits
ac770 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 of data and one
ac780 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 flag bit.**
ac790 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78 78 C = xxxxxxxx
ac7a0 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64 61 8 bits of da
ac7b0 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73 ta.**.** 7 bits
ac7c0 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20 - A.** 14 bits
ac7d0 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20 - BA.** 21 bits
ac7e0 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 73 - BBA.** 28 bits
ac7f0 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 69 - BBBA.** 35 bi
ac800 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32 ts - BBBBA.** 42
ac810 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2a bits - BBBBBA.*
ac820 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42 42 * 49 bits - BBBB
ac830 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d BBA.** 56 bits -
ac840 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 20 BBBBBBBA.** 64
ac850 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42 43 bits - BBBBBBBBC
ac860 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 .*/../*.** Write
ac870 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 a 64-bit variab
ac880 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le-length intege
ac890 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 r to memory star
ac8a0 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a ting at p[0]..**
ac8b0 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 The length of d
ac8c0 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20 62 ata write will b
ac8d0 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 e between 1 and
ac8e0 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e 75 9 bytes. The nu
ac8f0 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 mber.** of bytes
ac900 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74 75 written is retu
ac910 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 rned..**.** A va
ac920 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e riable-length in
ac930 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f teger consists o
ac940 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 f the lower 7 bi
ac950 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65 0a ts of each byte.
ac960 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73 ** for all bytes
ac970 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20 38 that have the 8
ac980 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20 6f th bit set and o
ac990 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68 65 ne byte with the
ac9a0 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61 8th.** bit clea
ac9b0 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 77 r. Except, if w
ac9c0 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74 68 e get to the 9th
ac9d0 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65 73 byte, it stores
ac9e0 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 the full.** 8 b
ac9f0 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20 6c its and is the l
aca00 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c ast byte..*/.SQL
aca10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
aca20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
aca30 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
aca40 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 p, u64 v){. int
aca50 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 i, j, n;. u8 b
aca60 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 20 uf[10];. if( v
aca70 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30 30 & (((u64)0xff000
aca80 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20 000)<<32) ){.
aca90 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0a 20 p[8] = (u8)v;.
acaa0 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 v >>= 8;.
acab0 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 for(i=7; i>=0; i
acac0 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20 --){. p[i]
acad0 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37 66 = (u8)((v & 0x7f
acae0 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 20 ) | 0x80);.
acaf0 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d 0a v >>= 7;. }.
acb00 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a 20 20 return 9;.
acb10 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b 0a 20 } . n = 0;.
acb20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e 2b 2b do{. buf[n++
acb30 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 ] = (u8)((v & 0x
acb40 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 7f) | 0x80);.
acb50 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 v >>= 7;. }whi
acb60 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 75 le( v!=0 );. bu
acb70 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20 f[0] &= 0x7f;.
acb80 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a assert( n<=9 );.
acb90 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 for(i=0, j=n-1
acba0 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b ; j>=0; j--, i++
acbb0 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 75 ){. p[i] = bu
acbc0 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 f[j];. }. retu
acbd0 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn n;.}../*.** T
acbe0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
acbf0 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e 20 faster version
acc00 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 of sqlite3PutVar
acc10 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a int() that only.
acc20 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d ** works for 32-
acc30 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e 74 bit positive int
acc40 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68 20 egers and which
acc50 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 is optimized for
acc60 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 .** the common c
acc70 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74 ase of small int
acc80 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f 20 egers. A MACRO
acc90 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 69 version, putVari
acca0 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76 nt32,.** is prov
accb0 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e ided which inlin
accc0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 es the single-by
accd0 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f te case. All co
acce0 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a de should use.**
accf0 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69 the MACRO versi
acd00 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63 74 on as this funct
acd10 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 ion assumes the
acd20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 single-byte case
acd30 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 has.** already
acd40 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f been handled..*/
acd50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acd60 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 int sqlite3PutVa
acd70 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 rint32(unsigned
acd80 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b char *p, u32 v){
acd90 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72 69 .#ifndef putVari
acda0 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26 20 nt32. if( (v &
acdb0 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20 ~0x7f)==0 ){.
acdc0 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72 p[0] = v;. r
acdd0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e eturn 1;. }.#en
acde0 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20 7e dif. if( (v & ~
acdf0 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20 0x3fff)==0 ){.
ace00 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 28 76 p[0] = (u8)((v
ace10 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 >>7) | 0x80);.
ace20 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 20 p[1] = (u8)(v
ace30 26 20 30 78 37 66 29 3b 0a 20 20 20 20 72 65 74 & 0x7f);. ret
ace40 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 65 74 urn 2;. }. ret
ace50 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 56 61 urn sqlite3PutVa
ace60 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a 2f rint(p, v);.}../
ace70 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d 62 *.** Read a 64-b
ace80 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 it variable-leng
ace90 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 th integer from
acea0 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 memory starting
aceb0 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 at p[0]..** Retu
acec0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
aced0 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 bytes read. Th
acee0 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 e value is store
acef0 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 d in *v..*/.SQLI
acf00 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 TE_PRIVATE u8 sq
acf10 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 63 lite3GetVarint(c
acf20 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
acf30 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b 0a ar *p, u64 *v){.
acf40 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 20 u32 a,b,s;..
acf50 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 a = *p;. /* a:
acf60 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p0 (unmasked) */
acf70 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 . if (!(a&0x80)
acf80 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 61 ). {. *v = a
acf90 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
acfa0 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 }.. p++;. b
acfb0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 = *p;. /* b: p1
acfc0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
acfd0 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a if (!(b&0x80)).
acfe0 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 37 {. a &= 0x7
acff0 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b f;. a = a<<7;
ad000 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
ad010 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 *v = a;. ret
ad020 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b urn 2;. }.. p+
ad030 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a +;. a = a<<14;.
ad040 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 a |= *p;. /*
ad050 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 a: p0<<14 | p2 (
ad060 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
ad070 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
ad080 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 {. a &= (0x7f
ad090 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
ad0a0 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 b &= 0x7f;.
ad0b0 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 b = b<<7;. a
ad0c0 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 |= b;. *v =
ad0d0 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b a;. return 3;
ad0e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 20 . }.. /* CSE1
ad0f0 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 from below */.
ad100 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
ad110 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 (0x7f);. p++;.
ad120 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 b = b<<14;. b
ad130 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 |= *p;. /* b: p
ad140 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 1<<14 | p3 (unma
ad150 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
ad160 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (b&0x80)). {.
ad170 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 b &= (0x7f<<14
ad180 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a )|(0x7f);. /*
ad190 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a moved CSE1 up *
ad1a0 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 /. /* a &= (0
ad1b0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
ad1c0 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 */. a = a<<7
ad1d0 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
ad1e0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
ad1f0 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f turn 4;. }.. /
ad200 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 * a: p0<<14 | p2
ad210 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f (masked) */. /
ad220 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 * b: p1<<14 | p3
ad230 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
ad240 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 66 20 70 /* 1:save off p
ad250 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 0<<21 | p1<<14 |
ad260 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 p2<<7 | p3 (mas
ad270 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 ked) */. /* mov
ad280 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20 ed CSE1 up */.
ad290 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 /* a &= (0x7f<<1
ad2a0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 4)|(0x7f); */.
ad2b0 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c b &= (0x7f<<14)|
ad2c0 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61 3b (0x7f);. s = a;
ad2d0 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 31 34 20 . /* s: p0<<14
ad2e0 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f | p2 (masked) */
ad2f0 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 .. p++;. a = a
ad300 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b <<14;. a |= *p;
ad310 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 . /* a: p0<<28
ad320 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 | p2<<14 | p4 (u
ad330 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
ad340 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b (!(a&0x80)). {
ad350 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 . /* we can s
ad360 6b 69 70 20 74 68 65 73 65 20 63 61 75 73 65 20 kip these cause
ad370 74 68 65 79 20 77 65 72 65 20 28 65 66 66 65 63 they were (effec
ad380 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f tively) done abo
ad390 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 ve in calc'ing s
ad3a0 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 */. /* a &=
ad3b0 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 (0x7f<<28)|(0x7f
ad3c0 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f <<14)|(0x7f); */
ad3d0 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 . /* b &= (0x
ad3e0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 7f<<14)|(0x7f);
ad3f0 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b */. b = b<<7;
ad400 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
ad410 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 s = s>>18;.
ad420 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 *v = ((u64)s)<<3
ad430 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 2 | a;. retur
ad440 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 32 n 5;. }.. /* 2
ad450 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 :save off p0<<21
ad460 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c | p1<<14 | p2<<
ad470 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 7 | p3 (masked)
ad480 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a 20 */. s = s<<7;.
ad490 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73 3a s |= b;. /* s:
ad4a0 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 p0<<21 | p1<<14
ad4b0 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d | p2<<7 | p3 (m
ad4c0 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b asked) */.. p++
ad4d0 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 ;. b = b<<14;.
ad4e0 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b |= *p;. /* b
ad4f0 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c 31 : p1<<28 | p3<<1
ad500 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65 64 4 | p5 (unmasked
ad510 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 ) */. if (!(b&0
ad520 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a x80)). {. /*
ad530 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 69 we can skip thi
ad540 73 20 63 61 75 73 65 20 69 74 20 77 61 73 20 28 s cause it was (
ad550 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e effectively) don
ad560 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 e above in calc'
ad570 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 ing s */. /*
ad580 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c b &= (0x7f<<28)|
ad590 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
ad5a0 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 ); */. a &= (
ad5b0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
ad5c0 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a ;. a = a<<7;.
ad5d0 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 a |= b;.
ad5e0 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a s = s>>18;. *
ad5f0 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 v = ((u64)s)<<32
ad600 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e | a;. return
ad610 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 6;. }.. p++;.
ad620 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 a = a<<14;. a
ad630 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 |= *p;. /* a:
ad640 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c 31 34 20 p2<<28 | p4<<14
ad650 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p6 (unmasked)
ad660 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 */. if (!(a&0x8
ad670 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 0)). {. a &=
ad680 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 (0x1f<<28)|(0x7
ad690 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
ad6a0 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 b &= (0x7f<<1
ad6b0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
ad6c0 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = b<<7;. a |
ad6d0 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e = b;. s = s>>
ad6e0 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 11;. *v = ((u
ad6f0 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 64)s)<<32 | a;.
ad700 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d return 7;. }
ad710 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f 6d .. /* CSE2 from
ad720 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d below */. a &=
ad730 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
ad740 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d f);. p++;. b =
ad750 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a b<<14;. b |= *
ad760 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 3c 3c 32 p;. /* b: p3<<2
ad770 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 70 37 20 8 | p5<<14 | p7
ad780 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
ad790 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 if (!(b&0x80)).
ad7a0 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 31 {. b &= (0x1
ad7b0 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 f<<28)|(0x7f<<14
ad7c0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a )|(0x7f);. /*
ad7d0 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a moved CSE2 up *
ad7e0 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 /. /* a &= (0
ad7f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
ad800 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 */. a = a<<7
ad810 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
ad820 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 20 20 20 s = s>>4;.
ad830 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 *v = ((u64)s)<<3
ad840 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 2 | a;. retur
ad850 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b n 8;. }.. p++;
ad860 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0a 20 20 . a = a<<15;.
ad870 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a |= *p;. /* a:
ad880 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31 35 p4<<29 | p6<<15
ad890 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64 29 | p8 (unmasked)
ad8a0 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 */.. /* moved
ad8b0 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 CSE2 up */. /*
ad8c0 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 39 29 7c a &= (0x7f<<29)|
ad8d0 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 78 66 66 (0x7f<<15)|(0xff
ad8e0 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 ); */. b &= (0x
ad8f0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
ad900 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 20 61 20 b = b<<8;. a
ad910 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 73 3c 3c |= b;.. s = s<<
ad920 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b 0a 4;. b = p[-4];.
ad930 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 62 b &= 0x7f;. b
ad940 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d 20 = b>>3;. s |=
ad950 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 75 36 34 b;.. *v = ((u64
ad960 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20 20 )s)<<32 | a;..
ad970 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a return 9;.}../*.
ad980 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 ** Read a 32-bit
ad990 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 variable-length
ad9a0 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 integer from me
ad9b0 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 mory starting at
ad9c0 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e p[0]..** Return
ad9d0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
ad9e0 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65 20 ytes read. The
ad9f0 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 value is stored
ada00 69 6e 20 2a 76 2e 0a 2a 2a 20 41 20 4d 41 43 52 in *v..** A MACR
ada10 4f 20 76 65 72 73 69 6f 6e 2c 20 67 65 74 56 61 O version, getVa
ada20 72 69 6e 74 33 32 2c 20 69 73 20 70 72 6f 76 69 rint32, is provi
ada30 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 ded which inline
ada40 73 20 74 68 65 20 0a 2a 2a 20 73 69 6e 67 6c 65 s the .** single
ada50 2d 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c -byte case. All
ada60 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 code should use
ada70 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69 the MACRO versi
ada80 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 69 73 20 66 on as .** this f
ada90 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 unction assumes
adaa0 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 the single-byte
adab0 63 61 73 65 20 68 61 73 20 61 6c 72 65 61 64 79 case has already
adac0 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a been handled..*
adad0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
adae0 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 u8 sqlite3GetVa
adaf0 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 rint32(const uns
adb00 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 igned char *p, u
adb10 33 32 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 32 *v){. u32 a,
adb20 62 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 b;.. a = *p;.
adb30 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b /* a: p0 (unmask
adb40 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 ed) */.#ifndef g
adb50 65 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 20 etVarint32. if
adb60 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(a&0x80)). {.
adb70 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 *v = a;.
adb80 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 return 1;. }.#e
adb90 6e 64 69 66 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 ndif.. p++;. b
adba0 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 = *p;. /* b: p
adbb0 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 1 (unmasked) */.
adbc0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
adbd0 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 . {. a &= 0x
adbe0 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 7f;. a = a<<7
adbf0 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 ;. *v = a | b
adc00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a ;. return 2;.
adc10 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 }.. p++;. a
adc20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 = a<<14;. a |=
adc30 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c *p;. /* a: p0<<
adc40 31 34 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 14 | p2 (unmaske
adc50 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 d) */. if (!(a&
adc60 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 0x80)). {. a
adc70 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
adc80 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 0x7f);. b &=
adc90 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 0x7f;. b = b<
adca0 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c <7;. *v = a |
adcb0 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 b;. return 3
adcc0 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 ;. }.. p++;.
adcd0 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c b = b<<14;. b |
adce0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 = *p;. /* b: p1
adcf0 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 <<14 | p3 (unmas
add00 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
add10 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 b&0x80)). {.
add20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 b &= (0x7f<<14)
add30 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 26 |(0x7f);. a &
add40 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
add50 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 7f);. a = a<<
add60 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 7;. *v = a |
add70 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b b;. return 4;
add80 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 . }.. p++;. a
add90 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d = a<<14;. a |=
adda0 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c *p;. /* a: p0<
addb0 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 <28 | p2<<14 | p
addc0 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 4 (unmasked) */.
addd0 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 if (!(a&0x80))
adde0 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 . {. a &= (0
addf0 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c x1f<<28)|(0x7f<<
ade00 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
ade10 62 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c b &= (0x1f<<28)|
ade20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
ade30 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b );. b = b<<7;
ade40 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b . *v = a | b;
ade50 0a 20 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 . return 5;.
ade60 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20 }.. /* We can
ade70 6f 6e 6c 79 20 72 65 61 63 68 20 74 68 69 73 20 only reach this
ade80 70 6f 69 6e 74 20 77 68 65 6e 20 72 65 61 64 69 point when readi
ade90 6e 67 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 ng a corrupt dat
adea0 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e abase. ** file.
adeb0 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 77 In that case w
adec0 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79 e are not in any
aded0 20 68 75 72 72 79 2e 20 20 55 73 65 20 74 68 65 hurry. Use the
adee0 20 28 72 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a (relatively. *
adef0 2a 20 73 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d * slow) general-
adf00 70 75 72 70 6f 73 65 20 73 71 6c 69 74 65 33 47 purpose sqlite3G
adf10 65 74 56 61 72 69 6e 74 28 29 20 72 6f 75 74 69 etVarint() routi
adf20 6e 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 ne to extract th
adf30 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f e. ** value. */
adf40 0a 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 . {. u64 v64
adf50 3b 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 ;. u8 n;..
adf60 20 70 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d p -= 4;. n =
adf70 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
adf80 74 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 t(p, &v64);.
adf90 61 73 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e assert( n>5 && n
adfa0 3c 3d 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 <=9 );. *v =
adfb0 28 75 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 (u32)v64;. re
adfc0 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn n;. }.}../
adfd0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
adfe0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
adff0 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65 that will be nee
ae000 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ded to store the
ae010 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 given.** 64-bit
ae020 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c integer..*/.SQL
ae030 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
ae040 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e sqlite3VarintLen
ae050 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 (u64 v){. int i
ae060 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 = 0;. do{.
ae070 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 i++;. v >>= 7
ae080 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 ;. }while( v!=0
ae090 20 26 26 20 69 3c 39 20 29 3b 0a 20 20 72 65 74 && i<9 );. ret
ae0a0 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn i;.}.../*.**
ae0b0 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 Read or write a
ae0c0 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 four-byte big-e
ae0d0 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 ndian integer va
ae0e0 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 lue..*/.SQLITE_P
ae0f0 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
ae100 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 e3Get4byte(const
ae110 20 75 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 u8 *p){. retur
ae120 6e 20 28 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 n (p[0]<<24) | (
ae130 70 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32 p[1]<<16) | (p[2
ae140 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a ]<<8) | p[3];.}.
ae150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
ae160 6f 69 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 oid sqlite3Put4b
ae170 79 74 65 28 75 6e 73 69 67 6e 65 64 20 63 68 61 yte(unsigned cha
ae180 72 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 20 20 r *p, u32 v){.
ae190 70 5b 30 5d 20 3d 20 28 75 38 29 28 76 3e 3e 32 p[0] = (u8)(v>>2
ae1a0 34 29 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 75 38 4);. p[1] = (u8
ae1b0 29 28 76 3e 3e 31 36 29 3b 0a 20 20 70 5b 32 5d )(v>>16);. p[2]
ae1c0 20 3d 20 28 75 38 29 28 76 3e 3e 38 29 3b 0a 20 = (u8)(v>>8);.
ae1d0 20 70 5b 33 5d 20 3d 20 28 75 38 29 76 3b 0a 7d p[3] = (u8)v;.}
ae1e0 0a 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 ....#if !defined
ae1f0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f (SQLITE_OMIT_BLO
ae200 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 B_LITERAL) || de
ae210 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 fined(SQLITE_HAS
ae220 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 _CODEC)./*.** Tr
ae230 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 anslate a single
ae240 20 62 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 byte of Hex int
ae250 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a o an integer..**
ae260 20 54 68 69 73 20 72 6f 75 74 69 6e 65 6e 20 6f This routinen o
ae270 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 68 20 72 nly works if h r
ae280 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 eally is a valid
ae290 20 68 65 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 hexadecimal.**
ae2a0 63 68 61 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 character: 0..9
ae2b0 61 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 a..fA..F.*/.stat
ae2c0 69 63 20 75 38 20 68 65 78 54 6f 49 6e 74 28 69 ic u8 hexToInt(i
ae2d0 6e 74 20 68 29 7b 0a 20 20 61 73 73 65 72 74 28 nt h){. assert(
ae2e0 20 28 68 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 (h>='0' && h<='
ae2f0 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20 9') || (h>='a'
ae300 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28 && h<='f') || (
ae310 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d 27 46 27 h>='A' && h<='F'
ae320 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 ) );.#ifdef SQLI
ae330 54 45 5f 41 53 43 49 49 0a 20 20 68 20 2b 3d 20 TE_ASCII. h +=
ae340 39 2a 28 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65 9*(1&(h>>6));.#e
ae350 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
ae360 54 45 5f 45 42 43 44 49 43 0a 20 20 68 20 2b 3d TE_EBCDIC. h +=
ae370 20 39 2a 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 9*(1&~(h>>4));.
ae380 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
ae390 28 75 38 29 28 68 20 26 20 30 78 66 29 3b 0a 7d (u8)(h & 0xf);.}
ae3a0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 .#endif /* !SQLI
ae3b0 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 TE_OMIT_BLOB_LIT
ae3c0 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 ERAL || SQLITE_H
ae3d0 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 AS_CODEC */..#if
ae3e0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
ae3f0 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 _OMIT_BLOB_LITER
ae400 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 AL) || defined(S
ae410 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 QLITE_HAS_CODEC)
ae420 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
ae430 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c 20 6f 66 BLOB literal of
ae440 20 74 68 65 20 66 6f 72 6d 20 22 78 27 68 68 68 the form "x'hhh
ae450 68 68 68 27 22 20 69 6e 74 6f 20 69 74 73 20 62 hhh'" into its b
ae460 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 inary.** value.
ae470 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
ae480 72 20 74 6f 20 69 74 73 20 62 69 6e 61 72 79 20 r to its binary
ae490 76 61 6c 75 65 2e 20 20 53 70 61 63 65 20 74 6f value. Space to
ae4a0 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 62 69 6e hold the.** bin
ae4b0 61 72 79 20 76 61 6c 75 65 20 68 61 73 20 62 65 ary value has be
ae4c0 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d en obtained from
ae4d0 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 malloc and must
ae4e0 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 be freed by.**
ae4f0 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 the calling rout
ae500 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ine..*/.SQLITE_P
ae510 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
ae520 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 ite3HexToBlob(sq
ae530 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
ae540 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 char *z, int n)
ae550 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b {. char *zBlob;
ae560 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c . int i;.. zBl
ae570 6f 62 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c ob = (char *)sql
ae580 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
ae590 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 db, n/2 + 1);.
ae5a0 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42 6c 6f 62 n--;. if( zBlob
ae5b0 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ){. for(i=0;
ae5c0 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 i<n; i+=2){.
ae5d0 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 zBlob[i/2] =
ae5e0 28 68 65 78 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c (hexToInt(z[i])<
ae5f0 3c 34 29 20 7c 20 68 65 78 54 6f 49 6e 74 28 7a <4) | hexToInt(z
ae600 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 [i+1]);. }.
ae610 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 zBlob[i/2] = 0
ae620 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
ae630 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f Blob;.}.#endif /
ae640 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 * !SQLITE_OMIT_B
ae650 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 LOB_LITERAL || S
ae660 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 QLITE_HAS_CODEC
ae670 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 */.../*.** Chang
ae680 65 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 e the sqlite.mag
ae690 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d ic from SQLITE_M
ae6a0 41 47 49 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c AGIC_OPEN to SQL
ae6b0 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a ITE_MAGIC_BUSY..
ae6c0 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 ** Return an err
ae6d0 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 or (non-zero) if
ae6e0 20 74 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e the magic was n
ae6f0 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f ot SQLITE_MAGIC_
ae700 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 OPEN.** when thi
ae710 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
ae720 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 led..**.** This
ae730 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
ae740 64 20 77 68 65 6e 20 65 6e 74 65 72 69 6e 67 20 d when entering
ae750 61 6e 20 53 51 4c 69 74 65 20 41 50 49 2e 20 20 an SQLite API.
ae760 54 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 The SQLITE_MAGIC
ae770 5f 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 _OPEN.** value i
ae780 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 ndicates that th
ae790 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
ae7a0 63 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 ction passed int
ae7b0 6f 20 74 68 65 20 41 50 49 20 69 73 0a 2a 2a 20 o the API is.**
ae7c0 6f 70 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 open and is not
ae7d0 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 61 6e being used by an
ae7e0 6f 74 68 65 72 20 74 68 72 65 61 64 2e 20 20 42 other thread. B
ae7f0 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 76 y changing the v
ae800 61 6c 75 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 alue.** to SQLIT
ae810 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 77 65 20 E_MAGIC_BUSY we
ae820 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 indicate that th
ae830 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
ae840 69 6e 20 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 in use..** sqlit
ae850 65 33 53 61 66 65 74 79 4f 66 66 28 29 20 62 65 e3SafetyOff() be
ae860 6c 6f 77 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 low will change
ae870 74 68 65 20 76 61 6c 75 65 20 62 61 63 6b 20 74 the value back t
ae880 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f o SQLITE_MAGIC_O
ae890 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 PEN.** when the
ae8a0 41 50 49 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a API exits. .**.*
ae8b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
ae8c0 73 20 61 20 61 74 74 65 6d 70 74 20 74 6f 20 64 s a attempt to d
ae8d0 65 74 65 63 74 20 69 66 20 74 77 6f 20 74 68 72 etect if two thr
ae8e0 65 61 64 73 20 75 73 65 20 74 68 65 0a 2a 2a 20 eads use the.**
ae8f0 73 61 6d 65 20 73 71 6c 69 74 65 2a 20 70 6f 69 same sqlite* poi
ae900 6e 74 65 72 20 61 74 20 74 68 65 20 73 61 6d 65 nter at the same
ae910 20 74 69 6d 65 2e 20 20 54 68 65 72 65 20 69 73 time. There is
ae920 20 61 20 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 a race .** cond
ae930 69 74 69 6f 6e 20 73 6f 20 69 74 20 69 73 20 70 ition so it is p
ae940 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 ossible that the
ae950 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 64 65 error is not de
ae960 74 65 63 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75 tected..** But u
ae970 73 75 61 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c sually the probl
ae980 65 6d 20 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e em will be seen.
ae990 20 20 54 68 65 20 72 65 73 75 6c 74 20 77 69 6c The result wil
ae9a0 6c 20 62 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 l be an.** error
ae9b0 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 which can be us
ae9c0 65 64 20 74 6f 20 64 65 62 75 67 20 74 68 65 20 ed to debug the
ae9d0 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 application that
ae9e0 20 69 73 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c is.** using SQL
ae9f0 69 74 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e ite incorrectly.
aea00 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 .**.** Ticket #2
aea10 30 32 3a 20 20 49 66 20 64 62 2d 3e 6d 61 67 69 02: If db->magi
aea20 63 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 c is not a valid
aea30 20 6f 70 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b open value, tak
aea40 65 20 63 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f e care not.** to
aea50 20 6d 6f 64 69 66 79 20 74 68 65 20 64 62 20 73 modify the db s
aea60 74 72 75 63 74 75 72 65 20 61 74 20 61 6c 6c 2e tructure at all.
aea70 20 20 49 74 20 63 6f 75 6c 64 20 62 65 20 74 68 It could be th
aea80 61 74 20 64 62 20 69 73 20 61 20 73 74 61 6c 65 at db is a stale
aea90 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e .** pointer. In
aeaa0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74 other words, it
aeab0 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74 could be that t
aeac0 68 65 72 65 20 68 61 73 20 62 65 65 6e 20 61 20 here has been a
aead0 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f prior.** call to
aeae0 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 sqlite3_close(d
aeaf0 62 29 20 61 6e 64 20 64 62 20 68 61 73 20 62 65 b) and db has be
aeb00 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 en deallocated.
aeb10 20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f And we do.** no
aeb20 74 20 77 61 6e 74 20 74 6f 20 77 72 69 74 65 20 t want to write
aeb30 69 6e 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64 into deallocated
aeb40 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 memory..*/.#ifd
aeb50 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
aeb60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
aeb70 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 nt sqlite3Safety
aeb80 4f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b On(sqlite3 *db){
aeb90 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 . if( db->magic
aeba0 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f ==SQLITE_MAGIC_O
aebb0 50 45 4e 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d PEN ){. db->m
aebc0 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 agic = SQLITE_MA
aebd0 47 49 43 5f 42 55 53 59 3b 0a 20 20 20 20 61 73 GIC_BUSY;. as
aebe0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
aebf0 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
aec00 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 ex) );. retur
aec10 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 n 0;. }else if(
aec20 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 db->magic==SQLI
aec30 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b TE_MAGIC_BUSY ){
aec40 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d . db->magic =
aec50 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 SQLITE_MAGIC_ER
aec60 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e ROR;. db->u1.
aec70 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 isInterrupted =
aec80 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
aec90 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 1;.}.#endif../*.
aeca0 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6d 61 ** Change the ma
aecb0 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f gic from SQLITE_
aecc0 4d 41 47 49 43 5f 42 55 53 59 20 74 6f 20 53 51 MAGIC_BUSY to SQ
aecd0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e LITE_MAGIC_OPEN.
aece0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 .** Return an er
aecf0 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 ror (non-zero) i
aed00 66 20 74 68 65 20 6d 61 67 69 63 20 77 61 73 20 f the magic was
aed10 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 not SQLITE_MAGIC
aed20 5f 42 55 53 59 0a 2a 2a 20 77 68 65 6e 20 74 68 _BUSY.** when th
aed30 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
aed40 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 lled..*/.#ifdef
aed50 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
aed60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
aed70 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
aed80 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
aed90 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d if( db->magic==
aeda0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
aedb0 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 Y ){. db->mag
aedc0 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 ic = SQLITE_MAGI
aedd0 43 5f 4f 50 45 4e 3b 0a 20 20 20 20 61 73 73 65 C_OPEN;. asse
aede0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
aedf0 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 x_held(db->mutex
aee00 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) );. return
aee10 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
aee20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 db->magic = SQLI
aee30 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a TE_MAGIC_ERROR;.
aee40 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 db->u1.isInt
aee50 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 errupted = 1;.
aee60 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
aee70 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
aee80 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 Check to make su
aee90 72 65 20 77 65 20 68 61 76 65 20 61 20 76 61 6c re we have a val
aeea0 69 64 20 64 62 20 70 6f 69 6e 74 65 72 2e 20 20 id db pointer.
aeeb0 54 68 69 73 20 74 65 73 74 20 69 73 20 6e 6f 74 This test is not
aeec0 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 .** foolproof bu
aeed0 74 20 69 74 20 64 6f 65 73 20 70 72 6f 76 69 64 t it does provid
aeee0 65 20 73 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f e some measure o
aeef0 66 20 70 72 6f 74 65 63 74 69 6f 6e 20 61 67 61 f protection aga
aef00 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f inst.** misuse o
aef10 66 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 f the interface
aef20 73 75 63 68 20 61 73 20 70 61 73 73 69 6e 67 20 such as passing
aef30 69 6e 20 64 62 20 70 6f 69 6e 74 65 72 73 20 74 in db pointers t
aef40 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 hat are.** NULL
aef50 6f 72 20 77 68 69 63 68 20 68 61 76 65 20 62 65 or which have be
aef60 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c en previously cl
aef70 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 72 osed. If this r
aef80 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a outine returns.*
aef90 2a 20 31 20 69 74 20 6d 65 61 6e 73 20 74 68 61 * 1 it means tha
aefa0 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72 t the db pointer
aefb0 20 69 73 20 76 61 6c 69 64 20 61 6e 64 20 30 20 is valid and 0
aefc0 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 if it should not
aefd0 20 62 65 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e be.** dereferen
aefe0 63 65 64 20 66 6f 72 20 61 6e 79 20 72 65 61 73 ced for any reas
aeff0 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 on. The calling
af000 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
af010 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 invoke.** SQLIT
af020 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65 64 69 61 E_MISUSE immedia
af030 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 tely..**.** sqli
af040 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b te3SafetyCheckOk
af050 28 29 20 72 65 71 75 69 72 65 73 20 74 68 61 74 () requires that
af060 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 the db pointer
af070 62 65 20 76 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 be valid for.**
af080 75 73 65 2e 20 20 73 71 6c 69 74 65 33 53 61 66 use. sqlite3Saf
af090 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b etyCheckSickOrOk
af0a0 28 29 20 61 6c 6c 6f 77 73 20 61 20 64 62 20 70 () allows a db p
af0b0 6f 69 6e 74 65 72 20 74 68 61 74 20 66 61 69 6c ointer that fail
af0c0 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 ed to.** open pr
af0d0 6f 70 65 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f operly and is no
af0e0 74 20 66 69 74 20 66 6f 72 20 67 65 6e 65 72 61 t fit for genera
af0f0 6c 20 75 73 65 20 62 75 74 20 77 68 69 63 68 20 l use but which
af100 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20 61 can be.** used a
af110 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f s an argument to
af120 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 sqlite3_errmsg(
af130 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f ) or sqlite3_clo
af140 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f se()..*/.SQLITE_
af150 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
af160 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b te3SafetyCheckOk
af170 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
af180 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 69 66 u32 magic;. if
af190 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72 6e ( db==0 ) return
af1a0 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 0;. magic = db
af1b0 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 6d ->magic;. if( m
af1c0 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 agic!=SQLITE_MAG
af1d0 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 IC_OPEN &&.
af1e0 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d magic!=SQLITE_M
af1f0 41 47 49 43 5f 42 55 53 59 20 29 20 72 65 74 75 AGIC_BUSY ) retu
af200 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 rn 0;. return 1
af210 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
af220 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 TE int sqlite3Sa
af230 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f fetyCheckSickOrO
af240 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a k(sqlite3 *db){.
af250 20 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 69 u32 magic;. i
af260 66 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72 f( db==0 ) retur
af270 6e 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 n 0;. magic = d
af280 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 b->magic;. if(
af290 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 magic!=SQLITE_MA
af2a0 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 20 GIC_SICK &&.
af2b0 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f magic!=SQLITE_
af2c0 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 MAGIC_OPEN &&.
af2d0 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 magic!=SQLIT
af2e0 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72 E_MAGIC_BUSY ) r
af2f0 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 eturn 0;. retur
af300 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a n 1;.}../*******
af310 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 ******* End of u
af320 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a til.c **********
af330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af350 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
af360 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
af370 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a le hash.c ******
af380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af3a0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
af3b0 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 01 September 22.
af3c0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
af3d0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
af3e0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
af3f0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
af400 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
af410 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
af420 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
af430 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
af440 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
af450 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
af460 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
af470 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
af480 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
af490 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
af4a0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
af4b0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
af4c0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
af4d0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
af4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
af520 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d * This is the im
af530 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
af540 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 generic hash-tab
af550 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 les.** used in S
af560 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 QLite..**.** $Id
af570 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 33 20 : hash.c,v 1.33
af580 32 30 30 39 2f 30 31 2f 30 39 20 30 31 3a 31 32 2009/01/09 01:12
af590 3a 32 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :28 drh Exp $.*/
af5a0 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d ../* Turn bulk m
af5b0 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 emory into a has
af5c0 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 h table object b
af5d0 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 y initializing t
af5e0 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 he.** fields of
af5f0 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 the Hash structu
af600 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 re..**.** "pNew"
af610 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
af620 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
af630 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e that is to be in
af640 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 22 63 itialized..** "c
af650 6f 70 79 4b 65 79 22 20 69 73 20 74 72 75 65 20 opyKey" is true
af660 69 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c if the hash tabl
af670 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 e should make it
af680 73 20 6f 77 6e 20 70 72 69 76 61 74 65 0a 2a 2a s own private.**
af690 20 63 6f 70 79 20 6f 66 20 6b 65 79 73 20 61 6e copy of keys an
af6a0 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 73 68 d false if it sh
af6b0 6f 75 6c 64 20 6a 75 73 74 20 75 73 65 20 74 68 ould just use th
af6c0 65 20 73 75 70 70 6c 69 65 64 20 70 6f 69 6e 74 e supplied point
af6d0 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
af6e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
af6f0 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 20 e3HashInit(Hash
af700 2a 70 4e 65 77 2c 20 69 6e 74 20 63 6f 70 79 4b *pNew, int copyK
af710 65 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 ey){. assert( p
af720 4e 65 77 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 New!=0 );. pNew
af730 2d 3e 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 ->copyKey = copy
af740 4b 65 79 21 3d 30 3b 0a 20 20 70 4e 65 77 2d 3e Key!=0;. pNew->
af750 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 first = 0;. pNe
af760 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 w->count = 0;.
af770 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 pNew->htsize = 0
af780 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 ;. pNew->ht = 0
af790 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 ;.}../* Remove a
af7a0 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 ll entries from
af7b0 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 a hash table. R
af7c0 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 eclaim all memor
af7d0 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 y..** Call this
af7e0 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 routine to delet
af7f0 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f e a hash table o
af800 72 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 r to reset a has
af810 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 h table.** to th
af820 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a e empty state..*
af830 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
af840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 void sqlite3Has
af850 68 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 hClear(Hash *pH)
af860 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c {. HashElem *el
af870 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 em; /* F
af880 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 or looping over
af890 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 all elements of
af8a0 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 the table */..
af8b0 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b assert( pH!=0 );
af8c0 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 . elem = pH->fi
af8d0 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 rst;. pH->first
af8e0 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f = 0;. sqlite3_
af8f0 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 free(pH->ht);.
af900 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 pH->ht = 0;. pH
af910 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 ->htsize = 0;.
af920 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 while( elem ){.
af930 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 HashElem *nex
af940 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e t_elem = elem->n
af950 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d ext;. if( pH-
af960 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 20 >copyKey ){.
af970 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65 sqlite3_free(e
af980 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 lem->pKey);.
af990 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
af9a0 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c ee(elem);. el
af9b0 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a em = next_elem;.
af9c0 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 }. pH->count
af9d0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 = 0;.}../*.** Ha
af9e0 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f sh and compariso
af9f0 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e n functions when
afa00 20 74 68 65 20 6d 6f 64 65 20 69 73 20 53 51 4c the mode is SQL
afa10 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 0a ITE_HASH_STRING.
afa20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 */.static int st
afa30 72 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 rHash(const void
afa40 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
afa50 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ){. const char
afa60 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 *z = (const char
afa70 20 2a 29 70 4b 65 79 3b 0a 20 20 69 6e 74 20 68 *)pKey;. int h
afa80 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 4b 65 79 = 0;. if( nKey
afa90 3c 3d 30 20 29 20 6e 4b 65 79 20 3d 20 73 71 6c <=0 ) nKey = sql
afaa0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b ite3Strlen30(z);
afab0 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e . while( nKey >
afac0 20 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 0 ){. h = (
afad0 68 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 h<<3) ^ h ^ sqli
afae0 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b te3UpperToLower[
afaf0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a (unsigned char)*
afb00 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d z++];. nKey--
afb10 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 ;. }. return h
afb20 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d & 0x7fffffff;.}
afb30 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 72 43 .static int strC
afb40 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 ompare(const voi
afb50 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 d *pKey1, int n1
afb60 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b , const void *pK
afb70 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 ey2, int n2){.
afb80 69 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 if( n1!=n2 ) ret
afb90 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 urn 1;. return
afba0 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 sqlite3StrNICmp(
afbb0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 (const char*)pKe
afbc0 79 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 y1,(const char*)
afbd0 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 0a 2f pKey2,n1);.}.../
afbe0 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e * Link an elemen
afbf0 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 t into the hash
afc00 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 table.*/.static
afc10 76 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 void insertEleme
afc20 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 nt(. Hash *pH,
afc30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
afc40 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 The complete has
afc50 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 h table */. str
afc60 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c uct _ht *pEntry,
afc70 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 /* The entry
afc80 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 into which pNew
afc90 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a is inserted */.
afca0 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 HashElem *pNew
afcb0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
afcc0 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e element to be in
afcd0 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 serted */.){. H
afce0 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 ashElem *pHead;
afcf0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 /* First e
afd00 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 lement already i
afd10 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 70 48 n pEntry */. pH
afd20 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 ead = pEntry->ch
afd30 61 69 6e 3b 0a 20 20 69 66 28 20 70 48 65 61 64 ain;. if( pHead
afd40 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 ){. pNew->ne
afd50 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 xt = pHead;.
afd60 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 pNew->prev = pHe
afd70 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 ad->prev;. if
afd80 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b ( pHead->prev ){
afd90 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 pHead->prev->ne
afda0 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 xt = pNew; }.
afdb0 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 else
afdc0 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 { pH->first =
afdd0 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 pNew; }. pHea
afde0 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a d->prev = pNew;.
afdf0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 }else{. pNe
afe00 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 w->next = pH->fi
afe10 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d rst;. if( pH-
afe20 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 >first ){ pH->fi
afe30 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 rst->prev = pNew
afe40 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 ; }. pNew->pr
afe50 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e ev = 0;. pH->
afe60 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 first = pNew;.
afe70 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e }. pEntry->coun
afe80 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 63 t++;. pEntry->c
afe90 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a hain = pNew;.}..
afea0 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68 ./* Resize the h
afeb0 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61 ash table so tha
afec0 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e t it cantains "n
afed0 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73 ew_size" buckets
afee0 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20 ..** "new_size"
afef0 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 must be a power
aff00 6f 66 20 32 2e 20 20 54 68 65 20 68 61 73 68 20 of 2. The hash
aff10 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c table might fail
aff20 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20 69 .** to resize i
aff30 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 f sqlite3_malloc
aff40 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 () fails..*/.sta
aff50 74 69 63 20 76 6f 69 64 20 72 65 68 61 73 68 28 tic void rehash(
aff60 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65 Hash *pH, int ne
aff70 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 w_size){. struc
aff80 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 t _ht *new_ht;
aff90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
affa0 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 new hash table
affb0 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 */. HashElem *e
affc0 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b lem, *next_elem;
affd0 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 /* For loopi
affe0 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 ng over existing
afff0 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 69 elements */..#i
b0000 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c fdef SQLITE_MALL
b0010 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 0a 20 20 OC_SOFT_LIMIT.
b0020 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a if( new_size*siz
b0030 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e eof(struct _ht)>
b0040 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f SQLITE_MALLOC_SO
b0050 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 FT_LIMIT ){.
b0060 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54 new_size = SQLIT
b0070 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 E_MALLOC_SOFT_LI
b0080 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 MIT/sizeof(struc
b0090 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66 t _ht);. }. if
b00a0 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e ( new_size==pH->
b00b0 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e 3b htsize ) return;
b00c0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 .#endif.. /* Th
b00d0 65 72 65 20 69 73 20 61 20 63 61 6c 6c 20 74 6f ere is a call to
b00e0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
b00f0 29 20 69 6e 73 69 64 65 20 72 65 68 61 73 68 28 ) inside rehash(
b0100 29 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 20 ). If there is.
b0110 20 2a 2a 20 61 6c 72 65 61 64 79 20 61 6e 20 61 ** already an a
b0120 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 20 70 48 2d llocation at pH-
b0130 3e 68 74 2c 20 74 68 65 6e 20 69 66 20 74 68 69 >ht, then if thi
b0140 73 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 s malloc() fails
b0150 20 69 74 0a 20 20 2a 2a 20 69 73 20 62 65 6e 69 it. ** is beni
b0160 67 6e 20 28 73 69 6e 63 65 20 66 61 69 6c 69 6e gn (since failin
b0170 67 20 74 6f 20 72 65 73 69 7a 65 20 61 20 68 61 g to resize a ha
b0180 73 68 20 74 61 62 6c 65 20 69 73 20 61 20 70 65 sh table is a pe
b0190 72 66 6f 72 6d 61 6e 63 65 0a 20 20 2a 2a 20 68 rformance. ** h
b01a0 69 74 20 6f 6e 6c 79 2c 20 6e 6f 74 20 61 20 66 it only, not a f
b01b0 61 74 61 6c 20 65 72 72 6f 72 29 2e 0a 20 20 2a atal error).. *
b01c0 2f 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 /. if( pH->htsi
b01d0 7a 65 3e 30 20 29 20 73 71 6c 69 74 65 33 42 65 ze>0 ) sqlite3Be
b01e0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
b01f0 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 );. new_ht = (s
b0200 74 72 75 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 truct _ht *)sqli
b0210 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 6e te3MallocZero( n
b0220 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 ew_size*sizeof(s
b0230 74 72 75 63 74 20 5f 68 74 29 20 29 3b 0a 20 20 truct _ht) );.
b0240 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 if( pH->htsize>0
b0250 20 29 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e ) sqlite3EndBen
b0260 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 ignMalloc();..
b0270 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20 if( new_ht==0 )
b0280 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 return;. sqlite
b0290 33 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 3_free(pH->ht);.
b02a0 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 pH->ht = new_h
b02b0 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 t;. pH->htsize
b02c0 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 66 6f = new_size;. fo
b02d0 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74 r(elem=pH->first
b02e0 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65 , pH->first=0; e
b02f0 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74 lem; elem = next
b0300 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e 74 20 _elem){. int
b0310 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d h = strHash(elem
b0320 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b ->pKey, elem->nK
b0330 65 79 29 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d ey) & (new_size-
b0340 31 29 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 1);. next_ele
b0350 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a m = elem->next;.
b0360 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e insertElemen
b0370 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d t(pH, &new_ht[h]
b0380 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a , elem);. }.}..
b0390 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
b03a0 20 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 (for internal u
b03b0 73 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 se only) locates
b03c0 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 an element in a
b03d0 6e 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 n.** hash table
b03e0 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 that matches the
b03f0 20 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 given key. The
b0400 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b hash for this k
b0410 65 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 ey has.** alread
b0420 79 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 y been computed
b0430 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 73 and is passed as
b0440 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 the 4th paramet
b0450 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61 er..*/.static Ha
b0460 73 68 45 6c 65 6d 20 2a 66 69 6e 64 45 6c 65 6d shElem *findElem
b0470 65 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 entGivenHash(.
b0480 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 const Hash *pH,
b0490 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 74 6f /* The pH to
b04a0 20 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a be searched */.
b04b0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b const void *pK
b04c0 65 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 ey, /* The key
b04d0 20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e we are searchin
b04e0 67 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e g for */. int n
b04f0 4b 65 79 2c 0a 20 20 69 6e 74 20 68 20 20 20 20 Key,. int h
b0500 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
b0510 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 e hash for this
b0520 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 key. */.){. Has
b0530 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 hElem *elem;
b0540 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
b0550 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 sed to loop thru
b0560 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 the element lis
b0570 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 t */. int count
b0580 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b0590 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
b05a0 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74 of elements left
b05b0 20 74 6f 20 74 65 73 74 20 2a 2f 0a 0a 20 20 69 to test */.. i
b05c0 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 f( pH->ht ){.
b05d0 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e struct _ht *pEn
b05e0 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d try = &pH->ht[h]
b05f0 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e ;. elem = pEn
b0600 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20 try->chain;.
b0610 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e count = pEntry->
b0620 63 6f 75 6e 74 3b 0a 20 20 20 20 77 68 69 6c 65 count;. while
b0630 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65 ( count-- && ele
b0640 6d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 m ){. if( s
b0650 74 72 43 6f 6d 70 61 72 65 28 65 6c 65 6d 2d 3e trCompare(elem->
b0660 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c pKey,elem->nKey,
b0670 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b pKey,nKey)==0 ){
b0680 20 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e . return
b0690 20 65 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 elem;. }.
b06a0 20 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d elem = elem
b06b0 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 ->next;. }.
b06c0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
b06d0 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e ./* Remove a sin
b06e0 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 gle entry from t
b06f0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 he hash table gi
b0700 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ven a pointer to
b0710 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 that.** element
b0720 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74 and a hash on t
b0730 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 he element's key
b0740 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
b0750 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 removeElementGi
b0760 76 65 6e 48 61 73 68 28 0a 20 20 48 61 73 68 20 venHash(. Hash
b0770 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 *pH, /*
b0780 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e The pH containin
b0790 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 48 61 g "elem" */. Ha
b07a0 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20 shElem* elem,
b07b0 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 /* The element t
b07c0 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f o be removed fro
b07d0 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e m the pH */. in
b07e0 74 20 68 20 20 20 20 20 20 20 20 20 20 20 20 20 t h
b07f0 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f /* Hash value fo
b0800 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f r the element */
b0810 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 68 74 .){. struct _ht
b0820 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 *pEntry;. if(
b0830 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 elem->prev ){.
b0840 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 elem->prev->ne
b0850 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b xt = elem->next;
b0860 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 . }else{. p
b0870 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d H->first = elem-
b0880 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 >next;. }. if(
b0890 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 elem->next ){.
b08a0 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 elem->next->p
b08b0 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 rev = elem->prev
b08c0 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d ;. }. pEntry =
b08d0 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 &pH->ht[h];. i
b08e0 66 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e f( pEntry->chain
b08f0 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45 ==elem ){. pE
b0900 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c ntry->chain = el
b0910 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 em->next;. }.
b0920 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b pEntry->count--;
b0930 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 . if( pEntry->c
b0940 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70 ount<=0 ){. p
b0950 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 Entry->chain = 0
b0960 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e ;. }. if( pH->
b0970 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 copyKey ){. s
b0980 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d qlite3_free(elem
b0990 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 73 ->pKey);. }. s
b09a0 71 6c 69 74 65 33 5f 66 72 65 65 28 20 65 6c 65 qlite3_free( ele
b09b0 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 m );. pH->count
b09c0 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f --;. if( pH->co
b09d0 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 unt<=0 ){. as
b09e0 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73 74 3d sert( pH->first=
b09f0 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
b0a00 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 ( pH->count==0 )
b0a10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 ;. sqlite3Has
b0a20 68 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a hClear(pH);. }.
b0a30 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f }../* Attempt to
b0a40 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 locate an eleme
b0a50 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 nt of the hash t
b0a60 61 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b able pH with a k
b0a70 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 ey.** that match
b0a80 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 es pKey,nKey. R
b0a90 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
b0aa0 74 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e to the correspon
b0ab0 64 69 6e 67 20 0a 2a 2a 20 48 61 73 68 45 6c 65 ding .** HashEle
b0ac0 6d 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 m structure for
b0ad0 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 this element if
b0ae0 69 74 20 69 73 20 66 6f 75 6e 64 2c 20 6f 72 20 it is found, or
b0af0 4e 55 4c 4c 0a 2a 2a 20 6f 74 68 65 72 77 69 73 NULL.** otherwis
b0b00 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
b0b10 56 41 54 45 20 48 61 73 68 45 6c 65 6d 20 2a 73 VATE HashElem *s
b0b20 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 45 6c qlite3HashFindEl
b0b30 65 6d 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 em(const Hash *p
b0b40 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 H, const void *p
b0b50 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a Key, int nKey){.
b0b60 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 int h;
b0b70 20 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20 6f /* A hash o
b0b80 6e 20 6b 65 79 20 2a 2f 0a 20 20 48 61 73 68 45 n key */. HashE
b0b90 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a lem *elem; /*
b0ba0 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 The element tha
b0bb0 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f t matches key */
b0bc0 0a 0a 20 20 69 66 28 20 70 48 3d 3d 30 20 7c 7c .. if( pH==0 ||
b0bd0 20 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65 74 pH->ht==0 ) ret
b0be0 75 72 6e 20 30 3b 0a 20 20 68 20 3d 20 73 74 72 urn 0;. h = str
b0bf0 48 61 73 68 28 70 4b 65 79 2c 6e 4b 65 79 29 3b Hash(pKey,nKey);
b0c00 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c . elem = findEl
b0c10 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 ementGivenHash(p
b0c20 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 25 H,pKey,nKey, h %
b0c30 20 70 48 2d 3e 68 74 73 69 7a 65 29 3b 0a 20 20 pH->htsize);.
b0c40 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 7d 0a 0a return elem;.}..
b0c50 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f /* Attempt to lo
b0c60 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 cate an element
b0c70 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c of the hash tabl
b0c80 65 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a e pH with a key.
b0c90 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 ** that matches
b0ca0 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 pKey,nKey. Retu
b0cb0 72 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 rn the data for
b0cc0 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 this element if
b0cd0 69 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 it is.** found,
b0ce0 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 or NULL if there
b0cf0 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f is no match..*/
b0d00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0d10 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 void *sqlite3Has
b0d20 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 hFind(const Hash
b0d30 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 *pH, const void
b0d40 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
b0d50 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 ){. HashElem *e
b0d60 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 lem; /* The e
b0d70 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 lement that matc
b0d80 68 65 73 20 6b 65 79 20 2a 2f 0a 20 20 65 6c 65 hes key */. ele
b0d90 6d 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 m = sqlite3HashF
b0da0 69 6e 64 45 6c 65 6d 28 70 48 2c 20 70 4b 65 79 indElem(pH, pKey
b0db0 2c 20 6e 4b 65 79 29 3b 0a 20 20 72 65 74 75 72 , nKey);. retur
b0dc0 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 n elem ? elem->d
b0dd0 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 ata : 0;.}../* I
b0de0 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74 nsert an element
b0df0 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 into the hash t
b0e00 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20 6b 65 able pH. The ke
b0e10 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a y is pKey,nKey.*
b0e20 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 * and the data i
b0e30 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 s "data"..**.**
b0e40 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78 If no element ex
b0e50 69 73 74 73 20 77 69 74 68 20 61 20 6d 61 74 63 ists with a matc
b0e60 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e 20 61 hing key, then a
b0e70 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 new.** element
b0e80 69 73 20 63 72 65 61 74 65 64 2e 20 20 41 20 63 is created. A c
b0e90 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69 opy of the key i
b0ea0 73 20 6d 61 64 65 20 69 66 20 74 68 65 20 63 6f s made if the co
b0eb0 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20 69 73 pyKey.** flag is
b0ec0 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73 20 72 set. NULL is r
b0ed0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 eturned..**.** I
b0ee0 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e f another elemen
b0ef0 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 t already exists
b0f00 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b with the same k
b0f10 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 ey, then the.**
b0f20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 new data replace
b0f30 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 s the old data a
b0f40 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 nd the old data
b0f50 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
b0f60 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 The key is not c
b0f70 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e opied in this in
b0f80 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 stance. If a ma
b0f90 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e lloc fails, then
b0fa0 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 .** the new data
b0fb0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
b0fc0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
b0fd0 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a is unchanged..**
b0fe0 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 .** If the "data
b0ff0 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 " parameter to t
b1000 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
b1010 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a NULL, then the.*
b1020 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 * element corres
b1030 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 ponding to "key"
b1040 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d is removed from
b1050 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e the hash table.
b1060 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b1070 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
b1080 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 HashInsert(Hash
b1090 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 *pH, const void
b10a0 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c *pKey, int nKey,
b10b0 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 void *data){.
b10c0 69 6e 74 20 68 72 61 77 3b 20 20 20 20 20 20 20 int hraw;
b10d0 20 20 20 20 20 20 2f 2a 20 52 61 77 20 68 61 73 /* Raw has
b10e0 68 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b h value of the k
b10f0 65 79 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 ey */. int h;
b1100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
b1110 20 74 68 65 20 68 61 73 68 20 6f 66 20 74 68 65 the hash of the
b1120 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 key modulo hash
b1130 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 table size */.
b1140 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b HashElem *elem;
b1150 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 /* Used t
b1160 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 o loop thru the
b1170 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a element list */.
b1180 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f HashElem *new_
b1190 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65 elem; /* New e
b11a0 6c 65 6d 65 6e 74 20 61 64 64 65 64 20 74 6f 20 lement added to
b11b0 74 68 65 20 70 48 20 2a 2f 0a 0a 20 20 61 73 73 the pH */.. ass
b11c0 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 ert( pH!=0 );.
b11d0 68 72 61 77 20 3d 20 73 74 72 48 61 73 68 28 70 hraw = strHash(p
b11e0 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 69 66 Key, nKey);. if
b11f0 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a ( pH->htsize ){.
b1200 20 20 20 20 68 20 3d 20 68 72 61 77 20 25 20 70 h = hraw % p
b1210 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 65 H->htsize;. e
b1220 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e lem = findElemen
b1230 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 70 4b tGivenHash(pH,pK
b1240 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 20 20 ey,nKey,h);.
b1250 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 if( elem ){.
b1260 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 void *old_data
b1270 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20 = elem->data;.
b1280 20 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30 if( data==0
b1290 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6d 6f ){. remo
b12a0 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 veElementGivenHa
b12b0 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 sh(pH,elem,h);.
b12c0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
b12d0 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d elem->data =
b12e0 20 64 61 74 61 3b 0a 20 20 20 20 20 20 20 20 69 data;. i
b12f0 66 28 20 21 70 48 2d 3e 63 6f 70 79 4b 65 79 20 f( !pH->copyKey
b1300 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 65 ){. ele
b1310 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 20 m->pKey = (void
b1320 2a 29 70 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 *)pKey;.
b1330 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
b1340 28 6e 4b 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 (nKey==elem->nKe
b1350 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 y);. }.
b1360 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 return old_dat
b1370 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 a;. }. }. i
b1380 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 f( data==0 ) ret
b1390 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 urn 0;. new_ele
b13a0 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 m = (HashElem*)s
b13b0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 qlite3Malloc( si
b13c0 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29 zeof(HashElem) )
b13d0 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d ;. if( new_elem
b13e0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 ==0 ) return dat
b13f0 61 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 a;. if( pH->cop
b1400 79 4b 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20 yKey && pKey!=0
b1410 29 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d ){. new_elem-
b1420 3e 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d >pKey = sqlite3M
b1430 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20 alloc( nKey );.
b1440 20 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d if( new_elem-
b1450 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 >pKey==0 ){.
b1460 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6e sqlite3_free(n
b1470 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 ew_elem);.
b1480 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 return data;.
b1490 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76 }. memcpy((v
b14a0 6f 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 oid*)new_elem->p
b14b0 4b 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 Key, pKey, nKey)
b14c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e ;. }else{. n
b14d0 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 ew_elem->pKey =
b14e0 28 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d (void*)pKey;. }
b14f0 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 . new_elem->nKe
b1500 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e y = nKey;. pH->
b1510 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 count++;. if( p
b1520 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a H->htsize==0 ){.
b1530 20 20 20 20 72 65 68 61 73 68 28 70 48 2c 20 31 rehash(pH, 1
b1540 32 38 2f 73 69 7a 65 6f 66 28 70 48 2d 3e 68 74 28/sizeof(pH->ht
b1550 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 70 [0]));. if( p
b1560 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a H->htsize==0 ){.
b1570 20 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e 74 20 pH->count
b1580 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 70 = 0;. if( p
b1590 48 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 H->copyKey ){.
b15a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
b15b0 65 65 28 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 ee(new_elem->pKe
b15c0 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 y);. }.
b15d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6e sqlite3_free(n
b15e0 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 ew_elem);.
b15f0 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 return data;.
b1600 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d }. }. if( pH-
b1610 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68 74 73 >count > pH->hts
b1620 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 68 61 73 ize ){. rehas
b1630 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a h(pH,pH->htsize*
b1640 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 2);. }. assert
b1650 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 ( pH->htsize>0 )
b1660 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 25 20 70 ;. h = hraw % p
b1670 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 69 6e 73 H->htsize;. ins
b1680 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 ertElement(pH, &
b1690 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 pH->ht[h], new_e
b16a0 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d lem);. new_elem
b16b0 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 ->data = data;.
b16c0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
b16d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
b16e0 64 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a 2a 2a d of hash.c ****
b16f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b1720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
b1730 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 gin file opcodes
b1740 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
b1750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b1770 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 Automatically g
b1780 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f enerated. Do no
b1790 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 t edit */./* See
b17a0 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e 61 the mkopcodec.a
b17b0 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 wk script for de
b17c0 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 21 64 tails. */.#if !d
b17d0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
b17e0 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 IT_EXPLAIN) || !
b17f0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 defined(NDEBUG)
b1800 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f || defined(VDBE_
b1810 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 PROFILE) || defi
b1820 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
b1830 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
b1840 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
b1850 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 ite3OpcodeName(i
b1860 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 63 20 63 nt i){. static c
b1870 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 onst char *const
b1880 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22 3f azName[] = { "?
b1890 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 20 2a ",. /* 1 *
b18a0 2f 20 22 56 4e 65 78 74 22 2c 0a 20 20 20 20 20 / "VNext",.
b18b0 2f 2a 20 20 20 32 20 2a 2f 20 22 41 66 66 69 6e /* 2 */ "Affin
b18c0 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 ity",. /*
b18d0 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22 2c 0a 20 3 */ "Column",.
b18e0 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 53 /* 4 */ "S
b18f0 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 etCookie",.
b1900 2f 2a 20 20 20 35 20 2a 2f 20 22 53 65 65 6b 22 /* 5 */ "Seek"
b1910 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36 20 2a 2f ,. /* 6 */
b1920 20 22 53 65 71 75 65 6e 63 65 22 2c 0a 20 20 20 "Sequence",.
b1930 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22 53 61 76 /* 7 */ "Sav
b1940 65 70 6f 69 6e 74 22 2c 0a 20 20 20 20 20 2f 2a epoint",. /*
b1950 20 20 20 38 20 2a 2f 20 22 52 6f 77 4b 65 79 22 8 */ "RowKey"
b1960 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39 20 2a 2f ,. /* 9 */
b1970 20 22 53 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f "SCopy",. /
b1980 2a 20 20 31 30 20 2a 2f 20 22 4f 70 65 6e 57 72 * 10 */ "OpenWr
b1990 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 ite",. /* 1
b19a0 31 20 2a 2f 20 22 49 66 22 2c 0a 20 20 20 20 20 1 */ "If",.
b19b0 2f 2a 20 20 31 32 20 2a 2f 20 22 56 52 6f 77 69 /* 12 */ "VRowi
b19c0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 33 20 d",. /* 13
b19d0 2a 2f 20 22 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 */ "CollSeq",.
b19e0 20 20 20 2f 2a 20 20 31 34 20 2a 2f 20 22 4f 70 /* 14 */ "Op
b19f0 65 6e 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a enRead",. /*
b1a00 20 20 31 35 20 2a 2f 20 22 45 78 70 69 72 65 22 15 */ "Expire"
b1a10 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 2a 2f ,. /* 16 */
b1a20 20 22 41 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 "AutoCommit",.
b1a30 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 50 /* 17 */ "P
b1a40 61 67 65 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20 agecount",.
b1a50 2f 2a 20 20 31 38 20 2a 2f 20 22 49 6e 74 65 67 /* 18 */ "Integ
b1a60 72 69 74 79 43 6b 22 2c 0a 20 20 20 20 20 2f 2a rityCk",. /*
b1a70 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 2c 0a 20 19 */ "Not",.
b1a80 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 22 53 /* 20 */ "S
b1a90 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 ort",. /* 2
b1aa0 31 20 2a 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 1 */ "Copy",.
b1ab0 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 54 72 61 /* 22 */ "Tra
b1ac0 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 33 ce",. /* 23
b1ad0 20 2a 2f 20 22 46 75 6e 63 74 69 6f 6e 22 2c 0a */ "Function",.
b1ae0 20 20 20 20 20 2f 2a 20 20 32 34 20 2a 2f 20 22 /* 24 */ "
b1af0 49 66 4e 65 67 22 2c 0a 20 20 20 20 20 2f 2a 20 IfNeg",. /*
b1b00 20 32 35 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 25 */ "Noop",.
b1b10 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 52 /* 26 */ "R
b1b20 65 74 75 72 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 eturn",. /*
b1b30 20 32 37 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64 27 */ "NewRowid
b1b40 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a ",. /* 28 *
b1b50 2f 20 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20 / "Variable",.
b1b60 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 22 53 74 /* 29 */ "St
b1b70 72 69 6e 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ring",. /*
b1b80 33 30 20 2a 2f 20 22 52 65 61 6c 41 66 66 69 6e 30 */ "RealAffin
b1b90 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 ity",. /* 3
b1ba0 31 20 2a 2f 20 22 56 52 65 6e 61 6d 65 22 2c 0a 1 */ "VRename",.
b1bb0 20 20 20 20 20 2f 2a 20 20 33 32 20 2a 2f 20 22 /* 32 */ "
b1bc0 50 61 72 73 65 53 63 68 65 6d 61 22 2c 0a 20 20 ParseSchema",.
b1bd0 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22 56 4f /* 33 */ "VO
b1be0 70 65 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 pen",. /* 3
b1bf0 34 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20 4 */ "Close",.
b1c00 20 20 20 2f 2a 20 20 33 35 20 2a 2f 20 22 43 72 /* 35 */ "Cr
b1c10 65 61 74 65 49 6e 64 65 78 22 2c 0a 20 20 20 20 eateIndex",.
b1c20 20 2f 2a 20 20 33 36 20 2a 2f 20 22 49 73 55 6e /* 36 */ "IsUn
b1c30 69 71 75 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ique",. /*
b1c40 33 37 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e 64 22 37 */ "NotFound"
b1c50 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f ,. /* 38 */
b1c60 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 2f "Int64",. /
b1c70 2a 20 20 33 39 20 2a 2f 20 22 4d 75 73 74 42 65 * 39 */ "MustBe
b1c80 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 Int",. /* 4
b1c90 30 20 2a 2f 20 22 48 61 6c 74 22 2c 0a 20 20 20 0 */ "Halt",.
b1ca0 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 52 6f 77 /* 41 */ "Row
b1cb0 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 32 id",. /* 42
b1cc0 20 2a 2f 20 22 49 64 78 4c 54 22 2c 0a 20 20 20 */ "IdxLT",.
b1cd0 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 41 64 64 /* 43 */ "Add
b1ce0 49 6d 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 Imm",. /* 4
b1cf0 34 20 2a 2f 20 22 53 74 61 74 65 6d 65 6e 74 22 4 */ "Statement"
b1d00 2c 0a 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f ,. /* 45 */
b1d10 20 22 52 6f 77 44 61 74 61 22 2c 0a 20 20 20 20 "RowData",.
b1d20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4d 65 6d 4d /* 46 */ "MemM
b1d30 61 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 37 ax",. /* 47
b1d40 20 2a 2f 20 22 4e 6f 74 45 78 69 73 74 73 22 2c */ "NotExists",
b1d50 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a 2f 20 . /* 48 */
b1d60 22 47 6f 73 75 62 22 2c 0a 20 20 20 20 20 2f 2a "Gosub",. /*
b1d70 20 20 34 39 20 2a 2f 20 22 49 6e 74 65 67 65 72 49 */ "Integer
b1d80 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 2a ",. /* 50 *
b1d90 2f 20 22 50 72 65 76 22 2c 0a 20 20 20 20 20 2f / "Prev",. /
b1da0 2a 20 20 35 31 20 2a 2f 20 22 52 6f 77 53 65 74 * 51 */ "RowSet
b1db0 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Read",. /*
b1dc0 35 32 20 2a 2f 20 22 52 6f 77 53 65 74 41 64 64 52 */ "RowSetAdd
b1dd0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 33 20 2a ",. /* 53 *
b1de0 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 / "VColumn",.
b1df0 20 20 2f 2a 20 20 35 34 20 2a 2f 20 22 43 72 65 /* 54 */ "Cre
b1e00 61 74 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 ateTable",.
b1e10 2f 2a 20 20 35 35 20 2a 2f 20 22 4c 61 73 74 22 /* 55 */ "Last"
b1e20 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 20 2a 2f ,. /* 56 */
b1e30 20 22 53 65 65 6b 4c 65 22 2c 0a 20 20 20 20 20 "SeekLe",.
b1e40 2f 2a 20 20 35 37 20 2a 2f 20 22 49 6e 63 72 56 /* 57 */ "IncrV
b1e50 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 acuum",. /*
b1e60 20 35 38 20 2a 2f 20 22 49 64 78 52 6f 77 69 64 58 */ "IdxRowid
b1e70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 39 20 2a ",. /* 59 *
b1e80 2f 20 22 52 65 73 65 74 43 6f 75 6e 74 22 2c 0a / "ResetCount",.
b1e90 20 20 20 20 20 2f 2a 20 20 36 30 20 2a 2f 20 22 /* 60 */ "
b1ea0 43 6f 6e 74 65 78 74 50 75 73 68 22 2c 0a 20 20 ContextPush",.
b1eb0 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 59 69 /* 61 */ "Yi
b1ec0 65 6c 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 eld",. /* 6
b1ed0 32 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 2 */ "DropTrigge
b1ee0 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 33 20 r",. /* 63
b1ef0 2a 2f 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a */ "DropIndex",.
b1f00 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20 22 /* 64 */ "
b1f10 49 64 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a 20 IdxGE",. /*
b1f20 20 36 35 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 */ "IdxDelet
b1f30 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 e",. /* 66
b1f40 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20 20 2f 2a */ "Or",. /*
b1f50 20 20 36 37 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 67 */ "And",.
b1f60 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f 20 22 56 /* 68 */ "V
b1f70 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 acuum",. /*
b1f80 20 36 39 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a 69 */ "IfNot",.
b1f90 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 22 /* 70 */ "
b1fa0 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 DropTable",.
b1fb0 20 2f 2a 20 20 37 31 20 2a 2f 20 22 49 73 4e 75 /* 71 */ "IsNu
b1fc0 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32 ll",. /* 72
b1fd0 20 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 */ "NotNull",.
b1fe0 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f 20 22 4e /* 73 */ "N
b1ff0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 34 20 e",. /* 74
b2000 2a 2f 20 22 45 71 22 2c 0a 20 20 20 20 20 2f 2a */ "Eq",. /*
b2010 20 20 37 35 20 2a 2f 20 22 47 74 22 2c 0a 20 20 75 */ "Gt",.
b2020 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 4c 65 /* 76 */ "Le
b2030 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a ",. /* 77 *
b2040 2f 20 22 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 / "Lt",. /*
b2050 20 37 38 20 2a 2f 20 22 47 65 22 2c 0a 20 20 20 78 */ "Ge",.
b2060 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 53 65 65 /* 79 */ "See
b2070 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 kLt",. /* 8
b2080 30 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20 0 */ "BitAnd",.
b2090 20 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22 42 /* 81 */ "B
b20a0 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 itOr",. /*
b20b0 38 32 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74 82 */ "ShiftLeft
b20c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a ",. /* 83 *
b20d0 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a / "ShiftRight",.
b20e0 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 22 /* 84 */ "
b20f0 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 Add",. /* 8
b2100 35 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c 5 */ "Subtract",
b2110 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20 . /* 86 */
b2120 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20 "Multiply",.
b2130 20 2f 2a 20 20 38 37 20 2a 2f 20 22 44 69 76 69 /* 87 */ "Divi
b2140 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 38 de",. /* 88
b2150 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c */ "Remainder",
b2160 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f 20 . /* 89 */
b2170 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f "Concat",. /
b2180 2a 20 20 39 30 20 2a 2f 20 22 4d 61 6b 65 52 65 * 90 */ "MakeRe
b2190 63 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 cord",. /*
b21a0 39 31 20 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77 91 */ "ResultRow
b21b0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 2a ",. /* 92 *
b21c0 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20 20 20 / "Delete",.
b21d0 20 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e /* 93 */ "BitN
b21e0 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 ot",. /* 94
b21f0 20 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 */ "String8",.
b2200 20 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 41 /* 95 */ "A
b2210 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f ggFinal",. /
b2220 2a 20 20 39 36 20 2a 2f 20 22 43 6f 6d 70 61 72 * 96 */ "Compar
b2230 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37 20 e",. /* 97
b2240 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20 */ "Goto",.
b2250 2f 2a 20 20 39 38 20 2a 2f 20 22 54 61 62 6c 65 /* 98 */ "Table
b2260 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Lock",. /*
b2270 39 39 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 99 */ "Clear",.
b2280 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 56 /* 100 */ "V
b2290 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20 20 erifyCookie",.
b22a0 20 20 20 2f 2a 20 31 30 31 20 2a 2f 20 22 41 67 /* 101 */ "Ag
b22b0 67 53 74 65 70 22 2c 0a 20 20 20 20 20 2f 2a 20 gStep",. /*
b22c0 31 30 32 20 2a 2f 20 22 53 65 74 4e 75 6d 43 6f 102 */ "SetNumCo
b22d0 6c 75 6d 6e 73 22 2c 0a 20 20 20 20 20 2f 2a 20 lumns",. /*
b22e0 31 30 33 20 2a 2f 20 22 54 72 61 6e 73 61 63 74 103 */ "Transact
b22f0 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 ion",. /* 10
b2300 34 20 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c 0a 4 */ "VFilter",.
b2310 20 20 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 /* 105 */ "
b2320 56 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 VDestroy",.
b2330 2f 2a 20 31 30 36 20 2a 2f 20 22 43 6f 6e 74 65 /* 106 */ "Conte
b2340 78 74 50 6f 70 22 2c 0a 20 20 20 20 20 2f 2a 20 xtPop",. /*
b2350 31 30 37 20 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 107 */ "Next",.
b2360 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22 43 /* 108 */ "C
b2370 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 ount",. /* 1
b2380 30 39 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 09 */ "IdxInsert
b2390 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30 20 2a ",. /* 110 *
b23a0 2f 20 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 / "SeekGe",.
b23b0 20 2f 2a 20 31 31 31 20 2a 2f 20 22 49 6e 73 65 /* 111 */ "Inse
b23c0 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 32 rt",. /* 112
b23d0 20 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a 20 */ "Destroy",.
b23e0 20 20 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 52 /* 113 */ "R
b23f0 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 eadCookie",.
b2400 20 2f 2a 20 31 31 34 20 2a 2f 20 22 4c 6f 61 64 /* 114 */ "Load
b2410 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 Analysis",.
b2420 2f 2a 20 31 31 35 20 2a 2f 20 22 45 78 70 6c 61 /* 115 */ "Expla
b2430 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 in",. /* 116
b2440 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22 */ "HaltIfNull"
b2450 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f ,. /* 117 */
b2460 20 22 4f 70 65 6e 50 73 65 75 64 6f 22 2c 0a 20 "OpenPseudo",.
b2470 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f 20 22 4f /* 118 */ "O
b2480 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c 0a 20 penEphemeral",.
b2490 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22 4e /* 119 */ "N
b24a0 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 ull",. /* 12
b24b0 30 20 2a 2f 20 22 4d 6f 76 65 22 2c 0a 20 20 20 0 */ "Move",.
b24c0 20 20 2f 2a 20 31 32 31 20 2a 2f 20 22 42 6c 6f /* 121 */ "Blo
b24d0 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 32 20 b",. /* 122
b24e0 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20 */ "Rewind",.
b24f0 20 20 2f 2a 20 31 32 33 20 2a 2f 20 22 53 65 65 /* 123 */ "See
b2500 6b 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 kGt",. /* 12
b2510 34 20 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20 4 */ "VBegin",.
b2520 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f 20 22 56 /* 125 */ "V
b2530 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a Update",. /*
b2540 20 31 32 36 20 2a 2f 20 22 49 66 5a 65 72 6f 22 126 */ "IfZero"
b2550 2c 0a 20 20 20 20 20 2f 2a 20 31 32 37 20 2a 2f ,. /* 127 */
b2560 20 22 56 43 72 65 61 74 65 22 2c 0a 20 20 20 20 "VCreate",.
b2570 20 2f 2a 20 31 32 38 20 2a 2f 20 22 46 6f 75 6e /* 128 */ "Foun
b2580 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 39 20 d",. /* 129
b2590 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20 20 20 20 */ "IfPos",.
b25a0 20 2f 2a 20 31 33 30 20 2a 2f 20 22 52 65 61 6c /* 130 */ "Real
b25b0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a ",. /* 131 *
b25c0 2f 20 22 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 / "NullRow",.
b25d0 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 4a 75 6d /* 132 */ "Jum
b25e0 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 p",. /* 133
b25f0 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e 22 */ "Permutation"
b2600 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f ,. /* 134 */
b2610 20 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a "NotUsed_134",.
b2620 20 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 /* 135 */ "
b2630 4e 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 NotUsed_135",.
b2640 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f /* 136 */ "No
b2650 74 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 tUsed_136",.
b2660 20 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 /* 137 */ "NotU
b2670 73 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f sed_137",. /
b2680 2a 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 * 138 */ "NotUse
b2690 64 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 d_138",. /*
b26a0 31 33 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 139 */ "NotUsed_
b26b0 31 33 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 139",. /* 14
b26c0 30 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 0 */ "NotUsed_14
b26d0 30 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 0",. /* 141
b26e0 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 */ "ToText",.
b26f0 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 /* 142 */ "ToB
b2700 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 lob",. /* 14
b2710 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 3 */ "ToNumeric"
b2720 2c 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f ,. /* 144 */
b2730 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f "ToInt",. /
b2740 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c * 145 */ "ToReal
b2750 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e ",. };. return
b2760 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 azName[i];.}.#e
b2770 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
b2780 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 ***** End of opc
b2790 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a odes.c *********
b27a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b27b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b27c0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
b27d0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
b27e0 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a os_os2.c ******
b27f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2810 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 ****/./*.** 2006
b2820 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 Feb 14.**.** Th
b2830 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
b2840 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
b2850 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
b2860 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
b2870 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
b2880 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
b2890 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
b28a0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
b28b0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
b28c0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
b28d0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
b28e0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
b28f0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
b2900 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
b2910 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
b2920 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
b2930 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
b2940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
b2990 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
b29a0 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 ains code that i
b29b0 73 20 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 s specific to OS
b29c0 2f 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f /2..**.** $Id: o
b29d0 73 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32 s_os2.c,v 1.63 2
b29e0 30 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 008/12/10 19:26:
b29f0 32 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 24 drh Exp $.*/.
b2a00 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
b2a10 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 OS2../*.** A Not
b2a20 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 e About Memory A
b2a30 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a llocation:.**.**
b2a40 20 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65 This driver use
b2a50 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 s malloc()/free(
b2a60 29 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 ) directly rathe
b2a70 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 r than going thr
b2a80 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 ough.** the SQLi
b2a90 74 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 te-wrappers sqli
b2aa0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c te3_malloc()/sql
b2ab0 69 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 ite3_free(). Th
b2ac0 6f 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 ose wrappers.**
b2ad0 61 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 are designed for
b2ae0 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 use on embedded
b2af0 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d systems where m
b2b00 65 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 emory is scarce
b2b10 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 and.** malloc fa
b2b20 69 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 ilures happen fr
b2b30 65 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 equently. OS/2
b2b40 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c does not typical
b2b50 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 ly run on.** emb
b2b60 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 edded systems, a
b2b70 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 nd when it does
b2b80 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e the developers n
b2b90 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 ormally have big
b2ba0 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 ger.** problems
b2bb0 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 to worry about t
b2bc0 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 han running out
b2bd0 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 of memory. So t
b2be0 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 here is not.** a
b2bf0 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 compelling need
b2c00 20 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 to use the wrap
b2c10 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 pers..**.** But
b2c20 74 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 there is a good
b2c30 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 reason to not us
b2c40 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 e the wrappers.
b2c50 20 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a If we use the.*
b2c60 2a 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 * wrappers then
b2c70 77 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 we will get simu
b2c80 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 lated malloc() f
b2c90 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 ailures within t
b2ca0 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 his.** driver.
b2cb0 41 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 And that causes
b2cc0 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f all kinds of pro
b2cd0 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 blems for our te
b2ce0 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c sts. We.** coul
b2cf0 64 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 d enhance SQLite
b2d00 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 to deal with si
b2d10 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 mulated malloc f
b2d20 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a ailures within.*
b2d30 2a 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c * the OS driver,
b2d40 20 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f but the code to
b2d50 20 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 deal with those
b2d60 20 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e failure would n
b2d70 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 ot.** be exercis
b2d80 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 ed on Linux (whi
b2d90 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 ch does not need
b2da0 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 to malloc() in
b2db0 74 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 the driver).** a
b2dc0 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 nd so we would h
b2dd0 61 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 ave difficulty w
b2de0 72 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 riting coverage
b2df0 74 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a tests for that.*
b2e00 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 * code. Better
b2e10 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 to leave the cod
b2e20 65 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e e out, we think.
b2e30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 .**.** The point
b2e40 20 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 of this discuss
b2e50 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 ion is as follow
b2e60 73 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e s: When creatin
b2e70 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 g a new.** OS la
b2e80 79 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 yer for an embed
b2e90 64 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 ded system, if y
b2ea0 6f 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 ou use this file
b2eb0 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a as an example,.
b2ec0 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 ** avoid the use
b2ed0 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 of malloc()/fre
b2ee0 65 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 e(). Those rout
b2ef0 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 ines work ok on
b2f00 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 OS/2.** desktops
b2f10 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c but not so well
b2f20 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 in embedded sys
b2f30 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 tems..*/../*.**
b2f40 4d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 Macros used to d
b2f50 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
b2f60 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 or not to use t
b2f70 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 hreads..*/.#if d
b2f80 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 efined(SQLITE_TH
b2f90 52 45 41 44 53 41 46 45 29 20 26 26 20 53 51 4c READSAFE) && SQL
b2fa0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 ITE_THREADSAFE.#
b2fb0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
b2fc0 53 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e S2_THREADS 1.#en
b2fd0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 dif../*.** Inclu
b2fe0 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 de code that is
b2ff0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 common to all os
b3000 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a _*.c files.*/./*
b3010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
b3020 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e clude os_common.
b3030 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
b3040 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a of os_os2.c ****
b3050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b3060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
b3070 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d gin file os_comm
b3080 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
b3090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b30a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b30b0 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a .** 2004 May 22.
b30c0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
b30d0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
b30e0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
b30f0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
b3100 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
b3110 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
b3120 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
b3130 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
b3140 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
b3150 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
b3160 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
b3170 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
b3180 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
b3190 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
b31a0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
b31b0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
b31c0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
b31d0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
b31e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b31f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3220 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
b3230 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 ile contains mac
b3240 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 ros and a little
b3250 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 bit of code tha
b3260 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a t is common to.*
b3270 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 * all of the pla
b3280 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 tform-specific f
b3290 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e iles (os_*.c) an
b32a0 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 d is #included i
b32b0 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c nto those.** fil
b32c0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 es..**.** This f
b32d0 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 ile should be #i
b32e0 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f ncluded by the o
b32f0 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 s_*.c files only
b3300 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a . It is not a.*
b3310 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 * general purpos
b3320 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a e header file..*
b3330 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d *.** $Id: os_com
b3340 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 mon.h,v 1.38 200
b3350 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 9/02/24 18:40:50
b3360 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
b3370 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f $.*/.#ifndef _O
b3380 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 S_COMMON_H_.#def
b3390 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ine _OS_COMMON_H
b33a0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 _../*.** At leas
b33b0 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 t two bugs have
b33c0 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 slipped in becau
b33d0 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 se we changed th
b33e0 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a e MEMORY_DEBUG.*
b33f0 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 * macro to SQLIT
b3400 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 E_DEBUG and some
b3410 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 older makefiles
b3420 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 have not yet ma
b3430 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 de the.** switch
b3440 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
b3450 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 code should cat
b3460 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ch this problem
b3470 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e at compile-time.
b3480 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 .*/.#ifdef MEMOR
b3490 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 Y_DEBUG.# error
b34a0 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 "The MEMORY_DEBU
b34b0 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c G macro is obsol
b34c0 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 ete. Use SQLITE
b34d0 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 _DEBUG instead."
b34e0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
b34f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
b3500 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b3510 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d sqlite3OSTrace =
b3520 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 0;.#define OSTR
b3530 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 ACE1(X)
b3540 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
b3550 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
b3560 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 gPrintf(X).#defi
b3570 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
b3580 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
b3590 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
b35a0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
b35b0 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
b35c0 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 ACE3(X,Y,Z)
b35d0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
b35e0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
b35f0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 gPrintf(X,Y,Z).#
b3600 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 define OSTRACE4(
b3610 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 X,Y,Z,A) if( s
b3620 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
b3630 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
b3640 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 tf(X,Y,Z,A).#def
b3650 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 ine OSTRACE5(X,Y
b3660 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 ,Z,A,B) if( sqli
b3670 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
b3680 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
b3690 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
b36a0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
b36b0 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 Z,A,B,C) \. i
b36c0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
b36d0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
b36e0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
b36f0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
b3700 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
b3710 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
b3720 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
b3730 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
b3740 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c Y,Z,A,B,C,D).#el
b3750 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 se.#define OSTRA
b3760 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f CE1(X).#define O
b3770 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 STRACE2(X,Y).#de
b3780 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
b3790 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Y,Z).#define OST
b37a0 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 RACE4(X,Y,Z,A).#
b37b0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
b37c0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
b37d0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
b37e0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
b37f0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
b3800 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a A,B,C,D).#endif.
b3810 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f ./*.** Macros fo
b3820 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 r performance tr
b3830 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 acing. Normally
b3840 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e turned off. On
b3850 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 ly works.** on i
b3860 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 486 hardware..*/
b3870 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 .#ifdef SQLITE_P
b3880 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 ERFORMANCE_TRACE
b3890 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e ../* .** hwtime.
b38a0 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e h contains inlin
b38b0 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 e assembler code
b38c0 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e for implementin
b38d0 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f g .** high-perfo
b38e0 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f rmance timing ro
b38f0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a utines..*/./****
b3900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
b3910 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 de hwtime.h in t
b3920 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f he middle of os_
b3930 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
b3940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
b3950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
b3960 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a file hwtime.h *
b3970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
b39a0 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2008 May 27.**.
b39b0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
b39c0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
b39d0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
b39e0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
b39f0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
b3a00 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
b3a10 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
b3a20 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
b3a30 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
b3a40 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
b3a50 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
b3a60 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
b3a70 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
b3a80 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
b3a90 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
b3aa0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
b3ab0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
b3ac0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
b3ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3b10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
b3b20 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
b3b30 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 asm code for re
b3b40 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 trieving "high-p
b3b50 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 erformance".** c
b3b60 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 ounters for x86
b3b70 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a class CPUs..**.*
b3b80 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c * $Id: hwtime.h,
b3b90 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 v 1.3 2008/08/01
b3ba0 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 14:33:15 shane
b3bb0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
b3bc0 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 _HWTIME_H_.#def
b3bd0 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a ine _HWTIME_H_..
b3be0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
b3bf0 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 ing routine only
b3c00 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 works on pentiu
b3c10 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 m-class (or newe
b3c20 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a r) processors..*
b3c30 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 * It uses the RD
b3c40 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 TSC opcode to re
b3c50 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 ad the cycle cou
b3c60 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 nt value out of
b3c70 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 the.** processor
b3c80 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 and returns tha
b3c90 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 t value. This c
b3ca0 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 an be used for h
b3cb0 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 igh-res.** profi
b3cc0 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 ling..*/.#if (de
b3cd0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
b3ce0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f || defined(_MSC_
b3cf0 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 VER)) && \.
b3d00 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 (defined(i386)
b3d10 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 || defined(__i38
b3d20 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 6__) || defined(
b3d30 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 _M_IX86)).. #if
b3d40 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f defined(__GNUC_
b3d50 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f _).. __inline__
b3d60 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
b3d70 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
b3d80 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 d){. unsigne
b3d90 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 d int lo, hi;.
b3da0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
b3db0 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
b3dc0 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d : "=a" (lo), "=
b3dd0 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 d" (hi));. r
b3de0 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 eturn (sqlite_ui
b3df0 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 nt64)hi << 32 |
b3e00 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 lo;. }.. #elif
b3e10 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
b3e20 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 R).. __declspec
b3e30 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 (naked) __inline
b3e40 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f sqlite_uint64 _
b3e50 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 _cdecl sqlite3Hw
b3e60 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
b3e70 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 __asm {.
b3e80 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 rdtsc. r
b3e90 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 et ; retur
b3ea0 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 n value at EDX:E
b3eb0 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 AX. }. }..
b3ec0 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 #endif..#elif (
b3ed0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
b3ee0 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 ) && defined(__x
b3ef0 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 86_64__)).. __i
b3f00 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
b3f10 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
b3f20 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
b3f30 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 unsigned long v
b3f40 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f al;. __asm_
b3f50 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
b3f60 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 "rdtsc" : "=A" (
b3f70 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 val));. ret
b3f80 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 urn val;. }. .#
b3f90 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
b3fa0 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
b3fb0 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 ed(__ppc__))..
b3fc0 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
b3fd0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
b3fe0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
b3ff0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
b4000 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 g long retval;.
b4010 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
b4020 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f ng junk;. _
b4030 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
b4040 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 e__ ("\n\.
b4050 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 1: mftb
b4060 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 u %1\n\.
b4070 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 mftb
b4080 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 %L0\n\.
b4090 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 mft
b40a0 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 bu %0\n\.
b40b0 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 cmp
b40c0 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 w %0,%1\n\.
b40d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b40e0 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 bne 1b".
b40f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 :
b4100 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 "=r" (retval), "
b4110 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 =r" (junk));.
b4120 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c return retval
b4130 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 ;. }..#else..
b4140 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c #error Need impl
b4150 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
b4160 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
b4170 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e r your platform.
b4180 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 .. /*. ** To c
b4190 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 ompile without i
b41a0 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 mplementing sqli
b41b0 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
b41c0 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 your platform,.
b41d0 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f ** you can remo
b41e0 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 ve the above #er
b41f0 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 ror and use the
b4200 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 following. ** s
b4210 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 tub function. Y
b4220 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d ou will lose tim
b4230 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 ing support for
b4240 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 many. ** of the
b4250 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 debugging and t
b4260 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 esting utilities
b4270 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 , but it should
b4280 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f at. ** least co
b4290 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 mpile and run..
b42a0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
b42b0 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 TE sqlite_uint
b42c0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
b42d0 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 (void){ return (
b42e0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
b42f0 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 ); }..#endif..#e
b4300 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
b4310 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a (_HWTIME_H_) */.
b4320 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b4330 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 End of hwtime.h
b4340 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
b4350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b4370 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b4380 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
b4390 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
b43a0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
b43b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b43c0 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f ..static sqlite_
b43d0 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a uint64 g_start;.
b43e0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 static sqlite_ui
b43f0 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a nt64 g_elapsed;.
b4400 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 #define TIMER_ST
b4410 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 ART g_star
b4420 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 t=sqlite3Hwtime(
b4430 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f ).#define TIMER_
b4440 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c END g_el
b4450 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 apsed=sqlite3Hwt
b4460 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 ime()-g_start.#d
b4470 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
b4480 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 SED g_elapse
b4490 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 d.#else.#define
b44a0 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 TIMER_START.#def
b44b0 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 ine TIMER_END.#d
b44c0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
b44d0 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 SED ((sqlite
b44e0 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 _uint64)0).#endi
b44f0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 f../*.** If we c
b4500 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 ompile with the
b4510 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 SQLITE_TEST macr
b4520 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 o set, then the
b4530 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a following block.
b4540 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 ** of code will
b4550 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c give us the abil
b4560 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 ity to simulate
b4570 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 a disk I/O error
b4580 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 . This.** is us
b4590 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 ed for testing t
b45a0 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 he I/O recovery
b45b0 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 logic..*/.#ifdef
b45c0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
b45d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
b45e0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
b45f0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
b4600 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
b4610 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a of I/O Errors *
b4620 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
b4630 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
b4640 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 r_hardhit = 0;
b4650 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
b4660 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 of non-benign er
b4670 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 rors */.SQLITE_A
b4680 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
b4690 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 o_error_pending
b46a0 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 = 0; /* C
b46b0 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 ount down to fir
b46c0 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a st I/O error */.
b46d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
b46e0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
b46f0 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 persist = 0;
b4700 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 /* True if I
b4710 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 /O errors persis
b4720 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 t */.SQLITE_API
b4730 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
b4740 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b rror_benign = 0;
b4750 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
b4760 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 if errors are b
b4770 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f enign */.SQLITE_
b4780 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
b4790 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
b47a0 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 = 0;.SQLITE_API
b47b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 int sqlite3_dis
b47c0 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 kfull = 0;.#defi
b47d0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
b47e0 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 orBenign(X) sqli
b47f0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
b4800 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 ign=(X).#define
b4810 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
b4820 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 CODE) \. if( (
b4830 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
b4840 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 _persist && sqli
b4850 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
b4860 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 ) \. || sq
b4870 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
b4880 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 ending-- == 1 )
b4890 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
b48a0 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 { local_ioerr()
b48b0 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 ; CODE; }.static
b48c0 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 void local_ioer
b48d0 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 r(){. IOTRACE((
b48e0 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 "IOERR\n"));. s
b48f0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
b4900 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 hit++;. if( !sq
b4910 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
b4920 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f enign ) sqlite3_
b4930 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
b4940 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 ++;.}.#define Si
b4950 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
b4960 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 ror(CODE) \. i
b4970 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
b4980 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c ull_pending ){ \
b4990 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 . if( sqlite
b49a0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
b49b0 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 ng == 1 ){ \.
b49c0 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 local_ioerr(
b49d0 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 ); \. sqli
b49e0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 te3_diskfull = 1
b49f0 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 ; \. sqlit
b4a00 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 e3_io_error_hit
b4a10 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f = 1; \. CO
b4a20 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 DE; \. }else
b4a30 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 { \. sqlit
b4a40 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
b4a50 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 ing--; \. }
b4a60 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 \. }.#else.#de
b4a70 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
b4a80 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 rrorBenign(X).#d
b4a90 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
b4aa0 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 Error(A).#define
b4ab0 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
b4ac0 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 lError(A).#endif
b4ad0 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 ../*.** When tes
b4ae0 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 ting, keep a cou
b4af0 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 nt of the number
b4b00 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a of open files..
b4b10 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
b4b20 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
b4b30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 int sqlite3_ope
b4b40 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 n_file_count = 0
b4b50 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f ;.#define OpenCo
b4b60 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 unter(X) sqlite
b4b70 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 3_open_file_coun
b4b80 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 t+=(X).#else.#de
b4b90 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 fine OpenCounter
b4ba0 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 (X).#endif..#end
b4bb0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
b4bc0 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f OS_COMMON_H_) */
b4bd0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
b4be0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d * End of os_comm
b4bf0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
b4c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4c20 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
b4c30 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
b4c40 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
b4c50 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a n os_os2.c *****
b4c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4c70 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 /../*.** The os2
b4c80 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 File structure i
b4c90 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 s subclass of sq
b4ca0 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 lite3_file speci
b4cb0 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 fic for the OS/2
b4cc0 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 .** protability
b4cd0 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 layer..*/.typede
b4ce0 66 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 f struct os2File
b4cf0 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 os2File;.struct
b4d00 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e os2File {. con
b4d10 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
b4d20 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 thods *pMethod;
b4d30 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 /* Always the f
b4d40 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 irst entry */.
b4d50 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 HFILE h;
b4d60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e /* Han
b4d70 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e dle for accessin
b4d80 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 g the file */.
b4d90 63 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b char* pathToDel;
b4da0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
b4db0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c e of file to del
b4dc0 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 ete on close, NU
b4dd0 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 LL if not */. u
b4de0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 nsigned char loc
b4df0 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 ktype; /* Type
b4e00 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 of lock current
b4e10 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 ly held on this
b4e20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 file */.};..#def
b4e30 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ine LOCK_TIMEOUT
b4e40 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 10L /* the defa
b4e50 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 ult locking time
b4e60 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a out */../*******
b4e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4eb0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 ******.** The ne
b4ec0 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 xt group of rout
b4ed0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 ines implement t
b4ee0 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 he I/O methods s
b4ef0 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 pecified.** by t
b4f00 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
b4f10 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a thods object..**
b4f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
b4f70 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c *.** Close a fil
b4f80 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
b4f90 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 os2Close( sqlit
b4fa0 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 e3_file *id ){.
b4fb0 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
b4fc0 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 ERROR;. os2File
b4fd0 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 *pFile;. if( i
b4fe0 64 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f d && (pFile = (o
b4ff0 73 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 s2File*)id) != 0
b5000 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 ){. OSTRACE2
b5010 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 ( "CLOSE %d\n",
b5020 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 pFile->h );.
b5030 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 rc = DosClose( p
b5040 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 File->h );. p
b5050 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
b5060 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 NO_LOCK;. if
b5070 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 ( pFile->pathToD
b5080 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 el != NULL ){.
b5090 20 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 rc = DosForc
b50a0 65 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 eDelete( (PSZ)pF
b50b0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 ile->pathToDel )
b50c0 3b 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 ;. free( pF
b50d0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 ile->pathToDel )
b50e0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 ;. pFile->p
b50f0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b athToDel = NULL;
b5100 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 . }. id =
b5110 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 0;. OpenCount
b5120 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 er( -1 );. }..
b5130 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f return rc == NO
b5140 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
b5150 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
b5160 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 R;.}../*.** Read
b5170 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c data from a fil
b5180 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e e into a buffer.
b5190 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
b51a0 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 OK if all.** byt
b51b0 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 es were read suc
b51c0 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 cessfully and SQ
b51d0 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e LITE_IOERR if an
b51e0 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 ything goes.** w
b51f0 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rong..*/.static
b5200 69 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 int os2Read(. s
b5210 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
b5220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
b5230 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 * File to read f
b5240 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 rom */. void *p
b5250 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 Buf,
b5260 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
b5270 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 e content into t
b5280 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 his buffer */.
b5290 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 int amt,
b52a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b52b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
b52c0 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 es to read */.
b52d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 sqlite3_int64 of
b52e0 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 fset
b52f0 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 /* Begin reading
b5300 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 at this offset
b5310 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 */.){. ULONG fi
b5320 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b leLocation = 0L;
b5330 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 . ULONG got;.
b5340 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d os2File *pFile =
b5350 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 (os2File*)id;.
b5360 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
b5370 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
b5380 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
b5390 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b TE_IOERR_READ );
b53a0 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 . OSTRACE3( "RE
b53b0 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 AD %d lock=%d\n"
b53c0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
b53d0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
b53e0 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 if( DosSetFileP
b53f0 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 tr(pFile->h, off
b5400 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c set, FILE_BEGIN,
b5410 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 &fileLocation)
b5420 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 != NO_ERROR ){.
b5430 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
b5440 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 _IOERR;. }. if
b5450 28 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 ( DosRead( pFile
b5460 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 ->h, pBuf, amt,
b5470 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 &got ) != NO_ERR
b5480 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OR ){. return
b5490 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 SQLITE_IOERR_RE
b54a0 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f AD;. }. if( go
b54b0 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 t == (ULONG)amt
b54c0 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ). return SQL
b54d0 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b ITE_OK;. else {
b54e0 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 . /* Unread p
b54f0 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 ortions of the i
b5500 6e 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 nput buffer must
b5510 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 be zero-filled
b5520 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 */. memset(&(
b5530 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 (char*)pBuf)[got
b5540 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a ], 0, amt-got);.
b5550 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
b5560 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 E_IOERR_SHORT_RE
b5570 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a AD;. }.}../*.**
b5580 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d Write data from
b5590 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 a buffer into a
b55a0 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 file. Return S
b55b0 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
b55c0 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f ess.** or some o
b55d0 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 ther error code
b55e0 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 on failure..*/.s
b55f0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 tatic int os2Wri
b5600 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 te(. sqlite3_fi
b5610 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 le *id,
b5620 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f /* File to
b5630 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 write into */.
b5640 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 const void *pBu
b5650 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f,
b5660 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f /* The bytes to
b5670 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 be written */.
b5680 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 int amt,
b5690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b56a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
b56b0 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
b56c0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
b56d0 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 offset
b56e0 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f /* Offset into
b56f0 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 the file to beg
b5700 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f in writing at */
b5710 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 .){. ULONG file
b5720 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 Location = 0L;.
b5730 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
b5740 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 ERROR;. ULONG w
b5750 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 rote;. os2File
b5760 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
b5770 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 e*)id;. assert(
b5780 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 id!=0 );. Simu
b5790 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
b57a0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
b57b0 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 _WRITE );. Simu
b57c0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
b57d0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
b57e0 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 _FULL );. OSTRA
b57f0 43 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c CE3( "WRITE %d l
b5800 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ock=%d\n", pFile
b5810 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
b5820 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f type );. if( Do
b5830 73 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c sSetFilePtr(pFil
b5840 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 e->h, offset, FI
b5850 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c LE_BEGIN, &fileL
b5860 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 ocation) != NO_E
b5870 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 RROR ){. retu
b5880 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
b5890 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 . }. assert( a
b58a0 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 mt>0 );. while(
b58b0 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 amt > 0 &&.
b58c0 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 ( rc = DosW
b58d0 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 rite( pFile->h,
b58e0 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 (PVOID)pBuf, amt
b58f0 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 , &wrote ) ) ==
b5900 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 NO_ERROR &&.
b5910 20 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 wrote > 0.
b5920 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 ){. amt -= w
b5930 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d rote;. pBuf =
b5940 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b &((char*)pBuf)[
b5950 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 wrote];. }.. r
b5960 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f eturn ( rc != NO
b5970 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 _ERROR || amt >
b5980 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 (int)wrote ) ? S
b5990 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c QLITE_FULL : SQL
b59a0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
b59b0 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 Truncate an ope
b59c0 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 n file to a spec
b59d0 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 ified size.*/.st
b59e0 61 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e atic int os2Trun
b59f0 63 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 cate( sqlite3_fi
b5a00 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 le *id, i64 nByt
b5a10 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 e ){. APIRET rc
b5a20 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f = NO_ERROR;. o
b5a30 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
b5a40 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
b5a50 4f 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 OSTRACE3( "TRUNC
b5a60 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 ATE %d %lld\n",
b5a70 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 pFile->h, nByte
b5a80 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
b5a90 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
b5aa0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 ITE_IOERR_TRUNCA
b5ab0 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 TE );. rc = Dos
b5ac0 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 SetFileSize( pFi
b5ad0 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a le->h, nByte );.
b5ae0 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e return rc == N
b5af0 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 O_ERROR ? SQLITE
b5b00 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
b5b10 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a RR_TRUNCATE;.}..
b5b20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
b5b30 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 ST./*.** Count t
b5b40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c he number of ful
b5b50 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 lsyncs and norma
b5b60 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 l syncs. This i
b5b70 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a s used to test.*
b5b80 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 * that syncs and
b5b90 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f fullsyncs are o
b5ba0 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 ccuring at the r
b5bb0 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 ight times..*/.S
b5bc0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
b5bd0 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 lite3_sync_count
b5be0 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 = 0;.SQLITE_API
b5bf0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c int sqlite3_ful
b5c00 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b lsync_count = 0;
b5c10 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d .#endif../*.** M
b5c20 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 ake sure all wri
b5c30 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 tes to a particu
b5c40 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d lar file are com
b5c50 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a mitted to disk..
b5c60 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
b5c70 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 2Sync( sqlite3_f
b5c80 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 ile *id, int fla
b5c90 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 gs ){. os2File
b5ca0 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
b5cb0 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 e*)id;. OSTRACE
b5cc0 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3( "SYNC %d lock
b5cd0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
b5ce0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
b5cf0 65 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 e );.#ifdef SQLI
b5d00 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c TE_TEST. if( fl
b5d10 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e ags & SQLITE_SYN
b5d20 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c C_FULL){. sql
b5d30 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
b5d40 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c unt++;. }. sql
b5d50 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b ite3_sync_count+
b5d60 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 +;.#endif. /* I
b5d70 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 f we compiled wi
b5d80 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f th the SQLITE_NO
b5d90 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e _SYNC flag, then
b5da0 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 syncing is a.
b5db0 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 ** no-op. */.#i
b5dc0 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 fdef SQLITE_NO_S
b5dd0 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 YNC. UNUSED_PAR
b5de0 41 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 AMETER(pFile);.
b5df0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
b5e00 4b 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 K;.#else. retur
b5e10 6e 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 n DosResetBuffer
b5e20 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 ( pFile->h ) ==
b5e30 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 NO_ERROR ? SQLIT
b5e40 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f E_OK : SQLITE_IO
b5e50 45 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f ERR;.#endif.}../
b5e60 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 *.** Determine t
b5e70 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
b5e80 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 of a file in byt
b5e90 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 es.*/.static int
b5ea0 20 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 os2FileSize( sq
b5eb0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
b5ec0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 sqlite3_int64 *p
b5ed0 53 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 Size ){. APIRET
b5ee0 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a rc = NO_ERROR;.
b5ef0 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 FILESTATUS3 fs
b5f00 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d ts3FileInfo;. m
b5f10 65 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 emset(&fsts3File
b5f20 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Info, 0, sizeof(
b5f30 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b fsts3FileInfo));
b5f40 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
b5f50 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f );. SimulateIO
b5f60 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
b5f70 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 LITE_IOERR_FSTAT
b5f80 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 );. rc = DosQu
b5f90 65 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f eryFileInfo( ((o
b5fa0 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 s2File*)id)->h,
b5fb0 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 FIL_STANDARD, &f
b5fc0 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 sts3FileInfo, si
b5fd0 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 zeof(FILESTATUS3
b5fe0 29 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d ) );. if( rc ==
b5ff0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
b6000 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 *pSize = fsts3F
b6010 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a ileInfo.cbFile;.
b6020 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
b6030 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
b6040 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
b6050 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 _IOERR_FSTAT;.
b6060 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 }.}../*.** Acqui
b6070 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b re a reader lock
b6080 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
b6090 67 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 getReadLock( os2
b60a0 46 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 File *pFile ){.
b60b0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 FILELOCK LockA
b60c0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rea,.
b60d0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 UnlockArea;. A
b60e0 50 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d PIRET res;. mem
b60f0 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 set(&LockArea, 0
b6100 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 , sizeof(LockAre
b6110 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 a));. memset(&U
b6120 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 nlockArea, 0, si
b6130 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 zeof(UnlockArea)
b6140 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f );. LockArea.lO
b6150 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 ffset = SHARED_F
b6160 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 IRST;. LockArea
b6170 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 .lRange = SHARED
b6180 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 _SIZE;. UnlockA
b6190 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
b61a0 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c ;. UnlockArea.l
b61b0 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 Range = 0L;. re
b61c0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
b61d0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
b61e0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
b61f0 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
b6200 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 OUT, 1L );. OST
b6210 52 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c RACE3( "GETREADL
b6220 4f 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 OCK %d res=%d\n"
b6230 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
b6240 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b );. return res;
b6250 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 .}../*.** Undo a
b6260 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 readlock.*/.sta
b6270 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 tic int unlockRe
b6280 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 adLock( os2File
b6290 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 *id ){. FILELOC
b62a0 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 K LockArea,.
b62b0 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 UnlockA
b62c0 72 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 rea;. APIRET re
b62d0 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 s;. memset(&Loc
b62e0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
b62f0 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d (LockArea));. m
b6300 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 emset(&UnlockAre
b6310 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c a, 0, sizeof(Unl
b6320 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 ockArea));. Loc
b6330 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
b6340 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 0L;. LockArea.l
b6350 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e Range = 0L;. Un
b6360 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
b6370 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
b6380 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 . UnlockArea.lR
b6390 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 ange = SHARED_SI
b63a0 5a 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 ZE;. res = DosS
b63b0 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d etFileLocks( id-
b63c0 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c >h, &UnlockArea,
b63d0 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b &LockArea, LOCK
b63e0 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a _TIMEOUT, 1L );.
b63f0 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c OSTRACE3( "UNL
b6400 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c OCK-READLOCK fil
b6410 65 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d e handle=%d res=
b6420 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 %d?\n", id->h, r
b6430 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 es );. return r
b6440 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 es;.}../*.** Loc
b6450 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
b6460 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
b6470 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
b6480 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
b6490 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
b64a0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
b64b0 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
b64c0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
b64d0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
b64e0 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
b64f0 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
b6500 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
b6510 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
b6520 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
b6530 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
b6540 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
b6550 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
b6560 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
b6570 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
b6580 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
b6590 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
b65a0 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
b65b0 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
b65c0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
b65d0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
b65e0 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
b65f0 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
b6600 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
b6610 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
b6620 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
b6630 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
b6640 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
b6650 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
b6660 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
b6670 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
b6680 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
b6690 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
b66a0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
b66b0 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
b66c0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
b66d0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
b66e0 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
b66f0 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 XCLUSIVE.**.** T
b6700 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
b6710 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
b6720 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 lock. The os2U
b6730 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a nlock() routine.
b6740 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f ** erases all lo
b6750 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 cks at once and
b6760 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 returns us immed
b6770 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e iately to lockin
b6780 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 g level 0..** It
b6790 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
b67a0 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f to lower the lo
b67b0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 cking level one
b67c0 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 step at a time.
b67d0 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 You.** must go
b67e0 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b straight to lock
b67f0 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a ing level 0..*/.
b6800 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f static int os2Lo
b6810 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck( sqlite3_file
b6820 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
b6830 70 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d pe ){. int rc =
b6840 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
b6850 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
b6860 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 from subroutine
b6870 73 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 s */. APIRET re
b6880 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 s = NO_ERROR;
b6890 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e /* Result of an
b68a0 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 OS/2 lock call
b68b0 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b */. int newLock
b68c0 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 type; /* S
b68d0 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 et pFile->lockty
b68e0 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 pe to this value
b68f0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
b6900 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 */. int gotPend
b6910 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 ingLock = 0;/* T
b6920 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 rue if we acquir
b6930 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 ed a PENDING loc
b6940 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 k this time */.
b6950 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 FILELOCK LockA
b6960 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rea,.
b6970 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f UnlockArea;. o
b6980 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
b6990 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
b69a0 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 memset(&LockArea
b69b0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b , 0, sizeof(Lock
b69c0 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 Area));. memset
b69d0 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c (&UnlockArea, 0,
b69e0 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 sizeof(UnlockAr
b69f0 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ea));. assert(
b6a00 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 pFile!=0 );. OS
b6a10 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 TRACE4( "LOCK %d
b6a20 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 %d was %d\n", p
b6a30 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
b6a40 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 e, pFile->lockty
b6a50 70 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 pe );.. /* If t
b6a60 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
b6a70 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 a lock of this t
b6a80 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 ype or more rest
b6a90 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 rictive on the.
b6aa0 20 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 ** os2File, do
b6ab0 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 nothing. Don't u
b6ac0 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a se the end_lock:
b6ad0 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 exit path, as.
b6ae0 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ** sqlite3_mute
b6af0 78 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 x_enter() hasn't
b6b00 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 been called yet
b6b10 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 .. */. if( pFi
b6b20 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f le->locktype>=lo
b6b30 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 cktype ){. OS
b6b40 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
b6b50 20 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 %d ok (already
b6b60 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d held)\n", pFile-
b6b70 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a >h, locktype );.
b6b80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
b6b90 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
b6ba0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f Make sure the lo
b6bb0 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 cking sequence i
b6bc0 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 s correct. */.
b6bd0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
b6be0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
b6bf0 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 K || locktype==S
b6c00 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
b6c10 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
b6c20 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 !=PENDING_LOCK )
b6c30 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
b6c40 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c type!=RESERVED_L
b6c50 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f OCK || pFile->lo
b6c60 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
b6c70 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 OCK );.. /* Loc
b6c80 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f k the PENDING_LO
b6c90 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 CK byte if we ne
b6ca0 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 ed to acquire a
b6cb0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a PENDING lock or.
b6cc0 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f ** a SHARED lo
b6cd0 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 ck. If we are a
b6ce0 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 cquiring a SHARE
b6cf0 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 D lock, the acqu
b6d00 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 isition of. **
b6d10 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b the PENDING_LOCK
b6d20 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 byte is tempora
b6d30 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f ry.. */. newLo
b6d40 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e cktype = pFile->
b6d50 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 locktype;. if(
b6d60 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
b6d70 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c =NO_LOCK. |
b6d80 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 | (locktype==EXC
b6d90 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
b6da0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
b6db0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 RESERVED_LOCK).
b6dc0 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 ){. LockArea
b6dd0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 .lOffset = PENDI
b6de0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 NG_BYTE;. Loc
b6df0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 kArea.lRange = 1
b6e00 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
b6e10 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
b6e20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
b6e30 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 Range = 0L;..
b6e40 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 /* wait longer
b6e50 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 than LOCK_TIMEOU
b6e60 54 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 T here not to ha
b6e70 76 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 ve to try multip
b6e80 6c 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 le times */.
b6e90 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 res = DosSetFile
b6ea0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
b6eb0 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
b6ec0 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 ockArea, 100L, 0
b6ed0 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 L );. if( res
b6ee0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
b6ef0 20 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 gotPending
b6f00 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 Lock = 1;.
b6f10 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 OSTRACE3( "LOCK
b6f20 25 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 %d pending lock
b6f30 62 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 boolean set. re
b6f40 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s=%d\n", pFile->
b6f50 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a h, res );. }.
b6f60 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
b6f70 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a e a shared lock.
b6f80 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
b6f90 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
b6fa0 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 && res == NO_ER
b6fb0 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ROR ){. asser
b6fc0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
b6fd0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 pe==NO_LOCK );.
b6fe0 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 res = getRead
b6ff0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
b7000 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 if( res == NO_E
b7010 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 RROR ){. ne
b7020 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 wLocktype = SHAR
b7030 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 ED_LOCK;. }.
b7040 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f OSTRACE3( "LO
b7050 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68 CK %d acquire sh
b7060 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 ared lock. res=%
b7070 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
b7080 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a res );. }.. /*
b7090 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 Acquire a RESER
b70a0 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 VED lock. */.
b70b0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 if( locktype==RE
b70c0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 SERVED_LOCK && r
b70d0 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
b70e0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 {. assert( pF
b70f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
b7100 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
b7110 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
b7120 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 et = RESERVED_BY
b7130 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 TE;. LockArea
b7140 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
b7150 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
b7160 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
b7170 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
b7180 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 0L;. res =
b7190 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
b71a0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
b71b0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
b71c0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
b71d0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 , 0L );. if(
b71e0 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
b71f0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
b7200 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f type = RESERVED_
b7210 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 LOCK;. }.
b7220 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 OSTRACE3( "LOCK
b7230 25 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72 %d acquire reser
b7240 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 ved lock. res=%d
b7250 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
b7260 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 es );. }.. /*
b7270 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e Acquire a PENDIN
b7280 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 G lock. */. if
b7290 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c ( locktype==EXCL
b72a0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 USIVE_LOCK && re
b72b0 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
b72c0 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 . newLocktype
b72d0 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b = PENDING_LOCK;
b72e0 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c . gotPendingL
b72f0 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 ock = 0;. OST
b7300 52 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 RACE2( "LOCK %d
b7310 61 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 acquire pending
b7320 6c 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f lock. pending lo
b7330 63 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 ck boolean unset
b7340 2e 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 .\n", pFile->h )
b7350 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
b7360 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 ire an EXCLUSIVE
b7370 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
b7380 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
b7390 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 SIVE_LOCK && res
b73a0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
b73b0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
b73c0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 e->locktype>=SHA
b73d0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
b73e0 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 res = unlockRead
b73f0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
b7400 20 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 OSTRACE2( "unre
b7410 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 adlock = %d\n",
b7420 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 res );. LockA
b7430 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 rea.lOffset = SH
b7440 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 ARED_FIRST;.
b7450 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
b7460 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 = SHARED_SIZE;.
b7470 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
b7480 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
b7490 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
b74a0 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 e = 0L;. res
b74b0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
b74c0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
b74d0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
b74e0 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
b74f0 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 T, 0L );. if(
b7500 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 res == NO_ERROR
b7510 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 ){. newLoc
b7520 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 ktype = EXCLUSIV
b7530 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 E_LOCK;. }els
b7540 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 e{. OSTRACE
b7550 32 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 2( "OS/2 error-c
b7560 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 ode = %d\n", res
b7570 20 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 );. getRea
b7580 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 dLock(pFile);.
b7590 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 }. OSTRACE3
b75a0 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 ( "LOCK %d acqui
b75b0 72 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 re exclusive loc
b75c0 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 k. res=%d\n", p
b75d0 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a File->h, res );.
b75e0 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 }.. /* If we
b75f0 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 are holding a PE
b7600 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 NDING lock that
b7610 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 ought to be rele
b7620 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 ased, then. **
b7630 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a release it now..
b7640 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 */. if( gotPe
b7650 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 ndingLock && loc
b7660 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
b7670 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b CK ){. int r;
b7680 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f . LockArea.lO
b7690 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
b76a0 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
b76b0 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 0L;. Unlock
b76c0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 Area.lOffset = P
b76d0 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
b76e0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e UnlockArea.lRan
b76f0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d ge = 1L;. r =
b7700 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
b7710 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
b7720 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
b7730 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
b7740 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
b7750 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 ACE3( "LOCK %d u
b7760 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 nlocking pending
b7770 2f 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 /is shared. r=%d
b7780 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
b7790 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 );. }.. /* Up
b77a0 64 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f date the state o
b77b0 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 f the lock has h
b77c0 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 eld in the file
b77d0 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a descriptor then.
b77e0 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 ** return the
b77f0 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 appropriate resu
b7800 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 lt code.. */.
b7810 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 if( res == NO_ER
b7820 52 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ROR ){. rc =
b7830 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
b7840 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 se{. OSTRACE4
b7850 28 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 ( "LOCK FAILED %
b7860 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 d trying for %d
b7870 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 but got %d\n", p
b7880 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
b7890 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c locktype,
b78a0 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a newLocktype );.
b78b0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
b78c0 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c BUSY;. }. pFil
b78d0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 e->locktype = ne
b78e0 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 wLocktype;. OST
b78f0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 RACE3( "LOCK %d
b7900 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 now %d\n", pFile
b7910 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
b7920 74 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e type );. return
b7930 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
b7940 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
b7950 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
b7960 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
b7970 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
b7980 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
b7990 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
b79a0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
b79b0 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
b79c0 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d , return.** non-
b79d0 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 zero, otherwise
b79e0 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 zero..*/.static
b79f0 69 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 int os2CheckRese
b7a00 72 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 rvedLock( sqlite
b7a10 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
b7a20 2a 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 *pOut ){. int r
b7a30 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 = 0;. os2File
b7a40 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
b7a50 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 e*)id;. assert(
b7a60 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 pFile!=0 );. i
b7a70 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
b7a80 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe>=RESERVED_LOC
b7a90 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a K ){. r = 1;.
b7aa0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 OSTRACE3( "T
b7ab0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
b7ac0 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 d (local)\n", pF
b7ad0 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d ile->h, r );. }
b7ae0 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f else{. FILELO
b7af0 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 CK LockArea,.
b7b00 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f Unlo
b7b10 63 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 ckArea;. APIR
b7b20 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 ET rc = NO_ERROR
b7b30 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f ;. memset(&Lo
b7b40 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f ckArea, 0, sizeo
b7b50 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 f(LockArea));.
b7b60 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b memset(&Unlock
b7b70 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
b7b80 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 UnlockArea));.
b7b90 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
b7ba0 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 et = RESERVED_BY
b7bb0 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 TE;. LockArea
b7bc0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
b7bd0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
b7be0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
b7bf0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
b7c00 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 = 0L;. rc =
b7c10 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
b7c20 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
b7c30 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
b7c40 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
b7c50 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
b7c60 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f CE3( "TEST WR-LO
b7c70 43 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 CK %d lock reser
b7c80 76 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e ved byte rc=%d\n
b7c90 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 ", pFile->h, rc
b7ca0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d );. if( rc ==
b7cb0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
b7cc0 20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 APIRET rcu =
b7cd0 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 NO_ERROR; /* ret
b7ce0 75 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c urn code for unl
b7cf0 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 ocking */.
b7d00 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
b7d10 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 = 0L;. Loc
b7d20 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
b7d30 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 L;. UnlockA
b7d40 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 rea.lOffset = RE
b7d50 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 SERVED_BYTE;.
b7d60 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 UnlockArea.lR
b7d70 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 ange = 1L;.
b7d80 20 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c rcu = DosSetFil
b7d90 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 eLocks( pFile->h
b7da0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
b7db0 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
b7dc0 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 IMEOUT, 0L );.
b7dd0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 OSTRACE3( "T
b7de0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 EST WR-LOCK %d u
b7df0 6e 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 nlock reserved b
b7e00 79 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 yte r=%d\n", pFi
b7e10 6c 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 le->h, rcu );.
b7e20 20 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 }. r = !(rc
b7e30 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 == NO_ERROR);.
b7e40 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 OSTRACE3( "TE
b7e50 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
b7e60 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 (remote)\n", pF
b7e70 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d ile->h, r );. }
b7e80 0a 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 . *pOut = r;.
b7e90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
b7ea0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 ;.}../*.** Lower
b7eb0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
b7ec0 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 el on file descr
b7ed0 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b iptor id to lock
b7ee0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
b7ef0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
b7f00 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
b7f10 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
b7f20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
b7f30 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
b7f40 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
b7f50 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
b7f60 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
b7f70 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
b7f80 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
b7f90 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
b7fa0 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 * It is not poss
b7fb0 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f ible for this ro
b7fc0 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 utine to fail if
b7fd0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
b7fe0 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f ment.** is NO_LO
b7ff0 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f CK. If the seco
b8000 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 nd argument is S
b8010 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 HARED_LOCK then
b8020 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
b8030 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c might return SQL
b8040 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 ITE_IOERR;.*/.st
b8050 61 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f atic int os2Unlo
b8060 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck( sqlite3_file
b8070 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
b8080 70 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 pe ){. int type
b8090 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ;. os2File *pFi
b80a0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
b80b0 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d d;. APIRET rc =
b80c0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 SQLITE_OK;. AP
b80d0 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 IRET res = NO_ER
b80e0 52 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 ROR;. FILELOCK
b80f0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 LockArea,.
b8100 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 UnlockAre
b8110 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 a;. memset(&Loc
b8120 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
b8130 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d (LockArea));. m
b8140 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 emset(&UnlockAre
b8150 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c a, 0, sizeof(Unl
b8160 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 ockArea));. ass
b8170 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b ert( pFile!=0 );
b8180 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
b8190 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
b81a0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 );. OSTRACE4(
b81b0 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 "UNLOCK %d to %d
b81c0 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c was %d\n", pFil
b81d0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 e->h, locktype,
b81e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
b81f0 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c );. type = pFil
b8200 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 e->locktype;. i
b8210 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 f( type>=EXCLUSI
b8220 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c VE_LOCK ){. L
b8230 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
b8240 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 = 0L;. LockAr
b8250 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a ea.lRange = 0L;.
b8260 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
b8270 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f Offset = SHARED_
b8280 46 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 FIRST;. Unloc
b8290 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 kArea.lRange = S
b82a0 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 HARED_SIZE;.
b82b0 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 res = DosSetFile
b82c0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
b82d0 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
b82e0 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 ockArea, LOCK_TI
b82f0 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 MEOUT, 0L );.
b8300 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f OSTRACE3( "UNLO
b8310 43 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 CK %d exclusive
b8320 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 lock res=%d\n",
b8330 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
b8340 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 . if( locktyp
b8350 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 e==SHARED_LOCK &
b8360 26 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 & getReadLock(pF
b8370 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 ile) != NO_ERROR
b8380 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 ){. /* Thi
b8390 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 s should never h
b83a0 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c appen. We shoul
b83b0 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 d always be able
b83c0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 to. ** rea
b83d0 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 cquire the read
b83e0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 lock */. OS
b83f0 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 TRACE3( "UNLOCK
b8400 25 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 %d to %d getRead
b8410 4c 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 Lock() failed\n"
b8420 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
b8430 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 type );. rc
b8440 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
b8450 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 UNLOCK;. }.
b8460 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 }. if( type>=RE
b8470 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SERVED_LOCK ){.
b8480 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
b8490 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f set = 0L;. Lo
b84a0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
b84b0 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 0L;. UnlockAr
b84c0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 ea.lOffset = RES
b84d0 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 ERVED_BYTE;.
b84e0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
b84f0 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 e = 1L;. res
b8500 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
b8510 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
b8520 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
b8530 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
b8540 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 T, 0L );. OST
b8550 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 RACE3( "UNLOCK %
b8560 64 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 d reserved res=%
b8570 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
b8580 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 res );. }. if(
b8590 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f locktype==NO_LO
b85a0 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 CK && type>=SHAR
b85b0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
b85c0 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c es = unlockReadL
b85d0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
b85e0 4f 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 OSTRACE5( "UNLOC
b85f0 4b 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 K %d is %d want
b8600 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 %d res=%d\n", pF
b8610 69 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f ile->h, type, lo
b8620 63 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 cktype, res );.
b8630 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 }. if( type>=P
b8640 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 ENDING_LOCK ){.
b8650 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
b8660 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f set = 0L;. Lo
b8670 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
b8680 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 0L;. UnlockAr
b8690 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e ea.lOffset = PEN
b86a0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 DING_BYTE;. U
b86b0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
b86c0 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 1L;. res =
b86d0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
b86e0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
b86f0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
b8700 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
b8710 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
b8720 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
b8730 20 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c pending res=%d\
b8740 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
b8750 73 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 s );. }. pFile
b8760 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
b8770 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 ktype;. OSTRACE
b8780 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 3( "UNLOCK %d no
b8790 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e w %d\n", pFile->
b87a0 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
b87b0 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 pe );. return r
b87c0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 c;.}../*.** Cont
b87d0 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 rol and query of
b87e0 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 the open file h
b87f0 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 andle..*/.static
b8800 20 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 int os2FileCont
b8810 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 rol(sqlite3_file
b8820 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f *id, int op, vo
b8830 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 id *pArg){. swi
b8840 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 tch( op ){. c
b8850 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
b8860 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 _LOCKSTATE: {.
b8870 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 *(int*)pArg
b8880 3d 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 = ((os2File*)id)
b8890 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 ->locktype;.
b88a0 20 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e OSTRACE3( "FCN
b88b0 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 TL_LOCKSTATE %d
b88c0 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 lock=%d\n", ((os
b88d0 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 2File*)id)->h, (
b88e0 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c (os2File*)id)->l
b88f0 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 ocktype );.
b8900 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
b8910 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 K;. }. }. r
b8920 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
b8930 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 OR;.}../*.** Ret
b8940 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 urn the sector s
b8950 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
b8960 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 the underlying b
b8970 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a lock device for.
b8980 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ** the specified
b8990 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 file. This is a
b89a0 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 lmost always 512
b89b0 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 bytes, but may
b89c0 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 be.** larger for
b89d0 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a some devices..*
b89e0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 *.** SQLite code
b89f0 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 assumes this fu
b8a00 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 nction cannot fa
b8a10 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 il. It also assu
b8a20 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 mes that.** if t
b8a30 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 wo files are cre
b8a40 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 ated in the same
b8a50 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 file-system dir
b8a60 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 ectory (i.e..**
b8a70 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 a database and i
b8a80 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 ts journal file)
b8a90 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 that the sector
b8aa0 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 size will be th
b8ab0 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f e.** same for bo
b8ac0 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th..*/.static in
b8ad0 74 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 t os2SectorSize(
b8ae0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
b8af0 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 ){. return SQLI
b8b00 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f TE_DEFAULT_SECTO
b8b10 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a R_SIZE;.}../*.**
b8b20 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 Return a vector
b8b30 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 of device chara
b8b40 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 cteristics..*/.s
b8b50 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 tatic int os2Dev
b8b60 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
b8b70 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
b8b80 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 *id){. return 0
b8b90 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 ;.}.../*.** Char
b8ba0 61 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 acter set conver
b8bb0 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 sion objects use
b8bc0 64 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 d by conversion
b8bd0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 routines..*/.sta
b8be0 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 tic UconvObject
b8bf0 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f ucUtf8 = NULL; /
b8c00 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 * convert betwee
b8c10 6e 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d n UTF-8 and UCS-
b8c20 32 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 2 */.static Ucon
b8c30 76 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 vObject uclCp =
b8c40 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 NULL; /* conver
b8c50 74 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 t between local
b8c60 63 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 codepage and UCS
b8c70 2d 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c -2 */../*.** Hel
b8c80 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 per function to
b8c90 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 initialize the c
b8ca0 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 onversion object
b8cb0 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 s from and to UT
b8cc0 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 F-8..*/.static v
b8cd0 6f 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a oid initUconvObj
b8ce0 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 ects( void ){.
b8cf0 69 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f if( UniCreateUco
b8d00 6e 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c nvObject( UTF_8,
b8d10 20 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c &ucUtf8 ) != UL
b8d20 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 S_SUCCESS ).
b8d30 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 ucUtf8 = NULL;.
b8d40 20 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 if ( UniCreateU
b8d50 63 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 convObject( (Uni
b8d60 43 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 Char *)L"@path=y
b8d70 65 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d es", &uclCp ) !=
b8d80 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 ULS_SUCCESS ).
b8d90 20 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b uclCp = NULL;
b8da0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 .}../*.** Helper
b8db0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 function to fre
b8dc0 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e e the conversion
b8dd0 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e objects from an
b8de0 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 d to UTF-8..*/.s
b8df0 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 tatic void freeU
b8e00 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 convObjects( voi
b8e10 64 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 d ){. if ( ucUt
b8e20 66 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 f8 ). UniFree
b8e30 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 UconvObject( ucU
b8e40 74 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 tf8 );. if ( uc
b8e50 6c 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 lCp ). UniFre
b8e60 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 eUconvObject( uc
b8e70 6c 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 lCp );. ucUtf8
b8e80 3d 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 = NULL;. uclCp
b8e90 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a = NULL;.}../*.**
b8ea0 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e Helper function
b8eb0 20 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d to convert UTF-
b8ec0 38 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 8 filenames to l
b8ed0 6f 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 ocal OS/2 codepa
b8ee0 67 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 ge..** The two-s
b8ef0 74 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 tep process: fir
b8f00 73 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 st convert the i
b8f10 6e 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 ncoming UTF-8 st
b8f20 72 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 ring.** into UCS
b8f30 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d -2 and then from
b8f40 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 UCS-2 to the cu
b8f50 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a rrent codepage..
b8f60 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
b8f70 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 char pointer has
b8f80 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f to be freed..*/
b8f90 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f .static char *co
b8fa0 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 nvertUtf8PathToC
b8fb0 70 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 p( const char *i
b8fc0 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 n ){. UniChar t
b8fd0 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 empPath[CCHMAXPA
b8fe0 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 TH];. char *out
b8ff0 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f = (char *)callo
b9000 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 c( CCHMAXPATH, 1
b9010 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 );.. if( !out
b9020 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c ). return NUL
b9030 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 L;.. if( !ucUtf
b9040 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 8 || !uclCp ).
b9050 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 initUconvObjec
b9060 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 ts();.. /* dete
b9070 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 rmine string for
b9080 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
b9090 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 of UTF-8 which i
b90a0 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 s CP1208 */. if
b90b0 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 ( UniStrToUcs( u
b90c0 63 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c cUtf8, tempPath,
b90d0 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 (char *)in, CCH
b90e0 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 MAXPATH ) != ULS
b90f0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 _SUCCESS ). r
b9100 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 eturn out; /* if
b9110 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c conversion fail
b9120 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d s, return the em
b9130 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 pty string */..
b9140 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 /* conversion f
b9150 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 or current codep
b9160 61 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 age which can be
b9170 20 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 used for paths
b9180 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 */. UniStrFromU
b9190 63 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 cs( uclCp, out,
b91a0 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 tempPath, CCHMAX
b91b0 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 PATH );.. retur
b91c0 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n out;.}../*.**
b91d0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 Helper function
b91e0 74 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e to convert filen
b91f0 61 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 ames from local
b9200 63 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d codepage to UTF-
b9210 38 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 8..** The two-st
b9220 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 ep process: firs
b9230 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e t convert the in
b9240 63 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d coming codepage-
b9250 73 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 specific.** stri
b9260 6e 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e ng into UCS-2 an
b9270 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d d then from UCS-
b9280 32 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 2 to the codepag
b9290 65 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 e of UTF-8..** T
b92a0 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 he returned char
b92b0 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 pointer has to
b92c0 62 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 be freed..**.**
b92d0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
b92e0 20 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 non-static to b
b92f0 65 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 e able to use th
b9300 69 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e is in shell.c an
b9310 64 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 d.** similar app
b9320 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 lications that t
b9330 61 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 ake command line
b9340 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 arguments..*/.c
b9350 68 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 har *convertCpPa
b9360 74 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 thToUtf8( const
b9370 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e char *in ){. Un
b9380 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 iChar tempPath[C
b9390 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 CHMAXPATH];. ch
b93a0 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 ar *out = (char
b93b0 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 *)calloc( CCHMAX
b93c0 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 PATH, 1 );.. if
b93d0 28 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 ( !out ). ret
b93e0 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 urn NULL;.. if(
b93f0 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c !ucUtf8 || !ucl
b9400 43 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f Cp ). initUco
b9410 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 nvObjects();..
b9420 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f /* conversion fo
b9430 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 r current codepa
b9440 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 ge which can be
b9450 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a used for paths *
b9460 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f /. if( UniStrTo
b9470 55 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 Ucs( uclCp, temp
b9480 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e Path, (char *)in
b9490 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 , CCHMAXPATH ) !
b94a0 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a = ULS_SUCCESS ).
b94b0 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 return out;
b94c0 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e /* if conversion
b94d0 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 fails, return t
b94e0 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 he empty string
b94f0 2a 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 */.. /* determi
b9500 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 ne string for th
b9510 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 e conversion of
b9520 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 UTF-8 which is C
b9530 50 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 P1208 */. UniSt
b9540 72 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 rFromUcs( ucUtf8
b9550 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c , out, tempPath,
b9560 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a CCHMAXPATH );..
b9570 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a return out;.}.
b9580 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 ./*.** This vect
b9590 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 or defines all t
b95a0 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 he methods that
b95b0 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 can operate on a
b95c0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c n.** sqlite3_fil
b95d0 65 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 e for os2..*/.st
b95e0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
b95f0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 e3_io_methods os
b9600 32 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 2IoMethod = {.
b9610 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
b9620 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
b9630 72 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c rsion */. os2Cl
b9640 6f 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a ose,. os2Read,.
b9650 20 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 os2Write,. os
b9660 32 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 2Truncate,. os2
b9670 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 Sync,. os2FileS
b9680 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a ize,. os2Lock,.
b9690 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f os2Unlock,. o
b96a0 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c s2CheckReservedL
b96b0 6f 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f ock,. os2FileCo
b96c0 6e 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 ntrol,. os2Sect
b96d0 6f 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 orSize,. os2Dev
b96e0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
b96f0 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a cs.};../********
b9700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9740 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 ***.** Here ends
b9750 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 the I/O methods
b9760 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 that form the s
b9770 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
b9780 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 s object..**.**
b9790 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f The next block o
b97a0 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 f code implement
b97b0 73 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 s the VFS method
b97c0 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s..*************
b97d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b97f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b9810 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
b9820 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
b9830 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a name in zBuf. z
b9840 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 Buf must be big
b9850 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c enough to.** hol
b9860 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 d at pVfs->mxPat
b9870 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 hname characters
b9880 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
b9890 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 getTempname(int
b98a0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
b98b0 20 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e ){. static con
b98c0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
b98d0 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 zChars[] =.
b98e0 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f "abcdefghijklmno
b98f0 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 pqrstuvwxyz".
b9900 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e "ABCDEFGHIJKLMN
b9910 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 OPQRSTUVWXYZ".
b9920 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a "0123456789";.
b9930 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 int i, j;. ch
b9940 61 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b ar zTempPathBuf[
b9950 33 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 3];. PSZ zTempP
b9960 61 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d ath = (PSZ)&zTem
b9970 70 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 pPathBuf;. if(
b9980 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
b9990 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 ectory ){. zT
b99a0 65 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 empPath = sqlite
b99b0 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
b99c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
b99d0 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 f( DosScanEnv( (
b99e0 50 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 PSZ)"TEMP", &zTe
b99f0 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 mpPath ) ){.
b9a00 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 if( DosScanEnv
b9a10 28 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a ( (PSZ)"TMP", &z
b9a20 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 TempPath ) ){.
b9a30 20 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 if( DosSca
b9a40 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 nEnv( (PSZ)"TMPD
b9a50 49 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 IR", &zTempPath
b9a60 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
b9a70 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d ULONG ulDriveNum
b9a80 20 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 = 0, ulDriveMap
b9a90 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
b9aa0 20 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 DosQueryCurrent
b9ab0 44 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 Disk( &ulDriveNu
b9ac0 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 m, &ulDriveMap )
b9ad0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72 ;. spr
b9ae0 69 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 intf( (char*)zTe
b9af0 6d 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 mpPath, "%c:", (
b9b00 63 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 char)( 'A' + ulD
b9b10 72 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b riveNum - 1 ) );
b9b20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
b9b30 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f }. }. }. /
b9b40 2a 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 * Strip off a tr
b9b50 61 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f ailing slashes o
b9b60 72 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f r backslashes, o
b9b70 74 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c therwise we woul
b9b80 64 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c d get *. * mul
b9b90 74 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 tiple (back)slas
b9ba0 68 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 hes which causes
b9bb0 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 DosOpen() to fa
b9bc0 69 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 il.
b9bd0 20 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 *. * Trailing
b9be0 20 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 spaces are not
b9bf0 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e allowed, either.
b9c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b9c10 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 */.
b9c20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c j = sqlite3Strl
b9c30 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b en30(zTempPath);
b9c40 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 . while( j > 0
b9c50 26 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a && ( zTempPath[j
b9c60 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a -1] == '\\' || z
b9c70 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d TempPath[j-1] ==
b9c80 20 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 '/'.
b9c90 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d || zTem
b9ca0 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 pPath[j-1] == '
b9cb0 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a ' ) ){. j--;.
b9cc0 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b }. zTempPath[
b9cd0 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 j] = '\0';. if(
b9ce0 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 !sqlite3_temp_d
b9cf0 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
b9d00 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 char *zTempPathU
b9d10 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 TF = convertCpPa
b9d20 74 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 thToUtf8( zTempP
b9d30 61 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ath );. sqlit
b9d40 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 e3_snprintf( nBu
b9d50 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 f-30, zBuf,.
b9d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b9d70 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 "%s\\"SQLITE_T
b9d80 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c EMP_FILE_PREFIX,
b9d90 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b zTempPathUTF );
b9da0 0a 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 . free( zTemp
b9db0 50 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c PathUTF );. }el
b9dc0 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f se{. sqlite3_
b9dd0 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 snprintf( nBuf-3
b9de0 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 0, zBuf,.
b9df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
b9e00 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 %s\\"SQLITE_TEMP
b9e10 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 _FILE_PREFIX, zT
b9e20 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 empPath );. }.
b9e30 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c j = sqlite3Strl
b9e40 65 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 en30( zBuf );.
b9e50 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
b9e60 73 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d ss( 20, &zBuf[j]
b9e70 20 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 );. for( i = 0
b9e80 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a ; i < 20; i++, j
b9e90 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a ++ ){. zBuf[j
b9ea0 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 ] = (char)zChars
b9eb0 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 [ ((unsigned cha
b9ec0 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 r)zBuf[j])%(size
b9ed0 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b of(zChars)-1) ];
b9ee0 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d . }. zBuf[j] =
b9ef0 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 0;. OSTRACE2(
b9f00 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 "TEMP FILENAME:
b9f10 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 %s\n", zBuf );.
b9f20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
b9f30 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 K;.}.../*.** Tur
b9f40 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 n a relative pat
b9f50 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c hname into a ful
b9f60 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 l pathname. Wri
b9f70 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 te the full.** p
b9f80 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 athname into zFu
b9f90 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 ll[]. zFull[] w
b9fa0 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 ill be at least
b9fb0 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
b9fc0 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a .** bytes in siz
b9fd0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
b9fe0 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 os2FullPathname
b9ff0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
ba000 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 *pVfs,
ba010 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 /* Pointer to vf
ba020 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f s object */. co
ba030 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 nst char *zRelat
ba040 69 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 ive, /* Pos
ba050 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 sibly relative i
ba060 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 nput path */. i
ba070 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 nt nFull,
ba080 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
ba090 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
ba0a0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
ba0b0 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 char *zFull
ba0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ba0d0 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a Output buffer *
ba0e0 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 /.){. char *zRe
ba0f0 6c 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 lativeCp = conve
ba100 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 rtUtf8PathToCp(
ba110 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 zRelative );. c
ba120 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d har zFullCp[CCHM
ba130 41 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a AXPATH] = "\0";.
ba140 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 char *zFullUTF
ba150 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 ;. APIRET rc =
ba160 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f DosQueryPathInfo
ba170 28 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 ( zRelativeCp, F
ba180 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 IL_QUERYFULLNAME
ba190 2c 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 , zFullCp,.
ba1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ba1b0 20 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 CCHMA
ba1c0 58 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 XPATH );. free(
ba1d0 20 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a zRelativeCp );.
ba1e0 20 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e zFullUTF = con
ba1f0 76 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 vertCpPathToUtf8
ba200 28 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 ( zFullCp );. s
ba210 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
ba220 20 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a nFull, zFull, z
ba230 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 FullUTF );. fre
ba240 65 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 e( zFullUTF );.
ba250 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f return rc == NO
ba260 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
ba270 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
ba280 52 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 R;.}.../*.** Ope
ba290 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 n a file..*/.sta
ba2a0 74 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 tic int os2Open(
ba2b0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
ba2c0 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
ba2d0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a /* Not used */.
ba2e0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
ba2f0 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ame,
ba300 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 /* Name of the f
ba310 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ile */. sqlite3
ba320 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 _file *id,
ba330 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
ba340 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 the SQLite file
ba350 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 handle here */.
ba360 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 int flags,
ba370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ba380 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 * Open mode flag
ba390 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 s */. int *pOut
ba3a0 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 Flags
ba3b0 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 /* Status r
ba3c0 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 eturn flags */.)
ba3d0 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 {. HFILE h;. U
ba3e0 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 LONG ulFileAttri
ba3f0 62 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d bute = FILE_NORM
ba400 41 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 AL;. ULONG ulOp
ba410 65 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 enFlags = 0;. U
ba420 4c 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 LONG ulOpenMode
ba430 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a = 0;. os2File *
ba440 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
ba450 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 *)id;. APIRET r
ba460 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
ba470 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a ULONG ulAction;.
ba480 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b char *zNameCp;
ba490 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 . char zTmpname
ba4a0 5b 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 [CCHMAXPATH+1];
ba4b0 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 /* Buffer to
ba4c0 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d hold name of tem
ba4d0 70 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 p file */.. /*
ba4e0 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
ba4f0 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 gument to this f
ba500 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c unction is NULL,
ba510 20 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a generate a . *
ba520 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 * temporary file
ba530 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 name to use .
ba540 2a 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 */. if( !zName
ba550 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 ){. int rc =
ba560 67 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d getTempname(CCHM
ba570 41 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 AXPATH+1, zTmpna
ba580 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 me);. if( rc!
ba590 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
ba5a0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
ba5b0 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d }. zName =
ba5c0 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a zTmpname;. }..
ba5d0 0a 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 . memset( pFile
ba5e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 , 0, sizeof(*pFi
ba5f0 6c 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 le) );.. OSTRAC
ba600 45 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 E2( "OPEN want %
ba610 64 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a d\n", flags );..
ba620 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 if( flags & SQ
ba630 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
ba640 49 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 ITE ){. ulOpe
ba650 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 nMode |= OPEN_AC
ba660 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a CESS_READWRITE;.
ba670 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f OSTRACE1( "O
ba680 50 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e PEN read/write\n
ba690 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 " );. }else{.
ba6a0 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 ulOpenMode |=
ba6b0 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 OPEN_ACCESS_READ
ba6c0 4f 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 ONLY;. OSTRAC
ba6d0 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f E1( "OPEN read o
ba6e0 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 nly\n" );. }..
ba6f0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
ba700 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 ITE_OPEN_CREATE
ba710 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 ){. ulOpenFla
ba720 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f gs |= OPEN_ACTIO
ba730 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 N_OPEN_IF_EXISTS
ba740 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 | OPEN_ACTION_C
ba750 52 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 REATE_IF_NEW;.
ba760 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
ba770 4e 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 N open new/creat
ba780 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b e\n" );. }else{
ba790 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 . ulOpenFlags
ba7a0 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f |= OPEN_ACTION_
ba7b0 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c OPEN_IF_EXISTS |
ba7c0 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 OPEN_ACTION_FAI
ba7d0 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 L_IF_NEW;. OS
ba7e0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 TRACE1( "OPEN op
ba7f0 65 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 en existing\n" )
ba800 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 ;. }.. if( fla
ba810 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
ba820 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 _MAIN_DB ){.
ba830 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 ulOpenMode |= OP
ba840 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e EN_SHARE_DENYNON
ba850 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 E;. OSTRACE1(
ba860 20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 "OPEN share rea
ba870 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 d/write\n" );.
ba880 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 }else{. ulOpe
ba890 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 nMode |= OPEN_SH
ba8a0 41 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 ARE_DENYWRITE;.
ba8b0 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 OSTRACE1( "OP
ba8c0 45 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e EN share read on
ba8d0 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 ly\n" );. }..
ba8e0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
ba8f0 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
ba900 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 CLOSE ){. cha
ba910 72 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 r pathUtf8[CCHMA
ba920 58 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e XPATH];.#ifdef N
ba930 44 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 DEBUG /* when de
ba940 62 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 bugging we want
ba950 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 to make sure it
ba960 69 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 is deleted */.
ba970 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 ulFileAttribut
ba980 65 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b e = FILE_HIDDEN;
ba990 0a 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 .#endif. os2F
ba9a0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 ullPathname( pVf
ba9b0 73 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 s, zName, CCHMAX
ba9c0 50 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 PATH, pathUtf8 )
ba9d0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 ;. pFile->pat
ba9e0 68 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 hToDel = convert
ba9f0 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 Utf8PathToCp( pa
baa00 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 thUtf8 );. OS
baa10 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 TRACE1( "OPEN hi
baa20 64 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 dden/delete on c
baa30 6c 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 lose file attrib
baa40 75 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c utes\n" );. }el
baa50 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 se{. pFile->p
baa60 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b athToDel = NULL;
baa70 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
baa80 4f 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 OPEN normal file
baa90 20 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b attribute\n" );
baaa0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 . }.. /* alway
baab0 73 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d s open in random
baac0 20 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 access mode for
baad0 20 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 possibly better
baae0 20 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 speed */. ulOp
baaf0 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 enMode |= OPEN_F
bab00 4c 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 LAGS_RANDOM;. u
bab10 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 lOpenMode |= OPE
bab20 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f N_FLAGS_FAIL_ON_
bab30 45 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d ERROR;. ulOpenM
bab40 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 ode |= OPEN_FLAG
bab50 53 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 S_NOINHERIT;..
bab60 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 zNameCp = conver
bab70 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a tUtf8PathToCp( z
bab80 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 Name );. rc = D
bab90 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 osOpen( (PSZ)zNa
baba0 6d 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 meCp,.
babb0 20 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 &h,.
babc0 20 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 &ulAct
babd0 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ion,.
babe0 20 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 0L,.
babf0 20 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 ulFileA
bac00 74 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 ttribute,.
bac10 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e ulOpen
bac20 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 Flags,.
bac30 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 ulOpenMod
bac40 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
bac50 20 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 (PEAOP2)NULL
bac60 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 );. free( zName
bac70 43 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 Cp );. if( rc !
bac80 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
bac90 20 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 OSTRACE7( "OPE
baca0 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 N Invalid handle
bacb0 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 rc=%d: zName=%s
bacc0 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c , ulAction=%#lx,
bacd0 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c ulAttr=%#lx, ul
bace0 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f Flags=%#lx, ulMo
bacf0 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 de=%#lx\n",.
bad00 20 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e rc, zN
bad10 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 ame, ulAction, u
bad20 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 lFileAttribute,
bad30 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f ulOpenFlags, ulO
bad40 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 penMode );. i
bad50 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f f( pFile->pathTo
bad60 44 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 Del ). free
bad70 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 ( pFile->pathToD
bad80 65 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d el );. pFile-
bad90 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c >pathToDel = NUL
bada0 4c 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 L;. if( flags
badb0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
badc0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 EADWRITE ){.
badd0 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 OSTRACE2( "OPE
bade0 4e 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e N %d Invalid han
badf0 64 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 dle\n", ((flags
bae00 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 | SQLITE_OPEN_RE
bae10 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 ADONLY) & ~SQLIT
bae20 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
bae30 29 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ) );. retur
bae40 6e 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c n os2Open( pVfs,
bae50 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 zName, id,.
bae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bae70 20 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 ((flags | SQLI
bae80 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
bae90 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e ) & ~SQLITE_OPEN
baea0 5f 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 _READWRITE),.
baeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
baec0 20 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a pOutFlags );.
baed0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
baee0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
baef0 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 ANTOPEN;. }.
baf00 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c }.. if( pOutFl
baf10 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 ags ){. *pOut
baf20 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 Flags = flags &
baf30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
baf40 57 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f WRITE ? SQLITE_O
baf50 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 PEN_READWRITE :
baf60 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
baf70 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 ONLY;. }.. pFi
baf80 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f le->pMethod = &o
baf90 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 s2IoMethod;. pF
bafa0 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 ile->h = h;. Op
bafb0 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 enCounter(+1);.
bafc0 20 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e OSTRACE3( "OPEN
bafd0 20 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 %d pOutFlags=%d
bafe0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
baff0 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 OutFlags );. re
bb000 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
bb010 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
bb020 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a the named file..
bb030 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
bb040 32 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 2Delete(. sqlit
bb050 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
bb060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb070 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e /* Not used on
bb080 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 os2 */. const
bb090 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
bb0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb0b0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 /* Name of file
bb0c0 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 to delete */.
bb0d0 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 int syncDir
bb0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb0f0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
bb100 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a ed on os2 */.){.
bb110 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f APIRET rc = NO
bb120 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a _ERROR;. char *
bb130 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f zFilenameCp = co
bb140 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 nvertUtf8PathToC
bb150 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a p( zFilename );.
bb160 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
bb170 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
bb180 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b _IOERR_DELETE );
bb190 0a 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 . rc = DosDelet
bb1a0 65 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d e( (PSZ)zFilenam
bb1b0 65 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a eCp );. free( z
bb1c0 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 FilenameCp );.
bb1d0 4f 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 OSTRACE2( "DELET
bb1e0 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 E \"%s\"\n", zFi
bb1f0 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 lename );. retu
bb200 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f rn rc == NO_ERRO
bb210 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 R ? SQLITE_OK :
bb220 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c SQLITE_IOERR_DEL
bb230 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 ETE;.}../*.** Ch
bb240 65 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 eck the existanc
bb250 65 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 e and status of
bb260 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
bb270 63 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 c int os2Access(
bb280 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
bb290 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 pVfs, /*
bb2a0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 Not used on os2
bb2b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
bb2c0 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f *zFilename, /
bb2d0 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 * Name of file t
bb2e0 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 o check */. int
bb2f0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
bb300 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f /* Type o
bb310 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f f test to make o
bb320 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 n this file */.
bb330 20 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 int *pOut
bb340 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
bb350 69 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 ite results here
bb360 20 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 */.){. FILESTA
bb370 54 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 TUS3 fsts3Config
bb380 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 Info;. APIRET r
bb390 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
bb3a0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 char *zFilenameC
bb3b0 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 p = convertUtf8P
bb3c0 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 athToCp( zFilena
bb3d0 6d 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 me );.. memset(
bb3e0 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 &fsts3ConfigInf
bb3f0 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 o, 0, sizeof(fst
bb400 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b s3ConfigInfo) );
bb410 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 . rc = DosQuery
bb420 50 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a PathInfo( (PSZ)z
bb430 46 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f FilenameCp, FIL_
bb440 53 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 STANDARD,.
bb450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb460 20 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 &fsts3ConfigI
bb470 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 nfo, sizeof(FILE
bb480 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 STATUS3) );. fr
bb490 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 ee( zFilenameCp
bb4a0 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 );. OSTRACE4( "
bb4b0 41 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 ACCESS fsts3Conf
bb4c0 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d igInfo.attrFile=
bb4d0 25 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 %d flags=%d rc=%
bb4e0 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
bb4f0 20 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 fsts3ConfigInf
bb500 6f 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 o.attrFile, flag
bb510 73 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 s, rc );. switc
bb520 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 h( flags ){.
bb530 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 case SQLITE_ACCE
bb540 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 SS_READ:. cas
bb550 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
bb560 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 EXISTS:. rc
bb570 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 = (rc == NO_ERR
bb580 4f 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 OR);. OSTRA
bb590 43 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 CE3( "ACCESS %s
bb5a0 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 access of read a
bb5b0 6e 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 nd exists rc=%d
bb5c0 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 \n", zFilename,
bb5d0 72 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 rc );. brea
bb5e0 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 k;. case SQLI
bb5f0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
bb600 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 ITE:. rc =
bb610 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 (rc == NO_ERROR)
bb620 20 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 && ( (fsts3Conf
bb630 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 igInfo.attrFile
bb640 26 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 & FILE_READONLY)
bb650 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f == 0 );. O
bb660 53 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 STRACE3( "ACCESS
bb670 20 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 %s access of re
bb680 61 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c ad/write rc=%d\
bb690 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 n", zFilename, r
bb6a0 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b c );. break
bb6b0 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 ;. default:.
bb6c0 20 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49 assert( !"I
bb6d0 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 nvalid flags arg
bb6e0 75 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 ument" );. }.
bb6f0 2a 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 *pOut = rc;. re
bb700 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
bb710 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }...#ifndef SQLI
bb720 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
bb730 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 ENSION./*.** Int
bb740 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e erfaces for open
bb750 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 ing a shared lib
bb760 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e rary, finding en
bb770 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 try points.** wi
bb780 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 thin the shared
bb790 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f library, and clo
bb7a0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 sing the shared
bb7b0 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a library..*/./*.*
bb7c0 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 * Interfaces for
bb7d0 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 opening a share
bb7e0 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 d library, findi
bb7f0 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a ng entry points.
bb800 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 ** within the sh
bb810 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e ared library, an
bb820 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 d closing the sh
bb830 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f ared library..*/
bb840 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 .static void *os
bb850 32 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 2DlOpen(sqlite3_
bb860 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 vfs *pVfs, const
bb870 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
bb880 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 ){. UCHAR loadE
bb890 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 rr[256];. HMODU
bb8a0 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 LE hmod;. APIRE
bb8b0 54 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 T rc;. char *zF
bb8c0 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 ilenameCp = conv
bb8d0 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
bb8e0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 zFilename);. rc
bb8f0 20 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 = DosLoadModule
bb900 28 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 ((PSZ)loadErr, s
bb910 69 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 izeof(loadErr),
bb920 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d zFilenameCp, &hm
bb930 6f 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c od);. free(zFil
bb940 65 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 enameCp);. retu
bb950 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f rn rc != NO_ERRO
bb960 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 R ? 0 : (void*)h
bb970 6d 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e mod;.}./*.** A n
bb980 6f 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 o-op since the e
bb990 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
bb9a0 75 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 urned on the Dos
bb9b0 4c 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e LoadModule call.
bb9c0 0a 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 .** os2Dlopen re
bb9d0 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f turns zero if Do
bb9e0 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e sLoadModule is n
bb9f0 6f 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a ot successful..*
bba00 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 /.static void os
bba10 32 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 2DlError(sqlite3
bba20 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
bba30 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
bba40 4f 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a Out){./* no-op *
bba50 2f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 /.}.static void
bba60 2a 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 *os2DlSym(sqlite
bba70 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
bba80 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 d *pHandle, cons
bba90 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 t char *zSymbol)
bbaa0 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 {. PFN pfn;. A
bbab0 50 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d PIRET rc;. rc =
bbac0 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 DosQueryProcAdd
bbad0 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 r((HMODULE)pHand
bbae0 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c le, 0L, zSymbol,
bbaf0 20 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 &pfn);. if( rc
bbb00 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a != NO_ERROR ){.
bbb10 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 /* if the sy
bbb20 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 mbol itself was
bbb30 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 not found, searc
bbb40 68 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 h again for the
bbb50 73 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 same. * symb
bbb60 6f 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 ol with an extra
bbb70 20 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 underscore, tha
bbb80 74 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 t might be neede
bbb90 64 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 d depending.
bbba0 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e * on the callin
bbbb0 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a g convention */.
bbbc0 20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f char _zSymbo
bbbd0 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 l[256] = "_";.
bbbe0 20 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 strncat(_zSymb
bbbf0 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 ol, zSymbol, 255
bbc00 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 );. rc = DosQ
bbc10 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d ueryProcAddr((HM
bbc20 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 ODULE)pHandle, 0
bbc30 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 L, _zSymbol, &pf
bbc40 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e n);. }. return
bbc50 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc != NO_ERROR
bbc60 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e ? 0 : (void*)pfn
bbc70 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
bbc80 6f 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 os2DlClose(sqlit
bbc90 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f e3_vfs *pVfs, vo
bbca0 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 id *pHandle){.
bbcb0 44 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 DosFreeModule((H
bbcc0 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b MODULE)pHandle);
bbcd0 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 .}.#else /* if S
bbce0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
bbcf0 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 EXTENSION is def
bbd00 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 ined: */. #defi
bbd10 6e 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 ne os2DlOpen 0.
bbd20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 #define os2DlEr
bbd30 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 ror 0. #define
bbd40 6f 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 os2DlSym 0. #de
bbd50 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 fine os2DlClose
bbd60 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
bbd70 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 Write up to nBu
bbd80 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f f bytes of rando
bbd90 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e mness into zBuf.
bbda0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
bbdb0 73 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c s2Randomness(sql
bbdc0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
bbdd0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
bbde0 7a 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 zBuf ){. int n
bbdf0 3d 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 = 0;.#if defined
bbe00 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 (SQLITE_TEST).
bbe10 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 n = nBuf;. mems
bbe20 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 et(zBuf, 0, nBuf
bbe30 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 );.#else. int s
bbe40 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a izeofULong = siz
bbe50 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 eof(ULONG);. if
bbe60 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 ( (int)sizeof(DA
bbe70 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 TETIME) <= nBuf
bbe80 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 - n ){. DATET
bbe90 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 IME x;. DosGe
bbea0 74 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 tDateTime(&x);.
bbeb0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
bbec0 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 n], &x, sizeof(x
bbed0 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a ));. n += siz
bbee0 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 eof(x);. }.. i
bbef0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c f( sizeofULong <
bbf00 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 = nBuf - n ){.
bbf10 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 PPIB ppib;.
bbf20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b DosGetInfoBlock
bbf30 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a s(NULL, &ppib);.
bbf40 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
bbf50 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f [n], &ppib->pib_
bbf60 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f ulpid, sizeofULo
bbf70 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 ng);. n += si
bbf80 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a zeofULong;. }..
bbf90 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
bbfa0 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
bbfb0 0a 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a . PTIB ptib;.
bbfc0 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
bbfd0 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c ocks(&ptib, NULL
bbfe0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
bbff0 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 Buf[n], &ptib->t
bc000 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 ib_ptib2->tib2_u
bc010 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e ltid, sizeofULon
bc020 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a g);. n += siz
bc030 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 eofULong;. }..
bc040 20 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 /* if we still
bc050 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 haven't filled t
bc060 68 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68 he buffer yet th
bc070 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c e following will
bc080 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 */. /* grab ev
bc090 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e erything once in
bc0a0 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 stead of making
bc0b0 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f several calls fo
bc0c0 72 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 r a single item
bc0d0 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 */. if( sizeofU
bc0e0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e Long <= nBuf - n
bc0f0 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c ){. ULONG ul
bc100 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d SysInfo[QSV_MAX]
bc110 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 ;. DosQuerySy
bc120 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 sInfo(1L, QSV_MA
bc130 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 X, ulSysInfo, si
bc140 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f zeofULong * QSV_
bc150 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 MAX);.. memcp
bc160 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 y(&zBuf[n], &ulS
bc170 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f ysInfo[QSV_MS_CO
bc180 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 UNT - 1], sizeof
bc190 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d ULong);. n +=
bc1a0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 sizeofULong;..
bc1b0 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f if( sizeofULo
bc1c0 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 ng <= nBuf - n )
bc1d0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 {. memcpy(&
bc1e0 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 zBuf[n], &ulSysI
bc1f0 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e nfo[QSV_TIMER_IN
bc200 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a TERVAL - 1], siz
bc210 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 eofULong);.
bc220 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e n += sizeofULon
bc230 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 g;. }. if(
bc240 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 sizeofULong <=
bc250 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 nBuf - n ){.
bc260 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
bc270 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 ], &ulSysInfo[QS
bc280 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c V_TIME_LOW - 1],
bc290 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 sizeofULong);.
bc2a0 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 n += sizeof
bc2b0 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 ULong;. }.
bc2c0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
bc2d0 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
bc2e0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 memcpy(&zB
bc2f0 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 uf[n], &ulSysInf
bc300 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 o[QSV_TIME_HIGH
bc310 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e - 1], sizeofULon
bc320 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 g);. n += s
bc330 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 izeofULong;.
bc340 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 }. if( sizeof
bc350 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 ULong <= nBuf -
bc360 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 n ){. memcp
bc370 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 y(&zBuf[n], &ulS
bc380 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 ysInfo[QSV_TOTAV
bc390 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a AILMEM - 1], siz
bc3a0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 eofULong);.
bc3b0 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e n += sizeofULon
bc3c0 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e g;. }. }.#en
bc3d0 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b dif.. return n;
bc3e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 .}../*.** Sleep
bc3f0 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 for a little whi
bc400 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 le. Return the
bc410 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 amount of time s
bc420 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 lept..** The arg
bc430 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d ument is the num
bc440 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
bc450 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 nds we want to s
bc460 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 leep..** The ret
bc470 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 urn value is the
bc480 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f number of micro
bc490 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 seconds of sleep
bc4a0 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 actually.** req
bc4b0 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 uested from the
bc4c0 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 underlying opera
bc4d0 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e ting system, a n
bc4e0 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d umber which.** m
bc4f0 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20 ight be greater
bc500 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
bc510 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 the argument, b
bc520 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 ut not less.** t
bc530 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 han the argument
bc540 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
bc550 6f 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 os2Sleep( sqlite
bc560 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
bc570 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 microsec ){. D
bc580 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 osSleep( (micros
bc590 65 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 ec/1000) );. re
bc5a0 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d turn microsec;.}
bc5b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
bc5c0 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 owing variable,
bc5d0 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d if set to a non-
bc5e0 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f zero value, beco
bc5f0 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a mes the result.*
bc600 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 * returned from
bc610 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
bc620 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 Time(). This is
bc630 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
bc640 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c g..*/.#ifdef SQL
bc650 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
bc660 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
bc670 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 current_time = 0
bc680 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
bc690 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 Find the current
bc6a0 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 time (in Univer
bc6b0 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 sal Coordinated
bc6c0 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 Time). Write th
bc6d0 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d e.** current tim
bc6e0 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 e and date as a
bc6f0 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 Julian Day numbe
bc700 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e r into *prNow an
bc710 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 d.** return 0.
bc720 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
bc730 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 time and date ca
bc740 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a nnot be found..*
bc750 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 /.int os2Current
bc760 54 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 Time( sqlite3_vf
bc770 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 s *pVfs, double
bc780 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 *prNow ){. doub
bc790 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 le now;. SHORT
bc7a0 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 minute; /* needs
bc7b0 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 to be able to c
bc7c0 6f 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 ope with negativ
bc7d0 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 e timezone offse
bc7e0 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 t */. USHORT se
bc7f0 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 cond, hour,.
bc800 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c day, month,
bc810 20 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d year;. DATETIM
bc820 45 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 E dt;. DosGetDa
bc830 74 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 teTime( &dt );.
bc840 20 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 second = (USHOR
bc850 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 T)dt.seconds;.
bc860 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 minute = (SHORT)
bc870 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e dt.minutes + dt.
bc880 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 timezone;. hour
bc890 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f = (USHORT)dt.ho
bc8a0 75 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 urs;. day = (US
bc8b0 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d HORT)dt.day;. m
bc8c0 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 onth = (USHORT)d
bc8d0 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 t.month;. year
bc8e0 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 = (USHORT)dt.yea
bc8f0 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 r;.. /* Calcula
bc900 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a tions from http:
bc910 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c //www.astro.keel
bc920 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 e.ac.uk/~rno/Ast
bc930 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a ronomy/hjd.html.
bc940 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e http://www.
bc950 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 astro.keele.ac.u
bc960 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 k/~rno/Astronomy
bc970 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 /hjd-0.1.c */.
bc980 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 /* Calculate the
bc990 20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a Julian days */.
bc9a0 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 now = day - 32
bc9b0 30 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 076 +. 1461*(
bc9c0 79 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d year + 4800 + (m
bc9d0 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 onth - 14)/12)/4
bc9e0 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 +. 367*(mont
bc9f0 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d h - 2 - (month -
bca00 20 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 14)/12*12)/12 -
bca10 0a 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 . 3*((year +
bca20 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 4900 + (month -
bca30 31 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 14)/12)/100)/4;.
bca40 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 . /* Add the fr
bca50 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 actional hours,
bca60 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 mins and seconds
bca70 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f */. now += (ho
bca80 75 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b ur + 12.0)/24.0;
bca90 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 . now += minute
bcaa0 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b /1440.0;. now +
bcab0 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 = second/86400.0
bcac0 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 ;. *prNow = now
bcad0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
bcae0 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 TEST. if( sqlit
bcaf0 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
bcb00 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 ){. *prNow =
bcb10 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f sqlite3_current_
bcb20 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 time/86400.0 + 2
bcb30 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 440587.5;. }.#e
bcb40 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b ndif. return 0;
bcb50 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .}..static int o
bcb60 73 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 s2GetLastError(s
bcb70 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
bcb80 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
bcb90 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 *zBuf){. retur
bcba0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e n 0;.}../*.** In
bcbb0 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 itialize and dei
bcbc0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 nitialize the op
bcbd0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
bcbe0 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c nterface..*/.SQL
bcbf0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
bcc00 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 te3_os_init(void
bcc10 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
bcc20 74 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d te3_vfs os2Vfs =
bcc30 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 {. 1,
bcc40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
bcc50 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a rsion */. siz
bcc60 65 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 eof(os2File),
bcc70 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 /* szOsFile */.
bcc80 20 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 CCHMAXPATH,
bcc90 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e /* mxPathn
bcca0 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
bccb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bccc0 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f pNext */. "o
bccd0 73 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 s2",
bcce0 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 /* zName */.
bccf0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
bcd00 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 /* pAppData
bcd10 2a 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c */.. os2Open,
bcd20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f /* xO
bcd30 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 pen */. os2De
bcd40 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a lete, /*
bcd50 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 xDelete */.
bcd60 6f 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 os2Access,
bcd70 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f /* xAccess */
bcd80 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 . os2FullPath
bcd90 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c name, /* xFull
bcda0 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 Pathname */.
bcdb0 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 os2DlOpen,
bcdc0 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f /* xDlOpen */
bcdd0 0a 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c . os2DlError,
bcde0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 /* xDlEr
bcdf0 72 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c ror */. os2Dl
bce00 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Sym, /*
bce10 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f xDlSym */. o
bce20 73 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 s2DlClose,
bce30 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f /* xDlClose */
bce40 0a 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 . os2Randomne
bce50 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 ss, /* xRand
bce60 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 omness */. os
bce70 32 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 2Sleep,
bce80 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 /* xSleep */.
bce90 20 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 os2CurrentTime
bcea0 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 , /* xCurrent
bceb0 54 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 Time */. os2G
bcec0 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f etLastError /
bced0 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 * xGetLastError
bcee0 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 */. };. sqlite
bcef0 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 3_vfs_register(&
bcf00 6f 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e os2Vfs, 1);. in
bcf10 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 itUconvObjects()
bcf20 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
bcf30 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 E_OK;.}.SQLITE_A
bcf40 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
bcf50 73 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 s_end(void){. f
bcf60 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 reeUconvObjects(
bcf70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
bcf80 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 TE_OK;.}..#endif
bcf90 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 /* SQLITE_OS_OS
bcfa0 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2 */../*********
bcfb0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f ***** End of os_
bcfc0 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os2.c **********
bcfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcff0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
bd000 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
bd010 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a os_unix.c *****
bd020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd040 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
bd050 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 May 22.**.** Th
bd060 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
bd070 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
bd080 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
bd090 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
bd0a0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
bd0b0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
bd0c0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
bd0d0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
bd0e0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
bd0f0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
bd100 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
bd110 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
bd120 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
bd130 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
bd140 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
bd150 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
bd160 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
bd170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
bd1c0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
bd1d0 61 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 ains the VFS imp
bd1e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 lementation for
bd1f0 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 unix-like operat
bd200 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 ing systems.** i
bd210 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 nclude Linux, Ma
bd220 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c cOSX, *BSD, QNX,
bd230 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 VxWorks, AIX, H
bd240 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e PUX, and others.
bd250 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
bd260 20 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 actually severa
bd270 6c 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 l different VFS
bd280 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
bd290 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a in this file..**
bd2a0 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 The differences
bd2b0 20 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 are in the way
bd2c0 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e that file lockin
bd2d0 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 g is done. The
bd2e0 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 default.** imple
bd2f0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 mentation uses P
bd300 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f osix Advisory Lo
bd310 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 cks. Alternativ
bd320 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
bd330 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 s.** use flock()
bd340 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 , dot-files, var
bd350 69 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 ious proprietary
bd360 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 locking schemas
bd370 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 , or simply.** s
bd380 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 kip locking all
bd390 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 together..**.**
bd3a0 54 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 This source file
bd3b0 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e is organized in
bd3c0 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 to divisions whe
bd3d0 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 re the logic for
bd3e0 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 various.** subf
bd3f0 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 unctions is cont
bd400 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 ained within the
bd410 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 appropriate div
bd420 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a ision. PLEASE.*
bd430 2a 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 * KEEP THE STRUC
bd440 54 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c TURE OF THIS FIL
bd450 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 E INTACT. New c
bd460 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c ode should be pl
bd470 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 aced.** in the c
bd480 6f 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 orrect division
bd490 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c and should be cl
bd4a0 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a early labeled..*
bd4b0 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 *.** The layout
bd4c0 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 of divisions is
bd4d0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
bd4e0 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 * * General-p
bd4f0 75 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 urpose declarati
bd500 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 ons and utility
bd510 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 functions..**
bd520 2a 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 * Unique file I
bd530 44 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 D logic used by
bd540 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 VxWorks..** *
bd550 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 Various locking
bd560 20 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 primitive imple
bd570 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 mentations (all
bd580 65 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 except proxy loc
bd590 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b king):.** +
bd5a0 20 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 for Posix Advis
bd5b0 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 ory Locks.**
bd5c0 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f + for no-op lo
bd5d0 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f cks.** + fo
bd5e0 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 r dot-file locks
bd5f0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 .** + for f
bd600 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a lock() locking.*
bd610 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d * + for nam
bd620 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 ed semaphore loc
bd630 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 ks (VxWorks only
bd640 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 ).** + for
bd650 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c AFP filesystem l
bd660 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c ocks (MacOSX onl
bd670 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 y).** * sqlit
bd680 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 e3_file methods
bd690 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 not associated w
bd6a0 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 ith locking..**
bd6b0 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 * Definitions
bd6c0 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d of sqlite3_io_m
bd6d0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 ethods objects f
bd6e0 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a or all locking.*
bd6f0 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 * methods p
bd700 6c 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e lus "finder" fun
bd710 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 ctions for each
bd720 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a locking method..
bd730 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f ** * sqlite3_
bd740 76 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 vfs method imple
bd750 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 mentations..**
bd760 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d * Locking prim
bd770 69 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 itives for the p
bd780 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e roxy uber-lockin
bd790 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 g-method. (MacOS
bd7a0 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 X only).** *
bd7b0 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 Definitions of s
bd7c0 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
bd7d0 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 ts for all locki
bd7e0 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 ng methods.**
bd7f0 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e plus implemen
bd800 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 tations of sqlit
bd810 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 e3_os_init() and
bd820 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
bd830 29 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 )..**.** $Id: os
bd840 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 32 34 38 20 _unix.c,v 1.248
bd850 32 30 30 39 2f 30 33 2f 33 30 20 30 37 3a 33 39 2009/03/30 07:39
bd860 3a 33 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :35 danielk1977
bd870 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20 53 51 4c Exp $.*/.#if SQL
bd880 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 ITE_OS_UNIX
bd890 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
bd8a0 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e file is used on
bd8b0 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f unix only */../
bd8c0 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 76 *.** There are v
bd8d0 61 72 69 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 arious methods f
bd8e0 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 or file locking
bd8f0 75 73 65 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 used for concurr
bd900 65 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a ency.** control:
bd910 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 .**.** 1. POSI
bd920 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 X locking (the d
bd930 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e efault),.** 2.
bd940 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 No locking,.**
bd950 20 20 33 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 3. Dot-file lo
bd960 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 cking,.** 4. f
bd970 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a lock() locking,.
bd980 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b ** 5. AFP lock
bd990 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a ing (OSX only),.
bd9a0 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f ** 6. Named PO
bd9b0 53 49 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28 SIX semaphores (
bd9c0 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a VXWorks only),.*
bd9d0 2a 20 20 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 * 7. proxy loc
bd9e0 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 king. (OSX only)
bd9f0 0a 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c .**.** Styles 4,
bda00 20 35 2c 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 5, and 7 are on
bda10 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 ly available of
bda20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
bda30 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 CKING_STYLE.** i
bda40 73 20 64 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 s defined to 1.
bda50 20 54 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 The SQLITE_ENAB
bda60 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
bda70 20 61 6c 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 also enables au
bda80 74 6f 6d 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 tomatic.** selec
bda90 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 70 70 72 tion of the appr
bdaa0 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 opriate locking
bdab0 73 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 style based on t
bdac0 68 65 20 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a he filesystem.**
bdad0 20 77 68 65 72 65 20 74 68 65 20 64 61 74 61 62 where the datab
bdae0 61 73 65 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 ase is located.
bdaf0 20 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 .*/.#if !define
bdb00 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
bdb10 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 LOCKING_STYLE).#
bdb20 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 if defined(__A
bdb30 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 PPLE__).# def
bdb40 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c ine SQLITE_ENABL
bdb50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
bdb60 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 1.# else.# d
bdb70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 efine SQLITE_ENA
bdb80 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
bdb90 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e E 0.# endif.#en
bdba0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e dif../*.** Defin
bdbb0 65 20 74 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53 e the OS_VXWORKS
bdbc0 20 70 72 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d pre-processor m
bdbd0 61 63 72 6f 20 74 6f 20 31 20 69 66 20 62 75 69 acro to 1 if bui
bdbe0 6c 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 lding on .** vxw
bdbf0 6f 72 6b 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 orks, or 0 other
bdc00 77 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 wise..*/.#ifndef
bdc10 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 OS_VXWORKS.# i
bdc20 66 20 64 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f f defined(__RTP_
bdc30 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 _) || defined(_W
bdc40 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20 RS_KERNEL).#
bdc50 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b define OS_VXWORK
bdc60 53 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 S 1.# else.#
bdc70 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 define OS_VXWOR
bdc80 4b 53 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 KS 0.# endif.#e
bdc90 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 ndif../*.** Thes
bdca0 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c e #defines shoul
bdcb0 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 d enable >2GB fi
bdcc0 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f le support on Po
bdcd0 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e six if the.** un
bdce0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
bdcf0 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 ng system suppor
bdd00 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f ts it. If the O
bdd10 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 S lacks.** large
bdd20 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 file support, t
bdd30 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e hese should be n
bdd40 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 o-ops..**.** Lar
bdd50 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 ge file support
bdd60 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 can be disabled
bdd70 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 using the -DSQLI
bdd80 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 TE_DISABLE_LFS s
bdd90 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 witch.** on the
bdda0 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 compiler command
bddb0 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 line. This is
bddc0 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 necessary if you
bddd0 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a are compiling.*
bdde0 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 * on a recent ma
bddf0 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 chine (ex: RedHa
bde00 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 t 7.2) but you w
bde10 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f ant your code to
bde20 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f work.** on an o
bde30 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 lder machine (ex
bde40 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20 : RedHat 6.0).
bde50 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f If you compile o
bde60 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 n RedHat 7.2.**
bde70 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74 without this opt
bde80 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 ion, LFS is enab
bde90 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65 le. But LFS doe
bdea0 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 s not exist in t
bdeb0 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 he kernel.** in
bdec0 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 RedHat 6.0, so t
bded0 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f he code won't wo
bdee0 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 rk. Hence, for
bdef0 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a maximum binary.*
bdf00 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f * portability yo
bdf10 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 u should omit LF
bdf20 53 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 S..**.** The pre
bdf30 76 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 20 vious paragraph
bdf40 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 20 32 was written in 2
bdf50 30 30 35 2e 20 20 28 54 68 69 73 20 70 61 72 61 005. (This para
bdf60 67 72 61 70 68 20 69 73 20 77 72 69 74 74 65 6e graph is written
bdf70 0a 2a 2a 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32 .** on 2008-11-2
bdf80 38 2e 29 20 54 68 65 73 65 20 64 61 79 73 2c 20 8.) These days,
bdf90 61 6c 6c 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c all Linux kernel
bdfa0 73 20 73 75 70 70 6f 72 74 20 6c 61 72 67 65 20 s support large
bdfb0 66 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 files, so.** you
bdfc0 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 should probably
bdfd0 20 6c 65 61 76 65 20 4c 46 53 20 65 6e 61 62 6c leave LFS enabl
bdfe0 65 64 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 6d ed. But some em
bdff0 62 65 64 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 bedded platforms
be000 20 6d 69 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c might.** lack L
be010 46 53 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 FS in which case
be020 20 74 68 65 20 53 51 4c 49 54 45 5f 44 49 53 41 the SQLITE_DISA
be030 42 4c 45 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 BLE_LFS macro mi
be040 67 68 74 20 73 74 69 6c 6c 20 62 65 20 75 73 65 ght still be use
be050 66 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ful..*/.#ifndef
be060 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c SQLITE_DISABLE_L
be070 46 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 FS.# define _LAR
be080 47 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a GE_FILE 1.
be090 23 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f # ifndef _FILE_O
be0a0 46 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 FFSET_BITS.# d
be0b0 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 efine _FILE_OFFS
be0c0 45 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 ET_BITS 64.# end
be0d0 69 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 if.# define _LAR
be0e0 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a GEFILE_SOURCE 1.
be0f0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 #endif../*.** st
be100 61 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 andard include f
be110 69 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 iles..*/.#includ
be120 65 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a e <sys/types.h>.
be130 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 #include <sys/st
be140 61 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c at.h>.#include <
be150 66 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 fcntl.h>.#includ
be160 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e e <unistd.h>.#in
be170 63 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e clude <sys/time.
be180 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 h>.#include <err
be190 6e 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 no.h>..#if SQLIT
be1a0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
be1b0 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 _STYLE.# include
be1c0 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 <sys/ioctl.h>.#
be1d0 20 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 if OS_VXWORKS.#
be1e0 20 20 69 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70 include <semap
be1f0 68 6f 72 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 hore.h>.# inclu
be200 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 de <limits.h>.#
be210 65 6c 73 65 0a 23 20 20 69 6e 63 6c 75 64 65 20 else.# include
be220 3c 73 79 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 <sys/file.h>.#
be230 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 include <sys/par
be240 61 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 am.h>.# include
be250 20 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 <sys/mount.h>.#
be260 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a endif.#endif /*
be270 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
be280 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a OCKING_STYLE */.
be290 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 ./*.** If we are
be2a0 20 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73 61 to be thread-sa
be2b0 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65 20 fe, include the
be2c0 70 74 68 72 65 61 64 73 20 68 65 61 64 65 72 20 pthreads header
be2d0 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 and define.** th
be2e0 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 e SQLITE_UNIX_TH
be2f0 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a READS macro..*/.
be300 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
be310 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 DSAFE.# define S
be320 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 QLITE_UNIX_THREA
be330 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a DS 1.#endif../*.
be340 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69 ** Default permi
be350 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 ssions when crea
be360 74 69 6e 67 20 61 20 6e 65 77 20 66 69 6c 65 0a ting a new file.
be370 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
be380 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 E_DEFAULT_FILE_P
be390 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 ERMISSIONS.# def
be3a0 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ine SQLITE_DEFAU
be3b0 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 LT_FILE_PERMISSI
be3c0 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 69 66 0a ONS 0644.#endif.
be3d0 0a 2f 2a 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20 ./*. ** Default
be3e0 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e permissions when
be3f0 20 63 72 65 61 74 69 6e 67 20 61 75 74 6f 20 70 creating auto p
be400 72 6f 78 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66 roxy dir. */.#if
be410 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
be420 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 ULT_PROXYDIR_PER
be430 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e MISSIONS.# defin
be440 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
be450 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 _PROXYDIR_PERMIS
be460 53 49 4f 4e 53 20 30 37 35 35 0a 23 65 6e 64 69 SIONS 0755.#endi
be470 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d f../*.** Maximum
be480 20 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 2d supported path-
be490 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 length..*/.#defi
be4a0 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 ne MAX_PATHNAME
be4b0 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 512../*.** Only
be4c0 73 65 74 20 74 68 65 20 6c 61 73 74 45 72 72 6e set the lastErrn
be4d0 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 63 o if the error c
be4e0 6f 64 65 20 69 73 20 61 20 72 65 61 6c 20 65 72 ode is a real er
be4f0 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 ror and not .**
be500 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 a normal expecte
be510 64 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 d return code of
be520 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 SQLITE_BUSY or
be530 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 SQLITE_OK.*/.#de
be540 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 fine IS_LOCK_ERR
be550 4f 52 28 78 29 20 20 28 28 78 20 21 3d 20 53 51 OR(x) ((x != SQ
be560 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 LITE_OK) && (x !
be570 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 29 29 0a = SQLITE_BUSY)).
be580 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69 78 ../*.** The unix
be590 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 File structure i
be5a0 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 s subclass of sq
be5b0 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 lite3_file speci
be5c0 66 69 63 20 74 6f 20 74 68 65 20 75 6e 69 78 0a fic to the unix.
be5d0 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 ** VFS implement
be5e0 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 ations..*/.typed
be5f0 65 66 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 ef struct unixFi
be600 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73 74 72 le unixFile;.str
be610 75 63 74 20 75 6e 69 78 46 69 6c 65 20 7b 0a 20 uct unixFile {.
be620 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
be630 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68 ods const *pMeth
be640 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 od; /* Always t
be650 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a he first entry *
be660 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f /. struct unixO
be670 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 penCnt *pOpen;
be680 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f /* Info abo
be690 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27 73 ut all open fd's
be6a0 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a on this inode *
be6b0 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c /. struct unixL
be6c0 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 ockInfo *pLock;
be6d0 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f /* Info abo
be6e0 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73 ut locks on this
be6f0 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 inode */. int
be700 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
be710 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
be720 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 The file descrip
be730 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 tor */. int dir
be740 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 fd;
be750 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
be760 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 e descriptor for
be770 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 2a the directory *
be780 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
be790 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 r locktype;
be7a0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 79 70 65 /* The type
be7b0 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e of lock held on
be7c0 20 74 68 69 73 20 66 64 20 2a 2f 0a 20 20 69 6e this fd */. in
be7d0 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 20 t lastErrno;
be7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
be7f0 2a 20 54 68 65 20 75 6e 69 78 20 65 72 72 6e 6f * The unix errno
be800 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 from the last I
be810 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 76 6f /O error */. vo
be820 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 id *lockingConte
be830 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f xt; /
be840 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 * Locking style
be850 73 70 65 63 69 66 69 63 20 73 74 61 74 65 20 2a specific state *
be860 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 /.#if SQLITE_ENA
be870 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
be880 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 E. int openFlag
be890 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
be8a0 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 67 /* The flag
be8b0 73 20 73 70 65 63 69 66 69 65 64 20 61 74 20 6f s specified at o
be8c0 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a pen() */.#endif.
be8d0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
be8e0 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 DSAFE && defined
be8f0 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 (__linux__). pt
be900 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20 20 hread_t tid;
be910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
be920 2a 20 54 68 65 20 74 68 72 65 61 64 20 74 68 61 * The thread tha
be930 74 20 22 6f 77 6e 73 22 20 74 68 69 73 20 75 6e t "owns" this un
be940 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 ixFile */.#endif
be950 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
be960 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 3b 20 int isDelete;
be970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be980 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 /* Delete on
be990 63 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f close if true */
be9a0 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b . struct vxwork
be9b0 73 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 20 sFileId *pId;
be9c0 20 20 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 69 /* Unique fi
be9d0 6c 65 20 49 44 20 2a 2f 0a 23 65 6e 64 69 66 0a le ID */.#endif.
be9e0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
be9f0 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f /* The next gro
bea00 75 70 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 up of variables
bea10 61 72 65 20 75 73 65 64 20 74 6f 20 74 72 61 63 are used to trac
bea20 6b 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 k whether or not
bea30 20 74 68 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 the. ** transa
bea40 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 6e ction counter in
bea50 20 62 79 74 65 73 20 32 34 2d 32 37 20 6f 66 20 bytes 24-27 of
bea60 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 61 database files a
bea70 72 65 20 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 re updated. **
bea80 77 68 65 6e 65 76 65 72 20 61 6e 79 20 70 61 72 whenever any par
bea90 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 t of the databas
beaa0 65 20 63 68 61 6e 67 65 73 2e 20 20 41 6e 20 61 e changes. An a
beab0 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 ssertion fault w
beac0 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69 ill. ** occur i
bead0 66 20 61 20 66 69 6c 65 20 69 73 20 75 70 64 61 f a file is upda
beae0 74 65 64 20 77 69 74 68 6f 75 74 20 61 6c 73 6f ted without also
beaf0 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 74 72 updating the tr
beb00 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 ansaction. ** c
beb10 6f 75 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 ounter. This te
beb20 73 74 20 69 73 20 6d 61 64 65 20 74 6f 20 61 76 st is made to av
beb30 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73 oid new problems
beb40 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65 0a similar to the.
beb50 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63 72 69 62 ** one describ
beb60 65 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 35 ed by ticket #35
beb70 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 84. . */. unsi
beb80 67 6e 65 64 20 63 68 61 72 20 74 72 61 6e 73 43 gned char transC
beb90 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a 20 54 72 ntrChng; /* Tr
beba0 75 65 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 ue if the transa
bebb0 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 63 68 ction counter ch
bebc0 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 anged */. unsig
bebd0 6e 65 64 20 63 68 61 72 20 64 62 55 70 64 61 74 ned char dbUpdat
bebe0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 e; /* Tru
bebf0 65 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66 e if any part of
bec00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 database file c
bec10 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 hanged */. unsi
bec20 67 6e 65 64 20 63 68 61 72 20 69 6e 4e 6f 72 6d gned char inNorm
bec30 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a 20 54 72 alWrite; /* Tr
bec40 75 65 20 69 66 20 69 6e 20 61 20 6e 6f 72 6d 61 ue if in a norma
bec50 6c 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f l write operatio
bec60 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 72 n */.. /* If tr
bec70 75 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 ue, that means w
bec80 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 e are dealing wi
bec90 74 68 20 61 20 64 61 74 61 62 61 73 65 20 66 69 th a database fi
beca0 6c 65 20 74 68 61 74 20 68 61 73 0a 20 20 2a 2a le that has. **
becb0 20 61 20 72 61 6e 67 65 20 6f 66 20 6c 6f 63 6b a range of lock
becc0 69 6e 67 20 62 79 74 65 73 20 66 72 6f 6d 20 50 ing bytes from P
becd0 45 4e 44 49 4e 47 5f 42 59 54 45 20 74 68 72 6f ENDING_BYTE thro
bece0 75 67 68 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 ugh PENDING_BYTE
becf0 2b 35 31 31 0a 20 20 2a 2a 20 77 68 69 63 68 20 +511. ** which
bed00 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 should never be
bed10 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e read or written.
bed20 20 20 41 73 73 65 72 74 73 28 29 20 77 69 6c 6c Asserts() will
bed30 20 76 65 72 69 66 79 20 74 68 69 73 20 2a 2f 0a verify this */.
bed40 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
bed50 69 73 4c 6f 63 6b 61 62 6c 65 3b 20 20 20 20 20 isLockable;
bed60 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 6c 65 /* True if file
bed70 20 6d 69 67 68 74 20 62 65 20 6c 6f 63 6b 65 64 might be locked
bed80 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 */.#endif.#ifde
bed90 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
beda0 2f 2a 20 49 6e 20 74 65 73 74 20 6d 6f 64 65 2c /* In test mode,
bedb0 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 increase the si
bedc0 7a 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ze of this struc
bedd0 74 75 72 65 20 61 20 62 69 74 20 73 6f 20 74 68 ture a bit so th
bede0 61 74 20 0a 20 20 2a 2a 20 69 74 20 69 73 20 6c at . ** it is l
bedf0 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 73 arger than the s
bee00 74 72 75 63 74 20 43 72 61 73 68 46 69 6c 65 20 truct CrashFile
bee10 64 65 66 69 6e 65 64 20 69 6e 20 74 65 73 74 36 defined in test6
bee20 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 .c.. */. char
bee30 61 50 61 64 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 aPadding[32];.#e
bee40 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 ndif.};../*.** I
bee50 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 nclude code that
bee60 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c is common to al
bee70 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a l os_*.c files.*
bee80 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
bee90 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d * Include os_com
beea0 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 mon.h in the mid
beeb0 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 dle of os_unix.c
beec0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
beed0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
beee0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
beef0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
bef00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bef10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bef20 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
bef30 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
bef40 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
bef50 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
bef60 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
bef70 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
bef80 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
bef90 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
befa0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
befb0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
befc0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
befd0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
befe0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
beff0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
bf000 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
bf010 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
bf020 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
bf030 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
bf040 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
bf050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf090 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
bf0a0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
bf0b0 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 macros and a li
bf0c0 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 ttle bit of code
bf0d0 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
bf0e0 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 to.** all of the
bf0f0 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 platform-specif
bf100 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 ic files (os_*.c
bf110 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 ) and is #includ
bf120 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a ed into those.**
bf130 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 files..**.** Th
bf140 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 is file should b
bf150 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 e #included by t
bf160 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 he os_*.c files
bf170 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 only. It is not
bf180 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 a.** general pu
bf190 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c rpose header fil
bf1a0 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 e..**.** $Id: os
bf1b0 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 _common.h,v 1.38
bf1c0 20 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 2009/02/24 18:4
bf1d0 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 0:50 danielk1977
bf1e0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
bf1f0 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a f _OS_COMMON_H_.
bf200 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d #define _OS_COMM
bf210 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 ON_H_../*.** At
bf220 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 least two bugs h
bf230 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 ave slipped in b
bf240 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 ecause we change
bf250 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 d the MEMORY_DEB
bf260 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 UG.** macro to S
bf270 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 QLITE_DEBUG and
bf280 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 some older makef
bf290 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 iles have not ye
bf2a0 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 t made the.** sw
bf2b0 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f itch. The follo
bf2c0 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 wing code should
bf2d0 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 catch this prob
bf2e0 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 lem at compile-t
bf2f0 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d ime..*/.#ifdef M
bf300 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 EMORY_DEBUG.# er
bf310 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f ror "The MEMORY_
bf320 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f DEBUG macro is o
bf330 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 bsolete. Use SQ
bf340 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 LITE_DEBUG inste
bf350 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 ad.".#endif..#if
bf360 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
bf370 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
bf380 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 int sqlite3OSTra
bf390 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 ce = 0;.#define
bf3a0 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 OSTRACE1(X)
bf3b0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
bf3c0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
bf3d0 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 DebugPrintf(X).#
bf3e0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 define OSTRACE2(
bf3f0 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 X,Y) if( s
bf400 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
bf410 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
bf420 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 tf(X,Y).#define
bf430 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 OSTRACE3(X,Y,Z)
bf440 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
bf450 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
bf460 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
bf470 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
bf480 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 CE4(X,Y,Z,A) i
bf490 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
bf4a0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
bf4b0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a Printf(X,Y,Z,A).
bf4c0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 #define OSTRACE5
bf4d0 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 (X,Y,Z,A,B) if(
bf4e0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
bf4f0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
bf500 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 ntf(X,Y,Z,A,B).#
bf510 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
bf520 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 X,Y,Z,A,B,C) \.
bf530 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 if(sqlite3OST
bf540 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 race) sqlite3Deb
bf550 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
bf560 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,B,C).#define OS
bf570 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE7(X,Y,Z,A,B
bf580 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 ,C,D) \. if(s
bf590 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 qlite3OSTrace) s
bf5a0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
bf5b0 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 f(X,Y,Z,A,B,C,D)
bf5c0 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
bf5d0 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 STRACE1(X).#defi
bf5e0 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
bf5f0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bf600 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 3(X,Y,Z).#define
bf610 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c OSTRACE4(X,Y,Z,
bf620 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
bf630 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 CE5(X,Y,Z,A,B).#
bf640 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
bf650 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 X,Y,Z,A,B,C).#de
bf660 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c fine OSTRACE7(X,
bf670 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e Y,Z,A,B,C,D).#en
bf680 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f dif../*.** Macro
bf690 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 s for performanc
bf6a0 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d e tracing. Norm
bf6b0 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e ally turned off.
bf6c0 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 Only works.**
bf6d0 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 on i486 hardware
bf6e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
bf6f0 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 TE_PERFORMANCE_T
bf700 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 RACE../* .** hwt
bf710 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 ime.h contains i
bf720 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 nline assembler
bf730 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 code for impleme
bf740 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 nting .** high-p
bf750 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e erformance timin
bf760 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f g routines..*/./
bf770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
bf780 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 nclude hwtime.h
bf790 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
bf7a0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
bf7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
bf7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
bf7d0 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 egin file hwtime
bf7e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
bf7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
bf810 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 *.** 2008 May 27
bf820 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
bf830 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
bf840 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
bf850 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
bf860 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
bf870 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
bf880 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
bf890 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
bf8a0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
bf8b0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
bf8c0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
bf8d0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
bf8e0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
bf8f0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
bf900 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
bf910 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
bf920 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
bf930 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
bf940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf980 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
bf990 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e file contains in
bf9a0 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f line asm code fo
bf9b0 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 r retrieving "hi
bf9c0 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a gh-performance".
bf9d0 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 ** counters for
bf9e0 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a x86 class CPUs..
bf9f0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d **.** $Id: hwtim
bfa00 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 e.h,v 1.3 2008/0
bfa10 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 8/01 14:33:15 sh
bfa20 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 ane Exp $.*/.#if
bfa30 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a ndef _HWTIME_H_.
bfa40 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f #define _HWTIME_
bfa50 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f H_../*.** The fo
bfa60 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 llowing routine
bfa70 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 only works on pe
bfa80 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 ntium-class (or
bfa90 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 newer) processor
bfaa0 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 s..** It uses th
bfab0 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 e RDTSC opcode t
bfac0 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 o read the cycle
bfad0 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 count value out
bfae0 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 of the.** proce
bfaf0 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 ssor and returns
bfb00 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 that value. Th
bfb10 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 is can be used f
bfb20 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 or high-res.** p
bfb30 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 rofiling..*/.#if
bfb40 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
bfb50 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
bfb60 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 MSC_VER)) && \.
bfb70 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 (defined(i3
bfb80 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 86) || defined(_
bfb90 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 _i386__) || defi
bfba0 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 ned(_M_IX86))..
bfbb0 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 #if defined(__G
bfbc0 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 NUC__).. __inli
bfbd0 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
bfbe0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
bfbf0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 (void){. uns
bfc00 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 igned int lo, hi
bfc10 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f ;. __asm__ _
bfc20 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 _volatile__ ("rd
bfc30 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 tsc" : "=a" (lo)
bfc40 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 , "=d" (hi));.
bfc50 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 return (sqlit
bfc60 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 e_uint64)hi << 3
bfc70 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 2 | lo;. }.. #
bfc80 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 elif defined(_MS
bfc90 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c C_VER).. __decl
bfca0 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e spec(naked) __in
bfcb0 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 line sqlite_uint
bfcc0 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 64 __cdecl sqlit
bfcd0 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
bfce0 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 __asm {.
bfcf0 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 rdtsc.
bfd00 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 ret ; r
bfd10 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 eturn value at E
bfd20 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 DX:EAX. }.
bfd30 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c }.. #endif..#el
bfd40 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
bfd50 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
bfd60 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 (__x86_64__))..
bfd70 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
bfd80 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
bfd90 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
bfda0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
bfdb0 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f ng val;. __
bfdc0 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
bfdd0 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
bfde0 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 A" (val));.
bfdf0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d return val;. }
bfe00 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 . .#elif (define
bfe10 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 d(__GNUC__) && d
bfe20 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 efined(__ppc__))
bfe30 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
bfe40 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
bfe50 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
bfe60 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
bfe70 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 long long retva
bfe80 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 l;. unsigne
bfe90 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 d long junk;.
bfea0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
bfeb0 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 atile__ ("\n\.
bfec0 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 1:
bfed0 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 mftbu %1\n\.
bfee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bfef0 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 mftb %L0\n\.
bff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bff10 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 mftbu %0\n\.
bff20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bff30 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e cmpw %0,%1\n
bff40 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
bff50 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a bne 1b".
bff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bff70 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c : "=r" (retval
bff80 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b ), "=r" (junk));
bff90 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 . return re
bffa0 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 tval;. }..#else
bffb0 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 .. #error Need
bffc0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
bffd0 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 f sqlite3Hwtime(
bffe0 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 ) for your platf
bfff0 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 orm... /*. **
c0000 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f To compile witho
c0010 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 ut implementing
c0020 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
c0030 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 for your platfor
c0040 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 m,. ** you can
c0050 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 remove the above
c0060 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 #error and use
c0070 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
c0080 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e ** stub function
c0090 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 . You will lose
c00a0 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 timing support
c00b0 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 for many. ** of
c00c0 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 the debugging a
c00d0 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 nd testing utili
c00e0 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f ties, but it sho
c00f0 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 uld at. ** leas
c0100 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 t compile and ru
c0110 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 n.. */.SQLITE_P
c0120 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f RIVATE sqlite_
c0130 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
c0140 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 time(void){ retu
c0150 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 rn ((sqlite_uint
c0160 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 64)0); }..#endif
c0170 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 ..#endif /* !def
c0180 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 ined(_HWTIME_H_)
c0190 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
c01a0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 **** End of hwti
c01b0 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a me.h ***********
c01c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c01d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c01e0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c01f0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
c0200 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
c0210 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 f in os_common.h
c0220 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c0230 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c ***/..static sql
c0240 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 ite_uint64 g_sta
c0250 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 rt;.static sqlit
c0260 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 e_uint64 g_elaps
c0270 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 ed;.#define TIME
c0280 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f R_START g_
c0290 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 start=sqlite3Hwt
c02a0 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 ime().#define TI
c02b0 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 MER_END
c02c0 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 g_elapsed=sqlite
c02d0 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 3Hwtime()-g_star
c02e0 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f t.#define TIMER_
c02f0 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c ELAPSED g_el
c0300 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 apsed.#else.#def
c0310 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a ine TIMER_START.
c0320 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e #define TIMER_EN
c0330 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f D.#define TIMER_
c0340 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 ELAPSED ((sq
c0350 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 lite_uint64)0).#
c0360 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
c0370 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 we compile with
c0380 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 the SQLITE_TEST
c0390 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 macro set, then
c03a0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c the following bl
c03b0 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 ock.** of code w
c03c0 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 ill give us the
c03d0 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c ability to simul
c03e0 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 ate a disk I/O e
c03f0 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 rror. This.** i
c0400 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
c0410 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 ng the I/O recov
c0420 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 ery logic..*/.#i
c0430 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
c0440 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
c0450 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c0460 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 _hit = 0;
c0470 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
c0480 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f mber of I/O Erro
c0490 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 rs */.SQLITE_API
c04a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
c04b0 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 error_hardhit =
c04c0 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 0; /* Num
c04d0 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 ber of non-benig
c04e0 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 n errors */.SQLI
c04f0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c0500 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
c0510 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ing = 0;
c0520 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f /* Count down to
c0530 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 first I/O error
c0540 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
c0550 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
c0560 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b ror_persist = 0;
c0570 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
c0580 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 if I/O errors pe
c0590 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f rsist */.SQLITE_
c05a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c05b0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 io_error_benign
c05c0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
c05d0 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 True if errors a
c05e0 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c re benign */.SQL
c05f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c0600 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
c0610 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 ding = 0;.SQLITE
c0620 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
c0630 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 _diskfull = 0;.#
c0640 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
c0650 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 OErrorBenign(X)
c0660 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c0670 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 _benign=(X).#def
c0680 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
c0690 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 ror(CODE) \. i
c06a0 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 f( (sqlite3_io_e
c06b0 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 rror_persist &&
c06c0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c06d0 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c _hit) \. |
c06e0 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 | sqlite3_io_err
c06f0 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 or_pending-- ==
c0700 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 1 ) \.
c0710 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 { local_ioe
c0720 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 rr(); CODE; }.st
c0730 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f atic void local_
c0740 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 ioerr(){. IOTRA
c0750 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b CE(("IOERR\n"));
c0760 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 . sqlite3_io_er
c0770 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 ror_hit++;. if(
c0780 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 !sqlite3_io_err
c0790 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 or_benign ) sqli
c07a0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 te3_io_error_har
c07b0 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e dhit++;.}.#defin
c07c0 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 e SimulateDiskfu
c07d0 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a llError(CODE) \.
c07e0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 if( sqlite3_d
c07f0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 iskfull_pending
c0800 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 ){ \. if( sq
c0810 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
c0820 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c ending == 1 ){ \
c0830 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f . local_io
c0840 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 err(); \.
c0850 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
c0860 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 = 1; \. s
c0870 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c0880 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 hit = 1; \.
c0890 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d CODE; \. }
c08a0 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 else{ \. s
c08b0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
c08c0 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 pending--; \.
c08d0 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 } \. }.#else
c08e0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
c08f0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 eIOErrorBenign(X
c0900 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
c0910 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 teIOError(A).#de
c0920 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
c0930 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 kfullError(A).#e
c0940 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e ndif../*.** When
c0950 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 testing, keep a
c0960 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 count of the nu
c0970 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c mber of open fil
c0980 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 es..*/.#ifdef SQ
c0990 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
c09a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
c09b0 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 _open_file_count
c09c0 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 = 0;.#define Op
c09d0 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 enCounter(X) sq
c09e0 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f lite3_open_file_
c09f0 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 count+=(X).#else
c0a00 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 .#define OpenCou
c0a10 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a nter(X).#endif..
c0a20 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
c0a30 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f ed(_OS_COMMON_H_
c0a40 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
c0a50 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f ***** End of os_
c0a60 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
c0a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0a90 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
c0aa0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
c0ab0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
c0ac0 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 ff in os_unix.c
c0ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0ae0 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 ****/../*.** Def
c0af0 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 ine various macr
c0b00 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 os that are miss
c0b10 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 ing from some sy
c0b20 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 stems..*/.#ifnde
c0b30 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 f O_LARGEFILE.#
c0b40 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 define O_LARGEFI
c0b50 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 LE 0.#endif.#ifd
c0b60 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c ef SQLITE_DISABL
c0b70 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f E_LFS.# undef O_
c0b80 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 LARGEFILE.# defi
c0b90 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 ne O_LARGEFILE 0
c0ba0 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
c0bb0 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 O_NOFOLLOW.# def
c0bc0 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 ine O_NOFOLLOW 0
c0bd0 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
c0be0 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e O_BINARY.# defin
c0bf0 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e e O_BINARY 0.#en
c0c00 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 dif../*.** The D
c0c10 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e JGPP compiler en
c0c20 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 vironment looks
c0c30 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 mostly like Unix
c0c40 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b , but it.** lack
c0c50 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 s the fcntl() sy
c0c60 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 stem call. So r
c0c70 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 edefine fcntl()
c0c80 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a to be something.
c0c90 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 ** that always s
c0ca0 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d ucceeds. This m
c0cb0 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e eans that lockin
c0cc0 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 g does not occur
c0cd0 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e under.** DJGPP.
c0ce0 20 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 But it is DOS
c0cf0 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 - what did you e
c0d00 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 xpect?.*/.#ifdef
c0d10 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 __DJGPP__.# def
c0d20 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 ine fcntl(A,B,C)
c0d30 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
c0d40 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 The threadid ma
c0d50 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 cro resolves to
c0d60 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 the thread-id or
c0d70 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 to 0. Used for
c0d80 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 .** testing and
c0d90 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a debugging only..
c0da0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
c0db0 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 READSAFE.#define
c0dc0 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61 threadid pthrea
c0dd0 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 d_self().#else.#
c0de0 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 define threadid
c0df0 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
c0e00 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e Helper function
c0e10 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 s to obtain and
c0e20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 relinquish the g
c0e30 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 0a 2a 2f 0a lobal mutex..*/.
c0e40 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 static void unix
c0e50 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29 EnterMutex(void)
c0e60 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
c0e70 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d x_enter(sqlite3M
c0e80 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
c0e90 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
c0ea0 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 STER));.}.static
c0eb0 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 4d void unixLeaveM
c0ec0 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 utex(void){. sq
c0ed0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
c0ee0 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c e(sqlite3MutexAl
c0ef0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
c0f00 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 _STATIC_MASTER))
c0f10 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c ;.}...#ifdef SQL
c0f20 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ITE_DEBUG./*.**
c0f30 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 Helper function
c0f40 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 for printing out
c0f50 20 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 trace informati
c0f60 6f 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e on from debuggin
c0f70 67 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 g.** binaries. T
c0f80 68 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 his returns the
c0f90 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 string represeta
c0fa0 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 tion of the supp
c0fb0 6c 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 lied.** integer
c0fc0 6c 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 lock-type..*/.st
c0fd0 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
c0fe0 2a 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e *locktypeName(in
c0ff0 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 t locktype){. s
c1000 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 witch( locktype
c1010 29 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 ){. case NO_LOC
c1020 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 K: return "NONE"
c1030 3b 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f ;. case SHARED_
c1040 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 LOCK: return "SH
c1050 41 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45 ARED";. case RE
c1060 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 SERVED_LOCK: ret
c1070 75 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a urn "RESERVED";.
c1080 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c case PENDING_L
c1090 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e OCK: return "PEN
c10a0 44 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58 DING";. case EX
c10b0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 CLUSIVE_LOCK: re
c10c0 74 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 turn "EXCLUSIVE"
c10d0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 ;. }. return "
c10e0 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 ERROR";.}.#endif
c10f0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
c1100 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a LOCK_TRACE./*.**
c1110 20 50 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 Print out infor
c1120 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c mation about all
c1130 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 locking operati
c1140 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ons..**.** This
c1150 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
c1160 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 for troubleshoot
c1170 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c ing locks on mul
c1180 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c tithreaded.** pl
c1190 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 atforms. Enable
c11a0 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 by compiling wi
c11b0 74 68 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f th the -DSQLITE_
c11c0 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f LOCK_TRACE.** co
c11d0 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f mmand-line optio
c11e0 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 n on the compile
c11f0 72 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 r. This code is
c1200 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 normally.** tur
c1210 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 ned off..*/.stat
c1220 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 ic int lockTrace
c1230 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c (int fd, int op,
c1240 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 struct flock *p
c1250 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 ){. char *zOpNa
c1260 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e me, *zType;. in
c1270 74 20 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 t s;. int saved
c1280 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d Errno;. if( op=
c1290 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 =F_GETLK ){.
c12a0 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b zOpName = "GETLK
c12b0 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f ";. }else if( o
c12c0 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 p==F_SETLK ){.
c12d0 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 zOpName = "SET
c12e0 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 LK";. }else{.
c12f0 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 s = fcntl(fd,
c1300 6f 70 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 op, p);. sqli
c1310 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 te3DebugPrintf("
c1320 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 fcntl unknown %d
c1330 20 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f %d %d\n", fd, o
c1340 70 2c 20 73 29 3b 0a 20 20 20 20 72 65 74 75 72 p, s);. retur
c1350 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 n s;. }. if( p
c1360 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 ->l_type==F_RDLC
c1370 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d K ){. zType =
c1380 20 22 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 "RDLCK";. }els
c1390 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d e if( p->l_type=
c13a0 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_WRLCK ){.
c13b0 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b zType = "WRLCK";
c13c0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
c13d0 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 l_type==F_UNLCK
c13e0 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 ){. zType = "
c13f0 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b UNLCK";. }else{
c1400 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 . assert( 0 )
c1410 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
c1420 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 p->l_whence==SEE
c1430 4b 5f 53 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 K_SET );. s = f
c1440 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b cntl(fd, op, p);
c1450 0a 20 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 . savedErrno =
c1460 65 72 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 errno;. sqlite3
c1470 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e DebugPrintf("fcn
c1480 74 6c 20 25 64 20 25 64 20 25 73 20 25 73 20 25 tl %d %d %s %s %
c1490 64 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 d %d %d %d\n",.
c14a0 20 20 20 20 74 68 72 65 61 64 69 64 2c 20 66 64 threadid, fd
c14b0 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 , zOpName, zType
c14c0 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 , (int)p->l_star
c14d0 74 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e t, (int)p->l_len
c14e0 2c 0a 20 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c ,. (int)p->l
c14f0 5f 70 69 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 _pid, s);. if(
c1500 73 3d 3d 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 s==(-1) && op==F
c1510 5f 53 45 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f _SETLK && (p->l_
c1520 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c type==F_RDLCK ||
c1530 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 p->l_type==F_WR
c1540 4c 43 4b 29 20 29 7b 0a 20 20 20 20 73 74 72 75 LCK) ){. stru
c1550 63 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 ct flock l2;.
c1560 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 l2 = *p;. fc
c1570 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c ntl(fd, F_GETLK,
c1580 20 26 6c 32 29 3b 0a 20 20 20 20 69 66 28 20 6c &l2);. if( l
c1590 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 2.l_type==F_RDLC
c15a0 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 K ){. zType
c15b0 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 = "RDLCK";.
c15c0 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 }else if( l2.l_t
c15d0 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a ype==F_WRLCK ){.
c15e0 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 zType = "W
c15f0 52 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 RLCK";. }else
c1600 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d if( l2.l_type==
c1610 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 F_UNLCK ){.
c1620 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 zType = "UNLCK"
c1630 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
c1640 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a assert( 0 );.
c1650 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
c1660 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 3DebugPrintf("fc
c1670 6e 74 6c 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 ntl-failure-reas
c1680 6f 6e 3a 20 25 73 20 25 64 20 25 64 20 25 64 5c on: %s %d %d %d\
c1690 6e 22 2c 0a 20 20 20 20 20 20 20 7a 54 79 70 65 n",. zType
c16a0 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 , (int)l2.l_star
c16b0 74 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e t, (int)l2.l_len
c16c0 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 , (int)l2.l_pid)
c16d0 3b 0a 20 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 ;. }. errno =
c16e0 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 savedErrno;. re
c16f0 74 75 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e turn s;.}.#defin
c1700 65 20 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 e fcntl lockTrac
c1710 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 e.#endif /* SQLI
c1720 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f TE_LOCK_TRACE */
c1730 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 ..../*.** This r
c1740 6f 75 74 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 outine translate
c1750 73 20 61 20 73 74 61 6e 64 61 72 64 20 50 4f 53 s a standard POS
c1760 49 58 20 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e IX errno code in
c1770 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 to something.**
c1780 75 73 65 66 75 6c 20 74 6f 20 74 68 65 20 63 6c useful to the cl
c1790 69 65 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c ients of the sql
c17a0 69 74 65 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 ite3 functions.
c17b0 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 Specifically, i
c17c0 74 20 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 t is.** intended
c17d0 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 61 20 to translate a
c17e0 76 61 72 69 65 74 79 20 6f 66 20 22 74 72 79 20 variety of "try
c17f0 61 67 61 69 6e 22 20 65 72 72 6f 72 73 20 69 6e again" errors in
c1800 74 6f 20 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a to SQLITE_BUSY.*
c1810 2a 20 61 6e 64 20 61 20 76 61 72 69 65 74 79 20 * and a variety
c1820 6f 66 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 of "please close
c1830 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
c1840 70 74 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 ptor NOW" errors
c1850 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 into .** SQLITE
c1860 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 _IOERR.** .** Er
c1870 72 6f 72 73 20 64 75 72 69 6e 67 20 69 6e 69 74 rors during init
c1880 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f ialization of lo
c1890 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 cks, or file sys
c18a0 74 65 6d 20 73 75 70 70 6f 72 74 20 66 6f 72 20 tem support for
c18b0 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 locks,.** should
c18c0 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 handle ENOLCK,
c18d0 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 ENOTSUP, EOPNOTS
c18e0 55 50 50 20 73 65 70 61 72 61 74 65 6c 79 2e 0a UPP separately..
c18f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
c1900 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
c1910 69 78 45 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 ixError(int posi
c1920 78 45 72 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 xError, int sqli
c1930 74 65 49 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 teIOErr) {. swi
c1940 74 63 68 20 28 70 6f 73 69 78 45 72 72 6f 72 29 tch (posixError)
c1950 20 7b 0a 20 20 63 61 73 65 20 30 3a 20 0a 20 20 {. case 0: .
c1960 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
c1970 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 OK;. . case
c1980 45 41 47 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 EAGAIN:. case E
c1990 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 TIMEDOUT:. case
c19a0 20 45 42 55 53 59 3a 0a 20 20 63 61 73 65 20 45 EBUSY:. case E
c19b0 49 4e 54 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f INTR:. case ENO
c19c0 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 LCK: . /* ra
c19d0 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72 79 20 65 ndom NFS retry e
c19e0 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 rror, unless dur
c19f0 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 ing file system
c1a00 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 support . *
c1a10 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 introspection, i
c1a20 6e 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 n which it actua
c1a30 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 lly means what i
c1a40 74 20 73 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 t says */. re
c1a50 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
c1a60 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 ;. . case EA
c1a70 43 43 45 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 CCES: . /* EA
c1a80 43 43 45 53 20 69 73 20 6c 69 6b 65 20 45 41 47 CCES is like EAG
c1a90 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 AIN during locki
c1aa0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 ng operations, b
c1ab0 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 ut not any other
c1ac0 20 74 69 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 time*/. if(
c1ad0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 (sqliteIOErr ==
c1ae0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
c1af0 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 K) || ..(sqliteI
c1b00 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 OErr == SQLITE_I
c1b10 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 7c 7c 20 OERR_UNLOCK) ||
c1b20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d ..(sqliteIOErr =
c1b30 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 = SQLITE_IOERR_R
c1b40 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73 71 6c 69 DLOCK) ||..(sqli
c1b50 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 teIOErr == SQLIT
c1b60 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
c1b70 45 52 56 45 44 4c 4f 43 4b 29 20 29 7b 0a 20 20 ERVEDLOCK) ){.
c1b80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
c1b90 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 E_BUSY;. }.
c1ba0 20 20 2f 2a 20 65 6c 73 65 20 66 61 6c 6c 20 74 /* else fall t
c1bb0 68 72 6f 75 67 68 20 2a 2f 0a 20 20 63 61 73 65 hrough */. case
c1bc0 20 45 50 45 52 4d 3a 20 0a 20 20 20 20 72 65 74 EPERM: . ret
c1bd0 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b urn SQLITE_PERM;
c1be0 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 44 45 . . case EDE
c1bf0 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74 75 72 6e ADLK:. return
c1c00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c SQLITE_IOERR_BL
c1c10 4f 43 4b 45 44 3b 0a 20 20 20 20 0a 23 69 66 20 OCKED;. .#if
c1c20 45 4f 50 4e 4f 54 53 55 50 50 21 3d 45 4e 4f 54 EOPNOTSUPP!=ENOT
c1c30 53 55 50 0a 20 20 63 61 73 65 20 45 4f 50 4e 4f SUP. case EOPNO
c1c40 54 53 55 50 50 3a 20 0a 20 20 20 20 2f 2a 20 73 TSUPP: . /* s
c1c50 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 74 65 omething went te
c1c60 72 72 69 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c rribly awry, unl
c1c70 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 ess during file
c1c80 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a system support .
c1c90 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 * introspec
c1ca0 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 tion, in which i
c1cb0 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 t actually means
c1cc0 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f what it says */
c1cd0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 45 .#endif.#ifdef E
c1ce0 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4e NOTSUP. case EN
c1cf0 4f 54 53 55 50 3a 20 0a 20 20 20 20 2f 2a 20 69 OTSUP: . /* i
c1d00 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e 6c 65 73 nvalid fd, unles
c1d10 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 s during file sy
c1d20 73 74 65 6d 20 73 75 70 70 6f 72 74 20 69 6e 74 stem support int
c1d30 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 rospection, in w
c1d40 68 69 63 68 20 0a 20 20 20 20 20 2a 20 69 74 20 hich . * it
c1d50 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 actually means w
c1d60 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 hat it says */.#
c1d70 65 6e 64 69 66 0a 20 20 63 61 73 65 20 45 49 4f endif. case EIO
c1d80 3a 0a 20 20 63 61 73 65 20 45 42 41 44 46 3a 0a :. case EBADF:.
c1d90 20 20 63 61 73 65 20 45 49 4e 56 41 4c 3a 0a 20 case EINVAL:.
c1da0 20 63 61 73 65 20 45 4e 4f 54 43 4f 4e 4e 3a 0a case ENOTCONN:.
c1db0 20 20 63 61 73 65 20 45 4e 4f 44 45 56 3a 0a 20 case ENODEV:.
c1dc0 20 63 61 73 65 20 45 4e 58 49 4f 3a 0a 20 20 63 case ENXIO:. c
c1dd0 61 73 65 20 45 4e 4f 45 4e 54 3a 0a 20 20 63 61 ase ENOENT:. ca
c1de0 73 65 20 45 53 54 41 4c 45 3a 0a 20 20 63 61 73 se ESTALE:. cas
c1df0 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20 20 2f 2a e ENOSYS:. /*
c1e00 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 66 6f these should fo
c1e10 72 63 65 20 74 68 65 20 63 6c 69 65 6e 74 20 74 rce the client t
c1e20 6f 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 o close the file
c1e30 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63 74 20 2a and reconnect *
c1e40 2f 0a 20 20 20 20 0a 20 20 64 65 66 61 75 6c 74 /. . default
c1e50 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 : . return sq
c1e60 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d liteIOErr;. }.}
c1e70 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..../***********
c1e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1ec0 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
c1ed0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69 ****** Begin Uni
c1ee0 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c que File ID Util
c1ef0 69 74 79 20 55 73 65 64 20 42 79 20 56 78 57 6f ity Used By VxWo
c1f00 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a rks ************
c1f10 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73 ***.**.** On mos
c1f20 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e t versions of un
c1f30 69 78 2c 20 77 65 20 63 61 6e 20 67 65 74 20 61 ix, we can get a
c1f40 20 75 6e 69 71 75 65 20 49 44 20 66 6f 72 20 61 unique ID for a
c1f50 20 66 69 6c 65 20 62 79 20 63 6f 6e 63 61 74 65 file by concate
c1f60 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65 nating.** the de
c1f70 76 69 63 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 vice number and
c1f80 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 the inode number
c1f90 2e 20 20 42 75 74 20 74 68 69 73 20 64 6f 65 73 . But this does
c1fa0 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 not work on VxW
c1fb0 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f orks..** On VxWo
c1fc0 72 6b 73 2c 20 61 20 75 6e 69 71 75 65 20 66 69 rks, a unique fi
c1fd0 6c 65 20 69 64 20 6d 75 73 74 20 62 65 20 62 61 le id must be ba
c1fe0 73 65 64 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e sed on the canon
c1ff0 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a ical filename..*
c2000 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 *.** A pointer t
c2010 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 o an instance of
c2020 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
c2030 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 tructure can be
c2040 75 73 65 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69 used as a.** uni
c2050 71 75 65 20 66 69 6c 65 20 49 44 20 69 6e 20 56 que file ID in V
c2060 78 57 6f 72 6b 73 2e 20 20 45 61 63 68 20 69 6e xWorks. Each in
c2070 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
c2080 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
c2090 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 s.** a copy of t
c20a0 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c he canonical fil
c20b0 65 6e 61 6d 65 2e 20 20 54 68 65 72 65 20 69 73 ename. There is
c20c0 20 61 6c 73 6f 20 61 20 72 65 66 65 72 65 6e 63 also a referenc
c20d0 65 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68 e count. .** Th
c20e0 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 e structure is r
c20f0 65 63 6c 61 69 6d 65 64 20 77 68 65 6e 20 74 68 eclaimed when th
c2100 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e e number of poin
c2110 74 65 72 73 20 74 6f 20 69 74 20 64 72 6f 70 73 ters to it drops
c2120 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a to.** zero..**.
c2130 2a 2a 20 54 68 65 72 65 20 61 72 65 20 6e 65 76 ** There are nev
c2140 65 72 20 76 65 72 79 20 6d 61 6e 79 20 66 69 6c er very many fil
c2150 65 73 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 es open at one t
c2160 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 ime and lookups
c2170 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72 are not.** a per
c2180 66 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 formance-critica
c2190 6c 20 70 61 74 68 2c 20 73 6f 20 69 74 20 69 73 l path, so it is
c21a0 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 70 sufficient to p
c21b0 75 74 20 74 68 65 73 65 0a 2a 2a 20 73 74 72 75 ut these.** stru
c21c0 63 74 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b ctures on a link
c21d0 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 ed list..*/.stru
c21e0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
c21f0 20 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f {. struct vxwo
c2200 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 78 74 rksFileId *pNext
c2210 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20 ; /* Next in a
c2220 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c list of them all
c2230 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
c2240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2250 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
c2260 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
c2270 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 his one */. int
c2280 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 nName;
c2290 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
c22a0 6e 67 74 68 20 6f 66 20 74 68 65 20 7a 43 61 6e ngth of the zCan
c22b0 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72 onicalName[] str
c22c0 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ing */. char *z
c22d0 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20 CanonicalName;
c22e0 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69 /* Canoni
c22f0 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a cal filename */.
c2300 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 };..#if OS_VXWOR
c2310 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e KS./* .** All un
c2320 69 71 75 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 ique filenames a
c2330 72 65 20 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e re held on a lin
c2340 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 ked list headed
c2350 62 79 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 by this.** varia
c2360 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ble:.*/.static s
c2370 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c truct vxworksFil
c2380 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 eId *vxworksFile
c2390 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a List = 0;../*.**
c23a0 20 53 69 6d 70 6c 69 66 79 20 61 20 66 69 6c 65 Simplify a file
c23b0 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73 20 63 61 name into its ca
c23c0 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20 nonical form.**
c23d0 62 79 20 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f by making the fo
c23e0 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 3a llowing changes:
c23f0 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69 .**.** * removi
c2400 6e 67 20 61 6e 79 20 74 72 61 69 6c 69 6e 67 20 ng any trailing
c2410 61 6e 64 20 64 75 70 6c 69 63 61 74 65 20 2f 0a and duplicate /.
c2420 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e ** * convert /.
c2430 2f 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a / into just /.**
c2440 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e * convert /A/.
c2450 2e 2f 20 77 68 65 72 65 20 41 20 69 73 20 61 6e ./ where A is an
c2460 79 20 73 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e y simple name in
c2470 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20 to just /.**.**
c2480 43 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 Changes are made
c2490 20 69 6e 2d 70 6c 61 63 65 2e 20 20 52 65 74 75 in-place. Retu
c24a0 72 6e 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 20 rn the new name
c24b0 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 length..**.** Th
c24c0 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e e original filen
c24d0 61 6d 65 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e ame is in z[0..n
c24e0 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 74 68 65 -1]. Return the
c24f0 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 number of.** ch
c2500 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 aracters in the
c2510 73 69 6d 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e simplified name.
c2520 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 .*/.static int v
c2530 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 xworksSimplifyNa
c2540 6d 65 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 me(char *z, int
c2550 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a n){. int i, j;.
c2560 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 while( n>1 &&
c2570 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e z[n-1]=='/' ){ n
c2580 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d --; }. for(i=j=
c2590 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 0; i<n; i++){.
c25a0 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20 if( z[i]=='/'
c25b0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 ){. if( z[i
c25c0 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 +1]=='/' ) conti
c25d0 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 7a nue;. if( z
c25e0 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b [i+1]=='.' && i+
c25f0 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2<n && z[i+2]=='
c2600 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20 /' ){. i
c2610 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 6f += 1;. co
c2620 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a ntinue;. }.
c2630 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d if( z[i+1]
c2640 3d 3d 27 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26 =='.' && i+3<n &
c2650 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26 & z[i+2]=='.' &&
c2660 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a z[i+3]=='/' ){.
c2670 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a while( j
c2680 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f >0 && z[j-1]!='/
c2690 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 ' ){ j--; }.
c26a0 20 20 20 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a if( j>0 ){ j
c26b0 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 20 --; }. i
c26c0 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 63 6f += 2;. co
c26d0 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a ntinue;. }.
c26e0 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d }. z[j++]
c26f0 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a = z[i];. }. z
c2700 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 [j] = 0;. retur
c2710 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 n j;.}../*.** Fi
c2720 6e 64 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 nd a unique file
c2730 20 49 44 20 66 6f 72 20 74 68 65 20 67 69 76 65 ID for the give
c2740 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 6e n absolute pathn
c2750 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 ame. Return.**
c2760 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
c2770 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f vxworksFileId o
c2780 62 6a 65 63 74 2e 20 20 54 68 69 73 20 70 6f 69 bject. This poi
c2790 6e 74 65 72 20 69 73 20 74 68 65 20 75 6e 69 71 nter is the uniq
c27a0 75 65 0a 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a ue.** file ID..*
c27b0 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 66 20 66 69 *.** The nRef fi
c27c0 65 6c 64 20 6f 66 20 74 68 65 20 76 78 77 6f 72 eld of the vxwor
c27d0 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 ksFileId object
c27e0 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 is incremented b
c27f0 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a efore.** the obj
c2800 65 63 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e ect is returned.
c2810 20 20 41 20 6e 65 77 20 76 78 77 6f 72 6b 73 46 A new vxworksF
c2820 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20 ileId object is
c2830 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61 created.** and a
c2840 64 64 65 64 20 74 6f 20 74 68 65 20 67 6c 6f 62 dded to the glob
c2850 61 6c 20 6c 69 73 74 20 69 66 20 6e 65 63 65 73 al list if neces
c2860 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 sary..**.** If a
c2870 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
c2880 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c on error occurs,
c2890 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f return NULL..*/
c28a0 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76 .static struct v
c28b0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 xworksFileId *vx
c28c0 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 worksFindFileId(
c28d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 62 73 const char *zAbs
c28e0 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74 oluteName){. st
c28f0 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
c2900 49 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 Id *pNew;
c2910 20 20 2f 2a 20 73 65 61 72 63 68 20 6b 65 79 20 /* search key
c2920 61 6e 64 20 6e 65 77 20 66 69 6c 65 20 49 44 20 and new file ID
c2930 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f */. struct vxwo
c2940 72 6b 73 46 69 6c 65 49 64 20 2a 70 43 61 6e 64 rksFileId *pCand
c2950 69 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20 idate; /* For
c2960 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 looping over exi
c2970 73 74 69 6e 67 20 66 69 6c 65 20 49 44 73 20 2a sting file IDs *
c2980 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 /. int n;
c2990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c29a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 /* Lengt
c29b0 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61 h of zAbsoluteNa
c29c0 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 me string */..
c29d0 61 73 73 65 72 74 28 20 7a 41 62 73 6f 6c 75 74 assert( zAbsolut
c29e0 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b eName[0]=='/' );
c29f0 0a 20 20 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c . n = (int)strl
c2a00 65 6e 28 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 en(zAbsoluteName
c2a10 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 );. pNew = sqli
c2a20 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 te3_malloc( size
c2a30 6f 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31 of(*pNew) + (n+1
c2a40 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d ) );. if( pNew=
c2a50 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
c2a60 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 pNew->zCanonica
c2a70 6c 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 lName = (char*)&
c2a80 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 pNew[1];. memcp
c2a90 79 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 y(pNew->zCanonic
c2aa0 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74 alName, zAbsolut
c2ab0 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e eName, n+1);. n
c2ac0 20 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 = vxworksSimpli
c2ad0 66 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61 fyName(pNew->zCa
c2ae0 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b nonicalName, n);
c2af0 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f .. /* Search fo
c2b00 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e r an existing en
c2b10 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 69 6e try that matchin
c2b20 67 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 g the canonical
c2b30 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f name.. ** If fo
c2b40 75 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 und, increment t
c2b50 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
c2b60 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 nt and return a
c2b70 70 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20 pointer to. **
c2b80 74 68 65 20 65 78 69 73 74 69 6e 67 20 66 69 6c the existing fil
c2b90 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 e ID.. */. uni
c2ba0 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
c2bb0 20 66 6f 72 28 70 43 61 6e 64 69 64 61 74 65 3d for(pCandidate=
c2bc0 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b vxworksFileList;
c2bd0 20 70 43 61 6e 64 69 64 61 74 65 3b 20 70 43 61 pCandidate; pCa
c2be0 6e 64 69 64 61 74 65 3d 70 43 61 6e 64 69 64 61 ndidate=pCandida
c2bf0 74 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 te->pNext){.
c2c00 69 66 28 20 70 43 61 6e 64 69 64 61 74 65 2d 3e if( pCandidate->
c2c10 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26 nName==n . &
c2c20 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69 64 & memcmp(pCandid
c2c30 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e ate->zCanonicalN
c2c40 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f ame, pNew->zCano
c2c50 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30 nicalName, n)==0
c2c60 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 73 . ){. s
c2c70 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 qlite3_free(pNew
c2c80 29 3b 0a 20 20 20 20 20 20 20 70 43 61 6e 64 69 );. pCandi
c2c90 64 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 date->nRef++;.
c2ca0 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 unixLeaveMu
c2cb0 74 65 78 28 29 3b 0a 20 20 20 20 20 20 20 72 65 tex();. re
c2cc0 74 75 72 6e 20 70 43 61 6e 64 69 64 61 74 65 3b turn pCandidate;
c2cd0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
c2ce0 20 4e 6f 20 6d 61 74 63 68 20 77 61 73 20 66 6f No match was fo
c2cf0 75 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61 und. We will ma
c2d00 6b 65 20 61 20 6e 65 77 20 66 69 6c 65 20 49 44 ke a new file ID
c2d10 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 */. pNew->nRef
c2d20 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e = 1;. pNew->nN
c2d30 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d ame = n;. pNew-
c2d40 3e 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 >pNext = vxworks
c2d50 46 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f FileList;. vxwo
c2d60 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e rksFileList = pN
c2d70 65 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d ew;. unixLeaveM
c2d80 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e utex();. return
c2d90 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pNew;.}../*.**
c2da0 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 Decrement the re
c2db0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e ference count on
c2dc0 20 61 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 a vxworksFileId
c2dd0 20 6f 62 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a object. Free.*
c2de0 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 77 68 65 * the object whe
c2df0 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 n the reference
c2e00 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 count reaches ze
c2e10 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ro..*/.static vo
c2e20 69 64 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 id vxworksReleas
c2e30 65 46 69 6c 65 49 64 28 73 74 72 75 63 74 20 76 eFileId(struct v
c2e40 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 xworksFileId *pI
c2e50 64 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d d){. unixEnterM
c2e60 75 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 utex();. assert
c2e70 28 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b ( pId->nRef>0 );
c2e80 0a 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a . pId->nRef--;.
c2e90 20 20 69 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d if( pId->nRef=
c2ea0 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 =0 ){. struct
c2eb0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a vxworksFileId *
c2ec0 2a 70 70 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d *pp;. for(pp=
c2ed0 26 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 &vxworksFileList
c2ee0 3b 20 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 49 ; *pp && *pp!=pI
c2ef0 64 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 29 2d d; pp = &((*pp)-
c2f00 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20 61 >pNext)){}. a
c2f10 73 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 20 ssert( *pp==pId
c2f20 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 49 64 );. *pp = pId
c2f30 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c ->pNext;. sql
c2f40 69 74 65 33 5f 66 72 65 65 28 70 49 64 29 3b 0a ite3_free(pId);.
c2f50 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d }. unixLeaveM
c2f60 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 utex();.}.#endif
c2f70 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a /* OS_VXWORKS *
c2f80 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c2f90 2a 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71 75 65 ** End of Unique
c2fa0 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79 File ID Utility
c2fb0 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73 Used By VxWorks
c2fc0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c2fd0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
c2fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3020 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /.../***********
c3030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3070 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
c3080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
c3090 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c Posix Advisory L
c30a0 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ocking *********
c30b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c30c0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 ***.**.** POSIX
c30d0 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 advisory locks a
c30e0 72 65 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 re broken by des
c30f0 69 67 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 31 ign. ANSI STD 1
c3100 30 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20 003.1 (1996).**
c3110 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 section 6.5.2.2
c3120 6c 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75 67 lines 483 throug
c3130 68 20 34 39 30 20 73 70 65 63 69 66 79 20 74 68 h 490 specify th
c3140 61 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65 73 at when a proces
c3150 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c 65 s.** sets or cle
c3160 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74 ars a lock, that
c3170 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 72 operation overr
c3180 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20 6c ides any prior l
c3190 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20 74 ocks set.** by t
c31a0 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e he same process.
c31b0 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 It does not ex
c31c0 70 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f 2c plicitly say so,
c31d0 20 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69 65 but this implie
c31e0 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f 76 65 s.** that it ove
c31f0 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 65 74 rrides locks set
c3200 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f by the same pro
c3210 63 65 73 73 20 75 73 69 6e 67 20 61 20 64 69 66 cess using a dif
c3220 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 ferent.** file d
c3230 65 73 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73 escriptor. Cons
c3240 69 64 65 72 20 74 68 69 73 20 74 65 73 74 20 63 ider this test c
c3250 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ase:.**.**
c3260 20 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e 28 int fd1 = open(
c3270 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57 "./file1", O_RDW
c3280 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 R|O_CREAT, 0644)
c3290 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 ;.** int f
c32a0 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c d2 = open("./fil
c32b0 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 e2", O_RDWR|O_CR
c32c0 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a EAT, 0644);.**.*
c32d0 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65 * Suppose ./file
c32e0 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72 1 and ./file2 ar
c32f0 65 20 72 65 61 6c 6c 79 20 74 68 65 20 73 61 6d e really the sam
c3300 65 20 66 69 6c 65 20 28 62 65 63 61 75 73 65 0a e file (because.
c3310 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 72 64 ** one is a hard
c3320 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e or symbolic lin
c3330 6b 20 74 6f 20 74 68 65 20 6f 74 68 65 72 29 20 k to the other)
c3340 74 68 65 6e 20 69 66 20 79 6f 75 20 73 65 74 0a then if you set.
c3350 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 ** an exclusive
c3360 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65 lock on fd1, the
c3370 6e 20 74 72 79 20 74 6f 20 67 65 74 20 61 6e 20 n try to get an
c3380 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a exclusive lock.*
c3390 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f 72 * on fd2, it wor
c33a0 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 61 76 ks. I would hav
c33b0 65 20 65 78 70 65 63 74 65 64 20 74 68 65 20 73 e expected the s
c33c0 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a econd lock to.**
c33d0 20 66 61 69 6c 20 73 69 6e 63 65 20 74 68 65 72 fail since ther
c33e0 65 20 77 61 73 20 61 6c 72 65 61 64 79 20 61 20 e was already a
c33f0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 lock on the file
c3400 20 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 due to fd1..**
c3410 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e But not so. Sin
c3420 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 ce both locks ca
c3430 6d 65 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 me from the same
c3440 20 70 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a process, the.**
c3450 20 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 second override
c3460 73 20 74 68 65 20 66 69 72 73 74 2c 20 65 76 65 s the first, eve
c3470 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20 77 65 n though they we
c3480 72 65 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a re on different.
c3490 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ** file descript
c34a0 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 ors opened on di
c34b0 66 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d fferent file nam
c34c0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d es..**.** This m
c34d0 65 61 6e 73 20 74 68 61 74 20 77 65 20 63 61 6e eans that we can
c34e0 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c 6f not use POSIX lo
c34f0 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 cks to synchroni
c3500 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73 0a 2a ze file access.*
c3510 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e * among competin
c3520 67 20 74 68 72 65 61 64 73 20 6f 66 20 74 68 65 g threads of the
c3530 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 same process.
c3540 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c POSIX locks will
c3550 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f work fine.** to
c3560 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63 synchronize acc
c3570 65 73 73 20 66 6f 72 20 74 68 72 65 61 64 73 20 ess for threads
c3580 69 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 in separate proc
c3590 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a esses, but not.*
c35a0 2a 20 74 68 72 65 61 64 73 20 77 69 74 68 69 6e * threads within
c35b0 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 the same proces
c35c0 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b s..**.** To work
c35d0 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f 62 around the prob
c35e0 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 20 lem, SQLite has
c35f0 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c to manage file l
c3600 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a ocks internally.
c3610 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20 ** on its own.
c3620 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 64 Whenever a new d
c3630 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 atabase is opene
c3640 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 69 d, we have to fi
c3650 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 nd the.** specif
c3660 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 20 ic inode of the
c3670 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 74 database file (t
c3680 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74 65 he inode is dete
c3690 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a rmined by the.**
c36a0 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f 69 st_dev and st_i
c36b0 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 no fields of the
c36c0 20 73 74 61 74 20 73 74 72 75 63 74 75 72 65 20 stat structure
c36d0 74 68 61 74 20 66 73 74 61 74 28 29 20 66 69 6c that fstat() fil
c36e0 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 ls in).** and ch
c36f0 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c eck for locks al
c3700 72 65 61 64 79 20 65 78 69 73 74 69 6e 67 20 6f ready existing o
c3710 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 57 n that inode. W
c3720 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a hen locks are.**
c3730 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d 6f created or remo
c3740 76 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 ved, we have to
c3750 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20 look at our own
c3760 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 internal record
c3770 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 of the.** locks
c3780 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65 to see if anothe
c3790 72 20 74 68 72 65 61 64 20 68 61 73 20 70 72 65 r thread has pre
c37a0 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c 6f viously set a lo
c37b0 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a ck on that same.
c37c0 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ** inode..**.**
c37d0 28 41 73 69 64 65 3a 20 54 68 65 20 75 73 65 20 (Aside: The use
c37e0 6f 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 of inode numbers
c37f0 20 61 73 20 75 6e 69 71 75 65 20 49 44 73 20 64 as unique IDs d
c3800 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 oes not work on
c3810 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20 VxWorks..** For
c3820 56 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 65 VxWorks, we have
c3830 20 74 6f 20 75 73 65 20 74 68 65 20 61 6c 74 65 to use the alte
c3840 72 6e 61 74 69 76 65 20 75 6e 69 71 75 65 20 49 rnative unique I
c3850 44 20 73 79 73 74 65 6d 20 62 61 73 65 64 20 6f D system based o
c3860 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66 n.** canonical f
c3870 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c ilename and impl
c3880 65 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 70 emented in the p
c3890 72 65 76 69 6f 75 73 20 64 69 76 69 73 69 6f 6e revious division
c38a0 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c .).**.** The sql
c38b0 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 ite3_file struct
c38c0 75 72 65 20 66 6f 72 20 50 4f 53 49 58 20 69 73 ure for POSIX is
c38d0 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20 no longer just
c38e0 61 6e 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a an integer file.
c38f0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 ** descriptor.
c3900 49 74 20 69 73 20 6e 6f 77 20 61 20 73 74 72 75 It is now a stru
c3910 63 74 75 72 65 20 74 68 61 74 20 68 6f 6c 64 73 cture that holds
c3920 20 74 68 65 20 69 6e 74 65 67 65 72 20 66 69 6c the integer fil
c3930 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 e.** descriptor
c3940 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f and a pointer to
c3950 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 a structure tha
c3960 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 t describes the
c3970 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b internal.** lock
c3980 73 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 s on the corresp
c3990 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54 onding inode. T
c39a0 68 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b here is one lock
c39b0 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a ing structure.**
c39c0 20 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69 per inode, so i
c39d0 66 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 f the same inode
c39e0 20 69 73 20 6f 70 65 6e 65 64 20 74 77 69 63 65 is opened twice
c39f0 2c 20 62 6f 74 68 20 75 6e 69 78 46 69 6c 65 20 , both unixFile
c3a00 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f structures.** po
c3a10 69 6e 74 20 74 6f 20 74 68 65 20 73 61 6d 65 20 int to the same
c3a20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 locking structur
c3a30 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 e. The locking
c3a40 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 0a structure keeps.
c3a50 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 ** a reference c
c3a60 6f 75 6e 74 20 28 73 6f 20 77 65 20 77 69 6c 6c ount (so we will
c3a70 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64 65 know when to de
c3a80 6c 65 74 65 20 69 74 29 20 61 6e 64 20 61 20 22 lete it) and a "
c3a90 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68 cnt".** field th
c3aa0 61 74 20 74 65 6c 6c 73 20 75 73 20 69 74 73 20 at tells us its
c3ab0 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 internal lock st
c3ac0 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 atus. cnt==0 me
c3ad0 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 ans the.** file
c3ae0 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e is unlocked. cn
c3af0 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65 20 t==-1 means the
c3b00 66 69 6c 65 20 68 61 73 20 61 6e 20 65 78 63 6c file has an excl
c3b10 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 usive lock..** c
c3b20 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68 65 72 65 nt>0 means there
c3b30 20 61 72 65 20 63 6e 74 20 73 68 61 72 65 64 20 are cnt shared
c3b40 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c locks on the fil
c3b50 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 e..**.** Any att
c3b60 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 empt to lock or
c3b70 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 unlock a file fi
c3b80 72 73 74 20 63 68 65 63 6b 73 20 74 68 65 20 6c rst checks the l
c3b90 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 ocking.** struct
c3ba0 75 72 65 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 ure. The fcntl(
c3bb0 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 ) system call is
c3bc0 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f only invoked to
c3bd0 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58 set a .** POSIX
c3be0 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 6e 74 lock if the int
c3bf0 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63 ernal lock struc
c3c00 74 75 72 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 ture transitions
c3c10 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f between.** a lo
c3c20 63 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f cked and an unlo
c3c30 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a cked state..**.*
c3c40 2a 20 42 75 74 20 77 61 69 74 3a 20 20 74 68 65 * But wait: the
c3c50 72 65 20 61 72 65 20 79 65 74 20 6d 6f 72 65 20 re are yet more
c3c60 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 50 4f problems with PO
c3c70 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 SIX advisory loc
c3c80 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 ks..**.** If you
c3c90 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 close a file de
c3ca0 73 63 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f scriptor that po
c3cb0 69 6e 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74 ints to a file t
c3cc0 68 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a hat has locks,.*
c3cd0 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 * all locks on t
c3ce0 68 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 hat file that ar
c3cf0 65 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63 e owned by the c
c3d00 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61 urrent process a
c3d10 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 re.** released.
c3d20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 To work around
c3d30 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 this problem, ea
c3d40 63 68 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 ch unixFile stru
c3d50 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a cture contains.*
c3d60 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * a pointer to a
c3d70 6e 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 n unixOpenCnt st
c3d80 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 ructure. There
c3d90 69 73 20 6f 6e 65 20 75 6e 69 78 4f 70 65 6e 43 is one unixOpenC
c3da0 6e 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 nt structure.**
c3db0 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20 per open inode,
c3dc0 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 which means that
c3dd0 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69 78 46 69 multiple unixFi
c3de0 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20 le can point to
c3df0 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 75 6e 69 78 a single.** unix
c3e00 4f 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 OpenCnt. When a
c3e10 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 n attempt is mad
c3e20 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e e to close an un
c3e30 69 78 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65 ixFile, if there
c3e40 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e are.** other un
c3e50 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 ixFile open on t
c3e60 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 he same inode th
c3e70 61 74 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c at are holding l
c3e80 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a ocks, the call.*
c3e90 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 * to close() the
c3ea0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
c3eb0 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 is deferred unt
c3ec0 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f il all of the lo
c3ed0 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 cks clear..** Th
c3ee0 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 e unixOpenCnt st
c3ef0 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 61 20 ructure keeps a
c3f00 6c 69 73 74 20 6f 66 20 66 69 6c 65 20 64 65 73 list of file des
c3f10 63 72 69 70 74 6f 72 73 20 74 68 61 74 20 6e 65 criptors that ne
c3f20 65 64 20 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 ed to.** be clos
c3f30 65 64 20 61 6e 64 20 74 68 61 74 20 6c 69 73 74 ed and that list
c3f40 20 69 73 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 is walked (and
c3f50 63 6c 65 61 72 65 64 29 20 77 68 65 6e 20 74 68 cleared) when th
c3f60 65 20 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 e last lock.** c
c3f70 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 65 74 lears..**.** Yet
c3f80 20 61 6e 6f 74 68 65 72 20 70 72 6f 62 6c 65 6d another problem
c3f90 3a 20 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 : LinuxThreads
c3fa0 64 6f 20 6e 6f 74 20 70 6c 61 79 20 77 65 6c 6c do not play well
c3fb0 20 77 69 74 68 20 70 6f 73 69 78 20 6c 6f 63 6b with posix lock
c3fc0 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 6f 6c s..**.** Many ol
c3fd0 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 der versions of
c3fe0 6c 69 6e 75 78 20 75 73 65 20 74 68 65 20 4c 69 linux use the Li
c3ff0 6e 75 78 54 68 72 65 61 64 73 20 6c 69 62 72 61 nuxThreads libra
c4000 72 79 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6e ry which is.** n
c4010 6f 74 20 70 6f 73 69 78 20 63 6f 6d 70 6c 69 61 ot posix complia
c4020 6e 74 2e 20 20 55 6e 64 65 72 20 4c 69 6e 75 78 nt. Under Linux
c4030 54 68 72 65 61 64 73 2c 20 61 20 6c 6f 63 6b 20 Threads, a lock
c4040 63 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61 created by threa
c4050 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f 74 20 62 65 d.** A cannot be
c4060 20 6d 6f 64 69 66 69 65 64 20 6f 72 20 6f 76 65 modified or ove
c4070 72 72 69 64 64 65 6e 20 62 79 20 61 20 64 69 66 rridden by a dif
c4080 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 42 2e ferent thread B.
c4090 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72 65 61 64 20 .** Only thread
c40a0 41 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 A can modify the
c40b0 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b 69 6e 67 20 lock. Locking
c40c0 62 65 68 61 76 69 6f 72 20 69 73 20 63 6f 72 72 behavior is corr
c40d0 65 63 74 0a 2a 2a 20 69 66 20 74 68 65 20 61 70 ect.** if the ap
c40e0 70 6c 69 61 74 69 6f 6e 20 75 73 65 73 20 74 68 pliation uses th
c40f0 65 20 6e 65 77 65 72 20 4e 61 74 69 76 65 20 50 e newer Native P
c4100 6f 73 69 78 20 54 68 72 65 61 64 20 4c 69 62 72 osix Thread Libr
c4110 61 72 79 20 28 4e 50 54 4c 29 0a 2a 2a 20 6f 6e ary (NPTL).** on
c4120 20 6c 69 6e 75 78 20 2d 20 77 69 74 68 20 4e 50 linux - with NP
c4130 54 4c 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 TL a lock create
c4140 64 20 62 79 20 74 68 72 65 61 64 20 41 20 63 61 d by thread A ca
c4150 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 n override locks
c4160 0a 2a 2a 20 69 6e 20 74 68 72 65 61 64 20 42 2e .** in thread B.
c4170 20 20 42 75 74 20 74 68 65 72 65 20 69 73 20 6e But there is n
c4180 6f 20 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 o way to know at
c4190 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 compile-time wh
c41a0 69 63 68 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67 ich.** threading
c41b0 20 6c 69 62 72 61 72 79 20 69 73 20 62 65 69 6e library is bein
c41c0 67 20 75 73 65 64 2e 20 20 53 6f 20 74 68 65 72 g used. So ther
c41d0 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b e is no way to k
c41e0 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c now at.** compil
c41f0 65 2d 74 69 6d 65 20 77 68 65 74 68 65 72 20 6f e-time whether o
c4200 72 20 6e 6f 74 20 74 68 72 65 61 64 20 41 20 63 r not thread A c
c4210 61 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b an override lock
c4220 73 20 6f 6e 20 74 68 72 65 61 64 20 42 2e 0a 2a s on thread B..*
c4230 2a 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 * We have to do
c4240 61 20 72 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b a run-time check
c4250 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 to discover the
c4260 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 behavior of the
c4270 0a 2a 2a 20 63 75 72 72 65 6e 74 20 70 72 6f 63 .** current proc
c4280 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 79 ess..**.** On sy
c4290 73 74 65 6d 73 20 77 68 65 72 65 20 74 68 72 65 stems where thre
c42a0 61 64 20 41 20 69 73 20 75 6e 61 62 6c 65 20 74 ad A is unable t
c42b0 6f 20 6d 6f 64 69 66 79 20 6c 6f 63 6b 73 20 63 o modify locks c
c42c0 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 72 reated by.** thr
c42d0 65 61 64 20 42 2c 20 77 65 20 68 61 76 65 20 74 ead B, we have t
c42e0 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 o keep track of
c42f0 77 68 69 63 68 20 74 68 72 65 61 64 20 63 72 65 which thread cre
c4300 61 74 65 64 20 65 61 63 68 0a 2a 2a 20 6c 6f 63 ated each.** loc
c4310 6b 2e 20 20 48 65 6e 63 65 20 74 68 65 72 65 20 k. Hence there
c4320 69 73 20 61 6e 20 65 78 74 72 61 20 66 69 65 6c is an extra fiel
c4330 64 20 69 6e 20 74 68 65 20 6b 65 79 20 74 6f 20 d in the key to
c4340 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f the unixLockInfo
c4350 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f .** structure to
c4360 20 72 65 63 6f 72 64 20 74 68 69 73 20 69 6e 66 record this inf
c4370 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 6e 64 20 6f ormation. And o
c4380 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d 73 20 n those systems
c4390 69 74 0a 2a 2a 20 69 73 20 69 6c 6c 65 67 61 6c it.** is illegal
c43a0 20 74 6f 20 62 65 67 69 6e 20 61 20 74 72 61 6e to begin a tran
c43b0 73 61 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 saction in one t
c43c0 68 72 65 61 64 20 61 6e 64 20 66 69 6e 69 73 68 hread and finish
c43d0 20 69 74 0a 2a 2a 20 69 6e 20 61 6e 6f 74 68 65 it.** in anothe
c43e0 72 2e 20 20 46 6f 72 20 74 68 69 73 20 6c 61 74 r. For this lat
c43f0 74 65 72 20 72 65 73 74 72 69 63 74 69 6f 6e 2c ter restriction,
c4400 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 there is no wor
c4410 6b 2d 61 72 6f 75 6e 64 2e 0a 2a 2a 20 49 74 20 k-around..** It
c4420 69 73 20 61 20 6c 69 6d 69 74 61 74 69 6f 6e 20 is a limitation
c4430 6f 66 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e of LinuxThreads.
c4440 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f .*/../*.** Set o
c4450 72 20 63 68 65 63 6b 20 74 68 65 20 75 6e 69 78 r check the unix
c4460 46 69 6c 65 2e 74 69 64 20 66 69 65 6c 64 2e 20 File.tid field.
c4470 20 54 68 69 73 20 66 69 65 6c 64 20 69 73 20 73 This field is s
c4480 65 74 20 77 68 65 6e 20 61 6e 20 75 6e 69 78 46 et when an unixF
c4490 69 6c 65 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 ile.** is first
c44a0 6f 70 65 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62 opened. All sub
c44b0 73 65 71 75 65 6e 74 20 75 73 65 73 20 6f 66 20 sequent uses of
c44c0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 76 65 72 the unixFile ver
c44d0 69 66 79 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ify that the.**
c44e0 73 61 6d 65 20 74 68 72 65 61 64 20 69 73 20 6f same thread is o
c44f0 70 65 72 61 74 69 6e 67 20 6f 6e 20 74 68 65 20 perating on the
c4500 75 6e 69 78 46 69 6c 65 2e 20 20 53 6f 6d 65 20 unixFile. Some
c4510 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
c4520 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61 6c 6c 6f s do.** not allo
c4530 77 20 6c 6f 63 6b 73 20 74 6f 20 62 65 20 6f 76 w locks to be ov
c4540 65 72 72 69 64 64 65 6e 20 62 79 20 6f 74 68 65 erridden by othe
c4550 72 20 74 68 72 65 61 64 73 20 61 6e 64 20 74 68 r threads and th
c4560 61 74 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a at restriction.*
c4570 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 73 71 6c * means that sql
c4580 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 ite3* database h
c4590 61 6e 64 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 andles cannot be
c45a0 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 moved from one
c45b0 74 68 72 65 61 64 0a 2a 2a 20 74 6f 20 61 6e 6f thread.** to ano
c45c0 74 68 65 72 20 77 68 69 6c 65 20 6c 6f 63 6b 73 ther while locks
c45d0 20 61 72 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a are held..**.**
c45e0 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e 31 20 28 Version 3.3.1 (
c45f0 32 30 30 36 2d 30 31 2d 31 35 29 3a 20 20 75 6e 2006-01-15): un
c4600 69 78 46 69 6c 65 20 63 61 6e 20 62 65 20 6d 6f ixFile can be mo
c4610 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72 ved from one thr
c4620 65 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 ead to.** anothe
c4630 72 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 r as long as we
c4640 61 72 65 20 72 75 6e 6e 69 6e 67 20 6f 6e 20 61 are running on a
c4650 20 73 79 73 74 65 6d 20 74 68 61 74 20 73 75 70 system that sup
c4660 70 6f 72 74 73 20 74 68 72 65 61 64 73 0a 2a 2a ports threads.**
c4670 20 6f 76 65 72 72 69 64 69 6e 67 20 65 61 63 68 overriding each
c4680 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 77 others locks (w
c4690 68 69 63 68 20 69 73 20 6e 6f 77 20 74 68 65 20 hich is now the
c46a0 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61 most common beha
c46b0 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e vior).** or if n
c46c0 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 o locks are held
c46d0 2e 20 20 42 75 74 20 74 68 65 20 75 6e 69 78 46 . But the unixF
c46e0 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20 ile.pLock field
c46f0 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72 needs to be.** r
c4700 65 63 6f 6d 70 75 74 65 64 20 62 65 63 61 75 73 ecomputed becaus
c4710 65 20 69 74 73 20 6b 65 79 20 69 6e 63 6c 75 64 e its key includ
c4720 65 73 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 es the thread-id
c4730 2e 20 20 53 65 65 20 74 68 65 20 0a 2a 2a 20 74 . See the .** t
c4740 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 ransferOwnership
c4750 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f () function belo
c4760 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c w for additional
c4770 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a information.*/.
c4780 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
c4790 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 DSAFE && defined
c47a0 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 (__linux__).# de
c47b0 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49 fine SET_THREADI
c47c0 44 28 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20 D(X) (X)->tid
c47d0 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
c47e0 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f .# define CHECK_
c47f0 54 48 52 45 41 44 49 44 28 58 29 20 28 74 68 72 THREADID(X) (thr
c4800 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 eadsOverrideEach
c4810 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 OthersLocks==0 &
c4820 26 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 & \.
c4830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4840 21 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28 !pthread_equal((
c4850 58 29 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64 X)->tid, pthread
c4860 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a _self())).#else.
c4870 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 # define SET_THR
c4880 45 41 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e EADID(X).# defin
c4890 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 e CHECK_THREADID
c48a0 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a (X) 0.#endif../*
c48b0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
c48c0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
c48d0 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 structure serve
c48e0 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 s as the key use
c48f0 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 d.** to locate a
c4900 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 particular unix
c4910 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 OpenCnt structur
c4920 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 e given its inod
c4930 65 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 74 e. This.** is t
c4940 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 75 he same as the u
c4950 6e 69 78 4c 6f 63 6b 4b 65 79 20 65 78 63 65 70 nixLockKey excep
c4960 74 20 74 68 61 74 20 74 68 65 20 74 68 72 65 61 t that the threa
c4970 64 20 49 44 20 69 73 20 6f 6d 69 74 74 65 64 2e d ID is omitted.
c4980 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 46 .*/.struct unixF
c4990 69 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f 74 20 ileId {. dev_t
c49a0 64 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 dev;
c49b0 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 /* Device
c49c0 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53 number */.#if OS
c49d0 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 _VXWORKS. struc
c49e0 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
c49f0 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 *pId; /* Unique
c4a00 20 66 69 6c 65 20 49 44 20 66 6f 72 20 76 78 77 file ID for vxw
c4a10 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20 orks. */.#else.
c4a20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 ino_t ino;
c4a30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c4a40 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a Inode number */.
c4a50 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
c4a60 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
c4a70 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
c4a80 72 75 63 74 75 72 65 20 73 65 72 76 65 73 20 61 ructure serves a
c4a90 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a s the key used.*
c4aa0 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 * to locate a pa
c4ab0 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4c 6f 63 rticular unixLoc
c4ac0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 kInfo structure
c4ad0 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e given its inode.
c4ae0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 72 65 61 64 .**.** If thread
c4af0 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 s cannot overrid
c4b00 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f e each others lo
c4b10 63 6b 73 20 28 4c 69 6e 75 78 54 68 72 65 61 64 cks (LinuxThread
c4b20 73 29 2c 20 74 68 65 6e 20 77 65 0a 2a 2a 20 73 s), then we.** s
c4b30 65 74 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b et the unixLockK
c4b40 65 79 2e 74 69 64 20 66 69 65 6c 64 20 74 6f 20 ey.tid field to
c4b50 74 68 65 20 74 68 72 65 61 64 20 49 44 2e 20 20 the thread ID.
c4b60 49 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f If threads can o
c4b70 76 65 72 72 69 64 65 0a 2a 2a 20 65 61 63 68 20 verride.** each
c4b80 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 50 6f others locks (Po
c4b90 73 69 78 20 61 6e 64 20 4e 50 54 4c 29 20 74 68 six and NPTL) th
c4ba0 65 6e 20 74 69 64 20 69 73 20 61 6c 77 61 79 73 en tid is always
c4bb0 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a set to zero..**
c4bc0 20 74 69 64 20 69 73 20 6f 6d 69 74 74 65 64 20 tid is omitted
c4bd0 69 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 if we compile wi
c4be0 74 68 6f 75 74 20 74 68 72 65 61 64 69 6e 67 20 thout threading
c4bf0 73 75 70 70 6f 72 74 20 6f 72 20 6f 6e 20 61 6e support or on an
c4c00 20 4f 53 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 OS.** other tha
c4c10 6e 20 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 74 72 75 n linux..*/.stru
c4c20 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 7b ct unixLockKey {
c4c30 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 . struct unixFi
c4c40 6c 65 49 64 20 66 69 64 3b 20 20 2f 2a 20 55 6e leId fid; /* Un
c4c50 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 ique identifier
c4c60 66 6f 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a for the file */.
c4c70 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
c4c80 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 DSAFE && defined
c4c90 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 (__linux__). pt
c4ca0 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 2f 2a hread_t tid; /*
c4cb0 20 54 68 72 65 61 64 20 49 44 20 6f 66 20 6c 6f Thread ID of lo
c4cc0 63 6b 20 6f 77 6e 65 72 2e 20 5a 65 72 6f 20 69 ck owner. Zero i
c4cd0 66 20 6e 6f 74 20 75 73 69 6e 67 20 4c 69 6e 75 f not using Linu
c4ce0 78 54 68 72 65 61 64 73 20 2a 2f 0a 23 65 6e 64 xThreads */.#end
c4cf0 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 if.};../*.** An
c4d00 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
c4d10 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
c4d20 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ure is allocated
c4d30 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a for each open.*
c4d40 2a 20 69 6e 6f 64 65 2e 20 20 4f 72 2c 20 6f 6e * inode. Or, on
c4d50 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 74 LinuxThreads, t
c4d60 68 65 72 65 20 69 73 20 6f 6e 65 20 6f 66 20 74 here is one of t
c4d70 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 20 hese structures
c4d80 66 6f 72 0a 2a 2a 20 65 61 63 68 20 69 6e 6f 64 for.** each inod
c4d90 65 20 6f 70 65 6e 65 64 20 62 79 20 65 61 63 68 e opened by each
c4da0 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 thread..**.** A
c4db0 20 73 69 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61 single inode ca
c4dc0 6e 20 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 n have multiple
c4dd0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
c4de0 2c 20 73 6f 20 65 61 63 68 20 75 6e 69 78 46 69 , so each unixFi
c4df0 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 le.** structure
c4e00 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 contains a point
c4e10 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 er to an instanc
c4e20 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
c4e30 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a and this.** obj
c4e40 65 63 74 20 6b 65 65 70 73 20 61 20 63 6f 75 6e ect keeps a coun
c4e50 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 t of the number
c4e60 6f 66 20 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e of unixFile poin
c4e70 74 69 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73 ting to it..*/.s
c4e80 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e truct unixLockIn
c4e90 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e fo {. struct un
c4ea0 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 ixLockKey lockKe
c4eb0 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f y; /* The lo
c4ec0 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e okup key */. in
c4ed0 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 t cnt;
c4ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c4ef0 20 4e 75 6d 62 65 72 20 6f 66 20 53 48 41 52 45 Number of SHARE
c4f00 44 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a D locks held */.
c4f10 20 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 int locktype;
c4f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4f30 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52 /* One of SHAR
c4f40 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45 ED_LOCK, RESERVE
c4f50 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20 D_LOCK etc. */.
c4f60 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
c4f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4f80 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f /* Number of po
c4f90 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 inters to this s
c4fa0 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 tructure */. st
c4fb0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 ruct unixLockInf
c4fc0 6f 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a o *pNext; /*
c4fd0 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 List of all uni
c4fe0 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 xLockInfo object
c4ff0 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e s */. struct un
c5000 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 50 72 65 ixLockInfo *pPre
c5010 76 3b 20 20 20 20 20 2f 2a 20 20 20 20 2e 2e 2e v; /* ...
c5020 2e 20 64 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20 . doubly linked
c5030 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
c5040 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
c5050 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
c5060 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ure is allocated
c5070 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a for each open.*
c5080 2a 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 * inode. This s
c5090 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 tructure keeps t
c50a0 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 rack of the numb
c50b0 65 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 er of locks on t
c50c0 68 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 hat.** inode. I
c50d0 66 20 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74 f a close is att
c50e0 65 6d 70 74 65 64 20 61 67 61 69 6e 73 74 20 61 empted against a
c50f0 6e 20 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20 n inode that is
c5100 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 holding.** locks
c5110 2c 20 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64 , the close is d
c5120 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c eferred until al
c5130 6c 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 l locks clear by
c5140 20 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 adding the.** f
c5150 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
c5160 6f 20 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 o be closed to t
c5170 68 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e he pending list.
c5180 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 20 43 6f .**.** TODO: Co
c5190 6e 73 69 64 65 72 20 63 68 61 6e 67 69 6e 67 20 nsider changing
c51a0 74 68 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 this so that the
c51b0 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e re is only a sin
c51c0 67 6c 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 gle file.** desc
c51d0 72 69 70 74 6f 72 20 66 6f 72 20 65 61 63 68 20 riptor for each
c51e0 6f 70 65 6e 20 66 69 6c 65 2c 20 65 76 65 6e 20 open file, even
c51f0 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 when it is opene
c5200 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 d multiple times
c5210 2e 0a 2a 2a 20 54 68 65 20 63 6c 6f 73 65 28 29 ..** The close()
c5220 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 77 6f 75 system call wou
c5230 6c 64 20 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68 ld only occur wh
c5240 65 6e 20 74 68 65 20 6c 61 73 74 20 64 61 74 61 en the last data
c5250 62 61 73 65 0a 2a 2a 20 75 73 69 6e 67 20 74 68 base.** using th
c5260 65 20 66 69 6c 65 20 63 6c 6f 73 65 73 2e 0a 2a e file closes..*
c5270 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 /.struct unixOpe
c5280 6e 43 6e 74 20 7b 0a 20 20 73 74 72 75 63 74 20 nCnt {. struct
c5290 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 unixFileId fileI
c52a0 64 3b 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b d; /* The look
c52b0 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 up key */. int
c52c0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
c52d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
c52e0 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f r of pointers to
c52f0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
c5300 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 */. int nLock;
c5310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c5320 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 /* Number of ou
c5330 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 tstanding locks
c5340 2a 2f 0a 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e */. int nPendin
c5350 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
c5360 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 65 /* Number of pe
c5370 6e 64 69 6e 67 20 63 6c 6f 73 65 28 29 20 6f 70 nding close() op
c5380 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e erations */. in
c5390 74 20 2a 61 50 65 6e 64 69 6e 67 3b 20 20 20 20 t *aPending;
c53a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f /* Mallo
c53b0 63 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69 6e ced space holdin
c53c0 67 20 66 64 27 73 20 61 77 61 69 74 69 6e 67 20 g fd's awaiting
c53d0 61 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 23 69 66 a close() */.#if
c53e0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 OS_VXWORKS. se
c53f0 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 m_t *pSem;
c5400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c5410 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 * Named POSIX se
c5420 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 maphore */. cha
c5430 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 r aSemName[MAX_P
c5440 41 54 48 4e 41 4d 45 2b 31 5d 3b 20 20 20 2f 2a ATHNAME+1]; /*
c5450 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65 Name of that se
c5460 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 maphore */.#endi
c5470 66 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f f. struct unixO
c5480 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a penCnt *pNext, *
c5490 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 pPrev; /* List
c54a0 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e of all unixOpen
c54b0 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d Cnt objects */.}
c54c0 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f ;../*.** Lists o
c54d0 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e f all unixLockIn
c54e0 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 fo and unixOpenC
c54f0 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 nt objects. The
c5500 73 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 61 se used to be ha
c5510 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 sh.** tables. B
c5520 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ut the number of
c5530 20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 65 objects is rare
c5540 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 ly more than a d
c5550 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 ozen and.** neve
c5560 72 20 65 78 63 65 65 64 73 20 61 20 66 65 77 20 r exceeds a few
c5570 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c thousand. And l
c5580 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 ookup is not on
c5590 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 a critical.** pa
c55a0 74 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c th so a simple l
c55b0 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 inked list will
c55c0 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 suffice..*/.stat
c55d0 69 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f ic struct unixLo
c55e0 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 ckInfo *lockList
c55f0 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72 = 0;.static str
c5600 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 uct unixOpenCnt
c5610 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a *openList = 0;..
c5620 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61 /*.** This varia
c5630 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 68 ble remembers wh
c5640 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 ether or not thr
c5650 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 eads can overrid
c5660 65 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a e each others.**
c5670 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 locks..**.**
c5680 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 0: No. Thread
c5690 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 s cannot overrid
c56a0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f e each others lo
c56b0 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 cks. (LinuxThre
c56c0 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 ads).** 1: Y
c56d0 65 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e es. Threads can
c56e0 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f override each o
c56f0 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 thers locks. (P
c5700 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 osix & NLPT).**
c5710 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 -1: We don't
c5720 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 know yet..**.**
c5730 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c On some systems,
c5740 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 we know at comp
c5750 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65 ile-time if thre
c5760 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 ads can override
c5770 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 each.** others
c5780 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 locks. On those
c5790 20 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51 systems, the SQ
c57a0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 LITE_THREAD_OVER
c57b0 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a RIDE_LOCK macro.
c57c0 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61 ** will be set a
c57d0 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f ppropriately. O
c57e0 6e 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c n other systems,
c57f0 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63 we have to chec
c5800 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e k at.** runtime.
c5810 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65 On these latte
c5820 72 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 r systems, SQLTI
c5830 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 E_THREAD_OVERRID
c5840 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 E_LOCK is.** und
c5850 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 efined..**.** Th
c5860 69 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d is variable norm
c5870 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63 ally has file sc
c5880 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 ope only. But d
c5890 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 uring testing, w
c58a0 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 e make.** it a g
c58b0 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68 lobal so that th
c58c0 65 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20 e test code can
c58d0 63 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65 change its value
c58e0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72 in order to ver
c58f0 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 ify.** that the
c5900 72 69 67 68 74 20 73 74 75 66 66 20 68 61 70 70 right stuff happ
c5910 65 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 61 ens in either ca
c5920 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 se..*/.#if SQLIT
c5930 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
c5940 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
c5950 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 4c _).# ifndef SQL
c5960 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 ITE_THREAD_OVERR
c5970 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 IDE_LOCK.# de
c5980 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 fine SQLITE_THRE
c5990 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b AD_OVERRIDE_LOCK
c59a0 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 -1.# endif.#
c59b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
c59c0 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 T.int threadsOve
c59d0 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
c59e0 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 ocks = SQLITE_TH
c59f0 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f READ_OVERRIDE_LO
c5a00 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 CK;.# else.stat
c5a10 69 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 ic int threadsOv
c5a20 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
c5a30 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 Locks = SQLITE_T
c5a40 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c HREAD_OVERRIDE_L
c5a50 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 OCK;.# endif.#e
c5a60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
c5a70 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 structure holds
c5a80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 information pas
c5a90 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 sed into individ
c5aa0 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 ual test.** thre
c5ab0 61 64 73 20 62 79 20 74 68 65 20 74 65 73 74 54 ads by the testT
c5ac0 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 hreadLockingBeha
c5ad0 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a vior() routine..
c5ae0 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 64 */.struct thread
c5af0 54 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 TestData {. int
c5b00 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 fd;
c5b10 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 /* File to b
c5b20 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 e locked */. st
c5b30 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b ruct flock lock;
c5b40 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b /* The lock
c5b50 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f ing operation */
c5b60 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 . int result;
c5b70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
c5b80 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 ult of the locki
c5b90 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a ng operation */.
c5ba0 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 };..#if SQLITE_T
c5bb0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
c5bc0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
c5bd0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
c5be0 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74 ion is used as t
c5bf0 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 he main routine
c5c00 66 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 75 for a thread lau
c5c10 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 nched by.** test
c5c20 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 ThreadLockingBeh
c5c30 61 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 74 avior(). It test
c5c40 73 20 77 68 65 74 68 65 72 20 74 68 65 20 73 68 s whether the sh
c5c50 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e ared-lock obtain
c5c60 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 ed.** by the mai
c5c70 6e 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 74 n thread in test
c5c80 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 ThreadLockingBeh
c5c90 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 avior() conflict
c5ca0 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f s with a.** hypo
c5cb0 74 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d 6c thetical write-l
c5cc0 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 20 ock obtained by
c5cd0 74 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 74 this thread on t
c5ce0 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a he same file..**
c5cf0 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c 6f .** The write-lo
c5d00 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c ck is not actual
c5d10 6c 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 20 ly acquired, as
c5d20 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 this is not poss
c5d30 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 ible if .** the
c5d40 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 file is open in
c5d50 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 read-only mode (
c5d60 73 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 32 see ticket #3472
c5d70 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f )..*/ .static vo
c5d80 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e id *threadLockin
c5d90 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67 gTest(void *pArg
c5da0 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 ){. struct thre
c5db0 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74 adTestData *pDat
c5dc0 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65 a = (struct thre
c5dd0 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67 adTestData*)pArg
c5de0 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c ;. pData->resul
c5df0 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d t = fcntl(pData-
c5e00 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 >fd, F_GETLK, &p
c5e10 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 Data->lock);. r
c5e20 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 eturn pArg;.}.#e
c5e30 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 ndif /* SQLITE_T
c5e40 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
c5e50 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 ined(__linux__)
c5e60 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f */...#if SQLITE_
c5e70 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
c5e80 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
c5e90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 ./*.** This proc
c5ea0 65 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 edure attempts t
c5eb0 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 o determine whet
c5ec0 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 her or not threa
c5ed0 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 ds.** can overri
c5ee0 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c de each others l
c5ef0 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 ocks then sets t
c5f00 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 he .** threadsOv
c5f10 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
c5f20 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 Locks variable a
c5f30 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f ppropriately..*/
c5f40 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 .static void tes
c5f50 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 tThreadLockingBe
c5f60 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 havior(int fd_or
c5f70 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 ig){. int fd;.
c5f80 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 int rc;. struc
c5f90 74 20 74 68 72 65 61 64 54 65 73 74 44 61 74 61 t threadTestData
c5fa0 20 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f d;. struct flo
c5fb0 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f ck l;. pthread_
c5fc0 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 t t;.. fd = dup
c5fd0 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 (fd_orig);. if(
c5fe0 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a fd<0 ) return;.
c5ff0 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 memset(&l, 0,
c6000 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e sizeof(l));. l.
c6010 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b l_type = F_RDLCK
c6020 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b ;. l.l_len = 1;
c6030 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 . l.l_start = 0
c6040 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d ;. l.l_whence =
c6050 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 SEEK_SET;. rc
c6060 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c = fcntl(fd_orig,
c6070 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 F_SETLK, &l);.
c6080 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74 if( rc!=0 ) ret
c6090 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 urn;. memset(&d
c60a0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b , 0, sizeof(d));
c60b0 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 . d.fd = fd;.
c60c0 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e d.lock = l;. d.
c60d0 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f lock.l_type = F_
c60e0 57 52 4c 43 4b 3b 0a 20 20 70 74 68 72 65 61 64 WRLCK;. pthread
c60f0 5f 63 72 65 61 74 65 28 26 74 2c 20 30 2c 20 74 _create(&t, 0, t
c6100 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 hreadLockingTest
c6110 2c 20 26 64 29 3b 0a 20 20 70 74 68 72 65 61 64 , &d);. pthread
c6120 5f 6a 6f 69 6e 28 74 2c 20 30 29 3b 0a 20 20 63 _join(t, 0);. c
c6130 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20 lose(fd);. if(
c6140 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65 d.result!=0 ) re
c6150 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f turn;. threadsO
c6160 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 verrideEachOther
c6170 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b sLocks = (d.lock
c6180 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b .l_type==F_UNLCK
c6190 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
c61a0 51 4c 49 54 45 5f 54 48 45 52 41 44 53 41 46 45 QLITE_THERADSAFE
c61b0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
c61c0 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a nux__) */../*.**
c61d0 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c Release a unixL
c61e0 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 ockInfo structur
c61f0 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c e previously all
c6200 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f ocated by findLo
c6210 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a 73 74 61 ckInfo()..*/.sta
c6220 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 tic void release
c6230 4c 6f 63 6b 49 6e 66 6f 28 73 74 72 75 63 74 20 LockInfo(struct
c6240 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c unixLockInfo *pL
c6250 6f 63 6b 29 7b 0a 20 20 69 66 28 20 70 4c 6f 63 ock){. if( pLoc
c6260 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e k ){. pLock->
c6270 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 nRef--;. if(
c6280 70 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 pLock->nRef==0 )
c6290 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 {. if( pLoc
c62a0 6b 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 k->pPrev ){.
c62b0 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
c62c0 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d k->pPrev->pNext=
c62d0 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 =pLock );.
c62e0 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e pLock->pPrev->
c62f0 70 4e 65 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 pNext = pLock->p
c6300 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 Next;. }els
c6310 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 e{. asser
c6320 74 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f t( lockList==pLo
c6330 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f ck );. lo
c6340 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e ckList = pLock->
c6350 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 pNext;. }.
c6360 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e if( pLock->
c6370 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 pNext ){.
c6380 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
c6390 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c pNext->pPrev==pL
c63a0 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70 ock );. p
c63b0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 Lock->pNext->pPr
c63c0 65 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 ev = pLock->pPre
c63d0 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 v;. }.
c63e0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c sqlite3_free(pL
c63f0 6f 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ock);. }. }.
c6400 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 }../*.** Release
c6410 20 61 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 a unixOpenCnt s
c6420 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 tructure previou
c6430 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 sly allocated by
c6440 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e findLockInfo().
c6450 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
c6460 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 releaseOpenCnt(s
c6470 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
c6480 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 69 66 28 t *pOpen){. if(
c6490 20 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f pOpen ){. pO
c64a0 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 pen->nRef--;.
c64b0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 if( pOpen->nRef
c64c0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
c64d0 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b pOpen->pPrev ){
c64e0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
c64f0 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 pOpen->pPrev->p
c6500 4e 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 Next==pOpen );.
c6510 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 pOpen->pP
c6520 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 rev->pNext = pOp
c6530 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 en->pNext;.
c6540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
c6550 61 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 assert( openList
c6560 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 ==pOpen );.
c6570 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f openList = pO
c6580 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 pen->pNext;.
c6590 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f }. if( pO
c65a0 70 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 pen->pNext ){.
c65b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
c65c0 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 pen->pNext->pPre
c65d0 76 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 v==pOpen );.
c65e0 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 pOpen->pNext
c65f0 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d ->pPrev = pOpen-
c6600 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a >pPrev;. }.
c6610 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
c6620 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 ee(pOpen->aPendi
c6630 6e 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ng);. sqlit
c6640 65 33 5f 66 72 65 65 28 70 4f 70 65 6e 29 3b 0a e3_free(pOpen);.
c6650 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
c6660 2a 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65 20 ** Given a file
c6670 64 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61 descriptor, loca
c6680 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 te unixLockInfo
c6690 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 and unixOpenCnt
c66a0 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 0a structures that.
c66b0 2a 2a 20 64 65 73 63 72 69 62 65 73 20 74 68 61 ** describes tha
c66c0 74 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f t file descripto
c66d0 72 2e 20 20 43 72 65 61 74 65 20 6e 65 77 20 6f r. Create new o
c66e0 6e 65 73 20 69 66 20 6e 65 63 65 73 73 61 72 79 nes if necessary
c66f0 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e . The.** return
c6700 20 76 61 6c 75 65 73 20 6d 69 67 68 74 20 62 65 values might be
c6710 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 uninitialized i
c6720 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
c6730 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 s..**.** Return
c6740 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 an appropriate e
c6750 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 rror code..*/.st
c6760 61 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 atic int findLoc
c6770 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78 46 69 6c kInfo(. unixFil
c6780 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 e *pFile,
c6790 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 78 20 /* Unix
c67a0 66 69 6c 65 20 77 69 74 68 20 66 69 6c 65 20 64 file with file d
c67b0 65 73 63 20 75 73 65 64 20 69 6e 20 74 68 65 20 esc used in the
c67c0 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 key */. struct
c67d0 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 unixLockInfo **p
c67e0 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 74 75 72 pLock, /* Retur
c67f0 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e n the unixLockIn
c6800 66 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 72 fo structure her
c6810 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e e */. struct un
c6820 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 70 4f 70 ixOpenCnt **ppOp
c6830 65 6e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 en /* Return
c6840 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 the unixOpenCnt
c6850 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a structure here *
c6860 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 /.){. int rc;
c6870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6880 20 20 20 20 20 20 2f 2a 20 53 79 73 74 65 6d 20 /* System
c6890 63 61 6c 6c 20 72 65 74 75 72 6e 20 63 6f 64 65 call return code
c68a0 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 */. int fd;
c68b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c68c0 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 /* The file
c68d0 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 descriptor for
c68e0 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 pFile */. struc
c68f0 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f t unixLockKey lo
c6900 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 4c 6f 6f ckKey; /* Loo
c6910 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 kup key for the
c6920 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 unixLockInfo str
c6930 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 ucture */. stru
c6940 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 ct unixFileId fi
c6950 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a 20 4c 6f leId; /* Lo
c6960 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 okup key for the
c6970 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 unixOpenCnt str
c6980 75 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 uct */. struct
c6990 73 74 61 74 20 73 74 61 74 62 75 66 3b 20 20 20 stat statbuf;
c69a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c /* Low-l
c69b0 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d evel file inform
c69c0 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 ation */. struc
c69d0 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a t unixLockInfo *
c69e0 70 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 43 61 6e pLock; /* Can
c69f0 64 69 64 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 didate unixLockI
c6a00 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 nfo object */.
c6a10 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
c6a20 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 2f nt *pOpen; /
c6a30 2a 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 * Candidate unix
c6a40 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 20 2a OpenCnt object *
c6a50 2f 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d /.. /* Get low-
c6a60 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f level informatio
c6a70 6e 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 n about the file
c6a80 20 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 that we can use
c6a90 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 d to. ** create
c6aa0 20 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 a unique name f
c6ab0 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a or the file.. *
c6ac0 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e /. fd = pFile->
c6ad0 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 h;. rc = fstat(
c6ae0 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 fd, &statbuf);.
c6af0 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 if( rc!=0 ){.
c6b00 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
c6b10 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 no = errno;.#ifd
c6b20 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 ef EOVERFLOW.
c6b30 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 if( pFile->last
c6b40 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 Errno==EOVERFLOW
c6b50 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
c6b60 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 _NOLFS;.#endif.
c6b70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
c6b80 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66 _IOERR;. }..#if
c6b90 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20 def __APPLE__.
c6ba0 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e /* On OS X on an
c6bb0 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 msdos filesyste
c6bc0 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d m, the inode num
c6bd0 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 0a ber is reported.
c6be0 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79 ** incorrectly
c6bf0 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66 for zero-size f
c6c00 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b 65 iles. See ticke
c6c10 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f 72 t #3260. To wor
c6c20 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68 k. ** around th
c6c30 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 63 is problem (we c
c6c40 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 67 onsider it a bug
c6c50 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51 in OS X, not SQ
c6c60 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c Lite). ** we al
c6c70 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 68 ways increase th
c6c80 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 31 e file size to 1
c6c90 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 69 by writing a si
c6ca0 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 70 ngle byte. ** p
c6cb0 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 6e rior to accessin
c6cc0 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 g the inode numb
c6cd0 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 74 er. The one byt
c6ce0 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 2a e written is. *
c6cf0 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 63 * an ASCII 'S' c
c6d00 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20 61 haracter which a
c6d10 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 62 lso happens to b
c6d20 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 e the first byte
c6d30 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 . ** in the hea
c6d40 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 4c der of every SQL
c6d50 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 ite database. I
c6d60 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 n this way, if t
c6d70 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 72 here. ** is a r
c6d80 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 ace condition su
c6d90 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 ch that another
c6da0 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 61 thread has alrea
c6db0 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a dy populated. *
c6dc0 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 * the first page
c6dd0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
c6de0 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64 , no damage is d
c6df0 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 one.. */. if(
c6e00 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d statbuf.st_size=
c6e10 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 =0 ){. rc = w
c6e20 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 29 rite(fd, "S", 1)
c6e30 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 20 ;. if( rc!=1
c6e40 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
c6e50 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 SQLITE_IOERR;.
c6e60 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73 74 }. rc = fst
c6e70 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 at(fd, &statbuf)
c6e80 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 ;. if( rc!=0
c6e90 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
c6ea0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
c6eb0 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 o;. return
c6ec0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 SQLITE_IOERR;.
c6ed0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }. }.#endif..
c6ee0 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 memset(&lockKe
c6ef0 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 y, 0, sizeof(loc
c6f00 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 kKey));. lockKe
c6f10 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61 74 y.fid.dev = stat
c6f20 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 buf.st_dev;.#if
c6f30 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 OS_VXWORKS. loc
c6f40 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 kKey.fid.pId = p
c6f50 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 File->pId;.#else
c6f60 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 . lockKey.fid.i
c6f70 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f no = statbuf.st_
c6f80 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 ino;.#endif.#if
c6f90 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
c6fa0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c E && defined(__l
c6fb0 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 inux__). if( th
c6fc0 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 readsOverrideEac
c6fd0 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 hOthersLocks<0 )
c6fe0 7b 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 64 {. testThread
c6ff0 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 LockingBehavior(
c7000 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b fd);. }. lockK
c7010 65 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64 73 ey.tid = threads
c7020 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
c7030 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 rsLocks ? 0 : pt
c7040 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 hread_self();.#e
c7050 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 ndif. fileId =
c7060 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 lockKey.fid;. i
c7070 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a f( ppLock!=0 ){.
c7080 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b pLock = lock
c7090 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 List;. while(
c70a0 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 pLock && memcmp
c70b0 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 (&lockKey, &pLoc
c70c0 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 k->lockKey, size
c70d0 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a of(lockKey)) ){.
c70e0 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c pLock = pL
c70f0 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 ock->pNext;.
c7100 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d }. if( pLock=
c7110 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 =0 ){. pLoc
c7120 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c k = sqlite3_mall
c7130 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 oc( sizeof(*pLoc
c7140 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 k) );. if(
c7150 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 pLock==0 ){.
c7160 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
c7170 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 NOMEM;. g
c7180 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 oto exit_findloc
c7190 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 kinfo;. }.
c71a0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b pLock->lock
c71b0 4b 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a 20 Key = lockKey;.
c71c0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 pLock->nRef
c71d0 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f 63 = 1;. pLoc
c71e0 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 k->cnt = 0;.
c71f0 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 pLock->locktyp
c7200 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f e = 0;. pLo
c7210 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b ck->pNext = lock
c7220 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f 63 List;. pLoc
c7230 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 k->pPrev = 0;.
c7240 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 74 if( lockList
c7250 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 72 ) lockList->pPr
c7260 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 ev = pLock;.
c7270 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f lockList = pLo
c7280 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ck;. }else{.
c7290 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 pLock->nRef
c72a0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 ++;. }. *p
c72b0 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 pLock = pLock;.
c72c0 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e 21 }. if( ppOpen!
c72d0 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 20 =0 ){. pOpen
c72e0 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 = openList;.
c72f0 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 20 while( pOpen &&
c7300 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c 20 memcmp(&fileId,
c7310 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c 20 &pOpen->fileId,
c7320 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20 sizeof(fileId))
c7330 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d ){. pOpen =
c7340 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 pOpen->pNext;.
c7350 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 }. if( pOp
c7360 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 en==0 ){. p
c7370 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d Open = sqlite3_m
c7380 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 alloc( sizeof(*p
c7390 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 69 Open) );. i
c73a0 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 f( pOpen==0 ){.
c73b0 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c 6f releaseLo
c73c0 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 ckInfo(pLock);.
c73d0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
c73e0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
c73f0 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 goto exit_find
c7400 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 lockinfo;.
c7410 7d 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 }. pOpen->f
c7420 69 6c 65 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a ileId = fileId;.
c7430 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 pOpen->nRe
c7440 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 f = 1;. pOp
c7450 65 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 en->nLock = 0;.
c7460 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e pOpen->nPen
c7470 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 ding = 0;.
c7480 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 pOpen->aPending
c7490 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e = 0;. pOpen
c74a0 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 ->pNext = openLi
c74b0 73 74 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d st;. pOpen-
c74c0 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 >pPrev = 0;.
c74d0 20 20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 if( openList )
c74e0 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 openList->pPrev
c74f0 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 = pOpen;.
c7500 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e openList = pOpen
c7510 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 ;.#if OS_VXWORKS
c7520 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 53 . pOpen->pS
c7530 65 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 em = NULL;.
c7540 20 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 pOpen->aSemName
c7550 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 23 65 6e 64 [0] = '\0';.#end
c7560 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 if. }else{.
c7570 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b pOpen->nRef+
c7580 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 +;. }. *pp
c7590 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 Open = pOpen;.
c75a0 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b }..exit_findlock
c75b0 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 info:. return r
c75c0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 c;.}../*.** If w
c75d0 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 e are currently
c75e0 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 in a different t
c75f0 68 72 65 61 64 20 74 68 61 6e 20 74 68 65 20 74 hread than the t
c7600 68 72 65 61 64 20 74 68 61 74 20 74 68 65 0a 2a hread that the.*
c7610 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 67 75 6d * unixFile argum
c7620 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 ent belongs to,
c7630 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 6f 77 then transfer ow
c7640 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 75 nership of the u
c7650 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 nixFile.** over
c7660 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 to the current t
c7670 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 hread..**.** A u
c7680 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20 nixFile is only
c7690 6f 77 6e 65 64 20 62 79 20 61 20 74 68 72 65 61 owned by a threa
c76a0 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 d on systems tha
c76b0 74 20 75 73 65 20 4c 69 6e 75 78 54 68 72 65 61 t use LinuxThrea
c76c0 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 ds..**.** Owners
c76d0 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 hip transfer is
c76e0 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 only allowed if
c76f0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 the unixFile is
c7700 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b currently unlock
c7710 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e ed..** If the un
c7720 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 ixFile is locked
c7730 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 and an ownershi
c7740 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e p is wrong, then
c7750 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 return.** SQLIT
c7760 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 E_MISUSE. SQLIT
c7770 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
c7780 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 if everything w
c7790 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c orks..*/.#if SQL
c77a0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
c77b0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 & defined(__linu
c77c0 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e 74 20 x__).static int
c77d0 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 transferOwnershi
c77e0 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c p(unixFile *pFil
c77f0 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 e){. int rc;.
c7800 70 74 68 72 65 61 64 5f 74 20 68 53 65 6c 66 3b pthread_t hSelf;
c7810 0a 20 20 69 66 28 20 74 68 72 65 61 64 73 4f 76 . if( threadsOv
c7820 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
c7830 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 Locks ){. /*
c7840 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 Ownership transf
c7850 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f ers not needed o
c7860 6e 20 74 68 69 73 20 73 79 73 74 65 6d 20 2a 2f n this system */
c7870 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
c7880 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 TE_OK;. }. hSe
c7890 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c lf = pthread_sel
c78a0 66 28 29 3b 0a 20 20 69 66 28 20 70 74 68 72 65 f();. if( pthre
c78b0 61 64 5f 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e ad_equal(pFile->
c78c0 74 69 64 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 tid, hSelf) ){.
c78d0 20 20 20 2f 2a 20 57 65 20 61 72 65 20 73 74 69 /* We are sti
c78e0 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d 65 20 74 ll in the same t
c78f0 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 hread */. OST
c7900 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 RACE1("No-transf
c7910 65 72 2c 20 73 61 6d 65 20 74 68 72 65 61 64 5c er, same thread\
c7920 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 n");. return
c7930 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
c7940 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
c7950 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b type!=NO_LOCK ){
c7960 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f . /* We canno
c7970 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 t change ownersh
c7980 69 70 20 77 68 69 6c 65 20 77 65 20 61 72 65 20 ip while we are
c7990 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 holding a lock!
c79a0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 */. return SQ
c79b0 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
c79c0 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 72 61 . OSTRACE4("Tra
c79d0 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 nsfer ownership
c79e0 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 20 74 6f of %d from %d to
c79f0 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %d\n",.
c7a00 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 pFile->h, pF
c7a10 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 ile->tid, hSelf)
c7a20 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d ;. pFile->tid =
c7a30 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 hSelf;. if (pF
c7a40 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 ile->pLock != NU
c7a50 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 LL) {. releas
c7a60 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d eLockInfo(pFile-
c7a70 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 >pLock);. rc
c7a80 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 = findLockInfo(p
c7a90 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e 70 4c File, &pFile->pL
c7aa0 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54 ock, 0);. OST
c7ab0 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 RACE5("LOCK %
c7ac0 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 d is now %s(%s,%
c7ad0 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c d)\n", pFile->h,
c7ae0 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b . lock
c7af0 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e typeName(pFile->
c7b00 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 locktype),.
c7b10 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 locktypeNa
c7b20 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d me(pFile->pLock-
c7b30 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46 69 6c >locktype), pFil
c7b40 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a e->pLock->cnt);.
c7b50 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
c7b60 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 } else {. re
c7b70 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c7b80 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 }.}.#else /*
c7b90 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f 54 48 if not SQLITE_TH
c7ba0 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 20 2f 2a READSAFE */. /*
c7bb0 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 On single-threa
c7bc0 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 ded builds, owne
c7bd0 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 rship transfer i
c7be0 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 s a no-op */.# d
c7bf0 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 efine transferOw
c7c00 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c 49 54 nership(X) SQLIT
c7c10 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 E_OK.#endif /* S
c7c20 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
c7c30 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 */.../*.** This
c7c40 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
c7c50 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 if there is a RE
c7c60 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 SERVED lock held
c7c70 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 on the specifie
c7c80 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 d.** file by thi
c7c90 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
c7ca0 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 rocess. If such
c7cb0 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 a lock is held,
c7cc0 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 set *pResOut.**
c7cd0 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 to a non-zero va
c7ce0 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 lue otherwise *p
c7cf0 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f ResOut is set to
c7d00 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 zero. The retu
c7d10 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 rn value.** is s
c7d20 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 et to SQLITE_OK
c7d30 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 unless an I/O er
c7d40 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
c7d50 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e g lock checking.
c7d60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
c7d70 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 nixCheckReserved
c7d80 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
c7d90 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
c7da0 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d Out){. int rc =
c7db0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
c7dc0 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a t reserved = 0;.
c7dd0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
c7de0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
c7df0 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f d;.. SimulateIO
c7e00 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
c7e10 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
c7e20 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b RESERVEDLOCK; );
c7e30 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
c7e40 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 e );. unixEnter
c7e50 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 Mutex(); /* Beca
c7e60 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b use pFile->pLock
c7e70 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 is shared acros
c7e80 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 s threads */..
c7e90 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 /* Check if a th
c7ea0 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f read in this pro
c7eb0 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 cess holds such
c7ec0 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 a lock */. if(
c7ed0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f pFile->pLock->lo
c7ee0 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f cktype>SHARED_LO
c7ef0 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 CK ){. reserv
c7f00 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f ed = 1;. }.. /
c7f10 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 * Otherwise see
c7f20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 if some other pr
c7f30 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a ocess holds it..
c7f40 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 5f 44 */.#ifndef __D
c7f50 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 21 72 65 JGPP__. if( !re
c7f60 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 74 served ){. st
c7f70 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b ruct flock lock;
c7f80 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e . lock.l_when
c7f90 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
c7fa0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
c7fb0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b = RESERVED_BYTE;
c7fc0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 . lock.l_len
c7fd0 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f = 1;. lock.l_
c7fe0 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a type = F_WRLCK;.
c7ff0 20 20 20 20 69 66 20 28 2d 31 20 3d 3d 20 66 63 if (-1 == fc
c8000 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f ntl(pFile->h, F_
c8010 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b GETLK, &lock)) {
c8020 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e . int tErrn
c8030 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
c8040 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
c8050 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
c8060 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
c8070 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
c8080 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 70 EDLOCK);. p
c8090 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
c80a0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 = tErrno;. }
c80b0 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f else if( lock.l_
c80c0 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b type!=F_UNLCK ){
c80d0 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 . reserved
c80e0 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 = 1;. }. }.#
c80f0 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69 78 4c endif. . unixL
c8100 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f eaveMutex();. O
c8110 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 STRACE4("TEST WR
c8120 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e -LOCK %d %d %d\n
c8130 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c ", pFile->h, rc,
c8140 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a reserved);.. *
c8150 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 pResOut = reserv
c8160 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ed;. return rc;
c8170 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
c8180 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
c8190 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
c81a0 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
c81b0 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
c81c0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
c81d0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
c81e0 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
c81f0 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
c8200 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
c8210 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
c8220 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
c8230 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
c8240 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
c8250 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
c8260 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
c8270 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
c8280 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
c8290 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
c82a0 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
c82b0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
c82c0 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
c82d0 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
c82e0 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
c82f0 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
c8300 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
c8310 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
c8320 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
c8330 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
c8340 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
c8350 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
c8360 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
c8370 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
c8380 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
c8390 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
c83a0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
c83b0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
c83c0 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
c83d0 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
c83e0 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
c83f0 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
c8400 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
c8410 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
c8420 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
c8430 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
c8440 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
c8450 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c ck. Use the sql
c8460 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a ite3OsUnlock().*
c8470 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 * routine to low
c8480 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 er a locking lev
c8490 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e el..*/.static in
c84a0 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 t unixLock(sqlit
c84b0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
c84c0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a locktype){. /*
c84d0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 The following d
c84e0 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70 escribes the imp
c84f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
c8500 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 he various locks
c8510 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 and. ** lock t
c8520 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65 ransitions in te
c8530 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58 rms of the POSIX
c8540 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64 advisory shared
c8550 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20 and exclusive.
c8560 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69 ** lock primiti
c8570 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64 ves (called read
c8580 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65 -locks and write
c8590 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f -locks below, to
c85a0 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 avoid. ** conf
c85b0 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74 usion with SQLit
c85c0 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54 e lock names). T
c85d0 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 he algorithms ar
c85e0 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20 e complicated.
c85f0 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f ** slightly in o
c8600 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61 rder to be compa
c8610 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f tible with windo
c8620 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c ws systems simul
c8630 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 taneously. ** a
c8640 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d ccessing the sam
c8650 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
c8660 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73 in case that is
c8670 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a ever required..
c8680 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c **. ** Symbol
c8690 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e s defined in os.
c86a0 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20 h indentify the
c86b0 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61 'pending byte' a
c86c0 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64 nd the 'reserved
c86d0 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63 . ** byte', eac
c86e0 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61 h single bytes a
c86f0 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 t well known off
c8700 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73 sets, and the 's
c8710 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 hared byte. **
c8720 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20 range', a range
c8730 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20 of 510 bytes at
c8740 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 a well known off
c8750 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 set.. **. ** T
c8760 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45 o obtain a SHARE
c8770 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c D lock, a read-l
c8780 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ock is obtained
c8790 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a on the 'pending.
c87a0 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20 ** byte'. If
c87b0 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66 this is successf
c87c0 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 ul, a random byt
c87d0 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72 e from the 'shar
c87e0 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e ed byte. ** ran
c87f0 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b ge' is read-lock
c8800 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 ed and the lock
c8810 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 on the 'pending
c8820 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a byte' released..
c8830 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 **. ** A proc
c8840 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 ess may only obt
c8850 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c ain a RESERVED l
c8860 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 ock after it has
c8870 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a a SHARED lock..
c8880 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20 ** A RESERVED
c8890 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e lock is implemen
c88a0 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20 ted by grabbing
c88b0 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
c88c0 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76 the. ** 'reserv
c88d0 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a ed byte'. . **.
c88e0 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d ** A process m
c88f0 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 ay only obtain a
c8900 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66 PENDING lock af
c8910 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69 ter it has obtai
c8920 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45 ned a. ** SHARE
c8930 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e D lock. A PENDIN
c8940 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d G lock is implem
c8950 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 ented by obtaini
c8960 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a ng a write-lock.
c8970 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e ** on the 'pen
c8980 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73 ding byte'. This
c8990 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f ensures that no
c89a0 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b new SHARED lock
c89b0 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 s can be. ** ob
c89c0 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73 tained, but exis
c89d0 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b ting SHARED lock
c89e0 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f s are allowed to
c89f0 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63 persist. A proc
c8a00 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f ess. ** does no
c8a10 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e t have to obtain
c8a20 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
c8a30 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61 on the way to a
c8a40 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 PENDING lock..
c8a50 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74 ** This propert
c8a60 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 y is used by the
c8a70 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72 algorithm for r
c8a80 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f olling back a jo
c8a90 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 urnal file. **
c8aa0 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20 after a crash..
c8ab0 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c **. ** An EXCL
c8ac0 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 USIVE lock, obta
c8ad0 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e ined after a PEN
c8ae0 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c DING lock is hel
c8af0 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 d, is. ** imple
c8b00 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e mented by obtain
c8b10 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
c8b20 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27 on the entire '
c8b30 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a shared byte. **
c8b40 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 range'. Since a
c8b50 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 ll other locks r
c8b60 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f equire a read-lo
c8b70 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 ck on one of the
c8b80 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68 bytes. ** with
c8b90 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 in this range, t
c8ba0 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 his ensures that
c8bb0 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 no other locks
c8bc0 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a are held on the.
c8bd0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a ** database. .
c8be0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65 **. ** The re
c8bf0 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79 ason a single by
c8c00 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 te cannot be use
c8c10 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 d instead of the
c8c20 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 'shared byte.
c8c30 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61 ** range' is tha
c8c40 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 t some versions
c8c50 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f of windows do no
c8c60 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c t support read-l
c8c70 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f ocks. By. ** lo
c8c80 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62 cking a random b
c8c90 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65 yte from a range
c8ca0 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41 , concurrent SHA
c8cb0 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78 RED locks may ex
c8cc0 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 ist. ** even if
c8cd0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 the locking pri
c8ce0 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61 mitive used is a
c8cf0 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f lways a write-lo
c8d00 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 ck.. */. int r
c8d10 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
c8d20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
c8d30 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
c8d40 3b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c ;. struct unixL
c8d50 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d ockInfo *pLock =
c8d60 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 pFile->pLock;.
c8d70 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f struct flock lo
c8d80 63 6b 3b 0a 20 20 69 6e 74 20 73 3b 0a 0a 20 20 ck;. int s;..
c8d90 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
c8da0 0a 20 20 4f 53 54 52 41 43 45 37 28 22 4c 4f 43 . OSTRACE7("LOC
c8db0 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 25 K %d %s was %
c8dc0 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 64 5c s(%s,%d) pid=%d\
c8dd0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
c8de0 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 locktypeName
c8df0 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b (locktype), lock
c8e00 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e typeName(pFile->
c8e10 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 locktype),.
c8e20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c locktypeName(pL
c8e30 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 ock->locktype),
c8e40 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 pLock->cnt , get
c8e50 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 pid());.. /* If
c8e60 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 there is alread
c8e70 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 y a lock of this
c8e80 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 type or more re
c8e90 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 strictive on the
c8ea0 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 . ** unixFile,
c8eb0 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 do nothing. Don'
c8ec0 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f t use the end_lo
c8ed0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 ck: exit path, a
c8ee0 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 s. ** unixEnter
c8ef0 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 Mutex() hasn't b
c8f00 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a een called yet..
c8f10 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 */. if( pFile
c8f20 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b ->locktype>=lock
c8f30 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 type ){. OSTR
c8f40 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE3("LOCK %d
c8f50 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 %s ok (already
c8f60 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d held)\n", pFile-
c8f70 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 >h,.
c8f80 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 locktypeName(loc
c8f90 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 ktype));. ret
c8fa0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
c8fb0 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 }.. /* Make su
c8fc0 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 re the locking s
c8fd0 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 equence is corre
c8fe0 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ct. */. assert
c8ff0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
c9000 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f e!=NO_LOCK || lo
c9010 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
c9020 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OCK );. assert(
c9030 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 locktype!=PENDI
c9040 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 NG_LOCK );. ass
c9050 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 ert( locktype!=R
c9060 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 ESERVED_LOCK ||
c9070 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
c9080 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
c9090 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 . /* This mutex
c90a0 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 is needed becau
c90b0 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 se pFile->pLock
c90c0 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 is shared across
c90d0 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 threads. */.
c90e0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
c90f0 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 ;.. /* Make sur
c9100 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 e the current th
c9110 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 read owns the pF
c9120 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d ile.. */. rc =
c9130 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 transferOwnersh
c9140 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 ip(pFile);. if(
c9150 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
c9160 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d {. unixLeaveM
c9170 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 utex();. retu
c9180 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f rn rc;. }. pLo
c9190 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 ck = pFile->pLoc
c91a0 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 k;.. /* If some
c91b0 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 thread using th
c91c0 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 is PID has a loc
c91d0 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e k via a differen
c91e0 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a t unixFile*. **
c91f0 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 handle that pre
c9200 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65 cludes the reque
c9210 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 sted lock, retur
c9220 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 n BUSY.. */. i
c9230 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 f( (pFile->lockt
c9240 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b ype!=pLock->lock
c9250 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 20 type && .
c9260 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 (pLock->lockt
c9270 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype>=PENDING_LOC
c9280 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48 K || locktype>SH
c9290 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b ARED_LOCK)). ){
c92a0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
c92b0 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 _BUSY;. goto
c92c0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 end_lock;. }..
c92d0 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 /* If a SHARED
c92e0 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 lock is requeste
c92f0 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 d, and some thre
c9300 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 ad using this PI
c9310 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 D already. ** h
c9320 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 as a SHARED or R
c9330 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 ESERVED lock, th
c9340 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 en increment ref
c9350 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e erence counts an
c9360 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 d. ** return SQ
c9370 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 LITE_OK.. */.
c9380 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
c9390 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 ARED_LOCK && .
c93a0 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b (pLock->lock
c93b0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
c93c0 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b K || pLock->lock
c93d0 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c type==RESERVED_L
c93e0 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65 OCK) ){. asse
c93f0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 rt( locktype==SH
c9400 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 ARED_LOCK );.
c9410 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
c9420 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20 locktype==0 );.
c9430 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
c9440 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70 ->cnt>0 );. p
c9450 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
c9460 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
c9470 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a pLock->cnt++;.
c9480 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e pFile->pOpen
c9490 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 ->nLock++;. g
c94a0 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 oto end_lock;.
c94b0 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 }.. lock.l_len
c94c0 3d 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f = 1L;.. lock.l_
c94d0 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
c94e0 54 3b 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 T;.. /* A PENDI
c94f0 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 NG lock is neede
c9500 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 d before acquiri
c9510 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ng a SHARED lock
c9520 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a and before. **
c9530 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 acquiring an EX
c9540 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 CLUSIVE lock. F
c9550 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f or the SHARED lo
c9560 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 ck, the PENDING
c9570 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c will. ** be rel
c9580 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 eased.. */. if
c9590 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
c95a0 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c ED_LOCK . |
c95b0 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 | (locktype==EXC
c95c0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
c95d0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 File->locktype<P
c95e0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 ENDING_LOCK). )
c95f0 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 {. lock.l_typ
c9600 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 e = (locktype==S
c9610 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c HARED_LOCK?F_RDL
c9620 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 CK:F_WRLCK);.
c9630 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 lock.l_start =
c9640 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 PENDING_BYTE;.
c9650 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c s = fcntl(pFil
c9660 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 e->h, F_SETLK, &
c9670 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 lock);. if( s
c9680 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 ==(-1) ){.
c9690 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
c96a0 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 no;. rc = s
c96b0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
c96c0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
c96d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
c96e0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
c96f0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
c9700 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
c9710 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
c9720 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
c9730 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f goto end_lo
c9740 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a ck;. }. }...
c9750 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 /* If control
c9760 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 gets to this poi
c9770 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c nt, then actuall
c9780 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d y go ahead and m
c9790 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 ake. ** operati
c97a0 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 ng system calls
c97b0 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 for the specifie
c97c0 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 d lock.. */. i
c97d0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
c97e0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
c97f0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a int tErrno = 0;.
c9800 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
c9810 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 k->cnt==0 );.
c9820 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
c9830 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a locktype==0 );..
c9840 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 /* Now get t
c9850 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a he read-lock */.
c9860 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
c9870 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
c9880 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 . lock.l_len
c9890 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 = SHARED_SIZE;.
c98a0 20 20 20 69 66 28 20 28 73 20 3d 20 66 63 6e 74 if( (s = fcnt
c98b0 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 l(pFile->h, F_SE
c98c0 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 3d 3d 28 2d TLK, &lock))==(-
c98d0 31 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 1) ){. tErr
c98e0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
c98f0 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 }. /* Drop th
c9900 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 e temporary PEND
c9910 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ING lock */.
c9920 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 lock.l_start = P
c9930 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
c9940 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c lock.l_len = 1L
c9950 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 ;. lock.l_typ
c9960 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 e = F_UNLCK;.
c9970 20 69 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 if( fcntl(pFile
c9980 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
c9990 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ock)!=0 ){.
c99a0 20 69 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a if( s != -1 ){.
c99b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
c99c0 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 could happen wit
c99d0 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e h a network moun
c99e0 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72 t */. tEr
c99f0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 rno = errno; .
c9a00 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
c9a10 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
c9a20 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
c9a30 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
c9a40 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 ); . if(
c9a50 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
c9a60 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
c9a70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
c9a80 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
c9a90 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f }. goto
c9aa0 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 end_lock;.
c9ab0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
c9ac0 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 s==(-1) ){.
c9ad0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
c9ae0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
c9af0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
c9b00 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 IOERR_LOCK);.
c9b10 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
c9b20 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
c9b30 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
c9b40 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
c9b50 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
c9b60 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
c9b70 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 ocktype = SHARED
c9b80 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 _LOCK;. pFi
c9b90 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b le->pOpen->nLock
c9ba0 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d ++;. pLock-
c9bb0 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a >cnt = 1;. }.
c9bc0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b }else if( lock
c9bd0 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f type==EXCLUSIVE_
c9be0 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 LOCK && pLock->c
c9bf0 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 nt>1 ){. /* W
c9c00 65 20 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 e are trying for
c9c10 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
c9c20 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 ck but another t
c9c30 68 72 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 hread in this.
c9c40 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 ** same proces
c9c50 73 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 s is still holdi
c9c60 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b ng a shared lock
c9c70 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 . */. rc = SQ
c9c80 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c LITE_BUSY;. }el
c9c90 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 se{. /* The r
c9ca0 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 equest was for a
c9cb0 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 RESERVED or EXC
c9cc0 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 LUSIVE lock. It
c9cd0 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d is. ** assum
c9ce0 65 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 ed that there is
c9cf0 20 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 a SHARED or gre
c9d00 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ater lock on the
c9d10 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 file. ** alr
c9d20 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 eady.. */.
c9d30 20 61 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c assert( 0!=pFil
c9d40 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
c9d50 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d lock.l_type =
c9d60 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 F_WRLCK;. sw
c9d70 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 itch( locktype )
c9d80 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 45 53 {. case RES
c9d90 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 ERVED_LOCK:.
c9da0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
c9db0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 = RESERVED_BYTE
c9dc0 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
c9dd0 0a 20 20 20 20 20 20 63 61 73 65 20 45 58 43 4c . case EXCL
c9de0 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 USIVE_LOCK:.
c9df0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
c9e00 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
c9e10 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
c9e20 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a len = SHARED_SIZ
c9e30 45 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b E;. break
c9e40 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a ;. default:
c9e50 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
c9e60 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 20 0);. }. s
c9e70 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 = fcntl(pFile->h
c9e80 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
c9e90 29 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d );. if( s==(-
c9ea0 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 1) ){. int
c9eb0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
c9ec0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
c9ed0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
c9ee0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
c9ef0 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b ITE_IOERR_LOCK);
c9f00 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f . if( IS_LO
c9f10 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
c9f20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
c9f30 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
c9f40 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d o;. }. }
c9f50 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 . }. ..#ifndef
c9f60 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 NDEBUG. /* Set
c9f70 20 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 up the transact
c9f80 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e ion-counter chan
c9f90 67 65 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 ge checking flag
c9fa0 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e s when. ** tran
c9fb0 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 sitioning from a
c9fc0 20 53 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 SHARED to a RES
c9fd0 45 52 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 ERVED lock. The
c9fe0 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f change. ** fro
c9ff0 6d 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 m SHARED to RESE
ca000 52 56 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 RVED marks the b
ca010 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f eginning of a no
ca020 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 rmal. ** write
ca030 6f 70 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 operation (not a
ca040 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot journal rol
ca050 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 lback).. */. i
ca060 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
ca070 0a 20 20 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f . && pFile->lo
ca080 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
ca090 4f 43 4b 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 OCK. && lockty
ca0a0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe==RESERVED_LOC
ca0b0 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 K. ){. pFile
ca0c0 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 ->transCntrChng
ca0d0 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e = 0;. pFile->
ca0e0 64 62 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 dbUpdate = 0;.
ca0f0 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 pFile->inNorma
ca100 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a lWrite = 1;. }.
ca110 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 #endif... if( r
ca120 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
ca130 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
ca140 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
ca150 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 pLock->lockt
ca160 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
ca170 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b }else if( lock
ca180 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f type==EXCLUSIVE_
ca190 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c LOCK ){. pFil
ca1a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 e->locktype = PE
ca1b0 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 NDING_LOCK;.
ca1c0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 pLock->locktype
ca1d0 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a = PENDING_LOCK;.
ca1e0 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 }..end_lock:.
ca1f0 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
ca200 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c );. OSTRACE4("L
ca210 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c OCK %d %s %s\
ca220 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
ca230 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 cktypeName(lockt
ca240 79 70 65 29 2c 20 0a 20 20 20 20 20 20 72 63 3d ype), . rc=
ca250 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b =SQLITE_OK ? "ok
ca260 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 " : "failed");.
ca270 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
ca280 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c *.** Lower the l
ca290 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 ocking level on
ca2a0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
ca2b0 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 pFile to locktyp
ca2c0 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 e. locktype.**
ca2d0 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e must be either N
ca2e0 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 O_LOCK or SHARED
ca2f0 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 _LOCK..**.** If
ca300 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
ca310 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 l of the file de
ca320 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 scriptor is alre
ca330 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a ady at or below.
ca340 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 ** the requested
ca350 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 locking level,
ca360 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
ca370 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 a no-op..*/.stat
ca380 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 ic int unixUnloc
ca390 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
ca3a0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
ca3b0 29 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 ){. struct unix
ca3c0 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b LockInfo *pLock;
ca3d0 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 . struct flock
ca3e0 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 20 3d lock;. int rc =
ca3f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e SQLITE_OK;. un
ca400 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
ca410 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
ca420 20 69 6e 74 20 68 3b 0a 0a 20 20 61 73 73 65 72 int h;.. asser
ca430 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 t( pFile );. OS
ca440 54 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 TRACE7("UNLOCK
ca450 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c %d %d was %d(%d,
ca460 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %d) pid=%d\n", p
ca470 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
ca480 65 2c 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e e,. pFile->
ca490 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d locktype, pFile-
ca4a0 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 >pLock->locktype
ca4b0 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e , pFile->pLock->
ca4c0 63 6e 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a cnt, getpid());.
ca4d0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
ca4e0 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
ca4f0 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d );. if( pFile-
ca500 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 >locktype<=lockt
ca510 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
ca520 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
ca530 0a 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 . if( CHECK_THR
ca540 45 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a EADID(pFile) ){.
ca550 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ca560 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
ca570 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
ca580 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68 ;. h = pFile->h
ca590 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c ;. pLock = pFil
ca5a0 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65 e->pLock;. asse
ca5b0 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d rt( pLock->cnt!=
ca5c0 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 0 );. if( pFile
ca5d0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 ->locktype>SHARE
ca5e0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 D_LOCK ){. as
ca5f0 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 sert( pLock->loc
ca600 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f ktype==pFile->lo
ca610 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 53 69 cktype );. Si
ca620 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
ca630 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 ign(1);. Simu
ca640 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 lateIOError( h=(
ca650 2d 31 29 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 -1) ). Simula
ca660 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
ca670 30 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 0);..#ifndef NDE
ca680 42 55 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 BUG. /* When
ca690 72 65 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 reducing a lock
ca6a0 73 75 63 68 20 74 68 61 74 20 6f 74 68 65 72 20 such that other
ca6b0 70 72 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74 processes can st
ca6c0 61 72 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 art. ** readi
ca6d0 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ng the database
ca6e0 66 69 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 file again, make
ca6f0 20 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 20 sure that the.
ca700 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f ** transactio
ca710 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 n counter was up
ca720 64 61 74 65 64 20 69 66 20 61 6e 79 20 70 61 72 dated if any par
ca730 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 t of the databas
ca740 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 e. ** file ch
ca750 61 6e 67 65 64 2e 20 20 49 66 20 74 68 65 20 74 anged. If the t
ca760 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
ca770 65 72 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65 er is not update
ca780 64 2c 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 d,. ** other
ca790 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 connections to t
ca7a0 68 65 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 he same file mig
ca7b0 68 74 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 ht not realize t
ca7c0 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 hat. ** the f
ca7d0 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 ile has changed
ca7e0 61 6e 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20 and hence might
ca7f0 6e 6f 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 not know to flus
ca800 68 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 h their. ** c
ca810 61 63 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f ache. The use o
ca820 66 20 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 f a stale cache
ca830 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 can lead to data
ca840 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e base corruption.
ca850 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
ca860 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 rt( pFile->inNor
ca870 6d 61 6c 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 malWrite==0.
ca880 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 || pFile->d
ca890 62 55 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 bUpdate==0.
ca8a0 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72 || pFile->tr
ca8b0 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29 ansCntrChng==1 )
ca8c0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e ;. pFile->inN
ca8d0 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a ormalWrite = 0;.
ca8e0 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20 69 66 28 #endif... if(
ca8f0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
ca900 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 D_LOCK ){.
ca910 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f lock.l_type = F_
ca920 52 44 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 RDLCK;. loc
ca930 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 k.l_whence = SEE
ca940 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 K_SET;. loc
ca950 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 k.l_start = SHAR
ca960 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 ED_FIRST;.
ca970 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 lock.l_len = SHA
ca980 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 RED_SIZE;.
ca990 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 if( fcntl(h, F_S
ca9a0 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d ETLK, &lock)==(-
ca9b0 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 1) ){. in
ca9c0 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f t tErrno = errno
ca9d0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
ca9e0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
ca9f0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
caa00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 SQLITE_IOERR_RD
caa10 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 LOCK);. i
caa20 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
caa30 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (rc) ){.
caa40 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
caa50 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
caa60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 }. g
caa70 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a oto end_unlock;.
caa80 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
caa90 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 lock.l_type =
caaa0 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63 F_UNLCK;. loc
caab0 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 k.l_whence = SEE
caac0 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e K_SET;. lock.
caad0 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e l_start = PENDIN
caae0 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b G_BYTE;. lock
caaf0 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 73 .l_len = 2L; as
cab00 73 65 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 59 sert( PENDING_BY
cab10 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f 42 TE+1==RESERVED_B
cab20 59 54 45 20 29 3b 0a 20 20 20 20 69 66 28 20 66 YTE );. if( f
cab30 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c cntl(h, F_SETLK,
cab40 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b &lock)!=(-1) ){
cab50 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f . pLock->lo
cab60 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f cktype = SHARED_
cab70 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LOCK;. }else{
cab80 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e . int tErrn
cab90 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
caba0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
cabb0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
cabc0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
cabd0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 OERR_UNLOCK);.
cabe0 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
cabf0 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 ERROR(rc) ){.
cac00 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
cac10 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
cac20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f }. go
cac30 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 to end_unlock;.
cac40 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c }. }. if( l
cac50 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
cac60 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 ){. struct u
cac70 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 nixOpenCnt *pOpe
cac80 6e 3b 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d n;. int rc2 =
cac90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 20 SQLITE_OK;..
caca0 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 /* Decrement th
cacb0 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f e shared lock co
cacc0 75 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 unter. Release
cacd0 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 the lock using a
cace0 6e 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c n. ** OS call
cacf0 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 only when all t
cad00 68 72 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 hreads in this s
cad10 61 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 ame process have
cad20 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a released. **
cad30 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a the lock.. *
cad40 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 /. pLock->cnt
cad50 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 --;. if( pLoc
cad60 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 k->cnt==0 ){.
cad70 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d lock.l_type =
cad80 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 F_UNLCK;.
cad90 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 lock.l_whence =
cada0 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 SEEK_SET;.
cadb0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c lock.l_start = l
cadc0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a ock.l_len = 0L;.
cadd0 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f SimulateIO
cade0 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a ErrorBenign(1);.
cadf0 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f SimulateIO
cae00 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a Error( h=(-1) ).
cae10 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f SimulateIO
cae20 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a ErrorBenign(0);.
cae30 20 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 if( fcntl(
cae40 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 h, F_SETLK, &loc
cae50 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 k)!=(-1) ){.
cae60 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 pLock->lockt
cae70 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 ype = NO_LOCK;.
cae80 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
cae90 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
caea0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
caeb0 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
caec0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
caed0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
caee0 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 ERR_UNLOCK);.
caef0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
caf00 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
caf10 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
caf20 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
caf30 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 o;. }.
caf40 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b pLock->lock
caf50 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a type = NO_LOCK;.
caf60 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
caf70 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 ocktype = NO_LOC
caf80 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d K;. }. }
caf90 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 .. /* Decreme
cafa0 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 nt the count of
cafb0 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68 locks against th
cafc0 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 is same file. W
cafd0 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 hen the. ** c
cafe0 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 ount reaches zer
caff0 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 o, close any oth
cb000 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 er file descript
cb010 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a ors whose close.
cb020 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72 ** was defer
cb030 72 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f red because of o
cb040 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 utstanding locks
cb050 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70 .. */. pOp
cb060 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 en = pFile->pOpe
cb070 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c n;. pOpen->nL
cb080 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 ock--;. asser
cb090 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e t( pOpen->nLock>
cb0a0 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f =0 );. if( pO
cb0b0 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26 pen->nLock==0 &&
cb0c0 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 pOpen->nPending
cb0d0 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 >0 ){. int
cb0e0 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 i;. for(i=0
cb0f0 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 ; i<pOpen->nPend
cb100 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 ing; i++){.
cb110 20 20 20 2f 2a 20 63 6c 6f 73 65 20 70 65 6e 64 /* close pend
cb120 69 6e 67 20 66 64 73 2c 20 62 75 74 20 69 66 20 ing fds, but if
cb130 63 6c 6f 73 69 6e 67 20 66 61 69 6c 73 20 64 6f closing fails do
cb140 6e 27 74 20 66 72 65 65 20 74 68 65 20 61 72 72 n't free the arr
cb150 61 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73 ay. ** as
cb160 73 69 67 6e 20 2d 31 20 74 6f 20 74 68 65 20 73 sign -1 to the s
cb170 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6c 6f 73 uccessfully clos
cb180 65 64 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 ed descriptors a
cb190 6e 64 20 72 65 63 6f 72 64 20 74 68 65 0a 20 20 nd record the.
cb1a0 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 2e 20 ** error.
cb1b0 20 54 68 65 20 6e 65 78 74 20 61 74 74 65 6d 70 The next attemp
cb1c0 74 20 74 6f 20 75 6e 6c 6f 63 6b 20 77 69 6c 6c t to unlock will
cb1d0 20 74 72 79 20 61 67 61 69 6e 2e 20 2a 2f 0a 20 try again. */.
cb1e0 20 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e if( pOpen
cb1f0 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3c 20 ->aPending[i] <
cb200 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
cb210 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 if( close(
cb220 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b pOpen->aPending[
cb230 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 i]) ){.
cb240 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
cb250 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
cb260 20 20 20 20 20 72 63 32 20 3d 20 53 51 4c 49 54 rc2 = SQLIT
cb270 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 E_IOERR_CLOSE;.
cb280 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
cb290 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 pOpen->a
cb2a0 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d 31 3b Pending[i] = -1;
cb2b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
cb2c0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 32 }. if( rc2
cb2d0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
cb2e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
cb2f0 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 ree(pOpen->aPend
cb300 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 70 4f ing);. pO
cb310 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d 20 pen->nPending =
cb320 30 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 0;. pOpen
cb330 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a ->aPending = 0;.
cb340 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
cb350 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
cb360 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
cb370 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 20 20 7d = rc2;. }. }
cb380 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 ...end_unlock:.
cb390 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
cb3a0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
cb3b0 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e ITE_OK ) pFile->
cb3c0 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
cb3d0 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ype;. return rc
cb3e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
cb3f0 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d function perform
cb400 73 20 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 s the parts of t
cb410 68 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 he "close file"
cb420 6f 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f operation .** co
cb430 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b mmon to all lock
cb440 69 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 ing schemes. It
cb450 63 6c 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 closes the direc
cb460 74 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a tory and file.**
cb470 20 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 handles, if the
cb480 79 20 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 y are valid, and
cb490 20 73 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 sets all fields
cb4a0 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 of the unixFile
cb4b0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f .** structure to
cb4c0 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 0..**.** It is
cb4d0 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20 *not* necessary
cb4e0 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 to hold the mute
cb4f0 78 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 x when this rout
cb500 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a ine is called,.*
cb510 2a 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b * even on VxWork
cb520 73 2e 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c s. A mutex will
cb530 20 62 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20 be acquired on
cb540 56 78 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a VxWorks by the.*
cb550 2a 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 * vxworksRelease
cb560 46 69 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65 FileId() routine
cb570 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
cb580 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 closeUnixFile(sq
cb590 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
cb5a0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
cb5b0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
cb5c0 69 64 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 id;. if( pFile
cb5d0 29 7b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 ){. if( pFile
cb5e0 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 ->dirfd>=0 ){.
cb5f0 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c int err = cl
cb600 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 ose(pFile->dirfd
cb610 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 );. if( err
cb620 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
cb630 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
cb640 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 rrno;. re
cb650 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
cb660 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 R_DIR_CLOSE;.
cb670 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
cb680 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d 2d pFile->dirfd=-
cb690 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
cb6a0 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
cb6b0 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e h>=0 ){. in
cb6c0 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 t err = close(pF
cb6d0 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 69 ile->h);. i
cb6e0 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 f( err ){.
cb6f0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
cb700 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
cb710 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
cb720 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 E_IOERR_CLOSE;.
cb730 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 }. }.#if
cb740 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 OS_VXWORKS.
cb750 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 29 if( pFile->pId )
cb760 7b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c {. if( pFil
cb770 65 2d 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a 20 e->isDelete ){.
cb780 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46 unlink(pF
cb790 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e ile->pId->zCanon
cb7a0 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 icalName);.
cb7b0 20 7d 0a 20 20 20 20 20 20 76 78 77 6f 72 6b 73 }. vxworks
cb7c0 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46 ReleaseFileId(pF
cb7d0 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 20 ile->pId);.
cb7e0 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b pFile->pId = 0;
cb7f0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
cb800 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 OSTRACE2("CLOS
cb810 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 E %-3d\n", pFi
cb820 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 6e le->h);. Open
cb830 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20 Counter(-1);.
cb840 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 memset(pFile, 0
cb850 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c , sizeof(unixFil
cb860 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 e));. }. retur
cb870 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
cb880 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
cb890 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
cb8a0 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 t unixClose(sqli
cb8b0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
cb8c0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
cb8d0 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b _OK;. if( id ){
cb8e0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
cb8f0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
cb900 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 *)id;. unixU
cb910 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
cb920 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 K);. unixEnte
cb930 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 rMutex();. if
cb940 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 ( pFile->pOpen &
cb950 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e & pFile->pOpen->
cb960 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f nLock ){. /
cb970 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f * If there are o
cb980 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 utstanding locks
cb990 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c , do not actuall
cb9a0 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 y close the file
cb9b0 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 just. ** y
cb9c0 65 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20 et because that
cb9d0 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 would clear thos
cb9e0 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 e locks. Instea
cb9f0 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a d, add the file.
cba00 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 ** descrip
cba10 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 tor to pOpen->aP
cba20 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c 6c ending. It will
cba30 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c be automaticall
cba40 79 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20 20 y closed when.
cba50 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 ** the last
cba60 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e lock is cleared.
cba70 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
cba80 69 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 20 20 20 int *aNew;.
cba90 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e struct unixOpen
cbaa0 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 Cnt *pOpen = pFi
cbab0 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 le->pOpen;.
cbac0 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f aNew = sqlite3_
cbad0 72 65 61 6c 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 realloc(pOpen->a
cbae0 50 65 6e 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d Pending, (pOpen-
cbaf0 3e 6e 50 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a >nPending+1)*siz
cbb00 65 6f 66 28 69 6e 74 29 20 29 3b 0a 20 20 20 20 eof(int) );.
cbb10 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b if( aNew==0 ){
cbb20 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 . /* If a
cbb30 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a malloc fails, j
cbb40 75 73 74 20 6c 65 61 6b 20 74 68 65 20 66 69 6c ust leak the fil
cbb50 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a e descriptor */.
cbb60 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
cbb70 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e pOpen->aPen
cbb80 64 69 6e 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 ding = aNew;.
cbb90 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e pOpen->aPen
cbba0 64 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e ding[pOpen->nPen
cbbb0 64 69 6e 67 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 ding] = pFile->h
cbbc0 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d ;. pOpen-
cbbd0 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 >nPending++;.
cbbe0 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 pFile->h =
cbbf0 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 -1;. }.
cbc00 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 }. releaseLoc
cbc10 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f kInfo(pFile->pLo
cbc20 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 ck);. release
cbc30 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 OpenCnt(pFile->p
cbc40 4f 70 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d 20 Open);. rc =
cbc50 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 closeUnixFile(id
cbc60 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 );. unixLeave
cbc70 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 Mutex();. }. r
cbc80 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a eturn rc;.}../**
cbc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
cbca0 20 6f 66 20 74 68 65 20 70 6f 73 69 78 20 61 64 of the posix ad
cbcb0 76 69 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c visory lock impl
cbcc0 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a ementation *****
cbcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
cbce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
cbd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
cbd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d ************ No-
cbda0 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a op Locking *****
cbdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
cbdd0 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61 72 69 6f .** Of the vario
cbde0 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 us locking imple
cbdf0 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 6c mentations avail
cbe00 61 62 6c 65 2c 20 74 68 69 73 20 69 73 20 62 79 able, this is by
cbe10 20 66 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 far the.** simp
cbe20 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 lest: locking i
cbe30 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 s ignored. No a
cbe40 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 ttempt is made t
cbe50 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 o lock the datab
cbe60 61 73 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 ase.** file for
cbe70 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
cbe80 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c ng..**.** This l
cbe90 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 ocking mode is a
cbea0 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 ppropriate for u
cbeb0 73 65 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 se on read-only
cbec0 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 databases.** (ex
cbed0 3a 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 : databases that
cbee0 20 61 72 65 20 62 75 72 6e 65 64 20 69 6e 74 6f are burned into
cbef0 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 CD-ROM, for exa
cbf00 6d 70 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a mple.) It can.*
cbf10 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 69 * also be used i
cbf20 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f f the applicatio
cbf30 6e 20 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 n employs some e
cbf40 78 74 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 xternal mechanis
cbf50 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 m to.** prevent
cbf60 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 simultaneous acc
cbf70 65 73 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ess of the same
cbf80 64 61 74 61 62 61 73 65 20 62 79 20 74 77 6f 20 database by two
cbf90 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 or more.** datab
cbfa0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
cbfb0 20 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61 But there is a
cbfc0 20 73 65 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 serious risk of
cbfd0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 database.** cor
cbfe0 72 75 70 74 69 6f 6e 20 69 66 20 74 68 69 73 20 ruption if this
cbff0 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 locking mode is
cc000 75 73 65 64 20 69 6e 20 73 69 74 75 61 74 69 6f used in situatio
cc010 6e 73 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c ns where multipl
cc020 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f e.** database co
cc030 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 61 63 nnections are ac
cc040 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 cessing the same
cc050 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
cc060 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 t the same.** ti
cc070 6d 65 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f me and one or mo
cc080 72 65 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e re of those conn
cc090 65 63 74 69 6f 6e 73 20 61 72 65 20 77 72 69 74 ections are writ
cc0a0 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 ing..*/..static
cc0b0 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 int nolockCheckR
cc0c0 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
cc0d0 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 te3_file *NotUse
cc0e0 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 d, int *pResOut)
cc0f0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
cc100 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
cc110 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 *pResOut = 0;.
cc120 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
cc130 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 K;.}.static int
cc140 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 nolockLock(sqlit
cc150 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 e3_file *NotUsed
cc160 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b , int NotUsed2){
cc170 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
cc180 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
cc190 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 tUsed2);. retur
cc1a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 n SQLITE_OK;.}.s
cc1b0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b tatic int nolock
cc1c0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
cc1d0 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e ile *NotUsed, in
cc1e0 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 t NotUsed2){. U
cc1f0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
cc200 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
cc210 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 d2);. return SQ
cc220 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
cc230 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 * Close the file
cc240 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
cc250 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 nolockClose(sqli
cc260 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a te3_file *id) {.
cc270 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e return closeUn
cc280 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f ixFile(id);.}../
cc290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc2a0 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e *** End of the n
cc2b0 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d o-op lock implem
cc2c0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
cc2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
cc2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
cc330 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
cc340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
cc380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc390 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
cc3a0 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 dot-file Locking
cc3b0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
cc3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
cc3d0 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c **.** The dotfil
cc3e0 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d e locking implem
cc3f0 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 74 68 entation uses th
cc400 65 20 65 78 69 73 74 69 6e 67 20 6f 66 20 73 65 e existing of se
cc410 70 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 parate lock.** f
cc420 69 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f iles in order to
cc430 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 control access
cc440 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e to the database.
cc450 20 20 54 68 69 73 20 77 6f 72 6b 73 20 6f 6e 20 This works on
cc460 6a 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 65 76 just.** about ev
cc470 65 72 79 20 66 69 6c 65 73 79 73 74 65 6d 20 69 ery filesystem i
cc480 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 74 20 maginable. But
cc490 74 68 65 72 65 20 61 72 65 20 73 65 72 69 6f 75 there are seriou
cc4a0 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a s downsides:.**.
cc4b0 2a 2a 20 20 20 20 28 31 29 20 20 54 68 65 72 65 ** (1) There
cc4c0 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 72 72 is zero concurr
cc4d0 65 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 ency. A single
cc4e0 72 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 61 6c reader blocks al
cc4f0 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 l other.**
cc500 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 connections f
cc510 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 20 77 rom reading or w
cc520 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 riting the datab
cc530 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 ase..**.** (2
cc540 29 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f ) An applicatio
cc550 6e 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 n crash or power
cc560 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20 loss can leave
cc570 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73 stale lock files
cc580 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74 .** sitt
cc590 69 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20 ing around that
cc5a0 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 need to be clear
cc5b0 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a ed manually..**.
cc5c0 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c ** Nevertheless,
cc5d0 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61 6e a dotlock is an
cc5e0 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 appropriate loc
cc5f0 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75 73 king mode for us
cc600 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 e if no.** other
cc610 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
cc620 79 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a y is available..
cc630 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 6c 6f **.** Dotfile lo
cc640 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 20 63 cking works by c
cc650 72 65 61 74 69 6e 67 20 61 20 66 69 6c 65 20 69 reating a file i
cc660 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 n the same direc
cc670 74 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a 20 64 tory as the.** d
cc680 61 74 61 62 61 73 65 20 61 6e 64 20 77 69 74 68 atabase and with
cc690 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 the same name b
cc6a0 75 74 20 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b ut with a ".lock
cc6b0 22 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 64 65 " extension adde
cc6c0 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61 d..** The exista
cc6d0 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 66 69 nce of a lock fi
cc6e0 6c 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58 le implies an EX
cc6f0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41 CLUSIVE lock. A
cc700 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a ll other lock.**
cc710 20 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20 types (SHARED,
cc720 52 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e RESERVED, PENDIN
cc730 47 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e G) are mapped in
cc740 74 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f to EXCLUSIVE..*/
cc750 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 ../*.** The file
cc760 20 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f suffix added to
cc770 20 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66 the data base f
cc780 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72 ilename in order
cc790 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a to create the.*
cc7a0 2a 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a * lock file..*/.
cc7b0 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f #define DOTLOCK_
cc7c0 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a SUFFIX ".lock"..
cc7d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
cc7e0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 ne checks if the
cc7f0 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 re is a RESERVED
cc800 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 lock held on th
cc810 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 e specified.** f
cc820 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 ile by this or a
cc830 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ny other process
cc840 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b . If such a lock
cc850 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 is held, set *p
cc860 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e ResOut.** to a n
cc870 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 on-zero value ot
cc880 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 herwise *pResOut
cc890 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e is set to zero.
cc8a0 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
cc8b0 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 ue.** is set to
cc8c0 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 SQLITE_OK unless
cc8d0 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 an I/O error oc
cc8e0 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b curs during lock
cc8f0 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a checking..**.**
cc900 20 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b In dotfile lock
cc910 69 6e 67 2c 20 65 69 74 68 65 72 20 61 20 6c 6f ing, either a lo
cc920 63 6b 20 65 78 69 73 74 73 20 6f 72 20 69 74 20 ck exists or it
cc930 64 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 69 6e does not. So in
cc940 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 74 69 this.** variati
cc950 6f 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 65 72 on of CheckReser
cc960 76 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 65 73 vedLock(), *pRes
cc970 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 Out is set to tr
cc980 75 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a ue if any lock.*
cc990 2a 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 * is held on the
cc9a0 20 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 65 20 file and false
cc9b0 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 if the file is u
cc9c0 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 nlocked..*/.stat
cc9d0 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 ic int dotlockCh
cc9e0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
cc9f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
cca00 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 , int *pResOut)
cca10 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
cca20 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 ITE_OK;. int re
cca30 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e served = 0;. un
cca40 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
cca50 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a (unixFile*)id;..
cca60 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
cca70 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
cca80 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
cca90 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a RVEDLOCK; );. .
ccaa0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
ccab0 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 );.. /* Check i
ccac0 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 f a thread in th
ccad0 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 is process holds
ccae0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a such a lock */.
ccaf0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
ccb00 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 ktype>SHARED_LOC
ccb10 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 K ){. /* Eith
ccb20 65 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 er this connecti
ccb30 6f 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 on or some other
ccb40 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 connection in t
ccb50 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 0a he same process.
ccb60 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c ** holds a l
ccb70 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e ock on the file.
ccb80 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 No need to che
ccb90 63 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 ck further. */.
ccba0 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b reserved = 1;
ccbb0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
ccbc0 20 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c The lock is hel
ccbd0 64 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 d if and only if
ccbe0 20 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 the lockfile ex
ccbf0 69 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 ists */. cons
ccc00 74 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c t char *zLockFil
ccc10 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a e = (const char*
ccc20 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
ccc30 6f 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 73 65 ontext;. rese
ccc40 72 76 65 64 20 3d 20 61 63 63 65 73 73 28 7a 4c rved = access(zL
ccc50 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a ockFile, 0)==0;.
ccc60 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 }. OSTRACE4("
ccc70 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 TEST WR-LOCK %d
ccc80 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d %d %d\n", pFile-
ccc90 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 >h, rc, reserved
ccca0 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 );. *pResOut =
cccb0 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 reserved;. retu
cccc0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
cccd0 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 Lock the file wi
ccce0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 th the lock spec
cccf0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
ccd00 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e er locktype - on
ccd10 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c e.** of the foll
ccd20 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 owing:.**.**
ccd30 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b (1) SHARED_LOCK
ccd40 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 .** (2) RESE
ccd50 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 RVED_LOCK.**
ccd60 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 (3) PENDING_LOC
ccd70 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 K.** (4) EXC
ccd80 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a LUSIVE_LOCK.**.*
ccd90 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e * Sometimes when
ccda0 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 requesting one
ccdb0 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 lock state, addi
ccdc0 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 tional lock stat
ccdd0 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 es.** are insert
ccde0 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 ed in between.
ccdf0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 The locking migh
cce00 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 t fail on one of
cce10 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 the later.** tr
cce20 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e ansitions leavin
cce30 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 g the lock state
cce40 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
cce50 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 what it started
cce60 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f but.** still sho
cce70 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 rt of its goal.
cce80 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
cce90 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 hart shows the a
ccea0 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 llowed.** transi
cceb0 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e tions and the in
ccec0 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 serted intermedi
cced0 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a ate states:.**.*
ccee0 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e * UNLOCKED ->
ccef0 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 SHARED.** SH
ccf00 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 ARED -> RESERVED
ccf10 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
ccf20 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 (PENDING) -> EX
ccf30 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 CLUSIVE.** RE
ccf40 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 SERVED -> (PENDI
ccf50 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
ccf60 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d .** PENDING -
ccf70 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a > EXCLUSIVE.**.*
ccf80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
ccf90 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 ill only increas
ccfa0 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 e a lock. Use t
ccfb0 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f he sqlite3OsUnlo
ccfc0 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 ck().** routine
ccfd0 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 to lower a locki
ccfe0 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 ng level..**.**
ccff0 57 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f 63 With dotfile loc
cd000 6b 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 20 king, we really
cd010 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 61 only support sta
cd020 74 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 56 te (4): EXCLUSIV
cd030 45 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 61 E..** But we tra
cd040 63 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f 63 ck the other loc
cd050 6b 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 65 king levels inte
cd060 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 rnally..*/.stati
cd070 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 c int dotlockLoc
cd080 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
cd090 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
cd0a0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a ) {. unixFile *
cd0b0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
cd0c0 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b e*)id;. int fd;
cd0d0 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 . char *zLockFi
cd0e0 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 le = (char *)pFi
cd0f0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
cd100 78 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 xt;. int rc = S
cd110 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a QLITE_OK;... /*
cd120 20 49 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 If we have any
cd130 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 6c lock, then the l
cd140 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 64 79 ock file already
cd150 20 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 77 65 exists. All we
cd160 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 6f have. ** to do
cd170 20 69 73 20 61 64 6a 75 73 74 20 6f 75 72 20 69 is adjust our i
cd180 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f nternal record o
cd190 66 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 65 6c f the lock level
cd1a0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 .. */. if( pFi
cd1b0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e le->locktype > N
cd1c0 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 O_LOCK ){. pF
cd1d0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
cd1e0 6c 6f 63 6b 74 79 70 65 3b 0a 23 69 66 20 21 4f locktype;.#if !O
cd1f0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 2f 2a S_VXWORKS. /*
cd200 20 41 6c 77 61 79 73 20 75 70 64 61 74 65 20 74 Always update t
cd210 68 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e 20 he timestamp on
cd220 74 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a the old file */.
cd230 20 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b utimes(zLock
cd240 46 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e File, NULL);.#en
cd250 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 dif. return S
cd260 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
cd270 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 . /* grab an ex
cd280 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a clusive lock */.
cd290 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 fd = open(zLoc
cd2a0 6b 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f kFile,O_RDONLY|O
cd2b0 5f 43 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 _CREAT|O_EXCL,06
cd2c0 30 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 00);. if( fd<0
cd2d0 29 7b 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 ){. /* failed
cd2e0 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 to open/create
cd2f0 74 68 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e the file, someon
cd300 65 20 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 e else may have
cd310 73 74 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 stolen the lock
cd320 2a 2f 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e */. int tErrn
cd330 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 o = errno;. i
cd340 66 28 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 f( EEXIST == tEr
cd350 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 rno ){. rc
cd360 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
cd370 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
cd380 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
cd390 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
cd3a0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
cd3b0 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 IOERR_LOCK);.
cd3c0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
cd3d0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
cd3e0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
cd3f0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
cd400 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
cd410 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 return rc;. }
cd420 0a 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 . if( close(fd)
cd430 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
cd440 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
cd450 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
cd460 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 E_IOERR_CLOSE;.
cd470 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 }. . /* got i
cd480 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 t, set the type
cd490 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f and return ok */
cd4a0 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 . pFile->lockty
cd4b0 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
cd4c0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
cd4d0 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c *.** Lower the l
cd4e0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 ocking level on
cd4f0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
cd500 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 pFile to locktyp
cd510 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 e. locktype.**
cd520 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e must be either N
cd530 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 O_LOCK or SHARED
cd540 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 _LOCK..**.** If
cd550 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
cd560 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 l of the file de
cd570 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 scriptor is alre
cd580 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a ady at or below.
cd590 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 ** the requested
cd5a0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 locking level,
cd5b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
cd5c0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 a no-op..**.** W
cd5d0 68 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 hen the locking
cd5e0 6c 65 76 65 6c 20 72 65 61 63 68 65 73 20 4e 4f level reaches NO
cd5f0 5f 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 74 68 _LOCK, delete th
cd600 65 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a e lock file..*/.
cd610 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f static int dotlo
cd620 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 ckUnlock(sqlite3
cd630 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
cd640 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 ocktype) {. uni
cd650 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
cd660 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
cd670 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 char *zLockFile
cd680 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d = (char *)pFile-
cd690 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
cd6a0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
cd6b0 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 e );. OSTRACE5(
cd6c0 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 "UNLOCK %d %d w
cd6d0 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c as %d pid=%d\n",
cd6e0 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
cd6f0 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e ype,.. pFile->
cd700 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 locktype, getpid
cd710 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c ());. assert( l
cd720 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f ocktype<=SHARED_
cd730 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 LOCK );. . /*
cd740 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c no-op if possibl
cd750 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 e */. if( pFile
cd760 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b ->locktype==lock
cd770 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 type ){. retu
cd780 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
cd790 7d 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 }.. /* To downg
cd7a0 72 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20 rade to shared,
cd7b0 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75 simply update ou
cd7c0 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f r internal notio
cd7d0 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f n of the. ** lo
cd7e0 63 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65 ck state. No ne
cd7f0 65 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20 ed to mess with
cd800 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b the file on disk
cd810 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 .. */. if( loc
cd820 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
cd830 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK ){. pFile-
cd840 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 >locktype = SHAR
cd850 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 ED_LOCK;. ret
cd860 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
cd870 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 }. . /* To fu
cd880 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 lly unlock the d
cd890 61 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 atabase, delete
cd8a0 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f the lock file */
cd8b0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
cd8c0 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a ype==NO_LOCK );.
cd8d0 20 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f if( unlink(zLo
cd8e0 63 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69 ckFile) ){. i
cd8f0 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 nt rc, tErrno =
cd900 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45 errno;. if( E
cd910 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 NOENT != tErrno
cd920 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
cd930 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
cd940 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
cd950 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
cd960 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OCK);. }.
cd970 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
cd980 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 R(rc) ){. p
cd990 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
cd9a0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a = tErrno;. }.
cd9b0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a return rc; .
cd9c0 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 }. pFile->loc
cd9d0 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b ktype = NO_LOCK;
cd9e0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
cd9f0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
cda00 6f 73 65 20 61 20 66 69 6c 65 2e 20 20 4d 61 6b ose a file. Mak
cda10 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 20 e sure the lock
cda20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 has been release
cda30 64 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67 d before closing
cda40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
cda50 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c dotlockClose(sql
cda60 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
cda70 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 . int rc;. if(
cda80 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 id ){. unixF
cda90 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
cdaa0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 ixFile*)id;.
cdab0 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 dotlockUnlock(id
cdac0 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 , NO_LOCK);.
cdad0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 sqlite3_free(pFi
cdae0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
cdaf0 78 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 xt);. }. rc =
cdb00 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 closeUnixFile(id
cdb10 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
cdb20 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }./*************
cdb30 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 ***** End of the
cdb40 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20 69 dot-file lock i
cdb50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a mplementation **
cdb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdb70 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
cdb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdbc0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
cdbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdc10 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
cdc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
cdc30 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b 69 6e gin flock Lockin
cdc40 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a g **************
cdc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdc60 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 **.**.** Use the
cdc70 20 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 6d 20 flock() system
cdc80 63 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c 65 20 call to do file
cdc90 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 locking..**.** f
cdca0 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 69 lock() locking i
cdcb0 73 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c 65 20 s like dot-file
cdcc0 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 74 20 locking in that
cdcd0 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 66 the various.** f
cdce0 69 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b 69 6e ine-grain lockin
cdcf0 67 20 6c 65 76 65 6c 73 20 73 75 70 70 6f 72 74 g levels support
cdd00 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 ed by SQLite are
cdd10 20 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 6f 0a collapsed into.
cdd20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 63 6c ** a single excl
cdd30 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 6e 20 usive lock. In
cdd40 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 53 48 41 other words, SHA
cdd50 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 61 RED, RESERVED, a
cdd60 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 6c 6f nd.** PENDING lo
cdd70 63 6b 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 cks are the same
cdd80 20 74 68 69 6e 67 20 61 73 20 61 6e 20 45 58 43 thing as an EXC
cdd90 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 53 51 LUSIVE lock. SQ
cdda0 4c 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f Lite.** still wo
cddb0 72 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 rks when you do
cddc0 74 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 75 72 this, but concur
cddd0 72 65 6e 63 79 20 69 73 20 72 65 64 75 63 65 64 rency is reduced
cdde0 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 20 61 since.** only a
cddf0 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 single process
cde00 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 74 can be reading t
cde10 68 65 20 64 61 74 61 62 61 73 65 20 61 74 20 61 he database at a
cde20 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 time..**.** Omi
cde30 74 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20 69 t this section i
cde40 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
cde50 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 LOCKING_STYLE is
cde60 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 69 turned off or i
cde70 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 f.** compiling f
cde80 6f 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 or VXWORKS..*/.#
cde90 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
cdea0 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 _LOCKING_STYLE &
cdeb0 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f & !OS_VXWORKS../
cdec0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
cded0 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
cdee0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
cdef0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
cdf00 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
cdf10 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
cdf20 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
cdf30 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
cdf40 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 is held, set *pR
cdf50 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f esOut.** to a no
cdf60 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 n-zero value oth
cdf70 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 erwise *pResOut
cdf80 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
cdf90 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
cdfa0 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 e.** is set to S
cdfb0 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 QLITE_OK unless
cdfc0 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 an I/O error occ
cdfd0 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 urs during lock
cdfe0 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 checking..*/.sta
cdff0 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 tic int flockChe
ce000 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
ce010 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
ce020 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
ce030 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
ce040 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 E_OK;. int rese
ce050 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 rved = 0;. unix
ce060 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
ce070 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a nixFile*)id;. .
ce080 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
ce090 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
ce0a0 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
ce0b0 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a RVEDLOCK; );. .
ce0c0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
ce0d0 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b );. . /* Check
ce0e0 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 if a thread in
ce0f0 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c this process hol
ce100 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a ds such a lock *
ce110 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
ce120 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
ce130 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 OCK ){. reser
ce140 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a ved = 1;. }. .
ce150 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 /* Otherwise s
ce160 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ee if some other
ce170 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 process holds i
ce180 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 t. */. if( !res
ce190 65 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 erved ){. /*
ce1a0 61 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 attempt to get t
ce1b0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 he lock */. i
ce1c0 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 nt lrc = flock(p
ce1d0 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 File->h, LOCK_EX
ce1e0 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 | LOCK_NB);.
ce1f0 20 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 20 20 if( !lrc ){.
ce200 20 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 6c 6f /* got the lo
ce210 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f ck, unlock it */
ce220 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 66 6c 6f . lrc = flo
ce230 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 ck(pFile->h, LOC
ce240 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 66 20 K_UN);. if
ce250 28 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 20 20 ( lrc ) {.
ce260 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
ce270 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a rrno;. /*
ce280 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77 unlock failed w
ce290 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a ith an error */.
ce2a0 20 20 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 lrc = sq
ce2b0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
ce2c0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
ce2d0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
ce2e0 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 OCK); . i
ce2f0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
ce300 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 (lrc) ){.
ce310 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
ce320 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
ce330 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 rc = lrc
ce340 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
ce350 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b }. } else {
ce360 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e . int tErrn
ce370 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
ce380 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 reserved = 1;.
ce390 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 /* someone
ce3a0 65 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65 20 else might have
ce3b0 69 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 it reserved */.
ce3c0 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 lrc = sqlit
ce3d0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
ce3e0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
ce3f0 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b ITE_IOERR_LOCK);
ce400 20 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c . if( IS_L
ce410 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 OCK_ERROR(lrc) )
ce420 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
ce430 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
ce440 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 rno;. rc
ce450 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 = lrc;. }.
ce460 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 }. }. OSTRA
ce470 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 CE4("TEST WR-LOC
ce480 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 K %d %d %d\n", p
ce490 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 File->h, rc, res
ce4a0 65 72 76 65 64 29 3b 0a 0a 23 69 66 64 65 66 20 erved);..#ifdef
ce4b0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c SQLITE_IGNORE_FL
ce4c0 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a OCK_LOCK_ERRORS.
ce4d0 20 20 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 if( (rc & SQLI
ce4e0 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c TE_IOERR) == SQL
ce4f0 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 ITE_IOERR ){.
ce500 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
ce510 0a 20 20 20 20 72 65 73 65 72 76 65 64 3d 31 3b . reserved=1;
ce520 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
ce530 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
ce540 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a CK_LOCK_ERRORS *
ce550 2f 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 /. *pResOut = r
ce560 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 eserved;. retur
ce570 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
ce580 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 ock the file wit
ce590 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 h the lock speci
ce5a0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
ce5b0 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 r locktype - one
ce5c0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f .** of the follo
ce5d0 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 wing:.**.**
ce5e0 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a (1) SHARED_LOCK.
ce5f0 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 ** (2) RESER
ce600 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 VED_LOCK.**
ce610 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b (3) PENDING_LOCK
ce620 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c .** (4) EXCL
ce630 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a USIVE_LOCK.**.**
ce640 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 Sometimes when
ce650 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c requesting one l
ce660 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 ock state, addit
ce670 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 ional lock state
ce680 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 s.** are inserte
ce690 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 d in between. T
ce6a0 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 he locking might
ce6b0 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 fail on one of
ce6c0 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 the later.** tra
ce6d0 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 nsitions leaving
ce6e0 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 the lock state
ce6f0 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 different from w
ce700 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 hat it started b
ce710 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 ut.** still shor
ce720 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 t of its goal.
ce730 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 The following ch
ce740 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c art shows the al
ce750 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 lowed.** transit
ce760 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 ions and the ins
ce770 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 erted intermedia
ce780 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a te states:.**.**
ce790 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 UNLOCKED ->
ce7a0 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 SHARED.** SHA
ce7b0 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a RED -> RESERVED.
ce7c0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
ce7d0 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
ce7e0 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 LUSIVE.** RES
ce7f0 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e ERVED -> (PENDIN
ce800 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
ce810 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e ** PENDING ->
ce820 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a EXCLUSIVE.**.**
ce830 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 65 flock() only re
ce840 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 43 ally support EXC
ce850 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 LUSIVE locks. W
ce860 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 e track intermed
ce870 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 iate.** lock sta
ce880 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 tes in the sqlit
ce890 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 e3_file structur
ce8a0 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 e, but all locks
ce8b0 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 SHARED or.** ab
ce8c0 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45 ove are really E
ce8d0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 XCLUSIVE locks a
ce8e0 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f nd exclude all o
ce8f0 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66 ther processes f
ce900 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 rom.** access th
ce910 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
ce920 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
ce930 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 only increase a
ce940 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 lock. Use the s
ce950 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 qlite3OsUnlock()
ce960 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c .** routine to l
ce970 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c ower a locking l
ce980 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 evel..*/.static
ce990 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73 71 int flockLock(sq
ce9a0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
ce9b0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
ce9c0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
ce9d0 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 E_OK;. unixFile
ce9e0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
ce9f0 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 ile*)id;.. asse
cea00 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 rt( pFile );..
cea10 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 /* if we already
cea20 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 have a lock, it
cea30 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 is exclusive.
cea40 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 . ** Just adjus
cea50 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 t level and punt
cea60 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 on outta here.
cea70 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e */. if (pFile->
cea80 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f locktype > NO_LO
cea90 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK) {. pFile-
ceaa0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
ceab0 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e type;. return
ceac0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
cead0 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 . /* grab an
ceae0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a exclusive lock *
ceaf0 2f 0a 20 20 0a 20 20 69 66 20 28 66 6c 6f 63 6b /. . if (flock
ceb00 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f (pFile->h, LOCK_
ceb10 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b EX | LOCK_NB)) {
ceb20 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 . int tErrno
ceb30 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a 20 = errno;. /*
ceb40 64 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74 didn't get, must
ceb50 20 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20 be busy */.
ceb60 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
ceb70 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
ceb80 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
ceb90 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 ERR_LOCK);. i
ceba0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
cebb0 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 (rc) ){. pF
cebc0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
cebd0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 tErrno;. }.
cebe0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a } else {. /*
cebf0 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 got it, set the
cec00 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e type and return
cec10 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 ok */. pFile
cec20 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
cec30 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 4f 53 54 ktype;. }. OST
cec40 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 RACE4("LOCK %
cec50 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c d %s %s\n", pFil
cec60 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 e->h, locktypeNa
cec70 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 me(locktype), .
cec80 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 rc==SQ
cec90 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a LITE_OK ? "ok" :
ceca0 20 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 66 64 "failed");.#ifd
cecb0 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 ef SQLITE_IGNORE
cecc0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
cecd0 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 RS. if( (rc & S
cece0 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 QLITE_IOERR) ==
cecf0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a SQLITE_IOERR ){.
ced00 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
ced10 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 BUSY;. }.#endif
ced20 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 /* SQLITE_IGNOR
ced30 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 E_FLOCK_LOCK_ERR
ced40 4f 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 ORS */. return
ced50 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}.../*.** Lo
ced60 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
ced70 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 level on file de
ced80 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 scriptor pFile t
ced90 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 o locktype. loc
ceda0 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 ktype.** must be
cedb0 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 either NO_LOCK
cedc0 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a or SHARED_LOCK..
cedd0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 **.** If the loc
cede0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 king level of th
cedf0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
cee00 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 r is already at
cee10 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 or below.** the
cee20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e requested lockin
cee30 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f g level, this ro
cee40 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
cee50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
cee60 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 flockUnlock(sqli
cee70 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
cee80 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 t locktype) {.
cee90 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
ceea0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
ceeb0 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 . . assert( pF
ceec0 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 ile );. OSTRACE
ceed0 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 5("UNLOCK %d %d
ceee0 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e was %d pid=%d\n
ceef0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 ", pFile->h, loc
cef00 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 ktype,.
cef10 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
cef20 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 e, getpid());.
cef30 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
cef40 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
cef50 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 . . /* no-op i
cef60 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 f possible */.
cef70 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
cef80 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype==locktype ){
cef90 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
cefa0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
cefb0 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 /* shared can ju
cefc0 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 st be set becaus
cefd0 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 e we always have
cefe0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f an exclusive */
ceff0 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d . if (locktype=
cf000 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a =SHARED_LOCK) {.
cf010 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
cf020 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
cf030 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
cf040 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
cf050 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e * no, really, un
cf060 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 lock. */. int r
cf070 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d c = flock(pFile-
cf080 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 >h, LOCK_UN);.
cf090 69 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69 6e if (rc) {. in
cf0a0 74 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 t r, tErrno = er
cf0b0 72 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71 6c rno;. r = sql
cf0c0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
cf0d0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
cf0e0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
cf0f0 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f CK);. if( IS_
cf100 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29 7b LOCK_ERROR(r) ){
cf110 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
cf120 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
cf130 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 ;. }.#ifdef S
cf140 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
cf150 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 CK_LOCK_ERRORS.
cf160 20 20 20 69 66 28 20 28 72 20 26 20 53 51 4c 49 if( (r & SQLI
cf170 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c TE_IOERR) == SQL
cf180 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 ITE_IOERR ){.
cf190 20 20 20 72 20 3d 20 53 51 4c 49 54 45 5f 42 55 r = SQLITE_BU
cf1a0 53 59 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 SY;. }.#endif
cf1b0 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 /* SQLITE_IGNOR
cf1c0 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 E_FLOCK_LOCK_ERR
cf1d0 4f 52 53 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 ORS */. .
cf1e0 72 65 74 75 72 6e 20 72 3b 0a 20 20 7d 20 65 6c return r;. } el
cf1f0 73 65 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e se {. pFile->
cf200 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f locktype = NO_LO
cf210 43 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 CK;. return S
cf220 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a QLITE_OK;. }.}.
cf230 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 ./*.** Close a f
cf240 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
cf250 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 nt flockClose(sq
cf260 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 lite3_file *id)
cf270 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 {. if( id ){.
cf280 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 flockUnlock(id
cf290 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a , NO_LOCK);. }.
cf2a0 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e return closeUn
cf2b0 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23 ixFile(id);.}..#
cf2c0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
cf2d0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
cf2e0 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f TYLE && !OS_VXWO
cf2f0 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a RK */../********
cf300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
cf310 6f 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f 63 of the flock loc
cf320 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
cf330 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
cf340 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
cf350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf390 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a *****/../*******
cf3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf3e0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
cf3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf400 20 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 6d Begin Named Sem
cf410 61 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 2a aphore Locking *
cf420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf430 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 *******.**.** Na
cf440 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f med semaphore lo
cf450 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 75 cking is only su
cf460 70 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f 72 pported on VxWor
cf470 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 ks..**.** Semaph
cf480 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c ore locking is l
cf490 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e 64 ike dot-lock and
cf4a0 20 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 69 flock in that i
cf4b0 74 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a t really only.**
cf4c0 20 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 53 supports EXCLUS
cf4d0 49 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f 6e IVE locking. On
cf4e0 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 ly a single proc
cf4f0 65 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 20 ess can read or
cf500 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 74 write.** the dat
cf510 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 20 abase file at a
cf520 74 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 75 time. This redu
cf530 63 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 6f ces potential co
cf540 6e 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a 2a ncurrency, but.*
cf550 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 6b * makes the lock
cf560 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
cf570 6d 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f 0a much easier..*/.
cf580 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a #if OS_VXWORKS..
cf590 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
cf5a0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 ne checks if the
cf5b0 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 re is a RESERVED
cf5c0 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 lock held on th
cf5d0 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 e specified.** f
cf5e0 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 ile by this or a
cf5f0 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ny other process
cf600 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b . If such a lock
cf610 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 is held, set *p
cf620 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e ResOut.** to a n
cf630 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 on-zero value ot
cf640 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 herwise *pResOut
cf650 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e is set to zero.
cf660 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
cf670 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 ue.** is set to
cf680 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 SQLITE_OK unless
cf690 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 an I/O error oc
cf6a0 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b curs during lock
cf6b0 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 checking..*/.st
cf6c0 61 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65 63 atic int semChec
cf6d0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
cf6e0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
cf6f0 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a int *pResOut) {.
cf700 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
cf710 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 E_OK;. int rese
cf720 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 rved = 0;. unix
cf730 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
cf740 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 nixFile*)id;..
cf750 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
cf760 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
cf770 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
cf780 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 EDLOCK; );. .
cf790 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
cf7a0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 .. /* Check if
cf7b0 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 a thread in this
cf7c0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 process holds s
cf7d0 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 uch a lock */.
cf7e0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
cf7f0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
cf800 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 ){. reserved
cf810 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a = 1;. }. . /*
cf820 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 Otherwise see i
cf830 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f f some other pro
cf840 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a cess holds it. *
cf850 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 /. if( !reserve
cf860 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20 2a d ){. sem_t *
cf870 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f pSem = pFile->pO
cf880 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 20 20 73 pen->pSem;. s
cf890 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74 42 truct stat statB
cf8a0 75 66 3b 0a 0a 20 20 20 20 69 66 28 20 73 65 6d uf;.. if( sem
cf8b0 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d _trywait(pSem)==
cf8c0 2d 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 -1 ){. int
cf8d0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
cf8e0 20 20 20 20 20 20 69 66 28 20 45 41 47 41 49 4e if( EAGAIN
cf8f0 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 != tErrno ){.
cf900 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
cf910 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
cf920 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
cf930 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
cf940 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 ESERVEDLOCK);.
cf950 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
cf960 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
cf970 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a . } else {.
cf980 20 20 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f /* someo
cf990 6e 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20 ne else has the
cf9a0 6c 6f 63 6b 20 77 68 65 6e 20 77 65 20 61 72 65 lock when we are
cf9b0 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 in NO_LOCK */.
cf9c0 20 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 reserved
cf9d0 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 = (pFile->lockty
cf9e0 70 65 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43 4b pe < SHARED_LOCK
cf9f0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
cfa00 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 77 else{. /* w
cfa10 65 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74 20 e could have it
cfa20 69 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a 2f if we want it */
cfa30 0a 20 20 20 20 20 20 73 65 6d 5f 70 6f 73 74 28 . sem_post(
cfa40 70 53 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d pSem);. }. }
cfa50 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 . OSTRACE4("TES
cfa60 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
cfa70 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
cfa80 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a rc, reserved);.
cfa90 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 . *pResOut = re
cfaa0 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e served;. return
cfab0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
cfac0 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 ck the file with
cfad0 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 the lock specif
cfae0 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
cfaf0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a locktype - one.
cfb00 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
cfb10 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 ing:.**.** (
cfb20 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 1) SHARED_LOCK.*
cfb30 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 * (2) RESERV
cfb40 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
cfb50 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 3) PENDING_LOCK.
cfb60 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 ** (4) EXCLU
cfb70 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 SIVE_LOCK.**.**
cfb80 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 Sometimes when r
cfb90 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f equesting one lo
cfba0 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 ck state, additi
cfbb0 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 onal lock states
cfbc0 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
cfbd0 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 in between. Th
cfbe0 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 e locking might
cfbf0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 fail on one of t
cfc00 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e he later.** tran
cfc10 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 sitions leaving
cfc20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 the lock state d
cfc30 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 ifferent from wh
cfc40 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 at it started bu
cfc50 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 t.** still short
cfc60 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 of its goal. T
cfc70 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
cfc80 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c rt shows the all
cfc90 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 owed.** transiti
cfca0 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 ons and the inse
cfcb0 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 rted intermediat
cfcc0 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 e states:.**.**
cfcd0 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 UNLOCKED -> S
cfce0 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 HARED.** SHAR
cfcf0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a ED -> RESERVED.*
cfd00 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 * SHARED -> (
cfd10 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
cfd20 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 USIVE.** RESE
cfd30 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 RVED -> (PENDING
cfd40 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
cfd50 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 * PENDING ->
cfd60 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 EXCLUSIVE.**.**
cfd70 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 Semaphore locks
cfd80 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70 only really supp
cfd90 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f ort EXCLUSIVE lo
cfda0 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69 cks. We track i
cfdb0 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c ntermediate.** l
cfdc0 6f 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68 ock states in th
cfdd0 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 e sqlite3_file s
cfde0 74 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c tructure, but al
cfdf0 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f l locks SHARED o
cfe00 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72 r.** above are r
cfe10 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20 eally EXCLUSIVE
cfe20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 locks and exclud
cfe30 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 e all other proc
cfe40 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 esses from.** ac
cfe50 63 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a cess the file..*
cfe60 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
cfe70 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
cfe80 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 ease a lock. Us
cfe90 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 e the sqlite3OsU
cfea0 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 nlock().** routi
cfeb0 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f ne to lower a lo
cfec0 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a cking level..*/.
cfed0 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c 6f static int semLo
cfee0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
cfef0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
cff00 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 e) {. unixFile
cff10 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
cff20 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 le*)id;. int fd
cff30 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 ;. sem_t *pSem
cff40 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e = pFile->pOpen->
cff50 70 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d pSem;. int rc =
cff60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f SQLITE_OK;.. /
cff70 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 * if we already
cff80 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 have a lock, it
cff90 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a is exclusive. .
cffa0 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 ** Just adjust
cffb0 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 level and punt
cffc0 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a on outta here. *
cffd0 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c /. if (pFile->l
cffe0 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 ocktype > NO_LOC
cfff0 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K) {. pFile->
d0000 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
d0010 79 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 ype;. rc = SQ
d0020 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74 LITE_OK;. got
d0030 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a o sem_end_lock;.
d0040 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 6b }. . /* lock
d0050 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62 semaphore now b
d0060 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 6e ut bail out when
d0070 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e already locked.
d0080 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 72 */. if( sem_tr
d0090 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 ywait(pSem)==-1
d00a0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
d00b0 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 TE_BUSY;. got
d00c0 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a o sem_end_lock;.
d00d0 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74 }.. /* got it
d00e0 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 , set the type a
d00f0 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a nd return ok */.
d0100 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d0110 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 e = locktype;..
d0120 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 sem_end_lock:.
d0130 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
d0140 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
d0150 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
d0160 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
d0170 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
d0180 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
d0190 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
d01a0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
d01b0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
d01c0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
d01d0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
d01e0 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
d01f0 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
d0200 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
d0210 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
d0220 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
d0230 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
d0240 63 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b 28 c int semUnlock(
d0250 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d0260 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 , int locktype)
d0270 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
d0280 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d0290 29 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 )id;. sem_t *pS
d02a0 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 em = pFile->pOpe
d02b0 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 65 n->pSem;.. asse
d02c0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 rt( pFile );. a
d02d0 73 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a 20 ssert( pSem );.
d02e0 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 OSTRACE5("UNLOC
d02f0 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 K %d %d was %d
d0300 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 pid=%d\n", pFile
d0310 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 ->h, locktype,..
d0320 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d0330 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 pe, getpid());.
d0340 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d0350 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
d0360 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 ;. . /* no-op
d0370 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 if possible */.
d0380 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d0390 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 type==locktype )
d03a0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
d03b0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 ITE_OK;. }. .
d03c0 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a /* shared can j
d03d0 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 ust be set becau
d03e0 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 se we always hav
d03f0 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a e an exclusive *
d0400 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 /. if (locktype
d0410 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b ==SHARED_LOCK) {
d0420 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d0430 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d0440 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d0450 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d0460 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e /* no, really un
d0470 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20 lock. */. if (
d0480 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d sem_post(pSem)==
d0490 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 -1 ) {. int r
d04a0 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e c, tErrno = errn
d04b0 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 o;. rc = sqli
d04c0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d04d0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d04e0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
d04f0 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c K);. if( IS_L
d0500 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
d0510 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
d0520 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d0530 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
d0540 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 rn rc; . }. pF
d0550 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
d0560 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 NO_LOCK;. retur
d0570 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
d0580 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 /*. ** Close a f
d0590 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 ile.. */.static
d05a0 69 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c int semClose(sql
d05b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
d05c0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 . if( id ){.
d05d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d05e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d05f0 3b 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 ;. semUnlock(
d0600 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 id, NO_LOCK);.
d0610 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d0620 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 );. unixEnter
d0630 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c Mutex();. rel
d0640 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 easeLockInfo(pFi
d0650 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 le->pLock);.
d0660 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 releaseOpenCnt(p
d0670 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 File->pOpen);.
d0680 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 closeUnixFile(
d0690 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 id);. unixLea
d06a0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 veMutex();. }.
d06b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d06c0 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 K;.}..#endif /*
d06d0 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a OS_VXWORKS */./*
d06e0 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 .** Named semaph
d06f0 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f ore locking is o
d0700 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e nly available on
d0710 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a VxWorks..**.***
d0720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
d0730 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 65 of the named se
d0740 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d 70 maphore lock imp
d0750 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a lementation ****
d0760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
d0770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d07a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d07b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f ***********/.../
d07c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d07d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d07e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d07f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
d0810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
d0830 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a AFP Locking ***
d0840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
d0860 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 20 *.** AFP is the
d0870 41 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 6f Apple Filing Pro
d0880 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 61 tocol. AFP is a
d0890 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73 network filesys
d08a0 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 tem found.** on
d08b0 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20 Apple Macintosh
d08c0 63 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 68 computers - both
d08d0 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a 2a OS9 and OSX..**
d08e0 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20 .** Third-party
d08f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
d0900 6f 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c of AFP are avail
d0910 61 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20 able. But this
d0920 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e 6c code here.** onl
d0930 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0a y works on OSX..
d0940 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 */..#if defined(
d0950 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 __APPLE__) && SQ
d0960 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
d0970 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 ING_STYLE./*.**
d0980 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f The afpLockingCo
d0990 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20 ntext structure
d09a0 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 70 contains all afp
d09b0 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 73 lock specific s
d09c0 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 tate.*/.typedef
d09d0 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e struct afpLockin
d09e0 67 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 6b gContext afpLock
d09f0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 ingContext;.stru
d0a00 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e ct afpLockingCon
d0a10 74 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e 65 text {. unsigne
d0a20 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72 d long long shar
d0a30 65 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20 edByte;. const
d0a40 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 char *dbPath;
d0a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
d0a60 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 e of the open fi
d0a70 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 le */.};..struct
d0a80 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 ByteRangeLockPB
d0a90 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 2.{. unsigned l
d0aa0 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b ong long offset;
d0ab0 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 /* offse
d0ac0 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 t to first byte
d0ad0 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 to lock */. uns
d0ae0 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
d0af0 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f length; /
d0b00 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 * nbr of bytes t
d0b10 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 o lock */. unsi
d0b20 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 gned long long r
d0b30 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a etRangeStart; /*
d0b40 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65 nbr of 1st byte
d0b50 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65 locked if succe
d0b60 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 ssful */. unsig
d0b70 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46 ned char unLockF
d0b80 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 lag; /*
d0b90 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20 1 = unlock, 0 =
d0ba0 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e lock */. unsign
d0bb0 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64 ed char startEnd
d0bc0 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31 Flag; /* 1
d0bd0 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66 =rel to end of f
d0be0 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74 ork, 0=rel to st
d0bf0 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b art */. int fd;
d0c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d0c10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 /* fi
d0c20 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63 le desc to assoc
d0c30 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20 this lock with
d0c40 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61 */.};..#define a
d0c50 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 fpfsByteRangeLoc
d0c60 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f k2FSCTL _
d0c70 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 IOWR('z', 23, st
d0c80 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f ruct ByteRangeLo
d0c90 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 ckPB2)../*.** Th
d0ca0 69 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 is is a utility
d0cb0 66 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63 for setting or c
d0cc0 6c 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61 learing a bit-ra
d0cd0 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a nge lock on an.*
d0ce0 2a 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d * AFP filesystem
d0cf0 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 ..** .** Return
d0d00 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
d0d10 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53 cess, SQLITE_BUS
d0d20 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f Y on failure..*/
d0d30 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53 .static int afpS
d0d40 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20 etLock(. const
d0d50 63 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 char *path,
d0d60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
d0d70 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 of the file to
d0d80 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c be locked or unl
d0d90 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46 ocked */. unixF
d0da0 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 ile *pFile,
d0db0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
d0dc0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
d0dd0 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75 r on path */. u
d0de0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
d0df0 67 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a g offset, /*
d0e00 20 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62 First byte to b
d0e10 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e e locked */. un
d0e20 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
d0e30 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20 length, /*
d0e40 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
d0e50 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 to lock */. int
d0e60 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20 setLockFlag
d0e70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
d0e80 72 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e rue to set lock.
d0e90 20 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72 False to clear
d0ea0 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74 lock */.){. st
d0eb0 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f ruct ByteRangeLo
d0ec0 63 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20 ckPB2 pb;. int
d0ed0 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c err;. . pb.unL
d0ee0 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63 ockFlag = setLoc
d0ef0 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20 kFlag ? 0 : 1;.
d0f00 20 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67 pb.startEndFlag
d0f10 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65 = 0;. pb.offse
d0f20 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62 t = offset;. pb
d0f30 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68 .length = length
d0f40 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69 ; . pb.fd = pFi
d0f50 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52 le->h;. . OSTR
d0f60 41 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 4b ACE6("AFPSETLOCK
d0f70 20 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69 [%s] for %d%s i
d0f80 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c n range %llx:%ll
d0f90 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c x\n", . (setL
d0fa0 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 ockFlag?"ON":"OF
d0fb0 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28 F"), pFile->h, (
d0fc0 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74 pb.fd==-1?"[test
d0fd0 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20 val-1]":""),.
d0fe0 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 offset, length)
d0ff0 3b 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28 ;. err = fsctl(
d1000 70 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 52 path, afpfsByteR
d1010 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 angeLock2FSCTL,
d1020 26 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 &pb, 0);. if (
d1030 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 err==-1 ) {.
d1040 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 20 int rc;. int
d1050 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d1060 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 46 OSTRACE4("AF
d1070 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 20 PSETLOCK failed
d1080 74 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 20 to fsctl() '%s'
d1090 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 %d %s\n",.
d10a0 20 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 72 path, tEr
d10b0 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 45 rno, strerror(tE
d10c0 72 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 53 rrno));.#ifdef S
d10d0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 QLITE_IGNORE_AFP
d10e0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 _LOCK_ERRORS.
d10f0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
d1100 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20 Y;.#else. rc
d1110 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d1120 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d1130 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 no,.
d1140 20 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b 46 setLockF
d1150 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45 lag ? SQLITE_IOE
d1160 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45 RR_LOCK : SQLITE
d1170 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a _IOERR_UNLOCK);.
d1180 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
d1190 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b _IGNORE_AFP_LOCK
d11a0 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 69 _ERRORS */. i
d11b0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d11c0 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 (rc) ){. pF
d11d0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d11e0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 tErrno;. }.
d11f0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
d1200 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 } else {. ret
d1210 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
d1220 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
d1230 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
d1240 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 if there is a RE
d1250 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 SERVED lock held
d1260 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 on the specifie
d1270 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 d.** file by thi
d1280 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
d1290 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 rocess. If such
d12a0 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 a lock is held,
d12b0 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 set *pResOut.**
d12c0 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 to a non-zero va
d12d0 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 lue otherwise *p
d12e0 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f ResOut is set to
d12f0 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 zero. The retu
d1300 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 rn value.** is s
d1310 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 et to SQLITE_OK
d1320 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 unless an I/O er
d1330 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
d1340 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e g lock checking.
d1350 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
d1360 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c fpCheckReservedL
d1370 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d1380 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f *id, int *pResO
d1390 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ut){. int rc =
d13a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
d13b0 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 reserved = 0;.
d13c0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d13d0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d13e0 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 ;. . SimulateI
d13f0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
d1400 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
d1410 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 KRESERVEDLOCK; )
d1420 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
d1430 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63 File );. afpLoc
d1440 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e kingContext *con
d1450 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 text = (afpLocki
d1460 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 ngContext *) pFi
d1470 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
d1480 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 xt;. . /* Chec
d1490 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e k if a thread in
d14a0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f this process ho
d14b0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 lds such a lock
d14c0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
d14d0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
d14e0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 LOCK ){. rese
d14f0 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 rved = 1;. }.
d1500 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 . /* Otherwise
d1510 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 see if some othe
d1520 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 r process holds
d1530 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 it.. */. if(
d1540 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 !reserved ){.
d1550 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53 /* lock the RES
d1560 45 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20 ERVED byte */.
d1570 20 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 53 int lrc = afpS
d1580 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
d1590 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 dbPath, pFile, R
d15a0 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c ESERVED_BYTE, 1,
d15b0 31 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 51 1); . if( SQ
d15c0 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a LITE_OK==lrc ){.
d15d0 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73 /* if we s
d15e0 75 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69 ucceeded in taki
d15f0 6e 67 20 74 68 65 20 72 65 73 65 72 76 65 64 20 ng the reserved
d1600 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 lock, unlock it
d1610 74 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20 to restore.
d1620 20 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c ** the original
d1630 20 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20 state */.
d1640 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b lrc = afpSetLock
d1650 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
d1660 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 , pFile, RESERVE
d1670 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 D_BYTE, 1, 0);.
d1680 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
d1690 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65 /* if we faile
d16a0 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 d to get the loc
d16b0 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65 k then someone e
d16c0 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74 lse must have it
d16d0 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 76 */. reserv
d16e0 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ed = 1;. }.
d16f0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d1700 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 ROR(lrc) ){.
d1710 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d 0a rc=lrc;. }.
d1720 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 }. . OSTRACE
d1730 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 4("TEST WR-LOCK
d1740 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 %d %d %d\n", pFi
d1750 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 le->h, rc, reser
d1760 76 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65 73 ved);. . *pRes
d1770 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a Out = reserved;.
d1780 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
d1790 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 /*.** Lock the f
d17a0 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 ile with the loc
d17b0 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 k specified by p
d17c0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 arameter locktyp
d17d0 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 e - one.** of th
d17e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
d17f0 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 ** (1) SHARE
d1800 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 D_LOCK.** (2
d1810 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a ) RESERVED_LOCK.
d1820 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 ** (3) PENDI
d1830 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 NG_LOCK.** (
d1840 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4) EXCLUSIVE_LOC
d1850 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 K.**.** Sometime
d1860 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e s when requestin
d1870 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 g one lock state
d1880 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 , additional loc
d1890 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 k states.** are
d18a0 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 inserted in betw
d18b0 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e een. The lockin
d18c0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 g might fail on
d18d0 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 one of the later
d18e0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
d18f0 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b leaving the lock
d1900 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 state different
d1910 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 from what it st
d1920 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 arted but.** sti
d1930 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 ll short of its
d1940 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f goal. The follo
d1950 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 wing chart shows
d1960 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 the allowed.**
d1970 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 transitions and
d1980 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 the inserted int
d1990 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 ermediate states
d19a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 :.**.** UNLOC
d19b0 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a KED -> SHARED.**
d19c0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 SHARED -> RE
d19d0 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 SERVED.** SHA
d19e0 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 RED -> (PENDING)
d19f0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
d1a00 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 RESERVED ->
d1a10 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
d1a20 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e LUSIVE.** PEN
d1a30 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 DING -> EXCLUSIV
d1a40 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 E.**.** This rou
d1a50 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 tine will only i
d1a60 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 ncrease a lock.
d1a70 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 Use the sqlite3
d1a80 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f OsUnlock().** ro
d1a90 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 utine to lower a
d1aa0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a locking level..
d1ab0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 */.static int af
d1ac0 70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 pLock(sqlite3_fi
d1ad0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
d1ae0 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 type){. int rc
d1af0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 = SQLITE_OK;. u
d1b00 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d1b10 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d1b20 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 afpLockingCont
d1b30 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 ext *context = (
d1b40 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
d1b50 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b t *) pFile->lock
d1b60 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 ingContext;. .
d1b70 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d1b80 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f ;. OSTRACE5("LO
d1b90 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 CK %d %s was
d1ba0 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 %s pid=%d\n", pF
d1bb0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
d1bc0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f locktypeName(lo
d1bd0 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 cktype), locktyp
d1be0 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 eName(pFile->loc
d1bf0 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64 28 29 ktype), getpid()
d1c00 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 );.. /* If ther
d1c10 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c e is already a l
d1c20 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 ock of this type
d1c30 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 or more restric
d1c40 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a tive on the. **
d1c50 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f unixFile, do no
d1c60 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 thing. Don't use
d1c70 20 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 the afp_end_loc
d1c80 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 k: exit path, as
d1c90 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d . ** unixEnterM
d1ca0 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 utex() hasn't be
d1cb0 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 en called yet..
d1cc0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
d1cd0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 >locktype>=lockt
d1ce0 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 ype ){. OSTRA
d1cf0 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 CE3("LOCK %d
d1d00 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 %s ok (already h
d1d10 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e eld)\n", pFile->
d1d20 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f h,. lo
d1d30 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 cktypeName(lockt
d1d40 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 72 ype));. retur
d1d50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d1d60 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
d1d70 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 the locking seq
d1d80 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 uence is correct
d1d90 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
d1da0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 pFile->locktype!
d1db0 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b =NO_LOCK || lock
d1dc0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d1dd0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c K );. assert( l
d1de0 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 ocktype!=PENDING
d1df0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
d1e00 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 t( locktype!=RES
d1e10 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 ERVED_LOCK || pF
d1e20 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
d1e30 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
d1e40 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 . /* This mutex
d1e50 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 is needed becau
d1e60 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 se pFile->pLock
d1e70 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 is shared across
d1e80 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 threads. */.
d1e90 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
d1ea0 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 ;.. /* Make sur
d1eb0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 e the current th
d1ec0 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 read owns the pF
d1ed0 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d ile.. */. rc =
d1ee0 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 transferOwnersh
d1ef0 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 ip(pFile);. if(
d1f00 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
d1f10 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d {. unixLeaveM
d1f20 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 utex();. retu
d1f30 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a rn rc;. }. .
d1f40 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c /* A PENDING l
d1f50 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 ock is needed be
d1f60 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 fore acquiring a
d1f70 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 SHARED lock and
d1f80 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 before. ** acq
d1f90 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 uiring an EXCLUS
d1fa0 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 IVE lock. For t
d1fb0 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 he SHARED lock,
d1fc0 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c the PENDING will
d1fd0 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 . ** be release
d1fe0 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f d.. */. if( lo
d1ff0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
d2000 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c OCK . || (l
d2010 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
d2020 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 VE_LOCK && pFile
d2030 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 ->locktype<PENDI
d2040 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 NG_LOCK). ){.
d2050 20 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20 int failed;.
d2060 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 failed = afpSe
d2070 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
d2080 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 bPath, pFile, PE
d2090 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 31 NDING_BYTE, 1, 1
d20a0 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c 65 );. if (faile
d20b0 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 d) {. rc =
d20c0 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 67 6f failed;. go
d20d0 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b to afp_end_lock;
d20e0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 . }. }. .
d20f0 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 /* If control ge
d2100 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 ts to this point
d2110 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 , then actually
d2120 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b go ahead and mak
d2130 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 e. ** operating
d2140 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f system calls fo
d2150 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 r the specified
d2160 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 lock.. */. if(
d2170 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
d2180 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e D_LOCK ){. in
d2190 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 63 32 t lk, lrc1, lrc2
d21a0 2c 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 , lrc1Errno;.
d21b0 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 . /* Now get
d21c0 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 the read-lock S
d21d0 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 HARED_LOCK */.
d21e0 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 /* note that t
d21f0 68 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68 he quality of th
d2200 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 e randomness doe
d2210 73 6e 27 74 20 6d 61 74 74 65 72 20 74 68 61 74 sn't matter that
d2220 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 much */. lk
d2230 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 = random(); .
d2240 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 context->shared
d2250 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 Byte = (lk & 0x7
d2260 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44 fffffff)%(SHARED
d2270 5f 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 _SIZE - 1);.
d2280 6c 72 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc1 = afpSetLoc
d2290 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
d22a0 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 h, pFile, .
d22b0 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 SHARED_FIRS
d22c0 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 T+context->share
d22d0 64 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 dByte, 1, 1);.
d22e0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d22f0 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 ROR(lrc1) ){.
d2300 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 lrc1Errno = p
d2310 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b File->lastErrno;
d2320 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 . }. /* Dr
d2330 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 op the temporary
d2340 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f PENDING lock */
d2350 0a 20 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53 . lrc2 = afpS
d2360 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
d2370 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 dbPath, pFile, P
d2380 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 ENDING_BYTE, 1,
d2390 30 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 0);. . if(
d23a0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
d23b0 72 63 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70 rc1) ) {. p
d23c0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d23d0 3d 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 = lrc1Errno;.
d23e0 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 rc = lrc1;.
d23f0 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 goto afp_end
d2400 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 _lock;. } els
d2410 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 e if( IS_LOCK_ER
d2420 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 ROR(lrc2) ){.
d2430 20 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 rc = lrc2;.
d2440 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 goto afp_end
d2450 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 _lock;. } els
d2460 65 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51 e if( lrc1 != SQ
d2470 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 LITE_OK ) {.
d2480 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 rc = lrc1;.
d2490 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
d24a0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d24b0 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 = SHARED_LOCK;.
d24c0 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 pFile->pOpe
d24d0 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 n->nLock++;.
d24e0 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f }. }else{. /
d24f0 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 * The request wa
d2500 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 s for a RESERVED
d2510 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f or EXCLUSIVE lo
d2520 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a ck. It is. *
d2530 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 * assumed that t
d2540 68 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 here is a SHARED
d2550 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b or greater lock
d2560 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 on the file.
d2570 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 ** already..
d2580 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c */. int fail
d2590 65 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 ed = 0;. asse
d25a0 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f rt( 0!=pFile->lo
d25b0 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 cktype );. if
d25c0 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 (locktype >= RE
d25d0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 SERVED_LOCK && p
d25e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c File->locktype <
d25f0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 RESERVED_LOCK)
d2600 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 {. /* Acq
d2610 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 uire a RESERVED
d2620 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 lock */.
d2630 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c failed = afpSetL
d2640 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
d2650 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 ath, pFile, RESE
d2660 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b RVED_BYTE, 1,1);
d2670 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21 . }. if (!
d2680 66 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 failed && lockty
d2690 70 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f pe == EXCLUSIVE_
d26a0 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a LOCK) {. /*
d26b0 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c Acquire an EXCL
d26c0 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 USIVE lock */.
d26d0 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 . /*
d26e0 52 65 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65 Remove the share
d26f0 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 d lock before tr
d2700 79 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20 ying the range.
d2710 20 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a we'll need to .
d2720 20 20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62 ** reestab
d2730 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 lish the shared
d2740 6c 6f 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74 lock if we can't
d2750 20 67 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c get the afpUnl
d2760 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ock. */.
d2770 20 20 20 69 66 28 20 21 28 66 61 69 6c 65 64 20 if( !(failed
d2780 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e = afpSetLock(con
d2790 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 text->dbPath, pF
d27a0 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ile, SHARED_FIRS
d27b0 54 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 T +.
d27c0 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e con
d27d0 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 text->sharedByte
d27e0 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 , 1, 0)) ){.
d27f0 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20 int failed2
d2800 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
d2810 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 /* now atte
d2820 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 mmpt to get the
d2830 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 exclusive lock r
d2840 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 ange */.
d2850 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c failed = afpSetL
d2860 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
d2870 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 ath, pFile, SHAR
d2880 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20 ED_FIRST, .
d2890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d28a0 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 SHARED
d28b0 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 _SIZE, 1);.
d28c0 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 26 26 if( failed &&
d28d0 20 28 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53 (failed2 = afpS
d28e0 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
d28f0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a dbPath, pFile, .
d2900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d2910 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 SHARED_FI
d2920 52 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 RST + context->s
d2930 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 haredByte, 1, 1)
d2940 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f ) ){. /
d2950 2a 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c * Can't reestabl
d2960 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c ish the shared l
d2970 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e ock. Sqlite can
d2980 27 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 't deal, this is
d2990 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 . ** a
d29a0 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 critical I/O err
d29b0 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a or. */.
d29c0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 28 rc = (
d29d0 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45 (failed & SQLITE
d29e0 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 _IOERR) == SQLIT
d29f0 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 E_IOERR) ? faile
d2a00 64 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20 d2 : .
d2a10 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 SQLITE_IOER
d2a20 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 R_LOCK;.
d2a30 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c goto afp_end_l
d2a40 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a ock;. } .
d2a50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
d2a60 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 rc = failed
d2a70 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d ; . }. }
d2a80 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 . if( failed
d2a90 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61 ){. rc = fa
d2aa0 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a iled;. }. }.
d2ab0 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
d2ac0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 ITE_OK ){. pF
d2ad0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
d2ae0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 locktype;. }els
d2af0 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d e if( locktype==
d2b00 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
d2b10 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
d2b20 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f ktype = PENDING_
d2b30 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 LOCK;. }. .afp
d2b40 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 _end_lock:. uni
d2b50 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
d2b60 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 OSTRACE4("LOCK
d2b70 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 %d %s %s\n",
d2b80 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
d2b90 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 peName(locktype)
d2ba0 2c 20 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d , . rc==
d2bb0 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 SQLITE_OK ? "ok"
d2bc0 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 : "failed");.
d2bd0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
d2be0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
d2bf0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
d2c00 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
d2c10 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
d2c20 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
d2c30 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
d2c40 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
d2c50 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
d2c60 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
d2c70 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
d2c80 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
d2c90 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
d2ca0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
d2cb0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
d2cc0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
d2cd0 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
d2ce0 63 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 c int afpUnlock(
d2cf0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d2d00 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 , int locktype)
d2d10 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
d2d20 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 ITE_OK;. unixFi
d2d30 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d2d40 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 xFile*)id;. afp
d2d50 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
d2d60 70 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 pCtx = (afpLocki
d2d70 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 ngContext *) pFi
d2d80 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
d2d90 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 xt;.. assert( p
d2da0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
d2db0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 E5("UNLOCK %d %
d2dc0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c d was %d pid=%d\
d2dd0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
d2de0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 cktype,.
d2df0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d2e00 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 , getpid());..
d2e10 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
d2e20 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
d2e30 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d2e40 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 cktype<=locktype
d2e50 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
d2e60 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
d2e70 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 if( CHECK_THREAD
d2e80 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 ID(pFile) ){.
d2e90 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d return SQLITE_M
d2ea0 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 ISUSE;. }. uni
d2eb0 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
d2ec0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d2ed0 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
d2ee0 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 ){. . if(
d2ef0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d2f00 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
d2f10 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
d2f20 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e fpSetLock(pCtx->
d2f30 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 dbPath, pFile, S
d2f40 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41 HARED_FIRST, SHA
d2f50 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 RED_SIZE, 0);.
d2f60 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
d2f70 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 TE_OK && locktyp
d2f80 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
d2f90 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c {. /* onl
d2fa0 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 y re-establish t
d2fb0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 he shared lock i
d2fc0 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 f necessary */.
d2fd0 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65 int share
d2fe0 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 dLockByte = SHAR
d2ff0 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 ED_FIRST+pCtx->s
d3000 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20 haredByte;.
d3010 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f rc = afpSetLo
d3020 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c ck(pCtx->dbPath,
d3030 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f pFile, sharedLo
d3040 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 ckByte, 1, 1);.
d3050 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
d3060 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
d3070 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 OK && pFile->loc
d3080 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c ktype>=PENDING_L
d3090 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 OCK ){. rc
d30a0 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 = afpSetLock(pCt
d30b0 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 x->dbPath, pFile
d30c0 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 , PENDING_BYTE,
d30d0 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 1, 0);. } .
d30e0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
d30f0 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f _OK && pFile->lo
d3100 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 cktype>=RESERVED
d3110 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 _LOCK ){. r
d3120 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 c = afpSetLock(p
d3130 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 Ctx->dbPath, pFi
d3140 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 le, RESERVED_BYT
d3150 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a E, 1, 0);. }.
d3160 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b }else if( lock
d3170 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b type==NO_LOCK ){
d3180 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 . /* clear th
d3190 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f e shared lock */
d31a0 0a 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c . int sharedL
d31b0 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 ockByte = SHARED
d31c0 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 _FIRST+pCtx->sha
d31d0 72 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20 redByte;. rc
d31e0 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 = afpSetLock(pCt
d31f0 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 x->dbPath, pFile
d3200 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 , sharedLockByte
d3210 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 , 1, 0);. }..
d3220 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
d3230 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 K ){. if( loc
d3240 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 ktype==NO_LOCK )
d3250 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75 {. struct u
d3260 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 nixOpenCnt *pOpe
d3270 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e n = pFile->pOpen
d3280 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e ;. pOpen->n
d3290 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 Lock--;. as
d32a0 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f sert( pOpen->nLo
d32b0 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 ck>=0 );. i
d32c0 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d f( pOpen->nLock=
d32d0 3d 30 20 26 26 20 70 4f 70 65 6e 2d 3e 6e 50 65 =0 && pOpen->nPe
d32e0 6e 64 69 6e 67 3e 30 20 29 7b 0a 20 20 20 20 20 nding>0 ){.
d32f0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
d3300 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70 for(i=0; i<pOp
d3310 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3b 20 69 2b en->nPending; i+
d3320 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
d3330 28 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e ( pOpen->aPendin
d3340 67 5b 69 5d 20 3c 20 30 20 29 20 63 6f 6e 74 69 g[i] < 0 ) conti
d3350 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 69 nue;. i
d3360 66 28 20 63 6c 6f 73 65 28 70 4f 70 65 6e 2d 3e f( close(pOpen->
d3370 61 50 65 6e 64 69 6e 67 5b 69 5d 29 20 29 7b 0a aPending[i]) ){.
d3380 20 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c pFil
d3390 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
d33a0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 rrno;.
d33b0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
d33c0 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 ERR_CLOSE;.
d33d0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
d33e0 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 pOpen->a
d33f0 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d 31 3b Pending[i] = -1;
d3400 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
d3410 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
d3420 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
d3430 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
d3440 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e lite3_free(pOpen
d3450 2d 3e 61 50 65 6e 64 69 6e 67 29 3b 0a 20 20 20 ->aPending);.
d3460 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 pOpen->nP
d3470 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 ending = 0;.
d3480 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 pOpen->aPe
d3490 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 nding = 0;.
d34a0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
d34b0 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 }. }. unixLea
d34c0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 veMutex();. if(
d34d0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
d34e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d34f0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 = locktype;. r
d3500 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d3510 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 ** Close a file
d3520 26 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 & cleanup AFP sp
d3530 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 ecific locking c
d3540 6f 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 ontext .*/.stati
d3550 63 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 c int afpClose(s
d3560 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
d3570 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 {. if( id ){.
d3580 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 unixFile *pFi
d3590 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d35a0 69 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 id;. afpUnloc
d35b0 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a k(id, NO_LOCK);.
d35c0 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 unixEnterMut
d35d0 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 ex();. if( pF
d35e0 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 ile->pOpen && pF
d35f0 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 ile->pOpen->nLoc
d3600 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 k ){. /* If
d3610 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 there are outst
d3620 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f anding locks, do
d3630 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c not actually cl
d3640 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 ose the file jus
d3650 74 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 t. ** yet b
d3660 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c ecause that woul
d3670 64 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f d clear those lo
d3680 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 cks. Instead, a
d3690 64 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 dd the file.
d36a0 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 ** descriptor
d36b0 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 to pOpen->aPendi
d36c0 6e 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 ng. It will be
d36d0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c automatically cl
d36e0 6f 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 osed when.
d36f0 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b ** the last lock
d3700 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 is cleared..
d3710 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 */. int
d3720 2a 61 4e 65 77 3b 0a 20 20 20 20 20 20 73 74 72 *aNew;. str
d3730 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 uct unixOpenCnt
d3740 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e *pOpen = pFile->
d3750 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 61 4e 65 pOpen;. aNe
d3760 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c w = sqlite3_real
d3770 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 loc(pOpen->aPend
d3780 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e 6e 50 65 ing, (pOpen->nPe
d3790 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65 6f 66 28 nding+1)*sizeof(
d37a0 69 6e 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 int) );. if
d37b0 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 ( aNew==0 ){.
d37c0 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c /* If a mal
d37d0 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75 73 74 20 loc fails, just
d37e0 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64 65 leak the file de
d37f0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 20 20 scriptor */.
d3800 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
d3810 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 pOpen->aPending
d3820 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 = aNew;.
d3830 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 pOpen->aPending
d3840 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 [pOpen->nPending
d3850 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 ] = pFile->h;.
d3860 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 pOpen->nPe
d3870 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 20 20 20 20 nding++;.
d3880 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a pFile->h = -1;.
d3890 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
d38a0 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 releaseOpenCnt
d38b0 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a (pFile->pOpen);.
d38c0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
d38d0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 (pFile->lockingC
d38e0 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 63 6c 6f ontext);. clo
d38f0 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a seUnixFile(id);.
d3900 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 unixLeaveMut
d3910 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ex();. }. retu
d3920 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
d3930 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e .#endif /* defin
d3940 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
d3950 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
d3960 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a OCKING_STYLE */.
d3970 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 61 /*.** The code a
d3980 62 6f 76 65 20 69 73 20 74 68 65 20 41 46 50 20 bove is the AFP
d3990 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
d39a0 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64 65 20 69 ion. The code i
d39b0 73 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f s specific.** to
d39c0 20 4d 61 63 4f 53 58 20 61 6e 64 20 64 6f 65 73 MacOSX and does
d39d0 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 6f 74 68 not work on oth
d39e0 65 72 20 75 6e 69 78 20 70 6c 61 74 66 6f 72 6d er unix platform
d39f0 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e 61 74 69 s. No alternati
d3a00 76 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 62 ve.** is availab
d3a10 6c 65 2e 20 20 49 66 20 79 6f 75 20 64 6f 6e 27 le. If you don'
d3a20 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72 20 61 20 t compile for a
d3a30 6d 61 63 2c 20 74 68 65 6e 20 74 68 65 20 22 75 mac, then the "u
d3a40 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56 46 53 20 nix-afp".** VFS
d3a50 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
d3a60 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
d3a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
d3a80 66 20 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 f the AFP lock i
d3a90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a mplementation **
d3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3ab0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
d3ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3b00 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ***/.../********
d3b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3b50 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
d3b60 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f 63 6b ******* Non-lock
d3b70 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ing sqlite3_file
d3b80 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a methods *******
d3b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d3ba0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 ******.**.** The
d3bb0 20 6e 65 78 74 20 64 69 76 69 73 69 6f 6e 20 63 next division c
d3bc0 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e ontains implemen
d3bd0 74 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 20 tations for all
d3be0 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 0a methods of the .
d3bf0 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ** sqlite3_file
d3c00 6f 62 6a 65 63 74 20 6f 74 68 65 72 20 74 68 61 object other tha
d3c10 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 n the locking me
d3c20 74 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f 63 6b thods. The lock
d3c30 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 77 ing.** methods w
d3c40 65 72 65 20 64 65 66 69 6e 65 64 20 69 6e 20 64 ere defined in d
d3c50 69 76 69 73 69 6f 6e 73 20 61 62 6f 76 65 20 28 ivisions above (
d3c60 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 one locking meth
d3c70 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76 69 73 69 od per.** divisi
d3c80 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65 74 68 on). Those meth
d3c90 6f 64 73 20 74 68 61 74 20 61 72 65 20 63 6f 6d ods that are com
d3ca0 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 mon to all locki
d3cb0 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 72 65 20 ng modes.** are
d3cc0 67 61 74 68 65 72 20 74 6f 67 65 74 68 65 72 20 gather together
d3cd0 69 6e 74 6f 20 74 68 69 73 20 64 69 76 69 73 69 into this divisi
d3ce0 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 on..*/../*.** Se
d3cf0 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 ek to the offset
d3d00 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
d3d10 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 econd argument,
d3d20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 0a 2a then read cnt .*
d3d30 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 75 * bytes into pBu
d3d40 66 2e 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 f. Return the nu
d3d50 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 mber of bytes ac
d3d60 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a 0a tually read..**.
d3d70 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 20 64 ** NB: If you d
d3d80 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 20 efine USE_PREAD
d3d90 6f 72 20 55 53 45 5f 50 52 45 41 44 36 34 2c 20 or USE_PREAD64,
d3da0 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 61 6c then it might al
d3db0 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 61 so.** be necessa
d3dc0 72 79 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f ry to define _XO
d3dd0 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 62 65 PEN_SOURCE to be
d3de0 20 35 30 30 2e 20 20 54 68 69 73 20 76 61 72 69 500. This vari
d3df0 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 73 es from.** one s
d3e00 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 65 72 ystem to another
d3e10 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 74 65 20 . Since SQLite
d3e20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 does not define
d3e30 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 6e 79 USE_PREAD.** any
d3e40 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 65 66 any form by def
d3e50 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 6e 6f ault, we will no
d3e60 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 66 t attempt to def
d3e70 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 ine _XOPEN_SOURC
d3e80 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65 74 E..** See ticket
d3e90 73 20 23 32 37 34 31 20 61 6e 64 20 23 32 36 38 s #2741 and #268
d3ea0 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 1..**.** To avoi
d3eb0 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 d stomping the e
d3ec0 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 rrno value on a
d3ed0 66 61 69 6c 65 64 20 72 65 61 64 20 74 68 65 20 failed read the
d3ee0 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a lastErrno value.
d3ef0 2a 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 ** is set before
d3f00 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 returning..*/.s
d3f10 74 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e tatic int seekAn
d3f20 64 52 65 61 64 28 75 6e 69 78 46 69 6c 65 20 2a dRead(unixFile *
d3f30 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 id, sqlite3_int6
d3f40 34 20 6f 66 66 73 65 74 2c 20 76 6f 69 64 20 2a 4 offset, void *
d3f50 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a pBuf, int cnt){.
d3f60 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34 int got;. i64
d3f70 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 49 newOffset;. TI
d3f80 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 64 MER_START;.#if d
d3f90 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 efined(USE_PREAD
d3fa0 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 61 64 28 ). got = pread(
d3fb0 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 id->h, pBuf, cnt
d3fc0 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 53 69 6d , offset);. Sim
d3fd0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 67 6f ulateIOError( go
d3fe0 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 69 66 20 t = -1 );.#elif
d3ff0 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 defined(USE_PREA
d4000 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 D64). got = pre
d4010 61 64 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 ad64(id->h, pBuf
d4020 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a , cnt, offset);.
d4030 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
d4040 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 r( got = -1 );.#
d4050 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 else. newOffset
d4060 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 = lseek(id->h,
d4070 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 offset, SEEK_SET
d4080 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
d4090 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 74 2d rror( newOffset-
d40a0 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 - );. if( newOf
d40b0 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a fset!=offset ){.
d40c0 20 20 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 if( newOffse
d40d0 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 t == -1 ){.
d40e0 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
d40f0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
d4100 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a rno;. }else{.
d4110 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 ((unixFile
d4120 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f *)id)->lastErrno
d4130 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 = 0;.... }.
d4140 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 return -1;.
d4150 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61 64 28 69 }. got = read(i
d4160 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 d->h, pBuf, cnt)
d4170 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 ;.#endif. TIMER
d4180 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c _END;. if( got<
d4190 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 0 ){. ((unixF
d41a0 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
d41b0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d rno = errno;. }
d41c0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 52 45 41 . OSTRACE5("REA
d41d0 44 20 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 D %-3d %5d %7
d41e0 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d lld %llu\n", id-
d41f0 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c >h, got, offset,
d4200 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b TIMER_ELAPSED);
d4210 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d . return got;.}
d4220 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 ../*.** Read dat
d4230 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e a from a file in
d4240 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 to a buffer. Re
d4250 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 turn SQLITE_OK i
d4260 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 f all.** bytes w
d4270 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 ere read success
d4280 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 fully and SQLITE
d4290 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 _IOERR if anythi
d42a0 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 ng goes.** wrong
d42b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d42c0 75 6e 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69 unixRead(. sqli
d42d0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 te3_file *id, .
d42e0 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 void *pBuf, .
d42f0 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 int amt,. sqlit
d4300 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a e3_int64 offset.
d4310 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 ){. int got;.
d4320 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 0a 20 assert( id );..
d4330 20 2f 2a 20 4e 65 76 65 72 20 72 65 61 64 20 6f /* Never read o
d4340 72 20 77 72 69 74 65 20 61 6e 79 20 6f 66 20 74 r write any of t
d4350 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 he bytes in the
d4360 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 2a 2f locking range */
d4370 0a 20 20 61 73 73 65 72 74 28 20 28 28 75 6e 69 . assert( ((uni
d4380 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 73 4c 6f xFile*)id)->isLo
d4390 63 6b 61 62 6c 65 3d 3d 30 0a 20 20 20 20 20 20 ckable==0.
d43a0 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 || offset>=P
d43b0 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a ENDING_BYTE+512.
d43c0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 || off
d43d0 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 set+amt<=PENDING
d43e0 5f 42 59 54 45 20 29 3b 0a 0a 20 20 67 6f 74 20 _BYTE );.. got
d43f0 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 28 75 = seekAndRead((u
d4400 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20 6f 66 66 nixFile*)id, off
d4410 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 3b set, pBuf, amt);
d4420 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 6d 74 20 . if( got==amt
d4430 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
d4440 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
d4450 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 if( got<0 ){.
d4460 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 /* lastErrno s
d4470 65 74 20 62 79 20 73 65 65 6b 41 6e 64 52 65 61 et by seekAndRea
d4480 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 d */. return
d4490 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 SQLITE_IOERR_REA
d44a0 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 D;. }else{.
d44b0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
d44c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 >lastErrno = 0;
d44d0 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 /* not a system
d44e0 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 error */. /*
d44f0 55 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 Unread parts of
d4500 74 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 the buffer must
d4510 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a be zero-filled *
d4520 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 /. memset(&((
d4530 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d char*)pBuf)[got]
d4540 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 , 0, amt-got);.
d4550 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d4560 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
d4570 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D;. }.}../*.**
d4580 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 Seek to the offs
d4590 65 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 et in id->offset
d45a0 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 then read cnt b
d45b0 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a ytes into pBuf..
d45c0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
d45d0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 mber of bytes ac
d45e0 74 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 tually read. Up
d45f0 64 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e date the offset.
d4600 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 .**.** To avoid
d4610 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 stomping the err
d4620 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 no value on a fa
d4630 69 6c 65 64 20 77 72 69 74 65 20 74 68 65 20 6c iled write the l
d4640 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a astErrno value.*
d4650 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 * is set before
d4660 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 returning..*/.st
d4670 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 atic int seekAnd
d4680 57 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a Write(unixFile *
d4690 69 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 id, i64 offset,
d46a0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 const void *pBuf
d46b0 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e , int cnt){. in
d46c0 74 20 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 t got;. i64 new
d46d0 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f Offset;. TIMER_
d46e0 53 54 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e START;.#if defin
d46f0 65 64 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 ed(USE_PREAD).
d4700 67 6f 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d got = pwrite(id-
d4710 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
d4720 66 66 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 ffset);.#elif de
d4730 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 fined(USE_PREAD6
d4740 34 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 4). got = pwrit
d4750 65 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c e64(id->h, pBuf,
d4760 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 cnt, offset);.#
d4770 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 else. newOffset
d4780 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 = lseek(id->h,
d4790 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 offset, SEEK_SET
d47a0 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 );. if( newOffs
d47b0 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 et!=offset ){.
d47c0 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20 if( newOffset
d47d0 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 == -1 ){. (
d47e0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
d47f0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
d4800 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 o;. }else{.
d4810 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
d4820 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d id)->lastErrno =
d4830 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 0;.... }.
d4840 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a return -1;. }.
d4850 20 20 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 got = write(id
d4860 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b ->h, pBuf, cnt);
d4870 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f .#endif. TIMER_
d4880 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 END;. if( got<0
d4890 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 ){. ((unixFi
d48a0 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 le*)id)->lastErr
d48b0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a no = errno;. }.
d48c0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 57 52 49 . OSTRACE5("WRI
d48d0 54 45 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 TE %-3d %5d %7
d48e0 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d lld %llu\n", id-
d48f0 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c >h, got, offset,
d4900 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b TIMER_ELAPSED);
d4910 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d . return got;.}
d4920 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 .../*.** Write d
d4930 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 ata from a buffe
d4940 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 r into a file.
d4950 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
d4960 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f on success.** o
d4970 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 r some other err
d4980 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 or code on failu
d4990 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e re..*/.static in
d49a0 74 20 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73 t unixWrite(. s
d49b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d49c0 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
d49d0 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 pBuf, . int amt
d49e0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ,. sqlite3_int6
d49f0 34 20 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 69 4 offset .){. i
d4a00 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20 nt wrote = 0;.
d4a10 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 assert( id );.
d4a20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b assert( amt>0 );
d4a30 0a 0a 20 20 2f 2a 20 4e 65 76 65 72 20 72 65 61 .. /* Never rea
d4a40 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 6f d or write any o
d4a50 66 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 f the bytes in t
d4a60 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 he locking range
d4a70 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 28 */. assert( ((
d4a80 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 unixFile*)id)->i
d4a90 73 4c 6f 63 6b 61 62 6c 65 3d 3d 30 0a 20 20 20 sLockable==0.
d4aa0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 || offset
d4ab0 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 >=PENDING_BYTE+5
d4ac0 31 32 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 12. ||
d4ad0 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 offset+amt<=PEND
d4ae0 49 4e 47 5f 42 59 54 45 20 29 3b 0a 0a 23 69 66 ING_BYTE );..#if
d4af0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a ndef NDEBUG. /*
d4b00 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 If we are doing
d4b10 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 a normal write
d4b20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 to a database fi
d4b30 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 le (as opposed t
d4b40 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68 o. ** doing a h
d4b50 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
d4b60 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74 ack or a write t
d4b70 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65 o some file othe
d4b80 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f r than a. ** no
d4b90 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 rmal database fi
d4ba0 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20 le) then record
d4bb0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 the fact that th
d4bc0 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 e database. **
d4bd0 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66 has changed. If
d4be0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
d4bf0 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69 counter is modi
d4c00 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61 fied, record tha
d4c10 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e t. ** fact too.
d4c20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 28 75 6e . */. if( ((un
d4c30 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 6e 4e ixFile*)id)->inN
d4c40 6f 72 6d 61 6c 57 72 69 74 65 20 29 7b 0a 20 20 ormalWrite ){.
d4c50 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d4c60 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d4c70 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62 d;. pFile->db
d4c80 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a 20 Update = 1; /*
d4c90 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 The database has
d4ca0 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 2a been modified *
d4cb0 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 74 /. if( offset
d4cc0 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b 61 <=24 && offset+a
d4cd0 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 20 mt>=27 ){.
d4ce0 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63 68 int rc;. ch
d4cf0 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a 20 ar oldCntr[4];.
d4d00 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
d4d10 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 rrorBenign(1);.
d4d20 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41 6e rc = seekAn
d4d30 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34 2c dRead(pFile, 24,
d4d40 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 20 oldCntr, 4);.
d4d50 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
d4d60 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 rorBenign(0);.
d4d70 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c 7c if( rc!=4 ||
d4d80 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72 2c memcmp(oldCntr,
d4d90 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b &((char*)pBuf)[
d4da0 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21 3d 24-offset], 4)!=
d4db0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 0 ){. pFi
d4dc0 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e le->transCntrChn
d4dd0 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20 74 g = 1; /* The t
d4de0 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
d4df0 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a er has changed *
d4e00 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a /. }. }.
d4e10 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77 68 }.#endif.. wh
d4e20 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77 ile( amt>0 && (w
d4e30 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 rote = seekAndWr
d4e40 69 74 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ite((unixFile*)i
d4e50 64 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c d, offset, pBuf,
d4e60 20 61 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 amt))>0 ){.
d4e70 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 amt -= wrote;.
d4e80 20 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 offset += wrot
d4e90 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 e;. pBuf = &(
d4ea0 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f (char*)pBuf)[wro
d4eb0 74 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c te];. }. Simul
d4ec0 61 74 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f ateIOError(( wro
d4ed0 74 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 te=(-1), amt=1 )
d4ee0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 );. SimulateDis
d4ef0 6b 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f kfullError(( wro
d4f00 74 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a te=0, amt=1 ));.
d4f10 20 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 if( amt>0 ){.
d4f20 20 20 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 if( wrote<0 )
d4f30 7b 0a 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 {. /* lastE
d4f40 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b rrno set by seek
d4f50 41 6e 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 AndWrite */.
d4f60 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d4f70 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 IOERR_WRITE;.
d4f80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 }else{. ((
d4f90 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
d4fa0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a astErrno = 0; /*
d4fb0 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 not a system er
d4fc0 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 ror */. ret
d4fd0 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b urn SQLITE_FULL;
d4fe0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
d4ff0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
d5000 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
d5010 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 TEST./*.** Count
d5020 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 the number of f
d5030 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 ullsyncs and nor
d5040 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 mal syncs. This
d5050 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 is used to test
d5060 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 .** that syncs a
d5070 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 nd fullsyncs are
d5080 20 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 occurring at th
d5090 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a e right times..*
d50a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
d50b0 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f sqlite3_sync_co
d50c0 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f unt = 0;.SQLITE_
d50d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
d50e0 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d fullsync_count =
d50f0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
d5100 2a 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 * We do not trus
d5110 74 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f t systems to pro
d5120 76 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 vide a working f
d5130 64 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d datasync(). Som
d5140 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 e do..** Others
d5150 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 do no. To be sa
d5160 66 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 fe, we will stic
d5170 6b 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 k with the (slow
d5180 65 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 er) fsync()..**
d5190 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 If you know that
d51a0 20 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 your system doe
d51b0 73 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 s support fdatas
d51c0 79 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c ync() correctly,
d51d0 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 .** then simply
d51e0 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 compile with -Df
d51f0 64 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 datasync=fdatasy
d5200 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e nc.*/.#if !defin
d5210 65 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 ed(fdatasync) &&
d5220 20 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 !defined(__linu
d5230 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 x__).# define fd
d5240 61 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 atasync fsync.#e
d5250 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 ndif../*.** Defi
d5260 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e ne HAVE_FULLFSYN
d5270 43 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 C to 0 or 1 depe
d5280 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 nding on whether
d5290 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 or not.** the F
d52a0 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f _FULLFSYNC macro
d52b0 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f is defined. F_
d52c0 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 FULLFSYNC is cur
d52d0 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 rently.** only a
d52e0 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 vailable on Mac
d52f0 4f 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 OS X. But that
d5300 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f could change..*/
d5310 0a 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 .#ifdef F_FULLFS
d5320 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 YNC.# define HAV
d5330 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 E_FULLFSYNC 1.#e
d5340 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 lse.# define HAV
d5350 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 E_FULLFSYNC 0.#e
d5360 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ndif.../*.** The
d5370 20 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 fsync() system
d5380 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f call does not wo
d5390 72 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64 rk as advertised
d53a0 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 on many.** unix
d53b0 20 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 systems. The f
d53c0 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 ollowing procedu
d53d0 72 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 re is an attempt
d53e0 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 to make.** it w
d53f0 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a ork better..**.*
d5400 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f * The SQLITE_NO_
d5410 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 SYNC macro disab
d5420 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 les all fsync()s
d5430 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75 . This is usefu
d5440 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 l.** for testing
d5450 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f when we want to
d5460 20 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 run through the
d5470 20 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63 test suite quic
d5480 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 kly..** You are
d5490 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 strongly advised
d54a0 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 *not* to deploy
d54b0 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f with SQLITE_NO_
d54c0 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c SYNC.** enabled,
d54d0 20 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 however, since
d54e0 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 with SQLITE_NO_S
d54f0 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 YNC enabled, an
d5500 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 OS crash.** or p
d5510 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c ower failure wil
d5520 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 l likely corrupt
d5530 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
d5540 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 le..**.** SQLite
d5550 20 73 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e sets the dataOn
d5560 6c 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 ly flag if the s
d5570 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
d5580 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a is unchanged..**
d5590 20 54 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 The idea behind
d55a0 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 dataOnly is tha
d55b0 74 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 t it should only
d55c0 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 write the file
d55d0 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 content.** to di
d55e0 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 sk, not the inod
d55f0 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 e. We only set
d5600 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 dataOnly if the
d5610 66 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a file size is .**
d5620 20 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 unchanged since
d5630 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 the file size i
d5640 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e s part of the in
d5650 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a ode. However, .
d5660 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c ** Ted Ts'o tell
d5670 73 20 75 73 20 74 68 61 74 20 66 64 61 74 61 73 s us that fdatas
d5680 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 ync() will also
d5690 77 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 write the inode
d56a0 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 if the.** file s
d56b0 69 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e ize has changed.
d56c0 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 The only real
d56d0 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 difference betwe
d56e0 65 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a en fdatasync().*
d56f0 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 * and fsync(), T
d5700 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 ed tells us, is
d5710 74 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 that fdatasync()
d5720 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 will not flush
d5730 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 the.** inode if
d5740 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e the mtime or own
d5750 65 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 er or other inod
d5760 65 20 61 74 74 72 69 62 75 74 65 73 20 68 61 76 e attributes hav
d5770 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 e changed..** We
d5780 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 only care about
d5790 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 the file size,
d57a0 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 not the other fi
d57b0 6c 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 le attributes, s
d57c0 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 o.** as far as S
d57d0 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e QLite is concern
d57e0 65 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 ed, an fdatasync
d57f0 28 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 () is always ade
d5800 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 quate..** So, we
d5810 20 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 always use fdat
d5820 61 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 async() if it is
d5830 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 available, rega
d5840 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 rdless of.** the
d5850 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 value of the da
d5860 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a taOnly flag..*/.
d5870 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f static int full_
d5880 66 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e fsync(int fd, in
d5890 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 t fullSync, int
d58a0 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 dataOnly){. int
d58b0 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 rc;.. /* The f
d58c0 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f ollowing "ifdef/
d58d0 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 elif/else/" bloc
d58e0 6b 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 k has the same s
d58f0 74 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a tructure as. **
d5900 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 the one below.
d5910 49 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 It is replicated
d5920 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 here solely to
d5930 61 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 avoid cluttering
d5940 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 . ** up the re
d5950 61 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 al code with the
d5960 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
d5970 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f R() macros.. */
d5980 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e .#ifdef SQLITE_N
d5990 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f O_SYNC. UNUSED_
d59a0 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 PARAMETER(fd);.
d59b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
d59c0 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 R(fullSync);. U
d59d0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
d59e0 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 dataOnly);.#elif
d59f0 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a HAVE_FULLFSYNC.
d5a00 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
d5a10 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 ER(dataOnly);.#e
d5a20 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 lse. UNUSED_PAR
d5a30 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 AMETER(fullSync)
d5a40 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
d5a50 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a ETER(dataOnly);.
d5a60 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 #endif.. /* Rec
d5a70 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ord the number o
d5a80 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 f times that we
d5a90 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e do a normal fsyn
d5aa0 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 c() and . ** FU
d5ab0 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 LLSYNC. This is
d5ac0 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 used during tes
d5ad0 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 ting to verify t
d5ae0 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 hat this procedu
d5af0 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c re. ** gets cal
d5b00 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 led with the cor
d5b10 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a rect arguments..
d5b20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 */.#ifdef SQLI
d5b30 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 TE_TEST. if( fu
d5b40 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 llSync ) sqlite3
d5b50 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b _fullsync_count+
d5b60 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e +;. sqlite3_syn
d5b70 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 c_count++;.#endi
d5b80 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f f.. /* If we co
d5b90 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 mpiled with the
d5ba0 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 SQLITE_NO_SYNC f
d5bb0 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e lag, then syncin
d5bc0 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f g is a. ** no-o
d5bd0 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 p. */.#ifdef SQ
d5be0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 LITE_NO_SYNC. r
d5bf0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 c = SQLITE_OK;.#
d5c00 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 elif HAVE_FULLFS
d5c10 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 YNC. if( fullSy
d5c20 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 nc ){. rc = f
d5c30 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 cntl(fd, F_FULLF
d5c40 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 SYNC, 0);. }els
d5c50 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 e{. rc = 1;.
d5c60 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 }. /* If the F
d5c70 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c ULLFSYNC failed,
d5c80 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 fall back to at
d5c90 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e tempting an fsyn
d5ca0 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f c().. ** It sho
d5cb0 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 uldn't be possib
d5cc0 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 le for fullfsync
d5cd0 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 to fail on the
d5ce0 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 local . ** file
d5cf0 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 system (on OSX)
d5d00 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 , so failure ind
d5d10 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c icates that FULL
d5d20 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 FSYNC. ** isn't
d5d30 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 supported for t
d5d40 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e his file system.
d5d50 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 So, attempt an
d5d60 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 fsync . ** and
d5d70 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 (for now) ignore
d5d80 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 the overhead of
d5d90 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 a superfluous f
d5da0 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a cntl call. . *
d5db0 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65 72 * It'd be better
d5dc0 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 to detect fullf
d5dd0 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 sync support onc
d5de0 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a e and avoid . *
d5df0 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c * the fcntl call
d5e00 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 every time sync
d5e10 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f is called.. */
d5e20 0a 20 20 69 66 28 20 72 63 20 29 20 72 63 20 3d . if( rc ) rc =
d5e30 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c fsync(fd);..#el
d5e40 73 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 se . rc = fdata
d5e50 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 sync(fd);.#if OS
d5e60 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 _VXWORKS. if( r
d5e70 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d c==-1 && errno==
d5e80 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 ENOTSUP ){. r
d5e90 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 c = fsync(fd);.
d5ea0 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f }.#endif /* OS_
d5eb0 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 VXWORKS */.#endi
d5ec0 66 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 f /* ifdef SQLIT
d5ed0 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 E_NO_SYNC elif H
d5ee0 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f AVE_FULLFSYNC */
d5ef0 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 .. if( OS_VXWOR
d5f00 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b KS && rc!= -1 ){
d5f10 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d . rc = 0;. }
d5f20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d5f30 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
d5f40 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 all writes to a
d5f50 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 particular file
d5f60 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 are committed t
d5f70 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 o disk..**.** If
d5f80 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 dataOnly==0 the
d5f90 6e 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 n both the file
d5fa0 69 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d itself and its m
d5fb0 65 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a etadata (file.**
d5fc0 20 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 size, access ti
d5fd0 6d 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e me, etc) are syn
d5fe0 63 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c ced. If dataOnl
d5ff0 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 y!=0 then only t
d6000 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 he.** file data
d6010 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a is synced..**.**
d6020 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 Under Unix, als
d6030 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
d6040 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 the directory e
d6050 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c ntry for the fil
d6060 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 e.** has been cr
d6070 65 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 eated by fsync-i
d6080 6e 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 ng the directory
d6090 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
d60a0 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 he file..** If w
d60b0 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 e do not do this
d60c0 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 and we encounte
d60d0 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 r a power failur
d60e0 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 e, the directory
d60f0 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 .** entry for th
d6100 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 e journal might
d6110 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 20 not exist after
d6120 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 we reboot. The
d6130 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 next.** SQLite t
d6140 6f 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c o access the fil
d6150 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 e will not know
d6160 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c that the journal
d6170 20 65 78 69 73 74 73 20 28 62 65 63 61 75 73 65 exists (because
d6180 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 .** the director
d6190 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 y entry for the
d61a0 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 journal was neve
d61b0 72 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 r created) and t
d61c0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a he transaction.*
d61d0 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 * will not roll
d61e0 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 back - possibly
d61f0 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 leading to datab
d6200 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a ase corruption..
d6210 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
d6220 69 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 ixSync(sqlite3_f
d6230 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 ile *id, int fla
d6240 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 gs){. int rc;.
d6250 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d6260 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d6270 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f ;.. int isDataO
d6280 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c nly = (flags&SQL
d6290 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c ITE_SYNC_DATAONL
d62a0 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c Y);. int isFull
d62b0 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 sync = (flags&0x
d62c0 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 0F)==SQLITE_SYNC
d62d0 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 _FULL;.. /* Che
d62e0 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 ck that one of S
d62f0 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 QLITE_SYNC_NORMA
d6300 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 L or FULL was pa
d6310 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 ssed */. assert
d6320 28 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 ((flags&0x0F)==S
d6330 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 QLITE_SYNC_NORMA
d6340 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 L. || (flag
d6350 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f s&0x0F)==SQLITE_
d6360 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a SYNC_FULL. );..
d6370 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 /* Unix cannot
d6380 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 , but some syste
d6390 6d 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 ms may return SQ
d63a0 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 LITE_FULL from h
d63b0 65 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c ere. This. ** l
d63c0 69 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 ine is to test t
d63d0 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 hat doing so doe
d63e0 73 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 s not cause any
d63f0 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 problems.. */.
d6400 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
d6410 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 lError( return S
d6420 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 QLITE_FULL );..
d6430 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d6440 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 ;. OSTRACE2("SY
d6450 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 NC %-3d\n", p
d6460 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d File->h);. rc =
d6470 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c full_fsync(pFil
d6480 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 e->h, isFullsync
d6490 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 , isDataOnly);.
d64a0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d64b0 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 ( rc=1 );. if(
d64c0 72 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d rc ){. pFile-
d64d0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
d64e0 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 no;. return S
d64f0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e QLITE_IOERR_FSYN
d6500 43 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 C;. }. if( pFi
d6510 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a le->dirfd>=0 ){.
d6520 20 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 int err;.
d6530 20 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59 OSTRACE4("DIRSY
d6540 4e 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 NC %-3d (have_fu
d6550 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 llfsync=%d fulls
d6560 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c ync=%d)\n", pFil
d6570 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 e->dirfd,.
d6580 20 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 HAVE_FULLF
d6590 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 SYNC, isFullsync
d65a0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
d65b0 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e E_DISABLE_DIRSYN
d65c0 43 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 C. /* The dir
d65d0 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f ectory sync is o
d65e0 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 nly attempted if
d65f0 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 full_fsync is.
d6600 20 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 ** turned off
d6610 20 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e or unavailable.
d6620 20 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e If a full_fsyn
d6630 63 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 c occurred above
d6640 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 ,. ** then th
d6650 65 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 e directory sync
d6660 20 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e is superfluous.
d6670 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
d6680 28 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 (!HAVE_FULLFSYNC
d6690 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 || !isFullsync)
d66a0 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 && full_fsync(p
d66b0 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 File->dirfd,0,0)
d66c0 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 ){. /*.
d66d0 20 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 ** We have
d66e0 72 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c received multipl
d66f0 65 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 e reports of fsy
d6700 6e 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 nc() returning.
d6710 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 ** errors
d6720 77 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 when applied to
d6730 64 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 directories on c
d6740 65 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 ertain file syst
d6750 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 ems.. ** A
d6760 20 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 failed director
d6770 79 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 y sync is not a
d6780 62 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 big deal. So it
d6790 20 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a seems. **
d67a0 20 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 better to ignor
d67b0 65 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 e the error. Ti
d67c0 63 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 cket #1657.
d67d0 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 */. /* p
d67e0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d67f0 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 = errno; */.
d6800 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c /* return SQL
d6810 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 ITE_IOERR; */.
d6820 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 }.#endif. e
d6830 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 rr = close(pFile
d6840 2d 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c ->dirfd); /* Onl
d6850 79 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f y need to sync o
d6860 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 nce, so close th
d6870 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 e */. if( err
d6880 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 ==0 ){
d6890 20 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 /* directory
d68a0 20 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e when we are don
d68b0 65 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 e */. pFile
d68c0 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 ->dirfd = -1;.
d68d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
d68e0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d68f0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 = errno;. r
d6900 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
d6910 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 _DIR_CLOSE;.
d6920 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
d6930 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e c;.}../*.** Trun
d6940 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c cate an open fil
d6950 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 e to a specified
d6960 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 size.*/.static
d6970 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 int unixTruncate
d6980 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d6990 64 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 d, i64 nByte){.
d69a0 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
d69b0 74 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c t( id );. Simul
d69c0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
d69d0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
d69e0 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 TRUNCATE );. rc
d69f0 20 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 = ftruncate(((u
d6a00 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c nixFile*)id)->h,
d6a10 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a (off_t)nByte);.
d6a20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
d6a30 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
d6a40 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
d6a50 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 no;. return S
d6a60 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e QLITE_IOERR_TRUN
d6a70 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 CATE;. }else{.
d6a80 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d6a90 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a _OK;. }.}../*.*
d6aa0 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 * Determine the
d6ab0 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 current size of
d6ac0 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a a file in bytes.
d6ad0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
d6ae0 69 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 ixFileSize(sqlit
d6af0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 e3_file *id, i64
d6b00 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 *pSize){. int
d6b10 72 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 rc;. struct sta
d6b20 74 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 t buf;. assert(
d6b30 20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 id );. rc = fs
d6b40 74 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 tat(((unixFile*)
d6b50 69 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 id)->h, &buf);.
d6b60 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d6b70 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 ( rc=1 );. if(
d6b80 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 rc!=0 ){. ((u
d6b90 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 nixFile*)id)->la
d6ba0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
d6bb0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d6bc0 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a TE_IOERR_FSTAT;.
d6bd0 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 }. *pSize = b
d6be0 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f uf.st_size;.. /
d6bf0 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 * When opening a
d6c00 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 zero-size datab
d6c10 61 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 ase, the findLoc
d6c20 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 kInfo() procedur
d6c30 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 e. ** writes a
d6c40 73 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f single byte into
d6c50 20 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 that file in or
d6c60 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 der to work arou
d6c70 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e nd a bug. ** in
d6c80 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 the OS-X msdos
d6c90 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 filesystem. In
d6ca0 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 order to avoid p
d6cb0 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 roblems with upp
d6cc0 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 er. ** layers,
d6cd0 77 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 we need to repor
d6ce0 74 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 t this file size
d6cf0 20 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 as zero even th
d6d00 6f 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 ough it is. **
d6d10 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b really 1. Tick
d6d20 65 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 et #3260.. */.
d6d30 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 if( *pSize==1 )
d6d40 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 *pSize = 0;...
d6d50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d6d60 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 K;.}..#if SQLITE
d6d70 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
d6d80 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 STYLE && defined
d6d90 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a (__APPLE__)./*.*
d6da0 2a 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 * Handler for pr
d6db0 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 oxy-locking file
d6dc0 2d 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 -control verbs.
d6dd0 20 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 Defined below i
d6de0 6e 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e n the.** proxyin
d6df0 67 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 g locking divisi
d6e00 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e on..*/.static in
d6e10 74 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 t proxyFileContr
d6e20 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ol(sqlite3_file*
d6e30 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e ,int,void*);.#en
d6e40 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f dif.../*.** Info
d6e50 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 rmation and cont
d6e60 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 rol of an open f
d6e70 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 ile handle..*/.s
d6e80 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 tatic int unixFi
d6e90 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
d6ea0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
d6eb0 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b op, void *pArg){
d6ec0 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b . switch( op ){
d6ed0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
d6ee0 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 _FCNTL_LOCKSTATE
d6ef0 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a : {. *(int*
d6f00 29 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 )pArg = ((unixFi
d6f10 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 le*)id)->locktyp
d6f20 65 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e;. return
d6f30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
d6f40 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
d6f50 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 _LAST_ERRNO: {.
d6f60 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 *(int*)pArg
d6f70 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 = ((unixFile*)i
d6f80 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 d)->lastErrno;.
d6f90 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
d6fa0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 TE_OK;. }.#if
d6fb0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 ndef NDEBUG.
d6fc0 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 6c /* The pager cal
d6fd0 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 ls this method t
d6fe0 6f 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 o signal that it
d6ff0 20 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a has done. **
d7000 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 a rollback and
d7010 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 that the databas
d7020 65 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 e is therefore u
d7030 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 nchanged and.
d7040 20 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 ** it hence it
d7050 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 is OK for the tr
d7060 61 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 ansaction change
d7070 20 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 counter to be.
d7080 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e ** unchanged.
d7090 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 . */. case
d70a0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 SQLITE_FCNTL_DB
d70b0 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 _UNCHANGED: {.
d70c0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
d70d0 69 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 id)->dbUpdate =
d70e0 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 0;. return
d70f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
d7100 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 .#endif.#if SQLI
d7110 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
d7120 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e G_STYLE && defin
d7130 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 ed(__APPLE__).
d7140 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 case SQLITE_SE
d7150 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a T_LOCKPROXYFILE:
d7160 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
d7170 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _GET_LOCKPROXYFI
d7180 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 LE: {. retu
d7190 72 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 rn proxyFileCont
d71a0 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b rol(id,op,pArg);
d71b0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.#endif /*
d71c0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
d71d0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 OCKING_STYLE &&
d71e0 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
d71f0 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 _) */. }. retu
d7200 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
d7210 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
d7220 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 the sector size
d7230 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 in bytes of the
d7240 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 underlying bloc
d7250 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 k device for.**
d7260 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 the specified fi
d7270 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f le. This is almo
d7280 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 st always 512 by
d7290 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a tes, but may be.
d72a0 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f ** larger for so
d72b0 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a me devices..**.*
d72c0 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 * SQLite code as
d72d0 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 sumes this funct
d72e0 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e ion cannot fail.
d72f0 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 It also assumes
d7300 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 that.** if two
d7310 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 files are create
d7320 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 d in the same fi
d7330 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 le-system direct
d7340 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 ory (i.e..** a d
d7350 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 atabase and its
d7360 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 journal file) th
d7370 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 at the sector si
d7380 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a ze will be the.*
d7390 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e * same for both.
d73a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
d73b0 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 nixSectorSize(sq
d73c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 lite3_file *NotU
d73d0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
d73e0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
d73f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
d7400 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f TE_DEFAULT_SECTO
d7410 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a R_SIZE;.}../*.**
d7420 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 Return the devi
d7430 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 ce characteristi
d7440 63 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e cs for the file.
d7450 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 This is always
d7460 30 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 0 for unix..*/.s
d7470 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 tatic int unixDe
d7480 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
d7490 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ics(sqlite3_file
d74a0 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
d74b0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
d74c0 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
d74d0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 n 0;.}../*.** He
d74e0 72 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c re ends the impl
d74f0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c ementation of al
d7500 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d l sqlite3_file m
d7510 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a ethods..**.*****
d7520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7530 2a 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 * End sqlite3_fi
d7540 6c 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a le Methods *****
d7550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
d7570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d75a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d75b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
d75c0 2a 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 * This division
d75d0 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 contains definit
d75e0 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f ions of sqlite3_
d75f0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 io_methods objec
d7600 74 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 ts that.** imple
d7610 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c ment various fil
d7620 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 e locking strate
d7630 67 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 gies. It also c
d7640 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 ontains definiti
d7650 6f 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 ons.** of "finde
d7660 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 r" functions. A
d7670 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
d7680 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 is used to loca
d7690 74 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 te the appropria
d76a0 74 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f te.** sqlite3_io
d76b0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 _methods object
d76c0 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 for a particular
d76d0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
d76e0 20 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a The pAppData.**
d76f0 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 field of the sq
d7700 6c 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 lite3_vfs VFS ob
d7710 6a 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 jects are initia
d7720 6c 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e lized to be poin
d7730 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 ters to.** the c
d7740 6f 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 orrect finder-fu
d7750 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 nction for that
d7760 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 VFS..**.** Most
d7770 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 finder functions
d7780 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 return a pointe
d7790 72 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c r to a fixed sql
d77a0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a ite3_io_methods.
d77b0 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 ** object. The
d77c0 6f 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 only interesting
d77d0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
d77e0 20 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 is autolockIoFi
d77f0 6e 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c nder, which.** l
d7800 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 ooks at the file
d7810 73 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 system type and
d7820 74 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 tries to guess t
d7830 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a he best locking.
d7840 2a 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d ** strategy from
d7850 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 that..**.** For
d7860 20 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 finder-funtion
d7870 46 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 F, two objects a
d7880 72 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a re created:.**.*
d7890 2a 20 20 20 20 28 31 29 20 54 68 65 20 72 65 61 * (1) The rea
d78a0 6c 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f l finder-functio
d78b0 6e 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 n named "FImpt()
d78c0 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 "..**.** (2)
d78d0 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 A constant point
d78e0 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 er to this funct
d78f0 69 6f 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 io named just "F
d7900 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f "..**.**.** A po
d7910 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 inter to the F p
d7920 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 ointer is used a
d7930 73 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 s the pAppData v
d7940 61 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 alue for VFS.**
d7950 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 objects. We hav
d7960 65 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 e to do this ins
d7970 74 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 tead of letting
d7980 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a pAppData point.*
d7990 2a 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 * directly at th
d79a0 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f e finder-functio
d79b0 6e 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 n since C90 rule
d79c0 73 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 s prevent a void
d79d0 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 *.** from be cas
d79e0 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f t into a functio
d79f0 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a n pointer..**.**
d7a00 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 .** Each instanc
d7a10 65 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 e of this macro
d7a20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 generates two ob
d7a30 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a jects:.**.** *
d7a40 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c A constant sql
d7a50 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
d7a60 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 object call METH
d7a70 4f 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b OD that has lock
d7a80 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 ing.** meth
d7a90 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c ods CLOSE, LOCK,
d7aa0 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f UNLOCK, CKRESLO
d7ab0 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 CK..**.** * A
d7ac0 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e n I/O method fin
d7ad0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c der function cal
d7ae0 6c 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 led FINDER that
d7af0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
d7b00 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 r.** to the
d7b10 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 METHOD object i
d7b20 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 n the previous b
d7b30 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e ullet..*/.#defin
d7b40 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 e IOMETHODS(FIND
d7b50 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 ER, METHOD, CLOS
d7b60 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c E, LOCK, UNLOCK,
d7b70 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 CKLOCK)
d7b80 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 \.static
d7b90 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
d7ba0 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 _methods METHOD
d7bb0 3d 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 = {
d7bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7bd0 20 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 \. 1,
d7be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7bf0 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
d7c00 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 on */
d7c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7c20 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c \. CLOSE,
d7c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7c40 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 /* xClose
d7c50 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
d7c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7c70 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61 \. unixRea
d7c80 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
d7c90 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f /* xRead */
d7ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7cc0 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 \. unixWrit
d7cd0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
d7ce0 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f /* xWrite */
d7cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7d10 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 \. unixTrunc
d7d20 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ate,
d7d30 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 /* xTruncate
d7d40 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
d7d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7d60 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 \. unixSync,
d7d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7d80 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 /* xSync */
d7d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7db0 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a \. unixFileSiz
d7dc0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
d7dd0 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f /* xFileSize */
d7de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
d7e00 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 . LOCK,
d7e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7e20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 /* xLock */
d7e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
d7e50 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 UNLOCK,
d7e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d7e70 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 * xUnlock */
d7e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7e90 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
d7ea0 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 CKLOCK,
d7eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d7ec0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
d7ed0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ock */
d7ee0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
d7ef0 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c unixFileControl
d7f00 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
d7f10 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 xFileControl */
d7f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7f30 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
d7f40 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 unixSectorSize,
d7f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
d7f60 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 SectorSize */
d7f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7f80 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 \. u
d7f90 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 nixDeviceCharact
d7fa0 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 eristics /* xD
d7fb0 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 eviceCapabilitie
d7fc0 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 s */
d7fd0 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 \.};
d7fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8020 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 \.static
d8030 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
d8040 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 o_methods *FINDE
d8050 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 R##Impl(const ch
d8060 61 72 20 2a 7a 2c 20 69 6e 74 20 68 29 7b 20 20 ar *z, int h){
d8070 20 20 20 20 20 20 20 5c 0a 20 20 55 4e 55 53 45 \. UNUSE
d8080 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 D_PARAMETER(z);
d8090 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
d80a0 28 68 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 (h);
d80b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d80c0 20 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e \. return
d80d0 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 &METHOD;
d80e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d80f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8110 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 \.}
d8120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8160 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e \.static con
d8170 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
d8180 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 thods *(*const F
d8190 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 INDER)(const cha
d81a0 72 2a 2c 69 6e 74 29 20 20 20 20 20 20 20 20 20 r*,int)
d81b0 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 \. = FINDE
d81c0 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 R##Impl;../*.**
d81d0 48 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 Here are all of
d81e0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d the sqlite3_io_m
d81f0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 ethods objects f
d8200 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a or each of the.*
d8210 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 * locking strate
d8220 67 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 gies. Functions
d8230 20 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 that return poi
d8240 6e 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d nters to these m
d8250 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c ethods.** are al
d8260 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 so created..*/.I
d8270 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 OMETHODS(. posi
d8280 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 xIoFinder,
d8290 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
d82a0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
d82b0 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 . posixIoMethod
d82c0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
d82d0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
d82e0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
d82f0 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 /. unixClose,
d8300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d8310 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
d8320 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 /. unixLock,
d8330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d8340 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
d8350 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 . unixUnlock,
d8360 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d8370 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
d8380 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 /. unixCheckRes
d8390 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a ervedLock /*
d83a0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
d83b0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
d83c0 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c IOMETHODS(. nol
d83d0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 ockIoFinder,
d83e0 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 /* Finder
d83f0 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
d8400 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 /. nolockIoMeth
d8410 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ods, /*
d8420 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
d8430 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ods object name
d8440 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 */. nolockClose
d8450 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
d8460 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 * xClose method
d8470 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c */. nolockLock,
d8480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d8490 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a * xLock method *
d84a0 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b /. nolockUnlock
d84b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
d84c0 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 xUnlock method
d84d0 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b */. nolockCheck
d84e0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f ReservedLock /
d84f0 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
d8500 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 Lock method */.)
d8510 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f .IOMETHODS(. do
d8520 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 tlockIoFinder,
d8530 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
d8540 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
d8550 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 */. dotlockIoMe
d8560 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f thods, /
d8570 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
d8580 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
d8590 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f */. dotlockClo
d85a0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 se,
d85b0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
d85c0 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 */. dotlockLoc
d85d0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
d85e0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
d85f0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f */. dotlockUnlo
d8600 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ck, /
d8610 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
d8620 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 */. dotlockChe
d8630 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 ckReservedLock
d8640 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
d8650 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
d8660 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e )..#if SQLITE_EN
d8670 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
d8680 4c 45 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 LE.IOMETHODS(.
d8690 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 flockIoFinder,
d86a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e /* Fin
d86b0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d der function nam
d86c0 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 e */. flockIoMe
d86d0 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 thods,
d86e0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d /* sqlite3_io_m
d86f0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 ethods object na
d8700 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f me */. flockClo
d8710 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 se,
d8720 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 /* xClose meth
d8730 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 od */. flockLoc
d8740 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
d8750 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f /* xLock metho
d8760 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f d */. flockUnlo
d8770 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
d8780 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 /* xUnlock meth
d8790 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 od */. flockChe
d87a0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 ckReservedLock
d87b0 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
d87c0 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a vedLock method *
d87d0 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 /.).#endif..#if
d87e0 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 OS_VXWORKS.IOMET
d87f0 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e HODS(. semIoFin
d8800 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 der,
d8810 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 /* Finder func
d8820 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 tion name */. s
d8830 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 emIoMethods,
d8840 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 /* sqli
d8850 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
d8860 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 bject name */.
d8870 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 semClose,
d8880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c /* xCl
d8890 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ose method */.
d88a0 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 semLock,
d88b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f /* xLo
d88c0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 ck method */. s
d88d0 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 emUnlock,
d88e0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c /* xUnl
d88f0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
d8900 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 semCheckReserved
d8910 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 Lock /* xCh
d8920 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
d8930 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 method */.).#end
d8940 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 if..#if defined(
d8950 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 __APPLE__) && SQ
d8960 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
d8970 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 ING_STYLE.IOMETH
d8980 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 ODS(. afpIoFind
d8990 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 er,
d89a0 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 /* Finder funct
d89b0 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 ion name */. af
d89c0 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 pIoMethods,
d89d0 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 /* sqlit
d89e0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
d89f0 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 ject name */. a
d8a00 66 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 fpClose,
d8a10 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f /* xClo
d8a20 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 se method */. a
d8a30 66 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 fpLock,
d8a40 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
d8a50 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 k method */. af
d8a60 70 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 pUnlock,
d8a70 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f /* xUnlo
d8a80 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 ck method */. a
d8a90 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c fpCheckReservedL
d8aa0 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 ock /* xChe
d8ab0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d ckReservedLock m
d8ac0 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 ethod */.).#endi
d8ad0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f f../*.** The pro
d8ae0 78 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f xy locking metho
d8af0 64 20 69 73 20 61 20 22 73 75 70 65 72 2d 6d 65 d is a "super-me
d8b00 74 68 6f 64 22 20 69 6e 20 74 68 65 20 73 65 6e thod" in the sen
d8b10 73 65 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70 se that it.** op
d8b20 65 6e 73 20 73 65 63 6f 6e 64 61 72 79 20 66 69 ens secondary fi
d8b30 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 66 le descriptors f
d8b40 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 61 6e 64 or the conch and
d8b50 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a lock files and.
d8b60 2a 2a 20 69 74 20 75 73 65 73 20 70 72 6f 78 79 ** it uses proxy
d8b70 2c 20 64 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c , dot-file, AFP,
d8b80 20 61 6e 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 and flock() loc
d8b90 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 king methods on
d8ba0 74 68 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 those.** seconda
d8bb0 72 79 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 74 ry files. For t
d8bc0 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 his reason, the
d8bd0 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20 69 6d division that im
d8be0 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 plements.** prox
d8bf0 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63 y locking is loc
d8c00 61 74 65 64 20 6d 75 63 68 20 66 75 72 74 68 65 ated much furthe
d8c10 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 66 69 r down in the fi
d8c20 6c 65 2e 20 20 42 75 74 20 77 65 20 6e 65 65 64 le. But we need
d8c30 0a 2a 2a 20 74 6f 20 67 6f 20 61 68 65 61 64 20 .** to go ahead
d8c40 61 6e 64 20 64 65 66 69 6e 65 20 74 68 65 20 73 and define the s
d8c50 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
d8c60 73 20 61 6e 64 20 66 69 6e 64 65 72 20 66 75 6e s and finder fun
d8c70 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f ction.** for pro
d8c80 78 79 20 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e xy locking here.
d8c90 20 20 53 6f 20 77 65 20 66 6f 72 77 61 72 64 20 So we forward
d8ca0 64 65 63 6c 61 72 65 20 74 68 65 20 49 2f 4f 20 declare the I/O
d8cb0 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 methods..*/.#if
d8cc0 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
d8cd0 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 _) && SQLITE_ENA
d8ce0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
d8cf0 45 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f E.static int pro
d8d00 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f xyClose(sqlite3_
d8d10 66 69 6c 65 2a 29 3b 0a 73 74 61 74 69 63 20 69 file*);.static i
d8d20 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c nt proxyLock(sql
d8d30 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 ite3_file*, int)
d8d40 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f ;.static int pro
d8d50 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 xyUnlock(sqlite3
d8d60 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 _file*, int);.st
d8d70 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 atic int proxyCh
d8d80 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
d8d90 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
d8da0 6e 74 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 nt*);.IOMETHODS(
d8db0 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 . proxyIoFinder
d8dc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
d8dd0 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 Finder function
d8de0 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49 name */. proxyI
d8df0 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
d8e00 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 /* sqlite3_i
d8e10 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
d8e20 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 name */. proxy
d8e30 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
d8e40 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d /* xClose m
d8e50 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 ethod */. proxy
d8e60 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 Lock,
d8e70 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 /* xLock me
d8e80 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 55 thod */. proxyU
d8e90 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 nlock,
d8ea0 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d /* xUnlock m
d8eb0 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 ethod */. proxy
d8ec0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d8ed0 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 k /* xCheckRe
d8ee0 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f servedLock metho
d8ef0 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 0a d */.).#endif...
d8f00 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 #if defined(__AP
d8f10 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
d8f20 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
d8f30 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 STYLE./* .** Thi
d8f40 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 s "finder" funct
d8f50 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ion attempts to
d8f60 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65 determine the be
d8f70 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 st locking strat
d8f80 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 egy .** for the
d8f90 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 22 66 database file "f
d8fa0 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 74 68 ilePath". It th
d8fb0 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 en returns the s
d8fc0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
d8fd0 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 s.** object that
d8fe0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 implements that
d8ff0 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a strategy..**.**
d9000 20 54 68 69 73 20 69 73 20 66 6f 72 20 4d 61 63 This is for Mac
d9010 4f 53 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 OSX only..*/.sta
d9020 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
d9030 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 3_io_methods *au
d9040 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d tolockIoFinderIm
d9050 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 pl(. const char
d9060 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f *filePath, /
d9070 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 * name of the da
d9080 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
d9090 20 69 6e 74 20 66 64 20 20 20 20 20 20 20 20 20 int fd
d90a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c /* fil
d90b0 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 e descriptor ope
d90c0 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 n on the databas
d90d0 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 e file */.){. s
d90e0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 tatic const stru
d90f0 63 74 20 4d 61 70 70 69 6e 67 20 7b 0a 20 20 20 ct Mapping {.
d9100 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
d9110 6c 65 73 79 73 74 65 6d 3b 20 20 20 20 20 20 20 lesystem;
d9120 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 73 79 /* Filesy
d9130 73 74 65 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a stem type name *
d9140 2f 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 /. const sqli
d9150 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
d9160 70 4d 65 74 68 6f 64 73 3b 20 20 20 2f 2a 20 41 pMethods; /* A
d9170 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 ppropriate locki
d9180 6e 67 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7d ng method */. }
d9190 20 61 4d 61 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 aMap[] = {.
d91a0 7b 20 22 68 66 73 22 2c 20 20 20 20 26 70 6f 73 { "hfs", &pos
d91b0 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 ixIoMethods },.
d91c0 20 20 20 7b 20 22 75 66 73 22 2c 20 20 20 20 26 { "ufs", &
d91d0 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d posixIoMethods }
d91e0 2c 0a 20 20 20 20 7b 20 22 61 66 70 66 73 22 2c ,. { "afpfs",
d91f0 20 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 &afpIoMethods
d9200 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 },.#ifdef SQLITE
d9210 5f 45 4e 41 42 4c 45 5f 41 46 50 5f 4c 4f 43 4b _ENABLE_AFP_LOCK
d9220 49 4e 47 5f 53 4d 42 0a 20 20 20 20 7b 20 22 73 ING_SMB. { "s
d9230 6d 62 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 mbfs", &afpIoMe
d9240 74 68 6f 64 73 20 7d 2c 0a 23 65 6c 73 65 0a 20 thods },.#else.
d9250 20 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 { "smbfs", &
d9260 66 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d flockIoMethods }
d9270 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 ,.#endif. { "
d9280 77 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 6b webdav", &nolock
d9290 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 IoMethods },.
d92a0 20 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a 20 { 0, 0 }. };.
d92b0 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
d92c0 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a statfs fsInfo;.
d92d0 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
d92e0 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 ockInfo;.. if(
d92f0 21 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 !filePath ){.
d9300 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d /* If filePath=
d9310 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 =NULL that means
d9320 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 we are dealing
d9330 77 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 with a transient
d9340 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 file. ** tha
d9350 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 t does not need
d9360 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f to be locked. */
d9370 0a 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c . return &nol
d9380 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 ockIoMethods;.
d9390 7d 0a 20 20 69 66 28 20 73 74 61 74 66 73 28 66 }. if( statfs(f
d93a0 69 6c 65 50 61 74 68 2c 20 26 66 73 49 6e 66 6f ilePath, &fsInfo
d93b0 29 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 69 ) != -1 ){. i
d93c0 66 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 f( fsInfo.f_flag
d93d0 73 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 s & MNT_RDONLY )
d93e0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 26 {. return &
d93f0 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b nolockIoMethods;
d9400 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
d9410 3d 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c =0; aMap[i].zFil
d9420 65 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a 20 esystem; i++){.
d9430 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 if( strcmp(
d9440 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e fsInfo.f_fstypen
d9450 61 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 ame, aMap[i].zFi
d9460 6c 65 73 79 73 74 65 6d 29 3d 3d 30 20 29 7b 0a lesystem)==0 ){.
d9470 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 return a
d9480 4d 61 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 3b Map[i].pMethods;
d9490 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d94a0 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 }.. /* Default
d94b0 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 case. Handles,
d94c0 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 amongst others,
d94d0 22 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 65 73 74 "nfs".. ** Test
d94e0 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b byte-range lock
d94f0 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20 using fcntl().
d9500 49 66 20 74 68 65 20 63 61 6c 6c 20 73 75 63 63 If the call succ
d9510 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75 eeds, . ** assu
d9520 6d 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 me that the file
d9530 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 -system supports
d9540 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 POSIX style loc
d9550 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b ks. . */. lock
d9560 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a Info.l_len = 1;.
d9570 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 lockInfo.l_sta
d9580 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e rt = 0;. lockIn
d9590 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 fo.l_whence = SE
d95a0 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e EK_SET;. lockIn
d95b0 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 fo.l_type = F_RD
d95c0 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c LCK;. if( fcntl
d95d0 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c (fd, F_GETLK, &l
d95e0 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b ockInfo)!=-1 ) {
d95f0 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 . return &pos
d9600 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d ixIoMethods;. }
d9610 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
d9620 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f &dotlockIoMetho
d9630 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 ds;. }.}.static
d9640 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
d9650 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e o_methods *(*con
d9660 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e st autolockIoFin
d9670 64 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a der)(const char*
d9680 2c 69 6e 74 29 0a 20 20 20 20 20 20 20 20 3d 20 ,int). =
d9690 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 autolockIoFinder
d96a0 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a Impl;..#endif /*
d96b0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
d96c0 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e __) && SQLITE_EN
d96d0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
d96e0 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 LE */../*.** An
d96f0 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66 6f abstract type fo
d9700 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 r a pointer to a
d9710 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64 65 IO method finde
d9720 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f 0a 74 r function:.*/.t
d9730 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 71 6c ypedef const sql
d9740 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
d9750 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 29 28 *(*finder_type)(
d9760 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 const char*,int)
d9770 3b 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.../***********
d9780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d97a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d97b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d97c0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
d97d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 ************** s
d97e0 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f qlite3_vfs metho
d97f0 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ds *************
d9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
d9810 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 73 **.** This divis
d9820 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ion contains the
d9830 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
d9840 6f 66 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 of methods on th
d9850 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73 e.** sqlite3_vfs
d9860 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a object..*/../*.
d9870 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** Initialize th
d9880 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
d9890 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 e unixFile struc
d98a0 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 ture pointed to
d98b0 62 79 20 70 49 64 2e 0a 2a 2f 0a 73 74 61 74 69 by pId..*/.stati
d98c0 63 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e 69 78 c int fillInUnix
d98d0 46 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f File(. sqlite3_
d98e0 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
d98f0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 /* Pointer to vf
d9900 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e s object */. in
d9910 74 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 t h,
d9920 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 /* Open fi
d9930 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 66 le descriptor of
d9940 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e file being open
d9950 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 ed */. int dirf
d9960 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
d9970 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c /* Directory fil
d9980 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a e descriptor */.
d9990 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
d99a0 70 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 pId, /* Wri
d99b0 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69 te to the unixFi
d99c0 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 65 72 le structure her
d99d0 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
d99e0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f r *zFilename, /
d99f0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 * Name of the fi
d9a00 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 le being opened
d9a10 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 2c */. int noLock,
d9a20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d9a30 4f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 66 20 Omit locking if
d9a40 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 true */. int is
d9a50 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 Delete
d9a60 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63 /* Delete on c
d9a70 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a lose if true */.
d9a80 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 ){. const sqlit
d9a90 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 e3_io_methods *p
d9aa0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 LockingStyle;.
d9ab0 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d unixFile *pNew =
d9ac0 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49 64 (unixFile *)pId
d9ad0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
d9ae0 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 ITE_OK;.. asser
d9af0 74 28 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b 3d 3d t( pNew->pLock==
d9b00 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 NULL );. assert
d9b10 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 3d 3d 4e ( pNew->pOpen==N
d9b20 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 50 61 72 ULL );.. /* Par
d9b30 61 6d 65 74 65 72 20 69 73 44 65 6c 65 74 65 20 ameter isDelete
d9b40 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 6f 6e 20 is only used on
d9b50 76 78 77 6f 72 6b 73 2e 0a 20 20 2a 2a 20 45 78 vxworks.. ** Ex
d9b60 70 72 65 73 73 20 74 68 69 73 20 65 78 70 6c 69 press this expli
d9b70 63 69 74 6c 79 20 68 65 72 65 20 74 6f 20 70 72 citly here to pr
d9b80 65 76 65 6e 74 20 63 6f 6d 70 69 6c 65 72 20 77 event compiler w
d9b90 61 72 6e 69 6e 67 73 0a 20 20 2a 2a 20 61 62 6f arnings. ** abo
d9ba0 75 74 20 75 6e 75 73 65 64 20 70 61 72 61 6d 65 ut unused parame
d9bb0 74 65 72 73 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 ters.. */.#if !
d9bc0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 55 4e 55 OS_VXWORKS. UNU
d9bd0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 73 SED_PARAMETER(is
d9be0 44 65 6c 65 74 65 29 3b 0a 23 65 6e 64 69 66 0a Delete);.#endif.
d9bf0 0a 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 . OSTRACE3("OPE
d9c00 4e 20 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c N %-3d %s\n",
d9c10 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 h, zFilename);
d9c20 20 20 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 . pNew->h =
d9c30 68 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 h;. pNew->dirfd
d9c40 20 3d 20 64 69 72 66 64 3b 0a 20 20 53 45 54 5f = dirfd;. SET_
d9c50 54 48 52 45 41 44 49 44 28 70 4e 65 77 29 3b 0a THREADID(pNew);.
d9c60 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
d9c70 20 20 70 4e 65 77 2d 3e 70 49 64 20 3d 20 76 78 pNew->pId = vx
d9c80 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 worksFindFileId(
d9c90 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 zFilename);. if
d9ca0 28 20 70 4e 65 77 2d 3e 70 49 64 3d 3d 30 20 29 ( pNew->pId==0 )
d9cb0 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b 20 3d 20 31 {. noLock = 1
d9cc0 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
d9cd0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e E_NOMEM;. }.#en
d9ce0 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f 4c 6f 63 dif.. if( noLoc
d9cf0 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e k ){. pLockin
d9d00 67 53 74 79 6c 65 20 3d 20 26 6e 6f 6c 6f 63 6b gStyle = &nolock
d9d10 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c IoMethods;. }el
d9d20 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67 se{. pLocking
d9d30 53 74 79 6c 65 20 3d 20 28 2a 2a 28 66 69 6e 64 Style = (**(find
d9d40 65 72 5f 74 79 70 65 2a 29 70 56 66 73 2d 3e 70 er_type*)pVfs->p
d9d50 41 70 70 44 61 74 61 29 28 7a 46 69 6c 65 6e 61 AppData)(zFilena
d9d60 6d 65 2c 20 68 29 3b 0a 23 69 66 20 53 51 4c 49 me, h);.#if SQLI
d9d70 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
d9d80 47 5f 53 54 59 4c 45 0a 20 20 20 20 2f 2a 20 43 G_STYLE. /* C
d9d90 61 63 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 69 ache zFilename i
d9da0 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f n the locking co
d9db0 6e 74 65 78 74 20 28 41 46 50 20 61 6e 64 20 64 ntext (AFP and d
d9dc0 6f 74 6c 6f 63 6b 20 6f 76 65 72 72 69 64 65 29 otlock override)
d9dd0 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 72 6f 78 for. ** prox
d9de0 79 4c 6f 63 6b 20 61 63 74 69 76 61 74 69 6f 6e yLock activation
d9df0 20 69 73 20 70 6f 73 73 69 62 6c 65 20 28 72 65 is possible (re
d9e00 6d 6f 74 65 20 70 72 6f 78 79 20 69 73 20 62 61 mote proxy is ba
d9e10 73 65 64 20 6f 6e 20 64 62 20 6e 61 6d 65 29 0a sed on db name).
d9e20 20 20 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 ** zFilename
d9e30 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 remains valid u
d9e40 6e 74 69 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f ntil file is clo
d9e50 73 65 64 2c 20 74 6f 20 73 75 70 70 6f 72 74 20 sed, to support
d9e60 2a 2f 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 */. pNew->loc
d9e70 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 28 76 kingContext = (v
d9e80 6f 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 3b 0a oid*)zFilename;.
d9e90 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 69 66 #endif. }.. if
d9ea0 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 ( pLockingStyle
d9eb0 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f == &posixIoMetho
d9ec0 64 73 20 29 7b 0a 20 20 20 20 75 6e 69 78 45 6e ds ){. unixEn
d9ed0 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 terMutex();.
d9ee0 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 rc = findLockInf
d9ef0 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 o(pNew, &pNew->p
d9f00 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 Lock, &pNew->pOp
d9f10 65 6e 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 en);. unixLea
d9f20 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 0a veMutex();. }..
d9f30 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
d9f40 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
d9f50 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 && defined(__APP
d9f60 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 66 28 LE__). else if(
d9f70 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d pLockingStyle =
d9f80 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 = &afpIoMethods
d9f90 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 6c 6f ){. /* AFP lo
d9fa0 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 cking uses the f
d9fb0 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e ile path so it n
d9fc0 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 eeds to be inclu
d9fd0 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 ded in. ** th
d9fe0 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 e afpLockingCont
d9ff0 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ext.. */.
da000 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
da010 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 4e 65 t *pCtx;. pNe
da020 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 w->lockingContex
da030 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c 69 74 t = pCtx = sqlit
da040 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f e3_malloc( sizeo
da050 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 20 20 f(*pCtx) );.
da060 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20 if( pCtx==0 ){.
da070 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
da080 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 _NOMEM;. }els
da090 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a 20 e{. /* NB:
da0a0 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 73 zFilename exists
da0b0 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 6c and remains val
da0c0 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 6c id until the fil
da0d0 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 20 e is closed.
da0e0 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74 ** according t
da0f0 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 31 o requirement F1
da100 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 20 1141. So we do
da110 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 not need to make
da120 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 a. ** copy
da130 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 of the filename
da140 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78 2d . */. pCtx-
da150 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65 6e >dbPath = zFilen
da160 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 6e 64 ame;. srand
da170 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 20 75 omdev();. u
da180 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
da190 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e 64 . rc = find
da1a0 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 4e LockInfo(pNew, N
da1b0 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65 ULL, &pNew->pOpe
da1c0 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 4c 65 n);. unixLe
da1d0 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20 20 aveMutex();
da1e0 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 . }. }.#e
da1f0 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 66 28 ndif.. else if(
da200 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d pLockingStyle =
da210 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 = &dotlockIoMeth
da220 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f ods ){. /* Do
da230 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 tfile locking us
da240 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 es the file path
da250 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 so it needs to
da260 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 be included in.
da270 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f 63 ** the dotloc
da280 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 kLockingContext
da290 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 72 . */. char
da2a0 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20 *zLockFile;.
da2b0 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b 0a int nFilename;.
da2c0 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 3d 20 nFilename =
da2d0 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 69 6c (int)strlen(zFil
da2e0 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 20 20 ename) + 6;.
da2f0 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 zLockFile = (cha
da300 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c r *)sqlite3_mall
da310 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 oc(nFilename);.
da320 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 6c 65 if( zLockFile
da330 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ==0 ){. rc
da340 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
da350 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
da360 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
da370 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a 4c 6f f(nFilename, zLo
da380 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 4f 54 ckFile, "%s" DOT
da390 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a 46 69 LOCK_SUFFIX, zFi
da3a0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 lename);. }.
da3b0 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 pNew->locking
da3c0 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 6b 46 Context = zLockF
da3d0 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 4f 53 ile;. }..#if OS
da3e0 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 65 20 _VXWORKS. else
da3f0 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c if( pLockingStyl
da400 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 68 6f e == &semIoMetho
da410 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 61 6d ds ){. /* Nam
da420 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 ed semaphore loc
da430 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69 king uses the fi
da440 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 le path so it ne
da450 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a eds to be. **
da460 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 included in the
da470 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 semLockingConte
da480 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 6e xt. */. un
da490 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
da4a0 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 rc = findLoc
da4b0 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65 kInfo(pNew, &pNe
da4c0 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d w->pLock, &pNew-
da4d0 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 66 28 >pOpen);. if(
da4e0 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 (rc==SQLITE_OK)
da4f0 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 65 6e && (pNew->pOpen
da500 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 29 7b ->pSem==NULL) ){
da510 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53 65 . char *zSe
da520 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e 70 4f mName = pNew->pO
da530 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b 0a 20 pen->aSemName;.
da540 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 int n;.
da550 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
da560 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c tf(MAX_PATHNAME,
da570 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 25 73 2e 73 zSemName, "%s.s
da580 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 em",.
da590 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 pNew
da5a0 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 ->pId->zCanonica
da5b0 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 66 6f lName);. fo
da5c0 72 28 20 6e 3d 30 3b 20 7a 53 65 6d 4e 61 6d 65 r( n=0; zSemName
da5d0 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20 20 20 [n]; n++ ).
da5e0 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d 65 5b if( zSemName[
da5f0 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d 4e 61 n]=='/' ) zSemNa
da600 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 20 20 me[n] = '_';.
da610 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e pNew->pOpen->
da620 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 6e 28 pSem = sem_open(
da630 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 45 41 zSemName, O_CREA
da640 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20 20 20 T, 0666, 1);.
da650 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 4f 70 if( pNew->pOp
da660 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 45 4d 5f en->pSem == SEM_
da670 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 20 20 20 FAILED ){.
da680 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
da690 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 MEM;. pNe
da6a0 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 w->pOpen->aSemNa
da6b0 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 me[0] = '\0';.
da6c0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
da6d0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
da6e0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a ;. }.#endif. .
da6f0 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 72 72 6e pNew->lastErrn
da700 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 5f 56 58 o = 0;.#if OS_VX
da710 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 21 3d WORKS. if( rc!=
da720 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
da730 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e 61 6d unlink(zFilenam
da740 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65 74 65 e);. isDelete
da750 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 = 0;. }. pNew
da760 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 69 73 44 ->isDelete = isD
da770 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 0a 20 20 elete;.#endif.
da780 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
da790 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 64 69 72 K ){. if( dir
da7a0 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 64 69 fd>=0 ) close(di
da7b0 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 20 rfd); /* silent
da7c0 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c leak if fail, al
da7d0 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a ready in error *
da7e0 2f 0a 20 20 20 20 63 6c 6f 73 65 28 68 29 3b 0a /. close(h);.
da7f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 }else{. pNe
da800 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f w->pMethod = pLo
da810 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 ckingStyle;.
da820 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b OpenCounter(+1);
da830 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
da840 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
da850 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f a file descripto
da860 72 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f r to the directo
da870 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 ry containing fi
da880 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a le zFilename..**
da890 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
da8a0 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 *pFd is set to t
da8b0 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 he opened file d
da8c0 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a escriptor and.**
da8d0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
da8e0 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 turned. If an er
da8f0 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 ror occurs, eith
da900 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a er SQLITE_NOMEM.
da910 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e ** or SQLITE_CAN
da920 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 TOPEN is returne
da930 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 d and *pFd is se
da940 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 t to an undefine
da950 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a d.** value..**.*
da960 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 * If SQLITE_OK i
da970 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 s returned, the
da980 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e caller is respon
da990 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e sible for closin
da9a0 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 g.** the file de
da9b0 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 scriptor *pFd us
da9c0 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a ing close()..*/.
da9d0 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 static int openD
da9e0 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 irectory(const c
da9f0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
daa00 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 int *pFd){. int
daa10 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 ii;. int fd =
daa20 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e -1;. char zDirn
daa30 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ame[MAX_PATHNAME
daa40 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f +1];.. sqlite3_
daa50 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
daa60 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c HNAME, zDirname,
daa70 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 "%s", zFilename
daa80 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 );. for(ii=(int
daa90 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 )strlen(zDirname
daaa0 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69 72 6e ); ii>1 && zDirn
daab0 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 ame[ii]!='/'; ii
daac0 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 --);. if( ii>0
daad0 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b ){. zDirname[
daae0 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 ii] = '\0';.
daaf0 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 fd = open(zDirna
dab00 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 me, O_RDONLY|O_B
dab10 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 INARY, 0);. i
dab20 66 28 20 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 f( fd>=0 ){.#ifd
dab30 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 ef FD_CLOEXEC.
dab40 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f fcntl(fd, F_
dab50 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c SETFD, fcntl(fd,
dab60 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 F_GETFD, 0) | F
dab70 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 D_CLOEXEC);.#end
dab80 69 66 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 if. OSTRACE
dab90 33 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 3("OPENDIR %-3d
daba0 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e %s\n", fd, zDirn
dabb0 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ame);. }. }.
dabc0 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 *pFd = fd;. r
dabd0 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c eturn (fd>=0?SQL
dabe0 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 ITE_OK:SQLITE_CA
dabf0 4e 54 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a NTOPEN);.}../*.*
dac00 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f * Create a tempo
dac10 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 rary file name i
dac20 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 n zBuf. zBuf mu
dac30 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a st be allocated.
dac40 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ** by the callin
dac50 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 g process and mu
dac60 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 st be big enough
dac70 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 to hold at leas
dac80 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 t.** pVfs->mxPat
dac90 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a hname bytes..*/.
daca0 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 static int getTe
dacb0 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c mpname(int nBuf,
dacc0 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 char *zBuf){.
dacd0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
dace0 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a r *azDirs[] = {.
dacf0 20 20 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 0,. 0,.
dad00 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c "/var/tmp",
dad10 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 . "/usr/tmp"
dad20 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 ,. "/tmp",.
dad30 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 ".",. };.
dad40 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
dad50 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72 igned char zChar
dad60 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 s[] =. "abcde
dad70 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 fghijklmnopqrstu
dad80 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 vwxyz". "ABCD
dad90 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 EFGHIJKLMNOPQRST
dada0 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 UVWXYZ". "012
dadb0 33 34 35 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 3456789";. unsi
dadc0 67 6e 65 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 gned int i, j;.
dadd0 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 struct stat buf
dade0 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
dadf0 7a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f zDir = ".";.. /
dae00 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 * It's odd to si
dae10 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 mulate an io-err
dae20 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 or here, but rea
dae30 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 lly this is just
dae40 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 . ** using the
dae50 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 io-error infrast
dae60 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 ructure to test
dae70 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 that SQLite hand
dae80 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 les this. ** fu
dae90 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 nction failing.
daea0 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 . */. Simulate
daeb0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
daec0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a SQLITE_IOERR );.
daed0 0a 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 . azDirs[0] = s
daee0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 qlite3_temp_dire
daef0 63 74 6f 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c ctory;. if (NUL
daf00 4c 20 3d 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 L == azDirs[1])
daf10 7b 0a 20 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 {. azDirs[1]
daf20 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44 49 52 = getenv("TMPDIR
daf30 22 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 ");. }. . for
daf40 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 (i=0; i<sizeof(a
daf50 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a zDirs)/sizeof(az
daf60 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a Dirs[0]); i++){.
daf70 20 20 20 20 69 66 28 20 61 7a 44 69 72 73 5b 69 if( azDirs[i
daf80 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b ]==0 ) continue;
daf90 0a 20 20 20 20 69 66 28 20 73 74 61 74 28 61 7a . if( stat(az
dafa0 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 Dirs[i], &buf) )
dafb0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 continue;. i
dafc0 66 28 20 21 53 5f 49 53 44 49 52 28 62 75 66 2e f( !S_ISDIR(buf.
dafd0 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 st_mode) ) conti
dafe0 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61 63 63 nue;. if( acc
daff0 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 ess(azDirs[i], 0
db000 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 7) ) continue;.
db010 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 zDir = azDirs
db020 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a [i];. break;.
db030 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
db040 74 68 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 that the output
db050 62 75 66 66 65 72 20 69 73 20 6c 61 72 67 65 20 buffer is large
db060 65 6e 6f 75 67 68 20 66 6f 72 20 74 68 65 20 74 enough for the t
db070 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 emporary file .
db080 20 2a 2a 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 ** name. If it
db090 69 73 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 is not, return S
db0a0 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a QLITE_ERROR.. *
db0b0 2f 0a 20 20 69 66 28 20 28 73 74 72 6c 65 6e 28 /. if( (strlen(
db0c0 7a 44 69 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 zDir) + strlen(S
db0d0 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f QLITE_TEMP_FILE_
db0e0 50 52 45 46 49 58 29 20 2b 20 31 37 29 20 3e 3d PREFIX) + 17) >=
db0f0 20 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b (size_t)nBuf ){
db100 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
db110 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 TE_ERROR;. }..
db120 20 64 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 do{. sqlite3
db130 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 _snprintf(nBuf-1
db140 37 2c 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 7, zBuf, "%s/"SQ
db150 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 LITE_TEMP_FILE_P
db160 52 45 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 REFIX, zDir);.
db170 20 20 6a 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 j = (int)strle
db180 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c n(zBuf);. sql
db190 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
db1a0 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 15, &zBuf[j]);.
db1b0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 for(i=0; i<15
db1c0 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 ; i++, j++){.
db1d0 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 zBuf[j] = (ch
db1e0 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 ar)zChars[ ((uns
db1f0 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b igned char)zBuf[
db200 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 j])%(sizeof(zCha
db210 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a rs)-1) ];. }.
db220 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b zBuf[j] = 0;
db230 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73 . }while( acces
db240 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a s(zBuf,0)==0 );.
db250 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
db260 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 OK;.}..#if SQLIT
db270 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
db280 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
db290 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a d(__APPLE__)./*.
db2a0 2a 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 ** Routine to tr
db2b0 61 6e 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 ansform a unixFi
db2c0 6c 65 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d le into a proxy-
db2d0 6c 6f 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 locking unixFile
db2e0 2e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 ..** Implementat
db2f0 69 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79 ion in the proxy
db300 2d 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 -lock division,
db310 62 75 74 20 75 73 65 64 20 62 79 20 75 6e 69 78 but used by unix
db320 4f 70 65 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c Open().** if SQL
db330 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 ITE_PREFER_PROXY
db340 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69 _LOCKING is defi
db350 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
db360 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 nt proxyTransfor
db370 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 mUnixFile(unixFi
db380 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a le*, const char*
db390 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a );.#endif.../*.*
db3a0 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 * Open the file
db3b0 7a 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 zPath..** .** Pr
db3c0 65 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 eviously, the SQ
db3d0 4c 69 74 65 20 4f 53 20 6c 61 79 65 72 20 75 73 Lite OS layer us
db3e0 65 64 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f ed three functio
db3f0 6e 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 ns in place of t
db400 68 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a his.** one:.**.*
db410 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f * sqlite3OsO
db420 70 65 6e 52 65 61 64 57 72 69 74 65 28 29 3b 0a penReadWrite();.
db430 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
db440 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a OpenReadOnly();.
db450 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
db460 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 3b OpenExclusive();
db470 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c .**.** These cal
db480 6c 73 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f ls correspond to
db490 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 the following c
db4a0 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66 ombinations of f
db4b0 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 lags:.**.**
db4c0 52 65 61 64 57 72 69 74 65 28 29 20 2d 3e 20 20 ReadWrite() ->
db4d0 20 20 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 (READWRITE |
db4e0 43 52 45 41 54 45 29 0a 2a 2a 20 20 20 20 20 52 CREATE).** R
db4f0 65 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20 eadOnly() ->
db500 20 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a (READONLY) .**
db510 20 20 20 20 20 4f 70 65 6e 45 78 63 6c 75 73 69 OpenExclusi
db520 76 65 28 29 20 2d 3e 20 28 52 45 41 44 57 52 49 ve() -> (READWRI
db530 54 45 20 7c 20 43 52 45 41 54 45 20 7c 20 45 58 TE | CREATE | EX
db540 43 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54 CLUSIVE).**.** T
db550 68 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75 he old OpenExclu
db560 73 69 76 65 28 29 20 61 63 63 65 70 74 65 64 20 sive() accepted
db570 61 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 a boolean argume
db580 6e 74 20 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20 nt - "delFlag".
db590 49 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 If.** true, the
db5a0 66 69 6c 65 20 77 61 73 20 63 6f 6e 66 69 67 75 file was configu
db5b0 72 65 64 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 red to be automa
db5c0 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 tically deleted
db5d0 77 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65 when the.** file
db5e0 20 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 handle closed.
db5f0 54 6f 20 61 63 68 69 65 76 65 20 74 68 65 20 73 To achieve the s
db600 61 6d 65 20 65 66 66 65 63 74 20 75 73 69 6e 67 ame effect using
db610 20 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e this new .** in
db620 74 65 72 66 61 63 65 2c 20 61 64 64 20 74 68 65 terface, add the
db630 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 DELETEONCLOSE f
db640 6c 61 67 20 74 6f 20 74 68 6f 73 65 20 73 70 65 lag to those spe
db650 63 69 66 69 65 64 20 61 62 6f 76 65 20 66 6f 72 cified above for
db660 20 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69 .** OpenExclusi
db670 76 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ve()..*/.static
db680 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20 int unixOpen(.
db690 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
db6a0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
db6b0 54 68 65 20 56 46 53 20 66 6f 72 20 77 68 69 63 The VFS for whic
db6c0 68 20 74 68 69 73 20 69 73 20 74 68 65 20 78 4f h this is the xO
db6d0 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 pen method */.
db6e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
db6f0 68 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 h, /*
db700 50 61 74 68 6e 61 6d 65 20 6f 66 20 66 69 6c 65 Pathname of file
db710 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f to be opened */
db720 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
db730 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 *pFile,
db740 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 /* The file desc
db750 72 69 70 74 6f 72 20 74 6f 20 62 65 20 66 69 6c riptor to be fil
db760 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 led in */. int
db770 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
db780 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 /* Inpu
db790 74 20 66 6c 61 67 73 20 74 6f 20 63 6f 6e 74 72 t flags to contr
db7a0 6f 6c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 2a ol the opening *
db7b0 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 /. int *pOutFla
db7c0 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 gs
db7d0 20 2f 2a 20 4f 75 74 70 75 74 20 66 6c 61 67 73 /* Output flags
db7e0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 51 4c returned to SQL
db7f0 69 74 65 20 63 6f 72 65 20 2a 2f 0a 29 7b 0a 20 ite core */.){.
db800 20 69 6e 74 20 66 64 20 3d 20 30 3b 20 20 20 20 int fd = 0;
db810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
db820 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 /* File descript
db830 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 6f or returned by o
db840 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 64 pen() */. int d
db850 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 irfd = -1;
db860 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 /* Dir
db870 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 ectory file desc
db880 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 riptor */. int
db890 6f 70 65 6e 46 6c 61 67 73 20 3d 20 30 3b 20 20 openFlags = 0;
db8a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
db8b0 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f 20 6f ags to pass to o
db8c0 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 pen() */. int e
db8d0 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46 Type = flags&0xF
db8e0 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70 FFFFF00; /* Typ
db8f0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65 e of file to ope
db900 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 n */. int noLoc
db910 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
db920 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
db930 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 70 72 omit locking pr
db940 69 6d 69 74 69 76 65 73 20 2a 2f 0a 20 20 69 6e imitives */. in
db950 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
db960 3b 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 6c 75 ;.. int isExclu
db970 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 20 26 sive = (flags &
db980 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
db990 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 20 69 LUSIVE);. int i
db9a0 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 28 66 sDelete = (f
db9b0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
db9c0 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
db9d0 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 );. int isCreat
db9e0 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26 e = (flags &
db9f0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 SQLITE_OPEN_CRE
dba00 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 ATE);. int isRe
dba10 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c 61 67 adonly = (flag
dba20 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
dba30 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 READONLY);. int
dba40 20 69 73 52 65 61 64 57 72 69 74 65 20 20 3d 20 isReadWrite =
dba50 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f (flags & SQLITE_
dba60 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 3b OPEN_READWRITE);
dba70 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69 .. /* If creati
dba80 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20 6d ng a master or m
dba90 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61 6c ain-file journal
dbaa0 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
dbab0 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 will open. ** a
dbac0 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 file-descriptor
dbad0 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 on the director
dbae0 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73 74 y too. The first
dbaf0 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28 29 time unixSync()
dbb00 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 . ** is called
dbb10 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 66 69 the directory fi
dbb20 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 69 le descriptor wi
dbb30 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64 20 ll be fsync()ed
dbb40 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a 20 20 and close()d..
dbb50 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e 44 */. int isOpenD
dbb60 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 43 72 irectory = (isCr
dbb70 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 20 28 eate && . (
dbb80 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
dbb90 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 EN_MASTER_JOURNA
dbba0 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 L || eType==SQLI
dbbb0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
dbbc0 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 2f 2a RNAL). );.. /*
dbbd0 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 50 61 If argument zPa
dbbe0 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 th is a NULL poi
dbbf0 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 74 nter, this funct
dbc00 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 20 ion is required
dbc10 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 74 to open. ** a t
dbc20 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 55 emporary file. U
dbc30 73 65 20 74 68 69 73 20 62 75 66 66 65 72 20 74 se this buffer t
dbc40 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 o store the file
dbc50 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f 0a 20 name in.. */.
dbc60 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d char zTmpname[M
dbc70 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 0a AX_PATHNAME+1];.
dbc80 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
dbc90 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a 20 20 ame = zPath;..
dbca0 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 6f 6c /* Check the fol
dbcb0 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 lowing statement
dbcc0 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 20 2a s are true: . *
dbcd0 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 78 61 *. ** (a) Exa
dbce0 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 ctly one of the
dbcf0 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 52 45 READWRITE and RE
dbd00 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d 75 73 ADONLY flags mus
dbd10 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 0a 20 t be set, and .
dbd20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 52 45 ** (b) if CRE
dbd30 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e ATE is set, then
dbd40 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 74 20 READWRITE must
dbd50 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 6e 64 also be set, and
dbd60 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 20 45 . ** (c) if E
dbd70 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 74 2c XCLUSIVE is set,
dbd80 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 73 then CREATE mus
dbd90 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 t also be set..
dbda0 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 45 4c ** (d) if DEL
dbdb0 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 73 65 ETEONCLOSE is se
dbdc0 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d t, then CREATE m
dbdd0 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e ust also be set.
dbde0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 28 . */. assert((
dbdf0 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c 7c isReadonly==0 ||
dbe00 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d 30 29 isReadWrite==0)
dbe10 20 26 26 20 28 69 73 52 65 61 64 57 72 69 74 65 && (isReadWrite
dbe20 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 29 29 || isReadonly))
dbe30 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 72 65 ;. assert(isCre
dbe40 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 ate==0 || isRead
dbe50 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 72 74 Write);. assert
dbe60 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 (isExclusive==0
dbe70 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 20 20 || isCreate);.
dbe80 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 3d assert(isDelete=
dbe90 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b =0 || isCreate);
dbea0 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 .. /* The main
dbeb0 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c DB, main journal
dbec0 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a 6f 75 , and master jou
dbed0 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 20 61 rnal are never a
dbee0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 20 2a utomatically. *
dbef0 2a 20 64 65 6c 65 74 65 64 0a 20 20 2a 2f 0a 20 * deleted. */.
dbf00 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d assert( eType!=
dbf10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
dbf20 5f 44 42 20 7c 7c 20 21 69 73 44 65 6c 65 74 65 _DB || !isDelete
dbf30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 54 );. assert( eT
dbf40 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype!=SQLITE_OPEN
dbf50 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c _MAIN_JOURNAL ||
dbf60 20 21 69 73 44 65 6c 65 74 65 20 29 3b 0a 20 20 !isDelete );.
dbf70 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 assert( eType!=S
dbf80 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 QLITE_OPEN_MASTE
dbf90 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21 69 73 R_JOURNAL || !is
dbfa0 44 65 6c 65 74 65 20 29 3b 0a 0a 20 20 2f 2a 20 Delete );.. /*
dbfb0 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 Assert that the
dbfc0 75 70 70 65 72 20 6c 61 79 65 72 20 68 61 73 20 upper layer has
dbfd0 73 65 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 22 set one of the "
dbfe0 66 69 6c 65 2d 74 79 70 65 22 20 66 6c 61 67 73 file-type" flags
dbff0 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 65 . */. assert( e
dc000 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
dc010 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 20 7c N_MAIN_DB |
dc020 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f | eType==SQLITE_
dc030 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 0a 20 20 OPEN_TEMP_DB .
dc040 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 || eType==S
dc050 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
dc060 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 JOURNAL || eType
dc070 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 ==SQLITE_OPEN_TE
dc080 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 MP_JOURNAL .
dc090 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c || eType==SQL
dc0a0 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 ITE_OPEN_SUBJOUR
dc0b0 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d NAL || eType==
dc0c0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 SQLITE_OPEN_MAST
dc0d0 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 ER_JOURNAL .
dc0e0 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c || eType==SQL
dc0f0 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 ITE_OPEN_TRANSIE
dc100 4e 54 5f 44 42 0a 20 20 29 3b 0a 0a 20 20 6d 65 NT_DB. );.. me
dc110 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 mset(pFile, 0, s
dc120 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 izeof(unixFile))
dc130 3b 0a 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 ;.. if( !zName
dc140 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 69 73 ){. assert(is
dc150 44 65 6c 65 74 65 20 26 26 20 21 69 73 4f 70 65 Delete && !isOpe
dc160 6e 44 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 nDirectory);.
dc170 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d rc = getTempnam
dc180 65 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 e(MAX_PATHNAME+1
dc190 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 , zTmpname);.
dc1a0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
dc1b0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
dc1c0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
dc1d0 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d zName = zTmpnam
dc1e0 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 e;. }.. if( is
dc1f0 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f 70 65 6e Readonly ) open
dc200 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c Flags |= O_RDONL
dc210 59 3b 0a 20 20 69 66 28 20 69 73 52 65 61 64 57 Y;. if( isReadW
dc220 72 69 74 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 rite ) openFlags
dc230 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20 69 66 |= O_RDWR;. if
dc240 28 20 69 73 43 72 65 61 74 65 20 29 20 20 20 20 ( isCreate )
dc250 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 43 openFlags |= O_C
dc260 52 45 41 54 3b 0a 20 20 69 66 28 20 69 73 45 78 REAT;. if( isEx
dc270 63 6c 75 73 69 76 65 20 29 20 6f 70 65 6e 46 6c clusive ) openFl
dc280 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c 7c 4f ags |= (O_EXCL|O
dc290 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 20 6f 70 _NOFOLLOW);. op
dc2a0 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 4c 41 enFlags |= (O_LA
dc2b0 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41 52 59 RGEFILE|O_BINARY
dc2c0 29 3b 0a 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 );.. fd = open(
dc2d0 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 zName, openFlags
dc2e0 2c 20 69 73 44 65 6c 65 74 65 3f 30 36 30 30 3a , isDelete?0600:
dc2f0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
dc300 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 ILE_PERMISSIONS)
dc310 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4f 50 ;. OSTRACE4("OP
dc320 45 4e 58 20 20 20 25 2d 33 64 20 25 73 20 30 25 ENX %-3d %s 0%
dc330 6f 5c 6e 22 2c 20 66 64 2c 20 7a 4e 61 6d 65 2c o\n", fd, zName,
dc340 20 6f 70 65 6e 46 6c 61 67 73 29 3b 0a 20 20 69 openFlags);. i
dc350 66 28 20 66 64 3c 30 20 26 26 20 65 72 72 6e 6f f( fd<0 && errno
dc360 21 3d 45 49 53 44 49 52 20 26 26 20 69 73 52 65 !=EISDIR && isRe
dc370 61 64 57 72 69 74 65 20 26 26 20 21 69 73 45 78 adWrite && !isEx
dc380 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 2f clusive ){. /
dc390 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e * Failed to open
dc3a0 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 65 the file for re
dc3b0 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e ad/write access.
dc3c0 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 Try read-only.
dc3d0 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 26 3d 20 */. flags &=
dc3e0 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 ~(SQLITE_OPEN_RE
dc3f0 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f ADWRITE|SQLITE_O
dc400 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 20 PEN_CREATE);.
dc410 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 flags |= SQLITE
dc420 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a _OPEN_READONLY;.
dc430 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78 4f return unixO
dc440 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c pen(pVfs, zPath,
dc450 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 pFile, flags, p
dc460 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 OutFlags);. }.
dc470 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 if( fd<0 ){.
dc480 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
dc490 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 69 ANTOPEN;. }. i
dc4a0 66 28 20 69 73 44 65 6c 65 74 65 20 29 7b 0a 23 f( isDelete ){.#
dc4b0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
dc4c0 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61 6d 65 3b zPath = zName;
dc4d0 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e 6c 69 6e .#else. unlin
dc4e0 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 k(zName);.#endif
dc4f0 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f . }.#if SQLITE_
dc500 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
dc510 54 59 4c 45 0a 20 20 65 6c 73 65 7b 0a 20 20 20 TYLE. else{.
dc520 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 ((unixFile*)pFi
dc530 6c 65 29 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d le)->openFlags =
dc540 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 20 7d 0a openFlags;. }.
dc550 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4f 75 #endif. if( pOu
dc560 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 tFlags ){. *p
dc570 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 OutFlags = flags
dc580 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 4e ;. }..#ifndef N
dc590 44 45 42 55 47 0a 20 20 69 66 28 20 28 66 6c 61 DEBUG. if( (fla
dc5a0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
dc5b0 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20 29 7b 0a _MAIN_DB)!=0 ){.
dc5c0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
dc5d0 70 46 69 6c 65 29 2d 3e 69 73 4c 6f 63 6b 61 62 pFile)->isLockab
dc5e0 6c 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 le = 1;. }.#end
dc5f0 69 66 0a 0a 20 20 61 73 73 65 72 74 28 66 64 21 if.. assert(fd!
dc600 3d 30 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 =0);. if( isOpe
dc610 6e 44 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 nDirectory ){.
dc620 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 rc = openDirec
dc630 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72 tory(zPath, &dir
dc640 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 fd);. if( rc!
dc650 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
dc660 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f close(fd); /
dc670 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 * silently leak
dc680 69 66 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 79 if fail, already
dc690 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 in error */.
dc6a0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
dc6b0 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 }. }..#ifdef
dc6c0 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e FD_CLOEXEC. fcn
dc6d0 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 tl(fd, F_SETFD,
dc6e0 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 fcntl(fd, F_GETF
dc6f0 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 D, 0) | FD_CLOEX
dc700 45 43 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e EC);.#endif.. n
dc710 6f 4c 6f 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 oLock = eType!=S
dc720 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
dc730 44 42 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f DB;..#if SQLITE_
dc740 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 PREFER_PROXY_LOC
dc750 4b 49 4e 47 0a 20 20 69 66 28 20 7a 50 61 74 68 KING. if( zPath
dc760 21 3d 4e 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 !=NULL && !noLoc
dc770 6b 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 65 k ){. char *e
dc780 6e 76 66 6f 72 63 65 20 3d 20 67 65 74 65 6e 76 nvforce = getenv
dc790 28 22 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 ("SQLITE_FORCE_P
dc7a0 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a ROXY_LOCKING");.
dc7b0 20 20 20 20 69 6e 74 20 75 73 65 50 72 6f 78 79 int useProxy
dc7c0 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 = 0;.. /* SQ
dc7d0 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 LITE_FORCE_PROXY
dc7e0 5f 4c 4f 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e _LOCKING==1 mean
dc7f0 73 20 66 6f 72 63 65 20 61 6c 77 61 79 73 20 75 s force always u
dc800 73 65 20 70 72 6f 78 79 2c 20 0a 20 20 20 20 2a se proxy, . *
dc810 2a 20 30 20 6d 65 61 6e 73 20 6e 65 76 65 72 20 * 0 means never
dc820 75 73 65 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 use proxy, NULL
dc830 6d 65 61 6e 73 20 75 73 65 20 70 72 6f 78 79 20 means use proxy
dc840 66 6f 72 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 for non-local fi
dc850 6c 65 73 20 6f 6e 6c 79 0a 20 20 20 20 2a 2f 0a les only. */.
dc860 20 20 20 20 69 66 28 20 65 6e 76 66 6f 72 63 65 if( envforce
dc870 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 !=NULL ){.
dc880 75 73 65 50 72 6f 78 79 20 3d 20 61 74 6f 69 28 useProxy = atoi(
dc890 65 6e 76 66 6f 72 63 65 29 3e 30 3b 0a 20 20 20 envforce)>0;.
dc8a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 }else{. st
dc8b0 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49 6e ruct statfs fsIn
dc8c0 66 6f 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 73 fo;.. if( s
dc8d0 74 61 74 66 73 28 7a 50 61 74 68 2c 20 26 66 73 tatfs(zPath, &fs
dc8e0 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 7b 0a 09 Info) == -1 ){..
dc8f0 09 09 09 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 ...((unixFile*)p
dc900 46 69 6c 65 29 2d 3e 6c 61 73 74 45 72 72 6e 6f File)->lastErrno
dc910 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
dc920 20 20 69 66 28 20 64 69 72 66 64 3e 3d 30 20 29 if( dirfd>=0 )
dc930 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f close(dirfd); /
dc940 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 * silently leak
dc950 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f if fail, in erro
dc960 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6c 6f r */. clo
dc970 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e se(fd); /* silen
dc980 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c tly leak if fail
dc990 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 , in error */.
dc9a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
dc9b0 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 ITE_IOERR_ACCESS
dc9c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
dc9d0 75 73 65 50 72 6f 78 79 20 3d 20 21 28 66 73 49 useProxy = !(fsI
dc9e0 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f nfo.f_flags&MNT_
dc9f0 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 LOCAL);. }.
dca00 20 20 69 66 28 20 75 73 65 50 72 6f 78 79 20 29 if( useProxy )
dca10 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6c {. rc = fil
dca20 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 lInUnixFile(pVfs
dca30 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69 , fd, dirfd, pFi
dca40 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 le, zPath, noLoc
dca50 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a 20 20 k, isDelete);.
dca60 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
dca70 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
dca80 20 72 63 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 rc = proxyTrans
dca90 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 28 75 6e formUnixFile((un
dcaa0 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 2c 20 22 ixFile*)pFile, "
dcab0 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 20 20 20 20 :auto:");.
dcac0 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 }. return r
dcad0 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e c;. }. }.#en
dcae0 64 69 66 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 dif. . return
dcaf0 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 fillInUnixFile(p
dcb00 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 Vfs, fd, dirfd,
dcb10 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f pFile, zPath, no
dcb20 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b Lock, isDelete);
dcb30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
dcb40 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61 the file at zPa
dcb50 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 79 th. If the dirSy
dcb60 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 nc argument is t
dcb70 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a 20 rue, fsync().**
dcb80 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 66 the directory af
dcb90 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 ter deleting the
dcba0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
dcbb0 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 28 int unixDelete(
dcbc0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
dcbd0 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a 20 NotUsed, /*
dcbe0 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 VFS containing t
dcbf0 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c 65 his as the xDele
dcc00 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 te method */. c
dcc10 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
dcc20 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 , /* Name
dcc30 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 64 of file to be d
dcc40 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 eleted */. int
dcc50 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20 20 dirSync
dcc60 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 /* If true
dcc70 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63 74 , fsync() direct
dcc80 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 ory after deleti
dcc90 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 ng file */.){.
dcca0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
dccb0 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 OK;. UNUSED_PAR
dccc0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
dccd0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
dcce0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 or(return SQLITE
dccf0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a _IOERR_DELETE);.
dcd00 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b unlink(zPath);
dcd10 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
dcd20 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a DISABLE_DIRSYNC.
dcd30 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b if( dirSync ){
dcd40 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20 . int fd;.
dcd50 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 rc = openDirect
dcd60 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b ory(zPath, &fd);
dcd70 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
dcd80 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f 53 ITE_OK ){.#if OS
dcd90 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69 _VXWORKS. i
dcda0 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31 f( fsync(fd)==-1
dcdb0 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69 ).#else. i
dcdc0 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23 f( fsync(fd) ).#
dcdd0 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 endif. {.
dcde0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
dcdf0 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e E_IOERR_DIR_FSYN
dce00 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 C;. }.
dce10 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26 26 if( close(fd)&&
dce20 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 !rc ){. r
dce30 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
dce40 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 _DIR_CLOSE;.
dce50 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 }. }. }.#e
dce60 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 ndif. return rc
dce70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 ;.}../*.** Test
dce80 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 the existance of
dce90 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69 or access permi
dcea0 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a ssions of file z
dceb0 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73 Path. The.** tes
dcec0 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65 t performed depe
dced0 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 nds on the value
dcee0 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a of flags:.**.**
dcef0 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 SQLITE_ACCE
dcf00 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72 SS_EXISTS: Retur
dcf10 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 n 1 if the file
dcf20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51 exists.** SQ
dcf30 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
dcf40 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20 WRITE: Return 1
dcf50 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 if the file is r
dcf60 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65 ead and writable
dcf70 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f ..** SQLITE_
dcf80 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a ACCESS_READONLY:
dcf90 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
dcfa0 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c file is readabl
dcfb0 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 e..**.** Otherwi
dcfc0 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a se return 0..*/.
dcfd0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41 static int unixA
dcfe0 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 ccess(. sqlite3
dcff0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 _vfs *NotUsed,
dd000 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e 74 /* The VFS cont
dd010 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63 63 aining this xAcc
dd020 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ess method */.
dd030 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
dd040 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20 h, /* Path
dd050 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 65 of the file to e
dd060 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 xamine */. int
dd070 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
dd080 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 77 /* What do w
dd090 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e 20 e want to learn
dd0a0 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68 20 about the zPath
dd0b0 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 2a file? */. int *
dd0c0 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 pResOut
dd0d0 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 /* Write resu
dd0e0 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 20 lt boolean here
dd0f0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64 */.){. int amod
dd100 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f e = 0;. UNUSED_
dd110 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
dd120 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f d);. SimulateIO
dd130 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
dd140 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 LITE_IOERR_ACCES
dd150 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 S; );. switch(
dd160 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 flags ){. cas
dd170 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
dd180 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d EXISTS:. am
dd190 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20 ode = F_OK;.
dd1a0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
dd1b0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
dd1c0 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 READWRITE:.
dd1d0 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f amode = W_OK|R_
dd1e0 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b OK;. break;
dd1f0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
dd200 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 _ACCESS_READ:.
dd210 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b amode = R_OK
dd220 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a ;. break;..
dd230 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
dd240 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 assert(!"Inva
dd250 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 lid flags argume
dd260 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 nt");. }. *pRe
dd270 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a sOut = (access(z
dd280 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29 Path, amode)==0)
dd290 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
dd2a0 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 E_OK;.}.../*.**
dd2b0 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 Turn a relative
dd2c0 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 pathname into a
dd2d0 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54 full pathname. T
dd2e0 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 he relative path
dd2f0 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73 .** is stored as
dd300 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 a nul-terminate
dd310 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 d string in the
dd320 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 buffer pointed t
dd330 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a o by.** zPath. .
dd340 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74 **.** zOut point
dd350 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 s to a buffer of
dd360 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65 at least sqlite
dd370 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 3_vfs.mxPathname
dd380 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74 bytes .** (in t
dd390 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41 his case, MAX_PA
dd3a0 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54 THNAME bytes). T
dd3b0 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20 he full-path is
dd3c0 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68 written to.** th
dd3d0 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 65 is buffer before
dd3e0 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 returning..*/.s
dd3f0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75 tatic int unixFu
dd400 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 llPathname(. sq
dd410 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
dd420 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
dd430 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 ointer to vfs ob
dd440 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ject */. const
dd450 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 char *zPath,
dd460 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 /* Possi
dd470 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 bly relative inp
dd480 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 ut path */. int
dd490 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 nOut,
dd4a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
dd4b0 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
dd4c0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
dd4d0 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20 char *zOut
dd4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd4f0 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 /* Output buffer
dd500 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27 */.){.. /* It'
dd510 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 s odd to simulat
dd520 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 e an io-error he
dd530 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 re, but really t
dd540 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a his is just. **
dd550 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 using the io-er
dd560 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 ror infrastructu
dd570 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 re to test that
dd580 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 SQLite handles t
dd590 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
dd5a0 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20 n failing. This
dd5b0 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66 function could f
dd5c0 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d ail if, for exam
dd5d0 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75 ple, the. ** cu
dd5e0 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69 rrent working di
dd5f0 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e rectory has been
dd600 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a unlinked.. */.
dd610 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
dd620 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
dd630 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73 _ERROR );.. ass
dd640 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74 ert( pVfs->mxPat
dd650 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e hname==MAX_PATHN
dd660 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f AME );. UNUSED_
dd670 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b PARAMETER(pVfs);
dd680 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d .. zOut[nOut-1]
dd690 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 7a = '\0';. if( z
dd6a0 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b 0a Path[0]=='/' ){.
dd6b0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
dd6c0 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 2c intf(nOut, zOut,
dd6d0 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 "%s", zPath);.
dd6e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
dd6f0 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67 65 nCwd;. if( ge
dd700 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d tcwd(zOut, nOut-
dd710 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 1)==0 ){. r
dd720 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
dd730 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 TOPEN;. }.
dd740 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74 72 nCwd = (int)str
dd750 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 len(zOut);. s
dd760 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
dd770 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 nOut-nCwd, &zOut
dd780 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a [nCwd], "/%s", z
dd790 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Path);. }. ret
dd7a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
dd7b0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
dd7c0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
dd7d0 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 NSION./*.** Inte
dd7e0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 rfaces for openi
dd7f0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 ng a shared libr
dd800 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 ary, finding ent
dd810 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 ry points.** wit
dd820 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c hin the shared l
dd830 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 ibrary, and clos
dd840 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c ing the shared l
dd850 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c ibrary..*/.#incl
dd860 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 74 ude <dlfcn.h>.st
dd870 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 44 atic void *unixD
dd880 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 lOpen(sqlite3_vf
dd890 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e 73 s *NotUsed, cons
dd8a0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
dd8b0 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 e){. UNUSED_PAR
dd8c0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
dd8d0 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 6e . return dlopen
dd8e0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c 44 (zFilename, RTLD
dd8f0 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f 42 _NOW | RTLD_GLOB
dd900 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 AL);.}../*.** SQ
dd910 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 Lite calls this
dd920 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 61 function immedia
dd930 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61 6c tely after a cal
dd940 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 29 l to unixDlSym()
dd950 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 65 or.** unixDlOpe
dd960 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75 72 n() fails (retur
dd970 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 ns a null pointe
dd980 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 65 r). If a more de
dd990 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a 20 tailed error.**
dd9a0 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 6c message is avail
dd9b0 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69 74 able, it is writ
dd9c0 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e 20 ten to zBufOut.
dd9d0 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 If no error mess
dd9e0 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 age.** is availa
dd9f0 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 20 ble, zBufOut is
dda00 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 20 left unmodified
dda10 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73 20 and SQLite uses
dda20 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 72 a default.** err
dda30 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73 or message..*/.s
dda40 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44 tatic void unixD
dda50 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 lError(sqlite3_v
dda60 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 fs *NotUsed, int
dda70 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
dda80 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a fOut){. char *z
dda90 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 Err;. UNUSED_PA
ddaa0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
ddab0 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 ;. unixEnterMut
ddac0 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 ex();. zErr = d
ddad0 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 lerror();. if(
ddae0 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 zErr ){. sqli
ddaf0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
ddb00 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 f, zBufOut, "%s"
ddb10 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75 , zErr);. }. u
ddb20 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
ddb30 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 .}.static void (
ddb40 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 *unixDlSym(sqlit
ddb50 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
ddb60 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 void *p, const
ddb70 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 char*zSym))(void
ddb80 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43 ){. /* . ** GC
ddb90 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 C with -pedantic
ddba0 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 -errors says tha
ddbb0 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 t C90 does not a
ddbc0 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 llow a void* to
ddbd0 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74 be. ** cast int
ddbe0 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 o a pointer to a
ddbf0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20 function. And
ddc00 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 yet the library
ddc10 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a dlsym() routine.
ddc20 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 ** returns a v
ddc30 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65 oid* which is re
ddc40 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 ally a pointer t
ddc50 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 o a function. S
ddc60 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a o how do we. **
ddc70 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74 use dlsym() wit
ddc80 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f h -pedantic-erro
ddc90 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 rs?. **. ** Va
ddca0 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69 riable x below i
ddcb0 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 s defined to be
ddcc0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 a pointer to a f
ddcd0 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20 unction taking.
ddce0 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76 ** parameters v
ddcf0 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63 oid* and const c
ddd00 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69 har* and returni
ddd10 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ng a pointer to
ddd20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a a function.. **
ddd30 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78 We initialize x
ddd40 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74 by assigning it
ddd50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
ddd60 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69 e dlsym() functi
ddd70 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61 on.. ** (That a
ddd80 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 ssignment requir
ddd90 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65 es a cast.) The
ddda0 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75 n we call the fu
dddb0 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a nction that. **
dddc0 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a x points to. .
dddd0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77 **. ** This w
ddde0 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e ork-around is un
dddf0 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63 likely to work c
dde00 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20 orrectly on any
dde10 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a system where. *
dde20 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e * you really can
dde30 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74 not cast a funct
dde40 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f ion pointer into
dde50 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65 void*. But the
dde60 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f n, on the. ** o
dde70 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d ther hand, dlsym
dde80 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b () will not work
dde90 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65 on such a syste
ddea0 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20 m either, so we
ddeb0 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 have. ** not re
ddec0 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 ally lost anythi
dded0 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 ng.. */. void
ddee0 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e (*(*x)(void*,con
ddef0 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 st char*))(void)
ddf00 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
ddf10 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
ddf20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28 x = (void(*(*)(
ddf30 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 void*,const char
ddf40 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b *))(void))dlsym;
ddf50 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70 . return (*x)(p
ddf60 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69 , zSym);.}.stati
ddf70 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f c void unixDlClo
ddf80 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a se(sqlite3_vfs *
ddf90 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 NotUsed, void *p
ddfa0 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 Handle){. UNUSE
ddfb0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
ddfc0 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 sed);. dlclose(
ddfd0 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 pHandle);.}.#els
ddfe0 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f e /* if SQLITE_O
ddff0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
de000 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a ON is defined: *
de010 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 /. #define unix
de020 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 DlOpen 0. #def
de030 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 ine unixDlError
de040 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 0. #define unix
de050 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 DlSym 0. #def
de060 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 ine unixDlClose
de070 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
de080 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73 Write nBuf bytes
de090 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 of random data
de0a0 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 to the supplied
de0b0 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a buffer zBuf..*/.
de0c0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 static int unixR
de0d0 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 andomness(sqlite
de0e0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
de0f0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
de100 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f zBuf){. UNUSED_
de110 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
de120 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69 d);. assert((si
de130 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 ze_t)nBuf>=(size
de140 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f of(time_t)+sizeo
de150 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 f(int)));.. /*
de160 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 We have to initi
de170 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 alize zBuf to pr
de180 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 event valgrind f
de190 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 rom reporting.
de1a0 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 ** errors. The
de1b0 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 reports issued b
de1c0 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 y valgrind are i
de1d0 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f ncorrect - we wo
de1e0 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 uld. ** prefer
de1f0 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e that the randomn
de200 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 ess be increased
de210 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f by making use o
de220 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 f the. ** unini
de230 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 tialized space i
de240 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c n zBuf - but val
de250 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e grind errors ten
de260 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 d to worry. **
de270 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 some users. Rat
de280 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 her than argue,
de290 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 it seems easier
de2a0 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 just to initiali
de2b0 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c ze. ** the whol
de2c0 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 e array and sile
de2d0 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 nce valgrind, ev
de2e0 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 en if that means
de2f0 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 less randomness
de300 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e . ** in the ran
de310 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 dom seed.. **.
de320 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 ** When testing
de330 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a , initializing z
de340 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 Buf[] to zero is
de350 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 all we do. Tha
de360 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 t means. ** tha
de370 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 t we always use
de380 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 the same random
de390 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e number sequence.
de3a0 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 This makes the
de3b0 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 . ** tests repe
de3c0 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d atable.. */. m
de3d0 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e emset(zBuf, 0, n
de3e0 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e Buf);.#if !defin
de3f0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a ed(SQLITE_TEST).
de400 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c {. int pid,
de410 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 fd;. fd = op
de420 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d en("/dev/urandom
de430 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 ", O_RDONLY);.
de440 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
de450 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 time_t t;.
de460 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 time(&t);.
de470 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c memcpy(zBuf,
de480 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b &t, sizeof(t));
de490 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 74 . pid = get
de4a0 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d pid();. mem
de4b0 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 cpy(&zBuf[sizeof
de4c0 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 (t)], &pid, size
de4d0 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20 of(pid));.
de4e0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74 assert( sizeof(t
de4f0 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 )+sizeof(pid)<=(
de500 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 size_t)nBuf );.
de510 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 nBuf = size
de520 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 of(t) + sizeof(p
de530 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a id);. }else{.
de540 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65 61 nBuf = rea
de550 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66 d(fd, zBuf, nBuf
de560 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66 );. close(f
de570 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 d);. }. }.#e
de580 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 42 ndif. return nB
de590 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c uf;.}.../*.** Sl
de5a0 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 eep for a little
de5b0 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 while. Return
de5c0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 the amount of ti
de5d0 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 me slept..** The
de5e0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
de5f0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f number of micro
de600 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 seconds we want
de610 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 to sleep..** The
de620 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
de630 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d the number of m
de640 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 icroseconds of s
de650 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a leep actually.**
de660 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 requested from
de670 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f the underlying o
de680 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c perating system,
de690 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a a number which.
de6a0 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 61 ** might be grea
de6b0 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
de6c0 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e l to the argumen
de6d0 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a t, but not less.
de6e0 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 75 ** than the argu
de6f0 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ment..*/.static
de700 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73 71 int unixSleep(sq
de710 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 lite3_vfs *NotUs
de720 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 ed, int microsec
de730 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 58 onds){.#if OS_VX
de740 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74 WORKS. struct t
de750 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 73 imespec sp;.. s
de760 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f p.tv_sec = micro
de770 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 30 seconds / 100000
de780 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 20 0;. sp.tv_nsec
de790 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 = (microseconds
de7a0 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30 30 % 1000000) * 100
de7b0 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26 0;. nanosleep(&
de7c0 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e 55 sp, NULL);. UNU
de7d0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
de7e0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
de7f0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 microseconds;.#
de800 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 elif defined(HAV
de810 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56 E_USLEEP) && HAV
de820 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 65 E_USLEEP. uslee
de830 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b p(microseconds);
de840 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
de850 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
de860 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f return microseco
de870 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 nds;.#else. int
de880 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72 seconds = (micr
de890 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29 oseconds+999999)
de8a0 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65 /1000000;. slee
de8b0 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e p(seconds);. UN
de8c0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
de8d0 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
de8e0 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 30 n seconds*100000
de8f0 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 0;.#endif.}../*.
de900 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
de910 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 variable, if se
de920 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 t to a non-zero
de930 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 70 value, is interp
de940 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 reted as.** the
de950 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e 64 number of second
de960 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e 64 s since 1970 and
de970 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 is used to set
de980 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
de990 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
de9a0 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 74 tTime() during t
de9b0 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 esting..*/.#ifde
de9c0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
de9d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
de9e0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d ite3_current_tim
de9f0 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 20 e = 0; /* Fake
dea00 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 73 system time in s
dea10 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 econds since 197
dea20 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0. */.#endif../*
dea30 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 .** Find the cur
dea40 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e rent time (in Un
dea50 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 iversal Coordina
dea60 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 ted Time). Writ
dea70 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 e the.** current
dea80 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 time and date a
dea90 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e s a Julian Day n
deaa0 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f umber into *prNo
deab0 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 w and.** return
deac0 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 0. Return 1 if
dead0 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 the time and dat
deae0 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e e cannot be foun
deaf0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
deb00 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 unixCurrentTime
deb10 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f (sqlite3_vfs *No
deb20 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a 70 tUsed, double *p
deb30 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69 6e rNow){.#if defin
deb40 65 64 28 4e 4f 5f 47 45 54 54 4f 44 29 0a 20 20 ed(NO_GETTOD).
deb50 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 time_t t;. time
deb60 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d (&t);. *prNow =
deb70 20 74 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 t/86400.0 + 244
deb80 30 35 38 37 2e 35 3b 0a 23 65 6c 69 66 20 4f 53 0587.5;.#elif OS
deb90 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 _VXWORKS. struc
deba0 74 20 74 69 6d 65 73 70 65 63 20 73 4e 6f 77 3b t timespec sNow;
debb0 0a 20 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d 65 . clock_gettime
debc0 28 43 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45 2c (CLOCK_REALTIME,
debd0 20 26 73 4e 6f 77 29 3b 0a 20 20 2a 70 72 4e 6f &sNow);. *prNo
debe0 77 20 3d 20 32 34 34 30 35 38 37 2e 35 20 2b 20 w = 2440587.5 +
debf0 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 30 sNow.tv_sec/8640
dec00 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73 0.0 + sNow.tv_ns
dec10 65 63 2f 38 36 34 30 30 30 30 30 30 30 30 30 30 ec/8640000000000
dec20 30 2e 30 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72 0.0;.#else. str
dec30 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e 6f 77 uct timeval sNow
dec40 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64 61 79 ;. gettimeofday
dec50 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20 2a 70 (&sNow, 0);. *p
dec60 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 2e 35 rNow = 2440587.5
dec70 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38 + sNow.tv_sec/8
dec80 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76 6400.0 + sNow.tv
dec90 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30 30 30 _usec/8640000000
deca0 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 0.0;.#endif..#if
decb0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
decc0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 if( sqlite3_cu
decd0 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 rrent_time ){.
dece0 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 *prNow = sqlit
decf0 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f e3_current_time/
ded00 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 86400.0 + 244058
ded10 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7.5;. }.#endif.
ded20 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
ded30 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
ded40 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
ded50 2a 20 57 65 20 61 64 64 65 64 20 74 68 65 20 78 * We added the x
ded60 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 6d GetLastError() m
ded70 65 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 69 ethod with the i
ded80 6e 74 65 6e 74 69 6f 6e 20 6f 66 20 70 72 6f 76 ntention of prov
ded90 69 64 69 6e 67 0a 2a 2a 20 62 65 74 74 65 72 20 iding.** better
deda0 6c 6f 77 2d 6c 65 76 65 6c 20 65 72 72 6f 72 20 low-level error
dedb0 6d 65 73 73 61 67 65 73 20 77 68 65 6e 20 6f 70 messages when op
dedc0 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 70 erating-system p
dedd0 72 6f 62 6c 65 6d 73 20 63 6f 6d 65 20 75 70 0a roblems come up.
dede0 2a 2a 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65 ** during SQLite
dedf0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 operation. But
dee00 20 73 6f 20 66 61 72 2c 20 6e 6f 6e 65 20 6f 66 so far, none of
dee10 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 69 that has been i
dee20 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e mplemented.** in
dee30 20 74 68 65 20 63 6f 72 65 2e 20 20 53 6f 20 74 the core. So t
dee40 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e his routine is n
dee50 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 46 6f ever called. Fo
dee60 72 20 6e 6f 77 2c 20 69 74 20 69 73 20 6d 65 72 r now, it is mer
dee70 65 6c 79 0a 2a 2a 20 61 20 70 6c 61 63 65 2d 68 ely.** a place-h
dee80 6f 6c 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 older..*/.static
dee90 20 69 6e 74 20 75 6e 69 78 47 65 74 4c 61 73 74 int unixGetLast
deea0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 Error(sqlite3_vf
deeb0 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 s *NotUsed, int
deec0 4e 6f 74 55 73 65 64 32 2c 20 63 68 61 72 20 2a NotUsed2, char *
deed0 4e 6f 74 55 73 65 64 33 29 7b 0a 20 20 55 4e 55 NotUsed3){. UNU
deee0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
deef0 74 55 73 65 64 29 3b 0a 20 20 55 4e 55 53 45 44 tUsed);. UNUSED
def00 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
def10 65 64 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 ed2);. UNUSED_P
def20 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
def30 33 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 3);. return 0;.
def40 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../*.**********
def50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
def60 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 nd of sqlite3_vf
def70 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a s methods ******
def80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
def90 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
defa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
defb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
defc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
defd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
defe0 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
deff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
df000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
df010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
df020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
df030 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
df040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
df050 2a 20 42 65 67 69 6e 20 50 72 6f 78 79 20 4c 6f * Begin Proxy Lo
df060 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a cking **********
df070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
df080 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 72 6f ******.**.** Pro
df090 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 20 xy locking is a
df0a0 22 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 "uber-locking-me
df0b0 74 68 6f 64 22 20 69 6e 20 74 68 69 73 20 73 65 thod" in this se
df0c0 6e 73 65 3a 20 20 49 74 20 75 73 65 73 20 74 68 nse: It uses th
df0d0 65 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 e.** other locki
df0e0 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 73 65 ng methods on se
df0f0 63 6f 6e 64 61 72 79 20 6c 6f 63 6b 20 66 69 6c condary lock fil
df100 65 73 2e 20 20 50 72 6f 78 79 20 6c 6f 63 6b 69 es. Proxy locki
df110 6e 67 20 69 73 20 61 0a 2a 2a 20 6d 65 74 61 2d ng is a.** meta-
df120 6c 61 79 65 72 20 6f 76 65 72 20 74 6f 70 20 6f layer over top o
df130 66 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 f the primitive
df140 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e locking implemen
df150 74 65 64 20 61 62 6f 76 65 2e 20 20 46 6f 72 0a ted above. For.
df160 2a 2a 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 ** this reason,
df170 74 68 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61 the division tha
df180 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6f 66 20 t implements of
df190 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 proxy locking is
df1a0 20 64 65 66 65 72 72 65 64 0a 2a 2a 20 75 6e 74 deferred.** unt
df1b0 69 6c 20 6c 61 74 65 20 69 6e 20 74 68 65 20 66 il late in the f
df1c0 69 6c 65 20 28 68 65 72 65 29 20 61 66 74 65 72 ile (here) after
df1d0 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 all of the othe
df1e0 72 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 68 61 r I/O methods ha
df1f0 76 65 0a 2a 2a 20 62 65 65 6e 20 64 65 66 69 6e ve.** been defin
df200 65 64 20 2d 20 73 6f 20 74 68 61 74 20 74 68 65 ed - so that the
df210 20 70 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 primitive locki
df220 6e 67 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 ng methods are a
df230 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 73 vailable.** as s
df240 65 72 76 69 63 65 73 20 74 6f 20 68 65 6c 70 20 ervices to help
df250 77 69 74 68 20 74 68 65 20 69 6d 70 6c 65 6d 65 with the impleme
df260 6e 74 61 74 69 6f 6e 20 6f 66 20 70 72 6f 78 79 ntation of proxy
df270 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 2a locking..**.***
df280 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 *.**.** The defa
df290 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 ult locking sche
df2a0 6d 65 73 20 69 6e 20 53 51 4c 69 74 65 20 75 73 mes in SQLite us
df2b0 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 e byte-range loc
df2c0 6b 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 ks on the.** dat
df2d0 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 6f abase file to co
df2e0 6f 72 64 69 6e 61 74 65 20 73 61 66 65 2c 20 63 ordinate safe, c
df2f0 6f 6e 63 75 72 72 65 6e 74 20 61 63 63 65 73 73 oncurrent access
df300 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 by multiple rea
df310 64 65 72 73 0a 2a 2a 20 61 6e 64 20 77 72 69 74 ders.** and writ
df320 65 72 73 20 5b 68 74 74 70 3a 2f 2f 73 71 6c 69 ers [http://sqli
df330 74 65 2e 6f 72 67 2f 6c 6f 63 6b 69 6e 67 76 33 te.org/lockingv3
df340 2e 68 74 6d 6c 5d 2e 20 20 54 68 65 20 66 69 76 .html]. The fiv
df350 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a e file locking.*
df360 2a 20 73 74 61 74 65 73 20 28 55 4e 4c 4f 43 4b * states (UNLOCK
df370 45 44 2c 20 50 45 4e 44 49 4e 47 2c 20 53 48 41 ED, PENDING, SHA
df380 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 45 RED, RESERVED, E
df390 58 43 4c 55 53 49 56 45 29 20 61 72 65 20 69 6d XCLUSIVE) are im
df3a0 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 61 73 20 plemented.** as
df3b0 50 4f 53 49 58 20 72 65 61 64 20 26 20 77 72 69 POSIX read & wri
df3c0 74 65 20 6c 6f 63 6b 73 20 6f 76 65 72 20 66 69 te locks over fi
df3d0 78 65 64 20 73 65 74 20 6f 66 20 6c 6f 63 61 74 xed set of locat
df3e0 69 6f 6e 73 20 28 76 69 61 20 66 73 63 74 6c 29 ions (via fsctl)
df3f0 2c 0a 2a 2a 20 6f 6e 20 41 46 50 20 61 6e 64 20 ,.** on AFP and
df400 53 4d 42 20 6f 6e 6c 79 20 65 78 63 6c 75 73 69 SMB only exclusi
df410 76 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f ve byte-range lo
df420 63 6b 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c cks are availabl
df430 65 20 76 69 61 20 66 73 63 74 6c 0a 2a 2a 20 77 e via fsctl.** w
df440 69 74 68 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 ith _IOWR('z', 2
df450 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61 3, struct ByteRa
df460 6e 67 65 4c 6f 63 6b 50 42 32 29 20 74 6f 20 74 ngeLockPB2) to t
df470 72 61 63 6b 20 74 68 65 20 73 61 6d 65 20 35 20 rack the same 5
df480 73 74 61 74 65 73 2e 0a 2a 2a 20 54 6f 20 73 69 states..** To si
df490 6d 75 6c 61 74 65 20 61 20 46 5f 52 44 4c 43 4b mulate a F_RDLCK
df4a0 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 72 on the shared r
df4b0 61 6e 67 65 2c 20 6f 6e 20 41 46 50 20 61 20 72 ange, on AFP a r
df4c0 61 6e 64 6f 6d 6c 79 20 73 65 6c 65 63 74 65 64 andomly selected
df4d0 0a 2a 2a 20 61 64 64 72 65 73 73 20 69 6e 20 74 .** address in t
df4e0 68 65 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 he shared range
df4f0 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 20 53 is taken for a S
df500 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 HARED lock, the
df510 65 6e 74 69 72 65 0a 2a 2a 20 73 68 61 72 65 64 entire.** shared
df520 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 range is taken
df530 66 6f 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45 for an EXCLUSIVE
df540 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 lock):.**.**
df550 20 20 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 PENDING_BYTE
df560 20 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30 0x4000000
df570 30 09 09 20 20 20 09 0a 2a 2a 20 20 20 20 20 20 0.. ..**
df580 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 RESERVED_BYTE
df590 20 20 20 20 30 78 34 30 30 30 30 30 30 31 0a 2a 0x40000001.*
df5a0 2a 20 20 20 20 20 20 53 48 41 52 45 44 5f 52 41 * SHARED_RA
df5b0 4e 47 45 20 20 20 20 20 20 20 20 30 78 34 30 30 NGE 0x400
df5c0 30 30 30 30 32 20 2d 3e 20 30 78 34 30 30 30 30 00002 -> 0x40000
df5d0 32 30 30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 200.**.** This w
df5e0 6f 72 6b 73 20 77 65 6c 6c 20 6f 6e 20 74 68 65 orks well on the
df5f0 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 local file syst
df600 65 6d 2c 20 62 75 74 20 73 68 6f 77 73 20 61 20 em, but shows a
df610 6e 65 61 72 6c 79 20 31 30 30 78 0a 2a 2a 20 73 nearly 100x.** s
df620 6c 6f 77 64 6f 77 6e 20 69 6e 20 72 65 61 64 20 lowdown in read
df630 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 6e 20 41 performance on A
df640 46 50 20 62 65 63 61 75 73 65 20 74 68 65 20 41 FP because the A
df650 46 50 20 63 6c 69 65 6e 74 20 64 69 73 61 62 6c FP client disabl
df660 65 73 0a 2a 2a 20 74 68 65 20 72 65 61 64 20 63 es.** the read c
df670 61 63 68 65 20 77 68 65 6e 20 62 79 74 65 2d 72 ache when byte-r
df680 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 70 ange locks are p
df690 72 65 73 65 6e 74 2e 20 20 45 6e 61 62 6c 69 6e resent. Enablin
df6a0 67 20 74 68 65 20 72 65 61 64 0a 2a 2a 20 63 61 g the read.** ca
df6b0 63 68 65 20 65 78 70 6f 73 65 73 20 61 20 63 61 che exposes a ca
df6c0 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20 70 72 che coherency pr
df6d0 6f 62 6c 65 6d 20 74 68 61 74 20 69 73 20 70 72 oblem that is pr
df6e0 65 73 65 6e 74 20 6f 6e 20 61 6c 6c 20 4f 53 20 esent on all OS
df6f0 58 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 20 6e X.** supported n
df700 65 74 77 6f 72 6b 20 66 69 6c 65 20 73 79 73 74 etwork file syst
df710 65 6d 73 2e 20 20 4e 46 53 20 61 6e 64 20 41 46 ems. NFS and AF
df720 50 20 62 6f 74 68 20 6f 62 73 65 72 76 65 20 74 P both observe t
df730 68 65 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f 2d 6f he.** close-to-o
df740 70 65 6e 20 73 65 6d 61 6e 74 69 63 73 20 66 6f pen semantics fo
df750 72 20 65 6e 73 75 72 69 6e 67 20 63 61 63 68 65 r ensuring cache
df760 20 63 6f 68 65 72 65 6e 63 79 0a 2a 2a 20 5b 68 coherency.** [h
df770 74 74 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72 63 65 ttp://nfs.source
df780 66 6f 72 67 65 2e 6e 65 74 2f 23 66 61 71 5f 61 forge.net/#faq_a
df790 38 5d 2c 20 77 68 69 63 68 20 64 6f 65 73 20 6e 8], which does n
df7a0 6f 74 20 65 66 66 65 63 74 69 76 65 6c 79 0a 2a ot effectively.*
df7b0 2a 20 61 64 64 72 65 73 73 20 74 68 65 20 72 65 * address the re
df7c0 71 75 69 72 65 6d 65 6e 74 73 20 66 6f 72 20 63 quirements for c
df7d0 6f 6e 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 oncurrent databa
df7e0 73 65 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c se access by mul
df7f0 74 69 70 6c 65 0a 2a 2a 20 72 65 61 64 65 72 73 tiple.** readers
df800 20 61 6e 64 20 77 72 69 74 65 72 73 0a 2a 2a 20 and writers.**
df810 5b 68 74 74 70 3a 2f 2f 77 77 77 2e 6e 61 62 62 [http://www.nabb
df820 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d 6f 6e le.com/SQLite-on
df830 2d 4e 46 53 2d 63 61 63 68 65 2d 63 6f 68 65 72 -NFS-cache-coher
df840 65 6e 63 79 2d 74 64 31 35 36 35 35 37 30 31 2e ency-td15655701.
df850 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 html]..**.** To
df860 61 64 64 72 65 73 73 20 74 68 65 20 70 65 72 66 address the perf
df870 6f 72 6d 61 6e 63 65 20 61 6e 64 20 63 61 63 68 ormance and cach
df880 65 20 63 6f 68 65 72 65 6e 63 79 20 69 73 73 75 e coherency issu
df890 65 73 2c 20 70 72 6f 78 79 20 66 69 6c 65 20 6c es, proxy file l
df8a0 6f 63 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65 ocking.** change
df8b0 73 20 74 68 65 20 77 61 79 20 64 61 74 61 62 61 s the way databa
df8c0 73 65 20 61 63 63 65 73 73 20 69 73 20 63 6f 6e se access is con
df8d0 74 72 6f 6c 6c 65 64 20 62 79 20 6c 69 6d 69 74 trolled by limit
df8e0 69 6e 67 20 61 63 63 65 73 73 20 74 6f 20 61 0a ing access to a.
df8f0 2a 2a 20 73 69 6e 67 6c 65 20 68 6f 73 74 20 61 ** single host a
df900 74 20 61 20 74 69 6d 65 20 61 6e 64 20 6d 6f 76 t a time and mov
df910 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73 20 6f ing file locks o
df920 66 66 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ff of the databa
df930 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6f se file.** and o
df940 6e 74 6f 20 61 20 70 72 6f 78 79 20 66 69 6c 65 nto a proxy file
df950 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 on the local fi
df960 6c 65 20 73 79 73 74 65 6d 2e 20 20 0a 2a 2a 0a le system. .**.
df970 2a 2a 0a 2a 2a 20 55 73 69 6e 67 20 70 72 6f 78 **.** Using prox
df980 79 20 6c 6f 63 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d y locks.** -----
df990 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a ------------.**.
df9a0 2a 2a 20 43 20 41 50 49 73 0a 2a 2a 0a 2a 2a 20 ** C APIs.**.**
df9b0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f sqlite3_file_co
df9c0 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65 ntrol(db, dbname
df9d0 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 , SQLITE_SET_LOC
df9e0 4b 50 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a 20 20 KPROXYFILE,.**
df9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dfa00 20 20 20 20 20 3c 70 72 6f 78 79 5f 70 61 74 68 <proxy_path
dfa10 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 2a > | ":auto:");.*
dfa20 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f * sqlite3_file_
dfa30 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 control(db, dbna
dfa40 6d 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c me, SQLITE_GET_L
dfa50 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 26 3c OCKPROXYFILE, &<
dfa60 70 72 6f 78 79 5f 70 61 74 68 3e 29 3b 0a 2a 2a proxy_path>);.**
dfa70 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70 72 61 67 6d .**.** SQL pragm
dfa80 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47 4d 41 as.**.** PRAGMA
dfa90 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b [database.]lock
dfaa0 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 3c 70 72 6f _proxy_file=<pro
dfab0 78 79 5f 70 61 74 68 3e 20 7c 20 3a 61 75 74 6f xy_path> | :auto
dfac0 3a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 :.** PRAGMA [da
dfad0 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f tabase.]lock_pro
dfae0 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20 53 70 xy_file.**.** Sp
dfaf0 65 63 69 66 79 69 6e 67 20 22 3a 61 75 74 6f 3a ecifying ":auto:
dfb00 22 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20 " means that if
dfb10 74 68 65 72 65 20 69 73 20 61 20 63 6f 6e 63 68 there is a conch
dfb20 20 66 69 6c 65 20 77 69 74 68 20 61 20 6d 61 74 file with a mat
dfb30 63 68 69 6e 67 0a 2a 2a 20 68 6f 73 74 20 49 44 ching.** host ID
dfb40 20 69 6e 20 69 74 2c 20 74 68 65 20 70 72 6f 78 in it, the prox
dfb50 79 20 70 61 74 68 20 69 6e 20 74 68 65 20 63 6f y path in the co
dfb60 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 nch file will be
dfb70 20 75 73 65 64 2c 20 6f 74 68 65 72 77 69 73 65 used, otherwise
dfb80 0a 2a 2a 20 61 20 70 72 6f 78 79 20 70 61 74 68 .** a proxy path
dfb90 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 75 73 based on the us
dfba0 65 72 27 73 20 74 65 6d 70 20 64 69 72 0a 2a 2a er's temp dir.**
dfbb0 20 28 76 69 61 20 63 6f 6e 66 73 74 72 28 5f 43 (via confstr(_C
dfbc0 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 S_DARWIN_USER_TE
dfbd0 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29 20 77 69 6c MP_DIR,...)) wil
dfbe0 6c 20 62 65 20 75 73 65 64 20 61 6e 64 20 74 68 l be used and th
dfbf0 65 0a 2a 2a 20 61 63 74 75 61 6c 20 70 72 6f 78 e.** actual prox
dfc00 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 67 y file name is g
dfc10 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 74 68 enerated from th
dfc20 65 20 6e 61 6d 65 20 61 6e 64 20 70 61 74 68 20 e name and path
dfc30 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 of the.** databa
dfc40 73 65 20 66 69 6c 65 2e 20 20 46 6f 72 20 65 78 se file. For ex
dfc50 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ample:.**.**
dfc60 20 20 20 46 6f 72 20 64 61 74 61 62 61 73 65 20 For database
dfc70 70 61 74 68 20 22 2f 55 73 65 72 73 2f 6d 65 2f path "/Users/me/
dfc80 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20 20 20 20 20 foo.db" .**
dfc90 20 20 54 68 65 20 6c 6f 63 6b 20 70 61 74 68 20 The lock path
dfca0 77 69 6c 6c 20 62 65 20 22 3c 74 6d 70 64 69 72 will be "<tmpdir
dfcb0 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 6b 73 2f 5f >/sqliteplocks/_
dfcc0 55 73 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 62 3a Users_me_foo.db:
dfcd0 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e auto:").**.** On
dfce0 63 65 20 61 20 6c 6f 63 6b 20 70 72 6f 78 79 20 ce a lock proxy
dfcf0 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f is configured fo
dfd00 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e r a database con
dfd10 6e 65 63 74 69 6f 6e 2c 20 69 74 20 63 61 6e 20 nection, it can
dfd20 6e 6f 74 0a 2a 2a 20 62 65 20 72 65 6d 6f 76 65 not.** be remove
dfd30 64 2c 20 68 6f 77 65 76 65 72 20 69 74 20 6d 61 d, however it ma
dfd40 79 20 62 65 20 73 77 69 74 63 68 65 64 20 74 6f y be switched to
dfd50 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f a different pro
dfd60 78 79 20 70 61 74 68 20 76 69 61 0a 2a 2a 20 74 xy path via.** t
dfd70 68 65 20 61 62 6f 76 65 20 41 50 49 73 20 28 61 he above APIs (a
dfd80 73 73 75 6d 69 6e 67 20 74 68 65 20 63 6f 6e 63 ssuming the conc
dfd90 68 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 62 65 h file is not be
dfda0 69 6e 67 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 ing held by anot
dfdb0 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f her.** connectio
dfdc0 6e 20 6f 72 20 70 72 6f 63 65 73 73 29 2e 20 0a n or process). .
dfdd0 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70 72 6f **.**.** How pro
dfde0 78 79 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 xy locking works
dfdf0 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ------------
dfe00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a -----------.**.*
dfe10 2a 20 50 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 * Proxy file loc
dfe20 6b 69 6e 67 20 72 65 6c 69 65 73 20 70 72 69 6d king relies prim
dfe30 61 72 69 6c 79 20 6f 6e 20 74 77 6f 20 6e 65 77 arily on two new
dfe40 20 73 75 70 70 6f 72 74 69 6e 67 20 66 69 6c 65 supporting file
dfe50 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 63 s: .**.** * c
dfe60 6f 6e 63 68 20 66 69 6c 65 20 74 6f 20 6c 69 6d onch file to lim
dfe70 69 74 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 it access to the
dfe80 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
dfe90 6f 20 61 20 73 69 6e 67 6c 65 20 68 6f 73 74 0a o a single host.
dfea0 2a 2a 20 20 20 20 20 20 61 74 20 61 20 74 69 6d ** at a tim
dfeb0 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70 72 6f e.**.** * pro
dfec0 78 79 20 66 69 6c 65 20 74 6f 20 61 63 74 20 61 xy file to act a
dfed0 73 20 61 20 70 72 6f 78 79 20 66 6f 72 20 74 68 s a proxy for th
dfee0 65 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 e advisory locks
dfef0 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20 20 20 normally.**
dff00 20 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 taken on the d
dff10 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 54 68 atabase.**.** Th
dff20 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2d 20 74 e conch file - t
dff30 6f 20 75 73 65 20 61 20 70 72 6f 78 79 20 66 69 o use a proxy fi
dff40 6c 65 2c 20 73 71 6c 69 74 65 20 6d 75 73 74 20 le, sqlite must
dff50 66 69 72 73 74 20 22 68 6f 6c 64 20 74 68 65 20 first "hold the
dff60 63 6f 6e 63 68 22 0a 2a 2a 20 62 79 20 74 61 6b conch".** by tak
dff70 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 2d 73 74 ing an sqlite-st
dff80 79 6c 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 yle shared lock
dff90 6f 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c on the conch fil
dffa0 65 2c 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a e, reading the.*
dffb0 2a 20 63 6f 6e 74 65 6e 74 73 20 61 6e 64 20 63 * contents and c
dffc0 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 68 6f 73 omparing the hos
dffd0 74 27 73 20 75 6e 69 71 75 65 20 68 6f 73 74 20 t's unique host
dffe0 49 44 20 28 73 65 65 20 62 65 6c 6f 77 29 20 61 ID (see below) a
dfff0 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f 78 79 nd lock.** proxy
e0000 20 70 61 74 68 20 61 67 61 69 6e 73 74 20 74 68 path against th
e0010 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 e values stored
e0020 69 6e 20 74 68 65 20 63 6f 6e 63 68 2e 20 20 54 in the conch. T
e0030 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 he conch file is
e0040 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 .** stored in th
e0050 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 e same directory
e0060 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 as the database
e0070 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 file and the fi
e0080 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 70 61 le name.** is pa
e0090 74 74 65 72 6e 65 64 20 61 66 74 65 72 20 74 68 tterned after th
e00a0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
e00b0 6e 61 6d 65 20 61 73 20 22 2e 3c 64 61 74 61 62 name as ".<datab
e00c0 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68 22 2e asename>-conch".
e00d0 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 63 68 .** If the conch
e00e0 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 file does not e
e00f0 78 69 73 74 2c 20 6f 72 20 69 74 27 73 20 63 6f xist, or it's co
e0100 6e 74 65 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 ntents do not ma
e0110 74 63 68 20 74 68 65 0a 2a 2a 20 68 6f 73 74 20 tch the.** host
e0120 49 44 20 61 6e 64 2f 6f 72 20 70 72 6f 78 79 20 ID and/or proxy
e0130 70 61 74 68 2c 20 74 68 65 6e 20 74 68 65 20 6c path, then the l
e0140 6f 63 6b 20 69 73 20 65 73 63 61 6c 61 74 65 64 ock is escalated
e0150 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 to an exclusive
e0160 0a 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 74 68 65 .** lock and the
e0170 20 63 6f 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74 conch file cont
e0180 65 6e 74 73 20 69 73 20 75 70 64 61 74 65 64 20 ents is updated
e0190 77 69 74 68 20 74 68 65 20 68 6f 73 74 20 49 44 with the host ID
e01a0 20 61 6e 64 20 70 72 6f 78 79 0a 2a 2a 20 70 61 and proxy.** pa
e01b0 74 68 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 th and the lock
e01c0 69 73 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f is downgraded to
e01d0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 a shared lock a
e01e0 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20 63 6f gain. If the co
e01f0 6e 63 68 0a 2a 2a 20 69 73 20 68 65 6c 64 20 62 nch.** is held b
e0200 79 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 y another proces
e0210 73 20 28 77 69 74 68 20 61 20 73 68 61 72 65 64 s (with a shared
e0220 20 6c 6f 63 6b 29 2c 20 74 68 65 20 65 78 63 6c lock), the excl
e0230 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 77 69 usive lock.** wi
e0240 6c 6c 20 66 61 69 6c 20 61 6e 64 20 53 51 4c 49 ll fail and SQLI
e0250 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 TE_BUSY is retur
e0260 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ned..**.** The p
e0270 72 6f 78 79 20 66 69 6c 65 20 2d 20 61 20 73 69 roxy file - a si
e0280 6e 67 6c 65 2d 62 79 74 65 20 66 69 6c 65 20 75 ngle-byte file u
e0290 73 65 64 20 66 6f 72 20 61 6c 6c 20 61 64 76 69 sed for all advi
e02a0 73 6f 72 79 20 66 69 6c 65 20 6c 6f 63 6b 73 0a sory file locks.
e02b0 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 74 61 6b 65 ** normally take
e02c0 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 n on the databas
e02d0 65 20 66 69 6c 65 2e 20 20 20 54 68 69 73 20 61 e file. This a
e02e0 6c 6c 6f 77 73 20 66 6f 72 20 73 61 66 65 20 73 llows for safe s
e02f0 68 61 72 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 haring.** of the
e0300 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
e0310 6f 72 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 or multiple read
e0320 65 72 73 20 61 6e 64 20 77 72 69 74 65 72 73 20 ers and writers
e0330 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 68 on the same.** h
e0340 6f 73 74 20 28 74 68 65 20 63 6f 6e 63 68 20 65 ost (the conch e
e0350 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 65 79 nsures that they
e0360 20 61 6c 6c 20 75 73 65 20 74 68 65 20 73 61 6d all use the sam
e0370 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c e local lock fil
e0380 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 e)..**.** There
e0390 69 73 20 61 20 74 68 69 72 64 20 66 69 6c 65 20 is a third file
e03a0 2d 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 - the host ID fi
e03b0 6c 65 20 2d 20 75 73 65 64 20 61 73 20 61 20 70 le - used as a p
e03c0 65 72 73 69 73 74 65 6e 74 20 72 65 63 6f 72 64 ersistent record
e03d0 0a 2a 2a 20 6f 66 20 61 20 75 6e 69 71 75 65 20 .** of a unique
e03e0 69 64 65 6e 74 69 66 69 65 72 20 66 6f 72 20 74 identifier for t
e03f0 68 65 20 68 6f 73 74 2c 20 61 20 31 32 38 2d 62 he host, a 128-b
e0400 79 74 65 20 75 6e 69 71 75 65 20 68 6f 73 74 20 yte unique host
e0410 69 64 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 74 68 id file.** in th
e0420 65 20 70 61 74 68 20 64 65 66 69 6e 65 64 20 62 e path defined b
e0430 79 20 74 68 65 20 48 4f 53 54 49 44 50 41 54 48 y the HOSTIDPATH
e0440 20 6d 61 63 72 6f 20 28 64 65 66 61 75 6c 74 20 macro (default
e0450 76 61 6c 75 65 20 69 73 0a 2a 2a 20 2f 4c 69 62 value is.** /Lib
e0460 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d rary/Caches/.com
e0470 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e .apple.sqliteCon
e0480 63 68 48 6f 73 74 49 64 29 2e 0a 2a 2a 0a 2a 2a chHostId)..**.**
e0490 20 52 65 71 75 65 73 74 69 6e 67 20 74 68 65 20 Requesting the
e04a0 6c 6f 63 6b 20 70 72 6f 78 79 20 64 6f 65 73 20 lock proxy does
e04b0 6e 6f 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 not immediately
e04c0 74 61 6b 65 20 74 68 65 20 63 6f 6e 63 68 2c 20 take the conch,
e04d0 69 74 20 69 73 0a 2a 2a 20 6f 6e 6c 79 20 74 61 it is.** only ta
e04e0 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 66 69 72 ken when the fir
e04f0 73 74 20 72 65 71 75 65 73 74 20 74 6f 20 6c 6f st request to lo
e0500 63 6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ck database file
e0510 20 69 73 20 6d 61 64 65 2e 20 20 0a 2a 2a 20 54 is made. .** T
e0520 68 69 73 20 6d 61 74 63 68 65 73 20 74 68 65 20 his matches the
e0530 73 65 6d 61 6e 74 69 63 73 20 6f 66 20 74 68 65 semantics of the
e0540 20 74 72 61 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 traditional loc
e0550 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 2c 20 77 king behavior, w
e0560 68 65 72 65 0a 2a 2a 20 6f 70 65 6e 69 6e 67 20 here.** opening
e0570 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 a connection to
e0580 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 a database file
e0590 64 6f 65 73 20 6e 6f 74 20 74 61 6b 65 20 61 20 does not take a
e05a0 6c 6f 63 6b 20 6f 6e 20 69 74 2e 0a 2a 2a 20 54 lock on it..** T
e05b0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 he shared lock a
e05c0 6e 64 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 nd an open file
e05d0 64 65 73 63 72 69 70 74 6f 72 20 61 72 65 20 6d descriptor are m
e05e0 61 69 6e 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 aintained until
e05f0 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 .** the connecti
e0600 6f 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 on to the databa
e0610 73 65 20 69 73 20 63 6c 6f 73 65 64 2e 20 0a 2a se is closed. .*
e0620 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66 *.** The proxy f
e0630 69 6c 65 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b ile and the lock
e0640 20 66 69 6c 65 20 61 72 65 20 6e 65 76 65 72 20 file are never
e0650 64 65 6c 65 74 65 64 20 73 6f 20 74 68 65 79 20 deleted so they
e0660 6f 6e 6c 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 only need.** to
e0670 62 65 20 63 72 65 61 74 65 64 20 74 68 65 20 66 be created the f
e0680 69 72 73 74 20 74 69 6d 65 20 74 68 65 79 20 61 irst time they a
e0690 72 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 re used..**.** C
e06a0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
e06b0 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d ions.** --------
e06c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
e06d0 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 45 46 .** SQLITE_PREF
e06e0 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 ER_PROXY_LOCKING
e06f0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 44 61 74 .**.** Dat
e0700 61 62 61 73 65 20 66 69 6c 65 73 20 61 63 63 65 abase files acce
e0710 73 73 65 64 20 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 ssed on non-loca
e0720 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20 61 l file systems a
e0730 72 65 0a 2a 2a 20 20 20 20 20 20 20 61 75 74 6f re.** auto
e0740 6d 61 74 69 63 61 6c 6c 79 20 63 6f 6e 66 69 67 matically config
e0750 75 72 65 64 20 66 6f 72 20 70 72 6f 78 79 20 6c ured for proxy l
e0760 6f 63 6b 69 6e 67 2c 20 6c 6f 63 6b 20 66 69 6c ocking, lock fil
e0770 65 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20 es are.**
e0780 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61 named automatica
e0790 6c 6c 79 20 75 73 69 6e 67 20 74 68 65 20 73 61 lly using the sa
e07a0 6d 65 20 6c 6f 67 69 63 20 61 73 0a 2a 2a 20 20 me logic as.**
e07b0 20 20 20 20 20 50 52 41 47 4d 41 20 6c 6f 63 6b PRAGMA lock
e07c0 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 22 3a 61 75 _proxy_file=":au
e07d0 74 6f 3a 22 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 to:".** .**
e07e0 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 SQLITE_PROXY_DEB
e07f0 55 47 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 45 UG.**.** E
e0800 6e 61 62 6c 65 73 20 74 68 65 20 6c 6f 67 67 69 nables the loggi
e0810 6e 67 20 6f 66 20 65 72 72 6f 72 20 6d 65 73 73 ng of error mess
e0820 61 67 65 73 20 64 75 72 69 6e 67 20 68 6f 73 74 ages during host
e0830 20 69 64 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 id file.**
e0840 20 20 72 65 74 72 69 65 76 61 6c 20 61 6e 64 20 retrieval and
e0850 63 72 65 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 creation.**.**
e0860 48 4f 53 54 49 44 50 41 54 48 0a 2a 2a 0a 2a 2a HOSTIDPATH.**.**
e0870 20 20 20 20 20 20 20 4f 76 65 72 72 69 64 65 73 Overrides
e0880 20 74 68 65 20 64 65 66 61 75 6c 74 20 68 6f 73 the default hos
e0890 74 20 49 44 20 66 69 6c 65 20 70 61 74 68 20 6c t ID file path l
e08a0 6f 63 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c ocation.**.** L
e08b0 4f 43 4b 50 52 4f 58 59 44 49 52 0a 2a 2a 0a 2a OCKPROXYDIR.**.*
e08c0 2a 20 20 20 20 20 20 20 4f 76 65 72 72 69 64 65 * Override
e08d0 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 64 69 s the default di
e08e0 72 65 63 74 6f 72 79 20 75 73 65 64 20 66 6f 72 rectory used for
e08f0 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 lock proxy file
e0900 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 s that.**
e0910 61 72 65 20 6e 61 6d 65 64 20 61 75 74 6f 6d 61 are named automa
e0920 74 69 63 61 6c 6c 79 20 76 69 61 20 74 68 65 20 tically via the
e0930 22 3a 61 75 74 6f 3a 22 20 73 65 74 74 69 6e 67 ":auto:" setting
e0940 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 44 .**.** SQLITE_D
e0950 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
e0960 50 45 52 4d 49 53 53 49 4f 4e 53 0a 2a 2a 0a 2a PERMISSIONS.**.*
e0970 2a 20 20 20 20 20 20 20 50 65 72 6d 69 73 73 69 * Permissi
e0980 6f 6e 73 20 74 6f 20 75 73 65 20 77 68 65 6e 20 ons to use when
e0990 63 72 65 61 74 69 6e 67 20 61 20 64 69 72 65 63 creating a direc
e09a0 74 6f 72 79 20 66 6f 72 20 73 74 6f 72 69 6e 67 tory for storing
e09b0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 6c 6f the.** lo
e09c0 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 2c 20 ck proxy files,
e09d0 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 4c only used when L
e09e0 4f 43 4b 50 52 4f 58 59 44 49 52 20 69 73 20 6e OCKPROXYDIR is n
e09f0 6f 74 20 73 65 74 2e 0a 2a 2a 20 20 20 20 0a 2a ot set..** .*
e0a00 2a 20 20 20 20 0a 2a 2a 20 41 73 20 6d 65 6e 74 * .** As ment
e0a10 69 6f 6e 65 64 20 61 62 6f 76 65 2c 20 77 68 65 ioned above, whe
e0a20 6e 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 n compiled with
e0a30 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 SQLITE_PREFER_PR
e0a40 4f 58 59 5f 4c 4f 43 4b 49 4e 47 2c 0a 2a 2a 20 OXY_LOCKING,.**
e0a50 73 65 74 74 69 6e 67 20 74 68 65 20 65 6e 76 69 setting the envi
e0a60 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 ronment variable
e0a70 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 SQLITE_FORCE_PR
e0a80 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 74 6f 20 31 OXY_LOCKING to 1
e0a90 20 77 69 6c 6c 0a 2a 2a 20 66 6f 72 63 65 20 70 will.** force p
e0aa0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 roxy locking to
e0ab0 62 65 20 75 73 65 64 20 66 6f 72 20 65 76 65 72 be used for ever
e0ac0 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 y database file
e0ad0 6f 70 65 6e 65 64 2c 20 61 6e 64 20 30 0a 2a 2a opened, and 0.**
e0ae0 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 75 74 6f will force auto
e0af0 6d 61 74 69 63 20 70 72 6f 78 79 20 6c 6f 63 6b matic proxy lock
e0b00 69 6e 67 20 74 6f 20 62 65 20 64 69 73 61 62 6c ing to be disabl
e0b10 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 ed for all datab
e0b20 61 73 65 0a 2a 2a 20 66 69 6c 65 73 20 28 65 78 ase.** files (ex
e0b30 70 6c 69 63 69 74 79 20 63 61 6c 6c 69 6e 67 20 plicity calling
e0b40 74 68 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c the SQLITE_SET_L
e0b50 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 70 72 61 OCKPROXYFILE pra
e0b60 67 6d 61 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 gma or.** sqlite
e0b70 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 41 50 _file_control AP
e0b80 49 20 69 73 20 6e 6f 74 20 61 66 66 65 63 74 65 I is not affecte
e0b90 64 20 62 79 20 53 51 4c 49 54 45 5f 46 4f 52 43 d by SQLITE_FORC
e0ba0 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29 E_PROXY_LOCKING)
e0bb0 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 ..*/../*.** Prox
e0bc0 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c y locking is onl
e0bd0 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d y available on M
e0be0 61 63 4f 53 58 20 0a 2a 2f 0a 23 69 66 20 64 65 acOSX .*/.#if de
e0bf0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e0c00 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
e0c10 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
e0c20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
e0c30 45 53 54 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20 EST./* simulate
e0c40 6d 75 6c 74 69 70 6c 65 20 68 6f 73 74 73 20 62 multiple hosts b
e0c50 79 20 63 72 65 61 74 69 6e 67 20 75 6e 69 71 75 y creating uniqu
e0c60 65 20 68 6f 73 74 69 64 20 66 69 6c 65 20 70 61 e hostid file pa
e0c70 74 68 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ths */.SQLITE_AP
e0c80 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 68 6f I int sqlite3_ho
e0c90 73 74 69 64 5f 6e 75 6d 20 3d 20 30 3b 0a 23 65 stid_num = 0;.#e
e0ca0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
e0cb0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
e0cc0 65 78 74 20 68 61 73 20 74 68 65 20 70 61 74 68 ext has the path
e0cd0 20 61 6e 64 20 66 69 6c 65 20 73 74 72 75 63 74 and file struct
e0ce0 75 72 65 73 20 66 6f 72 20 74 68 65 20 72 65 6d ures for the rem
e0cf0 6f 74 65 20 0a 2a 2a 20 61 6e 64 20 6c 6f 63 61 ote .** and loca
e0d00 6c 20 70 72 6f 78 79 20 66 69 6c 65 73 20 69 6e l proxy files in
e0d10 20 69 74 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 it.*/.typedef s
e0d20 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69 truct proxyLocki
e0d30 6e 67 43 6f 6e 74 65 78 74 20 70 72 6f 78 79 4c ngContext proxyL
e0d40 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 ockingContext;.s
e0d50 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69 truct proxyLocki
e0d60 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e ngContext {. un
e0d70 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c ixFile *conchFil
e0d80 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 e; /* Op
e0d90 65 6e 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f en conch file */
e0da0 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 46 69 . char *conchFi
e0db0 6c 65 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 lePath;
e0dc0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
e0dd0 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 75 onch file */. u
e0de0 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f nixFile *lockPro
e0df0 78 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f xy; /* O
e0e00 70 65 6e 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 pen proxy lock f
e0e10 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c ile */. char *l
e0e20 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 20 20 20 ockProxyPath;
e0e30 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
e0e40 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 the proxy lock
e0e50 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a file */. char *
e0e60 64 62 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 dbPath;
e0e70 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
e0e80 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 f the open file
e0e90 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 63 68 48 65 */. int conchHe
e0ea0 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ld;
e0eb0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
e0ec0 20 63 6f 6e 63 68 20 69 73 20 63 75 72 72 65 6e conch is curren
e0ed0 74 6c 79 20 68 65 6c 64 20 2a 2f 0a 20 20 76 6f tly held */. vo
e0ee0 69 64 20 2a 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f id *oldLockingCo
e0ef0 6e 74 65 78 74 3b 20 20 20 20 20 2f 2a 20 4f 72 ntext; /* Or
e0f00 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 63 6f iginal lockingco
e0f10 6e 74 65 78 74 20 74 6f 20 72 65 73 74 6f 72 65 ntext to restore
e0f20 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 73 on close */. s
e0f30 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e0f40 73 20 63 6f 6e 73 74 20 2a 70 4f 6c 64 4d 65 74 s const *pOldMet
e0f50 68 6f 64 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 hod; /* Orig
e0f60 69 6e 61 6c 20 49 2f 4f 20 6d 65 74 68 6f 64 73 inal I/O methods
e0f70 20 66 6f 72 20 63 6c 6f 73 65 20 2a 2f 0a 7d 3b for close */.};
e0f80 0a 0a 2f 2a 20 48 4f 53 54 49 44 4c 45 4e 20 61 ../* HOSTIDLEN a
e0f90 6e 64 20 43 4f 4e 43 48 4c 45 4e 20 62 6f 74 68 nd CONCHLEN both
e0fa0 20 69 6e 63 6c 75 64 65 20 73 70 61 63 65 20 66 include space f
e0fb0 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 0a 2a or the string .*
e0fc0 2a 20 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 * terminating nu
e0fd0 6c 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 48 4f l .*/.#define HO
e0fe0 53 54 49 44 4c 45 4e 20 20 20 20 20 20 20 20 20 STIDLEN
e0ff0 31 32 38 0a 23 64 65 66 69 6e 65 20 43 4f 4e 43 128.#define CONC
e1000 48 4c 45 4e 20 20 20 20 20 20 20 20 20 20 28 4d HLEN (M
e1010 41 58 50 41 54 48 4c 45 4e 2b 48 4f 53 54 49 44 AXPATHLEN+HOSTID
e1020 4c 45 4e 2b 31 29 0a 23 69 66 6e 64 65 66 20 48 LEN+1).#ifndef H
e1030 4f 53 54 49 44 50 41 54 48 0a 23 20 64 65 66 69 OSTIDPATH.# defi
e1040 6e 65 20 48 4f 53 54 49 44 50 41 54 48 20 20 20 ne HOSTIDPATH
e1050 20 20 20 20 22 2f 4c 69 62 72 61 72 79 2f 43 61 "/Library/Ca
e1060 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e ches/.com.apple.
e1070 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 sqliteConchHostI
e1080 64 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 62 61 d".#endif../* ba
e1090 73 69 63 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f sically a copy o
e10a0 66 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 f unixRandomness
e10b0 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74 0a with different.
e10c0 2a 2a 20 74 65 73 74 20 62 65 68 61 76 69 6f 72 ** test behavior
e10d0 20 62 75 69 6c 74 20 69 6e 20 2a 2f 0a 73 74 61 built in */.sta
e10e0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 6e tic int proxyGen
e10f0 65 72 61 74 65 48 6f 73 74 49 44 28 63 68 61 72 erateHostID(char
e1100 20 2a 70 48 6f 73 74 49 44 29 7b 0a 20 20 69 6e *pHostID){. in
e1110 74 20 70 69 64 2c 20 66 64 2c 20 6c 65 6e 3b 0a t pid, fd, len;.
e1120 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
e1130 2a 6b 65 79 20 3d 20 28 75 6e 73 69 67 6e 65 64 *key = (unsigned
e1140 20 63 68 61 72 20 2a 29 70 48 6f 73 74 49 44 3b char *)pHostID;
e1150 0a 20 20 0a 20 20 6d 65 6d 73 65 74 28 6b 65 79 . . memset(key
e1160 2c 20 30 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b , 0, HOSTIDLEN);
e1170 0a 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 66 64 . len = 0;. fd
e1180 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 = open("/dev/ur
e1190 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 andom", O_RDONLY
e11a0 29 3b 0a 20 20 69 66 28 20 66 64 3e 3d 30 20 29 );. if( fd>=0 )
e11b0 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 72 65 61 64 {. len = read
e11c0 28 66 64 2c 20 6b 65 79 2c 20 48 4f 53 54 49 44 (fd, key, HOSTID
e11d0 4c 45 4e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 LEN);. close(
e11e0 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 fd); /* silently
e11f0 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 leak the fd if
e1200 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 7d 0a it fails */. }.
e1210 20 20 69 66 28 20 6c 65 6e 20 3c 20 48 4f 53 54 if( len < HOST
e1220 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 74 69 6d IDLEN ){. tim
e1230 65 5f 74 20 74 3b 0a 20 20 20 20 74 69 6d 65 28 e_t t;. time(
e1240 26 74 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 &t);. memcpy(
e1250 6b 65 79 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 key, &t, sizeof(
e1260 74 29 29 3b 0a 20 20 20 20 70 69 64 20 3d 20 67 t));. pid = g
e1270 65 74 70 69 64 28 29 3b 0a 20 20 20 20 6d 65 6d etpid();. mem
e1280 63 70 79 28 26 6b 65 79 5b 73 69 7a 65 6f 66 28 cpy(&key[sizeof(
e1290 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f t)], &pid, sizeo
e12a0 66 28 70 69 64 29 29 3b 0a 20 20 7d 0a 20 20 0a f(pid));. }. .
e12b0 23 69 66 64 65 66 20 4d 41 4b 45 5f 50 52 45 54 #ifdef MAKE_PRET
e12c0 54 59 5f 48 4f 53 54 49 44 0a 20 20 7b 0a 20 20 TY_HOSTID. {.
e12d0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 2f 2a 20 int i;. /*
e12e0 66 69 6c 74 65 72 20 74 68 65 20 62 79 74 65 73 filter the bytes
e12f0 20 69 6e 74 6f 20 70 72 69 6e 74 61 62 6c 65 20 into printable
e1300 61 73 63 69 69 20 63 68 61 72 61 63 74 65 72 73 ascii characters
e1310 20 61 6e 64 20 4e 55 4c 20 74 65 72 6d 69 6e 61 and NUL termina
e1320 74 65 20 2a 2f 0a 20 20 20 20 6b 65 79 5b 28 48 te */. key[(H
e1330 4f 53 54 49 44 4c 45 4e 2d 31 29 5d 20 3d 20 30 OSTIDLEN-1)] = 0
e1340 78 30 30 3b 0a 20 20 20 20 66 6f 72 28 20 69 3d x00;. for( i=
e1350 30 3b 20 69 3c 28 48 4f 53 54 49 44 4c 45 4e 2d 0; i<(HOSTIDLEN-
e1360 31 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 1); i++ ){.
e1370 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70 unsigned char p
e1380 61 20 3d 20 6b 65 79 5b 69 5d 26 30 78 37 46 3b a = key[i]&0x7F;
e1390 0a 20 20 20 20 20 20 69 66 28 20 70 61 3c 30 78 . if( pa<0x
e13a0 32 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 20 ){. ke
e13b0 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 y[i] = (key[i]&0
e13c0 78 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 x80 == 0x80) ? p
e13d0 61 2b 30 78 34 30 20 3a 20 70 61 2b 30 78 32 30 a+0x40 : pa+0x20
e13e0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
e13f0 28 20 70 61 3d 3d 30 78 37 46 20 29 7b 0a 20 20 ( pa==0x7F ){.
e1400 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 key[i] = (
e1410 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 key[i]&0x80 == 0
e1420 78 38 30 29 20 3f 20 70 61 3d 30 78 32 30 20 3a x80) ? pa=0x20 :
e1430 20 70 61 2b 30 78 37 45 3b 0a 20 20 20 20 20 20 pa+0x7E;.
e1440 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 }. }. }.#end
e1450 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 if. return SQLI
e1460 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 77 72 69 TE_OK;.}../* wri
e1470 74 65 73 20 74 68 65 20 68 6f 73 74 20 69 64 20 tes the host id
e1480 70 61 74 68 20 74 6f 20 70 61 74 68 2c 20 70 61 path to path, pa
e1490 74 68 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 th should be an
e14a0 70 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 62 75 pre-allocated bu
e14b0 66 66 65 72 0a 2a 2a 20 77 69 74 68 20 65 6e 6f ffer.** with eno
e14c0 75 67 68 20 73 70 61 63 65 20 66 6f 72 20 61 20 ugh space for a
e14d0 70 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 path .*/.static
e14e0 76 6f 69 64 20 70 72 6f 78 79 47 65 74 48 6f 73 void proxyGetHos
e14f0 74 49 44 50 61 74 68 28 63 68 61 72 20 2a 70 61 tIDPath(char *pa
e1500 74 68 2c 20 73 69 7a 65 5f 74 20 6c 65 6e 29 7b th, size_t len){
e1510 0a 20 20 73 74 72 6c 63 70 79 28 70 61 74 68 2c . strlcpy(path,
e1520 20 48 4f 53 54 49 44 50 41 54 48 2c 20 6c 65 6e HOSTIDPATH, len
e1530 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
e1540 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 _TEST. if( sqli
e1550 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3e 30 te3_hostid_num>0
e1560 20 29 7b 0a 20 20 20 20 63 68 61 72 20 73 75 66 ){. char suf
e1570 66 69 78 5b 32 5d 20 3d 20 22 31 22 3b 0a 20 20 fix[2] = "1";.
e1580 20 20 73 75 66 66 69 78 5b 30 5d 20 3d 20 73 75 suffix[0] = su
e1590 66 66 69 78 5b 30 5d 20 2b 20 73 71 6c 69 74 65 ffix[0] + sqlite
e15a0 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3b 0a 20 20 3_hostid_num;.
e15b0 20 20 73 74 72 6c 63 61 74 28 70 61 74 68 2c 20 strlcat(path,
e15c0 73 75 66 66 69 78 2c 20 6c 65 6e 29 3b 0a 20 20 suffix, len);.
e15d0 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 53 54 52 41 }.#endif. OSTRA
e15e0 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 50 41 CE3("GETHOSTIDPA
e15f0 54 48 20 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 TH %s pid=%d\n"
e1600 2c 20 70 61 74 68 2c 20 67 65 74 70 69 64 28 29 , path, getpid()
e1610 29 3b 0a 7d 0a 0a 2f 2a 20 67 65 74 20 74 68 65 );.}../* get the
e1620 20 68 6f 73 74 20 49 44 20 66 72 6f 6d 20 61 20 host ID from a
e1630 73 71 6c 69 74 65 20 68 6f 73 74 69 64 20 66 69 sqlite hostid fi
e1640 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 le stored in the
e1650 20 0a 2a 2a 20 75 73 65 72 2d 73 70 65 63 69 66 .** user-specif
e1660 69 63 20 74 6d 70 20 64 69 72 65 63 74 6f 72 79 ic tmp directory
e1670 2c 20 63 72 65 61 74 65 20 74 68 65 20 49 44 20 , create the ID
e1680 69 66 20 69 74 27 73 20 6e 6f 74 20 74 68 65 72 if it's not ther
e1690 65 20 61 6c 72 65 61 64 79 20 0a 2a 2f 0a 73 74 e already .*/.st
e16a0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 atic int proxyGe
e16b0 74 48 6f 73 74 49 44 28 63 68 61 72 20 2a 70 48 tHostID(char *pH
e16c0 6f 73 74 49 44 2c 20 69 6e 74 20 2a 70 45 72 72 ostID, int *pErr
e16d0 6f 72 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 or){. int fd;.
e16e0 20 63 68 61 72 20 70 61 74 68 5b 4d 41 58 50 41 char path[MAXPA
e16f0 54 48 4c 45 4e 5d 3b 20 0a 20 20 73 69 7a 65 5f THLEN]; . size_
e1700 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3d t len;. int rc=
e1710 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 70 72 SQLITE_OK;.. pr
e1720 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 oxyGetHostIDPath
e1730 28 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 (path, MAXPATHLE
e1740 4e 29 3b 0a 20 20 2f 2a 20 74 72 79 20 74 6f 20 N);. /* try to
e1750 63 72 65 61 74 65 20 74 68 65 20 68 6f 73 74 20 create the host
e1760 49 44 20 66 69 6c 65 2c 20 69 66 20 69 74 20 61 ID file, if it a
e1770 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 72 65 lready exists re
e1780 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ad the contents
e1790 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 */. fd = open(p
e17a0 61 74 68 2c 20 4f 5f 43 52 45 41 54 7c 4f 5f 57 ath, O_CREAT|O_W
e17b0 52 4f 4e 4c 59 7c 4f 5f 45 58 43 4c 2c 20 30 36 RONLY|O_EXCL, 06
e17c0 34 34 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 44);. if( fd<0
e17d0 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3d 65 ){. int err=e
e17e0 72 72 6e 6f 3b 0a 09 09 0a 20 20 20 20 69 66 28 rrno;.... if(
e17f0 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b 0a err!=EEXIST ){.
e1800 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52 #ifdef SQLITE_PR
e1810 4f 58 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 OXY_DEBUG /* set
e1820 20 74 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f the sqlite erro
e1830 72 20 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61 r message instea
e1840 64 20 2a 2f 0a 20 20 20 20 20 20 66 70 72 69 6e d */. fprin
e1850 74 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69 tf(stderr, "sqli
e1860 74 65 20 65 72 72 6f 72 20 63 72 65 61 74 69 6e te error creatin
e1870 67 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 g host ID file %
e1880 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 s: %s\n",.
e1890 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 path, st
e18a0 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 rerror(err));.#e
e18b0 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 ndif. retur
e18c0 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 n SQLITE_PERM;.
e18d0 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 75 6c }. /* coul
e18e0 64 6e 27 74 20 63 72 65 61 74 65 20 74 68 65 20 dn't create the
e18f0 66 69 6c 65 2c 20 72 65 61 64 20 69 74 20 69 6e file, read it in
e1900 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 66 64 20 stead */. fd
e1910 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 52 = open(path, O_R
e1920 44 4f 4e 4c 59 7c 4f 5f 45 58 43 4c 29 3b 0a 20 DONLY|O_EXCL);.
e1930 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 23 if( fd<0 ){.#
e1940 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f ifdef SQLITE_PRO
e1950 58 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 20 XY_DEBUG /* set
e1960 74 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f 72 the sqlite error
e1970 20 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61 64 message instead
e1980 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 72 */. int er
e1990 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 r = errno;.
e19a0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
e19b0 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6f "sqlite error o
e19c0 70 65 6e 69 6e 67 20 68 6f 73 74 20 49 44 20 66 pening host ID f
e19d0 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20 ile %s: %s\n",.
e19e0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74 pat
e19f0 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 h, strerror(err)
e1a00 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 );.#endif.
e1a10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 return SQLITE_PE
e1a20 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 RM;. }. le
e1a30 6e 20 3d 20 70 72 65 61 64 28 66 64 2c 20 70 48 n = pread(fd, pH
e1a40 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e ostID, HOSTIDLEN
e1a50 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 , 0);. if( le
e1a60 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 n<0 ){. *pE
e1a70 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rror = errno;.
e1a80 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e1a90 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 20 20 IOERR_READ;.
e1aa0 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f }else if( len<HO
e1ab0 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 STIDLEN ){.
e1ac0 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 *pError = 0;.
e1ad0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e1ae0 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
e1af0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 ;. }. clos
e1b00 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 e(fd); /* silent
e1b10 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 ly leak the fd i
e1b20 66 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 f it fails */.
e1b30 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 OSTRACE3("GETH
e1b40 4f 53 54 49 44 20 20 72 65 61 64 20 25 73 20 70 OSTID read %s p
e1b50 69 64 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 id=%d\n", pHostI
e1b60 44 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 D, getpid());.
e1b70 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
e1b80 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 77 65 27 else{. /* we'
e1b90 72 65 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 re creating the
e1ba0 68 6f 73 74 20 49 44 20 66 69 6c 65 20 28 75 73 host ID file (us
e1bb0 65 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e e a random strin
e1bc0 67 20 6f 66 20 62 79 74 65 73 29 20 2a 2f 0a 20 g of bytes) */.
e1bd0 20 20 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 proxyGenerate
e1be0 48 6f 73 74 49 44 28 70 48 6f 73 74 49 44 29 3b HostID(pHostID);
e1bf0 0a 20 20 20 20 6c 65 6e 20 3d 20 70 77 72 69 74 . len = pwrit
e1c00 65 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 e(fd, pHostID, H
e1c10 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 OSTIDLEN, 0);.
e1c20 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 if( len<0 ){.
e1c30 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 *pError = e
e1c40 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
e1c50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 SQLITE_IOERR_WR
e1c60 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 ITE;. }else i
e1c70 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e f( len<HOSTIDLEN
e1c80 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f ){. *pErro
e1c90 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 r = 0;. rc
e1ca0 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 = SQLITE_IOERR_W
e1cb0 52 49 54 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 RITE;. }.
e1cc0 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 close(fd); /* si
e1cd0 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 lently leak the
e1ce0 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a fd if it fails *
e1cf0 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 /. OSTRACE3("
e1d00 47 45 54 48 4f 53 54 49 44 20 20 77 72 6f 74 65 GETHOSTID wrote
e1d10 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %s pid=%d\n", p
e1d20 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 29 HostID, getpid()
e1d30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 );. return rc
e1d40 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 ;. }.}..static
e1d50 69 6e 74 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b int proxyGetLock
e1d60 50 61 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 Path(const char
e1d70 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a 6c *dbPath, char *l
e1d80 50 61 74 68 2c 20 73 69 7a 65 5f 74 20 6d 61 78 Path, size_t max
e1d90 4c 65 6e 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b Len){. int len;
e1da0 0a 20 20 69 6e 74 20 64 62 4c 65 6e 3b 0a 20 20 . int dbLen;.
e1db0 69 6e 74 20 69 3b 0a 0a 23 69 66 64 65 66 20 4c int i;..#ifdef L
e1dc0 4f 43 4b 50 52 4f 58 59 44 49 52 0a 20 20 6c 65 OCKPROXYDIR. le
e1dd0 6e 20 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74 n = strlcpy(lPat
e1de0 68 2c 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 2c h, LOCKPROXYDIR,
e1df0 20 6d 61 78 4c 65 6e 29 3b 0a 23 65 6c 73 65 0a maxLen);.#else.
e1e00 23 20 69 66 64 65 66 20 5f 43 53 5f 44 41 52 57 # ifdef _CS_DARW
e1e10 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 IN_USER_TEMP_DIR
e1e20 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 66 73 74 72 . {. confstr
e1e30 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 (_CS_DARWIN_USER
e1e40 5f 54 45 4d 50 5f 44 49 52 2c 20 6c 50 61 74 68 _TEMP_DIR, lPath
e1e50 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 20 6c , maxLen);. l
e1e60 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 en = strlcat(lPa
e1e70 74 68 2c 20 22 73 71 6c 69 74 65 70 6c 6f 63 6b th, "sqliteplock
e1e80 73 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 s", maxLen);.
e1e90 20 69 66 28 20 6d 6b 64 69 72 28 6c 50 61 74 68 if( mkdir(lPath
e1ea0 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 , SQLITE_DEFAULT
e1eb0 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 _PROXYDIR_PERMIS
e1ec0 53 49 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20 SIONS) ){.
e1ed0 2f 2a 20 69 66 20 6d 6b 64 69 72 20 66 61 69 6c /* if mkdir fail
e1ee0 73 2c 20 68 61 6e 64 6c 65 20 61 73 20 6c 6f 63 s, handle as loc
e1ef0 6b 20 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20 k file creation
e1f00 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 20 20 20 failure */.
e1f10 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f int err = errno
e1f20 3b 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 ;.# ifdef SQLIT
e1f30 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 E_DEBUG. if
e1f40 28 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b ( err!=EEXIST ){
e1f50 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 . fprintf
e1f60 28 73 74 64 65 72 72 2c 20 22 70 72 6f 78 79 47 (stderr, "proxyG
e1f70 65 74 4c 6f 63 6b 50 61 74 68 3a 20 6d 6b 64 69 etLockPath: mkdi
e1f80 72 28 25 73 2c 30 25 6f 29 20 65 72 72 6f 72 20 r(%s,0%o) error
e1f90 25 64 20 25 73 5c 6e 22 2c 20 6c 50 61 74 68 2c %d %s\n", lPath,
e1fa0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
e1fb0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
e1fc0 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
e1fd0 49 4f 4e 53 2c 20 65 72 72 2c 20 73 74 72 65 72 IONS, err, strer
e1fe0 72 6f 72 28 65 72 72 29 29 3b 0a 20 20 20 20 20 ror(err));.
e1ff0 20 7d 0a 23 20 20 65 6e 64 69 66 0a 20 20 20 20 }.# endif.
e2000 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54 }else{. OST
e2010 52 41 43 45 33 28 22 47 45 54 4c 4f 43 4b 50 41 RACE3("GETLOCKPA
e2020 54 48 20 20 6d 6b 64 69 72 20 25 73 20 70 69 64 TH mkdir %s pid
e2030 3d 25 64 5c 6e 22 2c 20 6c 50 61 74 68 2c 20 67 =%d\n", lPath, g
e2040 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 7d 0a etpid());. }.
e2050 20 20 20 20 0a 20 20 7d 0a 23 20 65 6c 73 65 0a . }.# else.
e2060 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28 len = strlcpy(
e2070 6c 50 61 74 68 2c 20 22 2f 74 6d 70 2f 22 2c 20 lPath, "/tmp/",
e2080 6d 61 78 4c 65 6e 29 3b 0a 23 20 65 6e 64 69 66 maxLen);.# endif
e2090 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6c .#endif.. if( l
e20a0 50 61 74 68 5b 6c 65 6e 2d 31 5d 21 3d 27 2f 27 Path[len-1]!='/'
e20b0 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 ){. len = st
e20c0 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 2f 22 rlcat(lPath, "/"
e20d0 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 7d 0a 20 , maxLen);. }.
e20e0 20 0a 20 20 2f 2a 20 74 72 61 6e 73 66 6f 72 6d . /* transform
e20f0 20 74 68 65 20 64 62 20 70 61 74 68 20 74 6f 20 the db path to
e2100 61 20 75 6e 69 71 75 65 20 63 61 63 68 65 20 6e a unique cache n
e2110 61 6d 65 20 2a 2f 0a 20 20 64 62 4c 65 6e 20 3d ame */. dbLen =
e2120 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 (int)strlen(dbP
e2130 61 74 68 29 3b 0a 20 20 66 6f 72 28 20 69 3d 30 ath);. for( i=0
e2140 3b 20 69 3c 64 62 4c 65 6e 20 26 26 20 28 69 2b ; i<dbLen && (i+
e2150 6c 65 6e 2b 37 29 3c 6d 61 78 4c 65 6e 3b 20 69 len+7)<maxLen; i
e2160 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 63 20 ++){. char c
e2170 3d 20 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20 = dbPath[i];.
e2180 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d 20 3d 20 lPath[i+len] =
e2190 28 63 3d 3d 27 2f 27 29 3f 27 5f 27 3a 63 3b 0a (c=='/')?'_':c;.
e21a0 20 20 7d 0a 20 20 6c 50 61 74 68 5b 69 2b 6c 65 }. lPath[i+le
e21b0 6e 5d 3d 27 5c 30 27 3b 0a 20 20 73 74 72 6c 63 n]='\0';. strlc
e21c0 61 74 28 6c 50 61 74 68 2c 20 22 3a 61 75 74 6f at(lPath, ":auto
e21d0 3a 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 72 :", maxLen);. r
e21e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e21f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
e2200 20 61 20 6e 65 77 20 56 46 53 20 66 69 6c 65 20 a new VFS file
e2210 64 65 73 63 72 69 70 74 6f 72 20 28 73 74 6f 72 descriptor (stor
e2220 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 ed in memory obt
e2230 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 ained from.** sq
e2240 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 29 20 61 6e lite3_malloc) an
e2250 64 20 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 d open the file
e2260 6e 61 6d 65 64 20 22 70 61 74 68 22 20 69 6e 20 named "path" in
e2270 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
e2280 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 tor..**.** The c
e2290 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 aller is respons
e22a0 69 62 6c 65 20 6e 6f 74 20 6f 6e 6c 79 20 66 6f ible not only fo
e22b0 72 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 66 69 r closing the fi
e22c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a le descriptor.**
e22d0 20 62 75 74 20 61 6c 73 6f 20 66 6f 72 20 66 72 but also for fr
e22e0 65 65 69 6e 67 20 74 68 65 20 6d 65 6d 6f 72 79 eeing the memory
e22f0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
e2300 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
e2310 70 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ptor..*/.static
e2320 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 55 int proxyCreateU
e2330 6e 69 78 46 69 6c 65 28 63 6f 6e 73 74 20 63 68 nixFile(const ch
e2340 61 72 20 2a 70 61 74 68 2c 20 75 6e 69 78 46 69 ar *path, unixFi
e2350 6c 65 20 2a 2a 70 70 46 69 6c 65 29 20 7b 0a 20 le **ppFile) {.
e2360 20 69 6e 74 20 66 64 3b 0a 20 20 69 6e 74 20 64 int fd;. int d
e2370 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 75 6e 69 irfd = -1;. uni
e2380 78 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69 xFile *pNew;. i
e2390 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
e23a0 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 K;. sqlite3_vfs
e23b0 20 64 75 6d 6d 79 56 66 73 3b 0a 0a 20 20 66 64 dummyVfs;.. fd
e23c0 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f = open(path, O_
e23d0 52 44 57 52 20 7c 20 4f 5f 43 52 45 41 54 2c 20 RDWR | O_CREAT,
e23e0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
e23f0 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 ILE_PERMISSIONS)
e2400 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a ;. if( fd<0 ){.
e2410 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e2420 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a E_CANTOPEN;. }.
e2430 20 20 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69 . pNew = (uni
e2440 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f xFile *)sqlite3_
e2450 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e malloc(sizeof(un
e2460 69 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20 ixFile));. if(
e2470 70 4e 65 77 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 pNew==NULL ){.
e2480 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
e2490 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e MEM;. goto en
e24a0 64 5f 63 72 65 61 74 65 5f 70 72 6f 78 79 3b 0a d_create_proxy;.
e24b0 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65 }. memset(pNe
e24c0 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 w, 0, sizeof(uni
e24d0 78 46 69 6c 65 29 29 3b 0a 0a 20 20 64 75 6d 6d xFile));.. dumm
e24e0 79 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20 yVfs.pAppData =
e24f0 28 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b (void*)&autolock
e2500 49 6f 46 69 6e 64 65 72 3b 0a 20 20 72 63 20 3d IoFinder;. rc =
e2510 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 fillInUnixFile(
e2520 26 64 75 6d 6d 79 56 66 73 2c 20 66 64 2c 20 64 &dummyVfs, fd, d
e2530 69 72 66 64 2c 20 28 73 71 6c 69 74 65 33 5f 66 irfd, (sqlite3_f
e2540 69 6c 65 2a 29 70 4e 65 77 2c 20 70 61 74 68 2c ile*)pNew, path,
e2550 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 0, 0);. if( rc
e2560 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
e2570 20 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 *ppFile = pNe
e2580 77 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 w;. return SQ
e2590 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 65 6e 64 LITE_OK;. }.end
e25a0 5f 63 72 65 61 74 65 5f 70 72 6f 78 79 3a 20 20 _create_proxy:
e25b0 20 20 0a 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 . close(fd);
e25c0 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b /* silently leak
e25d0 20 66 64 20 69 66 20 65 72 72 6f 72 2c 20 77 65 fd if error, we
e25e0 27 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20 65 're already in e
e25f0 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 rror */. sqlite
e2600 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 3_free(pNew);.
e2610 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
e2620 20 74 61 6b 65 73 20 74 68 65 20 63 6f 6e 63 68 takes the conch
e2630 20 62 79 20 74 61 6b 69 6e 67 20 61 20 73 68 61 by taking a sha
e2640 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 red lock and rea
e2650 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 63 d the contents c
e2660 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a 20 6c 6f 63 onch, if .** loc
e2670 6b 50 61 74 68 20 69 73 20 6e 6f 6e 2d 4e 55 4c kPath is non-NUL
e2680 4c 2c 20 74 68 65 20 68 6f 73 74 20 49 44 20 61 L, the host ID a
e2690 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 nd lock file pat
e26a0 68 20 6d 75 73 74 20 6d 61 74 63 68 2e 20 20 41 h must match. A
e26b0 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f 63 6b 50 61 NULL .** lockPa
e26c0 74 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 th means that th
e26d0 65 20 6c 6f 63 6b 50 61 74 68 20 69 6e 20 74 68 e lockPath in th
e26e0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 6c e conch file wil
e26f0 6c 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65 l be used if the
e2700 20 0a 2a 2a 20 68 6f 73 74 20 49 44 73 20 6d 61 .** host IDs ma
e2710 74 63 68 2c 20 6f 72 20 61 20 6e 65 77 20 6c 6f tch, or a new lo
e2720 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 ck path will be
e2730 67 65 6e 65 72 61 74 65 64 20 61 75 74 6f 6d 61 generated automa
e2740 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 61 6e 64 20 tically .** and
e2750 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 63 written to the c
e2760 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 onch file..*/.st
e2770 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 61 atic int proxyTa
e2780 6b 65 43 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 keConch(unixFile
e2790 20 2a 70 46 69 6c 65 29 7b 0a 20 20 70 72 6f 78 *pFile){. prox
e27a0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
e27b0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
e27c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 ckingContext *)p
e27d0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
e27e0 74 65 78 74 3b 20 0a 20 20 0a 20 20 69 66 28 20 text; . . if(
e27f0 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 3e pCtx->conchHeld>
e2800 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
e2810 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
e2820 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 se{. unixFile
e2830 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 *conchFile = pC
e2840 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 tx->conchFile;.
e2850 20 20 20 63 68 61 72 20 74 65 73 74 56 61 6c 75 char testValu
e2860 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 e[CONCHLEN];.
e2870 20 63 68 61 72 20 63 6f 6e 63 68 56 61 6c 75 65 char conchValue
e2880 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 [CONCHLEN];.
e2890 63 68 61 72 20 6c 6f 63 6b 50 61 74 68 5b 4d 41 char lockPath[MA
e28a0 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 XPATHLEN];. c
e28b0 68 61 72 20 2a 74 4c 6f 63 6b 50 61 74 68 20 3d har *tLockPath =
e28c0 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 NULL;. int r
e28d0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
e28e0 20 20 20 69 6e 74 20 72 65 61 64 52 63 20 3d 20 int readRc =
e28f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 SQLITE_OK;. i
e2900 6e 74 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 30 nt syncPerms = 0
e2910 3b 0a 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 ;.. OSTRACE4(
e2920 22 54 41 4b 45 43 4f 4e 43 48 20 20 25 64 20 66 "TAKECONCH %d f
e2930 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c or %s pid=%d\n",
e2940 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 conchFile->h,.
e2950 20 20 20 20 20 20 20 20 20 20 20 20 28 70 43 74 (pCt
e2960 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
e2970 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f ? pCtx->lockPro
e2980 78 79 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a xyPath : ":auto:
e2990 22 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a "), getpid());..
e29a0 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 rc = conchFi
e29b0 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f le->pMethod->xLo
e29c0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
e29d0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 *)conchFile, SHA
e29e0 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 RED_LOCK);. i
e29f0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
e2a00 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 45 ){. int pE
e2a10 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 rror = 0;.
e2a20 6d 65 6d 73 65 74 28 74 65 73 74 56 61 6c 75 65 memset(testValue
e2a30 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 4e 29 3b 20 , 0, CONCHLEN);
e2a40 2f 2a 20 63 6f 6e 63 68 20 69 73 20 66 69 78 65 /* conch is fixe
e2a50 64 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 d size */.
e2a60 72 63 20 3d 20 70 72 6f 78 79 47 65 74 48 6f 73 rc = proxyGetHos
e2a70 74 49 44 28 74 65 73 74 56 61 6c 75 65 2c 20 26 tID(testValue, &
e2a80 70 45 72 72 6f 72 29 3b 0a 20 20 20 20 20 20 69 pError);. i
e2a90 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51 f( (rc&0xff)==SQ
e2aa0 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 LITE_IOERR ){.
e2ab0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
e2ac0 74 45 72 72 6e 6f 20 3d 20 70 45 72 72 6f 72 3b tErrno = pError;
e2ad0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
e2ae0 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f f( pCtx->lockPro
e2af0 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 xyPath ){.
e2b00 20 20 73 74 72 6c 63 70 79 28 26 74 65 73 74 56 strlcpy(&testV
e2b10 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c alue[HOSTIDLEN],
e2b20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
e2b30 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e Path, MAXPATHLEN
e2b40 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
e2b50 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
e2b60 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
e2b70 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e goto end_takecon
e2b80 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 ch;. }. .
e2b90 20 20 20 72 65 61 64 52 63 20 3d 20 75 6e 69 78 readRc = unix
e2ba0 52 65 61 64 28 28 73 71 6c 69 74 65 33 5f 66 69 Read((sqlite3_fi
e2bb0 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 le *)conchFile,
e2bc0 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 conchValue, CONC
e2bd0 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 HLEN, 0);. if
e2be0 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 45 ( readRc!=SQLITE
e2bf0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
e2c00 44 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 D ){. if( r
e2c10 65 61 64 52 63 21 3d 53 51 4c 49 54 45 5f 4f 4b eadRc!=SQLITE_OK
e2c20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
e2c30 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 (rc&0xff)==SQLIT
e2c40 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 E_IOERR ){.
e2c50 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
e2c60 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 68 46 69 6c Errno = conchFil
e2c70 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 e->lastErrno;.
e2c80 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
e2c90 72 63 20 3d 20 72 65 61 64 52 63 3b 0a 20 20 20 rc = readRc;.
e2ca0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 goto end_ta
e2cb0 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 7d keconch;. }
e2cc0 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 74 68 65 . /* if the
e2cd0 20 63 6f 6e 63 68 20 68 61 73 20 64 61 74 61 20 conch has data
e2ce0 63 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 compare the cont
e2cf0 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 66 ents */. if
e2d00 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f ( !pCtx->lockPro
e2d10 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 xyPath ){.
e2d20 20 20 2f 2a 20 66 6f 72 20 61 75 74 6f 2d 6e 61 /* for auto-na
e2d30 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 med local lock f
e2d40 69 6c 65 2c 20 6a 75 73 74 20 63 68 65 63 6b 20 ile, just check
e2d50 74 68 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 the host ID and
e2d60 77 65 27 6c 6c 0a 20 20 20 20 20 20 20 20 20 2a we'll. *
e2d70 2a 20 75 73 65 20 74 68 65 20 6c 6f 63 61 6c 20 * use the local
e2d80 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 20 74 lock file path t
e2d90 68 61 74 27 73 20 61 6c 72 65 61 64 79 20 69 6e hat's already in
e2da0 20 74 68 65 72 65 20 2a 2f 0a 20 20 20 20 20 20 there */.
e2db0 20 20 69 66 28 20 21 6d 65 6d 63 6d 70 28 74 65 if( !memcmp(te
e2dc0 73 74 56 61 6c 75 65 2c 20 63 6f 6e 63 68 56 61 stValue, conchVa
e2dd0 6c 75 65 2c 20 48 4f 53 54 49 44 4c 45 4e 29 20 lue, HOSTIDLEN)
e2de0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 4c 6f ){. tLo
e2df0 63 6b 50 61 74 68 20 3d 20 28 63 68 61 72 20 2a ckPath = (char *
e2e00 29 26 63 6f 6e 63 68 56 61 6c 75 65 5b 48 4f 53 )&conchValue[HOS
e2e10 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 20 20 20 20 TIDLEN];.
e2e20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 goto end_take
e2e30 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d conch;. }
e2e40 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
e2e50 20 20 20 20 20 20 2f 2a 20 77 65 27 76 65 20 67 /* we've g
e2e60 6f 74 20 74 68 65 20 63 6f 6e 63 68 20 69 66 20 ot the conch if
e2e70 63 6f 6e 63 68 56 61 6c 75 65 20 6d 61 74 63 68 conchValue match
e2e80 65 73 20 6f 75 72 20 70 61 74 68 20 61 6e 64 20 es our path and
e2e90 68 6f 73 74 20 49 44 20 2a 2f 0a 20 20 20 20 20 host ID */.
e2ea0 20 20 20 69 66 28 20 21 6d 65 6d 63 6d 70 28 74 if( !memcmp(t
e2eb0 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e 63 68 56 estValue, conchV
e2ec0 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 29 20 alue, CONCHLEN)
e2ed0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
e2ee0 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b o end_takeconch;
e2ef0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
e2f00 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
e2f10 20 20 20 20 2f 2a 20 61 20 73 68 6f 72 74 20 72 /* a short r
e2f20 65 61 64 20 6d 65 61 6e 73 20 77 65 27 72 65 20 ead means we're
e2f30 22 63 72 65 61 74 69 6e 67 22 20 74 68 65 20 63 "creating" the c
e2f40 6f 6e 63 68 20 28 65 76 65 6e 20 74 68 6f 75 67 onch (even thoug
e2f50 68 20 69 74 20 63 6f 75 6c 64 20 0a 20 20 20 20 h it could .
e2f60 20 20 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 75 ** have been u
e2f70 73 65 72 2d 69 6e 74 65 72 76 65 6e 74 69 6f 6e ser-intervention
e2f80 29 2c 20 69 66 20 77 65 20 61 63 71 75 69 72 65 ), if we acquire
e2f90 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c the exclusive l
e2fa0 6f 63 6b 2c 0a 20 20 20 20 20 20 2a 2a 20 77 65 ock,. ** we
e2fb0 27 6c 6c 20 74 72 79 20 74 6f 20 6d 61 74 63 68 'll try to match
e2fc0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 6e 2d the current on-
e2fd0 64 69 73 6b 20 70 65 72 6d 69 73 73 69 6f 6e 73 disk permissions
e2fe0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
e2ff0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
e3000 73 79 6e 63 50 65 72 6d 73 20 3d 20 31 3b 0a 20 syncPerms = 1;.
e3010 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a }. . /*
e3020 20 65 69 74 68 65 72 20 63 6f 6e 63 68 20 77 61 either conch wa
e3030 73 20 65 6d 74 70 79 20 6f 72 20 64 69 64 6e 27 s emtpy or didn'
e3040 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 69 t match */. i
e3050 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 f( !pCtx->lockPr
e3060 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20 oxyPath ){.
e3070 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 proxyGetLockPat
e3080 68 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 h(pCtx->dbPath,
e3090 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 50 41 54 lockPath, MAXPAT
e30a0 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 74 4c 6f HLEN);. tLo
e30b0 63 6b 50 61 74 68 20 3d 20 6c 6f 63 6b 50 61 74 ckPath = lockPat
e30c0 68 3b 0a 20 20 20 20 20 20 73 74 72 6c 63 70 79 h;. strlcpy
e30d0 28 26 74 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 (&testValue[HOST
e30e0 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b 50 61 74 68 IDLEN], lockPath
e30f0 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 , MAXPATHLEN);.
e3100 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a }. . /*
e3110 20 75 70 64 61 74 65 20 63 6f 6e 63 68 20 77 69 update conch wi
e3120 74 68 20 68 6f 73 74 20 61 6e 64 20 70 61 74 68 th host and path
e3130 20 28 74 68 69 73 20 77 69 6c 6c 20 66 61 69 6c (this will fail
e3140 20 69 66 20 6f 74 68 65 72 20 70 72 6f 63 65 73 if other proces
e3150 73 0a 20 20 20 20 20 2a 2a 20 68 61 73 20 61 20 s. ** has a
e3160 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6c 72 65 shared lock alre
e3170 61 64 79 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d ady) */. rc =
e3180 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 conchFile->pMet
e3190 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 hod->xLock((sqli
e31a0 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 te3_file*)conchF
e31b0 69 6c 65 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c ile, EXCLUSIVE_L
e31c0 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 OCK);. if( rc
e31d0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
e31e0 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 57 72 rc = unixWr
e31f0 69 74 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ite((sqlite3_fil
e3200 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 74 e *)conchFile, t
e3210 65 73 74 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c estValue, CONCHL
e3220 45 4e 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 EN, 0);. if
e3230 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
e3240 26 26 20 73 79 6e 63 50 65 72 6d 73 20 29 7b 0a && syncPerms ){.
e3250 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 73 struct s
e3260 74 61 74 20 62 75 66 3b 0a 20 20 20 20 20 20 20 tat buf;.
e3270 20 69 6e 74 20 65 72 72 20 3d 20 66 73 74 61 74 int err = fstat
e3280 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62 75 66 29 (pFile->h, &buf)
e3290 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 72 ;. if( er
e32a0 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 r==0 ){.
e32b0 20 20 2f 2a 20 74 72 79 20 74 6f 20 6d 61 74 63 /* try to matc
e32c0 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 h the database f
e32d0 69 6c 65 20 70 65 72 6d 69 73 73 69 6f 6e 73 2c ile permissions,
e32e0 20 69 67 6e 6f 72 65 20 66 61 69 6c 75 72 65 20 ignore failure
e32f0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
e3300 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 0a 20 20 E_PROXY_DEBUG.
e3310 20 20 20 20 20 20 20 20 66 63 68 6d 6f 64 28 63 fchmod(c
e3320 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 onchFile->h, buf
e3330 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 65 6c 73 65 .st_mode);.#else
e3340 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 . if( f
e3350 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d chmod(conchFile-
e3360 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 >h, buf.st_mode)
e3370 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=0 ){.
e3380 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 int code = er
e3390 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 rno;.
e33a0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
e33b0 20 22 66 63 68 6d 6f 64 20 25 6f 20 46 41 49 4c "fchmod %o FAIL
e33c0 45 44 20 77 69 74 68 20 25 64 20 25 73 5c 6e 22 ED with %d %s\n"
e33d0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
e33e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 b
e33f0 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 63 6f 64 65 uf.st_mode, code
e3400 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29 , strerror(code)
e3410 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20 65 );. } e
e3420 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 20 lse {.
e3430 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
e3440 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 53 55 43 , "fchmod %o SUC
e3450 43 45 44 45 44 5c 6e 22 2c 62 75 66 2e 73 74 5f CEDED\n",buf.st_
e3460 6d 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 20 mode);.
e3470 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
e3480 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 {. int
e3490 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 code = errno;.
e34a0 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 fprintf(
e34b0 73 74 64 65 72 72 2c 20 22 53 54 41 54 20 46 41 stderr, "STAT FA
e34c0 49 4c 45 44 5b 25 64 5d 20 77 69 74 68 20 25 64 ILED[%d] with %d
e34d0 20 25 73 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 %s\n", .
e34e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e34f0 20 20 20 65 72 72 2c 20 63 6f 64 65 2c 20 73 74 err, code, st
e3500 72 65 72 72 6f 72 28 63 6f 64 65 29 29 3b 0a 23 rerror(code));.#
e3510 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7d 0a endif. }.
e3520 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
e3530 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 conchFile->pMe
e3540 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 thod->xUnlock((s
e3550 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
e3560 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c chFile, SHARED_L
e3570 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 5f 74 61 6b OCK);. .end_tak
e3580 65 63 6f 6e 63 68 3a 0a 20 20 20 20 4f 53 54 52 econch:. OSTR
e3590 41 43 45 32 28 22 54 52 41 4e 53 50 52 4f 58 59 ACE2("TRANSPROXY
e35a0 3a 20 43 4c 4f 53 45 20 20 25 64 5c 6e 22 2c 20 : CLOSE %d\n",
e35b0 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 69 pFile->h);. i
e35c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
e35d0 20 26 26 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 && pFile->openF
e35e0 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 69 66 lags ){. if
e35f0 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b ( pFile->h>=0 ){
e3600 0a 23 69 66 64 65 66 20 53 54 52 49 43 54 5f 43 .#ifdef STRICT_C
e3610 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 20 20 20 20 LOSE_ERROR.
e3620 20 20 20 69 66 28 20 63 6c 6f 73 65 28 70 46 69 if( close(pFi
e3630 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 20 20 le->h) ){.
e3640 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
e3650 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
e3660 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
e3670 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 QLITE_IOERR_CLOS
e3680 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6c E;. }.#el
e3690 73 65 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 se. close
e36a0 28 70 46 69 6c 65 2d 3e 68 29 3b 20 2f 2a 20 73 (pFile->h); /* s
e36b0 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 66 64 20 ilently leak fd
e36c0 69 66 20 66 61 69 6c 20 2a 2f 0a 23 65 6e 64 69 if fail */.#endi
e36d0 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 f. }.
e36e0 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 pFile->h = -1;.
e36f0 20 20 20 20 20 69 6e 74 20 66 64 20 3d 20 6f 70 int fd = op
e3700 65 6e 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c en(pCtx->dbPath,
e3710 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 pFile->openFlag
e3720 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 s,.
e3730 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 SQLITE_DE
e3740 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
e3750 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 20 20 4f SSIONS);. O
e3760 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 52 STRACE2("TRANSPR
e3770 4f 58 59 3a 20 4f 50 45 4e 20 20 25 64 5c 6e 22 OXY: OPEN %d\n"
e3780 2c 20 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 , fd);. if(
e3790 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 fd>=0 ){.
e37a0 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 66 64 3b pFile->h = fd;
e37b0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
e37c0 20 20 20 20 20 20 72 63 3d 53 51 4c 49 54 45 5f rc=SQLITE_
e37d0 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a 20 53 51 4c CANTOPEN; /* SQL
e37e0 49 54 45 5f 42 55 53 59 3f 20 70 72 6f 78 79 54 ITE_BUSY? proxyT
e37f0 61 6b 65 43 6f 6e 63 68 20 63 61 6c 6c 65 64 0a akeConch called.
e3800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
e3820 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f uring locking */
e3830 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
e3840 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
e3850 45 5f 4f 4b 20 26 26 20 21 70 43 74 78 2d 3e 6c E_OK && !pCtx->l
e3860 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 ockProxy ){.
e3870 20 20 63 68 61 72 20 2a 70 61 74 68 20 3d 20 74 char *path = t
e3880 4c 6f 63 6b 50 61 74 68 20 3f 20 74 4c 6f 63 6b LockPath ? tLock
e3890 50 61 74 68 20 3a 20 70 43 74 78 2d 3e 6c 6f 63 Path : pCtx->loc
e38a0 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 kProxyPath;.
e38b0 20 20 2f 2a 20 41 43 53 3a 20 4e 65 65 64 20 74 /* ACS: Need t
e38c0 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 o make a copy of
e38d0 20 70 61 74 68 20 73 6f 6d 65 74 69 6d 65 73 20 path sometimes
e38e0 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 72 */. rc = pr
e38f0 6f 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c oxyCreateUnixFil
e3900 65 28 70 61 74 68 2c 20 26 70 43 74 78 2d 3e 6c e(path, &pCtx->l
e3910 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d ockProxy);. }
e3920 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
e3930 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
e3940 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 pCtx->conchHeld
e3950 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 = 1;.. if(
e3960 74 4c 6f 63 6b 50 61 74 68 20 29 7b 0a 20 20 20 tLockPath ){.
e3970 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 pCtx->lockP
e3980 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74 roxyPath = sqlit
e3990 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 74 4c e3DbStrDup(0, tL
e39a0 6f 63 6b 50 61 74 68 29 3b 0a 20 20 20 20 20 20 ockPath);.
e39b0 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b if( pCtx->lock
e39c0 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 20 3d Proxy->pMethod =
e39d0 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 = &afpIoMethods
e39e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 28 28 61 ){. ((a
e39f0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
e3a00 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f *)pCtx->lockPro
e3a10 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 xy->lockingConte
e3a20 78 74 29 2d 3e 64 62 50 61 74 68 20 3d 0a 20 20 xt)->dbPath =.
e3a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3a40 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f pCtx->lockPro
e3a50 78 79 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20 xyPath;.
e3a60 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 }. }. }
e3a70 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 63 6f 6e else {. con
e3a80 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d chFile->pMethod-
e3a90 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 >xUnlock((sqlite
e3aa0 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 3_file*)conchFil
e3ab0 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 e, NO_LOCK);.
e3ac0 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 }. OSTRACE3(
e3ad0 22 54 41 4b 45 43 4f 4e 43 48 20 20 25 64 20 25 "TAKECONCH %d %
e3ae0 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d s\n", conchFile-
e3af0 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f >h, rc==SQLITE_O
e3b00 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c 65 64 22 29 K?"ok":"failed")
e3b10 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
e3b20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
e3b30 20 70 46 69 6c 65 20 68 6f 6c 64 73 20 61 20 6c pFile holds a l
e3b40 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e 63 68 20 66 ock on a conch f
e3b50 69 6c 65 2c 20 74 68 65 6e 20 72 65 6c 65 61 73 ile, then releas
e3b60 65 20 74 68 61 74 20 6c 6f 63 6b 2e 0a 2a 2f 0a e that lock..*/.
e3b70 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
e3b80 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 75 6e 69 ReleaseConch(uni
e3b90 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 xFile *pFile){.
e3ba0 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
e3bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e3bc0 53 75 62 72 6f 75 74 69 6e 65 20 72 65 74 75 72 Subroutine retur
e3bd0 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 70 72 6f 78 n code */. prox
e3be0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
e3bf0 2a 70 43 74 78 3b 20 20 2f 2a 20 54 68 65 20 6c *pCtx; /* The l
e3c00 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 ocking context f
e3c10 6f 72 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 or the proxy loc
e3c20 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 k */. unixFile
e3c30 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 *conchFile;
e3c40 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
e3c50 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a e conch file */.
e3c60 0a 20 20 70 43 74 78 20 3d 20 28 70 72 6f 78 79 . pCtx = (proxy
e3c70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
e3c80 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
e3c90 6f 6e 74 65 78 74 3b 0a 20 20 63 6f 6e 63 68 46 ontext;. conchF
e3ca0 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 ile = pCtx->conc
e3cb0 68 46 69 6c 65 3b 0a 20 20 4f 53 54 52 41 43 45 hFile;. OSTRACE
e3cc0 34 28 22 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 4("RELEASECONCH
e3cd0 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 %d for %s pid=%
e3ce0 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d d\n", conchFile-
e3cf0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 >h,. (
e3d00 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
e3d10 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b ath ? pCtx->lock
e3d20 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 75 ProxyPath : ":au
e3d30 74 6f 3a 22 29 2c 20 0a 20 20 20 20 20 20 20 20 to:"), .
e3d40 20 20 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 getpid());.
e3d50 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 pCtx->conchHeld
e3d60 3d 20 30 3b 0a 20 20 72 63 20 3d 20 63 6f 6e 63 = 0;. rc = conc
e3d70 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e hFile->pMethod->
e3d80 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 xUnlock((sqlite3
e3d90 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 _file*)conchFile
e3da0 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 4f 53 , NO_LOCK);. OS
e3db0 54 52 41 43 45 33 28 22 52 45 4c 45 41 53 45 43 TRACE3("RELEASEC
e3dc0 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 ONCH %d %s\n",
e3dd0 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 conchFile->h,.
e3de0 20 20 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 (rc==SQ
e3df0 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a LITE_OK ? "ok" :
e3e00 20 22 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 "failed"));. r
e3e10 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
e3e20 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d ** Given the nam
e3e30 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 e of a database
e3e40 66 69 6c 65 2c 20 63 6f 6d 70 75 74 65 20 74 68 file, compute th
e3e50 65 20 6e 61 6d 65 20 6f 66 20 69 74 73 20 63 6f e name of its co
e3e60 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a 20 53 74 6f nch file..** Sto
e3e70 72 65 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c re the conch fil
e3e80 65 6e 61 6d 65 20 69 6e 20 6d 65 6d 6f 72 79 20 ename in memory
e3e90 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
e3ea0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a lite3_malloc()..
e3eb0 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e 63 68 50 ** Make *pConchP
e3ec0 61 74 68 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 ath point to the
e3ed0 20 6e 65 77 20 6e 61 6d 65 2e 20 20 52 65 74 75 new name. Retu
e3ee0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 rn SQLITE_OK on
e3ef0 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 53 51 success.** or SQ
e3f00 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 75 6e LITE_NOMEM if un
e3f10 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 6d able to obtain m
e3f20 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 emory..**.** The
e3f30 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f caller is respo
e3f40 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 nsible for ensur
e3f50 69 6e 67 20 74 68 61 74 20 74 68 65 20 61 6c 6c ing that the all
e3f60 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a ocated memory.**
e3f70 20 73 70 61 63 65 20 69 73 20 65 76 65 6e 74 75 space is eventu
e3f80 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a ally freed..**.*
e3f90 2a 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 69 73 * *pConchPath is
e3fa0 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 69 66 20 set to NULL if
e3fb0 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
e3fc0 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ion error occurs
e3fd0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e3fe0 70 72 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 68 proxyCreateConch
e3ff0 50 61 74 68 6e 61 6d 65 28 63 68 61 72 20 2a 64 Pathname(char *d
e4000 62 50 61 74 68 2c 20 63 68 61 72 20 2a 2a 70 43 bPath, char **pC
e4010 6f 6e 63 68 50 61 74 68 29 7b 0a 20 20 69 6e 74 onchPath){. int
e4020 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
e4030 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
e4040 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
e4050 69 6e 74 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73 int len = (int)s
e4060 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 20 2f trlen(dbPath); /
e4070 2a 20 4c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 * Length of data
e4080 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 2d 20 base filename -
e4090 64 62 50 61 74 68 20 2a 2f 0a 20 20 63 68 61 72 dbPath */. char
e40a0 20 2a 63 6f 6e 63 68 50 61 74 68 3b 20 20 20 20 *conchPath;
e40b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 62 75 66 /* buf
e40c0 66 65 72 20 69 6e 20 77 68 69 63 68 20 74 6f 20 fer in which to
e40d0 63 6f 6e 73 74 72 75 63 74 20 63 6f 6e 63 68 20 construct conch
e40e0 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c name */.. /* Al
e40f0 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
e4100 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e the conch filen
e4110 61 6d 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ame and initiali
e4120 7a 65 20 74 68 65 20 6e 61 6d 65 20 74 6f 0a 20 ze the name to.
e4130 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 ** the name of
e4140 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 the original dat
e4150 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 20 20 abase file. */
e4160 0a 20 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 3d . *pConchPath =
e4170 20 63 6f 6e 63 68 50 61 74 68 20 3d 20 28 63 68 conchPath = (ch
e4180 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c ar *)sqlite3_mal
e4190 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 3b 0a 20 20 loc(len + 8);.
e41a0 69 66 28 20 63 6f 6e 63 68 50 61 74 68 3d 3d 30 if( conchPath==0
e41b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
e41c0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
e41d0 0a 20 20 6d 65 6d 63 70 79 28 63 6f 6e 63 68 50 . memcpy(conchP
e41e0 61 74 68 2c 20 64 62 50 61 74 68 2c 20 6c 65 6e ath, dbPath, len
e41f0 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 77 +1);. . /* now
e4200 20 69 6e 73 65 72 74 20 61 20 22 2e 22 20 62 65 insert a "." be
e4210 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 2f 20 fore the last /
e4220 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 66 character */. f
e4230 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 29 3b 20 69 or( i=(len-1); i
e4240 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a 20 20 20 20 >=0; i-- ){.
e4250 69 66 28 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d if( conchPath[i]
e4260 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 =='/' ){. i
e4270 2b 2b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ++;. break;
e4280 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e . }. }. con
e4290 63 68 50 61 74 68 5b 69 5d 3d 27 2e 27 3b 0a 20 chPath[i]='.';.
e42a0 20 77 68 69 6c 65 20 28 20 69 3c 6c 65 6e 20 29 while ( i<len )
e42b0 7b 0a 20 20 20 20 63 6f 6e 63 68 50 61 74 68 5b {. conchPath[
e42c0 69 2b 31 5d 3d 64 62 50 61 74 68 5b 69 5d 3b 0a i+1]=dbPath[i];.
e42d0 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 i++;. }..
e42e0 2f 2a 20 61 70 70 65 6e 64 20 74 68 65 20 22 2d /* append the "-
e42f0 63 6f 6e 63 68 22 20 73 75 66 66 69 78 20 74 6f conch" suffix to
e4300 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 6d the file */. m
e4310 65 6d 63 70 79 28 26 63 6f 6e 63 68 50 61 74 68 emcpy(&conchPath
e4320 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63 68 22 2c [i+1], "-conch",
e4330 20 37 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 7);. assert( (
e4340 69 6e 74 29 73 74 72 6c 65 6e 28 63 6f 6e 63 68 int)strlen(conch
e4350 50 61 74 68 29 20 3d 3d 20 6c 65 6e 2b 37 20 29 Path) == len+7 )
e4360 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 ;.. return SQLI
e4370 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 20 54 61 TE_OK;.}.../* Ta
e4380 6b 65 73 20 61 20 66 75 6c 6c 79 20 63 6f 6e 66 kes a fully conf
e4390 69 67 75 72 65 64 20 70 72 6f 78 79 20 6c 6f 63 igured proxy loc
e43a0 6b 69 6e 67 2d 73 74 79 6c 65 20 75 6e 69 78 20 king-style unix
e43b0 66 69 6c 65 20 61 6e 64 20 73 77 69 74 63 68 65 file and switche
e43c0 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 6c s.** the local l
e43d0 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 20 0a 2a ock file path .*
e43e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 77 69 /.static int swi
e43f0 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 tchLockProxyPath
e4400 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
e4410 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 , const char *pa
e4420 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 th) {. proxyLoc
e4430 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 kingContext *pCt
e4440 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e x = (proxyLockin
e4450 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d gContext*)pFile-
e4460 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
e4470 0a 20 20 63 68 61 72 20 2a 6f 6c 64 50 61 74 68 . char *oldPath
e4480 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f = pCtx->lockPro
e4490 78 79 50 61 74 68 3b 0a 20 20 69 6e 74 20 72 63 xyPath;. int rc
e44a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
e44b0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
e44c0 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b type!=NO_LOCK ){
e44d0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
e44e0 54 45 5f 42 55 53 59 3b 0a 20 20 7d 20 20 0a 0a TE_BUSY;. } ..
e44f0 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 /* nothing to
e4500 64 6f 20 69 66 20 74 68 65 20 70 61 74 68 20 69 do if the path i
e4510 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 6f 3a 20 6f s NULL, :auto: o
e4520 72 20 6d 61 74 63 68 65 73 20 74 68 65 20 65 78 r matches the ex
e4530 69 73 74 69 6e 67 20 70 61 74 68 20 2a 2f 0a 20 isting path */.
e4540 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 70 61 if( !path || pa
e4550 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 21 th[0]=='\0' || !
e4560 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 3a 61 strcmp(path, ":a
e4570 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 20 20 28 6f uto:") ||. (o
e4580 6c 64 50 61 74 68 20 26 26 20 21 73 74 72 6e 63 ldPath && !strnc
e4590 6d 70 28 6f 6c 64 50 61 74 68 2c 20 70 61 74 68 mp(oldPath, path
e45a0 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 29 20 29 , MAXPATHLEN)) )
e45b0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
e45c0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
e45d0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c . unixFile *l
e45e0 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 78 2d ockProxy = pCtx-
e45f0 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 >lockProxy;.
e4600 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3d pCtx->lockProxy=
e4610 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 74 78 2d 3e NULL;. pCtx->
e4620 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20 conchHeld = 0;.
e4630 20 20 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 if( lockProxy
e4640 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 !=NULL ){.
e4650 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d rc=lockProxy->pM
e4660 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 ethod->xClose((s
e4670 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 6c 6f qlite3_file *)lo
e4680 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 ckProxy);.
e4690 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
e46a0 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 rc;. sqlite
e46b0 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79 3_free(lockProxy
e46c0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
e46d0 69 74 65 33 5f 66 72 65 65 28 6f 6c 64 50 61 74 ite3_free(oldPat
e46e0 68 29 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f h);. pCtx->lo
e46f0 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71 ckProxyPath = sq
e4700 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c lite3DbStrDup(0,
e4710 20 70 61 74 68 29 3b 0a 20 20 7d 0a 20 20 0a 20 path);. }. .
e4720 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
e4730 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 73 20 61 20 *.** pFile is a
e4740 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 62 65 file that has be
e4750 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70 en opened by a p
e4760 72 69 6f 72 20 78 4f 70 65 6e 20 63 61 6c 6c 2e rior xOpen call.
e4770 20 20 64 62 50 61 74 68 0a 2a 2a 20 69 73 20 61 dbPath.** is a
e4780 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 61 string buffer a
e4790 74 20 6c 65 61 73 74 20 4d 41 58 50 41 54 48 4c t least MAXPATHL
e47a0 45 4e 2b 31 20 63 68 61 72 61 63 74 65 72 73 20 EN+1 characters
e47b0 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 in size..**.** T
e47c0 68 69 73 20 72 6f 75 74 69 6e 65 20 66 69 6e 64 his routine find
e47d0 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 73 the filename as
e47e0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 46 sociated with pF
e47f0 69 6c 65 20 61 6e 64 20 77 72 69 74 65 73 20 69 ile and writes i
e4800 74 0a 2a 2a 20 69 6e 74 20 64 62 50 61 74 68 2e t.** int dbPath.
e4810 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
e4820 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f 72 roxyGetDbPathFor
e4830 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c UnixFile(unixFil
e4840 65 20 2a 70 46 69 6c 65 2c 20 63 68 61 72 20 2a e *pFile, char *
e4850 64 62 50 61 74 68 29 7b 0a 23 69 66 20 64 65 66 dbPath){.#if def
e4860 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a ined(__APPLE__).
e4870 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 if( pFile->pMe
e4880 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 thod == &afpIoMe
e4890 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
e48a0 61 66 70 20 73 74 79 6c 65 20 6b 65 65 70 73 20 afp style keeps
e48b0 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 a reference to t
e48c0 68 65 20 64 62 20 70 61 74 68 20 69 6e 20 74 68 he db path in th
e48d0 65 20 66 69 6c 65 50 61 74 68 20 66 69 65 6c 64 e filePath field
e48e0 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 . ** of the
e48f0 73 74 72 75 63 74 20 2a 2f 0a 20 20 20 20 61 73 struct */. as
e4900 73 65 72 74 28 20 28 69 6e 74 29 73 74 72 6c 65 sert( (int)strle
e4910 6e 28 28 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e n((char*)pFile->
e4920 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c lockingContext)<
e4930 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20 =MAXPATHLEN );.
e4940 20 20 20 73 74 72 63 70 79 28 64 62 50 61 74 68 strcpy(dbPath
e4950 2c 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f , ((afpLockingCo
e4960 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c ntext *)pFile->l
e4970 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e ockingContext)->
e4980 64 62 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 dbPath);. }else
e4990 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 46 .#endif. if( pF
e49a0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 ile->pMethod ==
e49b0 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &dotlockIoMethod
e49c0 73 20 29 7b 0a 20 20 20 20 2f 2a 20 64 6f 74 20 s ){. /* dot
e49d0 6c 6f 63 6b 20 73 74 79 6c 65 20 75 73 65 73 20 lock style uses
e49e0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 the locking cont
e49f0 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ext to store the
e4a00 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 20 20 2a 2a dot lock. **
e4a10 20 66 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 file path */.
e4a20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 73 74 72 6c int len = strl
e4a30 65 6e 28 28 63 68 61 72 20 2a 29 70 46 69 6c 65 en((char *)pFile
e4a40 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
e4a50 29 20 2d 20 73 74 72 6c 65 6e 28 44 4f 54 4c 4f ) - strlen(DOTLO
e4a60 43 4b 5f 53 55 46 46 49 58 29 3b 0a 20 20 20 20 CK_SUFFIX);.
e4a70 6d 65 6d 63 70 79 28 64 62 50 61 74 68 2c 20 28 memcpy(dbPath, (
e4a80 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f char *)pFile->lo
e4a90 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c 20 6c 65 ckingContext, le
e4aa0 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b n + 1);. }else{
e4ab0 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6f 74 68 65 . /* all othe
e4ac0 72 20 73 74 79 6c 65 73 20 75 73 65 20 74 68 65 r styles use the
e4ad0 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
e4ae0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 64 62 to store the db
e4af0 20 66 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 file path */.
e4b00 20 20 61 73 73 65 72 74 28 20 73 74 72 6c 65 6e assert( strlen
e4b10 28 28 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c ((char*)pFile->l
e4b20 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d ockingContext)<=
e4b30 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20 20 MAXPATHLEN );.
e4b40 20 20 73 74 72 63 70 79 28 64 62 50 61 74 68 2c strcpy(dbPath,
e4b50 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e (char *)pFile->
e4b60 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b lockingContext);
e4b70 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
e4b80 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
e4b90 2a 20 54 61 6b 65 73 20 61 6e 20 61 6c 72 65 61 * Takes an alrea
e4ba0 64 79 20 66 69 6c 6c 65 64 20 69 6e 20 75 6e 69 dy filled in uni
e4bb0 78 20 66 69 6c 65 20 61 6e 64 20 61 6c 74 65 72 x file and alter
e4bc0 73 20 69 74 20 73 6f 20 61 6c 6c 20 66 69 6c 65 s it so all file
e4bd0 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 77 69 6c locking .** wil
e4be0 6c 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 6f l be performed o
e4bf0 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 72 6f 78 n the local prox
e4c00 79 20 6c 6f 63 6b 20 66 69 6c 65 2e 20 20 54 68 y lock file. Th
e4c10 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c e following fiel
e4c20 64 73 0a 2a 2a 20 61 72 65 20 70 72 65 73 65 72 ds.** are preser
e4c30 76 65 64 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 ved in the locki
e4c40 6e 67 20 63 6f 6e 74 65 78 74 20 73 6f 20 74 68 ng context so th
e4c50 61 74 20 74 68 65 79 20 63 61 6e 20 62 65 20 72 at they can be r
e4c60 65 73 74 6f 72 65 64 20 61 6e 64 20 0a 2a 2a 20 estored and .**
e4c70 74 68 65 20 75 6e 69 78 20 73 74 72 75 63 74 75 the unix structu
e4c80 72 65 20 70 72 6f 70 65 72 6c 79 20 63 6c 65 61 re properly clea
e4c90 6e 65 64 20 75 70 20 61 74 20 63 6c 6f 73 65 20 ned up at close
e4ca0 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e 6c 6f 63 6b time:.** ->lock
e4cb0 69 6e 67 43 6f 6e 74 65 78 74 0a 2a 2a 20 20 2d ingContext.** -
e4cc0 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a 73 74 61 74 >pMethod.*/.stat
e4cd0 69 63 20 69 6e 74 20 70 72 6f 78 79 54 72 61 6e ic int proxyTran
e4ce0 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e sformUnixFile(un
e4cf0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 ixFile *pFile, c
e4d00 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 29 onst char *path)
e4d10 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e {. proxyLockin
e4d20 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a gContext *pCtx;.
e4d30 20 20 63 68 61 72 20 64 62 50 61 74 68 5b 4d 41 char dbPath[MA
e4d40 58 50 41 54 48 4c 45 4e 2b 31 5d 3b 20 20 20 20 XPATHLEN+1];
e4d50 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
e4d60 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
e4d70 2a 2f 0a 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 */. char *lockP
e4d80 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20 ath=NULL;. int
e4d90 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
e4da0 20 20 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
e4db0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
e4dc0 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
e4dd0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
e4de0 0a 20 20 70 72 6f 78 79 47 65 74 44 62 50 61 74 . proxyGetDbPat
e4df0 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 70 46 69 hForUnixFile(pFi
e4e00 6c 65 2c 20 64 62 50 61 74 68 29 3b 0a 20 20 69 le, dbPath);. i
e4e10 66 28 20 21 70 61 74 68 20 7c 7c 20 70 61 74 68 f( !path || path
e4e20 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 21 73 74 [0]=='\0' || !st
e4e30 72 63 6d 70 28 70 61 74 68 2c 20 22 3a 61 75 74 rcmp(path, ":aut
e4e40 6f 3a 22 29 20 29 7b 0a 20 20 20 20 6c 6f 63 6b o:") ){. lock
e4e50 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 7d 65 6c Path=NULL;. }el
e4e60 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68 se{. lockPath
e4e70 3d 28 63 68 61 72 20 2a 29 70 61 74 68 3b 0a 20 =(char *)path;.
e4e80 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 34 }. . OSTRACE4
e4e90 28 22 54 52 41 4e 53 50 52 4f 58 59 20 20 25 64 ("TRANSPROXY %d
e4ea0 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e for %s pid=%d\n
e4eb0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 ", pFile->h,.
e4ec0 20 20 20 20 20 20 20 20 28 6c 6f 63 6b 50 61 74 (lockPat
e4ed0 68 20 3f 20 6c 6f 63 6b 50 61 74 68 20 3a 20 22 h ? lockPath : "
e4ee0 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 :auto:"), getpid
e4ef0 28 29 29 3b 0a 0a 20 20 70 43 74 78 20 3d 20 73 ());.. pCtx = s
e4f00 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 qlite3_malloc( s
e4f10 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a izeof(*pCtx) );.
e4f20 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b if( pCtx==0 ){
e4f30 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
e4f40 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
e4f50 6d 65 6d 73 65 74 28 70 43 74 78 2c 20 30 2c 20 memset(pCtx, 0,
e4f60 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 29 3b 0a sizeof(*pCtx));.
e4f70 0a 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 . rc = proxyCre
e4f80 61 74 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 ateConchPathname
e4f90 28 64 62 50 61 74 68 2c 20 26 70 43 74 78 2d 3e (dbPath, &pCtx->
e4fa0 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 0a conchFilePath);.
e4fb0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
e4fc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 _OK ){. rc =
e4fd0 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78 46 proxyCreateUnixF
e4fe0 69 6c 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 ile(pCtx->conchF
e4ff0 69 6c 65 50 61 74 68 2c 20 26 70 43 74 78 2d 3e ilePath, &pCtx->
e5000 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 7d 20 conchFile);. }
e5010 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 . if( rc==SQLI
e5020 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 50 61 74 TE_OK && lockPat
e5030 68 20 29 7b 0a 20 20 20 20 70 43 74 78 2d 3e 6c h ){. pCtx->l
e5040 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 ockProxyPath = s
e5050 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 qlite3DbStrDup(0
e5060 2c 20 6c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 7d , lockPath);. }
e5070 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
e5080 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 TE_OK ){. /*
e5090 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c all memory is al
e50a0 6c 6f 63 61 74 65 64 2c 20 70 72 6f 78 79 73 20 located, proxys
e50b0 61 72 65 20 63 72 65 61 74 65 64 20 61 6e 64 20 are created and
e50c0 61 73 73 69 67 6e 65 64 2c 20 0a 20 20 20 20 2a assigned, . *
e50d0 2a 20 73 77 69 74 63 68 20 74 68 65 20 6c 6f 63 * switch the loc
e50e0 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 king context and
e50f0 20 70 4d 65 74 68 6f 64 20 74 68 65 6e 20 72 65 pMethod then re
e5100 74 75 72 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 turn.. */.
e5110 20 70 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20 pCtx->dbPath =
e5120 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
e5130 30 2c 20 64 62 50 61 74 68 29 3b 0a 20 20 20 20 0, dbPath);.
e5140 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 pCtx->oldLocking
e5150 43 6f 6e 74 65 78 74 20 3d 20 70 46 69 6c 65 2d Context = pFile-
e5160 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
e5170 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
e5180 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 ingContext = pCt
e5190 78 3b 0a 20 20 20 20 70 43 74 78 2d 3e 70 4f 6c x;. pCtx->pOl
e51a0 64 4d 65 74 68 6f 64 20 3d 20 70 46 69 6c 65 2d dMethod = pFile-
e51b0 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 20 20 70 46 >pMethod;. pF
e51c0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 ile->pMethod = &
e51d0 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 3b 0a proxyIoMethods;.
e51e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 }else{. if(
e51f0 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 pCtx->conchFile
e5200 20 29 7b 20 0a 20 20 20 20 20 20 72 63 20 3d 20 ){ . rc =
e5210 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 2d pCtx->conchFile-
e5220 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 >pMethod->xClose
e5230 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ((sqlite3_file *
e5240 29 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 )pCtx->conchFile
e5250 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
e5260 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
e5270 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
e5280 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 pCtx->conchFile)
e5290 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
e52a0 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 te3_free(pCtx->c
e52b0 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 20 0a onchFilePath); .
e52c0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
e52d0 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20 20 4f 53 (pCtx);. }. OS
e52e0 54 52 41 43 45 33 28 22 54 52 41 4e 53 50 52 4f TRACE3("TRANSPRO
e52f0 58 59 20 20 25 64 20 25 73 5c 6e 22 2c 20 70 46 XY %d %s\n", pF
e5300 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
e5310 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f (rc==SQLITE_O
e5320 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c K ? "ok" : "fail
e5330 65 64 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 ed"));. return
e5340 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}.../*.** Th
e5350 69 73 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c is routine handl
e5360 65 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f es sqlite3_file_
e5370 63 6f 6e 74 72 6f 6c 28 29 20 63 61 6c 6c 73 20 control() calls
e5380 74 68 61 74 20 61 72 65 20 73 70 65 63 69 66 69 that are specifi
e5390 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 79 20 6c 6f c.** to proxy lo
e53a0 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 cking..*/.static
e53b0 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 6f int proxyFileCo
e53c0 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 ntrol(sqlite3_fi
e53d0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 le *id, int op,
e53e0 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 void *pArg){. s
e53f0 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 witch( op ){.
e5400 20 63 61 73 65 20 53 51 4c 49 54 45 5f 47 45 54 case SQLITE_GET
e5410 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 _LOCKPROXYFILE:
e5420 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c 65 {. unixFile
e5430 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
e5440 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 ile*)id;. i
e5450 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f f( pFile->pMetho
e5460 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 d == &proxyIoMet
e5470 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 hods ){.
e5480 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
e5490 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
e54a0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
e54b0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
e54c0 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 Context;.
e54d0 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 proxyTakeConch(
e54e0 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 pFile);.
e54f0 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 if( pCtx->lockPr
e5500 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20 oxyPath ){.
e5510 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 *(const cha
e5520 72 20 2a 2a 29 70 41 72 67 20 3d 20 70 43 74 78 r **)pArg = pCtx
e5530 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b ->lockProxyPath;
e5540 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
e5550 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 *(cons
e5560 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d t char **)pArg =
e5570 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74 20 68 65 ":auto: (not he
e5580 6c 64 29 22 3b 0a 20 20 20 20 20 20 20 20 7d 0a ld)";. }.
e5590 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 } else {.
e55a0 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 *(const c
e55b0 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 4e 55 har **)pArg = NU
e55c0 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 LL;. }.
e55d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e55e0 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 OK;. }. ca
e55f0 73 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f se SQLITE_SET_LO
e5600 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 CKPROXYFILE: {.
e5610 20 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 unixFile *p
e5620 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
e5630 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 *)id;. int
e5640 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
e5650 20 20 20 20 20 20 69 6e 74 20 69 73 50 72 6f 78 int isProx
e5660 79 53 74 79 6c 65 20 3d 20 28 70 46 69 6c 65 2d yStyle = (pFile-
e5670 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f >pMethod == &pro
e5680 78 79 49 6f 4d 65 74 68 6f 64 73 29 3b 0a 20 20 xyIoMethods);.
e5690 20 20 20 20 69 66 28 20 70 41 72 67 3d 3d 4e 55 if( pArg==NU
e56a0 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 20 63 68 61 LL || (const cha
e56b0 72 20 2a 29 70 41 72 67 3d 3d 30 20 29 7b 0a 20 r *)pArg==0 ){.
e56c0 20 20 20 20 20 20 20 69 66 28 20 69 73 50 72 6f if( isPro
e56d0 78 79 53 74 79 6c 65 20 29 7b 0a 20 20 20 20 20 xyStyle ){.
e56e0 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 /* turn off
e56f0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d proxy locking -
e5700 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 2a not supported *
e5710 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d /. rc =
e5720 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 2f 2a SQLITE_ERROR /*
e5730 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 3f SQLITE_PROTOCOL?
e5740 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3f 2a SQLITE_MISUSE?*
e5750 2f 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 /;. }else
e5760 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 {. /* t
e5770 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f urn off proxy lo
e5780 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61 64 79 20 cking - already
e5790 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f 0a 20 20 off - NOOP */.
e57a0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
e57b0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 ITE_OK;.
e57c0 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
e57d0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 const cha
e57e0 72 20 2a 70 72 6f 78 79 50 61 74 68 20 3d 20 28 r *proxyPath = (
e57f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 41 72 const char *)pAr
e5800 67 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 g;. if( i
e5810 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 sProxyStyle ){.
e5820 20 20 20 20 20 20 20 20 20 70 72 6f 78 79 4c 6f proxyLo
e5830 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
e5840 74 78 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 tx = .
e5850 20 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 (proxyLockingC
e5860 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c ontext*)pFile->l
e5870 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 ockingContext;.
e5880 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73 74 if( !st
e5890 72 63 6d 70 28 70 41 72 67 2c 20 22 3a 61 75 74 rcmp(pArg, ":aut
e58a0 6f 3a 22 29 20 0a 20 20 20 20 20 20 20 20 20 20 o:") .
e58b0 20 7c 7c 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 || (pCtx->lockP
e58c0 72 6f 78 79 50 61 74 68 20 26 26 0a 20 20 20 20 roxyPath &&.
e58d0 20 20 20 20 20 20 20 20 20 20 20 21 73 74 72 6e !strn
e58e0 63 6d 70 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 cmp(pCtx->lockPr
e58f0 6f 78 79 50 61 74 68 2c 20 70 72 6f 78 79 50 61 oxyPath, proxyPa
e5900 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 29 th, MAXPATHLEN))
e5910 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 . ){.
e5920 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
e5930 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
e5940 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
e5950 20 20 20 20 20 20 20 72 63 20 3d 20 73 77 69 74 rc = swit
e5960 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 28 chLockProxyPath(
e5970 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74 68 pFile, proxyPath
e5980 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
e5990 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
e59a0 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 /* turn
e59b0 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65 20 6c 6f on proxy file lo
e59c0 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 cking */.
e59d0 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72 61 rc = proxyTra
e59e0 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 70 nsformUnixFile(p
e59f0 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74 68 29 File, proxyPath)
e5a00 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
e5a10 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e }. return
e5a20 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 rc;. }. d
e5a30 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
e5a40 61 73 73 65 72 74 28 20 30 20 29 3b 20 20 2f 2a assert( 0 ); /*
e5a50 20 54 68 65 20 63 61 6c 6c 20 61 73 73 75 72 65 The call assure
e5a60 73 20 74 68 61 74 20 6f 6e 6c 79 20 76 61 6c 69 s that only vali
e5a70 64 20 6f 70 63 6f 64 65 73 20 61 72 65 20 73 65 d opcodes are se
e5a80 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a nt */. }. }.
e5a90 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f /*NOTREACHED*/
e5aa0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
e5ab0 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _ERROR;.}../*.**
e5ac0 20 57 69 74 68 69 6e 20 74 68 69 73 20 64 69 76 Within this div
e5ad0 69 73 69 6f 6e 20 28 74 68 65 20 70 72 6f 78 79 ision (the proxy
e5ae0 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c ing locking impl
e5af0 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74 68 65 20 ementation) the
e5b00 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 61 62 procedures.** ab
e5b10 6f 76 65 20 74 68 69 73 20 70 6f 69 6e 74 20 61 ove this point a
e5b20 72 65 20 61 6c 6c 20 75 74 69 6c 69 74 69 65 73 re all utilities
e5b30 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72 65 6c 61 . The lock-rela
e5b40 74 65 64 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 ted methods of t
e5b50 68 65 0a 2a 2a 20 70 72 6f 78 79 2d 6c 6f 63 6b he.** proxy-lock
e5b60 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d ing sqlite3_io_m
e5b70 65 74 68 6f 64 20 6f 62 6a 65 63 74 20 66 6f 6c ethod object fol
e5b80 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 low..*/.../*.**
e5b90 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
e5ba0 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 cks if there is
e5bb0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
e5bc0 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 held on the spec
e5bd0 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 ified.** file by
e5be0 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 this or any oth
e5bf0 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 er process. If s
e5c00 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 uch a lock is he
e5c10 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 ld, set *pResOut
e5c20 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 .** to a non-zer
e5c30 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 o value otherwis
e5c40 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 e *pResOut is se
e5c50 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 t to zero. The
e5c60 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
e5c70 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 is set to SQLITE
e5c80 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f _OK unless an I/
e5c90 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 O error occurs d
e5ca0 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b uring lock check
e5cb0 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
e5cc0 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 nt proxyCheckRes
e5cd0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
e5ce0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
e5cf0 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 75 6e *pResOut) {. un
e5d00 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
e5d10 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
e5d20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54 int rc = proxyT
e5d30 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b akeConch(pFile);
e5d40 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
e5d50 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 E_OK ){. prox
e5d60 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
e5d70 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
e5d80 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 ckingContext *)p
e5d90 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
e5da0 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 text;. unixFi
e5db0 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 le *proxy = pCtx
e5dc0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 ->lockProxy;.
e5dd0 20 72 65 74 75 72 6e 20 70 72 6f 78 79 2d 3e 70 return proxy->p
e5de0 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63 6b 52 65 Method->xCheckRe
e5df0 73 65 72 76 65 64 4c 6f 63 6b 28 28 73 71 6c 69 servedLock((sqli
e5e00 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c te3_file*)proxy,
e5e10 20 70 52 65 73 4f 75 74 29 3b 0a 20 20 7d 0a 20 pResOut);. }.
e5e20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
e5e30 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 *.** Lock the fi
e5e40 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b le with the lock
e5e50 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
e5e60 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 rameter locktype
e5e70 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 - one.** of the
e5e80 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
e5e90 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 * (1) SHARED
e5ea0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 _LOCK.** (2)
e5eb0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a RESERVED_LOCK.*
e5ec0 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e * (3) PENDIN
e5ed0 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 G_LOCK.** (4
e5ee0 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ) EXCLUSIVE_LOCK
e5ef0 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
e5f00 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 when requesting
e5f10 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c one lock state,
e5f20 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b additional lock
e5f30 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 states.** are i
e5f40 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 nserted in betwe
e5f50 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 en. The locking
e5f60 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f might fail on o
e5f70 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a ne of the later.
e5f80 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c ** transitions l
e5f90 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 eaving the lock
e5fa0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 state different
e5fb0 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 from what it sta
e5fc0 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c rted but.** stil
e5fd0 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 l short of its g
e5fe0 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 oal. The follow
e5ff0 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 ing chart shows
e6000 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 the allowed.** t
e6010 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 ransitions and t
e6020 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 he inserted inte
e6030 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a rmediate states:
e6040 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b .**.** UNLOCK
e6050 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 ED -> SHARED.**
e6060 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 SHARED -> RES
e6070 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 ERVED.** SHAR
e6080 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
e6090 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
e60a0 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 RESERVED -> (
e60b0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
e60c0 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 USIVE.** PEND
e60d0 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 ING -> EXCLUSIVE
e60e0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
e60f0 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e ine will only in
e6100 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 crease a lock.
e6110 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f Use the sqlite3O
e6120 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 sUnlock().** rou
e6130 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 tine to lower a
e6140 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a locking level..*
e6150 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
e6160 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 xyLock(sqlite3_f
e6170 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
e6180 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 ktype) {. unixF
e6190 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
e61a0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e ixFile*)id;. in
e61b0 74 20 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 t rc = proxyTake
e61c0 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 Conch(pFile);.
e61d0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
e61e0 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f K ){. proxyLo
e61f0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
e6200 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
e6210 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
e6220 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
e6230 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 t;. unixFile
e6240 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c *proxy = pCtx->l
e6250 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 63 ockProxy;. rc
e6260 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f = proxy->pMetho
e6270 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 d->xLock((sqlite
e6280 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c 3_file*)proxy, l
e6290 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 70 46 ocktype);. pF
e62a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
e62b0 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b proxy->locktype;
e62c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
e62d0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 ;.}.../*.** Lowe
e62e0 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 r the locking le
e62f0 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 vel on file desc
e6300 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 riptor pFile to
e6310 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 locktype. lockt
e6320 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 ype.** must be e
e6330 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 ither NO_LOCK or
e6340 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a SHARED_LOCK..**
e6350 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 .** If the locki
e6360 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 ng level of the
e6370 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e6380 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 is already at or
e6390 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 below.** the re
e63a0 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 quested locking
e63b0 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 level, this rout
e63c0 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
e63d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
e63e0 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 oxyUnlock(sqlite
e63f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
e6400 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e locktype) {. un
e6410 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
e6420 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
e6430 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54 int rc = proxyT
e6440 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b akeConch(pFile);
e6450 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
e6460 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 E_OK ){. prox
e6470 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
e6480 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
e6490 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 ckingContext *)p
e64a0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
e64b0 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 text;. unixFi
e64c0 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 le *proxy = pCtx
e64d0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 ->lockProxy;.
e64e0 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65 rc = proxy->pMe
e64f0 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 thod->xUnlock((s
e6500 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f qlite3_file*)pro
e6510 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 xy, locktype);.
e6520 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
e6530 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b pe = proxy->lock
e6540 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 type;. }. retu
e6550 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
e6560 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 74 68 61 Close a file tha
e6570 74 20 75 73 65 73 20 70 72 6f 78 79 20 6c 6f 63 t uses proxy loc
e6580 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ks..*/.static in
e6590 74 20 70 72 6f 78 79 43 6c 6f 73 65 28 73 71 6c t proxyClose(sql
e65a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
e65b0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 . if( id ){.
e65c0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
e65d0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
e65e0 3b 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 ;. proxyLocki
e65f0 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 ngContext *pCtx
e6600 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 = (proxyLockingC
e6610 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e ontext *)pFile->
e6620 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
e6630 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f unixFile *lo
e6640 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e ckProxy = pCtx->
e6650 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 75 lockProxy;. u
e6660 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 nixFile *conchFi
e6670 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 le = pCtx->conch
e6680 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 File;. int rc
e6690 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
e66a0 20 20 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 . if( lockP
e66b0 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63 roxy ){. rc
e66c0 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d = lockProxy->pM
e66d0 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 ethod->xUnlock((
e66e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f sqlite3_file*)lo
e66f0 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f 4c 4f 43 4b ckProxy, NO_LOCK
e6700 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
e6710 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
e6720 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 rc = lockProx
e6730 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f y->pMethod->xClo
e6740 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 se((sqlite3_file
e6750 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 *)lockProxy);.
e6760 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
e6770 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 urn rc;. sq
e6780 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 lite3_free(lockP
e6790 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 70 43 74 roxy);. pCt
e67a0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 30 x->lockProxy = 0
e67b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
e67c0 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0a 20 20 20 conchFile ){.
e67d0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e if( pCtx->con
e67e0 63 68 48 65 6c 64 20 29 7b 0a 20 20 20 20 20 20 chHeld ){.
e67f0 20 20 72 63 20 3d 20 70 72 6f 78 79 52 65 6c 65 rc = proxyRele
e6800 61 73 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b aseConch(pFile);
e6810 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
e6820 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
e6830 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
e6840 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 conchFile->pMeth
e6850 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 od->xClose((sqli
e6860 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 te3_file*)conchF
e6870 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ile);. if(
e6880 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
e6890 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
e68a0 65 65 28 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 ee(conchFile);.
e68b0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
e68c0 5f 66 72 65 65 28 70 43 74 78 2d 3e 6c 6f 63 6b _free(pCtx->lock
e68d0 50 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20 ProxyPath);.
e68e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
e68f0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 x->conchFilePath
e6900 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
e6910 72 65 65 28 70 43 74 78 2d 3e 64 62 50 61 74 68 ree(pCtx->dbPath
e6920 29 3b 0a 20 20 20 20 2f 2a 20 72 65 73 74 6f 72 );. /* restor
e6930 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6c e the original l
e6940 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 61 ocking context a
e6950 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 6e 20 nd pMethod then
e6960 63 6c 6f 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 close it */.
e6970 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
e6980 6e 74 65 78 74 20 3d 20 70 43 74 78 2d 3e 6f 6c ntext = pCtx->ol
e6990 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b dLockingContext;
e69a0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 . pFile->pMet
e69b0 68 6f 64 20 3d 20 70 43 74 78 2d 3e 70 4f 6c 64 hod = pCtx->pOld
e69c0 4d 65 74 68 6f 64 3b 0a 20 20 20 20 73 71 6c 69 Method;. sqli
e69d0 74 65 33 5f 66 72 65 65 28 70 43 74 78 29 3b 0a te3_free(pCtx);.
e69e0 20 20 20 20 72 65 74 75 72 6e 20 70 46 69 6c 65 return pFile
e69f0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 ->pMethod->xClos
e6a00 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 e(id);. }. ret
e6a10 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
e6a20 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 ....#endif /* de
e6a30 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e6a40 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
e6a50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
e6a60 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f */./*.** The pro
e6a70 78 79 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 xy locking style
e6a80 20 69 73 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 is intended for
e6a90 20 75 73 65 20 77 69 74 68 20 41 46 50 20 66 69 use with AFP fi
e6aa0 6c 65 73 79 73 74 65 6d 73 2e 0a 2a 2a 20 41 6e lesystems..** An
e6ab0 64 20 73 69 6e 63 65 20 41 46 50 20 69 73 20 6f d since AFP is o
e6ac0 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e nly supported on
e6ad0 20 4d 61 63 4f 53 58 2c 20 74 68 65 20 70 72 6f MacOSX, the pro
e6ae0 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 6c xy locking is al
e6af0 73 6f 0a 2a 2a 20 72 65 73 74 72 69 63 74 65 64 so.** restricted
e6b00 20 74 6f 20 4d 61 63 4f 53 58 2e 0a 2a 2a 20 0a to MacOSX..** .
e6b10 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
e6b20 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 ****** End of th
e6b30 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 69 6d 70 e proxy lock imp
e6b40 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a lementation ****
e6b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6b60 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
e6b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6bb0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 */../*.** Initia
e6bc0 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 lize the operati
e6bd0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
e6be0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ace..**.** This
e6bf0 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65 72 routine register
e6c00 73 20 61 6c 6c 20 56 46 53 20 69 6d 70 6c 65 6d s all VFS implem
e6c10 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 75 6e entations for un
e6c20 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e ix-like operatin
e6c30 67 0a 2a 2a 20 73 79 73 74 65 6d 73 2e 20 20 54 g.** systems. T
e6c40 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 64 his routine, and
e6c50 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f the sqlite3_os_
e6c60 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 20 74 68 end() routine th
e6c70 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a 2a 2a 20 73 at follows,.** s
e6c80 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c hould be the onl
e6c90 79 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 y routines in th
e6ca0 69 73 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 is file that are
e6cb0 20 76 69 73 69 62 6c 65 20 66 72 6f 6d 20 6f 74 visible from ot
e6cc0 68 65 72 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a her.** files..**
e6cd0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
e6ce0 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 is called once
e6cf0 64 75 72 69 6e 67 20 53 51 4c 69 74 65 20 69 6e during SQLite in
e6d00 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 itialization and
e6d10 20 62 79 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 by a.** single
e6d20 74 68 72 65 61 64 2e 20 20 54 68 65 20 6d 65 6d thread. The mem
e6d30 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 ory allocation a
e6d40 6e 64 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 nd mutex subsyst
e6d50 65 6d 73 20 68 61 76 65 20 6e 6f 74 0a 2a 2a 20 ems have not.**
e6d60 6e 65 63 65 73 73 61 72 69 6c 79 20 62 65 65 6e necessarily been
e6d70 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 initialized whe
e6d80 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
e6d90 73 20 63 61 6c 6c 65 64 2c 20 61 6e 64 20 73 6f s called, and so
e6da0 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 they.** should
e6db0 6e 6f 74 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a not be used..*/.
e6dc0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
e6dd0 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 qlite3_os_init(v
e6de0 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 0a 20 20 2a oid){ . /* . *
e6df0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
e6e00 6d 61 63 72 6f 20 64 65 66 69 6e 65 73 20 61 6e macro defines an
e6e10 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 66 6f 72 initializer for
e6e20 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 66 73 20 an sqlite3_vfs
e6e30 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 54 68 65 object.. ** The
e6e40 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 56 46 53 name of the VFS
e6e50 20 69 73 20 4e 41 4d 45 2e 20 20 54 68 65 20 70 is NAME. The p
e6e60 41 70 70 44 61 74 61 20 69 73 20 61 20 70 6f 69 AppData is a poi
e6e70 6e 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 nter to a pointe
e6e80 72 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 66 r. ** to the "f
e6e90 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 2e inder" function.
e6ea0 20 20 28 70 41 70 70 44 61 74 61 20 69 73 20 61 (pAppData is a
e6eb0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f pointer to a po
e6ec0 69 6e 74 65 72 20 62 65 63 61 75 73 65 0a 20 20 inter because.
e6ed0 2a 2a 20 73 69 6c 6c 79 20 43 39 30 20 72 75 6c ** silly C90 rul
e6ee0 65 73 20 70 72 6f 68 69 62 69 74 20 61 20 76 6f es prohibit a vo
e6ef0 69 64 2a 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 id* from being c
e6f00 61 73 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ast to a functio
e6f10 6e 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 61 n pointer. ** a
e6f20 6e 64 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f nd so we have to
e6f30 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 65 20 go through the
e6f40 69 6e 74 65 72 6d 65 64 69 61 74 65 20 70 6f 69 intermediate poi
e6f50 6e 74 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72 nter to avoid pr
e6f60 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 77 68 65 6e oblems. ** when
e6f70 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 compiling with
e6f80 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73 -pedantic-errors
e6f90 20 6f 6e 20 47 43 43 2e 29 0a 20 20 2a 2a 0a 20 on GCC.). **.
e6fa0 20 2a 2a 20 54 68 65 20 46 49 4e 44 45 52 20 70 ** The FINDER p
e6fb0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 arameter to this
e6fc0 20 6d 61 63 72 6f 20 69 73 20 74 68 65 20 6e 61 macro is the na
e6fd0 6d 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 me of the pointe
e6fe0 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 66 69 r to the. ** fi
e6ff0 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 2e 20 20 nder-function.
e7000 54 68 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 The finder-funct
e7010 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f ion returns a po
e7020 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a inter to the. *
e7030 2a 20 73 71 6c 69 74 65 5f 69 6f 5f 6d 65 74 68 * sqlite_io_meth
e7040 6f 64 73 20 6f 62 6a 65 63 74 20 74 68 61 74 20 ods object that
e7050 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 implements the d
e7060 65 73 69 72 65 64 20 6c 6f 63 6b 69 6e 67 0a 20 esired locking.
e7070 20 2a 2a 20 62 65 68 61 76 69 6f 72 73 2e 20 20 ** behaviors.
e7080 53 65 65 20 74 68 65 20 64 69 76 69 73 69 6f 6e See the division
e7090 20 61 62 6f 76 65 20 74 68 61 74 20 63 6f 6e 74 above that cont
e70a0 61 69 6e 73 20 74 68 65 20 49 4f 4d 45 54 48 4f ains the IOMETHO
e70b0 44 53 0a 20 20 2a 2a 20 6d 61 63 72 6f 20 66 6f DS. ** macro fo
e70c0 72 20 61 64 64 69 74 69 6f 6e 20 69 6e 66 6f 72 r addition infor
e70d0 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 6e 64 65 72 mation on finder
e70e0 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a -functions.. **
e70f0 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 . ** Most finde
e7100 72 73 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e rs simply return
e7110 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
e7120 66 69 78 65 64 20 73 71 6c 69 74 65 33 5f 69 6f fixed sqlite3_io
e7130 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 2a 20 6f 62 _methods. ** ob
e7140 6a 65 63 74 2e 20 20 42 75 74 20 74 68 65 20 22 ject. But the "
e7150 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 autolockIoFinder
e7160 22 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d " available on M
e7170 61 63 4f 53 58 20 64 6f 65 73 20 61 20 6c 69 74 acOSX does a lit
e7180 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 65 20 74 68 tle. ** more th
e7190 61 6e 20 74 68 61 74 3b 20 69 74 20 6c 6f 6f 6b an that; it look
e71a0 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 79 73 s at the filesys
e71b0 74 65 6d 20 74 79 70 65 20 74 68 61 74 20 68 6f tem type that ho
e71c0 73 74 73 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 sts the . ** da
e71d0 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
e71e0 74 72 69 65 73 20 74 6f 20 63 68 6f 6f 73 65 20 tries to choose
e71f0 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f an locking metho
e7200 64 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f d appropriate fo
e7210 72 0a 20 20 2a 2a 20 74 68 61 74 20 66 69 6c 65 r. ** that file
e7220 73 79 73 74 65 6d 20 74 69 6d 65 2e 0a 20 20 2a system time.. *
e7230 2f 0a 20 20 23 64 65 66 69 6e 65 20 55 4e 49 58 /. #define UNIX
e7240 56 46 53 28 56 46 53 4e 41 4d 45 2c 20 46 49 4e VFS(VFSNAME, FIN
e7250 44 45 52 29 20 7b 20 20 20 20 20 20 20 20 20 20 DER) {
e7260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e7270 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 1,
e7280 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
e7290 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 rsion */
e72a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e72b0 20 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c sizeof(unixFil
e72c0 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a 4f 73 46 e), /* szOsF
e72d0 69 6c 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ile */
e72e0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
e72f0 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 20 20 MAX_PATHNAME,
e7300 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e /* mxPathn
e7310 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ame */
e7320 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 30 2c \. 0,
e7330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7340 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 20 /* pNext */
e7350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7360 20 20 20 20 20 20 5c 0a 20 20 20 20 56 46 53 4e \. VFSN
e7370 41 4d 45 2c 20 20 20 20 20 20 20 20 20 20 20 20 AME,
e7380 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 20 20 20 /* zName */
e7390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e73a0 20 20 20 20 5c 0a 20 20 20 20 28 76 6f 69 64 2a \. (void*
e73b0 29 26 46 49 4e 44 45 52 2c 20 20 20 20 20 20 20 )&FINDER,
e73c0 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 20 20 /* pAppData */
e73d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e73e0 20 20 5c 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e \. unixOpen
e73f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
e7400 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 xOpen */
e7410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7420 5c 0a 20 20 20 20 75 6e 69 78 44 65 6c 65 74 65 \. unixDelete
e7430 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 , /* x
e7440 44 65 6c 65 74 65 20 2a 2f 20 20 20 20 20 20 20 Delete */
e7450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e7460 20 20 20 20 75 6e 69 78 41 63 63 65 73 73 2c 20 unixAccess,
e7470 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 /* xAc
e7480 63 65 73 73 20 2a 2f 20 20 20 20 20 20 20 20 20 cess */
e7490 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e74a0 20 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 unixFullPathna
e74b0 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46 75 6c 6c me, /* xFull
e74c0 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 Pathname */
e74d0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
e74e0 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 20 20 20 20 unixDlOpen,
e74f0 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e /* xDlOpen
e7500 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e7510 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
e7520 69 78 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 ixDlError,
e7530 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 /* xDlError
e7540 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e7550 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
e7560 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 DlSym,
e7570 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 20 20 /* xDlSym */
e7580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7590 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c \. unixDl
e75a0 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
e75b0 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 20 20 /* xDlClose */
e75c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e75d0 20 20 5c 0a 20 20 20 20 75 6e 69 78 52 61 6e 64 \. unixRand
e75e0 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 20 2f 2a omness, /*
e75f0 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 20 xRandomness */
e7600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7610 5c 0a 20 20 20 20 75 6e 69 78 53 6c 65 65 70 2c \. unixSleep,
e7620 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e7630 53 6c 65 65 70 20 2a 2f 20 20 20 20 20 20 20 20 Sleep */
e7640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e7650 20 20 20 20 75 6e 69 78 43 75 72 72 65 6e 74 54 unixCurrentT
e7660 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20 78 43 75 ime, /* xCu
e7670 72 72 65 6e 74 54 69 6d 65 20 2a 2f 20 20 20 20 rrentTime */
e7680 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e7690 20 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72 unixGetLastErr
e76a0 6f 72 20 20 20 20 20 20 2f 2a 20 78 47 65 74 4c or /* xGetL
e76b0 61 73 74 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 astError */
e76c0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a \. }.
e76d0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 20 64 . /*. ** All d
e76e0 65 66 61 75 6c 74 20 56 46 53 65 73 20 66 6f 72 efault VFSes for
e76f0 20 75 6e 69 78 20 61 72 65 20 63 6f 6e 74 61 69 unix are contai
e7700 6e 65 64 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f ned in the follo
e7710 77 69 6e 67 20 61 72 72 61 79 2e 0a 20 20 2a 2a wing array.. **
e7720 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 . ** Note that
e7730 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e the sqlite3_vfs.
e7740 70 4e 65 78 74 20 66 69 65 6c 64 20 6f 66 20 74 pNext field of t
e7750 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 69 73 he VFS object is
e7760 20 6d 6f 64 69 66 69 65 64 0a 20 20 2a 2a 20 62 modified. ** b
e7770 79 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72 y the SQLite cor
e7780 65 20 77 68 65 6e 20 74 68 65 20 56 46 53 20 69 e when the VFS i
e7790 73 20 72 65 67 69 73 74 65 72 65 64 2e 20 20 53 s registered. S
e77a0 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a o the following.
e77b0 20 20 2a 2a 20 61 72 72 61 79 20 63 61 6e 6e 6f ** array canno
e77c0 74 20 62 65 20 63 6f 6e 73 74 2e 0a 20 20 2a 2f t be const.. */
e77d0 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
e77e0 33 5f 76 66 73 20 61 56 66 73 5b 5d 20 3d 20 7b 3_vfs aVfs[] = {
e77f0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
e7800 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e7810 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 && defined(__AP
e7820 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 58 56 PLE__). UNIXV
e7830 46 53 28 22 75 6e 69 78 22 2c 20 20 20 20 20 20 FS("unix",
e7840 20 20 20 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 autolockIoFi
e7850 6e 64 65 72 20 29 2c 0a 23 65 6c 73 65 0a 20 20 nder ),.#else.
e7860 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22 UNIXVFS("unix"
e7870 2c 20 20 20 20 20 20 20 20 20 20 70 6f 73 69 78 , posix
e7880 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 IoFinder ),.#end
e7890 69 66 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 if. UNIXVFS("
e78a0 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20 20 20 20 unix-none",
e78b0 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 nolockIoFinder )
e78c0 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 ,. UNIXVFS("u
e78d0 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c 20 20 64 nix-dotfile", d
e78e0 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 otlockIoFinder )
e78f0 2c 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 ,.#if OS_VXWORKS
e7900 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e . UNIXVFS("un
e7910 69 78 2d 6e 61 6d 65 64 73 65 6d 22 2c 20 73 65 ix-namedsem", se
e7920 6d 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e mIoFinder ),.#en
e7930 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 dif.#if SQLITE_E
e7940 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e7950 59 4c 45 0a 20 20 20 20 55 4e 49 58 56 46 53 28 YLE. UNIXVFS(
e7960 22 75 6e 69 78 2d 70 6f 73 69 78 22 2c 20 20 20 "unix-posix",
e7970 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 posixIoFinder )
e7980 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 ,. UNIXVFS("u
e7990 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 20 66 nix-flock", f
e79a0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a lockIoFinder ),.
e79b0 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
e79c0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
e79d0 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
e79e0 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 d(__APPLE__).
e79f0 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 61 UNIXVFS("unix-a
e7a00 66 70 22 2c 20 20 20 20 20 20 61 66 70 49 6f 46 fp", afpIoF
e7a10 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 inder ),. UNI
e7a20 58 56 46 53 28 22 75 6e 69 78 2d 70 72 6f 78 79 XVFS("unix-proxy
e7a30 22 2c 20 20 20 20 70 72 6f 78 79 49 6f 46 69 6e ", proxyIoFin
e7a40 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 der ),.#endif.
e7a50 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e };. unsigned in
e7a60 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a t i; /*
e7a70 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
e7a80 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 .. /* Register
e7a90 61 6c 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65 all VFSes define
e7aa0 64 20 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20 d in the aVfs[]
e7ab0 61 72 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 array */. for(i
e7ac0 3d 30 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56 =0; i<(sizeof(aV
e7ad0 66 73 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74 fs)/sizeof(sqlit
e7ae0 65 33 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a e3_vfs)); i++){.
e7af0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f sqlite3_vfs_
e7b00 72 65 67 69 73 74 65 72 28 26 61 56 66 73 5b 69 register(&aVfs[i
e7b10 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 ], i==0);. }.
e7b20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e7b30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 ; .}../*.** Shut
e7b40 64 6f 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 down the operati
e7b50 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
e7b60 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 ace..**.** Some
e7b70 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
e7b80 73 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 s might need to
e7b90 64 6f 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20 do some cleanup
e7ba0 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c in this routine,
e7bb0 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64 .** to release d
e7bc0 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
e7bd0 61 74 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42 ated objects. B
e7be0 75 74 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a ut not on unix..
e7bf0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
e7c00 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 is a no-op for u
e7c10 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 nix..*/.SQLITE_A
e7c20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
e7c30 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 s_end(void){ .
e7c40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e7c50 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a ; .}. .#endif /*
e7c60 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 SQLITE_OS_UNIX
e7c70 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
e7c80 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e *** End of os_un
e7c90 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ix.c ***********
e7ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e7cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e7cc0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
e7cd0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
e7ce0 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_win.c ********
e7cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e7d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e7d10 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d **/./*.** 2004 M
e7d20 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 22.**.** The
e7d30 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
e7d40 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
e7d50 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
e7d60 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
e7d70 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
e7d80 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
e7d90 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
e7da0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
e7db0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
e7dc0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
e7dd0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
e7de0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
e7df0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
e7e00 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
e7e10 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
e7e20 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
e7e30 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
e7e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e7e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e7e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e7e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e7e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
e7e90 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
e7ea0 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ns code that is
e7eb0 73 70 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64 specific to wind
e7ec0 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ows..**.** $Id:
e7ed0 6f 73 5f 77 69 6e 2e 63 2c 76 20 31 2e 31 35 33 os_win.c,v 1.153
e7ee0 20 32 30 30 39 2f 30 33 2f 33 31 20 30 33 3a 34 2009/03/31 03:4
e7ef0 31 3a 35 37 20 73 68 61 6e 65 20 45 78 70 20 24 1:57 shane Exp $
e7f00 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
e7f10 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 S_WIN
e7f20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 /* This file
e7f30 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e is used for win
e7f40 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f dows only */.../
e7f50 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 *.** A Note Abou
e7f60 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 t Memory Allocat
e7f70 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion:.**.** This
e7f80 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c driver uses mall
e7f90 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 oc()/free() dire
e7fa0 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e ctly rather than
e7fb0 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a going through.*
e7fc0 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 * the SQLite-wra
e7fd0 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 ppers sqlite3_ma
e7fe0 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 lloc()/sqlite3_f
e7ff0 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 ree(). Those wr
e8000 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 appers.** are de
e8010 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f signed for use o
e8020 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
e8030 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 ms where memory
e8040 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a is scarce and.**
e8050 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
e8060 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 happen frequent
e8070 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 ly. Win32 does
e8080 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 not typically ru
e8090 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 n on.** embedded
e80a0 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 systems, and wh
e80b0 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 en it does the d
e80c0 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c evelopers normal
e80d0 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a ly have bigger.*
e80e0 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f * problems to wo
e80f0 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 rry about than r
e8100 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 unning out of me
e8110 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 mory. So there
e8120 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 is not.** a comp
e8130 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 elling need to u
e8140 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e se the wrappers.
e8150 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 .**.** But there
e8160 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f is a good reaso
e8170 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 n to not use the
e8180 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 wrappers. If w
e8190 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 e use the.** wra
e81a0 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 ppers then we wi
e81b0 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 ll get simulated
e81c0 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 malloc() failur
e81d0 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a es within this.*
e81e0 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 * driver. And t
e81f0 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b hat causes all k
e8200 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 inds of problems
e8210 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 for our tests.
e8220 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 We.** could enh
e8230 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 ance SQLite to d
e8240 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 eal with simulat
e8250 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ed malloc failur
e8260 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 es within.** the
e8270 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 OS driver, but
e8280 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c the code to deal
e8290 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c with those fail
e82a0 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a ure would not.**
e82b0 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e be exercised on
e82c0 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f Linux (which do
e82d0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
e82e0 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 alloc() in the d
e82f0 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f river).** and so
e8300 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 we would have d
e8310 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e ifficulty writin
e8320 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 g coverage tests
e8330 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 for that.** cod
e8340 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 e. Better to le
e8350 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 ave the code out
e8360 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a , we think..**.*
e8370 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 * The point of t
e8380 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 his discussion i
e8390 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 s as follows: W
e83a0 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e hen creating a n
e83b0 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 ew.** OS layer f
e83c0 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 or an embedded s
e83d0 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 ystem, if you us
e83e0 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 e this file as a
e83f0 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 n example,.** av
e8400 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d oid the use of m
e8410 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 alloc()/free().
e8420 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 Those routines
e8430 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f work ok on windo
e8440 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 ws.** desktops b
e8450 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 ut not so well i
e8460 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
e8470 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 ms..*/..#include
e8480 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 <winbase.h>..#i
e8490 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a fdef __CYGWIN__.
e84a0 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 # include <sys/c
e84b0 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a ygwin.h>.#endif.
e84c0 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 ./*.** Macros us
e84d0 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
e84e0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
e84f0 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a o use threads..*
e8500 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 /.#if defined(TH
e8510 52 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 READSAFE) && THR
e8520 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 EADSAFE.# define
e8530 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 SQLITE_W32_THRE
e8540 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a ADS 1.#endif../*
e8550 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 .** Include code
e8560 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
e8570 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 to all os_*.c fi
e8580 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a les.*/./********
e8590 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f ****** Include o
e85a0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 s_common.h in th
e85b0 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 e middle of os_w
e85c0 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a in.c ***********
e85d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
e85e0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
e85f0 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a e os_common.h **
e8600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8620 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
e8630 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 4 May 22.**.** T
e8640 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
e8650 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
e8660 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
e8670 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
e8680 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
e8690 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
e86a0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
e86b0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
e86c0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
e86d0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
e86e0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
e86f0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
e8700 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
e8710 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
e8720 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
e8730 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
e8740 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
e8750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
e87a0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
e87b0 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 tains macros and
e87c0 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 a little bit of
e87d0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
e87e0 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f mmon to.** all o
e87f0 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 f the platform-s
e8800 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f pecific files (o
e8810 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 s_*.c) and is #i
e8820 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f ncluded into tho
e8830 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a se.** files..**.
e8840 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f ** This file sho
e8850 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 uld be #included
e8860 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 by the os_*.c f
e8870 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 iles only. It i
e8880 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 s not a.** gener
e8890 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 al purpose heade
e88a0 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 r file..**.** $I
e88b0 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 d: os_common.h,v
e88c0 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 1.38 2009/02/24
e88d0 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 18:40:50 daniel
e88e0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 k1977 Exp $.*/.#
e88f0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f ifndef _OS_COMMO
e8900 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 N_H_.#define _OS
e8910 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a _COMMON_H_../*.*
e8920 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 * At least two b
e8930 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 ugs have slipped
e8940 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 in because we c
e8950 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 hanged the MEMOR
e8960 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f Y_DEBUG.** macro
e8970 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 to SQLITE_DEBUG
e8980 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 and some older
e8990 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e makefiles have n
e89a0 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a ot yet made the.
e89b0 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 ** switch. The
e89c0 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 following code s
e89d0 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 hould catch this
e89e0 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 problem at comp
e89f0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 ile-time..*/.#if
e8a00 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 def MEMORY_DEBUG
e8a10 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 .# error "The ME
e8a20 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f MORY_DEBUG macro
e8a30 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 is obsolete. U
e8a40 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 se SQLITE_DEBUG
e8a50 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 instead.".#endif
e8a60 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
e8a70 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
e8a80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
e8a90 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 OSTrace = 0;.#de
e8aa0 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 fine OSTRACE1(X)
e8ab0 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
e8ac0 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
e8ad0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
e8ae0 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 (X).#define OSTR
e8af0 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 ACE2(X,Y)
e8b00 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
e8b10 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
e8b20 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 gPrintf(X,Y).#de
e8b30 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
e8b40 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c Y,Z) if( sql
e8b50 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
e8b60 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
e8b70 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
e8b80 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 OSTRACE4(X,Y,Z,A
e8b90 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f ) if( sqlite3O
e8ba0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
e8bb0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
e8bc0 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Z,A).#define OST
e8bd0 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 RACE5(X,Y,Z,A,B)
e8be0 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
e8bf0 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
e8c00 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
e8c10 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
e8c20 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
e8c30 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
e8c40 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
e8c50 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
e8c60 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 Y,Z,A,B,C).#defi
e8c70 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c ne OSTRACE7(X,Y,
e8c80 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 Z,A,B,C,D) \.
e8c90 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 if(sqlite3OSTra
e8ca0 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 ce) sqlite3Debug
e8cb0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
e8cc0 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 ,C,D).#else.#def
e8cd0 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a ine OSTRACE1(X).
e8ce0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 #define OSTRACE2
e8cf0 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 (X,Y).#define OS
e8d00 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 TRACE3(X,Y,Z).#d
e8d10 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 efine OSTRACE4(X
e8d20 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 ,Y,Z,A).#define
e8d30 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 OSTRACE5(X,Y,Z,A
e8d40 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
e8d50 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
e8d60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
e8d70 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
e8d80 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
e8d90 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f Macros for perfo
e8da0 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 rmance tracing.
e8db0 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 Normally turned
e8dc0 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b off. Only work
e8dd0 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 s.** on i486 har
e8de0 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 dware..*/.#ifdef
e8df0 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 SQLITE_PERFORMA
e8e00 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a NCE_TRACE../* .*
e8e10 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 * hwtime.h conta
e8e20 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d ins inline assem
e8e30 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d bler code for im
e8e40 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 plementing .** h
e8e50 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 igh-performance
e8e60 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e timing routines.
e8e70 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
e8e80 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 *** Include hwti
e8e90 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 me.h in the midd
e8ea0 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e le of os_common.
e8eb0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
e8ec0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
e8ed0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
e8ee0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
e8ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8f10 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d **/./*.** 2008 M
e8f20 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 27.**.** The
e8f30 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
e8f40 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
e8f50 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
e8f60 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
e8f70 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
e8f80 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
e8f90 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
e8fa0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
e8fb0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
e8fc0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
e8fd0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
e8fe0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
e8ff0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
e9000 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
e9010 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
e9020 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
e9030 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
e9040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9080 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
e9090 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
e90a0 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f ns inline asm co
e90b0 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e de for retrievin
e90c0 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 g "high-performa
e90d0 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 nce".** counters
e90e0 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 for x86 class C
e90f0 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 PUs..**.** $Id:
e9100 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 hwtime.h,v 1.3 2
e9110 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 008/08/01 14:33:
e9120 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 15 shane Exp $.*
e9130 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d /.#ifndef _HWTIM
e9140 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 E_H_.#define _HW
e9150 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 TIME_H_../*.** T
e9160 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
e9170 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tine only works
e9180 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 on pentium-class
e9190 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 (or newer) proc
e91a0 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 essors..** It us
e91b0 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 es the RDTSC opc
e91c0 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 ode to read the
e91d0 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 cycle count valu
e91e0 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 e out of the.**
e91f0 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 processor and re
e9200 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 turns that value
e9210 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
e9220 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 sed for high-res
e9230 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a .** profiling..*
e9240 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f /.#if (defined(_
e9250 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 _GNUC__) || defi
e9260 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 ned(_MSC_VER)) &
e9270 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e & \. (defin
e9280 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 ed(i386) || defi
e9290 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c ned(__i386__) ||
e92a0 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 defined(_M_IX86
e92b0 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 )).. #if define
e92c0 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f d(__GNUC__).. _
e92d0 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
e92e0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
e92f0 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
e9300 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c unsigned int l
e9310 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 o, hi;. __as
e9320 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
e9330 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 ("rdtsc" : "=a"
e9340 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 (lo), "=d" (hi)
e9350 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 );. return (
e9360 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 sqlite_uint64)hi
e9370 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d << 32 | lo;. }
e9380 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 .. #elif define
e9390 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f d(_MSC_VER).. _
e93a0 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 _declspec(naked)
e93b0 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 __inline sqlite
e93c0 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 _uint64 __cdecl
e93d0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
e93e0 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 id){. __asm
e93f0 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a {. rdtsc.
e9400 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 ret
e9410 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 ; return value
e9420 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 at EDX:EAX.
e9430 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 }. }.. #endif
e9440 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 ..#elif (defined
e9450 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
e9460 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f fined(__x86_64__
e9470 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
e9480 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
e9490 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
e94a0 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
e94b0 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 ed long val;.
e94c0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
e94d0 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
e94e0 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a : "=A" (val));.
e94f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c return val
e9500 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 ;. }. .#elif (d
e9510 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
e9520 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 && defined(__pp
e9530 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e c__)).. __inlin
e9540 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
e9550 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
e9560 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
e9570 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
e9580 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e retval;. un
e9590 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b signed long junk
e95a0 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
e95b0 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c __volatile__ ("\
e95c0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 n\. 1:
e95d0 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c mftbu %1\
e95e0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
e95f0 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 mftb %L0
e9600 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
e9610 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 mftbu %0
e9620 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
e9630 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 cmpw %0
e9640 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 ,%1\n\.
e9650 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 bne
e9660 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 1b".
e9670 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 : "=r" (r
e9680 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 etval), "=r" (ju
e9690 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 nk));. retu
e96a0 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a rn retval;. }..
e96b0 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 #else.. #error
e96c0 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 Need implementat
e96d0 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 ion of sqlite3Hw
e96e0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
e96f0 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a platform... /*.
e9700 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 ** To compile
e9710 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e without implemen
e9720 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 ting sqlite3Hwti
e9730 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
e9740 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 atform,. ** you
e9750 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 can remove the
e9760 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 above #error and
e9770 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 use the followi
e9780 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e ng. ** stub fun
e9790 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c ction. You will
e97a0 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 lose timing sup
e97b0 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 port for many.
e97c0 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 ** of the debugg
e97d0 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 ing and testing
e97e0 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 utilities, but i
e97f0 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a t should at. **
e9800 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 least compile a
e9810 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c nd run.. */.SQL
e9820 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 ITE_PRIVATE sq
e9830 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
e9840 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
e9850 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 return ((sqlite
e9860 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 _uint64)0); }..#
e9870 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
e9880 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d !defined(_HWTIM
e9890 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a E_H_) */../*****
e98a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
e98b0 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a hwtime.h ******
e98c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e98d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e98e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
e98f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
e9900 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
e9910 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d ft off in os_com
e9920 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mon.h **********
e9930 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 ********/..stati
e9940 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 c sqlite_uint64
e9950 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 g_start;.static
e9960 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
e9970 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 elapsed;.#define
e9980 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 TIMER_START
e9990 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 g_start=sqlit
e99a0 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 e3Hwtime().#defi
e99b0 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 ne TIMER_END
e99c0 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 g_elapsed=s
e99d0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 qlite3Hwtime()-g
e99e0 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 _start.#define T
e99f0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 IMER_ELAPSED
e9a00 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 g_elapsed.#else
e9a10 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 .#define TIMER_S
e9a20 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d TART.#define TIM
e9a30 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 ER_END.#define T
e9a40 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 IMER_ELAPSED
e9a50 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ((sqlite_uint64
e9a60 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a )0).#endif../*.*
e9a70 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 * If we compile
e9a80 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
e9a90 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 TEST macro set,
e9aa0 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 then the followi
e9ab0 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 ng block.** of c
e9ac0 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 ode will give us
e9ad0 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 the ability to
e9ae0 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 simulate a disk
e9af0 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 I/O error. This
e9b00 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 .** is used for
e9b10 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 testing the I/O
e9b20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a recovery logic..
e9b30 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
e9b40 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
e9b50 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
e9b60 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 error_hit = 0;
e9b70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
e9b80 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f al number of I/O
e9b90 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 Errors */.SQLIT
e9ba0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
e9bb0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 3_io_error_hardh
e9bc0 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f it = 0; /
e9bd0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d * Number of non-
e9be0 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f benign errors */
e9bf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
e9c00 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
e9c10 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 _pending = 0;
e9c20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f /* Count do
e9c30 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 wn to first I/O
e9c40 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f error */.SQLITE_
e9c50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
e9c60 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 io_error_persist
e9c70 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
e9c80 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f True if I/O erro
e9c90 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 rs persist */.SQ
e9ca0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
e9cb0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 ite3_io_error_be
e9cc0 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 nign = 0;
e9cd0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 /* True if err
e9ce0 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a ors are benign *
e9cf0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
e9d00 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
e9d10 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 l_pending = 0;.S
e9d20 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e9d30 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d lite3_diskfull =
e9d40 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 0;.#define Simu
e9d50 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
e9d60 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f n(X) sqlite3_io_
e9d70 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 error_benign=(X)
e9d80 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
e9d90 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 eIOError(CODE)
e9da0 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 \. if( (sqlite3
e9db0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 _io_error_persis
e9dc0 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f t && sqlite3_io_
e9dd0 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 error_hit) \.
e9de0 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 || sqlite3_i
e9df0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d o_error_pending-
e9e00 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 - == 1 ) \.
e9e10 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 { loca
e9e20 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b l_ioerr(); CODE;
e9e30 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c }.static void l
e9e40 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 ocal_ioerr(){.
e9e50 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c IOTRACE(("IOERR\
e9e60 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f n"));. sqlite3_
e9e70 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a io_error_hit++;.
e9e80 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 if( !sqlite3_i
e9e90 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 o_error_benign )
e9ea0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
e9eb0 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 r_hardhit++;.}.#
e9ec0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 define SimulateD
e9ed0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 iskfullError(COD
e9ee0 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 E) \. if( sqli
e9ef0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
e9f00 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 ding ){ \. i
e9f10 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
e9f20 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 ull_pending == 1
e9f30 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 ){ \. loc
e9f40 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 al_ioerr(); \.
e9f50 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 sqlite3_dis
e9f60 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 kfull = 1; \.
e9f70 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 sqlite3_io_e
e9f80 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a rror_hit = 1; \.
e9f90 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 CODE; \.
e9fa0 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 }else{ \.
e9fb0 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b sqlite3_disk
e9fc0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 full_pending--;
e9fd0 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a \. } \. }.
e9fe0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 #else.#define Si
e9ff0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
ea000 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 ign(X).#define S
ea010 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 imulateIOError(A
ea020 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
ea030 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
ea040 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a A).#endif../*.**
ea050 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b When testing, k
ea060 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 eep a count of t
ea070 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 he number of ope
ea080 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 n files..*/.#ifd
ea090 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
ea0a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
ea0b0 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f lite3_open_file_
ea0c0 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 count = 0;.#defi
ea0d0 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 ne OpenCounter(X
ea0e0 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f ) sqlite3_open_
ea0f0 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a file_count+=(X).
ea100 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 #else.#define Op
ea110 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e enCounter(X).#en
ea120 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
ea130 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d defined(_OS_COMM
ea140 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a ON_H_) */../****
ea150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
ea160 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a f os_common.h **
ea170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
ea1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
ea1b0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
ea1c0 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69 eft off in os_wi
ea1d0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
ea1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
ea1f0 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 * Some microsoft
ea200 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 compilers lack
ea210 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e this definition.
ea220 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 .*/.#ifndef INVA
ea230 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 LID_FILE_ATTRIBU
ea240 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 TES.# define INV
ea250 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
ea260 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29 UTES ((DWORD)-1)
ea270 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 .#endif../*.**
ea280 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 Determine if we
ea290 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
ea2a0 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 WindowsCE - whi
ea2b0 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a ch has a much.**
ea2c0 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f reduced API..*/
ea2d0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
ea2e0 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72 INCE.# define Ar
ea2f0 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 eFileApisANSI()
ea300 31 0a 23 20 64 65 66 69 6e 65 20 47 65 74 44 69 1.# define GetDi
ea310 73 6b 46 72 65 65 53 70 61 63 65 57 28 29 20 30 skFreeSpaceW() 0
ea320 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
ea330 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76 inCE lacks nativ
ea340 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69 e support for fi
ea350 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65 le locking so we
ea360 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74 have to fake it
ea370 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f .** with some co
ea380 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a de of our own..*
ea390 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
ea3a0 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74 WINCE.typedef st
ea3b0 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b ruct winceLock {
ea3c0 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b . int nReaders;
ea3d0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
ea3e0 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 of reader locks
ea3f0 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 obtained */. B
ea400 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20 OOL bPending;
ea410 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 /* Indicates
ea420 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 a pending lock h
ea430 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 as been obtained
ea440 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65 */. BOOL bRese
ea450 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64 rved; /* Ind
ea460 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65 icates a reserve
ea470 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 d lock has been
ea480 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f obtained */. BO
ea490 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20 OL bExclusive;
ea4a0 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 /* Indicates a
ea4b0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
ea4c0 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e has been obtain
ea4d0 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 ed */.} winceLoc
ea4e0 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a k;.#endif../*.**
ea4f0 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72 The winFile str
ea500 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63 ucture is a subc
ea510 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f lass of sqlite3_
ea520 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74 file* specific t
ea530 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70 o the win32.** p
ea540 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72 ortability layer
ea550 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
ea560 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46 uct winFile winF
ea570 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46 ile;.struct winF
ea580 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 ile {. const sq
ea590 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
ea5a0 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 *pMethod;/* Mus
ea5b0 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 t be first */.
ea5c0 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20 HANDLE h;
ea5d0 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c /* Handl
ea5e0 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 e for accessing
ea5f0 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e the file */. un
ea600 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b signed char lock
ea610 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66 type; /* Type of
ea620 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 lock currently
ea630 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c held on this fil
ea640 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61 e */. short sha
ea650 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f redLockByte; /
ea660 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 * Randomly chose
ea670 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61 n byte used as a
ea680 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a shared lock */.
ea690 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e DWORD lastErrn
ea6a0 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 o; /* The
ea6b0 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 66 Windows errno f
ea6c0 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f rom the last I/O
ea6d0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f 52 error */. DWOR
ea6e0 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 D sectorSize;
ea6f0 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 /* Sector si
ea700 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65 ze of the device
ea710 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a 23 file is on */.#
ea720 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ea730 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c CE. WCHAR *zDel
ea740 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 eteOnClose; /*
ea750 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
ea760 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73 delete when clos
ea770 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 ing */. HANDLE
ea780 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 hMutex;
ea790 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74 /* Mutex used t
ea7a0 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
ea7b0 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 to shared lock
ea7c0 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53 */ . HANDLE hS
ea7d0 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f hared; /
ea7e0 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * Shared memory
ea7f0 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72 segment used for
ea800 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 locking */. wi
ea810 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 nceLock local;
ea820 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f /* Locks o
ea830 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 btained by this
ea840 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46 instance of winF
ea850 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f ile */. winceLo
ea860 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20 ck *shared;
ea870 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65 /* Global share
ea880 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f d lock memory fo
ea890 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23 r the file */.#
ea8a0 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
ea8b0 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 70 Forward prototyp
ea8c0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
ea8d0 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 t getSectorSize(
ea8e0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
ea8f0 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 *pVfs,. cons
ea900 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
ea910 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 e /* UTF-8 f
ea920 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a ile name */.);..
ea930 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
ea940 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20 ing variable is
ea950 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f (normally) set o
ea960 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68 nce and never ch
ea970 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66 anges.** thereaf
ea980 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73 ter. It records
ea990 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65 whether the ope
ea9a0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 rating system is
ea9b0 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e Win95.** or Win
ea9c0 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f NT..**.** 0: O
ea9d0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
ea9e0 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 unknown..** 1:
ea9f0 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 Operating syste
eaa00 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 m is Win95..** 2
eaa10 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 : Operating sy
eaa20 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a stem is WinNT..*
eaa30 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f *.** In order to
eaa40 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74 facilitate test
eaa50 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73 ing on a WinNT s
eaa60 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20 ystem, the test
eaa70 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d fixture.** can m
eaa80 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73 anually set this
eaa90 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65 value to 1 to e
eaaa0 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68 mulate Win98 beh
eaab0 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 avior..*/.#ifdef
eaac0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
eaad0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
eaae0 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b te3_os_type = 0;
eaaf0 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e .#else.static in
eab00 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 t sqlite3_os_typ
eab10 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f e = 0;.#endif../
eab20 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
eab30 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 (non-zero) if w
eab40 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e e are running un
eab50 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b der WinNT, Win2K
eab60 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 , WinXP,.** or W
eab70 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 inCE. Return fa
eab80 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 lse (zero) for W
eab90 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 in95, Win98, or
eaba0 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 WinME..**.** Her
eabb0 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 e is an interest
eabc0 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a ing observation:
eabd0 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 Win95, Win98,
eabe0 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a and WinME lack.*
eabf0 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 * the LockFileEx
eac00 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 () API. But we
eac10 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 can still static
eac20 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 ally link agains
eac30 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 t that.** API as
eac40 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 long as we don'
eac50 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 72 t call it when r
eac60 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f unning Win95/98/
eac70 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a ME. A call to.*
eac80 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * this routine i
eac90 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
eaca0 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 ine if the host
eacb0 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f is Win95/98/ME o
eacc0 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 r.** WinNT/2K/XP
eacd0 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c so that we will
eace0 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 know whether or
eacf0 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 not we can safe
ead00 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c ly call.** the L
ead10 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
ead20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
ead30 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 S_WINCE.# define
ead40 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c isNT() (1).#el
ead50 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 se. static int
ead60 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 isNT(void){.
ead70 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 if( sqlite3_os_t
ead80 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ype==0 ){.
ead90 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 OSVERSIONINFO sI
eada0 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f nfo;. sInfo
eadb0 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f .dwOSVersionInfo
eadc0 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 Size = sizeof(sI
eadd0 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 nfo);. GetV
eade0 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 ersionEx(&sInfo)
eadf0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
eae00 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e os_type = sInfo.
eae10 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 dwPlatformId==VE
eae20 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 R_PLATFORM_WIN32
eae30 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 _NT ? 2 : 1;.
eae40 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 }. return sq
eae50 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 lite3_os_type==2
eae60 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
eae70 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
eae80 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 */../*.** Conver
eae90 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 t a UTF-8 string
eaea0 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e to microsoft un
eaeb0 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e icode (UTF-16?).
eaec0 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f .**.** Space to
eaed0 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e hold the return
eaee0 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 ed string is obt
eaef0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
eaf00 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 c..*/.static WCH
eaf10 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64 AR *utf8ToUnicod
eaf20 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 e(const char *zF
eaf30 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 ilename){. int
eaf40 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a nChar;. WCHAR *
eaf50 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a zWideFilename;..
eaf60 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 nChar = MultiB
eaf70 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 yteToWideChar(CP
eaf80 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e _UTF8, 0, zFilen
eaf90 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 ame, -1, NULL, 0
eafa0 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 );. zWideFilena
eafb0 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 me = malloc( nCh
eafc0 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46 ar*sizeof(zWideF
eafd0 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 ilename[0]) );.
eafe0 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61 if( zWideFilena
eaff0 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 me==0 ){. ret
eb000 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 urn 0;. }. nCh
eb010 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f ar = MultiByteTo
eb020 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 WideChar(CP_UTF8
eb030 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
eb040 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d -1, zWideFilenam
eb050 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 e, nChar);. if(
eb060 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 nChar==0 ){.
eb070 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e free(zWideFilen
eb080 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46 ame);. zWideF
eb090 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d ilename = 0;. }
eb0a0 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46 . return zWideF
eb0b0 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ilename;.}../*.*
eb0c0 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 * Convert micros
eb0d0 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55 oft unicode to U
eb0e0 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 TF-8. Space to
eb0f0 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 hold the returne
eb100 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f d string is.** o
eb110 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c btained from mal
eb120 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
eb130 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f char *unicodeTo
eb140 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52 Utf8(const WCHAR
eb150 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 *zWideFilename)
eb160 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 {. int nByte;.
eb170 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
eb180 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 ;.. nByte = Wid
eb190 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 eCharToMultiByte
eb1a0 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 (CP_UTF8, 0, zWi
eb1b0 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 deFilename, -1,
eb1c0 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 0, 0, 0, 0);. z
eb1d0 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f Filename = mallo
eb1e0 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 c( nByte );. if
eb1f0 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 ( zFilename==0 )
eb200 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
eb210 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 }. nByte = Wi
eb220 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 deCharToMultiByt
eb230 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 e(CP_UTF8, 0, zW
eb240 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c ideFilename, -1,
eb250 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 zFilename, nByt
eb260 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
eb270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eb280 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 0, 0);. if( nB
eb290 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 yte == 0 ){.
eb2a0 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b free(zFilename);
eb2b0 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d . zFilename =
eb2c0 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
eb2d0 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f zFilename;.}../
eb2e0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 *.** Convert an
eb2f0 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d ansi string to m
eb300 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
eb310 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a , based on the.*
eb320 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 * current codepa
eb330 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 ge settings for
eb340 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a file apis..** .*
eb350 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
eb360 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 the returned str
eb370 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a ing is obtained.
eb380 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a ** from malloc..
eb390 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 */.static WCHAR
eb3a0 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63 *mbcsToUnicode(c
eb3b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
eb3c0 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 name){. int nBy
eb3d0 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62 te;. WCHAR *zMb
eb3e0 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e csFilename;. in
eb3f0 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 t codepage = Are
eb400 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f FileApisANSI() ?
eb410 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d CP_ACP : CP_OEM
eb420 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d CP;.. nByte = M
eb430 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 ultiByteToWideCh
eb440 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 ar(codepage, 0,
eb450 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e zFilename, -1, N
eb460 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43 ULL,0)*sizeof(WC
eb470 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c HAR);. zMbcsFil
eb480 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 ename = malloc(
eb490 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 nByte*sizeof(zMb
eb4a0 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 csFilename[0]) )
eb4b0 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c ;. if( zMbcsFil
eb4c0 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 ename==0 ){.
eb4d0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
eb4e0 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74 nByte = MultiByt
eb4f0 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65 eToWideChar(code
eb500 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 page, 0, zFilena
eb510 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c me, -1, zMbcsFil
eb520 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20 ename, nByte);.
eb530 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b if( nByte==0 ){
eb540 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46 . free(zMbcsF
eb550 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d ilename);. zM
eb560 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b bcsFilename = 0;
eb570 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d . }. return zM
eb580 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a bcsFilename;.}..
eb590 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 /*.** Convert mi
eb5a0 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 crosoft unicode
eb5b0 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 to multibyte cha
eb5c0 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 racter string, b
eb5d0 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 ased on the.** u
eb5e0 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70 ser's Ansi codep
eb5f0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 age..**.** Space
eb600 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 to hold the ret
eb610 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 urned string is
eb620 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
eb630 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 malloc()..*/.st
eb640 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f atic char *unico
eb650 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57 deToMbcs(const W
eb660 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e CHAR *zWideFilen
eb670 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ame){. int nByt
eb680 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 e;. char *zFile
eb690 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 name;. int code
eb6a0 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70 page = AreFileAp
eb6b0 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 isANSI() ? CP_AC
eb6c0 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 P : CP_OEMCP;..
eb6d0 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 nByte = WideCha
eb6e0 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 rToMultiByte(cod
eb6f0 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 epage, 0, zWideF
eb700 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 ilename, -1, 0,
eb710 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 0, 0, 0);. zFil
eb720 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 ename = malloc(
eb730 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a nByte );. if( z
eb740 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 Filename==0 ){.
eb750 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
eb760 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 . nByte = WideC
eb770 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 harToMultiByte(c
eb780 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 odepage, 0, zWid
eb790 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a eFilename, -1, z
eb7a0 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c Filename, nByte,
eb7b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
eb7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
eb7d0 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 , 0);. if( nByt
eb7e0 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 e == 0 ){. fr
eb7f0 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 ee(zFilename);.
eb800 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 zFilename = 0
eb810 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
eb820 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a Filename;.}../*.
eb830 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69 ** Convert multi
eb840 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 byte character s
eb850 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20 tring to UTF-8.
eb860 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
eb870 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 he.** returned s
eb880 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 tring is obtaine
eb890 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e d from malloc().
eb8a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
eb8b0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e har *sqlite3_win
eb8c0 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 32_mbcs_to_utf8(
eb8d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
eb8e0 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a ename){. char *
eb8f0 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 zFilenameUtf8;.
eb900 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 WCHAR *zTmpWide
eb910 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 ;.. zTmpWide =
eb920 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 mbcsToUnicode(zF
eb930 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 ilename);. if(
eb940 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 zTmpWide==0 ){.
eb950 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
eb960 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 . zFilenameUtf8
eb970 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 = unicodeToUtf8
eb980 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 (zTmpWide);. fr
eb990 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 ee(zTmpWide);.
eb9a0 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 return zFilename
eb9b0 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 Utf8;.}../*.** C
eb9c0 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20 onvert UTF-8 to
eb9d0 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 multibyte charac
eb9e0 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61 ter string. Spa
eb9f0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a ce to hold the .
eba00 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 ** returned stri
eba10 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 ng is obtained f
eba20 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f rom malloc()..*/
eba30 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74 .static char *ut
eba40 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 f8ToMbcs(const c
eba50 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
eba60 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 . char *zFilena
eba70 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20 meMbcs;. WCHAR
eba80 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 *zTmpWide;.. zT
eba90 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 mpWide = utf8ToU
ebaa0 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 nicode(zFilename
ebab0 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 );. if( zTmpWid
ebac0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 e==0 ){. retu
ebad0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c rn 0;. }. zFil
ebae0 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 enameMbcs = unic
ebaf0 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 odeToMbcs(zTmpWi
ebb00 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 de);. free(zTmp
ebb10 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 Wide);. return
ebb20 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d zFilenameMbcs;.}
ebb30 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
ebb40 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a WINCE./*********
ebb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebb90 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e .** This section
ebba0 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 contains code f
ebbb0 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a or WinCE only..*
ebbc0 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 /./*.** WindowsC
ebbd0 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 E does not have
ebbe0 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 a localtime() fu
ebbf0 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 nction. So crea
ebc00 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 te a.** substitu
ebc10 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d te..*/.struct tm
ebc20 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 *__cdecl localt
ebc30 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 ime(const time_t
ebc40 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 *t).{. static
ebc50 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 struct tm y;. F
ebc60 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d ILETIME uTm, lTm
ebc70 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70 ;. SYSTEMTIME p
ebc80 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e Tm;. sqlite3_in
ebc90 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d t64 t64;. t64 =
ebca0 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36 *t;. t64 = (t6
ebcb0 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29 4 + 11644473600)
ebcc0 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d *10000000;. uTm
ebcd0 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d .dwLowDateTime =
ebce0 20 74 36 34 20 26 20 30 78 46 46 46 46 46 46 46 t64 & 0xFFFFFFF
ebcf0 46 3b 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44 F;. uTm.dwHighD
ebd00 61 74 65 54 69 6d 65 3d 20 74 36 34 20 3e 3e 20 ateTime= t64 >>
ebd10 33 32 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 32;. FileTimeTo
ebd20 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 LocalFileTime(&u
ebd30 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 Tm,&lTm);. File
ebd40 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 TimeToSystemTime
ebd50 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 (&lTm,&pTm);. y
ebd60 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 .tm_year = pTm.w
ebd70 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 Year - 1900;. y
ebd80 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d .tm_mon = pTm.wM
ebd90 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d onth - 1;. y.tm
ebda0 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 _wday = pTm.wDay
ebdb0 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d OfWeek;. y.tm_m
ebdc0 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a day = pTm.wDay;.
ebdd0 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 y.tm_hour = pT
ebde0 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f m.wHour;. y.tm_
ebdf0 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 min = pTm.wMinut
ebe00 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 e;. y.tm_sec =
ebe10 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 pTm.wSecond;. r
ebe20 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 eturn &y;.}../*
ebe30 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 This will never
ebe40 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 be called, but d
ebe50 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 efined to make t
ebe60 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 he code compile
ebe70 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 74 54 65 */.#define GetTe
ebe80 6d 70 50 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 mpPathA(a,b)..#d
ebe90 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 efine LockFile(a
ebea0 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 20 20 ,b,c,d,e)
ebeb0 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 winceLockFile(&a
ebec0 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 , b, c, d, e).#d
ebed0 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 efine UnlockFile
ebee0 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 (a,b,c,d,e)
ebef0 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 winceUnlockFile(
ebf00 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a &a, b, c, d, e).
ebf10 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 #define LockFile
ebf20 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 Ex(a,b,c,d,e,f)
ebf30 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 winceLockFileE
ebf40 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 x(&a, b, c, d, e
ebf50 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 , f)..#define HA
ebf60 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 NDLE_TO_WINFILE(
ebf70 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 a) (winFile*)&((
ebf80 63 68 61 72 2a 29 61 29 5b 2d 6f 66 66 73 65 74 char*)a)[-offset
ebf90 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a of(winFile,h)]..
ebfa0 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 /*.** Acquire a
ebfb0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 6e 64 lock on the hand
ebfc0 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63 20 76 le h.*/.static v
ebfd0 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 78 41 63 oid winceMutexAc
ebfe0 71 75 69 72 65 28 48 41 4e 44 4c 45 20 68 29 7b quire(HANDLE h){
ebff0 0a 20 20 20 44 57 4f 52 44 20 64 77 45 72 72 3b . DWORD dwErr;
ec000 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 64 77 . do {. dw
ec010 45 72 72 20 3d 20 57 61 69 74 46 6f 72 53 69 6e Err = WaitForSin
ec020 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49 4e 46 gleObject(h, INF
ec030 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77 68 69 INITE);. } whi
ec040 6c 65 20 28 64 77 45 72 72 20 21 3d 20 57 41 49 le (dwErr != WAI
ec050 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20 64 77 T_OBJECT_0 && dw
ec060 45 72 72 20 21 3d 20 57 41 49 54 5f 41 42 41 4e Err != WAIT_ABAN
ec070 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 DONED);.}./*.**
ec080 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b 20 61 Release a lock a
ec090 63 71 75 69 72 65 64 20 62 79 20 77 69 6e 63 65 cquired by wince
ec0a0 4d 75 74 65 78 41 63 71 75 69 72 65 28 29 0a 2a MutexAcquire().*
ec0b0 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e 63 65 4d /.#define winceM
ec0c0 75 74 65 78 52 65 6c 65 61 73 65 28 68 29 20 52 utexRelease(h) R
ec0d0 65 6c 65 61 73 65 4d 75 74 65 78 28 68 29 0a 0a eleaseMutex(h)..
ec0e0 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 /*.** Create the
ec0f0 20 6d 75 74 65 78 20 61 6e 64 20 73 68 61 72 65 mutex and share
ec100 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f d memory used fo
ec110 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65 r locking in the
ec120 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 file.** descrip
ec130 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 tor pFile.*/.sta
ec140 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 43 72 tic BOOL winceCr
ec150 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 eateLock(const c
ec160 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
ec170 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b winFile *pFile){
ec180 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a . WCHAR *zTok;.
ec190 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65 20 3d WCHAR *zName =
ec1a0 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a utf8ToUnicode(z
ec1b0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f Filename);. BOO
ec1c0 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a L bInit = TRUE;.
ec1d0 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 . /* Initialize
ec1e0 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 the local lockd
ec1f0 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d ata */. ZeroMem
ec200 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 ory(&pFile->loca
ec210 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c 65 2d l, sizeof(pFile-
ec220 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 >local));.. /*
ec230 52 65 70 6c 61 63 65 20 74 68 65 20 62 61 63 6b Replace the back
ec240 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 74 68 65 slashes from the
ec250 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 6c 6f filename and lo
ec260 77 65 72 63 61 73 65 20 69 74 0a 20 20 2a 2a 20 wercase it. **
ec270 74 6f 20 64 65 72 69 76 65 20 61 20 6d 75 74 65 to derive a mute
ec280 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f x name. */. zTo
ec290 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 57 28 7a k = CharLowerW(z
ec2a0 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28 3b 2a Name);. for (;*
ec2b0 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 zTok;zTok++){.
ec2c0 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 if (*zTok == '
ec2d0 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 \\') *zTok = '_'
ec2e0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 ;. }.. /* Crea
ec2f0 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61 6d 65 te/open the name
ec300 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70 46 69 d mutex */. pFi
ec310 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43 72 65 le->hMutex = Cre
ec320 61 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c 2c 20 ateMutexW(NULL,
ec330 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 FALSE, zName);.
ec340 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 if (!pFile->hMu
ec350 74 65 78 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d tex){. pFile-
ec360 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 >lastErrno = Get
ec370 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
ec380 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 free(zName);.
ec390 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a return FALSE;.
ec3a0 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
ec3b0 65 20 74 68 65 20 6d 75 74 65 78 20 62 65 66 6f e the mutex befo
ec3c0 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f re continuing */
ec3d0 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 . winceMutexAcq
ec3e0 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 uire(pFile->hMut
ec3f0 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53 69 6e ex);. . /* Sin
ec400 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 ce the names of
ec410 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c 20 73 named mutexes, s
ec420 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c 65 20 emaphores, file
ec430 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61 72 65 mappings etc are
ec440 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65 6e 73 . ** case-sens
ec450 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64 76 61 itive, take adva
ec460 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20 62 79 ntage of that by
ec470 20 75 70 70 65 72 63 61 73 69 6e 67 20 74 68 65 uppercasing the
ec480 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20 2a 2a mutex name. **
ec490 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61 74 20 and using that
ec4a0 61 73 20 74 68 65 20 73 68 61 72 65 64 20 66 69 as the shared fi
ec4b0 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65 2e 0a lemapping name..
ec4c0 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70 65 72 */. CharUpper
ec4d0 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46 69 6c W(zName);. pFil
ec4e0 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43 72 65 e->hShared = Cre
ec4f0 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 57 28 ateFileMappingW(
ec500 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 INVALID_HANDLE_V
ec510 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20 ALUE, NULL,.
ec520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ec530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ec540 20 20 20 50 41 47 45 5f 52 45 41 44 57 52 49 54 PAGE_READWRIT
ec550 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e E, 0, sizeof(win
ec560 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20 20 20 ceLock),.
ec570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ec580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ec590 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f 2a 20 zName); .. /*
ec5a0 53 65 74 20 61 20 66 6c 61 67 20 74 68 61 74 20 Set a flag that
ec5b0 69 6e 64 69 63 61 74 65 73 20 77 65 27 72 65 20 indicates we're
ec5c0 74 68 65 20 66 69 72 73 74 20 74 6f 20 63 72 65 the first to cre
ec5d0 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 ate the memory s
ec5e0 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73 74 20 o it . ** must
ec5f0 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61 6c 69 be zero-initiali
ec600 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47 65 74 zed */. if (Get
ec610 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d 20 45 LastError() == E
ec620 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45 58 49 RROR_ALREADY_EXI
ec630 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69 74 20 STS){. bInit
ec640 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 = FALSE;. }..
ec650 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20 free(zName);..
ec660 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65 65 64 /* If we succeed
ec670 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74 68 65 ed in making the
ec680 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 shared memory h
ec690 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e 20 2a andle, map it. *
ec6a0 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 /. if (pFile->h
ec6b0 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70 46 69 Shared){. pFi
ec6c0 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28 77 69 le->shared = (wi
ec6d0 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69 65 77 nceLock*)MapView
ec6e0 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 53 OfFile(pFile->hS
ec6f0 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20 20 20 hared, .
ec700 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f 52 45 FILE_MAP_RE
ec710 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52 49 54 AD|FILE_MAP_WRIT
ec720 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 E, 0, 0, sizeof(
ec730 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 20 winceLock));.
ec740 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67 20 66 /* If mapping f
ec750 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74 68 65 ailed, close the
ec760 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 shared memory h
ec770 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73 65 20 andle and erase
ec780 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 21 70 it */. if (!p
ec790 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b 0a 20 File->shared){.
ec7a0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
ec7b0 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 Errno = GetLastE
ec7c0 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 43 6c rror();. Cl
ec7d0 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d oseHandle(pFile-
ec7e0 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 >hShared);.
ec7f0 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 pFile->hShared
ec800 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 = NULL;. }.
ec810 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 72 65 }.. /* If share
ec820 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e d memory could n
ec830 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c 20 74 ot be created, t
ec840 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 6d 75 hen close the mu
ec850 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0a tex and fail */.
ec860 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 if (pFile->hSh
ec870 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 ared == NULL){.
ec880 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c winceMutexRel
ec890 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 ease(pFile->hMut
ec8a0 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 ex);. CloseHa
ec8b0 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 ndle(pFile->hMut
ec8c0 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ex);. pFile->
ec8d0 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 hMutex = NULL;.
ec8e0 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b return FALSE;
ec8f0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 . }. . /* Ini
ec900 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68 61 72 tialize the shar
ec910 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 65 27 ed memory if we'
ec920 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f 20 2a re supposed to *
ec930 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29 20 7b /. if (bInit) {
ec940 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 . ZeroMemory(
ec950 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c 20 73 pFile->shared, s
ec960 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 izeof(winceLock)
ec970 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d );. }.. winceM
ec980 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
ec990 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 e->hMutex);. re
ec9a0 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a turn TRUE;.}../*
ec9b0 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 65 20 .** Destroy the
ec9c0 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c 65 20 part of winFile
ec9d0 74 68 61 74 20 64 65 61 6c 73 20 77 69 74 68 20 that deals with
ec9e0 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 wince locks.*/.s
ec9f0 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65 tatic void wince
eca00 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 DestroyLock(winF
eca10 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 ile *pFile){. i
eca20 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 f (pFile->hMutex
eca30 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 69 72 ){. /* Acquir
eca40 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 e the mutex */.
eca50 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 winceMutexAcq
eca60 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 uire(pFile->hMut
eca70 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 ex);.. /* The
eca80 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
eca90 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c s should probabl
ecaa0 79 20 61 73 73 65 72 74 20 69 6e 20 64 65 62 75 y assert in debu
ecab0 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 65 79 g mode, but they
ecac0 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f 20 63 . are to c
ecad0 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65 20 61 leanup in case a
ecae0 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 6e 65 ny locks remaine
ecaf0 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 66 d open */. if
ecb00 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e (pFile->local.n
ecb10 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 Readers){.
ecb20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e pFile->shared->n
ecb30 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 Readers --;.
ecb40 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d }. if (pFile-
ecb50 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 >local.bReserved
ecb60 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
ecb70 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 shared->bReserve
ecb80 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d d = FALSE;. }
ecb90 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
ecba0 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b local.bPending){
ecbb0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 . pFile->sh
ecbc0 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d ared->bPending =
ecbd0 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 FALSE;. }.
ecbe0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
ecbf0 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a al.bExclusive){.
ecc00 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
ecc10 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 red->bExclusive
ecc20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a = FALSE;. }..
ecc30 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 72 65 /* De-refere
ecc40 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 6f 75 nce and close ou
ecc50 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 68 r copy of the sh
ecc60 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 ared memory hand
ecc70 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 70 56 le */. UnmapV
ecc80 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d iewOfFile(pFile-
ecc90 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20 43 6c >shared);. Cl
ecca0 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d oseHandle(pFile-
eccb0 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 20 20 >hShared);..
eccc0 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65 /* Done with the
eccd0 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 mutex */. wi
ecce0 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 nceMutexRelease(
eccf0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 pFile->hMutex);
ecd00 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e . CloseHan
ecd10 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 dle(pFile->hMute
ecd20 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 x);. pFile->h
ecd30 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 Mutex = NULL;.
ecd40 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 }.}../* .** An i
ecd50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
ecd60 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20 the LockFile()
ecd70 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 API of windows f
ecd80 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 or wince.*/.stat
ecd90 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 ic BOOL winceLoc
ecda0 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 kFile(. HANDLE
ecdb0 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 *phFile,. DWORD
ecdc0 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 dwFileOffsetLow
ecdd0 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 ,. DWORD dwFile
ecde0 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 OffsetHigh,. DW
ecdf0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
ece00 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 esToLockLow,. D
ece10 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 WORD nNumberOfBy
ece20 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b tesToLockHigh.){
ece30 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
ece40 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 e = HANDLE_TO_WI
ece50 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 NFILE(phFile);.
ece60 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 BOOL bReturn =
ece70 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 FALSE;.. if (!p
ece80 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 File->hMutex) re
ece90 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e turn TRUE;. win
ecea0 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 ceMutexAcquire(p
eceb0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a File->hMutex);..
ecec0 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 /* Wanting an
eced0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 exclusive lock?
ecee0 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f */. if (dwFileO
ecef0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52 ffsetLow == SHAR
ecf00 45 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 ED_FIRST.
ecf10 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 && nNumberOfByte
ecf20 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 sToLockLow == SH
ecf30 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 ARED_SIZE){.
ecf40 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 if (pFile->share
ecf50 64 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 d->nReaders == 0
ecf60 20 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 && pFile->share
ecf70 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d d->bExclusive ==
ecf80 20 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 0){. pFil
ecf90 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c e->shared->bExcl
ecfa0 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 usive = TRUE;.
ecfb0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
ecfc0 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 l.bExclusive = T
ecfd0 52 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74 RUE;. bRet
ecfe0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
ecff0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 }. }.. /* Want
ed000 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 a read-only loc
ed010 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 k? */. else if
ed020 28 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f ((dwFileOffsetLo
ed030 77 20 3e 3d 20 53 48 41 52 45 44 5f 46 49 52 53 w >= SHARED_FIRS
ed040 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 T &&.
ed050 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 dwFileOffsetLow
ed060 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 < SHARED_FIRST
ed070 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29 20 26 + SHARED_SIZE) &
ed080 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e &. nN
ed090 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f umberOfBytesToLo
ed0a0 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 ckLow == 1){.
ed0b0 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 if (pFile->shar
ed0c0 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d ed->bExclusive =
ed0d0 3d 20 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c = 0){. pFil
ed0e0 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 e->local.nReader
ed0f0 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 s ++;. if (
ed100 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 pFile->local.nRe
ed110 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 aders == 1){.
ed120 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
ed130 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b ed->nReaders ++;
ed140 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
ed150 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 Return = TRUE;.
ed160 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 }. }.. /* W
ed170 61 6e 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f ant a pending lo
ed180 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 ck? */. else if
ed190 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f (dwFileOffsetLo
ed1a0 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 w == PENDING_BYT
ed1b0 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 E && nNumberOfBy
ed1c0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 tesToLockLow ==
ed1d0 31 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 1){. /* If no
ed1e0 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 pending lock ha
ed1f0 73 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c s been acquired,
ed200 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74 then acquire it
ed210 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c */. if (pFil
ed220 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 e->shared->bPend
ed230 69 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 ing == 0) {.
ed240 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
ed250 3e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 >bPending = TRUE
ed260 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ;. pFile->l
ed270 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 ocal.bPending =
ed280 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 TRUE;. bRet
ed290 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
ed2a0 7d 0a 20 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 }. }. /* Want
ed2b0 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f a reserved lock?
ed2c0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
ed2d0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
ed2e0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 = RESERVED_BYTE
ed2f0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 && nNumberOfByte
ed300 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 sToLockLow == 1)
ed310 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d {. if (pFile-
ed320 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 >shared->bReserv
ed330 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 ed == 0) {.
ed340 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
ed350 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45 bReserved = TRUE
ed360 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ;. pFile->l
ed370 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d ocal.bReserved =
ed380 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 TRUE;. bRe
ed390 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
ed3a0 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d }. }.. winceM
ed3b0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
ed3c0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 e->hMutex);. re
ed3d0 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a turn bReturn;.}.
ed3e0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d ./*.** An implem
ed3f0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
ed400 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f UnlockFile API o
ed410 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 f windows for wi
ed420 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f nce.*/.static BO
ed430 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 OL winceUnlockFi
ed440 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 le(. HANDLE *ph
ed450 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 File,. DWORD dw
ed460 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 FileOffsetLow,.
ed470 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 DWORD dwFileOff
ed480 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 setHigh,. DWORD
ed490 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
ed4a0 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 oUnlockLow,. DW
ed4b0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
ed4c0 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 esToUnlockHigh.)
ed4d0 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 {. winFile *pFi
ed4e0 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 le = HANDLE_TO_W
ed4f0 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a INFILE(phFile);.
ed500 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d BOOL bReturn =
ed510 20 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 FALSE;.. if (!
ed520 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 pFile->hMutex) r
ed530 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 eturn TRUE;. wi
ed540 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 nceMutexAcquire(
ed550 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
ed560 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 . /* Releasing
ed570 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 a reader lock or
ed580 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
ed590 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 ck */. if (dwFi
ed5a0 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 leOffsetLow >= S
ed5b0 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 HARED_FIRST &&.
ed5c0 20 20 20 20 20 20 64 77 46 69 6c 65 4f 66 66 73 dwFileOffs
ed5d0 65 74 4c 6f 77 20 3c 20 53 48 41 52 45 44 5f 46 etLow < SHARED_F
ed5e0 49 52 53 54 20 2b 20 53 48 41 52 45 44 5f 53 49 IRST + SHARED_SI
ed5f0 5a 45 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 ZE){. /* Did
ed600 77 65 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 we have an exclu
ed610 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 sive lock? */.
ed620 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
ed630 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a al.bExclusive){.
ed640 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
ed650 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 al.bExclusive =
ed660 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 FALSE;. pFi
ed670 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 le->shared->bExc
ed680 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a lusive = FALSE;.
ed690 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
ed6a0 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 TRUE;. }..
ed6b0 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 74 20 /* Did we just
ed6c0 68 61 76 65 20 61 20 72 65 61 64 65 72 20 6c 6f have a reader lo
ed6d0 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 ck? */. else
ed6e0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
ed6f0 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 .nReaders){.
ed700 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e pFile->local.n
ed710 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 Readers --;.
ed720 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
ed730 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 al.nReaders == 0
ed740 29 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 ). {.
ed750 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
ed760 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 >nReaders --;.
ed770 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 }. bRet
ed780 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
ed790 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 }. }.. /* Rele
ed7a0 61 73 69 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 asing a pending
ed7b0 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 lock */. else i
ed7c0 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c f (dwFileOffsetL
ed7d0 6f 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 ow == PENDING_BY
ed7e0 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 TE && nNumberOfB
ed7f0 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 ytesToUnlockLow
ed800 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 == 1){. if (p
ed810 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e File->local.bPen
ed820 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69 ding){. pFi
ed830 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 le->local.bPendi
ed840 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ng = FALSE;.
ed850 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
ed860 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 >bPending = FALS
ed870 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
ed880 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
ed890 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e }. /* Releasin
ed8a0 67 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 g a reserved loc
ed8b0 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 k */. else if (
ed8c0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
ed8d0 3d 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 == RESERVED_BYTE
ed8e0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 && nNumberOfByt
ed8f0 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d esToUnlockLow ==
ed900 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 1){. if (pFi
ed910 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 le->local.bReser
ed920 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 69 ved) {. pFi
ed930 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 le->local.bReser
ed940 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ved = FALSE;.
ed950 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
ed960 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 ->bReserved = FA
ed970 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 LSE;. bRetu
ed980 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d rn = TRUE;. }
ed990 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 . }.. winceMut
ed9a0 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d exRelease(pFile-
ed9b0 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 >hMutex);. retu
ed9c0 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f rn bReturn;.}../
ed9d0 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e *.** An implemen
ed9e0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f tation of the Lo
ed9f0 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 6f ckFileEx() API o
eda00 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 f windows for wi
eda10 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f nce.*/.static BO
eda20 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 OL winceLockFile
eda30 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 Ex(. HANDLE *ph
eda40 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 File,. DWORD dw
eda50 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 64 Flags,. DWORD d
eda60 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 4f wReserved,. DWO
eda70 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 RD nNumberOfByte
eda80 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 sToLockLow,. DW
eda90 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
edaa0 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 esToLockHigh,.
edab0 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 4f LPOVERLAPPED lpO
edac0 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f verlapped.){. /
edad0 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 * If the caller
edae0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72 wants a shared r
edaf0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 ead lock, forwar
edb00 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a d this call. **
edb10 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c to winceLockFil
edb20 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 e */. if (lpOve
edb30 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 rlapped->Offset
edb40 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 == SHARED_FIRST
edb50 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73 &&. dwFlags
edb60 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e == 1 &&. n
edb70 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
edb80 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 ockLow == SHARED
edb90 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 _SIZE){. retu
edba0 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 rn winceLockFile
edbb0 28 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f (phFile, SHARED_
edbc0 46 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b FIRST, 0, 1, 0);
edbd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 . }. return FA
edbe0 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 LSE;.}./*.** End
edbf0 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 of the special
edc00 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a code for wince.*
edc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 ************/.#e
edc60 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
edc70 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a S_WINCE */../***
edc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
edcd0 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 e next group of
edce0 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 routines impleme
edcf0 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f nt the I/O metho
edd00 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 ds specified.**
edd10 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 by the sqlite3_i
edd20 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
edd30 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**************
edd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edd80 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 /../*.** Close a
edd90 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 file..**.** It
edda0 69 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 is reported that
eddb0 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 an attempt to c
eddc0 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 lose a handle mi
eddd0 67 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a ght sometimes.**
edde0 20 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 fail. This is
eddf0 61 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 a very unreasona
ede00 62 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 ble result, but
ede10 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 windows is notor
ede20 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e ious.** for bein
ede30 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 g unreasonable s
ede40 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 o I do not doubt
ede50 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 that it might h
ede60 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 appen. If.** th
ede70 65 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 e close fails, w
ede80 65 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 e pause for 100
ede90 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 milliseconds and
edea0 20 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a try again. As.
edeb0 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c ** many as MX_CL
edec0 4f 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 OSE_ATTEMPT atte
eded0 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 mpts to close th
edee0 65 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 e handle are mad
edef0 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 e before.** givi
edf00 6e 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e ng up and return
edf10 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f ing an error..*/
edf20 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 .#define MX_CLOS
edf30 45 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 E_ATTEMPT 3.stat
edf40 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 ic int winClose(
edf50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
edf60 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 ){. int rc, cnt
edf70 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 = 0;. winFile
edf80 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
edf90 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 e*)id;.. assert
edfa0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 ( id!=0 );. OST
edfb0 52 41 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c RACE2("CLOSE %d\
edfc0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 n", pFile->h);.
edfd0 20 64 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c do{. rc = Cl
edfe0 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d oseHandle(pFile-
edff0 3e 68 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 >h);. }while( r
ee000 63 3d 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20 c==0 && ++cnt <
ee010 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 MX_CLOSE_ATTEMPT
ee020 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c && (Sleep(100),
ee030 20 31 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 1) );.#if SQLIT
ee040 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69 E_OS_WINCE.#defi
ee050 6e 65 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f ne WINCE_DELETIO
ee060 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77 N_ATTEMPTS 3. w
ee070 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 inceDestroyLock(
ee080 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 pFile);. if( pF
ee090 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c ile->zDeleteOnCl
ee0a0 6f 73 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 ose ){. int c
ee0b0 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c nt = 0;. whil
ee0c0 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 44 65 e(. De
ee0d0 6c 65 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d leteFileW(pFile-
ee0e0 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 >zDeleteOnClose)
ee0f0 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 47 ==0. && G
ee100 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 etFileAttributes
ee110 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 W(pFile->zDelete
ee120 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66 OnClose)!=0xffff
ee130 66 66 66 66 20 0a 20 20 20 20 20 20 20 20 26 26 ffff . &&
ee140 20 63 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 cnt++ < WINCE_D
ee150 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 ELETION_ATTEMPTS
ee160 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53 . ){. S
ee170 6c 65 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57 leep(100); /* W
ee180 61 69 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66 ait a little bef
ee190 6f 72 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e ore trying again
ee1a0 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 */. }. fr
ee1b0 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 ee(pFile->zDelet
ee1c0 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 eOnClose);. }.#
ee1d0 65 6e 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e endif. OpenCoun
ee1e0 74 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 ter(-1);. retur
ee1f0 6e 20 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b n rc ? SQLITE_OK
ee200 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b : SQLITE_IOERR;
ee210 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d .}../*.** Some m
ee220 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 icrosoft compile
ee230 72 73 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 rs lack this def
ee240 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e inition..*/.#ifn
ee250 64 65 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f def INVALID_SET_
ee260 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 FILE_POINTER.# d
ee270 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 efine INVALID_SE
ee280 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 T_FILE_POINTER (
ee290 28 44 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 (DWORD)-1).#endi
ee2a0 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 f../*.** Read da
ee2b0 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 ta from a file i
ee2c0 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 nto a buffer. R
ee2d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
ee2e0 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 if all.** bytes
ee2f0 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 were read succes
ee300 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 sfully and SQLIT
ee310 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 E_IOERR if anyth
ee320 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e ing goes.** wron
ee330 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
ee340 20 77 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69 winRead(. sqli
ee350 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 te3_file *id,
ee360 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 /* File t
ee370 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 o read from */.
ee380 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 void *pBuf,
ee390 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
ee3a0 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 rite content int
ee3b0 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f o this buffer */
ee3c0 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 . int amt,
ee3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ee3e0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
ee3f0 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 to read */. sq
ee400 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
ee410 65 74 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 et /* Begi
ee420 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 n reading at thi
ee430 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 s offset */.){.
ee440 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 LONG upperBits
ee450 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 = (LONG)((offset
ee460 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 >>32) & 0x7fffff
ee470 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 ff);. LONG lowe
ee480 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f rBits = (LONG)(o
ee490 66 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66 ffset & 0xffffff
ee4a0 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b ff);. DWORD rc;
ee4b0 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
ee4c0 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 e = (winFile*)id
ee4d0 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b ;. DWORD error;
ee4e0 0a 20 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 . DWORD got;..
ee4f0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
ee500 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
ee510 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
ee520 45 5f 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 E_IOERR_READ);.
ee530 20 4f 53 54 52 41 43 45 33 28 22 52 45 41 44 20 OSTRACE3("READ
ee540 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 %d lock=%d\n", p
ee550 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e File->h, pFile->
ee560 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 locktype);. rc
ee570 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 = SetFilePointer
ee580 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 (pFile->h, lower
ee590 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 Bits, &upperBits
ee5a0 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 , FILE_BEGIN);.
ee5b0 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 if( rc==INVALID
ee5c0 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 _SET_FILE_POINTE
ee5d0 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c R && (error=GetL
ee5e0 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f astError())!=NO_
ee5f0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 ERROR ){. pFi
ee600 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
ee610 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 error;. retur
ee620 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 n SQLITE_FULL;.
ee630 20 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 }. if( !ReadFi
ee640 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 le(pFile->h, pBu
ee650 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 f, amt, &got, 0)
ee660 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
ee670 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 astErrno = GetLa
ee680 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 stError();. r
ee690 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
ee6a0 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 RR_READ;. }. i
ee6b0 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 f( got==(DWORD)a
ee6c0 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e mt ){. return
ee6d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
ee6e0 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 lse{. /* Unre
ee6f0 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 ad parts of the
ee700 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a buffer must be z
ee710 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 ero-filled */.
ee720 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 memset(&((char
ee730 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c *)pBuf)[got], 0,
ee740 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 amt-got);. r
ee750 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
ee760 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 RR_SHORT_READ;.
ee770 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 }.}../*.** Writ
ee780 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 e data from a bu
ee790 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 ffer into a file
ee7a0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
ee7b0 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a _OK on success.*
ee7c0 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 * or some other
ee7d0 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 error code on fa
ee7e0 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 ilure..*/.static
ee7f0 20 69 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 int winWrite(.
ee800 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
ee810 64 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 d, /* Fi
ee820 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f le to write into
ee830 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
ee840 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 *pBuf,
ee850 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 /* The bytes to
ee860 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 be written */.
ee870 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 int amt,
ee880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ee890 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
ee8a0 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 write */. sqlit
ee8b0 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 e3_int64 offset
ee8c0 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
ee8d0 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 nto the file to
ee8e0 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 begin writing at
ee8f0 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 */.){. LONG up
ee900 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 perBits = (LONG)
ee910 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 ((offset>>32) &
ee920 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 0x7fffffff);. L
ee930 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 ONG lowerBits =
ee940 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 (LONG)(offset &
ee950 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 0xffffffff);. D
ee960 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 WORD rc;. winFi
ee970 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
ee980 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 File*)id;. DWOR
ee990 44 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 D error;. DWORD
ee9a0 20 77 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61 wrote = 0;.. a
ee9b0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
ee9c0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
ee9d0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
ee9e0 49 4f 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 IOERR_WRITE);.
ee9f0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
eea00 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c Error(return SQL
eea10 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 ITE_FULL);. OST
eea20 52 41 43 45 33 28 22 57 52 49 54 45 20 25 64 20 RACE3("WRITE %d
eea30 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c lock=%d\n", pFil
eea40 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 e->h, pFile->loc
eea50 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 ktype);. rc = S
eea60 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 etFilePointer(pF
eea70 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 ile->h, lowerBit
eea80 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 s, &upperBits, F
eea90 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 ILE_BEGIN);. if
eeaa0 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 ( rc==INVALID_SE
eeab0 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 T_FILE_POINTER &
eeac0 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 & (error=GetLast
eead0 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 Error())!=NO_ERR
eeae0 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d OR ){. pFile-
eeaf0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
eeb00 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 or;. return S
eeb10 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a QLITE_FULL;. }.
eeb20 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 assert( amt>0
eeb30 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 );. while(.
eeb40 20 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 amt>0. && (
eeb50 72 63 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 rc = WriteFile(p
eeb60 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 File->h, pBuf, a
eeb70 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 mt, &wrote, 0))!
eeb80 3d 30 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 =0. && wrote
eeb90 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 >0. ){. amt
eeba0 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 -= wrote;. pB
eebb0 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 uf = &((char*)pB
eebc0 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a uf)[wrote];. }.
eebd0 20 20 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 if( !rc || amt
eebe0 3e 28 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 >(int)wrote ){.
eebf0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
eec00 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 rno = GetLastErr
eec10 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e or();. return
eec20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 SQLITE_FULL;.
eec30 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
eec40 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
eec50 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 runcate an open
eec60 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 file to a specif
eec70 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 ied size.*/.stat
eec80 69 63 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 ic int winTrunca
eec90 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
eeca0 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 *id, sqlite3_int
eecb0 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 64 nByte){. LON
eecc0 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c G upperBits = (L
eecd0 4f 4e 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 ONG)((nByte>>32)
eece0 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a & 0x7fffffff);.
eecf0 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 LONG lowerBits
eed00 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 = (LONG)(nByte
eed10 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 & 0xffffffff);.
eed20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e DWORD rc;. win
eed30 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 File *pFile = (w
eed40 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 inFile*)id;. DW
eed50 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 ORD error;.. as
eed60 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
eed70 20 4f 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 OSTRACE3("TRUNC
eed80 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 ATE %d %lld\n",
eed90 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 pFile->h, nByte)
eeda0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
eedb0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
eedc0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 E_IOERR_TRUNCATE
eedd0 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c );. rc = SetFil
eede0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e ePointer(pFile->
eedf0 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 h, lowerBits, &u
eee00 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 pperBits, FILE_B
eee10 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d EGIN);. if( rc=
eee20 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c =INVALID_SET_FIL
eee30 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 E_POINTER && (er
eee40 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 ror=GetLastError
eee50 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b ())!=NO_ERROR ){
eee60 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
eee70 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 Errno = error;.
eee80 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
eee90 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b _IOERR_TRUNCATE;
eeea0 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 . }. /* SetEnd
eeeb0 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c OfFile will fail
eeec0 20 69 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67 if nByte is neg
eeed0 61 74 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21 ative */. if( !
eeee0 53 65 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69 SetEndOfFile(pFi
eeef0 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46 le->h) ){. pF
eef00 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
eef10 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b GetLastError();
eef20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
eef30 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 TE_IOERR_TRUNCAT
eef40 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 E;. }. return
eef50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 SQLITE_OK;.}..#i
eef60 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
eef70 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 ./*.** Count the
eef80 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 number of fulls
eef90 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 yncs and normal
eefa0 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 syncs. This is
eefb0 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 used to test.**
eefc0 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 that syncs and f
eefd0 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 ullsyncs are occ
eefe0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 uring at the rig
eeff0 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c ht times..*/.SQL
ef000 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
ef010 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d te3_sync_count =
ef020 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 0;.SQLITE_API i
ef030 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 nt sqlite3_fulls
ef040 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 ync_count = 0;.#
ef050 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b endif../*.** Mak
ef060 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 e sure all write
ef070 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 s to a particula
ef080 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 r file are commi
ef090 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f tted to disk..*/
ef0a0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 .static int winS
ef0b0 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ync(sqlite3_file
ef0c0 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 *id, int flags)
ef0d0 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
ef0e0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69 _NO_SYNC. winFi
ef0f0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
ef100 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 File*)id;.. ass
ef110 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
ef120 4f 53 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 OSTRACE3("SYNC %
ef130 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 d lock=%d\n", pF
ef140 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c ile->h, pFile->l
ef150 6f 63 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a ocktype);.#else.
ef160 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
ef170 45 52 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 ER(id);.#endif.#
ef180 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 ifndef SQLITE_TE
ef190 53 54 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ST. UNUSED_PARA
ef1a0 4d 45 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65 METER(flags);.#e
ef1b0 6c 73 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20 lse. if( flags
ef1c0 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 & SQLITE_SYNC_FU
ef1d0 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 LL ){. sqlite
ef1e0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
ef1f0 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ++;. }. sqlite
ef200 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 3_sync_count++;.
ef210 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 #endif. /* If w
ef220 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 e compiled with
ef230 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 the SQLITE_NO_SY
ef240 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 NC flag, then sy
ef250 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 ncing is a. **
ef260 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 no-op. */.#ifde
ef270 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 f SQLITE_NO_SYNC
ef280 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
ef290 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 TE_OK;.#else. i
ef2a0 66 28 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 f( FlushFileBuff
ef2b0 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b ers(pFile->h) ){
ef2c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
ef2d0 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
ef2e0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
ef2f0 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 rrno = GetLastEr
ef300 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 ror();. retur
ef310 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a n SQLITE_IOERR;.
ef320 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a }.#endif.}../*
ef330 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 .** Determine th
ef340 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
ef350 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 f a file in byte
ef360 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 s.*/.static int
ef370 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 winFileSize(sqli
ef380 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 te3_file *id, sq
ef390 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 lite3_int64 *pSi
ef3a0 7a 65 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70 ze){. DWORD upp
ef3b0 65 72 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20 erBits;. DWORD
ef3c0 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e lowerBits;. win
ef3d0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 File *pFile = (w
ef3e0 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 inFile*)id;. DW
ef3f0 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 ORD error;.. as
ef400 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
ef410 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
ef420 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 (return SQLITE_I
ef430 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c OERR_FSTAT);. l
ef440 6f 77 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 owerBits = GetFi
ef450 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c leSize(pFile->h,
ef460 20 26 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 &upperBits);.
ef470 69 66 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 if( (lowerBits
ef480 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 == INVALID_FILE
ef490 5f 53 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28 _SIZE). && (
ef4a0 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 (error = GetLast
ef4b0 45 72 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45 Error()) != NO_E
ef4c0 52 52 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20 RROR) ). {.
ef4d0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
ef4e0 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 = error;. re
ef4f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
ef500 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a R_FSTAT;. }. *
ef510 70 53 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 pSize = (((sqlit
ef520 65 33 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 e3_int64)upperBi
ef530 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 ts)<<32) + lower
ef540 42 69 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 Bits;. return S
ef550 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
ef560 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c ** LOCKFILE_FAIL
ef570 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 _IMMEDIATELY is
ef580 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d undefined on som
ef590 65 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d e Windows system
ef5a0 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f s..*/.#ifndef LO
ef5b0 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 CKFILE_FAIL_IMME
ef5c0 44 49 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 DIATELY.# define
ef5d0 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 LOCKFILE_FAIL_I
ef5e0 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e MMEDIATELY 1.#en
ef5f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 dif../*.** Acqui
ef600 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b re a reader lock
ef610 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 ..** Different A
ef620 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 PI routines are
ef630 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 called depending
ef640 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e on whether or n
ef650 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 ot this.** is Wi
ef660 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f n95 or WinNT..*/
ef670 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 .static int getR
ef680 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 eadLock(winFile
ef690 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 *pFile){. int r
ef6a0 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 es;. if( isNT()
ef6b0 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 ){. OVERLAPP
ef6c0 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c ED ovlp;. ovl
ef6d0 70 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 p.Offset = SHARE
ef6e0 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c D_FIRST;. ovl
ef6f0 70 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 p.OffsetHigh = 0
ef700 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e ;. ovlp.hEven
ef710 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d t = 0;. res =
ef720 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c LockFileEx(pFil
ef730 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 e->h, LOCKFILE_F
ef740 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c AIL_IMMEDIATELY,
ef750 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ef760 20 20 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 0, SHARED_
ef770 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b SIZE, 0, &ovlp);
ef780 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
ef790 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ef7a0 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
ef7b0 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
ef7c0 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 cuted. .*/.#if S
ef7d0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
ef7e0 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 0. }else{. i
ef7f0 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 nt lk;. sqlit
ef800 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
ef810 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a zeof(lk), &lk);.
ef820 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
ef830 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f dLockByte = (sho
ef840 72 74 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 rt)((lk & 0x7fff
ef850 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 ffff)%(SHARED_SI
ef860 5a 45 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65 ZE - 1));. re
ef870 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 s = LockFile(pFi
ef880 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 le->h, SHARED_FI
ef890 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 RST+pFile->share
ef8a0 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c dLockByte, 0, 1,
ef8b0 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0);.#endif. }.
ef8c0 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 if( res == 0 )
ef8d0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
ef8e0 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
ef8f0 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 Error();. }. r
ef900 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a eturn res;.}../*
ef910 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c .** Undo a readl
ef920 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ock.*/.static in
ef930 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b t unlockReadLock
ef940 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 (winFile *pFile)
ef950 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 {. int res;. i
ef960 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
ef970 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c res = UnlockFil
ef980 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 e(pFile->h, SHAR
ef990 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 ED_FIRST, 0, SHA
ef9a0 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a RED_SIZE, 0);./*
ef9b0 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
ef9c0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
ef9d0 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
ef9e0 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
ef9f0 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 ed. .*/.#if SQLI
efa00 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 TE_OS_WINCE==0.
efa10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 }else{. res
efa20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 = UnlockFile(pFi
efa30 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 le->h, SHARED_FI
efa40 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 RST + pFile->sha
efa50 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 redLockByte, 0,
efa60 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 1, 0);.#endif.
efa70 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 }. if( res == 0
efa80 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
efa90 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 astErrno = GetLa
efaa0 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 stError();. }.
efab0 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a return res;.}..
efac0 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 /*.** Lock the f
efad0 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 ile with the loc
efae0 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 k specified by p
efaf0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 arameter locktyp
efb00 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 e - one.** of th
efb10 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
efb20 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 ** (1) SHARE
efb30 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 D_LOCK.** (2
efb40 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a ) RESERVED_LOCK.
efb50 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 ** (3) PENDI
efb60 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 NG_LOCK.** (
efb70 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4) EXCLUSIVE_LOC
efb80 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 K.**.** Sometime
efb90 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e s when requestin
efba0 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 g one lock state
efbb0 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 , additional loc
efbc0 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 k states.** are
efbd0 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 inserted in betw
efbe0 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e een. The lockin
efbf0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 g might fail on
efc00 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 one of the later
efc10 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
efc20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b leaving the lock
efc30 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 state different
efc40 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 from what it st
efc50 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 arted but.** sti
efc60 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 ll short of its
efc70 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f goal. The follo
efc80 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 wing chart shows
efc90 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 the allowed.**
efca0 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 transitions and
efcb0 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 the inserted int
efcc0 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 ermediate states
efcd0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 :.**.** UNLOC
efce0 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a KED -> SHARED.**
efcf0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 SHARED -> RE
efd00 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 SERVED.** SHA
efd10 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 RED -> (PENDING)
efd20 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
efd30 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 RESERVED ->
efd40 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
efd50 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e LUSIVE.** PEN
efd60 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 DING -> EXCLUSIV
efd70 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 E.**.** This rou
efd80 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 tine will only i
efd90 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 ncrease a lock.
efda0 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 The winUnlock()
efdb0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 routine.** eras
efdc0 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 es all locks at
efdd0 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 once and returns
efde0 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 us immediately
efdf0 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c to locking level
efe00 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 0..** It is not
efe10 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 possible to low
efe20 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
efe30 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 evel one step at
efe40 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a a time. You.**
efe50 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 must go straigh
efe60 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 t to locking lev
efe70 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 el 0..*/.static
efe80 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 int winLock(sqli
efe90 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
efea0 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 t locktype){. i
efeb0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
efec0 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 K; /* Return
efed0 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 code from subrou
efee0 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 tines */. int r
efef0 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 es = 1;
eff00 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 /* Result of a
eff10 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 windows lock ca
eff20 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c ll */. int newL
eff30 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f ocktype; /
eff40 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 * Set pFile->loc
eff50 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 ktype to this va
eff60 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 lue before exiti
eff70 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 ng */. int gotP
eff80 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f endingLock = 0;/
eff90 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 63 71 * True if we acq
effa0 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 uired a PENDING
effb0 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a lock this time *
effc0 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 /. winFile *pFi
effd0 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 le = (winFile*)i
effe0 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 d;. DWORD error
efff0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 = NO_ERROR;..
f0000 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
f0010 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 . OSTRACE5("LOC
f0020 4b 20 25 64 20 25 64 20 77 61 73 20 25 64 28 25 K %d %d was %d(%
f0030 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 d)\n",.
f0040 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
f0050 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ype, pFile->lock
f0060 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 type, pFile->sha
f0070 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 redLockByte);..
f0080 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
f0090 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f already a lock o
f00a0 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d f this type or m
f00b0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 ore restrictive
f00c0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 on the. ** OsFi
f00d0 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 le, do nothing.
f00e0 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e Don't use the en
f00f0 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 d_lock: exit pat
f0100 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 h, as. ** sqlit
f0110 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 e3OsEnterMutex()
f0120 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c hasn't been cal
f0130 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 led yet.. */.
f0140 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
f0150 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype>=locktype ){
f0160 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
f0170 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a TE_OK;. }.. /*
f0180 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c Make sure the l
f0190 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 ocking sequence
f01a0 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a is correct. */.
f01b0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
f01c0 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f >locktype!=NO_LO
f01d0 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d CK || locktype==
f01e0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
f01f0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
f0200 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 e!=PENDING_LOCK
f0210 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 );. assert( loc
f0220 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f ktype!=RESERVED_
f0230 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c LOCK || pFile->l
f0240 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
f0250 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f LOCK );.. /* Lo
f0260 63 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c ck the PENDING_L
f0270 4f 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e OCK byte if we n
f0280 65 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 eed to acquire a
f0290 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 PENDING lock or
f02a0 0a 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c . ** a SHARED l
f02b0 6f 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 ock. If we are
f02c0 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 acquiring a SHAR
f02d0 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 ED lock, the acq
f02e0 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a uisition of. **
f02f0 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 the PENDING_LOC
f0300 4b 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 K byte is tempor
f0310 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c ary.. */. newL
f0320 6f 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d ocktype = pFile-
f0330 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 >locktype;. if(
f0340 20 20 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 (pFile->lockt
f0350 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 ype==NO_LOCK).
f0360 20 20 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74 || ( (lockt
f0370 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
f0380 4f 43 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26 OCK). &&
f0390 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 (pFile->locktyp
f03a0 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e==RESERVED_LOCK
f03b0 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 )). ){. int
f03c0 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 cnt = 3;. whi
f03d0 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 le( cnt-->0 && (
f03e0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 res = LockFile(p
f03f0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 File->h, PENDING
f0400 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 _BYTE, 0, 1, 0))
f0410 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ==0 ){. /*
f0420 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 Try 3 times to g
f0430 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c et the pending l
f0440 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e ock. The pendin
f0450 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a g lock might be.
f0460 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 ** held by
f0470 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 another reader
f0480 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c process who will
f0490 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 release it mome
f04a0 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a ntarily.. *
f04b0 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 /. OSTRACE2
f04c0 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 ("could not get
f04d0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 a PENDING lock.
f04e0 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b cnt=%d\n", cnt);
f04f0 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b . Sleep(1);
f0500 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 . }. gotPe
f0510 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b ndingLock = res;
f0520 0a 20 20 20 20 69 66 28 20 21 72 65 73 20 29 7b . if( !res ){
f0530 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 . error = G
f0540 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
f0550 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
f0560 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64 20 cquire a shared
f0570 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
f0580 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
f0590 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a _LOCK && res ){.
f05a0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
f05b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f e->locktype==NO_
f05c0 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 LOCK );. res
f05d0 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 = getReadLock(pF
f05e0 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 ile);. if( re
f05f0 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f s ){. newLo
f0600 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f cktype = SHARED_
f0610 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LOCK;. }else{
f0620 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 . error = G
f0630 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
f0640 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
f0650 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45 cquire a RESERVE
f0660 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 D lock. */. if
f0670 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 ( locktype==RESE
f0680 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 RVED_LOCK && res
f0690 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
f06a0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
f06b0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
f06c0 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 res = LockFi
f06d0 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 le(pFile->h, RES
f06e0 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 ERVED_BYTE, 0, 1
f06f0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 , 0);. if( re
f0700 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f s ){. newLo
f0710 63 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 cktype = RESERVE
f0720 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 D_LOCK;. }els
f0730 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d e{. error =
f0740 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b GetLastError();
f0750 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
f0760 20 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 Acquire a PENDI
f0770 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 NG lock. */. i
f0780 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 f( locktype==EXC
f0790 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 LUSIVE_LOCK && r
f07a0 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 es ){. newLoc
f07b0 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f ktype = PENDING_
f07c0 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e LOCK;. gotPen
f07d0 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 dingLock = 0;.
f07e0 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
f07f0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
f0800 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 k. */. if( loc
f0810 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
f0820 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a _LOCK && res ){.
f0830 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
f0840 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 e->locktype>=SHA
f0850 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
f0860 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 res = unlockRead
f0870 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
f0880 20 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 OSTRACE2("unrea
f0890 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 dlock = %d\n", r
f08a0 65 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c es);. res = L
f08b0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 ockFile(pFile->h
f08c0 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 , SHARED_FIRST,
f08d0 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 0, SHARED_SIZE,
f08e0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 0);. if( res
f08f0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
f0900 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 type = EXCLUSIVE
f0910 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 _LOCK;. }else
f0920 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 {. error =
f0930 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
f0940 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 OSTRACE2("
f0950 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c error-code = %d\
f0960 6e 22 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20 n", error);.
f0970 20 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 getReadLock(pF
f0980 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ile);. }. }.
f0990 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 . /* If we are
f09a0 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e holding a PENDIN
f09b0 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 G lock that ough
f09c0 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 t to be released
f09d0 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 , then. ** rele
f09e0 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f ase it now.. */
f09f0 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e . if( gotPendin
f0a00 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 gLock && locktyp
f0a10 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
f0a20 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 {. UnlockFile
f0a30 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 (pFile->h, PENDI
f0a40 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 NG_BYTE, 0, 1, 0
f0a50 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 );. }.. /* Upd
f0a60 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 ate the state of
f0a70 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 the lock has he
f0a80 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 ld in the file d
f0a90 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 escriptor then.
f0aa0 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 ** return the a
f0ab0 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c ppropriate resul
f0ac0 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 t code.. */. i
f0ad0 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 f( res ){. rc
f0ae0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
f0af0 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 }else{. OSTRA
f0b00 43 45 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 CE4("LOCK FAILED
f0b10 20 25 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 %d trying for %
f0b20 64 20 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c d but got %d\n",
f0b30 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
f0b40 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 locktype,
f0b50 6e 65 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 newLocktype);.
f0b60 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
f0b70 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 no = error;.
f0b80 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
f0b90 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c ;. }. pFile->l
f0ba0 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65 ocktype = (u8)ne
f0bb0 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 wLocktype;. ret
f0bc0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
f0bd0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
f0be0 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 ecks if there is
f0bf0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
f0c00 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 held on the spe
f0c10 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 cified.** file b
f0c20 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 y this or any ot
f0c30 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 her process. If
f0c40 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 such a lock is h
f0c50 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e eld, return.** n
f0c60 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 on-zero, otherwi
f0c70 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 se zero..*/.stat
f0c80 69 63 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 ic int winCheckR
f0c90 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
f0ca0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
f0cb0 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 t *pResOut){. i
f0cc0 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 nt rc;. winFile
f0cd0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
f0ce0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 le*)id;.. asser
f0cf0 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66 t( id!=0 );. if
f0d00 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
f0d10 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e>=RESERVED_LOCK
f0d20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a ){. rc = 1;.
f0d30 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 OSTRACE3("TE
f0d40 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
f0d50 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 (local)\n", pFi
f0d60 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65 le->h, rc);. }e
f0d70 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f lse{. rc = Lo
f0d80 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
f0d90 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 RESERVED_BYTE,
f0da0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 0, 1, 0);. if
f0db0 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e ( rc ){. Un
f0dc0 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e lockFile(pFile->
f0dd0 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 h, RESERVED_BYTE
f0de0 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 , 0, 1, 0);.
f0df0 7d 0a 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a }. rc = !rc;.
f0e00 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 OSTRACE3("TE
f0e10 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
f0e20 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 (remote)\n", pF
f0e30 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d ile->h, rc);. }
f0e40 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 . *pResOut = rc
f0e50 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
f0e60 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c E_OK;.}../*.** L
f0e70 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
f0e80 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
f0e90 65 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 escriptor id to
f0ea0 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 locktype. lockt
f0eb0 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 ype.** must be e
f0ec0 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 ither NO_LOCK or
f0ed0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a SHARED_LOCK..**
f0ee0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 .** If the locki
f0ef0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 ng level of the
f0f00 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
f0f10 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 is already at or
f0f20 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 below.** the re
f0f30 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 quested locking
f0f40 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 level, this rout
f0f50 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
f0f60 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 **.** It is not
f0f70 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 possible for thi
f0f80 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 s routine to fai
f0f90 6c 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 l if the second
f0fa0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e argument.** is N
f0fb0 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 O_LOCK. If the
f0fc0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
f0fd0 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 is SHARED_LOCK t
f0fe0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
f0ff0 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e .** might return
f1000 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a SQLITE_IOERR;.*
f1010 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
f1020 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
f1030 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
f1040 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79 ktype){. int ty
f1050 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 pe;. winFile *p
f1060 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
f1070 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 )id;. int rc =
f1080 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 SQLITE_OK;. ass
f1090 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b ert( pFile!=0 );
f10a0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
f10b0 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
f10c0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 );. OSTRACE5("
f10d0 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 UNLOCK %d to %d
f10e0 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70 was %d(%d)\n", p
f10f0 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
f1100 65 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 e,. pFi
f1110 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 le->locktype, pF
f1120 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 ile->sharedLockB
f1130 79 74 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 yte);. type = p
f1140 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a File->locktype;.
f1150 20 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c if( type>=EXCL
f1160 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 USIVE_LOCK ){.
f1170 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 UnlockFile(pFi
f1180 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 le->h, SHARED_FI
f1190 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 RST, 0, SHARED_S
f11a0 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 IZE, 0);. if(
f11b0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
f11c0 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65 D_LOCK && !getRe
f11d0 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b adLock(pFile) ){
f11e0 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 . /* This s
f11f0 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 hould never happ
f1200 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 en. We should a
f1210 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f lways be able to
f1220 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 . ** reacqu
f1230 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 ire the read loc
f1240 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 k */. rc =
f1250 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
f1260 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 OCK;. }. }.
f1270 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 if( type>=RESER
f1280 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 VED_LOCK ){.
f1290 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 UnlockFile(pFile
f12a0 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 ->h, RESERVED_BY
f12b0 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 TE, 0, 1, 0);.
f12c0 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 }. if( locktype
f12d0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 ==NO_LOCK && typ
f12e0 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e>=SHARED_LOCK )
f12f0 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64 {. unlockRead
f1300 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d Lock(pFile);. }
f1310 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e . if( type>=PEN
f1320 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 DING_LOCK ){.
f1330 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
f1340 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 e->h, PENDING_BY
f1350 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 TE, 0, 1, 0);.
f1360 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 }. pFile->lockt
f1370 79 70 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 ype = (u8)lockty
f1380 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b pe;. return rc;
f1390 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f .}../*.** Contro
f13a0 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 l and query of t
f13b0 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e he open file han
f13c0 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 dle..*/.static i
f13d0 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f nt winFileContro
f13e0 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a l(sqlite3_file *
f13f0 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 id, int op, void
f1400 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 *pArg){. switc
f1410 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 h( op ){. cas
f1420 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c e SQLITE_FCNTL_L
f1430 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 OCKSTATE: {.
f1440 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 *(int*)pArg =
f1450 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e ((winFile*)id)->
f1460 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 locktype;.
f1470 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f1480 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
f1490 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 SQLITE_LAST_ERR
f14a0 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e NO: {. *(in
f14b0 74 2a 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28 t*)pArg = (int)(
f14c0 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c (winFile*)id)->l
f14d0 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 astErrno;.
f14e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f14f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
f1500 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
f1510 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 R;.}../*.** Retu
f1520 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 rn the sector si
f1530 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 ze in bytes of t
f1540 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c he underlying bl
f1550 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a ock device for.*
f1560 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 * the specified
f1570 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c file. This is al
f1580 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 most always 512
f1590 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 bytes, but may b
f15a0 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 e.** larger for
f15b0 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a some devices..**
f15c0 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 .** SQLite code
f15d0 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e assumes this fun
f15e0 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 ction cannot fai
f15f0 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d l. It also assum
f1600 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 es that.** if tw
f1610 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 o files are crea
f1620 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ted in the same
f1630 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 file-system dire
f1640 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 ctory (i.e..** a
f1650 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 database and it
f1660 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 s journal file)
f1670 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 that the sector
f1680 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 size will be the
f1690 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 .** same for bot
f16a0 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 h..*/.static int
f16b0 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 winSectorSize(s
f16c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
f16d0 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d {. assert( id!=
f16e0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 0 );. return (i
f16f0 6e 74 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 nt)(((winFile*)i
f1700 64 29 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b d)->sectorSize);
f1710 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
f1720 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 a vector of dev
f1730 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 ice characterist
f1740 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ics..*/.static i
f1750 6e 74 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 nt winDeviceChar
f1760 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 acteristics(sqli
f1770 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
f1780 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f1790 52 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 R(id);. return
f17a0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 0;.}../*.** This
f17b0 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 vector defines
f17c0 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 all the methods
f17d0 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 that can operate
f17e0 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 on an.** sqlite
f17f0 33 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 3_file for win32
f1800 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
f1810 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
f1820 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 hods winIoMethod
f1830 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 = {. 1,
f1840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1850 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
f1860 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 winClose,. wi
f1870 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 nRead,. winWrit
f1880 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 e,. winTruncate
f1890 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 ,. winSync,. w
f18a0 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 inFileSize,. wi
f18b0 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f nLock,. winUnlo
f18c0 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 ck,. winCheckRe
f18d0 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 servedLock,. wi
f18e0 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 nFileControl,.
f18f0 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 winSectorSize,.
f1900 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 winDeviceCharac
f1910 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a teristics.};../*
f1920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 **********.** He
f1970 72 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 re ends the I/O
f1980 6d 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 methods that for
f1990 6d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f m the sqlite3_io
f19a0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e _methods object.
f19b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 .**.** The next
f19c0 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d block of code im
f19d0 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 plements the VFS
f19e0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a methods..******
f19f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1a30 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 ******/../*.** C
f1a40 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 onvert a UTF-8 f
f1a50 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 ilename into wha
f1a60 74 65 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 tever form the u
f1a70 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 nderlying.** ope
f1a80 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 rating system wa
f1a90 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e nts filenames in
f1aa0 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 . Space to hold
f1ab0 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 the result.** i
f1ac0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
f1ad0 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 malloc and must
f1ae0 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 be freed by the
f1af0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 calling.** funct
f1b00 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
f1b10 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 oid *convertUtf8
f1b20 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 Filename(const c
f1b30 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
f1b40 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 . void *zConver
f1b50 74 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 ted = 0;. if( i
f1b60 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f sNT() ){. zCo
f1b70 6e 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f nverted = utf8To
f1b80 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d Unicode(zFilenam
f1b90 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 e);./* isNT() is
f1ba0 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
f1bb0 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
f1bc0 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
f1bd0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 executed. .*/.#i
f1be0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
f1bf0 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 E==0. }else{.
f1c00 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 zConverted = u
f1c10 74 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e tf8ToMbcs(zFilen
f1c20 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d ame);.#endif. }
f1c30 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c . /* caller wil
f1c40 6c 20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 l handle out of
f1c50 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 memory */. retu
f1c60 72 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d rn zConverted;.}
f1c70 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
f1c80 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
f1c90 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a name in zBuf. z
f1ca0 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 Buf must be big
f1cb0 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c enough to.** hol
f1cc0 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 d at pVfs->mxPat
f1cd0 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 hname characters
f1ce0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f1cf0 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 getTempname(int
f1d00 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
f1d10 29 7b 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 ){. static char
f1d20 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 zChars[] =.
f1d30 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f "abcdefghijklmno
f1d40 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 pqrstuvwxyz".
f1d50 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e "ABCDEFGHIJKLMN
f1d60 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 OPQRSTUVWXYZ".
f1d70 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a "0123456789";.
f1d80 20 20 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 size_t i, j;.
f1d90 20 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b char zTempPath[
f1da0 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 MAX_PATH+1];. i
f1db0 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f f( sqlite3_temp_
f1dc0 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 directory ){.
f1dd0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
f1de0 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a f(MAX_PATH-30, z
f1df0 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 TempPath, "%s",
f1e00 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
f1e10 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 ectory);. }else
f1e20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
f1e30 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b char *zMulti;
f1e40 0a 20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 . WCHAR zWide
f1e50 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a Path[MAX_PATH];.
f1e60 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 GetTempPathW
f1e70 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 (MAX_PATH-30, zW
f1e80 69 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d idePath);. zM
f1e90 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f ulti = unicodeTo
f1ea0 55 74 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b Utf8(zWidePath);
f1eb0 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 . if( zMulti
f1ec0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
f1ed0 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 _snprintf(MAX_PA
f1ee0 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 TH-30, zTempPath
f1ef0 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b , "%s", zMulti);
f1f00 0a 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c . free(zMul
f1f10 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ti);. }else{.
f1f20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
f1f30 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
f1f40 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
f1f50 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f1f60 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
f1f70 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
f1f80 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 cuted. .** Since
f1f90 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 the ASCII versi
f1fa0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 on of these Wind
f1fb0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 ows API do not e
f1fc0 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a xist for WINCE,.
f1fd0 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e ** it's importan
f1fe0 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e t to not referen
f1ff0 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 ce them for WINC
f2000 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 E builds..*/.#if
f2010 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f2020 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
f2030 20 63 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 char *zUtf8;.
f2040 20 20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 char zMbcsPath
f2050 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 [MAX_PATH];.
f2060 47 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 GetTempPathA(MAX
f2070 5f 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 _PATH-30, zMbcsP
f2080 61 74 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 ath);. zUtf8
f2090 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f = sqlite3_win32_
f20a0 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 mbcs_to_utf8(zMb
f20b0 63 73 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 csPath);. if(
f20c0 20 7a 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 zUtf8 ){.
f20d0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
f20e0 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 (MAX_PATH-30, zT
f20f0 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a empPath, "%s", z
f2100 55 74 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 Utf8);. fre
f2110 65 28 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 e(zUtf8);. }e
f2120 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
f2130 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
f2140 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
f2150 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 . for(i=sqlite3
f2160 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 Strlen30(zTempPa
f2170 74 68 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d th); i>0 && zTem
f2180 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 pPath[i-1]=='\\'
f2190 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 ; i--){}. zTemp
f21a0 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 Path[i] = 0;. s
f21b0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
f21c0 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 nBuf-30, zBuf,.
f21d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f21e0 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 "%s\\"SQLITE_T
f21f0 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c EMP_FILE_PREFIX,
f2200 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a zTempPath);. j
f2210 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
f2220 33 30 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 30(zBuf);. sqli
f2230 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 te3_randomness(2
f2240 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 0, &zBuf[j]);.
f2250 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 for(i=0; i<20; i
f2260 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 ++, j++){. zB
f2270 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 uf[j] = (char)zC
f2280 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 hars[ ((unsigned
f2290 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 char)zBuf[j])%(
f22a0 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 sizeof(zChars)-1
f22b0 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b ) ];. }. zBuf[
f22c0 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 j] = 0;. OSTRAC
f22d0 45 32 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d E2("TEMP FILENAM
f22e0 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b E: %s\n", zBuf);
f22f0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
f2300 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 _OK; .}../*.** T
f2310 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
f2320 6f 66 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d of getLastErrorM
f2330 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 sg.** is zero if
f2340 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
f2350 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 ge fits in the b
f2360 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 uffer, or non-ze
f2370 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 ro.** otherwise
f2380 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 (if the message
f2390 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a was truncated)..
f23a0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
f23b0 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e tLastErrorMsg(in
f23c0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
f23d0 75 66 29 7b 0a 20 20 44 57 4f 52 44 20 65 72 72 uf){. DWORD err
f23e0 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f or = GetLastErro
f23f0 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 r();..#if SQLITE
f2400 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73 71 6c 69 _OS_WINCE. sqli
f2410 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
f2420 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f f, zBuf, "OsErro
f2430 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 r 0x%x (%u)", er
f2440 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 23 65 6c ror, error);.#el
f2450 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 se. /* FormatMe
f2460 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 20 ssage returns 0
f2470 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 on failure. Oth
f2480 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72 erwise it. ** r
f2490 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
f24a0 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 74 r of TCHARs writ
f24b0 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75 ten to the outpu
f24c0 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 t. ** buffer, e
f24d0 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72 xcluding the ter
f24e0 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 minating null ch
f24f0 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 20 28 21 ar.. */. if (!
f2500 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46 FormatMessageA(F
f2510 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 ORMAT_MESSAGE_FR
f2520 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 20 20 20 OM_SYSTEM,.
f2530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2540 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 NULL,.
f2550 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 err
f2560 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 or,.
f2570 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 0,.
f2580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2590 20 20 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 zBuf,.
f25a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e n
f25b0 42 75 66 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 Buf-1,.
f25c0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 29 0))
f25d0 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 . {. sqlite3
f25e0 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 _snprintf(nBuf,
f25f0 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 zBuf, "OsError 0
f2600 78 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72 x%x (%u)", error
f2610 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d 0a 23 65 , error);. }.#e
f2620 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 30 ndif.. return 0
f2630 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
f2640 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
f2650 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28 0a 20 c int winOpen(.
f2660 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
f2670 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f fs, /* No
f2680 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 t used */. cons
f2690 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 t char *zName,
f26a0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
f26b0 20 74 68 65 20 66 69 6c 65 20 28 55 54 46 2d 38 the file (UTF-8
f26c0 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 ) */. sqlite3_f
f26d0 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 ile *id,
f26e0 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 /* Write the SQ
f26f0 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 Lite file handle
f2700 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 here */. int f
f2710 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
f2720 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 /* Open mod
f2730 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 e flags */. int
f2740 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 *pOutFlags
f2750 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 /* Status
f2760 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f return flags */
f2770 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a .){. HANDLE h;.
f2780 20 20 44 57 4f 52 44 20 64 77 44 65 73 69 72 65 DWORD dwDesire
f2790 64 41 63 63 65 73 73 3b 0a 20 20 44 57 4f 52 44 dAccess;. DWORD
f27a0 20 64 77 53 68 61 72 65 4d 6f 64 65 3b 0a 20 20 dwShareMode;.
f27b0 44 57 4f 52 44 20 64 77 43 72 65 61 74 69 6f 6e DWORD dwCreation
f27c0 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 20 44 Disposition;. D
f27d0 57 4f 52 44 20 64 77 46 6c 61 67 73 41 6e 64 41 WORD dwFlagsAndA
f27e0 74 74 72 69 62 75 74 65 73 20 3d 20 30 3b 0a 23 ttributes = 0;.#
f27f0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f2800 43 45 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 20 CE. int isTemp
f2810 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 77 69 = 0;.#endif. wi
f2820 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 nFile *pFile = (
f2830 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 76 winFile*)id;. v
f2840 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b oid *zConverted;
f2850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2860 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 /* Filename in
f2870 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 OS encoding */.
f2880 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 74 const char *zUt
f2890 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20 f8Name = zName;
f28a0 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 /* Filename i
f28b0 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 n UTF-8 encoding
f28c0 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e */. char zTmpn
f28d0 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b ame[MAX_PATH+1];
f28e0 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 /* Buffe
f28f0 72 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 r used to create
f2900 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d 65 20 2a temp filename *
f2910 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 /.. assert( id!
f2920 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 =0 );. UNUSED_P
f2930 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
f2940 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63 . /* If the sec
f2950 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
f2960 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
f2970 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 NULL, generate
f2980 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 a . ** temporar
f2990 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 y file name to u
f29a0 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 se . */. if( !
f29b0 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20 zUtf8Name ){.
f29c0 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d int rc = getTem
f29d0 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b 31 pname(MAX_PATH+1
f29e0 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 , zTmpname);.
f29f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
f2a00 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
f2a10 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
f2a20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 6d zUtf8Name = zTm
f2a30 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a pname;. }.. /*
f2a40 20 43 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c Convert the fil
f2a50 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73 ename to the sys
f2a60 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f tem encoding. */
f2a70 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 . zConverted =
f2a80 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e convertUtf8Filen
f2a90 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a ame(zUtf8Name);.
f2aa0 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 if( zConverted
f2ab0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f2ac0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
f2ad0 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 }.. if( flags
f2ae0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
f2af0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 EADWRITE ){.
f2b00 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20 dwDesiredAccess
f2b10 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c = GENERIC_READ |
f2b20 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 3b 0a GENERIC_WRITE;.
f2b30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 44 }else{. dwD
f2b40 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 47 esiredAccess = G
f2b50 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20 20 7d ENERIC_READ;. }
f2b60 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 . if( flags & S
f2b70 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
f2b80 45 20 29 7b 0a 20 20 20 20 64 77 43 72 65 61 74 E ){. dwCreat
f2b90 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d ionDisposition =
f2ba0 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 20 OPEN_ALWAYS;.
f2bb0 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 43 72 65 }else{. dwCre
f2bc0 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e ationDisposition
f2bd0 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47 = OPEN_EXISTING
f2be0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 ;. }. if( flag
f2bf0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
f2c00 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 64 MAIN_DB ){. d
f2c10 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c wShareMode = FIL
f2c20 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46 E_SHARE_READ | F
f2c30 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b ILE_SHARE_WRITE;
f2c40 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 . }else{. dw
f2c50 53 68 61 72 65 4d 6f 64 65 20 3d 20 30 3b 0a 20 ShareMode = 0;.
f2c60 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 }. if( flags &
f2c70 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
f2c80 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 ETEONCLOSE ){.#i
f2c90 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
f2ca0 45 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 E. dwFlagsAnd
f2cb0 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c Attributes = FIL
f2cc0 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 E_ATTRIBUTE_HIDD
f2cd0 45 4e 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d EN;. isTemp =
f2ce0 20 31 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77 1;.#else. dw
f2cf0 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 FlagsAndAttribut
f2d00 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 es = FILE_ATTRIB
f2d10 55 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20 UTE_TEMPORARY.
f2d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2d30 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 | F
f2d40 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 ILE_ATTRIBUTE_HI
f2d50 44 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20 DDEN.
f2d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2d70 20 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f | FILE_FLAG_
f2d80 44 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b DELETE_ON_CLOSE;
f2d90 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b .#endif. }else{
f2da0 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 . dwFlagsAndA
f2db0 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 ttributes = FILE
f2dc0 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 _ATTRIBUTE_NORMA
f2dd0 4c 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f L;. }. /* Repo
f2de0 72 74 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 rts from the int
f2df0 65 72 6e 65 74 20 61 72 65 20 74 68 61 74 20 70 ernet are that p
f2e00 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c erformance is al
f2e10 77 61 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72 ways. ** better
f2e20 20 69 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 if FILE_FLAG_RA
f2e30 4e 44 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75 NDOM_ACCESS is u
f2e40 73 65 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 sed. Ticket #26
f2e50 39 39 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 99. */.#if SQLIT
f2e60 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46 E_OS_WINCE. dwF
f2e70 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
f2e80 73 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 s |= FILE_FLAG_R
f2e90 41 4e 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65 ANDOM_ACCESS;.#e
f2ea0 6e 64 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28 ndif. if( isNT(
f2eb0 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 ) ){. h = Cre
f2ec0 61 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a ateFileW((WCHAR*
f2ed0 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 )zConverted,.
f2ee0 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 dwDesiredAcc
f2ef0 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 ess,. dwSh
f2f00 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 areMode,.
f2f10 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 NULL,. dwC
f2f20 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 reationDispositi
f2f30 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 on,. dwFla
f2f40 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c gsAndAttributes,
f2f50 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 . NULL.
f2f60 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 );./* isNT() is
f2f70 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
f2f80 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
f2f90 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
f2fa0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 executed. .** Si
f2fb0 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 nce the ASCII ve
f2fc0 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 rsion of these W
f2fd0 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f indows API do no
f2fe0 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 t exist for WINC
f2ff0 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 E,.** it's impor
f3000 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 tant to not refe
f3010 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 rence them for W
f3020 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a INCE builds..*/.
f3030 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
f3040 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a NCE==0. }else{.
f3050 20 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 h = CreateFi
f3060 6c 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 leA((char*)zConv
f3070 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 erted,. dw
f3080 44 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 DesiredAccess,.
f3090 20 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 dwShareMod
f30a0 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a e,. NULL,.
f30b0 20 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f dwCreatio
f30c0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 nDisposition,.
f30d0 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 dwFlagsAndA
f30e0 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 ttributes,.
f30f0 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 NULL. );.#e
f3100 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68 ndif. }. if( h
f3110 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 ==INVALID_HANDLE
f3120 5f 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72 _VALUE ){. fr
f3130 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
f3140 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 if( flags &
f3150 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
f3160 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 WRITE ){. r
f3170 65 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 eturn winOpen(pV
f3180 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a fs, zName, id, .
f3190 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66 ((f
f31a0 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e lags|SQLITE_OPEN
f31b0 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 _READONLY)&~SQLI
f31c0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
f31d0 45 29 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a E), pOutFlags);.
f31e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
f31f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
f3200 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 ANTOPEN;. }.
f3210 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 }. if( pOutFla
f3220 67 73 20 29 7b 0a 20 20 20 20 69 66 28 20 66 6c gs ){. if( fl
f3230 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
f3240 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 N_READWRITE ){.
f3250 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 *pOutFlags
f3260 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 = SQLITE_OPEN_RE
f3270 41 44 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c ADWRITE;. }el
f3280 73 65 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 se{. *pOutF
f3290 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 lags = SQLITE_OP
f32a0 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 EN_READONLY;.
f32b0 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 }. }. memset(
f32c0 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 pFile, 0, sizeof
f32d0 28 2a 70 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 (*pFile));. pFi
f32e0 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 le->pMethod = &w
f32f0 69 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 inIoMethod;. pF
f3300 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 46 ile->h = h;. pF
f3310 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
f3320 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 70 46 69 NO_ERROR;. pFi
f3330 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d le->sectorSize =
f3340 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 getSectorSize(p
f3350 56 66 73 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b Vfs, zUtf8Name);
f3360 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
f3370 49 4e 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 INCE. if( (flag
f3380 73 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e s & (SQLITE_OPEN
f3390 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
f33a0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 E_OPEN_MAIN_DB))
f33b0 20 3d 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 ==.
f33c0 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f (SQLITE_OPEN_
f33d0 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 READWRITE|SQLITE
f33e0 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 _OPEN_MAIN_DB).
f33f0 20 20 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 && !winceC
f3400 72 65 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c reateLock(zName,
f3410 20 70 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 pFile). ){.
f3420 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b CloseHandle(h);
f3430 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 . free(zConve
f3440 72 74 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 rted);. retur
f3450 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 n SQLITE_CANTOPE
f3460 4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 N;. }. if( isT
f3470 65 6d 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 emp ){. pFile
f3480 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 ->zDeleteOnClose
f3490 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 = zConverted;.
f34a0 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 }else.#endif.
f34b0 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 {. free(zConv
f34c0 65 72 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 erted);. }. Op
f34d0 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 enCounter(+1);.
f34e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f34f0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 K;.}../*.** Dele
f3500 74 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c te the named fil
f3510 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 e..**.** Note th
f3520 61 74 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 at windows does
f3530 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 not allow a file
f3540 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 to be deleted i
f3550 66 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 f some other.**
f3560 70 72 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f process has it o
f3570 70 65 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 pen. Sometimes
f3580 61 20 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 a virus scanner
f3590 6f 72 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 or indexing prog
f35a0 72 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e ram.** will open
f35b0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 a journal file
f35c0 73 68 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 shortly after it
f35d0 20 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f is created in o
f35e0 72 64 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 rder to do.** wh
f35f0 61 74 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 atever it does.
f3600 20 57 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 While this othe
f3610 72 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c r process is hol
f3620 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 ding the.** file
f3630 20 6f 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 open, we will b
f3640 65 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 e unable to dele
f3650 74 65 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 te it. To work
f3660 61 72 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 around this.** p
f3670 72 6f 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 roblem, we delay
f3680 20 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 100 millisecond
f3690 73 20 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c s and try to del
f36a0 65 74 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a ete again. Up.*
f36b0 2a 20 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e * to MX_DELETION
f36c0 5f 41 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 _ATTEMPTs deleti
f36d0 6f 6e 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 on attempts are
f36e0 72 75 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e run before givin
f36f0 67 0a 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 g.** up and retu
f3700 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a rning an error..
f3710 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 */.#define MX_DE
f3720 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 LETION_ATTEMPTS
f3730 35 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 5.static int win
f3740 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 Delete(. sqlite
f3750 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
f3760 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 /* Not use
f3770 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 d on win32 */.
f3780 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
f3790 65 6e 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e ename, /* N
f37a0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 ame of file to d
f37b0 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 elete */. int s
f37c0 79 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 yncDir
f37d0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
f37e0 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 ed on win32 */.)
f37f0 7b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b {. int cnt = 0;
f3800 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 . DWORD rc;. D
f3810 57 4f 52 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a WORD error = 0;.
f3820 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 void *zConvert
f3830 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 ed = convertUtf8
f3840 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 Filename(zFilena
f3850 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 me);. UNUSED_PA
f3860 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 RAMETER(pVfs);.
f3870 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f3880 52 28 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 R(syncDir);. if
f3890 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 ( zConverted==0
f38a0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
f38b0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
f38c0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
f38d0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
f38e0 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 IOERR_DELETE);.
f38f0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
f3900 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c do{. Del
f3910 65 74 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 eteFileW(zConver
f3920 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 ted);. }while
f3930 28 20 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 ( ( ((rc = G
f3940 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 etFileAttributes
f3950 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 W(zConverted)) !
f3960 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 = INVALID_FILE_A
f3970 54 54 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 TTRIBUTES).
f3980 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 || ((e
f3990 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 rror = GetLastEr
f39a0 72 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f ror()) == ERROR_
f39b0 41 43 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a ACCESS_DENIED)).
f39c0 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2b && (+
f39d0 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 +cnt < MX_DELETI
f39e0 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 ON_ATTEMPTS).
f39f0 20 20 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 && (Slee
f3a00 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a p(100), 1) );./*
f3a10 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
f3a20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
f3a30 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
f3a40 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
f3a50 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 ed. .** Since th
f3a60 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 e ASCII version
f3a70 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 of these Windows
f3a80 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 API do not exis
f3a90 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 t for WINCE,.**
f3aa0 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 it's important t
f3ab0 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 o not reference
f3ac0 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 them for WINCE b
f3ad0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 uilds..*/.#if SQ
f3ae0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 LITE_OS_WINCE==0
f3af0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f . }else{. do
f3b00 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 {. DeleteFi
f3b10 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b leA(zConverted);
f3b20 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 . }while( (
f3b30 20 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c ((rc = GetFil
f3b40 65 41 74 74 72 69 62 75 74 65 73 41 28 7a 43 6f eAttributesA(zCo
f3b50 6e 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 nverted)) != INV
f3b60 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
f3b70 55 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 UTES).
f3b80 20 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 || ((error
f3b90 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
f3ba0 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 ) == ERROR_ACCES
f3bb0 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 S_DENIED)).
f3bc0 20 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 && (++cnt
f3bd0 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 < MX_DELETION_AT
f3be0 54 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 TEMPTS).
f3bf0 20 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 && (Sleep(100
f3c00 29 2c 20 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a ), 1) );.#endif.
f3c10 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 }. free(zConv
f3c20 65 72 74 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 erted);. OSTRAC
f3c30 45 32 28 22 44 45 4c 45 54 45 20 5c 22 25 73 5c E2("DELETE \"%s\
f3c40 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 "\n", zFilename)
f3c50 3b 0a 20 20 72 65 74 75 72 6e 20 28 20 20 20 28 ;. return ( (
f3c60 72 63 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 rc == INVALID_FI
f3c70 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 20 0a LE_ATTRIBUTES) .
f3c80 20 20 20 20 20 20 20 20 20 20 26 26 20 28 65 72 && (er
f3c90 72 6f 72 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c ror == ERROR_FIL
f3ca0 45 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 E_NOT_FOUND)) ?
f3cb0 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 SQLITE_OK : SQLI
f3cc0 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b TE_IOERR_DELETE;
f3cd0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 .}../*.** Check
f3ce0 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e the existance an
f3cf0 64 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 d status of a fi
f3d00 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
f3d10 74 20 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 t winAccess(. s
f3d20 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
f3d30 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 , /* Not
f3d40 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a used on win32 *
f3d50 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
f3d60 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f zFilename, /
f3d70 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 * Name of file t
f3d80 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 o check */. int
f3d90 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
f3da0 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 /* Type
f3db0 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 of test to make
f3dc0 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a on this file */.
f3dd0 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 int *pResOut
f3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f3df0 4f 55 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 OUT: Result */.)
f3e00 7b 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a {. DWORD attr;.
f3e10 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 int rc = 0;.
f3e20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 void *zConverted
f3e30 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 = convertUtf8Fi
f3e40 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 lename(zFilename
f3e50 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
f3e60 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 METER(pVfs);. i
f3e70 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 f( zConverted==0
f3e80 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
f3e90 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
f3ea0 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b . if( isNT() ){
f3eb0 0a 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 . attr = GetF
f3ec0 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 28 ileAttributesW((
f3ed0 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 WCHAR*)zConverte
f3ee0 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 d);./* isNT() is
f3ef0 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
f3f00 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
f3f10 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
f3f20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 executed. .** Si
f3f30 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 nce the ASCII ve
f3f40 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 rsion of these W
f3f50 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f indows API do no
f3f60 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 t exist for WINC
f3f70 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 E,.** it's impor
f3f80 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 tant to not refe
f3f90 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 rence them for W
f3fa0 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a INCE builds..*/.
f3fb0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
f3fc0 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a NCE==0. }else{.
f3fd0 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 attr = GetFi
f3fe0 6c 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63 leAttributesA((c
f3ff0 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 har*)zConverted)
f4000 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 ;.#endif. }. f
f4010 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
f4020 0a 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 . switch( flags
f4030 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
f4040 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a ITE_ACCESS_READ:
f4050 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
f4060 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a _ACCESS_EXISTS:.
f4070 20 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 rc = attr!
f4080 3d 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 =INVALID_FILE_AT
f4090 54 52 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 TRIBUTES;.
f40a0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
f40b0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 SQLITE_ACCESS_RE
f40c0 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 ADWRITE:. r
f40d0 63 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 c = (attr & FILE
f40e0 5f 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f _ATTRIBUTE_READO
f40f0 4e 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 NLY)==0;. b
f4100 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c reak;. defaul
f4110 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 t:. assert(
f4120 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 !"Invalid flags
f4130 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a argument");. }.
f4140 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b *pResOut = rc;
f4150 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
f4160 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 _OK;.}.../*.** T
f4170 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 urn a relative p
f4180 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 athname into a f
f4190 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 ull pathname. W
f41a0 72 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a rite the full.**
f41b0 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a pathname into z
f41c0 4f 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 Out[]. zOut[] w
f41d0 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 ill be at least
f41e0 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
f41f0 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a .** bytes in siz
f4200 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
f4210 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 winFullPathname
f4220 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
f4230 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 *pVfs,
f4240 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
f4250 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 vfs object */.
f4260 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c const char *zRel
f4270 61 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a ative, /*
f4280 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 Possibly relati
f4290 76 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f ve input path */
f42a0 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 . int nFull,
f42b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f42c0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 /* Size of outp
f42d0 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 ut buffer in byt
f42e0 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 es */. char *zF
f42f0 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 ull
f4300 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 /* Output
f4310 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a buffer */.){. .
f4320 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 #if defined(__CY
f4330 47 57 49 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 GWIN__). UNUSED
f4340 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c _PARAMETER(nFull
f4350 29 3b 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 );. cygwin_conv
f4360 5f 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 _to_full_win32_p
f4370 61 74 68 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a ath(zRelative, z
f4380 46 75 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 Full);. return
f4390 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 SQLITE_OK;.#endi
f43a0 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 f..#if SQLITE_OS
f43b0 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f _WINCE. UNUSED_
f43c0 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 PARAMETER(nFull)
f43d0 3b 0a 20 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 ;. /* WinCE has
f43e0 20 6e 6f 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 no concept of a
f43f0 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 relative pathna
f4400 6d 65 2c 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 me, or so I am t
f4410 6f 6c 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 old. */. sqlite
f4420 33 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3_snprintf(pVfs-
f4430 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 >mxPathname, zFu
f4440 6c 6c 2c 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 ll, "%s", zRelat
f4450 69 76 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ive);. return S
f4460 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 QLITE_OK;.#endif
f4470 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 ..#if !SQLITE_OS
f4480 5f 57 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e _WINCE && !defin
f4490 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 ed(__CYGWIN__).
f44a0 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f int nByte;. vo
f44b0 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a id *zConverted;.
f44c0 20 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 char *zOut;.
f44d0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f44e0 28 6e 46 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 (nFull);. zConv
f44f0 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 erted = convertU
f4500 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c tf8Filename(zRel
f4510 61 74 69 76 65 29 3b 0a 20 20 69 66 28 20 69 73 ative);. if( is
f4520 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 43 48 41 NT() ){. WCHA
f4530 52 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 R *zTemp;. nB
f4540 79 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 yte = GetFullPat
f4550 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a hNameW((WCHAR*)z
f4560 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c Converted, 0, 0,
f4570 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 0) + 3;. zTe
f4580 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 mp = malloc( nBy
f4590 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b te*sizeof(zTemp[
f45a0 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 0]) );. if( z
f45b0 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Temp==0 ){.
f45c0 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 free(zConverted
f45d0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
f45e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
f45f0 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 }. GetFullP
f4600 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a athNameW((WCHAR*
f4610 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 )zConverted, nBy
f4620 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 te, zTemp, 0);.
f4630 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 free(zConvert
f4640 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 ed);. zOut =
f4650 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 unicodeToUtf8(zT
f4660 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a emp);. free(z
f4670 54 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 Temp);./* isNT()
f4680 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
f4690 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
f46a0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
f46b0 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a er executed. .**
f46c0 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 Since the ASCII
f46d0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 version of thes
f46e0 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f e Windows API do
f46f0 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 not exist for W
f4700 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d INCE,.** it's im
f4710 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 portant to not r
f4720 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f eference them fo
f4730 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a r WINCE builds..
f4740 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f4750 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 _WINCE==0. }els
f4760 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 e{. char *zTe
f4770 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 mp;. nByte =
f4780 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 GetFullPathNameA
f4790 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 ((char*)zConvert
f47a0 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 ed, 0, 0, 0) + 3
f47b0 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 ;. zTemp = ma
f47c0 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 lloc( nByte*size
f47d0 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a of(zTemp[0]) );.
f47e0 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 if( zTemp==0
f47f0 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a ){. free(z
f4800 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
f4810 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f4820 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 NOMEM;. }.
f4830 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 GetFullPathName
f4840 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 A((char*)zConver
f4850 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d ted, nByte, zTem
f4860 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 p, 0);. free(
f4870 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
f4880 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f zOut = sqlite3_
f4890 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 win32_mbcs_to_ut
f48a0 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 f8(zTemp);. f
f48b0 72 65 65 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 ree(zTemp);.#end
f48c0 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 if. }. if( zOu
f48d0 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
f48e0 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e _snprintf(pVfs->
f48f0 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c mxPathname, zFul
f4900 6c 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a l, "%s", zOut);.
f4910 20 20 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a free(zOut);.
f4920 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
f4930 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
f4940 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f4950 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 _NOMEM;. }.#end
f4960 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 if.}../*.** Get
f4970 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
f4980 6f 66 20 74 68 65 20 64 65 76 69 63 65 20 75 73 of the device us
f4990 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 ed to store.** f
f49a0 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
f49b0 6e 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 nt getSectorSize
f49c0 28 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 (. sqlite3_vf
f49d0 73 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e s *pVfs,. con
f49e0 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 st char *zRelati
f49f0 76 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 ve /* UTF-8
f4a00 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a file name */.){.
f4a10 20 20 44 57 4f 52 44 20 62 79 74 65 73 50 65 72 DWORD bytesPer
f4a20 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f Sector = SQLITE_
f4a30 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 DEFAULT_SECTOR_S
f4a40 49 5a 45 3b 0a 20 20 63 68 61 72 20 7a 46 75 6c IZE;. char zFul
f4a50 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 lpath[MAX_PATH+1
f4a60 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 ];. int rc;. D
f4a70 57 4f 52 44 20 64 77 52 65 74 20 3d 20 30 3b 0a WORD dwRet = 0;.
f4a80 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 6e 65 . /*. ** We ne
f4a90 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 66 75 ed to get the fu
f4aa0 6c 6c 20 70 61 74 68 20 6e 61 6d 65 20 6f 66 20 ll path name of
f4ab0 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 6f the file. ** to
f4ac0 20 67 65 74 20 74 68 65 20 64 72 69 76 65 20 6c get the drive l
f4ad0 65 74 74 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 etter to look up
f4ae0 20 74 68 65 20 73 65 63 74 6f 72 0a 20 20 2a 2a the sector. **
f4af0 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 size.. */. rc
f4b00 20 3d 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 = winFullPathna
f4b10 6d 65 28 70 56 66 73 2c 20 7a 52 65 6c 61 74 69 me(pVfs, zRelati
f4b20 76 65 2c 20 4d 41 58 5f 50 41 54 48 2c 20 7a 46 ve, MAX_PATH, zF
f4b30 75 6c 6c 70 61 74 68 29 3b 0a 20 20 69 66 28 20 ullpath);. if(
f4b40 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 rc == SQLITE_OK
f4b50 29 0a 20 20 7b 0a 20 20 20 20 76 6f 69 64 20 2a ). {. void *
f4b60 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e zConverted = con
f4b70 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 vertUtf8Filename
f4b80 28 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 20 (zFullpath);.
f4b90 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 20 if( zConverted
f4ba0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 4e ){. if( isN
f4bb0 54 28 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 T() ){. i
f4bc0 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 2f 2a nt i;. /*
f4bd0 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a 75 trim path to ju
f4be0 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 6e st drive referen
f4bf0 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 57 43 ce */. WC
f4c00 48 41 52 20 2a 70 20 3d 20 7a 43 6f 6e 76 65 72 HAR *p = zConver
f4c10 74 65 64 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 ted;. for
f4c20 28 69 3d 30 3b 69 3c 4d 41 58 5f 50 41 54 48 3b (i=0;i<MAX_PATH;
f4c30 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 i++){.
f4c40 69 66 28 20 70 5b 69 5d 20 3d 3d 20 27 5c 5c 27 if( p[i] == '\\'
f4c50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
f4c60 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 i++;.
f4c70 20 70 5b 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 p[i] = '\0';.
f4c80 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
f4c90 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
f4ca0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 }. d
f4cb0 77 52 65 74 20 3d 20 47 65 74 44 69 73 6b 46 72 wRet = GetDiskFr
f4cc0 65 65 53 70 61 63 65 57 28 28 57 43 48 41 52 2a eeSpaceW((WCHAR*
f4cd0 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 )zConverted,.
f4ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e N
f4d00 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ULL,.
f4d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4d20 20 20 20 20 20 20 20 26 62 79 74 65 73 50 65 72 &bytesPer
f4d30 53 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 Sector,.
f4d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4d50 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a NULL,.
f4d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4d80 20 20 4e 55 4c 4c 29 3b 0a 23 69 66 20 53 51 4c NULL);.#if SQL
f4d90 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
f4da0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
f4db0 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
f4dc0 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 /* trim path
f4dd0 20 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 to just drive r
f4de0 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 eference */.
f4df0 20 20 20 20 43 48 41 52 20 2a 70 20 3d 20 28 43 CHAR *p = (C
f4e00 48 41 52 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 HAR *)zConverted
f4e10 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d ;. for(i=
f4e20 30 3b 69 3c 4d 41 58 5f 50 41 54 48 3b 69 2b 2b 0;i<MAX_PATH;i++
f4e30 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
f4e40 20 70 5b 69 5d 20 3d 3d 20 27 5c 5c 27 20 29 7b p[i] == '\\' ){
f4e50 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b . i++
f4e60 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 5b ;. p[
f4e70 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 i] = '\0';.
f4e80 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
f4e90 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
f4ea0 20 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65 }. dwRe
f4eb0 74 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53 t = GetDiskFreeS
f4ec0 70 61 63 65 41 28 28 43 48 41 52 2a 29 7a 43 6f paceA((CHAR*)zCo
f4ed0 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 nverted,.
f4ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4ef0 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c NULL,
f4f00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f4f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4f20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 63 74 &bytesPerSect
f4f30 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 or,.
f4f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4f50 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 NULL,.
f4f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 NU
f4f80 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 LL);.#endif.
f4f90 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65 28 7a }. free(z
f4fa0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
f4fb0 7d 0a 20 20 20 20 69 66 28 20 21 64 77 52 65 74 }. if( !dwRet
f4fc0 20 29 7b 0a 20 20 20 20 20 20 62 79 74 65 73 50 ){. bytesP
f4fd0 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 erSector = SQLIT
f4fe0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
f4ff0 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d _SIZE;. }. }
f5000 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 20 . return (int)
f5010 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 3b 20 bytesPerSector;
f5020 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
f5030 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
f5040 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 ENSION./*.** Int
f5050 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e erfaces for open
f5060 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 ing a shared lib
f5070 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e rary, finding en
f5080 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 try points.** wi
f5090 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 thin the shared
f50a0 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f library, and clo
f50b0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 sing the shared
f50c0 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a library..*/./*.*
f50d0 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 * Interfaces for
f50e0 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 opening a share
f50f0 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 d library, findi
f5100 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a ng entry points.
f5110 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 ** within the sh
f5120 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e ared library, an
f5130 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 d closing the sh
f5140 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f ared library..*/
f5150 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69 .static void *wi
f5160 6e 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f nDlOpen(sqlite3_
f5170 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 vfs *pVfs, const
f5180 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f5190 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 ){. HANDLE h;.
f51a0 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 void *zConverte
f51b0 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 d = convertUtf8F
f51c0 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d ilename(zFilenam
f51d0 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 e);. UNUSED_PAR
f51e0 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
f51f0 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d if( zConverted==
f5200 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
f5210 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 0;. }. if( isN
f5220 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 4c T() ){. h = L
f5230 6f 61 64 4c 69 62 72 61 72 79 57 28 28 57 43 48 oadLibraryW((WCH
f5240 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b AR*)zConverted);
f5250 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
f5260 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f5270 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
f5280 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
f5290 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 cuted. .** Since
f52a0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 the ASCII versi
f52b0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 on of these Wind
f52c0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 ows API do not e
f52d0 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a xist for WINCE,.
f52e0 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e ** it's importan
f52f0 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e t to not referen
f5300 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 ce them for WINC
f5310 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 E builds..*/.#if
f5320 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f5330 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
f5340 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 h = LoadLibrary
f5350 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 A((char*)zConver
f5360 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d ted);.#endif. }
f5370 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 . free(zConvert
f5380 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 ed);. return (v
f5390 6f 69 64 2a 29 68 3b 0a 7d 0a 73 74 61 74 69 63 oid*)h;.}.static
f53a0 20 76 6f 69 64 20 77 69 6e 44 6c 45 72 72 6f 72 void winDlError
f53b0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
f53c0 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 fs, int nBuf, ch
f53d0 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 ar *zBufOut){.
f53e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f53f0 28 70 56 66 73 29 3b 0a 20 20 67 65 74 4c 61 73 (pVfs);. getLas
f5400 74 45 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 tErrorMsg(nBuf,
f5410 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 zBufOut);.}.void
f5420 20 28 2a 77 69 6e 44 6c 53 79 6d 28 73 71 6c 69 (*winDlSym(sqli
f5430 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 te3_vfs *pVfs, v
f5440 6f 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f oid *pHandle, co
f5450 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f nst char *zSymbo
f5460 6c 29 29 28 76 6f 69 64 29 7b 0a 20 20 55 4e 55 l))(void){. UNU
f5470 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 SED_PARAMETER(pV
f5480 66 73 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f fs);.#if SQLITE_
f5490 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 54 68 OS_WINCE. /* Th
f54a0 65 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 e GetProcAddress
f54b0 41 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 6f A() routine is o
f54c0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e nly available on
f54d0 20 77 69 6e 63 65 2e 20 2a 2f 0a 20 20 72 65 74 wince. */. ret
f54e0 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 urn (void(*)(voi
f54f0 64 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 d))GetProcAddres
f5500 73 41 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 sA((HANDLE)pHand
f5510 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 le, zSymbol);.#e
f5520 6c 73 65 0a 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 lse. /* All oth
f5530 65 72 20 77 69 6e 64 6f 77 73 20 70 6c 61 74 66 er windows platf
f5540 6f 72 6d 73 20 65 78 70 65 63 74 20 47 65 74 50 orms expect GetP
f5550 72 6f 63 41 64 64 72 65 73 73 28 29 20 74 6f 20 rocAddress() to
f5560 74 61 6b 65 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 take. ** an Ans
f5570 69 20 73 74 72 69 6e 67 20 72 65 67 61 72 64 6c i string regardl
f5580 65 73 73 20 6f 66 20 74 68 65 20 5f 55 4e 49 43 ess of the _UNIC
f5590 4f 44 45 20 73 65 74 74 69 6e 67 20 2a 2f 0a 20 ODE setting */.
f55a0 20 72 65 74 75 72 6e 20 28 76 6f 69 64 28 2a 29 return (void(*)
f55b0 28 76 6f 69 64 29 29 47 65 74 50 72 6f 63 41 64 (void))GetProcAd
f55c0 64 72 65 73 73 28 28 48 41 4e 44 4c 45 29 70 48 dress((HANDLE)pH
f55d0 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b andle, zSymbol);
f55e0 0a 23 65 6e 64 69 66 0a 7d 0a 76 6f 69 64 20 77 .#endif.}.void w
f55f0 69 6e 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 inDlClose(sqlite
f5600 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
f5610 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 d *pHandle){. U
f5620 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
f5630 70 56 66 73 29 3b 0a 20 20 46 72 65 65 4c 69 62 pVfs);. FreeLib
f5640 72 61 72 79 28 28 48 41 4e 44 4c 45 29 70 48 61 rary((HANDLE)pHa
f5650 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f ndle);.}.#else /
f5660 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 * if SQLITE_OMIT
f5670 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 _LOAD_EXTENSION
f5680 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 is defined: */.
f5690 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70 #define winDlOp
f56a0 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 en 0. #define
f56b0 77 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 winDlError 0. #
f56c0 64 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 6d 20 define winDlSym
f56d0 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 0. #define wi
f56e0 6e 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 nDlClose 0.#endi
f56f0 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 f.../*.** Write
f5700 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 up to nBuf bytes
f5710 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 of randomness i
f5720 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 nto zBuf..*/.sta
f5730 74 69 63 20 69 6e 74 20 77 69 6e 52 61 6e 64 6f tic int winRando
f5740 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 mness(sqlite3_vf
f5750 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 s *pVfs, int nBu
f5760 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
f5770 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 55 int n = 0;. U
f5780 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
f5790 70 56 66 73 29 3b 0a 23 69 66 20 64 65 66 69 6e pVfs);.#if defin
f57a0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a ed(SQLITE_TEST).
f57b0 20 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 n = nBuf;. me
f57c0 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 mset(zBuf, 0, nB
f57d0 75 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 uf);.#else. if(
f57e0 20 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54 49 sizeof(SYSTEMTI
f57f0 4d 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 ME)<=nBuf-n ){.
f5800 20 20 20 53 59 53 54 45 4d 54 49 4d 45 20 78 3b SYSTEMTIME x;
f5810 0a 20 20 20 20 47 65 74 53 79 73 74 65 6d 54 69 . GetSystemTi
f5820 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 me(&x);. memc
f5830 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c py(&zBuf[n], &x,
f5840 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 sizeof(x));.
f5850 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b n += sizeof(x);
f5860 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f . }. if( sizeo
f5870 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e f(DWORD)<=nBuf-n
f5880 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 70 69 ){. DWORD pi
f5890 64 20 3d 20 47 65 74 43 75 72 72 65 6e 74 50 72 d = GetCurrentPr
f58a0 6f 63 65 73 73 49 64 28 29 3b 0a 20 20 20 20 6d ocessId();. m
f58b0 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 emcpy(&zBuf[n],
f58c0 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 &pid, sizeof(pid
f58d0 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a ));. n += siz
f58e0 65 6f 66 28 70 69 64 29 3b 0a 20 20 7d 0a 20 20 eof(pid);. }.
f58f0 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 if( sizeof(DWORD
f5900 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 )<=nBuf-n ){.
f5910 20 44 57 4f 52 44 20 63 6e 74 20 3d 20 47 65 74 DWORD cnt = Get
f5920 54 69 63 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20 TickCount();.
f5930 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
f5940 2c 20 26 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63 , &cnt, sizeof(c
f5950 6e 74 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 nt));. n += s
f5960 69 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a izeof(cnt);. }.
f5970 20 20 69 66 28 20 73 69 7a 65 6f 66 28 4c 41 52 if( sizeof(LAR
f5980 47 45 5f 49 4e 54 45 47 45 52 29 3c 3d 6e 42 75 GE_INTEGER)<=nBu
f5990 66 2d 6e 20 29 7b 0a 20 20 20 20 4c 41 52 47 45 f-n ){. LARGE
f59a0 5f 49 4e 54 45 47 45 52 20 69 3b 0a 20 20 20 20 _INTEGER i;.
f59b0 51 75 65 72 79 50 65 72 66 6f 72 6d 61 6e 63 65 QueryPerformance
f59c0 43 6f 75 6e 74 65 72 28 26 69 29 3b 0a 20 20 20 Counter(&i);.
f59d0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
f59e0 2c 20 26 69 2c 20 73 69 7a 65 6f 66 28 69 29 29 , &i, sizeof(i))
f59f0 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f ;. n += sizeo
f5a00 66 28 69 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 f(i);. }.#endif
f5a10 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
f5a20 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 ./*.** Sleep for
f5a30 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e a little while.
f5a40 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f Return the amo
f5a50 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 unt of time slep
f5a60 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
f5a70 20 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 74 65 winSleep(sqlite
f5a80 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
f5a90 20 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 53 6c microsec){. Sl
f5aa0 65 65 70 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 eep((microsec+99
f5ab0 39 29 2f 31 30 30 30 29 3b 0a 20 20 55 4e 55 53 9)/1000);. UNUS
f5ac0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
f5ad0 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6d s);. return ((m
f5ae0 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 icrosec+999)/100
f5af0 30 29 2a 31 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 0)*1000;.}../*.*
f5b00 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
f5b10 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74 variable, if set
f5b20 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 to a non-zero v
f5b30 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 68 alue, becomes th
f5b40 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75 e result.** retu
f5b50 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 rned from sqlite
f5b60 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 3OsCurrentTime()
f5b70 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 . This is used
f5b80 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a for testing..*/.
f5b90 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
f5ba0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
f5bb0 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e t sqlite3_curren
f5bc0 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 t_time = 0;.#end
f5bd0 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 if../*.** Find t
f5be0 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 he current time
f5bf0 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f (in Universal Co
f5c00 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e ordinated Time).
f5c10 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 Write the.** c
f5c20 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 urrent time and
f5c30 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e date as a Julian
f5c40 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f Day number into
f5c50 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 *prNow and.** r
f5c60 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e eturn 0. Return
f5c70 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 1 if the time a
f5c80 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 nd date cannot b
f5c90 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 e found..*/.int
f5ca0 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 28 73 winCurrentTime(s
f5cb0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
f5cc0 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 , double *prNow)
f5cd0 7b 0a 20 20 46 49 4c 45 54 49 4d 45 20 66 74 3b {. FILETIME ft;
f5ce0 0a 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45 20 73 . /* FILETIME s
f5cf0 74 72 75 63 74 75 72 65 20 69 73 20 61 20 36 34 tructure is a 64
f5d00 2d 62 69 74 20 76 61 6c 75 65 20 72 65 70 72 65 -bit value repre
f5d10 73 65 6e 74 69 6e 67 20 74 68 65 20 6e 75 6d 62 senting the numb
f5d20 65 72 20 6f 66 20 0a 20 20 20 20 20 31 30 30 2d er of . 100-
f5d30 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72 nanosecond inter
f5d40 76 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e 75 61 vals since Janua
f5d50 72 79 20 31 2c 20 31 36 30 31 20 28 3d 20 4a 44 ry 1, 1601 (= JD
f5d60 20 32 33 30 35 38 31 33 2e 35 29 2e 20 0a 20 20 2305813.5). .
f5d70 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 */. sqlite3_int
f5d80 36 34 20 74 69 6d 65 57 3b 20 20 20 2f 2a 20 57 64 timeW; /* W
f5d90 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20 73 hole days */. s
f5da0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 69 6d qlite3_int64 tim
f5db0 65 46 3b 20 20 20 2f 2a 20 46 72 61 63 74 69 6f eF; /* Fractio
f5dc0 6e 61 6c 20 44 61 79 73 20 2a 2f 0a 0a 20 20 2f nal Days */.. /
f5dd0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 30 2d * Number of 100-
f5de0 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72 nanosecond inter
f5df0 76 61 6c 73 20 69 6e 20 61 20 73 69 6e 67 6c 65 vals in a single
f5e00 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 74 69 63 day */. static
f5e10 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
f5e20 6e 74 36 34 20 6e 74 75 50 65 72 44 61 79 20 3d nt64 ntuPerDay =
f5e30 20 0a 20 20 20 20 20 20 31 30 30 30 30 30 30 30 . 10000000
f5e40 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 *(sqlite3_int64)
f5e50 38 36 34 30 30 3b 0a 0a 20 20 2f 2a 20 4e 75 6d 86400;.. /* Num
f5e60 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 ber of 100-nanos
f5e70 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 econd intervals
f5e80 69 6e 20 68 61 6c 66 20 6f 66 20 61 20 64 61 79 in half of a day
f5e90 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e */. static con
f5ea0 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 st sqlite3_int64
f5eb0 20 6e 74 75 50 65 72 48 61 6c 66 44 61 79 20 3d ntuPerHalfDay =
f5ec0 20 0a 20 20 20 20 20 20 31 30 30 30 30 30 30 30 . 10000000
f5ed0 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 *(sqlite3_int64)
f5ee0 34 33 32 30 30 3b 0a 0a 20 20 2f 2a 20 32 5e 33 43200;.. /* 2^3
f5ef0 32 20 2d 20 74 6f 20 61 76 6f 69 64 20 75 73 65 2 - to avoid use
f5f00 20 6f 66 20 4c 4c 20 61 6e 64 20 77 61 72 6e 69 of LL and warni
f5f10 6e 67 73 20 69 6e 20 67 63 63 20 2a 2f 0a 20 20 ngs in gcc */.
f5f20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
f5f30 69 74 65 33 5f 69 6e 74 36 34 20 6d 61 78 33 32 ite3_int64 max32
f5f40 42 69 74 56 61 6c 75 65 20 3d 20 0a 20 20 20 20 BitValue = .
f5f50 20 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 (sqlite3_int64
f5f60 29 32 30 30 30 30 30 30 30 30 30 20 2b 20 28 73 )2000000000 + (s
f5f70 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 qlite3_int64)200
f5f80 30 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 0000000 + (sqlit
f5f90 65 33 5f 69 6e 74 36 34 29 32 39 34 39 36 37 32 e3_int64)2949672
f5fa0 39 36 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 96;..#if SQLITE_
f5fb0 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 53 54 45 OS_WINCE. SYSTE
f5fc0 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20 47 65 MTIME time;. Ge
f5fd0 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74 69 6d tSystemTime(&tim
f5fe0 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 79 73 74 e);. /* if Syst
f5ff0 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 emTimeToFileTime
f6000 28 29 20 66 61 69 6c 73 2c 20 69 74 20 72 65 74 () fails, it ret
f6010 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 urns zero. */.
f6020 69 66 20 28 21 53 79 73 74 65 6d 54 69 6d 65 54 if (!SystemTimeT
f6030 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d 65 2c oFileTime(&time,
f6040 26 66 74 29 29 7b 0a 20 20 20 20 72 65 74 75 72 &ft)){. retur
f6050 6e 20 31 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 n 1;. }.#else.
f6060 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 41 73 GetSystemTimeAs
f6070 46 69 6c 65 54 69 6d 65 28 20 26 66 74 20 29 3b FileTime( &ft );
f6080 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 .#endif. UNUSED
f6090 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
f60a0 3b 0a 20 20 74 69 6d 65 57 20 3d 20 28 28 28 73 ;. timeW = (((s
f60b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e qlite3_int64)ft.
f60c0 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 29 2a dwHighDateTime)*
f60d0 6d 61 78 33 32 42 69 74 56 61 6c 75 65 29 20 2b max32BitValue) +
f60e0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
f60f0 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 ft.dwLowDateTime
f6100 3b 0a 20 20 74 69 6d 65 46 20 3d 20 74 69 6d 65 ;. timeF = time
f6110 57 20 25 20 6e 74 75 50 65 72 44 61 79 3b 20 20 W % ntuPerDay;
f6120 20 20 20 20 20 20 20 20 2f 2a 20 66 72 61 63 74 /* fract
f6130 69 6f 6e 61 6c 20 64 61 79 73 20 28 31 30 30 2d ional days (100-
f6140 6e 61 6e 6f 73 65 63 6f 6e 64 73 29 20 2a 2f 0a nanoseconds) */.
f6150 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 timeW = timeW
f6160 2f 20 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20 / ntuPerDay;
f6170 20 20 20 20 20 20 2f 2a 20 77 68 6f 6c 65 20 64 /* whole d
f6180 61 79 73 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d ays */. timeW =
f6190 20 74 69 6d 65 57 20 2b 20 32 33 30 35 38 31 33 timeW + 2305813
f61a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
f61b0 61 64 64 20 77 68 6f 6c 65 20 64 61 79 73 20 28 add whole days (
f61c0 66 72 6f 6d 20 32 33 30 35 38 31 33 2e 35 29 20 from 2305813.5)
f61d0 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 69 6d */. timeF = tim
f61e0 65 46 20 2b 20 6e 74 75 50 65 72 48 61 6c 66 44 eF + ntuPerHalfD
f61f0 61 79 3b 20 20 20 20 20 20 2f 2a 20 61 64 64 20 ay; /* add
f6200 68 61 6c 66 20 61 20 64 61 79 20 28 66 72 6f 6d half a day (from
f6210 20 32 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 2305813.5) */.
f6220 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b timeW = timeW +
f6230 20 28 74 69 6d 65 46 2f 6e 74 75 50 65 72 44 61 (timeF/ntuPerDa
f6240 79 29 3b 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c y); /* add whol
f6250 65 20 64 61 79 20 69 66 20 68 61 6c 66 20 64 61 e day if half da
f6260 79 20 6d 61 64 65 20 6f 6e 65 20 2a 2f 0a 20 20 y made one */.
f6270 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 25 20 timeF = timeF %
f6280 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 ntuPerDay;
f6290 20 20 20 20 2f 2a 20 63 6f 6d 70 75 74 65 20 6e /* compute n
f62a0 65 77 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 ew fractional da
f62b0 79 73 20 2a 2f 0a 20 20 2a 70 72 4e 6f 77 20 3d ys */. *prNow =
f62c0 20 28 64 6f 75 62 6c 65 29 74 69 6d 65 57 20 2b (double)timeW +
f62d0 20 28 28 64 6f 75 62 6c 65 29 74 69 6d 65 46 20 ((double)timeF
f62e0 2f 20 28 64 6f 75 62 6c 65 29 6e 74 75 50 65 72 / (double)ntuPer
f62f0 44 61 79 29 3b 0a 23 69 66 64 65 66 20 53 51 4c Day);.#ifdef SQL
f6300 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 ITE_TEST. if( s
f6310 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 qlite3_current_t
f6320 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f ime ){. *prNo
f6330 77 20 3d 20 28 28 64 6f 75 62 6c 65 29 73 71 6c w = ((double)sql
f6340 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d ite3_current_tim
f6350 65 20 2b 20 28 64 6f 75 62 6c 65 29 34 33 32 30 e + (double)4320
f6360 30 29 20 2f 20 28 64 6f 75 62 6c 65 29 38 36 34 0) / (double)864
f6370 30 30 20 2b 20 28 64 6f 75 62 6c 65 29 32 34 34 00 + (double)244
f6380 30 35 38 37 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0587;. }.#endif
f6390 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
f63a0 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 69 /*.** The idea i
f63b0 73 20 74 68 61 74 20 74 68 69 73 20 66 75 6e 63 s that this func
f63c0 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 tion works like
f63d0 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 a combination of
f63e0 0a 2a 2a 20 47 65 74 4c 61 73 74 45 72 72 6f 72 .** GetLastError
f63f0 28 29 20 61 6e 64 20 46 6f 72 6d 61 74 4d 65 73 () and FormatMes
f6400 73 61 67 65 28 29 20 6f 6e 20 77 69 6e 64 6f 77 sage() on window
f6410 73 20 28 6f 72 20 65 72 72 6e 6f 20 61 6e 64 0a s (or errno and.
f6420 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 29 20 ** strerror_r()
f6430 6f 6e 20 75 6e 69 78 29 2e 20 41 66 74 65 72 20 on unix). After
f6440 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 an error is retu
f6450 72 6e 65 64 20 62 79 20 61 6e 20 4f 53 0a 2a 2a rned by an OS.**
f6460 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c 69 74 function, SQLit
f6470 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e e calls this fun
f6480 63 74 69 6f 6e 20 77 69 74 68 20 7a 42 75 66 20 ction with zBuf
f6490 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 61 pointing to.** a
f64a0 20 62 75 66 66 65 72 20 6f 66 20 6e 42 75 66 20 buffer of nBuf
f64b0 62 79 74 65 73 2e 20 54 68 65 20 4f 53 20 6c 61 bytes. The OS la
f64c0 79 65 72 20 73 68 6f 75 6c 64 20 70 6f 70 75 6c yer should popul
f64d0 61 74 65 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 ate the.** buffe
f64e0 72 20 77 69 74 68 20 61 20 6e 75 6c 2d 74 65 72 r with a nul-ter
f64f0 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 65 6e minated UTF-8 en
f6500 63 6f 64 65 64 20 65 72 72 6f 72 20 6d 65 73 73 coded error mess
f6510 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e age.** describin
f6520 67 20 74 68 65 20 6c 61 73 74 20 49 4f 20 65 72 g the last IO er
f6530 72 6f 72 20 74 6f 20 68 61 76 65 20 6f 63 63 75 ror to have occu
f6540 72 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 rred within the
f6550 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 72 65 61 calling.** threa
f6560 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
f6570 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 error message is
f6580 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 74 too large for t
f6590 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66 66 he supplied buff
f65a0 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f 75 6c 64 er,.** it should
f65b0 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e 20 54 be truncated. T
f65c0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
f65d0 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 of xGetLastError
f65e0 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74 .** is zero if t
f65f0 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
f6600 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66 fits in the buf
f6610 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f fer, or non-zero
f6620 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69 .** otherwise (i
f6630 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61 f the message wa
f6640 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20 49 66 s truncated). If
f6650 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74 non-zero is ret
f6660 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 69 urned,.** then i
f6670 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 t is not necessa
f6680 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 ry to include th
f6690 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 e nul-terminator
f66a0 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69 6e character.** in
f66b0 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 the output buff
f66c0 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 75 er..**.** Not su
f66d0 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 72 6f 72 pplying an error
f66e0 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 68 61 message will ha
f66f0 76 65 20 6e 6f 20 61 64 76 65 72 73 65 20 65 66 ve no adverse ef
f6700 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 4c 69 74 fect.** on SQLit
f6710 65 2e 20 49 74 20 69 73 20 66 69 6e 65 20 74 6f e. It is fine to
f6720 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 65 6d 65 have an impleme
f6730 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6e 65 76 ntation that nev
f6740 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 6e er.** returns an
f6750 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 3a 0a error message:.
f6760 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 47 65 74 **.** int xGet
f6770 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 LastError(sqlite
f6780 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
f6790 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
f67a0 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 f){.** asser
f67b0 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c 30 27 29 t(zBuf[0]=='\0')
f67c0 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 72 6e 20 ;.** return
f67d0 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 0;.** }.**.**
f67e0 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20 65 72 However if an er
f67f0 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 73 ror message is s
f6800 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69 6c 6c upplied, it will
f6810 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 65 64 be incorporated
f6820 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20 69 6e .** by sqlite in
f6830 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 to the error mes
f6840 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65 20 74 sage available t
f6850 6f 20 74 68 65 20 75 73 65 72 20 75 73 69 6e 67 o the user using
f6860 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d .** sqlite3_errm
f6870 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79 20 6d sg(), possibly m
f6880 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 20 aking IO errors
f6890 65 61 73 69 65 72 20 74 6f 20 64 65 62 75 67 2e easier to debug.
f68a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
f68b0 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 inGetLastError(s
f68c0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
f68d0 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
f68e0 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 *zBuf){. UNUSE
f68f0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
f6900 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 65 74 4c );. return getL
f6910 61 73 74 45 72 72 6f 72 4d 73 67 28 6e 42 75 66 astErrorMsg(nBuf
f6920 2c 20 7a 42 75 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , zBuf);.}../*.*
f6930 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 * Initialize and
f6940 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 deinitialize th
f6950 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
f6960 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f em interface..*/
f6970 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f6980 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
f6990 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
f69a0 73 71 6c 69 74 65 33 5f 76 66 73 20 77 69 6e 56 sqlite3_vfs winV
f69b0 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 fs = {. 1,
f69c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f69d0 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 iVersion */.
f69e0 20 73 69 7a 65 6f 66 28 77 69 6e 46 69 6c 65 29 sizeof(winFile)
f69f0 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 , /* szOsFile
f6a00 2a 2f 0a 20 20 20 20 4d 41 58 5f 50 41 54 48 2c */. MAX_PATH,
f6a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 /* mxP
f6a20 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 athname */. 0
f6a30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
f6a40 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 /* pNext */.
f6a50 20 20 22 77 69 6e 33 32 22 2c 20 20 20 20 20 20 "win32",
f6a60 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f /* zName */
f6a70 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
f6a80 20 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70 44 /* pAppD
f6a90 61 74 61 20 2a 2f 0a 20 0a 20 20 20 20 77 69 6e ata */. . win
f6aa0 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 Open,
f6ab0 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 /* xOpen */.
f6ac0 77 69 6e 44 65 6c 65 74 65 2c 20 20 20 20 20 20 winDelete,
f6ad0 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f /* xDelete */
f6ae0 0a 20 20 20 20 77 69 6e 41 63 63 65 73 73 2c 20 . winAccess,
f6af0 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 63 65 /* xAcce
f6b00 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 46 75 6c ss */. winFul
f6b10 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f 2a 20 lPathname, /*
f6b20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f xFullPathname */
f6b30 0a 20 20 20 20 77 69 6e 44 6c 4f 70 65 6e 2c 20 . winDlOpen,
f6b40 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70 /* xDlOp
f6b50 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 45 en */. winDlE
f6b60 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 rror, /*
f6b70 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 20 20 xDlError */.
f6b80 77 69 6e 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 winDlSym,
f6b90 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a /* xDlSym */.
f6ba0 20 20 20 20 77 69 6e 44 6c 43 6c 6f 73 65 2c 20 winDlClose,
f6bb0 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f /* xDlClo
f6bc0 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e 52 61 6e se */. winRan
f6bd0 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a 20 domness, /*
f6be0 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 xRandomness */.
f6bf0 20 20 20 77 69 6e 53 6c 65 65 70 2c 20 20 20 20 winSleep,
f6c00 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 /* xSleep
f6c10 2a 2f 0a 20 20 20 20 77 69 6e 43 75 72 72 65 6e */. winCurren
f6c20 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78 43 75 tTime, /* xCu
f6c30 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20 20 rrentTime */.
f6c40 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72 winGetLastError
f6c50 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 /* xGetLastE
f6c60 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 rror */. };. s
f6c70 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
f6c80 74 65 72 28 26 77 69 6e 56 66 73 2c 20 31 29 3b ter(&winVfs, 1);
f6c90 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
f6ca0 5f 4f 4b 3b 20 0a 7d 0a 53 51 4c 49 54 45 5f 41 _OK; .}.SQLITE_A
f6cb0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
f6cc0 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 s_end(void){ .
f6cd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f6ce0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
f6cf0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a QLITE_OS_WIN */.
f6d00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
f6d10 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 End of os_win.c
f6d20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
f6d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
f6d50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
f6d60 20 42 65 67 69 6e 20 66 69 6c 65 20 62 69 74 76 Begin file bitv
f6d70 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ec.c ***********
f6d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
f6da0 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46 65 62 72 ./*.** 2008 Febr
f6db0 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 uary 16.**.** Th
f6dc0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
f6dd0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
f6de0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
f6df0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
f6e00 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
f6e10 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
f6e20 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
f6e30 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
f6e40 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
f6e50 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
f6e60 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
f6e70 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
f6e80 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
f6e90 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
f6ea0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
f6eb0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
f6ec0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
f6ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6f10 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
f6f20 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 ile implements a
f6f30 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 72 65 n object that re
f6f40 70 72 65 73 65 6e 74 73 20 61 20 66 69 78 65 64 presents a fixed
f6f50 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69 74 6d 61 -length.** bitma
f6f60 70 2e 20 20 42 69 74 73 20 61 72 65 20 6e 75 6d p. Bits are num
f6f70 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 77 bered starting w
f6f80 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62 ith 1..**.** A b
f6f90 69 74 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f itmap is used to
f6fa0 20 72 65 63 6f 72 64 20 77 68 69 63 68 20 70 61 record which pa
f6fb0 67 65 73 20 6f 66 20 61 20 64 61 74 61 62 61 73 ges of a databas
f6fc0 65 20 66 69 6c 65 20 68 61 76 65 20 62 65 65 6e e file have been
f6fd0 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 .** journalled d
f6fe0 75 72 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 uring a transact
f6ff0 69 6f 6e 2c 20 6f 72 20 77 68 69 63 68 20 70 61 ion, or which pa
f7000 67 65 73 20 68 61 76 65 20 74 68 65 20 22 64 6f ges have the "do
f7010 6e 74 2d 77 72 69 74 65 22 0a 2a 2a 20 70 72 6f nt-write".** pro
f7020 70 65 72 74 79 2e 20 20 55 73 75 61 6c 6c 79 20 perty. Usually
f7030 6f 6e 6c 79 20 61 20 66 65 77 20 70 61 67 65 73 only a few pages
f7040 20 61 72 65 20 6d 65 65 74 20 65 69 74 68 65 72 are meet either
f7050 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53 condition..** S
f7060 6f 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 o the bitmap is
f7070 75 73 75 61 6c 6c 79 20 73 70 61 72 73 65 20 61 usually sparse a
f7080 6e 64 20 68 61 73 20 6c 6f 77 20 63 61 72 64 69 nd has low cardi
f7090 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 73 nality..** But s
f70a0 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72 20 65 78 ometimes (for ex
f70b0 61 6d 70 6c 65 20 77 68 65 6e 20 64 75 72 69 6e ample when durin
f70c0 67 20 61 20 44 52 4f 50 20 6f 66 20 61 20 6c 61 g a DROP of a la
f70d0 72 67 65 20 74 61 62 6c 65 29 20 6d 6f 73 74 0a rge table) most.
f70e0 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 ** or all of the
f70f0 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61 pages in a data
f7100 62 61 73 65 20 63 61 6e 20 67 65 74 20 6a 6f 75 base can get jou
f7110 72 6e 61 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f rnalled. In tho
f7120 73 65 20 63 61 73 65 73 2c 20 0a 2a 2a 20 74 68 se cases, .** th
f7130 65 20 62 69 74 6d 61 70 20 62 65 63 6f 6d 65 73 e bitmap becomes
f7140 20 64 65 6e 73 65 20 77 69 74 68 20 68 69 67 68 dense with high
f7150 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 20 20 54 cardinality. T
f7160 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65 he algorithm nee
f7170 64 73 20 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 ds .** to handle
f7180 20 62 6f 74 68 20 63 61 73 65 73 20 77 65 6c 6c both cases well
f7190 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 ..**.** The size
f71a0 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 20 69 of the bitmap i
f71b0 73 20 66 69 78 65 64 20 77 68 65 6e 20 74 68 65 s fixed when the
f71c0 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 object is creat
f71d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69 ed..**.** All bi
f71e0 74 73 20 61 72 65 20 63 6c 65 61 72 20 77 68 65 ts are clear whe
f71f0 6e 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 n the bitmap is
f7200 63 72 65 61 74 65 64 2e 20 20 49 6e 64 69 76 69 created. Indivi
f7210 64 75 61 6c 20 62 69 74 73 0a 2a 2a 20 6d 61 79 dual bits.** may
f7220 20 62 65 20 73 65 74 20 6f 72 20 63 6c 65 61 72 be set or clear
f7230 65 64 20 6f 6e 65 20 61 74 20 61 20 74 69 6d 65 ed one at a time
f7240 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20 6f 70 65 ..**.** Test ope
f7250 72 61 74 69 6f 6e 73 20 61 72 65 20 61 62 6f 75 rations are abou
f7260 74 20 31 30 30 20 74 69 6d 65 73 20 6d 6f 72 65 t 100 times more
f7270 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 73 65 74 common that set
f7280 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 operations..**
f7290 43 6c 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 Clear operations
f72a0 20 61 72 65 20 65 78 63 65 65 64 69 6e 67 6c 79 are exceedingly
f72b0 20 72 61 72 65 2e 20 20 54 68 65 72 65 20 61 72 rare. There ar
f72c0 65 20 75 73 75 61 6c 6c 79 20 62 65 74 77 65 65 e usually betwee
f72d0 6e 0a 2a 2a 20 35 20 61 6e 64 20 35 30 30 20 73 n.** 5 and 500 s
f72e0 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 70 65 et operations pe
f72f0 72 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2c r Bitvec object,
f7300 20 74 68 6f 75 67 68 20 74 68 65 20 6e 75 6d 62 though the numb
f7310 65 72 20 6f 66 20 73 65 74 73 20 63 61 6e 0a 2a er of sets can.*
f7320 2a 20 73 6f 6d 65 74 69 6d 65 73 20 67 72 6f 77 * sometimes grow
f7330 20 69 6e 74 6f 20 74 65 6e 73 20 6f 66 20 74 68 into tens of th
f7340 6f 75 73 61 6e 64 73 20 6f 72 20 6c 61 72 67 65 ousands or large
f7350 72 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 r. The size of
f7360 74 68 65 0a 2a 2a 20 42 69 74 76 65 63 20 6f 62 the.** Bitvec ob
f7370 6a 65 63 74 20 69 73 20 74 68 65 20 6e 75 6d 62 ject is the numb
f7380 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
f7390 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
f73a0 20 61 74 20 74 68 65 0a 2a 2a 20 73 74 61 72 74 at the.** start
f73b0 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f of a transactio
f73c0 6e 2c 20 61 6e 64 20 69 73 20 74 68 75 73 20 75 n, and is thus u
f73d0 73 75 61 6c 6c 79 20 6c 65 73 73 20 74 68 61 6e sually less than
f73e0 20 61 20 66 65 77 20 74 68 6f 75 73 61 6e 64 2c a few thousand,
f73f0 0a 2a 2a 20 62 75 74 20 63 61 6e 20 62 65 20 61 .** but can be a
f7400 73 20 6c 61 72 67 65 20 61 73 20 32 20 62 69 6c s large as 2 bil
f7410 6c 69 6f 6e 20 66 6f 72 20 61 20 72 65 61 6c 6c lion for a reall
f7420 79 20 62 69 67 20 64 61 74 61 62 61 73 65 2e 0a y big database..
f7430 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
f7440 62 69 74 76 65 63 2e 63 2c 76 20 31 2e 31 33 20 bitvec.c,v 1.13
f7450 32 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36 2009/01/20 17:06
f7460 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :27 danielk1977
f7470 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a Exp $.*/../* Siz
f7480 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 e of the Bitvec
f7490 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 74 structure in byt
f74a0 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 es. */.#define B
f74b0 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20 ITVEC_SZ
f74c0 35 31 32 0a 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 512../* Round th
f74d0 65 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 e union size dow
f74e0 6e 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 n to the nearest
f74f0 20 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 pointer boundar
f7500 79 2c 20 73 69 6e 63 65 20 74 68 61 74 27 73 20 y, since that's
f7510 68 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 how .** it will
f7520 62 65 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69 be aligned withi
f7530 6e 20 74 68 65 20 42 69 74 76 65 63 20 73 74 72 n the Bitvec str
f7540 75 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 uct. */.#define
f7550 42 49 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20 BITVEC_USIZE
f7560 20 28 28 28 42 49 54 56 45 43 5f 53 5a 2d 28 33 (((BITVEC_SZ-(3
f7570 2a 73 69 7a 65 6f 66 28 75 33 32 29 29 29 2f 73 *sizeof(u32)))/s
f7580 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a izeof(Bitvec*))*
f7590 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 sizeof(Bitvec*))
f75a0 0a 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 ../* Type of the
f75b0 20 61 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22 array "element"
f75c0 20 66 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20 for the bitmap
f75d0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 representation.
f75e0 0a 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20 .** Should be a
f75f0 70 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20 power of 2, and
f7600 69 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20 ideally, evenly
f7610 64 69 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56 divide into BITV
f7620 45 43 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 53 65 EC_USIZE. .** Se
f7630 74 74 69 6e 67 20 74 68 69 73 20 74 6f 20 74 68 tting this to th
f7640 65 20 22 6e 61 74 75 72 61 6c 20 77 6f 72 64 22 e "natural word"
f7650 20 73 69 7a 65 20 6f 66 20 79 6f 75 72 20 43 50 size of your CP
f7660 55 20 6d 61 79 20 69 6d 70 72 6f 76 65 0a 2a 2a U may improve.**
f7670 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 2a 2f performance. */
f7680 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f .#define BITVEC_
f7690 54 45 4c 45 4d 20 20 20 20 20 75 38 0a 2f 2a 20 TELEM u8./*
f76a0 53 69 7a 65 2c 20 69 6e 20 62 69 74 73 2c 20 6f Size, in bits, o
f76b0 66 20 74 68 65 20 62 69 74 6d 61 70 20 65 6c 65 f the bitmap ele
f76c0 6d 65 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 ment. */.#define
f76d0 20 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 20 20 BITVEC_SZELEM
f76e0 20 20 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 8./* Number of
f76f0 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 20 62 elements in a b
f7700 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a itmap array. */.
f7710 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e #define BITVEC_N
f7720 45 4c 45 4d 20 20 20 20 20 28 42 49 54 56 45 43 ELEM (BITVEC
f7730 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 49 _USIZE/sizeof(BI
f7740 54 56 45 43 5f 54 45 4c 45 4d 29 29 0a 2f 2a 20 TVEC_TELEM))./*
f7750 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 Number of bits i
f7760 6e 20 74 68 65 20 62 69 74 6d 61 70 20 61 72 72 n the bitmap arr
f7770 61 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 ay. */.#define B
f7780 49 54 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20 ITVEC_NBIT
f7790 28 42 49 54 56 45 43 5f 4e 45 4c 45 4d 2a 42 49 (BITVEC_NELEM*BI
f77a0 54 56 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a 2f 2a TVEC_SZELEM)../*
f77b0 20 4e 75 6d 62 65 72 20 6f 66 20 75 33 32 20 76 Number of u32 v
f77c0 61 6c 75 65 73 20 69 6e 20 68 61 73 68 20 74 61 alues in hash ta
f77d0 62 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ble. */.#define
f77e0 42 49 54 56 45 43 5f 4e 49 4e 54 20 20 20 20 20 BITVEC_NINT
f77f0 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 (BITVEC_USIZE/s
f7800 69 7a 65 6f 66 28 75 33 32 29 29 0a 2f 2a 20 4d izeof(u32))./* M
f7810 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
f7820 20 65 6e 74 72 69 65 73 20 69 6e 20 68 61 73 68 entries in hash
f7830 20 74 61 62 6c 65 20 62 65 66 6f 72 65 20 0a 2a table before .*
f7840 2a 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 * sub-dividing a
f7850 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a nd re-hashing. *
f7860 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
f7870 5f 4d 58 48 41 53 48 20 20 20 20 28 42 49 54 56 _MXHASH (BITV
f7880 45 43 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 48 61 EC_NINT/2)./* Ha
f7890 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 shing function f
f78a0 6f 72 20 74 68 65 20 61 48 61 73 68 20 72 65 70 or the aHash rep
f78b0 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 resentation..**
f78c0 45 6d 70 69 72 69 63 61 6c 20 74 65 73 74 69 6e Empirical testin
f78d0 67 20 73 68 6f 77 65 64 20 74 68 61 74 20 74 68 g showed that th
f78e0 65 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65 72 e *37 multiplier
f78f0 20 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74 72 61 .** (an arbitra
f7900 72 79 20 70 72 69 6d 65 29 69 6e 20 74 68 65 20 ry prime)in the
f7910 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 70 72 hash function pr
f7920 6f 76 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 66 65 ovided .** no fe
f7930 77 65 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 74 wer collisions t
f7940 68 61 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 2a 31 han the no-op *1
f7950 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 . */.#define BIT
f7960 56 45 43 5f 48 41 53 48 28 58 29 20 20 20 28 28 VEC_HASH(X) ((
f7970 28 58 29 2a 31 29 25 42 49 54 56 45 43 5f 4e 49 (X)*1)%BITVEC_NI
f7980 4e 54 29 0a 0a 23 64 65 66 69 6e 65 20 42 49 54 NT)..#define BIT
f7990 56 45 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42 VEC_NPTR (B
f79a0 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 ITVEC_USIZE/size
f79b0 6f 66 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 0a of(Bitvec *))...
f79c0 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 /*.** A bitmap i
f79d0 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
f79e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
f79f0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
f7a00 54 68 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f This bitmap reco
f7a10 72 64 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 rds the existanc
f7a20 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 e of zero or mor
f7a30 65 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76 e bits.** with v
f7a40 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 alues between 1
f7a50 61 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 and iSize, inclu
f7a60 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 sive..**.** Ther
f7a70 65 20 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 e are three poss
f7a80 69 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 ible representat
f7a90 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d ions of the bitm
f7aa0 61 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c ap..** If iSize<
f7ab0 3d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68 =BITVEC_NBIT, th
f7ac0 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74 en Bitvec.u.aBit
f7ad0 6d 61 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69 map[] is a strai
f7ae0 67 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 ght.** bitmap.
f7af0 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 The least signif
f7b00 69 63 61 6e 74 20 62 69 74 20 69 73 20 62 69 74 icant bit is bit
f7b10 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69 1..**.** If iSi
f7b20 7a 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61 ze>BITVEC_NBIT a
f7b30 6e 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74 nd iDivisor==0 t
f7b40 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61 hen Bitvec.u.aHa
f7b50 73 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73 sh[] is.** a has
f7b60 68 20 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c h table that wil
f7b70 6c 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54 l hold up to BIT
f7b80 56 45 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69 VEC_MXHASH disti
f7b90 6e 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a nct values..**.*
f7ba0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 * Otherwise, the
f7bb0 20 76 61 6c 75 65 20 69 20 69 73 20 72 65 64 69 value i is redi
f7bc0 72 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20 rected into one
f7bd0 6f 66 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a of BITVEC_NPTR.*
f7be0 2a 20 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f * sub-bitmaps po
f7bf0 69 6e 74 65 64 20 74 6f 20 62 79 20 42 69 74 76 inted to by Bitv
f7c00 65 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45 ec.u.apSub[]. E
f7c10 61 63 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a ach subbitmap.**
f7c20 20 68 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69 handles up to i
f7c30 44 69 76 69 73 6f 72 20 73 65 70 61 72 61 74 65 Divisor separate
f7c40 20 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61 values of i. a
f7c50 70 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a pSub[0] holds.**
f7c60 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 values between
f7c70 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20 1 and iDivisor.
f7c80 20 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20 apSub[1] holds
f7c90 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a values between.*
f7ca0 2a 20 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 * iDivisor+1 and
f7cb0 20 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70 2*iDivisor. ap
f7cc0 53 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c Sub[N] holds val
f7cd0 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e ues between.** N
f7ce0 2a 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 *iDivisor+1 and
f7cf0 28 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20 (N+1)*iDivisor.
f7d00 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 20 Each subbitmap
f7d10 69 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a is normalized.**
f7d20 20 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69 to hold deal wi
f7d30 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 th values betwee
f7d40 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 n 1 and iDivisor
f7d50 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76 ..*/.struct Bitv
f7d60 65 63 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65 ec {. u32 iSize
f7d70 3b 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 ; /* Maximu
f7d80 6d 20 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61 m bit index. Ma
f7d90 78 20 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34 x iSize is 4,294
f7da0 2c 39 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 20 75 ,967,296. */. u
f7db0 33 32 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f 32 nSet; /
f7dc0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 * Number of bits
f7dd0 20 74 68 61 74 20 61 72 65 20 73 65 74 20 2d 20 that are set -
f7de0 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 only valid for a
f7df0 48 61 73 68 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a Hash element */.
f7e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7e10 20 20 2f 2a 20 4d 61 78 20 6e 53 65 74 20 69 73 /* Max nSet is
f7e20 20 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 BITVEC_NINT. F
f7e30 6f 72 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 or BITVEC_SZ of
f7e40 35 31 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 512, this would
f7e50 62 65 20 31 32 35 2e 20 2a 2f 0a 20 20 75 33 32 be 125. */. u32
f7e60 20 69 44 69 76 69 73 6f 72 3b 20 20 20 2f 2a 20 iDivisor; /*
f7e70 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 68 Number of bits h
f7e80 61 6e 64 6c 65 64 20 62 79 20 65 61 63 68 20 61 andled by each a
f7e90 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e 20 2a 2f pSub[] entry. */
f7ea0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f7eb0 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d 30 /* Should >=0
f7ec0 20 66 6f 72 20 61 70 53 75 62 20 65 6c 65 6d 65 for apSub eleme
f7ed0 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 nt. */.
f7ee0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 /* Max
f7ef0 69 44 69 76 69 73 6f 72 20 69 73 20 6d 61 78 28 iDivisor is max(
f7f00 75 33 32 29 20 2f 20 42 49 54 56 45 43 5f 4e 50 u32) / BITVEC_NP
f7f10 54 52 20 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 20 TR + 1. */.
f7f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f7f30 20 46 6f 72 20 61 20 42 49 54 56 45 43 5f 53 5a For a BITVEC_SZ
f7f40 20 6f 66 20 35 31 32 2c 20 74 68 69 73 20 77 6f of 512, this wo
f7f50 75 6c 64 20 62 65 20 33 34 2c 33 35 39 2c 37 33 uld be 34,359,73
f7f60 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 9. */. union {.
f7f70 20 20 20 20 42 49 54 56 45 43 5f 54 45 4c 45 4d BITVEC_TELEM
f7f80 20 61 42 69 74 6d 61 70 5b 42 49 54 56 45 43 5f aBitmap[BITVEC_
f7f90 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 69 NELEM]; /* Bi
f7fa0 74 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 tmap representat
f7fb0 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 33 32 20 61 ion */. u32 a
f7fc0 48 61 73 68 5b 42 49 54 56 45 43 5f 4e 49 4e 54 Hash[BITVEC_NINT
f7fd0 5d 3b 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 ]; /* Hash
f7fe0 74 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 table representa
f7ff0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 42 69 74 76 tion */. Bitv
f8000 65 63 20 2a 61 70 53 75 62 5b 42 49 54 56 45 43 ec *apSub[BITVEC
f8010 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 52 65 63 75 _NPTR]; /* Recu
f8020 72 73 69 76 65 20 72 65 70 72 65 73 65 6e 74 61 rsive representa
f8030 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d tion */. } u;.}
f8040 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 ;../*.** Create
f8050 61 20 6e 65 77 20 62 69 74 6d 61 70 20 6f 62 6a a new bitmap obj
f8060 65 63 74 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 ect able to hand
f8070 6c 65 20 62 69 74 73 20 62 65 74 77 65 65 6e 20 le bits between
f8080 30 20 61 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 20 0 and iSize,.**
f8090 69 6e 63 6c 75 73 69 76 65 2e 20 20 52 65 74 75 inclusive. Retu
f80a0 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
f80b0 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 the new object.
f80c0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 Return NULL if
f80d0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 .** malloc fails
f80e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
f80f0 41 54 45 20 42 69 74 76 65 63 20 2a 73 71 6c 69 ATE Bitvec *sqli
f8100 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 te3BitvecCreate(
f8110 75 33 32 20 69 53 69 7a 65 29 7b 0a 20 20 42 69 u32 iSize){. Bi
f8120 74 76 65 63 20 2a 70 3b 0a 20 20 61 73 73 65 72 tvec *p;. asser
f8130 74 28 20 73 69 7a 65 6f 66 28 2a 70 29 3d 3d 42 t( sizeof(*p)==B
f8140 49 54 56 45 43 5f 53 5a 20 29 3b 0a 20 20 70 20 ITVEC_SZ );. p
f8150 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a = sqlite3MallocZ
f8160 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 ero( sizeof(*p)
f8170 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 );. if( p ){.
f8180 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 69 53 69 p->iSize = iSi
f8190 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ze;. }. return
f81a0 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 p;.}../*.** Che
f81b0 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 ck to see if the
f81c0 20 69 2d 74 68 20 62 69 74 20 69 73 20 73 65 74 i-th bit is set
f81d0 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 6f . Return true o
f81e0 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 70 r false..** If p
f81f0 20 69 73 20 4e 55 4c 4c 20 28 69 66 20 74 68 65 is NULL (if the
f8200 20 62 69 74 6d 61 70 20 68 61 73 20 6e 6f 74 20 bitmap has not
f8210 62 65 65 6e 20 63 72 65 61 74 65 64 29 20 6f 72 been created) or
f8220 20 69 66 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20 if.** i is out
f8230 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 72 of range, then r
f8240 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a eturn false..*/.
f8250 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
f8260 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 nt sqlite3Bitvec
f8270 54 65 73 74 28 42 69 74 76 65 63 20 2a 70 2c 20 Test(Bitvec *p,
f8280 75 33 32 20 69 29 7b 0a 20 20 69 66 28 20 70 3d u32 i){. if( p=
f8290 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
f82a0 20 69 66 28 20 69 3e 70 2d 3e 69 53 69 7a 65 20 if( i>p->iSize
f82b0 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74 75 72 6e || i==0 ) return
f82c0 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 0;. i--;. whi
f82d0 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 le( p->iDivisor
f82e0 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d ){. u32 bin =
f82f0 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a i/p->iDivisor;.
f8300 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 i = i%p->iDi
f8310 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 visor;. p = p
f8320 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a ->u.apSub[bin];.
f8330 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 if (!p) {.
f8340 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
f8350 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d }. }. if( p-
f8360 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e >iSize<=BITVEC_N
f8370 42 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 BIT ){. retur
f8380 6e 20 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b n (p->u.aBitmap[
f8390 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d i/BITVEC_SZELEM]
f83a0 20 26 20 28 31 3c 3c 28 69 26 28 42 49 54 56 45 & (1<<(i&(BITVE
f83b0 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d C_SZELEM-1))))!=
f83c0 30 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a 20 20 20 0;. } else{.
f83d0 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f u32 h = BITVEC_
f83e0 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 20 20 77 HASH(i++);. w
f83f0 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 hile( p->u.aHash
f8400 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 [h] ){. if(
f8410 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d p->u.aHash[h]==
f8420 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 i ) return 1;.
f8430 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 69 h++;. i
f8440 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e f( h>=BITVEC_NIN
f8450 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 7d T ) h = 0;. }
f8460 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
f8470 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 }.}../*.** Set
f8480 74 68 65 20 69 2d 74 68 20 62 69 74 2e 20 20 52 the i-th bit. R
f8490 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 eturn 0 on succe
f84a0 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 ss and an error
f84b0 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 code if.** anyth
f84c0 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a ing goes wrong..
f84d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
f84e0 6e 65 20 6d 69 67 68 74 20 63 61 75 73 65 20 73 ne might cause s
f84f0 75 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62 65 ub-bitmaps to be
f8500 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 allocated. Fai
f8510 6c 69 6e 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74 ling.** to get t
f8520 68 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 he memory needed
f8530 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62 to hold the sub
f8540 2d 62 69 74 6d 61 70 20 69 73 20 74 68 65 20 6f -bitmap is the o
f8550 6e 6c 79 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 nly.** that can
f8560 67 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61 6e go wrong with an
f8570 20 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69 6e insert, assumin
f8580 67 20 70 20 61 6e 64 20 69 20 61 72 65 20 76 61 g p and i are va
f8590 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 lid..**.** The c
f85a0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
f85b0 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 must ensure that
f85c0 20 70 20 69 73 20 61 20 76 61 6c 69 64 20 42 69 p is a valid Bi
f85d0 74 76 65 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 tvec object.** a
f85e0 6e 64 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 nd that the valu
f85f0 65 20 66 6f 72 20 22 69 22 20 69 73 20 77 69 74 e for "i" is wit
f8600 68 69 6e 20 72 61 6e 67 65 20 6f 66 20 74 68 65 hin range of the
f8610 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a Bitvec object..
f8620 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 ** Otherwise the
f8630 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
f8640 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
f8650 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
f8660 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 42 lite3BitvecSet(B
f8670 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 itvec *p, u32 i)
f8680 7b 0a 20 20 75 33 32 20 68 3b 0a 20 20 61 73 73 {. u32 h;. ass
f8690 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 ert( p!=0 );. a
f86a0 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 ssert( i>0 );.
f86b0 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 assert( i<=p->iS
f86c0 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 ize );. i--;.
f86d0 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a 65 20 while((p->iSize
f86e0 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 29 20 26 > BITVEC_NBIT) &
f86f0 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 29 20 7b & p->iDivisor) {
f8700 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 . u32 bin = i
f8710 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 /p->iDivisor;.
f8720 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 i = i%p->iDivi
f8730 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e sor;. if( p->
f8740 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 u.apSub[bin]==0
f8750 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 2e 61 70 ){. p->u.ap
f8760 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74 Sub[bin] = sqlit
f8770 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 20 e3BitvecCreate(
f8780 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b 0a 20 p->iDivisor );.
f8790 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 if( p->u.ap
f87a0 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65 Sub[bin]==0 ) re
f87b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
f87c0 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d M;. }. p =
f87d0 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d p->u.apSub[bin]
f87e0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 ;. }. if( p->i
f87f0 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 Size<=BITVEC_NBI
f8800 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 T ){. p->u.aB
f8810 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 itmap[i/BITVEC_S
f8820 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20 28 ZELEM] |= 1 << (
f8830 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d i&(BITVEC_SZELEM
f8840 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e -1));. return
f8850 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
f8860 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 h = BITVEC_HAS
f8870 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66 20 H(i++);. /* if
f8880 74 68 65 72 65 20 77 61 73 6e 27 74 20 61 20 68 there wasn't a h
f8890 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 61 ash collision, a
f88a0 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27 74 20 nd this doesn't
f88b0 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65 74 65 */. /* complete
f88c0 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61 73 68 ly fill the hash
f88d0 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64 64 20 , then just add
f88e0 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a 20 20 it without */.
f88f0 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f 75 74 /* worring about
f8900 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e sub-dividing an
f8910 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f d re-hashing. */
f8920 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61 48 61 . if( !p->u.aHa
f8930 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 66 20 sh[h] ){. if
f8940 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56 45 43 (p->nSet<(BITVEC
f8950 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20 20 20 _NINT-1)) {.
f8960 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 goto bitvec_se
f8970 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65 6c 73 t_end;. } els
f8980 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 e {. goto b
f8990 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 68 itvec_set_rehash
f89a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a ;. }. }. /*
f89b0 20 74 68 65 72 65 20 77 61 73 20 61 20 63 6f 6c there was a col
f89c0 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20 74 6f lision, check to
f89d0 20 73 65 65 20 69 66 20 69 74 27 73 20 61 6c 72 see if it's alr
f89e0 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e 20 eady */. /* in
f89f0 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20 74 72 hash, if not, tr
f8a00 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70 6f 74 y to find a spot
f8a10 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64 6f 20 for it */. do
f8a20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 {. if( p->u.a
f8a30 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74 Hash[h]==i ) ret
f8a40 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
f8a50 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 28 20 h++;. if(
f8a60 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 h>=BITVEC_NINT )
f8a70 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c h = 0;. } whil
f8a80 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d e( p->u.aHash[h]
f8a90 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69 64 6e );. /* we didn
f8aa0 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20 74 68 't find it in th
f8ab0 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69 6e 74 e hash. h point
f8ac0 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 2a s to the first *
f8ad0 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62 6c 65 /. /* available
f8ae0 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68 65 63 free spot. chec
f8af0 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73 k to see if this
f8b00 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a 2f 0a is going to */.
f8b10 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20 68 61 /* make our ha
f8b20 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e 20 20 sh too "full".
f8b30 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f 72 65 */.bitvec_set_re
f8b40 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d 3e 6e hash:. if( p->n
f8b50 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58 48 41 Set>=BITVEC_MXHA
f8b60 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e SH ){. unsign
f8b70 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 69 6e ed int j;. in
f8b80 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20 61 69 t rc;. u32 ai
f8b90 56 61 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 Values[BITVEC_NI
f8ba0 4e 54 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 NT];. memcpy(
f8bb0 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 aiValues, p->u.a
f8bc0 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 Hash, sizeof(aiV
f8bd0 61 6c 75 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d alues));. mem
f8be0 73 65 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 set(p->u.apSub,
f8bf0 30 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 0, sizeof(aiValu
f8c00 65 73 29 29 3b 0a 20 20 20 20 70 2d 3e 69 44 69 es));. p->iDi
f8c10 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a visor = (p->iSiz
f8c20 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52 20 e + BITVEC_NPTR
f8c30 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54 52 - 1)/BITVEC_NPTR
f8c40 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
f8c50 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 69 e3BitvecSet(p, i
f8c60 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 );. for(j=0;
f8c70 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a j<BITVEC_NINT; j
f8c80 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 ++){. if( a
f8c90 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63 20 iValues[j] ) rc
f8ca0 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 |= sqlite3Bitvec
f8cb0 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73 5b Set(p, aiValues[
f8cc0 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 j]);. }. r
f8cd0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 62 69 eturn rc;. }.bi
f8ce0 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 20 tvec_set_end:.
f8cf0 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d 3e p->nSet++;. p->
f8d00 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a u.aHash[h] = i;.
f8d10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f8d20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 OK;.}../*.** Cle
f8d30 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e ar the i-th bit.
f8d40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
f8d50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
f8d60 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 65 itvecClear(Bitve
f8d70 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 c *p, u32 i){.
f8d80 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a assert( p!=0 );.
f8d90 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b assert( i>0 );
f8da0 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 . i--;. while(
f8db0 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a p->iDivisor ){.
f8dc0 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f u32 bin = i/
f8dd0 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 p->iDivisor;.
f8de0 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 i = i%p->iDivis
f8df0 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 or;. p = p->u
f8e00 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 .apSub[bin];.
f8e10 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 20 if (!p) {.
f8e20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
f8e30 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a }. if( p->iSiz
f8e40 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 e<=BITVEC_NBIT )
f8e50 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d {. p->u.aBitm
f8e60 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c ap[i/BITVEC_SZEL
f8e70 45 4d 5d 20 26 3d 20 7e 28 31 20 3c 3c 20 28 69 EM] &= ~(1 << (i
f8e80 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d &(BITVEC_SZELEM-
f8e90 31 29 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 1)));. }else{.
f8ea0 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
f8eb0 6a 3b 0a 20 20 20 20 75 33 32 20 61 69 56 61 6c j;. u32 aiVal
f8ec0 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d ues[BITVEC_NINT]
f8ed0 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 ;. memcpy(aiV
f8ee0 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 alues, p->u.aHas
f8ef0 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 h, sizeof(aiValu
f8f00 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 es));. memset
f8f10 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c 20 (p->u.aHash, 0,
f8f20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29 sizeof(aiValues)
f8f30 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d );. p->nSet =
f8f40 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 0;. for(j=0;
f8f50 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 j<BITVEC_NINT;
f8f60 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 j++){. if(
f8f70 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20 61 aiValues[j] && a
f8f80 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b 31 iValues[j]!=(i+1
f8f90 29 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32 ) ){. u32
f8fa0 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 h = BITVEC_HASH
f8fb0 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d 31 29 3b (aiValues[j]-1);
f8fc0 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 53 65 74 . p->nSet
f8fd0 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c ++;. whil
f8fe0 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d e( p->u.aHash[h]
f8ff0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 68 2b ){. h+
f9000 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 +;. if(
f9010 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 h>=BITVEC_NINT
f9020 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 ) h = 0;.
f9030 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 75 2e }. p->u.
f9040 61 48 61 73 68 5b 68 5d 20 3d 20 61 69 56 61 6c aHash[h] = aiVal
f9050 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a ues[j];. }.
f9060 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
f9070 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69 74 ** Destroy a bit
f9080 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65 63 map object. Rec
f9090 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 laim all memory
f90a0 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f used..*/.SQLITE_
f90b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
f90c0 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
f90d0 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 y(Bitvec *p){.
f90e0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
f90f0 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 44 69 76 n;. if( p->iDiv
f9100 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 6e 73 69 isor ){. unsi
f9110 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 gned int i;.
f9120 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 54 56 45 for(i=0; i<BITVE
f9130 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20 20 C_NPTR; i++){.
f9140 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
f9150 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61 70 cDestroy(p->u.ap
f9160 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 Sub[i]);. }.
f9170 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
f9180 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 e(p);.}../*.** R
f9190 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 eturn the value
f91a0 6f 66 20 74 68 65 20 69 53 69 7a 65 20 70 61 72 of the iSize par
f91b0 61 6d 65 74 65 72 20 73 70 65 63 69 66 69 65 64 ameter specified
f91c0 20 77 68 65 6e 20 42 69 74 76 65 63 20 2a 70 0a when Bitvec *p.
f91d0 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a ** was created..
f91e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
f91f0 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 69 74 E u32 sqlite3Bit
f9200 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 20 2a vecSize(Bitvec *
f9210 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
f9220 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 iSize;.}..#ifnde
f9230 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
f9240 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a ILTIN_TEST./*.**
f9250 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 6e 20 61 Let V[] be an a
f9260 72 72 61 79 20 6f 66 20 75 6e 73 69 67 6e 65 64 rray of unsigned
f9270 20 63 68 61 72 61 63 74 65 72 73 20 73 75 66 66 characters suff
f9280 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a icient to hold.*
f9290 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 73 2e 20 * up to N bits.
f92a0 20 4c 65 74 20 49 20 62 65 20 61 6e 20 69 6e 74 Let I be an int
f92b0 65 67 65 72 20 62 65 74 77 65 65 6e 20 30 20 61 eger between 0 a
f92c0 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a nd N. 0<=I<N..*
f92d0 2a 20 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f * Then the follo
f92e0 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e 20 wing macros can
f92f0 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 2c 20 be used to set,
f9300 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a clear, or test.*
f9310 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 62 69 74 * individual bit
f9320 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23 s within V..*/.#
f9330 64 65 66 69 6e 65 20 53 45 54 42 49 54 28 56 2c define SETBIT(V,
f9340 49 29 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20 I) V[I>>3]
f9350 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 |= (1<<(I&7)).#d
f9360 65 66 69 6e 65 20 43 4c 45 41 52 42 49 54 28 56 efine CLEARBIT(V
f9370 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26 ,I) V[I>>3] &
f9380 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 = ~(1<<(I&7)).#d
f9390 65 66 69 6e 65 20 54 45 53 54 42 49 54 28 56 2c efine TESTBIT(V,
f93a0 49 29 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26 I) (V[I>>3]&
f93b0 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a (1<<(I&7)))!=0..
f93c0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
f93d0 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 74 65 6e ne runs an exten
f93e0 73 69 76 65 20 74 65 73 74 20 6f 66 20 74 68 65 sive test of the
f93f0 20 42 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a Bitvec code..**
f9400 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73 .** The input is
f9410 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74 an array of int
f9420 65 67 65 72 73 20 74 68 61 74 20 61 63 74 73 20 egers that acts
f9430 61 73 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 as a program.**
f9440 74 6f 20 74 65 73 74 20 74 68 65 20 42 69 74 76 to test the Bitv
f9450 65 63 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72 ec. The integer
f9460 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f s are opcodes fo
f9470 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20 llowed.** by 0,
f9480 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 6e 64 73 1, or 3 operands
f9490 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 , depending on t
f94a0 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74 he opcode. Anot
f94b0 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f her.** opcode fo
f94c0 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 74 65 6c llows immediatel
f94d0 79 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 y after the last
f94e0 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 operand..**.**
f94f0 54 68 65 72 65 20 61 72 65 20 36 20 6f 70 63 6f There are 6 opco
f9500 64 65 73 20 6e 75 6d 62 65 72 65 64 20 66 72 6f des numbered fro
f9510 6d 20 30 20 74 68 72 6f 75 67 68 20 35 2e 20 20 m 0 through 5.
f9520 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c 0 is the.** "hal
f9530 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 20 63 61 t" opcode and ca
f9540 75 73 65 73 20 74 68 65 20 74 65 73 74 20 74 6f uses the test to
f9550 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 end..**.** 0
f9560 20 20 20 20 20 20 20 20 20 20 48 61 6c 74 20 61 Halt a
f9570 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 nd return the nu
f9580 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a mber of errors.*
f9590 2a 20 20 20 20 31 20 4e 20 53 20 58 20 20 20 20 * 1 N S X
f95a0 53 65 74 20 4e 20 62 69 74 73 20 62 65 67 69 6e Set N bits begin
f95b0 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 ning with S and
f95c0 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 incrementing by
f95d0 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 20 58 20 X.** 2 N S X
f95e0 20 20 20 43 6c 65 61 72 20 4e 20 62 69 74 73 20 Clear N bits
f95f0 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 beginning with S
f9600 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e and incrementin
f9610 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e g by X.** 3 N
f9620 20 20 20 20 20 20 20 20 53 65 74 20 4e 20 72 61 Set N ra
f9630 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 ndomly chosen bi
f9640 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 20 ts.** 4 N
f9650 20 20 20 20 43 6c 65 61 72 20 4e 20 72 61 6e 64 Clear N rand
f9660 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 omly chosen bits
f9670 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 58 20 20 .** 5 N S X
f9680 20 20 53 65 74 20 4e 20 62 69 74 73 20 66 72 6f Set N bits fro
f9690 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 20 m S increment X
f96a0 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e in array only, n
f96b0 6f 74 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a ot in bitvec.**.
f96c0 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 73 20 31 ** The opcodes 1
f96d0 20 74 68 72 6f 75 67 68 20 34 20 70 65 72 66 6f through 4 perfo
f96e0 72 6d 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72 rm set and clear
f96f0 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 operations are
f9700 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20 performed.** on
f9710 62 6f 74 68 20 61 20 42 69 74 76 65 63 20 6f 62 both a Bitvec ob
f9720 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69 ject and on a li
f9730 6e 65 61 72 20 61 72 72 61 79 20 6f 66 20 62 69 near array of bi
f9740 74 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ts obtained from
f9750 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f malloc..** Opco
f9760 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68 de 5 works on th
f9770 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f e linear array o
f9780 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20 nly, not on the
f9790 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64 Bitvec..** Opcod
f97a0 65 20 35 20 69 73 20 75 73 65 64 20 74 6f 20 64 e 5 is used to d
f97b0 65 6c 69 62 65 72 61 74 65 6c 79 20 69 6e 64 75 eliberately indu
f97c0 63 65 20 61 20 66 61 75 6c 74 20 69 6e 20 6f 72 ce a fault in or
f97d0 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72 der to.** confir
f97e0 6d 20 74 68 61 74 20 65 72 72 6f 72 20 64 65 74 m that error det
f97f0 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a ection works..**
f9800 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f 6e 63 6c .** At the concl
f9810 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 73 usion of the tes
f9820 74 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 t the linear arr
f9830 61 79 20 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a ay is compared.*
f9840 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 42 69 * against the Bi
f9850 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 49 66 tvec object. If
f9860 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 64 there are any d
f9870 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61 ifferences,.** a
f9880 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
f9890 6e 65 64 2e 20 20 49 66 20 74 68 65 79 20 61 72 ned. If they ar
f98a0 65 20 74 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f e the same, zero
f98b0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
f98c0 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 .** If a memory
f98d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
f98e0 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 occurs, return
f98f0 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 -1..*/.SQLITE_PR
f9900 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
f9910 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 3BitvecBuiltinTe
f9920 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a st(int sz, int *
f9930 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 63 20 2a aOp){. Bitvec *
f9940 70 42 69 74 76 65 63 20 3d 20 30 3b 0a 20 20 75 pBitvec = 0;. u
f9950 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 56 nsigned char *pV
f9960 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d = 0;. int rc =
f9970 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78 -1;. int i, nx
f9980 2c 20 70 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a 20 , pc, op;.. /*
f9990 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69 74 Allocate the Bit
f99a0 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65 64 vec to be tested
f99b0 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61 72 and a linear ar
f99c0 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 ray of. ** bits
f99d0 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72 to act as the r
f99e0 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42 eference */. pB
f99f0 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33 42 itvec = sqlite3B
f9a00 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a 20 itvecCreate( sz
f9a10 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74 65 );. pV = sqlite
f9a20 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29 3_malloc( (sz+7)
f9a30 2f 38 20 2b 20 31 20 29 3b 0a 20 20 69 66 28 20 /8 + 1 );. if(
f9a40 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 pBitvec==0 || pV
f9a50 3d 3d 30 20 29 20 67 6f 74 6f 20 62 69 74 76 65 ==0 ) goto bitve
f9a60 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28 c_end;. memset(
f9a70 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20 pV, 0, (sz+7)/8
f9a80 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 + 1);.. /* Run
f9a90 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 the program */.
f9aa0 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 pc = 0;. while
f9ab0 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 ( (op = aOp[pc])
f9ac0 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63 !=0 ){. switc
f9ad0 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 h( op ){. c
f9ae0 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73 ase 1:. cas
f9af0 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 e 2:. case
f9b00 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 5: {. nx
f9b10 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d = 4;. i =
f9b20 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a aOp[pc+2] - 1;.
f9b30 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 aOp[pc+2
f9b40 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a ] += aOp[pc+3];.
f9b50 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
f9b60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 }. cas
f9b70 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 e 3:. case
f9b80 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c 4: . defaul
f9b90 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 t: {. nx
f9ba0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c = 2;. sql
f9bb0 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
f9bc0 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a sizeof(i), &i);.
f9bd0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
f9be0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
f9bf0 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 if( (--aOp[pc+1
f9c00 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b ]) > 0 ) nx = 0;
f9c10 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 . pc += nx;.
f9c20 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 66 i = (i & 0x7f
f9c30 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 ffffff)%sz;.
f9c40 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20 if( (op & 1)!=0
f9c50 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 28 ){. SETBIT(
f9c60 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 pV, (i+1));.
f9c70 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 if( op!=5 ){.
f9c80 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
f9c90 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69 74 e3BitvecSet(pBit
f9ca0 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f vec, i+1) ) goto
f9cb0 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 bitvec_end;.
f9cc0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
f9cd0 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 70 CLEARBIT(p
f9ce0 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 V, (i+1));.
f9cf0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c sqlite3BitvecCl
f9d00 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31 ear(pBitvec, i+1
f9d10 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
f9d20 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 /* Test to make
f9d30 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20 sure the linear
f9d40 61 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61 array exactly ma
f9d50 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42 tches the. ** B
f9d60 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53 itvec object. S
f9d70 74 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73 tart with the as
f9d80 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68 sumption that th
f9d90 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 ey do. ** match
f9da0 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67 (rc==0). Chang
f9db0 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f e rc to non-zero
f9dc0 20 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63 if a discrepanc
f9dd0 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e y. ** is found.
f9de0 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c . */. rc = sql
f9df0 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30 ite3BitvecTest(0
f9e00 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74 ,0) + sqlite3Bit
f9e10 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c vecTest(pBitvec,
f9e20 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20 sz+1).
f9e30 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 + sqlite3Bitvec
f9e40 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29 Test(pBitvec, 0)
f9e50 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d ;. for(i=1; i<=
f9e60 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 sz; i++){. if
f9e70 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69 ( (TESTBIT(pV,i
f9e80 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65 ))!=sqlite3Bitve
f9e90 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29 cTest(pBitvec,i)
f9ea0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 ){. rc = i
f9eb0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
f9ec0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 }. }.. /* F
f9ed0 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 ree allocated st
f9ee0 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 ructure */.bitve
f9ef0 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 c_end:. sqlite3
f9f00 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 6c _free(pV);. sql
f9f10 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
f9f20 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 65 y(pBitvec);. re
f9f30 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
f9f40 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
f9f50 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f _BUILTIN_TEST */
f9f60 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
f9f70 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 2e * End of bitvec.
f9f80 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
f9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9fb0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
f9fc0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 61 * Begin file pca
f9fd0 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a che.c **********
f9fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa000 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 /./*.** 2008 Aug
fa010 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 05.**.** The
fa020 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
fa030 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
fa040 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
fa050 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
fa060 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
fa070 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
fa080 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
fa090 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
fa0a0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
fa0b0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
fa0c0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
fa0d0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
fa0e0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
fa0f0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
fa100 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
fa110 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
fa120 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
fa130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa170 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
fa180 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 le implements th
fa190 61 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a 2a at page cache..*
fa1a0 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 *.** @(#) $Id: p
fa1b0 63 61 63 68 65 2e 63 2c 76 20 31 2e 34 34 20 32 cache.c,v 1.44 2
fa1c0 30 30 39 2f 30 33 2f 33 31 20 30 31 3a 33 32 3a 009/03/31 01:32:
fa1d0 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 18 drh Exp $.*/.
fa1e0 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 ./*.** A complet
fa1f0 65 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 e page cache is
fa200 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
fa210 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a his structure..*
fa220 2f 0a 73 74 72 75 63 74 20 50 43 61 63 68 65 20 /.struct PCache
fa230 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 {. PgHdr *pDirt
fa240 79 2c 20 2a 70 44 69 72 74 79 54 61 69 6c 3b 20 y, *pDirtyTail;
fa250 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
fa260 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 69 of dirty pages i
fa270 6e 20 4c 52 55 20 6f 72 64 65 72 20 2a 2f 0a 20 n LRU order */.
fa280 20 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 3b PgHdr *pSynced;
fa290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fa2a0 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e /* Last syn
fa2b0 63 65 64 20 70 61 67 65 20 69 6e 20 64 69 72 74 ced page in dirt
fa2c0 79 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 y page list */.
fa2d0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
fa2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fa2f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
fa300 66 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 f referenced pag
fa310 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 es */. int nMax
fa320 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
fa330 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
fa340 6f 6e 66 69 67 75 72 65 64 20 63 61 63 68 65 20 onfigured cache
fa350 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a size */. int sz
fa360 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
fa370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
fa380 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 Size of every p
fa390 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 63 68 age in this cach
fa3a0 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 e */. int szExt
fa3b0 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ra;
fa3c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
fa3d0 7a 65 20 6f 66 20 65 78 74 72 61 20 73 70 61 63 ze of extra spac
fa3e0 65 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 e for each page
fa3f0 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 */. int bPurgea
fa400 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ble;
fa410 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
fa420 20 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e if pages are on
fa430 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a backing store *
fa440 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 /. int (*xStres
fa450 73 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 s)(void*,PgHdr*)
fa460 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 ; /* Call
fa470 74 6f 20 74 72 79 20 6d 61 6b 65 20 61 20 70 61 to try make a pa
fa480 67 65 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f ge clean */. vo
fa490 69 64 20 2a 70 53 74 72 65 73 73 3b 20 20 20 20 id *pStress;
fa4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fa4b0 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
fa4c0 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 73 71 xStress */. sq
fa4d0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 43 lite3_pcache *pC
fa4e0 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 ache;
fa4f0 20 20 2f 2a 20 50 6c 75 67 67 61 62 6c 65 20 63 /* Pluggable c
fa500 61 63 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 ache module */.
fa510 20 50 67 48 64 72 20 2a 70 50 61 67 65 31 3b 20 PgHdr *pPage1;
fa520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fa530 20 20 20 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 /* Referenc
fa540 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 7d e to page 1 */.}
fa550 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 ;../*.** Some of
fa560 20 74 68 65 20 61 73 73 65 72 74 28 29 20 6d 61 the assert() ma
fa570 63 72 6f 73 20 69 6e 20 74 68 69 73 20 63 6f 64 cros in this cod
fa580 65 20 61 72 65 20 74 6f 6f 20 65 78 70 65 6e 73 e are too expens
fa590 69 76 65 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 76 ive to run.** ev
fa5a0 65 6e 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c en during normal
fa5b0 20 64 65 62 75 67 67 69 6e 67 2e 20 20 55 73 65 debugging. Use
fa5c0 20 74 68 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c them only rarel
fa5d0 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e y on long-runnin
fa5e0 67 0a 2a 2a 20 74 65 73 74 73 2e 20 20 45 6e 61 g.** tests. Ena
fa5f0 62 6c 65 20 74 68 65 20 65 78 70 65 6e 73 69 76 ble the expensiv
fa600 65 20 61 73 73 65 72 74 73 20 75 73 69 6e 67 20 e asserts using
fa610 74 68 65 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f the.** -DSQLITE_
fa620 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 ENABLE_EXPENSIVE
fa630 5f 41 53 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c _ASSERT=1 compil
fa640 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a e-time option..*
fa650 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
fa660 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 ENABLE_EXPENSIVE
fa670 5f 41 53 53 45 52 54 0a 23 20 64 65 66 69 6e 65 _ASSERT.# define
fa680 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 expensive_asser
fa690 74 28 58 29 20 20 61 73 73 65 72 74 28 58 29 0a t(X) assert(X).
fa6a0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 65 #else.# define e
fa6b0 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 xpensive_assert(
fa6c0 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a X).#endif../****
fa6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c ************** L
fa6f0 69 6e 6b 65 64 20 4c 69 73 74 20 4d 61 6e 61 67 inked List Manag
fa700 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ement **********
fa710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 **********/..#if
fa720 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
fa730 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
fa740 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
fa750 53 49 56 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a SIVE_ASSERT)./*.
fa760 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 ** Check that th
fa770 65 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 e pCache->pSynce
fa780 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 d variable is se
fa790 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 49 66 20 t correctly. If
fa7a0 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 65 69 it.** is not, ei
fa7b0 74 68 65 72 20 66 61 69 6c 20 61 6e 20 61 73 73 ther fail an ass
fa7c0 65 72 74 20 6f 72 20 72 65 74 75 72 6e 20 7a 65 ert or return ze
fa7d0 72 6f 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 ro. Otherwise, r
fa7e0 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 eturn.** non-zer
fa7f0 6f 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 o. This is only
fa800 75 73 65 64 20 69 6e 20 64 65 62 75 67 67 69 6e used in debuggin
fa810 67 20 62 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c g builds, as fol
fa820 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 65 78 lows:.**.** ex
fa830 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 pensive_assert(
fa840 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 pcacheCheckSynce
fa850 64 28 70 43 61 63 68 65 29 20 29 3b 0a 2a 2f 0a d(pCache) );.*/.
fa860 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 static int pcach
fa870 65 43 68 65 63 6b 53 79 6e 63 65 64 28 50 43 61 eCheckSynced(PCa
fa880 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
fa890 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 PgHdr *p;. for(
fa8a0 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 p=pCache->pDirty
fa8b0 54 61 69 6c 3b 20 70 21 3d 70 43 61 63 68 65 2d Tail; p!=pCache-
fa8c0 3e 70 53 79 6e 63 65 64 3b 20 70 3d 70 2d 3e 70 >pSynced; p=p->p
fa8d0 44 69 72 74 79 50 72 65 76 29 7b 0a 20 20 20 20 DirtyPrev){.
fa8e0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 20 assert( p->nRef
fa8f0 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 || (p->flags&PGH
fa900 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 3b DR_NEED_SYNC) );
fa910 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 . }. return (p
fa920 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66 20 7c ==0 || p->nRef |
fa930 7c 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 | (p->flags&PGHD
fa940 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d 30 29 R_NEED_SYNC)==0)
fa950 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e ;.}.#endif /* !N
fa960 44 45 42 55 47 20 26 26 20 53 51 4c 49 54 45 5f DEBUG && SQLITE_
fa970 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 ENABLE_EXPENSIVE
fa980 5f 41 53 53 45 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a _ASSERT */../*.*
fa990 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 70 50 * Remove page pP
fa9a0 61 67 65 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 age from the lis
fa9b0 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 t of dirty pages
fa9c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
fa9d0 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f pcacheRemoveFro
fa9e0 6d 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 mDirtyList(PgHdr
fa9f0 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 *pPage){. PCac
faa00 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 he *p = pPage->p
faa10 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 Cache;.. assert
faa20 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e ( pPage->pDirtyN
faa30 65 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d ext || pPage==p-
faa40 3e 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 >pDirtyTail );.
faa50 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
faa60 70 44 69 72 74 79 50 72 65 76 20 7c 7c 20 70 50 pDirtyPrev || pP
faa70 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 age==p->pDirty )
faa80 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 ;.. /* Update t
faa90 68 65 20 50 43 61 63 68 65 31 2e 70 53 79 6e 63 he PCache1.pSync
faaa0 65 64 20 76 61 72 69 61 62 6c 65 20 69 66 20 6e ed variable if n
faab0 65 63 65 73 73 61 72 79 2e 20 2a 2f 0a 20 20 69 ecessary. */. i
faac0 66 28 20 70 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 f( p->pSynced==p
faad0 50 61 67 65 20 29 7b 0a 20 20 20 20 50 67 48 64 Page ){. PgHd
faae0 72 20 2a 70 53 79 6e 63 65 64 20 3d 20 70 50 61 r *pSynced = pPa
faaf0 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a ge->pDirtyPrev;.
fab00 20 20 20 20 77 68 69 6c 65 28 20 70 53 79 6e 63 while( pSync
fab10 65 64 20 26 26 20 28 70 53 79 6e 63 65 64 2d 3e ed && (pSynced->
fab20 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
fab30 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 _SYNC) ){.
fab40 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 pSynced = pSynce
fab50 64 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 d->pDirtyPrev;.
fab60 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 53 79 6e }. p->pSyn
fab70 63 65 64 20 3d 20 70 53 79 6e 63 65 64 3b 0a 20 ced = pSynced;.
fab80 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d }.. if( pPage-
fab90 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 >pDirtyNext ){.
faba0 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 pPage->pDirty
fabb0 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 Next->pDirtyPrev
fabc0 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 = pPage->pDirty
fabd0 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Prev;. }else{.
fabe0 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
fabf0 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 ==p->pDirtyTail
fac00 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 );. p->pDirty
fac10 54 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44 Tail = pPage->pD
fac20 69 72 74 79 50 72 65 76 3b 0a 20 20 7d 0a 20 20 irtyPrev;. }.
fac30 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 if( pPage->pDirt
fac40 79 50 72 65 76 20 29 7b 0a 20 20 20 20 70 50 61 yPrev ){. pPa
fac50 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 2d 3e ge->pDirtyPrev->
fac60 70 44 69 72 74 79 4e 65 78 74 20 3d 20 70 50 61 pDirtyNext = pPa
fac70 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a ge->pDirtyNext;.
fac80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
fac90 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 ert( pPage==p->p
faca0 44 69 72 74 79 20 29 3b 0a 20 20 20 20 70 2d 3e Dirty );. p->
facb0 70 44 69 72 74 79 20 3d 20 70 50 61 67 65 2d 3e pDirty = pPage->
facc0 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a pDirtyNext;. }.
facd0 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e pPage->pDirtyN
face0 65 78 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 ext = 0;. pPage
facf0 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 30 ->pDirtyPrev = 0
fad00 3b 0a 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 ;.. expensive_a
fad10 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 ssert( pcacheChe
fad20 63 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d ckSynced(p) );.}
fad30 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 70 61 67 65 ../*.** Add page
fad40 20 70 50 61 67 65 20 74 6f 20 74 68 65 20 68 65 pPage to the he
fad50 61 64 20 6f 66 20 74 68 65 20 64 69 72 74 79 20 ad of the dirty
fad60 6c 69 73 74 20 28 50 43 61 63 68 65 31 2e 70 44 list (PCache1.pD
fad70 69 72 74 79 20 69 73 20 73 65 74 20 74 6f 0a 2a irty is set to.*
fad80 2a 20 70 50 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 * pPage)..*/.sta
fad90 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 41 tic void pcacheA
fada0 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28 50 67 ddToDirtyList(Pg
fadb0 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 Hdr *pPage){. P
fadc0 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 65 Cache *p = pPage
fadd0 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 ->pCache;.. ass
fade0 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 ert( pPage->pDir
fadf0 74 79 4e 65 78 74 3d 3d 30 20 26 26 20 70 50 61 tyNext==0 && pPa
fae00 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d ge->pDirtyPrev==
fae10 30 20 26 26 20 70 2d 3e 70 44 69 72 74 79 21 3d 0 && p->pDirty!=
fae20 70 50 61 67 65 20 29 3b 0a 0a 20 20 70 50 61 67 pPage );.. pPag
fae30 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 e->pDirtyNext =
fae40 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20 69 66 28 p->pDirty;. if(
fae50 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 pPage->pDirtyNe
fae60 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 xt ){. assert
fae70 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e ( pPage->pDirtyN
fae80 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 3d ext->pDirtyPrev=
fae90 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d =0 );. pPage-
faea0 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 >pDirtyNext->pDi
faeb0 72 74 79 50 72 65 76 20 3d 20 70 50 61 67 65 3b rtyPrev = pPage;
faec0 0a 20 20 7d 0a 20 20 70 2d 3e 70 44 69 72 74 79 . }. p->pDirty
faed0 20 3d 20 70 50 61 67 65 3b 0a 20 20 69 66 28 20 = pPage;. if(
faee0 21 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 !p->pDirtyTail )
faef0 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 {. p->pDirtyT
faf00 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d ail = pPage;. }
faf10 0a 20 20 69 66 28 20 21 70 2d 3e 70 53 79 6e 63 . if( !p->pSync
faf20 65 64 20 26 26 20 30 3d 3d 28 70 50 61 67 65 2d ed && 0==(pPage-
faf30 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
faf40 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 D_SYNC) ){. p
faf50 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67 ->pSynced = pPag
faf60 65 3b 0a 20 20 7d 0a 20 20 65 78 70 65 6e 73 69 e;. }. expensi
faf70 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 ve_assert( pcach
faf80 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20 eCheckSynced(p)
faf90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 );.}../*.** Wrap
fafa0 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 70 per around the p
fafb0 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 73 20 luggable caches
fafc0 78 55 6e 70 69 6e 20 6d 65 74 68 6f 64 2e 20 49 xUnpin method. I
fafd0 66 20 74 68 65 20 63 61 63 68 65 20 69 73 0a 2a f the cache is.*
fafe0 2a 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72 * being used for
faff0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
fb000 74 61 62 61 73 65 2c 20 74 68 69 73 20 66 75 6e tabase, this fun
fb010 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
fb020 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
fb030 20 70 63 61 63 68 65 55 6e 70 69 6e 28 50 67 48 pcacheUnpin(PgH
fb040 64 72 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 dr *p){. PCache
fb050 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 *pCache = p->pC
fb060 61 63 68 65 3b 0a 20 20 69 66 28 20 70 43 61 63 ache;. if( pCac
fb070 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 he->bPurgeable )
fb080 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e {. if( p->pgn
fb090 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 o==1 ){. pC
fb0a0 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 ache->pPage1 = 0
fb0b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
fb0c0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
fb0d0 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 pcache.xUnpin(pC
fb0e0 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c ache->pCache, p,
fb0f0 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 0);. }.}../***
fb100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fb110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fb120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fb130 20 47 65 6e 65 72 61 6c 20 49 6e 74 65 72 66 61 General Interfa
fb140 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a ces ******.**.**
fb150 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
fb160 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 shutdown the pag
fb170 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 e cache subsyste
fb180 6d 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 m. Neither of th
fb190 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ese .** function
fb1a0 73 20 61 72 65 20 74 68 72 65 61 64 73 61 66 65 s are threadsafe
fb1b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
fb1c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
fb1d0 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 cacheInitialize(
fb1e0 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c void){. if( sql
fb1f0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
fb200 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30 .pcache.xInit==0
fb210 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 ){. sqlite3P
fb220 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 CacheSetDefault(
fb230 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
fb240 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
fb250 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 fig.pcache.xInit
fb260 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
fb270 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67 nfig.pcache.pArg
fb280 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
fb290 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
fb2a0 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 PcacheShutdown(v
fb2b0 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 oid){. if( sqli
fb2c0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
fb2d0 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e pcache.xShutdown
fb2e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ){. sqlite3G
fb2f0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
fb300 68 65 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c he.xShutdown(sql
fb310 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
fb320 2e 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 20 .pcache.pArg);.
fb330 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
fb340 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 rn the size in b
fb350 79 74 65 73 20 6f 66 20 61 20 50 43 61 63 68 65 ytes of a PCache
fb360 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 object..*/.SQLI
fb370 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
fb380 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 qlite3PcacheSize
fb390 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 (void){ return s
fb3a0 69 7a 65 6f 66 28 50 43 61 63 68 65 29 3b 20 7d izeof(PCache); }
fb3b0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
fb3c0 20 6e 65 77 20 50 43 61 63 68 65 20 6f 62 6a 65 new PCache obje
fb3d0 63 74 2e 20 53 74 6f 72 61 67 65 20 73 70 61 63 ct. Storage spac
fb3e0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 e to hold the ob
fb3f0 6a 65 63 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 ject.** has alre
fb400 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 ady been allocat
fb410 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 ed and is passed
fb420 20 69 6e 20 61 73 20 74 68 65 20 70 20 70 6f 69 in as the p poi
fb430 6e 74 65 72 2e 20 0a 2a 2a 20 54 68 65 20 63 61 nter. .** The ca
fb440 6c 6c 65 72 20 64 69 73 63 6f 76 65 72 73 20 68 ller discovers h
fb450 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 6e 65 ow much space ne
fb460 65 64 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 eds to be alloca
fb470 74 65 64 20 62 79 20 0a 2a 2a 20 63 61 6c 6c 69 ted by .** calli
fb480 6e 67 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 ng sqlite3Pcache
fb490 53 69 7a 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 Size()..*/.SQLIT
fb4a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
fb4b0 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e qlite3PcacheOpen
fb4c0 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 (. int szPage,
fb4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fb4e0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 /* Size of ever
fb4f0 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 y page */. int
fb500 73 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 szExtra,
fb510 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
fb520 61 20 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 a space associat
fb530 65 64 20 77 69 74 68 20 65 61 63 68 20 70 61 67 ed with each pag
fb540 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 e */. int bPurg
fb550 65 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 eable,
fb560 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
fb570 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b ages are on back
fb580 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 ing store */. i
fb590 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f nt (*xStress)(vo
fb5a0 69 64 2a 2c 50 67 48 64 72 2a 29 2c 2f 2a 20 43 id*,PgHdr*),/* C
fb5b0 61 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 all to try to ma
fb5c0 6b 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a ke pages clean *
fb5d0 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 /. void *pStres
fb5e0 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s,
fb5f0 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 /* Argument to
fb600 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 xStress */. PCa
fb610 63 68 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 che *p
fb620 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 /* Pre
fb630 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 allocated space
fb640 66 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a for the PCache *
fb650 2f 0a 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c /.){. memset(p,
fb660 20 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 0, sizeof(PCach
fb670 65 29 29 3b 0a 20 20 70 2d 3e 73 7a 50 61 67 65 e));. p->szPage
fb680 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 70 2d 3e = szPage;. p->
fb690 73 7a 45 78 74 72 61 20 3d 20 73 7a 45 78 74 72 szExtra = szExtr
fb6a0 61 3b 0a 20 20 70 2d 3e 62 50 75 72 67 65 61 62 a;. p->bPurgeab
fb6b0 6c 65 20 3d 20 62 50 75 72 67 65 61 62 6c 65 3b le = bPurgeable;
fb6c0 0a 20 20 70 2d 3e 78 53 74 72 65 73 73 20 3d 20 . p->xStress =
fb6d0 78 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53 xStress;. p->pS
fb6e0 74 72 65 73 73 20 3d 20 70 53 74 72 65 73 73 3b tress = pStress;
fb6f0 0a 20 20 70 2d 3e 6e 4d 61 78 20 3d 20 31 30 30 . p->nMax = 100
fb700 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
fb710 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 e the page size
fb720 66 6f 72 20 50 43 61 63 68 65 20 6f 62 6a 65 63 for PCache objec
fb730 74 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 t. The caller mu
fb740 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 st ensure that t
fb750 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6f here.** are no o
fb760 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 utstanding page
fb770 72 65 66 65 72 65 6e 63 65 73 20 77 68 65 6e 20 references when
fb780 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
fb790 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 called..*/.SQLI
fb7a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
fb7b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 sqlite3PcacheSet
fb7c0 50 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 20 PageSize(PCache
fb7d0 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 73 7a 50 *pCache, int szP
fb7e0 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 age){. assert(
fb7f0 70 43 61 63 68 65 2d 3e 6e 52 65 66 3d 3d 30 20 pCache->nRef==0
fb800 26 26 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 && pCache->pDirt
fb810 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 y==0 );. if( pC
fb820 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a ache->pCache ){.
fb830 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
fb840 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 lConfig.pcache.x
fb850 44 65 73 74 72 6f 79 28 70 43 61 63 68 65 2d 3e Destroy(pCache->
fb860 70 43 61 63 68 65 29 3b 0a 20 20 20 20 70 43 61 pCache);. pCa
fb870 63 68 65 2d 3e 70 43 61 63 68 65 20 3d 20 30 3b che->pCache = 0;
fb880 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 2d 3e 73 . }. pCache->s
fb890 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a zPage = szPage;.
fb8a0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 }../*.** Try to
fb8b0 6f 62 74 61 69 6e 20 61 20 70 61 67 65 20 66 72 obtain a page fr
fb8c0 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f om the cache..*/
fb8d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
fb8e0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
fb8f0 65 46 65 74 63 68 28 0a 20 20 50 43 61 63 68 65 eFetch(. PCache
fb900 20 2a 70 43 61 63 68 65 2c 20 20 20 20 20 20 20 *pCache,
fb910 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 /* Obtain the pa
fb920 67 65 20 66 72 6f 6d 20 74 68 69 73 20 63 61 63 ge from this cac
fb930 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e he */. Pgno pgn
fb940 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a o, /*
fb950 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 Page number to
fb960 6f 62 74 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 obtain */. int
fb970 63 72 65 61 74 65 46 6c 61 67 2c 20 20 20 20 20 createFlag,
fb980 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 63 72 /* If true, cr
fb990 65 61 74 65 20 70 61 67 65 20 69 66 20 69 74 20 eate page if it
fb9a0 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 61 does not exist a
fb9b0 6c 72 65 61 64 79 20 2a 2f 0a 20 20 50 67 48 64 lready */. PgHd
fb9c0 72 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 20 r **ppPage
fb9d0 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 /* Write the p
fb9e0 61 67 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 age here */.){.
fb9f0 20 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 PgHdr *pPage =
fba00 30 3b 0a 20 20 69 6e 74 20 65 43 72 65 61 74 65 0;. int eCreate
fba10 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 ;.. assert( pCa
fba20 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 che!=0 );. asse
fba30 72 74 28 20 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20 rt( pgno>0 );..
fba40 20 2f 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67 /* If the plugg
fba50 61 62 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69 able cache (sqli
fba60 74 65 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73 te3_pcache*) has
fba70 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 not been alloca
fba80 74 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 ted,. ** alloca
fba90 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a te it now.. */.
fbaa0 20 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e 70 if( !pCache->p
fbab0 43 61 63 68 65 20 26 26 20 63 72 65 61 74 65 46 Cache && createF
fbac0 6c 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 lag ){. sqlit
fbad0 65 33 5f 70 63 61 63 68 65 20 2a 70 3b 0a 20 20 e3_pcache *p;.
fbae0 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 int nByte;.
fbaf0 20 6e 42 79 74 65 20 3d 20 70 43 61 63 68 65 2d nByte = pCache-
fbb00 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 63 68 65 >szPage + pCache
fbb10 2d 3e 73 7a 45 78 74 72 61 20 2b 20 73 69 7a 65 ->szExtra + size
fbb20 6f 66 28 50 67 48 64 72 29 3b 0a 20 20 20 20 70 of(PgHdr);. p
fbb30 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
fbb40 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43 Config.pcache.xC
fbb50 72 65 61 74 65 28 6e 42 79 74 65 2c 20 70 43 61 reate(nByte, pCa
fbb60 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29 che->bPurgeable)
fbb70 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a ;. if( !p ){.
fbb80 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
fbb90 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
fbba0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
fbbb0 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
fbbc0 78 43 61 63 68 65 73 69 7a 65 28 70 2c 20 70 43 xCachesize(p, pC
fbbd0 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 ache->nMax);.
fbbe0 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
fbbf0 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 43 72 65 = p;. }.. eCre
fbc00 61 74 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67 ate = createFlag
fbc10 20 3f 20 31 20 3a 20 30 3b 0a 20 20 69 66 28 20 ? 1 : 0;. if(
fbc20 65 43 72 65 61 74 65 20 26 26 20 28 21 70 43 61 eCreate && (!pCa
fbc30 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 che->bPurgeable
fbc40 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 44 69 72 || !pCache->pDir
fbc50 74 79 29 20 29 7b 0a 20 20 20 20 65 43 72 65 61 ty) ){. eCrea
fbc60 74 65 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 te = 2;. }. if
fbc70 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ( pCache->pCache
fbc80 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 ){. pPage =
fbc90 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
fbca0 66 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 fig.pcache.xFetc
fbcb0 68 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 h(pCache->pCache
fbcc0 2c 20 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 , pgno, eCreate)
fbcd0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 ;. }.. if( !pP
fbce0 61 67 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d age && eCreate==
fbcf0 31 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 1 ){. PgHdr *
fbd00 70 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e pPg;.. /* Fin
fbd10 64 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74 d a dirty page t
fbd20 6f 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 o write-out and
fbd30 72 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 recycle. First t
fbd40 72 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 ry to find a .
fbd50 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 ** page that d
fbd60 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 oes not require
fbd70 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 a journal-sync (
fbd80 6f 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e one with PGHDR_N
fbd90 45 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 EED_SYNC. **
fbda0 63 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 cleared), but if
fbdb0 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 that is not pos
fbdc0 73 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 sible settle for
fbdd0 20 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 any other .
fbde0 2a 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 ** unreferenced
fbdf0 64 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 dirty page..
fbe00 2a 2f 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65 */. expensive
fbe10 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 _assert( pcacheC
fbe20 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 heckSynced(pCach
fbe30 65 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 e) );. for(pP
fbe40 67 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 g=pCache->pSynce
fbe50 64 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 20 d; . pPg
fbe60 26 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c && (pPg->nRef ||
fbe70 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 (pPg->flags&PGH
fbe80 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 DR_NEED_SYNC));
fbe90 0a 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67 . pPg=pPg
fbea0 2d 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 20 ->pDirtyPrev.
fbeb0 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 67 );. if( !pPg
fbec0 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 50 ){. for(pP
fbed0 67 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 g=pCache->pDirty
fbee0 54 61 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67 Tail; pPg && pPg
fbef0 2d 3e 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d ->nRef; pPg=pPg-
fbf00 3e 70 44 69 72 74 79 50 72 65 76 29 3b 0a 20 20 >pDirtyPrev);.
fbf10 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 67 20 }. if( pPg
fbf20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b ){. int rc;
fbf30 0a 20 20 20 20 20 20 72 63 20 3d 20 70 43 61 63 . rc = pCac
fbf40 68 65 2d 3e 78 53 74 72 65 73 73 28 70 43 61 63 he->xStress(pCac
fbf50 68 65 2d 3e 70 53 74 72 65 73 73 2c 20 70 50 67 he->pStress, pPg
fbf60 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
fbf70 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 =SQLITE_OK && rc
fbf80 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b !=SQLITE_BUSY ){
fbf90 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
fbfa0 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
fbfb0 7d 0a 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 }.. pPage = s
fbfc0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
fbfd0 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 ig.pcache.xFetch
fbfe0 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
fbff0 20 70 67 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a pgno, 2);. }..
fc000 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 if( pPage ){.
fc010 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70 if( !pPage->p
fc020 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 Data ){. me
fc030 6d 73 65 74 28 70 50 61 67 65 2c 20 30 2c 20 73 mset(pPage, 0, s
fc040 69 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 izeof(PgHdr) + p
fc050 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b Cache->szExtra);
fc060 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 45 . pPage->pE
fc070 78 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 xtra = (void*)&p
fc080 50 61 67 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 Page[1];. p
fc090 50 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 Page->pData = (v
fc0a0 6f 69 64 20 2a 29 26 28 28 63 68 61 72 20 2a 29 oid *)&((char *)
fc0b0 70 50 61 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 pPage)[sizeof(Pg
fc0c0 48 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 Hdr) + pCache->s
fc0d0 7a 45 78 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 zExtra];. p
fc0e0 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 Page->pCache = p
fc0f0 43 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50 61 Cache;. pPa
fc100 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b ge->pgno = pgno;
fc110 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
fc120 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 t( pPage->pCache
fc130 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 20 20 ==pCache );.
fc140 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
fc150 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 gno==pgno );.
fc160 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
fc170 70 45 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 pExtra==(void *)
fc180 26 70 50 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 &pPage[1] );..
fc190 20 20 69 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e if( 0==pPage->
fc1a0 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 70 43 nRef ){. pC
fc1b0 61 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 ache->nRef++;.
fc1c0 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e }. pPage->n
fc1d0 52 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 Ref++;. if( p
fc1e0 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 gno==1 ){.
fc1f0 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d pCache->pPage1 =
fc200 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 pPage;. }.
fc210 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 }. *ppPage = pP
fc220 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 age;. return (p
fc230 50 61 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61 Page==0 && eCrea
fc240 74 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d te) ? SQLITE_NOM
fc250 45 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a EM : SQLITE_OK;.
fc260 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 }../*.** Decreme
fc270 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 nt the reference
fc280 20 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 count on a page
fc290 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 . If the page is
fc2a0 20 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a clean and the.*
fc2b0 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e * reference coun
fc2c0 74 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 t drops to 0, th
fc2d0 65 6e 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c en it is made el
fc2e0 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 ible for recycli
fc2f0 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ng..*/.SQLITE_PR
fc300 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
fc310 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 e3PcacheRelease(
fc320 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 PgHdr *p){. ass
fc330 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 ert( p->nRef>0 )
fc340 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 ;. p->nRef--;.
fc350 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 if( p->nRef==0
fc360 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 ){. PCache *p
fc370 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 Cache = p->pCach
fc380 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e e;. pCache->n
fc390 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 Ref--;. if( (
fc3a0 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 p->flags&PGHDR_D
fc3b0 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 IRTY)==0 ){.
fc3c0 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 pcacheUnpin(p)
fc3d0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
fc3e0 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 /* Move the p
fc3f0 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 age to the head
fc400 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 of the dirty lis
fc410 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 t. */. pcac
fc420 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
fc430 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 yList(p);.
fc440 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 pcacheAddToDirty
fc450 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 List(p);. }.
fc460 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 }.}../*.** Incr
fc470 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e ease the referen
fc480 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 ce count of a su
fc490 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31 pplied page by 1
fc4a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
fc4b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
fc4c0 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20 PcacheRef(PgHdr
fc4d0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d *p){. assert(p-
fc4e0 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e >nRef>0);. p->n
fc4f0 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Ref++;.}../*.**
fc500 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d Drop a page from
fc510 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72 the cache. Ther
fc520 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c e must be exactl
fc530 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 y one reference
fc540 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 to the.** page.
fc550 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 This function de
fc560 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72 letes that refer
fc570 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69 ence, so after i
fc580 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a t returns the.**
fc590 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f page pointed to
fc5a0 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64 by p is invalid
fc5b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
fc5c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
fc5d0 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 PcacheDrop(PgHdr
fc5e0 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a *p){. PCache *
fc5f0 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 pCache;. assert
fc600 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a ( p->nRef==1 );.
fc610 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 if( p->flags&P
fc620 47 48 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 GHDR_DIRTY ){.
fc630 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 pcacheRemoveFr
fc640 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a omDirtyList(p);.
fc650 20 20 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70 }. pCache = p
fc660 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63 ->pCache;. pCac
fc670 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 he->nRef--;. if
fc680 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a ( p->pgno==1 ){.
fc690 20 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 pCache->pPag
fc6a0 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 e1 = 0;. }. sq
fc6b0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
fc6c0 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 g.pcache.xUnpin(
fc6d0 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
fc6e0 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 p, 1);.}../*.**
fc6f0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 Make sure the pa
fc700 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 ge is marked as
fc710 64 69 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e dirty. If it isn
fc720 27 74 20 64 69 72 74 79 20 61 6c 72 65 61 64 79 't dirty already
fc730 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e ,.** make it so.
fc740 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
fc750 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
fc760 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 cacheMakeDirty(P
fc770 67 48 64 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 gHdr *p){. p->f
fc780 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 lags &= ~PGHDR_D
fc790 4f 4e 54 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 ONT_WRITE;. ass
fc7a0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 ert( p->nRef>0 )
fc7b0 3b 0a 20 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 ;. if( 0==(p->f
fc7c0 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 lags & PGHDR_DIR
fc7d0 54 59 29 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c TY) ){. p->fl
fc7e0 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 ags |= PGHDR_DIR
fc7f0 54 59 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64 TY;. pcacheAd
fc800 64 54 6f 44 69 72 74 79 4c 69 73 74 28 20 70 29 dToDirtyList( p)
fc810 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d ;. }.}../*.** M
fc820 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67 ake sure the pag
fc830 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63 e is marked as c
fc840 6c 65 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 lean. If it isn'
fc850 74 20 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c t clean already,
fc860 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a .** make it so..
fc870 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
fc880 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
fc890 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 acheMakeClean(Pg
fc8a0 48 64 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 Hdr *p){. if( (
fc8b0 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 p->flags & PGHDR
fc8c0 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 _DIRTY) ){. p
fc8d0 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 cacheRemoveFromD
fc8e0 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 irtyList(p);.
fc8f0 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 p->flags &= ~(P
fc900 47 48 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52 GHDR_DIRTY|PGHDR
fc910 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 _NEED_SYNC);.
fc920 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 if( p->nRef==0
fc930 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 ){. pcacheU
fc940 6e 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 npin(p);. }.
fc950 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 }.}../*.** Make
fc960 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 every page in t
fc970 68 65 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a he cache clean..
fc980 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
fc990 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
fc9a0 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 acheCleanAll(PCa
fc9b0 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
fc9c0 50 67 48 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c PgHdr *p;. whil
fc9d0 65 28 20 28 70 20 3d 20 70 43 61 63 68 65 2d 3e e( (p = pCache->
fc9e0 70 44 69 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 pDirty)!=0 ){.
fc9f0 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
fca00 61 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d akeClean(p);. }
fca10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
fca20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 the PGHDR_NEED_S
fca30 59 4e 43 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c YNC flag from al
fca40 6c 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a l dirty pages..*
fca50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
fca60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
fca70 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 cheClearSyncFlag
fca80 73 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 s(PCache *pCache
fca90 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 ){. PgHdr *p;.
fcaa0 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 for(p=pCache->p
fcab0 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 Dirty; p; p=p->p
fcac0 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 DirtyNext){.
fcad0 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 p->flags &= ~PGH
fcae0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
fcaf0 7d 0a 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e }. pCache->pSyn
fcb00 63 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 ced = pCache->pD
fcb10 69 72 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a irtyTail;.}../*.
fcb20 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 ** Change the pa
fcb30 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 ge number of pag
fcb40 65 20 70 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 e p to newPgno.
fcb50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
fcb60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
fcb70 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 20 cacheMove(PgHdr
fcb80 2a 70 2c 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f *p, Pgno newPgno
fcb90 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 ){. PCache *pCa
fcba0 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
fcbb0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 . assert( p->nR
fcbc0 65 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 ef>0 );. assert
fcbd0 28 20 6e 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 ( newPgno>0 );.
fcbe0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
fcbf0 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 52 65 6b nfig.pcache.xRek
fcc00 65 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 ey(pCache->pCach
fcc10 65 2c 20 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e e, p, p->pgno, n
fcc20 65 77 50 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 ewPgno);. p->pg
fcc30 6e 6f 20 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 no = newPgno;.
fcc40 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 if( (p->flags&PG
fcc50 48 44 52 5f 44 49 52 54 59 29 20 26 26 20 28 70 HDR_DIRTY) && (p
fcc60 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
fcc70 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 ED_SYNC) ){.
fcc80 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d pcacheRemoveFrom
fcc90 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 DirtyList(p);.
fcca0 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 pcacheAddToDir
fccb0 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d tyList(p);. }.}
fccc0 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 ../*.** Drop eve
fccd0 72 79 20 63 61 63 68 65 20 65 6e 74 72 79 20 77 ry cache entry w
fcce0 68 6f 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 hose page number
fccf0 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
fcd00 20 22 70 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 "pgno". The.**
fcd10 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 caller must ensu
fcd20 72 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72 re that there ar
fcd30 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 e no outstanding
fcd40 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 references to a
fcd50 6e 79 20 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 ny pages.** othe
fcd60 72 20 74 68 61 6e 20 70 61 67 65 20 31 20 77 69 r than page 1 wi
fcd70 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 th a page number
fcd80 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 67 greater than pg
fcd90 6e 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 no..**.** If the
fcda0 72 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 re is a referenc
fcdb0 65 20 74 6f 20 70 61 67 65 20 31 20 61 6e 64 20 e to page 1 and
fcdc0 74 68 65 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 the pgno paramet
fcdd0 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 er passed to thi
fcde0 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 s.** function is
fcdf0 20 30 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 0, then the dat
fce00 61 20 61 72 65 61 20 61 73 73 6f 63 69 61 74 65 a area associate
fce10 64 20 77 69 74 68 20 70 61 67 65 20 31 20 69 73 d with page 1 is
fce20 20 7a 65 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 zeroed, but.**
fce30 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 the page object
fce40 69 73 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a is not dropped..
fce50 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
fce60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
fce70 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 acheTruncate(PCa
fce80 63 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e che *pCache, Pgn
fce90 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 o pgno){. if( p
fcea0 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b Cache->pCache ){
fceb0 0a 20 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 . PgHdr *p;.
fcec0 20 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b PgHdr *pNext;
fced0 0a 20 20 20 20 66 6f 72 28 70 3d 70 43 61 63 68 . for(p=pCach
fcee0 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d e->pDirty; p; p=
fcef0 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e pNext){. pN
fcf00 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e ext = p->pDirtyN
fcf10 65 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ext;. if( p
fcf20 2d 3e 70 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 ->pgno>pgno ){.
fcf30 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
fcf40 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
fcf50 52 54 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73 RTY );. s
fcf60 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
fcf70 43 6c 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 Clean(p);.
fcf80 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
fcf90 70 67 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 pgno==0 && pCach
fcfa0 65 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 e->pPage1 ){.
fcfb0 20 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 memset(pCache
fcfc0 2d 3e 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c ->pPage1->pData,
fcfd0 20 30 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 0, pCache->szPa
fcfe0 67 65 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 ge);. pgno
fcff0 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 = 1;. }. s
fd000 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
fd010 69 67 2e 70 63 61 63 68 65 2e 78 54 72 75 6e 63 ig.pcache.xTrunc
fd020 61 74 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63 ate(pCache->pCac
fd030 68 65 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d he, pgno+1);. }
fd040 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
fd050 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 a cache..*/.SQLI
fd060 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
fd070 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f sqlite3PcacheClo
fd080 73 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 se(PCache *pCach
fd090 65 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 e){. if( pCache
fd0a0 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 ->pCache ){.
fd0b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
fd0c0 66 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 fig.pcache.xDest
fd0d0 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 roy(pCache->pCac
fd0e0 68 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a he);. }.}../* .
fd0f0 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63 ** Discard the c
fd100 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 ontents of the c
fd110 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ache..*/.SQLITE_
fd120 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
fd130 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 ite3PcacheClear(
fd140 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
fd150 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
fd160 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c Truncate(pCache,
fd170 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 0);.}../*.** Me
fd180 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 rge two lists of
fd190 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 pages connected
fd1a0 20 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 by pDirty and i
fd1b0 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a n pgno order..**
fd1c0 20 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 Do not both fix
fd1d0 69 6e 67 20 74 68 65 20 70 44 69 72 74 79 50 72 ing the pDirtyPr
fd1e0 65 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a ev pointers..*/.
fd1f0 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 static PgHdr *pc
fd200 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 acheMergeDirtyLi
fd210 73 74 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67 st(PgHdr *pA, Pg
fd220 48 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 Hdr *pB){. PgHd
fd230 72 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c r result, *pTail
fd240 3b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73 ;. pTail = &res
fd250 75 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 ult;. while( pA
fd260 20 26 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66 && pB ){. if
fd270 28 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 ( pA->pgno<pB->p
fd280 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61 gno ){. pTa
fd290 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b il->pDirty = pA;
fd2a0 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 . pTail = p
fd2b0 41 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 A;. pA = pA
fd2c0 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 ->pDirty;. }e
fd2d0 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c lse{. pTail
fd2e0 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 ->pDirty = pB;.
fd2f0 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b pTail = pB;
fd300 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e . pB = pB->
fd310 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 pDirty;. }.
fd320 7d 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 }. if( pA ){.
fd330 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 pTail->pDirty
fd340 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = pA;. }else if
fd350 28 20 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69 ( pB ){. pTai
fd360 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a l->pDirty = pB;.
fd370 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 }else{. pTa
fd380 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a il->pDirty = 0;.
fd390 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 }. return res
fd3a0 75 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f ult.pDirty;.}../
fd3b0 2a 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 *.** Sort the li
fd3c0 73 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 st of pages in a
fd3d0 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 ccending order b
fd3e0 79 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 y pgno. Pages a
fd3f0 72 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 re.** connected
fd400 62 79 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 by pDirty pointe
fd410 72 73 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 rs. The pDirtyP
fd420 72 65 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 rev pointers are
fd430 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 .** corrupted by
fd440 20 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 this sort..*/.#
fd450 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 define N_SORT_BU
fd460 43 4b 45 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 CKET_ALLOC 25.#d
fd470 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 efine N_SORT_BUC
fd480 4b 45 54 20 20 20 20 20 20 20 32 35 0a 23 69 66 KET 25.#if
fd490 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
fd4a0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 int sqlite3_pa
fd4b0 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 ger_n_sort_bucke
fd4c0 74 20 3d 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 t = 0;. #undef
fd4d0 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 N_SORT_BUCKET.
fd4e0 23 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 #define N_SORT_B
fd4f0 55 43 4b 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 UCKET \. (sqli
fd500 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 te3_pager_n_sort
fd510 5f 62 75 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f _bucket?sqlite3_
fd520 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 pager_n_sort_buc
fd530 6b 65 74 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 ket:N_SORT_BUCKE
fd540 54 5f 41 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a T_ALLOC).#endif.
fd550 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 static PgHdr *pc
fd560 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 acheSortDirtyLis
fd570 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 t(PgHdr *pIn){.
fd580 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 PgHdr *a[N_SORT
fd590 5f 42 55 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 _BUCKET_ALLOC],
fd5a0 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d *p;. int i;. m
fd5b0 65 6d 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 emset(a, 0, size
fd5c0 6f 66 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 of(a));. while(
fd5d0 20 70 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 pIn ){. p =
fd5e0 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 pIn;. pIn = p
fd5f0 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d ->pDirty;. p-
fd600 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 >pDirty = 0;.
fd610 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f for(i=0; i<N_SO
fd620 52 54 5f 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b RT_BUCKET-1; i++
fd630 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69 ){. if( a[i
fd640 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ]==0 ){.
fd650 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 a[i] = p;.
fd660 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
fd670 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20 else{. p
fd680 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 = pcacheMergeDir
fd690 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b tyList(a[i], p);
fd6a0 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 . a[i] =
fd6b0 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
fd6c0 0a 20 20 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f . if( i==N_SO
fd6d0 52 54 5f 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 RT_BUCKET-1 ){.
fd6e0 20 20 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 /* Coverage
fd6f0 3a 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 : To get here, t
fd700 68 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 here need to be
fd710 32 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2^(N_SORT_BUCKET
fd720 29 20 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d ) . ** elem
fd730 65 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 70 75 ents in the inpu
fd740 74 20 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 t list. This is
fd750 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 69 6d possible, but im
fd760 70 72 61 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 practical..
fd770 20 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 69 73 ** Testing this
fd780 20 6c 69 6e 65 20 69 73 20 74 68 65 20 70 6f 69 line is the poi
fd790 6e 74 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 nt of global var
fd7a0 69 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 iable. ** s
fd7b0 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 qlite3_pager_n_s
fd7c0 6f 72 74 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 ort_bucket..
fd7d0 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 */. a[i]
fd7e0 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 = pcacheMergeDir
fd7f0 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b tyList(a[i], p);
fd800 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d . }. }. p =
fd810 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 a[0];. for(i=1
fd820 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 ; i<N_SORT_BUCKE
fd830 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d T; i++){. p =
fd840 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 pcacheMergeDirt
fd850 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a yList(p, a[i]);.
fd860 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
fd870 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
fd880 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 a list of all di
fd890 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 rty pages in the
fd8a0 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 cache, sorted b
fd8b0 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a y page number..*
fd8c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
fd8d0 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 PgHdr *sqlite3P
fd8e0 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 cacheDirtyList(P
fd8f0 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
fd900 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f PgHdr *p;. fo
fd910 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 r(p=pCache->pDir
fd920 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 ty; p; p=p->pDir
fd930 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e tyNext){. p->
fd940 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69 72 pDirty = p->pDir
fd950 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 tyNext;. }. re
fd960 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74 44 turn pcacheSortD
fd970 69 72 74 79 4c 69 73 74 28 70 43 61 63 68 65 2d irtyList(pCache-
fd980 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 >pDirty);.}../*
fd990 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 .** Return the t
fd9a0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 otal number of r
fd9b0 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 eferenced pages
fd9c0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 63 68 held by the cach
fd9d0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
fd9e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
fd9f0 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50 PcacheRefCount(P
fda00 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
fda10 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d return pCache-
fda20 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 >nRef;.}../*.**
fda30 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
fda40 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
fda50 74 6f 20 74 68 65 20 70 61 67 65 20 73 75 70 70 to the page supp
fda60 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d lied as an argum
fda70 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ent..*/.SQLITE_P
fda80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
fda90 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 e3PcachePageRefc
fdaa0 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b 0a ount(PgHdr *p){.
fdab0 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 return p->nRef
fdac0 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 ;.}../* .** Retu
fdad0 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d rn the total num
fdae0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
fdaf0 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 the cache..*/.SQ
fdb00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
fdb10 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 sqlite3PcachePa
fdb20 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a gecount(PCache *
fdb30 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e pCache){. int n
fdb40 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 Page = 0;. if(
fdb50 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 pCache->pCache )
fdb60 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73 71 {. nPage = sq
fdb70 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
fdb80 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63 6f g.pcache.xPageco
fdb90 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61 63 unt(pCache->pCac
fdba0 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 he);. }. retur
fdbb0 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 n nPage;.}..#ifd
fdbc0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f ef SQLITE_TEST./
fdbd0 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75 67 *.** Get the sug
fdbe0 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a gested cache-siz
fdbf0 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 e value..*/.SQLI
fdc00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
fdc10 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 qlite3PcacheGetC
fdc20 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 achesize(PCache
fdc30 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 75 *pCache){. retu
fdc40 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b rn pCache->nMax;
fdc50 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
fdc60 20 53 65 74 20 74 68 65 20 73 75 67 67 65 73 74 Set the suggest
fdc70 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 ed cache-size va
fdc80 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 lue..*/.SQLITE_P
fdc90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
fdca0 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 te3PcacheSetCach
fdcb0 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 esize(PCache *pC
fdcc0 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67 65 ache, int mxPage
fdcd0 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 ){. pCache->nMa
fdce0 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69 66 x = mxPage;. if
fdcf0 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ( pCache->pCache
fdd00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ){. sqlite3G
fdd10 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
fdd20 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 43 he.xCachesize(pC
fdd30 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 ache->pCache, mx
fdd40 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 Page);. }.}..#i
fdd50 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 fdef SQLITE_CHEC
fdd60 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 46 6f K_PAGES./*.** Fo
fdd70 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 r all dirty page
fdd80 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 s currently in t
fdd90 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 he cache, invoke
fdda0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
fddb0 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 * callback. This
fddc0 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 is only used if
fddd0 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43 the SQLITE_CHEC
fdde0 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73 K_PAGES macro is
fddf0 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a .** defined..*/.
fde00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
fde10 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
fde20 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 eIterateDirty(PC
fde30 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f ache *pCache, vo
fde40 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 id (*xIter)(PgHd
fde50 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a r *)){. PgHdr *
fde60 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 pDirty;. for(pD
fde70 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 irty=pCache->pDi
fde80 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69 rty; pDirty; pDi
fde90 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 rty=pDirty->pDir
fdea0 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74 tyNext){. xIt
fdeb0 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a er(pDirty);. }.
fdec0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a }.#endif../*****
fded0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
fdee0 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a pcache.c ******
fdef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdf00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdf10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
fdf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
fdf30 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a file pcache1.c *
fdf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdf50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdf60 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
fdf70 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 2008 November 05
fdf80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
fdf90 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
fdfa0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
fdfb0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
fdfc0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
fdfd0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
fdfe0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
fdff0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
fe000 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
fe010 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
fe020 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
fe030 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
fe040 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
fe050 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
fe060 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
fe070 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
fe080 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
fe090 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
fe0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fe0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fe0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fe0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
fe0e0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
fe0f0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 implements the d
fe100 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 efault page cach
fe110 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
fe120 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 (the.** sqlite3
fe130 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 _pcache interfac
fe140 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 e). It also cont
fe150 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 ains part of the
fe160 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
fe170 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 ** of the SQLITE
fe180 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
fe190 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 E and sqlite3_re
fe1a0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 lease_memory() f
fe1b0 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 eatures..** If t
fe1c0 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 he default page
fe1d0 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 cache implementa
fe1e0 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 tion is override
fe1f0 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 n, then neither
fe200 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 of.** these two
fe210 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61 features are ava
fe220 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 ilable..**.** @(
fe230 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e #) $Id: pcache1.
fe240 63 2c 76 20 31 2e 31 30 20 32 30 30 39 2f 30 33 c,v 1.10 2009/03
fe250 2f 32 33 20 30 34 3a 33 33 3a 33 33 20 64 61 6e /23 04:33:33 dan
fe260 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a ielk1977 Exp $.*
fe270 2f 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 /...typedef stru
fe280 63 74 20 50 43 61 63 68 65 31 20 50 43 61 63 68 ct PCache1 PCach
fe290 65 31 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 e1;.typedef stru
fe2a0 63 74 20 50 67 48 64 72 31 20 50 67 48 64 72 31 ct PgHdr1 PgHdr1
fe2b0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
fe2c0 20 50 67 46 72 65 65 73 6c 6f 74 20 50 67 46 72 PgFreeslot PgFr
fe2d0 65 65 73 6c 6f 74 3b 0a 0a 2f 2a 20 50 6f 69 6e eeslot;../* Poin
fe2e0 74 65 72 73 20 74 6f 20 73 74 72 75 63 74 75 72 ters to structur
fe2f0 65 73 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 es of this type
fe300 61 72 65 20 63 61 73 74 20 61 6e 64 20 72 65 74 are cast and ret
fe310 75 72 6e 65 64 20 61 73 20 0a 2a 2a 20 6f 70 61 urned as .** opa
fe320 71 75 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 que sqlite3_pcac
fe330 68 65 2a 20 68 61 6e 64 6c 65 73 0a 2a 2f 0a 73 he* handles.*/.s
fe340 74 72 75 63 74 20 50 43 61 63 68 65 31 20 7b 0a truct PCache1 {.
fe350 20 20 2f 2a 20 43 61 63 68 65 20 63 6f 6e 66 69 /* Cache confi
fe360 67 75 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 guration paramet
fe370 65 72 73 2e 20 50 61 67 65 20 73 69 7a 65 20 28 ers. Page size (
fe380 73 7a 50 61 67 65 29 20 61 6e 64 20 74 68 65 20 szPage) and the
fe390 70 75 72 67 65 61 62 6c 65 0a 20 20 2a 2a 20 66 purgeable. ** f
fe3a0 6c 61 67 20 28 62 50 75 72 67 65 61 62 6c 65 29 lag (bPurgeable)
fe3b0 20 61 72 65 20 73 65 74 20 77 68 65 6e 20 74 68 are set when th
fe3c0 65 20 63 61 63 68 65 20 69 73 20 63 72 65 61 74 e cache is creat
fe3d0 65 64 2e 20 6e 4d 61 78 20 6d 61 79 20 62 65 20 ed. nMax may be
fe3e0 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 . ** modified a
fe3f0 74 20 61 6e 79 20 74 69 6d 65 20 62 79 20 61 20 t any time by a
fe400 63 61 6c 6c 20 74 6f 20 74 68 65 20 70 63 61 63 call to the pcac
fe410 68 65 31 43 61 63 68 65 53 69 7a 65 28 29 20 6d he1CacheSize() m
fe420 65 74 68 6f 64 2e 0a 20 20 2a 2a 20 54 68 65 20 ethod.. ** The
fe430 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 global mutex mus
fe440 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 61 t be held when a
fe450 63 63 65 73 73 69 6e 67 20 6e 4d 61 78 2e 0a 20 ccessing nMax..
fe460 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 */. int szPage
fe470 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
fe480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
fe490 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 70 e of allocated p
fe4a0 61 67 65 73 20 69 6e 20 62 79 74 65 73 20 2a 2f ages in bytes */
fe4b0 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c . int bPurgeabl
fe4c0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
fe4d0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
fe4e0 66 20 63 61 63 68 65 20 69 73 20 70 75 72 67 65 f cache is purge
fe4f0 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e able */. unsign
fe500 65 64 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 ed int nMin;
fe510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
fe520 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 Minimum number
fe530 6f 66 20 70 61 67 65 73 20 72 65 73 65 72 76 65 of pages reserve
fe540 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 d */. unsigned
fe550 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 20 20 int nMax;
fe560 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
fe570 6e 66 69 67 75 72 65 64 20 22 63 61 63 68 65 5f nfigured "cache_
fe580 73 69 7a 65 22 20 76 61 6c 75 65 20 2a 2f 0a 0a size" value */..
fe590 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 /* Hash table
fe5a0 6f 66 20 61 6c 6c 20 70 61 67 65 73 2e 20 54 68 of all pages. Th
fe5b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 e following vari
fe5c0 61 62 6c 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 ables may only b
fe5d0 65 20 61 63 63 65 73 73 65 64 0a 20 20 2a 2a 20 e accessed. **
fe5e0 77 68 65 6e 20 74 68 65 20 61 63 63 65 73 73 6f when the accesso
fe5f0 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 r is holding the
fe600 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 28 73 global mutex (s
fe610 65 65 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d ee pcache1EnterM
fe620 75 74 65 78 28 29 20 0a 20 20 2a 2a 20 61 6e 64 utex() . ** and
fe630 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
fe640 65 78 28 29 29 2e 0a 20 20 2a 2f 0a 20 20 75 6e ex()).. */. un
fe650 73 69 67 6e 65 64 20 69 6e 74 20 6e 52 65 63 79 signed int nRecy
fe660 63 6c 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 clable;
fe670 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
fe680 61 67 65 73 20 69 6e 20 74 68 65 20 4c 52 55 20 ages in the LRU
fe690 6c 69 73 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e list */. unsign
fe6a0 65 64 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 ed int nPage;
fe6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
fe6c0 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
fe6d0 20 70 61 67 65 73 20 69 6e 20 61 70 48 61 73 68 pages in apHash
fe6e0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
fe6f0 6e 74 20 6e 48 61 73 68 3b 20 20 20 20 20 20 20 nt nHash;
fe700 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
fe710 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 ber of slots in
fe720 61 70 48 61 73 68 5b 5d 20 2a 2f 0a 20 20 50 67 apHash[] */. Pg
fe730 48 64 72 31 20 2a 2a 61 70 48 61 73 68 3b 20 20 Hdr1 **apHash;
fe740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe750 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 /* Hash table
fe760 66 6f 72 20 66 61 73 74 20 6c 6f 6f 6b 75 70 20 for fast lookup
fe770 62 79 20 6b 65 79 20 2a 2f 0a 0a 20 20 75 6e 73 by key */.. uns
fe780 69 67 6e 65 64 20 69 6e 74 20 69 4d 61 78 4b 65 igned int iMaxKe
fe790 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
fe7a0 20 2f 2a 20 4c 61 72 67 65 73 74 20 6b 65 79 20 /* Largest key
fe7b0 73 65 65 6e 20 73 69 6e 63 65 20 78 54 72 75 6e seen since xTrun
fe7c0 63 61 74 65 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a cate() */.};../*
fe7d0 0a 2a 2a 20 45 61 63 68 20 63 61 63 68 65 20 65 .** Each cache e
fe7e0 6e 74 72 79 20 69 73 20 72 65 70 72 65 73 65 6e ntry is represen
fe7f0 74 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e ted by an instan
fe800 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
fe810 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 ing .** structur
fe820 65 2e 20 41 20 62 75 66 66 65 72 20 6f 66 20 50 e. A buffer of P
fe830 67 48 64 72 31 2e 70 43 61 63 68 65 2d 3e 73 7a gHdr1.pCache->sz
fe840 50 61 67 65 20 62 79 74 65 73 20 69 73 20 61 6c Page bytes is al
fe850 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 64 69 72 65 located .** dire
fe860 63 74 6c 79 20 61 66 74 65 72 20 74 68 65 20 73 ctly after the s
fe870 74 72 75 63 74 75 72 65 20 69 6e 20 6d 65 6d 6f tructure in memo
fe880 72 79 20 28 73 65 65 20 74 68 65 20 50 47 48 44 ry (see the PGHD
fe890 52 31 5f 54 4f 5f 50 41 47 45 28 29 20 0a 2a 2a R1_TO_PAGE() .**
fe8a0 20 6d 61 63 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a macro below)..*
fe8b0 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 31 20 /.struct PgHdr1
fe8c0 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 {. unsigned int
fe8d0 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 iKey;
fe8e0 20 20 20 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 /* Key value
fe8f0 28 70 61 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f (page number) */
fe900 0a 20 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 . PgHdr1 *pNext
fe910 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
fe920 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 /* Next in has
fe930 68 20 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f h table chain */
fe940 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 . PCache1 *pCac
fe950 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 he;
fe960 20 20 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 /* Cache that
fe970 63 75 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 currently owns t
fe980 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 his page */. Pg
fe990 48 64 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 Hdr1 *pLruNext;
fe9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fe9b0 4e 65 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 Next in LRU list
fe9c0 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 of unpinned pag
fe9d0 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a es */. PgHdr1 *
fe9e0 70 4c 72 75 50 72 65 76 3b 20 20 20 20 20 20 20 pLruPrev;
fe9f0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f /* Previo
fea00 75 73 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f us in LRU list o
fea10 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 f unpinned pages
fea20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 */.};../*.** Fr
fea30 65 65 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 ee slots in the
fea40 61 6c 6c 6f 63 61 74 6f 72 20 75 73 65 64 20 74 allocator used t
fea50 6f 20 64 69 76 69 64 65 20 75 70 20 74 68 65 20 o divide up the
fea60 62 75 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 buffer provided
fea70 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c using.** the SQL
fea80 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
fea90 41 43 48 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a ACHE mechanism..
feaa0 2a 2f 0a 73 74 72 75 63 74 20 50 67 46 72 65 65 */.struct PgFree
feab0 73 6c 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73 slot {. PgFrees
feac0 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 lot *pNext; /*
fead0 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a Next free slot *
feae0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 /.};../*.** Glob
feaf0 61 6c 20 64 61 74 61 20 75 73 65 64 20 62 79 20 al data used by
feb00 74 68 69 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 this cache..*/.s
feb10 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
feb20 20 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c struct PCacheGl
feb30 6f 62 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 obal {. sqlite3
feb40 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 _mutex *mutex;
feb50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
feb60 73 74 61 74 69 63 20 6d 75 74 65 78 20 4d 55 54 static mutex MUT
feb70 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 2a 2f EX_STATIC_LRU */
feb80 0a 0a 20 20 69 6e 74 20 6e 4d 61 78 50 61 67 65 .. int nMaxPage
feb90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
feba0 20 20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f /* Sum o
febb0 66 20 6e 4d 61 78 50 61 67 65 20 66 6f 72 20 70 f nMaxPage for p
febc0 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 urgeable caches
febd0 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e 50 61 67 */. int nMinPag
febe0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
febf0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 /* Sum
fec00 6f 66 20 6e 4d 69 6e 50 61 67 65 20 66 6f 72 20 of nMinPage for
fec10 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 purgeable caches
fec20 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 */. int nCurre
fec30 6e 74 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 ntPage;
fec40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
fec50 62 65 72 20 6f 66 20 70 75 72 67 65 61 62 6c 65 ber of purgeable
fec60 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 pages allocated
fec70 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c */. PgHdr1 *pL
fec80 72 75 48 65 61 64 2c 20 2a 70 4c 72 75 54 61 69 ruHead, *pLruTai
fec90 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 52 55 l; /* LRU
feca0 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 list of unpinne
fecb0 64 20 70 61 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a d pages */.. /*
fecc0 20 56 61 72 69 61 62 6c 65 73 20 72 65 6c 61 74 Variables relat
fecd0 65 64 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e ed to SQLITE_CON
fece0 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 73 65 FIG_PAGECACHE se
fecf0 74 74 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 ttings. */. int
fed00 20 73 7a 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 szSlot;
fed10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fed20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 /* Size of each
fed30 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 free slot */.
fed40 76 6f 69 64 20 2a 70 53 74 61 72 74 2c 20 2a 70 void *pStart, *p
fed50 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 End;
fed60 20 20 20 20 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 /* Bounds of
fed70 20 70 61 67 65 63 61 63 68 65 20 6d 61 6c 6c 6f pagecache mallo
fed80 63 20 72 61 6e 67 65 20 2a 2f 0a 20 20 50 67 46 c range */. PgF
fed90 72 65 65 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 reeslot *pFree;
feda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fedb0 20 2f 2a 20 46 72 65 65 20 70 61 67 65 20 62 6c /* Free page bl
fedc0 6f 63 6b 73 20 2a 2f 0a 7d 20 70 63 61 63 68 65 ocks */.} pcache
fedd0 31 5f 67 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 1_g;../*.** All
fede0 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c code in this fil
fedf0 65 20 73 68 6f 75 6c 64 20 61 63 63 65 73 73 20 e should access
fee00 74 68 65 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 the global struc
fee10 74 75 72 65 20 61 62 6f 76 65 20 76 69 61 20 74 ture above via t
fee20 68 65 0a 2a 2a 20 61 6c 69 61 73 20 22 70 63 61 he.** alias "pca
fee30 63 68 65 31 22 2e 20 54 68 69 73 20 65 6e 73 75 che1". This ensu
fee40 72 65 73 20 74 68 61 74 20 74 68 65 20 57 53 44 res that the WSD
fee50 20 65 6d 75 6c 61 74 69 6f 6e 20 69 73 20 75 73 emulation is us
fee60 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 ed when.** compi
fee70 6c 69 6e 67 20 66 6f 72 20 73 79 73 74 65 6d 73 ling for systems
fee80 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 73 75 70 that do not sup
fee90 70 6f 72 74 20 72 65 61 6c 20 57 53 44 2e 0a 2a port real WSD..*
feea0 2f 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 /.#define pcache
feeb0 31 20 28 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 1 (GLOBAL(struct
feec0 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70 PCacheGlobal, p
feed0 63 61 63 68 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a cache1_g))../*.*
feee0 2a 20 57 68 65 6e 20 61 20 50 67 48 64 72 31 20 * When a PgHdr1
feef0 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c structure is all
fef00 6f 63 61 74 65 64 2c 20 74 68 65 20 61 73 73 6f ocated, the asso
fef10 63 69 61 74 65 64 20 50 43 61 63 68 65 31 2e 73 ciated PCache1.s
fef20 7a 50 61 67 65 0a 2a 2a 20 62 79 74 65 73 20 6f zPage.** bytes o
fef30 66 20 64 61 74 61 20 61 72 65 20 6c 6f 63 61 74 f data are locat
fef40 65 64 20 64 69 72 65 63 74 6c 79 20 61 66 74 65 ed directly afte
fef50 72 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 r it in memory (
fef60 69 2e 65 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a i.e. the total.*
fef70 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c * size of the al
fef80 6c 6f 63 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 location is size
fef90 6f 66 28 50 67 48 64 72 31 29 2b 50 43 61 63 68 of(PgHdr1)+PCach
fefa0 65 31 2e 73 7a 50 61 67 65 20 62 79 74 65 29 2e e1.szPage byte).
fefb0 20 54 68 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 The.** PGHDR1_T
fefc0 4f 5f 50 41 47 45 28 29 20 6d 61 63 72 6f 20 74 O_PAGE() macro t
fefd0 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 akes a pointer t
fefe0 6f 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63 o a PgHdr1 struc
feff0 74 75 72 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 ture as.** an ar
ff000 67 75 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 gument and retur
ff010 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
ff020 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 62 the associated b
ff030 6c 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a lock of szPage.*
ff040 2a 20 62 79 74 65 73 2e 20 54 68 65 20 50 41 47 * bytes. The PAG
ff050 45 5f 54 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 E_TO_PGHDR1() ma
ff060 63 72 6f 20 64 6f 65 73 20 74 68 65 20 6f 70 70 cro does the opp
ff070 6f 73 69 74 65 3a 20 69 74 73 20 61 72 67 75 6d osite: its argum
ff080 65 6e 74 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e ent is.** a poin
ff090 74 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f ter to a block o
ff0a0 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 20 6f f szPage bytes o
ff0b0 66 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 72 f data and the r
ff0c0 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a eturn value is.*
ff0d0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 * a pointer to t
ff0e0 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 50 67 he associated Pg
ff0f0 48 64 72 31 20 73 74 72 75 63 74 75 72 65 2e 0a Hdr1 structure..
ff100 2a 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 **.** assert(
ff110 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 PGHDR1_TO_PAGE(P
ff120 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 58 29 AGE_TO_PGHDR1(X)
ff130 29 3d 3d 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 )==X );.*/.#defi
ff140 6e 65 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 ne PGHDR1_TO_PAG
ff150 45 28 70 29 20 28 76 6f 69 64 20 2a 29 28 26 28 E(p) (void *)(&(
ff160 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
ff170 29 70 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 )p)[sizeof(PgHdr
ff180 31 29 5d 29 0a 23 64 65 66 69 6e 65 20 50 41 47 1)]).#define PAG
ff190 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 29 20 28 E_TO_PGHDR1(p) (
ff1a0 50 67 48 64 72 31 20 2a 29 28 26 28 28 75 6e 73 PgHdr1 *)(&((uns
ff1b0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 5b igned char *)p)[
ff1c0 2d 31 2a 28 69 6e 74 29 73 69 7a 65 6f 66 28 50 -1*(int)sizeof(P
ff1d0 67 48 64 72 31 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 gHdr1)])../*.**
ff1e0 4d 61 63 72 6f 73 20 74 6f 20 65 6e 74 65 72 20 Macros to enter
ff1f0 61 6e 64 20 6c 65 61 76 65 20 74 68 65 20 67 6c and leave the gl
ff200 6f 62 61 6c 20 4c 52 55 20 6d 75 74 65 78 2e 0a obal LRU mutex..
ff210 2a 2f 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68 */.#define pcach
ff220 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 20 73 e1EnterMutex() s
ff230 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
ff240 65 72 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 er(pcache1.mutex
ff250 29 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 ).#define pcache
ff260 31 4c 65 61 76 65 4d 75 74 65 78 28 29 20 73 71 1LeaveMutex() sq
ff270 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
ff280 65 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 e(pcache1.mutex)
ff290 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
ff2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ff2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ff2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ff2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ff2e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 */./******** Pag
ff2f0 65 20 41 6c 6c 6f 63 61 74 69 6f 6e 2f 53 51 4c e Allocation/SQL
ff300 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 ITE_CONFIG_PCACH
ff310 45 20 52 65 6c 61 74 65 64 20 46 75 6e 63 74 69 E Related Functi
ff320 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ons ************
ff330 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 **/../*.** This
ff340 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
ff350 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 ed during initia
ff360 6c 69 7a 61 74 69 6f 6e 20 69 66 20 61 20 73 74 lization if a st
ff370 61 74 69 63 20 62 75 66 66 65 72 20 69 73 20 0a atic buffer is .
ff380 2a 2a 20 73 75 70 70 6c 69 65 64 20 74 6f 20 75 ** supplied to u
ff390 73 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d se for the page-
ff3a0 63 61 63 68 65 20 62 79 20 70 61 73 73 69 6e 67 cache by passing
ff3b0 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 the SQLITE_CONF
ff3c0 49 47 5f 50 41 47 45 43 41 43 48 45 0a 2a 2a 20 IG_PAGECACHE.**
ff3d0 76 65 72 62 20 74 6f 20 73 71 6c 69 74 65 33 5f verb to sqlite3_
ff3e0 63 6f 6e 66 69 67 28 29 2e 20 50 61 72 61 6d 65 config(). Parame
ff3f0 74 65 72 20 70 42 75 66 20 70 6f 69 6e 74 73 20 ter pBuf points
ff400 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e to an allocation
ff410 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 large.** enough
ff420 20 74 6f 20 63 6f 6e 74 61 69 6e 20 27 6e 27 20 to contain 'n'
ff430 62 75 66 66 65 72 73 20 6f 66 20 27 73 7a 27 20 buffers of 'sz'
ff440 62 79 74 65 73 20 65 61 63 68 2e 0a 2a 2f 0a 53 bytes each..*/.S
ff450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
ff460 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 id sqlite3PCache
ff470 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 64 BufferSetup(void
ff480 20 2a 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 *pBuf, int sz,
ff490 69 6e 74 20 6e 29 7b 0a 20 20 50 67 46 72 65 65 int n){. PgFree
ff4a0 73 6c 6f 74 20 2a 70 3b 0a 20 20 73 7a 20 3d 20 slot *p;. sz =
ff4b0 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a ROUNDDOWN8(sz);.
ff4c0 20 20 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74 pcache1.szSlot
ff4d0 20 3d 20 73 7a 3b 0a 20 20 70 63 61 63 68 65 31 = sz;. pcache1
ff4e0 2e 70 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a .pStart = pBuf;.
ff4f0 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 pcache1.pFree
ff500 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d = 0;. while( n-
ff510 2d 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 50 67 - ){. p = (Pg
ff520 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a Freeslot*)pBuf;.
ff530 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
ff540 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 cache1.pFree;.
ff550 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 pcache1.pFree
ff560 3d 20 70 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 = p;. pBuf =
ff570 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 (void*)&((char*)
ff580 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 7d 0a 20 pBuf)[sz];. }.
ff590 20 70 63 61 63 68 65 31 2e 70 45 6e 64 20 3d 20 pcache1.pEnd =
ff5a0 70 42 75 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d pBuf;.}../*.** M
ff5b0 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 alloc function u
ff5c0 73 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 20 sed within this
ff5d0 66 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 file to allocate
ff5e0 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 space from the
ff5f0 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 buffer.** config
ff600 75 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 ured using sqlit
ff610 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 e3_config(SQLITE
ff620 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
ff630 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f E) option. If no
ff640 20 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 72 .** such buffer
ff650 20 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 65 exists or there
ff660 20 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 is no space lef
ff670 74 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 75 t in it, this fu
ff680 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a nction falls .**
ff690 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 back to sqlite3
ff6a0 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 Malloc()..*/.sta
ff6b0 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 tic void *pcache
ff6c0 31 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 1Alloc(int nByte
ff6d0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 ){. void *p;.
ff6e0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
ff6f0 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 mutex_held(pcach
ff700 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 e1.mutex) );. i
ff710 66 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 f( nByte<=pcache
ff720 31 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 1.szSlot && pcac
ff730 68 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 he1.pFree ){.
ff740 20 70 20 3d 20 28 50 67 48 64 72 31 20 2a 29 70 p = (PgHdr1 *)p
ff750 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 cache1.pFree;.
ff760 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 pcache1.pFree
ff770 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 2d = pcache1.pFree-
ff780 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 >pNext;. sqli
ff790 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c te3StatusSet(SQL
ff7a0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
ff7b0 41 43 48 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 ACHE_SIZE, nByte
ff7c0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 );. sqlite3St
ff7d0 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
ff7e0 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
ff7f0 55 53 45 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 USED, 1);. }els
ff800 65 7b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 e{.. /* Alloc
ff810 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 ate a new buffer
ff820 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 using sqlite3Ma
ff830 6c 6c 6f 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 lloc. Before doi
ff840 6e 67 20 73 6f 2c 20 65 78 69 74 20 74 68 65 0a ng so, exit the.
ff850 20 20 20 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 ** global pc
ff860 61 63 68 65 20 6d 75 74 65 78 20 61 6e 64 20 75 ache mutex and u
ff870 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2d nlock the pager-
ff880 63 61 63 68 65 20 6f 62 6a 65 63 74 20 70 43 61 cache object pCa
ff890 63 68 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20 che. This is .
ff8a0 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 69 66 20 ** so that if
ff8b0 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61 the attempt to a
ff8c0 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 62 75 llocate a new bu
ff8d0 66 66 65 72 20 63 61 75 73 65 73 20 74 68 65 20 ffer causes the
ff8e0 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 the . ** conf
ff8f0 69 67 75 72 65 64 20 73 6f 66 74 2d 68 65 61 70 igured soft-heap
ff900 2d 6c 69 6d 69 74 20 74 6f 20 62 65 20 62 72 65 -limit to be bre
ff910 61 63 68 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 ached, it will b
ff920 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 e possible to.
ff930 20 20 2a 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d ** reclaim mem
ff940 6f 72 79 20 66 72 6f 6d 20 74 68 69 73 20 70 61 ory from this pa
ff950 67 65 72 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a ger-cache.. *
ff960 2f 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 /. pcache1Lea
ff970 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 70 veMutex();. p
ff980 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
ff990 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 63 61 (nByte);. pca
ff9a0 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
ff9b0 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 ;. if( p ){.
ff9c0 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 int sz = sq
ff9d0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
ff9e0 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 p);. sqlite
ff9f0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
ffa00 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 E_STATUS_PAGECAC
ffa10 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 HE_OVERFLOW, sz)
ffa20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
ffa30 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
ffa40 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 Free an allocat
ffa50 65 64 20 62 75 66 66 65 72 20 6f 62 74 61 69 6e ed buffer obtain
ffa60 65 64 20 66 72 6f 6d 20 70 63 61 63 68 65 31 41 ed from pcache1A
ffa70 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 lloc()..*/.stati
ffa80 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 46 72 c void pcache1Fr
ffa90 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 ee(void *p){. a
ffaa0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
ffab0 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 utex_held(pcache
ffac0 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 1.mutex) );. if
ffad0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( p==0 ) return;
ffae0 0a 20 20 69 66 28 20 70 3e 3d 70 63 61 63 68 65 . if( p>=pcache
ffaf0 31 2e 70 53 74 61 72 74 20 26 26 20 70 3c 70 63 1.pStart && p<pc
ffb00 61 63 68 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 ache1.pEnd ){.
ffb10 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 PgFreeslot *pS
ffb20 6c 6f 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 lot;. sqlite3
ffb30 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 StatusAdd(SQLITE
ffb40 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
ffb50 45 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 E_USED, -1);.
ffb60 20 70 53 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 pSlot = (PgFree
ffb70 73 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c slot*)p;. pSl
ffb80 6f 74 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 ot->pNext = pcac
ffb90 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 he1.pFree;. p
ffba0 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 cache1.pFree = p
ffbb0 53 6c 6f 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Slot;. }else{.
ffbc0 20 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 int iSize = s
ffbd0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 qlite3MallocSize
ffbe0 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 (p);. sqlite3
ffbf0 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 StatusAdd(SQLITE
ffc00 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
ffc10 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 E_OVERFLOW, -iSi
ffc20 7a 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ze);. sqlite3
ffc30 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a _free(p);. }.}.
ffc40 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
ffc50 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 a new page objec
ffc60 74 20 69 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f t initially asso
ffc70 63 69 61 74 65 64 20 77 69 74 68 20 63 61 63 68 ciated with cach
ffc80 65 20 70 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 e pCache..*/.sta
ffc90 74 69 63 20 50 67 48 64 72 31 20 2a 70 63 61 63 tic PgHdr1 *pcac
ffca0 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 he1AllocPage(PCa
ffcb0 63 68 65 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 che1 *pCache){.
ffcc0 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a int nByte = siz
ffcd0 65 6f 66 28 50 67 48 64 72 31 29 20 2b 20 70 43 eof(PgHdr1) + pC
ffce0 61 63 68 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 ache->szPage;.
ffcf0 50 67 48 64 72 31 20 2a 70 20 3d 20 28 50 67 48 PgHdr1 *p = (PgH
ffd00 64 72 31 20 2a 29 70 63 61 63 68 65 31 41 6c 6c dr1 *)pcache1All
ffd10 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 oc(nByte);. if(
ffd20 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 p ){. if( pC
ffd30 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
ffd40 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ){. pcache
ffd50 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 2b 2b 1.nCurrentPage++
ffd60 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
ffd70 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
ffd80 20 46 72 65 65 20 61 20 70 61 67 65 20 6f 62 6a Free a page obj
ffd90 65 63 74 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 ect allocated by
ffda0 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 67 pcache1AllocPag
ffdb0 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 e()..*/.static v
ffdc0 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 50 oid pcache1FreeP
ffdd0 61 67 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a age(PgHdr1 *p){.
ffde0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 if( p ){. i
ffdf0 66 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 62 50 f( p->pCache->bP
ffe00 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 urgeable ){.
ffe10 20 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 pcache1.nCurre
ffe20 6e 74 50 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a ntPage--;. }.
ffe30 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 28 pcache1Free(
ffe40 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
ffe50 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e Malloc function
ffe60 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 used by SQLite
ffe70 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 to obtain space
ffe80 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 from the buffer
ffe90 63 6f 6e 66 69 67 75 72 65 64 0a 2a 2a 20 75 73 configured.** us
ffea0 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ing sqlite3_conf
ffeb0 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
ffec0 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69 _PAGECACHE) opti
ffed0 6f 6e 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 62 on. If no such b
ffee0 75 66 66 65 72 0a 2a 2a 20 65 78 69 73 74 73 2c uffer.** exists,
ffef0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 this function f
fff00 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20 73 71 6c alls back to sql
fff10 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f ite3Malloc()..*/
fff20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
fff30 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
fff40 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a 29 7b eMalloc(int sz){
fff50 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 70 63 . void *p;. pc
fff60 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
fff70 29 3b 0a 20 20 70 20 3d 20 70 63 61 63 68 65 31 );. p = pcache1
fff80 41 6c 6c 6f 63 28 73 7a 29 3b 0a 20 20 70 63 61 Alloc(sz);. pca
fff90 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
fffa0 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
fffb0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 ./*.** Free an a
fffc0 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 llocated buffer
fffd0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
fffe0 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 lite3PageMalloc(
ffff0 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
10000 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
10001 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 2a 3PageFree(void *
10002 70 29 7b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 p){. pcache1Ent
10003 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 erMutex();. pca
10004 63 68 65 31 46 72 65 65 28 70 29 3b 0a 20 20 70 che1Free(p);. p
10005 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10006 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ();.}../********
10007 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10008 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10009 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1000a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1000b 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
1000c 2a 20 47 65 6e 65 72 61 6c 20 49 6d 70 6c 65 6d * General Implem
1000d 65 6e 74 61 74 69 6f 6e 20 46 75 6e 63 74 69 6f entation Functio
1000e 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ns *************
1000f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10010 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
10011 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
10012 20 75 73 65 64 20 74 6f 20 72 65 73 69 7a 65 20 used to resize
10013 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 75 the hash table u
10014 73 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 sed by the cache
10015 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 passed.** as th
10016 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
10017 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 ..**.** The glob
10018 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 al mutex must be
10019 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 held when this
1001a 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1001b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
1001c 74 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 48 t pcache1ResizeH
1001d 61 73 68 28 50 43 61 63 68 65 31 20 2a 70 29 7b ash(PCache1 *p){
1001e 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 70 4e 65 . PgHdr1 **apNe
1001f 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e w;. unsigned in
10020 74 20 6e 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e t nNew;. unsign
10021 65 64 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 ed int i;.. ass
10022 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
10023 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e ex_held(pcache1.
10024 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 6e 4e 65 mutex) );.. nNe
10025 77 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a w = p->nHash*2;.
10026 20 20 69 66 28 20 6e 4e 65 77 3c 32 35 36 20 29 if( nNew<256 )
10027 7b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 32 35 36 {. nNew = 256
10028 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 ;. }.. pcache1
10029 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
1002a 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20 if( p->nHash ){
1002b 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
1002c 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 gnMalloc(); }.
1002d 61 70 4e 65 77 20 3d 20 28 50 67 48 64 72 31 20 apNew = (PgHdr1
1002e 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f **)sqlite3_mallo
1002f 63 28 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20 c(sizeof(PgHdr1
10030 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20 *)*nNew);. if(
10031 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69 p->nHash ){ sqli
10032 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
10033 6f 63 28 29 3b 20 7d 0a 20 20 70 63 61 63 68 65 oc(); }. pcache
10034 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10035 20 69 66 28 20 61 70 4e 65 77 20 29 7b 0a 20 20 if( apNew ){.
10036 20 20 6d 65 6d 73 65 74 28 61 70 4e 65 77 2c 20 memset(apNew,
10037 30 2c 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 0, sizeof(PgHdr1
10038 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 20 20 66 *)*nNew);. f
10039 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61 or(i=0; i<p->nHa
1003a 73 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 sh; i++){.
1003b 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 PgHdr1 *pPage;.
1003c 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 4e 65 PgHdr1 *pNe
1003d 78 74 20 3d 20 70 2d 3e 61 70 48 61 73 68 5b 69 xt = p->apHash[i
1003e 5d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 ];. while(
1003f 28 70 50 61 67 65 20 3d 20 70 4e 65 78 74 29 21 (pPage = pNext)!
10040 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e =0 ){. un
10041 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 70 signed int h = p
10042 50 61 67 65 2d 3e 69 4b 65 79 20 25 20 6e 4e 65 Page->iKey % nNe
10043 77 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 w;. pNext
10044 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b = pPage->pNext;
10045 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e . pPage->
10046 70 4e 65 78 74 20 3d 20 61 70 4e 65 77 5b 68 5d pNext = apNew[h]
10047 3b 0a 20 20 20 20 20 20 20 20 61 70 4e 65 77 5b ;. apNew[
10048 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 h] = pPage;.
10049 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
1004a 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 lite3_free(p->ap
1004b 48 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 61 70 Hash);. p->ap
1004c 48 61 73 68 20 3d 20 61 70 4e 65 77 3b 0a 20 20 Hash = apNew;.
1004d 20 20 70 2d 3e 6e 48 61 73 68 20 3d 20 6e 4e 65 p->nHash = nNe
1004e 77 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e w;. }.. return
1004f 20 28 70 2d 3e 61 70 48 61 73 68 20 3f 20 53 51 (p->apHash ? SQ
10050 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
10051 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a _NOMEM);.}../*.*
10052 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
10053 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c is used internal
10054 6c 79 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 ly to remove the
10055 20 70 61 67 65 20 70 50 61 67 65 20 66 72 6f 6d page pPage from
10056 20 74 68 65 20 0a 2a 2a 20 67 6c 6f 62 61 6c 20 the .** global
10057 4c 52 55 20 6c 69 73 74 2c 20 69 66 20 69 73 20 LRU list, if is
10058 70 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 70 part of it. If p
10059 50 61 67 65 20 69 73 20 6e 6f 74 20 70 61 72 74 Page is not part
1005a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 0a 2a of the global.*
1005b 2a 20 4c 52 55 20 6c 69 73 74 2c 20 74 68 65 6e * LRU list, then
1005c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
1005d 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
1005e 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 The global mute
1005f 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 x must be held w
10060 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
10061 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a n is called..*/.
10062 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10063 68 65 31 50 69 6e 50 61 67 65 28 50 67 48 64 72 he1PinPage(PgHdr
10064 31 20 2a 70 50 61 67 65 29 7b 0a 20 20 61 73 73 1 *pPage){. ass
10065 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
10066 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e ex_held(pcache1.
10067 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
10068 70 50 61 67 65 20 26 26 20 28 70 50 61 67 65 2d pPage && (pPage-
10069 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 50 61 >pLruNext || pPa
1006a 67 65 3d 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 ge==pcache1.pLru
1006b 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 69 66 28 Tail) ){. if(
1006c 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 pPage->pLruPrev
1006d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d ){. pPage-
1006e 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e >pLruPrev->pLruN
1006f 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 ext = pPage->pLr
10070 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 uNext;. }.
10071 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 if( pPage->pLru
10072 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 50 Next ){. pP
10073 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 age->pLruNext->p
10074 4c 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 2d LruPrev = pPage-
10075 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d >pLruPrev;. }
10076 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 65 31 . if( pcache1
10077 2e 70 4c 72 75 48 65 61 64 3d 3d 70 50 61 67 65 .pLruHead==pPage
10078 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ){. pcache
10079 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 1.pLruHead = pPa
1007a 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 ge->pLruNext;.
1007b 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61 63 }. if( pcac
1007c 68 65 31 2e 70 4c 72 75 54 61 69 6c 3d 3d 70 50 he1.pLruTail==pP
1007d 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 age ){. pca
1007e 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20 che1.pLruTail =
1007f 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b pPage->pLruPrev;
10080 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
10081 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a ->pLruNext = 0;.
10082 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 pPage->pLruP
10083 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 rev = 0;. pPa
10084 67 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 65 63 ge->pCache->nRec
10085 79 63 6c 61 62 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d yclable--;. }.}
10086 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 .../*.** Remove
10087 74 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 the page supplie
10088 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 d as an argument
10089 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 from the hash t
1008a 61 62 6c 65 20 0a 2a 2a 20 28 50 43 61 63 68 65 able .** (PCache
1008b 31 2e 61 70 48 61 73 68 20 73 74 72 75 63 74 75 1.apHash structu
1008c 72 65 29 20 74 68 61 74 20 69 74 20 69 73 20 63 re) that it is c
1008d 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 urrently stored
1008e 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c in..**.** The gl
1008f 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 obal mutex must
10090 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 be held when thi
10091 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
10092 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lled..*/.static
10093 76 6f 69 64 20 70 63 61 63 68 65 31 52 65 6d 6f void pcache1Remo
10094 76 65 46 72 6f 6d 48 61 73 68 28 50 67 48 64 72 veFromHash(PgHdr
10095 31 20 2a 70 50 61 67 65 29 7b 0a 20 20 75 6e 73 1 *pPage){. uns
10096 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 50 igned int h;. P
10097 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
10098 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a pPage->pCache;.
10099 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 0a PgHdr1 **pp;..
1009a 20 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 h = pPage->iKe
1009b 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 y % pCache->nHas
1009c 68 3b 0a 20 20 66 6f 72 28 70 70 3d 26 70 43 61 h;. for(pp=&pCa
1009d 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 che->apHash[h];
1009e 28 2a 70 70 29 21 3d 70 50 61 67 65 3b 20 70 70 (*pp)!=pPage; pp
1009f 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 3b =&(*pp)->pNext);
100a0 0a 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e . *pp = (*pp)->
100a1 70 4e 65 78 74 3b 0a 0a 20 20 70 43 61 63 68 65 pNext;.. pCache
100a2 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a ->nPage--;.}../*
100a3 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 .** If there are
100a4 20 63 75 72 72 65 6e 74 6c 79 20 6d 6f 72 65 20 currently more
100a5 74 68 61 6e 20 70 63 61 63 68 65 2e 6e 4d 61 78 than pcache.nMax
100a6 50 61 67 65 20 70 61 67 65 73 20 61 6c 6c 6f 63 Page pages alloc
100a7 61 74 65 64 2c 20 74 72 79 0a 2a 2a 20 74 6f 20 ated, try.** to
100a8 72 65 63 79 63 6c 65 20 70 61 67 65 73 20 74 6f recycle pages to
100a9 20 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62 reduce the numb
100aa 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 er allocated to
100ab 70 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 2e pcache.nMaxPage.
100ac 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
100ad 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 pcache1EnforceMa
100ae 78 50 61 67 65 28 76 6f 69 64 29 7b 0a 20 20 61 xPage(void){. a
100af 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
100b0 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 utex_held(pcache
100b1 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 77 68 1.mutex) );. wh
100b2 69 6c 65 28 20 70 63 61 63 68 65 31 2e 6e 43 75 ile( pcache1.nCu
100b3 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63 68 65 rrentPage>pcache
100b4 31 2e 6e 4d 61 78 50 61 67 65 20 26 26 20 70 63 1.nMaxPage && pc
100b5 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 29 ache1.pLruTail )
100b6 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 20 {. PgHdr1 *p
100b7 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 = pcache1.pLruTa
100b8 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 il;. pcache1P
100b9 69 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 70 inPage(p);. p
100ba 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d cache1RemoveFrom
100bb 48 61 73 68 28 70 29 3b 0a 20 20 20 20 70 63 61 Hash(p);. pca
100bc 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b che1FreePage(p);
100bd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 . }.}../*.** Di
100be 73 63 61 72 64 20 61 6c 6c 20 70 61 67 65 73 20 scard all pages
100bf 66 72 6f 6d 20 63 61 63 68 65 20 70 43 61 63 68 from cache pCach
100c0 65 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 e with a page nu
100c1 6d 62 65 72 20 28 6b 65 79 20 76 61 6c 75 65 29 mber (key value)
100c2 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 .** greater tha
100c3 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69 4c n or equal to iL
100c4 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64 imit. Any pinned
100c5 20 70 61 67 65 73 20 74 68 61 74 20 6d 65 65 74 pages that meet
100c6 20 74 68 69 73 20 0a 2a 2a 20 63 72 69 74 65 72 this .** criter
100c7 69 61 20 61 72 65 20 75 6e 70 69 6e 6e 65 64 20 ia are unpinned
100c8 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 before they are
100c9 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a discarded..**.**
100ca 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 The global mute
100cb 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 x must be held w
100cc 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
100cd 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a n is called..*/.
100ce 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
100cf 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 he1TruncateUnsaf
100d0 65 28 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 e(. PCache1 *pC
100d1 61 63 68 65 2c 20 0a 20 20 75 6e 73 69 67 6e 65 ache, . unsigne
100d2 64 20 69 6e 74 20 69 4c 69 6d 69 74 20 0a 29 7b d int iLimit .){
100d3 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
100d4 68 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c h;. assert( sql
100d5 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
100d6 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
100d7 3b 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 ;. for(h=0; h<p
100d8 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b Cache->nHash; h+
100d9 2b 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a +){. PgHdr1 *
100da 2a 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 *pp = &pCache->a
100db 70 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 pHash[h]; . P
100dc 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 gHdr1 *pPage;.
100dd 20 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 while( (pPage
100de 3d 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 = *pp)!=0 ){.
100df 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 4b if( pPage->iK
100e0 65 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 ey>=iLimit ){.
100e1 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e pcache1Pin
100e2 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
100e3 20 20 20 20 20 2a 70 70 20 3d 20 70 50 61 67 65 *pp = pPage
100e4 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 ->pNext;.
100e5 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 pcache1FreePage
100e6 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d (pPage);. }
100e7 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 70 else{. pp
100e8 20 3d 20 26 70 50 61 67 65 2d 3e 70 4e 65 78 74 = &pPage->pNext
100e9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
100ea 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a }.}../********
100eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100ef 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
100f0 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 * sqlite3_pcache
100f1 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a Methods *******
100f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100f4 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
100f5 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
100f6 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
100f7 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 74 68 6f ache.xInit metho
100f8 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
100f9 20 70 63 61 63 68 65 31 49 6e 69 74 28 76 6f 69 pcache1Init(voi
100fa 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
100fb 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
100fc 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 73 NotUsed);. mems
100fd 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20 et(&pcache1, 0,
100fe 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29 sizeof(pcache1))
100ff 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 ;. if( sqlite3G
10100 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 lobalConfig.bCor
10101 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 70 63 eMutex ){. pc
10102 61 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 73 71 ache1.mutex = sq
10103 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
10104 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
10105 54 41 54 49 43 5f 4c 52 55 29 3b 0a 20 20 7d 0a TATIC_LRU);. }.
10106 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10107 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 OK;.}../*.** Imp
10108 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10109 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
1010a 65 2e 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 e.xShutdown meth
1010b 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f od..*/.static vo
1010c 69 64 20 70 63 61 63 68 65 31 53 68 75 74 64 6f id pcache1Shutdo
1010d 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 wn(void *NotUsed
1010e 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
1010f 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
10110 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a /* no-op */.}.
10111 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10112 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10113 69 74 65 33 5f 70 63 61 63 68 65 2e 78 43 72 65 ite3_pcache.xCre
10114 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a ate method..**.*
10115 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
10116 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 cache..*/.stati
10117 63 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 c sqlite3_pcache
10118 20 2a 70 63 61 63 68 65 31 43 72 65 61 74 65 28 *pcache1Create(
10119 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 int szPage, int
1011a 62 50 75 72 67 65 61 62 6c 65 29 7b 0a 20 20 50 bPurgeable){. P
1011b 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 0a Cache1 *pCache;.
1011c 0a 20 20 70 43 61 63 68 65 20 3d 20 28 50 43 61 . pCache = (PCa
1011d 63 68 65 31 20 2a 29 73 71 6c 69 74 65 33 5f 6d che1 *)sqlite3_m
1011e 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 43 61 alloc(sizeof(PCa
1011f 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 70 43 che1));. if( pC
10120 61 63 68 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73 ache ){. mems
10121 65 74 28 70 43 61 63 68 65 2c 20 30 2c 20 73 69 et(pCache, 0, si
10122 7a 65 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a zeof(PCache1));.
10123 20 20 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 pCache->szPa
10124 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20 ge = szPage;.
10125 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 pCache->bPurgea
10126 62 6c 65 20 3d 20 28 62 50 75 72 67 65 61 62 6c ble = (bPurgeabl
10127 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 e ? 1 : 0);.
10128 69 66 28 20 62 50 75 72 67 65 61 62 6c 65 20 29 if( bPurgeable )
10129 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e {. pCache->
1012a 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 20 20 20 nMin = 10;.
1012b 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
1012c 65 78 28 29 3b 0a 20 20 20 20 20 20 70 63 61 63 ex();. pcac
1012d 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 he1.nMinPage +=
1012e 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 pCache->nMin;.
1012f 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 pcache1Leave
10130 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 7d 0a 20 Mutex();. }.
10131 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c }. return (sql
10132 69 74 65 33 5f 70 63 61 63 68 65 20 2a 29 70 43 ite3_pcache *)pC
10133 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ache;.}../*.** I
10134 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
10135 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
10136 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 20 6d che.xCachesize m
10137 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f ethod. .**.** Co
10138 6e 66 69 67 75 72 65 20 74 68 65 20 63 61 63 68 nfigure the cach
10139 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 e_size limit for
1013a 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 a cache..*/.sta
1013b 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
1013c 43 61 63 68 65 73 69 7a 65 28 73 71 6c 69 74 65 Cachesize(sqlite
1013d 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 69 6e 74 3_pcache *p, int
1013e 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61 63 68 65 nMax){. PCache
1013f 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 1 *pCache = (PCa
10140 63 68 65 31 20 2a 29 70 3b 0a 20 20 69 66 28 20 che1 *)p;. if(
10141 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
10142 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 le ){. pcache
10143 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10144 20 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 pcache1.nMaxP
10145 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20 2d 20 70 age += (nMax - p
10146 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 Cache->nMax);.
10147 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d pCache->nMax =
10148 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63 61 63 68 nMax;. pcach
10149 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 e1EnforceMaxPage
1014a 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 4c ();. pcache1L
1014b 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
1014c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
1014d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
1014e 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
1014f 50 61 67 65 63 6f 75 6e 74 20 6d 65 74 68 6f 64 Pagecount method
10150 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 . .*/.static int
10151 20 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e pcache1Pagecoun
10152 74 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 t(sqlite3_pcache
10153 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 *p){. int n;.
10154 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
10155 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28 28 50 43 ex();. n = ((PC
10156 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e 50 61 67 ache1 *)p)->nPag
10157 65 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 e;. pcache1Leav
10158 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 eMutex();. retu
10159 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 rn n;.}../*.** I
1015a 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
1015b 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
1015c 63 68 65 2e 78 46 65 74 63 68 20 6d 65 74 68 6f che.xFetch metho
1015d 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 63 68 20 d. .**.** Fetch
1015e 61 20 70 61 67 65 20 62 79 20 6b 65 79 20 76 61 a page by key va
1015f 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 lue..**.** Wheth
10160 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e 65 77 20 er or not a new
10161 70 61 67 65 20 6d 61 79 20 62 65 20 61 6c 6c 6f page may be allo
10162 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75 cated by this fu
10163 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f nction depends o
10164 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f n.** the value o
10165 66 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 f the createFlag
10166 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
10167 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 There are three
10168 20 64 69 66 66 65 72 65 6e 74 20 61 70 70 72 6f different appro
10169 61 63 68 65 73 20 74 6f 20 6f 62 74 61 69 6e 69 aches to obtaini
1016a 6e 67 20 73 70 61 63 65 20 66 6f 72 20 61 20 70 ng space for a p
1016b 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e age,.** dependin
1016c 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f g on the value o
1016d 66 20 70 61 72 61 6d 65 74 65 72 20 63 72 65 61 f parameter crea
1016e 74 65 46 6c 61 67 20 28 77 68 69 63 68 20 6d 61 teFlag (which ma
1016f 79 20 62 65 20 30 2c 20 31 20 6f 72 20 32 29 2e y be 0, 1 or 2).
10170 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52 65 67 61 .**.** 1. Rega
10171 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 61 rdless of the va
10172 6c 75 65 20 6f 66 20 63 72 65 61 74 65 46 6c 61 lue of createFla
10173 67 2c 20 74 68 65 20 63 61 63 68 65 20 69 73 20 g, the cache is
10174 73 65 61 72 63 68 65 64 20 66 6f 72 20 61 20 0a searched for a .
10175 2a 2a 20 20 20 20 20 20 63 6f 70 79 20 6f 66 20 ** copy of
10176 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
10177 67 65 2e 20 49 66 20 6f 6e 65 20 69 73 20 66 6f ge. If one is fo
10178 75 6e 64 2c 20 69 74 20 69 73 20 72 65 74 75 72 und, it is retur
10179 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 ned..**.** 2.
1017a 49 66 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 If createFlag==0
1017b 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 and the page is
1017c 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 not already in
1017d 74 68 65 20 63 61 63 68 65 2c 20 4e 55 4c 4c 20 the cache, NULL
1017e 69 73 0a 2a 2a 20 20 20 20 20 20 72 65 74 75 72 is.** retur
1017f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 ned..**.** 3.
10180 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 If createFlag is
10181 20 31 2c 20 74 68 65 20 63 61 63 68 65 20 69 73 1, the cache is
10182 20 6d 61 72 6b 65 64 20 61 73 20 70 75 72 67 65 marked as purge
10183 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70 61 67 able and the pag
10184 65 20 69 73 20 0a 2a 2a 20 20 20 20 20 20 6e 6f e is .** no
10185 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 t already in the
10186 20 63 61 63 68 65 2c 20 61 6e 64 20 69 66 20 65 cache, and if e
10187 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f 6c ither of the fol
10188 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 2c lowing are true,
10189 20 0a 2a 2a 20 20 20 20 20 20 72 65 74 75 72 6e .** return
1018a 20 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 NULL:.**.**
1018b 20 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65 (a) the numbe
1018c 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 r of pages pinne
1018d 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 d by the cache i
1018e 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a s greater than.*
1018f 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 * PCac
10190 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 he1.nMax, or.**
10191 20 20 20 20 20 20 28 62 29 20 74 68 65 20 6e 75 (b) the nu
10192 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 mber of pages pi
10193 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 nned by the cach
10194 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 e is greater tha
10195 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 n.** t
10196 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 he sum of nMax f
10197 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 or all purgeable
10198 20 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 68 caches, less th
10199 65 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 e sum of .**
1019a 20 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 nMin for
1019b 61 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 61 all other purgea
1019c 62 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a ble caches. .**.
1019d 2a 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 ** 4. If none
1019e 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 of the first thr
1019f 65 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 ee conditions ap
101a0 70 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 68 ply and the cach
101a1 65 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 e is marked.**
101a2 20 20 20 20 61 73 20 70 75 72 67 65 61 62 6c 65 as purgeable
101a3 2c 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 , and if one of
101a4 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 the following is
101a5 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 true:.**.**
101a6 20 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65 (a) The numbe
101a7 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 r of pages alloc
101a8 61 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63 ated for the cac
101a9 68 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 2a he is already .*
101aa 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 * PCac
101ab 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a he1.nMax, or.**.
101ac 2a 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 65 ** (b) The
101ad 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
101ae 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 allocated for a
101af 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 ll purgeable cac
101b0 68 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 hes is.**
101b1 20 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 61 already equa
101b2 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 l to or greater
101b3 74 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 than the sum of
101b4 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 nMax for all.**
101b5 20 20 20 20 20 20 20 20 20 20 70 75 72 67 65 61 purgea
101b6 62 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a ble caches,.**.*
101b7 2a 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 65 * then atte
101b8 6d 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 61 mpt to recycle a
101b9 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 4c page from the L
101ba 52 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 69 RU list. If it i
101bb 73 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 s the right.**
101bc 20 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 6e size, return
101bd 20 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 75 the recycled bu
101be 66 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c ffer. Otherwise,
101bf 20 66 72 65 65 20 74 68 65 20 62 75 66 66 65 72 free the buffer
101c0 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f and.** pro
101c1 63 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e 20 ceed to step 5.
101c2 0a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 .**.** 5. Othe
101c3 72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 rwise, allocate
101c4 61 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 77 and return a new
101c5 20 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f page buffer..*/
101c6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 .static void *pc
101c7 61 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 74 ache1Fetch(sqlit
101c8 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e e3_pcache *p, un
101c9 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c signed int iKey,
101ca 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 int createFlag)
101cb 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 {. unsigned int
101cc 20 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 nPinned;. PCac
101cd 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 he1 *pCache = (P
101ce 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 Cache1 *)p;. Pg
101cf 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b Hdr1 *pPage = 0;
101d0 0a 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 .. pcache1Enter
101d1 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63 Mutex();. if( c
101d2 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 reateFlag==1 ) s
101d3 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
101d4 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a nMalloc();.. /*
101d5 20 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68 Search the hash
101d6 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78 table for an ex
101d7 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f isting entry. */
101d8 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e . if( pCache->n
101d9 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e Hash>0 ){. un
101da 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 signed int h = i
101db 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 Key % pCache->nH
101dc 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61 ash;. for(pPa
101dd 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 ge=pCache->apHas
101de 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61 h[h]; pPage&&pPa
101df 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20 ge->iKey!=iKey;
101e0 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65 pPage=pPage->pNe
101e1 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 xt);. }.. if(
101e2 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46 pPage || createF
101e3 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 lag==0 ){. pc
101e4 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 ache1PinPage(pPa
101e5 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65 ge);. goto fe
101e6 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 tch_out;. }..
101e7 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 65 61 /* Step 3 of hea
101e8 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a der comment. */.
101e9 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63 nPinned = pCac
101ea 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63 he->nPage - pCac
101eb 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b he->nRecyclable;
101ec 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 . if( createFla
101ed 67 3d 3d 31 20 26 26 20 70 43 61 63 68 65 2d 3e g==1 && pCache->
101ee 62 50 75 72 67 65 61 62 6c 65 20 26 26 20 28 0a bPurgeable && (.
101ef 20 20 20 20 20 20 20 20 6e 50 69 6e 6e 65 64 3e nPinned>
101f0 3d 28 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 =(pcache1.nMaxPa
101f1 67 65 2b 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 2d ge+pCache->nMin-
101f2 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 pcache1.nMinPage
101f3 29 0a 20 20 20 20 20 7c 7c 20 6e 50 69 6e 6e 65 ). || nPinne
101f4 64 3e 3d 28 70 43 61 63 68 65 2d 3e 6e 4d 61 78 d>=(pCache->nMax
101f5 20 2a 20 39 20 2f 20 31 30 29 0a 20 20 29 29 7b * 9 / 10). )){
101f6 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f . goto fetch_
101f7 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 out;. }.. if(
101f8 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3e 3d 70 pCache->nPage>=p
101f9 43 61 63 68 65 2d 3e 6e 48 61 73 68 20 26 26 20 Cache->nHash &&
101fa 70 63 61 63 68 65 31 52 65 73 69 7a 65 48 61 73 pcache1ResizeHas
101fb 68 28 70 43 61 63 68 65 29 20 29 7b 0a 20 20 20 h(pCache) ){.
101fc 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b goto fetch_out;
101fd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 . }.. /* Step
101fe 34 2e 20 54 72 79 20 74 6f 20 72 65 63 79 63 6c 4. Try to recycl
101ff 65 20 61 20 70 61 67 65 20 62 75 66 66 65 72 20 e a page buffer
10200 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 if appropriate.
10201 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d */. if( pCache-
10202 3e 62 50 75 72 67 65 61 62 6c 65 20 26 26 20 70 >bPurgeable && p
10203 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 cache1.pLruTail
10204 26 26 20 28 0a 20 20 20 20 20 20 70 43 61 63 68 && (. pCach
10205 65 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 e->nPage>=pCache
10206 2d 3e 6e 4d 61 78 2d 31 20 7c 7c 20 70 63 61 63 ->nMax-1 || pcac
10207 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 he1.nCurrentPage
10208 3e 3d 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 >=pcache1.nMaxPa
10209 67 65 0a 20 20 29 29 7b 0a 20 20 20 20 70 50 61 ge. )){. pPa
1020a 67 65 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 ge = pcache1.pLr
1020b 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 uTail;. pcach
1020c 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 e1RemoveFromHash
1020d 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63 61 (pPage);. pca
1020e 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 che1PinPage(pPag
1020f 65 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 e);. if( pPag
10210 65 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 e->pCache->szPag
10211 65 21 3d 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 e!=pCache->szPag
10212 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 e ){. pcach
10213 65 31 46 72 65 65 50 61 67 65 28 70 50 61 67 65 e1FreePage(pPage
10214 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d );. pPage =
10215 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
10216 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43 75 pcache1.nCu
10217 72 72 65 6e 74 50 61 67 65 20 2d 3d 20 28 70 50 rrentPage -= (pP
10218 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 62 50 75 age->pCache->bPu
10219 72 67 65 61 62 6c 65 20 2d 20 70 43 61 63 68 65 rgeable - pCache
1021a 2d 3e 62 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 ->bPurgeable);.
1021b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 }. }.. /* S
1021c 74 65 70 20 35 2e 20 49 66 20 61 20 75 73 61 62 tep 5. If a usab
1021d 6c 65 20 70 61 67 65 20 62 75 66 66 65 72 20 68 le page buffer h
1021e 61 73 20 73 74 69 6c 6c 20 6e 6f 74 20 62 65 65 as still not bee
1021f 6e 20 66 6f 75 6e 64 2c 20 0a 20 20 2a 2a 20 61 n found, . ** a
10220 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 ttempt to alloca
10221 74 65 20 61 20 6e 65 77 20 6f 6e 65 2e 20 0a 20 te a new one. .
10222 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67 65 */. if( !pPage
10223 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 ){. pPage =
10224 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 pcache1AllocPage
10225 28 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 (pCache);. }..
10226 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 if( pPage ){.
10227 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
10228 20 3d 20 69 4b 65 79 20 25 20 70 43 61 63 68 65 = iKey % pCache
10229 2d 3e 6e 48 61 73 68 3b 0a 20 20 20 20 2a 28 76 ->nHash;. *(v
1022a 6f 69 64 20 2a 2a 29 28 50 47 48 44 52 31 5f 54 oid **)(PGHDR1_T
1022b 4f 5f 50 41 47 45 28 70 50 61 67 65 29 29 20 3d O_PAGE(pPage)) =
1022c 20 30 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 0;. pCache->
1022d 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 nPage++;. pPa
1022e 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b ge->iKey = iKey;
1022f 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 . pPage->pNex
10230 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 t = pCache->apHa
10231 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 sh[h];. pPage
10232 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 ->pCache = pCach
10233 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c e;. pPage->pL
10234 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 ruPrev = 0;.
10235 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
10236 3d 20 30 3b 0a 20 20 20 20 70 43 61 63 68 65 2d = 0;. pCache-
10237 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 >apHash[h] = pPa
10238 67 65 3b 0a 20 20 7d 0a 0a 66 65 74 63 68 5f 6f ge;. }..fetch_o
10239 75 74 3a 0a 20 20 69 66 28 20 70 50 61 67 65 20 ut:. if( pPage
1023a 26 26 20 69 4b 65 79 3e 70 43 61 63 68 65 2d 3e && iKey>pCache->
1023b 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 iMaxKey ){. p
1023c 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d Cache->iMaxKey =
1023d 20 69 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 iKey;. }. if(
1023e 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 createFlag==1 )
1023f 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
10240 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 70 63 61 nMalloc();. pca
10241 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10242 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 ;. return (pPag
10243 65 20 3f 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 e ? PGHDR1_TO_PA
10244 47 45 28 70 50 61 67 65 29 20 3a 20 30 29 3b 0a GE(pPage) : 0);.
10245 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d }.../*.** Implem
10246 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
10247 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
10248 55 6e 70 69 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a Unpin method..**
10249 0a 2a 2a 20 4d 61 72 6b 20 61 20 70 61 67 65 20 .** Mark a page
1024a 61 73 20 75 6e 70 69 6e 6e 65 64 20 28 65 6c 69 as unpinned (eli
1024b 67 69 62 6c 65 20 66 6f 72 20 61 73 79 6e 63 68 gible for asynch
1024c 72 6f 6e 6f 75 73 20 72 65 63 79 63 6c 69 6e 67 ronous recycling
1024d 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
1024e 64 20 70 63 61 63 68 65 31 55 6e 70 69 6e 28 73 d pcache1Unpin(s
1024f 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 qlite3_pcache *p
10250 2c 20 76 6f 69 64 20 2a 70 50 67 2c 20 69 6e 74 , void *pPg, int
10251 20 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 29 7b reuseUnlikely){
10252 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 . PCache1 *pCac
10253 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 he = (PCache1 *)
10254 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 p;. PgHdr1 *pPa
10255 67 65 20 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 ge = PAGE_TO_PGH
10256 44 52 31 28 70 50 67 29 3b 0a 0a 20 20 70 63 61 DR1(pPg);.. pca
10257 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10258 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 61 6e ;.. /* It is an
10259 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 error to call t
1025a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 his function if
1025b 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 the page is alre
1025c 61 64 79 20 0a 20 20 2a 2a 20 70 61 72 74 20 6f ady . ** part o
1025d 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 f the global LRU
1025e 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 list.. */. as
1025f 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 4c 72 sert( pPage->pLr
10260 75 50 72 65 76 3d 3d 30 20 26 26 20 70 50 61 67 uPrev==0 && pPag
10261 65 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d 30 20 29 e->pLruNext==0 )
10262 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 ;. assert( pcac
10263 68 65 31 2e 70 4c 72 75 48 65 61 64 21 3d 70 50 he1.pLruHead!=pP
10264 61 67 65 20 26 26 20 70 63 61 63 68 65 31 2e 70 age && pcache1.p
10265 4c 72 75 54 61 69 6c 21 3d 70 50 61 67 65 20 29 LruTail!=pPage )
10266 3b 0a 0a 20 20 69 66 28 20 72 65 75 73 65 55 6e ;.. if( reuseUn
10267 6c 69 6b 65 6c 79 20 7c 7c 20 70 63 61 63 68 65 likely || pcache
10268 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70 1.nCurrentPage>p
10269 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 cache1.nMaxPage
1026a 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 ){. pcache1Re
1026b 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61 moveFromHash(pPa
1026c 67 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 ge);. pcache1
1026d 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b FreePage(pPage);
1026e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1026f 20 41 64 64 20 74 68 65 20 70 61 67 65 20 74 6f Add the page to
10270 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 the global LRU
10271 6c 69 73 74 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 list. Normally,
10272 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65 the page is adde
10273 64 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 d to. ** the
10274 68 65 61 64 20 6f 66 20 74 68 65 20 6c 69 73 74 head of the list
10275 20 28 6c 61 73 74 20 70 61 67 65 20 74 6f 20 62 (last page to b
10276 65 20 72 65 63 79 63 6c 65 64 29 2e 20 48 6f 77 e recycled). How
10277 65 76 65 72 2c 20 69 66 20 74 68 65 20 0a 20 20 ever, if the .
10278 20 20 2a 2a 20 72 65 75 73 65 55 6e 6c 69 6b 65 ** reuseUnlike
10279 6c 79 20 66 6c 61 67 20 70 61 73 73 65 64 20 74 ly flag passed t
1027a 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 o this function
1027b 69 73 20 74 72 75 65 2c 20 74 68 65 20 70 61 67 is true, the pag
1027c 65 20 69 73 20 61 64 64 65 64 0a 20 20 20 20 2a e is added. *
1027d 2a 20 74 6f 20 74 68 65 20 74 61 69 6c 20 6f 66 * to the tail of
1027e 20 74 68 65 20 6c 69 73 74 20 28 66 69 72 73 74 the list (first
1027f 20 70 61 67 65 20 74 6f 20 62 65 20 72 65 63 79 page to be recy
10280 63 6c 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 cled).. */.
10281 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c if( pcache1.pL
10282 72 75 48 65 61 64 20 29 7b 0a 20 20 20 20 20 20 ruHead ){.
10283 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
10284 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50 61 ->pLruPrev = pPa
10285 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d ge;. pPage-
10286 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 63 61 63 >pLruNext = pcac
10287 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 0a 20 20 he1.pLruHead;.
10288 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 pcache1.pLru
10289 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 Head = pPage;.
1028a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
1028b 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 cache1.pLruTail
1028c 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70 = pPage;. p
1028d 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 cache1.pLruHead
1028e 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 = pPage;. }.
1028f 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 pCache->nRecy
10290 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 clable++;. }..
10291 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10292 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ex();.}../*.** I
10293 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
10294 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
10295 63 68 65 2e 78 52 65 6b 65 79 20 6d 65 74 68 6f che.xRekey metho
10296 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f d. .*/.static vo
10297 69 64 20 70 63 61 63 68 65 31 52 65 6b 65 79 28 id pcache1Rekey(
10298 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 . sqlite3_pcach
10299 65 20 2a 70 2c 0a 20 20 76 6f 69 64 20 2a 70 50 e *p,. void *pP
1029a 67 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e g,. unsigned in
1029b 74 20 69 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 6e t iOld,. unsign
1029c 65 64 20 69 6e 74 20 69 4e 65 77 0a 29 7b 0a 20 ed int iNew.){.
1029d 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 PCache1 *pCache
1029e 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b = (PCache1 *)p;
1029f 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 . PgHdr1 *pPage
102a0 20 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 = PAGE_TO_PGHDR
102a1 31 28 70 50 67 29 3b 0a 20 20 50 67 48 64 72 31 1(pPg);. PgHdr1
102a2 20 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 **pp;. unsigne
102a3 64 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 d int h; . asse
102a4 72 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d rt( pPage->iKey=
102a5 3d 69 4f 6c 64 20 29 3b 0a 0a 20 20 70 63 61 63 =iOld );.. pcac
102a6 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
102a7 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 43 61 .. h = iOld%pCa
102a8 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 70 che->nHash;. pp
102a9 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 = &pCache->apHa
102aa 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 28 20 sh[h];. while(
102ab 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 7b 0a (*pp)!=pPage ){.
102ac 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d pp = &(*pp)-
102ad 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 >pNext;. }. *p
102ae 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 p = pPage->pNext
102af 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 70 43 ;.. h = iNew%pC
102b0 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 ache->nHash;. p
102b1 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 Page->iKey = iNe
102b2 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 w;. pPage->pNex
102b3 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 t = pCache->apHa
102b4 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 65 2d sh[h];. pCache-
102b5 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 >apHash[h] = pPa
102b6 67 65 3b 0a 0a 20 20 69 66 28 20 69 4e 65 77 3e ge;.. if( iNew>
102b7 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 pCache->iMaxKey
102b8 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69 ){. pCache->i
102b9 4d 61 78 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 MaxKey = iNew;.
102ba 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 }.. pcache1Lea
102bb 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a veMutex();.}../*
102bc 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
102bd 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
102be 33 5f 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 3_pcache.xTrunca
102bf 74 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a te method. .**.*
102c0 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75 6e * Discard all un
102c1 70 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e 20 pinned pages in
102c2 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20 61 the cache with a
102c3 20 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71 75 page number equ
102c4 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 61 al to.** or grea
102c5 74 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65 74 ter than paramet
102c6 65 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 er iLimit. Any p
102c7 69 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74 68 inned pages with
102c8 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a a page number.*
102c9 2a 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 * equal to or gr
102ca 65 61 74 65 72 20 74 68 61 6e 20 69 4c 69 6d 69 eater than iLimi
102cb 74 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 t are implicitly
102cc 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 unpinned..*/.st
102cd 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
102ce 31 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 1Truncate(sqlite
102cf 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 3_pcache *p, uns
102d0 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 igned int iLimit
102d1 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 ){. PCache1 *pC
102d2 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 ache = (PCache1
102d3 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e *)p;. pcache1En
102d4 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 terMutex();. if
102d5 28 20 69 4c 69 6d 69 74 3c 3d 70 43 61 63 68 65 ( iLimit<=pCache
102d6 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 ->iMaxKey ){.
102d7 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 pcache1Truncate
102d8 55 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20 69 Unsafe(pCache, i
102d9 4c 69 6d 69 74 29 3b 0a 20 20 20 20 70 43 61 63 Limit);. pCac
102da 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4c he->iMaxKey = iL
102db 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a 20 20 70 63 imit-1;. }. pc
102dc 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
102dd 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
102de 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
102df 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
102e0 2e 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 .xDestroy method
102e1 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 . .**.** Destroy
102e2 20 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 a cache allocat
102e3 65 64 20 75 73 69 6e 67 20 70 63 61 63 68 65 31 ed using pcache1
102e4 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a 73 74 61 Create()..*/.sta
102e5 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
102e6 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f Destroy(sqlite3_
102e7 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 50 43 pcache *p){. PC
102e8 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 ache1 *pCache =
102e9 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 (PCache1 *)p;.
102ea 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
102eb 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 54 72 x();. pcache1Tr
102ec 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 uncateUnsafe(pCa
102ed 63 68 65 2c 20 30 29 3b 0a 20 20 70 63 61 63 68 che, 0);. pcach
102ee 65 31 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20 70 e1.nMaxPage -= p
102ef 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 70 Cache->nMax;. p
102f0 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 cache1.nMinPage
102f1 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b -= pCache->nMin;
102f2 0a 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 . pcache1Enforc
102f3 65 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 70 63 eMaxPage();. pc
102f4 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
102f5 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
102f6 65 28 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 e(pCache->apHash
102f7 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
102f8 65 28 70 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a e(pCache);.}../*
102f9 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
102fa 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69 n is called duri
102fb 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ng initializatio
102fc 6e 20 28 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 n (sqlite3_initi
102fd 61 6c 69 7a 65 28 29 29 20 74 6f 0a 2a 2a 20 69 alize()) to.** i
102fe 6e 73 74 61 6c 6c 20 74 68 65 20 64 65 66 61 75 nstall the defau
102ff 6c 74 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 lt pluggable cac
10300 68 65 20 6d 6f 64 75 6c 65 2c 20 61 73 73 75 6d he module, assum
10301 69 6e 67 20 74 68 65 20 75 73 65 72 20 68 61 73 ing the user has
10302 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 not.** already
10303 70 72 6f 76 69 64 65 64 20 61 6e 20 61 6c 74 65 provided an alte
10304 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 rnative..*/.SQLI
10305 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
10306 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 sqlite3PCacheSet
10307 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 Default(void){.
10308 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
10309 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 64 pcache_methods d
1030a 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 efaultMethods =
1030b 7b 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 {. 0,
1030c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1030d 2a 20 70 41 72 67 20 2a 2f 0a 20 20 20 20 70 63 * pArg */. pc
1030e 61 63 68 65 31 49 6e 69 74 2c 20 20 20 20 20 20 ache1Init,
1030f 20 20 20 20 20 20 20 2f 2a 20 78 49 6e 69 74 20 /* xInit
10310 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 53 68 */. pcache1Sh
10311 75 74 64 6f 77 6e 2c 20 20 20 20 20 20 20 20 20 utdown,
10312 2f 2a 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f 0a /* xShutdown */.
10313 20 20 20 20 70 63 61 63 68 65 31 43 72 65 61 74 pcache1Creat
10314 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e, /*
10315 78 43 72 65 61 74 65 20 2a 2f 0a 20 20 20 20 70 xCreate */. p
10316 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 2c cache1Cachesize,
10317 20 20 20 20 20 20 20 20 2f 2a 20 78 43 61 63 68 /* xCach
10318 65 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63 61 esize */. pca
10319 63 68 65 31 50 61 67 65 63 6f 75 6e 74 2c 20 20 che1Pagecount,
1031a 20 20 20 20 20 20 2f 2a 20 78 50 61 67 65 63 6f /* xPageco
1031b 75 6e 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 unt */. pcach
1031c 65 31 46 65 74 63 68 2c 20 20 20 20 20 20 20 20 e1Fetch,
1031d 20 20 20 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f /* xFetch */
1031e 0a 20 20 20 20 70 63 61 63 68 65 31 55 6e 70 69 . pcache1Unpi
1031f 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a n, /*
10320 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 70 xUnpin */. p
10321 63 61 63 68 65 31 52 65 6b 65 79 2c 20 20 20 20 cache1Rekey,
10322 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6b 65 /* xReke
10323 79 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 y */. pcache1
10324 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 Truncate,
10325 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a /* xTruncate *
10326 2f 0a 20 20 20 20 70 63 61 63 68 65 31 44 65 73 /. pcache1Des
10327 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 2f troy /
10328 2a 20 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20 20 * xDestroy */.
10329 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e };. sqlite3_con
1032a 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 fig(SQLITE_CONFI
1032b 47 5f 50 43 41 43 48 45 2c 20 26 64 65 66 61 75 G_PCACHE, &defau
1032c 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 ltMethods);.}..#
1032d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
1032e 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 BLE_MEMORY_MANAG
1032f 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 EMENT./*.** This
10330 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
10331 6c 65 64 20 74 6f 20 66 72 65 65 20 73 75 70 65 led to free supe
10332 72 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63 61 rfluous dynamica
10333 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 lly allocated me
10334 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 mory.** held by
10335 74 68 65 20 70 61 67 65 72 20 73 79 73 74 65 6d the pager system
10336 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73 65 20 . Memory in use
10337 62 79 20 61 6e 79 20 53 51 4c 69 74 65 20 70 61 by any SQLite pa
10338 67 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a ger allocated.**
10339 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 by the current
1033a 74 68 72 65 61 64 20 6d 61 79 20 62 65 20 73 71 thread may be sq
1033b 6c 69 74 65 33 5f 66 72 65 65 28 29 65 64 2e 0a lite3_free()ed..
1033c 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74 68 **.** nReq is th
1033d 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1033e 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71 75 s of memory requ
1033f 69 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73 20 ired. Once this
10340 6d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65 6e much has.** been
10341 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20 66 released, the f
10342 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e unction returns.
10343 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
10344 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e e is the total n
10345 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 20 62 79 74 umber .** of byt
10346 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 6c es of memory rel
10347 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 eased..*/.SQLITE
10348 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
10349 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
1034a 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65 71 eMemory(int nReq
1034b 29 7b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d ){. int nFree =
1034c 20 30 3b 0a 20 20 69 66 28 20 70 63 61 63 68 65 0;. if( pcache
1034d 31 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b 0a 20 1.pStart==0 ){.
1034e 20 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 PgHdr1 *p;.
1034f 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10350 74 65 78 28 29 3b 0a 20 20 20 20 77 68 69 6c 65 tex();. while
10351 28 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 72 ( (nReq<0 || nFr
10352 65 65 3c 6e 52 65 71 29 20 26 26 20 28 70 3d 70 ee<nReq) && (p=p
10353 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 29 cache1.pLruTail)
10354 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 ){. nFree
10355 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 += sqlite3Malloc
10356 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 70 Size(p);. p
10357 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 cache1PinPage(p)
10358 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 52 ;. pcache1R
10359 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 29 emoveFromHash(p)
1035a 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 46 ;. pcache1F
1035b 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 20 20 reePage(p);.
1035c 7d 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 }. pcache1Lea
1035d 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 veMutex();. }.
1035e 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d return nFree;.}
1035f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
10360 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f E_ENABLE_MEMORY_
10361 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 23 MANAGEMENT */..#
10362 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
10363 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e T./*.** This fun
10364 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 ction is used by
10365 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 test procedures
10366 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20 to inspect the
10367 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0a 2a internal state.*
10368 2a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 * of the global
10369 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cache..*/.SQLITE
1036a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1036b 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74 73 lite3PcacheStats
1036c 28 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72 65 (. int *pnCurre
1036d 6e 74 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a nt, /* OUT:
1036e 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
1036f 20 70 61 67 65 73 20 63 61 63 68 65 64 20 2a 2f pages cached */
10370 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c 20 20 . int *pnMax,
10371 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
10372 47 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 63 Global maximum c
10373 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 ache size */. i
10374 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 20 20 20 20 nt *pnMin,
10375 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 75 6d 20 /* OUT: Sum
10376 6f 66 20 50 43 61 63 68 65 31 2e 6e 4d 69 6e 20 of PCache1.nMin
10377 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 for purgeable ca
10378 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 ches */. int *p
10379 6e 52 65 63 79 63 6c 61 62 6c 65 20 20 20 20 2f nRecyclable /
1037a 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d * OUT: Total num
1037b 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 76 61 ber of pages ava
1037c 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 63 79 63 ilable for recyc
1037d 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 ling */.){. PgH
1037e 64 72 31 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 52 dr1 *p;. int nR
1037f 65 63 79 63 6c 61 62 6c 65 20 3d 20 30 3b 0a 20 ecyclable = 0;.
10380 20 66 6f 72 28 70 3d 70 63 61 63 68 65 31 2e 70 for(p=pcache1.p
10381 4c 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d LruHead; p; p=p-
10382 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 >pLruNext){.
10383 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 nRecyclable++;.
10384 20 7d 0a 20 20 2a 70 6e 43 75 72 72 65 6e 74 20 }. *pnCurrent
10385 3d 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 = pcache1.nCurre
10386 6e 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 78 ntPage;. *pnMax
10387 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 = pcache1.nMaxP
10388 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d 20 age;. *pnMin =
10389 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 pcache1.nMinPage
1038a 3b 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c ;. *pnRecyclabl
1038b 65 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b e = nRecyclable;
1038c 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a .}.#endif../****
1038d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1038e 66 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a f pcache1.c ****
1038f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10391 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
10392 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
10393 20 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20 2a file rowset.c *
10394 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10395 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10396 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
10397 20 32 30 30 38 20 44 65 63 65 6d 62 65 72 20 33 2008 December 3
10398 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
10399 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1039a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1039b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1039c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1039d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1039e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1039f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
103a0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
103a1 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
103a2 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
103a3 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
103a4 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
103a5 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
103a6 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
103a7 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
103a8 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
103a9 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
103aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
103ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
103ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
103ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
103ae 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c **.** This modul
103af 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 e implements an
103b0 6f 62 6a 65 63 74 20 77 65 20 63 61 6c 6c 20 61 object we call a
103b1 20 22 52 6f 77 20 53 65 74 22 2e 0a 2a 2a 0a 2a "Row Set"..**.*
103b2 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a * The RowSet obj
103b3 65 63 74 20 69 73 20 61 20 62 61 67 20 6f 66 20 ect is a bag of
103b4 72 6f 77 69 64 73 2e 20 20 52 6f 77 69 64 73 0a rowids. Rowids.
103b5 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
103b6 69 6e 74 6f 20 74 68 65 20 62 61 67 20 69 6e 20 into the bag in
103b7 61 6e 20 61 72 62 69 74 72 61 72 79 20 6f 72 64 an arbitrary ord
103b8 65 72 2e 20 20 54 68 65 6e 20 74 68 65 79 20 61 er. Then they a
103b9 72 65 0a 2a 2a 20 70 75 6c 6c 65 64 20 66 72 6f re.** pulled fro
103ba 6d 20 74 68 65 20 62 61 67 20 69 6e 20 73 6f 72 m the bag in sor
103bb 74 65 64 20 6f 72 64 65 72 2e 20 20 52 6f 77 69 ted order. Rowi
103bc 64 73 20 6f 6e 6c 79 20 61 70 70 65 61 72 20 69 ds only appear i
103bd 6e 20 74 68 65 0a 2a 2a 20 62 61 67 20 6f 6e 63 n the.** bag onc
103be 65 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 e. If the same
103bf 72 6f 77 69 64 20 69 73 20 69 6e 73 65 72 74 65 rowid is inserte
103c0 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 d multiple times
103c1 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 , the.** second
103c2 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 69 and subsequent i
103c3 6e 73 65 72 74 73 20 6d 61 6b 65 20 6e 6f 20 64 nserts make no d
103c4 69 66 66 65 72 65 6e 63 65 20 6f 6e 20 74 68 65 ifference on the
103c5 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 54 output..**.** T
103c6 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
103c7 6f 6e 20 61 63 63 75 6d 75 6c 61 74 65 73 20 72 on accumulates r
103c8 6f 77 69 64 73 20 69 6e 20 61 20 6c 69 6e 6b 65 owids in a linke
103c9 64 20 6c 69 73 74 2e 20 20 46 6f 72 0a 2a 2a 20 d list. For.**
103ca 6f 75 74 70 75 74 2c 20 69 74 20 66 69 72 73 74 output, it first
103cb 20 73 6f 72 74 73 20 74 68 65 20 6c 69 6e 6b 65 sorts the linke
103cc 64 20 6c 69 73 74 20 28 72 65 6d 6f 76 69 6e 67 d list (removing
103cd 20 64 75 70 6c 69 63 61 74 65 73 20 64 75 72 69 duplicates duri
103ce 6e 67 0a 2a 2a 20 74 68 65 20 73 6f 72 74 29 20 ng.** the sort)
103cf 74 68 65 6e 20 72 65 74 75 72 6e 73 20 65 6c 65 then returns ele
103d0 6d 65 6e 74 73 20 6f 6e 65 20 62 79 20 6f 6e 65 ments one by one
103d1 20 62 79 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 by walking the
103d2 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 42 69 67 20 list..**.** Big
103d3 63 68 75 6e 6b 73 20 6f 66 20 72 6f 77 69 64 2f chunks of rowid/
103d4 6e 65 78 74 2d 70 74 72 20 70 61 69 72 73 20 61 next-ptr pairs a
103d5 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 re allocated at
103d6 61 20 74 69 6d 65 2c 20 74 6f 0a 2a 2a 20 72 65 a time, to.** re
103d7 64 75 63 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 duce the malloc
103d8 6f 76 65 72 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 overhead..**.**
103d9 24 49 64 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 $Id: rowset.c,v
103da 31 2e 33 20 32 30 30 39 2f 30 31 2f 31 33 20 32 1.3 2009/01/13 2
103db 30 3a 31 34 3a 31 36 20 64 72 68 20 45 78 70 20 0:14:16 drh Exp
103dc 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
103dd 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 number of rowset
103de 20 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c entries per all
103df 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a ocation chunk..*
103e0 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 /.#define ROWSET
103e1 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b _ENTRY_PER_CHUNK
103e2 20 20 36 33 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 63../*.** Each
103e3 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 entry in a RowS
103e4 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 et is an instanc
103e5 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
103e6 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 3a ng.** structure:
103e7 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 .*/.struct RowSe
103e8 74 45 6e 74 72 79 20 7b 20 20 20 20 20 20 20 20 tEntry {
103e9 20 20 20 20 0a 20 20 69 36 34 20 76 3b 20 20 20 . i64 v;
103ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
103eb 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 76 61 /* ROWID va
103ec 6c 75 65 20 66 6f 72 20 74 68 69 73 20 65 6e 74 lue for this ent
103ed 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ry */. struct R
103ee 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4e 65 78 owSetEntry *pNex
103ef 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e t; /* Next en
103f0 74 72 79 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 try on a list of
103f1 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 2a 2f 0a all entries */.
103f2 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 65 78 20 };../*.** Index
103f3 65 6e 74 72 69 65 73 20 61 72 65 20 61 6c 6c 6f entries are allo
103f4 63 61 74 65 64 20 69 6e 20 6c 61 72 67 65 20 63 cated in large c
103f5 68 75 6e 6b 73 20 28 69 6e 73 74 61 6e 63 65 73 hunks (instances
103f6 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f of the.** follo
103f7 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 29 20 wing structure)
103f8 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 to reduce memory
103f9 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 76 65 72 allocation over
103fa 68 65 61 64 2e 20 20 54 68 65 0a 2a 2a 20 63 68 head. The.** ch
103fb 75 6e 6b 73 20 61 72 65 20 6b 65 70 74 20 6f 6e unks are kept on
103fc 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 a linked list s
103fd 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 o that they can
103fe 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 0a 2a be deallocated.*
103ff 2a 20 77 68 65 6e 20 74 68 65 20 52 6f 77 53 65 * when the RowSe
10400 74 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0a t is destroyed..
10401 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74 */.struct RowSet
10402 43 68 75 6e 6b 20 7b 0a 20 20 73 74 72 75 63 74 Chunk {. struct
10403 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e RowSetChunk *pN
10404 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
10405 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 6f /* Next chunk o
10406 6e 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 n list of them a
10407 6c 6c 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ll */. struct R
10408 6f 77 53 65 74 45 6e 74 72 79 20 61 45 6e 74 72 owSetEntry aEntr
10409 79 5b 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 y[ROWSET_ENTRY_P
1040a 45 52 5f 43 48 55 4e 4b 5d 3b 20 2f 2a 20 41 6c ER_CHUNK]; /* Al
1040b 6c 6f 63 61 74 65 64 20 65 6e 74 72 69 65 73 20 located entries
1040c 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 52 */.};../*.** A R
1040d 6f 77 53 65 74 20 69 6e 20 61 6e 20 69 6e 73 74 owSet in an inst
1040e 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
1040f 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
10410 0a 2a 2a 0a 2a 2a 20 41 20 74 79 70 65 64 65 66 .**.** A typedef
10411 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
10412 72 65 20 69 66 20 66 6f 75 6e 64 20 69 6e 20 73 re if found in s
10413 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 73 qliteInt.h..*/.s
10414 74 72 75 63 74 20 52 6f 77 53 65 74 20 7b 0a 20 truct RowSet {.
10415 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 struct RowSetCh
10416 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 unk *pChunk;
10417 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 63 /* List of all c
10418 68 75 6e 6b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 hunk allocations
10419 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
1041a 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
1041b 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1041c 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1041d 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
1041e 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b etEntry *pEntry;
1041f 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65 /* List of e
10420 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 6f ntries in the ro
10421 77 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 wset */. struct
10422 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c RowSetEntry *pL
10423 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 ast; /* Last
10424 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 45 entry on the pE
10425 6e 74 72 79 20 6c 69 73 74 20 2a 2f 0a 20 20 73 ntry list */. s
10426 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
10427 79 20 2a 70 46 72 65 73 68 3b 20 20 20 20 2f 2a y *pFresh; /*
10428 20 53 6f 75 72 63 65 20 6f 66 20 6e 65 77 20 65 Source of new e
10429 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 2a 2f 0a ntry objects */.
1042a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20 20 20 u16 nFresh;
1042b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1042c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 /* Number of ob
1042d 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73 68 20 jects on pFresh
1042e 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74 65 64 */. u8 isSorted
1042f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10430 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 /* True if c
10431 6f 6e 74 65 6e 74 20 69 73 20 73 6f 72 74 65 64 ontent is sorted
10432 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 */.};../*.** Tu
10433 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 rn bulk memory i
10434 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f 62 6a nto a RowSet obj
10435 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 ect. N bytes of
10436 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 memory.** are a
10437 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53 70 61 vailable at pSpa
10438 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f 69 6e ce. The db poin
10439 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20 61 ter is used as a
1043a 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a memory context.
1043b 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 73 65 ** for any subse
1043c 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e quent allocation
1043d 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 6f s that need to o
1043e 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 ccur..** Return
1043f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
10440 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 new RowSet obje
10441 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 ct..**.** It mus
10442 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 t be the case th
10443 61 74 20 4e 20 69 73 20 73 75 66 66 69 63 69 65 at N is sufficie
10444 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 nt to make a Row
10445 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 set. If not.**
10446 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 an assertion fau
10447 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a lt occurs..** .*
10448 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67 65 72 * If N is larger
10449 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 than the minimu
1044a 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72 70 6c m, use the surpl
1044b 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69 61 6c us as an initial
1044c 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f .** allocation o
1044d 66 20 65 6e 74 72 69 65 73 20 61 76 61 69 6c 61 f entries availa
1044e 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 ble to be filled
1044f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10450 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 ATE RowSet *sqli
10451 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71 te3RowSetInit(sq
10452 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 lite3 *db, void
10453 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 *pSpace, unsigne
10454 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 d int N){. RowS
10455 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 et *p;. assert(
10456 20 4e 20 3e 3d 20 73 69 7a 65 6f 66 28 2a 70 29 N >= sizeof(*p)
10457 20 29 3b 0a 20 20 70 20 3d 20 70 53 70 61 63 65 );. p = pSpace
10458 3b 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 ;. p->pChunk =
10459 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0;. p->db = db;
1045a 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 . p->pEntry = 0
1045b 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 ;. p->pLast = 0
1045c 3b 0a 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 ;. p->pFresh =
1045d 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e (struct RowSetEn
1045e 74 72 79 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d try*)&p[1];. p-
1045f 3e 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 28 >nFresh = (u16)(
10460 28 4e 20 2d 20 73 69 7a 65 6f 66 28 2a 70 29 29 (N - sizeof(*p))
10461 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 52 /sizeof(struct R
10462 6f 77 53 65 74 45 6e 74 72 79 29 29 3b 0a 20 20 owSetEntry));.
10463 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b p->isSorted = 1;
10464 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
10465 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 /*.** Deallocate
10466 20 61 6c 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d all chunks from
10467 20 61 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 a RowSet..*/.SQ
10468 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
10469 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 d sqlite3RowSetC
1046a 6c 65 61 72 28 52 6f 77 53 65 74 20 2a 70 29 7b lear(RowSet *p){
1046b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
1046c 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a Chunk *pChunk, *
1046d 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 66 6f pNextChunk;. fo
1046e 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 r(pChunk=p->pChu
1046f 6e 6b 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68 75 nk; pChunk; pChu
10470 6e 6b 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29 nk = pNextChunk)
10471 7b 0a 20 20 20 20 70 4e 65 78 74 43 68 75 6e 6b {. pNextChunk
10472 20 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 = pChunk->pNext
10473 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
10474 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e ree(p->db, pChun
10475 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 k);. }. p->pCh
10476 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 unk = 0;. p->nF
10477 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 resh = 0;. p->p
10478 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e Entry = 0;. p->
10479 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e pLast = 0;. p->
1047a 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a isSorted = 1;.}.
1047b 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 ./*.** Insert a
1047c 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 new value into a
1047d 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 RowSet..**.** T
1047e 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
1047f 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 61 flag of the data
10480 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
10481 69 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d is set if a.** m
10482 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
10483 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 fails..*/.SQLIT
10484 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
10485 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 qlite3RowSetInse
10486 72 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 rt(RowSet *p, i6
10487 34 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 4 rowid){. stru
10488 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
10489 70 45 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 pEntry;. struct
1048a 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c RowSetEntry *pL
1048b 61 73 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 ast;. if( p==0
1048c 29 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 4d 75 ) return; /* Mu
1048d 73 74 20 68 61 76 65 20 62 65 65 6e 20 61 20 6d st have been a m
1048e 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f alloc failure */
1048f 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 65 73 68 . if( p->nFresh
10490 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 ==0 ){. struc
10491 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 t RowSetChunk *p
10492 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 New;. pNew =
10493 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
10494 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 aw(p->db, sizeof
10495 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 69 66 (*pNew));. if
10496 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 ( pNew==0 ){.
10497 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
10498 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 . pNew->pNext
10499 20 3d 20 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 = p->pChunk;.
1049a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e p->pChunk = pN
1049b 65 77 3b 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 ew;. p->pFres
1049c 68 20 3d 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 h = pNew->aEntry
1049d 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 ;. p->nFresh
1049e 3d 20 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 = ROWSET_ENTRY_P
1049f 45 52 5f 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 ER_CHUNK;. }.
104a0 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 pEntry = p->pFre
104a1 73 68 2b 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 sh++;. p->nFres
104a2 68 2d 2d 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 h--;. pEntry->v
104a3 20 3d 20 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 = rowid;. pEnt
104a4 72 79 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 ry->pNext = 0;.
104a5 20 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 pLast = p->pLas
104a6 74 3b 0a 20 20 69 66 28 20 70 4c 61 73 74 20 29 t;. if( pLast )
104a7 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 73 53 {. if( p->isS
104a8 6f 72 74 65 64 20 26 26 20 72 6f 77 69 64 3c 3d orted && rowid<=
104a9 70 4c 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 pLast->v ){.
104aa 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 p->isSorted =
104ab 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 0;. }. pLa
104ac 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 45 6e 74 st->pNext = pEnt
104ad 72 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ry;. }else{.
104ae 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 assert( p->pEnt
104af 72 79 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e ry==0 );. p->
104b0 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 3b pEntry = pEntry;
104b1 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 . }. p->pLast
104b2 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a = pEntry;.}../*.
104b3 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 ** Merge two lis
104b4 74 73 20 6f 66 20 52 6f 77 53 65 74 20 65 6e 74 ts of RowSet ent
104b5 72 69 65 73 2e 20 20 52 65 6d 6f 76 65 20 64 75 ries. Remove du
104b6 70 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 plicates..**.**
104b7 54 68 65 20 69 6e 70 75 74 20 6c 69 73 74 73 20 The input lists
104b8 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 are assumed to b
104b9 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 e in sorted orde
104ba 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 r..*/.static str
104bb 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
104bc 2a 62 6f 6f 6c 69 64 78 4d 65 72 67 65 28 0a 20 *boolidxMerge(.
104bd 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
104be 74 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 try *pA, /* F
104bf 69 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74 irst sorted list
104c0 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f to be merged */
104c1 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
104c2 45 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a Entry *pB /*
104c3 20 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c Second sorted l
104c4 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 ist to be merged
104c5 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 */.){. struct
104c6 52 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 RowSetEntry head
104c7 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 ;. struct RowSe
104c8 74 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a tEntry *pTail;..
104c9 20 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b pTail = &head;
104ca 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 . while( pA &&
104cb 70 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 pB ){. assert
104cc 28 20 70 41 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c ( pA->pNext==0 |
104cd 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 4e 65 | pA->v<=pA->pNe
104ce 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73 73 xt->v );. ass
104cf 65 72 74 28 20 70 42 2d 3e 70 4e 65 78 74 3d 3d ert( pB->pNext==
104d0 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 0 || pB->v<=pB->
104d1 70 4e 65 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20 pNext->v );.
104d2 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 if( pA->v<pB->v
104d3 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e ){. pTail->
104d4 70 4e 65 78 74 20 3d 20 70 41 3b 0a 20 20 20 20 pNext = pA;.
104d5 20 20 70 41 20 3d 20 70 41 2d 3e 70 4e 65 78 74 pA = pA->pNext
104d6 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 ;. pTail =
104d7 70 54 61 69 6c 2d 3e 70 4e 65 78 74 3b 0a 20 20 pTail->pNext;.
104d8 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d 3e }else if( pB->
104d9 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 20 20 20 v<pA->v ){.
104da 20 70 54 61 69 6c 2d 3e 70 4e 65 78 74 20 3d 20 pTail->pNext =
104db 70 42 3b 0a 20 20 20 20 20 20 70 42 20 3d 20 70 pB;. pB = p
104dc 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 B->pNext;.
104dd 70 54 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 pTail = pTail->p
104de 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Next;. }else{
104df 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e . pA = pA->
104e0 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d pNext;. }. }
104e1 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 . if( pA ){.
104e2 20 61 73 73 65 72 74 28 20 70 41 2d 3e 70 4e 65 assert( pA->pNe
104e3 78 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d xt==0 || pA->v<=
104e4 70 41 2d 3e 70 4e 65 78 74 2d 3e 76 20 29 3b 0a pA->pNext->v );.
104e5 20 20 20 20 70 54 61 69 6c 2d 3e 70 4e 65 78 74 pTail->pNext
104e6 20 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a = pA;. }else{.
104e7 20 20 20 20 61 73 73 65 72 74 28 20 70 42 3d 3d assert( pB==
104e8 30 20 7c 7c 20 70 42 2d 3e 70 4e 65 78 74 3d 3d 0 || pB->pNext==
104e9 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 0 || pB->v<=pB->
104ea 70 4e 65 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20 pNext->v );.
104eb 70 54 61 69 6c 2d 3e 70 4e 65 78 74 20 3d 20 70 pTail->pNext = p
104ec 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 B;. }. return
104ed 68 65 61 64 2e 70 4e 65 78 74 3b 0a 7d 0a 0a 2f head.pNext;.}../
104ee 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c *.** Sort all el
104ef 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 52 6f ements of the Ro
104f0 77 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 wSet into ascend
104f1 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 ing order..*/ .s
104f2 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
104f3 65 33 52 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 e3RowSetSort(Row
104f4 53 65 74 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 Set *p){. unsig
104f5 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 ned int i;. str
104f6 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
104f7 2a 70 45 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 *pEntry;. struc
104f8 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 t RowSetEntry *a
104f9 42 75 63 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 Bucket[40];.. a
104fa 73 73 65 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 ssert( p->isSort
104fb 65 64 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 ed==0 );. memse
104fc 74 28 61 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 t(aBucket, 0, si
104fd 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 29 3b 0a zeof(aBucket));.
104fe 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 while( p->pEnt
104ff 72 79 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 ry ){. pEntry
10500 20 3d 20 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 = p->pEntry;.
10501 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 p->pEntry = pE
10502 6e 74 72 79 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 ntry->pNext;.
10503 20 70 45 6e 74 72 79 2d 3e 70 4e 65 78 74 20 3d pEntry->pNext =
10504 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 0;. for(i=0;
10505 20 61 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b aBucket[i]; i++
10506 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20 ){. pEntry
10507 3d 20 62 6f 6f 6c 69 64 78 4d 65 72 67 65 28 61 = boolidxMerge(a
10508 42 75 63 6b 65 74 5b 69 5d 2c 70 45 6e 74 72 79 Bucket[i],pEntry
10509 29 3b 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74 );. aBucket
1050a 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 [i] = 0;. }.
1050b 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 aBucket[i] =
1050c 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 pEntry;. }. pE
1050d 6e 74 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 ntry = 0;. for(
1050e 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 i=0; i<sizeof(aB
1050f 75 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 ucket)/sizeof(aB
10510 75 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b ucket[0]); i++){
10511 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 62 6f . pEntry = bo
10512 6f 6c 69 64 78 4d 65 72 67 65 28 70 45 6e 74 72 olidxMerge(pEntr
10513 79 2c 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 y,aBucket[i]);.
10514 20 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d }. p->pEntry =
10515 20 70 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c pEntry;. p->pL
10516 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 ast = 0;. p->is
10517 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f Sorted = 1;.}../
10518 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 *.** Extract the
10519 20 6e 65 78 74 20 28 73 6d 61 6c 6c 65 73 74 29 next (smallest)
1051a 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 element from th
1051b 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 e RowSet..** Wri
1051c 74 65 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 te the element i
1051d 6e 74 6f 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 nto *pRowid. Re
1051e 74 75 72 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 turn 1 on succes
1051f 73 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 s. Return.** 0
10520 69 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 73 if the RowSet is
10521 20 61 6c 72 65 61 64 79 20 65 6d 70 74 79 2e 0a already empty..
10522 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
10523 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 E int sqlite3Row
10524 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 20 2a SetNext(RowSet *
10525 70 2c 20 69 36 34 20 2a 70 52 6f 77 69 64 29 7b p, i64 *pRowid){
10526 0a 20 20 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 . if( !p->isSor
10527 74 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ted ){. sqlit
10528 65 33 52 6f 77 53 65 74 53 6f 72 74 28 70 29 3b e3RowSetSort(p);
10529 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 45 . }. if( p->pE
1052a 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a 70 52 6f ntry ){. *pRo
1052b 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d wid = p->pEntry-
1052c 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 >v;. p->pEntr
1052d 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 70 y = p->pEntry->p
1052e 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 2d Next;. if( p-
1052f 3e 70 45 6e 74 72 79 3d 3d 30 20 29 7b 0a 20 20 >pEntry==0 ){.
10530 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 sqlite3RowSe
10531 74 43 6c 65 61 72 28 70 29 3b 0a 20 20 20 20 7d tClear(p);. }
10532 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
10533 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
10534 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a rn 0;. }.}../**
10535 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
10536 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a of rowset.c ***
10537 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
1053a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
1053b 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63 20 in file pager.c
1053c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1053d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1053e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
1053f 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
10540 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
10541 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
10542 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
10543 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
10544 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
10545 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
10546 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
10547 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
10548 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
10549 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
1054a 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
1054b 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
1054c 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
1054d 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
1054e 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
1054f 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
10550 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
10551 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
10552 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10553 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10556 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 ***.** This is t
10557 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
10558 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 n of the page ca
10559 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f 72 che subsystem or
1055a 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a "pager"..** .**
1055b 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75 73 The pager is us
1055c 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20 64 ed to access a d
1055d 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69 6c atabase disk fil
1055e 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 e. It implement
1055f 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d s.** atomic comm
10560 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 it and rollback
10561 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65 20 through the use
10562 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c of a journal fil
10563 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65 70 e that.** is sep
10564 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 64 arate from the d
10565 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
10566 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69 6d he pager also im
10567 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a plements file.**
10568 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 76 locking to prev
10569 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73 65 ent two processe
1056a 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 74 s from writing t
1056b 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
1056c 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 .** file simulta
1056d 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 neously, or one
1056e 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 61 process from rea
1056f 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 ding the databas
10570 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 e while.** anoth
10571 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a er is writing..*
10572 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 *.** @(#) $Id: p
10573 61 67 65 72 2e 63 2c 76 20 31 2e 35 37 36 20 32 ager.c,v 1.576 2
10574 30 30 39 2f 30 33 2f 33 31 20 30 32 3a 35 34 3a 009/03/31 02:54:
10575 34 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 40 drh Exp $.*/.
10576 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
10577 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a MIT_DISKIO../*.*
10578 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 72 6f * Macros for tro
10579 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 20 4e ubleshooting. N
1057a 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f ormally turned o
1057b 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 ff.*/.#if 0.int
1057c 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 sqlite3PagerTrac
1057d 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f e=1; /* True to
1057e 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 enable tracing
1057f 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 */.#define sqlit
10580 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 70 72 e3DebugPrintf pr
10581 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41 47 intf.#define PAG
10582 45 52 54 52 41 43 45 28 58 29 20 20 20 20 20 69 ERTRACE(X) i
10583 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 f( sqlite3PagerT
10584 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 44 race ){ sqlite3D
10585 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20 7d 0a ebugPrintf X; }.
10586 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50 41 #else.#define PA
10587 47 45 52 54 52 41 43 45 28 58 29 0a 23 65 6e 64 GERTRACE(X).#end
10588 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f if../*.** The fo
10589 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 72 llowing two macr
1058a 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 74 68 os are used with
1058b 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 41 43 in the PAGERTRAC
1058c 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76 65 E() macros above
1058d 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75 74 .** to print out
1058e 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 file-descriptor
1058f 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 s. .**.** PAGERI
10590 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f 69 6e D() takes a poin
10591 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 73 ter to a Pager s
10592 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67 truct as its arg
10593 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 61 73 ument. The.** as
10594 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d 64 65 sociated file-de
10595 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 74 75 scriptor is retu
10596 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 4c 45 rned. FILEHANDLE
10597 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 73 71 ID() takes an sq
10598 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 lite3_file.** st
10599 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67 75 ruct as its argu
1059a 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ment..*/.#define
1059b 20 50 41 47 45 52 49 44 28 70 29 20 28 28 69 6e PAGERID(p) ((in
1059c 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 66 69 t)(p->fd)).#defi
1059d 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 ne FILEHANDLEID(
1059e 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a 0a 2f fd) ((int)fd)../
1059f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63 61 *.** The page ca
105a0 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 20 69 che as a whole i
105a1 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e 65 20 s always in one
105a2 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
105a3 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a .** states:.**.*
105a4 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b * PAGER_UNLOCK
105a5 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67 65 The page
105a6 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 63 75 cache is not cu
105a7 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e 67 20 rrently reading
105a8 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or .**
105a9 20 20 20 20 20 20 20 20 20 20 20 20 20 77 72 69 wri
105aa 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 ting the databas
105ab 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 20 69 e file. There i
105ac 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 s no.**
105ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 da
105ae 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 ta held in memor
105af 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 y. This is the
105b0 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 20 20 initial.**
105b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
105b2 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 state..**.**
105b3 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 20 20 PAGER_SHARED
105b4 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 The page cac
105b5 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 74 68 he is reading th
105b6 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 e database..**
105b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
105b8 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 73 20 Writing is
105b9 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e 20 20 not permitted.
105ba 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 There can be.**
105bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
105bc 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 20 72 multiple r
105bd 65 61 64 65 72 73 20 61 63 63 65 73 73 69 6e 67 eaders accessing
105be 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
105bf 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 se.**
105c0 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 file
105c1 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
105c2 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 e..**.** PAGER
105c3 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 54 _RESERVED T
105c4 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 73 20 his process has
105c5 72 65 73 65 72 76 65 64 20 74 68 65 20 64 61 74 reserved the dat
105c6 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 69 6e abase for writin
105c7 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 g.**
105c8 20 20 20 20 20 20 20 20 20 20 20 62 75 74 20 68 but h
105c9 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 as not yet made
105ca 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 4f 6e any changes. On
105cb 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 0a 2a ly one process.*
105cc 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
105cd 20 20 20 20 20 20 20 20 61 74 20 61 20 74 69 6d at a tim
105ce 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 74 68 e can reserve th
105cf 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 e database. The
105d0 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 original.**
105d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
105d2 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 database file
105d3 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 6d 6f has not been mo
105d4 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 72 0a dified so other.
105d5 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
105d6 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 73 73 process
105d7 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 es may still be
105d8 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e 2d 64 reading the on-d
105d9 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 isk.**
105da 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 dat
105db 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
105dc 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c 55 53 * PAGER_EXCLUS
105dd 49 56 45 20 20 20 20 20 54 68 65 20 70 61 67 65 IVE The page
105de 20 63 61 63 68 65 20 69 73 20 77 72 69 74 69 6e cache is writin
105df 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a g the database..
105e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
105e1 20 20 20 20 20 20 20 20 20 41 63 63 65 73 73 20 Access
105e2 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 4e is exclusive. N
105e3 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 o other processe
105e4 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 s or.**
105e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
105e6 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 65 61 reads can be rea
105e7 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 ding or writing
105e8 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20 20 while one.**
105e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
105ea 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 77 72 process is wr
105eb 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 iting..**.** P
105ec 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20 20 AGER_SYNCED
105ed 20 20 20 54 68 65 20 70 61 67 65 72 20 6d 6f 76 The pager mov
105ee 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 74 65 es to this state
105ef 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 43 4c from PAGER_EXCL
105f0 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 20 20 USIVE.**
105f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
105f2 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 20 70 fter all dirty p
105f3 61 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 77 ages have been w
105f4 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a ritten to the.**
105f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
105f6 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 database
105f7 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 6c file and the fil
105f8 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e 63 65 e has been synce
105f9 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 d to.**
105fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 di
105fb 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 65 6d sk. All that rem
105fc 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 74 6f ains to do is to
105fd 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20 20 remove or.**
105fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
105ff 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 68 65 truncate the
10600 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e journal file an
10601 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f d the transactio
10602 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 n .**
10603 20 20 20 20 20 20 20 20 20 20 20 20 77 69 6c 6c will
10604 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a be committed..*
10605 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63 61 *.** The page ca
10606 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 6e 20 che comes up in
10607 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 PAGER_UNLOCK. T
10608 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 0a he first time a.
10609 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 ** sqlite3PagerG
1060a 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 68 65 et() occurs, the
1060b 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f state transitio
1060c 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 ns to PAGER_SHAR
1060d 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c ED..** After all
1060e 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e pages have been
1060f 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20 released using
10610 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e 72 65 sqlite_page_unre
10611 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 61 74 f(),.** the stat
10612 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 61 e transitions ba
10613 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e 4c 4f ck to PAGER_UNLO
10614 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 CK. The first t
10615 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 ime.** that sqli
10616 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
10617 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 73 is called, the s
10618 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 tate transitions
10619 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53 to.** PAGER_RES
1061a 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 74 68 ERVED. (Note th
1061b 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 at sqlite3PagerW
1061c 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c 79 20 rite() can only
1061d 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 be.** called on
1061e 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 an outstanding p
1061f 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e 73 20 age which means
10620 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 6d that the pager m
10621 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 41 47 ust.** be in PAG
10622 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f 72 65 ER_SHARED before
10623 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 it transitions
10624 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 to PAGER_RESERVE
10625 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53 D.).** PAGER_RES
10626 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 61 74 ERVED means that
10627 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f 70 65 there is an ope
10628 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e n rollback journ
10629 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 al..** The trans
1062a 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 5f 45 ition to PAGER_E
1062b 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 73 20 XCLUSIVE occurs
1062c 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 before any chang
1062d 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 es.** are made t
1062e 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
1062f 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 69 74 ile, though writ
10630 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 es to the rollba
10631 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63 ck.** journal oc
10632 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 20 50 curs with just P
10633 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 20 AGER_RESERVED.
10634 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 65 33 After an sqlite3
10635 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 0a PagerRollback().
10636 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 61 67 ** or sqlite3Pag
10637 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
10638 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 63 61 (), the state ca
10639 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 41 47 n go back to PAG
1063a 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 6f 72 ER_SHARED,.** or
1063b 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 74 20 it can stay at
1063c 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
1063d 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 78 63 if we are in exc
1063e 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 6d 6f lusive access mo
1063f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 de..*/.#define P
10640 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 AGER_UNLOCK
10641 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 0.#define PAGER
10642 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 20 20 _SHARED 1
10643 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 41 52 /* same as SHAR
10644 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 ED_LOCK */.#defi
10645 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 ne PAGER_RESERVE
10646 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 6d 65 D 2 /* same
10647 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 as RESERVED_LOC
10648 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 K */.#define PAG
10649 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 34 ER_EXCLUSIVE 4
1064a 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 45 58 /* same as EX
1064b 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a CLUSIVE_LOCK */.
1064c 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 59 #define PAGER_SY
1064d 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f 2a 0a NCED 5../*.
1064e 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 20 ** A macro used
1064f 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 for invoking the
10650 20 63 6f 64 65 63 20 69 66 20 74 68 65 72 65 20 codec if there
10651 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 is one.*/.#ifdef
10652 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 SQLITE_HAS_CODE
10653 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 C.# define CODEC
10654 31 28 50 2c 44 2c 4e 2c 58 29 20 69 66 28 20 50 1(P,D,N,X) if( P
10655 2d 3e 78 43 6f 64 65 63 21 3d 30 20 29 7b 20 50 ->xCodec!=0 ){ P
10656 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 ->xCodec(P->pCod
10657 65 63 41 72 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a ecArg,D,N,X); }.
10658 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 # define CODEC2(
10659 50 2c 44 2c 4e 2c 58 29 20 28 28 63 68 61 72 2a P,D,N,X) ((char*
1065a 29 28 50 2d 3e 78 43 6f 64 65 63 21 3d 30 3f 50 )(P->xCodec!=0?P
1065b 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 ->xCodec(P->pCod
1065c 65 63 41 72 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 ecArg,D,N,X):D))
1065d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
1065e 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20 CODEC1(P,D,N,X)
1065f 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 /* NO-OP */.# de
10660 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c fine CODEC2(P,D,
10661 4e 2c 58 29 20 28 28 63 68 61 72 2a 29 44 29 0a N,X) ((char*)D).
10662 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
10663 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 e maximum allowe
10664 64 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 31 d sector size. 1
10665 36 4d 42 2e 20 49 66 20 74 68 65 20 78 53 65 63 6MB. If the xSec
10666 74 6f 72 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 torsize() method
10667 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 .** returns a v
10668 61 6c 75 65 20 6c 61 72 67 65 72 20 74 68 61 6e alue larger than
10669 20 74 68 69 73 2c 20 74 68 65 6e 20 4d 41 58 5f this, then MAX_
1066a 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 73 20 75 SECTOR_SIZE is u
1066b 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 sed instead..**
1066c 54 68 69 73 20 63 6f 75 6c 64 20 63 6f 6e 63 65 This could conce
1066d 69 76 61 62 6c 79 20 63 61 75 73 65 20 63 6f 72 ivably cause cor
1066e 72 75 70 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e ruption followin
1066f 67 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 g a power failur
10670 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 20 61 20 73 e on.** such a s
10671 79 73 74 65 6d 2e 20 54 68 69 73 20 69 73 20 63 ystem. This is c
10672 75 72 72 65 6e 74 6c 79 20 61 6e 20 75 6e 64 6f urrently an undo
10673 63 75 6d 65 6e 74 65 64 20 6c 69 6d 69 74 2e 0a cumented limit..
10674 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 53 */.#define MAX_S
10675 45 43 54 4f 52 5f 53 49 5a 45 20 30 78 30 31 30 ECTOR_SIZE 0x010
10676 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 0000../*.** An i
10677 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
10678 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
10679 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 re is allocated
1067a 66 6f 72 20 65 61 63 68 20 61 63 74 69 76 65 0a for each active.
1067b 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 ** savepoint and
1067c 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1067d 61 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 79 action in the sy
1067e 73 74 65 6d 2e 20 41 6c 6c 20 73 75 63 68 20 73 stem. All such s
1067f 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 61 72 65 tructures.** are
10680 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 50 stored in the P
10681 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b ager.aSavepoint[
10682 5d 20 61 72 72 61 79 2c 20 77 68 69 63 68 20 69 ] array, which i
10683 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a s allocated and.
10684 2a 2a 20 72 65 73 69 7a 65 64 20 75 73 69 6e 67 ** resized using
10685 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 sqlite3Realloc(
10686 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 )..**.** When a
10687 73 61 76 65 70 6f 69 6e 74 20 69 73 20 63 72 65 savepoint is cre
10688 61 74 65 64 2c 20 74 68 65 20 50 61 67 65 72 53 ated, the PagerS
10689 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
1068a 73 65 74 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 set field is.**
1068b 73 65 74 20 74 6f 20 30 2e 20 49 66 20 61 20 6a set to 0. If a j
1068c 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 ournal-header is
1068d 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
1068e 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 e main journal w
1068f 68 69 6c 65 0a 2a 2a 20 74 68 65 20 73 61 76 65 hile.** the save
10690 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65 2c point is active,
10691 20 74 68 65 6e 20 69 48 64 72 4f 66 66 73 65 74 then iHdrOffset
10692 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 is set to the b
10693 79 74 65 20 6f 66 66 73 65 74 20 0a 2a 2a 20 69 yte offset .** i
10694 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f mmediately follo
10695 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 6a 6f wing the last jo
10696 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 72 69 urnal record wri
10697 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 tten into the ma
10698 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 in.** journal be
10699 66 6f 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c fore the journal
1069a 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20 69 73 -header. This is
1069b 20 72 65 71 75 69 72 65 64 20 64 75 72 69 6e 67 required during
1069c 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f savepoint.** ro
1069d 6c 6c 62 61 63 6b 20 28 73 65 65 20 70 61 67 65 llback (see page
1069e 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 rPlaybackSavepoi
1069f 6e 74 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 nt())..*/.typede
106a0 66 20 73 74 72 75 63 74 20 50 61 67 65 72 53 61 f struct PagerSa
106a1 76 65 70 6f 69 6e 74 20 50 61 67 65 72 53 61 76 vepoint PagerSav
106a2 65 70 6f 69 6e 74 3b 0a 73 74 72 75 63 74 20 50 epoint;.struct P
106a3 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 7b 0a agerSavepoint {.
106a4 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 i64 iOffset;
106a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
106a6 2a 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 * Starting offse
106a7 74 20 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 t in main journa
106a8 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f l */. i64 iHdrO
106a9 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
106aa 20 20 20 20 2f 2a 20 53 65 65 20 61 62 6f 76 65 /* See above
106ab 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 */. Bitvec *pI
106ac 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 nSavepoint;
106ad 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 /* Set of pag
106ae 65 73 20 69 6e 20 74 68 69 73 20 73 61 76 65 70 es in this savep
106af 6f 69 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e oint */. Pgno n
106b0 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 Orig;
106b1 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e /* Origin
106b2 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
106b3 65 73 20 69 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 es in file */.
106b4 50 67 6e 6f 20 69 53 75 62 52 65 63 3b 20 20 20 Pgno iSubRec;
106b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
106b6 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 20 72 Index of first r
106b7 65 63 6f 72 64 20 69 6e 20 73 75 62 2d 6a 6f 75 ecord in sub-jou
106b8 72 6e 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a rnal */.};../*.*
106b9 2a 20 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61 * A open page ca
106ba 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e che is an instan
106bb 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
106bc 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
106bd 2a 0a 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a *.** errCode.**.
106be 2a 2a 20 20 20 50 61 67 65 72 2e 65 72 72 43 6f ** Pager.errCo
106bf 64 65 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f de may be set to
106c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 SQLITE_IOERR, S
106c1 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f QLITE_CORRUPT, o
106c2 72 0a 2a 2a 20 20 20 6f 72 20 53 51 4c 49 54 45 r.** or SQLITE
106c3 5f 46 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 _FULL. Once one
106c4 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 of the first thr
106c5 65 65 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73 ee errors occurs
106c6 2c 20 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a , it persists.**
106c7 20 20 20 61 6e 64 20 69 73 20 72 65 74 75 72 6e and is return
106c8 65 64 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 ed as the result
106c9 20 6f 66 20 65 76 65 72 79 20 6d 61 6a 6f 72 20 of every major
106ca 70 61 67 65 72 20 41 50 49 20 63 61 6c 6c 2e 20 pager API call.
106cb 20 54 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 45 The.** SQLITE
106cc 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64 _FULL return cod
106cd 65 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 e is slightly di
106ce 66 66 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73 fferent. It pers
106cf 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 ists only until
106d0 74 68 65 0a 2a 2a 20 20 20 6e 65 78 74 20 73 75 the.** next su
106d1 63 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 ccessful rollbac
106d2 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 6f k is performed o
106d3 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 n the pager cach
106d4 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51 e. Also,.** SQ
106d5 4c 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e LITE_FULL does n
106d6 6f 74 20 61 66 66 65 63 74 20 74 68 65 20 73 71 ot affect the sq
106d7 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 lite3PagerGet()
106d8 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 and sqlite3Pager
106d9 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 41 50 Lookup().** AP
106da 49 73 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69 Is, they may sti
106db 6c 6c 20 62 65 20 75 73 65 64 20 73 75 63 63 65 ll be used succe
106dc 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 ssfully..**.** d
106dd 62 53 69 7a 65 56 61 6c 69 64 2c 20 64 62 53 69 bSizeValid, dbSi
106de 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a 65 2c 20 ze, dbOrigSize,
106df 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a dbFileSize.**.**
106e0 20 20 20 4d 61 6e 61 67 69 6e 67 20 74 68 65 20 Managing the
106e1 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
106e2 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 base file in pag
106e3 65 73 20 69 73 20 61 20 6c 69 74 74 6c 65 20 63 es is a little c
106e4 6f 6d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 20 20 omplicated..**
106e5 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 The variable Pa
106e6 67 65 72 2e 64 62 53 69 7a 65 20 63 6f 6e 74 61 ger.dbSize conta
106e7 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ins the number o
106e8 66 20 70 61 67 65 73 20 74 68 61 74 20 74 68 65 f pages that the
106e9 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 69 database.** i
106ea 6d 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 63 mage currently c
106eb 6f 6e 74 61 69 6e 73 2e 20 41 73 20 74 68 65 20 ontains. As the
106ec 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 67 database image g
106ed 72 6f 77 73 20 6f 72 20 73 68 72 69 6e 6b 73 20 rows or shrinks
106ee 74 68 69 73 0a 2a 2a 20 20 20 76 61 72 69 61 62 this.** variab
106ef 6c 65 20 69 73 20 75 70 64 61 74 65 64 2e 20 54 le is updated. T
106f0 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 he variable Page
106f1 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e r.dbFileSize con
106f2 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 tains the number
106f3 0a 2a 2a 20 20 20 6f 66 20 70 61 67 65 73 20 69 .** of pages i
106f4 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
106f5 69 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 62 65 ile. This may be
106f6 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
106f7 50 61 67 65 72 2e 64 62 53 69 7a 65 0a 2a 2a 20 Pager.dbSize.**
106f8 20 20 69 66 20 73 6f 6d 65 20 70 61 67 65 73 20 if some pages
106f9 68 61 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64 have been append
106fa 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ed to the databa
106fb 73 65 20 69 6d 61 67 65 20 62 75 74 20 6e 6f 74 se image but not
106fc 20 79 65 74 20 77 72 69 74 74 65 6e 0a 2a 2a 20 yet written.**
106fd 20 20 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63 out from the c
106fe 61 63 68 65 20 74 6f 20 74 68 65 20 61 63 74 75 ache to the actu
106ff 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e al file on disk.
10700 20 4f 72 20 69 66 20 74 68 65 20 69 6d 61 67 65 Or if the image
10701 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 20 20 74 has been.** t
10702 72 75 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69 runcated by an i
10703 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 ncremental-vacuu
10704 6d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 m operation. The
10705 20 50 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a Pager.dbOrigSiz
10706 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 20 20 e variable.**
10707 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d contains the num
10708 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
10709 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 the database ima
1070a 67 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 ge when the curr
1070b 65 6e 74 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 ent.** transac
1070c 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e tion was opened.
1070d 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 The contents of
1070e 20 61 6c 6c 20 74 68 72 65 65 20 6f 66 20 74 68 all three of th
1070f 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 69 73 ese variables is
10710 0a 2a 2a 20 20 20 6f 6e 6c 79 20 67 75 61 72 61 .** only guara
10711 6e 74 65 65 64 20 74 6f 20 62 65 20 63 6f 72 72 nteed to be corr
10712 65 63 74 20 69 66 20 74 68 65 20 62 6f 6f 6c 65 ect if the boole
10713 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 56 an Pager.dbSizeV
10714 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 2a 2a alid is true..**
10715 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 55 6e 64 65 .** TODO: Unde
10716 72 20 77 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e r what condition
10717 73 20 69 73 20 64 62 53 69 7a 65 56 61 6c 69 64 s is dbSizeValid
10718 20 73 65 74 3f 20 43 6c 65 61 72 65 64 3f 0a 2a set? Cleared?.*
10719 2a 0a 2a 2a 20 63 68 61 6e 67 65 43 6f 75 6e 74 *.** changeCount
1071a 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 Done.**.** Thi
1071b 73 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 s boolean variab
1071c 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 le is used to ma
1071d 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ke sure that the
1071e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
1071f 0a 2a 2a 20 20 20 28 74 68 65 20 34 2d 62 79 74 .** (the 4-byt
10720 65 20 68 65 61 64 65 72 20 66 69 65 6c 64 20 61 e header field a
10721 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 t byte offset 24
10722 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
10723 20 66 69 6c 65 29 20 69 73 20 0a 2a 2a 20 20 20 file) is .**
10724 6e 6f 74 20 75 70 64 61 74 65 64 20 6d 6f 72 65 not updated more
10725 20 6f 66 74 65 6e 20 74 68 61 6e 20 6e 65 63 65 often than nece
10726 73 73 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 ssary. .**.**
10727 49 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 It is set to tru
10728 65 20 77 68 65 6e 20 74 68 65 20 63 68 61 6e 67 e when the chang
10729 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 e-counter field
1072a 69 73 20 75 70 64 61 74 65 64 2c 20 77 68 69 63 is updated, whic
1072b 68 20 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c 79 h .** can only
1072c 20 68 61 70 70 65 6e 20 69 66 20 61 6e 20 65 78 happen if an ex
1072d 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 clusive lock is
1072e 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 held on the data
1072f 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 base file..**
10730 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 28 73 It is cleared (s
10731 65 74 20 74 6f 20 66 61 6c 73 65 29 20 77 68 65 et to false) whe
10732 6e 65 76 65 72 20 61 6e 20 65 78 63 6c 75 73 69 never an exclusi
10733 76 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 20 ve lock is .**
10734 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 6f 6e relinquished on
10735 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
10736 6c 65 2e 20 45 61 63 68 20 74 69 6d 65 20 61 20 le. Each time a
10737 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 transaction is c
10738 6f 6d 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 20 54 ommitted,.** T
10739 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f he changeCountDo
1073a 6e 65 20 66 6c 61 67 20 69 73 20 69 6e 73 70 65 ne flag is inspe
1073b 63 74 65 64 2e 20 49 66 20 69 74 20 69 73 20 74 cted. If it is t
1073c 72 75 65 2c 20 74 68 65 20 77 6f 72 6b 20 6f 66 rue, the work of
1073d 0a 2a 2a 20 20 20 75 70 64 61 74 69 6e 67 20 74 .** updating t
1073e 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
1073f 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66 6f 72 r is omitted for
10740 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
10741 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
10742 20 20 54 68 69 73 20 6d 65 63 68 61 6e 69 73 6d This mechanism
10743 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e means that when
10744 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c running in excl
10745 75 73 69 76 65 20 6d 6f 64 65 2c 20 61 20 63 6f usive mode, a co
10746 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e nnection .** n
10747 65 65 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 20 eed only update
10748 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
10749 65 72 20 6f 6e 63 65 2c 20 66 6f 72 20 74 68 65 er once, for the
1074a 20 66 69 72 73 74 20 74 72 61 6e 73 61 63 74 69 first transacti
1074b 6f 6e 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 74 65 on.** committe
1074c 64 2e 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66 d..**.** dbModif
1074d 69 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 ied.**.** The
1074e 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 dbModified flag
1074f 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 is set whenever
10750 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 a database page
10751 69 73 20 64 69 72 74 69 65 64 2e 0a 2a 2a 20 20 is dirtied..**
10752 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 61 It is cleared a
10753 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 t the end of eac
10754 68 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a h transaction..*
10755 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 75 73 65 *.** It is use
10756 64 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e d when committin
10757 67 20 6f 72 20 6f 74 68 65 72 77 69 73 65 20 65 g or otherwise e
10758 6e 64 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 nding a transact
10759 69 6f 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 68 65 ion. If.** the
1075a 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 dbModified flag
1075b 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20 6c is clear then l
1075c 65 73 73 20 77 6f 72 6b 20 68 61 73 20 74 6f 20 ess work has to
1075d 62 65 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a be done..**.** j
1075e 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 0a 2a 2a ournalStarted.**
1075f 0a 2a 2a 20 20 20 54 68 69 73 20 66 6c 61 67 20 .** This flag
10760 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 is set whenever
10761 74 68 65 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 the the main jou
10762 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 2e 20 rnal is synced.
10763 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 70 6f 69 .**.** The poi
10764 6e 74 20 6f 66 20 74 68 69 73 20 66 6c 61 67 20 nt of this flag
10765 69 73 20 74 68 61 74 20 69 74 20 6d 75 73 74 20 is that it must
10766 62 65 20 73 65 74 20 61 66 74 65 72 20 74 68 65 be set after the
10767 20 0a 2a 2a 20 20 20 66 69 72 73 74 20 6a 6f 75 .** first jou
10768 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 61 rnal header in a
10769 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
1076a 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f s been synced to
1076b 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 41 66 74 65 disk..** Afte
1076c 72 20 74 68 69 73 20 68 61 73 20 68 61 70 70 65 r this has happe
1076d 6e 65 64 2c 20 6e 65 77 20 70 61 67 65 73 20 61 ned, new pages a
1076e 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 64 ppended to the d
1076f 61 74 61 62 61 73 65 20 0a 2a 2a 20 20 20 64 6f atabase .** do
10770 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20 50 47 not need the PG
10771 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c HDR_NEED_SYNC fl
10772 61 67 20 73 65 74 2c 20 61 73 20 74 68 65 79 20 ag set, as they
10773 64 6f 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20 do not need.**
10774 20 74 6f 20 77 61 69 74 20 66 6f 72 20 61 20 6a to wait for a j
10775 6f 75 72 6e 61 6c 20 73 79 6e 63 20 62 65 66 6f ournal sync befo
10776 72 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 77 re they can be w
10777 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 2a 2a ritten out to.**
10778 20 20 20 74 68 65 20 64 61 74 61 62 61 73 65 20 the database
10779 66 69 6c 65 20 28 73 65 65 20 66 75 6e 63 74 69 file (see functi
1077a 6f 6e 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 on pager_write()
1077b 29 2e 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65 74 4d )..** .** setM
1077c 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 54 68 aster.**.** Th
1077d 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 is variable is u
1077e 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 sed to ensure th
1077f 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f at the master jo
10780 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 0a urnal file name.
10781 2a 2a 20 20 20 28 69 66 20 61 6e 79 29 20 69 73 ** (if any) is
10782 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 69 6e only written in
10783 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
10784 69 6c 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 ile once..**.**
10785 20 20 57 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e When committin
10786 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c g a transaction,
10787 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
10788 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 28 69 nal file name (i
10789 66 20 61 6e 79 29 0a 2a 2a 20 20 20 6d 61 79 20 f any).** may
1078a 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 be written into
1078b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1078c 20 77 68 69 6c 65 20 74 68 65 20 70 61 67 65 72 while the pager
1078d 20 69 73 20 73 74 69 6c 6c 20 69 6e 0a 2a 2a 20 is still in.**
1078e 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 PAGER_RESERVED
1078f 20 73 74 61 74 65 20 28 73 65 65 20 43 6f 6d 6d state (see Comm
10790 69 74 50 68 61 73 65 4f 6e 65 28 29 20 66 6f 72 itPhaseOne() for
10791 20 74 68 65 20 61 63 74 69 6f 6e 29 2e 20 49 74 the action). It
10792 0a 2a 2a 20 20 20 74 68 65 6e 20 61 74 74 65 6d .** then attem
10793 70 74 73 20 74 6f 20 75 70 67 72 61 64 65 20 74 pts to upgrade t
10794 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c o an exclusive l
10795 6f 63 6b 2e 20 49 66 20 74 68 69 73 20 61 74 74 ock. If this att
10796 65 6d 70 74 0a 2a 2a 20 20 20 66 61 69 6c 73 2c empt.** fails,
10797 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 42 55 53 then SQLITE_BUS
10798 59 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 Y may be returne
10799 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 61 6e d to the user an
1079a 64 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 20 20 d the user.**
1079b 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63 may attempt to c
1079c 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 ommit the transa
1079d 63 74 69 6f 6e 20 61 67 61 69 6e 20 6c 61 74 65 ction again late
1079e 72 20 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20 20 20 r (calling.**
1079f 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 CommitPhaseOne()
107a0 20 61 67 61 69 6e 29 2e 20 54 68 69 73 20 66 6c again). This fl
107a1 61 67 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e ag is used to en
107a2 73 75 72 65 20 74 68 61 74 20 74 68 65 20 0a 2a sure that the .*
107a3 2a 20 20 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e * master journ
107a4 61 6c 20 6e 61 6d 65 20 69 73 20 6f 6e 6c 79 20 al name is only
107a5 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
107a6 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 ournal file the
107a7 66 69 72 73 74 0a 2a 2a 20 20 20 74 69 6d 65 20 first.** time
107a8 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 CommitPhaseOne()
107a9 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a is called..**.*
107aa 2a 20 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a * doNotSync.**.*
107ab 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c * This variabl
107ac 65 20 69 73 20 73 65 74 20 61 6e 64 20 63 6c 65 e is set and cle
107ad 61 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 ared by sqlite3P
107ae 61 67 65 72 57 72 69 74 65 28 29 2e 0a 2a 2a 0a agerWrite()..**.
107af 2a 2a 20 6e 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a ** needSync.**.*
107b0 2a 20 20 20 54 4f 44 4f 3a 20 49 74 20 6d 69 67 * TODO: It mig
107b1 68 74 20 62 65 20 65 61 73 69 65 72 20 74 6f 20 ht be easier to
107b2 73 65 74 20 74 68 69 73 20 76 61 72 69 61 62 6c set this variabl
107b3 65 20 69 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 e in writeJourna
107b4 6c 48 64 72 28 29 0a 2a 2a 20 20 20 61 6e 64 20 lHdr().** and
107b5 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e writeMasterJourn
107b6 61 6c 28 29 20 6f 6e 6c 79 2e 20 43 68 61 6e 67 al() only. Chang
107b7 65 20 69 74 73 20 6d 65 61 6e 69 6e 67 20 74 6f e its meaning to
107b8 20 22 75 6e 73 79 6e 63 65 64 20 64 61 74 61 0a "unsynced data.
107b9 2a 2a 20 20 20 68 61 73 20 62 65 65 6e 20 77 72 ** has been wr
107ba 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
107bb 72 6e 61 6c 22 2e 0a 2a 2f 0a 73 74 72 75 63 74 rnal"..*/.struct
107bc 20 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 Pager {. sqlit
107bd 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 e3_vfs *pVfs;
107be 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e /* OS fun
107bf 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f ctions to use fo
107c0 72 20 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78 63 r IO */. u8 exc
107c1 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 lusiveMode;
107c2 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e /* Boolean
107c3 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e . True if lockin
107c4 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 g_mode==EXCLUSIV
107c5 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 E */. u8 journa
107c6 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 lMode;
107c7 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20 /* On of the
107c8 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
107c9 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 E_* values */.
107ca 75 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 u8 useJournal;
107cb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
107cc 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f se a rollback jo
107cd 75 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 urnal on this fi
107ce 6c 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 le */. u8 noRea
107cf 64 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 dlock;
107d0 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f /* Do not bo
107d1 74 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 ther to obtain r
107d2 65 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 eadlocks */. u8
107d3 20 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 20 20 noSync;
107d4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 /* Do
107d5 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 not sync the jou
107d6 72 6e 61 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a rnal if true */.
107d7 20 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 u8 fullSync;
107d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
107d9 20 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73 20 Do extra syncs
107da 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
107db 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f or robustness */
107dc 0a 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 67 73 . u8 sync_flags
107dd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
107de 2a 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f * One of SYNC_NO
107df 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c RMAL or SYNC_FUL
107e0 4c 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 46 69 L */. u8 tempFi
107e1 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
107e2 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 /* zFilename
107e3 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 is a temporary f
107e4 69 6c 65 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 ile */. u8 read
107e5 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 Only;
107e6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
107e7 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 a read-only dat
107e8 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 6d 65 abase */. u8 me
107e9 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 mDb;
107ea 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
107eb 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66 69 o inhibit all fi
107ec 6c 65 20 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a 20 le I/O */.. /*
107ed 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c The following bl
107ee 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 6f ock contains tho
107ef 73 65 20 63 6c 61 73 73 20 6d 65 6d 62 65 72 73 se class members
107f0 20 74 68 61 74 20 61 72 65 20 64 79 6e 61 6d 69 that are dynami
107f1 63 61 6c 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 66 cally. ** modif
107f2 69 65 64 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 ied during norma
107f3 6c 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 54 68 l operations. Th
107f4 65 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65 e other variable
107f5 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 s in this struct
107f6 75 72 65 0a 20 20 2a 2a 20 61 72 65 20 65 69 74 ure. ** are eit
107f7 68 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 her constant thr
107f8 6f 75 67 68 6f 75 74 20 74 68 65 20 6c 69 66 65 oughout the life
107f9 74 69 6d 65 20 6f 66 20 74 68 65 20 70 61 67 65 time of the page
107fa 72 2c 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 r, or else. **
107fb 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 63 6f used to store co
107fc 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72 61 nfiguration para
107fd 6d 65 74 65 72 73 20 74 68 61 74 20 61 66 66 65 meters that affe
107fe 63 74 20 74 68 65 20 77 61 79 20 74 68 65 20 70 ct the way the p
107ff 61 67 65 72 20 0a 20 20 2a 2a 20 6f 70 65 72 61 ager . ** opera
10800 74 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 tes.. **. ** T
10801 68 65 20 27 73 74 61 74 65 27 20 76 61 72 69 61 he 'state' varia
10802 62 6c 65 20 69 73 20 64 65 73 63 72 69 62 65 64 ble is described
10803 20 69 6e 20 6d 6f 72 65 20 64 65 74 61 69 6c 20 in more detail
10804 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 0a 20 along with the.
10805 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 73 ** descriptions
10806 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 of the values i
10807 74 20 6d 61 79 20 74 61 6b 65 20 2d 20 50 41 47 t may take - PAG
10808 45 52 5f 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 4d ER_UNLOCK etc. M
10809 61 6e 79 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 any of the. **
1080a 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 20 other variables
1080b 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b 20 61 72 in this block ar
1080c 65 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 e described in t
1080d 68 65 20 63 6f 6d 6d 65 6e 74 20 64 69 72 65 63 he comment direc
1080e 74 6c 79 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20 tly . ** above
1080f 74 68 69 73 20 63 6c 61 73 73 20 64 65 66 69 6e this class defin
10810 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 38 ition.. */. u8
10811 20 73 74 61 74 65 3b 20 20 20 20 20 20 20 20 20 state;
10812 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47 /* PAG
10813 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52 ER_UNLOCK, _SHAR
10814 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c 20 65 ED, _RESERVED, e
10815 74 63 2e 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f tc. */. u8 dbMo
10816 64 69 66 69 65 64 3b 20 20 20 20 20 20 20 20 20 dified;
10817 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
10818 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63 68 there are any ch
10819 61 6e 67 65 73 20 74 6f 20 74 68 65 20 44 62 20 anges to the Db
1081a 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e 63 */. u8 needSync
1081b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1081c 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 66 /* True if an f
1081d 73 79 6e 63 28 29 20 69 73 20 6e 65 65 64 65 64 sync() is needed
1081e 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 on the journal
1081f 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53 */. u8 journalS
10820 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 tarted;
10821 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 61 64 /* True if head
10822 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73 er of journal is
10823 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 synced */. u8
10824 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b changeCountDone;
10825 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 /* Set
10826 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 after incrementi
10827 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f ng the change-co
10828 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 73 65 unter */. u8 se
10829 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 tMaster;
1082a 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
1082b 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 f a m-j name has
1082c 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
1082d 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f jrnl */. u8 do
1082e 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20 20 20 NotSync;
1082f 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 /* Boolea
10830 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c 20 64 n. While true, d
10831 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65 20 o not spill the
10832 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 64 62 cache */. u8 db
10833 53 69 7a 65 56 61 6c 69 64 3b 20 20 20 20 20 20 SizeValid;
10834 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 77 68 /* Set wh
10835 65 6e 20 64 62 53 69 7a 65 20 69 73 20 63 6f 72 en dbSize is cor
10836 72 65 63 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 rect */. Pgno d
10837 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 bSize;
10838 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
10839 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
1083a 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 database */. Pg
1083b 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 no dbOrigSize;
1083c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 /* dbS
1083d 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 ize before the c
1083e 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
1083f 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 on */. Pgno dbF
10840 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 ileSize;
10841 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
10842 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 pages in the da
10843 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
10844 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 int errCode;
10845 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10846 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c 20 6b One of several k
10847 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 2a inds of errors *
10848 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 /. int nRec;
10849 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1084a 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c /* Pages journal
1084b 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74 20 6a led since last j
1084c 2d 68 65 61 64 65 72 20 77 72 69 74 74 65 6e 20 -header written
1084d 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 6e */. u32 cksumIn
1084e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 it;
1084f 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d /* Quasi-random
10850 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f 20 value added to
10851 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20 2a every checksum *
10852 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65 63 3b /. u32 nSubRec;
10853 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10854 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 /* Number of rec
10855 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74 6f 20 ords written to
10856 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 sub-journal */.
10857 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 Bitvec *pInJour
10858 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 nal; /*
10859 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 One bit for each
1085a 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 page in the dat
1085b 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
1085c 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 sqlite3_file *fd
1085d 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 ; /* F
1085e 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 ile descriptor f
1085f 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 or database */.
10860 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a sqlite3_file *j
10861 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 fd; /*
10862 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 File descriptor
10863 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c for main journal
10864 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
10865 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 le *sjfd;
10866 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 /* File descri
10867 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 ptor for sub-jou
10868 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f rnal */. i64 jo
10869 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 urnalOff;
1086a 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
1086b 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 69 6e write offset in
1086c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1086d 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e e */. i64 journ
1086e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 alHdr;
1086f 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 /* Byte offse
10870 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f t to previous jo
10871 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a urnal header */.
10872 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 PagerSavepoint
10873 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a *aSavepoint; /*
10874 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 76 65 Array of active
10875 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 savepoints */.
10876 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b int nSavepoint;
10877 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10878 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e Number of elemen
10879 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 ts in aSavepoint
1087a 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 [] */. char dbF
1087b 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 ileVers[16];
1087c 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 /* Changes w
1087d 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 73 65 henever database
1087e 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f file changes */
1087f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53 69 7a . u32 sectorSiz
10880 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
10881 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f 72 * Assumed sector
10882 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c size during rol
10883 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 69 6e 74 20 lback */.. int
10884 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 nExtra;
10885 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 /* Add t
10886 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74 his many bytes t
10887 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 o each in-memory
10888 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 76 page */. u32 v
10889 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 fsFlags;
1088a 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
1088b 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 73 2e for sqlite3_vfs.
1088c 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 xOpen() */. int
1088d 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 pageSize;
1088e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1088f 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 er of bytes in a
10890 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 page */. Pgno
10891 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 mxPgno;
10892 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
10893 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f m allowed size o
10894 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a f the database *
10895 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e /. char *zFilen
10896 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
10897 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 /* Name of the d
10898 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
10899 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c char *zJournal
1089a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1089b 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 Name of the jou
1089c 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 rnal file */. i
1089d 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 nt (*xBusyHandle
1089e 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 r)(void*); /* Fu
1089f 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 nction to call w
108a0 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f hen busy */. vo
108a1 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 id *pBusyHandler
108a2 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e Arg; /* Con
108a3 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f text argument fo
108a4 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a r xBusyHandler *
108a5 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
108a6 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c TEST. int nHit,
108a7 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20 nMiss;
108a8 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73 /* Cache hits
108a9 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a and missing */.
108aa 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 int nRead, nWr
108ab 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ite; /*
108ac 20 44 61 74 61 62 61 73 65 20 70 61 67 65 73 20 Database pages
108ad 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a read/written */.
108ae 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a #endif. void (*
108af 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 67 xReiniter)(DbPag
108b0 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 e*); /* Call thi
108b1 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 s routine when r
108b2 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a eloading pages *
108b3 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
108b4 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 HAS_CODEC. void
108b5 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 *(*xCodec)(void
108b6 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 *,void*,Pgno,int
108b7 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f ); /* Routine fo
108b8 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 r en/decoding da
108b9 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 ta */. void *pC
108ba 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20 20 20 odecArg;
108bb 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 /* First arg
108bc 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 28 ument to xCodec(
108bd 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 ) */.#endif. ch
108be 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 ar *pTmpSpace;
108bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
108c0 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 er.pageSize byte
108c1 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 s of space for t
108c2 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 34 20 mp use */. i64
108c3 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
108c4 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 ; /* Size
108c5 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69 73 limit for persis
108c6 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c tent journal fil
108c7 65 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a es */. PCache *
108c8 70 50 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 pPCache;
108c9 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
108ca 6f 20 70 61 67 65 20 63 61 63 68 65 20 6f 62 6a o page cache obj
108cb 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ect */. sqlite3
108cc 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 _backup *pBackup
108cd 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 ; /* Pointer
108ce 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f 69 to list of ongoi
108cf 6e 67 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 ng backup proces
108d0 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ses */.};../*.**
108d1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 The following g
108d2 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 lobal variables
108d3 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73 hold counters us
108d4 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e ed for.** testin
108d5 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e g purposes only.
108d6 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65 These variable
108d7 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 s do not exist i
108d8 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 n.** a non-testi
108d9 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65 ng build. These
108da 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e variables are n
108db 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a ot thread-safe..
108dc 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
108dd 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
108de 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 int sqlite3_pag
108df 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 er_readdb_count
108e0 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 = 0; /* Numbe
108e1 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 r of full pages
108e2 72 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a read from DB */.
108e3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
108e4 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 qlite3_pager_wri
108e5 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 tedb_count = 0;
108e6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
108e7 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65 ull pages writte
108e8 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54 n to DB */.SQLIT
108e9 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
108ea 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 3_pager_writej_c
108eb 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 ount = 0; /*
108ec 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
108ed 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e written to journ
108ee 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 al */.# define P
108ef 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b AGER_INCR(v) v+
108f0 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 +.#else.# define
108f1 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 PAGER_INCR(v).#
108f2 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a endif..../*.** J
108f3 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 ournal files beg
108f4 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c in with the foll
108f5 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69 owing magic stri
108f6 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a ng. The data.**
108f7 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 was obtained fr
108f8 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 om /dev/random.
108f9 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 It is used only
108fa 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65 as a sanity che
108fb 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 ck..**.** Since
108fc 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 version 2.8.0, t
108fd 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 he journal forma
108fe 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 t contains addit
108ff 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 ional sanity.**
10900 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 checking informa
10901 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f tion. If the po
10902 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20 wer fails while
10903 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 the journal is b
10904 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c eing.** written,
10905 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 semi-random gar
10906 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20 bage data might
10907 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f appear in the jo
10908 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 urnal.** file af
10909 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73 ter power is res
1090a 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74 tored. If an at
1090b 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61 tempt is then ma
1090c 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 de.** to roll th
1090d 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 e journal back,
1090e 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75 the database cou
1090f 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e ld be corrupted.
10910 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c The additional
10911 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b .** sanity check
10912 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61 ing data is an a
10913 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 ttempt to discov
10914 65 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69 er the garbage i
10915 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c n the.** journal
10916 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a and ignore it..
10917 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 **.** The sanity
10918 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d checking inform
10919 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 ation for the ne
1091a 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 w journal format
1091b 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 consists.** of
1091c 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75 a 32-bit checksu
1091d 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f m on each page o
1091e 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65 f data. The che
1091f 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 cksum covers bot
10920 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 h.** the page nu
10921 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61 mber and the pPa
10922 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 ger->pageSize by
10923 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20 tes of data for
10924 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 the page..** Thi
10925 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 s cksum is initi
10926 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 alized to a 32-b
10927 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 it random value
10928 74 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20 that appears in
10929 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
1092a 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20 ile right after
1092b 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 the header. The
1092c 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 random initiali
1092d 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 zer is important
1092e 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72 ,.** because gar
1092f 62 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61 bage data that a
10930 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e ppears at the en
10931 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 d of a journal i
10932 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 s likely.** data
10933 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69 that was once i
10934 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68 n other files th
10935 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e at have now been
10936 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68 deleted. If th
10937 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74 e.** garbage dat
10938 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f a came from an o
10939 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 bsolete journal
1093a 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73 file, the checks
1093b 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 ums might.** be
1093c 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79 correct. But by
1093d 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
1093e 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 e checksum to ra
1093f 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68 ndom value which
10940 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74 .** is different
10941 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e for every journ
10942 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 al, we minimize
10943 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 that risk..*/.st
10944 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
10945 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 ned char aJourna
10946 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 lMagic[] = {. 0
10947 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c xd9, 0xd5, 0x05,
10948 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61 0xf9, 0x20, 0xa
10949 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 1, 0x63, 0xd7,.}
1094a 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a ;../*.** The siz
1094b 65 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 68 e of the of each
1094c 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e 20 page record in
1094d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67 the journal is g
1094e 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 iven by.** the f
1094f 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a ollowing macro..
10950 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e */.#define JOURN
10951 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 AL_PG_SZ(pPager)
10952 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65 ((pPager->page
10953 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a Size) + 8)../*.*
10954 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 * The journal he
10955 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68 ader size for th
10956 69 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 69 is pager. This i
10957 73 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 61 s usually the sa
10958 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61 me .** size as a
10959 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 single disk sec
1095a 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 65 tor. See also se
1095b 74 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a tSectorSize()..*
1095c 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 /.#define JOURNA
1095d 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
1095e 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 (pPager->sector
1095f 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 Size)../*.** The
10960 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 macro MEMDB is
10961 74 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64 true if we are d
10962 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69 ealing with an i
10963 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
10964 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 e..** We do this
10965 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 as a macro so t
10966 68 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54 hat if the SQLIT
10967 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 E_OMIT_MEMORYDB
10968 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a macro is set,.**
10969 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 the value of ME
1096a 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f MDB will be a co
1096b 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63 nstant and the c
1096c 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 ompiler will opt
1096d 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 imize.** out cod
1096e 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 e that would nev
1096f 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 er execute..*/.#
10970 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
10971 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 T_MEMORYDB.# def
10972 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 ine MEMDB 0.#els
10973 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 e.# define MEMDB
10974 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 pPager->memDb.#
10975 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
10976 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 maximum legal p
10977 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 age number is (2
10978 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 ^31 - 1)..*/.#de
10979 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 fine PAGER_MAX_P
1097a 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a GNO 2147483647..
1097b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 0a #ifndef NDEBUG .
1097c 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a /*.** Usage:.**.
1097d 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 73 ** assert( ass
1097e 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 ert_pager_state(
1097f 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 74 pPager) );.*/.st
10980 61 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 5f atic int assert_
10981 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65 pager_state(Page
10982 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 2f r *pPager){.. /
10983 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 73 * A temp-file is
10984 20 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 52 always in PAGER
10985 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 41 _EXCLUSIVE or PA
10986 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 GER_SYNCED state
10987 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 . */. assert( p
10988 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d Pager->tempFile=
10989 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 =0 || pPager->st
1098a 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 ate>=PAGER_EXCLU
1098b 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 SIVE );.. /* Th
1098c 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e e changeCountDon
1098d 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 e flag is always
1098e 20 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 69 set for temp-fi
1098f 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 les */. assert(
10990 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
10991 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e e==0 || pPager->
10992 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 changeCountDone
10993 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a );.. return 1;.
10994 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
10995 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 Return true if i
10996 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 t is necessary t
10997 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 50 o write page *pP
10998 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a g into the sub-j
10999 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 ournal..** A pag
1099a 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 e needs to be wr
1099b 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 73 itten into the s
1099c 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68 ub-journal if th
1099d 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a 2a ere exists one.*
1099e 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 73 * or more open s
1099f 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 68 avepoints for wh
109a0 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 ich:.**.** * T
109a1 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 he page-number i
109a2 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
109a3 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 76 qual to PagerSav
109a4 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e epoint.nOrig, an
109a5 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 74 d.** * The bit
109a6 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
109a7 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 o the page-numbe
109a8 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e 0a r is not set in.
109a9 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 ** PagerSave
109aa 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 point.pInSavepoi
109ab 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nt..*/.static in
109ac 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 t subjRequiresPa
109ad 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a ge(PgHdr *pPg){.
109ae 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 Pgno pgno = pP
109af 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 g->pgno;. Pager
109b0 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
109b1 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 3b pPager;. int i;
109b2 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 . for(i=0; i<pP
109b3 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
109b4 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 ; i++){. Page
109b5 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 rSavepoint *p =
109b6 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f &pPager->aSavepo
109b7 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 int[i];. if(
109b8 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 p->nOrig>=pgno &
109b9 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 76 & 0==sqlite3Bitv
109ba 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 76 ecTest(p->pInSav
109bb 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 7b epoint, pgno) ){
109bc 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
109bd 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
109be 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
109bf 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
109c0 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 he page is alrea
109c1 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 dy in the journa
109c2 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 l file..*/.stati
109c3 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72 c int pageInJour
109c4 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b nal(PgHdr *pPg){
109c5 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
109c6 33 42 69 74 76 65 63 54 65 73 74 28 70 50 67 2d 3BitvecTest(pPg-
109c7 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 >pPager->pInJour
109c8 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b nal, pPg->pgno);
109c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 .}../*.** Read a
109ca 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
109cb 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 66 from the given f
109cc 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
109cd 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 Store the integ
109ce 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 er.** that is re
109cf 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 65 ad in *pRes. Re
109d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 turn SQLITE_OK i
109d1 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 f everything wor
109d2 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 ked, or an.** er
109d3 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 ror code is some
109d4 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
109d5 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 ..**.** All valu
109d6 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e es are stored on
109d7 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e 64 disk as big-end
109d8 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ian..*/.static i
109d9 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 73 71 nt read32bits(sq
109da 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 lite3_file *fd,
109db 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 i64 offset, u32
109dc 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e *pRes){. unsign
109dd 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 ed char ac[4];.
109de 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
109df 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20 3OsRead(fd, ac,
109e0 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 73 sizeof(ac), offs
109e1 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 et);. if( rc==S
109e2 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
109e3 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 *pRes = sqlite3G
109e4 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 7d et4byte(ac);. }
109e5 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
109e6 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 ./*.** Write a 3
109e7 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 2-bit integer in
109e8 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 to a string buff
109e9 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 6e er in big-endian
109ea 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a byte order..*/.
109eb 23 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 74 #define put32bit
109ec 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 50 s(A,B) sqlite3P
109ed 75 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c 42 ut4byte((u8*)A,B
109ee 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 )../*.** Write a
109ef 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
109f0 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 66 into the given f
109f1 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
109f2 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
109f3 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 K.** on success
109f4 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
109f5 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f is something go
109f6 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 es wrong..*/.sta
109f7 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62 tic int write32b
109f8 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 its(sqlite3_file
109f9 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 *fd, i64 offset
109fa 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 68 , u32 val){. ch
109fb 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 33 ar ac[4];. put3
109fc 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 0a 2bits(ac, val);.
109fd 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
109fe 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 20 OsWrite(fd, ac,
109ff 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 4, offset);.}../
10a00 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e *.** The argumen
10a01 74 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 t to this macro
10a02 69 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 is a file descri
10a03 70 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74 ptor (type sqlit
10a04 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 e3_file*)..** Re
10a05 74 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 20 turn 0 if it is
10a06 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e not open, or non
10a07 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31 -zero (but not 1
10a08 29 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a ) if it is..**.*
10a09 2a 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 * This is so tha
10a0a 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 t expressions ca
10a0b 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a n be written as:
10a0c 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f .**.** if( isO
10a0d 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
10a0e 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e ){ ....**.** in
10a0f 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 stead of.**.**
10a10 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64 if( pPager->jfd
10a11 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e ->pMethods ){ ..
10a12 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f ..*/.#define isO
10a13 70 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d pen(pFd) ((pFd)-
10a14 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a >pMethods)../*.*
10a15 2a 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 73 * If file pFd is
10a16 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 open, call sqli
10a17 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e te3OsUnlock() on
10a18 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 it..*/.static i
10a19 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 nt osUnlock(sqli
10a1a 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 te3_file *pFd, i
10a1b 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 nt eLock){. if(
10a1c 20 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 7b !isOpen(pFd) ){
10a1d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
10a1e 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 TE_OK;. }. ret
10a1f 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c urn sqlite3OsUnl
10a20 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b ock(pFd, eLock);
10a21 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
10a22 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e unction determin
10a23 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f es whether or no
10a24 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 t the atomic-wri
10a25 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a te optimization.
10a26 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 ** can be used w
10a27 69 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 ith this pager.
10a28 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e The optimization
10a29 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a can be used if:
10a2a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 .**.** (a) the
10a2b 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
10a2c 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 y OsDeviceCharac
10a2d 74 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 teristics() indi
10a2e 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 cates that.**
10a2f 20 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61 a database pa
10a30 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 ge may be writte
10a31 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e n atomically, an
10a32 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 d.** (b) the va
10a33 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
10a34 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 OsSectorSize() i
10a35 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
10a36 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 qual.** to
10a37 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a the page size..*
10a38 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a *.** The optimiz
10a39 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 6c ation is also al
10a3a 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f 72 ways enabled for
10a3b 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
10a3c 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 72 . It is.** an er
10a3d 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 ror to call this
10a3e 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 61 function if pPa
10a3f 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e ger is opened on
10a40 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a an in-memory.**
10a41 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
10a42 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 If the optimiza
10a43 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 tion cannot be u
10a44 73 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e sed, 0 is return
10a45 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 ed. If it can be
10a46 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 used,.** then t
10a47 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
10a48 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 d is the size of
10a49 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
10a4a 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e e when it.** con
10a4b 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64 tains rollback d
10a4c 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20 ata for exactly
10a4d 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 one page..*/.#if
10a4e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
10a4f 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73 E_ATOMIC_WRITE.s
10a50 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75 tatic int jrnlBu
10a51 66 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a fferSize(Pager *
10a52 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 pPager){. asser
10a53 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 t( !MEMDB );. i
10a54 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 f( !pPager->temp
10a55 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 File ){. int
10a56 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dc;
10a57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10a58 20 44 65 76 69 63 65 20 63 68 61 72 61 63 74 65 Device characte
10a59 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 69 ristics */. i
10a5a 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20 nt nSector;
10a5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10a5c 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 /* Sector size
10a5d 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 */. int szPag
10a5e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
10a5f 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
10a60 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 73 size */.. as
10a61 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
10a62 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 20 ger->fd) );.
10a63 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 dc = sqlite3OsDe
10a64 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
10a65 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b ics(pPager->fd);
10a66 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 70 . nSector = p
10a67 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
10a68 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d 20 e;. szPage =
10a69 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
10a6a 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 ;.. assert(SQ
10a6b 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
10a6c 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b C512==(512>>8));
10a6d 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 . assert(SQLI
10a6e 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 TE_IOCAP_ATOMIC6
10a6f 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 4K==(65536>>8));
10a70 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 26 . if( 0==(dc&
10a71 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 (SQLITE_IOCAP_AT
10a72 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 29 OMIC|(szPage>>8)
10a73 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a 50 ) || nSector>szP
10a74 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 age) ){. re
10a75 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
10a76 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 52 }.. return JOUR
10a77 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
10a78 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f r) + JOURNAL_PG_
10a79 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 65 SZ(pPager);.}.#e
10a7a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 ndif../*.** If S
10a7b 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
10a7c 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65 S is defined the
10a7d 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 6e n we do some san
10a7e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 ity checking.**
10a7f 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 69 on the cache usi
10a80 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 69 ng a hash functi
10a81 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 on. This is use
10a82 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a d for testing.**
10a83 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f and debugging o
10a84 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 nly..*/.#ifdef S
10a85 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
10a86 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 S./*.** Return a
10a87 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 20 32-bit hash of
10a88 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66 6f the page data fo
10a89 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 r pPage..*/.stat
10a8a 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 74 ic u32 pager_dat
10a8b 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 2c ahash(int nByte,
10a8c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
10a8d 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 61 pData){. u32 ha
10a8e 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b sh = 0;. int i;
10a8f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 . for(i=0; i<nB
10a90 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 68 yte; i++){. h
10a91 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 39 ash = (hash*1039
10a92 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 20 ) + pData[i];.
10a93 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 3b }. return hash;
10a94 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 .}.static u32 pa
10a95 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 48 ger_pagehash(PgH
10a96 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 dr *pPage){. re
10a97 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 68 turn pager_datah
10a98 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 65 ash(pPage->pPage
10a99 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 6e r->pageSize, (un
10a9a 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 50 signed char *)pP
10a9b 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a 73 age->pData);.}.s
10a9c 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 tatic void pager
10a9d 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 67 _set_pagehash(Pg
10a9e 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 70 Hdr *pPage){. p
10a9f 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 3d Page->pageHash =
10aa0 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 pager_pagehash(
10aa1 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pPage);.}../*.**
10aa2 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 20 The CHECK_PAGE
10aa3 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 67 macro takes a Pg
10aa4 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 6d Hdr* as an argum
10aa5 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f 43 ent. If SQLITE_C
10aa6 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 73 HECK_PAGES.** is
10aa7 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e 44 defined, and ND
10aa8 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 EBUG is not defi
10aa9 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 ned, an assert()
10aaa 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 6b statement check
10aab 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 61 s.** that the pa
10aac 67 65 20 69 73 20 65 69 74 68 65 72 20 64 69 72 ge is either dir
10aad 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 63 ty or still matc
10aae 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 74 hes the calculat
10aaf 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a 2f ed page-hash..*/
10ab0 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 .#define CHECK_P
10ab1 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 65 AGE(x) checkPage
10ab2 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 (x).static void
10ab3 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 20 checkPage(PgHdr
10ab4 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a *pPg){. Pager *
10ab5 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
10ab6 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 ager;. assert(
10ab7 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 7c !pPg->pageHash |
10ab8 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 | pPager->errCod
10ab9 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 2d e. || (pPg-
10aba 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
10abb 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65 TY) || pPg->page
10abc 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65 Hash==pager_page
10abd 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a hash(pPg) );.}..
10abe 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61 #else.#define pa
10abf 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59 ger_datahash(X,Y
10ac0 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67 ) 0.#define pag
10ac1 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20 er_pagehash(X)
10ac2 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 0.#define CHECK_
10ac3 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20 PAGE(x).#endif
10ac4 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f /* SQLITE_CHECK_
10ac5 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 PAGES */../*.**
10ac6 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c When this is cal
10ac7 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 led the journal
10ac8 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 file for pager p
10ac9 50 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70 Pager must be op
10aca 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 en..** This func
10acb 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f tion attempts to
10acc 20 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a read a master j
10acd 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
10ace 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e from the .** en
10acf 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e d of the file an
10ad0 64 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c d, if successful
10ad1 2c 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f , copies it into
10ad2 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 memory supplied
10ad3 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c .** by the call
10ad4 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 er. See comments
10ad5 20 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74 above writeMast
10ad6 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 erJournal() for
10ad7 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 the format.** us
10ad8 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61 ed to store a ma
10ad9 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
10ada 65 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e e name at the en
10adb 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 d of a journal f
10adc 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 ile..**.** zMast
10add 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f er must point to
10ade 20 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 a buffer of at
10adf 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 least nMaster by
10ae0 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 tes allocated by
10ae1 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 .** the caller.
10ae2 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 This should be s
10ae3 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 qlite3_vfs.mxPat
10ae4 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 hname+1 (to ensu
10ae5 72 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 re there is.** e
10ae6 6e 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 nough space to w
10ae7 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 rite the master
10ae8 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 journal name). I
10ae9 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 f the master jou
10aea 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 rnal.** name in
10aeb 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c the journal is l
10aec 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 onger than nMast
10aed 65 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 er bytes (includ
10aee 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 ing a.** nul-ter
10aef 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 minator), then t
10af0 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 his is handled a
10af1 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a s if no master j
10af2 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 ournal name.** w
10af3 65 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 ere present in t
10af4 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a he journal..**.*
10af5 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f * If a master jo
10af6 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
10af7 69 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 is present at th
10af8 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 e end of the jou
10af9 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 rnal.** file, th
10afa 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 en it is copied
10afb 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 into the buffer
10afc 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d pointed to by zM
10afd 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d aster. A.** nul-
10afe 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 terminator byte
10aff 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 is appended to t
10b00 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77 he buffer follow
10b01 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a ing the master.*
10b02 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e * journal file n
10b03 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 ame..**.** If it
10b04 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 is determined t
10b05 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f hat no master jo
10b06 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
10b07 69 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a is present .** z
10b08 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74 Master[0] is set
10b09 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 to 0 and SQLITE
10b0a 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a _OK returned..**
10b0b 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
10b0c 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 61 occurs while rea
10b0d 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f ding from the jo
10b0e 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53 urnal file, an S
10b0f 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63 QLite.** error c
10b10 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
10b11 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
10b12 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c eadMasterJournal
10b13 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
10b14 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73 Jrnl, char *zMas
10b15 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72 ter, u32 nMaster
10b16 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
10b17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b18 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
10b19 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20 /. u32 len;
10b1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10b1b 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 * Length in byte
10b1c 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 s of master jour
10b1d 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 nal name */. i6
10b1e 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 4 szJ;
10b1f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
10b20 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 l size in bytes
10b21 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 of journal file
10b22 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63 pJrnl */. u32 c
10b23 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 ksum;
10b24 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 /* MJ chec
10b25 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 ksum value read
10b26 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a from journal */.
10b27 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 u32 u;
10b28 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b29 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f Unsigned loop co
10b2a 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 unter */. unsig
10b2b 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b ned char aMagic[
10b2c 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65 8]; /* A buffe
10b2d 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 r to hold the ma
10b2e 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 gic header */.
10b2f 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 zMaster[0] = '\0
10b30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 ';.. if( SQLITE
10b31 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
10b32 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 e3OsFileSize(pJr
10b33 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c nl, &szJ)). ||
10b34 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51 szJ<16. || SQ
10b35 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
10b36 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c ead32bits(pJrnl,
10b37 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a szJ-16, &len)).
10b38 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 || len>=nMast
10b39 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 er . || SQLITE
10b3a 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 _OK!=(rc = read3
10b3b 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2bits(pJrnl, szJ
10b3c 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20 -12, &cksum)).
10b3d 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
10b3e 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
10b3f 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 ad(pJrnl, aMagic
10b40 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20 , 8, szJ-8)).
10b41 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 || memcmp(aMagic
10b42 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c , aJournalMagic,
10b43 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 8). || SQLITE
10b44 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
10b45 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 e3OsRead(pJrnl,
10b46 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a zMaster, len, sz
10b47 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a J-16-len)). ){.
10b48 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
10b49 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 }.. /* See if
10b4a 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 the checksum mat
10b4b 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 ches the master
10b4c 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a journal name */.
10b4d 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e for(u=0; u<len
10b4e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 ; u++){. cksu
10b4f 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b m -= zMaster[u];
10b50 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d . }. if( cksum
10b51 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
10b52 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e e checksum doesn
10b53 27 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 't add up, then
10b54 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 one or more of t
10b55 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a he disk sectors.
10b56 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e ** containin
10b57 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 g the master jou
10b58 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 rnal filename is
10b59 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 corrupted. This
10b5a 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 means. ** de
10b5b 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 finitely roll ba
10b5c 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 ck, so just retu
10b5d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 rn SQLITE_OK and
10b5e 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a report a (nul).
10b5f 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f ** master-jo
10b60 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a urnal filename..
10b61 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d */. len =
10b62 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 0;. }. zMaste
10b63 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 r[len] = '\0';.
10b64 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
10b65 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
10b66 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 Return the offse
10b67 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 t of the sector
10b68 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69 boundary at or i
10b69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66 mmediately .** f
10b6a 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c ollowing the val
10b6b 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f ue in pPager->jo
10b6c 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69 urnalOff, assumi
10b6d 6e 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20 ng a sector .**
10b6e 73 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e size of pPager->
10b6f 73 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73 sectorSize bytes
10b70 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 ..**.** i.e for
10b71 61 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 a sector size of
10b72 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 512:.**.** Pa
10b73 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 ger.journalOff
10b74 20 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 76 Return v
10b75 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d alue.** ------
10b76 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
10b77 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
10b78 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 -.** 0
10b79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b7a 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20 0.** 512
10b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b7c 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20 512.** 100
10b7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b7e 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30 512.** 20
10b7f 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00
10b80 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 2048.**
10b81 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a .*/.static i64 j
10b82 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 ournalHdrOffset(
10b83 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
10b84 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 i64 offset = 0
10b85 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 ;. i64 c = pPag
10b86 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a er->journalOff;.
10b87 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f if( c ){. o
10b88 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a ffset = ((c-1)/J
10b89 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
10b8a 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 ager) + 1) * JOU
10b8b 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
10b8c 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 er);. }. asser
10b8d 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 t( offset%JOURNA
10b8e 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
10b8f 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
10b90 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 offset>=c );.
10b91 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d assert( (offset-
10b92 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 c)<JOURNAL_HDR_S
10b93 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 Z(pPager) );. r
10b94 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a eturn offset;.}.
10b95 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e ./*.** The journ
10b96 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 al file must be
10b97 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 open when this f
10b98 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10b99 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 d..**.** This fu
10b9a 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
10b9b 70 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c p if the journal
10b9c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 file has not be
10b9d 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a en written to.**
10b9e 20 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 within the curr
10b9f 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
10ba0 28 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a (i.e. if Pager.j
10ba1 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a ournalOff==0)..*
10ba2 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61 *.** If doTrunca
10ba3 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f te is non-zero o
10ba4 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72 r the Pager.jour
10ba5 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72 nalSizeLimit var
10ba6 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20 iable is.** set
10ba7 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63 to 0, then trunc
10ba8 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ate the journal
10ba9 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74 file to zero byt
10baa 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65 es in size. Othe
10bab 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 rwise,.** zero t
10bac 68 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 65 he 28-byte heade
10bad 72 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f r at the start o
10bae 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
10baf 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 le. In either ca
10bb0 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 se, .** if the p
10bb1 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e ager is not in n
10bb2 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e o-sync mode, syn
10bb3 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 c the journal fi
10bb4 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a le immediately .
10bb5 2a 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e 67 ** after writing
10bb6 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69 or truncating i
10bb7 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65 t..**.** If Page
10bb8 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d r.journalSizeLim
10bb9 69 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 it is set to a p
10bba 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 ositive, non-zer
10bbb 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 o value, and.**
10bbc 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72 following the tr
10bbd 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f uncation or zero
10bbe 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 62 ing described ab
10bbf 6f 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ove the size of
10bc0 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the .** journal
10bc1 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73 file in bytes is
10bc2 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 larger than thi
10bc3 73 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72 s value, then tr
10bc4 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f uncate the.** jo
10bc5 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61 urnal file to Pa
10bc6 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c ger.journalSizeL
10bc7 69 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 20 imit bytes. The
10bc8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 journal file doe
10bc9 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f s.** not need to
10bca 20 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f be synced follo
10bcb 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 wing this operat
10bcc 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ion..**.** If an
10bcd 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
10bce 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 , abandon proces
10bcf 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20 sing and return
10bd0 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
10bd1 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c e..** Otherwise,
10bd2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
10bd3 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 K..*/.static int
10bd4 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 zeroJournalHdr(
10bd5 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
10bd6 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a nt doTruncate){.
10bd7 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
10bd8 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
10bd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10bda 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
10bdb 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 de */. assert(
10bdc 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
10bdd 66 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 fd) );. if( pPa
10bde 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
10bdf 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34 ){. const i64
10be0 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 iLimit = pPager
10be1 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d ->journalSizeLim
10be2 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 it; /* Local
10be3 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a cache of jsl */.
10be4 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a . IOTRACE(("J
10be5 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 ZEROHDR %p\n", p
10be6 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 Pager)). if(
10be7 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c doTruncate || iL
10be8 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 imit==0 ){.
10be9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
10bea 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
10beb 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c jfd, 0);. }el
10bec 73 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 se{. static
10bed 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f const char zero
10bee 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 Hdr[28] = {0};.
10bef 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
10bf0 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
10bf1 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 >jfd, zeroHdr, s
10bf2 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 izeof(zeroHdr),
10bf3 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0);. }. if
10bf4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
10bf5 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 && !pPager->noSy
10bf6 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d nc ){. rc =
10bf7 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
10bf8 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 Pager->jfd, SQLI
10bf9 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
10bfa 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c |pPager->sync_fl
10bfb 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ags);. }..
10bfc 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e /* At this poin
10bfd 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
10bfe 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 n is committed b
10bff 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 ut the write loc
10c00 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 k . ** is sti
10c01 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 ll held on the f
10c02 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 ile. If there is
10c03 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f a size limit co
10c04 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20 nfigured for .
10c05 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 ** the persist
10c06 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 ent journal and
10c07 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
10c08 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 currently consu
10c09 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 mes more. **
10c0a 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20 space than that
10c0b 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 limit allows for
10c0c 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f , truncate it no
10c0d 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e w. There is no n
10c0e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 eed. ** to sy
10c0f 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c nc the file foll
10c10 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 owing this opera
10c11 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
10c12 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
10c13 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 OK && iLimit>0 )
10c14 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a {. i64 sz;.
10c15 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
10c16 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
10c17 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a ger->jfd, &sz);.
10c18 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
10c19 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c LITE_OK && sz>iL
10c1a 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 imit ){.
10c1b 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 rc = sqlite3OsTr
10c1c 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a uncate(pPager->j
10c1d 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 fd, iLimit);.
10c1e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
10c1f 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
10c20 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c *.** The journal
10c21 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 file must be op
10c22 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 en when this rou
10c23 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 tine is called.
10c24 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 A journal.** hea
10c25 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 der (JOURNAL_HDR
10c26 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72 _SZ bytes) is wr
10c27 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
10c28 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 ournal file at t
10c29 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f he.** current lo
10c2a 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 cation..**.** Th
10c2b 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 e format for the
10c2c 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
10c2d 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
10c2e 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67 * - 8 bytes: Mag
10c2f 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a ic identifying j
10c30 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a ournal format..*
10c31 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d * - 4 bytes: Num
10c32 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 ber of records i
10c33 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 n journal, or -1
10c34 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 no-sync mode is
10c35 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 on..** - 4 byte
10c36 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 s: Random number
10c37 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68 used for page h
10c38 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 ash..** - 4 byte
10c39 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62 s: Initial datab
10c3a 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a ase page count..
10c3b 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65 ** - 4 bytes: Se
10c3c 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62 ctor size used b
10c3d 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 y the process th
10c3e 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f at wrote this jo
10c3f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 urnal..** - 4 by
10c40 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61 tes: Database pa
10c41 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 ge size..** .**
10c42 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 Followed by (JOU
10c43 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 RNAL_HDR_SZ - 28
10c44 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 ) bytes of unuse
10c45 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 d space..*/.stat
10c46 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 ic int writeJour
10c47 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 nalHdr(Pager *pP
10c48 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
10c49 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
10c4a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10c4b 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
10c4c 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d char *zHeader =
10c4d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 pPager->pTmpSpa
10c4e 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 ce; /* Temporar
10c4f 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 y space used to
10c50 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a build header */.
10c51 20 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d 20 u32 nHeader =
10c52 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
10c53 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 ; /* Size of
10c54 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 buffer pointed
10c55 74 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a 2f to by zHeader */
10c56 0a 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 20 . u32 nWrite;
10c57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10c58 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
10c59 6f 66 20 68 65 61 64 65 72 20 73 65 63 74 6f 72 of header sector
10c5a 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e written */. in
10c5b 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 t ii;
10c5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10c5d 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
10c5e 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 r */.. assert(
10c5f 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
10c60 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a fd) ); /* J
10c61 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
10c62 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 be open. */..
10c63 69 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 if( nHeader>JOUR
10c64 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
10c65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 r) ){. nHeade
10c66 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f r = JOURNAL_HDR_
10c67 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a SZ(pPager);. }.
10c68 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 . /* If there a
10c69 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f re active savepo
10c6a 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 ints and any of
10c6b 74 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 65 them were create
10c6c 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 d . ** since th
10c6d 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f e most recent jo
10c6e 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 73 urnal header was
10c6f 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 written, update
10c70 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 the . ** Pager
10c71 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 Savepoint.iHdrOf
10c72 66 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e fset fields now.
10c73 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 . */. for(ii=0
10c74 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 ; ii<pPager->nSa
10c75 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a vepoint; ii++){.
10c76 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
10c77 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 aSavepoint[ii].i
10c78 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a HdrOffset==0 ){.
10c79 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 pPager->aS
10c7a 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 avepoint[ii].iHd
10c7b 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 rOffset = pPager
10c7c 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 ->journalOff;.
10c7d 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 }. }.. pPage
10c7e 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 r->journalHdr =
10c7f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
10c80 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f ff = journalHdrO
10c81 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 ffset(pPager);.
10c82 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c memcpy(zHeader,
10c83 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
10c84 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
10c85 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a 20 0a 20 agic));.. /* .
10c86 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 ** Write the nR
10c87 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e ec Field - the n
10c88 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 umber of page re
10c89 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f cords that follo
10c8a 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 w this. ** jour
10c8b 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d nal header. Norm
10c8c 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72 ally, zero is wr
10c8d 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61 itten to this va
10c8e 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 lue at this time
10c8f 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65 .. ** After the
10c90 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 64 records are add
10c91 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 ed to the journa
10c92 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e l (and the journ
10c93 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a al synced, . **
10c94 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 if in full-sync
10c95 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f mode), the zero
10c96 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 is overwritten
10c97 77 69 74 68 20 74 68 65 20 74 72 75 65 20 6e 75 with the true nu
10c98 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63 mber. ** of rec
10c99 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f ords (see syncJo
10c9a 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 urnal()).. **.
10c9b 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74 ** A faster alt
10c9c 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77 ernative is to w
10c9d 72 69 74 65 20 30 78 46 46 46 46 46 46 46 46 20 rite 0xFFFFFFFF
10c9e 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c to the nRec fiel
10c9f 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61 d. When. ** rea
10ca0 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c ding the journal
10ca1 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c this value tell
10ca2 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75 s SQLite to assu
10ca3 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a me that the. **
10ca4 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 rest of the jou
10ca5 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 rnal file contai
10ca6 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 ns valid page re
10ca7 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 75 cords. This assu
10ca8 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 mption. ** is d
10ca9 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20 angerous, as if
10caa 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 a failure occurr
10cab 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 6e ed whilst writin
10cac 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c g to the journal
10cad 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61 . ** file it ma
10cae 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67 y contain some g
10caf 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 65 arbage data. The
10cb0 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e 61 re are two scena
10cb1 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 rios. ** where
10cb2 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 65 this risk can be
10cb3 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 ignored:. **.
10cb4 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 ** * When the
10cb5 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d pager is in no-
10cb6 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 sync mode. Corru
10cb7 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 ption can follow
10cb8 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 a. ** powe
10cb9 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69 r failure in thi
10cba 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a 20 s case anyway..
10cbb 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 **. ** * Whe
10cbc 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 n the SQLITE_IOC
10cbd 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 66 AP_SAFE_APPEND f
10cbe 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 73 lag is set. This
10cbf 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a guarantees. **
10cc0 20 20 20 20 20 74 68 61 74 20 67 61 72 62 61 67 that garbag
10cc1 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20 e data is never
10cc2 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 appended to the
10cc3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 journal file..
10cc4 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f */. assert( isO
10cc5 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
10cc6 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e || pPager->noSyn
10cc7 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 c );. if( (pPag
10cc8 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28 er->noSync) || (
10cc9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
10cca 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
10ccb 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20 ALMODE_MEMORY).
10ccc 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44 || (sqlite3OsD
10ccd 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
10cce 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
10ccf 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 &SQLITE_IOCAP_SA
10cd0 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b FE_APPEND) . ){
10cd1 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 . put32bits(&
10cd2 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
10cd3 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 JournalMagic)],
10cd4 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 7d 0xffffffff);. }
10cd5 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32 62 else{. put32b
10cd6 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
10cd7 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
10cd8 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 c)], 0);. }..
10cd9 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68 /* The random ch
10cda 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c eck-hash initial
10cdb 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74 iser */ . sqlit
10cdc 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
10cdd 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73 zeof(pPager->cks
10cde 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72 umInit), &pPager
10cdf 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 ->cksumInit);.
10ce0 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
10ce1 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
10ce2 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61 alMagic)+4], pPa
10ce3 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b ger->cksumInit);
10ce4 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 . /* The initia
10ce5 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 l database size
10ce6 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 */. put32bits(&
10ce7 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
10ce8 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d JournalMagic)+8]
10ce9 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 , pPager->dbOrig
10cea 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 Size);. /* The
10ceb 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 assumed sector s
10cec 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f ize for this pro
10ced 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62 cess */. put32b
10cee 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
10cef 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
10cf0 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e c)+12], pPager->
10cf1 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 sectorSize);..
10cf2 2f 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 /* The page size
10cf3 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 */. put32bits(
10cf4 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
10cf5 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 aJournalMagic)+1
10cf6 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 6], pPager->page
10cf7 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 Size);.. /* Ini
10cf8 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 61 tializing the ta
10cf9 69 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 il of the buffer
10cfa 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
10cfb 79 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a 20 y. Everything.
10cfc 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20 69 ** works find i
10cfd 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
10cfe 6d 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69 74 memset() is omit
10cff 74 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69 61 ted. But initia
10d00 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 lizing. ** the
10d01 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73 20 memory prevents
10d02 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63 6f valgrind from co
10d03 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77 65 mplaining, so we
10d04 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f 0a are willing to.
10d05 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70 65 ** take the pe
10d06 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a 20 rformance hit..
10d07 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a 48 */. memset(&zH
10d08 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
10d09 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d 2c urnalMagic)+20],
10d0a 20 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48 65 0,. nHe
10d0b 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a 6f ader-(sizeof(aJo
10d0c 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29 29 urnalMagic)+20))
10d0d 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 ;.. /* In theor
10d0e 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e 65 y, it is only ne
10d0f 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 cessary to write
10d10 20 74 68 65 20 32 38 20 62 79 74 65 73 20 74 68 the 28 bytes th
10d11 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f 75 at the . ** jou
10d12 72 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e 73 rnal header cons
10d13 75 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 72 umes to the jour
10d14 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20 54 nal file here. T
10d15 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 hen increment th
10d16 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a 6f e . ** Pager.jo
10d17 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62 6c urnalOff variabl
10d18 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 e by JOURNAL_HDR
10d19 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65 20 _SZ so that the
10d1a 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f 72 next . ** recor
10d1b 64 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 d is written to
10d1c 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 the following se
10d1d 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61 20 ctor (leaving a
10d1e 67 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65 0a gap in the file.
10d1f 20 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20 62 ** that will b
10d20 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69 6c e implicitly fil
10d21 6c 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f 53 led in by the OS
10d22 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 ).. **. ** How
10d23 65 76 65 72 20 69 74 20 68 61 73 20 62 65 65 6e ever it has been
10d24 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 discovered that
10d25 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 on some systems
10d26 20 74 68 69 73 20 70 61 74 74 65 72 6e 20 63 61 this pattern ca
10d27 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e 69 n . ** be signi
10d28 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 20 ficantly slower
10d29 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 6c than contiguousl
10d2a 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 y writing data t
10d2b 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a 2a o the file,. **
10d2c 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 even if that me
10d2d 61 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 77 ans explicitly w
10d2e 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 riting data to t
10d2f 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a he block of . *
10d30 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 * (JOURNAL_HDR_S
10d31 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 74 68 Z - 28) bytes th
10d32 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75 at will not be u
10d33 73 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73 20 sed. So that is
10d34 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f 6e what. ** is don
10d35 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 e. . **. ** Th
10d36 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69 72 e loop is requir
10d37 65 64 20 68 65 72 65 20 69 6e 20 63 61 73 65 20 ed here in case
10d38 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 the sector-size
10d39 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
10d3a 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 he . ** databas
10d3b 65 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69 6e e page size. Sin
10d3c 63 65 20 74 68 65 20 7a 48 65 61 64 65 72 20 62 ce the zHeader b
10d3d 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50 61 uffer is only Pa
10d3e 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 2a ger.pageSize. *
10d3f 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c * bytes in size,
10d40 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 63 more than one c
10d41 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 all to sqlite3Os
10d42 57 72 69 74 65 28 29 20 6d 61 79 20 62 65 20 72 Write() may be r
10d43 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f 20 equired. ** to
10d44 70 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e 74 populate the ent
10d45 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 ire journal head
10d46 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f 20 er sector.. */
10d47 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30 3b . for(nWrite=0;
10d48 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26 rc==SQLITE_OK&&
10d49 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48 nWrite<JOURNAL_H
10d4a 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20 6e DR_SZ(pPager); n
10d4b 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29 7b Write+=nHeader){
10d4c 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a . IOTRACE(("J
10d4d 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e HDR %p %lld %d\n
10d4e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 ", pPager, pPage
10d4f 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 6e r->journalHdr, n
10d50 48 65 61 64 65 72 29 29 0a 20 20 20 20 72 63 20 Header)). rc
10d51 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
10d52 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 (pPager->jfd, zH
10d53 65 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c 20 eader, nHeader,
10d54 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
10d55 66 66 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d ff);. pPager-
10d56 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 6e >journalOff += n
10d57 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 Header;. }.. r
10d58 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
10d59 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ** The journal f
10d5a 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e ile must be open
10d5b 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 when this is ca
10d5c 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 lled. A journal
10d5d 68 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20 28 header file.** (
10d5e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 JOURNAL_HDR_SZ b
10d5f 79 74 65 73 29 20 69 73 20 72 65 61 64 20 66 72 ytes) is read fr
10d60 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c om the current l
10d61 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a ocation in the j
10d62 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 ournal.** file.
10d63 54 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 The current loca
10d64 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 tion in the jour
10d65 6e 61 6c 20 66 69 6c 65 20 69 73 20 67 69 76 65 nal file is give
10d66 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 2d 3e n by.** pPager->
10d67 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65 20 journalOff. See
10d68 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 comments above f
10d69 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 unction writeJou
10d6a 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a 2a 2a rnalHdr() for.**
10d6b 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f a description o
10d6c 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 f the journal he
10d6d 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a ader format..**.
10d6e 2a 2a 20 49 66 20 74 68 65 20 68 65 61 64 65 72 ** If the header
10d6f 20 69 73 20 72 65 61 64 20 73 75 63 63 65 73 73 is read success
10d70 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 69 73 fully, *pNRec is
10d71 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 set to the numb
10d72 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72 65 er of.** page re
10d73 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 cords following
10d74 74 68 69 73 20 68 65 61 64 65 72 20 61 6e 64 20 this header and
10d75 2a 70 44 62 53 69 7a 65 20 69 73 20 73 65 74 20 *pDbSize is set
10d76 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 to the size of t
10d77 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 62 he.** database b
10d78 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 efore the transa
10d79 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e 20 ction began, in
10d7a 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50 61 pages. Also, pPa
10d7b 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a ger->cksumInit.*
10d7c 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 * is set to the
10d7d 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 value read from
10d7e 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
10d7f 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 er. SQLITE_OK is
10d80 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e 20 returned.** in
10d81 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a this case..**.**
10d82 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
10d83 68 65 61 64 65 72 20 66 69 6c 65 20 61 70 70 65 header file appe
10d84 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70 ars to be corrup
10d85 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 ted, SQLITE_DONE
10d86 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 is.** returned
10d87 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 20 2a and *pNRec and *
10d88 50 44 62 53 69 7a 65 20 61 72 65 20 75 6e 64 65 PDbSize are unde
10d89 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 52 4e fined. If JOURN
10d8a 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 0a AL_HDR_SZ bytes.
10d8b 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 61 ** cannot be rea
10d8c 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e d from the journ
10d8d 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 6f 72 al file an error
10d8e 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
10d8f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
10d90 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 readJournalHdr(
10d91 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
10d92 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
10d93 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
10d94 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c */. i64 journal
10d95 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 Size,
10d96 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
10d97 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 open journal fi
10d98 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 le in bytes */.
10d99 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 20 20 u32 *pNRec,
10d9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10d9b 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 61 64 OUT: Value read
10d9c 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 20 66 from the nRec f
10d9d 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 2a 70 ield */. u32 *p
10d9e 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 DbSize
10d9f 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 /* OUT: V
10da0 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e 61 6c alue of original
10da1 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 66 database size f
10da2 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 ield */.){. int
10da3 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
10da4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
10da5 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e urn code */. un
10da6 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 signed char aMag
10da7 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41 20 ic[8]; /* A
10da8 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 buffer to hold t
10da9 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 20 he magic header
10daa 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 */. i64 iHdrOff
10dab 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10dac 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 6a /* Offset of j
10dad 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 62 65 ournal header be
10dae 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61 ing read */.. a
10daf 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
10db0 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 20 ager->jfd) );
10db1 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 /* Journal fi
10db2 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e le must be open.
10db3 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 6e 63 */.. /* Advanc
10db4 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f e Pager.journalO
10db5 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 ff to the start
10db6 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 of the next sect
10db7 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 or. If the. **
10db8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
10db9 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68 too small for th
10dba 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 61 64 ere to be a head
10dbb 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 68 69 er stored at thi
10dbc 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 72 65 s. ** point, re
10dbd 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
10dbe 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d .. */. pPager-
10dbf 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f >journalOff = jo
10dc0 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 urnalHdrOffset(p
10dc1 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50 Pager);. if( pP
10dc2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
10dc3 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 +JOURNAL_HDR_SZ(
10dc4 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61 pPager) > journa
10dc5 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 lSize ){. ret
10dc6 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b urn SQLITE_DONE;
10dc7 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d . }. iHdrOff =
10dc8 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
10dc9 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 Off;.. /* Read
10dca 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 62 in the first 8 b
10dcb 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ytes of the jour
10dcc 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20 74 nal header. If t
10dcd 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 hey do not match
10dce 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69 63 . ** the magic
10dcf 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61 74 string found at
10dd0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 the start of ea
10dd1 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 ch journal heade
10dd2 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 53 r, return. ** S
10dd3 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 20 61 QLITE_DONE. If a
10dd4 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
10dd5 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 s, return an err
10dd6 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69 or code. Otherwi
10dd7 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 65 64 se,. ** proceed
10dd8 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 .. */. rc = sq
10dd9 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
10dda 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c er->jfd, aMagic,
10ddb 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c sizeof(aMagic),
10ddc 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 69 66 28 iHdrOff);. if(
10ddd 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 rc ){. retur
10dde 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 n rc;. }. if(
10ddf 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 memcmp(aMagic, a
10de0 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 JournalMagic, si
10de1 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d 30 zeof(aMagic))!=0
10de2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
10de3 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a QLITE_DONE;. }.
10de4 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 66 . /* Read the f
10de5 69 72 73 74 20 74 68 72 65 65 20 33 32 2d 62 69 irst three 32-bi
10de6 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 t fields of the
10de7 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a 20 journal header:
10de8 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 66 69 The nRec. ** fi
10de9 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73 75 eld, the checksu
10dea 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61 6e m-initializer an
10deb 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 d the database s
10dec 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72 74 ize at the start
10ded 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 61 . ** of the tra
10dee 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72 6e nsaction. Return
10def 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
10df0 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 f anything goes
10df1 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 wrong.. */. if
10df2 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
10df3 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 = read32bits(pP
10df4 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f ager->jfd, iHdrO
10df5 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a 20 20 ff+8, pNRec)).
10df6 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
10df7 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
10df8 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
10df9 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 72 rOff+12, &pPager
10dfa 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a 20 20 ->cksumInit)).
10dfb 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
10dfc 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
10dfd 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
10dfe 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 7a 65 rOff+16, pDbSize
10dff 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
10e00 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 rn rc;. }.. if
10e01 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
10e02 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 75 lOff==0 ){. u
10e03 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 20 20 32 iPageSize;
10e04 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
10e05 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f age-size field o
10e06 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 f journal header
10e07 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 65 63 */. u32 iSec
10e08 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 torSize;
10e09 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 2d 73 /* Sector-s
10e0a 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75 ize field of jou
10e0b 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 rnal header */.
10e0c 20 20 20 75 31 36 20 69 50 61 67 65 53 69 7a 65 u16 iPageSize
10e0d 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 16;
10e0e 2f 2a 20 43 6f 70 79 20 6f 66 20 69 50 61 67 65 /* Copy of iPage
10e0f 53 69 7a 65 20 69 6e 20 31 36 2d 62 69 74 20 76 Size in 16-bit v
10e10 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 ariable */..
10e11 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 /* Read the page
10e12 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 -size and sector
10e13 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20 68 65 -size journal he
10e14 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a 2f 0a ader fields. */.
10e15 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
10e16 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 K!=(rc = read32b
10e17 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
10e18 20 69 48 64 72 4f 66 66 2b 32 30 2c 20 26 69 53 iHdrOff+20, &iS
10e19 65 63 74 6f 72 53 69 7a 65 29 29 0a 20 20 20 20 ectorSize)).
10e1a 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
10e1b 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
10e1c 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
10e1d 72 4f 66 66 2b 32 34 2c 20 26 69 50 61 67 65 53 rOff+24, &iPageS
10e1e 69 7a 65 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 ize)). ){.
10e1f 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
10e20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 }.. /* Chec
10e21 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 k that the value
10e22 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 s read from the
10e23 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 page-size and se
10e24 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 73 ctor-size fields
10e25 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 69 74 68 . ** are with
10e26 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 62 65 20 in range. To be
10e27 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 6f 74 68 'in range', both
10e28 20 76 61 6c 75 65 73 20 6e 65 65 64 20 74 6f 20 values need to
10e29 62 65 20 61 20 70 6f 77 65 72 0a 20 20 20 20 2a be a power. *
10e2a 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 74 65 72 * of two greater
10e2b 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
10e2c 6f 20 35 31 32 2c 20 61 6e 64 20 6e 6f 74 20 67 o 512, and not g
10e2d 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 69 reater than thei
10e2e 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 65 63 r . ** respec
10e2f 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69 6d tive compile tim
10e30 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74 73 e maximum limits
10e31 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
10e32 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 20 20 iPageSize<512
10e33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10e34 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3c 35 || iSectorSize<5
10e35 31 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 65 12. || iPage
10e36 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f Size>SQLITE_MAX_
10e37 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 65 PAGE_SIZE || iSe
10e38 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43 ctorSize>MAX_SEC
10e39 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c 7c TOR_SIZE. ||
10e3a 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 26 ((iPageSize-1)&
10e3b 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 20 iPageSize)!=0
10e3c 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a 65 || ((iSectorSize
10e3d 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 29 -1)&iSectorSize)
10e3e 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 !=0 . ){.
10e3f 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 68 /* If the eith
10e40 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 er the page-size
10e41 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 or sector-size
10e42 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 in the journal-h
10e43 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 20 eader is .
10e44 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 6e ** invalid, then
10e45 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 the process tha
10e46 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 72 t wrote the jour
10e47 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 20 nal-header must
10e48 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 have . ** c
10e49 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 68 rashed before th
10e4a 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 6e e header was syn
10e4b 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ced. In this cas
10e4c 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 0a e stop reading .
10e4d 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 ** the jou
10e4e 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 0a rnal file here..
10e4f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
10e50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
10e51 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a E;. }.. /*
10e52 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 65 Update the page
10e53 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 -size to match t
10e54 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 he value read fr
10e55 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 om the journal.
10e56 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 65 . ** Use a te
10e57 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 74 stcase() macro t
10e58 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
10e59 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
10e5a 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 50 within . ** P
10e5b 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
10e5c 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 20 ) is tested..
10e5d 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69 7a */. iPageSiz
10e5e 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67 65 e16 = (u16)iPage
10e5f 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 Size;. rc = s
10e60 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
10e61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 gesize(pPager, &
10e62 69 50 61 67 65 53 69 7a 65 31 36 29 3b 0a 20 20 iPageSize16);.
10e63 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d testcase( rc!=
10e64 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
10e65 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
10e66 49 54 45 5f 4f 4b 20 7c 7c 20 69 50 61 67 65 53 ITE_OK || iPageS
10e67 69 7a 65 31 36 3d 3d 28 75 31 36 29 69 50 61 67 ize16==(u16)iPag
10e68 65 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20 2f 2a eSize );.. /*
10e69 20 55 70 64 61 74 65 20 74 68 65 20 61 73 73 75 Update the assu
10e6a 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 med sector-size
10e6b 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c to match the val
10e6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20 20 20 ue used by .
10e6d 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 ** the process t
10e6e 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73 hat created this
10e6f 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 69 journal. If thi
10e70 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 s journal was.
10e71 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 ** created by
10e72 61 20 70 72 6f 63 65 73 73 20 6f 74 68 65 72 20 a process other
10e73 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 than this one, t
10e74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
10e75 0a 20 20 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 . ** is being
10e76 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
10e77 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 62 61 hin pager_playba
10e78 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c 20 ck(). The local
10e79 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20 value. ** of
10e7a 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 Pager.sectorSize
10e7b 20 69 73 20 72 65 73 74 6f 72 65 64 20 61 74 20 is restored at
10e7c 74 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74 20 the end of that
10e7d 72 6f 75 74 69 6e 65 2e 0a 20 20 20 20 2a 2f 0a routine.. */.
10e7e 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 pPager->sect
10e7f 6f 72 53 69 7a 65 20 3d 20 69 53 65 63 74 6f 72 orSize = iSector
10e80 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 Size;. }.. pPa
10e81 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
10e82 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 += JOURNAL_HDR_S
10e83 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 Z(pPager);. ret
10e84 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
10e85 2a 20 57 72 69 74 65 20 74 68 65 20 73 75 70 70 * Write the supp
10e86 6c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 lied master jour
10e87 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 nal name into th
10e88 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 e journal file f
10e89 6f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 61 67 or pager.** pPag
10e8a 65 72 20 61 74 20 74 68 65 20 63 75 72 72 65 6e er at the curren
10e8b 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 t location. The
10e8c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
10e8d 61 6d 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 ame must be the
10e8e 6c 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72 last.** thing wr
10e8f 69 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e itten to a journ
10e90 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 al file. If the
10e91 70 61 67 65 72 20 69 73 20 69 6e 20 66 75 6c 6c pager is in full
10e92 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a -sync mode, the.
10e93 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
10e94 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 64 descriptor is ad
10e95 76 61 6e 63 65 64 20 74 6f 20 74 68 65 20 6e 65 vanced to the ne
10e96 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 xt sector bounda
10e97 72 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 ry before.** any
10e98 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e thing is written
10e99 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 69 73 3a . The format is:
10e9a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 .**.** + 4 byt
10e9b 65 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e es: PAGER_MJ_PGN
10e9c 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 74 65 O..** + N byte
10e9d 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 s: Master journa
10e9e 6c 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 75 74 l filename in ut
10e9f 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 f-8..** + 4 by
10ea0 74 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68 20 6f tes: N (length o
10ea1 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c f master journal
10ea2 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 2c 20 name in bytes,
10ea3 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f no nul-terminato
10ea4 72 29 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 r)..** + 4 byt
10ea5 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e es: Master journ
10ea6 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75 6d al name checksum
10ea7 2e 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74 65 73 ..** + 8 bytes
10ea8 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b : aJournalMagic[
10ea9 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 ]..**.** The mas
10eaa 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 ter journal page
10eab 20 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 65 checksum is the
10eac 20 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 65 sum of the byte
10ead 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 0a s in the master.
10eae 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c ** journal name,
10eaf 20 77 68 65 72 65 20 65 61 63 68 20 62 79 74 65 where each byte
10eb0 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 is interpreted
10eb1 61 73 20 61 20 73 69 67 6e 65 64 20 38 2d 62 69 as a signed 8-bi
10eb2 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a t integer..**.**
10eb3 20 49 66 20 7a 4d 61 73 74 65 72 20 69 73 20 61 If zMaster is a
10eb4 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 6f NULL pointer (o
10eb5 63 63 75 72 73 20 66 6f 72 20 61 20 73 69 6e 67 ccurs for a sing
10eb6 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e le database tran
10eb7 73 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 saction), .** th
10eb8 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d is call is a no-
10eb9 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e op..*/.static in
10eba 74 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 t writeMasterJou
10ebb 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 rnal(Pager *pPag
10ebc 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a er, const char *
10ebd 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 zMaster){. int
10ebe 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
10ebf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10ec0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
10ec1 20 69 6e 74 20 6e 4d 61 73 74 65 72 3b 20 20 20 int nMaster;
10ec2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ec3 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 /* Length of s
10ec4 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f tring zMaster */
10ec5 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 . i64 iHdrOff;
10ec6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ec7 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 /* Offset of
10ec8 20 68 65 61 64 65 72 20 69 6e 20 6a 6f 75 72 6e header in journ
10ec9 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 al file */. i64
10eca 20 6a 72 6e 6c 53 69 7a 65 3b 20 20 20 20 20 20 jrnlSize;
10ecb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10ecc 20 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c Size of journal
10ecd 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 2a 2f file on disk */
10ece 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 . u32 cksum = 0
10ecf 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10ed0 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 /* Checksum
10ed1 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 of string zMaste
10ed2 72 20 2a 2f 0a 0a 20 20 69 66 28 20 21 7a 4d 61 r */.. if( !zMa
10ed3 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e ster || pPager->
10ed4 73 65 74 4d 61 73 74 65 72 0a 20 20 20 7c 7c 20 setMaster. ||
10ed5 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
10ed6 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
10ed7 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 ALMODE_MEMORY .
10ed8 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 || pPager->jou
10ed9 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
10eda 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 JOURNALMODE_OFF
10edb 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e . ){. return
10edc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
10edd 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 pPager->setMas
10ede 74 65 72 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 ter = 1;. asser
10edf 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
10ee0 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 ->jfd) );.. /*
10ee1 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 6c 65 Calculate the le
10ee2 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 61 6e ngth in bytes an
10ee3 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6f d the checksum o
10ee4 66 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 66 f zMaster */. f
10ee5 6f 72 28 6e 4d 61 73 74 65 72 3d 30 3b 20 7a 4d or(nMaster=0; zM
10ee6 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 20 aster[nMaster];
10ee7 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 20 20 20 nMaster++){.
10ee8 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 cksum += zMaster
10ee9 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 7d 0a 0a [nMaster];. }..
10eea 20 20 2f 2a 20 49 66 20 69 6e 20 66 75 6c 6c 2d /* If in full-
10eeb 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64 76 61 6e sync mode, advan
10eec 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 64 ce to the next d
10eed 69 73 6b 20 73 65 63 74 6f 72 20 62 65 66 6f 72 isk sector befor
10eee 65 20 77 72 69 74 69 6e 67 0a 20 20 2a 2a 20 74 e writing. ** t
10eef 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
10ef0 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20 69 73 20 l name. This is
10ef1 69 6e 20 63 61 73 65 20 74 68 65 20 70 72 65 76 in case the prev
10ef2 69 6f 75 73 20 70 61 67 65 20 77 72 69 74 74 65 ious page writte
10ef3 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6a 6f n to. ** the jo
10ef4 75 72 6e 61 6c 20 68 61 73 20 61 6c 72 65 61 64 urnal has alread
10ef5 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a 20 y been synced..
10ef6 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 */. if( pPager
10ef7 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 ->fullSync ){.
10ef8 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
10ef9 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 lOff = journalHd
10efa 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b rOffset(pPager);
10efb 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d . }. iHdrOff =
10efc 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
10efd 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 Off;.. /* Write
10efe 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
10eff 6e 61 6c 20 64 61 74 61 20 74 6f 20 74 68 65 20 nal data to the
10f00 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e end of the journ
10f01 61 6c 20 66 69 6c 65 2e 20 49 66 0a 20 20 2a 2a al file. If. **
10f02 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
10f03 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 , return the err
10f04 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 or code to the c
10f05 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 aller.. */. if
10f06 28 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 ( (0 != (rc = wr
10f07 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 ite32bits(pPager
10f08 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2c 20 ->jfd, iHdrOff,
10f09 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 PAGER_MJ_PGNO(pP
10f0a 61 67 65 72 29 29 29 29 0a 20 20 20 7c 7c 20 28 ager)))). || (
10f0b 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 0 != (rc = sqlit
10f0c 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
10f0d 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 ->jfd, zMaster,
10f0e 6e 4d 61 73 74 65 72 2c 20 69 48 64 72 4f 66 66 nMaster, iHdrOff
10f0f 2b 34 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 +4))). || (0 !
10f10 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 = (rc = write32b
10f11 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
10f12 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 iHdrOff+4+nMast
10f13 65 72 2c 20 6e 4d 61 73 74 65 72 29 29 29 0a 20 er, nMaster))).
10f14 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d || (0 != (rc =
10f15 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
10f16 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 ger->jfd, iHdrOf
10f17 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 2c 20 63 f+4+nMaster+4, c
10f18 6b 73 75 6d 29 29 29 0a 20 20 20 7c 7c 20 28 30 ksum))). || (0
10f19 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 != (rc = sqlite
10f1a 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
10f1b 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 >jfd, aJournalMa
10f1c 67 69 63 2c 20 38 2c 20 69 48 64 72 4f 66 66 2b gic, 8, iHdrOff+
10f1d 34 2b 6e 4d 61 73 74 65 72 2b 38 29 29 29 0a 20 4+nMaster+8))).
10f1e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
10f1f 63 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d c;. }. pPager-
10f20 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 >journalOff += (
10f21 6e 4d 61 73 74 65 72 2b 32 30 29 3b 0a 20 20 70 nMaster+20);. p
10f22 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
10f23 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e = !pPager->noSyn
10f24 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 c;.. /* If the
10f25 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 72 69 pager is in peri
10f26 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f stent-journal mo
10f27 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 79 de, then the phy
10f28 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72 sical . ** jour
10f29 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78 74 nal-file may ext
10f2a 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e 64 end past the end
10f2b 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a of the master-j
10f2c 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a ournal name. **
10f2d 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66 20 and 8 bytes of
10f2e 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 74 20 magic data just
10f2f 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 written to the f
10f30 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20 ile. This is .
10f31 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65 63 ** dangerous bec
10f32 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 74 6f ause the code to
10f33 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d rollback a hot-
10f34 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a journal file. *
10f35 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 * will not be ab
10f36 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d le to find the m
10f37 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 aster-journal na
10f38 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 me to determine
10f39 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 . ** whether or
10f3a 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c not the journal
10f3b 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20 is hot. . **.
10f3c 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69 6e ** Easiest thin
10f3d 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73 20 g to do in this
10f3e 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20 74 scenario is to t
10f3f 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 runcate the jour
10f40 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74 nal . ** file t
10f41 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 20 73 o the required s
10f42 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 ize.. */ . if(
10f43 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 SQLITE_OK==(rc
10f44 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
10f45 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ize(pPager->jfd,
10f46 20 26 6a 72 6e 6c 53 69 7a 65 29 29 0a 20 20 20 &jrnlSize)).
10f47 26 26 20 6a 72 6e 6c 53 69 7a 65 3e 70 50 61 67 && jrnlSize>pPag
10f48 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 er->journalOff.
10f49 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
10f4a 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
10f4b 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 Pager->jfd, pPag
10f4c 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b er->journalOff);
10f4d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
10f4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 ;.}../*.** Find
10f4f 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 68 61 a page in the ha
10f50 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 69 sh table given i
10f51 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 ts page number.
10f52 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e Return.** a poin
10f53 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 ter to the page
10f54 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 72 or NULL if the r
10f55 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
10f56 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72 65 61 64 79 not .** already
10f57 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 in memory..*/.s
10f58 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 61 67 tatic PgHdr *pag
10f59 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20 er_lookup(Pager
10f5a 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 *pPager, Pgno pg
10f5b 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b no){. PgHdr *p;
10f5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f5d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
10f5e 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f rn value */.. /
10f5f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 * It is not poss
10f60 69 62 6c 65 20 66 6f 72 20 61 20 63 61 6c 6c 20 ible for a call
10f61 74 6f 20 50 63 61 63 68 65 46 65 74 63 68 28 29 to PcacheFetch()
10f62 20 77 69 74 68 20 63 72 65 61 74 65 46 6c 61 67 with createFlag
10f63 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20 66 61 69 6c ==0 to. ** fail
10f64 2c 20 73 69 6e 63 65 20 6e 6f 20 61 74 74 65 6d , since no attem
10f65 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 64 pt to allocate d
10f66 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 77 69 ynamic memory wi
10f67 6c 6c 20 62 65 20 6d 61 64 65 2e 0a 20 20 2a 2f ll be made.. */
10f68 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 . (void)sqlite3
10f69 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
10f6a 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
10f6b 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 65 74 o, 0, &p);. ret
10f6c 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
10f6d 55 6e 6c 65 73 73 20 74 68 65 20 70 61 67 65 72 Unless the pager
10f6e 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 is in error-sta
10f6f 74 65 2c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 te, discard all
10f70 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2e in-memory pages.
10f71 20 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 If.** the pager
10f72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 is in error-sta
10f73 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 te, then this ca
10f74 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ll is a no-op..*
10f75 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57 68 79 20 63 *.** TODO: Why c
10f76 61 6e 20 77 65 20 6e 6f 74 20 72 65 73 65 74 20 an we not reset
10f77 74 68 65 20 70 61 67 65 72 20 77 68 69 6c 65 20 the pager while
10f78 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 3f 0a in error state?.
10f79 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10f7a 61 67 65 72 5f 72 65 73 65 74 28 50 61 67 65 72 ager_reset(Pager
10f7b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 *pPager){. if(
10f7c 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 50 61 67 SQLITE_OK==pPag
10f7d 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 er->errCode ){.
10f7e 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 sqlite3Backup
10f7f 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e Restart(pPager->
10f80 70 42 61 63 6b 75 70 29 3b 0a 20 20 20 20 73 71 pBackup);. sq
10f81 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 lite3PcacheClear
10f82 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
10f83 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
10f84 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 74 75 Free all structu
10f85 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 65 72 res in the Pager
10f86 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 .aSavepoint[] ar
10f87 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f 74 68 ray and set both
10f88 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 65 70 .** Pager.aSavep
10f89 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72 2e 6e oint and Pager.n
10f8a 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65 72 Savepoint to zer
10f8b 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 75 62 o. Close the sub
10f8c 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20 69 -journal.** if i
10f8d 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 t is open and th
10f8e 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 e pager is not i
10f8f 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
10f90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
10f91 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 releaseAllSavep
10f92 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70 50 61 oints(Pager *pPa
10f93 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 ger){. int ii;
10f94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10f95 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20 6c 6f Iterator for lo
10f96 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 50 61 oping through Pa
10f97 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 2a ger.aSavepoint *
10f98 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 /. for(ii=0; ii
10f99 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f <pPager->nSavepo
10f9a 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 int; ii++){.
10f9b 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
10f9c 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 61 troy(pPager->aSa
10f9d 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 vepoint[ii].pInS
10f9e 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 20 avepoint);. }.
10f9f 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 if( !pPager->ex
10fa0 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 clusiveMode ){.
10fa1 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
10fa2 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b e(pPager->sjfd);
10fa3 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 . }. sqlite3_f
10fa4 72 65 65 28 70 50 61 67 65 72 2d 3e 61 53 61 76 ree(pPager->aSav
10fa5 65 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61 67 65 epoint);. pPage
10fa6 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 r->aSavepoint =
10fa7 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 0;. pPager->nSa
10fa8 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 vepoint = 0;. p
10fa9 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d Pager->nSubRec =
10faa 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 0;.}../*.** Set
10fab 20 74 68 65 20 62 69 74 20 6e 75 6d 62 65 72 20 the bit number
10fac 70 67 6e 6f 20 69 6e 20 74 68 65 20 50 61 67 65 pgno in the Page
10fad 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 rSavepoint.pInSa
10fae 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 76 vepoint .** bitv
10faf 65 63 73 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 ecs of all open
10fb0 73 61 76 65 70 6f 69 6e 74 73 2e 20 52 65 74 75 savepoints. Retu
10fb1 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 rn SQLITE_OK if
10fb2 73 75 63 63 65 73 73 66 75 6c 0a 2a 2a 20 6f 72 successful.** or
10fb3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 SQLITE_NOMEM if
10fb4 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 a malloc failur
10fb5 65 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 e occurs..*/.sta
10fb6 74 69 63 20 69 6e 74 20 61 64 64 54 6f 53 61 76 tic int addToSav
10fb7 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 50 61 epointBitvecs(Pa
10fb8 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e ger *pPager, Pgn
10fb9 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 69 o pgno){. int i
10fba 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
10fbb 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
10fbc 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 nter */. int rc
10fbd 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
10fbe 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f /* Result co
10fbf 64 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 3d de */.. for(ii=
10fc0 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 0; ii<pPager->nS
10fc1 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b avepoint; ii++){
10fc2 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f . PagerSavepo
10fc3 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 int *p = &pPager
10fc4 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d ->aSavepoint[ii]
10fc5 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d ;. if( pgno<=
10fc6 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20 p->nOrig ){.
10fc7 20 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 rc |= sqlite3B
10fc8 69 74 76 65 63 53 65 74 28 70 2d 3e 70 49 6e 53 itvecSet(p->pInS
10fc9 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 3b avepoint, pgno);
10fca 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
10fcb 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc==SQLITE_NOME
10fcc 4d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 M );. asser
10fcd 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
10fce 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e || rc==SQLITE_N
10fcf 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 OMEM );. }.
10fd0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
10fd1 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 ../*.** Unlock t
10fd2 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
10fd3 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
10fd4 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 is a no-op if th
10fd5 65 20 70 61 67 65 72 0a 2a 2a 20 69 73 20 69 6e e pager.** is in
10fd6 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e exclusive mode.
10fd7 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
10fd8 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ger is currently
10fd9 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c in error state,
10fda 20 64 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e discard the con
10fdb 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 tents of .** the
10fdc 20 63 61 63 68 65 20 61 6e 64 20 72 65 73 65 74 cache and reset
10fdd 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 the Pager struc
10fde 74 75 72 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 ture internal st
10fdf 61 74 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 ate. If there is
10fe0 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 .** an open jour
10fe1 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 nal-file, then t
10fe2 68 65 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 he next time a s
10fe3 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 hared-lock is ob
10fe4 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 tained.** on the
10fe5 20 70 61 67 65 72 20 66 69 6c 65 20 28 62 79 20 pager file (by
10fe6 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 this or any othe
10fe7 72 20 70 72 6f 63 65 73 73 29 2c 20 69 74 20 77 r process), it w
10fe8 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 ill be.** treate
10fe9 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e d as a hot-journ
10fea 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 al and rolled ba
10feb 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ck..*/.static vo
10fec 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 id pager_unlock(
10fed 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
10fee 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 if( !pPager->e
10fef 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a xclusiveMode ){.
10ff0 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 int rc;
10ff1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ff2 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
10ff3 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 */.. /* Alway
10ff4 73 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 s close the jour
10ff5 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64 72 nal file when dr
10ff6 6f 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 62 opping the datab
10ff7 61 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2a ase lock.. **
10ff8 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 6f 74 Otherwise, anot
10ff9 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 her connection w
10ffa 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 ith journal_mode
10ffb 3d 64 65 6c 65 74 65 20 6d 69 67 68 74 0a 20 20 =delete might.
10ffc 20 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 ** delete the
10ffd 66 69 6c 65 20 6f 75 74 20 66 72 6f 6d 20 75 6e file out from un
10ffe 64 65 72 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 der us.. */.
10fff 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
11000 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a e(pPager->jfd);.
11001 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
11002 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d cDestroy(pPager-
11003 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 >pInJournal);.
11004 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 pPager->pInJou
11005 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 rnal = 0;. re
11006 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e leaseAllSavepoin
11007 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 ts(pPager);..
11008 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 /* If the file
11009 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f 6d is unlocked, som
1100a 65 62 6f 64 79 20 65 6c 73 65 20 6d 69 67 68 74 ebody else might
1100b 20 63 68 61 6e 67 65 20 69 74 2e 20 54 68 65 0a change it. The.
1100c 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 73 74 ** values st
1100d 6f 72 65 64 20 69 6e 20 50 61 67 65 72 2e 64 62 ored in Pager.db
1100e 53 69 7a 65 20 65 74 63 2e 20 6d 69 67 68 74 20 Size etc. might
1100f 62 65 63 6f 6d 65 20 69 6e 76 61 6c 69 64 20 69 become invalid i
11010 66 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 68 61 f. ** this ha
11011 70 70 65 6e 73 2e 20 54 4f 44 4f 3a 20 52 65 61 ppens. TODO: Rea
11012 6c 6c 79 2c 20 74 68 69 73 20 64 6f 65 73 6e 27 lly, this doesn'
11013 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 65 t need to be cle
11014 61 72 65 64 0a 20 20 20 20 2a 2a 20 75 6e 74 69 ared. ** unti
11015 6c 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 l the change-cou
11016 6e 74 65 72 20 63 68 65 63 6b 20 66 61 69 6c 73 nter check fails
11017 20 69 6e 20 70 61 67 65 72 53 68 61 72 65 64 4c in pagerSharedL
11018 6f 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 ock().. */.
11019 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
1101a 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20 Valid = 0;..
1101b 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 rc = osUnlock(pP
1101c 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 ager->fd, NO_LOC
1101d 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 K);. if( rc )
1101e 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
1101f 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 errCode = rc;.
11020 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 28 }. IOTRACE(
11021 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 ("UNLOCK %p\n",
11022 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a pPager)).. /*
11023 20 49 66 20 50 61 67 65 72 2e 65 72 72 43 6f 64 If Pager.errCod
11024 65 20 69 73 20 73 65 74 2c 20 74 68 65 20 63 6f e is set, the co
11025 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
11026 67 65 72 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 ger cache cannot
11027 20 62 65 0a 20 20 20 20 2a 2a 20 74 72 75 73 74 be. ** trust
11028 65 64 2e 20 4e 6f 77 20 74 68 61 74 20 74 68 65 ed. Now that the
11029 20 70 61 67 65 72 20 66 69 6c 65 20 69 73 20 75 pager file is u
1102a 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 20 63 6f 6e nlocked, the con
1102b 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 20 tents of the.
1102c 20 2a 2a 20 63 61 63 68 65 20 63 61 6e 20 62 65 ** cache can be
1102d 20 64 69 73 63 61 72 64 65 64 20 61 6e 64 20 74 discarded and t
1102e 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 61 he error code sa
1102f 66 65 6c 79 20 63 6c 65 61 72 65 64 2e 0a 20 20 fely cleared..
11030 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 */. if( pPa
11031 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a ger->errCode ){.
11032 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
11033 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
11034 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f pPager->errCo
11035 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a de = SQLITE_OK;.
11036 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 61 }. pa
11037 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
11038 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 );. }.. pP
11039 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e ager->changeCoun
1103a 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 tDone = 0;. p
1103b 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
1103c 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d AGER_UNLOCK;. }
1103d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
1103e 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 unction should b
1103f 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e e called when an
11040 20 49 4f 45 52 52 2c 20 43 4f 52 52 55 50 54 20 IOERR, CORRUPT
11041 6f 72 20 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a or FULL error.**
11042 20 6d 61 79 20 68 61 76 65 20 6f 63 63 75 72 72 may have occurr
11043 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 ed. The first ar
11044 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e gument is a poin
11045 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 72 ter to the pager
11046 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 .** structure,
11047 74 68 65 20 73 65 63 6f 6e 64 20 74 68 65 20 65 the second the e
11048 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 rror-code about
11049 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 to be returned b
1104a 79 20 61 20 70 61 67 65 72 20 0a 2a 2a 20 41 50 y a pager .** AP
1104b 49 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 I function. The
1104c 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 value returned i
1104d 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
1104e 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
1104f 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66 75 6e 63 .** to this func
11050 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 tion. .**.** If
11051 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
11052 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 49 4f ent is SQLITE_IO
11053 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 ERR, SQLITE_CORR
11054 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46 UPT, or SQLITE_F
11055 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 ULL.** the error
11056 20 62 65 63 6f 6d 65 73 20 70 65 72 73 69 73 74 becomes persist
11057 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68 65 20 70 ent. Until the p
11058 65 72 73 69 73 74 65 6e 20 65 72 72 6f 72 20 69 ersisten error i
11059 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75 s cleared,.** su
1105a 62 73 65 71 75 65 6e 74 20 41 50 49 20 63 61 6c bsequent API cal
1105b 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 67 65 72 ls on this Pager
1105c 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c will immediatel
1105d 79 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 6d y return the sam
1105e 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 e .** error code
1105f 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73 ..**.** A persis
11060 74 65 6e 74 20 65 72 72 6f 72 20 69 6e 64 69 63 tent error indic
11061 61 74 65 73 20 74 68 61 74 20 74 68 65 20 63 6f ates that the co
11062 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
11063 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 ger-cache .** ca
11064 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e nnot be trusted.
11065 20 54 68 69 73 20 73 74 61 74 65 20 63 61 6e 20 This state can
11066 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 63 6f be cleared by co
11067 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 61 72 64 mpletely discard
11068 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 ing .** the cont
11069 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 ents of the page
1106a 72 2d 63 61 63 68 65 2e 20 49 66 20 61 20 74 72 r-cache. If a tr
1106b 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 61 63 ansaction was ac
1106c 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 tive when.** the
1106d 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f persistent erro
1106e 72 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e r occurred, then
1106f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
11070 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a urnal may need.*
11071 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 64 * to be replayed
11072 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 to restore the
11073 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
11074 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 database file (a
11075 73 20 69 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 s if.** it were
11076 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a a hot-journal)..
11077 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
11078 67 65 72 5f 65 72 72 6f 72 28 50 61 67 65 72 20 ger_error(Pager
11079 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 72 63 29 *pPager, int rc)
1107a 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 72 63 {. int rc2 = rc
1107b 20 26 20 30 78 66 66 3b 0a 20 20 61 73 73 65 72 & 0xff;. asser
1107c 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 65 72 t(. pPager
1107d 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 ->errCode==SQLIT
1107e 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 E_FULL ||.
1107f 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11080 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 ==SQLITE_OK ||.
11081 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 65 (pPager->e
11082 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 3d 3d rrCode & 0xff)==
11083 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 29 SQLITE_IOERR. )
11084 3b 0a 20 20 69 66 28 0a 20 20 20 20 72 63 32 3d ;. if(. rc2=
11085 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a =SQLITE_FULL ||.
11086 20 20 20 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f rc2==SQLITE_
11087 49 4f 45 52 52 20 7c 7c 0a 20 20 20 20 72 63 32 IOERR ||. rc2
11088 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 ==SQLITE_CORRUPT
11089 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 . ){. pPager
1108a 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a ->errCode = rc;.
1108b 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
1108c 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c state==PAGER_UNL
1108d 4f 43 4b 20 0a 20 20 20 20 20 26 26 20 73 71 6c OCK . && sql
1108e 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 ite3PcacheRefCou
1108f 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
11090 68 65 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 he)==0 . ){.
11091 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 /* If the p
11092 61 67 65 72 20 69 73 20 61 6c 72 65 61 64 79 20 ager is already
11093 75 6e 6c 6f 63 6b 65 64 2c 20 63 61 6c 6c 20 70 unlocked, call p
11094 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 6e 6f ager_unlock() no
11095 77 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6c w to. ** cl
11096 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 ear the error st
11097 61 74 65 20 61 6e 64 20 65 6e 73 75 72 65 20 74 ate and ensure t
11098 68 61 74 20 74 68 65 20 70 61 67 65 72 2d 63 61 hat the pager-ca
11099 63 68 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a che is . **
1109a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 completely empt
1109b 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 y.. */.
1109c 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 pager_unlock(p
1109d 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 Pager);. }.
1109e 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1109f 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 ../*.** Execute
110a0 61 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 a rollback if a
110a1 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
110a2 63 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b ctive and unlock
110a3 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 the .** databas
110a4 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 e file. .**.** I
110a5 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 f the pager has
110a6 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 already entered
110a7 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c the error state,
110a8 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 do not attempt
110a9 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b .** the rollback
110aa 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 49 at this time. I
110ab 6e 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 6e nstead, pager_un
110ac 6c 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 lock() is called
110ad 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f . The.** call to
110ae 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 pager_unlock()
110af 77 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c 6c will discard all
110b0 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 in-memory pages
110b1 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 , unlock.** the
110b2 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
110b3 64 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f d clear the erro
110b4 72 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 73 r state. If this
110b5 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 means that.** t
110b6 68 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f here is a hot-jo
110b7 75 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 urnal left in th
110b8 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 e file-system, t
110b9 68 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 he next connecti
110ba 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 on.** to obtain
110bb 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e a shared lock on
110bc 20 74 68 65 20 70 61 67 65 72 20 28 77 68 69 63 the pager (whic
110bd 68 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f 6e h may be this on
110be 65 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 e) will.** roll
110bf 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 it back..**.** I
110c0 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 f the pager has
110c1 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 65 not already ente
110c2 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 red the error st
110c3 61 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f ate, but an IO o
110c4 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f r.** malloc erro
110c5 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 r occurs during
110c6 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e a rollback, then
110c7 20 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 6c this will itsel
110c8 66 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 f cause .** the
110c9 70 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 pager to enter t
110ca 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 he error state.
110cb 57 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 6c Which will be cl
110cc 65 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 eared by the.**
110cd 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e call to pager_un
110ce 6c 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 72 lock(), as descr
110cf 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 ibed above..*/.s
110d0 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 tatic void pager
110d1 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 UnlockAndRollbac
110d2 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 k(Pager *pPager)
110d3 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e {. if( pPager->
110d4 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f errCode==SQLITE_
110d5 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 OK && pPager->st
110d6 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 ate>=PAGER_RESER
110d7 56 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 VED ){. sqlit
110d8 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
110d9 6c 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 loc();. sqlit
110da 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
110db 70 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71 6c pPager);. sql
110dc 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
110dd 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 loc();. }. pag
110de 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 er_unlock(pPager
110df 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 );.}../*.** This
110e0 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 routine ends a
110e1 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 transaction. A t
110e2 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73 ransaction is us
110e3 75 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a ually ended by .
110e4 2a 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d ** either a COMM
110e5 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b IT or a ROLLBACK
110e6 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 operation. This
110e7 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20 routine may be
110e8 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 called .** after
110e9 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 rollback of a h
110ea 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 ot-journal, or i
110eb 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
110ec 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a s while opening.
110ed 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ** the journal f
110ee 69 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74 ile or writing t
110ef 68 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f he very first jo
110f0 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 urnal-header of
110f1 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72 a.** database tr
110f2 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a ansaction..** .*
110f3 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
110f4 73 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45 s in PAGER_SHARE
110f5 44 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 D or PAGER_UNLOC
110f6 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 K state when thi
110f7 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 s.** routine is
110f8 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 called, it is a
110f9 6e 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20 53 no-op (returns S
110fa 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a QLITE_OK)..**.**
110fb 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 Otherwise, any
110fc 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 active savepoint
110fd 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e 0a s are released..
110fe 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 **.** If the jou
110ff 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 rnal file is ope
11100 6e 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22 66 n, then it is "f
11101 69 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 inalized". Once
11102 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 a journal .** fi
11103 6c 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 le has been fina
11104 6c 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74 20 lized it is not
11105 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 possible to use
11106 69 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 it to roll back
11107 61 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f a .** transactio
11108 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 n. Nor will it b
11109 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 e considered to
1110a 62 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c be a hot-journal
1110b 20 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 by this.** or a
1110c 6e 79 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 ny other databas
1110d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 e connection. Ex
1110e 61 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 actly how a jour
1110f 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 nal is finalized
11110 0a 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 .** depends on w
11111 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
11112 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 e pager is runni
11113 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 ng in exclusive
11114 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 mode and.** the
11115 63 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d current journal-
11116 6d 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 mode (Pager.jour
11117 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 nalMode value),
11118 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
11119 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d * journalMode=
1111a 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a =MEMORY.** J
1111b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 ournal file desc
1111c 72 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 riptor is simply
1111d 20 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64 65 closed. This de
1111e 73 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 stroys an .**
1111f 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 in-memory jour
11120 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 nal..**.** jou
11121 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 rnalMode==TRUNCA
11122 54 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 TE.** Journa
11123 6c 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 l file is trunca
11124 74 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 ted to zero byte
11125 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a s in size..**.**
11126 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d journalMode==
11127 50 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 PERSIST.** T
11128 68 65 20 66 69 72 73 74 20 32 38 20 62 79 74 65 he first 28 byte
11129 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c s of the journal
1112a 20 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 file are zeroed
1112b 2e 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61 74 . This invalidat
1112c 65 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66 69 es.** the fi
1112d 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 rst journal head
1112e 65 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 er in the file,
1112f 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65 6e and hence the en
11130 74 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 tire journal.**
11131 20 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 file. An inv
11132 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c alid journal fil
11133 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c e cannot be roll
11134 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 ed back..**.**
11135 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 journalMode==DE
11136 4c 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 LETE.** The
11137 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
11138 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 closed and delet
11139 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ed using sqlite3
1113a 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a OsDelete()..**.*
1113b 2a 20 20 20 20 20 49 66 20 74 68 65 20 70 61 67 * If the pag
1113c 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e er is running in
1113d 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c exclusive mode,
1113e 20 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 this method of
1113f 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 finalizing.**
11140 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 the journal fi
11141 6c 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 le is never used
11142 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74 68 . Instead, if th
11143 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 e journalMode is
11144 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 61 .** DELETE a
11145 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20 nd the pager is
11146 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
11147 65 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64 65 e, the method de
11148 73 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a scribed under.**
11149 20 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 journalMode
1114a 3d 3d 50 45 52 53 49 53 54 20 69 73 20 75 73 65 ==PERSIST is use
1114b 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a d instead..**.**
1114c 20 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 6e After the journ
1114d 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c al is finalized,
1114e 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e if running in n
1114f 6f 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 on-exclusive mod
11150 65 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 e, the.** pager
11151 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 53 moves to PAGER_S
11152 48 41 52 45 44 20 73 74 61 74 65 20 28 61 6e 64 HARED state (and
11153 20 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65 20 downgrades the
11154 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 lock on the.** d
11155 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 63 63 atabase file acc
11156 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a ordingly)..**.**
11157 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
11158 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c running in excl
11159 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 usive mode and i
1115a 73 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 s in PAGER_SYNCE
1115b 44 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d D state,.** it m
1115c 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45 58 oves to PAGER_EX
1115d 43 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b CLUSIVE. No lock
1115e 73 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65 64 s are downgraded
1115f 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e when running in
11160 0a 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d 6f .** exclusive mo
11161 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 de..**.** SQLITE
11162 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
11163 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 if no error occu
11164 72 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 rs. If an error
11165 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a occurs during.**
11166 20 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20 6f any of the IO o
11167 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e perations to fin
11168 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 alize the journa
11169 6c 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b l file or unlock
1116a 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
1116b 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 then the IO err
1116c 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
1116d 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e ned to the user.
1116e 20 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 If the .** oper
1116f 61 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a ation to finaliz
11170 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
11171 6c 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 le fails, then t
11172 68 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a he code still.**
11173 20 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b tries to unlock
11174 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
11175 6c 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 le if not in exc
11176 6c 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 lusive mode. If
11177 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 the.** unlock op
11178 65 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 eration fails as
11179 20 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 well, then the
1117a 66 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64 65 first error code
1117b 20 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 related.** to t
1117c 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 65 he first error e
1117d 6e 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65 20 ncountered (the
1117e 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 journal finaliza
1117f 74 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 tion one) is.**
11180 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 returned..*/.sta
11181 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e tic int pager_en
11182 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 d_transaction(Pa
11183 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
11184 20 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 hasMaster){. i
11185 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
11186 4b 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 K; /* Error
11187 20 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e code from journ
11188 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 al finalization
11189 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 operation */. i
1118a 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f nt rc2 = SQLITE_
1118b 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 OK; /* Error
1118c 20 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 69 code from db fi
1118d 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 le unlock operat
1118e 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 ion */.. if( pP
1118f 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 ager->state<PAGE
11190 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 R_RESERVED ){.
11191 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
11192 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 OK;. }. releas
11193 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 eAllSavepoints(p
11194 50 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 Pager);.. asser
11195 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
11196 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 ->jfd) || pPager
11197 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
11198 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 );. if( isOpen(
11199 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a pPager->jfd) ){.
1119a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 54 68 . /* TODO: Th
1119b 65 72 65 27 73 20 61 20 70 72 6f 62 6c 65 6d 20 ere's a problem
1119c 68 65 72 65 20 69 66 20 61 20 6a 6f 75 72 6e 61 here if a journa
1119d 6c 2d 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 l-file was opene
1119e 64 20 69 6e 20 4d 45 4d 4f 52 59 0a 20 20 20 20 d in MEMORY.
1119f 2a 2a 20 6d 6f 64 65 20 61 6e 64 20 74 68 65 6e ** mode and then
111a0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 the journal-mod
111a1 65 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 e is changed to
111a2 54 52 55 4e 43 41 54 45 20 6f 72 20 50 45 52 53 TRUNCATE or PERS
111a3 49 53 54 0a 20 20 20 20 2a 2a 20 64 75 72 69 6e IST. ** durin
111a4 67 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f g the transactio
111a5 6e 2e 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f n. This code sho
111a6 75 6c 64 20 62 65 20 63 68 61 6e 67 65 64 20 74 uld be changed t
111a7 6f 20 61 73 73 75 6d 65 0a 20 20 20 20 2a 2a 20 o assume. **
111a8 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c that the journal
111a9 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 63 68 mode has not ch
111aa 61 6e 67 65 64 20 73 69 6e 63 65 20 74 68 65 20 anged since the
111ab 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 0a transaction was.
111ac 20 20 20 20 2a 2a 20 73 74 61 72 74 65 64 2e 20 ** started.
111ad 41 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 50 And the sqlite3P
111ae 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 agerJournalMode(
111af 29 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c ) function shoul
111b0 64 20 62 65 0a 20 20 20 20 2a 2a 20 63 68 61 6e d be. ** chan
111b1 67 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ged to make sure
111b2 20 74 68 61 74 20 74 68 69 73 20 69 73 20 74 68 that this is th
111b3 65 20 63 61 73 65 20 74 6f 6f 2e 0a 20 20 20 20 e case too..
111b4 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c */.. /* Final
111b5 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ize the journal
111b6 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 file. */. if(
111b7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
111b8 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
111b9 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 NALMODE_MEMORY )
111ba 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 4d 65 {. int isMe
111bb 6d 6f 72 79 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 moryJournal = sq
111bc 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 lite3IsMemJourna
111bd 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a l(pPager->jfd);.
111be 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
111bf 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 lose(pPager->jfd
111c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 );. if( !is
111c1 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 6c 20 29 7b MemoryJournal ){
111c2 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
111c3 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 50 lite3OsDelete(pP
111c4 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 ager->pVfs, pPag
111c5 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 er->zJournal, 0)
111c6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
111c7 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e lse if( pPager->
111c8 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
111c9 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 ER_JOURNALMODE_T
111ca 52 55 4e 43 41 54 45 20 29 7b 0a 20 20 20 20 20 RUNCATE ){.
111cb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
111cc 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
111cd 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 jfd, 0);. p
111ce 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
111cf 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 f = 0;. pPa
111d0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 ger->journalStar
111d1 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c ted = 0;. }el
111d2 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 se if( pPager->e
111d3 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 xclusiveMode .
111d4 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f || pPager->jo
111d5 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
111d6 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 _JOURNALMODE_PER
111d7 53 49 53 54 0a 20 20 20 20 29 7b 0a 20 20 20 20 SIST. ){.
111d8 20 20 72 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e rc = zeroJourn
111d9 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 68 61 alHdr(pPager, ha
111da 73 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 sMaster);.
111db 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
111dc 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 70 er, rc);. p
111dd 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
111de 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 f = 0;. pPa
111df 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 ger->journalStar
111e0 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c ted = 0;. }el
111e1 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
111e2 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
111e3 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
111e4 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 RNALMODE_DELETE
111e5 7c 7c 20 72 63 20 29 3b 0a 20 20 20 20 20 20 73 || rc );. s
111e6 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
111e7 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 ager->jfd);.
111e8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
111e9 5f 4f 4b 20 26 26 20 21 70 50 61 67 65 72 2d 3e _OK && !pPager->
111ea 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 tempFile ){.
111eb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
111ec 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d OsDelete(pPager-
111ed 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a >pVfs, pPager->z
111ee 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 Journal, 0);.
111ef 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 64 }. }..#ifd
111f0 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f ef SQLITE_CHECK_
111f1 50 41 47 45 53 0a 20 20 20 20 73 71 6c 69 74 65 PAGES. sqlite
111f2 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44 69 3PcacheIterateDi
111f3 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61 rty(pPager->pPCa
111f4 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74 5f 70 che, pager_set_p
111f5 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64 69 66 agehash);.#endif
111f6 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 .. sqlite3Pca
111f7 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 cheCleanAll(pPag
111f8 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
111f9 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
111fa 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 estroy(pPager->p
111fb 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 InJournal);.
111fc 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
111fd 61 6c 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 al = 0;. pPag
111fe 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 er->nRec = 0;.
111ff 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 }.. if( !pPager
11200 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 ->exclusiveMode
11201 29 7b 0a 20 20 20 20 72 63 32 20 3d 20 6f 73 55 ){. rc2 = osU
11202 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 nlock(pPager->fd
11203 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a , SHARED_LOCK);.
11204 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
11205 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 e = PAGER_SHARED
11206 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 ;. pPager->ch
11207 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 angeCountDone =
11208 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 0;. }else if( p
11209 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 Pager->state==PA
1120a 47 45 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20 20 GER_SYNCED ){.
1120b 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
1120c 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 = PAGER_EXCLUSIV
1120d 45 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d E;. }. pPager-
1120e 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a >setMaster = 0;.
1120f 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 pPager->needSy
11210 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 nc = 0;. pPager
11211 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 ->dbModified = 0
11212 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 ;.. /* TODO: Is
11213 20 74 68 69 73 20 6f 70 74 69 6d 61 6c 3f 20 57 this optimal? W
11214 68 79 20 69 73 20 74 68 65 20 64 62 20 73 69 7a hy is the db siz
11215 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 68 65 e invalidated he
11216 72 65 20 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 re . ** when th
11217 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
11218 69 73 20 6e 6f 74 20 75 6e 6c 6f 63 6b 65 64 3f is not unlocked?
11219 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 */. pPager->db
1121a 4f 72 69 67 53 69 7a 65 20 3d 20 30 3b 0a 20 20 OrigSize = 0;.
1121b 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 sqlite3PcacheTru
1121c 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 ncate(pPager->pP
1121d 43 61 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64 Cache, pPager->d
1121e 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 4d bSize);. if( !M
1121f 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61 67 EMDB ){. pPag
11220 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
11221 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 = 0;. }.. retu
11222 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rn (rc==SQLITE_O
11223 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a 2f 2a K?rc2:rc);.}../*
11224 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 61 44 .** Parameter aD
11225 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 ata must point t
11226 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 70 50 o a buffer of pP
11227 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 ager->pageSize b
11228 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 2e ytes.** of data.
11229 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 72 65 74 Compute and ret
1122a 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d 20 62 urn a checksum b
1122b 61 73 65 64 20 6f 6e 74 20 74 68 65 20 63 6f 6e ased ont the con
1122c 74 65 6e 74 73 20 6f 66 20 74 68 65 20 0a 2a 2a tents of the .**
1122d 20 70 61 67 65 20 6f 66 20 64 61 74 61 20 61 6e page of data an
1122e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 d the current va
1122f 6c 75 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 63 lue of pPager->c
11230 6b 73 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 ksumInit..**.**
11231 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 72 65 This is not a re
11232 61 6c 20 63 68 65 63 6b 73 75 6d 2e 20 49 74 20 al checksum. It
11233 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 is really just t
11234 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 0a 2a he sum of the .*
11235 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c * random initial
11236 20 76 61 6c 75 65 20 28 70 50 61 67 65 72 2d 3e value (pPager->
11237 63 6b 73 75 6d 49 6e 69 74 29 20 61 6e 64 20 65 cksumInit) and e
11238 76 65 72 79 20 32 30 30 74 68 20 62 79 74 65 0a very 200th byte.
11239 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 ** of the page d
1123a 61 74 61 2c 20 73 74 61 72 74 69 6e 67 20 77 69 ata, starting wi
1123b 74 68 20 62 79 74 65 20 6f 66 66 73 65 74 20 28 th byte offset (
1123c 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
1123d 25 32 30 30 29 2e 0a 2a 2a 20 45 61 63 68 20 62 %200)..** Each b
1123e 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 yte is interpret
1123f 65 64 20 61 73 20 61 6e 20 38 2d 62 69 74 20 75 ed as an 8-bit u
11240 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e nsigned integer.
11241 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 .**.** Changing
11242 74 68 65 20 66 6f 72 6d 75 6c 61 20 75 73 65 64 the formula used
11243 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 69 73 to compute this
11244 20 63 68 65 63 6b 73 75 6d 20 72 65 73 75 6c 74 checksum result
11245 73 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6d s in an.** incom
11246 70 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c 20 patible journal
11247 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a file format..**.
11248 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63 6f ** If journal co
11249 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20 rruption occurs
1124a 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66 due to a power f
1124b 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74 ailure, the most
1124c 20 6c 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 65 6e likely .** scen
1124d 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f 6e 65 ario is that one
1124e 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65 end or the othe
1124f 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 r of the record
11250 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e will be changed.
11251 20 0a 2a 2a 20 49 74 20 69 73 20 6d 75 63 68 20 .** It is much
11252 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74 less likely that
11253 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66 the two ends of
11254 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 the journal rec
11255 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63 ord will be.** c
11256 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d orrect and the m
11257 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74 iddle be corrupt
11258 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22 63 . Thus, this "c
11259 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c hecksum" scheme,
1125a 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20 .** though fast
1125b 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63 and simple, catc
1125c 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c hes the mostly l
1125d 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f ikely kind of co
1125e 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 rruption..*/.sta
1125f 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 63 6b tic u32 pager_ck
11260 73 75 6d 28 50 61 67 65 72 20 2a 70 50 61 67 65 sum(Pager *pPage
11261 72 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 r, const u8 *aDa
11262 74 61 29 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d ta){. u32 cksum
11263 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d = pPager->cksum
11264 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Init; /*
11265 20 43 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 Checksum value
11266 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 to return */. i
11267 6e 74 20 69 20 3d 20 70 50 61 67 65 72 2d 3e 70 nt i = pPager->p
11268 61 67 65 53 69 7a 65 2d 32 30 30 3b 20 20 20 20 ageSize-200;
11269 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
1126a 75 6e 74 65 72 20 2a 2f 0a 20 20 77 68 69 6c 65 unter */. while
1126b 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b 73 ( i>0 ){. cks
1126c 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b 0a um += aData[i];.
1126d 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20 20 i -= 200;.
1126e 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 75 6d }. return cksum
1126f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
11270 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 66 72 a single page fr
11271 6f 6d 20 65 69 74 68 65 72 20 74 68 65 20 6a 6f om either the jo
11272 75 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 20 69 urnal file (if i
11273 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 31 29 20 6f 72 sMainJrnl==1) or
11274 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 75 62 .** from the sub
11275 2d 6a 6f 75 72 6e 61 6c 20 28 69 66 20 69 73 4d -journal (if isM
11276 61 69 6e 4a 72 6e 6c 3d 3d 30 29 20 61 6e 64 20 ainJrnl==0) and
11277 70 6c 61 79 62 61 63 6b 20 74 68 61 74 20 70 61 playback that pa
11278 67 65 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 ge..** The page
11279 62 65 67 69 6e 73 20 61 74 20 6f 66 66 73 65 74 begins at offset
1127a 20 2a 70 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 *pOffset into t
1127b 68 65 20 66 69 6c 65 2e 20 54 68 65 20 2a 70 4f he file. The *pO
1127c 66 66 73 65 74 0a 2a 2a 20 76 61 6c 75 65 20 69 ffset.** value i
1127d 73 20 69 6e 63 72 65 61 73 65 64 20 74 6f 20 74 s increased to t
1127e 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
1127f 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 next page in the
11280 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 journal..**.**
11281 54 68 65 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 66 The isMainJrnl f
11282 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74 lag is true if t
11283 68 69 73 20 69 73 20 74 68 65 20 6d 61 69 6e 20 his is the main
11284 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
11285 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 66 6f and.** false fo
11286 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 r the statement
11287 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 journal. The ma
11288 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 in rollback jour
11289 6e 61 6c 20 75 73 65 73 0a 2a 2a 20 63 68 65 63 nal uses.** chec
1128a 6b 73 75 6d 73 20 2d 20 74 68 65 20 73 74 61 74 ksums - the stat
1128b 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 6f ement journal do
1128c 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 es not..**.** If
1128d 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
1128e 20 6f 66 20 74 68 65 20 70 61 67 65 20 72 65 63 of the page rec
1128f 6f 72 64 20 72 65 61 64 20 66 72 6f 6d 20 74 68 ord read from th
11290 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 e (sub-)journal
11291 66 69 6c 65 0a 2a 2a 20 69 73 20 67 72 65 61 74 file.** is great
11292 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 er than the curr
11293 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61 67 ent value of Pag
11294 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 65 6e 20 er.dbSize, then
11295 70 6c 61 79 62 61 63 6b 20 69 73 0a 2a 2a 20 73 playback is.** s
11296 6b 69 70 70 65 64 20 61 6e 64 20 53 51 4c 49 54 kipped and SQLIT
11297 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
11298 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f 6e 65 ..**.** If pDone
11299 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 is not NULL, th
1129a 65 6e 20 69 74 20 69 73 20 61 20 72 65 63 6f 72 en it is a recor
1129b 64 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 d of pages that
1129c 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20 have already.**
1129d 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b been played back
1129e 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 61 . If the page a
1129f 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73 20 61 t *pOffset has a
112a0 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c 61 79 lready been play
112a1 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66 20 74 ed back.** (if t
112a2 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
112a3 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 73 65 pDone bit is se
112a4 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65 t) then skip the
112a5 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 4d 61 playback..** Ma
112a6 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 6f 6e ke sure the pDon
112a7 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 e bit correspond
112a8 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f 66 66 ing to the *pOff
112a9 73 65 74 20 70 61 67 65 20 69 73 20 73 65 74 0a set page is set.
112aa 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 75 ** prior to retu
112ab 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 rning..**.** If
112ac 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 64 20 the page record
112ad 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 is successfully
112ae 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 read from the (s
112af 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ub-)journal file
112b0 0a 2a 2a 20 61 6e 64 20 70 6c 61 79 65 64 20 62 .** and played b
112b1 61 63 6b 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 ack, then SQLITE
112b2 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
112b3 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
112b4 6f 63 63 75 72 73 0a 2a 2a 20 77 68 69 6c 65 20 occurs.** while
112b5 72 65 61 64 69 6e 67 20 74 68 65 20 72 65 63 6f reading the reco
112b6 72 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 rd from the (sub
112b7 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f -)journal file o
112b8 72 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67 0a r while writing.
112b9 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ** to the databa
112ba 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 se file, then th
112bb 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 e IO error code
112bc 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
112bd 64 61 74 61 0a 2a 2a 20 69 73 20 73 75 63 63 65 data.** is succe
112be 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f ssfully read fro
112bf 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 m the (sub-)jour
112c0 6e 61 6c 20 66 69 6c 65 20 62 75 74 20 61 70 70 nal file but app
112c1 65 61 72 73 20 74 6f 20 62 65 0a 2a 2a 20 63 6f ears to be.** co
112c2 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f rrupted, SQLITE_
112c3 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 DONE is returned
112c4 2e 20 44 61 74 61 20 69 73 20 63 6f 6e 73 69 64 . Data is consid
112c5 65 72 65 64 20 63 6f 72 72 75 70 74 65 64 20 69 ered corrupted i
112c6 6e 0a 2a 2a 20 74 77 6f 20 63 69 72 63 75 6d 73 n.** two circums
112c7 74 61 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 tances:.** .**
112c8 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 * If the record
112c9 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 page-number is
112ca 69 6c 6c 65 67 61 6c 20 28 30 20 6f 72 20 50 41 illegal (0 or PA
112cb 47 45 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 6f 72 GER_MJ_PGNO), or
112cc 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 72 .** * If the r
112cd 65 63 6f 72 64 20 69 73 20 62 65 69 6e 67 20 72 ecord is being r
112ce 6f 6c 6c 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 olled back from
112cf 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
112d0 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61 6e 64 file.** and
112d1 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 66 69 the checksum fi
112d2 65 6c 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 eld does not mat
112d3 63 68 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f ch the record co
112d4 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65 69 ntent..**.** Nei
112d5 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 ther of these tw
112d6 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72 65 20 o scenarios are
112d7 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e 67 20 possible during
112d8 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c a savepoint roll
112d9 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 back..**.** If t
112da 68 69 73 20 69 73 20 61 20 73 61 76 65 70 6f 69 his is a savepoi
112db 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 nt rollback, the
112dc 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68 61 76 n memory may hav
112dd 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 61 e to be dynamica
112de 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 lly.** allocated
112df 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
112e0 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 n. If this is th
112e1 65 20 63 61 73 65 20 61 6e 64 20 61 6e 20 61 6c e case and an al
112e2 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a location fails,.
112e3 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 ** SQLITE_NOMEM
112e4 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
112e5 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
112e6 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 _playback_one_pa
112e7 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 ge(. Pager *pPa
112e8 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ger,
112e9 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 /* The pager
112ea 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20 62 61 being played ba
112eb 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 61 ck */. int isMa
112ec 69 6e 4a 72 6e 6c 2c 20 20 20 20 20 20 20 20 20 inJrnl,
112ed 20 20 20 20 20 20 2f 2a 20 31 20 2d 3e 20 6d 61 /* 1 -> ma
112ee 69 6e 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 2d 3e in journal. 0 ->
112ef 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f sub-journal. */
112f0 0a 20 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 2c . int isUnsync,
112f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112f2 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 61 64 /* True if read
112f3 69 6e 67 20 66 72 6f 6d 20 75 6e 73 79 6e 63 65 ing from unsynce
112f4 64 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a d main journal *
112f5 2f 0a 20 20 69 36 34 20 2a 70 4f 66 66 73 65 74 /. i64 *pOffset
112f6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
112f7 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 72 /* Offset of r
112f8 65 63 6f 72 64 20 74 6f 20 70 6c 61 79 62 61 63 ecord to playbac
112f9 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 61 76 k */. int isSav
112fa 65 70 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 epnt,
112fb 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
112fc 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c a savepoint rol
112fd 6c 62 61 63 6b 20 2a 2f 0a 20 20 42 69 74 76 65 lback */. Bitve
112fe 63 20 2a 70 44 6f 6e 65 20 20 20 20 20 20 20 20 c *pDone
112ff 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76 /* Bitv
11300 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 ec of pages alre
11301 61 64 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20 ady played back
11302 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
11303 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20 20 20 PgHdr *pPg;
11304 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11305 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67 20 70 /* An existing p
11306 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 age in the cache
11307 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b */. Pgno pgno;
11308 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11309 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 /* The page
1130a 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61 67 65 number of a page
1130b 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 in journal */.
1130c 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 u32 cksum;
1130d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1130e 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65 64 20 * Checksum used
1130f 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63 6b for sanity check
11310 69 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 ing */. u8 *aDa
11311 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
11312 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 /* Tempor
11313 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f 72 20 ary storage for
11314 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 73 71 the page */. sq
11315 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b lite3_file *jfd;
11316 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
11317 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
11318 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e or for the journ
11319 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 al file */.. as
1131a 73 65 72 74 28 20 28 69 73 4d 61 69 6e 4a 72 6e sert( (isMainJrn
1131b 6c 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 l&~1)==0 );
1131c 20 2f 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 69 /* isMainJrnl i
1131d 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 s 0 or 1 */. as
1131e 73 65 72 74 28 20 28 69 73 53 61 76 65 70 6e 74 sert( (isSavepnt
1131f 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 20 &~1)==0 );
11320 20 2f 2a 20 69 73 53 61 76 65 70 6e 74 20 69 73 /* isSavepnt is
11321 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 73 0 or 1 */. ass
11322 65 72 74 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 ert( isMainJrnl
11323 7c 7c 20 70 44 6f 6e 65 20 29 3b 20 20 20 20 20 || pDone );
11324 2f 2a 20 70 44 6f 6e 65 20 61 6c 77 61 79 73 20 /* pDone always
11325 75 73 65 64 20 6f 6e 20 73 75 62 2d 6a 6f 75 72 used on sub-jour
11326 6e 61 6c 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 nals */. assert
11327 28 20 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 70 ( isSavepnt || p
11328 44 6f 6e 65 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 Done==0 ); /*
11329 70 44 6f 6e 65 20 6e 65 76 65 72 20 75 73 65 64 pDone never used
1132a 20 6f 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f 69 6e on non-savepoin
1132b 74 20 2a 2f 0a 0a 20 20 61 44 61 74 61 20 3d 20 t */.. aData =
1132c 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54 6d (u8*)pPager->pTm
1132d 70 53 70 61 63 65 3b 0a 20 20 61 73 73 65 72 74 pSpace;. assert
1132e 28 20 61 44 61 74 61 20 29 3b 20 20 20 20 20 20 ( aData );
1132f 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 /* Temp stora
11330 67 65 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72 ge must have alr
11331 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 eady been alloca
11332 74 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 ted */.. /* Rea
11333 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 d the page numbe
11334 72 20 61 6e 64 20 70 61 67 65 20 64 61 74 61 20 r and page data
11335 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c from the journal
11336 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a or sub-journal.
11337 20 20 2a 2a 20 66 69 6c 65 2e 20 52 65 74 75 72 ** file. Retur
11338 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 n an error code
11339 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 66 to the caller if
1133a 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
1133b 75 72 73 2e 0a 20 20 2a 2f 0a 20 20 6a 66 64 20 urs.. */. jfd
1133c 3d 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 3f 20 70 = isMainJrnl ? p
1133d 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70 50 61 Pager->jfd : pPa
1133e 67 65 72 2d 3e 73 6a 66 64 3b 0a 20 20 72 63 20 ger->sjfd;. rc
1133f 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 64 = read32bits(jfd
11340 2c 20 2a 70 4f 66 66 73 65 74 2c 20 26 70 67 6e , *pOffset, &pgn
11341 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 o);. if( rc!=SQ
11342 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e LITE_OK ) return
11343 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 rc;. rc = sqli
11344 74 65 33 4f 73 52 65 61 64 28 6a 66 64 2c 20 61 te3OsRead(jfd, a
11345 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 Data, pPager->pa
11346 67 65 53 69 7a 65 2c 20 28 2a 70 4f 66 66 73 65 geSize, (*pOffse
11347 74 29 2b 34 29 3b 0a 20 20 69 66 28 20 72 63 21 t)+4);. if( rc!
11348 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
11349 75 72 6e 20 72 63 3b 0a 20 20 2a 70 4f 66 66 73 urn rc;. *pOffs
1134a 65 74 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 et += pPager->pa
1134b 67 65 53 69 7a 65 20 2b 20 34 20 2b 20 69 73 4d geSize + 4 + isM
1134c 61 69 6e 4a 72 6e 6c 2a 34 3b 0a 0a 20 20 2f 2a ainJrnl*4;.. /*
1134d 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 Sanity checking
1134e 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20 20 54 on the page. T
1134f 68 69 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 6f his is more impo
11350 72 74 61 6e 74 20 74 68 61 74 20 49 20 6f 72 69 rtant that I ori
11351 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f ginally. ** tho
11352 75 67 68 74 2e 20 20 49 66 20 61 20 70 6f 77 65 ught. If a powe
11353 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 r failure occurs
11354 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e while the journ
11355 61 6c 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 al is being writ
11356 74 65 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f 75 ten,. ** it cou
11357 6c 64 20 63 61 75 73 65 20 69 6e 76 61 6c 69 64 ld cause invalid
11358 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 data to be writ
11359 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ten into the jou
1135a 72 6e 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 74 rnal. We need t
1135b 6f 0a 20 20 2a 2a 20 64 65 74 65 63 74 20 74 68 o. ** detect th
1135c 69 73 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 is invalid data
1135d 28 77 69 74 68 20 68 69 67 68 20 70 72 6f 62 61 (with high proba
1135e 62 69 6c 69 74 79 29 20 61 6e 64 20 69 67 6e 6f bility) and igno
1135f 72 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 re it.. */. if
11360 28 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e ( pgno==0 || pgn
11361 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f o==PAGER_MJ_PGNO
11362 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 (pPager) ){.
11363 61 73 73 65 72 74 28 20 21 69 73 53 61 76 65 70 assert( !isSavep
11364 6e 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e nt );. return
11365 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
11366 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 67 }. if( pgno>(Pg
11367 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a no)pPager->dbSiz
11368 65 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 e || sqlite3Bitv
11369 65 63 54 65 73 74 28 70 44 6f 6e 65 2c 20 70 67 ecTest(pDone, pg
1136a 6e 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 no) ){. retur
1136b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1136c 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e . if( isMainJrn
1136d 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 65 l ){. rc = re
1136e 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 28 2a ad32bits(jfd, (*
1136f 70 4f 66 66 73 65 74 29 2d 34 2c 20 26 63 6b 73 pOffset)-4, &cks
11370 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 um);. if( rc
11371 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
11372 20 69 66 28 20 21 69 73 53 61 76 65 70 6e 74 20 if( !isSavepnt
11373 26 26 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 && pager_cksum(p
11374 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63 Pager, aData)!=c
11375 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 ksum ){. re
11376 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
11377 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
11378 66 28 20 70 44 6f 6e 65 20 26 26 20 28 72 63 20 f( pDone && (rc
11379 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 = sqlite3BitvecS
1137a 65 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29 et(pDone, pgno))
1137b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1137c 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1137d 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 }.. assert( pPa
1137e 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
1137f 52 5f 52 45 53 45 52 56 45 44 20 7c 7c 20 70 50 R_RESERVED || pP
11380 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
11381 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a ER_EXCLUSIVE );.
11382 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 . /* If the pag
11383 65 72 20 69 73 20 69 6e 20 52 45 53 45 52 56 45 er is in RESERVE
11384 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 D state, then th
11385 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 63 6f ere must be a co
11386 70 79 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20 py of this. **
11387 70 61 67 65 20 69 6e 20 74 68 65 20 70 61 67 65 page in the page
11388 72 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 r cache. In this
11389 20 63 61 73 65 20 6a 75 73 74 20 75 70 64 61 74 case just updat
1138a 65 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 e the pager cach
1138b 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74 68 65 20 e,. ** not the
1138c 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
1138d 68 65 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 he page is left
1138e 6d 61 72 6b 65 64 20 64 69 72 74 79 20 69 6e 20 marked dirty in
1138f 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a this case.. **.
11390 20 20 2a 2a 20 41 6e 20 65 78 63 65 70 74 69 6f ** An exceptio
11391 6e 20 74 6f 20 74 68 65 20 61 62 6f 76 65 20 72 n to the above r
11392 75 6c 65 3a 20 49 66 20 74 68 65 20 64 61 74 61 ule: If the data
11393 62 61 73 65 20 69 73 20 69 6e 20 6e 6f 2d 73 79 base is in no-sy
11394 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 nc mode. ** and
11395 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 a page is moved
11396 20 64 75 72 69 6e 67 20 61 6e 20 69 6e 63 72 65 during an incre
11397 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 74 68 mental vacuum th
11398 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 0a en the page may.
11399 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69 6e 20 74 ** not be in t
1139a 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 he pager cache.
1139b 4c 61 74 65 72 3a 20 69 66 20 61 20 6d 61 6c 6c Later: if a mall
1139c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72 72 6f 72 oc() or IO error
1139d 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 64 75 72 occurs. ** dur
1139e 69 6e 67 20 61 20 4d 6f 76 65 70 61 67 65 28 29 ing a Movepage()
1139f 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 call, then the
113a0 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 page may not be
113a1 69 6e 20 74 68 65 20 63 61 63 68 65 0a 20 20 2a in the cache. *
113a2 2a 20 65 69 74 68 65 72 2e 20 53 6f 20 74 68 65 * either. So the
113a3 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 condition descr
113a4 69 62 65 64 20 69 6e 20 74 68 65 20 61 62 6f 76 ibed in the abov
113a5 65 20 70 61 72 61 67 72 61 70 68 20 69 73 20 6e e paragraph is n
113a6 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72 74 28 29 ot. ** assert()
113a7 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 able.. **. **
113a8 49 66 20 69 6e 20 45 58 43 4c 55 53 49 56 45 20 If in EXCLUSIVE
113a9 73 74 61 74 65 2c 20 74 68 65 6e 20 77 65 20 75 state, then we u
113aa 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20 pdate the pager
113ab 63 61 63 68 65 20 69 66 20 69 74 20 65 78 69 73 cache if it exis
113ac 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 ts. ** and the
113ad 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68 65 20 70 main file. The p
113ae 61 67 65 20 69 73 20 74 68 65 6e 20 6d 61 72 6b age is then mark
113af 65 64 20 6e 6f 74 20 64 69 72 74 79 2e 0a 20 20 ed not dirty..
113b0 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 **. ** Ticket #
113b1 31 31 37 31 3a 20 20 54 68 65 20 73 74 61 74 65 1171: The state
113b2 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 ment journal mig
113b3 68 74 20 63 6f 6e 74 61 69 6e 20 70 61 67 65 20 ht contain page
113b4 63 6f 6e 74 65 6e 74 20 74 68 61 74 20 69 73 0a content that is.
113b5 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 66 ** different f
113b6 72 6f 6d 20 74 68 65 20 70 61 67 65 20 63 6f 6e rom the page con
113b7 74 65 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 tent at the star
113b8 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 t of the transac
113b9 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 69 73 20 tion.. ** This
113ba 6f 63 63 75 72 73 20 77 68 65 6e 20 61 20 70 61 occurs when a pa
113bb 67 65 20 69 73 20 63 68 61 6e 67 65 64 20 70 72 ge is changed pr
113bc 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 ior to the start
113bd 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 0a of a statement.
113be 20 20 2a 2a 20 74 68 65 6e 20 63 68 61 6e 67 65 ** then change
113bf 64 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 d again within t
113c0 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 57 he statement. W
113c1 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b hen rolling back
113c2 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 74 61 such a. ** sta
113c3 74 65 6d 65 6e 74 20 77 65 20 6d 75 73 74 20 6e tement we must n
113c4 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 ot write to the
113c5 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 original databas
113c6 65 20 75 6e 6c 65 73 73 20 77 65 20 6b 6e 6f 77 e unless we know
113c7 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72 74 61 69 . ** for certai
113c8 6e 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 20 n that original
113c9 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 page contents ar
113ca 65 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 e synced into th
113cb 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a e main rollback.
113cc 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f ** journal. O
113cd 74 68 65 72 77 69 73 65 2c 20 61 20 70 6f 77 65 therwise, a powe
113ce 72 20 6c 6f 73 73 20 6d 69 67 68 74 20 6c 65 61 r loss might lea
113cf 76 65 20 6d 6f 64 69 66 69 65 64 20 64 61 74 61 ve modified data
113d0 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 in the. ** dat
113d1 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f abase file witho
113d2 75 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 ut an entry in t
113d3 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 he rollback jour
113d4 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a 20 20 2a nal that can. *
113d5 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 * restore the da
113d6 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72 tabase to its or
113d7 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 20 54 77 iginal form. Tw
113d8 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 73 o conditions mus
113d9 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74 20 62 65 t be. ** met be
113da 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 fore writing to
113db 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
113dc 65 73 2e 20 28 31 29 20 74 68 65 20 64 61 74 61 es. (1) the data
113dd 62 61 73 65 20 6d 75 73 74 20 62 65 0a 20 20 2a base must be. *
113de 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32 29 20 77 * locked. (2) w
113df 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 e know that the
113e0 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f original page co
113e1 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c 79 20 73 ntent is fully s
113e2 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e 20 74 68 ynced. ** in th
113e3 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 65 e main journal e
113e4 69 74 68 65 72 20 62 65 63 61 75 73 65 20 74 68 ither because th
113e5 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e e page is not in
113e6 20 63 61 63 68 65 20 6f 72 20 65 6c 73 65 0a 20 cache or else.
113e7 20 2a 2a 20 74 68 65 20 70 61 67 65 20 69 73 20 ** the page is
113e8 6d 61 72 6b 65 64 20 61 73 20 6e 65 65 64 53 79 marked as needSy
113e9 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a nc==0.. **. **
113ea 20 32 30 30 38 2d 30 34 2d 31 34 3a 20 20 57 68 2008-04-14: Wh
113eb 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f en attempting to
113ec 20 76 61 63 75 75 6d 20 61 20 63 6f 72 72 75 70 vacuum a corrup
113ed 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c t database file,
113ee 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73 it. ** is poss
113ef 69 62 6c 65 20 74 6f 20 66 61 69 6c 20 61 20 73 ible to fail a s
113f0 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61 20 64 61 tatement on a da
113f1 74 61 62 61 73 65 20 74 68 61 74 20 64 6f 65 73 tabase that does
113f2 20 6e 6f 74 20 79 65 74 20 65 78 69 73 74 2e 0a not yet exist..
113f3 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61 74 74 65 ** Do not atte
113f4 6d 70 74 20 74 6f 20 77 72 69 74 65 20 69 66 20 mpt to write if
113f5 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
113f6 73 20 6e 65 76 65 72 20 62 65 65 6e 20 6f 70 65 s never been ope
113f7 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 20 ned.. */. pPg
113f8 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 = pager_lookup(p
113f9 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 Pager, pgno);.
113fa 61 73 73 65 72 74 28 20 70 50 67 20 7c 7c 20 21 assert( pPg || !
113fb 4d 45 4d 44 42 20 29 3b 0a 20 20 50 41 47 45 52 MEMDB );. PAGER
113fc 54 52 41 43 45 28 28 22 50 4c 41 59 42 41 43 4b TRACE(("PLAYBACK
113fd 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 %d page %d hash
113fe 28 25 30 38 78 29 20 25 73 5c 6e 22 2c 0a 20 20 (%08x) %s\n",.
113ff 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 PAG
11400 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 ERID(pPager), pg
11401 6e 6f 2c 20 70 61 67 65 72 5f 64 61 74 61 68 61 no, pager_dataha
11402 73 68 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 sh(pPager->pageS
11403 69 7a 65 2c 20 61 44 61 74 61 29 2c 0a 20 20 20 ize, aData),.
11404 20 20 20 20 20 20 20 20 20 20 20 20 28 69 73 4d (isM
11405 61 69 6e 4a 72 6e 6c 3f 22 6d 61 69 6e 2d 6a 6f ainJrnl?"main-jo
11406 75 72 6e 61 6c 22 3a 22 73 75 62 2d 6a 6f 75 72 urnal":"sub-jour
11407 6e 61 6c 22 29 0a 20 20 29 29 3b 0a 20 20 69 66 nal"). ));. if
11408 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 ( (pPager->state
11409 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 >=PAGER_EXCLUSIV
1140a 45 29 0a 20 20 20 26 26 20 28 70 50 67 3d 3d 30 E). && (pPg==0
1140b 20 7c 7c 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 61 || 0==(pPg->fla
1140c 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
1140d 4e 43 29 29 0a 20 20 20 26 26 20 69 73 4f 70 65 NC)). && isOpe
1140e 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 0a 20 20 n(pPager->fd).
1140f 20 26 26 20 21 69 73 55 6e 73 79 6e 63 0a 20 20 && !isUnsync.
11410 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 73 74 20 ){. i64 ofst
11411 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 = (pgno-1)*(i64)
11412 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11413 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
11414 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
11415 2d 3e 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 ->fd, aData, pPa
11416 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f ger->pageSize, o
11417 66 73 74 29 3b 0a 20 20 20 20 69 66 28 20 70 67 fst);. if( pg
11418 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c no>pPager->dbFil
11419 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 eSize ){. p
1141a 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a Pager->dbFileSiz
1141b 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a e = pgno;. }.
1141c 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 sqlite3Backu
1141d 70 55 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e pUpdate(pPager->
1141e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 61 pBackup, pgno, a
1141f 44 61 74 61 29 3b 0a 20 20 7d 65 6c 73 65 20 69 Data);. }else i
11420 66 28 20 21 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 f( !isMainJrnl &
11421 26 20 70 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 & pPg==0 ){.
11422 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 /* If this is a
11423 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 73 61 rollback of a sa
11424 76 65 70 6f 69 6e 74 20 61 6e 64 20 64 61 74 61 vepoint and data
11425 20 77 61 73 20 6e 6f 74 20 77 72 69 74 74 65 6e was not written
11426 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 to. ** the d
11427 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20 atabase and the
11428 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 2d 6d page is not in-m
11429 65 6d 6f 72 79 2c 20 74 68 65 72 65 20 69 73 20 emory, there is
1142a 61 20 70 6f 74 65 6e 74 69 61 6c 0a 20 20 20 20 a potential.
1142b 2a 2a 20 70 72 6f 62 6c 65 6d 2e 20 57 68 65 6e ** problem. When
1142c 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 65 78 the page is nex
1142d 74 20 66 65 74 63 68 65 64 20 62 79 20 74 68 65 t fetched by the
1142e 20 62 2d 74 72 65 65 20 6c 61 79 65 72 2c 20 69 b-tree layer, i
1142f 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 t . ** will b
11430 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 e read from the
11431 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 database file, w
11432 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 79 20 hich may or may
11433 6e 6f 74 20 62 65 20 0a 20 20 20 20 2a 2a 20 63 not be . ** c
11434 75 72 72 65 6e 74 2e 20 0a 20 20 20 20 2a 2a 0a urrent. . **.
11435 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 72 65 ** There are
11436 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66 a couple of dif
11437 66 65 72 65 6e 74 20 77 61 79 73 20 74 68 69 73 ferent ways this
11438 20 63 61 6e 20 68 61 70 70 65 6e 2e 20 41 6c 6c can happen. All
11439 20 61 72 65 20 71 75 69 74 65 0a 20 20 20 20 2a are quite. *
1143a 2a 20 6f 62 73 63 75 72 65 2e 20 57 68 65 6e 20 * obscure. When
1143b 72 75 6e 6e 69 6e 67 20 69 6e 20 73 79 6e 63 68 running in synch
1143c 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 74 68 69 ronous mode, thi
1143d 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 s can only happe
1143e 6e 20 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 n . ** if the
1143f 20 70 61 67 65 20 69 73 20 6f 6e 20 74 68 65 20 page is on the
11440 66 72 65 65 2d 6c 69 73 74 20 61 74 20 74 68 65 free-list at the
11441 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 start of the tr
11442 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 0a ansaction, then.
11443 20 20 20 20 2a 2a 20 70 6f 70 75 6c 61 74 65 64 ** populated
11444 2c 20 74 68 65 6e 20 6d 6f 76 65 64 20 75 73 69 , then moved usi
11445 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d ng sqlite3PagerM
11446 6f 76 65 70 61 67 65 28 29 2e 0a 20 20 20 20 2a ovepage().. *
11447 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 6f 6c *. ** The sol
11448 75 74 69 6f 6e 20 69 73 20 74 6f 20 61 64 64 20 ution is to add
11449 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 an in-memory pag
1144a 65 20 74 6f 20 74 68 65 20 63 61 63 68 65 20 63 e to the cache c
1144b 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 20 20 2a 2a ontaining. **
1144c 20 74 68 65 20 64 61 74 61 20 6a 75 73 74 20 72 the data just r
1144d 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73 75 62 ead from the sub
1144e 2d 6a 6f 75 72 6e 61 6c 2e 20 4d 61 72 6b 20 74 -journal. Mark t
1144f 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74 79 he page as dirty
11450 20 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 66 20 . ** and if
11451 74 68 65 20 70 61 67 65 72 20 72 65 71 75 69 72 the pager requir
11452 65 73 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e es a journal-syn
11453 63 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 65 c, then mark the
11454 20 70 61 67 65 20 61 73 20 0a 20 20 20 20 2a 2a page as . **
11455 20 72 65 71 75 69 72 69 6e 67 20 61 20 6a 6f 75 requiring a jou
11456 72 6e 61 6c 2d 73 79 6e 63 20 62 65 66 6f 72 65 rnal-sync before
11457 20 69 74 20 69 73 20 77 72 69 74 74 65 6e 2e 0a it is written..
11458 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
11459 74 28 20 69 73 53 61 76 65 70 6e 74 20 29 3b 0a t( isSavepnt );.
1145a 20 20 20 20 69 66 28 20 28 72 63 20 3d 20 73 71 if( (rc = sq
1145b 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 lite3PagerAcquir
1145c 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 e(pPager, pgno,
1145d 26 70 50 67 2c 20 31 29 29 21 3d 53 51 4c 49 54 &pPg, 1))!=SQLIT
1145e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
1145f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
11460 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d pPg->flags &=
11461 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 ~PGHDR_NEED_REA
11462 44 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 D;. sqlite3Pc
11463 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 acheMakeDirty(pP
11464 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 g);. }. if( pP
11465 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 70 g ){. /* No p
11466 61 67 65 20 73 68 6f 75 6c 64 20 65 76 65 72 20 age should ever
11467 62 65 20 65 78 70 6c 69 63 69 74 6c 79 20 72 6f be explicitly ro
11468 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 74 20 69 lled back that i
11469 73 20 69 6e 20 75 73 65 2c 20 65 78 63 65 70 74 s in use, except
1146a 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 . ** for page
1146b 20 31 20 77 68 69 63 68 20 69 73 20 68 65 6c 64 1 which is held
1146c 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 64 65 72 in use in order
1146d 20 74 6f 20 6b 65 65 70 20 74 68 65 20 6c 6f 63 to keep the loc
1146e 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 k on the. **
1146f 64 61 74 61 62 61 73 65 20 61 63 74 69 76 65 2e database active.
11470 20 48 6f 77 65 76 65 72 20 73 75 63 68 20 61 20 However such a
11471 70 61 67 65 20 6d 61 79 20 62 65 20 72 6f 6c 6c page may be roll
11472 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 ed back as a res
11473 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 20 61 6e ult. ** of an
11474 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 6f 72 20 internal error
11475 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 61 6e 20 resulting in an
11476 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c 20 74 automatic call t
11477 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 o. ** sqlite3
11478 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 2e PagerRollback().
11479 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 6f 69 64 . */. void
1147a 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 70 44 61 *pData;. pDa
1147b 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b ta = pPg->pData;
1147c 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 44 61 74 . memcpy(pDat
1147d 61 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 a, aData, pPager
1147e 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
1147f 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78 52 65 if( pPager->xRe
11480 69 6e 69 74 65 72 20 29 7b 0a 20 20 20 20 20 20 initer ){.
11481 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 pPager->xReinite
11482 72 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 r(pPg);. }.
11483 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c if( isMainJrnl
11484 20 26 26 20 28 21 69 73 53 61 76 65 70 6e 74 20 && (!isSavepnt
11485 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70 50 61 || *pOffset<=pPa
11486 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 ger->journalHdr)
11487 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
11488 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
11489 74 68 69 73 20 70 61 67 65 20 77 65 72 65 20 6a this page were j
1148a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66 72 6f ust restored fro
1148b 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 20 20 20 m the main .
1148c 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ** journal fil
1148d 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f 6e 74 e, then its cont
1148e 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73 20 74 ent must be as t
1148f 68 65 79 20 77 65 72 65 20 77 68 65 6e 20 74 68 hey were when th
11490 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72 61 6e e . ** tran
11491 73 61 63 74 69 6f 6e 20 77 61 73 20 66 69 72 73 saction was firs
11492 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 t opened. In thi
11493 73 20 63 61 73 65 20 77 65 20 63 61 6e 20 6d 61 s case we can ma
11494 72 6b 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 rk the page.
11495 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e 2c 20 73 ** as clean, s
11496 69 6e 63 65 20 74 68 65 72 65 20 77 69 6c 6c 20 ince there will
11497 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 72 be no need to wr
11498 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 74 68 ite it out to th
11499 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 e.. **.
1149a 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e ** There is on
1149b 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 e exception to t
1149c 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74 68 65 his rule. If the
1149d 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 72 page is being r
1149e 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 olled. ** b
1149f 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66 20 61 ack as part of a
114a0 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72 20 73 savepoint (or s
114a1 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c 62 61 tatement) rollba
114a2 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20 20 20 ck from an .
114a3 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20 70 6f ** unsynced po
114a4 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 61 69 rtion of the mai
114a5 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 n journal file,
114a6 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 73 then it is not s
114a7 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 afe. ** to
114a8 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 mark the page as
114a9 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69 73 20 clean. This is
114aa 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e 67 20 because marking
114ab 74 68 65 20 70 61 67 65 20 61 73 0a 20 20 20 20 the page as.
114ac 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c 6c 20 ** clean will
114ad 63 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 5f clear the PGHDR_
114ae 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 2e 20 NEED_SYNC flag.
114af 53 69 6e 63 65 20 74 68 65 20 70 61 67 65 20 69 Since the page i
114b0 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 s. ** alrea
114b1 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 dy in the journa
114b2 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64 65 64 l file (recorded
114b3 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 in Pager.pInJou
114b4 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20 20 20 rnal) and.
114b5 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 ** the PGHDR_NEE
114b6 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 63 D_SYNC flag is c
114b7 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65 20 70 leared, if the p
114b8 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 age is written t
114b9 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e o. ** again
114ba 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 within this tra
114bb 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77 69 6c nsaction, it wil
114bc 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 l be marked as d
114bd 69 72 74 79 20 62 75 74 0a 20 20 20 20 20 20 2a irty but. *
114be 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 * the PGHDR_NEED
114bf 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c 6c 20 _SYNC flag will
114c0 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74 20 63 not be set. It c
114c1 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65 6e 74 ould then potent
114c2 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 62 ially. ** b
114c3 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 69 6e e written out in
114c4 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
114c5 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74 73 20 file before its
114c6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 20 journal file.
114c7 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20 69 73 ** segment is
114c8 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20 63 72 synced. If a cr
114c9 61 73 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e ash occurs durin
114ca 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 g or following t
114cb 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20 64 61 his,. ** da
114cc 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f tabase corruptio
114cd 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20 20 20 n may ensue..
114ce 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 */. sqli
114cf 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
114d0 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 23 an(pPg);. }.#
114d1 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 ifdef SQLITE_CHE
114d2 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 CK_PAGES. pPg
114d3 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 ->pageHash = pag
114d4 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 er_pagehash(pPg)
114d5 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 ;.#endif. /*
114d6 49 66 20 74 68 69 73 20 77 61 73 20 70 61 67 65 If this was page
114d7 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65 1, then restore
114d8 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 the value of Pa
114d9 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 2e 0a ger.dbFileVers..
114da 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 62 ** Do this b
114db 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f 64 69 efore any decodi
114dc 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 ng. */. if( p
114dd 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 gno==1 ){.
114de 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e memcpy(&pPager->
114df 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75 dbFileVers, &((u
114e0 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c 73 69 8*)pData)[24],si
114e1 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 zeof(pPager->dbF
114e2 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 7d ileVers));. }
114e3 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 65 20 .. /* Decode
114e4 74 68 65 20 70 61 67 65 20 6a 75 73 74 20 72 65 the page just re
114e5 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a ad from disk */.
114e6 20 20 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 CODEC1(pPage
114e7 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 r, pData, pPg->p
114e8 67 6e 6f 2c 20 33 29 3b 0a 20 20 20 20 73 71 6c gno, 3);. sql
114e9 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
114ea 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 e(pPg);. }. re
114eb 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 turn rc;.}..#if
114ec 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
114ed 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
114ee 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 TE_COVERAGE_TEST
114ef 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 )./*.** This rou
114f0 74 69 6e 65 20 6c 6f 6f 6b 73 20 61 68 65 61 64 tine looks ahead
114f1 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a into the main j
114f2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 ournal file and
114f3 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 determines.** wh
114f4 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
114f5 20 6e 65 78 74 20 72 65 63 6f 72 64 20 28 74 68 next record (th
114f6 65 20 72 65 63 6f 72 64 20 74 68 61 74 20 62 65 e record that be
114f7 67 69 6e 73 20 61 74 20 66 69 6c 65 0a 2a 2a 20 gins at file.**
114f8 6f 66 66 73 65 74 20 70 50 61 67 65 72 2d 3e 6a offset pPager->j
114f9 6f 75 72 6e 61 6c 4f 66 66 29 20 69 73 20 61 20 ournalOff) is a
114fa 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 70 61 67 65 well-formed page
114fb 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 69 record consisti
114fc 6e 67 0a 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 ng.** of a valid
114fd 20 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 70 50 page number, pP
114fe 61 67 65 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 age->pageSize by
114ff 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 2c 20 tes of content,
11500 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 61 followed.** by a
11501 20 76 61 6c 69 64 20 63 68 65 63 6b 73 75 6d 2e valid checksum.
11502 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 .**.** The pager
11503 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 never needs to
11504 6b 6e 6f 77 20 74 68 69 73 20 69 6e 20 6f 72 64 know this in ord
11505 65 72 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62 er to do its job
11506 2e 20 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 . This.** rout
11507 69 6e 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ine is only used
11508 20 66 72 6f 6d 20 77 69 74 68 20 61 73 73 65 72 from with asser
11509 74 28 29 20 61 6e 64 20 74 65 73 74 63 61 73 65 t() and testcase
1150a 28 29 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 () macros..*/.st
1150b 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 4e 65 atic int pagerNe
1150c 78 74 4a 6f 75 72 6e 61 6c 50 61 67 65 49 73 56 xtJournalPageIsV
1150d 61 6c 69 64 28 50 61 67 65 72 20 2a 70 50 61 67 alid(Pager *pPag
1150e 65 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f er){. Pgno pgno
1150f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
11510 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f he page number o
11511 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 f the page */.
11512 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 20 u32 cksum;
11513 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 /* The page
11514 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 69 checksum */. i
11515 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
11516 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
11517 64 65 20 66 72 6f 6d 20 72 65 61 64 20 6f 70 65 de from read ope
11518 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c rations */. sql
11519 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 20 20 ite3_file *fd;
1151a 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
1151b 73 63 72 69 70 74 6f 72 20 66 72 6f 6d 20 77 68 scriptor from wh
1151c 69 63 68 20 77 65 20 61 72 65 20 72 65 61 64 69 ich we are readi
1151d 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 ng */. u8 *aDat
1151e 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 a; /*
1151f 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 Content of the p
11520 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 age */.. /* Rea
11521 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 d the page numbe
11522 72 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 66 64 r header */. fd
11523 20 3d 20 70 50 61 67 65 72 2d 3e 6a 66 64 3b 0a = pPager->jfd;.
11524 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 rc = read32bit
11525 73 28 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f s(fd, pPager->jo
11526 75 72 6e 61 6c 4f 66 66 2c 20 26 70 67 6e 6f 29 urnalOff, &pgno)
11527 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
11528 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e 20 TE_OK ){ return
11529 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 0; }
1152a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1152b 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
1152c 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 /. if( pgno==0
1152d 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d || pgno==PAGER_M
1152e 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 J_PGNO(pPager) )
1152f 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 { return 0; }
11530 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
11531 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 67 /. if( pgno>(Pg
11532 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a no)pPager->dbSiz
11533 65 20 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d e ){ return 0; }
11534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11535 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
11536 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 /.. /* Read the
11537 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 72 checksum */. r
11538 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 66 c = read32bits(f
11539 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e d, pPager->journ
1153a 61 6c 4f 66 66 2b 70 50 61 67 65 72 2d 3e 70 61 alOff+pPager->pa
1153b 67 65 53 69 7a 65 2b 34 2c 20 26 63 6b 73 75 6d geSize+4, &cksum
1153c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
1153d 49 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e ITE_OK ){ return
1153e 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 0; }
1153f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11540 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 /*NO_TEST
11541 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 */.. /* Read th
11542 65 20 64 61 74 61 20 61 6e 64 20 76 65 72 69 66 e data and verif
11543 79 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 2a y the checksum *
11544 2f 0a 20 20 61 44 61 74 61 20 3d 20 28 75 38 2a /. aData = (u8*
11545 29 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 )pPager->pTmpSpa
11546 63 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 ce;. rc = sqlit
11547 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 44 61 e3OsRead(fd, aDa
11548 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ta, pPager->page
11549 53 69 7a 65 2c 20 70 50 61 67 65 72 2d 3e 6a 6f Size, pPager->jo
1154a 75 72 6e 61 6c 4f 66 66 2b 34 29 3b 0a 20 20 69 urnalOff+4);. i
1154b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1154c 20 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 20 ){ return 0; }
1154d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1154e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1154f 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 69 /*NO_TEST*/. i
11550 66 28 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 f( pager_cksum(p
11551 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63 Pager, aData)!=c
11552 6b 73 75 6d 20 29 7b 20 72 65 74 75 72 6e 20 30 ksum ){ return 0
11553 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 ; }
11554 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 20 20 /*NO_TEST*/..
11555 2f 2a 20 52 65 61 63 68 20 74 68 69 73 20 70 6f /* Reach this po
11556 69 6e 74 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 int only if the
11557 70 61 67 65 20 69 73 20 76 61 6c 69 64 20 2a 2f page is valid */
11558 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 . return 1;.}.#
11559 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 endif /* !define
1155a 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 d(NDEBUG) || def
1155b 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 ined(SQLITE_COVE
1155c 52 41 47 45 5f 54 45 53 54 29 20 2a 2f 0a 0a 2f RAGE_TEST) */../
1155d 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a *.** Parameter z
1155e 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e 61 Master is the na
1155f 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a me of a master j
11560 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 ournal file. A s
11561 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a ingle journal.**
11562 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 72 file that refer
11563 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65 red to the maste
11564 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 r journal file h
11565 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c as just been rol
11566 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 led back..** Thi
11567 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
11568 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 if it is possib
11569 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 le to delete the
1156a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1156b 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 file,.** and doe
1156c 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a s so if it is..*
1156d 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d *.** Argument zM
1156e 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 aster may point
1156f 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 to Pager.pTmpSpa
11570 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66 ce. So that buff
11571 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 er is not .** av
11572 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 ailable for use
11573 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 within this func
11574 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e tion..**.** When
11575 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
11576 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 l file is create
11577 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 74 d, it is populat
11578 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 ed with the name
11579 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 s .** of all of
1157a 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 its child journa
1157b 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 6e ls, one after an
1157c 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 64 other, formatted
1157d 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e as utf-8 .** en
1157e 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 20 coded text. The
1157f 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 6c end of each chil
11580 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 d journal file i
11581 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 20 s marked with a
11582 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 .** nul-terminat
11583 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e 20 or byte (0x00).
11584 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 20 i.e. the entire
11585 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61 contents of a ma
11586 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 ster journal.**
11587 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e 73 file for a trans
11588 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67 action involving
11589 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 6d two databases m
1158a 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20 ight be:.**.**
1158b 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64 "/home/bill/a.d
1158c 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f b-journal\x00/ho
1158d 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 me/bill/b.db-jou
1158e 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20 rnal\x00".**.**
1158f 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c A master journal
11590 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62 file may only b
11591 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 61 e deleted once a
11592 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 ll of its child
11593 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76 .** journals hav
11594 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 e been rolled ba
11595 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ck..**.** This f
11596 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 68 unction reads th
11597 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
11598 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c e master-journal
11599 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d file into .** m
1159a 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20 emory and loops
1159b 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 20 through each of
1159c 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 the child journa
1159d 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20 l names. For.**
1159e 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e each child journ
1159f 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 66 al, it checks if
115a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74 :.**.** * if t
115a1 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c he child journal
115a2 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 20 exists, and if
115a3 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 so.** * if the
115a4 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 child journal c
115a5 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 65 ontains a refere
115a6 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f nce to master jo
115a7 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69 urnal .** fi
115a8 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a le zMaster.**.**
115a9 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 72 If a child jour
115aa 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 nal can be found
115ab 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 6f that matches bo
115ac 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 72 th of the criter
115ad 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69 ia.** above, thi
115ae 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
115af 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 ns without doing
115b0 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 72 anything. Other
115b1 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73 wise, if.** no s
115b2 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 uch child journa
115b3 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 l can be found,
115b4 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20 file zMaster is
115b5 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 deleted from.**
115b6 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
115b7 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 using sqlite3OsD
115b8 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 elete()..**.** I
115b9 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 69 f an IO error wi
115ba 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 thin this functi
115bb 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 on, an error cod
115bc 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 e is returned. T
115bd 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 his.** function
115be 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79 allocates memory
115bf 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 by calling sqli
115c0 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 te3Malloc(). If
115c1 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a an allocation.**
115c2 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e fails, SQLITE_N
115c3 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
115c4 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 . Otherwise, if
115c5 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20 no IO or malloc
115c6 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72 errors .** occur
115c7 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
115c8 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
115c9 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 69 ODO: This functi
115ca 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 on allocates a s
115cb 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d ingle block of m
115cc 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a emory to load.**
115cd 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 the entire cont
115ce 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74 ents of the mast
115cf 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
115d0 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a This could be.*
115d1 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69 * a couple of ki
115d2 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d 20 lobytes or so -
115d3 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67 potentially larg
115d4 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65 er than the page
115d5 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 .** size..*/.st
115d6 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64 atic int pager_d
115d7 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a elmaster(Pager *
115d8 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 pPager, const ch
115d9 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
115da 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
115db 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 s = pPager->pVfs
115dc 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 ;. int rc;
115dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
115de 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
115df 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
115e0 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d pMaster; /* M
115e1 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a alloc'd master-j
115e2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 ournal file desc
115e3 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 riptor */. sqli
115e4 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e te3_file *pJourn
115e5 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 al; /* Malloc'
115e6 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20 d child-journal
115e7 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
115e8 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 */. char *zMast
115e9 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f erJournal = 0; /
115ea 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 * Contents of ma
115eb 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
115ec 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 e */. i64 nMast
115ed 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 erJournal;
115ee 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 /* Size of mast
115ef 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
115f0 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 */.. /* Allocat
115f1 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 68 e space for both
115f2 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 6e the pJournal an
115f3 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 64 d pMaster file d
115f4 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a 2a escriptors.. **
115f5 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
115f6 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 open the master
115f7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
115f8 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 reading.. */.
115f9 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69 pMaster = (sqli
115fa 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 te3_file *)sqlit
115fb 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66 e3MallocZero(pVf
115fc 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29 s->szOsFile * 2)
115fd 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28 ;. pJournal = (
115fe 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28 sqlite3_file *)(
115ff 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20 ((u8 *)pMaster)
11600 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 + pVfs->szOsFile
11601 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65 );. if( !pMaste
11602 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 r ){. rc = SQ
11603 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 LITE_NOMEM;. }e
11604 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 lse{. const i
11605 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 nt flags = (SQLI
11606 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
11607 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 |SQLITE_OPEN_MAS
11608 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 TER_JOURNAL);.
11609 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1160a 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74 Open(pVfs, zMast
1160b 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61 er, pMaster, fla
1160c 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 gs, 0);. }. if
1160d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1160e 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 ) goto delmaster
1160f 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71 _out;.. rc = sq
11610 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
11611 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 pMaster, &nMaste
11612 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 rJournal);. if(
11613 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11614 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
11615 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73 out;.. if( nMas
11616 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a terJournal>0 ){.
11617 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e char *zJourn
11618 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d al;. char *zM
11619 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 20 asterPtr = 0;.
1161a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72 int nMasterPtr
1161b 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e = pVfs->mxPathn
1161c 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c ame+1;.. /* L
1161d 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d oad the entire m
1161e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1161f 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 le into space ob
11620 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 20 tained from.
11621 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ** sqlite3_mallo
11622 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 20 c() and pointed
11623 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f 75 to by zMasterJou
11624 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 rnal. . */.
11625 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c zMasterJournal
11626 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 = (char *)sqlit
11627 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d e3Malloc((int)nM
11628 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e asterJournal + n
11629 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20 MasterPtr);.
1162a 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 if( !zMasterJour
1162b 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 nal ){. rc
1162c 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1162d 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
1162e 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a ster_out;. }.
1162f 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d zMasterPtr =
11630 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c &zMasterJournal
11631 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5d [nMasterJournal]
11632 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
11633 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 e3OsRead(pMaster
11634 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c , zMasterJournal
11635 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f , (int)nMasterJo
11636 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 urnal, 0);. i
11637 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
11638 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 ) goto delmaste
11639 72 5f 6f 75 74 3b 0a 0a 20 20 20 20 7a 4a 6f 75 r_out;.. zJou
1163a 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a 6f rnal = zMasterJo
1163b 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c 65 urnal;. while
1163c 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73 ( (zJournal-zMas
1163d 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73 terJournal)<nMas
1163e 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 terJournal ){.
1163f 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a int exists;.
11640 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
11641 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c e3OsAccess(pVfs,
11642 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 zJournal, SQLIT
11643 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
11644 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20 20 &exists);.
11645 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
11646 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
11647 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 to delmaster_out
11648 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
11649 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 20 if( exists ){.
1164a 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
1164b 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f 69 the journals poi
1164c 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 6d nted to by the m
1164d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 78 aster journal ex
1164e 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a ists.. **
1164f 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68 65 Open it and che
11650 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73 20 ck if it points
11651 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f at the master jo
11652 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 20 urnal. If.
11653 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e 20 ** so, return
11654 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e 67 without deleting
11655 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11656 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 nal file..
11657 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 */. int
11658 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 c;. int
11659 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f flags = (SQLITE_
1165a 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 OPEN_READONLY|SQ
1165b 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
1165c 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 20 OURNAL);.
1165d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
1165e 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e pen(pVfs, zJourn
1165f 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c al, pJournal, fl
11660 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ags, 0);.
11661 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
11662 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
11663 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f goto delmaster_o
11664 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 ut;. }..
11665 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 rc = read
11666 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a MasterJournal(pJ
11667 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50 ournal, zMasterP
11668 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b tr, nMasterPtr);
11669 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1166a 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c OsClose(pJournal
1166b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1166c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1166d 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 goto d
1166e 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 elmaster_out;.
1166f 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
11670 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 5b c = zMasterPtr[
11671 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70 28 0]!=0 && strcmp(
11672 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 73 zMasterPtr, zMas
11673 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 20 ter)==0;.
11674 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20 20 if( c ){.
11675 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 /* We have a
11676 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 64 match. Do not d
11677 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 elete the master
11678 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a journal file. *
11679 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f /. goto
1167a 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
1167b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1167c 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 6c }. zJournal
1167d 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c += (sqlite3Strl
1167e 65 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31 en30(zJournal)+1
1167f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a );. }. }. .
11680 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11681 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 Delete(pVfs, zMa
11682 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c 6d 61 ster, 0);..delma
11683 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20 ster_out:. if(
11684 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 zMasterJournal )
11685 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
11686 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 ee(zMasterJourna
11687 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 l);. } . if(
11688 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 73 pMaster ){. s
11689 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4d qlite3OsClose(pM
1168a 61 73 74 65 72 29 3b 0a 20 20 20 20 61 73 73 65 aster);. asse
1168b 72 74 28 20 21 69 73 4f 70 65 6e 28 70 4a 6f 75 rt( !isOpen(pJou
1168c 72 6e 61 6c 29 20 29 3b 0a 20 20 7d 0a 20 20 73 rnal) );. }. s
1168d 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 73 qlite3_free(pMas
1168e 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ter);. return r
1168f 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 c;.}.../*.** Thi
11690 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
11691 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ed to change the
11692 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 20 actual size of
11693 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a the database .**
11694 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c file in the fil
11695 65 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20 6f e-system. This o
11696 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e nly happens when
11697 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 committing a tr
11698 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 6f 72 ansaction,.** or
11699 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 rolling back a
1169a 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e 63 transaction (inc
1169b 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20 62 luding rolling b
1169c 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ack a hot-journa
1169d 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 l)..**.** If the
1169e 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
1169f 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c ile is not open,
116a0 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 or an exclusive
116a1 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 0a 2a 2a 20 lock is not.**
116a2 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74 held, this funct
116a3 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 ion is a no-op.
116a4 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73 Otherwise, the s
116a5 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
116a6 69 73 0a 2a 2a 20 63 68 61 6e 67 65 64 20 74 6f is.** changed to
116a7 20 6e 50 61 67 65 20 70 61 67 65 73 20 28 6e 50 nPage pages (nP
116a8 61 67 65 2a 70 50 61 67 65 72 2d 3e 70 61 67 65 age*pPager->page
116a9 53 69 7a 65 20 62 79 74 65 73 29 2e 20 49 66 20 Size bytes). If
116aa 74 68 65 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 the file.** on d
116ab 69 73 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 isk is currently
116ac 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6e 50 61 larger than nPa
116ad 67 65 20 70 61 67 65 73 2c 20 74 68 65 6e 20 75 ge pages, then u
116ae 73 65 20 74 68 65 20 56 46 53 0a 2a 2a 20 78 54 se the VFS.** xT
116af 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64 runcate() method
116b0 20 74 6f 20 74 72 75 6e 63 61 74 65 20 69 74 2e to truncate it.
116b1 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 74 20 6d 69 .**.** Or, it mi
116b2 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 ght might be the
116b3 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 66 case that the f
116b4 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 73 ile on disk is s
116b5 6d 61 6c 6c 65 72 20 74 68 61 6e 20 0a 2a 2a 20 maller than .**
116b6 6e 50 61 67 65 20 70 61 67 65 73 2e 20 53 6f 6d nPage pages. Som
116b7 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
116b8 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f em implementatio
116b9 6e 73 20 63 61 6e 20 67 65 74 20 63 6f 6e 66 75 ns can get confu
116ba 73 65 64 20 69 66 20 0a 2a 2a 20 79 6f 75 20 74 sed if .** you t
116bb 72 79 20 74 6f 20 74 72 75 6e 63 61 74 65 20 61 ry to truncate a
116bc 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69 file to some si
116bd 7a 65 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 ze that is large
116be 72 20 74 68 61 6e 20 69 74 20 0a 2a 2a 20 63 75 r than it .** cu
116bf 72 72 65 6e 74 6c 79 20 69 73 2c 20 73 6f 20 64 rrently is, so d
116c0 65 74 65 63 74 20 74 68 69 73 20 63 61 73 65 20 etect this case
116c1 61 6e 64 20 77 72 69 74 65 20 61 20 73 69 6e 67 and write a sing
116c2 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74 6f 20 le zero byte to
116c3 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 .** the end of t
116c4 68 65 20 6e 65 77 20 66 69 6c 65 20 69 6e 73 74 he new file inst
116c5 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 ead..**.** If su
116c6 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e ccessful, return
116c7 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 SQLITE_OK. If a
116c8 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
116c9 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 79 69 6e s while modifyin
116ca 67 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 g.** the databas
116cb 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e 20 74 e file, return t
116cc 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f he error code to
116cd 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a the caller..*/.
116ce 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
116cf 5f 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 20 _truncate(Pager
116d0 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 *pPager, Pgno nP
116d1 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d age){. int rc =
116d2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
116d3 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
116d4 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 =PAGER_EXCLUSIVE
116d5 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
116d6 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 69 36 r->fd) ){. i6
116d7 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20 6e 4 currentSize, n
116d8 65 77 53 69 7a 65 3b 0a 20 20 20 20 2f 2a 20 54 ewSize;. /* T
116d9 4f 44 4f 3a 20 49 73 20 69 74 20 73 61 66 65 20 ODO: Is it safe
116da 74 6f 20 75 73 65 20 50 61 67 65 72 2e 64 62 46 to use Pager.dbF
116db 69 6c 65 53 69 7a 65 20 68 65 72 65 3f 20 2a 2f ileSize here? */
116dc 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
116dd 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 3OsFileSize(pPag
116de 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e 74 er->fd, ¤t
116df 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53 69 Size);. newSi
116e0 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 ze = pPager->pag
116e1 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67 65 eSize*(i64)nPage
116e2 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
116e3 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 65 LITE_OK && curre
116e4 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 20 ntSize!=newSize
116e5 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 72 ){. if( cur
116e6 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a 65 rentSize>newSize
116e7 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
116e8 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
116e9 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6e te(pPager->fd, n
116ea 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d ewSize);. }
116eb 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
116ec 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
116ed 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22 22 e(pPager->fd, ""
116ee 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29 3b , 1, newSize-1);
116ef 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
116f0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
116f1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 ){. pPag
116f2 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d er->dbFileSize =
116f3 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a nPage;. }.
116f4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
116f5 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
116f6 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 Set the value of
116f7 20 74 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f the Pager.secto
116f8 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 66 rSize variable f
116f9 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 or the given.**
116fa 70 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20 74 pager based on t
116fb 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
116fc 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 d by the xSector
116fd 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f Size method.** o
116fe 66 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62 f the open datab
116ff 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73 65 ase file. The se
11700 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 ctor size will b
11701 65 20 75 73 65 64 20 75 73 65 64 20 0a 2a 2a 20 e used used .**
11702 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
11703 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 67 6e 6d size and alignm
11704 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 ent of journal h
11705 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a 20 6d 61 eader and .** ma
11706 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 ster journal poi
11707 6e 74 65 72 73 20 77 69 74 68 69 6e 20 63 72 65 nters within cre
11708 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ated journal fil
11709 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 65 es..**.** For te
1170a 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 74 68 mporary files th
1170b 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 e effective sect
1170c 6f 72 20 73 69 7a 65 20 69 73 20 61 6c 77 61 79 or size is alway
1170d 73 20 35 31 32 20 62 79 74 65 73 2e 0a 2a 2a 0a s 512 bytes..**.
1170e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f ** Otherwise, fo
1170f 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 72 79 20 r non-temporary
11710 66 69 6c 65 73 2c 20 74 68 65 20 65 66 66 65 63 files, the effec
11711 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65 tive sector size
11712 20 69 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 is.** the value
11713 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
11714 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d xSectorSize() m
11715 65 74 68 6f 64 20 72 6f 75 6e 64 65 64 20 75 70 ethod rounded up
11716 20 74 6f 20 35 31 32 20 69 66 0a 2a 2a 20 69 74 to 512 if.** it
11717 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 35 31 is less than 51
11718 32 2c 20 6f 72 20 72 6f 75 6e 64 65 64 20 64 6f 2, or rounded do
11719 77 6e 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f 52 wn to MAX_SECTOR
1171a 5f 53 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20 69 _SIZE if it.** i
1171b 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4d s greater than M
1171c 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e 0a AX_SECTOR_SIZE..
1171d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
1171e 65 74 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67 etSectorSize(Pag
1171f 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 er *pPager){. a
11720 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
11721 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 ager->fd) || pPa
11722 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
11723 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d .. if( !pPager-
11724 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 >tempFile ){.
11725 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 /* Sector size
11726 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 66 doesn't matter f
11727 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c or temporary fil
11728 65 73 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66 69 es. Also, the fi
11729 6c 65 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f le. ** may no
1172a 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e t have been open
1172b 65 64 20 79 65 74 2c 20 69 6e 20 77 68 69 63 68 ed yet, in which
1172c 20 63 61 73 65 20 74 68 65 20 4f 73 53 65 63 74 case the OsSect
1172d 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 orSize(). **
1172e 63 61 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61 75 call will segfau
1172f 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 lt.. */. p
11730 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
11731 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 e = sqlite3OsSec
11732 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d 3e torSize(pPager->
11733 66 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 fd);. }. if( p
11734 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
11735 65 3c 35 31 32 20 29 7b 0a 20 20 20 20 70 50 61 e<512 ){. pPa
11736 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 ger->sectorSize
11737 3d 20 35 31 32 3b 0a 20 20 7d 0a 20 20 69 66 28 = 512;. }. if(
11738 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 pPager->sectorS
11739 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 ize>MAX_SECTOR_S
1173a 49 5a 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 IZE ){. asser
1173b 74 28 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 t( MAX_SECTOR_SI
1173c 5a 45 3e 3d 35 31 32 20 29 3b 0a 20 20 20 20 70 ZE>=512 );. p
1173d 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
1173e 65 20 3d 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 e = MAX_SECTOR_S
1173f 49 5a 45 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a IZE;. }.}../*.*
11740 2a 20 50 6c 61 79 62 61 63 6b 20 74 68 65 20 6a * Playback the j
11741 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 75 73 20 ournal and thus
11742 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 restore the data
11743 62 61 73 65 20 66 69 6c 65 20 74 6f 0a 2a 2a 20 base file to.**
11744 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61 73 the state it was
11745 20 69 6e 20 62 65 66 6f 72 65 20 77 65 20 73 74 in before we st
11746 61 72 74 65 64 20 6d 61 6b 69 6e 67 20 63 68 61 arted making cha
11747 6e 67 65 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 nges. .**.** Th
11748 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 e journal file f
11749 6f 72 6d 61 74 20 69 73 20 61 73 20 66 6f 6c 6c ormat is as foll
1174a 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29 ows: .**.** (1)
1174b 20 20 38 20 62 79 74 65 20 70 72 65 66 69 78 2e 8 byte prefix.
1174c 20 20 41 20 63 6f 70 79 20 6f 66 20 61 4a 6f 75 A copy of aJou
1174d 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20 rnalMagic[]..**
1174e 20 28 32 29 20 20 34 20 62 79 74 65 20 62 69 67 (2) 4 byte big
1174f 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
11750 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d which is the num
11751 62 65 72 20 6f 66 20 76 61 6c 69 64 20 70 61 67 ber of valid pag
11752 65 20 72 65 63 6f 72 64 73 0a 2a 2a 20 20 20 20 e records.**
11753 20 20 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 in the journa
11754 6c 2e 20 20 49 66 20 74 68 69 73 20 76 61 6c 75 l. If this valu
11755 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66 2c e is 0xffffffff,
11756 20 74 68 65 6e 20 63 6f 6d 70 75 74 65 20 74 68 then compute th
11757 65 0a 2a 2a 20 20 20 20 20 20 20 6e 75 6d 62 65 e.** numbe
11758 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f 72 64 r of page record
11759 73 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e s from the journ
1175a 61 6c 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 33 29 al size..** (3)
1175b 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 4 byte big-end
1175c 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 ian integer whic
1175d 68 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c h is the initial
1175e 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 0a value for the .
1175f 2a 2a 20 20 20 20 20 20 20 73 61 6e 69 74 79 20 ** sanity
11760 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34 checksum..** (4
11761 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67 65 ) 4 byte intege
11762 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e r which is the n
11763 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 umber of pages t
11764 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a 2a o truncate the.*
11765 2a 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 * database
11766 20 74 6f 20 64 75 72 69 6e 67 20 61 20 72 6f 6c to during a rol
11767 6c 62 61 63 6b 2e 0a 2a 2a 20 20 28 35 29 20 20 lback..** (5)
11768 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 4 byte big-endia
11769 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
1176a 69 73 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 is the sector si
1176b 7a 65 2e 20 20 54 68 65 20 68 65 61 64 65 72 0a ze. The header.
1176c 2a 2a 20 20 20 20 20 20 20 69 73 20 74 68 69 73 ** is this
1176d 20 6d 61 6e 79 20 62 79 74 65 73 20 69 6e 20 73 many bytes in s
1176e 69 7a 65 2e 0a 2a 2a 20 20 28 36 29 20 20 34 20 ize..** (6) 4
1176f 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
11770 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
11771 20 74 68 65 20 70 61 67 65 20 63 61 73 65 2e 0a the page case..
11772 2a 2a 20 20 28 37 29 20 20 34 20 62 79 74 65 20 ** (7) 4 byte
11773 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
11774 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
11775 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73 74 ytes in the mast
11776 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 er journal.**
11777 20 20 20 20 6e 61 6d 65 2e 20 20 54 68 65 20 76 name. The v
11778 61 6c 75 65 20 6d 61 79 20 62 65 20 7a 65 72 6f alue may be zero
11779 20 28 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 (indicate that
1177a 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 there is no mast
1177b 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 er.** jour
1177c 6e 61 6c 2e 29 0a 2a 2a 20 20 28 38 29 20 20 4e nal.).** (8) N
1177d 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 6d 61 bytes of the ma
1177e 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
1177f 65 2e 20 20 54 68 65 20 6e 61 6d 65 20 77 69 6c e. The name wil
11780 6c 20 62 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 l be nul-termina
11781 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 61 6e 64 ted.** and
11782 20 6d 69 67 68 74 20 62 65 20 73 68 6f 72 74 65 might be shorte
11783 72 20 74 68 61 6e 20 74 68 65 20 76 61 6c 75 65 r than the value
11784 20 72 65 61 64 20 66 72 6f 6d 20 28 35 29 2e 20 read from (5).
11785 20 49 66 20 74 68 65 20 66 69 72 73 74 20 62 79 If the first by
11786 74 65 0a 2a 2a 20 20 20 20 20 20 20 6f 66 20 74 te.** of t
11787 68 65 20 6e 61 6d 65 20 69 73 20 5c 30 30 30 20 he name is \000
11788 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f then there is no
11789 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e master journal.
1178a 20 20 54 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 The master.**
1178b 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 6e 61 journal na
1178c 6d 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 me is stored in
1178d 55 54 46 2d 38 2e 0a 2a 2a 20 20 28 39 29 20 20 UTF-8..** (9)
1178e 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 Zero or more pag
1178f 65 73 20 69 6e 73 74 61 6e 63 65 73 2c 20 65 61 es instances, ea
11790 63 68 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a ch as follows:.*
11791 2a 20 20 20 20 20 20 20 20 2b 20 20 34 20 62 79 * + 4 by
11792 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a te page number..
11793 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 70 50 61 ** + pPa
11794 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 ger->pageSize by
11795 74 65 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 tes of data..**
11796 20 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65 + 4 byte
11797 20 63 68 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 checksum.**.**
11798 57 68 65 6e 20 77 65 20 73 70 65 61 6b 20 6f 66 When we speak of
11799 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
1179a 64 65 72 2c 20 77 65 20 6d 65 61 6e 20 74 68 65 der, we mean the
1179b 20 66 69 72 73 74 20 38 20 69 74 65 6d 73 20 61 first 8 items a
1179c 62 6f 76 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e bove..** Each en
1179d 74 72 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e try in the journ
1179e 61 6c 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 al is an instanc
1179f 65 20 6f 66 20 74 68 65 20 39 74 68 20 69 74 65 e of the 9th ite
117a0 6d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 m..**.** Call th
117a1 65 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 e value from the
117a2 20 73 65 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22 second bullet "
117a3 6e 52 65 63 22 2e 20 20 6e 52 65 63 20 69 73 20 nRec". nRec is
117a4 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
117a5 20 76 61 6c 69 64 20 70 61 67 65 20 65 6e 74 72 valid page entr
117a6 69 65 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ies in the journ
117a7 61 6c 2e 20 20 49 6e 20 6d 6f 73 74 20 63 61 73 al. In most cas
117a8 65 73 2c 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70 es, you can comp
117a9 75 74 65 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 ute the.** value
117aa 20 6f 66 20 6e 52 65 63 20 66 72 6f 6d 20 74 68 of nRec from th
117ab 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f e size of the jo
117ac 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 42 75 74 urnal file. But
117ad 20 69 66 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66 if a power.** f
117ae 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 20 ailure occurred
117af 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 while the journa
117b0 6c 20 77 61 73 20 62 65 69 6e 67 20 77 72 69 74 l was being writ
117b1 74 65 6e 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 ten, it could be
117b2 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 the.** case tha
117b3 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 t the size of th
117b4 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 e journal file h
117b5 61 64 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ad already been
117b6 69 6e 63 72 65 61 73 65 64 20 62 75 74 0a 2a 2a increased but.**
117b7 20 74 68 65 20 65 78 74 72 61 20 65 6e 74 72 69 the extra entri
117b8 65 73 20 68 61 64 20 6e 6f 74 20 79 65 74 20 6d es had not yet m
117b9 61 64 65 20 69 74 20 73 61 66 65 6c 79 20 74 6f ade it safely to
117ba 20 64 69 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 disk. In such
117bb 61 20 63 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76 a case,.** the v
117bc 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d alue of nRec com
117bd 70 75 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 puted from the f
117be 69 6c 65 20 73 69 7a 65 20 77 6f 75 6c 64 20 62 ile size would b
117bf 65 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f e too large. Fo
117c0 72 0a 2a 2a 20 74 68 61 74 20 72 65 61 73 6f 6e r.** that reason
117c1 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 , we always use
117c2 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 the nRec value i
117c3 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a n the header..**
117c4 0a 2a 2a 20 49 66 20 74 68 65 20 6e 52 65 63 20 .** If the nRec
117c5 76 61 6c 75 65 20 69 73 20 30 78 66 66 66 66 66 value is 0xfffff
117c6 66 66 66 20 69 74 20 6d 65 61 6e 73 20 74 68 61 fff it means tha
117c7 74 20 6e 52 65 63 20 73 68 6f 75 6c 64 20 62 65 t nRec should be
117c8 20 63 6f 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f computed.** fro
117c9 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2e m the file size.
117ca 20 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 This value is
117cb 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 75 73 used when the us
117cc 65 72 20 73 65 6c 65 63 74 73 20 74 68 65 0a 2a er selects the.*
117cd 2a 20 6e 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e * no-sync option
117ce 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c for the journal
117cf 2e 20 20 41 20 70 6f 77 65 72 20 66 61 69 6c 75 . A power failu
117d0 72 65 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f re could lead to
117d1 20 63 6f 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69 corruption.** i
117d2 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 42 75 n this case. Bu
117d3 74 20 66 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b t for things lik
117d4 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c e temporary tabl
117d5 65 20 28 77 68 69 63 68 20 77 69 6c 6c 20 62 65 e (which will be
117d6 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e .** deleted when
117d7 20 74 68 65 20 70 6f 77 65 72 20 69 73 20 72 65 the power is re
117d8 73 74 6f 72 65 64 29 20 77 65 20 64 6f 6e 27 74 stored) we don't
117d9 20 63 61 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 care. .**.** I
117da 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 f the file opene
117db 64 20 61 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c d as the journal
117dc 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 20 77 file is not a w
117dd 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f ell-formed.** jo
117de 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 urnal file then
117df 61 6c 6c 20 70 61 67 65 73 20 75 70 20 74 6f 20 all pages up to
117e0 74 68 65 20 66 69 72 73 74 20 63 6f 72 72 75 70 the first corrup
117e1 74 65 64 20 70 61 67 65 20 61 72 65 20 72 6f 6c ted page are rol
117e2 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20 led.** back (or
117e3 6e 6f 20 70 61 67 65 73 20 69 66 20 74 68 65 20 no pages if the
117e4 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 journal header i
117e5 73 20 63 6f 72 72 75 70 74 65 64 29 2e 20 54 68 s corrupted). Th
117e6 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a e journal file.*
117e7 2a 20 69 73 20 74 68 65 6e 20 64 65 6c 65 74 65 * is then delete
117e8 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 d and SQLITE_OK
117e9 72 65 74 75 72 6e 65 64 2c 20 6a 75 73 74 20 61 returned, just a
117ea 73 20 69 66 20 6e 6f 20 63 6f 72 72 75 70 74 69 s if no corrupti
117eb 6f 6e 20 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65 on had.** been e
117ec 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a ncountered..**.*
117ed 2a 20 49 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d * If an I/O or m
117ee 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 alloc() error oc
117ef 63 75 72 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 curs, the journa
117f0 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 l-file is not de
117f1 6c 65 74 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 leted.** and an
117f2 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
117f3 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 turned..**.** Th
117f4 65 20 69 73 48 6f 74 20 70 61 72 61 6d 65 74 65 e isHot paramete
117f5 72 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 r indicates that
117f6 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 we are trying t
117f7 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 6a 6f 75 o rollback a jou
117f8 72 6e 61 6c 0a 2a 2a 20 74 68 61 74 20 6d 69 67 rnal.** that mig
117f9 68 74 20 62 65 20 61 20 68 6f 74 20 6a 6f 75 72 ht be a hot jour
117fa 6e 61 6c 2e 20 20 4f 72 2c 20 69 74 20 63 6f 75 nal. Or, it cou
117fb 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 20 6a ld be that the j
117fc 6f 75 72 6e 61 6c 20 69 73 20 0a 2a 2a 20 70 72 ournal is .** pr
117fd 65 73 65 72 76 65 64 20 62 65 63 61 75 73 65 20 eserved because
117fe 6f 66 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 of JOURNALMODE_P
117ff 45 52 53 49 53 54 20 6f 72 20 4a 4f 55 52 4e 41 ERSIST or JOURNA
11800 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 2e 0a LMODE_TRUNCATE..
11801 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
11802 6c 20 72 65 61 6c 6c 79 20 69 73 20 68 6f 74 2c l really is hot,
11803 20 72 65 73 65 74 20 74 68 65 20 70 61 67 65 72 reset the pager
11804 20 63 61 63 68 65 20 70 72 69 6f 72 20 72 6f 6c cache prior rol
11805 6c 69 6e 67 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 ling.** back any
11806 20 63 6f 6e 74 65 6e 74 2e 20 20 49 66 20 74 68 content. If th
11807 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6d 65 72 e journal is mer
11808 65 6c 79 20 70 65 72 73 69 73 74 65 6e 74 2c 20 ely persistent,
11809 6e 6f 20 72 65 73 65 74 20 69 73 0a 2a 2a 20 6e no reset is.** n
1180a 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 eeded..*/.static
1180b 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 int pager_playb
1180c 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 ack(Pager *pPage
1180d 72 2c 20 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20 r, int isHot){.
1180e 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
1180f 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 fs = pPager->pVf
11810 73 3b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 s;. i64 szJ;
11811 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11812 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 Size of the jou
11813 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 rnal file in byt
11814 65 73 20 2a 2f 0a 20 20 75 33 32 20 6e 52 65 63 es */. u32 nRec
11815 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11816 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 /* Number of Re
11817 63 6f 72 64 73 20 69 6e 20 74 68 65 20 6a 6f 75 cords in the jou
11818 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b rnal */. u32 u;
11819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1181a 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c /* Unsigned l
1181b 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
1181c 20 50 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20 Pgno mxPg = 0;
1181d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
1181e 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 e of the origina
1181f 6c 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 l file in pages
11820 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
11821 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11822 20 52 65 73 75 6c 74 20 63 6f 64 65 20 6f 66 20 Result code of
11823 61 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a a subroutine */.
11824 20 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 int res = 1;
11825 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
11826 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
11827 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
11828 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 ) */. char *zMa
11829 73 74 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 ster = 0;
1182a 2f 2a 20 4e 61 6d 65 20 6f 66 20 6d 61 73 74 65 /* Name of maste
1182b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 r journal file i
1182c 66 20 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 6e f any */. int n
1182d 65 65 64 50 61 67 65 72 52 65 73 65 74 3b 20 20 eedPagerReset;
1182e 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 72 /* True to r
1182f 65 73 65 74 20 70 61 67 65 20 70 72 69 6f 72 20 eset page prior
11830 74 6f 20 66 69 72 73 74 20 70 61 67 65 20 72 6f to first page ro
11831 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 2f 2a 20 llback */.. /*
11832 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d Figure out how m
11833 61 6e 79 20 72 65 63 6f 72 64 73 20 61 72 65 20 any records are
11834 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 in the journal.
11835 20 41 62 6f 72 74 20 65 61 72 6c 79 20 69 66 0a Abort early if.
11836 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c ** the journal
11837 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a is empty.. */.
11838 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e assert( isOpen
11839 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b (pPager->jfd) );
1183a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
1183b 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 sFileSize(pPager
1183c 2d 3e 6a 66 64 2c 20 26 73 7a 4a 29 3b 0a 20 20 ->jfd, &szJ);.
1183d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1183e 4b 20 7c 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a 20 K || szJ==0 ){.
1183f 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 goto end_play
11840 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 back;. }.. /*
11841 52 65 61 64 20 74 68 65 20 6d 61 73 74 65 72 20 Read the master
11842 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f journal name fro
11843 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 m the journal, i
11844 66 20 69 74 20 69 73 20 70 72 65 73 65 6e 74 2e f it is present.
11845 0a 20 20 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 . ** If a maste
11846 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e r journal file n
11847 61 6d 65 20 69 73 20 73 70 65 63 69 66 69 65 64 ame is specified
11848 2c 20 62 75 74 20 74 68 65 20 66 69 6c 65 20 69 , but the file i
11849 73 20 6e 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 s not. ** prese
1184a 6e 74 20 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e nt on disk, then
1184b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
1184c 6e 6f 74 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 not hot and does
1184d 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 0a not need to be.
1184e 20 20 2a 2a 20 70 6c 61 79 65 64 20 62 61 63 6b ** played back
1184f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f .. **. ** TODO
11850 3a 20 54 65 63 68 6e 69 63 61 6c 6c 79 20 74 68 : Technically th
11851 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 e following is a
11852 6e 20 65 72 72 6f 72 20 62 65 63 61 75 73 65 20 n error because
11853 69 74 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a it assumes that.
11854 20 20 2a 2a 20 62 75 66 66 65 72 20 50 61 67 65 ** buffer Page
11855 72 2e 70 54 6d 70 53 70 61 63 65 20 69 73 20 28 r.pTmpSpace is (
11856 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 20 62 79 mxPathname+1) by
11857 74 65 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 69 tes or larger. i
11858 2e 65 2e 20 74 68 61 74 0a 20 20 2a 2a 20 28 70 .e. that. ** (p
11859 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 Pager->pageSize
1185a 3e 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d >= pPager->pVfs-
1185b 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 2e 20 >mxPathname+1).
1185c 55 73 69 6e 67 20 6f 73 5f 75 6e 69 78 2e 63 2c Using os_unix.c,
1185d 0a 20 20 2a 2a 20 20 6d 78 50 61 74 68 6e 61 6d . ** mxPathnam
1185e 65 20 69 73 20 35 31 32 2c 20 77 68 69 63 68 20 e is 512, which
1185f 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 is the same as t
11860 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 he minimum allow
11861 61 62 6c 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 able value. **
11862 66 6f 72 20 70 61 67 65 53 69 7a 65 2e 0a 20 20 for pageSize..
11863 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 */. zMaster = p
11864 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
11865 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 ;. rc = readMas
11866 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 terJournal(pPage
11867 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c r->jfd, zMaster,
11868 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d pPager->pVfs->m
11869 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 xPathname+1);.
1186a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1186b 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 K && zMaster[0]
1186c 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1186d 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 te3OsAccess(pVfs
1186e 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 , zMaster, SQLIT
1186f 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
11870 20 26 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d &res);. }. zM
11871 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 aster = 0;. if(
11872 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
11873 7c 20 21 72 65 73 20 29 7b 0a 20 20 20 20 67 6f | !res ){. go
11874 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b to end_playback;
11875 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a . }. pPager->j
11876 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 ournalOff = 0;.
11877 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 needPagerReset
11878 3d 20 69 73 48 6f 74 3b 0a 0a 20 20 2f 2a 20 54 = isHot;.. /* T
11879 68 69 73 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 his loop termina
1187a 74 65 73 20 65 69 74 68 65 72 20 77 68 65 6e 20 tes either when
1187b 61 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 a readJournalHdr
1187c 28 29 20 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 () or . ** page
1187d 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
1187e 61 67 65 28 29 20 63 61 6c 6c 20 72 65 74 75 72 age() call retur
1187f 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f ns SQLITE_DONE o
11880 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 20 r an IO error .
11881 20 2a 2a 20 6f 63 63 75 72 73 2e 20 0a 20 20 2a ** occurs. . *
11882 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a /. while( 1 ){.
11883 20 20 20 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 int isUnsync
11884 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 = 0;.. /* Re
11885 61 64 20 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 ad the next jour
11886 6e 61 6c 20 68 65 61 64 65 72 20 66 72 6f 6d 20 nal header from
11887 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11888 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 0a . If there are.
11889 20 20 20 20 2a 2a 20 6e 6f 74 20 65 6e 6f 75 67 ** not enoug
1188a 68 20 62 79 74 65 73 20 6c 65 66 74 20 69 6e 20 h bytes left in
1188b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1188c 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 20 for a complete
1188d 68 65 61 64 65 72 2c 20 6f 72 0a 20 20 20 20 2a header, or. *
1188e 2a 20 69 74 20 69 73 20 63 6f 72 72 75 70 74 65 * it is corrupte
1188f 64 2c 20 74 68 65 6e 20 61 20 70 72 6f 63 65 73 d, then a proces
11890 73 20 6d 75 73 74 20 6f 66 20 66 61 69 6c 65 64 s must of failed
11891 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67 20 69 while writing i
11892 74 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 t.. ** This i
11893 6e 64 69 63 61 74 65 73 20 6e 6f 74 68 69 6e 67 ndicates nothing
11894 20 6d 6f 72 65 20 6e 65 65 64 73 20 74 6f 20 62 more needs to b
11895 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 e rolled back..
11896 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 72 */. rc = r
11897 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 eadJournalHdr(pP
11898 61 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 ager, szJ, &nRec
11899 2c 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 , &mxPg);. if
1189a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1189b 29 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 ){ . if( rc
1189c 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
1189d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
1189e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
1189f 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f . goto end_
118a0 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a playback;. }.
118a1 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 . /* If nRec
118a2 69 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 is 0xffffffff, t
118a3 68 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c hen this journal
118a4 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 was created by
118a5 61 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a a process. **
118a6 20 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 working in no-s
118a7 79 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d ync mode. This m
118a8 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65 eans that the re
118a9 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 st of the journa
118aa 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f l. ** file co
118ab 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c nsists of pages,
118ac 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f there are no mo
118ad 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 re journal heade
118ae 72 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 rs. Compute.
118af 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
118b0 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68 nRec based on th
118b1 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 is assumption..
118b2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 */. if( nR
118b3 65 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29 ec==0xffffffff )
118b4 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
118b5 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
118b6 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ff==JOURNAL_HDR_
118b7 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 SZ(pPager) );.
118b8 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 nRec = (int)
118b9 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f ((szJ - JOURNAL_
118ba 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f HDR_SZ(pPager))/
118bb 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
118bc 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 ager));. }..
118bd 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 /* If nRec is
118be 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 0 and this roll
118bf 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61 back is of a tra
118c0 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64 nsaction created
118c1 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 by this. **
118c2 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74 process and if t
118c3 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c his is the final
118c4 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a header in the j
118c5 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 ournal, then it
118c6 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 means. ** tha
118c7 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74 t this part of t
118c8 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 he journal was b
118c9 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20 eing filled but
118ca 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
118cb 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 . ** synced t
118cc 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 o disk. Compute
118cd 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
118ce 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68 ages based on th
118cf 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 e remaining.
118d0 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 ** size of the f
118d1 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
118d2 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72 ** The third ter
118d3 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61 m of the test wa
118d4 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74 s added to fix t
118d5 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20 icket #2565..
118d6 20 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 ** When rolling
118d7 20 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 back a hot jour
118d8 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 nal, nRec==0 alw
118d9 61 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 ays means that t
118da 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 he next. ** c
118db 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 hunk of the jour
118dc 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 nal contains zer
118dd 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f o pages to be ro
118de 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a lled back. But.
118df 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e ** when doin
118e0 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 g a ROLLBACK and
118e1 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 the nRec==0 chu
118e2 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 63 nk is the last c
118e3 68 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 hunk in. ** t
118e4 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d he journal, it m
118e5 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f eans that the jo
118e6 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 urnal might cont
118e7 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 ain additional.
118e8 20 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74 ** pages that
118e9 20 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c need to be roll
118ea 65 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74 ed back and that
118eb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
118ec 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f ages . ** sho
118ed 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20 uld be computed
118ee 62 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 based on the jou
118ef 72 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a rnal file size..
118f0 20 20 20 20 2a 2f 0a 20 20 20 20 74 65 73 74 63 */. testc
118f1 61 73 65 28 20 6e 52 65 63 3d 3d 30 20 26 26 20 ase( nRec==0 &&
118f2 21 69 73 48 6f 74 0a 20 20 20 20 20 20 20 20 20 !isHot.
118f3 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
118f4 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 alHdr+JOURNAL_HD
118f5 52 5f 53 5a 28 70 50 61 67 65 72 29 21 3d 70 50 R_SZ(pPager)!=pP
118f6 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
118f7 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 28 73 . && ((s
118f8 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 zJ - pPager->jou
118f9 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e rnalOff) / JOURN
118fa 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 AL_PG_SZ(pPager)
118fb 29 3e 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 )>0. &&
118fc 70 61 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c pagerNextJournal
118fd 50 61 67 65 49 73 56 61 6c 69 64 28 70 50 61 67 PageIsValid(pPag
118fe 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 er). );. i
118ff 66 28 20 6e 52 65 63 3d 3d 30 20 26 26 20 21 69 f( nRec==0 && !i
11900 73 48 6f 74 20 26 26 0a 20 20 20 20 20 20 20 20 sHot &&.
11901 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
11902 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 dr+JOURNAL_HDR_S
11903 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 Z(pPager)==pPage
11904 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b r->journalOff ){
11905 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20 28 69 . nRec = (i
11906 6e 74 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 nt)((szJ - pPage
11907 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f r->journalOff) /
11908 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 JOURNAL_PG_SZ(p
11909 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 69 Pager));. i
1190a 73 55 6e 73 79 6e 63 20 3d 20 31 3b 0a 20 20 20 sUnsync = 1;.
1190b 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 }.. /* If th
1190c 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 is is the first
1190d 68 65 61 64 65 72 20 72 65 61 64 20 66 72 6f 6d header read from
1190e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 72 the journal, tr
1190f 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20 20 2a uncate the. *
11910 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
11911 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 back to its orig
11912 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 20 20 2a inal size.. *
11913 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 /. if( pPager
11914 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f ->journalOff==JO
11915 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
11916 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 ger) ){. rc
11917 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 = pager_truncat
11918 65 28 70 50 61 67 65 72 2c 20 6d 78 50 67 29 3b e(pPager, mxPg);
11919 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1191a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1191b 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 goto end_pla
1191c 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 yback;. }.
1191d 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 pPager->dbS
1191e 69 7a 65 20 3d 20 6d 78 50 67 3b 0a 20 20 20 20 ize = mxPg;.
1191f 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f }.. /* Copy o
11920 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 6f 75 riginal pages ou
11921 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c t of the journal
11922 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 and back into t
11923 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 he . ** datab
11924 61 73 65 20 66 69 6c 65 20 61 6e 64 2f 6f 72 20 ase file and/or
11925 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 20 20 page cache..
11926 2a 2f 0a 20 20 20 20 66 6f 72 28 75 3d 30 3b 20 */. for(u=0;
11927 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 u<nRec; u++){.
11928 20 20 20 20 69 66 28 20 6e 65 65 64 50 61 67 65 if( needPage
11929 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20 rReset ){.
1192a 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 pager_reset(pP
1192b 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 6e ager);. n
1192c 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d 20 eedPagerReset =
1192d 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
1192e 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 rc = pager_play
1192f 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 back_one_page(pP
11930 61 67 65 72 2c 31 2c 69 73 55 6e 73 79 6e 63 2c ager,1,isUnsync,
11931 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c &pPager->journal
11932 4f 66 66 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 Off,0,0);.
11933 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
11934 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 K ){. if(
11935 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc==SQLITE_DONE
11936 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
11937 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
11938 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
11939 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a journalOff = szJ
1193a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
1193b 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 k;. }else
1193c 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 {. /* I
1193d 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 f we are unable
1193e 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 to rollback, the
1193f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 n the database i
11940 73 20 70 72 6f 62 61 62 6c 79 0a 20 20 20 20 20 s probably.
11941 20 20 20 20 20 2a 2a 20 67 6f 69 6e 67 20 74 6f ** going to
11942 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 63 6f end up being co
11943 72 72 75 70 74 2e 20 20 49 74 20 69 73 20 63 6f rrupt. It is co
11944 72 72 75 70 74 20 74 6f 20 75 73 2c 20 61 6e 79 rrupt to us, any
11945 68 6f 77 2e 0a 20 20 20 20 20 20 20 20 20 20 2a how.. *
11946 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 65 * Perhaps the ne
11947 78 74 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f xt process to co
11948 6d 65 20 61 6c 6f 6e 67 20 63 61 6e 20 66 69 78 me along can fix
11949 20 69 74 2e 2e 2e 2e 0a 20 20 20 20 20 20 20 20 it.....
1194a 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 */. r
1194b 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
1194c 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
1194d 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 goto end_play
1194e 62 61 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a back;. }.
1194f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
11950 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 }. /*NOTREACHED
11951 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 */. assert( 0 )
11952 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3a ;..end_playback:
11953 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 . /* Following
11954 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 a rollback, the
11955 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 68 database file sh
11956 6f 75 6c 64 20 62 65 20 62 61 63 6b 20 69 6e 20 ould be back in
11957 69 74 73 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a its original. *
11958 2a 20 73 74 61 74 65 20 70 72 69 6f 72 20 74 6f * state prior to
11959 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
1195a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 73 e transaction, s
1195b 6f 20 69 6e 76 6f 6b 65 20 74 68 65 0a 20 20 2a o invoke the. *
1195c 2a 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 * SQLITE_FCNTL_D
1195d 42 5f 55 4e 43 48 41 4e 47 45 44 20 66 69 6c 65 B_UNCHANGED file
1195e 2d 63 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 -control method
1195f 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 0a 20 to disable the.
11960 20 2a 2a 20 61 73 73 65 72 74 69 6f 6e 20 74 68 ** assertion th
11961 61 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 at the transacti
11962 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 20 6d on counter was m
11963 6f 64 69 66 69 65 64 2e 0a 20 20 2a 2f 0a 20 20 odified.. */.
11964 61 73 73 65 72 74 28 0a 20 20 20 20 70 50 61 67 assert(. pPag
11965 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 er->fd->pMethods
11966 3d 3d 30 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74 ==0 ||. sqlit
11967 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 e3OsFileControl(
11968 70 50 61 67 65 72 2d 3e 66 64 2c 53 51 4c 49 54 pPager->fd,SQLIT
11969 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 E_FCNTL_DB_UNCHA
1196a 4e 47 45 44 2c 30 29 3e 3d 53 51 4c 49 54 45 5f NGED,0)>=SQLITE_
1196b 4f 4b 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 OK. );.. /* If
1196c 20 74 68 69 73 20 70 6c 61 79 62 61 63 6b 20 69 this playback i
1196d 73 20 68 61 70 70 65 6e 69 6e 67 20 61 75 74 6f s happening auto
1196e 6d 61 74 69 63 61 6c 6c 79 20 61 73 20 61 20 72 matically as a r
1196f 65 73 75 6c 74 20 6f 66 20 61 6e 20 49 4f 20 6f esult of an IO o
11970 72 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65 r . ** malloc e
11971 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 72 rror that occurr
11972 65 64 20 61 66 74 65 72 20 74 68 65 20 63 68 61 ed after the cha
11973 6e 67 65 2d 63 6f 75 6e 74 65 72 20 77 61 73 20 nge-counter was
11974 75 70 64 61 74 65 64 20 62 75 74 20 0a 20 20 2a updated but . *
11975 2a 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 * before the tra
11976 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 63 6f 6d nsaction was com
11977 6d 69 74 74 65 64 2c 20 74 68 65 6e 20 74 68 65 mitted, then the
11978 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
11979 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 . ** modificati
1197a 6f 6e 20 6d 61 79 20 6a 75 73 74 20 68 61 76 65 on may just have
1197b 20 62 65 65 6e 20 72 65 76 65 72 74 65 64 2e 20 been reverted.
1197c 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 20 If this happens
1197d 69 6e 20 65 78 63 6c 75 73 69 76 65 20 0a 20 20 in exclusive .
1197e 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 73 75 ** mode, then su
1197f 62 73 65 71 75 65 6e 74 20 74 72 61 6e 73 61 63 bsequent transac
11980 74 69 6f 6e 73 20 70 65 72 66 6f 72 6d 65 64 20 tions performed
11981 62 79 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f by the connectio
11982 6e 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 n will not. **
11983 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
11984 65 2d 63 6f 75 6e 74 65 72 20 61 74 20 61 6c 6c e-counter at all
11985 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 64 20 . This may lead
11986 74 6f 20 63 61 63 68 65 20 69 6e 63 6f 6e 73 69 to cache inconsi
11987 73 74 65 6e 63 79 0a 20 20 2a 2a 20 70 72 6f 62 stency. ** prob
11988 6c 65 6d 73 20 66 6f 72 20 6f 74 68 65 72 20 70 lems for other p
11989 72 6f 63 65 73 73 65 73 20 61 74 20 73 6f 6d 65 rocesses at some
1198a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 point in the fu
1198b 74 75 72 65 2e 20 53 6f 2c 20 6a 75 73 74 0a 20 ture. So, just.
1198c 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 69 73 ** in case this
1198d 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20 63 has happened, c
1198e 6c 65 61 72 20 74 68 65 20 63 68 61 6e 67 65 43 lear the changeC
1198f 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 6e 6f ountDone flag no
11990 77 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 w.. */. pPager
11991 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e ->changeCountDon
11992 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 e = pPager->temp
11993 46 69 6c 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d File;.. if( rc=
11994 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11995 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 67 zMaster = pPag
11996 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 er->pTmpSpace;.
11997 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 rc = readMast
11998 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 erJournal(pPager
11999 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 ->jfd, zMaster,
1199a 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 pPager->pVfs->mx
1199b 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 Pathname+1);.
1199c 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 testcase( rc!=S
1199d 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a QLITE_OK );. }.
1199e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1199f 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 _OK ){. rc =
119a0 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
119a1 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d ction(pPager, zM
119a2 61 73 74 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b aster[0]!='\0');
119a3 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 . testcase( r
119a4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
119a5 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
119a6 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 LITE_OK && zMast
119a7 65 72 5b 30 5d 20 26 26 20 72 65 73 20 29 7b 0a er[0] && res ){.
119a8 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
119a9 77 61 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 was a master jou
119aa 72 6e 61 6c 20 61 6e 64 20 74 68 69 73 20 72 6f rnal and this ro
119ab 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 utine will retur
119ac 6e 20 73 75 63 63 65 73 73 2c 0a 20 20 20 20 2a n success,. *
119ad 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20 70 * see if it is p
119ae 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 ossible to delet
119af 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
119b0 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 rnal.. */.
119b1 20 72 63 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d rc = pager_delm
119b2 61 73 74 65 72 28 70 50 61 67 65 72 2c 20 7a 4d aster(pPager, zM
119b3 61 73 74 65 72 29 3b 0a 20 20 20 20 74 65 73 74 aster);. test
119b4 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 case( rc!=SQLITE
119b5 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a _OK );. }.. /*
119b6 20 54 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f The Pager.secto
119b7 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d rSize variable m
119b8 61 79 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 ay have been upd
119b9 61 74 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 ated while rolli
119ba 6e 67 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a ng. ** back a j
119bb 6f 75 72 6e 61 6c 20 63 72 65 61 74 65 64 20 62 ournal created b
119bc 79 20 61 20 70 72 6f 63 65 73 73 20 77 69 74 68 y a process with
119bd 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 65 63 a different sec
119be 74 6f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 tor size. ** va
119bf 6c 75 65 2e 20 52 65 73 65 74 20 69 74 20 74 6f lue. Reset it to
119c0 20 74 68 65 20 63 6f 72 72 65 63 74 20 76 61 6c the correct val
119c1 75 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 ue for this proc
119c2 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 ess.. */. setS
119c3 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 ectorSize(pPager
119c4 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
119c5 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 }../*.** Playbac
119c6 6b 20 73 61 76 65 70 6f 69 6e 74 20 70 53 61 76 k savepoint pSav
119c7 65 70 6f 69 6e 74 2e 20 4f 72 2c 20 69 66 20 70 epoint. Or, if p
119c8 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c Savepoint==NULL,
119c9 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 0a 2a then playback.*
119ca 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 * the entire mas
119cb 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
119cc 2e 20 54 68 65 20 63 61 73 65 20 70 53 61 76 65 . The case pSave
119cd 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75 point==NULL occu
119ce 72 73 20 77 68 65 6e 20 0a 2a 2a 20 61 20 52 4f rs when .** a RO
119cf 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e LLBACK TO comman
119d0 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 d is invoked on
119d1 61 20 53 41 56 45 50 4f 49 4e 54 20 74 68 61 74 a SAVEPOINT that
119d2 20 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f is a transactio
119d3 6e 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e n .** savepoint.
119d4 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 53 61 76 .**.** When pSav
119d5 65 70 6f 69 6e 74 20 69 73 20 6e 6f 74 20 4e 55 epoint is not NU
119d6 4c 4c 20 28 6d 65 61 6e 69 6e 67 20 61 20 6e 6f LL (meaning a no
119d7 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 n-transaction sa
119d8 76 65 70 6f 69 6e 74 20 69 73 20 0a 2a 2a 20 62 vepoint is .** b
119d9 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b eing rolled back
119da 29 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c ), then the roll
119db 62 61 63 6b 20 63 6f 6e 73 69 73 74 73 20 6f 66 back consists of
119dc 20 75 70 20 74 6f 20 74 68 72 65 65 20 73 74 61 up to three sta
119dd 67 65 73 2c 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 ges,.** performe
119de 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 73 d in the order s
119df 70 65 63 69 66 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 pecified:.**.**
119e0 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 70 6c * Pages are pl
119e1 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 ayed back from t
119e2 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
119e3 73 74 61 72 74 69 6e 67 20 61 74 20 62 79 74 65 starting at byte
119e4 0a 2a 2a 20 20 20 20 20 6f 66 66 73 65 74 20 50 .** offset P
119e5 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f agerSavepoint.iO
119e6 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e ffset and contin
119e7 75 69 6e 67 20 74 6f 20 0a 2a 2a 20 20 20 20 20 uing to .**
119e8 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 PagerSavepoint.i
119e9 48 64 72 4f 66 66 73 65 74 2c 20 6f 72 20 74 6f HdrOffset, or to
119ea 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
119eb 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 main journal.**
119ec 20 20 20 20 66 69 6c 65 20 69 66 20 50 61 67 65 file if Page
119ed 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
119ee 66 66 73 65 74 20 69 73 20 7a 65 72 6f 2e 0a 2a ffset is zero..*
119ef 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 50 61 67 65 *.** * If Page
119f0 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
119f1 66 66 73 65 74 20 69 73 20 6e 6f 74 20 7a 65 72 ffset is not zer
119f2 6f 2c 20 74 68 65 6e 20 70 61 67 65 73 20 61 72 o, then pages ar
119f3 65 20 70 6c 61 79 65 64 0a 2a 2a 20 20 20 20 20 e played.**
119f4 62 61 63 6b 20 73 74 61 72 74 69 6e 67 20 66 72 back starting fr
119f5 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 om the journal h
119f6 65 61 64 65 72 20 69 6d 6d 65 64 69 61 74 65 6c eader immediatel
119f7 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 y following .**
119f8 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 PagerSavepoi
119f9 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 74 6f nt.iHdrOffset to
119fa 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
119fb 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c main journal fil
119fc 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 e..**.** * Pag
119fd 65 73 20 61 72 65 20 74 68 65 6e 20 70 6c 61 79 es are then play
119fe 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 ed back from the
119ff 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c sub-journal fil
11a00 65 2c 20 73 74 61 72 74 69 6e 67 0a 2a 2a 20 20 e, starting.**
11a01 20 20 20 77 69 74 68 20 74 68 65 20 50 61 67 65 with the Page
11a02 72 53 61 76 65 70 6f 69 6e 74 2e 69 53 75 62 52 rSavepoint.iSubR
11a03 65 63 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e ec and continuin
11a04 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 0a g to the end of.
11a05 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e ** the journ
11a06 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 al file..**.** T
11a07 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 72 6f hroughout the ro
11a08 6c 6c 62 61 63 6b 20 70 72 6f 63 65 73 73 2c 20 llback process,
11a09 65 61 63 68 20 74 69 6d 65 20 61 20 70 61 67 65 each time a page
11a0a 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c is rolled back,
11a0b 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f the.** correspo
11a0c 6e 64 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 nding bit is set
11a0d 20 69 6e 20 61 20 62 69 74 76 65 63 20 73 74 72 in a bitvec str
11a0e 75 63 74 75 72 65 20 28 76 61 72 69 61 62 6c 65 ucture (variable
11a0f 20 70 44 6f 6e 65 20 69 6e 20 74 68 65 0a 2a 2a pDone in the.**
11a10 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
11a11 62 65 6c 6f 77 29 2e 20 54 68 69 73 20 69 73 20 below). This is
11a12 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 used to ensure t
11a13 68 61 74 20 61 20 70 61 67 65 20 69 73 20 6f 6e hat a page is on
11a14 6c 79 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 ly.** rolled bac
11a15 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 k the first time
11a16 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 it is encounter
11a17 65 64 20 69 6e 20 65 69 74 68 65 72 20 6a 6f 75 ed in either jou
11a18 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 rnal..**.** If p
11a19 53 61 76 65 70 6f 69 6e 74 20 69 73 20 4e 55 4c Savepoint is NUL
11a1a 4c 2c 20 74 68 65 6e 20 70 61 67 65 73 20 61 72 L, then pages ar
11a1b 65 20 6f 6e 6c 79 20 70 6c 61 79 65 64 20 62 61 e only played ba
11a1c 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ck from the main
11a1d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
11a1e 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 . There is no ne
11a1f 65 64 20 66 6f 72 20 61 20 62 69 74 76 65 63 20 ed for a bitvec
11a20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a in this case..**
11a21 0a 2a 2a 20 49 6e 20 65 69 74 68 65 72 20 63 61 .** In either ca
11a22 73 65 2c 20 62 65 66 6f 72 65 20 70 6c 61 79 62 se, before playb
11a23 61 63 6b 20 63 6f 6d 6d 65 6e 63 65 73 20 74 68 ack commences th
11a24 65 20 50 61 67 65 72 2e 64 62 53 69 7a 65 20 76 e Pager.dbSize v
11a25 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 72 65 ariable.** is re
11a26 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 set to the value
11a27 20 74 68 61 74 20 69 74 20 68 65 6c 64 20 61 74 that it held at
11a28 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
11a29 65 20 73 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 e savepoint .**
11a2a 28 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 (or transaction)
11a2b 2e 20 4e 6f 20 70 61 67 65 20 77 69 74 68 20 61 . No page with a
11a2c 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 67 72 65 page-number gre
11a2d 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 20 76 ater than this v
11a2e 61 6c 75 65 0a 2a 2a 20 69 73 20 70 6c 61 79 65 alue.** is playe
11a2f 64 20 62 61 63 6b 2e 20 49 66 20 6f 6e 65 20 69 d back. If one i
11a30 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 74 s encountered it
11a31 20 69 73 20 73 69 6d 70 6c 79 20 73 6b 69 70 70 is simply skipp
11a32 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
11a33 74 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 t pagerPlaybackS
11a34 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a avepoint(Pager *
11a35 70 50 61 67 65 72 2c 20 50 61 67 65 72 53 61 76 pPager, PagerSav
11a36 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 epoint *pSavepoi
11a37 6e 74 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 nt){. i64 szJ;
11a38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a39 2f 2a 20 45 66 66 65 63 74 69 76 65 20 73 69 7a /* Effective siz
11a3a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f e of the main jo
11a3b 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 urnal */. i64 i
11a3c 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 HdrOff;
11a3d 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 /* End of fi
11a3e 72 73 74 20 73 65 67 6d 65 6e 74 20 6f 66 20 6d rst segment of m
11a3f 61 69 6e 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f ain-journal reco
11a40 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 rds */. int rc
11a41 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
11a42 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
11a43 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 44 */. Bitvec *pD
11a44 6f 6e 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f one = 0; /
11a45 2a 20 42 69 74 76 65 63 20 74 6f 20 65 6e 73 75 * Bitvec to ensu
11a46 72 65 20 70 61 67 65 73 20 70 6c 61 79 65 64 20 re pages played
11a47 62 61 63 6b 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a back only once *
11a48 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 /.. assert( pPa
11a49 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
11a4a 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 2f R_SHARED );.. /
11a4b 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 69 74 * Allocate a bit
11a4c 76 65 63 20 74 6f 20 75 73 65 20 74 6f 20 73 74 vec to use to st
11a4d 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 70 ore the set of p
11a4e 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b ages rolled back
11a4f 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 */. if( pSavep
11a50 6f 69 6e 74 20 29 7b 0a 20 20 20 20 70 44 6f 6e oint ){. pDon
11a51 65 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 e = sqlite3Bitve
11a52 63 43 72 65 61 74 65 28 70 53 61 76 65 70 6f 69 cCreate(pSavepoi
11a53 6e 74 2d 3e 6e 4f 72 69 67 29 3b 0a 20 20 20 20 nt->nOrig);.
11a54 69 66 28 20 21 70 44 6f 6e 65 20 29 7b 0a 20 20 if( !pDone ){.
11a55 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
11a56 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
11a57 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 }.. /* Set the
11a58 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 62 database size b
11a59 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c 75 65 ack to the value
11a5a 20 69 74 20 77 61 73 20 62 65 66 6f 72 65 20 74 it was before t
11a5b 68 65 20 73 61 76 65 70 6f 69 6e 74 20 0a 20 20 he savepoint .
11a5c 2a 2a 20 62 65 69 6e 67 20 72 65 76 65 72 74 65 ** being reverte
11a5d 64 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 20 20 d was opened..
11a5e 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 */. pPager->dbS
11a5f 69 7a 65 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 ize = pSavepoint
11a60 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e ? pSavepoint->n
11a61 4f 72 69 67 20 3a 20 70 50 61 67 65 72 2d 3e 64 Orig : pPager->d
11a62 62 4f 72 69 67 53 69 7a 65 3b 0a 0a 20 20 2f 2a bOrigSize;.. /*
11a63 20 55 73 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75 Use pPager->jou
11a64 72 6e 61 6c 4f 66 66 20 61 73 20 74 68 65 20 65 rnalOff as the e
11a65 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 ffective size of
11a66 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 the main rollba
11a67 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e ck. ** journal.
11a68 20 20 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c The actual fil
11a69 65 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 e might be large
11a6a 72 20 74 68 61 6e 20 74 68 69 73 20 69 6e 0a 20 r than this in.
11a6b 20 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ** PAGER_JOURNA
11a6c 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 6f LMODE_TRUNCATE o
11a6d 72 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d r PAGER_JOURNALM
11a6e 4f 44 45 5f 50 45 52 53 49 53 54 2e 20 20 42 75 ODE_PERSIST. Bu
11a6f 74 20 61 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20 t anything. **
11a70 70 61 73 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75 past pPager->jou
11a71 72 6e 61 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c rnalOff is off-l
11a72 69 6d 69 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a imits to us.. *
11a73 2f 0a 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 /. szJ = pPager
11a74 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 ->journalOff;..
11a75 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 72 6f 6c /* Begin by rol
11a76 6c 69 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 ling back record
11a77 73 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 s from the main
11a78 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 journal starting
11a79 20 61 74 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 at. ** PagerSa
11a7a 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 vepoint.iOffset
11a7b 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 and continuing t
11a7c 6f 20 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e o the next journ
11a7d 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 al header.. **
11a7e 54 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 72 There might be r
11a7f 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 6d 61 ecords in the ma
11a80 69 6e 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 in journal that
11a81 68 61 76 65 20 61 20 70 61 67 65 20 6e 75 6d 62 have a page numb
11a82 65 72 0a 20 20 2a 2a 20 67 72 65 61 74 65 72 20 er. ** greater
11a83 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 than the current
11a84 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 28 database size (
11a85 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 20 pPager->dbSize)
11a86 62 75 74 20 74 68 6f 73 65 0a 20 20 2a 2a 20 77 but those. ** w
11a87 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 20 61 ill be skipped a
11a88 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 50 utomatically. P
11a89 61 67 65 73 20 61 72 65 20 61 64 64 65 64 20 74 ages are added t
11a8a 6f 20 70 44 6f 6e 65 20 61 73 20 74 68 65 79 0a o pDone as they.
11a8b 20 20 2a 2a 20 61 72 65 20 70 6c 61 79 65 64 20 ** are played
11a8c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 back.. */. if(
11a8d 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 pSavepoint ){.
11a8e 20 20 20 69 48 64 72 4f 66 66 20 3d 20 70 53 61 iHdrOff = pSa
11a8f 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 vepoint->iHdrOff
11a90 73 65 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 set ? pSavepoint
11a91 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3a 20 73 ->iHdrOffset : s
11a92 7a 4a 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e zJ;. pPager->
11a93 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 70 53 61 journalOff = pSa
11a94 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 66 73 65 74 vepoint->iOffset
11a95 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d ;. while( rc=
11a96 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 =SQLITE_OK && pP
11a97 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11a98 3c 69 48 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 <iHdrOff ){.
11a99 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
11a9a 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 yback_one_page(p
11a9b 50 61 67 65 72 2c 20 31 2c 20 30 2c 20 26 70 50 Pager, 1, 0, &pP
11a9c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11a9d 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 , 1, pDone);.
11a9e 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 }. assert( r
11a9f 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 c!=SQLITE_DONE )
11aa0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
11aa1 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11aa2 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a f = 0;. }.. /*
11aa3 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e Continue rollin
11aa4 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 6f g back records o
11aa5 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a ut of the main j
11aa6 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 ournal starting
11aa7 61 74 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 at. ** the firs
11aa8 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
11aa9 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e seen and contin
11aaa 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 65 uing until the e
11aab 66 66 65 63 74 69 76 65 20 65 6e 64 0a 20 20 2a ffective end. *
11aac 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f * of the main jo
11aad 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f 6e urnal file. Con
11aae 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f 75 tinue to skip ou
11aaf 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 t-of-range pages
11ab0 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e and. ** contin
11ab1 75 65 20 61 64 64 69 6e 67 20 70 61 67 65 73 20 ue adding pages
11ab2 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20 70 rolled back to p
11ab3 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 Done.. */. whi
11ab4 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
11ab5 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 K && pPager->jou
11ab6 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 rnalOff<szJ ){.
11ab7 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 u32 ii;
11ab8 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
11ab9 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 unter */. u32
11aba 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 20 nJRec = 0;
11abb 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 /* Number of Jou
11abc 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a rnal Records */.
11abd 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 u32 dummy;.
11abe 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 rc = readJour
11abf 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 73 nalHdr(pPager, s
11ac0 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d zJ, &nJRec, &dum
11ac1 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 my);. assert(
11ac2 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc!=SQLITE_DONE
11ac3 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 );.. /*.
11ac4 2a 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e ** The "pPager->
11ac5 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e journalHdr+JOURN
11ac6 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
11ac7 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e )==pPager->journ
11ac8 61 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 alOff". ** te
11ac9 73 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f st is related to
11aca 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20 ticket #2565.
11acb 53 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69 See the discussi
11acc 6f 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a on in the. **
11acd 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
11ace 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 ) function for a
11acf 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
11ad0 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ation.. */.
11ad1 20 20 61 73 73 65 72 74 28 20 21 28 6e 4a 52 65 assert( !(nJRe
11ad2 63 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 c==0. &&
11ad3 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11ad4 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f Hdr+JOURNAL_HDR_
11ad5 53 5a 28 70 50 61 67 65 72 29 21 3d 70 50 61 67 SZ(pPager)!=pPag
11ad6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 er->journalOff.
11ad7 20 20 20 20 20 20 20 20 26 26 20 28 28 73 7a 4a && ((szJ
11ad8 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e - pPager->journ
11ad9 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c alOff) / JOURNAL
11ada 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3e _PG_SZ(pPager))>
11adb 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 61 0. && pa
11adc 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 61 gerNextJournalPa
11add 67 65 49 73 56 61 6c 69 64 28 70 50 61 67 65 72 geIsValid(pPager
11ade 29 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 )). );. if
11adf 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20 20 ( nJRec==0 .
11ae0 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
11ae1 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 nalHdr+JOURNAL_H
11ae2 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 DR_SZ(pPager)==p
11ae3 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11ae4 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e f. ){. n
11ae5 4a 52 65 63 20 3d 20 28 75 33 32 29 28 28 73 7a JRec = (u32)((sz
11ae6 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 J - pPager->jour
11ae7 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 4c 5f nalOff)/JOURNAL_
11ae8 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a PG_SZ(pPager));.
11ae9 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 }. for(ii
11aea 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f =0; rc==SQLITE_O
11aeb 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63 20 26 26 K && ii<nJRec &&
11aec 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11aed 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 7b 0a Off<szJ; ii++){.
11aee 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
11aef 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 _playback_one_pa
11af0 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20 30 2c ge(pPager, 1, 0,
11af1 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 &pPager->journa
11af2 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29 3b lOff, 1, pDone);
11af3 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
11af4 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f t( rc!=SQLITE_DO
11af5 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 NE );. }. asse
11af6 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f rt( rc!=SQLITE_O
11af7 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 K || pPager->jou
11af8 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 3b 0a rnalOff==szJ );.
11af9 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 20 . /* Finally,
11afa 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 73 20 66 rollback pages f
11afb 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 rom the sub-jour
11afc 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 61 74 20 nal. Page that
11afd 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 76 69 6f were. ** previo
11afe 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b usly rolled back
11aff 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e out of the main
11b00 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61 72 journal (and ar
11b01 65 20 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e 65 e hence in pDone
11b02 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 ). ** will be s
11b03 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d 6f 66 2d kipped. Out-of-
11b04 72 61 6e 67 65 20 70 61 67 65 73 20 61 72 65 20 range pages are
11b05 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e 0a 20 20 also skipped..
11b06 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f */. if( pSavepo
11b07 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 32 20 69 int ){. u32 i
11b08 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a i; /*
11b09 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
11b0a 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 . i64 offset
11b0b 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 = pSavepoint->iS
11b0c 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d ubRec*(4+pPager-
11b0d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 >pageSize);.
11b0e 66 6f 72 28 69 69 3d 70 53 61 76 65 70 6f 69 6e for(ii=pSavepoin
11b0f 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 63 3d 3d t->iSubRec; rc==
11b10 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c SQLITE_OK && ii<
11b11 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b pPager->nSubRec;
11b12 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 ii++){. as
11b13 73 65 72 74 28 20 6f 66 66 73 65 74 3d 3d 69 69 sert( offset==ii
11b14 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 *(4+pPager->page
11b15 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 20 20 72 Size) );. r
11b16 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 c = pager_playba
11b17 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 ck_one_page(pPag
11b18 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66 66 73 65 er, 0, 0, &offse
11b19 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 t, 1, pDone);.
11b1a 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
11b1b 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc!=SQLITE_DONE
11b1c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 );. }.. sqlite
11b1d 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
11b1e 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72 63 3d Done);. if( rc=
11b1f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11b20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11b21 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a lOff = szJ;. }.
11b22 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11b23 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
11b24 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
11b25 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 of in-memory pag
11b26 65 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f es that are allo
11b27 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 wed..*/.SQLITE_P
11b28 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
11b29 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 te3PagerSetCache
11b2a 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 size(Pager *pPag
11b2b 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b er, int mxPage){
11b2c 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
11b2d 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 SetCachesize(pPa
11b2e 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d 78 ger->pPCache, mx
11b2f 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Page);.}../*.**
11b30 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73 Adjust the robus
11b31 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61 74 tness of the dat
11b32 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20 abase to damage
11b33 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 due to OS crashe
11b34 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 s.** or power fa
11b35 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67 69 ilures by changi
11b36 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ng the number of
11b37 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77 syncs()s when w
11b38 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f riting.** the ro
11b39 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 llback journal.
11b3a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 There are three
11b3b 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20 levels:.**.**
11b3c 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69 OFF sqli
11b3d 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e te3OsSync() is n
11b3e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68 ever called. Th
11b3f 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75 6c is is the defaul
11b40 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
11b41 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 for temporary
11b42 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69 and transient fi
11b43 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f les..**.** NO
11b44 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72 RMAL The jour
11b45 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e nal is synced on
11b46 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 ce before writes
11b47 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a begin on the.**
11b48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 da
11b49 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 69 73 tabase. This is
11b4a 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61 normally adequa
11b4b 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62 te protection, b
11b4c 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ut.**
11b4d 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 65 74 it is theoret
11b4e 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c ically possible,
11b4f 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c though very unl
11b50 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20 ikely,.**
11b51 20 20 20 20 20 20 20 74 68 61 74 20 61 6e 20 69 that an i
11b52 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20 nopertune power
11b53 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 failure could le
11b54 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a ave the journal.
11b55 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
11b56 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 63 68 in a state which
11b57 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d would cause dam
11b58 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 age to the datab
11b59 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ase.**
11b5a 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 20 72 when it is r
11b5b 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a olled back..**.*
11b5c 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54 * FULL T
11b5d 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 he journal is sy
11b5e 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 6f 72 nced twice befor
11b5f 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f e writes begin o
11b60 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 n the.**
11b61 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28 database (
11b62 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69 with some additi
11b63 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
11b64 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c - the nRec fiel
11b65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 d.**
11b66 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c of the journal
11b67 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20 header - being
11b68 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65 written in betwe
11b69 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20 en the two.**
11b6a 20 20 20 20 20 20 20 20 20 20 20 73 79 6e 63 73 syncs
11b6b 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d 65 ). If we assume
11b6c 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a that writing a.
11b6d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
11b6e 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 single disk sect
11b6f 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68 or is atomic, th
11b70 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f en this mode pro
11b71 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 vides.**
11b72 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 65 20 assurance
11b73 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c that the journal
11b74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72 will not be cor
11b75 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a rupted to the.**
11b76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f po
11b77 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64 int of causing d
11b78 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 amage to the dat
11b79 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c abase during rol
11b7a 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d lback..**.** Num
11b7b 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f eric values asso
11b7c 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 73 ciated with thes
11b7d 65 20 73 74 61 74 65 73 20 61 72 65 20 4f 46 46 e states are OFF
11b7e 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a ==1, NORMAL=2,.*
11b7f 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f * and FULL=3..*/
11b80 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
11b81 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d OMIT_PAGER_PRAGM
11b82 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 AS.SQLITE_PRIVAT
11b83 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
11b84 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 gerSetSafetyLeve
11b85 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c l(Pager *pPager,
11b86 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 int level, int
11b87 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 70 bFullFsync){. p
11b88 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 Pager->noSync =
11b89 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 50 (level==1 || pP
11b8a 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 ager->tempFile)
11b8b 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e ?1:0;. pPager->
11b8c 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76 65 fullSync = (leve
11b8d 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 72 2d l==3 && !pPager-
11b8e 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b >tempFile) ?1:0;
11b8f 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f . pPager->sync_
11b90 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46 73 flags = (bFullFs
11b91 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f ync?SQLITE_SYNC_
11b92 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e 43 FULL:SQLITE_SYNC
11b93 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 20 _NORMAL);. if(
11b94 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 pPager->noSync )
11b95 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
11b96 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a c = 0;.}.#endif.
11b97 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
11b98 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 wing global vari
11b99 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e able is incremen
11b9a 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 ted whenever the
11b9b 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 65 library.** atte
11b9c 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 74 mpts to open a t
11b9d 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 emporary file.
11b9e 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e This information
11b9f 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 is used for.**
11ba0 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c testing and anal
11ba1 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a ysis only. .*/.
11ba2 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
11ba3 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
11ba4 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 t sqlite3_opente
11ba5 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 mp_count = 0;.#e
11ba6 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e ndif../*.** Open
11ba7 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c a temporary fil
11ba8 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 e..**.** Write t
11ba9 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
11baa 6f 72 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e 20 or into *pFile.
11bab 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
11bac 20 6f 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a 20 on success .**
11bad 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 or some other er
11bae 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 ror code if we f
11baf 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c ail. The OS will
11bb0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a automatically .
11bb1 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 74 65 ** delete the te
11bb2 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 77 68 65 mporary file whe
11bb3 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a n it is closed..
11bb4 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 **.** The flags
11bb5 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 56 46 passed to the VF
11bb6 53 20 6c 61 79 65 72 20 78 4f 70 65 6e 28 29 20 S layer xOpen()
11bb7 63 61 6c 6c 20 61 72 65 20 74 68 6f 73 65 20 73 call are those s
11bb8 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 70 pecified.** by p
11bb9 61 72 61 6d 65 74 65 72 20 76 66 73 46 6c 61 67 arameter vfsFlag
11bba 73 20 4f 52 65 64 20 77 69 74 68 20 74 68 65 20 s ORed with the
11bbb 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
11bbc 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
11bbd 5f 52 45 41 44 57 52 49 54 45 0a 2a 2a 20 20 20 _READWRITE.**
11bbe 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 SQLITE_OPEN_CR
11bbf 45 41 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 EATE.** SQLI
11bc0 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
11bc1 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f E.** SQLITE_
11bc2 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
11bc3 53 45 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 SE.*/.static int
11bc4 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 0a pagerOpentemp(.
11bc5 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c Pager *pPager,
11bc6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
11bc7 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ager object */.
11bc8 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
11bc9 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 20 File, /* Write
11bca 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
11bcb 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e tor here */. in
11bcc 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 t vfsFlags
11bcd 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 /* Flags pas
11bce 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 sed through to t
11bcf 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 he VFS */.){. i
11bd0 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
11bd1 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
11bd2 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 ode */..#ifdef S
11bd3 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c QLITE_TEST. sql
11bd4 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f ite3_opentemp_co
11bd5 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 20 unt++; /* Used
11bd6 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 for testing and
11bd7 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a 2f analysis only */
11bd8 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 46 6c .#endif.. vfsFl
11bd9 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f ags |= SQLITE_O
11bda 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 PEN_READWRITE |
11bdb 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
11bdc 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 TE |.
11bdd 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
11bde 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45 5f LUSIVE | SQLITE_
11bdf 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
11be0 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 SE;. rc = sqlit
11be1 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d e3OsOpen(pPager-
11be2 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c >pVfs, 0, pFile,
11be3 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a 20 vfsFlags, 0);.
11be4 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
11be5 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e ITE_OK || isOpen
11be6 28 70 46 69 6c 65 29 20 29 3b 0a 20 20 72 65 74 (pFile) );. ret
11be7 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
11be8 20 53 65 74 20 74 68 65 20 62 75 73 79 20 68 61 Set the busy ha
11be9 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0a ndler function..
11bea 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 **.** The pager
11beb 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73 79 invokes the busy
11bec 2d 68 61 6e 64 6c 65 72 20 69 66 20 73 71 6c 69 -handler if sqli
11bed 74 65 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74 75 te3OsLock() retu
11bee 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 rns .** SQLITE_B
11bef 55 53 59 20 77 68 65 6e 20 74 72 79 69 6e 67 20 USY when trying
11bf0 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 to upgrade from
11bf1 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48 41 no-lock to a SHA
11bf2 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72 20 RED lock,.** or
11bf3 77 68 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 when trying to u
11bf4 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45 pgrade from a RE
11bf5 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 SERVED lock to a
11bf6 6e 20 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a 20 n EXCLUSIVE .**
11bf7 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a 6e lock. It does *n
11bf8 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 ot* invoke the b
11bf9 75 73 79 20 68 61 6e 64 6c 65 72 20 77 68 65 6e usy handler when
11bfa 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 0a upgrading from.
11bfb 2a 2a 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 ** SHARED to RES
11bfc 45 52 56 45 44 2c 20 6f 72 20 77 68 65 6e 20 75 ERVED, or when u
11bfd 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 20 53 48 pgrading from SH
11bfe 41 52 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 ARED to EXCLUSIV
11bff 45 0a 2a 2a 20 28 77 68 69 63 68 20 6f 63 63 75 E.** (which occu
11c00 72 73 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f rs during hot-jo
11c01 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e urnal rollback).
11c02 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a 20 Summary:.**.**
11c03 20 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 20 Transition
11c04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c05 20 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 42 | Invokes xB
11c06 75 73 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20 20 usyHandler.**
11c07 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11c08 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11c09 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11c0a 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e 4f --------.** NO
11c0b 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20 53 _LOCK -> S
11c0c 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 20 HARED_LOCK
11c0d 7c 20 59 65 73 0a 2a 2a 20 20 20 53 48 41 52 45 | Yes.** SHARE
11c0e 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53 45 D_LOCK -> RESE
11c0f 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20 4e RVED_LOCK | N
11c10 6f 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f o.** SHARED_LO
11c11 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55 53 49 56 CK -> EXCLUSIV
11c12 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a 2a E_LOCK | No.**
11c13 20 20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b RESERVED_LOCK
11c14 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f -> EXCLUSIVE_LO
11c15 43 4b 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a 2a CK | Yes.**.**
11c16 20 49 66 20 74 68 65 20 62 75 73 79 2d 68 61 6e If the busy-han
11c17 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 dler callback re
11c18 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 turns non-zero,
11c19 74 68 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 the lock is .**
11c1a 72 65 74 72 69 65 64 2e 20 49 66 20 69 74 20 72 retried. If it r
11c1b 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 eturns zero, the
11c1c 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55 53 n the SQLITE_BUS
11c1d 59 20 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72 65 Y error is.** re
11c1e 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 turned to the ca
11c1f 6c 6c 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 ller of the page
11c20 72 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a r API function..
11c21 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
11c22 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
11c23 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 gerSetBusyhandle
11c24 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 r(. Pager *pPag
11c25 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 er,
11c26 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
11c27 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 er object */. i
11c28 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 nt (*xBusyHandle
11c29 72 29 28 76 6f 69 64 20 2a 29 2c 20 20 20 20 20 r)(void *),
11c2a 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
11c2b 6f 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66 o busy-handler f
11c2c 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 unction */. voi
11c2d 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 d *pBusyHandlerA
11c2e 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 rg
11c2f 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
11c30 20 70 61 73 73 20 74 6f 20 78 42 75 73 79 48 61 pass to xBusyHa
11c31 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 20 0a 20 20 ndler */.){ .
11c32 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e pPager->xBusyHan
11c33 64 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e 64 dler = xBusyHand
11c34 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 ler;. pPager->p
11c35 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d BusyHandlerArg =
11c36 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 pBusyHandlerArg
11c37 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
11c38 68 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 72 he reinitializer
11c39 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e for this pager.
11c3a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 If not NULL, th
11c3b 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 72 0a e reinitializer.
11c3c 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 ** is called whe
11c3d 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 n the content of
11c3e 20 61 20 70 61 67 65 20 69 6e 20 63 61 63 68 65 a page in cache
11c3f 20 69 73 20 6d 6f 64 69 66 69 65 64 20 28 72 65 is modified (re
11c40 73 74 6f 72 65 64 29 0a 2a 2a 20 61 73 20 70 61 stored).** as pa
11c41 72 74 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 rt of a transact
11c42 69 6f 6e 20 6f 72 20 73 61 76 65 70 6f 69 6e 74 ion or savepoint
11c43 20 72 6f 6c 6c 62 61 63 6b 2e 20 54 68 65 20 63 rollback. The c
11c44 61 6c 6c 62 61 63 6b 20 67 69 76 65 73 20 0a 2a allback gives .*
11c45 2a 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 63 * higher-level c
11c46 6f 64 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 ode an opportuni
11c47 74 79 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 ty to restore th
11c48 65 20 45 58 54 52 41 20 73 65 63 74 69 6f 6e 20 e EXTRA section
11c49 74 6f 20 0a 2a 2a 20 61 67 72 65 65 20 77 69 74 to .** agree wit
11c4a 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 70 h the restored p
11c4b 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c age data..*/.SQL
11c4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
11c4d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
11c4e 52 65 69 6e 69 74 65 72 28 50 61 67 65 72 20 2a Reiniter(Pager *
11c4f 70 50 61 67 65 72 2c 20 76 6f 69 64 20 28 2a 78 pPager, void (*x
11c50 52 65 69 6e 69 74 29 28 44 62 50 61 67 65 2a 29 Reinit)(DbPage*)
11c51 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65 ){. pPager->xRe
11c52 69 6e 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74 initer = xReinit
11c53 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
11c54 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 e the page size
11c55 75 73 65 64 20 62 79 20 74 68 65 20 50 61 67 65 used by the Page
11c56 72 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 65 r object. The ne
11c57 77 20 70 61 67 65 20 73 69 7a 65 20 0a 2a 2a 20 w page size .**
11c58 69 73 20 70 61 73 73 65 64 20 69 6e 20 2a 70 50 is passed in *pP
11c59 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 ageSize..**.** I
11c5a 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 f the pager is i
11c5b 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 n the error stat
11c5c 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 e when this func
11c5d 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 tion is called,
11c5e 69 74 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 it.** is a no-op
11c5f 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 . The value retu
11c60 72 6e 65 64 20 69 73 20 74 68 65 20 65 72 72 6f rned is the erro
11c61 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f r state error co
11c62 64 65 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 de (i.e. .** one
11c63 20 6f 66 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 of SQLITE_IOERR
11c64 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 , SQLITE_CORRUPT
11c65 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 or SQLITE_FULL)
11c66 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ..**.** Otherwis
11c67 65 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 e, if all of the
11c68 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 following are t
11c69 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 rue:.**.** * t
11c6a 68 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 he new page size
11c6b 20 28 76 61 6c 75 65 20 6f 66 20 2a 70 50 61 67 (value of *pPag
11c6c 65 53 69 7a 65 29 20 69 73 20 76 61 6c 69 64 20 eSize) is valid
11c6d 28 61 20 70 6f 77 65 72 20 0a 2a 2a 20 20 20 20 (a power .**
11c6e 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 of two between
11c6f 35 31 32 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 512 and SQLITE_M
11c70 41 58 5f 50 41 47 45 5f 53 49 5a 45 2c 20 69 6e AX_PAGE_SIZE, in
11c71 63 6c 75 73 69 76 65 29 2c 20 61 6e 64 0a 2a 2a clusive), and.**
11c72 0a 2a 2a 20 20 20 2a 20 74 68 65 72 65 20 61 72 .** * there ar
11c73 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 e no outstanding
11c74 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 page references
11c75 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 , and.**.** *
11c76 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
11c77 65 69 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e either not an in
11c78 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
11c79 20 6f 72 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 or it is.**
11c7a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
11c7b 74 61 62 61 73 65 20 74 68 61 74 20 63 75 72 72 tabase that curr
11c7c 65 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73 20 6f ently consists o
11c7d 66 20 7a 65 72 6f 20 70 61 67 65 73 2e 0a 2a 2a f zero pages..**
11c7e 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 .** then the pag
11c7f 65 72 20 6f 62 6a 65 63 74 20 70 61 67 65 20 73 er object page s
11c80 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 2a 70 ize is set to *p
11c81 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 PageSize..**.**
11c82 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 If the page size
11c83 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65 is changed, the
11c84 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
11c85 75 73 65 73 20 73 71 6c 69 74 65 33 50 61 67 65 uses sqlite3Page
11c86 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 6f rMalloc() .** to
11c87 20 6f 62 74 61 69 6e 20 61 20 6e 65 77 20 50 61 obtain a new Pa
11c88 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 ger.pTmpSpace bu
11c89 66 66 65 72 2e 20 49 66 20 74 68 69 73 20 61 6c ffer. If this al
11c8a 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 location attempt
11c8b 20 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 .** fails, SQLI
11c8c 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 TE_NOMEM is retu
11c8d 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 rned and the pag
11c8e 65 20 73 69 7a 65 20 72 65 6d 61 69 6e 73 20 75 e size remains u
11c8f 6e 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 6e nchanged. .** In
11c90 20 61 6c 6c 20 6f 74 68 65 72 20 63 61 73 65 73 all other cases
11c91 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
11c92 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 eturned..**.** I
11c93 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 f the page size
11c94 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2c 20 is not changed,
11c95 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20 6f either because o
11c96 6e 65 20 6f 66 20 74 68 65 20 65 6e 75 6d 65 72 ne of the enumer
11c97 61 74 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f ated.** conditio
11c98 6e 73 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 ns above is not
11c99 74 72 75 65 2c 20 74 68 65 20 70 61 67 65 72 20 true, the pager
11c9a 77 61 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61 was in error sta
11c9b 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 te when this.**
11c9c 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c function was cal
11c9d 6c 65 64 2c 20 6f 72 20 62 65 63 61 75 73 65 20 led, or because
11c9e 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
11c9f 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 66 61 ation attempt fa
11ca0 69 6c 65 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 2a iled, .** then *
11ca1 70 50 61 67 65 53 69 7a 65 20 69 73 20 73 65 74 pPageSize is set
11ca2 20 74 6f 20 74 68 65 20 6f 6c 64 2c 20 72 65 74 to the old, ret
11ca3 61 69 6e 65 64 20 70 61 67 65 20 73 69 7a 65 20 ained page size
11ca4 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
11ca5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11ca6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
11ca7 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
11ca8 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 75 Pager *pPager, u
11ca9 31 36 20 2a 70 50 61 67 65 53 69 7a 65 29 7b 0a 16 *pPageSize){.
11caa 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 65 int rc = pPage
11cab 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 69 66 r->errCode;. if
11cac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
11cad 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65 53 ){. u16 pageS
11cae 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a 65 ize = *pPageSize
11caf 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 ;. assert( pa
11cb0 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 61 geSize==0 || (pa
11cb1 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 geSize>=512 && p
11cb2 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f ageSize<=SQLITE_
11cb3 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20 29 MAX_PAGE_SIZE) )
11cb4 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 69 ;. if( pageSi
11cb5 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 3d ze && pageSize!=
11cb6 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11cb7 20 0a 20 20 20 20 20 26 26 20 28 70 50 61 67 65 . && (pPage
11cb8 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 r->memDb==0 || p
11cb9 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 Pager->dbSize==0
11cba 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 ). && sqlite
11cbb 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
11cbc 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
11cbd 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 ==0 . ){.
11cbe 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20 28 char *pNew = (
11cbf 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 char *)sqlite3Pa
11cc0 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 7a geMalloc(pageSiz
11cc1 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 e);. if( !p
11cc2 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 New ){. r
11cc3 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
11cc4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
11cc5 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 pager_res
11cc6 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 et(pPager);.
11cc7 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 pPager->page
11cc8 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b Size = pageSize;
11cc9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
11cca 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 2d PageFree(pPager-
11ccb 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 >pTmpSpace);.
11ccc 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d pPager->pTm
11ccd 70 53 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20 pSpace = pNew;.
11cce 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 sqlite3Pc
11ccf 61 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 acheSetPageSize(
11cd0 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
11cd1 20 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 pageSize);.
11cd2 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 }. }. *p
11cd3 50 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 PageSize = (u16)
11cd4 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11cd5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
11cd6 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
11cd7 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
11cd8 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79 20 70 the "temporary p
11cd9 61 67 65 22 20 62 75 66 66 65 72 20 68 65 6c 64 age" buffer held
11cda 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 internally.** b
11cdb 79 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 y the pager. Th
11cdc 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 74 is is a buffer t
11cdd 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67 hat is big enoug
11cde 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a h to hold the.**
11cdf 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 entire content
11ce0 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 of a database pa
11ce1 67 65 2e 20 20 54 68 69 73 20 62 75 66 66 65 72 ge. This buffer
11ce2 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 is used interna
11ce3 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f lly.** during ro
11ce4 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 llback and will
11ce5 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 be overwritten w
11ce6 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 henever a rollba
11ce7 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 ck.** occurs. B
11ce8 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 ut other modules
11ce9 20 61 72 65 20 66 72 65 65 20 74 6f 20 75 73 65 are free to use
11cea 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 it too, as long
11ceb 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61 as.** no rollba
11cec 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e cks are happenin
11ced 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
11cee 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
11cef 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 e3PagerTempSpace
11cf0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
11cf1 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 . return pPager
11cf2 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a ->pTmpSpace;.}..
11cf3 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f /*.** Attempt to
11cf4 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d set the maximum
11cf5 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63 database page c
11cf6 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69 ount if mxPage i
11cf7 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20 s positive. .**
11cf8 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 Make no changes
11cf9 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72 if mxPage is zer
11cfa 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20 o or negative.
11cfb 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75 63 65 And never reduce
11cfc 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 the.** maximum
11cfd 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77 page count below
11cfe 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a the current siz
11cff 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
11d00 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c e..**.** Regardl
11d01 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 ess of mxPage, r
11d02 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e eturn the curren
11d03 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 t maximum page c
11d04 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ount..*/.SQLITE_
11d05 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
11d06 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 te3PagerMaxPageC
11d07 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 ount(Pager *pPag
11d08 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b er, int mxPage){
11d09 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20 . if( mxPage>0
11d0a 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d ){. pPager->m
11d0b 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a xPgno = mxPage;.
11d0c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 }. sqlite3Pag
11d0d 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
11d0e 65 72 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e er, 0);. return
11d0f 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b pPager->mxPgno;
11d10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f .}../*.** The fo
11d11 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 llowing set of r
11d12 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
11d13 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20 to disable the
11d14 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f simulated.** I/O
11d15 20 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d error mechanism
11d16 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
11d17 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 76 s are used to av
11d18 6f 69 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a oid simulated.**
11d19 20 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 errors in place
11d1a 73 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f s where we do no
11d1b 74 20 63 61 72 65 20 61 62 6f 75 74 20 65 72 72 t care about err
11d1c 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 ors..**.** Unles
11d1d 73 20 2d 44 53 51 4c 49 54 45 5f 54 45 53 54 3d s -DSQLITE_TEST=
11d1e 31 20 69 73 20 75 73 65 64 2c 20 74 68 65 73 65 1 is used, these
11d1f 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c routines are al
11d20 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 l no-ops.** and
11d21 67 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 generate no code
11d22 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
11d23 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
11d24 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 PI extern int sq
11d25 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
11d26 65 6e 64 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 ending;.SQLITE_A
11d27 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 PI extern int sq
11d28 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
11d29 69 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 it;.static int s
11d2a 61 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 aved_cnt;.void d
11d2b 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 isable_simulated
11d2c 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 _io_errors(void)
11d2d 7b 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 {. saved_cnt =
11d2e 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
11d2f 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 _pending;. sqli
11d30 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
11d31 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 ding = -1;.}.voi
11d32 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 d enable_simulat
11d33 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 ed_io_errors(voi
11d34 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f d){. sqlite3_io
11d35 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d _error_pending =
11d36 20 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 saved_cnt;.}.#e
11d37 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 lse.# define dis
11d38 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
11d39 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 o_errors().# def
11d3a 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c ine enable_simul
11d3b 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
11d3c 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
11d3d 65 61 64 20 74 68 65 20 66 69 72 73 74 20 4e 20 ead the first N
11d3e 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 bytes from the b
11d3f 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
11d40 66 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 file into memory
11d41 0a 2a 2a 20 74 68 61 74 20 70 44 65 73 74 20 70 .** that pDest p
11d42 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a oints to. .**.**
11d43 20 49 66 20 74 68 65 20 70 61 67 65 72 20 77 61 If the pager wa
11d44 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 s opened on a tr
11d45 61 6e 73 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 ansient file (zF
11d46 69 6c 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 ilename==""), or
11d47 0a 2a 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 .** opened on a
11d48 66 69 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 4e file less than N
11d49 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 bytes in size,
11d4a 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
11d4b 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 r is.** zeroed a
11d4c 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
11d4d 75 72 6e 65 64 2e 20 54 68 65 20 72 61 74 69 6f urned. The ratio
11d4e 6e 61 6c 65 20 66 6f 72 20 74 68 69 73 20 69 73 nale for this is
11d4f 20 74 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 that this .** f
11d50 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
11d51 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 to read database
11d52 20 68 65 61 64 65 72 73 2c 20 61 6e 64 20 61 20 headers, and a
11d53 6e 65 77 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 new transient or
11d54 0a 2a 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 .** zero sized d
11d55 61 74 61 62 61 73 65 20 68 61 73 20 61 20 68 65 atabase has a he
11d56 61 64 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 ader than consis
11d57 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a ts entirely of z
11d58 65 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 eroes..**.** If
11d59 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61 any IO error apa
11d5a 72 74 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 rt from SQLITE_I
11d5b 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 OERR_SHORT_READ
11d5c 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a is encountered,.
11d5d 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 ** the error cod
11d5e 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f e is returned to
11d5f 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 the caller and
11d60 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
11d61 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 the.** output bu
11d62 66 66 65 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a ffer undefined..
11d63 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
11d64 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
11d65 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 erReadFileheader
11d66 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
11d67 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 int N, unsigned
11d68 63 68 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 char *pDest){.
11d69 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
11d6a 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 OK;. memset(pDe
11d6b 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73 st, 0, N);. ass
11d6c 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
11d6d 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 er->fd) || pPage
11d6e 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 r->tempFile );.
11d6f 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
11d70 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 49 er->fd) ){. I
11d71 4f 54 52 41 43 45 28 28 22 44 42 48 44 52 20 25 OTRACE(("DBHDR %
11d72 70 20 30 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 p 0 %d\n", pPage
11d73 72 2c 20 4e 29 29 0a 20 20 20 20 72 63 20 3d 20 r, N)). rc =
11d74 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 sqlite3OsRead(pP
11d75 61 67 65 72 2d 3e 66 64 2c 20 70 44 65 73 74 2c ager->fd, pDest,
11d76 20 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 N, 0);. if(
11d77 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc==SQLITE_IOERR
11d78 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 _SHORT_READ ){.
11d79 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
11d7a 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 _OK;. }. }.
11d7b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11d7c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
11d7d 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
11d7e 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
11d7f 61 62 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 abase file assoc
11d80 69 61 74 65 64 20 0a 2a 2a 20 77 69 74 68 20 70 iated .** with p
11d81 50 61 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c Pager. Normally,
11d82 20 74 68 69 73 20 69 73 20 63 61 6c 63 75 6c 61 this is calcula
11d83 74 65 64 20 61 73 20 28 3c 64 62 20 66 69 6c 65 ted as (<db file
11d84 20 73 69 7a 65 3e 2f 3c 70 61 67 65 2d 73 69 7a size>/<page-siz
11d85 65 3e 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c e>)..** However,
11d86 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 if the file is
11d87 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 3c 70 between 1 and <p
11d88 61 67 65 2d 73 69 7a 65 3e 20 62 79 74 65 73 20 age-size> bytes
11d89 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a in size, then .*
11d8a 2a 20 74 68 69 73 20 69 73 20 63 6f 6e 73 69 64 * this is consid
11d8b 65 72 65 64 20 61 20 31 20 70 61 67 65 20 66 69 ered a 1 page fi
11d8c 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 le..**.** If the
11d8d 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 pager is in err
11d8e 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 or state when th
11d8f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
11d90 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a alled, then the.
11d91 2a 2a 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 ** error state e
11d92 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
11d93 75 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67 urned and *pnPag
11d94 65 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 e left unchanged
11d95 2e 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 . Or,.** if the
11d96 66 69 6c 65 20 73 79 73 74 65 6d 20 68 61 73 20 file system has
11d97 74 6f 20 62 65 20 71 75 65 72 69 65 64 20 66 6f to be queried fo
11d98 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 r the size of th
11d99 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 e file and.** th
11d9a 65 20 71 75 65 72 79 20 61 74 74 65 6d 70 74 20 e query attempt
11d9b 72 65 74 75 72 6e 73 20 61 6e 20 49 4f 20 65 72 returns an IO er
11d9c 72 6f 72 2c 20 74 68 65 20 49 4f 20 65 72 72 6f ror, the IO erro
11d9d 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
11d9e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 ed.** and *pnPag
11d9f 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e e is left unchan
11da0 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 ged..**.** Other
11da1 77 69 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 wise, if everyth
11da2 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 ing is successfu
11da3 6c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f l, then SQLITE_O
11da4 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a K is returned.**
11da5 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20 and *pnPage is
11da6 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
11da7 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
11da8 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 e database..*/.S
11da9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
11daa 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 t sqlite3PagerPa
11dab 67 65 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 gecount(Pager *p
11dac 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 Pager, int *pnPa
11dad 67 65 29 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 ge){. Pgno nPag
11dae 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
11daf 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 /* Value to ret
11db0 75 72 6e 20 76 69 61 20 2a 70 6e 50 61 67 65 20 urn via *pnPage
11db1 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 */.. /* If the
11db2 70 61 67 65 72 20 69 73 20 61 6c 72 65 61 64 79 pager is already
11db3 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 in the error st
11db4 61 74 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 ate, return the
11db5 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 error code. */.
11db6 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
11db7 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 Code ){. retu
11db8 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f rn pPager->errCo
11db9 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 de;. }.. /* De
11dba 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 termine the numb
11dbb 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
11dbc 68 65 20 66 69 6c 65 2e 20 53 74 6f 72 65 20 74 he file. Store t
11dbd 68 69 73 20 69 6e 20 6e 50 61 67 65 2e 20 2a 2f his in nPage. */
11dbe 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 . if( pPager->d
11dbf 62 53 69 7a 65 56 61 6c 69 64 20 29 7b 0a 20 20 bSizeValid ){.
11dc0 20 20 6e 50 61 67 65 20 3d 20 70 50 61 67 65 72 nPage = pPager
11dc1 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 ->dbSize;. }els
11dc2 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20 e{. int rc;
11dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11dc4 2a 20 45 72 72 6f 72 20 72 65 74 75 72 6e 65 64 * Error returned
11dc5 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 by OsFileSize()
11dc6 20 2a 2f 0a 20 20 20 20 69 36 34 20 6e 20 3d 20 */. i64 n =
11dc7 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
11dc8 2f 2a 20 46 69 6c 65 20 73 69 7a 65 20 69 6e 20 /* File size in
11dc9 62 79 74 65 73 20 72 65 74 75 72 6e 65 64 20 62 bytes returned b
11dca 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a y OsFileSize() *
11dcb 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 /.. assert( i
11dcc 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
11dcd 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d ) || pPager->tem
11dce 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 pFile );. if(
11dcf 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
11dd0 66 64 29 20 26 26 20 28 30 20 21 3d 20 28 72 63 fd) && (0 != (rc
11dd1 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
11dd2 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c Size(pPager->fd,
11dd3 20 26 6e 29 29 29 20 29 7b 0a 20 20 20 20 20 20 &n))) ){.
11dd4 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
11dd5 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 72 er, rc);. r
11dd6 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
11dd7 20 20 20 20 69 66 28 20 6e 3e 30 20 26 26 20 6e if( n>0 && n
11dd8 3c 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a <pPager->pageSiz
11dd9 65 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 e ){. nPage
11dda 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 1;. }else{
11ddb 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 . nPage = (
11ddc 50 67 6e 6f 29 28 6e 20 2f 20 70 50 61 67 65 72 Pgno)(n / pPager
11ddd 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
11dde 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 }. if( pPage
11ddf 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f r->state!=PAGER_
11de0 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 UNLOCK ){.
11de1 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
11de2 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 nPage;. pP
11de3 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 ager->dbFileSize
11de4 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 = nPage;.
11de5 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
11de6 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 lid = 1;. }.
11de7 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
11de8 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f current number o
11de9 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 f pages in the f
11dea 69 6c 65 20 69 73 20 67 72 65 61 74 65 72 20 74 ile is greater t
11deb 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a 20 63 6f han the . ** co
11dec 6e 66 69 67 75 72 65 64 20 6d 61 78 69 6d 75 6d nfigured maximum
11ded 20 70 61 67 65 72 20 6e 75 6d 62 65 72 2c 20 69 pager number, i
11dee 6e 63 72 65 61 73 65 20 74 68 65 20 61 6c 6c 6f ncrease the allo
11def 77 65 64 20 6c 69 6d 69 74 20 73 6f 0a 20 20 2a wed limit so. *
11df0 2a 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 * that the file
11df1 63 61 6e 20 62 65 20 72 65 61 64 2e 0a 20 20 2a can be read.. *
11df2 2f 0a 20 20 69 66 28 20 6e 50 61 67 65 3e 70 50 /. if( nPage>pP
11df3 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a ager->mxPgno ){.
11df4 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 pPager->mxPg
11df5 6e 6f 20 3d 20 28 50 67 6e 6f 29 6e 50 61 67 65 no = (Pgno)nPage
11df6 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 ;. }.. /* Set
11df7 74 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 the output varia
11df8 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 ble and return S
11df9 51 4c 49 54 45 5f 4f 4b 20 2a 2f 0a 20 20 69 66 QLITE_OK */. if
11dfa 28 20 70 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 ( pnPage ){.
11dfb 2a 70 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b *pnPage = nPage;
11dfc 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
11dfd 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
11dfe 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e ** Try to obtain
11dff 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 a lock of type
11e00 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 74 68 65 20 locktype on the
11e01 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
11e02 66 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f f.** a similar o
11e03 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 r greater lock i
11e04 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 s already held,
11e05 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
11e06 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74 a no-op.** (ret
11e07 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b urning SQLITE_OK
11e08 20 69 6d 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a immediately)..*
11e09 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 *.** Otherwise,
11e0a 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 attempt to obtai
11e0b 6e 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 n the lock using
11e0c 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 sqlite3OsLock()
11e0d 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65 . Invoke .** the
11e0e 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 busy callback i
11e0f 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 63 75 f the lock is cu
11e10 72 72 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 rrently not avai
11e11 6c 61 62 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a lable. Repeat .*
11e12 2a 20 75 6e 74 69 6c 20 74 68 65 20 62 75 73 79 * until the busy
11e13 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
11e14 73 20 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c s false or until
11e15 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 the attempt to
11e16 0a 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6c .** obtain the l
11e17 6f 63 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a ock succeeds..**
11e18 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
11e19 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 E_OK on success
11e1a 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
11e1b 65 20 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f e if we cannot o
11e1c 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 btain.** the loc
11e1d 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 k. If the lock i
11e1e 73 20 6f 62 74 61 69 6e 65 64 20 73 75 63 63 65 s obtained succe
11e1f 73 73 66 75 6c 6c 79 2c 20 73 65 74 20 74 68 65 ssfully, set the
11e20 20 50 61 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a Pager.state .**
11e21 20 76 61 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63 variable to loc
11e22 6b 74 79 70 65 20 62 65 66 6f 72 65 20 72 65 74 ktype before ret
11e23 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 urning..*/.stati
11e24 63 20 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 c int pager_wait
11e25 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a _on_lock(Pager *
11e26 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b pPager, int lock
11e27 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b type){. int rc;
11e28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11e29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11e2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
11e2b 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 . /* The OS loc
11e2c 6b 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 k values must be
11e2d 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
11e2e 20 50 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 Pager lock valu
11e2f 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 es */. assert(
11e30 50 41 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 PAGER_SHARED==SH
11e31 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
11e32 73 73 65 72 74 28 20 50 41 47 45 52 5f 52 45 53 ssert( PAGER_RES
11e33 45 52 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f ERVED==RESERVED_
11e34 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
11e35 28 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 ( PAGER_EXCLUSIV
11e36 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 E==EXCLUSIVE_LOC
11e37 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 K );.. /* If th
11e38 65 20 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e e file is curren
11e39 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 tly unlocked the
11e3a 6e 20 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20 n the size must
11e3b 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 be unknown */.
11e3c 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
11e3d 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 state>=PAGER_SHA
11e3e 52 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 RED || pPager->d
11e3f 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b bSizeValid==0 );
11e40 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
11e41 74 20 74 68 69 73 20 69 73 20 65 69 74 68 65 72 t this is either
11e42 20 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61 75 73 a no-op (becaus
11e43 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 e the requested
11e44 6c 6f 63 6b 20 69 73 20 0a 20 20 2a 2a 20 61 6c lock is . ** al
11e45 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 72 20 6f ready held, or o
11e46 6e 65 20 6f 66 20 74 68 65 20 74 72 61 6e 73 69 ne of the transi
11e47 73 74 69 6f 6e 73 20 74 68 61 74 20 74 68 65 20 stions that the
11e48 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 20 20 2a busy-handler. *
11e49 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b 65 64 * may be invoked
11e4a 20 64 75 72 69 6e 67 2c 20 61 63 63 6f 72 64 69 during, accordi
11e4b 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d 65 6e ng to the commen
11e4c 74 20 61 62 6f 76 65 0a 20 20 2a 2a 20 73 71 6c t above. ** sql
11e4d 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 ite3PagerSetBusy
11e4e 68 61 6e 64 6c 65 72 28 29 2e 0a 20 20 2a 2f 0a handler().. */.
11e4f 20 20 61 73 73 65 72 74 28 20 28 70 50 61 67 65 assert( (pPage
11e50 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 r->state>=lockty
11e51 70 65 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 pe). || (p
11e52 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 Pager->state==PA
11e53 47 45 52 5f 55 4e 4c 4f 43 4b 20 26 26 20 6c 6f GER_UNLOCK && lo
11e54 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 53 48 cktype==PAGER_SH
11e55 41 52 45 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 ARED). ||
11e56 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d (pPager->state==
11e57 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 26 PAGER_RESERVED &
11e58 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45 & locktype==PAGE
11e59 52 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 29 R_EXCLUSIVE). )
11e5a 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d ;.. if( pPager-
11e5b 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 >state>=locktype
11e5c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
11e5d 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
11e5e 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 . do {.
11e5f 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f rc = sqlite3OsLo
11e60 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c ck(pPager->fd, l
11e61 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 ocktype);. }w
11e62 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
11e63 5f 42 55 53 59 20 26 26 20 70 50 61 67 65 72 2d _BUSY && pPager-
11e64 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50 >xBusyHandler(pP
11e65 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c ager->pBusyHandl
11e66 65 72 41 72 67 29 20 29 3b 0a 20 20 20 20 69 66 erArg) );. if
11e67 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
11e68 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
11e69 3e 73 74 61 74 65 20 3d 20 28 75 38 29 6c 6f 63 >state = (u8)loc
11e6a 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 49 4f 54 ktype;. IOT
11e6b 52 41 43 45 28 28 22 4c 4f 43 4b 20 25 70 20 25 RACE(("LOCK %p %
11e6c 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 6c 6f d\n", pPager, lo
11e6d 63 6b 74 79 70 65 29 29 0a 20 20 20 20 7d 0a 20 cktype)). }.
11e6e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
11e6f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 }../*.** Truncat
11e70 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 e the in-memory
11e71 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6d database file im
11e72 61 67 65 20 74 6f 20 6e 50 61 67 65 20 70 61 67 age to nPage pag
11e73 65 73 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e es. This .** fun
11e74 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 ction does not a
11e75 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 ctually modify t
11e76 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11e77 20 6f 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a on disk. It .**
11e78 20 6a 75 73 74 20 73 65 74 73 20 74 68 65 20 69 just sets the i
11e79 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 nternal state of
11e7a 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 the pager objec
11e7b 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 0a 2a t so that the .*
11e7c 2a 20 74 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c * truncation wil
11e7d 6c 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 l be done when t
11e7e 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
11e7f 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
11e80 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
11e81 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
11e82 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 te3PagerTruncate
11e83 49 6d 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 Image(Pager *pPa
11e84 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 ger, Pgno nPage)
11e85 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 {. assert( pPag
11e86 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
11e87 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
11e88 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 ger->dbSize>=nPa
11e89 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ge );. assert(
11e8a 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
11e8b 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b AGER_RESERVED );
11e8c 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a . pPager->dbSiz
11e8d 65 20 3d 20 6e 50 61 67 65 3b 0a 7d 0a 0a 2f 2a e = nPage;.}../*
11e8e 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 .** Shutdown the
11e8f 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 page cache. Fr
11e90 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e ee all memory an
11e91 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 d close all file
11e92 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 s..**.** If a tr
11e93 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e ansaction was in
11e94 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 progress when t
11e95 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
11e96 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 alled, that.** t
11e97 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f ransaction is ro
11e98 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 lled back. All
11e99 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
11e9a 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 s are invalidate
11e9b 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d d.** and their m
11e9c 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 emory is freed.
11e9d 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 Any attempt to
11e9e 75 73 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 use a page assoc
11e9f 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 iated.** with th
11ea0 69 73 20 70 61 67 65 20 63 61 63 68 65 20 61 66 is page cache af
11ea1 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f ter this functio
11ea2 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c n returns will l
11ea3 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 ikely.** result
11ea4 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a in a coredump..*
11ea5 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
11ea6 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 on always succee
11ea7 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 ds. If a transac
11ea8 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 tion is active a
11ea9 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 n attempt.** is
11eaa 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 made to roll it
11eab 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f back. If an erro
11eac 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 r occurs during
11ead 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a the rollback .**
11eae 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d a hot journal m
11eaf 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 ay be left in th
11eb0 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 e filesystem but
11eb1 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 no error is ret
11eb2 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 urned.** to the
11eb3 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 caller..*/.SQLIT
11eb4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
11eb5 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 lite3PagerClose(
11eb6 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11eb7 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 disable_simula
11eb8 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b ted_io_errors();
11eb9 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 . sqlite3BeginB
11eba 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
11ebb 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11ebc 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e = 0;. pPager->
11ebd 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 exclusiveMode =
11ebe 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65 74 0;. pager_reset
11ebf 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 (pPager);. if(
11ec0 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 61 67 MEMDB ){. pag
11ec1 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 er_unlock(pPager
11ec2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
11ec3 2f 2a 20 53 65 74 20 50 61 67 65 72 2e 6a 6f 75 /* Set Pager.jou
11ec4 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 20 66 6f rnalHdr to -1 fo
11ec5 72 20 74 68 65 20 62 65 6e 65 66 69 74 20 6f 66 r the benefit of
11ec6 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 79 62 the pager_playb
11ec7 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 ack() . ** ca
11ec8 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 ll which may be
11ec9 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 68 69 6e made from within
11eca 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 pagerUnlockAndR
11ecb 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 20 69 74 ollback(). If it
11ecc 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 2d . ** is not -
11ecd 31 2c 20 74 68 65 6e 20 74 68 65 20 75 6e 73 79 1, then the unsy
11ece 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 nced portion of
11ecf 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 an open journal
11ed0 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 2a 2a 20 file may. **
11ed1 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 69 be played back i
11ed2 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
11ed3 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 . If a power fai
11ed4 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c lure occurs whil
11ed5 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 e. ** this is
11ed6 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 68 65 20 happening, the
11ed7 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65 63 database may bec
11ed8 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 20 ome corrupt..
11ed9 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e */. pPager->
11eda 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 2d 31 3b journalHdr = -1;
11edb 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b . pagerUnlock
11edc 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 AndRollback(pPag
11edd 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 er);. }. sqlit
11ede 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
11edf 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73 69 c();. enable_si
11ee0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
11ee1 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 s();. PAGERTRAC
11ee2 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c E(("CLOSE %d\n",
11ee3 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
11ee4 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 ));. IOTRACE(("
11ee5 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50 61 CLOSE %p\n", pPa
11ee6 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65 33 4f ger)). sqlite3O
11ee7 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 sClose(pPager->f
11ee8 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 d);. sqlite3Pag
11ee9 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 eFree(pPager->pT
11eea 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 mpSpace);. sqli
11eeb 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 70 te3PcacheClose(p
11eec 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b Pager->pPCache);
11eed 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 .. assert( !pPa
11eee 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 ger->aSavepoint
11eef 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a && !pPager->pInJ
11ef0 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 ournal );. asse
11ef1 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 rt( !isOpen(pPag
11ef2 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 4f er->jfd) && !isO
11ef3 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 pen(pPager->sjfd
11ef4 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f ) );.. sqlite3_
11ef5 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 free(pPager);.
11ef6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
11ef7 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 ;.}..#if !define
11ef8 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 d(NDEBUG) || def
11ef9 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
11efa 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 )./*.** Return t
11efb 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 he page number f
11efc 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a or page pPg..*/.
11efd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 SQLITE_PRIVATE P
11efe 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 gno sqlite3Pager
11eff 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67 Pagenumber(DbPag
11f00 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 e *pPg){. retur
11f01 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 n pPg->pgno;.}.#
11f02 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 endif../*.** Inc
11f03 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 rement the refer
11f04 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 ence count for p
11f05 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49 age pPg..*/.SQLI
11f06 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
11f07 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 sqlite3PagerRef(
11f08 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
11f09 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
11f0a 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (pPg);.}../*.**
11f0b 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c Sync the journal
11f0c 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 . In other words
11f0d 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 , make sure all
11f0e 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68 the pages that h
11f0f 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 ave.** been writ
11f10 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ten to the journ
11f11 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79 al have actually
11f12 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72 reached the sur
11f13 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 face of the.** d
11f14 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 20 72 isk and can be r
11f15 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 65 estored in the e
11f16 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d 6a 6f vent of a hot-jo
11f17 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a urnal rollback..
11f18 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 61 67 **.** If the Pag
11f19 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 er.needSync flag
11f1a 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 68 65 is not set, the
11f1b 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
11f1c 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f is a.** no-op. O
11f1d 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 63 therwise, the ac
11f1e 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 64 tions required d
11f1f 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75 epend on the jou
11f20 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64 rnal-mode.** and
11f21 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61 72 the device char
11f22 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 acteristics of t
11f23 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 he the file-syst
11f24 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a em, as follows:.
11f25 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 **.** * If the
11f26 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
11f27 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f an in-memory jo
11f28 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61 urnal file, no a
11f29 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20 ction need.**
11f2a 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a be taken..**.*
11f2b 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65 2c * * Otherwise,
11f2c 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20 64 if the device d
11f2d 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 oes not support
11f2e 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44 20 the SAFE_APPEND
11f2f 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20 property,.**
11f30 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66 then the nRec f
11f31 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 ield of the most
11f32 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74 65 recently writte
11f33 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 n journal header
11f34 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61 74 .** is updat
11f35 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 ed to contain th
11f36 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72 e number of jour
11f37 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61 74 nal records that
11f38 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 65 have.** bee
11f39 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77 n written follow
11f3a 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20 70 ing it. If the p
11f3b 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69 6e ager is operatin
11f3c 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a g in full-sync.*
11f3d 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e * mode, then
11f3e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11f3f 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66 6f e is synced befo
11f40 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69 73 re this field is
11f41 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 updated..**.**
11f42 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69 63 * If the devic
11f43 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f e does not suppo
11f44 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49 41 rt the SEQUENTIA
11f45 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65 6e L property, then
11f46 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c .** journal
11f47 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e file is synced.
11f48 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73 .**.** Or, in ps
11f49 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a eudo-code:.**.**
11f4a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d if( NOT <in-m
11f4b 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29 emory journal> )
11f4c 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 {.** if( NOT
11f4d 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a SAFE_APPEND ){.
11f4e 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66 75 ** if( <fu
11f4f 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20 ll-sync mode> )
11f50 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 xSync(<journal f
11f51 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20 ile>);.**
11f52 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69 65 <update nRec fie
11f53 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a ld>.** } .**
11f54 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45 51 if( NOT SEQ
11f55 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 28 UENTIAL ) xSync(
11f56 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b <journal file>);
11f57 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 .** }.**.** Th
11f58 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 e Pager.needSync
11f59 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 20 62 flag is never b
11f5a 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 6f 72 e set for tempor
11f5b 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 61 6e ary files, or an
11f5c 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 61 74 y.** file operat
11f5d 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d ing in no-sync m
11f5e 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 79 6e ode (Pager.noSyn
11f5f 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 c set to non-zer
11f60 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 o)..**.** If suc
11f61 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 72 6f cessful, this ro
11f62 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65 utine clears the
11f63 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
11f64 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 20 0a flag of every .
11f65 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c ** page currentl
11f66 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 y held in memory
11f67 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
11f68 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 g SQLITE_OK. If
11f69 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 69 an IO.** error i
11f6a 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 s encountered, t
11f6b 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 hen the IO error
11f6c 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
11f6d 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e d to the caller.
11f6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
11f6f 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 yncJournal(Pager
11f70 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 *pPager){. if(
11f71 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
11f72 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
11f73 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 !pPager->tempFi
11f74 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 le );. if( pP
11f75 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
11f76 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e!=PAGER_JOURNAL
11f77 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 MODE_MEMORY ){.
11f78 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 int rc;
11f79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11f7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
11f7b 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20 urn code */.
11f7c 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 63 20 const int iDc
11f7d 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 = sqlite3OsDevic
11f7e 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
11f7f 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 (pPager->fd);.
11f80 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 assert( isOp
11f81 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
11f82 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d );.. if( 0=
11f83 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 =(iDc&SQLITE_IOC
11f84 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20 AP_SAFE_APPEND)
11f85 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 ){. /* Va
11f86 72 69 61 62 6c 65 20 69 4e 52 65 63 4f 66 66 73 riable iNRecOffs
11f87 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 et is set to the
11f88 20 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20 6a offset in the j
11f89 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 20 20 ournal file.
11f8a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 52 ** of the nR
11f8b 65 63 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 ec field of the
11f8c 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 77 72 most recently wr
11f8d 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 itten journal he
11f8e 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2a ader.. **
11f8f 20 54 68 69 73 20 66 69 65 6c 64 20 77 69 6c 6c This field will
11f90 20 62 65 20 75 70 64 61 74 65 64 20 66 6f 6c 6c be updated foll
11f91 6f 77 69 6e 67 20 74 68 65 20 78 53 79 6e 63 28 owing the xSync(
11f92 29 20 6f 70 65 72 61 74 69 6f 6e 0a 20 20 20 20 ) operation.
11f93 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6a 6f ** on the jo
11f94 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 urnal file. */.
11f95 20 20 20 20 20 20 20 69 36 34 20 69 4e 52 65 63 i64 iNRec
11f96 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d Offset = pPager-
11f97 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 2b 20 73 69 >journalHdr + si
11f98 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11f99 69 63 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a ic);.. /*
11f9a 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c This block deal
11f9b 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72 s with an obscur
11f9c 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 e problem. If th
11f9d 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f e last connectio
11f9e 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 n. ** tha
11f9f 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20 t wrote to this
11fa0 64 61 74 61 62 61 73 65 20 77 61 73 20 6f 70 65 database was ope
11fa1 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73 rating in persis
11fa2 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 tent-journal.
11fa3 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 ** mode, th
11fa4 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
11fa5 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73 20 ile may at this
11fa6 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 point actually b
11fa7 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20 e larger.
11fa8 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a ** than Pager.j
11fa9 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e ournalOff bytes.
11faa 20 49 66 20 74 68 65 20 6e 65 78 74 20 74 68 69 If the next thi
11fab 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ng in the journa
11fac 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c l. ** fil
11fad 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 e happens to be
11fae 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 a journal-header
11faf 20 28 77 72 69 74 74 65 6e 20 61 73 20 70 61 72 (written as par
11fb0 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 t of the.
11fb1 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e ** previous con
11fb2 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 nections transac
11fb3 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 tion), and a cra
11fb4 73 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c sh or power-fail
11fb5 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ure . **
11fb6 6f 63 63 75 72 73 20 61 66 74 65 72 20 6e 52 65 occurs after nRe
11fb7 63 20 69 73 20 75 70 64 61 74 65 64 20 62 75 74 c is updated but
11fb8 20 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f 6e before this con
11fb9 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 0a nection writes .
11fba 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 ** anyth
11fbb 69 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65 20 ing else to the
11fbc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 journal file (or
11fbd 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 commits/rolls b
11fbe 61 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20 20 ack its .
11fbf 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 ** transaction)
11fc0 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 , then SQLite ma
11fc1 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 y become confuse
11fc2 64 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 d when doing the
11fc3 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 . ** hot
11fc4 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 -journal rollbac
11fc5 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f k following reco
11fc6 76 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c very. It may rol
11fc7 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 l back all.
11fc8 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f ** of this co
11fc9 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 nnections data,
11fca 74 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 then proceed to
11fcb 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 rolling back the
11fcc 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a old,. **
11fcd 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 out-of-date dat
11fce 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 a that follows i
11fcf 74 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 t. Database corr
11fd0 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 uption..
11fd1 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 6f **. ** To
11fd2 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 work around thi
11fd3 73 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 s, if the journa
11fd4 6c 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70 65 l file does appe
11fd5 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 ar to contain.
11fd6 20 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 ** a valid
11fd7 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e header followin
11fd8 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f g Pager.journalO
11fd9 66 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 ff, then write a
11fda 20 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 0x00. **
11fdb 20 62 79 74 65 20 74 6f 20 74 68 65 20 73 74 61 byte to the sta
11fdc 72 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65 76 rt of it to prev
11fdd 65 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69 6e ent it from bein
11fde 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 g recognized..
11fdf 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
11fe0 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 65 ** Variable iNe
11fe1 78 74 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 xtHdrOffset is s
11fe2 65 74 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 et to the offset
11fe3 20 61 74 20 77 68 69 63 68 20 74 68 69 73 0a 20 at which this.
11fe4 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 ** proble
11fe5 6d 61 74 69 63 20 68 65 61 64 65 72 20 77 69 6c matic header wil
11fe6 6c 20 6f 63 63 75 72 2c 20 69 66 20 69 74 20 65 l occur, if it e
11fe7 78 69 73 74 73 2e 20 61 4d 61 67 69 63 20 69 73 xists. aMagic is
11fe8 20 75 73 65 64 20 0a 20 20 20 20 20 20 20 20 2a used . *
11fe9 2a 20 61 73 20 61 20 74 65 6d 70 6f 72 61 72 79 * as a temporary
11fea 20 62 75 66 66 65 72 20 74 6f 20 69 6e 73 70 65 buffer to inspe
11feb 63 74 20 74 68 65 20 66 69 72 73 74 20 63 6f 75 ct the first cou
11fec 70 6c 65 20 6f 66 20 62 79 74 65 73 20 6f 66 0a ple of bytes of.
11fed 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 ** the p
11fee 6f 74 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c otential journal
11fef 20 68 65 61 64 65 72 2e 0a 20 20 20 20 20 20 20 header..
11ff0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34 20 */. i64
11ff1 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20 3d iNextHdrOffset =
11ff2 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 journalHdrOffse
11ff3 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 t(pPager);.
11ff4 20 20 20 75 38 20 61 4d 61 67 69 63 5b 38 5d 3b u8 aMagic[8];
11ff5 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
11ff6 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
11ff7 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c er->jfd, aMagic,
11ff8 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 73 8, iNextHdrOffs
11ff9 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 et);. if(
11ffa 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
11ffb 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67 & 0==memcmp(aMag
11ffc 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 ic, aJournalMagi
11ffd 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20 20 c, 8) ){.
11ffe 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
11fff 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b u8 zerobyte = 0;
12000 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
12001 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
12002 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72 Pager->jfd, &zer
12003 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 48 obyte, 1, iNextH
12004 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 drOffset);.
12005 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
12006 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc!=SQLITE_OK &
12007 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 & rc!=SQLITE_IOE
12008 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b RR_SHORT_READ ){
12009 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
1200a 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
1200b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 . /* Writ
1200c 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 e the nRec value
1200d 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 into the journa
1200e 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49 l file header. I
1200f 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 f in. **
12010 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 full-synchronous
12011 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 mode, sync the
12012 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 journal first. T
12013 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 his ensures that
12014 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 . ** all
12015 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20 data has really
12016 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65 66 hit the disk bef
12017 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64 61 ore nRec is upda
12018 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 ted to mark.
12019 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63 ** it as a c
1201a 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c andidate for rol
1201b 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a lback.. *
1201c 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 *. ** Thi
1201d 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 s is not require
1201e 64 20 69 66 20 74 68 65 20 70 65 72 73 69 73 74 d if the persist
1201f 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72 ent media suppor
12020 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a ts the. *
12021 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 * SAFE_APPEND pr
12022 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 65 20 operty. Because
12023 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 20 in this case it
12024 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
12025 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 . ** for
12026 67 61 72 62 61 67 65 20 64 61 74 61 20 74 6f 20 garbage data to
12027 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 be appended to t
12028 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 he file, the nRe
12029 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20 c field.
1202a 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 ** is populated
1202b 77 69 74 68 20 30 78 46 46 46 46 46 46 46 46 20 with 0xFFFFFFFF
1202c 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c when the journal
1202d 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74 header is writt
1202e 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e en. ** an
1202f 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f d never needs to
12030 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20 be updated..
12031 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
12032 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c if( pPager->full
12033 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26 Sync && 0==(iDc&
12034 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
12035 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 UENTIAL) ){.
12036 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
12037 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 (("SYNC journal
12038 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 of %d\n", PAGERI
12039 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
1203a 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 IOTRACE((
1203b 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 "JSYNC %p\n", pP
1203c 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20 ager)).
1203d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 rc = sqlite3OsS
1203e 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ync(pPager->jfd,
1203f 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
12040 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ags);.
12041 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12042 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
12043 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12044 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 IOTRACE(("JHDR
12045 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 %p %lld %d\n", p
12046 50 61 67 65 72 2c 20 69 4e 52 65 63 4f 66 66 73 Pager, iNRecOffs
12047 65 74 2c 20 34 29 29 3b 0a 20 20 20 20 20 20 20 et, 4));.
12048 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 rc = write32bit
12049 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 s(pPager->jfd, i
1204a 4e 52 65 63 4f 66 66 73 65 74 2c 20 70 50 61 67 NRecOffset, pPag
1204b 65 72 2d 3e 6e 52 65 63 29 3b 0a 20 20 20 20 20 er->nRec);.
1204c 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1204d 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
1204e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1204f 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 if( 0==(iDc&SQLI
12050 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
12051 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 IAL) ){.
12052 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59 4e PAGERTRACE(("SYN
12053 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c C journal of %d\
12054 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
12055 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20 49 er)));. I
12056 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 OTRACE(("JSYNC %
12057 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 p\n", pPager)).
12058 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
12059 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 te3OsSync(pPager
1205a 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 ->jfd, pPager->s
1205b 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20 20 20 20 ync_flags| .
1205c 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 73 (pPager->s
1205d 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 54 ync_flags==SQLIT
1205e 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c 49 E_SYNC_FULL?SQLI
1205f 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
12060 3a 30 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 :0). );.
12061 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
12062 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
12063 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
12064 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 }.. /* The
12065 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 61 73 journal file was
12066 20 6a 75 73 74 20 73 75 63 63 65 73 73 66 75 6c just successful
12067 6c 79 20 73 79 6e 63 65 64 2e 20 53 65 74 20 50 ly synced. Set P
12068 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 ager.needSync .
12069 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f 20 61 6e ** to zero an
1206a 64 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 44 d clear the PGHD
1206b 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
1206c 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 73 2e 0a on all pagess..
1206d 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 */. pPage
1206e 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b r->needSync = 0;
1206f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
12070 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 31 3b rnalStarted = 1;
12071 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
12072 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 heClearSyncFlags
12073 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
12074 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e );. }.. return
12075 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
12076 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e *.** The argumen
12077 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 t is the first i
12078 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 n a linked list
12079 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 63 of dirty pages c
1207a 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 62 79 20 74 onnected.** by t
1207b 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79 20 he PgHdr.pDirty
1207c 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20 66 75 pointer. This fu
1207d 6e 63 74 69 6f 6e 20 77 72 69 74 65 73 20 65 61 nction writes ea
1207e 63 68 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a ch one of the.**
1207f 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 in-memory pages
12080 20 69 6e 20 74 68 65 20 6c 69 73 74 20 74 6f 20 in the list to
12081 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12082 65 2e 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 e. The argument
12083 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2c 20 may.** be NULL,
12084 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61 6e 20 representing an
12085 65 6d 70 74 79 20 6c 69 73 74 2e 20 49 6e 20 74 empty list. In t
12086 68 69 73 20 63 61 73 65 20 74 68 69 73 20 66 75 his case this fu
12087 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 20 6e nction is.** a n
12088 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 o-op..**.** The
12089 70 61 67 65 72 20 6d 75 73 74 20 68 6f 6c 64 20 pager must hold
1208a 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52 at least a RESER
1208b 56 45 44 20 6c 6f 63 6b 20 77 68 65 6e 20 74 68 VED lock when th
1208c 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 is function.** i
1208d 73 20 63 61 6c 6c 65 64 2e 20 42 65 66 6f 72 65 s called. Before
1208e 20 77 72 69 74 69 6e 67 20 61 6e 79 74 68 69 6e writing anythin
1208f 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 g to the databas
12090 65 20 66 69 6c 65 2c 20 74 68 69 73 20 6c 6f 63 e file, this loc
12091 6b 0a 2a 2a 20 69 73 20 75 70 67 72 61 64 65 64 k.** is upgraded
12092 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 to an EXCLUSIVE
12093 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f lock. If the lo
12094 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 ck cannot be obt
12095 61 69 6e 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 ained,.** SQLITE
12096 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 _BUSY is returne
12097 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 d and no data is
12098 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
12099 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1209a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 * .** If the pag
1209b 65 72 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c er is a temp-fil
1209c 65 20 70 61 67 65 72 20 61 6e 64 20 74 68 65 20 e pager and the
1209d 61 63 74 75 61 6c 20 66 69 6c 65 2d 73 79 73 74 actual file-syst
1209e 65 6d 20 66 69 6c 65 0a 2a 2a 20 69 73 20 6e 6f em file.** is no
1209f 74 20 79 65 74 20 6f 70 65 6e 2c 20 69 74 20 69 t yet open, it i
120a0 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 6f 70 s created and op
120a1 65 6e 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 ened before any
120a2 64 61 74 61 20 69 73 20 0a 2a 2a 20 77 72 69 74 data is .** writ
120a3 74 65 6e 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 4f ten out..**.** O
120a4 6e 63 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 nce the lock has
120a5 20 62 65 65 6e 20 75 70 67 72 61 64 65 64 20 61 been upgraded a
120a6 6e 64 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 nd, if necessary
120a7 2c 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 , the file opene
120a8 64 2c 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 20 d,.** the pages
120a9 61 72 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 are written out
120aa 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
120ab 66 69 6c 65 20 69 6e 20 6c 69 73 74 20 6f 72 64 file in list ord
120ac 65 72 2e 20 57 72 69 74 69 6e 67 0a 2a 2a 20 61 er. Writing.** a
120ad 20 70 61 67 65 20 69 73 20 73 6b 69 70 70 65 64 page is skipped
120ae 20 69 66 20 69 74 20 6d 65 65 74 73 20 65 69 74 if it meets eit
120af 68 65 72 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f her of the follo
120b0 77 69 6e 67 20 63 72 69 74 65 72 69 61 3a 0a 2a wing criteria:.*
120b1 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67 *.** * The pag
120b2 65 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61 e number is grea
120b3 74 65 72 20 74 68 61 6e 20 50 61 67 65 72 2e 64 ter than Pager.d
120b4 62 53 69 7a 65 2c 20 6f 72 0a 2a 2a 20 20 20 2a bSize, or.** *
120b5 20 54 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f The PGHDR_DONT_
120b6 57 52 49 54 45 20 66 6c 61 67 20 69 73 20 73 65 WRITE flag is se
120b7 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a t on the page..*
120b8 2a 0a 2a 2a 20 49 66 20 77 72 69 74 69 6e 67 20 *.** If writing
120b9 6f 75 74 20 61 20 70 61 67 65 20 63 61 75 73 65 out a page cause
120ba 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 s the database f
120bb 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 50 61 67 ile to grow, Pag
120bc 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a er.dbFileSize.**
120bd 20 69 73 20 75 70 64 61 74 65 64 20 61 63 63 6f is updated acco
120be 72 64 69 6e 67 6c 79 2e 20 49 66 20 70 61 67 65 rdingly. If page
120bf 20 31 20 69 73 20 77 72 69 74 74 65 6e 20 6f 75 1 is written ou
120c0 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 t, then the valu
120c1 65 20 63 61 63 68 65 64 0a 2a 2a 20 69 6e 20 50 e cached.** in P
120c2 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b ager.dbFileVers[
120c3 5d 20 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 ] is updated to
120c4 6d 61 74 63 68 20 74 68 65 20 6e 65 77 20 76 61 match the new va
120c5 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a lue stored in.**
120c6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
120c7 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76 65 le..**.** If eve
120c8 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 rything is succe
120c9 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b ssful, SQLITE_OK
120ca 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
120cb 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 2a 2a an IO error .**
120cc 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 occurs, an IO e
120cd 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
120ce 75 72 6e 65 64 2e 20 4f 72 2c 20 69 66 20 74 68 urned. Or, if th
120cf 65 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b e EXCLUSIVE lock
120d0 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 6f 62 cannot.** be ob
120d1 74 61 69 6e 65 64 2c 20 53 51 4c 49 54 45 5f 42 tained, SQLITE_B
120d2 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2e USY is returned.
120d3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
120d4 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c ager_write_pagel
120d5 69 73 74 28 50 67 48 64 72 20 2a 70 4c 69 73 74 ist(PgHdr *pList
120d6 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
120d7 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 er;
120d8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
120d9 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 er object */. i
120da 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
120db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
120dc 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
120dd 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4c 69 de */.. if( pLi
120de 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 st==0 ) return S
120df 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 QLITE_OK;. pPag
120e0 65 72 20 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 er = pList->pPag
120e1 65 72 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 er;.. /* At thi
120e2 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 s point there ma
120e3 79 20 62 65 20 65 69 74 68 65 72 20 61 20 52 45 y be either a RE
120e4 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 SERVED or EXCLUS
120e5 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a IVE lock on the.
120e6 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 ** database fi
120e7 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 le. If there is
120e8 61 6c 72 65 61 64 79 20 61 6e 20 45 58 43 4c 55 already an EXCLU
120e9 53 49 56 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 SIVE lock, the f
120ea 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 ollowing. ** ca
120eb 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 ll is a no-op..
120ec 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 **. ** Moving
120ed 74 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 the lock from RE
120ee 53 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53 SERVED to EXCLUS
120ef 49 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 IVE actually inv
120f0 6f 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a olves going. **
120f1 20 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 through an inte
120f2 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 20 50 rmediate state P
120f3 45 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 ENDING. A PEND
120f4 49 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 ING lock prevent
120f5 73 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 s new. ** reade
120f6 72 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e rs from attachin
120f7 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 g to the databas
120f8 65 20 62 75 74 20 69 73 20 75 6e 73 75 66 66 69 e but is unsuffi
120f9 63 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a cient for us to.
120fa 20 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65 ** write. The
120fb 20 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49 idea of a PENDI
120fc 4e 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 NG lock is to pr
120fd 65 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72 event new reader
120fe 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 s from. ** comi
120ff 6e 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77 ng in while we w
12100 61 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67 ait for existing
12101 20 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61 readers to clea
12102 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 r.. **. ** Whi
12103 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 le the pager is
12104 69 6e 20 74 68 65 20 52 45 53 45 52 56 45 44 20 in the RESERVED
12105 73 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69 state, the origi
12106 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c nal database fil
12107 65 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e e. ** is unchan
12108 67 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72 ged and we can r
12109 6f 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20 ollback without
1210a 68 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 having to playba
1210b 63 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 ck the. ** jour
1210c 6e 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 nal into the ori
1210d 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 ginal database f
1210e 69 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 ile. Once we tr
1210f 61 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a ansition to. **
12110 20 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d EXCLUSIVE, it m
12111 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 eans the databas
12112 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 e file has been
12113 63 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 changed and any
12114 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 rollback. ** wi
12115 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 ll require a jou
12116 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 rnal playback..
12117 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
12118 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
12119 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 ER_RESERVED );.
1211a 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 rc = pager_wait
1211b 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c _on_lock(pPager,
1211c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 EXCLUSIVE_LOCK)
1211d 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 ;.. /* If the f
1211e 69 6c 65 20 69 73 20 61 20 74 65 6d 70 2d 66 69 ile is a temp-fi
1211f 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 le has not yet b
12120 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e een opened, open
12121 20 69 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a it now. It. **
12122 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
12123 20 66 6f 72 20 72 63 20 74 6f 20 62 65 20 6f 74 for rc to be ot
12124 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f her than SQLITE_
12125 4f 4b 20 69 66 20 74 68 69 73 20 62 72 61 6e 63 OK if this branc
12126 68 0a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c h. ** is taken,
12127 20 61 73 20 70 61 67 65 72 5f 77 61 69 74 5f 6f as pager_wait_o
12128 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f n_lock() is a no
12129 2d 6f 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c -op for temp-fil
1212a 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 es.. */. if( !
1212b 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
1212c 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 d) ){. assert
1212d 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 ( pPager->tempFi
1212e 6c 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 le && rc==SQLITE
1212f 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 _OK );. rc =
12130 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 pagerOpentemp(pP
12131 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 ager, pPager->fd
12132 2c 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 , pPager->vfsFla
12133 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c gs);. }.. whil
12134 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e( rc==SQLITE_OK
12135 20 26 26 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 && pList ){.
12136 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 Pgno pgno = pLi
12137 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f st->pgno;.. /
12138 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 64 * If there are d
12139 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 irty pages in th
1213a 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74 e page cache wit
1213b 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67 h page numbers g
1213c 72 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 reater. ** th
1213d 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c an Pager.dbSize,
1213e 20 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 this means sqli
1213f 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 te3PagerTruncate
12140 49 6d 61 67 65 28 29 20 77 61 73 20 63 61 6c 6c Image() was call
12141 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b ed to. ** mak
12142 65 20 74 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c e the file small
12143 65 72 20 28 70 72 65 73 75 6d 61 62 6c 79 20 62 er (presumably b
12144 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f y auto-vacuum co
12145 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 de). Do not writ
12146 65 0a 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 e. ** any suc
12147 68 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 66 h pages to the f
12148 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
12149 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 ** Also, do not
1214a 77 72 69 74 65 20 6f 75 74 20 61 6e 79 20 70 61 write out any pa
1214b 67 65 20 74 68 61 74 20 68 61 73 20 74 68 65 20 ge that has the
1214c 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 PGHDR_DONT_WRITE
1214d 20 66 6c 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 flag. ** set
1214e 20 28 73 65 74 20 62 79 20 73 71 6c 69 74 65 33 (set by sqlite3
1214f 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 29 PagerDontWrite()
12150 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 ).. */. if
12151 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e ( pgno<=pPager->
12152 64 62 53 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c dbSize && 0==(pL
12153 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 ist->flags&PGHDR
12154 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a _DONT_WRITE) ){.
12155 20 20 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 i64 offset
12156 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 = (pgno-1)*(i64
12157 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a )pPager->pageSiz
12158 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 e; /* Of
12159 66 73 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f fset to write */
1215a 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 . char *pDa
1215b 74 61 20 3d 20 43 4f 44 45 43 32 28 70 50 61 67 ta = CODEC2(pPag
1215c 65 72 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 er, pList->pData
1215d 2c 20 70 67 6e 6f 2c 20 36 29 3b 20 2f 2a 20 44 , pgno, 6); /* D
1215e 61 74 61 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a ata to write */.
1215f 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 . /* Write
12160 6f 75 74 20 74 68 65 20 70 61 67 65 20 64 61 74 out the page dat
12161 61 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d a. */. rc =
12162 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
12163 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61 74 pPager->fd, pDat
12164 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 a, pPager->pageS
12165 69 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 ize, offset);..
12166 20 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 /* If page
12167 31 20 77 61 73 20 6a 75 73 74 20 77 72 69 74 74 1 was just writt
12168 65 6e 2c 20 75 70 64 61 74 65 20 50 61 67 65 72 en, update Pager
12169 2e 64 62 46 69 6c 65 56 65 72 73 20 74 6f 20 6d .dbFileVers to m
1216a 61 74 63 68 0a 20 20 20 20 20 20 2a 2a 20 74 68 atch. ** th
1216b 65 20 76 61 6c 75 65 20 6e 6f 77 20 73 74 6f 72 e value now stor
1216c 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ed in the databa
1216d 73 65 20 66 69 6c 65 2e 20 49 66 20 77 72 69 74 se file. If writ
1216e 69 6e 67 20 74 68 69 73 20 0a 20 20 20 20 20 20 ing this .
1216f 2a 2a 20 70 61 67 65 20 63 61 75 73 65 64 20 74 ** page caused t
12170 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12171 20 74 6f 20 67 72 6f 77 2c 20 75 70 64 61 74 65 to grow, update
12172 20 64 62 46 69 6c 65 53 69 7a 65 2e 20 0a 20 20 dbFileSize. .
12173 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
12174 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 pgno==1 ){.
12175 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 memcpy(&pPag
12176 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 er->dbFileVers,
12177 26 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a 65 &pData[24], size
12178 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c of(pPager->dbFil
12179 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d eVers));. }
1217a 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e . if( pgno>
1217b 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 pPager->dbFileSi
1217c 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 ze ){. pP
1217d 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 ager->dbFileSize
1217e 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d = pgno;. }
1217f 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 .. /* Updat
12180 65 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 6a e any backup obj
12181 65 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 65 ects copying the
12182 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 contents of thi
12183 73 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 20 s pager. */.
12184 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 sqlite3BackupU
12185 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 pdate(pPager->pB
12186 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 ackup, pgno, (u8
12187 20 2a 29 70 44 61 74 61 29 3b 0a 0a 20 20 20 20 *)pData);..
12188 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 PAGERTRACE(("S
12189 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 20 TORE %d page %d
1218a 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 hash(%08x)\n",.
1218b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1218c 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
1218d 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 ), pgno, pager_p
1218e 61 67 65 68 61 73 68 28 70 4c 69 73 74 29 29 29 agehash(pList)))
1218f 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 ;. IOTRACE(
12190 28 22 50 47 4f 55 54 20 25 70 20 25 64 5c 6e 22 ("PGOUT %p %d\n"
12191 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 , pPager, pgno))
12192 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e ;. PAGER_IN
12193 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 CR(sqlite3_pager
12194 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 29 3b _writedb_count);
12195 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 . PAGER_INC
12196 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 R(pPager->nWrite
12197 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
12198 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 PAGERTRACE((
12199 22 4e 4f 53 54 4f 52 45 20 25 64 20 70 61 67 65 "NOSTORE %d page
1219a 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
1219b 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 29 29 3b pPager), pgno));
1219c 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 . }.#ifdef SQ
1219d 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
1219e 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 67 65 . pList->page
1219f 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 Hash = pager_pag
121a0 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a 23 65 ehash(pList);.#e
121a1 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 20 3d ndif. pList =
121a2 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b 0a pList->pDirty;.
121a3 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
121a4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e ;.}../*.** Appen
121a5 64 20 61 20 72 65 63 6f 72 64 20 6f 66 20 74 68 d a record of th
121a6 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 e current state
121a7 6f 66 20 70 61 67 65 20 70 50 67 20 74 6f 20 74 of page pPg to t
121a8 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 he sub-journal.
121a9 0a 2a 2a 20 49 74 20 69 73 20 74 68 65 20 63 61 .** It is the ca
121aa 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 llers responsibi
121ab 6c 69 74 79 20 74 6f 20 75 73 65 20 73 75 62 6a lity to use subj
121ac 52 65 71 75 69 72 65 73 50 61 67 65 28 29 20 74 RequiresPage() t
121ad 6f 20 63 68 65 63 6b 20 0a 2a 2a 20 74 68 61 74 o check .** that
121ae 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65 it is really re
121af 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 63 61 quired before ca
121b0 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 lling this funct
121b1 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 ion..**.** If su
121b2 63 63 65 73 73 66 75 6c 2c 20 73 65 74 20 74 68 ccessful, set th
121b3 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 e bit correspond
121b4 69 6e 67 20 74 6f 20 70 50 67 2d 3e 70 67 6e 6f ing to pPg->pgno
121b5 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 73 0a in the bitvecs.
121b6 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 ** for all open
121b7 73 61 76 65 70 6f 69 6e 74 73 20 62 65 66 6f 72 savepoints befor
121b8 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a e returning..**.
121b9 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
121ba 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
121bb 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 OK if everything
121bc 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 is successful,
121bd 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 an IO.** error c
121be 6f 64 65 20 69 66 20 74 68 65 20 61 74 74 65 6d ode if the attem
121bf 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 pt to write to t
121c0 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 he sub-journal f
121c1 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c ails, or .** SQL
121c2 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d ITE_NOMEM if a m
121c3 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c alloc fails whil
121c4 65 20 73 65 74 74 69 6e 67 20 61 20 62 69 74 20 e setting a bit
121c5 69 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 0a 2a in a savepoint.*
121c6 2a 20 62 69 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 * bitvec..*/.sta
121c7 74 69 63 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e tic int subjourn
121c8 61 6c 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 alPage(PgHdr *pP
121c9 67 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 g){. int rc = S
121ca 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 QLITE_OK;. Page
121cb 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
121cc 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 69 >pPager;. if( i
121cd 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a sOpen(pPager->sj
121ce 66 64 29 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 fd) ){. void
121cf 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 *pData = pPg->pD
121d0 61 74 61 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 ata;. i64 off
121d1 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 set = pPager->nS
121d2 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d ubRec*(4+pPager-
121d3 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 >pageSize);.
121d4 63 68 61 72 20 2a 70 44 61 74 61 32 20 3d 20 43 char *pData2 = C
121d5 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 ODEC2(pPager, pD
121d6 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 ata, pPg->pgno,
121d7 37 29 3b 0a 20 20 0a 20 20 20 20 50 41 47 45 52 7);. . PAGER
121d8 54 52 41 43 45 28 28 22 53 54 4d 54 2d 4a 4f 55 TRACE(("STMT-JOU
121d9 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 5c RNAL %d page %d\
121da 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
121db 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 er), pPg->pgno))
121dc 3b 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28 ;. . assert(
121dd 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 pageInJournal(p
121de 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f Pg) || pPg->pgno
121df 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 >pPager->dbOrigS
121e0 69 7a 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ize );. rc =
121e1 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
121e2 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 73 65 74 er->sjfd, offset
121e3 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 , pPg->pgno);.
121e4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
121e5 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
121e6 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
121e7 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 70 (pPager->sjfd, p
121e8 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 Data2, pPager->p
121e9 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b ageSize, offset+
121ea 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4);. }. }.
121eb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
121ec 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d K ){. pPager-
121ed 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20 >nSubRec++;.
121ee 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
121ef 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a nSavepoint>0 );.
121f0 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 53 61 rc = addToSa
121f1 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 vepointBitvecs(p
121f2 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
121f3 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
121f4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
121f5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
121f6 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 c;.}.../*.** Thi
121f7 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
121f8 6c 6c 65 64 20 62 79 20 74 68 65 20 70 63 61 63 lled by the pcac
121f9 68 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69 74 he layer when it
121fa 20 68 61 73 20 72 65 61 63 68 65 64 20 73 6f 6d has reached som
121fb 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 e.** soft memory
121fc 20 6c 69 6d 69 74 2e 20 54 68 65 20 66 69 72 73 limit. The firs
121fd 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 t argument is a
121fe 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 pointer to a Pag
121ff 65 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61 er object.** (ca
12200 73 74 20 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 st as a void*).
12201 54 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 77 The pager is alw
12202 61 79 73 20 27 70 75 72 67 65 61 62 6c 65 27 20 ays 'purgeable'
12203 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 (not an in-memor
12204 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 2e 20 y.** database).
12205 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
12206 65 6e 74 20 69 73 20 61 20 72 65 66 65 72 65 6e ent is a referen
12207 63 65 20 74 6f 20 61 20 70 61 67 65 20 74 68 61 ce to a page tha
12208 74 20 69 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74 t is .** current
12209 6c 79 20 64 69 72 74 79 20 62 75 74 20 68 61 73 ly dirty but has
1220a 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
1220b 72 65 66 65 72 65 6e 63 65 73 2e 20 54 68 65 20 references. The
1220c 70 61 67 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 page.** is alway
1220d 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
1220e 68 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a 65 h the Pager obje
1220f 63 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ct passed as the
12210 20 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d first .** argum
12211 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a ent..**.** The j
12212 6f 62 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 ob of this funct
12213 69 6f 6e 20 69 73 20 74 6f 20 6d 61 6b 65 20 70 ion is to make p
12214 50 67 20 63 6c 65 61 6e 20 62 79 20 77 72 69 74 Pg clean by writ
12215 69 6e 67 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 ing its contents
12216 0a 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 .** out to the d
12217 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 66 atabase file, if
12218 20 70 6f 73 73 69 62 6c 65 2e 20 54 68 69 73 20 possible. This
12219 6d 61 79 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63 may involve sync
1221a 69 6e 67 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e ing the.** journ
1221b 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 al file. .**.**
1221c 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 If successful, s
1221d 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
1221e 43 6c 65 61 6e 28 29 20 69 73 20 63 61 6c 6c 65 Clean() is calle
1221f 64 20 6f 6e 20 74 68 65 20 70 61 67 65 20 61 6e d on the page an
12220 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 d.** SQLITE_OK r
12221 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 eturned. If an I
12222 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 O error occurs w
12223 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 6d hile trying to m
12224 61 6b 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 ake the.** page
12225 63 6c 65 61 6e 2c 20 74 68 65 20 49 4f 20 65 72 clean, the IO er
12226 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
12227 72 6e 65 64 2e 20 49 66 20 74 68 65 20 70 61 67 rned. If the pag
12228 65 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d e cannot be.** m
12229 61 64 65 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f ade clean for so
1222a 6d 65 20 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c me other reason,
1222b 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 but no error oc
1222c 63 75 72 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 curs, then SQLIT
1222d 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 E_OK.** is retur
1222e 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 63 ned by sqlite3Pc
1222f 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 acheMakeClean()
12230 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a is not called..*
12231 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
12232 65 72 53 74 72 65 73 73 28 76 6f 69 64 20 2a 70 erStress(void *p
12233 2c 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 , PgHdr *pPg){.
12234 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
12235 20 28 50 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 (Pager *)p;. i
12236 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
12237 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 K;.. assert( pP
12238 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 g->pPager==pPage
12239 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1223a 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
1223b 44 49 52 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 54 DIRTY );.. /* T
1223c 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 he doNotSync fla
1223d 67 20 69 73 20 73 65 74 20 62 79 20 74 68 65 20 g is set by the
1223e 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1223f 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 e() function whi
12240 6c 65 20 69 74 0a 20 20 2a 2a 20 69 73 20 6a 6f le it. ** is jo
12241 75 72 6e 61 6c 6c 69 6e 67 20 61 20 73 65 74 20 urnalling a set
12242 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 64 of two or more d
12243 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68 atabase pages th
12244 61 74 20 61 72 65 20 73 74 6f 72 65 64 0a 20 20 at are stored.
12245 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 ** on the same d
12246 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 79 6e 63 isk sector. Sync
12247 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ing the journal
12248 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 77 is not allowed w
12249 68 69 6c 65 0a 20 20 2a 2a 20 74 68 69 73 20 69 hile. ** this i
1224a 73 20 68 61 70 70 65 6e 69 6e 67 20 61 73 20 69 s happening as i
1224b 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 t is important t
1224c 68 61 74 20 61 6c 6c 20 6d 65 6d 62 65 72 73 20 hat all members
1224d 6f 66 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 of such a. ** s
1224e 65 74 20 6f 66 20 70 61 67 65 73 20 61 72 65 20 et of pages are
1224f 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 74 synced to disk t
12250 6f 67 65 74 68 65 72 2e 20 53 6f 2c 20 69 66 20 ogether. So, if
12251 74 68 65 20 70 61 67 65 20 74 68 69 73 20 66 75 the page this fu
12252 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 nction. ** is t
12253 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 63 6c rying to make cl
12254 65 61 6e 20 77 69 6c 6c 20 72 65 71 75 69 72 65 ean will require
12255 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 a journal sync
12256 61 6e 64 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e and the doNotSyn
12257 63 0a 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 c. ** flag is s
12258 65 74 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f et, return witho
12259 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e ut doing anythin
1225a 67 2e 20 54 68 65 20 70 63 61 63 68 65 20 6c 61 g. The pcache la
1225b 79 65 72 20 77 69 6c 6c 0a 20 20 2a 2a 20 6a 75 yer will. ** ju
1225c 73 74 20 68 61 76 65 20 74 6f 20 67 6f 20 61 68 st have to go ah
1225d 65 61 64 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 ead and allocate
1225e 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66 66 a new page buff
1225f 65 72 20 69 6e 73 74 65 61 64 20 6f 66 0a 20 20 er instead of.
12260 2a 2a 20 72 65 75 73 69 6e 67 20 70 50 67 2e 0a ** reusing pPg..
12261 20 20 2a 2a 0a 20 20 2a 2a 20 53 69 6d 69 6c 61 **. ** Simila
12262 72 6c 79 2c 20 69 66 20 74 68 65 20 70 61 67 65 rly, if the page
12263 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e r has already en
12264 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 tered the error
12265 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 0a 20 20 state, do not.
12266 2a 2a 20 74 72 79 20 74 6f 20 77 72 69 74 65 20 ** try to write
12267 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
12268 70 50 67 20 74 6f 20 64 69 73 6b 2e 0a 20 20 2a pPg to disk.. *
12269 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
1226a 65 72 72 43 6f 64 65 20 7c 7c 20 28 70 50 61 67 errCode || (pPag
1226b 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 26 26 er->doNotSync &&
1226c 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
1226d 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a R_NEED_SYNC) ){.
1226e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1226f 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
12270 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c Sync the journal
12271 20 66 69 6c 65 20 69 66 20 72 65 71 75 69 72 65 file if require
12272 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d d. */. if( pPg-
12273 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
12274 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 72 63 D_SYNC ){. rc
12275 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 = syncJournal(p
12276 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
12277 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
12278 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e pPager->fullSyn
12279 63 20 26 26 20 0a 20 20 20 20 20 20 21 28 70 50 c && . !(pP
1227a 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
1227b 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
1227c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26 26 0a MODE_MEMORY) &&.
1227d 20 20 20 20 20 20 21 28 73 71 6c 69 74 65 33 4f !(sqlite3O
1227e 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
1227f 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
12280 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f d)&SQLITE_IOCAP_
12281 53 41 46 45 5f 41 50 50 45 4e 44 29 0a 20 20 20 SAFE_APPEND).
12282 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
12283 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 ->nRec = 0;.
12284 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 rc = writeJour
12285 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a nalHdr(pPager);.
12286 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
12287 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 If the page numb
12288 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 er of this page
12289 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
1228a 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
1228b 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 of. ** the data
1228c 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74 20 6d base image, it m
1228d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 77 72 ay need to be wr
1228e 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 itten to the sub
1228f 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54 -journal.. ** T
12290 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 74 his is because t
12291 68 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 he call to pager
12292 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 _write_pagelist(
12293 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74 ) below will not
12294 0a 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 77 . ** actually w
12295 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 rite data to the
12296 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 file in this ca
12297 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f se.. **. ** Co
12298 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f nsider the follo
12299 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 wing sequence of
1229a 20 65 76 65 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20 events:. **.
1229b 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a ** BEGIN;. **
1229c 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 <journal pa
1229d 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 3c ge X>. ** <
1229e 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e 0a 20 modify page X>.
1229f 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e ** SAVEPOIN
122a0 54 20 73 70 3b 0a 20 20 2a 2a 20 20 20 20 20 20 T sp;. **
122a1 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62 61 73 <shrink databas
122a2 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61 67 65 e file to Y page
122a3 73 3e 0a 20 20 2a 2a 20 20 20 20 20 20 20 70 61 s>. ** pa
122a4 67 65 72 53 74 72 65 73 73 28 70 61 67 65 20 58 gerStress(page X
122a5 29 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 ). ** ROLLB
122a6 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a ACK TO sp;. **.
122a7 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 74 ** If (X>Y), t
122a8 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72 53 74 hen when pagerSt
122a9 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64 20 70 ress is called p
122aa 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62 age X will not b
122ab 65 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 6f e written. ** o
122ac 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
122ad 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c se file, but wil
122ae 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f l be dropped fro
122af 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 m the cache. The
122b0 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e n,. ** followin
122b1 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 g the "ROLLBACK
122b2 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65 6e 74 TO sp" statement
122b3 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 58 , reading page X
122b4 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 2a 2a 20 will read. **
122b5 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 data from the da
122b6 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 tabase file. Thi
122b7 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 63 6f s will be the co
122b8 70 79 20 6f 66 20 70 61 67 65 20 58 20 61 73 20 py of page X as
122b9 69 74 0a 20 20 2a 2a 20 77 61 73 20 77 68 65 6e it. ** was when
122ba 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
122bb 20 73 74 61 72 74 65 64 2c 20 6e 6f 74 20 61 73 started, not as
122bc 20 69 74 20 77 61 73 20 77 68 65 6e 20 22 53 41 it was when "SA
122bd 56 45 50 4f 49 4e 54 20 73 70 22 0a 20 20 2a 2a VEPOINT sp". **
122be 20 77 61 73 20 65 78 65 63 75 74 65 64 2e 0a 20 was executed..
122bf 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 6f 6c **. ** The sol
122c0 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 69 74 ution is to writ
122c1 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 e the current da
122c2 74 61 20 66 6f 72 20 70 61 67 65 20 58 20 69 6e ta for page X in
122c3 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 to the . ** sub
122c4 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f -journal file no
122c5 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 w (if it is not
122c6 61 6c 72 65 61 64 79 20 74 68 65 72 65 29 2c 20 already there),
122c7 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 0a so that it will.
122c8 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 65 64 ** be restored
122c9 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20 to its current
122ca 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 22 value when the "
122cb 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 ROLLBACK TO sp"
122cc 69 73 20 0a 20 20 2a 2a 20 65 78 65 63 75 74 65 is . ** execute
122cd 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 d.. */. if( rc
122ce 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
122cf 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d Pg->pgno>pPager-
122d0 3e 64 62 53 69 7a 65 20 26 26 20 73 75 62 6a 52 >dbSize && subjR
122d1 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 equiresPage(pPg)
122d2 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 75 62 ){. rc = sub
122d3 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29 journalPage(pPg)
122d4 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 ;. }.. /* Writ
122d5 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f e the contents o
122d6 66 20 74 68 65 20 70 61 67 65 20 6f 75 74 20 74 f the page out t
122d7 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
122d8 69 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 ile. */. if( rc
122d9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
122da 20 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d pPg->pDirty =
122db 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 0;. rc = pag
122dc 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 er_write_pagelis
122dd 74 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f t(pPg);. }.. /
122de 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 * Mark the page
122df 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 69 as clean. */. i
122e0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
122e1 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 ){. PAGERTRA
122e2 43 45 28 28 22 53 54 52 45 53 53 20 25 64 20 70 CE(("STRESS %d p
122e3 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 age %d\n", PAGER
122e4 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
122e5 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c >pgno));. sql
122e6 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c ite3PcacheMakeCl
122e7 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 ean(pPg);. }..
122e8 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 return pager_er
122e9 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b ror(pPager, rc);
122ea 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 .}.../*.** Alloc
122eb 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ate and initiali
122ec 7a 65 20 61 20 6e 65 77 20 50 61 67 65 72 20 6f ze a new Pager o
122ed 62 6a 65 63 74 20 61 6e 64 20 70 75 74 20 61 20 bject and put a
122ee 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a 2a pointer to it.**
122ef 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 20 54 68 in *ppPager. Th
122f0 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64 20 65 e pager should e
122f1 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 66 72 65 ventually be fre
122f2 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 ed by passing it
122f3 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 .** to sqlite3Pa
122f4 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a 2a gerClose()..**.*
122f5 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 * The zFilename
122f6 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
122f7 70 61 74 68 20 74 6f 20 74 68 65 20 64 61 74 61 path to the data
122f8 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 base file to ope
122f9 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61 n..** If zFilena
122fa 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 me is NULL then
122fb 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65 64 a randomly-named
122fc 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
122fd 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e is created.** an
122fe 64 20 75 73 65 64 20 61 73 20 74 68 65 20 66 69 d used as the fi
122ff 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64 2e le to be cached.
12300 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 Temporary files
12301 20 61 72 65 20 62 65 20 64 65 6c 65 74 65 64 0a are be deleted.
12302 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ** automatically
12303 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 63 when they are c
12304 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 6c 65 6e losed. If zFilen
12305 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a ame is ":memory:
12306 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20 69 " then .** all i
12307 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 nformation is he
12308 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 49 74 20 ld in cache. It
12309 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e is never written
1230a 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54 68 to disk. .** Th
1230b 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 is can be used t
1230c 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 o implement an i
1230d 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
1230e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 78 e..**.** The nEx
1230f 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 73 70 tra parameter sp
12310 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62 ecifies the numb
12311 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
12312 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a pace allocated.*
12313 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65 61 63 * along with eac
12314 68 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 h page reference
12315 2e 20 54 68 69 73 20 73 70 61 63 65 20 69 73 20 . This space is
12316 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 available to the
12317 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 74 68 65 user.** via the
12318 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
12319 45 78 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a 0a Extra() API..**.
1231a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 ** The flags arg
1231b 75 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f ument is used to
1231c 20 73 70 65 63 69 66 79 20 70 72 6f 70 65 72 74 specify propert
1231d 69 65 73 20 74 68 61 74 20 61 66 66 65 63 74 20 ies that affect
1231e 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e the.** operation
1231f 20 6f 66 20 74 68 65 20 70 61 67 65 72 2e 20 49 of the pager. I
12320 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 t should be pass
12321 65 64 20 73 6f 6d 65 20 62 69 74 77 69 73 65 20 ed some bitwise
12322 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f combination.** o
12323 66 20 74 68 65 20 50 41 47 45 52 5f 4f 4d 49 54 f the PAGER_OMIT
12324 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41 47 _JOURNAL and PAG
12325 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 66 ER_NO_READLOCK f
12326 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 lags..**.** The
12327 76 66 73 46 6c 61 67 73 20 70 61 72 61 6d 65 74 vfsFlags paramet
12328 65 72 20 69 73 20 61 20 62 69 74 6d 61 73 6b 20 er is a bitmask
12329 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 66 to pass to the f
1232a 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a 2a lags parameter.*
1232b 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 28 29 * of the xOpen()
1232c 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 73 method of the s
1232d 75 70 70 6c 69 65 64 20 56 46 53 20 77 68 65 6e upplied VFS when
1232e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e 20 opening files.
1232f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
12330 67 65 72 20 6f 62 6a 65 63 74 20 69 73 20 61 6c ger object is al
12331 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 located and the
12332 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 20 6f specified file o
12333 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65 73 pened .** succes
12334 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f 4f sfully, SQLITE_O
12335 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e K is returned an
12336 64 20 2a 70 70 50 61 67 65 72 20 73 65 74 20 74 d *ppPager set t
12337 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 68 o point to.** th
12338 65 20 6e 65 77 20 70 61 67 65 72 20 6f 62 6a 65 e new pager obje
12339 63 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 ct. If an error
1233a 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 67 65 72 occurs, *ppPager
1233b 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 0a is set to NULL.
1233c 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 63 6f 64 ** and error cod
1233d 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 e returned. This
1233e 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 function may re
1233f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
12340 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 6c M.** (sqlite3Mal
12341 6c 6f 63 28 29 20 69 73 20 75 73 65 64 20 74 6f loc() is used to
12342 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 allocate memory
12343 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 ), SQLITE_CANTOP
12344 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f 75 EN or .** variou
12345 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 20 s SQLITE_IO_XXX
12346 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 errors..*/.SQLIT
12347 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
12348 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 0a lite3PagerOpen(.
12349 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
1234a 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 Vfs, /* Th
1234b 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 e virtual file s
1234c 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f 0a ystem to use */.
1234d 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65 Pager **ppPage
1234e 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 r, /* OU
1234f 54 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50 61 T: Return the Pa
12350 67 65 72 20 73 74 72 75 63 74 75 72 65 20 68 65 ger structure he
12351 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 re */. const ch
12352 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
12353 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
12354 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
12355 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e open */. int n
12356 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 Extra,
12357 20 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79 74 /* Extra byt
12358 65 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61 63 es append to eac
12359 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 h in-memory page
1235a 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c */. int flags,
1235b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1235c 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 6c * flags controll
1235d 69 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a 2f ing this file */
1235e 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 . int vfsFlags
1235f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 /* f
12360 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f lags passed thro
12361 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 ugh to sqlite3_v
12362 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b fs.xOpen() */.){
12363 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 50 . u8 *pPtr;. P
12364 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 30 ager *pPager = 0
12365 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 ; /* Pager
12366 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f 63 object to alloc
12367 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 2a ate and return *
12368 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
12369 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 ITE_OK; /*
1236a 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
1236b 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 int tempFile =
1236c 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 0; /* Tru
1236d 65 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 73 e for temp files
1236e 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f 72 (incl. in-memor
1236f 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 6e y files) */. in
12370 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 20 t memDb = 0;
12371 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
12372 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d f this is an in-
12373 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a 20 memory file */.
12374 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 int readOnly =
12375 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 0; /* Tru
12376 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 72 e if this is a r
12377 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a 2f ead-only file */
12378 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 . int journalFi
12379 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 42 leSize; /* B
1237a 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 ytes to allocate
1237b 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 for each journa
1237c 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a l fd */. char *
1237d 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 zPathname = 0;
1237e 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 20 /* Full path
1237f 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 to database file
12380 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 6e */. int nPathn
12381 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f ame = 0; /
12382 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
12383 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 2a s in zPathname *
12384 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e /. int useJourn
12385 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 al = (flags & PA
12386 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c GER_OMIT_JOURNAL
12387 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 74 )==0; /* False t
12388 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 2a o omit journal *
12389 2f 0a 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c 6f /. int noReadlo
1238a 63 6b 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 ck = (flags & PA
1238b 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 29 GER_NO_READLOCK)
1238c 21 3d 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f !=0; /* True to
1238d 20 6f 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b 20 omit read-lock
1238e 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65 53 */. int pcacheS
1238f 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63 61 ize = sqlite3Pca
12390 63 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20 20 cheSize();
12391 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c /* Bytes to all
12392 6f 63 61 74 65 20 66 6f 72 20 50 43 61 63 68 65 ocate for PCache
12393 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50 61 67 65 */. u16 szPage
12394 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 Dflt = SQLITE_DE
12395 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b FAULT_PAGE_SIZE;
12396 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 67 /* Default pag
12397 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a 20 e size */.. /*
12398 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d Figure out how m
12399 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 71 uch space is req
1239a 75 69 72 65 64 20 66 6f 72 20 65 61 63 68 20 6a uired for each j
1239b 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 ournal file-hand
1239c 6c 65 0a 20 20 2a 2a 20 28 74 68 65 72 65 20 61 le. ** (there a
1239d 72 65 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c 20 re two of them,
1239e 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
1239f 20 61 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f 75 and the sub-jou
123a0 72 6e 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a 2a rnal). This. **
123a1 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 is the maximum
123a2 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 space required f
123a3 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 or an in-memory
123a4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e journal file han
123a5 64 6c 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61 20 dle . ** and a
123a6 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 20 regular journal
123a7 66 69 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f 74 file-handle. Not
123a8 65 20 74 68 61 74 20 61 20 22 72 65 67 75 6c 61 e that a "regula
123a9 72 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c 65 r journal-handle
123aa 22 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 61 20 ". ** may be a
123ab 77 72 61 70 70 65 72 20 63 61 70 61 62 6c 65 20 wrapper capable
123ac 6f 66 20 63 61 63 68 69 6e 67 20 74 68 65 20 66 of caching the f
123ad 69 72 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 irst portion of
123ae 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a the journal. **
123af 20 66 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79 20 file in memory
123b0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
123b1 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 atomic-write op
123b2 74 69 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65 20 timization (see
123b3 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20 66 69 6c . ** source fil
123b4 65 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20 20 e journal.c)..
123b5 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 */. if( sqlite3
123b6 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 JournalSize(pVfs
123b7 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 )>sqlite3MemJour
123b8 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 20 nalSize() ){.
123b9 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 journalFileSize
123ba 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 = sqlite3Journa
123bb 6c 53 69 7a 65 28 70 56 66 73 29 3b 0a 20 20 7d lSize(pVfs);. }
123bc 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 else{. journa
123bd 6c 46 69 6c 65 53 69 7a 65 20 3d 20 73 71 6c 69 lFileSize = sqli
123be 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a te3MemJournalSiz
123bf 65 28 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 e();. }.. /* S
123c0 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 et the output va
123c1 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20 69 riable to NULL i
123c2 6e 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72 20 n case an error
123c3 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70 70 occurs. */. *pp
123c4 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a Pager = 0;.. /*
123c5 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f Compute and sto
123c6 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 re the full path
123c7 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 name in an alloc
123c8 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e ated buffer poin
123c9 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a ted. ** to by z
123ca 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 Pathname, length
123cb 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 nPathname. Or,
123cc 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d if this is a tem
123cd 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a porary file,. *
123ce 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 * leave both nPa
123cf 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 thname and zPath
123d0 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 name set to 0..
123d1 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e */. if( zFilen
123d2 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 ame && zFilename
123d3 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68 [0] ){. nPath
123d4 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 name = pVfs->mxP
123d5 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a athname+1;. z
123d6 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 Pathname = sqlit
123d7 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 e3Malloc(nPathna
123d8 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a me*2);. if( z
123d9 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 Pathname==0 ){.
123da 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
123db 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
123dc 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
123dd 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 MIT_MEMORYDB.
123de 20 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c if( strcmp(zFil
123df 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22 ename,":memory:"
123e0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 )==0 ){. me
123e1 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a mDb = 1;. z
123e2 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b Pathname[0] = 0;
123e3 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 . }else.#endi
123e4 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 7a 50 f. {. zP
123e5 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 20 athname[0] = 0;
123e6 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 6e 69 /* Make sure ini
123e7 74 69 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 66 tialized even if
123e8 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 FullPathname()
123e9 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20 72 fails */. r
123ea 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c c = sqlite3OsFul
123eb 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 lPathname(pVfs,
123ec 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 zFilename, nPath
123ed 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 name, zPathname)
123ee 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50 61 ;. }.. nPa
123ef 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 thname = sqlite3
123f0 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61 Strlen30(zPathna
123f1 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d me);. if( rc=
123f2 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 50 =SQLITE_OK && nP
123f3 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d 3e athname+8>pVfs->
123f4 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 mxPathname ){.
123f5 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e /* This bran
123f6 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65 6e ch is taken when
123f7 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 74 the journal pat
123f8 68 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 20 h required by.
123f9 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 ** the datab
123fa 61 73 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 ase being opened
123fb 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 68 will be more th
123fc 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e an pVfs->mxPathn
123fd 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 ame. ** byt
123fe 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 68 es in length. Th
123ff 69 73 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 is means the dat
12400 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 abase cannot be
12401 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a opened,. **
12402 20 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 as it will not
12403 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f be possible to o
12404 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 pen the journal
12405 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 20 file or even.
12406 20 20 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 20 ** check for
12407 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 65 a hot-journal be
12408 66 6f 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 20 fore reading..
12409 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 */. rc
1240a 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 = SQLITE_CANTOPE
1240b 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 N;. }. if(
1240c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1240d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1240e 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b free(zPathname);
1240f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
12410 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
12411 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
12412 79 20 66 6f 72 20 74 68 65 20 50 61 67 65 72 20 y for the Pager
12413 73 74 72 75 63 74 75 72 65 2c 20 50 43 61 63 68 structure, PCach
12414 65 20 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20 20 e object, the.
12415 2a 2a 20 74 68 72 65 65 20 66 69 6c 65 20 64 65 ** three file de
12416 73 63 72 69 70 74 6f 72 73 2c 20 74 68 65 20 64 scriptors, the d
12417 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d atabase file nam
12418 65 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 e and the journa
12419 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61 6d l . ** file nam
1241a 65 2e 20 54 68 65 20 6c 61 79 6f 75 74 20 69 6e e. The layout in
1241b 20 6d 65 6d 6f 72 79 20 69 73 20 61 73 20 66 6f memory is as fo
1241c 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a llows:. **. **
1241d 20 20 20 20 20 50 61 67 65 72 20 6f 62 6a 65 63 Pager objec
1241e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
1241f 20 20 20 20 20 28 73 69 7a 65 6f 66 28 50 61 67 (sizeof(Pag
12420 65 72 29 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 er) bytes). **
12421 20 20 20 20 50 43 61 63 68 65 20 6f 62 6a 65 63 PCache objec
12422 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
12423 20 20 20 20 28 73 71 6c 69 74 65 33 50 63 61 63 (sqlite3Pcac
12424 68 65 53 69 7a 65 28 29 20 62 79 74 65 73 29 0a heSize() bytes).
12425 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61 73 ** Databas
12426 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 e file handle
12427 20 20 20 20 20 20 20 20 20 28 70 56 66 73 2d 3e (pVfs->
12428 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 29 0a szOsFile bytes).
12429 20 20 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f 75 ** Sub-jou
1242a 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 rnal file handle
1242b 20 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 (journa
1242c 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29 lFileSize bytes)
1242d 0a 20 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20 6a . ** Main j
1242e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 ournal file hand
1242f 6c 65 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e le (journ
12430 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 alFileSize bytes
12431 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 ). ** Datab
12432 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 ase file name
12433 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 (nPat
12434 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 29 0a 20 hname+1 bytes).
12435 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 ** Journal
12436 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20 file name
12437 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e 61 (nPathna
12438 6d 65 2b 38 2b 31 20 62 79 74 65 73 29 0a 20 20 me+8+1 bytes).
12439 2a 2f 0a 20 20 70 50 74 72 20 3d 20 28 75 38 20 */. pPtr = (u8
1243a 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a *)sqlite3MallocZ
1243b 65 72 6f 28 0a 20 20 20 20 73 69 7a 65 6f 66 28 ero(. sizeof(
1243c 2a 70 50 61 67 65 72 29 20 2b 20 20 20 20 20 20 *pPager) +
1243d 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73 74 /* Pager st
1243e 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 70 ructure */. p
1243f 63 61 63 68 65 53 69 7a 65 20 20 20 20 20 20 2b cacheSize +
12440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43 /* PC
12441 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ache object */.
12442 20 20 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c pVfs->szOsFil
12443 65 20 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f e + /
12444 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 * The main db fi
12445 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 le */. journa
12446 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 lFileSize * 2 +
12447 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 77 6f /* The two
12448 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a journal files *
12449 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 / . nPathname
1244a 20 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 + 1 +
1244b 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 /* zFilename
1244c 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 */. nPathname
1244d 20 2b 20 38 20 2b 20 31 20 20 20 20 20 20 20 20 + 8 + 1
1244e 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61 6c 20 2a /* zJournal *
1244f 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 70 50 /. );. if( !pP
12450 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 tr ){. sqlite
12451 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 3_free(zPathname
12452 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
12453 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
12454 20 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 pPager =
12455 20 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 (Pager*)
12456 28 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 (pPtr);. pPager
12457 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 ->pPCache = (
12458 50 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d PCache*)(pPtr +=
12459 20 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 sizeof(*pPager)
1245a 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64 20 );. pPager->fd
1245b 3d 20 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c = (sqlite3_fil
1245c 65 2a 29 28 70 50 74 72 20 2b 3d 20 70 63 61 63 e*)(pPtr += pcac
1245d 68 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 heSize);. pPage
1245e 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 r->sjfd = (sqlit
1245f 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b e3_file*)(pPtr +
12460 3d 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 = pVfs->szOsFile
12461 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 );. pPager->jfd
12462 20 3d 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c = (sqlite3_fil
12463 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 e*)(pPtr += jour
12464 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20 nalFileSize);.
12465 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
12466 65 20 3d 20 20 20 20 28 63 68 61 72 2a 29 28 70 e = (char*)(p
12467 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 Ptr += journalFi
12468 6c 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 46 leSize);.. /* F
12469 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 67 65 72 ill in the Pager
1246a 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50 .zFilename and P
1246b 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75 ager.zJournal bu
1246c 66 66 65 72 73 2c 20 69 66 20 72 65 71 75 69 72 ffers, if requir
1246d 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61 ed. */. if( zPa
1246e 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 70 50 thname ){. pP
1246f 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d ager->zJournal =
12470 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 72 20 (char*)(pPtr
12471 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 += nPathname + 1
12472 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 );. memcpy(pP
12473 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c ager->zFilename,
12474 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 zPathname, nPat
12475 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 hname);. memc
12476 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 py(pPager->zJour
12477 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 nal, zPathname,
12478 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 nPathname);.
12479 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e memcpy(&pPager->
1247a 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 zJournal[nPathna
1247b 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c me], "-journal",
1247c 20 38 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 8);. sqlite3
1247d 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 _free(zPathname)
1247e 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e ;. }. pPager->
1247f 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 70 pVfs = pVfs;. p
12480 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 20 Pager->vfsFlags
12481 3d 20 76 66 73 46 6c 61 67 73 3b 0a 0a 20 20 2f = vfsFlags;.. /
12482 2a 20 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72 * Open the pager
12483 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 file.. */. if
12484 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a ( zFilename && z
12485 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 Filename[0] && !
12486 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 69 6e 74 memDb ){. int
12487 20 66 6f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 fout = 0;
12488 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12489 20 56 46 53 20 66 6c 61 67 73 20 72 65 74 75 72 VFS flags retur
1248a 6e 65 64 20 62 79 20 78 4f 70 65 6e 28 29 20 2a ned by xOpen() *
1248b 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
1248c 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 e3OsOpen(pVfs, p
1248d 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 Pager->zFilename
1248e 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 76 66 , pPager->fd, vf
1248f 73 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a sFlags, &fout);.
12490 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 readOnly = (
12491 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e fout&SQLITE_OPEN
12492 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20 20 20 _READONLY);..
12493 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 /* If the file
12494 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 was successfully
12495 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 opened for read
12496 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c 0a 20 /write access,.
12497 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61 20 64 ** choose a d
12498 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 efault page size
12499 20 69 6e 20 63 61 73 65 20 77 65 20 68 61 76 65 in case we have
1249a 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 20 to create the.
1249b 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ** database f
1249c 69 6c 65 2e 20 54 68 65 20 64 65 66 61 75 6c 74 ile. The default
1249d 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 74 68 page size is th
1249e 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 e maximum of:.
1249f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b **. ** +
124a0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
124a1 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 2a PAGE_SIZE,. *
124a2 2a 20 20 20 20 2b 20 54 68 65 20 76 61 6c 75 65 * + The value
124a3 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
124a4 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 ite3OsSectorSize
124a5 28 29 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 (). ** + T
124a6 68 65 20 6c 61 72 67 65 73 74 20 70 61 67 65 20 he largest page
124a7 73 69 7a 65 20 74 68 61 74 20 63 61 6e 20 62 65 size that can be
124a8 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 written atomica
124a9 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 lly.. */.
124aa 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
124ab 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c 79 20 29 K && !readOnly )
124ac 7b 0a 20 20 20 20 20 20 73 65 74 53 65 63 74 6f {. setSecto
124ad 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 rSize(pPager);.
124ae 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 assert(SQLI
124af 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f TE_DEFAULT_PAGE_
124b0 53 49 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 SIZE<=SQLITE_MAX
124b1 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
124b2 5a 45 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 ZE);. if( s
124b3 7a 50 61 67 65 44 66 6c 74 3c 70 50 61 67 65 72 zPageDflt<pPager
124b4 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 29 7b 0a ->sectorSize ){.
124b5 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 if( pPag
124b6 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3e 53 er->sectorSize>S
124b7 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c QLITE_MAX_DEFAUL
124b8 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20 T_PAGE_SIZE ){.
124b9 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 szPageD
124ba 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 flt = SQLITE_MAX
124bb 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
124bc 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ZE;. }els
124bd 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 e{. szP
124be 61 67 65 44 66 6c 74 20 3d 20 28 75 31 36 29 70 ageDflt = (u16)p
124bf 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
124c0 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 e;. }.
124c1 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 }.#ifdef SQLI
124c2 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 TE_ENABLE_ATOMIC
124c3 5f 57 52 49 54 45 0a 20 20 20 20 20 20 7b 0a 20 _WRITE. {.
124c4 20 20 20 20 20 20 20 69 6e 74 20 69 44 63 20 3d int iDc =
124c5 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 sqlite3OsDevice
124c6 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
124c7 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 pPager->fd);.
124c8 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 int ii;.
124c9 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 assert(SQLI
124ca 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 TE_IOCAP_ATOMIC5
124cb 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 12==(512>>8));.
124cc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 assert(SQ
124cd 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
124ce 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 C64K==(65536>>8)
124cf 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
124d0 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 t(SQLITE_MAX_DEF
124d1 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d AULT_PAGE_SIZE<=
124d2 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20 20 65536);.
124d3 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 44 66 6c for(ii=szPageDfl
124d4 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 5f 4d 41 t; ii<=SQLITE_MA
124d5 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
124d6 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 7b 0a 20 IZE; ii=ii*2){.
124d7 20 20 20 20 20 20 20 20 20 69 66 28 20 69 44 63 if( iDc
124d8 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 &(SQLITE_IOCAP_A
124d9 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 29 20 29 TOMIC|(ii>>8)) )
124da 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 7a {. sz
124db 50 61 67 65 44 66 6c 74 20 3d 20 69 69 3b 0a 20 PageDflt = ii;.
124dc 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
124dd 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e }. }.#en
124de 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 dif. }. }els
124df 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 e{. /* If a t
124e0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 emporary file is
124e1 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 requested, it i
124e2 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d s not opened imm
124e3 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a ediately.. **
124e4 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 In this case we
124e5 20 61 63 63 65 70 74 20 74 68 65 20 64 65 66 61 accept the defa
124e6 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e ult page size an
124e7 64 20 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 d delay actually
124e8 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 . ** opening
124e9 74 68 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 the file until t
124ea 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f he first call to
124eb 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 OsWrite()..
124ec 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 **. ** This b
124ed 72 61 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 ranch is also ru
124ee 6e 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f n for an in-memo
124ef 72 79 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 ry database. An
124f0 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a in-memory. **
124f1 20 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 database is the
124f2 20 73 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d same as a temp-
124f3 66 69 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 file that is nev
124f4 65 72 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 er written out t
124f5 6f 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e o. ** disk an
124f6 64 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d d uses an in-mem
124f7 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 ory rollback jou
124f8 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 rnal.. */ .
124f9 20 20 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a tempFile = 1;.
124fa 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
124fb 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 e = PAGER_EXCLUS
124fc 49 56 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 IVE;. }.. /* T
124fd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c he following cal
124fe 6c 20 74 6f 20 50 61 67 65 72 53 65 74 50 61 67 l to PagerSetPag
124ff 65 73 69 7a 65 28 29 20 73 65 72 76 65 73 20 74 esize() serves t
12500 6f 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 o set the value
12501 6f 66 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 70 of . ** Pager.p
12502 61 67 65 53 69 7a 65 20 61 6e 64 20 74 6f 20 61 ageSize and to a
12503 6c 6c 6f 63 61 74 65 20 74 68 65 20 50 61 67 65 llocate the Page
12504 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 66 r.pTmpSpace buff
12505 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 er.. */. if( r
12506 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12507 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
12508 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29 3b 0a er->memDb==0 );.
12509 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1250a 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
1250b 28 70 50 61 67 65 72 2c 20 26 73 7a 50 61 67 65 (pPager, &szPage
1250c 44 66 6c 74 29 3b 0a 20 20 20 20 74 65 73 74 63 Dflt);. testc
1250d 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ase( rc!=SQLITE_
1250e 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 OK );. }.. /*
1250f 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
12510 72 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6f rred in either o
12511 66 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62 6f f the blocks abo
12512 76 65 2c 20 66 72 65 65 20 74 68 65 20 0a 20 20 ve, free the .
12513 2a 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 ** Pager structu
12514 72 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 re and close the
12515 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 file.. */. if
12516 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
12517 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
12518 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
12519 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 e );. sqlite3
1251a 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
1251b 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 fd);. sqlite3
1251c 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 _free(pPager);.
1251d 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1251e 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 }.. /* Initiali
1251f 7a 65 20 74 68 65 20 50 43 61 63 68 65 20 6f 62 ze the PCache ob
12520 6a 65 63 74 2e 20 2a 2f 0a 20 20 6e 45 78 74 72 ject. */. nExtr
12521 61 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 74 72 a = ROUND8(nExtr
12522 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 a);. sqlite3Pca
12523 63 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 44 66 cheOpen(szPageDf
12524 6c 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d 65 6d lt, nExtra, !mem
12525 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 Db,.
12526 20 20 20 20 20 20 20 20 21 6d 65 6d 44 62 3f 70 !memDb?p
12527 61 67 65 72 53 74 72 65 73 73 3a 30 2c 20 28 76 agerStress:0, (v
12528 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 50 oid *)pPager, pP
12529 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
1252a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
1252b 4f 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20 46 OPEN %d %s\n", F
1252c 49 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 61 67 ILEHANDLEID(pPag
1252d 65 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 72 2d er->fd), pPager-
1252e 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 >zFilename));.
1252f 49 4f 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 IOTRACE(("OPEN %
12530 70 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %s\n", pPager,
12531 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
12532 6d 65 29 29 0a 0a 20 20 70 50 61 67 65 72 2d 3e me)).. pPager->
12533 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 useJournal = (u8
12534 29 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 )useJournal;. p
12535 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 Pager->noReadloc
12536 6b 20 3d 20 28 6e 6f 52 65 61 64 6c 6f 63 6b 20 k = (noReadlock
12537 26 26 20 72 65 61 64 4f 6e 6c 79 29 20 3f 31 3a && readOnly) ?1:
12538 30 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 0;. /* pPager->
12539 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f stmtOpen = 0; */
1253a 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 . /* pPager->st
1253b 6d 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0a mtInUse = 0; */.
1253c 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65 /* pPager->nRe
1253d 66 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 f = 0; */. pPag
1253e 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
1253f 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 2f = (u8)memDb;. /
12540 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 * pPager->stmtSi
12541 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 ze = 0; */. /*
12542 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a pPager->stmtJSiz
12543 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 e = 0; */. /* p
12544 50 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30 Pager->nPage = 0
12545 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6d ; */. pPager->m
12546 78 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d xPgno = SQLITE_M
12547 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 AX_PAGE_COUNT;.
12548 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 /* pPager->stat
12549 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b e = PAGER_UNLOCK
1254a 3b 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 ; */. assert( p
1254b 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 Pager->state ==
1254c 28 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 (tempFile ? PAGE
1254d 52 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 R_EXCLUSIVE : PA
1254e 47 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 GER_UNLOCK) );.
1254f 20 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d /* pPager->errM
12550 61 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 ask = 0; */. pP
12551 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d ager->tempFile =
12552 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a 20 (u8)tempFile;.
12553 20 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c assert( tempFil
12554 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 e==PAGER_LOCKING
12555 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 MODE_NORMAL .
12556 20 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69 || tempFi
12557 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e le==PAGER_LOCKIN
12558 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 GMODE_EXCLUSIVE
12559 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
1255a 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 ER_LOCKINGMODE_E
1255b 58 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20 XCLUSIVE==1 );.
1255c 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
1255d 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 6d veMode = (u8)tem
1255e 70 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 72 pFile; . pPager
1255f 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e ->changeCountDon
12560 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 e = pPager->temp
12561 46 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e File;. pPager->
12562 6d 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d 44 memDb = (u8)memD
12563 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 61 b;. pPager->rea
12564 64 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61 64 dOnly = (u8)read
12565 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 Only;. /* pPage
12566 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b r->needSync = 0;
12567 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f */. pPager->no
12568 53 79 6e 63 20 3d 20 28 70 50 61 67 65 72 2d 3e Sync = (pPager->
12569 74 65 6d 70 46 69 6c 65 20 7c 7c 20 21 75 73 65 tempFile || !use
1256a 4a 6f 75 72 6e 61 6c 29 20 3f 31 3a 30 3b 0a 20 Journal) ?1:0;.
1256b 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e pPager->fullSyn
1256c 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 c = pPager->noSy
1256d 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 65 nc ?0:1;. pPage
1256e 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 r->sync_flags =
1256f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
12570 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d AL;. /* pPager-
12571 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a >pFirst = 0; */.
12572 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 /* pPager->pFi
12573 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a rstSynced = 0; *
12574 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 /. /* pPager->p
12575 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 Last = 0; */. p
12576 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20 Pager->nExtra =
12577 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 67 65 72 nExtra;. pPager
12578 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d ->journalSizeLim
12579 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 it = SQLITE_DEFA
1257a 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 ULT_JOURNAL_SIZE
1257b 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 65 72 74 _LIMIT;. assert
1257c 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
1257d 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46 69 6c 65 >fd) || tempFile
1257e 20 29 3b 0a 20 20 73 65 74 53 65 63 74 6f 72 53 );. setSectorS
1257f 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69 ize(pPager);. i
12580 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 f( memDb ){.
12581 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
12582 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 ode = PAGER_JOUR
12583 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a NALMODE_MEMORY;.
12584 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d }. /* pPager-
12585 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 >xBusyHandler =
12586 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
12587 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 r->pBusyHandlerA
12588 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 rg = 0; */. /*
12589 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 memset(pPager->a
1258a 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Hash, 0, sizeof(
1258b 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b pPager->aHash));
1258c 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d */. *ppPager =
1258d 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 pPager;. retur
1258e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1258f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
12590 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
12591 61 66 74 65 72 20 74 72 61 6e 73 69 74 69 6f 6e after transition
12592 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52 5f 55 ing from PAGER_U
12593 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 47 45 NLOCK to.** PAGE
12594 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 2e 20 R_SHARED state.
12595 49 74 20 74 65 73 74 73 20 69 66 20 74 68 65 72 It tests if ther
12596 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e e is a hot journ
12597 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a al present in.**
12598 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
12599 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 for the given p
1259a 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72 ager. A hot jour
1259b 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 nal is one that
1259c 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 .** needs to be
1259d 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63 played back. Acc
1259e 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66 ording to this f
1259f 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a unction, a hot-j
125a0 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 65 ournal.** file e
125a1 78 69 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c xists if the fol
125a2 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 20 lowing criteria
125a3 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 are met:.**.**
125a4 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 * The journal f
125a5 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 ile exists in th
125a6 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 61 e file system, a
125a7 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 72 6f nd.** * No pro
125a8 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53 cess holds a RES
125a9 45 52 56 45 44 20 6f 72 20 67 72 65 61 74 65 72 ERVED or greater
125aa 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
125ab 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 0a abase file, and.
125ac 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 ** * The datab
125ad 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20 ase file itself
125ae 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
125af 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0 bytes in size,
125b0 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 and.** * The
125b1 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 first byte of th
125b2 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 e journal file e
125b3 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74 xists and is not
125b4 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 0x00..**.** If
125b5 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 the current size
125b6 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
125b7 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 20 61 file is 0 but a
125b8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a journal file.**
125b9 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 69 73 exists, that is
125ba 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64 probably an old
125bb 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76 journal left ov
125bc 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a er from a prior.
125bd 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 74 68 ** database with
125be 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 the same name.
125bf 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
125c0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
125c1 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74 65 64 .** just deleted
125c2 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74 65 2c using OsDelete,
125c3 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65 74 *pExists is set
125c4 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 to 0 and SQLITE
125c5 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e _OK.** is return
125c6 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
125c7 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 outine does not
125c8 63 68 65 63 6b 20 69 66 20 74 68 65 72 65 20 69 check if there i
125c9 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e s a master journ
125ca 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 61 al filename.** a
125cb 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
125cc 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 file. If there
125cd 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73 is, and that mas
125ce 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
125cf 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 .** does not exi
125d0 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 st, then the jou
125d1 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 rnal file is not
125d2 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20 really hot. In
125d3 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 69 this.** case thi
125d4 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 s routine will r
125d5 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f eturn a false-po
125d6 73 69 74 69 76 65 2e 20 54 68 65 20 70 61 67 65 sitive. The page
125d7 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a 2a 20 r_playback().**
125d8 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73 routine will dis
125d9 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 20 6a cover that the j
125da 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
125db 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20 61 6e ot really hot an
125dc 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 d .** will not r
125dd 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a oll it back. .**
125de 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a 6f 75 .** If a hot-jou
125df 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 6f 75 rnal file is fou
125e0 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a 70 45 nd to exist, *pE
125e1 78 69 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 xists is set to
125e2 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 54 45 1 and .** SQLITE
125e3 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 _OK returned. If
125e4 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 no hot-journal
125e5 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c file is present,
125e6 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a 2a 20 *pExists is.**
125e7 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c set to 0 and SQL
125e8 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e ITE_OK returned.
125e9 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
125ea 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 occurs while try
125eb 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d ing.** to determ
125ec 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
125ed 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ot a hot-journal
125ee 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 74 68 file exists, th
125ef 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 63 6f e IO error.** co
125f0 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 de is returned a
125f1 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 nd the value of
125f2 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e 64 65 *pExists is unde
125f3 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 fined..*/.static
125f4 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 6e int hasHotJourn
125f5 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 al(Pager *pPager
125f6 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 29 7b , int *pExists){
125f7 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
125f8 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 const pVfs = pP
125f9 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e ager->pVfs;. in
125fa 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
125fb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
125fc 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
125fd 69 6e 74 20 65 78 69 73 74 73 3b 20 20 20 20 20 int exists;
125fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
125ff 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75 72 6e True if a journ
12600 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 al file is prese
12601 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 nt */.. assert(
12602 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 pPager!=0 );.
12603 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
12604 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 useJournal );.
12605 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
12606 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 Pager->fd) );.
12607 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 assert( !isOpen(
12608 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
12609 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b . *pExists = 0;
1260a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
1260b 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 50 sAccess(pVfs, pP
1260c 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
1260d 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
1260e 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a ISTS, &exists);.
1260f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
12610 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 7b _OK && exists ){
12611 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65 64 3b . int locked;
12612 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12613 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f 6d 65 /* True if some
12614 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 process holds a
12615 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a RESERVED lock *
12616 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
12617 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
12618 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 dLock(pPager->fd
12619 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20 , &locked);.
1261a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1261b 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a K && !locked ){.
1261c 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b int nPage;
1261d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b .. /* Check
1261e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1261f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
12620 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 20 6f If it consists o
12621 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 20 20 f 0 pages,.
12622 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 65 20 ** then delete
12623 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12624 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 . See the header
12625 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 66 comment above f
12626 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 or . ** the
12627 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 65 2e reasoning here.
12628 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
12629 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1262a 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 rPagecount(pPage
1262b 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 r, &nPage);.
1262c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1262d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 _OK ){. i
1262e 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 f( nPage==0 ){.
1262f 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
12630 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 lite3OsDelete(pV
12631 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
12632 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 rnal, 0);.
12633 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
12634 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /* The journa
12635 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e l file exists an
12636 64 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 d no other conne
12637 63 74 69 6f 6e 20 68 61 73 20 61 20 72 65 73 65 ction has a rese
12638 72 76 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a rved. *
12639 2a 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 * or greater loc
1263a 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
1263b 65 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68 65 63 e file. Now chec
1263c 6b 20 74 68 61 74 20 74 68 65 72 65 20 69 73 0a k that there is.
1263d 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20 ** at
1263e 6c 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 least one non-ze
1263f 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 ro bytes at the
12640 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 start of the jou
12641 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 rnal file..
12642 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 ** If there
12643 20 69 73 2c 20 74 68 65 6e 20 77 65 20 63 6f 6e is, then we con
12644 73 69 64 65 72 20 74 68 69 73 20 6a 6f 75 72 6e sider this journ
12645 61 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20 49 66 al to be hot. If
12646 20 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20 20 20 not, .
12647 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 69 67 ** it can be ig
12648 6e 6f 72 65 64 2e 0a 20 20 20 20 20 20 20 20 20 nored..
12649 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e */. in
1264a 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 t f = SQLITE_OPE
1264b 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 N_READONLY|SQLIT
1264c 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 E_OPEN_MAIN_JOUR
1264d 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 NAL;. r
1264e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
1264f 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e n(pVfs, pPager->
12650 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 zJournal, pPager
12651 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b 0a 20 ->jfd, f, &f);.
12652 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d if( rc=
12653 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12654 20 20 20 20 20 20 20 20 20 20 75 38 20 66 69 72 u8 fir
12655 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 st = 0;.
12656 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12657 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a OsRead(pPager->j
12658 66 64 2c 20 28 76 6f 69 64 20 2a 29 26 66 69 72 fd, (void *)&fir
12659 73 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 st, 1, 0);.
1265a 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
1265b 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
1265c 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 T_READ ){.
1265d 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1265e 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 ITE_OK;.
1265f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
12660 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
12661 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
12662 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 78 69 *pExi
12663 73 74 73 20 3d 20 28 66 69 72 73 74 21 3d 30 29 sts = (first!=0)
12664 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
12665 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
12666 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
12667 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
12668 20 52 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e Read the conten
12669 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 20 6f t for page pPg o
1266a 75 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ut of the databa
1266b 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 74 6f se file and into
1266c 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 61 74 61 2e .** pPg->pData.
1266d 20 41 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f A shared lock o
1266e 72 20 67 72 65 61 74 65 72 20 6d 75 73 74 20 62 r greater must b
1266f 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 e held on the da
12670 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 62 tabase.** file b
12671 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 efore this funct
12672 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ion is called..*
12673 2a 0a 2a 2a 20 49 66 20 70 61 67 65 20 31 20 69 *.** If page 1 i
12674 73 20 72 65 61 64 2c 20 74 68 65 6e 20 74 68 65 s read, then the
12675 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e value of Pager.
12676 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 dbFileVers[] is
12677 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 set to.** the va
12678 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 lue read from th
12679 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1267a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 .**.** If an IO
1267b 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
1267c 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 en the IO error
1267d 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
1267e 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 4f 74 he caller..** Ot
1267f 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f herwise, SQLITE_
12680 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a OK is returned..
12681 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
12682 61 64 44 62 50 61 67 65 28 50 67 48 64 72 20 2a adDbPage(PgHdr *
12683 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 pPg){. Pager *p
12684 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
12685 67 65 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62 ger; /* Pager ob
12686 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 20 ject associated
12687 77 69 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f with page pPg */
12688 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 . Pgno pgno = p
12689 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20 Pg->pgno;
1268a 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 /* Page number t
1268b 6f 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 o read */. int
1268c 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1268d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
1268e 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 36 34 rn code */. i64
1268f 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
12690 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
12691 65 20 6f 66 66 73 65 74 20 6f 66 20 66 69 6c 65 e offset of file
12692 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f to read from */
12693 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
12694 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
12695 5f 53 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44 _SHARED && !MEMD
12696 42 20 29 3b 0a 0a 20 20 69 66 28 20 21 69 73 4f B );.. if( !isO
12697 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
12698 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
12699 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
1269a 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 );. memset(pP
1269b 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 g->pData, 0, pPa
1269c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
1269d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1269e 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f 66 66 E_OK;. }. iOff
1269f 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 set = (pgno-1)*(
126a0 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 i64)pPager->page
126a1 53 69 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c Size;. rc = sql
126a2 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 ite3OsRead(pPage
126a3 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 74 r->fd, pPg->pDat
126a4 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 a, pPager->pageS
126a5 69 7a 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 ize, iOffset);.
126a6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
126a7 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
126a8 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
126a9 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 ITE_OK;. }. if
126aa 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ( pgno==1 ){.
126ab 20 75 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20 u8 *dbFileVers
126ac 3d 20 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 = &((u8*)pPg->pD
126ad 61 74 61 29 5b 32 34 5d 3b 0a 20 20 20 20 6d 65 ata)[24];. me
126ae 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 mcpy(&pPager->db
126af 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 FileVers, dbFile
126b0 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61 Vers, sizeof(pPa
126b1 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 ger->dbFileVers)
126b2 29 3b 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28 );. }. CODEC1(
126b3 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61 pPager, pPg->pDa
126b4 74 61 2c 20 70 67 6e 6f 2c 20 33 29 3b 0a 0a 20 ta, pgno, 3);..
126b5 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 PAGER_INCR(sqli
126b6 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62 te3_pager_readdb
126b7 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 52 _count);. PAGER
126b8 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 52 _INCR(pPager->nR
126b9 65 61 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 ead);. IOTRACE(
126ba 28 22 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 2c ("PGIN %p %d\n",
126bb 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b pPager, pgno));
126bc 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
126bd 46 45 54 43 48 20 25 64 20 70 61 67 65 20 25 64 FETCH %d page %d
126be 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a hash(%08x)\n",.
126bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
126c0 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
126c1 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 pgno, pager_page
126c2 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 hash(pPg)));..
126c3 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
126c4 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
126c5 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e n is called when
126c6 65 76 65 72 20 74 68 65 20 75 70 70 65 72 20 6c ever the upper l
126c7 61 79 65 72 20 72 65 71 75 65 73 74 73 20 61 20 ayer requests a
126c8 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 database.** page
126c9 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 62 is requested, b
126ca 65 66 6f 72 65 20 74 68 65 20 63 61 63 68 65 20 efore the cache
126cb 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72 20 61 is checked for a
126cc 20 73 75 69 74 61 62 6c 65 20 70 61 67 65 0a 2a suitable page.*
126cd 2a 20 6f 72 20 61 6e 79 20 64 61 74 61 20 69 73 * or any data is
126ce 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 read from the d
126cf 61 74 61 62 61 73 65 2e 20 49 74 20 70 65 72 66 atabase. It perf
126d0 6f 72 6d 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 orms the followi
126d1 6e 67 0a 2a 2a 20 74 77 6f 20 66 75 6e 63 74 69 ng.** two functi
126d2 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 ons:.**.** 1)
126d3 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
126d4 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 50 41 47 currently in PAG
126d5 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20 ER_UNLOCK state
126d6 28 6e 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a (no lock held.**
126d7 20 20 20 20 20 20 6f 6e 20 74 68 65 20 64 61 74 on the dat
126d8 61 62 61 73 65 20 66 69 6c 65 29 2c 20 74 68 65 abase file), the
126d9 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 n an attempt is
126da 6d 61 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 made to obtain a
126db 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 44 20 .** SHARED
126dc 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
126dd 62 61 73 65 20 66 69 6c 65 2e 20 49 6d 6d 65 64 base file. Immed
126de 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f 62 74 iately after obt
126df 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 aining.** t
126e0 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 he SHARED lock,
126e1 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
126e2 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72 20 61 is checked for a
126e3 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a hot-journal,.**
126e4 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 70 which is p
126e5 6c 61 79 65 64 20 62 61 63 6b 20 69 66 20 70 72 layed back if pr
126e6 65 73 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 esent. Following
126e7 20 61 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c any hot-journal
126e8 20 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61 .** rollba
126e9 63 6b 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ck, the contents
126ea 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 61 72 of the cache ar
126eb 65 20 76 61 6c 69 64 61 74 65 64 20 62 79 20 63 e validated by c
126ec 68 65 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 hecking.**
126ed 74 68 65 20 27 63 68 61 6e 67 65 2d 63 6f 75 6e the 'change-coun
126ee 74 65 72 27 20 66 69 65 6c 64 20 6f 66 20 74 68 ter' field of th
126ef 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
126f0 68 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20 20 20 header and.**
126f1 20 20 20 64 69 73 63 61 72 64 65 64 20 69 66 20 discarded if
126f2 74 68 65 79 20 61 72 65 20 66 6f 75 6e 64 20 74 they are found t
126f3 6f 20 62 65 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a o be invalid..**
126f4 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 65 20 .** 2) If the
126f5 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 pager is running
126f6 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f in exclusive-mo
126f7 64 65 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 de, and there ar
126f8 65 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 e currently.**
126f9 20 20 20 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 no outstandi
126fa 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f ng references to
126fb 20 61 6e 79 20 70 61 67 65 73 2c 20 61 6e 64 20 any pages, and
126fc 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 is in the error
126fd 73 74 61 74 65 2c 0a 2a 2a 20 20 20 20 20 20 74 state,.** t
126fe 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 hen an attempt i
126ff 73 20 6d 61 64 65 20 74 6f 20 63 6c 65 61 72 20 s made to clear
12700 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 the error state
12701 62 79 20 64 69 73 63 61 72 64 69 6e 67 0a 2a 2a by discarding.**
12702 20 20 20 20 20 20 74 68 65 20 63 6f 6e 74 65 6e the conten
12703 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 ts of the page c
12704 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 69 6e 67 ache and rolling
12705 20 62 61 63 6b 20 61 6e 79 20 6f 70 65 6e 20 6a back any open j
12706 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 66 ournal.** f
12707 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ile..**.** If th
12708 65 20 6f 70 65 72 61 74 69 6f 6e 20 64 65 73 63 e operation desc
12709 72 69 62 65 64 20 62 79 20 28 32 29 20 61 62 6f ribed by (2) abo
1270a 76 65 20 69 73 20 6e 6f 74 20 61 74 74 65 6d 70 ve is not attemp
1270b 74 65 64 2c 20 61 6e 64 20 69 66 20 74 68 65 0a ted, and if the.
1270c 2a 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 ** pager is in a
1270d 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74 n error state ot
1270e 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f her than SQLITE_
1270f 46 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 69 FULL when this i
12710 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 s called,.** the
12711 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 error state err
12712 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
12713 6e 65 64 2e 20 49 74 20 69 73 20 70 65 72 6d 69 ned. It is permi
12714 74 74 65 64 20 74 6f 20 72 65 61 64 20 74 68 65 tted to read the
12715 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 68 65 .** database whe
12716 6e 20 69 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c n in SQLITE_FULL
12717 20 65 72 72 6f 72 20 73 74 61 74 65 2e 0a 2a 2a error state..**
12718 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 .** Otherwise, i
12719 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 f everything is
1271a 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 successful, SQLI
1271b 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
1271c 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 49 4f 20 65 d. If an.** IO e
1271d 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
1271e 65 20 6c 6f 63 6b 69 6e 67 20 74 68 65 20 64 61 e locking the da
1271f 74 61 62 61 73 65 2c 20 63 68 65 63 6b 69 6e 67 tabase, checking
12720 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e for a hot-journ
12721 61 6c 0a 2a 2a 20 66 69 6c 65 20 6f 72 20 72 6f al.** file or ro
12722 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 lling back a jou
12723 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 49 rnal file, the I
12724 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
12725 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 returned..*/.sta
12726 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 68 61 tic int pagerSha
12727 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 redLock(Pager *p
12728 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
12729 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
1272a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1272b 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
1272c 20 69 6e 74 20 69 73 45 72 72 6f 72 52 65 73 65 int isErrorRese
1272d 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 t = 0;
1272e 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 /* True if r
1272f 65 63 6f 76 65 72 69 6e 67 20 66 72 6f 6d 20 65 ecovering from e
12730 72 72 6f 72 20 73 74 61 74 65 20 2a 2f 0a 0a 20 rror state */..
12731 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 /* If this data
12732 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 base is opened f
12733 6f 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 or exclusive acc
12734 65 73 73 2c 20 68 61 73 20 6e 6f 20 6f 75 74 73 ess, has no outs
12735 74 61 6e 64 69 6e 67 20 0a 20 20 2a 2a 20 70 61 tanding . ** pa
12736 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e ge references an
12737 64 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 d is in an error
12738 2d 73 74 61 74 65 2c 20 74 68 69 73 20 69 73 20 -state, this is
12739 61 20 63 68 61 6e 63 65 20 74 6f 20 63 6c 65 61 a chance to clea
1273a 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72 r. ** the error
1273b 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f . Discard the co
1273c 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
1273d 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20 74 72 ger-cache and tr
1273e 65 61 74 20 61 6e 79 0a 20 20 2a 2a 20 6f 70 65 eat any. ** ope
1273f 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 n journal file a
12740 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2e s a hot-journal.
12741 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4d 45 4d . */. if( !MEM
12742 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78 DB && pPager->ex
12743 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 clusiveMode .
12744 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 && sqlite3Pcache
12745 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
12746 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 26 26 20 >pPCache)==0 &&
12747 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
12748 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73 . ){. if( is
12749 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
1274a 29 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 72 ) ){. isErr
1274b 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 20 orReset = 1;.
1274c 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 }. pPager->e
1274d 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f rrCode = SQLITE_
1274e 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 65 OK;. pager_re
1274f 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d set(pPager);. }
12750 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
12751 67 65 72 20 69 73 20 73 74 69 6c 6c 20 69 6e 20 ger is still in
12752 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 an error state,
12753 64 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64 2e 20 do not proceed.
12754 54 68 65 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 The error . **
12755 73 74 61 74 65 20 77 69 6c 6c 20 62 65 20 63 6c state will be cl
12756 65 61 72 65 64 20 61 74 20 73 6f 6d 65 20 70 6f eared at some po
12757 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 int in the futur
12758 65 20 77 68 65 6e 20 61 6c 6c 20 70 61 67 65 20 e when all page
12759 0a 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 . ** references
1275a 20 61 72 65 20 64 72 6f 70 70 65 64 20 61 6e 64 are dropped and
1275b 20 74 68 65 20 63 61 63 68 65 20 63 61 6e 20 62 the cache can b
1275c 65 20 64 69 73 63 61 72 64 65 64 2e 0a 20 20 2a e discarded.. *
1275d 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
1275e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65 errCode && pPage
1275f 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 r->errCode!=SQLI
12760 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72 TE_FULL ){. r
12761 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
12762 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 rCode;. }.. if
12763 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d ( pPager->state=
12764 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c =PAGER_UNLOCK ||
12765 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b isErrorReset ){
12766 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
12767 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 * const pVfs =
12768 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 pPager->pVfs;.
12769 20 20 69 6e 74 20 69 73 48 6f 74 4a 6f 75 72 6e int isHotJourn
1276a 61 6c 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 al = 0;. asse
1276b 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 rt( !MEMDB );.
1276c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1276d 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
1276e 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
1276f 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 ==0 );. if( !
12770 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f pPager->noReadlo
12771 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ck ){. rc =
12772 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c pager_wait_on_l
12773 6f 63 6b 28 70 50 61 67 65 72 2c 20 53 48 41 52 ock(pPager, SHAR
12774 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 ED_LOCK);.
12775 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12776 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 K ){. ass
12777 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
12778 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te==PAGER_UNLOCK
12779 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 );. retu
1277a 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 rn pager_error(p
1277b 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 Pager, rc);.
1277c 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1277d 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d ( pPager->state=
1277e 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b =PAGER_UNLOCK ){
1277f 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 . pPager->s
12780 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 tate = PAGER_SHA
12781 52 45 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 RED;. }. a
12782 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
12783 74 61 74 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 tate>=SHARED_LOC
12784 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 K );.. /* If
12785 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 a journal file e
12786 78 69 73 74 73 2c 20 61 6e 64 20 74 68 65 72 65 xists, and there
12787 20 69 73 20 6e 6f 20 52 45 53 45 52 56 45 44 20 is no RESERVED
12788 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 lock on the.
12789 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
1278a 2c 20 74 68 65 6e 20 69 74 20 65 69 74 68 65 72 , then it either
1278b 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61 needs to be pla
1278c 79 65 64 20 62 61 63 6b 20 6f 72 20 64 65 6c 65 yed back or dele
1278d 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
1278e 69 66 28 20 21 69 73 45 72 72 6f 72 52 65 73 65 if( !isErrorRese
1278f 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 t ){. rc =
12790 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50 hasHotJournal(pP
12791 61 67 65 72 2c 20 26 69 73 48 6f 74 4a 6f 75 72 ager, &isHotJour
12792 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 nal);. if(
12793 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
12794 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 . goto fa
12795 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 iled;. }.
12796 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 45 72 }. if( isEr
12797 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 73 48 6f rorReset || isHo
12798 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 tJournal ){.
12799 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c /* Get an EXCL
1279a 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 USIVE lock on th
1279b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1279c 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 At this point i
1279d 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 6d t is. ** im
1279e 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 20 52 portant that a R
1279f 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 ESERVED lock is
127a0 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 not obtained on
127a1 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 0a 20 the way to the.
127a2 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 ** EXCLUSIV
127a3 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 77 65 E lock. If it we
127a4 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 re, another proc
127a5 65 73 73 20 6d 69 67 68 74 20 6f 70 65 6e 20 74 ess might open t
127a6 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 he. ** data
127a7 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74 65 63 base file, detec
127a8 74 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c t the RESERVED l
127a9 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c 75 64 ock, and conclud
127aa 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 20 e that the.
127ab 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 ** database is
127ac 73 61 66 65 20 74 6f 20 72 65 61 64 20 77 68 69 safe to read whi
127ad 6c 65 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 le this process
127ae 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c 69 6e 67 is still rolling
127af 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 68 the . ** h
127b0 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2e ot-journal back.
127b1 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 . ** .
127b2 20 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 ** Because the
127b3 69 6e 74 65 72 6d 65 64 69 61 74 65 20 52 45 53 intermediate RES
127b4 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f ERVED lock is no
127b5 74 20 72 65 71 75 65 73 74 65 64 2c 20 61 6e 79 t requested, any
127b6 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 . ** other
127b7 70 72 6f 63 65 73 73 20 61 74 74 65 6d 70 74 69 process attempti
127b8 6e 67 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ng to access the
127b9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
127ba 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20 20 20 20 ill get to .
127bb 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 6e 74 20 ** this point
127bc 69 6e 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20 in the code and
127bd 66 61 69 6c 20 74 6f 20 6f 62 74 61 69 6e 20 69 fail to obtain i
127be 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 49 56 45 ts own EXCLUSIVE
127bf 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20 2a 2a 20 lock . **
127c0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
127c1 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 file.. */.
127c2 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d if( pPager-
127c3 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 49 56 45 >state<EXCLUSIVE
127c4 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 _LOCK ){.
127c5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c rc = sqlite3OsL
127c6 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ock(pPager->fd,
127c7 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
127c8 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
127c9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
127ca 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
127cb 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
127cc 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 rc);.
127cd 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
127ce 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
127cf 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
127d0 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a AGER_EXCLUSIVE;.
127d1 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20 20 20 }. .
127d2 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 /* Open the jour
127d3 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f 77 72 69 nal for read/wri
127d4 74 65 20 61 63 63 65 73 73 2e 20 54 68 69 73 20 te access. This
127d5 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 0a 20 is because in .
127d6 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69 76 ** exclusiv
127d7 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 e-access mode th
127d8 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
127d9 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74 20 6f r will be kept o
127da 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a pen and. **
127db 20 70 6f 73 73 69 62 6c 79 20 75 73 65 64 20 66 possibly used f
127dc 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e or a transaction
127dd 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e 20 73 6f later on. On so
127de 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 0a me systems, the.
127df 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 75 6e 63 ** OsTrunc
127e0 61 74 65 28 29 20 63 61 6c 6c 20 75 73 65 64 20 ate() call used
127e1 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 in exclusive-acc
127e2 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f 20 72 65 ess mode also re
127e3 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a 2a 20 quires. **
127e4 61 20 72 65 61 64 2f 77 72 69 74 65 20 66 69 6c a read/write fil
127e5 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 20 20 e handle..
127e6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 */. if( !is
127e7 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
127e8 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 ) ){. int
127e9 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72 63 res;. rc
127ea 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 = sqlite3OsAcce
127eb 73 73 28 70 56 66 73 2c 70 50 61 67 65 72 2d 3e ss(pVfs,pPager->
127ec 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 54 45 5f zJournal,SQLITE_
127ed 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 26 72 ACCESS_EXISTS,&r
127ee 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 es);. if(
127ef 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
127f0 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
127f1 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 res ){.
127f2 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b int fout = 0;
127f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
127f4 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e f = SQLITE_OPEN
127f5 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
127f6 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 E_OPEN_MAIN_JOUR
127f7 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 NAL;.
127f8 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 assert( !pPager
127f9 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 ->tempFile );.
127fa 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
127fb 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
127fc 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
127fd 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 nal, pPager->jfd
127fe 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 , f, &fout);.
127ff 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
12800 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
12801 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d | isOpen(pPager-
12802 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 20 20 20 >jfd) );.
12803 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
12804 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 74 26 53 ITE_OK && fout&S
12805 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
12806 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 NLY ){.
12807 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
12808 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20 _CANTOPEN;.
12809 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1280a 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
1280b 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 jfd);.
1280c 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 }. }e
1280d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
1280e 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e /* If the journ
1280f 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 al does not exis
12810 74 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 73 6f t, that means so
12811 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 me other process
12812 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
12813 68 61 73 20 61 6c 72 65 61 64 79 20 72 6f 6c 6c has already roll
12814 65 64 20 69 74 20 62 61 63 6b 20 2a 2f 0a 20 20 ed it back */.
12815 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
12816 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
12817 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
12818 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
12819 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1281a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
1281b 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 o failed;.
1281c 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f }.. /* TODO
1281d 3a 20 57 68 79 20 61 72 65 20 74 68 65 73 65 20 : Why are these
1281e 63 6c 65 61 72 65 64 20 68 65 72 65 3f 20 49 73 cleared here? Is
1281f 20 69 74 20 6e 65 63 65 73 73 61 72 79 3f 20 2a it necessary? *
12820 2f 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e /. pPager->
12821 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
12822 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
12823 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 ->journalOff = 0
12824 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e ;. pPager->
12825 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 setMaster = 0;.
12826 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
12827 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a 20 rnalHdr = 0;. .
12828 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63 6b /* Playback
12829 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 and delete the
1282a 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20 74 journal. Drop t
1282b 68 65 20 64 61 74 61 62 61 73 65 20 77 72 69 74 he database writ
1282c 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 e. ** lock
1282d 61 6e 64 20 72 65 61 63 71 75 69 72 65 20 74 68 and reacquire th
1282e 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 75 72 e read lock. Pur
1282f 67 65 20 74 68 65 20 63 61 63 68 65 20 62 65 66 ge the cache bef
12830 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 6c 61 ore. ** pla
12831 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20 68 6f ying back the ho
12832 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 68 61 t-journal so tha
12833 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 20 75 t we don't end u
12834 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a 2a 20 p with. **
12835 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 an inconsistent
12836 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2f 0a cache.. */.
12837 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
12838 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 _playback(pPager
12839 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 1);. if(
1283a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1283b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 . rc = pa
1283c 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
1283d 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 67 , rc);. g
1283e 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
1283f 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
12840 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 ( (pPager->state
12841 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a ==PAGER_SHARED).
12842 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 || (p
12843 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
12844 4d 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e Mode && pPager->
12845 73 74 61 74 65 3e 50 41 47 45 52 5f 53 48 41 52 state>PAGER_SHAR
12846 45 44 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 ED). );.
12847 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }.. if( sqli
12848 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 te3PcachePagecou
12849 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
1284a 68 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f he)>0 ){. /
1284b 2a 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 * The shared-loc
1284c 6b 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 k has just been
1284d 61 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 acquired on the
1284e 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 database file.
1284f 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 ** and there
12850 20 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 are already pag
12851 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 es in the cache
12852 28 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 (from a previous
12853 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f . ** read o
12854 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 r write transact
12855 69 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 ion). Check to
12856 73 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62 see if the datab
12857 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 ase. ** has
12858 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 been modified.
12859 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
1285a 20 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c has changed, fl
1285b 75 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a ush the. **
1285c 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a cache.. **
1285d 0a 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 61 . ** Databa
1285e 73 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 65 se changes is de
1285f 74 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e tected by lookin
12860 67 20 61 74 20 31 35 20 62 79 74 65 73 20 62 65 g at 15 bytes be
12861 67 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a ginning. **
12862 20 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 6e at offset 24 in
12863 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 to the file. Th
12864 65 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 65 e first 4 of the
12865 73 65 20 31 36 20 62 79 74 65 73 20 61 72 65 0a se 16 bytes are.
12866 20 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 ** a 32-bi
12867 74 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69 t counter that i
12868 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 s incremented wi
12869 74 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20 th each change.
1286a 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 The. ** ot
1286b 68 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 her bytes change
1286c 20 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 randomly with e
1286d 61 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 ach file change
1286e 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 when. ** a
1286f 63 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e codec is in use.
12870 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 . ** .
12871 20 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 ** There is a v
12872 61 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c anishingly small
12873 20 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 63 chance that a c
12874 68 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 hange will not b
12875 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 65 e . ** dete
12876 63 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e 63 cted. The chanc
12877 65 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 74 e of an undetect
12878 65 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f 20 ed change is so
12879 73 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 small that.
1287a 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e 65 ** it can be ne
1287b 67 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 2a glected.. *
1287c 2f 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 46 /. char dbF
1287d 69 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70 ileVers[sizeof(p
1287e 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
1287f 73 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 s)];. sqlit
12880 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
12881 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 (pPager, 0);..
12882 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
12883 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 errCode ){.
12884 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e rc = pPager->
12885 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 errCode;.
12886 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
12887 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 }.. ass
12888 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 ert( pPager->dbS
12889 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 izeValid );.
1288a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 if( pPager->db
1288b 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 Size>0 ){.
1288c 20 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 IOTRACE(("CKVE
1288d 52 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 RS %p %d\n", pPa
1288e 67 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 ger, sizeof(dbFi
1288f 6c 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 leVers)));.
12890 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
12891 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 sRead(pPager->fd
12892 2c 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 , &dbFileVers, s
12893 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 izeof(dbFileVers
12894 29 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 ), 24);.
12895 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12896 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 K ){. g
12897 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
12898 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
12899 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 e{. memse
1289a 74 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c t(dbFileVers, 0,
1289b 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 sizeof(dbFileVe
1289c 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 rs));. }..
1289d 20 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 if( memcmp(
1289e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 pPager->dbFileVe
1289f 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20 rs, dbFileVers,
128a0 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 sizeof(dbFileVer
128a1 73 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 s))!=0 ){.
128a2 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 pager_reset(pP
128a3 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ager);. }.
128a4 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
128a5 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
128a6 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 veMode || pPager
128a7 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 ->state==PAGER_S
128a8 48 41 52 45 44 20 29 3b 0a 20 20 7d 0a 0a 20 66 HARED );. }.. f
128a9 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21 ailed:. if( rc!
128aa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
128ab 20 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63 /* pager_unloc
128ac 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 k() is a no-op f
128ad 6f 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 or exclusive mod
128ae 65 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 e and in-memory
128af 64 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20 databases. */.
128b0 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 pager_unlock(p
128b1 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 Pager);. }. re
128b2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
128b3 2a 20 49 66 20 74 68 65 20 72 65 66 65 72 65 6e * If the referen
128b4 63 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 61 ce count has rea
128b5 63 68 65 64 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62 ched zero, rollb
128b6 61 63 6b 20 61 6e 79 20 61 63 74 69 76 65 0a 2a ack any active.*
128b7 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e * transaction an
128b8 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 d unlock the pag
128b9 65 72 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 er..*/ .static v
128ba 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 oid pagerUnlockI
128bb 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a 70 fUnused(Pager *p
128bc 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 73 71 Pager){. if( sq
128bd 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
128be 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
128bf 63 68 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 che)==0 ){. p
128c0 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c agerUnlockAndRol
128c1 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 lback(pPager);.
128c2 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 }.}../*.** Drop
128c3 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
128c4 20 63 61 63 68 65 20 75 73 69 6e 67 20 73 71 6c cache using sql
128c5 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 29 ite3PcacheDrop()
128c6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 ..**.** If this
128c7 6d 65 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 means there are
128c8 6e 6f 77 20 6e 6f 20 70 61 67 65 73 20 77 69 74 now no pages wit
128c9 68 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 h references to
128ca 74 68 65 6d 2c 20 61 20 72 6f 6c 6c 62 61 63 6b them, a rollback
128cb 0a 2a 2a 20 6f 63 63 75 72 73 20 61 6e 64 20 74 .** occurs and t
128cc 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 he lock on the d
128cd 61 74 61 62 61 73 65 20 69 73 20 72 65 6d 6f 76 atabase is remov
128ce 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
128cf 69 64 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 id pagerDropPage
128d0 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 (DbPage *pPg){.
128d1 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
128d2 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
128d3 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f sqlite3PcacheDro
128d4 70 28 70 50 67 29 3b 0a 20 20 70 61 67 65 72 55 p(pPg);. pagerU
128d5 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50 nlockIfUnused(pP
128d6 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ager);.}../*.**
128d7 41 63 71 75 69 72 65 20 61 20 72 65 66 65 72 65 Acquire a refere
128d8 6e 63 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 nce to page numb
128d9 65 72 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 er pgno in pager
128da 20 70 50 61 67 65 72 20 28 61 20 70 61 67 65 0a pPager (a page.
128db 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 68 61 73 ** reference has
128dc 20 74 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20 type DbPage*).
128dd 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 If the requested
128de 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a reference is .*
128df 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f * successfully o
128e0 62 74 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63 btained, it is c
128e1 6f 70 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65 opied to *ppPage
128e2 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 and SQLITE_OK r
128e3 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
128e4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c his function cal
128e5 6c 73 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f ls pagerSharedLo
128e6 63 6b 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 61 ck() to obtain a
128e7 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 0a SHARED lock on.
128e8 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
128e9 66 69 6c 65 20 69 66 20 73 75 63 68 20 61 20 6c file if such a l
128ea 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 69 ock or greater i
128eb 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 s not already he
128ec 6c 64 2e 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20 ld..** This may
128ed 63 61 75 73 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 cause hot-journa
128ee 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 l rollback or a
128ef 63 61 63 68 65 20 70 75 72 67 65 2e 20 53 65 65 cache purge. See
128f0 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 61 62 6f comments.** abo
128f1 76 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 ve function page
128f2 72 53 68 61 72 65 64 4c 6f 63 6b 28 29 20 66 6f rSharedLock() fo
128f3 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a r details..**.**
128f4 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 If the requeste
128f5 64 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 d page is alread
128f6 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 y in the cache,
128f7 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 it is returned.
128f8 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 .** Otherwise, a
128f9 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 new page object
128fa 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
128fb 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 d populated with
128fc 20 64 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 72 data.** read fr
128fd 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
128fe 66 69 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 61 file. In some ca
128ff 73 65 73 2c 20 74 68 65 20 70 63 61 63 68 65 20 ses, the pcache
12900 6d 6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 module may.** ch
12901 6f 6f 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f oose not to allo
12902 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 cate a new page
12903 6f 62 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 72 object and may r
12904 65 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 euse an existing
12905 0a 2a 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 20 .** object with
12906 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 no outstanding r
12907 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a eferences..**.**
12908 20 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20 The extra data
12909 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 appended to a pa
1290a 67 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 ge is always ini
1290b 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f tialized to zero
1290c 73 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 s the .** first
1290d 74 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c time a page is l
1290e 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 oaded into memor
1290f 79 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 72 y. If the page r
12910 65 71 75 65 73 74 65 64 20 69 73 20 0a 2a 2a 20 equested is .**
12911 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 already in the c
12912 61 63 68 65 20 77 68 65 6e 20 74 68 69 73 20 66 ache when this f
12913 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
12914 64 2c 20 74 68 65 6e 20 74 68 65 20 65 78 74 72 d, then the extr
12915 61 0a 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 66 a.** data is lef
12916 74 20 61 73 20 69 74 20 77 61 73 20 77 68 65 6e t as it was when
12917 20 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 the page object
12918 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 0a was last used..
12919 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 **.** If the dat
1291a 61 62 61 73 65 20 69 6d 61 67 65 20 69 73 20 73 abase image is s
1291b 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 maller than the
1291c 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6f requested page o
1291d 72 20 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a r if a .** non-z
1291e 65 72 6f 20 76 61 6c 75 65 20 69 73 20 70 61 73 ero value is pas
1291f 73 65 64 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e sed as the noCon
12920 74 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 61 tent parameter a
12921 6e 64 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 65 nd the .** reque
12922 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 sted page is not
12923 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 already stored
12924 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 in the cache, th
12925 65 6e 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 6c en no .** actual
12926 20 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72 disk read occur
12927 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 s. In this case
12928 74 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 the memory image
12929 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 of the .** page
1292a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 is initialized
1292b 74 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a to all zeros. .*
1292c 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e *.** If noConten
1292d 74 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 t is true, it me
1292e 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e ans that we do n
1292f 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 ot care about th
12930 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 e contents.** of
12931 20 74 68 65 20 70 61 67 65 2e 20 54 68 69 73 20 the page. This
12932 6f 63 63 75 72 73 20 69 6e 20 74 77 6f 20 73 65 occurs in two se
12933 70 65 72 61 74 65 20 73 63 65 6e 61 72 69 6f 73 perate scenarios
12934 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 65 :.**.** a) Whe
12935 6e 20 72 65 61 64 69 6e 67 20 61 20 66 72 65 65 n reading a free
12936 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 -list leaf page
12937 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
12938 65 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 e, and.**.** b
12939 29 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 ) When a savepoi
1293a 6e 74 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c nt is being roll
1293b 65 64 20 62 61 63 6b 20 61 6e 64 20 77 65 20 6e ed back and we n
1293c 65 65 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 eed to load.**
1293d 20 20 20 20 61 20 6e 65 77 20 70 61 67 65 20 69 a new page i
1293e 6e 74 6f 20 74 68 65 20 63 61 63 68 65 20 74 6f nto the cache to
1293f 20 70 6f 70 75 6c 61 74 65 20 77 69 74 68 20 74 populate with t
12940 68 65 20 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 he data read.**
12941 20 20 20 20 20 66 72 6f 6d 20 74 68 65 20 73 61 from the sa
12942 76 65 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e vepoint journal.
12943 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 .**.** If noCont
12944 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 ent is true, the
12945 6e 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72 n the data retur
12946 6e 65 64 20 69 73 20 7a 65 72 6f 65 64 20 69 6e ned is zeroed in
12947 73 74 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e stead of.** bein
12948 67 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 g read from the
12949 64 61 74 61 62 61 73 65 2e 20 41 64 64 69 74 69 database. Additi
1294a 6f 6e 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 onally, the bits
1294b 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a corresponding.*
1294c 2a 20 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 * to pgno in Pag
1294d 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 er.pInJournal (b
1294e 69 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 itvec of pages a
1294f 6c 72 65 61 64 79 20 77 72 69 74 74 65 6e 20 74 lready written t
12950 6f 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c o the.** journal
12951 20 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50 file) and the P
12952 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 agerSavepoint.pI
12953 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 nSavepoint bitve
12954 63 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a cs of any open.*
12955 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 * savepoints are
12956 20 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 set. This means
12957 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
12958 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 61 74 made writable at
12959 20 61 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e any.** point in
1295a 20 74 68 65 20 66 75 74 75 72 65 2c 20 75 73 69 the future, usi
1295b 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c ng a call to sql
1295c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 ite3PagerWrite()
1295d 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a , its contents.*
1295e 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f * will not be jo
1295f 75 72 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 61 urnaled. This sa
12960 76 65 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 ves IO..**.** Th
12961 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 e acquisition mi
12962 67 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 ght fail for sev
12963 65 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 eral reasons. I
12964 6e 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 n all cases,.**
12965 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 an appropriate e
12966 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
12967 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 urned and *ppPag
12968 65 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c e is set to NULL
12969 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1296a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f sqlite3PagerLoo
1296b 6b 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 kup(). Both thi
1296c 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f s routine and Lo
1296d 6f 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a okup() attempt.*
1296e 2a 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 * to find a page
1296f 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 in the in-memor
12970 79 20 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 y cache first.
12971 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e If the page is n
12972 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e ot already.** in
12973 20 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f memory, this ro
12974 75 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 utine goes to di
12975 73 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e sk to read it in
12976 20 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 whereas Lookup(
12977 29 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e ).** just return
12978 73 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 s 0. This routi
12979 6e 65 20 61 63 71 75 69 72 65 73 20 61 20 72 65 ne acquires a re
1297a 61 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 ad-lock the firs
1297b 74 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 t time it.** has
1297c 20 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 to go to disk,
1297d 61 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 and could also p
1297e 6c 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a layback an old j
1297f 6f 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 ournal if necess
12980 61 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f ary..** Since Lo
12981 6f 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 okup() never goe
12982 73 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 s to disk, it ne
12983 76 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 ver has to deal
12984 77 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 with locks.** or
12985 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a journal files..
12986 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12987 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
12988 65 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 67 erAcquire(. Pag
12989 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 er *pPager,
1298a 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70 /* The pager op
1298b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 en on the databa
1298c 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e se file */. Pgn
1298d 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 o pgno,
1298e 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
1298f 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 to fetch */. Db
12990 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 Page **ppPage,
12991 20 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69 /* Write a poi
12992 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 nter to the page
12993 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e here */. int n
12994 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f oContent /
12995 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 * Do not bother
12996 72 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 reading content
12997 66 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75 from disk if tru
12998 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20 e */.){. PgHdr
12999 2a 70 50 67 20 3d 20 30 3b 0a 20 20 69 6e 74 20 *pPg = 0;. int
1299a 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 rc;.. assert( a
1299b 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 ssert_pager_stat
1299c 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61 e(pPager) );. a
1299d 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
1299e 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f tate==PAGER_UNLO
1299f 43 4b 20 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 CK . || sq
129a0 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
129a1 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
129a2 63 68 65 29 3e 30 20 0a 20 20 20 20 20 20 20 7c che)>0 . |
129a3 7c 20 70 67 6e 6f 3d 3d 31 0a 20 20 29 3b 0a 0a | pgno==1. );..
129a4 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d /* The maximum
129a5 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
129a6 32 5e 33 31 2e 20 52 65 74 75 72 6e 20 53 51 4c 2^31. Return SQL
129a7 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20 61 ITE_CORRUPT if a
129a8 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d 62 65 page. ** numbe
129a9 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 r greater than t
129aa 68 69 73 2c 20 6f 72 20 7a 65 72 6f 2c 20 69 73 his, or zero, is
129ab 20 72 65 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f requested.. */
129ac 0a 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 . if( pgno>PAGE
129ad 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 R_MAX_PGNO || pg
129ae 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 no==0 || pgno==P
129af 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
129b0 67 65 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ger) ){. retu
129b1 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
129b2 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f T_BKPT;. }.. /
129b3 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68 * Make sure we h
129b4 61 76 65 20 6e 6f 74 20 68 69 74 20 61 6e 79 20 ave not hit any
129b5 63 72 69 74 69 63 61 6c 20 65 72 72 6f 72 73 2e critical errors.
129b6 0a 20 20 2a 2f 20 0a 20 20 61 73 73 65 72 74 28 . */ . assert(
129b7 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 pPager!=0 );.
129b8 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 *ppPage = 0;..
129b9 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 /* If this is th
129ba 65 20 66 69 72 73 74 20 70 61 67 65 20 61 63 63 e first page acc
129bb 65 73 73 65 64 2c 20 74 68 65 6e 20 67 65 74 20 essed, then get
129bc 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 0a 20 20 a SHARED lock.
129bd 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ** on the databa
129be 73 65 20 66 69 6c 65 2e 20 70 61 67 65 72 53 68 se file. pagerSh
129bf 61 72 65 64 4c 6f 63 6b 28 29 20 69 73 20 61 20 aredLock() is a
129c0 6e 6f 2d 6f 70 20 69 66 20 0a 20 20 2a 2a 20 61 no-op if . ** a
129c1 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 69 database lock i
129c2 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a s already held..
129c3 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 */. rc = page
129c4 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 50 61 67 rSharedLock(pPag
129c5 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 er);. if( rc!=S
129c6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
129c7 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
129c8 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
129c9 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e >state!=PAGER_UN
129ca 4c 4f 43 4b 20 29 3b 0a 0a 20 20 72 63 20 3d 20 LOCK );.. rc =
129cb 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
129cc 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ch(pPager->pPCac
129cd 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 26 70 50 he, pgno, 1, &pP
129ce 67 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 g);. if( rc!=SQ
129cf 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
129d0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
129d1 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e assert( pPg->pgn
129d2 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 o==pgno );. ass
129d3 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 ert( pPg->pPager
129d4 3d 3d 70 50 61 67 65 72 20 7c 7c 20 70 50 67 2d ==pPager || pPg-
129d5 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b 0a 20 20 >pPager==0 );.
129d6 69 66 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d if( pPg->pPager=
129d7 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 =0 ){. /* The
129d8 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73 pager cache has
129d9 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70 created a new p
129da 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 age. Its content
129db 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a needs to . *
129dc 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 * be initialized
129dd 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 .. */. int
129de 20 6e 4d 61 78 3b 0a 20 20 20 20 50 41 47 45 52 nMax;. PAGER
129df 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d _INCR(pPager->nM
129e0 69 73 73 29 3b 0a 20 20 20 20 70 50 67 2d 3e 70 iss);. pPg->p
129e1 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a Pager = pPager;.
129e2 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
129e3 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
129e4 70 50 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b 0a pPager, &nMax);.
129e5 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
129e6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
129e7 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
129e8 28 70 50 67 29 3b 0a 20 20 20 20 20 20 72 65 74 (pPg);. ret
129e9 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 urn rc;. }..
129ea 20 20 20 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74 if( nMax<(int
129eb 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c )pgno || MEMDB |
129ec 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 | noContent ){.
129ed 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 if( pgno>pP
129ee 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a ager->mxPgno ){.
129ef 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
129f0 61 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a agerUnref(pPg);.
129f1 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
129f2 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 QLITE_FULL;.
129f3 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f }. if( no
129f4 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 Content ){.
129f5 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f /* Failure to
129f6 20 73 65 74 20 74 68 65 20 62 69 74 73 20 69 6e set the bits in
129f7 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 the InJournal b
129f8 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20 62 65 it-vectors is be
129f9 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a nign.. **
129fa 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73 It merely means
129fb 20 74 68 61 74 20 77 65 20 6d 69 67 68 74 20 64 that we might d
129fc 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77 6f 72 o some extra wor
129fd 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a k to journal a .
129fe 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 ** page
129ff 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 that does not ne
12a00 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c ed to be journal
12a01 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73 ed. Nevertheles
12a02 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20 20 20 s, be sure .
12a03 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 ** to test t
12a04 68 65 20 63 61 73 65 20 77 68 65 72 65 20 61 20 he case where a
12a05 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 malloc error occ
12a06 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 urs while trying
12a07 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20 20 20 to set .
12a08 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61 20 62 ** a bit in a b
12a09 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20 20 20 it vector..
12a0a 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 */. sq
12a0b 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
12a0c 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
12a0d 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 if( pgno<=pPag
12a0e 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 er->dbOrigSize )
12a0f 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45 53 54 {. TEST
12a10 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c ONLY( rc = ) sql
12a11 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50 ite3BitvecSet(pP
12a12 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
12a13 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 , pgno);.
12a14 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d testcase( rc=
12a15 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
12a16 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
12a17 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 TESTONLY( rc
12a18 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69 = ) addToSavepoi
12a19 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 ntBitvecs(pPager
12a1a 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 , pgno);.
12a1b 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 testcase( rc==S
12a1c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
12a1d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e sqlite3En
12a1e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
12a1f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
12a20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 memset(pPg
12a21 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 ->pData, 0, pPag
12a22 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 er->pageSize);.
12a23 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 }. IOT
12a24 52 41 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25 RACE(("ZERO %p %
12a25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 d\n", pPager, pg
12a26 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b no));. }else{
12a27 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
12a28 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 Pg->pPager==pPag
12a29 65 72 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d er );. rc =
12a2a 20 72 65 61 64 44 62 50 61 67 65 28 70 50 67 29 readDbPage(pPg)
12a2b 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
12a2c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
12a2d 20 20 20 20 20 70 61 67 65 72 44 72 6f 70 50 61 pagerDropPa
12a2e 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 ge(pPg);.
12a2f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
12a30 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 }. }.#ifdef
12a31 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
12a32 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 GES. pPg->pag
12a33 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 eHash = pager_pa
12a34 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 6e gehash(pPg);.#en
12a35 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 dif. }else{.
12a36 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 /* The requeste
12a37 64 20 70 61 67 65 20 69 73 20 69 6e 20 74 68 65 d page is in the
12a38 20 70 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a page cache. */.
12a39 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 PAGER_INCR(p
12a3a 50 61 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 Pager->nHit);.
12a3b 7d 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 }.. *ppPage = p
12a3c 50 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c Pg;. return SQL
12a3d 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
12a3e 20 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20 Acquire a page
12a3f 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 if it is already
12a40 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 in the in-memor
12a41 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 y cache. Do.**
12a42 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67 not read the pag
12a43 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 e from disk. Re
12a44 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
12a45 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f o the page,.** o
12a46 72 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20 r 0 if the page
12a47 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e is not in cache.
12a48 20 41 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 Also, return 0
12a49 69 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72 if the .** pager
12a4a 20 69 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c is in PAGER_UNL
12a4b 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 OCK state when t
12a4c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
12a4d 63 61 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 called,.** or if
12a4e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
12a4f 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 an error state
12a50 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 other than SQLIT
12a51 45 5f 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 E_FULL..**.** Se
12a52 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 e also sqlite3Pa
12a53 67 65 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 gerGet(). The d
12a54 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
12a55 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a n this routine.*
12a56 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 * and sqlite3Pag
12a57 65 72 47 65 74 28 29 20 69 73 20 74 68 61 74 20 erGet() is that
12a58 5f 67 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 _get() will go t
12a59 6f 20 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 o the disk and r
12a5a 65 61 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 ead.** in the pa
12a5b 67 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69 ge if the page i
12a5c 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e s not already in
12a5d 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f cache. This ro
12a5e 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 utine.** returns
12a5f 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 NULL if the pag
12a60 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 e is not in cach
12a61 65 20 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 e or if a disk I
12a62 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 /O error .** has
12a63 20 65 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a ever happened..
12a64 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12a65 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 E DbPage *sqlite
12a66 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 3PagerLookup(Pag
12a67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
12a68 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 pgno){. PgHdr
12a69 2a 70 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 *pPg = 0;. asse
12a6a 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b rt( pPager!=0 );
12a6b 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 . assert( pgno!
12a6c 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 70 50 =0 );.. if( (pP
12a6d 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
12a6e 45 52 5f 55 4e 4c 4f 43 4b 29 0a 20 20 20 26 26 ER_UNLOCK). &&
12a6f 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 (pPager->errCod
12a70 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 e==SQLITE_OK ||
12a71 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d pPager->errCode=
12a72 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29 0a 20 20 =SQLITE_FULL).
12a73 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 ){. sqlite3Pc
12a74 61 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 acheFetch(pPager
12a75 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c ->pPCache, pgno,
12a76 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 7d 0a 0a 0, &pPg);. }..
12a77 20 20 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a return pPg;.}.
12a78 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
12a79 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e page reference.
12a7a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 .**.** If the nu
12a7b 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 mber of referenc
12a7c 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 es to the page d
12a7d 72 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 rop to zero, the
12a7e 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 n the.** page is
12a7f 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 added to the LR
12a80 55 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c U list. When al
12a81 6c 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 l references to
12a82 61 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 all pages.** are
12a83 20 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c released, a rol
12a84 6c 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 lback occurs and
12a85 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 the lock on the
12a86 20 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 database is.**
12a87 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 removed..*/.SQLI
12a88 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12a89 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
12a8a 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a f(DbPage *pPg){.
12a8b 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 if( pPg ){.
12a8c 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
12a8d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
12a8e 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 sqlite3PcacheR
12a8f 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 elease(pPg);.
12a90 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e pagerUnlockIfUn
12a91 75 73 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 used(pPager);.
12a92 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 }.}../*.** If th
12a93 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 e main journal f
12a94 69 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 ile has already
12a95 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 been opened, ens
12a96 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ure that the.**
12a97 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 sub-journal file
12a98 20 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 is open too. If
12a99 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
12a9a 6c 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a l is not open,.*
12a9b 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
12a9c 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
12a9d 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
12a9e 65 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 eturned if every
12a9f 74 68 69 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 thing goes accor
12aa0 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a ding to plan. .*
12aa1 2a 20 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 * An SQLITE_IOER
12aa2 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 R_XXX error code
12aa3 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
12aa4 61 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 a call to .** sq
12aa5 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 lite3OsOpen() fa
12aa6 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ils..*/.static i
12aa7 6e 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 nt openSubJourna
12aa8 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 l(Pager *pPager)
12aa9 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
12aaa 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 ITE_OK;. if( is
12aab 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
12aac 29 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 ) && !isOpen(pPa
12aad 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 ger->sjfd) ){.
12aae 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f if( pPager->jo
12aaf 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
12ab0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
12ab1 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ORY ){. sql
12ab2 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 ite3MemJournalOp
12ab3 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 en(pPager->sjfd)
12ab4 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
12ab5 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 rc = pagerOpe
12ab6 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 ntemp(pPager, pP
12ab7 61 67 65 72 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 ager->sjfd, SQLI
12ab8 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e TE_OPEN_SUBJOURN
12ab9 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 AL);. }. }.
12aba 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
12abb 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
12abc 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 74 20 on is called at
12abd 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 76 65 the start of eve
12abe 72 79 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 ry write transac
12abf 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 6d tion..** There m
12ac0 75 73 74 20 61 6c 72 65 61 64 79 20 62 65 20 61 ust already be a
12ac1 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 RESERVED or EXC
12ac2 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 LUSIVE lock on t
12ac3 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 he database .**
12ac4 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 file when this r
12ac5 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
12ac6 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 ..**.** Open the
12ac7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f journal file fo
12ac8 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20 61 r pager pPager a
12ac9 6e 64 20 77 72 69 74 65 20 61 20 6a 6f 75 72 6e nd write a journ
12aca 61 6c 20 68 65 61 64 65 72 0a 2a 2a 20 74 6f 20 al header.** to
12acb 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 2e the start of it.
12acc 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 63 If there are ac
12acd 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 2c tive savepoints,
12ace 20 6f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f open the sub-jo
12acf 75 72 6e 61 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c urnal.** as well
12ad0 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
12ad1 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 is only used whe
12ad2 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
12ad3 6c 65 20 69 73 20 62 65 69 6e 67 20 0a 2a 2a 20 le is being .**
12ad4 6f 70 65 6e 65 64 20 74 6f 20 77 72 69 74 65 20 opened to write
12ad5 61 20 72 6f 6c 6c 62 61 63 6b 20 6c 6f 67 20 66 a rollback log f
12ad6 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e or a transaction
12ad7 2e 20 49 74 20 69 73 20 6e 6f 74 20 75 73 65 64 . It is not used
12ad8 20 0a 2a 2a 20 77 68 65 6e 20 6f 70 65 6e 69 6e .** when openin
12ad9 67 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 g a hot journal
12ada 66 69 6c 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 file to roll it
12adb 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 back..**.** If t
12adc 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
12add 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 is already open
12ade 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 69 6e (as it may be in
12adf 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 exclusive mode)
12ae0 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 66 ,.** then this f
12ae1 75 6e 63 74 69 6f 6e 20 6a 75 73 74 20 77 72 69 unction just wri
12ae2 74 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 68 65 tes a journal he
12ae3 61 64 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 ader to the star
12ae4 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 72 65 t of the.** alre
12ae5 61 64 79 20 6f 70 65 6e 20 66 69 6c 65 2e 20 0a ady open file. .
12ae6 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 **.** Whether or
12ae7 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c not the journal
12ae8 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 file is opened
12ae9 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e by this function
12aea 2c 20 74 68 65 0a 2a 2a 20 50 61 67 65 72 2e 70 , the.** Pager.p
12aeb 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 InJournal bitvec
12aec 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c structure is al
12aed 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 located..**.** R
12aee 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
12aef 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 if everything is
12af0 20 73 75 63 63 65 73 73 66 75 6c 2e 20 4f 74 68 successful. Oth
12af1 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 0a erwise, return .
12af2 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 ** SQLITE_NOMEM
12af3 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 if the attempt t
12af4 6f 20 61 6c 6c 6f 63 61 74 65 20 50 61 67 65 72 o allocate Pager
12af5 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 66 61 69 6c .pInJournal fail
12af6 73 2c 20 6f 72 20 0a 2a 2a 20 61 6e 20 49 4f 20 s, or .** an IO
12af7 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 6f 70 error code if op
12af8 65 6e 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 ening or writing
12af9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12afa 65 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 e fails..*/.stat
12afb 69 63 20 69 6e 74 20 70 61 67 65 72 5f 6f 70 65 ic int pager_ope
12afc 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 n_journal(Pager
12afd 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 *pPager){. int
12afe 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
12aff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12b00 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
12b01 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 code */. sqlit
12b02 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 e3_vfs * const p
12b03 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 Vfs = pPager->pV
12b04 66 73 3b 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 fs; /* Local c
12b05 61 63 68 65 20 6f 66 20 76 66 73 20 70 6f 69 6e ache of vfs poin
12b06 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ter */.. assert
12b07 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
12b08 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 =PAGER_RESERVED
12b09 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
12b0a 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
12b0b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
12b0c 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d ger->pInJournal=
12b0d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 =0 );. . /* If
12b0e 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
12b0f 65 72 72 6f 72 20 73 74 61 74 65 2c 20 74 68 69 error state, thi
12b10 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
12b11 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 no-op. */. if(
12b12 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
12b13 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 ){. return pP
12b14 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
12b15 20 7d 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 }.. /* TODO: I
12b16 73 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73 73 s it really poss
12b17 69 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72 65 ible to get here
12b18 20 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c 69 with dbSizeVali
12b19 64 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20 20 d==0? If not,.
12b1a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 50 ** the call to P
12b1b 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 agerPagecount()
12b1c 63 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e 0a can be removed..
12b1d 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 */. testcase(
12b1e 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
12b1f 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71 6c alid==0 );. sql
12b20 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
12b21 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a nt(pPager, 0);..
12b22 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 pPager->pInJou
12b23 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 rnal = sqlite3Bi
12b24 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65 tvecCreate(pPage
12b25 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 r->dbSize);. if
12b26 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ( pPager->pInJou
12b27 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 rnal==0 ){. r
12b28 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
12b29 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 EM;. }.. /* Op
12b2a 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
12b2b 69 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f 74 ile if it is not
12b2c 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 2a already open. *
12b2d 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 /. if( !isOpen(
12b2e 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a pPager->jfd) ){.
12b2f 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
12b30 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
12b31 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
12b32 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 73 EMORY ){. s
12b33 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
12b34 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
12b35 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
12b36 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c const int fl
12b37 61 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 20 ags =
12b38 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 /* VFS f
12b39 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f 75 lags to open jou
12b3a 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 rnal file */.
12b3b 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
12b3c 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
12b3d 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a 20 E_OPEN_CREATE|.
12b3e 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e (pPager->
12b3f 74 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 20 tempFile ? .
12b40 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 (SQLITE_OP
12b41 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
12b42 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d |SQLITE_OPEN_TEM
12b43 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 20 P_JOURNAL):.
12b44 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 (SQLITE_OP
12b45 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 EN_MAIN_JOURNAL)
12b46 0a 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66 64 . );.#ifd
12b47 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
12b48 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 _ATOMIC_WRITE.
12b49 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12b4a 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 JournalOpen(.
12b4b 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61 pVfs, pPa
12b4c 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 ger->zJournal, p
12b4d 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 Pager->jfd, flag
12b4e 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a s, jrnlBufferSiz
12b4f 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20 e(pPager).
12b50 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 72 );.#else. r
12b51 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
12b52 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e n(pVfs, pPager->
12b53 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 zJournal, pPager
12b54 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 ->jfd, flags, 0)
12b55 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 ;.#endif. }.
12b56 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
12b57 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 QLITE_OK || isOp
12b58 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
12b59 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72 );. }... /* Wr
12b5a 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f ite the first jo
12b5b 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 urnal header to
12b5c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12b5d 20 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a 20 and open . **
12b5e 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
12b5f 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 if necessary..
12b60 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c */. if( rc==SQL
12b61 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a ITE_OK ){. /*
12b62 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66 20 TODO: Check if
12b63 61 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 65 all of these are
12b64 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64 really required
12b65 2e 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d . */. pPager-
12b66 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50 >dbOrigSize = pP
12b67 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 ager->dbSize;.
12b68 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
12b69 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 lStarted = 0;.
12b6a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 pPager->needSy
12b6b 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 nc = 0;. pPag
12b6c 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 er->nRec = 0;.
12b6d 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
12b6e 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 50 lOff = 0;. pP
12b6f 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 ager->setMaster
12b70 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d = 0;. pPager-
12b71 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b >journalHdr = 0;
12b72 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4a . rc = writeJ
12b73 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 ournalHdr(pPager
12b74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
12b75 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 =SQLITE_OK && pP
12b76 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
12b77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 ){. rc = ope
12b78 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 nSubJournal(pPag
12b79 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 er);. }.. if(
12b7a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
12b7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 . sqlite3Bitv
12b7c 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 ecDestroy(pPager
12b7d 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 ->pInJournal);.
12b7e 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f pPager->pInJo
12b7f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 urnal = 0;. }.
12b80 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
12b81 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 69 *.** Begin a wri
12b82 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te-transaction o
12b83 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 n the specified
12b84 70 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 pager object. If
12b85 20 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 a .** write-tra
12b86 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 nsaction has alr
12b87 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 eady been opened
12b88 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
12b89 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
12b8a 2a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 * If the exFlag
12b8b 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 argument is fals
12b8c 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 e, then acquire
12b8d 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52 at least a RESER
12b8e 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 VED.** lock on t
12b8f 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12b90 2e 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 . If exFlag is t
12b91 72 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 rue, then acquir
12b92 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 6e e at least.** an
12b93 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
12b94 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
12b95 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c is already held,
12b96 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 no locking .**
12b97 66 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 62 functions need b
12b98 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 e called..**.**
12b99 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 If this is not a
12b9a 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 6e temporary or in
12b9b 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e 64 -memory file and
12b9c 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 , the journal fi
12b9d 6c 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65 64 le is .** opened
12b9e 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 if it has not b
12b9f 65 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f 72 een already. For
12ba0 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c a temporary fil
12ba1 65 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 0a e, the opening .
12ba2 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** of the journa
12ba3 6c 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 l file is deferr
12ba4 65 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 ed until there i
12ba5 73 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65 64 s an actual need
12ba6 20 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74 6f to .** write to
12ba7 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 4f the journal. TO
12ba8 44 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20 74 DO: Why handle t
12ba9 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 64 emporary files d
12baa 69 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a 2a ifferently?.**.*
12bab 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * If the journal
12bac 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 file is opened
12bad 28 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c 72 (or if it is alr
12bae 65 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65 6e eady open), then
12baf 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68 65 a.** journal-he
12bb0 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 ader is written
12bb1 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 to the start of
12bb2 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 it..*/.SQLITE_PR
12bb3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
12bb4 33 50 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 3PagerBegin(Page
12bb5 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 r *pPager, int e
12bb6 78 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 72 63 xFlag){. int rc
12bb7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
12bb8 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
12bb9 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c state!=PAGER_UNL
12bba 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 61 OCK );. if( pPa
12bbb 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
12bbc 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20 R_SHARED ){.
12bbd 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
12bbe 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b pInJournal==0 );
12bbf 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 . assert( !ME
12bc0 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 2d 3e MDB && !pPager->
12bc1 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 20 tempFile );..
12bc2 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 53 /* Obtain a RES
12bc3 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 ERVED lock on th
12bc4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
12bc5 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 70 If the exFlag p
12bc6 61 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a 20 arameter. **
12bc7 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69 6d is true, then im
12bc8 6d 65 64 69 61 74 65 6c 79 20 75 70 67 72 61 64 mediately upgrad
12bc9 65 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58 43 e this to an EXC
12bca 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 LUSIVE lock. The
12bcb 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e . ** busy-han
12bcc 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 61 dler callback ca
12bcd 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 75 n be used when u
12bce 70 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 20 pgrading to the
12bcf 45 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a 2a EXCLUSIVE. **
12bd0 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77 lock, but not w
12bd1 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 hen obtaining th
12bd2 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e e RESERVED lock.
12bd3 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
12bd4 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 sqlite3OsLock(p
12bd5 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 Pager->fd, RESER
12bd6 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 VED_LOCK);. i
12bd7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12bd8 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
12bd9 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
12bda 52 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20 20 RESERVED;.
12bdb 69 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 if( exFlag ){.
12bdc 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
12bdd 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 _wait_on_lock(pP
12bde 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f ager, EXCLUSIVE_
12bdf 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 LOCK);. }.
12be0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
12be1 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 the required loc
12be2 6b 73 20 77 65 72 65 20 73 75 63 63 65 73 73 66 ks were successf
12be3 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 6f ully obtained, o
12be4 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a pen the journal.
12be5 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 20 ** file and
12be6 77 72 69 74 65 20 74 68 65 20 66 69 72 73 74 20 write the first
12be7 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 74 journal-header t
12be8 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 o it.. */.
12be9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
12bea 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 OK && pPager->us
12beb 65 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 26 26 eJournal. &&
12bec 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12bed 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode!=PAGER_JOUR
12bee 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 NALMODE_OFF .
12bef 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 ){. rc = p
12bf0 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 ager_open_journa
12bf1 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d l(pPager);. }
12bf2 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4f . }else if( isO
12bf3 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
12bf4 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
12bf5 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 nalOff==0 ){.
12bf6 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 /* This happens
12bf7 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 when the pager
12bf8 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 was in exclusive
12bf9 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 -access mode the
12bfa 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d last. ** tim
12bfb 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72 69 e a (read or wri
12bfc 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 te) transaction
12bfd 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 was successfully
12bfe 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a concluded. *
12bff 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 * by this connec
12c00 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 tion. Instead of
12c01 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f deleting the jo
12c02 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 61 urnal file it wa
12c03 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f s . ** kept o
12c04 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20 77 pen and either w
12c05 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 as truncated to
12c06 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20 68 0 bytes or its h
12c07 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a 2a eader was. **
12c08 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 overwritten wit
12c09 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a h zeros.. */.
12c0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
12c0b 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 er->nRec==0 );.
12c0c 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
12c0d 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30 r->dbOrigSize==0
12c0e 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
12c0f 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
12c10 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 al==0 );. rc
12c11 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 = pager_open_jou
12c12 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 rnal(pPager);.
12c13 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 }.. PAGERTRACE(
12c14 28 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 ("TRANSACTION %d
12c15 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
12c16 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 ger)));. assert
12c17 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 ( !isOpen(pPager
12c18 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 ->jfd) || pPager
12c19 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c ->journalOff>0 |
12c1a 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 | rc!=SQLITE_OK
12c1b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
12c1c 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 }../*.** Mark a
12c1d 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 single data page
12c1e 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 as writeable. T
12c1f 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 he page is writt
12c20 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 en into the .**
12c21 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 main journal or
12c22 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 sub-journal as r
12c23 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 equired. If the
12c24 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 page is written
12c25 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 into.** one of t
12c26 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 he journals, the
12c27 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 corresponding b
12c28 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 it is set in the
12c29 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f .** Pager.pInJo
12c2a 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 urnal bitvec and
12c2b 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f the PagerSavepo
12c2c 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 int.pInSavepoint
12c2d 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 bitvecs.** of a
12c2e 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e ny open savepoin
12c2f 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 ts as appropriat
12c30 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
12c31 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 pager_write(PgH
12c32 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 dr *pPg){. void
12c33 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 *pData = pPg->p
12c34 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 Data;. Pager *p
12c35 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
12c36 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ger;. int rc =
12c37 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
12c38 20 43 68 65 63 6b 20 66 6f 72 20 65 72 72 6f 72 Check for error
12c39 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 s. */. if( pPa
12c3a 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 20 ger->errCode ){
12c3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 . return pPag
12c3c 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d er->errCode;. }
12c3d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 72 . if( pPager->r
12c3e 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 eadOnly ){. r
12c3f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 eturn SQLITE_PER
12c40 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 M;. }.. assert
12c41 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 ( !pPager->setMa
12c42 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b ster );.. CHECK
12c43 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f _PAGE(pPg);.. /
12c44 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 * Mark the page
12c45 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 as dirty. If th
12c46 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 e page has alrea
12c47 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a dy been written.
12c48 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 ** to the jour
12c49 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 nal then we can
12c4a 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 return right awa
12c4b 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 y.. */. sqlite
12c4c 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
12c4d 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67 (pPg);. if( pag
12c4e 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 eInJournal(pPg)
12c4f 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 && !subjRequires
12c50 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 Page(pPg) ){.
12c51 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 pPager->dbModif
12c52 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 ied = 1;. }else
12c53 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 {.. /* If we
12c54 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 get this far, it
12c55 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
12c56 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 page needs to be
12c57 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 . ** written
12c58 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 to the transacti
12c59 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 on journal or th
12c5a 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 e ckeckpoint jou
12c5b 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 rnal. ** or b
12c5c 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 oth.. **.
12c5d 2a 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74 ** First check t
12c5e 6f 20 73 65 65 20 74 68 61 74 20 74 68 65 20 74 o see that the t
12c5f 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e ransaction journ
12c60 61 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20 20 al exists and.
12c61 20 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20 69 ** create it i
12c62 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 f it does not..
12c63 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
12c64 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 ( pPager->state!
12c65 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b =PAGER_UNLOCK );
12c66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
12c67 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 61 67 3PagerBegin(pPag
12c68 65 72 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 er, 0);. if(
12c69 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
12c6a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
12c6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
12c6c 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
12c6d 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 e>=PAGER_RESERVE
12c6e 44 20 29 3b 0a 20 20 20 20 69 66 28 20 21 69 73 D );. if( !is
12c6f 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
12c70 29 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 ) && pPager->use
12c71 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 Journal.
12c72 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 && pPager->jou
12c73 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f rnalMode!=PAGER_
12c74 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 JOURNALMODE_OFF
12c75 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 ){. rc = pa
12c76 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c ger_open_journal
12c77 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
12c78 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12c79 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
12c7a 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d }. pPager-
12c7b 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b >dbModified = 1;
12c7c 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 . . /* The t
12c7d 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e ransaction journ
12c7e 61 6c 20 6e 6f 77 20 65 78 69 73 74 73 20 61 6e al now exists an
12c7f 64 20 77 65 20 68 61 76 65 20 61 20 52 45 53 45 d we have a RESE
12c80 52 56 45 44 20 6f 72 20 61 6e 0a 20 20 20 20 2a RVED or an. *
12c81 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b * EXCLUSIVE lock
12c82 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 on the main dat
12c83 61 62 61 73 65 20 66 69 6c 65 2e 20 20 57 72 69 abase file. Wri
12c84 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 te the current p
12c85 61 67 65 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 age to. ** th
12c86 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f e transaction jo
12c87 75 72 6e 61 6c 20 69 66 20 69 74 20 69 73 20 6e urnal if it is n
12c88 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 ot there already
12c89 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
12c8a 20 21 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 !pageInJournal(
12c8b 70 50 67 29 20 26 26 20 69 73 4f 70 65 6e 28 70 pPg) && isOpen(p
12c8c 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
12c8d 20 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 if( pPg->pg
12c8e 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 no<=pPager->dbOr
12c8f 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 igSize ){.
12c90 20 20 75 33 32 20 63 6b 73 75 6d 3b 0a 20 20 20 u32 cksum;.
12c91 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 char *pData
12c92 32 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 2;.. /* W
12c93 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 77 e should never w
12c94 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 rite to the jour
12c95 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 70 61 67 nal file the pag
12c96 65 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a e that. *
12c97 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 * contains the d
12c98 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 2e 20 20 atabase locks.
12c99 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 The following as
12c9a 73 65 72 74 20 76 65 72 69 66 69 65 73 0a 20 20 sert verifies.
12c9b 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 ** that we
12c9c 20 64 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 do not. */.
12c9d 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d assert( pPg-
12c9e 3e 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f >pgno!=PAGER_MJ_
12c9f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a PGNO(pPager) );.
12ca0 20 20 20 20 20 20 20 20 70 44 61 74 61 32 20 3d pData2 =
12ca1 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 CODEC2(pPager,
12ca2 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f pData, pPg->pgno
12ca3 2c 20 37 29 3b 0a 20 20 20 20 20 20 20 20 63 6b , 7);. ck
12ca4 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 sum = pager_cksu
12ca5 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70 m(pPager, (u8*)p
12ca6 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20 Data2);.
12ca7 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 rc = write32bits
12ca8 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
12ca9 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
12caa 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 , pPg->pgno);.
12cab 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
12cac 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12cad 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
12cae 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
12caf 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 >jfd, pData2, pP
12cb0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a ager->pageSize,.
12cb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12cb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 pP
12cb3 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
12cb4 20 2b 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 + 4);.
12cb5 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12cb6 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 Off += pPager->p
12cb7 61 67 65 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20 ageSize+4;.
12cb8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
12cb9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
12cba 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
12cbb 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
12cbc 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 ger->jfd, pPager
12cbd 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 63 6b ->journalOff, ck
12cbe 73 75 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 sum);.
12cbf 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12cc0 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 ff += 4;.
12cc1 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 }. IOTRA
12cc2 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64 20 CE(("JOUT %p %d
12cc3 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 %lld %d\n", pPag
12cc4 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a er, pPg->pgno, .
12cc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12cc6 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12cc7 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 Off, pPager->pag
12cc8 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20 20 eSize));.
12cc9 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 PAGER_INCR(sqli
12cca 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a te3_pager_writej
12ccb 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20 _count);.
12ccc 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4a 4f PAGERTRACE(("JO
12ccd 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 URNAL %d page %d
12cce 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73 needSync=%d has
12ccf 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 h(%08x)\n",.
12cd0 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 PAGERID
12cd1 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
12cd2 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 gno, .
12cd3 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 ((pPg->flags&
12cd4 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
12cd5 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61 67 ?1:0), pager_pag
12cd6 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 ehash(pPg)));..
12cd7 20 20 20 20 20 20 20 2f 2a 20 45 76 65 6e 20 69 /* Even i
12cd8 66 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 6b 66 f an IO or diskf
12cd9 75 6c 6c 20 65 72 72 6f 72 20 6f 63 63 75 72 72 ull error occurr
12cda 65 64 20 77 68 69 6c 65 20 6a 6f 75 72 6e 61 6c ed while journal
12cdb 6c 69 6e 67 20 74 68 65 0a 20 20 20 20 20 20 20 ling the.
12cdc 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 ** page in the
12cdd 62 6c 6f 63 6b 20 61 62 6f 76 65 2c 20 73 65 74 block above, set
12cde 20 74 68 65 20 6e 65 65 64 2d 73 79 6e 63 20 66 the need-sync f
12cdf 6c 61 67 20 66 6f 72 20 74 68 65 20 70 61 67 65 lag for the page
12ce0 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 .. ** Oth
12ce1 65 72 77 69 73 65 2c 20 77 68 65 6e 20 74 68 65 erwise, when the
12ce2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
12ce3 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 rolled back, the
12ce4 20 6c 6f 67 69 63 20 69 6e 0a 20 20 20 20 20 20 logic in.
12ce5 20 20 2a 2a 20 70 6c 61 79 62 61 63 6b 5f 6f 6e ** playback_on
12ce6 65 5f 70 61 67 65 28 29 20 77 69 6c 6c 20 74 68 e_page() will th
12ce7 69 6e 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 ink that the pag
12ce8 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 e needs to be re
12ce9 73 74 6f 72 65 64 0a 20 20 20 20 20 20 20 20 2a stored. *
12cea 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 * in the databas
12ceb 65 20 66 69 6c 65 2e 20 41 6e 64 20 69 66 20 61 e file. And if a
12cec 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
12ced 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f s while doing so
12cee 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 ,. ** the
12cef 6e 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79 n corruption may
12cf0 20 66 6f 6c 6c 6f 77 2e 0a 20 20 20 20 20 20 20 follow..
12cf1 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
12cf2 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 !pPager->noSync
12cf3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 ){. pPg
12cf4 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
12cf5 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 _NEED_SYNC;.
12cf6 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 pPager->ne
12cf7 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
12cf8 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f }.. /
12cf9 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f * An error has o
12cfa 63 63 75 72 72 65 64 20 77 72 69 74 69 6e 67 20 ccurred writing
12cfb 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
12cfc 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20 20 20 ile. The .
12cfd 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
12cfe 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 will be rolled
12cff 62 61 63 6b 20 62 79 20 74 68 65 20 6c 61 79 65 back by the laye
12d00 72 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 r above..
12d01 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
12d02 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
12d03 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
12d04 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
12d05 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
12d06 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 >nRec++;.
12d07 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
12d08 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 >pInJournal!=0 )
12d09 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
12d0a 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
12d0b 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
12d0c 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a al, pPg->pgno);.
12d0d 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
12d0e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
12d0f 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 EM );. as
12d10 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
12d11 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 _OK || rc==SQLIT
12d12 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
12d13 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f 53 61 rc |= addToSa
12d14 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 vepointBitvecs(p
12d15 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
12d16 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
12d17 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
12d18 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
12d19 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
12d1a 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 EM );.
12d1b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
12d1c 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
12d1d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 {. if( !p
12d1e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 Pager->journalSt
12d1f 61 72 74 65 64 20 26 26 20 21 70 50 61 67 65 72 arted && !pPager
12d20 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 ->noSync ){.
12d21 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 pPg->flags
12d22 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 |= PGHDR_NEED_S
12d23 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70 YNC;. p
12d24 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
12d25 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
12d26 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
12d27 45 28 28 22 41 50 50 45 4e 44 20 25 64 20 70 61 E(("APPEND %d pa
12d28 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 ge %d needSync=%
12d29 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
12d2a 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 PAGERID(pP
12d2b 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f ager), pPg->pgno
12d2c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
12d2d 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 ((pPg->flags&PG
12d2e 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 HDR_NEED_SYNC)?1
12d2f 3a 30 29 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 :0)));. }.
12d30 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 }. . /* I
12d31 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
12d32 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 journal is open
12d33 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 and the page is
12d34 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a not in it,. *
12d35 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 * then write the
12d36 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f current page to
12d37 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a the statement j
12d38 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68 ournal. Note th
12d39 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74 at. ** the st
12d3a 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 atement journal
12d3b 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73 20 66 format differs f
12d3c 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 rom the standard
12d3d 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a journal format.
12d3e 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74 20 69 ** in that i
12d3f 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68 65 63 t omits the chec
12d40 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20 68 65 ksums and the he
12d41 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ader.. */.
12d42 20 69 66 28 20 73 75 62 6a 52 65 71 75 69 72 65 if( subjRequire
12d43 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 sPage(pPg) ){.
12d44 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 rc = subjour
12d45 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 20 nalPage(pPg);.
12d46 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 }. }.. /* Up
12d47 64 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 date the databas
12d48 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72 e size and retur
12d49 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 n.. */. assert
12d4a 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
12d4b 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b =PAGER_SHARED );
12d4c 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 . if( pPager->d
12d4d 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20 bSize<pPg->pgno
12d4e 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 ){. pPager->d
12d4f 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e bSize = pPg->pgn
12d50 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 o;. }. return
12d51 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 rc;.}../*.** Mar
12d52 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 61 73 k a data page as
12d53 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 69 73 writeable. This
12d54 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 routine must be
12d55 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a called before .
12d56 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 ** making change
12d57 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 68 65 s to a page. The
12d58 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 68 65 caller must che
12d59 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ck the return va
12d5a 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20 lue .** of this
12d5b 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 65 20 function and be
12d5c 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63 careful not to c
12d5d 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64 hange any page d
12d5e 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74 ata unless .** t
12d5f 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
12d60 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a rns SQLITE_OK..*
12d61 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 *.** The differe
12d62 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 nce between this
12d63 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61 function and pa
12d64 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 20 74 ger_write() is t
12d65 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 hat this.** func
12d66 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20 tion also deals
12d67 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c with the special
12d68 20 63 61 73 65 20 77 68 65 72 65 20 32 20 6f 72 case where 2 or
12d69 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a 20 66 more pages.** f
12d6a 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 it on a single d
12d6b 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e 20 74 isk sector. In t
12d6c 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d his case all co-
12d6d 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 0a 2a resident pages.*
12d6e 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e * must have been
12d6f 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
12d70 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66 journal file bef
12d71 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
12d72 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
12d73 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f occurs, SQLITE_
12d74 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 NOMEM or an IO e
12d75 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
12d76 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 70 72 urned.** as appr
12d77 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 77 69 opriate. Otherwi
12d78 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a se, SQLITE_OK..*
12d79 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
12d7a 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
12d7b 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70 rWrite(DbPage *p
12d7c 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 DbPage){. int r
12d7d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
12d7e 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70 PgHdr *pPg = p
12d7f 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 DbPage;. Pager
12d80 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
12d81 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50 Pager;. Pgno nP
12d82 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d 20 28 agePerSector = (
12d83 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
12d84 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 53 ze/pPager->pageS
12d85 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61 ize);.. if( nPa
12d86 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20 29 7b gePerSector>1 ){
12d87 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 43 . Pgno nPageC
12d88 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f ount; /
12d89 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
12d8a 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 f pages in datab
12d8b 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 ase file */.
12d8c 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 20 20 Pgno pg1;
12d8d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
12d8e 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73 st page of the s
12d8f 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 ector pPg is loc
12d90 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 ated on. */.
12d91 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 int nPage;
12d92 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
12d93 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 61 ber of pages sta
12d94 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 6f 20 rting at pg1 to
12d95 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69 journal */. i
12d96 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
12d97 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
12d98 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
12d99 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 int needSync = 0
12d9a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
12d9b 65 20 69 66 20 61 6e 79 20 70 61 67 65 20 68 61 e if any page ha
12d9c 73 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s PGHDR_NEED_SYN
12d9d 43 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74 C */.. /* Set
12d9e 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 the doNotSync f
12d9f 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69 lag to 1. This i
12da0 73 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e s because we can
12da1 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 not allow a jour
12da2 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 nal. ** heade
12da3 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 r to be written
12da4 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 65 between the page
12da5 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 74 s journaled by t
12da6 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 his function..
12da7 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
12da8 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 !MEMDB );. a
12da9 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
12daa 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 oNotSync==0 );.
12dab 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 pPager->doNot
12dac 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f Sync = 1;.. /
12dad 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 73 * This trick ass
12dae 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 74 umes that both t
12daf 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 he page-size and
12db0 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 sector-size are
12db1 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 . ** an integ
12db2 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 er power of 2. I
12db3 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 t sets variable
12db4 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e 74 pg1 to the ident
12db5 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 ifier. ** of
12db6 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f the first page o
12db7 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 f the sector pPg
12db8 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a is located on..
12db9 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d */. pg1 =
12dba 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 ((pPg->pgno-1)
12dbb 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 74 & ~(nPagePerSect
12dbc 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 or-1)) + 1;..
12dbd 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12dbe 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28 ecount(pPager, (
12dbf 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e int *)&nPageCoun
12dc0 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d t);. if( pPg-
12dc1 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 >pgno>nPageCount
12dc2 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 ){. nPage
12dc3 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 = (pPg->pgno - p
12dc4 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 g1)+1;. }else
12dc5 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50 if( (pg1+nPageP
12dc6 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 erSector-1)>nPag
12dc7 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 eCount ){.
12dc8 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 nPage = nPageCou
12dc9 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 nt+1-pg1;. }e
12dca 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 lse{. nPage
12dcb 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f = nPagePerSecto
12dcc 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 r;. }. ass
12dcd 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 ert(nPage>0);.
12dce 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50 assert(pg1<=pP
12dcf 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 g->pgno);. as
12dd0 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29 sert((pg1+nPage)
12dd1 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 >pPg->pgno);..
12dd2 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e for(ii=0; ii<n
12dd3 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 Page && rc==SQLI
12dd4 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 TE_OK; ii++){.
12dd5 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 Pgno pg = pg
12dd6 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64 1+ii;. PgHd
12dd7 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20 r *pPage;.
12dd8 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e if( pg==pPg->pgn
12dd9 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74 o || !sqlite3Bit
12dda 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e vecTest(pPager->
12ddb 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 pInJournal, pg)
12ddc 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
12ddd 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f g!=PAGER_MJ_PGNO
12dde 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 (pPager) ){.
12ddf 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12de0 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 e3PagerGet(pPage
12de1 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a r, pg, &pPage);.
12de2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
12de3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
12de4 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
12de5 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67 pager_write(pPag
12de6 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
12de7 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 if( pPage->flags
12de8 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
12de9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
12dea 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a needSync = 1;.
12deb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 as
12dec 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 sert(pPager->nee
12ded 64 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 dSync);.
12dee 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
12def 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
12df0 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ref(pPage);.
12df1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
12df2 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
12df3 28 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72 ( (pPage = pager
12df4 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 _lookup(pPager,
12df5 70 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 pg))!=0 ){.
12df6 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c if( pPage->fl
12df7 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
12df8 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 YNC ){.
12df9 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 needSync = 1;.
12dfa 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12dfb 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
12dfc 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
12dfd 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
12dfe 20 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45 If the PGHDR_NE
12dff 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 ED_SYNC flag is
12e00 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 set for any of t
12e01 68 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a he nPage pages .
12e02 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 ** starting
12e03 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20 at pg1, then it
12e04 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20 needs to be set
12e05 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e for all of them.
12e06 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 Because. **
12e07 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f writing to any o
12e08 66 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61 f these nPage pa
12e09 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74 ges may damage t
12e0a 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 he others, the.
12e0b 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 ** journal fi
12e0c 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 le must contain
12e0d 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20 sync()ed copies
12e0e 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 of all of them.
12e0f 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 ** before any
12e10 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20 of them can be
12e11 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 written out to t
12e12 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12e13 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
12e14 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 needSync ){.
12e15 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 assert( !MEMD
12e16 42 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f 53 B && pPager->noS
12e17 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ync==0 );.
12e18 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 for(ii=0; ii<nPa
12e19 67 65 20 26 26 20 6e 65 65 64 53 79 6e 63 3b 20 ge && needSync;
12e1a 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 ii++){. P
12e1b 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 gHdr *pPage = pa
12e1c 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 ger_lookup(pPage
12e1d 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 r, pg1+ii);.
12e1e 20 20 20 20 69 66 28 20 70 50 61 67 65 20 29 7b if( pPage ){
12e1f 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 . pPage
12e20 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
12e21 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 _NEED_SYNC;.
12e22 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
12e23 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a erUnref(pPage);.
12e24 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
12e25 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 }. assert(p
12e26 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 Pager->needSync)
12e27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 ;. }.. ass
12e28 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e ert( pPager->doN
12e29 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 otSync==1 );.
12e2a 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 pPager->doNotSy
12e2b 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b nc = 0;. }else{
12e2c 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
12e2d 77 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a write(pDbPage);.
12e2e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
12e2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
12e30 20 54 52 55 45 20 69 66 20 74 68 65 20 70 61 67 TRUE if the pag
12e31 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 e given in the a
12e32 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 rgument was prev
12e33 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a iously passed.**
12e34 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 to sqlite3Pager
12e35 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 Write(). In oth
12e36 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e er words, return
12e37 20 54 52 55 45 20 69 66 20 69 74 20 69 73 20 6f TRUE if it is o
12e38 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 k.** to change t
12e39 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 he content of th
12e3a 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 e page..*/.#ifnd
12e3b 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 ef NDEBUG.SQLITE
12e3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
12e3d 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
12e3e 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 able(DbPage *pPg
12e3f 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d ){. return pPg-
12e40 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
12e41 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a TY;.}.#endif../*
12e42 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 .** A call to th
12e43 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 is routine tells
12e44 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74 20 the pager that
12e45 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 it is not necess
12e46 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 ary to.** write
12e47 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 the information
12e48 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61 63 6b on page pPg back
12e49 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76 to the disk, ev
12e4a 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 en though.** tha
12e4b 74 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 t page might be
12e4c 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e marked as dirty.
12e4d 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 This happens,
12e4e 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 for example, whe
12e4f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61 n.** the page ha
12e50 73 20 62 65 65 6e 20 61 64 64 65 64 20 61 73 20 s been added as
12e51 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72 a leaf of the fr
12e52 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74 eelist and so it
12e53 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 s.** content no
12e54 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a longer matters..
12e55 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 **.** The overly
12e56 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 ing software lay
12e57 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f er calls this ro
12e58 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f utine when all o
12e59 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e f the data.** on
12e5a 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 the given page
12e5b 69 73 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70 is unused. The p
12e5c 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70 ager marks the p
12e5d 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a age as clean so.
12e5e 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 ** that it does
12e5f 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 6e 20 not get written
12e60 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 to disk..**.** T
12e61 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 20 74 ests show that t
12e62 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e his optimization
12e63 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65 20 74 can quadruple t
12e64 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67 he speed of larg
12e65 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65 e .** DELETE ope
12e66 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 rations..*/.SQLI
12e67 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12e68 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
12e69 57 72 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 Write(PgHdr *pPg
12e6a 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
12e6b 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
12e6c 3b 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c ;. if( (pPg->fl
12e6d 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 ags&PGHDR_DIRTY)
12e6e 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 && pPager->nSav
12e6f 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 epoint==0 ){.
12e70 20 50 41 47 45 52 54 52 41 43 45 28 28 22 44 4f PAGERTRACE(("DO
12e71 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 NT_WRITE page %d
12e72 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e of %d\n", pPg->
12e73 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 pgno, PAGERID(pP
12e74 61 67 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54 ager)));. IOT
12e75 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20 RACE(("CLEAN %p
12e76 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
12e77 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70 Pg->pgno)). p
12e78 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 Pg->flags |= PGH
12e79 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23 DR_DONT_WRITE;.#
12e7a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 ifdef SQLITE_CHE
12e7b 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 CK_PAGES. pPg
12e7c 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 ->pageHash = pag
12e7d 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 er_pagehash(pPg)
12e7e 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a ;.#endif. }.}..
12e7f 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
12e80 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 ne is called to
12e81 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 increment the va
12e82 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 lue of the datab
12e83 61 73 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 ase file .** cha
12e84 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f nge-counter, sto
12e85 72 65 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 red as a 4-byte
12e86 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 big-endian integ
12e87 65 72 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a er starting at .
12e88 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 ** byte offset 2
12e89 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 4 of the pager f
12e8a 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ile..**.** If th
12e8b 65 20 69 73 44 69 72 65 63 74 20 66 6c 61 67 20 e isDirect flag
12e8c 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 is zero, then th
12e8d 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20 63 61 is is done by ca
12e8e 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 lling .** sqlite
12e8f 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 3PagerWrite() on
12e90 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 6d 6f page 1, then mo
12e91 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 difying the cont
12e92 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 70 ents of the.** p
12e93 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74 68 69 age data. In thi
12e94 73 20 63 61 73 65 20 74 68 65 20 66 69 6c 65 20 s case the file
12e95 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 64 20 will be updated
12e96 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 when the current
12e97 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
12e98 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a is committed..**
12e99 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65 63 74 .** The isDirect
12e9a 20 66 6c 61 67 20 6d 61 79 20 6f 6e 6c 79 20 62 flag may only b
12e9b 65 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 e non-zero if th
12e9c 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f e library was co
12e9d 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 mpiled.** with t
12e9e 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 he SQLITE_ENABLE
12e9f 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 20 6d 61 _ATOMIC_WRITE ma
12ea0 63 72 6f 20 64 65 66 69 6e 65 64 2e 20 49 6e 20 cro defined. In
12ea1 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 66 this case,.** if
12ea2 20 69 73 44 69 72 65 63 74 20 69 73 20 6e 6f 6e isDirect is non
12ea3 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 -zero, then the
12ea4 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
12ea5 20 75 70 64 61 74 65 64 20 64 69 72 65 63 74 6c updated directl
12ea6 79 0a 2a 2a 20 62 79 20 77 72 69 74 69 6e 67 20 y.** by writing
12ea7 61 6e 20 75 70 64 61 74 65 64 20 76 65 72 73 69 an updated versi
12ea8 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 75 73 69 on of page 1 usi
12ea9 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 ng a call to the
12eaa 20 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 57 72 .** sqlite3OsWr
12eab 69 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a ite() function..
12eac 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
12ead 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 ger_incr_changec
12eae 6f 75 6e 74 65 72 28 50 61 67 65 72 20 2a 70 50 ounter(Pager *pP
12eaf 61 67 65 72 2c 20 69 6e 74 20 69 73 44 69 72 65 ager, int isDire
12eb0 63 74 4d 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72 ctMode){. int r
12eb1 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
12eb2 20 20 2f 2a 20 44 65 63 6c 61 72 65 20 61 6e 64 /* Declare and
12eb3 20 69 6e 69 74 69 61 6c 69 7a 65 20 63 6f 6e 73 initialize cons
12eb4 74 61 6e 74 20 69 6e 74 65 67 65 72 20 27 69 73 tant integer 'is
12eb5 44 69 72 65 63 74 27 2e 20 49 66 20 74 68 65 0a Direct'. If the.
12eb6 20 20 2a 2a 20 61 74 6f 6d 69 63 2d 77 72 69 74 ** atomic-writ
12eb7 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
12eb8 73 20 65 6e 61 62 6c 65 64 20 69 6e 20 74 68 69 s enabled in thi
12eb9 73 20 62 75 69 6c 64 2c 20 74 68 65 6e 20 69 73 s build, then is
12eba 44 69 72 65 63 74 0a 20 20 2a 2a 20 69 73 20 69 Direct. ** is i
12ebb 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 74 68 nitialized to th
12ebc 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 e value passed a
12ebd 73 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f s the isDirectMo
12ebe 64 65 20 70 61 72 61 6d 65 74 65 72 0a 20 20 2a de parameter. *
12ebf 2a 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 * to this functi
12ec0 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 on. Otherwise, i
12ec1 74 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 t is always set
12ec2 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 to zero.. **.
12ec3 2a 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74 ** The idea is t
12ec4 68 61 74 20 69 66 20 74 68 65 20 61 74 6f 6d 69 hat if the atomi
12ec5 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
12ec6 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 2a 2a tion is not. **
12ec7 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 enabled at comp
12ec8 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 20 63 6f ile time, the co
12ec9 6d 70 69 6c 65 72 20 63 61 6e 20 6f 6d 69 74 20 mpiler can omit
12eca 74 68 65 20 74 65 73 74 73 20 6f 66 0a 20 20 2a the tests of. *
12ecb 2a 20 27 69 73 44 69 72 65 63 74 27 20 62 65 6c * 'isDirect' bel
12ecc 6f 77 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74 ow, as well as t
12ecd 68 65 20 62 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65 he block enclose
12ece 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 22 69 d in the. ** "i
12ecf 66 28 20 69 73 44 69 72 65 63 74 20 29 22 20 63 f( isDirect )" c
12ed0 6f 6e 64 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 ondition.. */.#
12ed1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e ifndef SQLITE_EN
12ed2 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 ABLE_ATOMIC_WRIT
12ed3 45 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 E. const int is
12ed4 44 69 72 65 63 74 20 3d 20 30 3b 0a 20 20 61 73 Direct = 0;. as
12ed5 73 65 72 74 28 20 69 73 44 69 72 65 63 74 4d 6f sert( isDirectMo
12ed6 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 de==0 );. UNUSE
12ed7 44 5f 50 41 52 41 4d 45 54 45 52 28 69 73 44 69 D_PARAMETER(isDi
12ed8 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65 6c 73 65 rectMode);.#else
12ed9 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 44 . const int isD
12eda 69 72 65 63 74 20 3d 20 69 73 44 69 72 65 63 74 irect = isDirect
12edb 4d 6f 64 65 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 Mode;.#endif..
12edc 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
12edd 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 state>=PAGER_RES
12ede 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21 ERVED );. if( !
12edf 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
12ee0 75 6e 74 44 6f 6e 65 20 26 26 20 70 50 61 67 65 untDone && pPage
12ee1 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20 r->dbSize>0 ){.
12ee2 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72 PgHdr *pPgHdr
12ee3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12ee4 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f /* Reference to
12ee5 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 20 20 75 page 1 */. u
12ee6 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 32 change_counte
12ee7 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
12ee8 49 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66 Initial value of
12ee9 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
12eea 66 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 61 73 field */.. as
12eeb 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 sert( !pPager->t
12eec 65 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65 empFile && isOpe
12eed 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b n(pPager->fd) );
12eee 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 .. /* Open pa
12eef 67 65 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65 ge 1 of the file
12ef0 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f for writing. */
12ef1 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
12ef2 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
12ef3 2c 20 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 , 1, &pPgHdr);.
12ef4 20 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 assert( pPgHd
12ef5 72 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 r==0 || rc==SQLI
12ef6 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a TE_OK );.. /*
12ef7 20 49 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73 If page one was
12ef8 20 66 65 74 63 68 65 64 20 73 75 63 63 65 73 73 fetched success
12ef9 66 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20 fully, and this
12efa 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a function is not.
12efb 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 ** operating
12efc 20 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c in direct-mode,
12efd 20 6d 61 6b 65 20 70 61 67 65 20 31 20 77 72 69 make page 1 wri
12efe 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 table.. */.
12eff 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
12f00 5f 4f 4b 20 26 26 20 21 69 73 44 69 72 65 63 74 _OK && !isDirect
12f01 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
12f02 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
12f03 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 7d 0a (pPgHdr);. }.
12f04 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
12f05 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12f06 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 /* Increment the
12f07 20 76 61 6c 75 65 20 6a 75 73 74 20 72 65 61 64 value just read
12f08 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 62 61 and write it ba
12f09 63 6b 20 74 6f 20 62 79 74 65 20 32 34 2e 20 2a ck to byte 24. *
12f0a 2f 0a 20 20 20 20 20 20 63 68 61 6e 67 65 5f 63 /. change_c
12f0b 6f 75 6e 74 65 72 20 3d 20 73 71 6c 69 74 65 33 ounter = sqlite3
12f0c 47 65 74 34 62 79 74 65 28 28 75 38 2a 29 70 50 Get4byte((u8*)pP
12f0d 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
12f0e 29 3b 0a 20 20 20 20 20 20 63 68 61 6e 67 65 5f );. change_
12f0f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20 20 20 20 20 counter++;.
12f10 20 70 75 74 33 32 62 69 74 73 28 28 28 63 68 61 put32bits(((cha
12f11 72 2a 29 70 50 67 48 64 72 2d 3e 70 44 61 74 61 r*)pPgHdr->pData
12f12 29 2b 32 34 2c 20 63 68 61 6e 67 65 5f 63 6f 75 )+24, change_cou
12f13 6e 74 65 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a nter);.. /*
12f14 20 49 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 64 If running in d
12f15 69 72 65 63 74 20 6d 6f 64 65 2c 20 77 72 69 74 irect mode, writ
12f16 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f e the contents o
12f17 66 20 70 61 67 65 20 31 20 74 6f 20 74 68 65 20 f page 1 to the
12f18 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 file. */. i
12f19 66 28 20 69 73 44 69 72 65 63 74 20 29 7b 0a 20 f( isDirect ){.
12f1a 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 const voi
12f1b 64 20 2a 7a 42 75 66 20 3d 20 70 50 67 48 64 72 d *zBuf = pPgHdr
12f1c 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 20 20 20 ->pData;.
12f1d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
12f1e 3e 64 62 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b >dbFileSize>0 );
12f1f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
12f20 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
12f21 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 ger->fd, zBuf, p
12f22 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c Pager->pageSize,
12f23 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 0);. }..
12f24 20 20 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 /* If everyt
12f25 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 73 65 74 hing worked, set
12f26 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 the changeCount
12f27 44 6f 6e 65 20 66 6c 61 67 2e 20 2a 2f 0a 20 20 Done flag. */.
12f28 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12f29 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
12f2a 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
12f2b 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 ountDone = 1;.
12f2c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
12f2d 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 /* Release the
12f2e 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 page reference.
12f2f 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 */. sqlite3Pa
12f30 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 gerUnref(pPgHdr)
12f31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
12f32 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 c;.}../*.** Sync
12f33 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 the pager file
12f34 74 6f 20 64 69 73 6b 2e 20 54 68 69 73 20 69 73 to disk. This is
12f35 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d a no-op for in-
12f36 6d 65 6d 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20 memory files.**
12f37 6f 72 20 70 61 67 65 73 20 77 69 74 68 20 74 68 or pages with th
12f38 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66 e Pager.noSync f
12f39 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 lag set..**.** I
12f3a 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 f successful, or
12f3b 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 called on a pag
12f3c 65 72 20 66 6f 72 20 77 68 69 63 68 20 69 74 20 er for which it
12f3d 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 is a no-op, this
12f3e 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 .** function ret
12f3f 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 urns SQLITE_OK.
12f40 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f Otherwise, an IO
12f41 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
12f42 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 eturned..*/.SQLI
12f43 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
12f44 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 qlite3PagerSync(
12f45 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
12f46 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
12f47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f48 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
12f49 20 63 6f 64 65 20 2a 2f 0a 20 20 69 66 28 20 4d code */. if( M
12f4a 45 4d 44 42 20 7c 7c 20 70 50 61 67 65 72 2d 3e EMDB || pPager->
12f4b 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 noSync ){. rc
12f4c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
12f4d 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
12f4e 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
12f4f 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 ager->fd, pPager
12f50 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 ->sync_flags);.
12f51 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
12f52 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 }../*.** Sync th
12f53 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
12f54 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 50 for the pager pP
12f55 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f ager. zMaster po
12f56 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65 ints to the name
12f57 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20 .** of a master
12f58 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 journal file tha
12f59 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 t should be writ
12f5a 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 ten into the ind
12f5b 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e ividual.** journ
12f5c 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 al file. zMaster
12f5d 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 may be NULL, wh
12f5e 69 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 74 ich is interpret
12f5f 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a ed as no master.
12f60 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 ** journal (a si
12f61 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
12f62 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
12f63 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 * This routine e
12f64 6e 73 75 72 65 73 20 74 68 61 74 3a 0a 2a 2a 0a nsures that:.**.
12f65 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 ** * The datab
12f66 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 2d ase file change-
12f67 63 6f 75 6e 74 65 72 20 69 73 20 75 70 64 61 74 counter is updat
12f68 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6a ed,.** * the j
12f69 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 ournal is synced
12f6a 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 74 6f (unless the ato
12f6b 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 mic-write optimi
12f6c 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 29 2c zation is used),
12f6d 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64 69 72 74 .** * all dirt
12f6e 79 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74 y pages are writ
12f6f 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 ten to the datab
12f70 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a 20 20 20 ase file, .**
12f71 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
12f72 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 ile is truncated
12f73 20 28 69 66 20 72 65 71 75 69 72 65 64 29 2c 20 (if required),
12f74 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 and.** * the d
12f75 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79 6e atabase file syn
12f76 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ced. .**.** The
12f77 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 61 74 20 only thing that
12f78 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 remains to commi
12f79 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
12f7a 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 n is to finalize
12f7b 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c 20 74 72 .** (delete, tr
12f7c 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 uncate or zero t
12f7d 68 65 20 66 69 72 73 74 20 70 61 72 74 20 6f 66 he first part of
12f7e 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 ) the journal fi
12f7f 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65 6c 65 74 le (or .** delet
12f80 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
12f81 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70 65 rnal file if spe
12f82 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e cified)..**.** N
12f83 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61 73 ote that if zMas
12f84 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 ter==NULL, this
12f85 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 does not overwri
12f86 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76 61 te a previous va
12f87 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f lue.** passed to
12f88 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 an sqlite3Pager
12f89 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 CommitPhaseOne()
12f8a 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 call..**.** If
12f8b 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65 the final parame
12f8c 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 69 ter - noSync - i
12f8d 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 s true, then the
12f8e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
12f8f 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74 20 tself.** is not
12f90 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 6c 6c synced. The call
12f91 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c er must call sql
12f92 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 29 20 ite3PagerSync()
12f93 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a 20 73 directly to.** s
12f94 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 ync the database
12f95 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63 61 6c file before cal
12f96 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61 73 65 ling CommitPhase
12f97 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74 65 20 Two() to delete
12f98 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
12f99 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 ile in this case
12f9a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
12f9b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
12f9c 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f agerCommitPhaseO
12f9d 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 ne(. Pager *pPa
12f9e 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ger,
12f9f 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f /* Pager o
12fa0 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 bject */. const
12fa1 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 char *zMaster,
12fa2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
12fa3 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 6d not NULL, the m
12fa4 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
12fa5 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53 79 me */. int noSy
12fa6 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nc
12fa7 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
12fa8 74 6f 20 6f 6d 69 74 20 74 68 65 20 78 53 79 6e to omit the xSyn
12fa9 63 20 6f 6e 20 74 68 65 20 64 62 20 66 69 6c 65 c on the db file
12faa 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 */.){. int rc
12fab 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
12fac 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
12fad 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 rn code */.. if
12fae 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
12faf 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
12fb0 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
12fb1 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 . }.. PAGERTRA
12fb2 43 45 28 28 22 44 41 54 41 42 41 53 45 20 53 59 CE(("DATABASE SY
12fb3 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73 NC: File=%s zMas
12fb4 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d 25 64 5c ter=%s nSize=%d\
12fb5 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 61 67 65 n", . pPage
12fb6 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 4d r->zFilename, zM
12fb7 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 64 aster, pPager->d
12fb8 62 53 69 7a 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 bSize));.. /* I
12fb9 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d f this is an in-
12fba 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 6e 6f memory db, or no
12fbb 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e pages have been
12fbc 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f 72 20 written to, or
12fbd 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 this. ** functi
12fbe 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 on has already b
12fbf 65 65 6e 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 een called, it i
12fc0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a s a no-op.. */.
12fc1 20 20 69 66 28 20 4d 45 4d 44 42 20 26 26 20 70 if( MEMDB && p
12fc2 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
12fc3 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 d ){. sqlite3
12fc4 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 BackupRestart(pP
12fc5 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a ager->pBackup);.
12fc6 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 }else if( pPag
12fc7 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 er->state!=PAGER
12fc8 5f 53 59 4e 43 45 44 20 26 26 20 70 50 61 67 65 _SYNCED && pPage
12fc9 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b r->dbModified ){
12fca 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c .. /* The fol
12fcb 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64 lowing block upd
12fcc 61 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d ates the change-
12fcd 63 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79 counter. Exactly
12fce 20 68 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20 64 how it. ** d
12fcf 6f 65 73 20 74 68 69 73 20 64 65 70 65 6e 64 73 oes this depends
12fd0 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e on whether or n
12fd1 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 ot the atomic-up
12fd2 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f date optimizatio
12fd3 6e 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 6e 61 n. ** was ena
12fd4 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 bled at compile
12fd5 74 69 6d 65 2c 20 61 6e 64 20 69 66 20 74 68 69 time, and if thi
12fd6 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 65 s transaction me
12fd7 65 74 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 ets the . **
12fd8 72 75 6e 74 69 6d 65 20 63 72 69 74 65 72 69 61 runtime criteria
12fd9 20 74 6f 20 75 73 65 20 74 68 65 20 6f 70 65 72 to use the oper
12fda 61 74 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a 20 ation: . **.
12fdb 20 20 20 2a 2a 20 20 20 20 2a 20 54 68 65 20 66 ** * The f
12fdc 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f ile-system suppo
12fdd 72 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 rts the atomic-w
12fde 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f rite property fo
12fdf 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c r. ** bl
12fe0 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 ocks of size pag
12fe1 65 2d 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 20 e-size, and .
12fe2 20 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f ** * This co
12fe3 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 mmit is not part
12fe4 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 of a multi-file
12fe5 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e transaction, an
12fe6 64 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78 d. ** * Ex
12fe7 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 actly one page h
12fe8 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 as been modified
12fe9 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 and store in th
12fea 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
12feb 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 **. ** If
12fec 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f the optimizatio
12fed 6e 20 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c 65 n was not enable
12fee 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d d at compile tim
12fef 65 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 e, then the.
12ff0 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 ** pager_incr_ch
12ff1 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66 75 angecounter() fu
12ff2 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
12ff3 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 to update the c
12ff4 68 61 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f 75 hange. ** cou
12ff5 6e 74 65 72 20 69 6e 20 27 69 6e 64 69 72 65 63 nter in 'indirec
12ff6 74 2d 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 20 t-mode'. If the
12ff7 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 optimization is
12ff8 63 6f 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 0a compiled in but.
12ff9 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 70 ** is not ap
12ffa 70 6c 69 63 61 62 6c 65 20 74 6f 20 74 68 69 73 plicable to this
12ffb 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63 61 transaction, ca
12ffc 6c 6c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 ll sqlite3Journa
12ffd 6c 43 72 65 61 74 65 28 29 0a 20 20 20 20 2a 2a lCreate(). **
12ffe 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
12fff 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 e journal file h
13000 61 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65 6e as actually been
13001 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 created, then c
13002 61 6c 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 all. ** pager
13003 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
13004 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 ter() to update
13005 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
13006 65 72 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20 er in indirect.
13007 20 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20 ** mode. .
13008 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 **. ** Other
13009 77 69 73 65 2c 20 69 66 20 74 68 65 20 6f 70 74 wise, if the opt
1300a 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f 74 imization is bot
1300b 68 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 61 70 h enabled and ap
1300c 70 6c 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a 2a plicable,. **
1300d 20 74 68 65 6e 20 63 61 6c 6c 20 70 61 67 65 72 then call pager
1300e 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
1300f 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 ter() to update
13010 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
13011 65 72 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64 69 er. ** in 'di
13012 72 65 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20 74 rect' mode. In t
13013 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 his case the jou
13014 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e rnal file will n
13015 65 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 63 ever be. ** c
13016 72 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 reated for this
13017 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 transaction..
13018 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
13019 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
1301a 57 52 49 54 45 0a 20 20 20 20 50 67 48 64 72 20 WRITE. PgHdr
1301b 2a 70 50 67 3b 0a 20 20 20 20 61 73 73 65 72 74 *pPg;. assert
1301c 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
1301d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d >jfd) || pPager-
1301e 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
1301f 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
13020 4f 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20 21 OFF );. if( !
13021 7a 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70 65 zMaster && isOpe
13022 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 0a n(pPager->jfd) .
13023 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e && pPager->
13024 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c journalOff==jrnl
13025 42 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 BufferSize(pPage
13026 72 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 r) . && pPag
13027 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 67 er->dbSize>=pPag
13028 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a 20 er->dbFileSize.
13029 20 20 20 20 26 26 20 28 30 3d 3d 28 70 50 67 20 && (0==(pPg
1302a 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 = sqlite3PcacheD
1302b 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d irtyList(pPager-
1302c 3e 70 50 43 61 63 68 65 29 29 20 7c 7c 20 30 3d >pPCache)) || 0=
1302d 3d 70 50 67 2d 3e 70 44 69 72 74 79 29 0a 20 20 =pPg->pDirty).
1302e 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70 ){. /* Up
1302f 64 61 74 65 20 74 68 65 20 64 62 20 66 69 6c 65 date the db file
13030 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
13031 76 69 61 20 74 68 65 20 64 69 72 65 63 74 2d 77 via the direct-w
13032 72 69 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 rite method. The
13033 20 0a 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f . ** follo
13034 77 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d wing call will m
13035 6f 64 69 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d odify the in-mem
13036 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 ory representati
13037 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 0a 20 20 on of page 1 .
13038 20 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 ** to includ
13039 65 20 74 68 65 20 75 70 64 61 74 65 64 20 63 68 e the updated ch
1303a 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 ange counter and
1303b 20 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 then write page
1303c 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 72 1 . ** dir
1303d 65 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74 ectly to the dat
1303e 61 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63 61 abase file. Beca
1303f 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d 69 use of the atomi
13040 63 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a c-write . *
13041 2a 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 * property of th
13042 65 20 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74 e host file-syst
13043 65 6d 2c 20 74 68 69 73 20 69 73 20 73 61 66 65 em, this is safe
13044 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
13045 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 rc = pager_incr
13046 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 _changecounter(p
13047 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 7d Pager, 1);. }
13048 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
13049 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 sqlite3JournalC
1304a 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 reate(pPager->jf
1304b 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 d);. if( rc
1304c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1304d 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 rc = page
1304e 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 r_incr_changecou
1304f 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b nter(pPager, 0);
13050 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
13051 65 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 70 61 else. rc = pa
13052 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 ger_incr_changec
13053 6f 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 ounter(pPager, 0
13054 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 );.#endif. if
13055 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13056 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 ) goto commit_ph
13057 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 ase_one_exit;..
13058 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 /* If this tr
13059 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 ansaction has ma
1305a 64 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 de the database
1305b 73 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c smaller, then al
1305c 6c 20 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 62 l pages. ** b
1305d 65 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 62 eing discarded b
1305e 79 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e y the truncation
1305f 20 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 6e must be written
13060 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a to the journal.
13061 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68 69 ** file. Thi
13062 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 s can only happe
13063 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d n in auto-vacuum
13064 20 6d 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 mode.. **.
13065 20 20 2a 2a 20 42 65 66 6f 72 65 20 72 65 61 64 ** Before read
13066 69 6e 67 20 74 68 65 20 70 61 67 65 73 20 77 69 ing the pages wi
13067 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 th page numbers
13068 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
13069 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 . ** current
1306a 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 value of Pager.d
1306b 62 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a bSize, set dbSiz
1306c 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 e back to the va
1306d 6c 75 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 lue. ** that
1306e 69 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 73 it took at the s
1306f 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e tart of the tran
13070 73 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69 saction. Otherwi
13071 73 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 se, the. ** c
13072 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 alls to sqlite3P
13073 61 67 65 72 47 65 74 28 29 20 72 65 74 75 72 6e agerGet() return
13074 20 7a 65 72 6f 65 64 20 70 61 67 65 73 20 69 6e zeroed pages in
13075 73 74 65 61 64 20 6f 66 20 0a 20 20 20 20 2a 2a stead of . **
13076 20 72 65 61 64 69 6e 67 20 64 61 74 61 20 66 72 reading data fr
13077 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
13078 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 file.. */.#if
13079 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1307a 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
1307b 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 if( pPager->dbSi
1307c 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 ze<pPager->dbOri
1307d 67 53 69 7a 65 0a 20 20 20 20 20 26 26 20 70 50 gSize. && pP
1307e 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
1307f 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e!=PAGER_JOURNAL
13080 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20 29 7b MODE_OFF . ){
13081 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20 . Pgno i;
13082 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13083 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13084 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
13085 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 63 iable */. c
13086 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 70 20 onst Pgno iSkip
13087 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 = PAGER_MJ_PGNO(
13088 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 6e 64 pPager); /* Pend
13089 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 2a 2f ing lock page */
1308a 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e . const Pgn
1308b 6f 20 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 o dbSize = pPage
1308c 72 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 20 20 r->dbSize;
1308d 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6d 61 /* Database ima
1308e 67 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20 20 20 ge size */ .
1308f 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
13090 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 = pPager->dbOri
13091 67 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 6f 72 gSize;. for
13092 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 3c ( i=dbSize+1; i<
13093 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 =pPager->dbOrigS
13094 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 ize; i++ ){.
13095 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 if( !sqlite3
13096 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 BitvecTest(pPage
13097 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 r->pInJournal, i
13098 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b ) && i!=iSkip ){
13099 0a 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72 . PgHdr
1309a 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 *pPage;
1309b 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 /* Page to
1309c 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 journal */.
1309d 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1309e 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
1309f 2c 20 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 , i, &pPage);.
130a0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
130a1 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
130a2 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e commit_phase_on
130a3 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 e_exit;.
130a4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
130a5 67 65 72 57 72 69 74 65 28 70 50 61 67 65 29 3b gerWrite(pPage);
130a6 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
130a7 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
130a8 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
130a9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
130aa 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 ) goto commit_p
130ab 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 hase_one_exit;.
130ac 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
130ad 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e . pPager->
130ae 64 62 53 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b dbSize = dbSize;
130af 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
130b0 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
130b1 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
130b2 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ame into the jou
130b3 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 20 rnal file. If a
130b4 6d 61 73 74 65 72 20 0a 20 20 20 20 2a 2a 20 6a master . ** j
130b5 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
130b6 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
130b7 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 n written to the
130b8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a journal file, .
130b9 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 ** or if zMa
130ba 73 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f ster is NULL (no
130bb 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29 master journal)
130bc 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c , then this call
130bd 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 is a no-op..
130be 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 72 69 */. rc = wri
130bf 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 teMasterJournal(
130c0 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 pPager, zMaster)
130c1 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
130c2 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 LITE_OK ) goto c
130c3 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
130c4 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 exit;.. /* Sy
130c5 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
130c6 69 6c 65 2e 20 49 66 20 74 68 65 20 61 74 6f 6d ile. If the atom
130c7 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69 ic-update optimi
130c8 7a 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 0a zation is being.
130c9 20 20 20 20 2a 2a 20 75 73 65 64 2c 20 74 68 69 ** used, thi
130ca 73 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20 s call will not
130cb 63 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e create the journ
130cc 61 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f al file or perfo
130cd 72 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20 72 65 rm any. ** re
130ce 61 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20 al IO.. */.
130cf 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e rc = syncJourn
130d0 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
130d1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
130d2 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f K ) goto commit_
130d3 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
130d4 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6c . /* Write al
130d5 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 74 6f l dirty pages to
130d6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
130d7 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 le. */. rc =
130d8 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 pager_write_page
130d9 6c 69 73 74 28 73 71 6c 69 74 65 33 50 63 61 63 list(sqlite3Pcac
130da 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 heDirtyList(pPag
130db 65 72 2d 3e 70 50 43 61 63 68 65 29 29 3b 0a 20 er->pPCache));.
130dc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
130dd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 E_OK ){. as
130de 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
130df 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 _IOERR_BLOCKED )
130e0 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6d ;. goto com
130e1 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
130e2 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 it;. }. sq
130e3 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
130e4 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 All(pPager->pPCa
130e5 63 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 che);.. /* If
130e6 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
130e7 6b 20 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d k is not the sam
130e8 65 20 73 69 7a 65 20 61 73 20 74 68 65 20 64 61 e size as the da
130e9 74 61 62 61 73 65 20 69 6d 61 67 65 2c 0a 20 20 tabase image,.
130ea 20 20 2a 2a 20 74 68 65 6e 20 75 73 65 20 70 61 ** then use pa
130eb 67 65 72 5f 74 72 75 6e 63 61 74 65 20 74 6f 20 ger_truncate to
130ec 67 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b 20 74 grow or shrink t
130ed 68 65 20 66 69 6c 65 20 68 65 72 65 2e 0a 20 20 he file here..
130ee 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 */. if( pPa
130ef 67 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61 ger->dbSize!=pPa
130f0 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
130f1 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e ){. Pgno nN
130f2 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ew = pPager->dbS
130f3 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64 ize - (pPager->d
130f4 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f bSize==PAGER_MJ_
130f5 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0a 20 PGNO(pPager));.
130f6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
130f7 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
130f8 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 R_EXCLUSIVE );.
130f9 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
130fa 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c truncate(pPager,
130fb 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66 nNew);. if
130fc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
130fd 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 ) goto commit_ph
130fe 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 ase_one_exit;.
130ff 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 }.. /* Fina
13100 6c 6c 79 2c 20 73 79 6e 63 20 74 68 65 20 64 61 lly, sync the da
13101 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a tabase file. */.
13102 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d if( !pPager-
13103 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 79 >noSync && !noSy
13104 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d nc ){. rc =
13105 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
13106 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 Pager->fd, pPage
13107 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a r->sync_flags);.
13108 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 }. IOTRAC
13109 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e 22 E(("DBSYNC %p\n"
1310a 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 , pPager))..
1310b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 pPager->state =
1310c 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20 20 PAGER_SYNCED;.
1310d 7d 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f }..commit_phase_
1310e 6f 6e 65 5f 65 78 69 74 3a 0a 20 20 69 66 28 20 one_exit:. if(
1310f 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc==SQLITE_IOERR
13110 5f 42 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 _BLOCKED ){.
13111 2f 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 /* pager_incr_ch
13112 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 6d 61 angecounter() ma
13113 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 y attempt to obt
13114 61 69 6e 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ain an exclusive
13115 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 74 6f 20 . ** lock to
13116 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20 spill the cache
13117 61 6e 64 20 72 65 74 75 72 6e 20 49 4f 45 52 52 and return IOERR
13118 5f 42 4c 4f 43 4b 45 44 2e 20 42 75 74 20 73 69 _BLOCKED. But si
13119 6e 63 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 72 nce . ** ther
1311a 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 e is no chance t
1311b 68 65 20 63 61 63 68 65 20 69 73 20 69 6e 63 6f he cache is inco
1311c 6e 73 69 73 74 65 6e 74 2c 20 69 74 20 69 73 0a nsistent, it is.
1311d 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f ** better to
1311e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
1311f 55 53 59 2e 0a 20 20 20 20 2a 2a 2f 0a 20 20 20 USY.. **/.
13120 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
13121 59 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 Y;. }. return
13122 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 rc;.}.../*.** Wh
13123 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
13124 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 is called, the
13125 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
13126 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c s been completel
13127 79 0a 2a 2a 20 75 70 64 61 74 65 64 20 74 6f 20 y.** updated to
13128 72 65 66 6c 65 63 74 20 74 68 65 20 63 68 61 6e reflect the chan
13129 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 ges made by the
1312a 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
1312b 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 6e 63 65 ion and.** synce
1312c 64 20 74 6f 20 64 69 73 6b 2e 20 54 68 65 20 6a d to disk. The j
1312d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c ournal file stil
1312e 6c 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 l exists in the
1312f 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a 2a 2a 20 file-system .**
13130 74 68 6f 75 67 68 2c 20 61 6e 64 20 69 66 20 61 though, and if a
13131 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 failure occurs
13132 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 74 at this point it
13133 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 will eventually
13134 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 73 20 61 .** be used as a
13135 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 hot-journal and
13136 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
13137 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 20 nsaction rolled
13138 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 back..**.** This
13139 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 function finali
1313a 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 zes the journal
1313b 66 69 6c 65 2c 20 65 69 74 68 65 72 20 62 79 20 file, either by
1313c 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a 20 74 72 deleting, .** tr
1313d 75 6e 63 61 74 69 6e 67 20 6f 72 20 70 61 72 74 uncating or part
1313e 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 20 69 74 ially zeroing it
1313f 2c 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e , so that it can
13140 6e 6f 74 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 not be used .**
13141 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 for hot-journal
13142 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 65 20 74 rollback. Once t
13143 68 69 73 20 69 73 20 64 6f 6e 65 20 74 68 65 20 his is done the
13144 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a transaction is.*
13145 2a 20 69 72 72 65 76 6f 63 61 62 6c 79 20 63 6f * irrevocably co
13146 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 mmitted..**.** I
13147 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
13148 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 s, an IO error c
13149 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
1314a 61 6e 64 20 74 68 65 20 70 61 67 65 72 0a 2a 2a and the pager.**
1314b 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74 68 65 20 moves into the
1314c 65 72 72 6f 72 20 73 74 61 74 65 2e 20 4f 74 68 error state. Oth
1314d 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f erwise, SQLITE_O
1314e 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a K is returned..*
1314f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13150 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13151 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 rCommitPhaseTwo(
13152 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
13153 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
13154 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
13155 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
13156 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 44 code */.. /* D
13157 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64 20 69 66 o not proceed if
13158 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 6c the pager is al
13159 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 ready in the err
1315a 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69 or state. */. i
1315b 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f f( pPager->errCo
1315c 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e de ){. return
1315d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
1315e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 ;. }.. /* This
1315f 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
13160 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 not be called i
13161 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6e f the pager is n
13162 6f 74 20 69 6e 20 61 74 20 6c 65 61 73 74 0a 20 ot in at least.
13163 20 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 ** PAGER_RESERV
13164 45 44 20 73 74 61 74 65 2e 20 41 6e 64 20 69 6e ED state. And in
13165 64 65 65 64 20 53 51 4c 69 74 65 20 6e 65 76 65 deed SQLite neve
13166 72 20 64 6f 65 73 20 74 68 69 73 2e 20 42 75 74 r does this. But
13167 20 69 74 20 69 73 0a 20 20 2a 2a 20 6e 69 63 65 it is. ** nice
13168 20 74 6f 20 68 61 76 65 20 74 68 69 73 20 64 65 to have this de
13169 66 65 6e 73 69 76 65 20 62 6c 6f 63 6b 20 68 65 fensive block he
1316a 72 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2f 0a re anyway.. */.
1316b 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 if( NEVER(pPag
1316c 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f er->state<PAGER_
1316d 52 45 53 45 52 56 45 44 29 20 29 7b 0a 20 20 20 RESERVED) ){.
1316e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
1316f 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 RROR;. }.. /*
13170 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e An optimization.
13171 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
13172 20 77 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c was not actuall
13173 79 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e y modified durin
13174 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 72 61 6e g. ** this tran
13175 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 70 61 67 saction, the pag
13176 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e er is running in
13177 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20 exclusive-mode
13178 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 73 69 6e and is. ** usin
13179 67 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 g persistent jou
1317a 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 68 69 73 rnals, then this
1317b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
1317c 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 o-op.. **. **
1317d 54 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 The start of the
1317e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75 journal file cu
1317f 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 rrently contains
13180 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 a single journa
13181 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77 l . ** header w
13182 69 74 68 20 74 68 65 20 6e 52 65 63 20 66 69 65 ith the nRec fie
13183 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20 ld set to 0. If
13184 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c 20 69 such a journal i
13185 73 20 75 73 65 64 20 61 73 0a 20 20 2a 2a 20 61 s used as. ** a
13186 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64 75 72 hot-journal dur
13187 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 ing hot-journal
13188 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68 61 6e rollback, 0 chan
13189 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 ges will be made
1318a 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 . ** to the dat
1318b 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f 20 74 abase file. So t
1318c 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
1318d 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f 75 72 to zero the jour
1318e 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72 nal . ** header
1318f 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65 . Since the page
13190 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 r is in exclusiv
13191 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20 69 73 e mode, there is
13192 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20 74 6f no need. ** to
13193 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 6b 73 20 drop any locks
13194 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 either.. */. i
13195 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 f( pPager->dbMod
13196 69 66 69 65 64 3d 3d 30 20 26 26 20 70 50 61 67 ified==0 && pPag
13197 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
13198 65 20 0a 20 20 20 26 26 20 70 50 61 67 65 72 2d e . && pPager-
13199 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
1319a 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
1319b 50 45 52 53 49 53 54 0a 20 20 29 7b 0a 20 20 20 PERSIST. ){.
1319c 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1319d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 >journalOff==JOU
1319e 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
1319f 65 72 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 er) );. retur
131a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
131a1 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 .. PAGERTRACE((
131a2 22 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50 "COMMIT %d\n", P
131a3 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 AGERID(pPager)))
131a4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
131a5 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
131a6 5f 53 59 4e 43 45 44 20 7c 7c 20 4d 45 4d 44 42 _SYNCED || MEMDB
131a7 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 64 62 4d || !pPager->dbM
131a8 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 72 63 20 odified );. rc
131a9 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e = pager_end_tran
131aa 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 saction(pPager,
131ab 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 pPager->setMaste
131ac 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 61 67 r);. return pag
131ad 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
131ae 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rc);.}../*.** R
131af 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e ollback all chan
131b0 67 65 73 2e 20 54 68 65 20 64 61 74 61 62 61 73 ges. The databas
131b1 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20 e falls back to
131b2 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d 6f 64 PAGER_SHARED mod
131b3 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 e..**.** This fu
131b4 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 nction performs
131b5 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a 2a 2a two tasks:.**.**
131b6 20 20 20 31 29 20 49 74 20 72 6f 6c 6c 73 20 62 1) It rolls b
131b7 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ack the journal
131b8 66 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e 67 20 file, restoring
131b9 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c all database fil
131ba 65 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 69 e and .** i
131bb 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 70 n-memory cache p
131bc 61 67 65 73 20 74 6f 20 74 68 65 20 73 74 61 74 ages to the stat
131bd 65 20 74 68 65 79 20 77 65 72 65 20 69 6e 20 77 e they were in w
131be 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 hen the transact
131bf 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 77 61 73 20 ion.** was
131c0 6f 70 65 6e 65 64 2c 20 61 6e 64 0a 2a 2a 20 20 opened, and.**
131c1 20 32 29 20 49 74 20 66 69 6e 61 6c 69 7a 65 73 2) It finalizes
131c2 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
131c3 65 2c 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 e, so that it is
131c4 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 20 68 6f not used for ho
131c5 74 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61 t.** rollba
131c6 63 6b 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 ck at any point
131c7 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 2a in the future..*
131c8 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74 6f 20 *.** subject to
131c9 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 the following qu
131ca 61 6c 69 66 69 63 61 74 69 6f 6e 73 3a 0a 2a 2a alifications:.**
131cb 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 6a 6f 75 .** * If the jou
131cc 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 rnal file is not
131cd 20 79 65 74 20 6f 70 65 6e 20 77 68 65 6e 20 74 yet open when t
131ce 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
131cf 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 74 68 65 called,.** the
131d0 6e 20 6f 6e 6c 79 20 28 32 29 20 69 73 20 70 65 n only (2) is pe
131d1 72 66 6f 72 6d 65 64 2e 20 49 6e 20 74 68 69 73 rformed. In this
131d2 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e case there is n
131d3 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a o journal file.*
131d4 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b * to roll back
131d5 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 69 6e 20 ..**.** * If in
131d6 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f an error state o
131d7 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 ther than SQLITE
131d8 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 74 61 73 6b _FULL, then task
131d9 20 28 31 29 20 69 73 20 0a 2a 2a 20 20 20 70 65 (1) is .** pe
131da 72 66 6f 72 6d 65 64 2e 20 49 66 20 73 75 63 63 rformed. If succ
131db 65 73 73 66 75 6c 2c 20 74 61 73 6b 20 28 32 29 essful, task (2)
131dc 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 . Regardless of
131dd 74 68 65 20 6f 75 74 63 6f 6d 65 0a 2a 2a 20 20 the outcome.**
131de 20 6f 66 20 65 69 74 68 65 72 2c 20 74 68 65 20 of either, the
131df 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f error state erro
131e0 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
131e1 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
131e2 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 65 69 74 68 .** (i.e. eith
131e3 65 72 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 er SQLITE_IOERR
131e4 6f 72 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 or SQLITE_CORRUP
131e5 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 T)..**.** * If t
131e6 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 he pager is in P
131e7 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 AGER_RESERVED st
131e8 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 ate, then attemp
131e9 74 20 28 31 29 2e 20 57 68 65 74 68 65 72 0a 2a t (1). Whether.*
131ea 2a 20 20 20 6f 72 20 6e 6f 74 20 28 31 29 20 69 * or not (1) i
131eb 73 20 73 75 63 63 75 73 73 66 75 6c 2c 20 61 6c s succussful, al
131ec 73 6f 20 61 74 74 65 6d 70 74 20 28 32 29 2e 20 so attempt (2).
131ed 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 If successful, r
131ee 65 74 75 72 6e 0a 2a 2a 20 20 20 53 51 4c 49 54 eturn.** SQLIT
131ef 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c E_OK. Otherwise,
131f0 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 enter the error
131f1 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 75 72 state and retur
131f2 6e 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 n the first .**
131f3 20 20 65 72 72 6f 72 20 63 6f 64 65 20 65 6e 63 error code enc
131f4 6f 75 6e 74 65 72 65 64 2e 20 0a 2a 2a 0a 2a 2a ountered. .**.**
131f5 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 In this case
131f6 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e there is no chan
131f7 63 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61 ce that the data
131f8 62 61 73 65 20 77 61 73 20 77 72 69 74 74 65 6e base was written
131f9 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 6f 20 69 73 to. .** So is
131fa 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a safe to finaliz
131fb 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
131fc 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 70 le even if the p
131fd 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 20 20 28 6f layback .** (o
131fe 70 65 72 61 74 69 6f 6e 20 31 29 20 66 61 69 6c peration 1) fail
131ff 65 64 2e 20 48 6f 77 65 76 65 72 20 74 68 65 20 ed. However the
13200 70 61 67 65 72 20 6d 75 73 74 20 65 6e 74 65 72 pager must enter
13201 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
13202 0a 2a 2a 20 20 20 61 73 20 74 68 65 20 63 6f 6e .** as the con
13203 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69 6e 2d tents of the in-
13204 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 61 72 65 memory cache are
13205 20 6e 6f 77 20 73 75 73 70 65 63 74 2e 0a 2a 2a now suspect..**
13206 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c 79 2c 20 69 .** * Finally, i
13207 66 20 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 f in PAGER_EXCLU
13208 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e SIVE state, then
13209 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 4f 6e attempt (1). On
1320a 6c 79 0a 2a 2a 20 20 20 61 74 74 65 6d 70 74 20 ly.** attempt
1320b 28 32 29 20 69 66 20 28 31 29 20 69 73 20 73 75 (2) if (1) is su
1320c 63 63 65 73 73 66 75 6c 2e 20 52 65 74 75 72 6e ccessful. Return
1320d 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 SQLITE_OK if su
1320e 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 20 20 6f ccessful,.** o
1320f 74 68 65 72 77 69 73 65 20 65 6e 74 65 72 20 74 therwise enter t
13210 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 61 he error state a
13211 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 nd return the er
13212 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 ror code from th
13213 65 20 0a 2a 2a 20 20 20 66 61 69 6c 69 6e 67 20 e .** failing
13214 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a operation..**.**
13215 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 In this case
13216 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13217 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 e may have been
13218 77 72 69 74 74 65 6e 20 74 6f 2e 20 53 6f 20 69 written to. So i
13219 66 20 74 68 65 0a 2a 2a 20 20 20 70 6c 61 79 62 f the.** playb
1321a 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 64 69 ack operation di
1321b 64 20 6e 6f 74 20 73 75 63 63 65 65 64 20 69 74 d not succeed it
1321c 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 would not be sa
1321d 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65 0a 2a fe to finalize.*
1321e 2a 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 * the journal
1321f 66 69 6c 65 2e 20 49 74 20 6e 65 65 64 73 20 74 file. It needs t
13220 6f 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 o be left in the
13221 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 6f 20 file-system so
13222 74 68 61 74 0a 2a 2a 20 20 20 73 6f 6d 65 20 6f that.** some o
13223 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e ther process can
13224 20 75 73 65 20 69 74 20 74 6f 20 72 65 73 74 6f use it to resto
13225 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 re the database
13226 73 74 61 74 65 20 28 62 79 0a 2a 2a 20 20 20 68 state (by.** h
13227 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
13228 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ack)..*/.SQLITE_
13229 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1322a 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
1322b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
1322c 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1322d 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
1322e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
1322f 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41 47 45 n code */. PAGE
13230 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42 41 43 RTRACE(("ROLLBAC
13231 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 K %d\n", PAGERID
13232 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 69 66 (pPager)));. if
13233 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 ( !pPager->dbMod
13234 69 66 69 65 64 20 7c 7c 20 21 69 73 4f 70 65 6e ified || !isOpen
13235 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
13236 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
13237 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
13238 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
13239 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 7d 65 setMaster);. }e
1323a 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e lse if( pPager->
1323b 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65 errCode && pPage
1323c 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 r->errCode!=SQLI
1323d 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 69 TE_FULL ){. i
1323e 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
1323f 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 >=PAGER_EXCLUSIV
13240 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72 E ){. pager
13241 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 _playback(pPager
13242 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 0);. }.
13243 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 rc = pPager->err
13244 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Code;. }else{.
13245 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 if( pPager->s
13246 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 tate==PAGER_RESE
13247 52 56 45 44 20 29 7b 0a 20 20 20 20 20 20 69 6e RVED ){. in
13248 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20 t rc2;. rc
13249 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
1324a 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 (pPager, 0);.
1324b 20 20 20 72 63 32 20 3d 20 70 61 67 65 72 5f 65 rc2 = pager_e
1324c 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 nd_transaction(p
1324d 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 Pager, pPager->s
1324e 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 etMaster);.
1324f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13250 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
13251 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a = rc2;. }.
13252 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13253 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 rc = pager_play
13254 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b back(pPager, 0);
13255 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
13256 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 !MEMDB ){.
13257 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
13258 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a lid = 0;. }..
13259 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 /* If an err
1325a 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
1325b 20 61 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 a ROLLBACK, we
1325c 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 can no longer tr
1325d 75 73 74 20 74 68 65 20 70 61 67 65 72 0a 20 20 ust the pager.
1325e 20 20 2a 2a 20 63 61 63 68 65 2e 20 53 6f 20 63 ** cache. So c
1325f 61 6c 6c 20 70 61 67 65 72 5f 65 72 72 6f 72 28 all pager_error(
13260 29 20 6f 6e 20 74 68 65 20 77 61 79 20 6f 75 74 ) on the way out
13261 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 65 72 72 to make any err
13262 6f 72 20 0a 20 20 20 20 2a 2a 20 70 65 72 73 69 or . ** persi
13263 73 74 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 stent.. */.
13264 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 rc = pager_err
13265 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
13266 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13267 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
13268 20 54 52 55 45 20 69 66 20 74 68 65 20 64 61 74 TRUE if the dat
13269 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 abase file is op
1326a 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 ened read-only.
1326b 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a Return FALSE.**
1326c 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 if the database
1326d 20 69 73 20 28 69 6e 20 74 68 65 6f 72 79 29 20 is (in theory)
1326e 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c writable..*/.SQL
1326f 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 ITE_PRIVATE u8 s
13270 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 qlite3PagerIsrea
13271 64 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 50 61 donly(Pager *pPa
13272 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 ger){. return p
13273 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b Pager->readOnly;
13274 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
13275 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
13276 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
13277 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 pager..*/.SQLIT
13278 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13279 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
1327a 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
1327b 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
1327c 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e te3PcacheRefCoun
1327d 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
1327e 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 e);.}../*.** Ret
1327f 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
13280 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 f references to
13281 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 the specified pa
13282 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ge..*/.SQLITE_PR
13283 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
13284 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
13285 6e 74 28 44 62 50 61 67 65 20 2a 70 50 61 67 65 nt(DbPage *pPage
13286 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
13287 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 te3PcachePageRef
13288 63 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a 7d 0a count(pPage);.}.
13289 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1328a 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 EST./*.** This r
1328b 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 outine is used f
1328c 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 or testing and a
1328d 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f nalysis only..*/
1328e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1328f 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 int *sqlite3Page
13290 72 53 74 61 74 73 28 50 61 67 65 72 20 2a 70 50 rStats(Pager *pP
13291 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69 63 20 ager){. static
13292 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 5b 30 int a[11];. a[0
13293 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 ] = sqlite3Pcach
13294 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 eRefCount(pPager
13295 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b ->pPCache);. a[
13296 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 1] = sqlite3Pcac
13297 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 hePagecount(pPag
13298 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
13299 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 a[2] = sqlite3Pc
1329a 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 acheGetCachesize
1329b 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
1329c 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 61 67 );. a[3] = pPag
1329d 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
1329e 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72 2d 3e ? (int) pPager->
1329f 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 20 61 dbSize : -1;. a
132a0 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 [4] = pPager->st
132a1 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 70 50 ate;. a[5] = pP
132a2 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
132a3 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[6] = pPager->
132a4 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d 20 70 nHit;. a[7] = p
132a5 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20 Pager->nMiss;.
132a6 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 55 73 a[8] = 0; /* Us
132a7 65 64 20 74 6f 20 62 65 20 70 50 61 67 65 72 2d ed to be pPager-
132a8 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d >nOvfl */. a[9]
132a9 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 = pPager->nRead
132aa 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 61 67 ;. a[10] = pPag
132ab 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 72 65 er->nWrite;. re
132ac 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64 69 66 turn a;.}.#endif
132ad 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
132ae 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 rue if this is a
132af 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 n in-memory page
132b0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
132b1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
132b2 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 PagerIsMemdb(Pag
132b3 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
132b4 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 0a eturn MEMDB;.}..
132b5 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 /*.** Check that
132b6 20 74 68 65 72 65 20 61 72 65 20 61 74 20 6c 65 there are at le
132b7 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 20 73 ast nSavepoint s
132b8 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2e 20 avepoints open.
132b9 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 If there are.**
132ba 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 20 74 currently less t
132bb 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 73 20 han nSavepoints
132bc 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 6e 20 open, then open
132bd 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 one or more save
132be 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d 61 6b points.** to mak
132bf 65 20 75 70 20 74 68 65 20 64 69 66 66 65 72 65 e up the differe
132c0 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75 6d 62 nce. If the numb
132c1 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 er of savepoints
132c2 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 is already.** e
132c3 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 6f 69 qual to nSavepoi
132c4 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 nt, then this fu
132c5 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
132c6 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 p..**.** If a me
132c7 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
132c8 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f fails, SQLITE_NO
132c9 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e MEM is returned.
132ca 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a If an error .**
132cb 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 occurs while op
132cc 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d 6a 6f ening the sub-jo
132cd 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e urnal file, then
132ce 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
132cf 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 e is.** returned
132d0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
132d1 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 ITE_OK..*/.SQLIT
132d2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
132d3 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 lite3PagerOpenSa
132d4 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
132d5 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 76 65 Pager, int nSave
132d6 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 point){. int rc
132d7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
132d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132d9 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
132da 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 de */. int nCur
132db 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e rent = pPager->n
132dc 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 Savepoint;
132dd 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d /* Current num
132de 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 ber of savepoint
132df 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53 61 76 s */.. if( nSav
132e0 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e 74 20 epoint>nCurrent
132e1 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f && pPager->useJo
132e2 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 urnal ){. int
132e3 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
132e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132e5 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
132e6 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 variable */.
132e7 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
132e8 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 *aNew;
132e9 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 /* New
132ea 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 Pager.aSavepoint
132eb 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20 20 2f array */.. /
132ec 2a 20 45 69 74 68 65 72 20 74 68 65 72 65 20 69 * Either there i
132ed 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f 75 72 s no active jour
132ee 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62 2d 6a nal or the sub-j
132ef 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 6f ournal is open o
132f0 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f r . ** the jo
132f1 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79 73 20 urnal is always
132f2 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 stored in memory
132f3 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
132f4 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
132f5 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65 6e 28 nt==0 || isOpen(
132f6 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 7c 7c pPager->sjfd) ||
132f7 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 . pPa
132f8 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
132f9 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
132fa 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 0a 20 ODE_MEMORY );..
132fb 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20 50 /* Grow the P
132fc 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 ager.aSavepoint
132fd 61 72 72 61 79 20 75 73 69 6e 67 20 72 65 61 6c array using real
132fe 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20 53 51 loc(). Return SQ
132ff 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20 20 2a LITE_NOMEM. *
13300 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 * if the allocat
13301 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68 65 72 ion fails. Other
13302 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65 20 6e wise, zero the n
13303 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20 63 61 ew portion in ca
13304 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d 61 6c se a . ** mal
13305 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 loc failure occu
13306 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c 61 74 rs while populat
13307 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20 66 6f ing it in the fo
13308 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65 6c 6f r(...) loop belo
13309 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 4e w.. */. aN
1330a 65 77 20 3d 20 28 50 61 67 65 72 53 61 76 65 70 ew = (PagerSavep
1330b 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33 52 65 oint *)sqlite3Re
1330c 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20 20 70 alloc(. p
1330d 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
1330e 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65 72 53 t, sizeof(PagerS
1330f 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76 65 70 avepoint)*nSavep
13310 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 oint. );.
13311 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 20 20 if( !aNew ){.
13312 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
13313 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
13314 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77 5b 6e memset(&aNew[n
13315 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28 6e 53 Current], 0, (nS
13316 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72 65 6e avepoint-nCurren
13317 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61 67 65 t) * sizeof(Page
13318 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a 20 20 rSavepoint));.
13319 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 pPager->aSavep
1331a 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20 20 20 oint = aNew;.
1331b 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f pPager->nSavepo
1331c 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69 6e 74 int = nSavepoint
1331d 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 ;.. /* Popula
1331e 74 65 20 74 68 65 20 50 61 67 65 72 53 61 76 65 te the PagerSave
1331f 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 73 point structures
13320 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 64 2e just allocated.
13321 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e */. for(ii=n
13322 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 61 76 Current; ii<nSav
13323 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 epoint; ii++){.
13324 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
13325 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
13326 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 );. aNew[i
13327 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61 67 65 i].nOrig = pPage
13328 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 20 r->dbSize;.
13329 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
1332a 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67 er->jfd) && pPag
1332b 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 er->journalOff>0
1332c 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 77 ){. aNew
1332d 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 70 [ii].iOffset = p
1332e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
1332f 66 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a f;. }else{.
13330 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d aNew[ii]
13331 2e 69 4f 66 66 73 65 74 20 3d 20 4a 4f 55 52 4e .iOffset = JOURN
13332 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
13333 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
13334 20 61 4e 65 77 5b 69 69 5d 2e 69 53 75 62 52 65 aNew[ii].iSubRe
13335 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 c = pPager->nSub
13336 52 65 63 3b 0a 20 20 20 20 20 20 61 4e 65 77 5b Rec;. aNew[
13337 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 ii].pInSavepoint
13338 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 = sqlite3Bitvec
13339 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 64 Create(pPager->d
1333a 62 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 bSize);. if
1333b 28 20 21 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 ( !aNew[ii].pInS
1333c 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 avepoint ){.
1333d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1333e 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d E_NOMEM;. }
1333f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f . }.. /* O
13340 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 pen the sub-jour
13341 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 6e 6f nal, if it is no
13342 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 t already opened
13343 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6f 70 . */. rc = op
13344 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 enSubJournal(pPa
13345 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 ger);. }.. ret
13346 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
13347 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
13348 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c s called to roll
13349 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 back or release
1334a 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70 (commit) a savep
1334b 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76 oint..** The sav
1334c 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73 epoint to releas
1334d 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 e or rollback ne
1334e 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f ed not be the mo
1334f 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20 st recently .**
13350 63 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e created savepoin
13351 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 t..**.** Paramet
13352 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20 er op is always
13353 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 either SAVEPOINT
13354 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56 _ROLLBACK or SAV
13355 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a EPOINT_RELEASE..
13356 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56 45 ** If it is SAVE
13357 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 POINT_RELEASE, t
13358 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20 hen release and
13359 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 destroy the save
1335a 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e point with.** in
1335b 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 dex iSavepoint.
1335c 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 If it is SAVEPOI
1335d 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 NT_ROLLBACK, the
1335e 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 n rollback all c
1335f 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68 hanges.** that h
13360 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e ave occurred sin
13361 63 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ce the specified
13362 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63 savepoint was c
13363 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 reated..**.** Th
13364 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 e savepoint to r
13365 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 ollback or relea
13366 73 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 se is identified
13367 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a by parameter .*
13368 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20 * iSavepoint. A
13369 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 value of 0 means
1336a 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 to operate on t
1336b 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 he outermost sav
1336c 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66 epoint.** (the f
1336d 69 72 73 74 20 63 72 65 61 74 65 64 29 2e 20 41 irst created). A
1336e 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72 value of (Pager
1336f 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d .nSavepoint-1) m
13370 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20 eans operate.**
13371 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 on the most rece
13372 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 61 76 ntly created sav
13373 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65 epoint. If iSave
13374 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 point is greater
13375 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e than.** (Pager.
13376 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74 nSavepoint-1), t
13377 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
13378 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
13379 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76 .** If a negativ
1337a 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 e value is passe
1337b 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 d to this functi
1337c 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 on, then the cur
1337d 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 rent.** transact
1337e 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 ion is rolled ba
1337f 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 ck. This is diff
13380 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67 erent to calling
13381 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 .** sqlite3Page
13382 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61 rRollback() beca
13383 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f use this functio
13384 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69 n does not termi
13385 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e nate.** the tran
13386 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63 saction or unloc
13387 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 k the database,
13388 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73 it just restores
13389 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 the .** content
1338a 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 s of the databas
1338b 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 e to its origina
1338c 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 l state. .**.**
1338d 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c In any case, all
1338e 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68 savepoints with
1338f 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 an index greate
13390 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e r than iSavepoin
13391 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f t .** are destro
13392 79 65 64 2e 20 49 66 20 74 68 69 73 20 69 73 20 yed. If this is
13393 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74 a release operat
13394 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49 ion (op==SAVEPOI
13395 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20 NT_RELEASE),.**
13396 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 then savepoint i
13397 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73 Savepoint is als
13398 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a o destroyed..**.
13399 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1339a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
1339b 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65 TE_NOMEM if a me
1339c 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
1339d 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20 fails,.** or an
1339e 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 IO error code if
1339f 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
133a0 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e urs while rollin
133a1 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76 g back a .** sav
133a2 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72 epoint. If no er
133a3 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49 rors occur, SQLI
133a4 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
133a5 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 d..*/ .SQLITE_PR
133a6 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
133a7 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 3PagerSavepoint(
133a8 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
133a9 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 nt op, int iSave
133aa 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 point){. int rc
133ab 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
133ac 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 assert( op==SAV
133ad 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c EPOINT_RELEASE |
133ae 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f | op==SAVEPOINT_
133af 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 73 ROLLBACK );. as
133b0 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74 sert( iSavepoint
133b1 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 >=0 || op==SAVEP
133b2 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b OINT_ROLLBACK );
133b3 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70 6f 69 .. if( iSavepoi
133b4 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 nt<pPager->nSave
133b5 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 point ){. int
133b6 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
133b7 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 /* Iterator vari
133b8 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 able */. int
133b9 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 2f nNew; /
133ba 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d 61 * Number of rema
133bb 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 73 ining savepoints
133bc 20 61 66 74 65 72 20 74 68 69 73 20 6f 70 2e 20 after this op.
133bd 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 */.. /* Figur
133be 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 73 e out how many s
133bf 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 73 avepoints will s
133c0 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65 20 61 till be active a
133c1 66 74 65 72 20 74 68 69 73 0a 20 20 20 20 2a 2a fter this. **
133c2 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f 72 operation. Stor
133c3 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 6e 20 e this value in
133c4 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65 65 20 nNew. Then free
133c5 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 resources associ
133c6 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 74 ated . ** wit
133c7 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 h any savepoints
133c8 20 74 68 61 74 20 61 72 65 20 64 65 73 74 72 6f that are destro
133c9 79 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 yed by this oper
133ca 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ation.. */.
133cb 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f nNew = iSavepo
133cc 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 50 int + (op==SAVEP
133cd 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a OINT_ROLLBACK);.
133ce 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 3b for(ii=nNew;
133cf 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 ii<pPager->nSav
133d0 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 epoint; ii++){.
133d1 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
133d2 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 ecDestroy(pPager
133d3 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d ->aSavepoint[ii]
133d4 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a .pInSavepoint);.
133d5 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 }. pPager
133d6 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e ->nSavepoint = n
133d7 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 New;.. /* If
133d8 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 this is a rollba
133d9 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c ck operation, pl
133da 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 69 ayback the speci
133db 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a fied savepoint..
133dc 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 ** If this i
133dd 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69 s a temp-file, i
133de 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 t is possible th
133df 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 at the journal f
133e0 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e ile has. ** n
133e1 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e ot yet been open
133e2 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
133e3 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 6e there have been
133e4 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 no changes to.
133e5 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ** the databa
133e6 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20 se file, so the
133e7 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 playback operati
133e8 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 on can be skippe
133e9 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 d.. */. if
133ea 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f ( op==SAVEPOINT_
133eb 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f 70 ROLLBACK && isOp
133ec 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
133ed 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 53 61 ){. PagerSa
133ee 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f vepoint *pSavepo
133ef 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 3f int = (nNew==0)?
133f0 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 0:&pPager->aSave
133f1 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a 20 point[nNew-1];.
133f2 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 rc = pagerP
133f3 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 laybackSavepoint
133f4 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 70 6f (pPager, pSavepo
133f5 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 int);. asse
133f6 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f rt(rc!=SQLITE_DO
133f7 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 NE);. }. .
133f8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
133f9 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65 a release of the
133fa 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70 outermost savep
133fb 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a oint, truncate .
133fc 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a ** the sub-j
133fd 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62 ournal to zero b
133fe 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f ytes in size. */
133ff 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30 . if( nNew==0
13400 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e && op==SAVEPOIN
13401 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69 73 4f T_RELEASE && isO
13402 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 pen(pPager->sjfd
13403 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ) ){. asser
13404 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
13405 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 );. rc = s
13406 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
13407 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 (pPager->sjfd, 0
13408 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d );. pPager-
13409 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 20 >nSubRec = 0;.
1340a 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1340b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
1340c 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 turn the full pa
1340d 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 thname of the da
1340e 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a tabase file..*/.
1340f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
13410 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
13411 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 e3PagerFilename(
13412 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
13413 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
13414 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f >zFilename;.}../
13415 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
13416 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f VFS structure fo
13417 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a r the pager..*/.
13418 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
13419 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 onst sqlite3_vfs
1341a 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 *sqlite3PagerVf
1341b 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 s(Pager *pPager)
1341c 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
1341d 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a r->pVfs;.}../*.*
1341e 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c * Return the fil
1341f 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 e handle for the
13420 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
13421 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
13422 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 h the pager. Th
13423 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 is might return
13424 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65 NULL if the file
13425 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 has.** not yet
13426 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a been opened..*/.
13427 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
13428 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c qlite3_file *sql
13429 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 ite3PagerFile(Pa
1342a 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
1342b 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66 return pPager->f
1342c 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 d;.}../*.** Retu
1342d 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 rn the full path
1342e 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 name of the jour
1342f 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c nal file..*/.SQL
13430 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
13431 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 t char *sqlite3P
13432 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 agerJournalname(
13433 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
13434 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
13435 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a >zJournal;.}../*
13436 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
13437 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c 6c 73 if fsync() calls
13438 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 66 6f are disabled fo
13439 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 52 r this pager. R
1343a 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 eturn FALSE.** i
1343b 66 20 66 73 79 6e 63 28 29 73 20 61 72 65 20 65 f fsync()s are e
1343c 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79 xecuted normally
1343d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1343e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
1343f 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 agerNosync(Pager
13440 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 *pPager){. ret
13441 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 urn pPager->noSy
13442 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 nc;.}..#ifdef SQ
13443 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f LITE_HAS_CODEC./
13444 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 64 *.** Set the cod
13445 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 ec for this page
13446 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 r.*/.SQLITE_PRIV
13447 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
13448 50 61 67 65 72 53 65 74 43 6f 64 65 63 28 0a 20 PagerSetCodec(.
13449 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 0a Pager *pPager,.
1344a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 void *(*xCodec
1344b 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 )(void*,void*,Pg
1344c 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 no,int),. void
1344d 2a 70 43 6f 64 65 63 41 72 67 0a 29 7b 0a 20 20 *pCodecArg.){.
1344e 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d pPager->xCodec =
1344f 20 78 43 6f 64 65 63 3b 0a 20 20 70 50 61 67 65 xCodec;. pPage
13450 72 2d 3e 70 43 6f 64 65 63 41 72 67 20 3d 20 70 r->pCodecArg = p
13451 43 6f 64 65 63 41 72 67 3b 0a 7d 0a 23 65 6e 64 CodecArg;.}.#end
13452 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
13453 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
13454 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 UM./*.** Move th
13455 65 20 70 61 67 65 20 70 50 67 20 74 6f 20 6c 6f e page pPg to lo
13456 63 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 cation pgno in t
13457 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 he file..**.** T
13458 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 here must be no
13459 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
1345a 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c e page previousl
1345b 79 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 y located at.**
1345c 70 67 6e 6f 20 28 77 68 69 63 68 20 77 65 20 63 pgno (which we c
1345d 61 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 all pPgOld) thou
1345e 67 68 20 74 68 61 74 20 70 61 67 65 20 69 73 20 gh that page is
1345f 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a allowed to be.**
13460 20 69 6e 20 63 61 63 68 65 2e 20 20 49 66 20 74 in cache. If t
13461 68 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 he page previous
13462 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 20 70 67 ly located at pg
13463 6e 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 no is not alread
13464 79 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c y.** in the roll
13465 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 back journal, it
13466 20 69 73 20 6e 6f 74 20 70 75 74 20 74 68 65 72 is not put ther
13467 65 20 62 79 20 62 79 20 74 68 69 73 20 72 6f 75 e by by this rou
13468 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 tine..**.** Refe
13469 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
1346a 67 65 20 70 50 67 20 72 65 6d 61 69 6e 20 76 61 ge pPg remain va
1346b 6c 69 64 2e 20 55 70 64 61 74 69 6e 67 20 61 6e lid. Updating an
1346c 79 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 y.** meta-data a
1346d 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 ssociated with p
1346e 50 67 20 28 69 2e 65 2e 20 64 61 74 61 20 73 74 Pg (i.e. data st
1346f 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 45 78 74 ored in the nExt
13470 72 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f ra bytes.** allo
13471 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 cated along with
13472 20 74 68 65 20 70 61 67 65 29 20 69 73 20 74 68 the page) is th
13473 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 e responsibility
13474 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a of the caller..
13475 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 **.** A transact
13476 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 63 74 69 ion must be acti
13477 76 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 ve when this rou
13478 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 tine is called.
13479 49 74 20 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a It used to be.**
1347a 20 72 65 71 75 69 72 65 64 20 74 68 61 74 20 61 required that a
1347b 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1347c 61 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61 action was not a
1347d 63 74 69 76 65 2c 20 62 75 74 20 74 68 69 73 20 ctive, but this
1347e 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 restriction.** h
1347f 61 73 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 as been removed
13480 28 43 52 45 41 54 45 20 49 4e 44 45 58 20 6e 65 (CREATE INDEX ne
13481 65 64 73 20 74 6f 20 6d 6f 76 65 20 61 20 70 61 eds to move a pa
13482 67 65 20 77 68 65 6e 20 61 20 73 74 61 74 65 6d ge when a statem
13483 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 ent.** transacti
13484 6f 6e 20 69 73 20 61 63 74 69 76 65 29 2e 0a 2a on is active)..*
13485 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 75 72 *.** If the four
13486 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 73 43 th argument, isC
13487 6f 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 ommit, is non-ze
13488 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 ro, then this pa
13489 67 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d ge is being.** m
1348a 6f 76 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 oved as part of
1348b 61 20 64 61 74 61 62 61 73 65 20 72 65 6f 72 67 a database reorg
1348c 61 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 anization just b
1348d 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 efore the transa
1348e 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69 ction .** is bei
1348f 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e ng committed. In
13490 20 74 68 69 73 20 63 61 73 65 2c 20 69 74 20 69 this case, it i
13491 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 s guaranteed tha
13492 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 t the database p
13493 61 67 65 20 0a 2a 2a 20 70 50 67 20 72 65 66 65 age .** pPg refe
13494 72 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 rs to will not b
13495 65 20 77 72 69 74 74 65 6e 20 74 6f 20 61 67 61 e written to aga
13496 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 in within this t
13497 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ransaction..**.*
13498 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
13499 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 may return SQLIT
1349a 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f E_NOMEM or an IO
1349b 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 error code if a
1349c 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 n error.** occur
1349d 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 s. Otherwise, it
1349e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
1349f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 OK..*/.SQLITE_PR
134a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
134a1 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 3PagerMovepage(P
134a2 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 44 62 ager *pPager, Db
134a3 50 61 67 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 Page *pPg, Pgno
134a4 70 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d pgno, int isComm
134a5 69 74 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 it){. PgHdr *pP
134a6 67 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 gOld;
134a7 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 /* The page
134a8 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 being overwritte
134a9 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 n. */. Pgno nee
134aa 64 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b 20 20 dSyncPgno = 0;
134ab 20 20 20 20 20 2f 2a 20 4f 6c 64 20 76 61 6c 75 /* Old valu
134ac 65 20 6f 66 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 e of pPg->pgno,
134ad 69 66 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 if sync is requi
134ae 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b red */. int rc;
134af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
134b0 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
134b1 63 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f code */. Pgno o
134b2 72 69 67 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 rigPgno;
134b3 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 /* The or
134b4 69 67 69 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 iginal page numb
134b5 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 er */.. assert(
134b6 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a pPg->nRef>0 );.
134b7 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 . /* If the pag
134b8 65 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 e being moved is
134b9 20 64 69 72 74 79 20 61 6e 64 20 68 61 73 20 6e dirty and has n
134ba 6f 74 20 62 65 65 6e 20 73 61 76 65 64 20 62 79 ot been saved by
134bb 20 74 68 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a the latest. **
134bc 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e savepoint, then
134bd 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65 6e save the curren
134be 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 t contents of th
134bf 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 e page into the
134c0 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 . ** sub-journa
134c1 6c 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 72 l now. This is r
134c2 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c equired to handl
134c3 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 e the following
134c4 73 63 65 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20 scenario:. **.
134c5 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a ** BEGIN;. *
134c6 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 * <journal p
134c7 61 67 65 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69 age X, then modi
134c8 66 79 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e fy it in memory>
134c9 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f . ** SAVEPO
134ca 49 4e 54 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20 INT one;. **
134cb 20 20 20 20 3c 4d 6f 76 65 20 70 61 67 65 20 58 <Move page X
134cc 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a to location Y>.
134cd 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 ** ROLLBAC
134ce 4b 20 54 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 K TO one;. **.
134cf 20 2a 2a 20 49 66 20 70 61 67 65 20 58 20 77 65 ** If page X we
134d0 72 65 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 re not written t
134d1 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 o the sub-journa
134d2 6c 20 68 65 72 65 2c 20 69 74 20 77 6f 75 6c 64 l here, it would
134d3 20 6e 6f 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73 not. ** be pos
134d4 73 69 62 6c 65 20 74 6f 20 72 65 73 74 6f 72 65 sible to restore
134d5 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 68 its contents wh
134d6 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b en the "ROLLBACK
134d7 20 54 4f 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74 TO one". ** st
134d8 61 74 65 6d 65 6e 74 20 77 65 72 65 20 69 73 20 atement were is
134d9 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2a 0a processed.. **.
134da 20 20 2a 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50 ** subjournalP
134db 61 67 65 28 29 20 6d 61 79 20 6e 65 65 64 20 74 age() may need t
134dc 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 o allocate space
134dd 20 74 6f 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 to store pPg->p
134de 67 6e 6f 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f 6e gno into. ** on
134df 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 6f e or more savepo
134e0 69 6e 74 20 62 69 74 76 65 63 73 2e 20 54 68 69 int bitvecs. Thi
134e1 73 20 69 73 20 74 68 65 20 72 65 61 73 6f 6e 20 s is the reason
134e2 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 this function.
134e3 2a 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 ** may return SQ
134e4 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f LITE_NOMEM.. */
134e5 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 . if( pPg->flag
134e6 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 0a 20 s&PGHDR_DIRTY .
134e7 20 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 && subjRequire
134e8 73 50 61 67 65 28 70 50 67 29 0a 20 20 20 26 26 sPage(pPg). &&
134e9 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
134ea 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 = subjournalPage
134eb 28 70 50 67 29 29 0a 20 20 29 7b 0a 20 20 20 20 (pPg)). ){.
134ec 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a return rc;. }..
134ed 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4d PAGERTRACE(("M
134ee 4f 56 45 20 25 64 20 70 61 67 65 20 25 64 20 28 OVE %d page %d (
134ef 6e 65 65 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 needSync=%d) mov
134f0 65 73 20 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 es to %d\n", .
134f1 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 PAGERID(pPag
134f2 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 er), pPg->pgno,
134f3 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
134f4 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 R_NEED_SYNC)?1:0
134f5 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 , pgno));. IOTR
134f6 41 43 45 28 28 22 4d 4f 56 45 20 25 70 20 25 64 ACE(("MOVE %p %d
134f7 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %d\n", pPager,
134f8 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 pPg->pgno, pgno)
134f9 29 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a ).. /* If the j
134fa 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 ournal needs to
134fb 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f be sync()ed befo
134fc 72 65 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e re page pPg->pgn
134fd 6f 20 63 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72 o can. ** be wr
134fe 69 74 74 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20 itten to, store
134ff 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 pPg->pgno in loc
13500 61 6c 20 76 61 72 69 61 62 6c 65 20 6e 65 65 64 al variable need
13501 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 SyncPgno.. **.
13502 20 2a 2a 20 49 66 20 74 68 65 20 69 73 43 6f 6d ** If the isCom
13503 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c mit flag is set,
13504 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
13505 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 d to remember th
13506 61 74 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 at. ** the jour
13507 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 nal needs to be
13508 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 sync()ed before
13509 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 50 database page pP
1350a 67 2d 3e 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 g->pgno . ** ca
1350b 6e 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e n be written to.
1350c 20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 The caller has
1350d 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 already promised
1350e 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f not to write to
1350f 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 it.. */. if(
13510 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
13511 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 26 26 20 R_NEED_SYNC) &&
13512 21 69 73 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 !isCommit ){.
13513 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 needSyncPgno =
13514 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 pPg->pgno;. a
13515 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 ssert( pageInJou
13516 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 rnal(pPg) || pPg
13517 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 ->pgno>pPager->d
13518 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 bOrigSize );.
13519 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c assert( pPg->fl
1351a 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 ags&PGHDR_DIRTY
1351b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1351c 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
1351d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
1351e 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69 the cache contai
1351f 6e 73 20 61 20 70 61 67 65 20 77 69 74 68 20 70 ns a page with p
13520 61 67 65 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c age-number pgno,
13521 20 72 65 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20 remove it. **
13522 66 72 6f 6d 20 69 74 73 20 68 61 73 68 20 63 68 from its hash ch
13523 61 69 6e 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68 ain. Also, if th
13524 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 e PgHdr.needSync
13525 20 77 61 73 20 73 65 74 20 66 6f 72 20 0a 20 20 was set for .
13526 2a 2a 20 70 61 67 65 20 70 67 6e 6f 20 62 65 66 ** page pgno bef
13527 6f 72 65 20 74 68 65 20 27 6d 6f 76 65 27 20 6f ore the 'move' o
13528 70 65 72 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65 peration, it nee
13529 64 73 20 74 6f 20 62 65 20 72 65 74 61 69 6e 65 ds to be retaine
1352a 64 20 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 d . ** for the
1352b 70 61 67 65 20 6d 6f 76 65 64 20 74 68 65 72 65 page moved there
1352c 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c .. */. pPg->fl
1352d 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 ags &= ~PGHDR_NE
1352e 45 44 5f 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c ED_SYNC;. pPgOl
1352f 64 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 d = pager_lookup
13530 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a (pPager, pgno);.
13531 20 20 61 73 73 65 72 74 28 20 21 70 50 67 4f 6c assert( !pPgOl
13532 64 20 7c 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65 d || pPgOld->nRe
13533 66 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 50 f==1 );. if( pP
13534 67 4f 6c 64 20 29 7b 0a 20 20 20 20 70 50 67 2d gOld ){. pPg-
13535 3e 66 6c 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c >flags |= (pPgOl
13536 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e d->flags&PGHDR_N
13537 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 7d 0a 0a EED_SYNC);. }..
13538 20 20 6f 72 69 67 50 67 6e 6f 20 3d 20 70 50 67 origPgno = pPg
13539 2d 3e 70 67 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 ->pgno;. sqlite
1353a 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 2c 3PcacheMove(pPg,
1353b 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 50 pgno);. if( pP
1353c 67 4f 6c 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 gOld ){. sqli
1353d 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 te3PcacheDrop(pP
1353e 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 gOld);. }.. sq
1353f 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
13540 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50 61 irty(pPg);. pPa
13541 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 ger->dbModified
13542 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65 64 = 1;.. if( need
13543 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20 20 SyncPgno ){.
13544 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50 67 /* If needSyncPg
13545 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 no is non-zero,
13546 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
13547 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 62 file needs to b
13548 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28 29 e . ** sync()
13549 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 61 ed before any da
1354a 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f ta is written to
1354b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 70 database file p
1354c 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f age needSyncPgno
1354d 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e 74 .. ** Current
1354e 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67 65 ly, no such page
1354f 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 70 exists in the p
13550 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74 68 age-cache and th
13551 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a 6f e . ** "is jo
13552 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63 20 urnaled" bitvec
13553 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73 65 flag has been se
13554 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74 6f t. This needs to
13555 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79 0a be remedied by.
13556 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20 74 ** loading t
13557 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 he page into the
13558 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 pager-cache and
13559 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67 48 setting the PgH
1355a 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 dr.needSync .
1355b 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a 2a ** flag.. **
1355c 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 61 . ** If the a
1355d 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 74 ttempt to load t
1355e 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 he page into the
1355f 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69 6c page-cache fail
13560 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20 74 s, (due. ** t
13561 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 o a malloc() or
13562 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c 65 IO failure), cle
13563 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74 68 ar the bit in th
13564 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a 20 e pInJournal[].
13565 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74 68 ** array. Oth
13566 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 70 erwise, if the p
13567 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61 6e age is loaded an
13568 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e 20 d written again
13569 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 74 in. ** this t
1356a 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 6d ransaction, it m
1356b 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f ay be written to
1356c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1356d 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a le before. **
1356e 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69 6e it is synced in
1356f 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
13570 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20 69 ile. This way, i
13571 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e 0a t may end up in.
13572 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e ** the journ
13573 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20 62 al file twice, b
13574 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 ut that is not a
13575 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a 2a problem.. **
13576 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 . ** The sqli
13577 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63 61 te3PagerGet() ca
13578 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 ll may cause the
13579 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e 63 journal to sync
1357a 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a . So make. **
1357b 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72 2e sure the Pager.
1357c 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 needSync flag is
1357d 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a 2f set too.. */
1357e 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 . PgHdr *pPgH
1357f 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 dr;. assert(
13580 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
13581 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
13582 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 ite3PagerGet(pPa
13583 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e ger, needSyncPgn
13584 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20 o, &pPgHdr);.
13585 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13586 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 OK ){. if(
13587 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
13588 61 6c 20 26 26 20 6e 65 65 64 53 79 6e 63 50 67 al && needSyncPg
13589 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 no<=pPager->dbOr
1358a 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 igSize ){.
1358b 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 sqlite3BitvecC
1358c 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 49 6e lear(pPager->pIn
1358d 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53 79 6e Journal, needSyn
1358e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 7d 0a cPgno);. }.
1358f 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
13590 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
13591 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b r->needSync = 1;
13592 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13593 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 ger->noSync==0 &
13594 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 & !MEMDB );.
13595 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d pPgHdr->flags |=
13596 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
13597 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 ;. sqlite3Pca
13598 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 cheMakeDirty(pPg
13599 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Hdr);. sqlite
1359a 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48 3PagerUnref(pPgH
1359b 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 dr);. }.. /*.
1359c 20 2a 2a 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65 ** For an in-me
1359d 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 6d mory database, m
1359e 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72 69 ake sure the ori
1359f 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 69 ginal page conti
135a0 6e 75 65 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69 nues. ** to exi
135a1 73 74 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 st, in case the
135a2 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 transaction need
135a3 73 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 s to roll back.
135a4 20 57 65 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a We allocate. *
135a5 2a 20 74 68 65 20 70 61 67 65 20 6e 6f 77 2c 20 * the page now,
135a6 69 6e 73 74 65 61 64 20 6f 66 20 61 74 20 72 6f instead of at ro
135a7 6c 6c 62 61 63 6b 2c 20 62 65 63 61 75 73 65 20 llback, because
135a8 77 65 20 63 61 6e 20 62 65 74 74 65 72 20 64 65 we can better de
135a9 61 6c 0a 20 20 2a 2a 20 77 69 74 68 20 61 6e 20 al. ** with an
135aa 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 out-of-memory er
135ab 72 6f 72 20 6e 6f 77 2e 20 20 54 69 63 6b 65 74 ror now. Ticket
135ac 20 23 33 37 36 31 2e 0a 20 20 2a 2f 0a 20 20 69 #3761.. */. i
135ad 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 f( MEMDB ){.
135ae 44 62 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 DbPage *pNew;.
135af 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
135b0 67 65 72 41 63 71 75 69 72 65 28 70 50 61 67 65 gerAcquire(pPage
135b1 72 2c 20 6f 72 69 67 50 67 6e 6f 2c 20 26 70 4e r, origPgno, &pN
135b2 65 77 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 ew, 1);. if(
135b3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
135b4 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 73 return rc;. s
135b5 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
135b6 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 (pNew);. }.. r
135b7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
135b8 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
135b9 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
135ba 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 66 6f r to the data fo
135bb 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 r the specified
135bc 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f page..*/.SQLITE_
135bd 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
135be 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
135bf 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a a(DbPage *pPg){.
135c0 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e assert( pPg->n
135c1 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 Ref>0 || pPg->pP
135c2 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20 ager->memDb );.
135c3 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 return pPg->pDa
135c4 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ta;.}../*.** Ret
135c5 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
135c6 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72 the Pager.nExtr
135c7 61 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72 a bytes of "extr
135c8 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c a" space .** all
135c9 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 ocated along wit
135ca 68 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 h the specified
135cb 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f page..*/.SQLITE_
135cc 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
135cd 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
135ce 72 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b ra(DbPage *pPg){
135cf 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
135d0 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
135d1 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 72 return (pPager
135d2 3f 70 50 67 2d 3e 70 45 78 74 72 61 3a 30 29 3b ?pPg->pExtra:0);
135d3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 .}../*.** Get/se
135d4 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f t the locking-mo
135d5 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 de for this page
135d6 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f r. Parameter eMo
135d7 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a de must be one.*
135d8 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49 * of PAGER_LOCKI
135d9 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41 NGMODE_QUERY, PA
135da 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
135db 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41 NORMAL or .** PA
135dc 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
135dd 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68 EXCLUSIVE. If th
135de 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e e parameter is n
135df 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a ot _QUERY, then.
135e0 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d ** the locking-m
135e1 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 ode is set to th
135e2 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 e value specifie
135e3 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 d..**.** The ret
135e4 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65 urned value is e
135e5 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b ither PAGER_LOCK
135e6 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f INGMODE_NORMAL o
135e7 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 r.** PAGER_LOCKI
135e8 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
135e9 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 , indicating the
135ea 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 current (possib
135eb 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c ly updated).** l
135ec 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a ocking-mode..*/.
135ed 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
135ee 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c nt sqlite3PagerL
135ef 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 ockingMode(Pager
135f0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d *pPager, int eM
135f1 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ode){. assert(
135f2 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 eMode==PAGER_LOC
135f3 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20 KINGMODE_QUERY.
135f4 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
135f5 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 ode==PAGER_LOCKI
135f6 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20 NGMODE_NORMAL.
135f7 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
135f8 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e de==PAGER_LOCKIN
135f9 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 GMODE_EXCLUSIVE
135fa 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
135fb 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 ER_LOCKINGMODE_Q
135fc 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65 UERY<0 );. asse
135fd 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e rt( PAGER_LOCKIN
135fe 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20 GMODE_NORMAL>=0
135ff 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 && PAGER_LOCKING
13600 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d MODE_EXCLUSIVE>=
13601 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65 0 );. if( eMode
13602 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e >=0 && !pPager->
13603 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 tempFile ){.
13604 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
13605 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 eMode = (u8)eMod
13606 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
13607 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63 (int)pPager->exc
13608 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f lusiveMode;.}../
13609 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 *.** Get/set the
1360a 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f journal-mode fo
1360b 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 r this pager. Pa
1360c 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 rameter eMode mu
1360d 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a st be one of:.**
1360e 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 .** PAGER_JOU
1360f 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 2a RNALMODE_QUERY.*
13610 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
13611 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a ALMODE_DELETE.**
13612 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
13613 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a LMODE_TRUNCATE.*
13614 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
13615 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a ALMODE_PERSIST.*
13616 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
13617 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 20 ALMODE_OFF.**
13618 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
13619 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a 20 DE_MEMORY.**.**
1361a 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 If the parameter
1361b 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 is not _QUERY,
1361c 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
1361d 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 -mode is set to
1361e 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 65 the.** value spe
1361f 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 cified..**.** Th
13620 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 e returned indic
13621 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ate the current
13622 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 (possibly update
13623 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e d) journal-mode.
13624 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13625 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
13626 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 gerJournalMode(P
13627 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
13628 74 20 65 4d 6f 64 65 29 7b 0a 20 20 69 66 28 20 t eMode){. if(
13629 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 61 73 !MEMDB ){. as
1362a 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 sert( eMode==PAG
1362b 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 ER_JOURNALMODE_Q
1362c 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 UERY.
1362d 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
1362e 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 ER_JOURNALMODE_D
1362f 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20 20 ELETE.
13630 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 || eMode==PA
13631 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
13632 54 52 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20 TRUNCATE.
13633 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d || eMode=
13634 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
13635 44 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20 DE_PERSIST.
13636 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 || eMod
13637 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
13638 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20 20 20 MODE_OFF .
13639 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 || eMode
1363a 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
1363b 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 ODE_MEMORY );.
1363c 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f assert( PAGER_
1363d 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 JOURNALMODE_QUER
1363e 59 3c 30 20 29 3b 0a 20 20 20 20 69 66 28 20 65 Y<0 );. if( e
1363f 4d 6f 64 65 3e 3d 30 20 29 7b 0a 20 20 20 20 20 Mode>=0 ){.
13640 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13641 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 Mode = (u8)eMode
13642 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
13643 20 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 assert( eMode
13644 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
13645 4f 44 45 5f 51 55 45 52 59 20 29 3b 0a 20 20 20 ODE_QUERY );.
13646 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
13647 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 (int)pPager->jou
13648 72 6e 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a rnalMode;.}../*.
13649 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 73 ** Get/set the s
1364a 69 7a 65 2d 6c 69 6d 69 74 20 75 73 65 64 20 66 ize-limit used f
1364b 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f or persistent jo
1364c 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a urnal files..*/.
1364d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1364e 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 64 sqlite3PagerJ
1364f 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 ournalSizeLimit(
13650 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
13651 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 64 iLimit){. if
13652 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a ( iLimit>=-1 ){.
13653 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
13654 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 nalSizeLimit = i
13655 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 Limit;. }. ret
13656 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 urn pPager->jour
13657 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a nalSizeLimit;.}.
13658 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
13659 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
1365a 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 Pager->pBackup v
1365b 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 ariable. The bac
1365c 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e kup module.** in
1365d 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 backup.c mainta
1365e 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ins the content
1365f 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 of this variable
13660 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a . This module.**
13661 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c uses it opaquel
13662 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 y as an argument
13663 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 to sqlite3Backu
13664 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a pRestart() and.*
13665 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 * sqlite3BackupU
13666 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f pdate() only..*/
13667 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13668 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
13669 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 *sqlite3PagerBac
1366a 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 kupPtr(Pager *pP
1366b 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
1366c 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 &pPager->pBackup
1366d 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
1366e 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
1366f 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a O */../*********
13670 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 ***** End of pag
13671 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
13672 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13674 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
13675 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
13676 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
13677 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13678 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13679 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
1367a 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a August 27.**.**
1367b 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1367c 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1367d 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1367e 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1367f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
13680 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
13681 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
13682 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
13683 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
13684 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
13685 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
13686 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
13687 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
13688 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
13689 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1368a 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1368b 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1368c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1368d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1368e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1368f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13690 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
13691 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76 $Id: btmutex.c,v
13692 20 31 2e 31 33 20 32 30 30 39 2f 30 33 2f 30 35 1.13 2009/03/05
13693 20 30 34 3a 32 30 3a 33 32 20 73 68 61 6e 65 20 04:20:32 shane
13694 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 Exp $.**.** This
13695 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
13696 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c ode used to impl
13697 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e ement mutexes on
13698 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a Btree objects..
13699 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 65 61 ** This code rea
1369a 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62 lly belongs in b
1369b 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72 tree.c. But btr
1369c 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20 ee.c is getting
1369d 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77 too.** big and w
1369e 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20 e want to break
1369f 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 it down some. T
136a0 68 69 73 20 70 61 63 6b 61 67 65 64 20 73 65 65 his packaged see
136a1 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f med like.** a go
136a2 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a od breakout..*/.
136a3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
136a4 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 Include btreeInt
136a5 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
136a6 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a of btmutex.c **
136a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
136a8 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
136a9 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 Begin file btree
136aa 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
136ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
136ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
136ad 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c /*.** 2004 April
136ae 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6.**.** The aut
136af 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
136b0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
136b1 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
136b2 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
136b3 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
136b4 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
136b5 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
136b6 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
136b7 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
136b8 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
136b9 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
136ba 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
136bb 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
136bc 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
136bd 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
136be 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
136bf 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
136c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
136c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
136c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
136c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
136c4 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49 *.** $Id: btreeI
136c5 6e 74 2e 68 2c 76 20 31 2e 34 36 20 32 30 30 39 nt.h,v 1.46 2009
136c6 2f 30 33 2f 32 30 20 31 34 3a 31 38 3a 35 32 20 /03/20 14:18:52
136c7 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
136c8 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c $.**.** This fil
136c9 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 e implements a e
136ca 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 xternal (disk-ba
136cb 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75 73 sed) database us
136cc 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 46 ing BTrees..** F
136cd 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 69 or a detailed di
136ce 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72 65 scussion of BTre
136cf 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a 0a es, refer to.**.
136d0 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45 2e ** Donald E.
136d1 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54 20 Knuth, THE ART
136d2 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f 47 OF COMPUTER PROG
136d3 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 RAMMING, Volume
136d4 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74 69 3:.** "Sorti
136d5 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e 67 ng And Searching
136d6 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38 30 ", pages 473-480
136d7 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65 79 . Addison-Wesley
136d8 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68 69 .** Publishi
136d9 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 64 ng Company, Read
136da 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65 74 ing, Massachuset
136db 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 ts..**.** The ba
136dc 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61 74 sic idea is that
136dd 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 each page of th
136de 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 e file contains
136df 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65 6e N database.** en
136e0 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70 6f tries and N+1 po
136e1 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61 67 inters to subpag
136e2 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d es..**.** ----
136e3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
136e4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
136e5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
136e6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 ------------.**
136e7 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b 65 | Ptr(0) | Ke
136e8 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c 20 y(0) | Ptr(1) |
136e9 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b Key(1) | ... | K
136ea 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e 29 ey(N-1) | Ptr(N)
136eb 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d |.** --------
136ec 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
136ed 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
136ee 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
136ef 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41 --------.**.** A
136f0 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f ll of the keys o
136f1 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 n the page that
136f2 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74 6f Ptr(0) points to
136f3 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65 73 have values les
136f4 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30 29 s.** than Key(0)
136f5 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 . All of the ke
136f6 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28 31 ys on page Ptr(1
136f7 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 ) and its subpag
136f8 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 es have.** value
136f9 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4b s greater than K
136fa 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20 74 ey(0) and less t
136fb 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c 6c han Key(1). All
136fc 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a 20 of the keys.**
136fd 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69 74 on Ptr(N) and it
136fe 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65 20 s subpages have
136ff 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 74 values greater t
13700 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20 41 han Key(N-1). A
13701 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e 0a nd.** so forth..
13702 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61 20 **.** Finding a
13703 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20 72 particular key r
13704 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67 20 equires reading
13705 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73 20 O(log(M)) pages
13706 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69 73 from the .** dis
13707 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68 65 k where M is the
13708 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 number of entri
13709 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a es in the tree..
1370a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d **.** In this im
1370b 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 20 plementation, a
1370c 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e 20 single file can
1370d 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 hold one or more
1370e 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42 54 separate .** BT
1370f 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72 65 rees. Each BTre
13710 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 e is identified
13711 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 by the index of
13712 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 its root page.
13713 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 The.** key and d
13714 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74 72 ata for any entr
13715 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 y are combined t
13716 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79 6c o form the "payl
13717 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78 65 oad". A.** fixe
13718 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c d amount of payl
13719 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72 69 oad can be carri
1371a 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20 74 ed directly on t
1371b 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 he database.** p
1371c 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 79 age. If the pay
1371d 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20 74 load is larger t
1371e 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20 61 han the preset a
1371f 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70 6c mount then surpl
13720 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65 20 us.** bytes are
13721 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66 6c stored on overfl
13722 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 70 ow pages. The p
13723 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65 6e ayload for an en
13724 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 try.** and the p
13725 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65 72 receding pointer
13726 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f are combined to
13727 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e 20 form a "Cell".
13728 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20 68 Each .** page h
13729 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64 65 as a small heade
1372a 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 r which contains
1372b 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69 6e the Ptr(N) poin
1372c 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a 2a ter and other.**
1372d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 63 information suc
1372e 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f 66 h as the size of
1372f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a key and data..*
13730 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54 41 *.** FORMAT DETA
13731 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ILS.**.** The fi
13732 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69 6e le is divided in
13733 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20 66 to pages. The f
13734 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61 6c irst page is cal
13735 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20 74 led page 1,.** t
13736 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61 67 he second is pag
13737 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 e 2, and so fort
13738 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62 65 h. A page numbe
13739 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63 61 r of zero indica
1373a 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 20 tes.** "no such
1373b 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67 65 page". The page
1373c 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e 79 size can be any
1373d 74 68 69 6e 67 20 62 65 74 77 65 65 6e 20 35 31 thing between 51
1373e 32 20 61 6e 64 20 36 35 35 33 36 2e 0a 2a 2a 20 2 and 65536..**
1373f 45 61 63 68 20 70 61 67 65 20 63 61 6e 20 62 65 Each page can be
13740 20 65 69 74 68 65 72 20 61 20 62 74 72 65 65 20 either a btree
13741 70 61 67 65 2c 20 61 20 66 72 65 65 6c 69 73 74 page, a freelist
13742 20 70 61 67 65 20 6f 72 20 61 6e 20 6f 76 65 72 page or an over
13743 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a flow.** page..**
13744 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 .** The first pa
13745 67 65 20 69 73 20 61 6c 77 61 79 73 20 61 20 62 ge is always a b
13746 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20 tree page. The
13747 66 69 72 73 74 20 31 30 30 20 62 79 74 65 73 20 first 100 bytes
13748 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 of the first.**
13749 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 61 20 73 page contain a s
1374a 70 65 63 69 61 6c 20 68 65 61 64 65 72 20 28 74 pecial header (t
1374b 68 65 20 22 66 69 6c 65 20 68 65 61 64 65 72 22 he "file header"
1374c 29 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 ) that describes
1374d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 the file..** Th
1374e 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 e format of the
1374f 66 69 6c 65 20 68 65 61 64 65 72 20 69 73 20 61 file header is a
13750 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
13751 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45 OFFSET SIZE
13752 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a DESCRIPTION.
13753 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 31 ** 0 1
13754 36 20 20 20 20 20 48 65 61 64 65 72 20 73 74 72 6 Header str
13755 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66 6f 72 ing: "SQLite for
13756 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a 20 20 20 mat 3\000".**
13757 20 20 31 36 20 20 20 20 20 20 20 32 20 20 20 20 16 2
13758 20 50 61 67 65 20 73 69 7a 65 20 69 6e 20 62 79 Page size in by
13759 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 20 31 38 tes. .** 18
1375a 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 6c 1 Fil
1375b 65 20 66 6f 72 6d 61 74 20 77 72 69 74 65 20 76 e format write v
1375c 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 31 39 ersion.** 19
1375d 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 6c 1 Fil
1375e 65 20 66 6f 72 6d 61 74 20 72 65 61 64 20 76 65 e format read ve
1375f 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 30 20 rsion.** 20
13760 20 20 20 20 20 20 31 20 20 20 20 20 42 79 74 65 1 Byte
13761 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 s of unused spac
13762 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 e at the end of
13763 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 20 20 20 each page.**
13764 20 32 31 20 20 20 20 20 20 20 31 20 20 20 20 20 21 1
13765 4d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 Max embedded pay
13766 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
13767 20 20 20 20 20 32 32 20 20 20 20 20 20 20 31 20 22 1
13768 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 64 65 64 Min embedded
13769 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
1376a 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 20 20 20 n.** 23
1376b 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65 61 66 1 Min leaf
1376c 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
1376d 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 20 20 20 n.** 24
1376e 20 20 34 20 20 20 20 20 46 69 6c 65 20 63 68 61 4 File cha
1376f 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 nge counter.**
13770 20 20 20 32 38 20 20 20 20 20 20 20 34 20 20 20 28 4
13771 20 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 Reserved for f
13772 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 20 20 20 uture use.**
13773 20 33 32 20 20 20 20 20 20 20 34 20 20 20 20 20 32 4
13774 46 69 72 73 74 20 66 72 65 65 6c 69 73 74 20 70 First freelist p
13775 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 20 20 20 age.** 36
13776 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 4 Number
13777 20 6f 66 20 66 72 65 65 6c 69 73 74 20 70 61 67 of freelist pag
13778 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a es in the file.*
13779 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 36 30 * 40 60
1377a 20 20 20 20 20 31 35 20 34 2d 62 79 74 65 20 6d 15 4-byte m
1377b 65 74 61 20 76 61 6c 75 65 73 20 70 61 73 73 65 eta values passe
1377c 64 20 74 6f 20 68 69 67 68 65 72 20 6c 61 79 65 d to higher laye
1377d 72 73 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 rs.**.** All of
1377e 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 the integer valu
1377f 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69 61 es are big-endia
13780 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 n (most signific
13781 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29 2e ant byte first).
13782 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 .**.** The file
13783 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 change counter i
13784 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 s incremented wh
13785 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
13786 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68 is changed.** Th
13787 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 is counter allow
13788 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 s other processe
13789 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74 s to know when t
1378a 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e he file has chan
1378b 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20 ged.** and thus
1378c 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20 74 when they need t
1378d 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63 61 o flush their ca
1378e 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d che..**.** The m
1378f 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c ax embedded payl
13790 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 oad fraction is
13791 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 the amount of th
13792 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a e total usable.*
13793 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61 67 * space in a pag
13794 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f e that can be co
13795 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e 67 nsumed by a sing
13796 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e le cell for stan
13797 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28 dard.** B-tree (
13798 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74 61 non-LEAFDATA) ta
13799 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20 6f bles. A value o
1379a 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30 25 f 255 means 100%
1379b 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a . The default.*
1379c 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74 68 * is to limit th
1379d 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 e maximum cell s
1379e 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20 6c ize so that at l
1379f 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69 6c east 4 cells wil
137a0 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 l fit.** on one
137a1 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65 20 page. Thus the
137a2 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62 65 default max embe
137a3 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 dded payload fra
137a4 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a ction is 64..**.
137a5 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 ** If the payloa
137a6 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 d for a cell is
137a7 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
137a8 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 max payload, the
137a9 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f n extra.** paylo
137aa 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 6f ad is spilled to
137ab 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
137ac 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c Once an overfl
137ad 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 ow page is alloc
137ae 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 ated,.** as many
137af 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 62 bytes as possib
137b0 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e 74 le are moved int
137b1 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 o the overflow p
137b2 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 74 ages without let
137b3 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c ting.** the cell
137b4 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77 size drop below
137b5 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 the min embedde
137b6 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
137b7 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 on..**.** The mi
137b8 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 n leaf payload f
137b9 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 raction is like
137ba 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 the min embedded
137bb 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
137bc 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 n.** except that
137bd 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20 6c it applies to l
137be 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c eaf nodes in a L
137bf 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20 54 EAFDATA tree. T
137c0 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 he maximum.** pa
137c1 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 66 yload fraction f
137c2 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 or a LEAFDATA tr
137c3 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30 30 ee is always 100
137c4 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20 69 % (or 255) and i
137c5 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66 69 t.** not specifi
137c6 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 ed in the header
137c7 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72 ..**.** Each btr
137c8 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76 69 ee pages is divi
137c9 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20 73 ded into three s
137ca 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68 65 ections: The he
137cb 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c ader, the.** cel
137cc 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 2c l pointer array,
137cd 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f and the cell co
137ce 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61 67 ntent area. Pag
137cf 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20 31 e 1 also has a 1
137d0 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20 00-byte.** file
137d1 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 75 header that occu
137d2 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70 61 rs before the pa
137d3 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a ge header..**.**
137d4 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------
137d5 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 -------|.**
137d6 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20 20 | file header
137d7 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73 2e | 100 bytes.
137d8 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a Page 1 only..*
137d9 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
137da 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
137db 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 20 | page header
137dc 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 66 | 8 bytes f
137dd 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 62 or leaves. 12 b
137de 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 6f ytes for interio
137df 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 r nodes.**
137e0 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------------
137e1 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c -|.** | cel
137e2 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20 20 l pointer |
137e3 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20 63 | 2 bytes per c
137e4 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72 64 ell. Sorted ord
137e5 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 er..** | ar
137e6 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20 20 ray |
137e7 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77 61 | Grows downwa
137e8 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20 20 rd.** |
137e9 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 |
137ea 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d v.** |-----
137eb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 -----------|.**
137ec 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 | unallocat
137ed 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 ed |.**
137ee 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20 20 | space
137ef 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d |.** |----
137f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 ------------|
137f1 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64 73 ^ Grows upwards
137f2 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 .** | cell
137f3 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c 20 content | |
137f4 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 Arbitrary order
137f5 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77 69 interspersed wi
137f6 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a th freeblocks..*
137f7 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20 20 * | area
137f8 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 61 | | a
137f9 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66 72 nd free space fr
137fa 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 agments..**
137fb 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
137fc 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 --|.**.** The pa
137fd 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73 ge headers looks
137fe 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
137ff 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a * OFFSET SIZ
13800 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49 4f E DESCRIPTIO
13801 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 N.** 0
13802 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 20 1 Flags.
13803 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 1: intkey, 2: ze
13804 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 rodata, 4: leafd
13805 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 ata, 8: leaf.**
13806 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20 20 1 2
13807 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 20 byte offset
13808 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65 to the first fre
13809 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33 eblock.** 3
1380a 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e 75 2 nu
1380b 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e mber of cells on
1380c 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 this page.**
1380d 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20 20 5 2
1380e 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 first byte of
1380f 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
13810 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37 20 area.** 7
13811 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75 6d 1 num
13812 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 ber of fragmente
13813 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a 20 d free bytes.**
13814 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20 20 8 4
13815 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64 20 Right child
13816 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c 75 (the Ptr(N) valu
13817 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20 e). Omitted on
13818 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 leaves..**.** Th
13819 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20 74 e flags define t
1381a 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 69 he format of thi
1381b 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 s btree page. T
1381c 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65 61 he leaf flag mea
1381d 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 20 ns that.** this
1381e 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69 6c page has no chil
1381f 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64 dren. The zerod
13820 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 ata flag means t
13821 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63 61 hat this page ca
13822 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 rries.** only ke
13823 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20 ys and no data.
13824 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61 67 The intkey flag
13825 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
13826 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65 72 key is a integer
13827 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74 6f .** which is sto
13828 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 73 red in the key s
13829 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68 65 ize entry of the
1382a 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61 74 cell header rat
1382b 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 her than in.** t
1382c 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 2e he payload area.
1382d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 .**.** The cell
1382e 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62 65 pointer array be
1382f 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72 73 gins on the firs
13830 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65 t byte after the
13831 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a page header..**
13832 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 The cell pointe
13833 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 r array contains
13834 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d zero or more 2-
13835 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68 69 byte numbers whi
13836 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65 74 ch are.** offset
13837 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e s from the begin
13838 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 ning of the page
13839 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e to the cell con
1383a 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c 6c tent in the cell
1383b 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 .** content area
1383c 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e . The cell poin
1383d 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 6f ters occur in so
1383e 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 65 rted order. The
1383f 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 0a system strives.
13840 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 20 ** to keep free
13841 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65 20 space after the
13842 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 last cell pointe
13843 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 65 r so that new ce
13844 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65 61 lls can.** be ea
13845 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68 6f sily added witho
13846 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65 66 ut having to def
13847 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65 ragment the page
13848 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e ..**.** Cell con
13849 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 61 tent is stored a
1384a 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 6f t the very end o
1384b 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 67 f the page and g
1384c 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65 0a rows toward the.
1384d 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 ** beginning of
1384e 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 the page..**.**
1384f 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69 74 Unused space wit
13850 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e hin the cell con
13851 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f 6c tent area is col
13852 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c 69 lected into a li
13853 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 nked list of.**
13854 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61 63 freeblocks. Eac
13855 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 61 h freeblock is a
13856 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 20 t least 4 bytes
13857 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62 79 in size. The by
13858 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f 20 te offset.** to
13859 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c the first freebl
1385a 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e 20 ock is given in
1385b 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72 65 the header. Fre
1385c 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69 6e eblocks occur in
1385d 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20 6f .** increasing o
1385e 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20 61 rder. Because a
1385f 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74 20 freeblock must
13860 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 be at least 4 by
13861 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 tes in size,.**
13862 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20 6f any group of 3 o
13863 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20 62 r fewer unused b
13864 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c ytes in the cell
13865 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63 61 content area ca
13866 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f 6e nnot.** exist on
13867 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 63 the freeblock c
13868 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20 6f hain. A group o
13869 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72 65 f 3 or fewer fre
1386a 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c 65 e bytes is calle
1386b 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74 2e d.** a fragment.
1386c 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 The total numb
1386d 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 er of bytes in a
1386e 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73 20 ll fragments is
1386f 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 recorded..** in
13870 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 the page header
13871 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a at offset 7..**.
13872 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 ** SIZE DE
13873 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 SCRIPTION.**
13874 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 66 2 Byte off
13875 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 set of the next
13876 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 freeblock.**
13877 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69 6e 2 Bytes in
13878 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b 0a this freeblock.
13879 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65 20 **.** Cells are
1387a 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 of variable leng
1387b 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20 73 th. Cells are s
1387c 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65 6c tored in the cel
1387d 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 61 l content area a
1387e 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 t.** the end of
1387f 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e 74 the page. Point
13880 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c 73 ers to the cells
13881 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c 6c are in the cell
13882 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 2a pointer array.*
13883 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 65 * that immediate
13884 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 70 ly follows the p
13885 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65 6c age header. Cel
13886 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 ls is not necess
13887 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 arily.** contigu
13888 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72 2c ous or in order,
13889 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 but cell pointe
1388a 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 rs are contiguou
1388b 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e 0a s and in order..
1388c 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 **.** Cell conte
1388d 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 20 nt makes use of
1388e 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 variable length
1388f 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61 72 integers. A var
13890 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 iable.** length
13891 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f 20 integer is 1 to
13892 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74 68 9 bytes where th
13893 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f e lower 7 bits o
13894 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65 20 f each .** byte
13895 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20 69 are used. The i
13896 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 nteger consists
13897 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61 of all bytes tha
13898 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65 74 t have bit 8 set
13899 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72 73 and.** the firs
1389a 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74 20 t byte with bit
1389b 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d 6f 8 clear. The mo
1389c 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 st significant b
1389d 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 yte of the integ
1389e 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66 69 er.** appears fi
1389f 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c 65 rst. A variable
138a0 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 -length integer
138a1 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65 20 may not be more
138a2 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f 6e than 9 bytes lon
138a3 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63 69 g..** As a speci
138a4 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20 62 al case, all 8 b
138a5 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68 20 ytes of the 9th
138a6 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61 73 byte are used as
138a7 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a 20 data. This.**
138a8 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 20 allows a 64-bit
138a9 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65 6e integer to be en
138aa 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65 73 coded in 9 bytes
138ab 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30 20 ..**.** 0x00
138ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
138ad 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 becomes 0x
138ae 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 30 00000000.** 0
138af 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 20 x7f
138b0 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 becomes
138b1 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 0x0000007f.**
138b2 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20 20 0x81 0x00
138b3 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 bec
138b4 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38 30 omes 0x00000080
138b5 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30 30 .** 0x82 0x00
138b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
138b7 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 becomes 0x0000
138b8 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30 20 0100.** 0x80
138b9 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 0x7f
138ba 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 becomes 0x
138bb 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 0000007f.** 0
138bc 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30 78 x8a 0x91 0xd1 0x
138bd 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65 73 ac 0x78 becomes
138be 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a 20 0x12345678.**
138bf 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78 38 0x81 0x81 0x8
138c0 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65 63 1 0x81 0x01 bec
138c1 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38 31 omes 0x10204081
138c2 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 .**.** Variable
138c3 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20 length integers
138c4 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f 77 are used for row
138c5 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64 20 ids and to hold
138c6 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
138c7 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61 6e bytes of key an
138c8 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72 65 d data in a btre
138c9 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 e cell..**.** Th
138ca 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 e content of a c
138cb 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 ell looks like t
138cc 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 his:.**.** SI
138cd 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f ZE DESCRIPTIO
138ce 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 N.** 4
138cf 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 Page number of t
138d0 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f he left child. O
138d1 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20 66 mitted if leaf f
138d2 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 lag is set..**
138d3 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 var Number
138d4 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 of bytes of dat
138d5 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 68 a. Omitted if th
138d6 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 e zerodata flag
138d7 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 is set..** v
138d8 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 ar Number of
138d9 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72 bytes of key. Or
138da 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 20 the key itself
138db 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 if intkey flag i
138dc 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20 2a s set..** *
138dd 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 Payload.**
138de 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 74 4 First
138df 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76 65 page of the ove
138e0 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f 6d rflow chain. Om
138e1 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65 72 itted if no over
138e2 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 flow.**.** Overf
138e3 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20 61 low pages form a
138e4 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 45 linked list. E
138e5 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74 20 ach page except
138e6 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d 70 the last is comp
138e7 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 letely.** filled
138e8 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67 65 with data (page
138e9 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29 2e size - 4 bytes).
138ea 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65 20 The last page
138eb 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74 74 can have as litt
138ec 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65 20 le.** as 1 byte
138ed 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 of data..**.**
138ee 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 SIZE DESCRI
138ef 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 PTION.** 4
138f0 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 Page number
138f1 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 of next overflow
138f2 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 page.** *
138f3 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 Data.**.** F
138f4 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63 6f reelist pages co
138f5 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79 70 me in two subtyp
138f6 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73 20 es: trunk pages
138f7 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e 20 and leaf pages.
138f8 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 The.** file hea
138f9 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 der points to th
138fa 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e e first in a lin
138fb 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75 6e ked list of trun
138fc 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74 72 k page. Each tr
138fd 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e unk.** page poin
138fe 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c ts to multiple l
138ff 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 20 eaf pages. The
13900 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 61 content of a lea
13901 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e 73 f page is.** uns
13902 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 75 pecified. A tru
13903 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 nk page looks li
13904 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
13905 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 SIZE DESCRI
13906 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 PTION.** 4
13907 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 Page number
13908 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70 61 of next trunk pa
13909 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ge.** 4
1390a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 Number of leaf
1390b 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69 73 pointers on this
1390c 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 page.** *
1390d 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 zero or more
1390e 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 6f pages numbers o
1390f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f 2a f leaves.*/.../*
13910 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
13911 61 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 69 alue is the maxi
13912 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 mum cell size as
13913 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d suming a maximum
13914 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 page.** size gi
13915 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 ve above..*/.#de
13916 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a fine MX_CELL_SIZ
13917 45 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 61 E(pBt) (pBt->pa
13918 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 geSize-8)../* Th
13919 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1391a 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 of cells on a s
1391b 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68 ingle page of th
1391c 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 e database. Thi
1391d 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d s.** assumes a m
1391e 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 inimum cell size
1391f 20 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 20 of 6 bytes (4
13920 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 65 bytes for the ce
13921 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 ll itself.** plu
13922 73 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 68 s 2 bytes for th
13923 65 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 63 e index to the c
13924 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 20 ell in the page
13925 68 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a 2a header). Such.*
13926 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 * small cells wi
13927 6c 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 20 ll be rare, but
13928 74 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c they are possibl
13929 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 e..*/.#define MX
1392a 5f 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74 _CELL(pBt) ((pBt
1392b 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 ->pageSize-8)/6)
1392c 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 ../* Forward dec
1392d 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 larations */.typ
1392e 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50 edef struct MemP
1392f 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 age MemPage;.typ
13930 65 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f edef struct BtLo
13931 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a ck BtLock;../*.*
13932 2a 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69 * This is a magi
13933 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 c string that ap
13934 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 pears at the beg
13935 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a inning of every.
13936 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 ** SQLite databa
13937 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 se in order to i
13938 64 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c 65 dentify the file
13939 20 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 62 as a real datab
1393a 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 ase..**.** You c
1393b 61 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 76 an change this v
1393c 61 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d alue at compile-
1393d 74 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 69 time by specifyi
1393e 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 ng a.** -DSQLITE
1393f 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e _FILE_HEADER="..
13940 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c ." on the compil
13941 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e er command-line.
13942 20 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 The.** header
13943 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 must be exactly
13944 31 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 69 16 bytes includi
13945 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d ng the zero-term
13946 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 inator so.** the
13947 20 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 73 string itself s
13948 68 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 72 hould be 15 char
13949 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 acters long. If
1394a 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 you change.** t
1394b 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e 20 he header, then
1394c 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 your custom libr
1394d 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 ary will not be
1394e 61 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a able to read .**
1394f 20 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 72 databases gener
13950 61 74 65 64 20 62 79 20 74 68 65 20 73 74 61 6e ated by the stan
13951 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 dard tools and t
13952 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c he standard tool
13953 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 s.** will not be
13954 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 61 able to read da
13955 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20 tabases created
13956 62 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c by your custom l
13957 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 ibrary..*/.#ifnd
13958 65 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 ef SQLITE_FILE_H
13959 45 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 37 EADER /* 1234567
1395a 38 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 89 123456 */.#
1395b 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 define SQLITE_FI
1395c 4c 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 74 LE_HEADER "SQLit
1395d 65 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 e format 3".#end
1395e 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 if../*.** Page t
1395f 79 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f ype flags. An O
13960 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 Red combination
13961 6f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61 of these flags a
13962 70 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 ppear as the.**
13963 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f 6e first byte of on
13964 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 -disk image of e
13965 76 65 72 79 20 42 54 72 65 65 20 70 61 67 65 2e very BTree page.
13966 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 5f .*/.#define PTF_
13967 49 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a 23 INTKEY 0x01.#
13968 64 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 define PTF_ZEROD
13969 41 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 6e ATA 0x02.#defin
1396a 65 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 20 e PTF_LEAFDATA
1396b 30 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 46 0x04.#define PTF
1396c 5f 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 0a _LEAF 0x08.
1396d 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 ./*.** As each p
1396e 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 age of the file
1396f 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d is loaded into m
13970 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e emory, an instan
13971 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
13972 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ing.** structure
13973 20 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e 64 is appended and
13974 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 initialized to
13975 7a 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 75 zero. This stru
13976 63 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 cture stores.**
13977 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
13978 74 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 t the page that
13979 69 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 is decoded from
1397a 74 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 67 the raw file pag
1397b 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 e..**.** The pPa
1397c 72 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 rent field point
1397d 73 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 61 s back to the pa
1397e 72 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 73 rent page. This
1397f 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a allows us to.**
13980 20 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72 walk up the BTr
13981 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 ee from any leaf
13982 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 to the root. C
13983 61 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65 are must be take
13984 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 n to.** unref()
13985 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
13986 70 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69 pointer when thi
13987 73 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e s page is no lon
13988 67 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0a ger referenced..
13989 2a 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 72 ** The pageDestr
1398a 75 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 uctor() routine
1398b 68 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f handles that cho
1398c 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 re..**.** Access
1398d 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f to all fields o
1398e 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
1398f 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 is controlled b
13990 79 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 y the mutex.** s
13991 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 tored in MemPage
13992 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a .pBt->mutex..*/.
13993 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 7b struct MemPage {
13994 0a 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 20 . u8 isInit;
13995 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
13996 69 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e if previously in
13997 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 itialized. MUST
13998 42 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 BE FIRST! */. u
13999 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 8 nOverflow;
1399a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1399b 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 overflow cell b
1399c 6f 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d odies in aCell[]
1399d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b */. u8 intKey;
1399e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
1399f 75 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 ue if intkey fla
139a0 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 g is set */. u8
139a1 20 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 leaf;
139a2 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 /* True if le
139a3 61 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a af flag is set *
139a4 2f 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b 20 /. u8 hasData;
139a5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
139a6 20 69 66 20 74 68 69 73 20 70 61 67 65 20 73 74 if this page st
139a7 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 ores data */. u
139a8 38 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 8 hdrOffset;
139a9 20 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 /* 100 for p
139aa 61 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72 77 age 1. 0 otherw
139ab 69 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c ise */. u8 chil
139ac 64 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a dPtrSize; /*
139ad 20 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 0 if leaf==1.
139ae 34 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 4 if leaf==0 */.
139af 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 u16 maxLocal;
139b0 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
139b1 66 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f f BtShared.maxLo
139b2 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e cal or BtShared.
139b3 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 maxLeaf */. u16
139b4 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 minLocal;
139b5 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 /* Copy of BtS
139b6 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f hared.minLocal o
139b7 72 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 r BtShared.minLe
139b8 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c af */. u16 cell
139b9 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 Offset; /*
139ba 49 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 6f Index in aData o
139bb 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 f first cell poi
139bc 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 nter */. u16 nF
139bd 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f ree; /
139be 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 * Number of free
139bf 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 bytes on the pa
139c0 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c ge */. u16 nCel
139c1 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 l; /*
139c2 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 Number of cells
139c3 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c 6f on this page, lo
139c4 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a cal and ovfl */.
139c5 20 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 u16 maskPage;
139c6 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 /* Mask f
139c7 6f 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 2a or page offset *
139c8 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c /. struct _Ovfl
139c9 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c Cell { /* Cell
139ca 73 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 s that will not
139cb 66 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a fit on aData[] *
139cc 2f 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b /. u8 *pCell;
139cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
139ce 6e 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f 64 nters to the bod
139cf 79 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f y of the overflo
139d0 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 w cell */. u1
139d1 36 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 6 idx;
139d2 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 73 /* Insert this
139d3 20 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 cell before idx
139d4 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 -th non-overflow
139d5 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 cell */. } aOv
139d6 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 fl[5];. BtShare
139d7 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f 2a d *pBt; /*
139d8 20 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 68 Pointer to BtSh
139d9 61 72 65 64 20 74 68 61 74 20 74 68 69 73 20 70 ared that this p
139da 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a age is part of *
139db 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 /. u8 *aData;
139dc 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
139dd 74 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 ter to disk imag
139de 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 e of the page da
139df 74 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a ta */. DbPage *
139e0 70 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 pDbPage; /*
139e1 50 61 67 65 72 20 70 61 67 65 20 68 61 6e 64 6c Pager page handl
139e2 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
139e3 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 ; /* P
139e4 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 age number for t
139e5 68 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a his page */.};..
139e6 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d /*.** The in-mem
139e7 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 64 ory image of a d
139e8 69 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 65 isk page has the
139e9 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 auxiliary infor
139ea 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a mation appended.
139eb 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 ** to the end.
139ec 45 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 68 EXTRA_SIZE is th
139ed 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
139ee 73 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 s of space neede
139ef 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 d to hold.** tha
139f0 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 t extra informat
139f1 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ion..*/.#define
139f2 45 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f EXTRA_SIZE sizeo
139f3 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 20 41 f(MemPage)../* A
139f4 20 42 74 72 65 65 20 68 61 6e 64 6c 65 0a 2a 2a Btree handle.**
139f5 0a 2a 2a 20 41 20 64 61 74 61 62 61 73 65 20 63 .** A database c
139f6 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69 onnection contai
139f7 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
139f8 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a an instance of.*
139f9 2a 20 74 68 69 73 20 6f 62 6a 65 63 74 20 66 6f * this object fo
139fa 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 r every database
139fb 20 66 69 6c 65 20 74 68 61 74 20 69 74 20 68 61 file that it ha
139fc 73 20 6f 70 65 6e 2e 20 20 54 68 69 73 20 73 74 s open. This st
139fd 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6f 70 ructure.** is op
139fe 61 71 75 65 20 74 6f 20 74 68 65 20 64 61 74 61 aque to the data
139ff 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
13a00 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 The database c
13a01 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 onnection cannot
13a02 0a 2a 2a 20 73 65 65 20 74 68 65 20 69 6e 74 65 .** see the inte
13a03 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 rnals of this st
13a04 72 75 63 74 75 72 65 20 61 6e 64 20 6f 6e 6c 79 ructure and only
13a05 20 64 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e deals with poin
13a06 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 ters to.** this
13a07 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
13a08 20 46 6f 72 20 73 6f 6d 65 20 64 61 74 61 62 61 For some databa
13a09 73 65 20 66 69 6c 65 73 2c 20 74 68 65 20 73 61 se files, the sa
13a0a 6d 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 me underlying da
13a0b 74 61 62 61 73 65 20 63 61 63 68 65 20 6d 69 67 tabase cache mig
13a0c 68 74 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 ht be .** shared
13a0d 20 62 65 74 77 65 65 6e 20 6d 75 6c 74 69 70 6c between multipl
13a0e 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 e connections.
13a0f 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 65 61 In that case, ea
13a10 63 68 20 63 6f 6e 74 65 63 74 69 6f 6e 0a 2a 2a ch contection.**
13a11 20 68 61 73 20 69 74 20 6f 77 6e 20 70 6f 69 6e has it own poin
13a12 74 65 72 20 74 6f 20 74 68 69 73 20 6f 62 6a 65 ter to this obje
13a13 63 74 2e 20 20 42 75 74 20 65 61 63 68 20 69 6e ct. But each in
13a14 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
13a15 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 bject.** points
13a16 74 6f 20 74 68 65 20 73 61 6d 65 20 42 74 53 68 to the same BtSh
13a17 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 ared object. Th
13a18 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 e database cache
13a19 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 63 68 65 and the.** sche
13a1a 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ma associated wi
13a1b 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 th the database
13a1c 66 69 6c 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e file are all con
13a1d 74 61 69 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a tained within.**
13a1e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 the BtShared ob
13a1f 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 ject..**.** All
13a20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 fields in this s
13a21 74 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 tructure are acc
13a22 65 73 73 65 64 20 75 6e 64 65 72 20 73 71 6c 69 essed under sqli
13a23 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 te3.mutex..** Th
13a24 65 20 70 42 74 20 70 6f 69 6e 74 65 72 20 69 74 e pBt pointer it
13a25 73 65 6c 66 20 6d 61 79 20 6e 6f 74 20 62 65 20 self may not be
13a26 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74 68 changed while th
13a27 65 72 65 20 65 78 69 73 74 73 20 63 75 72 73 6f ere exists curso
13a28 72 73 20 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 rs .** in the re
13a29 66 65 72 65 6e 63 65 64 20 42 74 53 68 61 72 65 ferenced BtShare
13a2a 64 20 74 68 61 74 20 70 6f 69 6e 74 20 62 61 63 d that point bac
13a2b 6b 20 74 6f 20 74 68 69 73 20 42 74 72 65 65 20 k to this Btree
13a2c 73 69 6e 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63 since those.** c
13a2d 75 72 73 6f 72 73 20 68 61 76 65 20 74 6f 20 64 ursors have to d
13a2e 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69 o go through thi
13a2f 73 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 s Btree to find
13a30 74 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61 their BtShared a
13a31 6e 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e nd.** they often
13a32 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68 do so without h
13a33 6f 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d olding sqlite3.m
13a34 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 utex..*/.struct
13a35 42 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 Btree {. sqlite
13a36 33 20 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 3 *db; /*
13a37 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
13a38 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 nection holding
13a39 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 this btree */.
13a3a 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
13a3b 20 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 /* Sharable c
13a3c 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62 ontent of this b
13a3d 74 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 tree */. u8 inT
13a3e 72 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 rans; /*
13a3f 54 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e TRANS_NONE, TRAN
13a40 53 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f S_READ or TRANS_
13a41 57 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 WRITE */. u8 sh
13a42 61 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a arable; /*
13a43 20 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20 True if we can
13a44 73 68 61 72 65 20 70 42 74 20 77 69 74 68 20 61 share pBt with a
13a45 6e 6f 74 68 65 72 20 64 62 20 2a 2f 0a 20 20 75 nother db */. u
13a46 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 8 locked;
13a47 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20 /* True if db
13a48 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42 currently has pB
13a49 74 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e t locked */. in
13a4a 74 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 t wantToLock;
13a4b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 /* Number of ne
13a4c 73 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 sted calls to sq
13a4d 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
13a4e 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b ) */. int nBack
13a4f 75 70 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d up; /* Num
13a50 62 65 72 20 6f 66 20 62 61 63 6b 75 70 20 6f 70 ber of backup op
13a51 65 72 61 74 69 6f 6e 73 20 72 65 61 64 69 6e 67 erations reading
13a52 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 this btree */.
13a53 20 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 Btree *pNext;
13a54 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f /* List of o
13a55 74 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74 ther sharable Bt
13a56 72 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 rees from the sa
13a57 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65 me db */. Btree
13a58 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a *pPrev; /*
13a59 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66 Back pointer of
13a5a 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a the same list *
13a5b 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65 /.};../*.** Btre
13a5c 65 2e 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 61 e.inTrans may ta
13a5d 6b 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f ke one of the fo
13a5e 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a llowing values..
13a5f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61 **.** If the sha
13a60 72 65 64 2d 64 61 74 61 20 65 78 74 65 6e 73 69 red-data extensi
13a61 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 on is enabled, t
13a62 68 65 72 65 20 6d 61 79 20 62 65 20 6d 75 6c 74 here may be mult
13a63 69 70 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66 iple users.** of
13a64 20 74 68 65 20 42 74 72 65 65 20 73 74 72 75 63 the Btree struc
13a65 74 75 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e ture. At most on
13a66 65 20 6f 66 20 74 68 65 73 65 20 6d 61 79 20 6f e of these may o
13a67 70 65 6e 20 61 20 77 72 69 74 65 20 74 72 61 6e pen a write tran
13a68 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 saction,.** but
13a69 61 6e 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 68 any number may h
13a6a 61 76 65 20 61 63 74 69 76 65 20 72 65 61 64 20 ave active read
13a6b 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f transactions..*/
13a6c 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e .#define TRANS_N
13a6d 4f 4e 45 20 20 30 0a 23 64 65 66 69 6e 65 20 54 ONE 0.#define T
13a6e 52 41 4e 53 5f 52 45 41 44 20 20 31 0a 23 64 65 RANS_READ 1.#de
13a6f 66 69 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 45 fine TRANS_WRITE
13a70 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 2../*.** An ins
13a71 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
13a72 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 ject represents
13a73 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 a single databas
13a74 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 e file..** .** A
13a75 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
13a76 20 66 69 6c 65 20 63 61 6e 20 62 65 20 69 6e 20 file can be in
13a77 75 73 65 20 61 73 20 74 68 65 20 73 61 6d 65 20 use as the same
13a78 74 69 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f time by two.** o
13a79 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 r more database
13a7a 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68 connections. Wh
13a7b 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 en two or more c
13a7c 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a onnections are.*
13a7d 2a 20 73 68 61 72 69 6e 67 20 74 68 65 20 73 61 * sharing the sa
13a7e 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 me database file
13a7f 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f , each connectio
13a80 6e 20 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20 n has it own.**
13a81 70 72 69 76 61 74 65 20 42 74 72 65 65 20 6f 62 private Btree ob
13a82 6a 65 63 74 20 66 6f 72 20 74 68 65 20 66 69 6c ject for the fil
13a83 65 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 e and each of th
13a84 6f 73 65 20 42 74 72 65 65 73 20 70 6f 69 6e 74 ose Btrees point
13a85 73 0a 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65 s.** to this one
13a86 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
13a87 2e 20 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 . BtShared.nRef
13a88 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
13a89 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 f.** connections
13a8a 20 63 75 72 72 65 6e 74 6c 79 20 73 68 61 72 69 currently shari
13a8b 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73 65 ng this database
13a8c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 file..**.** Fie
13a8d 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 lds in this stru
13a8e 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73 cture are access
13a8f 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 ed under the BtS
13a90 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d hared.mutex.** m
13a91 75 74 65 78 2c 20 65 78 63 65 70 74 20 66 6f 72 utex, except for
13a92 20 6e 52 65 66 20 61 6e 64 20 70 4e 65 78 74 20 nRef and pNext
13a93 77 68 69 63 68 20 61 72 65 20 61 63 63 65 73 73 which are access
13a94 65 64 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20 ed under the.**
13a95 67 6c 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55 global SQLITE_MU
13a96 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
13a97 52 20 6d 75 74 65 78 2e 20 20 54 68 65 20 70 50 R mutex. The pP
13a98 61 67 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61 ager field.** ma
13a99 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 y not be modifie
13a9a 64 20 6f 6e 63 65 20 69 74 20 69 73 20 69 6e 69 d once it is ini
13a9b 74 69 61 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f tially set as lo
13a9c 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a ng as nRef>0..**
13a9d 20 54 68 65 20 70 53 63 68 65 6d 61 20 66 69 65 The pSchema fie
13a9e 6c 64 20 6d 61 79 20 62 65 20 73 65 74 20 6f 6e ld may be set on
13a9f 63 65 20 75 6e 64 65 72 20 42 74 53 68 61 72 65 ce under BtShare
13aa0 64 2e 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74 d.mutex and.** t
13aa1 68 65 72 65 61 66 74 65 72 20 69 73 20 75 6e 63 hereafter is unc
13aa2 68 61 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 61 hanged as long a
13aa3 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20 s nRef>0..**.**
13aa4 69 73 50 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a isPending:.**.**
13aa5 20 20 20 49 66 20 61 20 42 74 53 68 61 72 65 64 If a BtShared
13aa6 20 63 6c 69 65 6e 74 20 66 61 69 6c 73 20 74 6f client fails to
13aa7 20 6f 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d obtain a write-
13aa8 6c 6f 63 6b 20 6f 6e 20 61 20 64 61 74 61 62 61 lock on a databa
13aa9 73 65 0a 2a 2a 20 20 20 74 61 62 6c 65 20 28 62 se.** table (b
13aaa 65 63 61 75 73 65 20 74 68 65 72 65 20 65 78 69 ecause there exi
13aab 73 74 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 sts one or more
13aac 72 65 61 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74 68 read-locks on th
13aad 65 20 74 61 62 6c 65 29 2c 0a 2a 2a 20 20 20 74 e table),.** t
13aae 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 he shared-cache
13aaf 65 6e 74 65 72 73 20 27 70 65 6e 64 69 6e 67 2d enters 'pending-
13ab0 6c 6f 63 6b 27 20 73 74 61 74 65 20 61 6e 64 20 lock' state and
13ab1 69 73 50 65 6e 64 69 6e 67 20 69 73 0a 2a 2a 20 isPending is.**
13ab2 20 20 73 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a set to true..*
13ab3 2a 0a 2a 2a 20 20 20 54 68 65 20 73 68 61 72 65 *.** The share
13ab4 64 2d 63 61 63 68 65 20 6c 65 61 76 65 73 20 74 d-cache leaves t
13ab5 68 65 20 27 70 65 6e 64 69 6e 67 20 6c 6f 63 6b he 'pending lock
13ab6 27 20 73 74 61 74 65 20 77 68 65 6e 20 65 69 74 ' state when eit
13ab7 68 65 72 20 6f 66 0a 2a 2a 20 20 20 74 68 65 20 her of.** the
13ab8 66 6f 6c 6c 6f 77 69 6e 67 20 6f 63 63 75 72 3a following occur:
13ab9 0a 2a 2a 0a 2a 2a 20 20 20 20 20 31 29 20 54 68 .**.** 1) Th
13aba 65 20 63 75 72 72 65 6e 74 20 77 72 69 74 65 72 e current writer
13abb 20 28 42 74 53 68 61 72 65 64 2e 70 57 72 69 74 (BtShared.pWrit
13abc 65 72 29 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 er) concludes it
13abd 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 4f s transaction, O
13abe 52 0a 2a 2a 20 20 20 20 20 32 29 20 54 68 65 20 R.** 2) The
13abf 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 number of locks
13ac0 68 65 6c 64 20 62 79 20 6f 74 68 65 72 20 63 6f held by other co
13ac1 6e 6e 65 63 74 69 6f 6e 73 20 64 72 6f 70 73 20 nnections drops
13ac2 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 to zero..**.**
13ac3 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 27 70 while in the 'p
13ac4 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 ending-lock' sta
13ac5 74 65 2c 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f te, no connectio
13ac6 6e 20 6d 61 79 20 73 74 61 72 74 20 61 20 6e 65 n may start a ne
13ac7 77 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69 w.** transacti
13ac8 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 on..**.** This
13ac9 20 66 65 61 74 75 72 65 20 69 73 20 69 6e 63 6c feature is incl
13aca 75 64 65 64 20 74 6f 20 68 65 6c 70 20 70 72 65 uded to help pre
13acb 76 65 6e 74 20 77 72 69 74 65 72 2d 73 74 61 72 vent writer-star
13acc 76 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 vation..*/.struc
13acd 74 20 42 74 53 68 61 72 65 64 20 7b 0a 20 20 50 t BtShared {. P
13ace 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 ager *pPager;
13acf 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 /* The page
13ad0 20 63 61 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69 cache */. sqli
13ad1 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
13ad2 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f /* Database co
13ad3 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 nnection current
13ad4 6c 79 20 75 73 69 6e 67 20 74 68 69 73 20 42 74 ly using this Bt
13ad5 72 65 65 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f ree */. BtCurso
13ad6 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f r *pCursor; /
13ad7 2a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 * A list of all
13ad8 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a open cursors */.
13ad9 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
13ada 31 3b 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 1; /* First
13adb 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 page of the dat
13adc 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 72 65 abase */. u8 re
13add 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 adOnly;
13ade 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
13adf 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 underlying file
13ae0 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 20 is readonly */.
13ae1 20 75 38 20 70 61 67 65 53 69 7a 65 46 69 78 65 u8 pageSizeFixe
13ae2 64 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 d; /* True i
13ae3 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 f the page size
13ae4 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 can no longer be
13ae5 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 69 66 6e changed */.#ifn
13ae6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
13ae7 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 75 38 20 AUTOVACUUM. u8
13ae8 61 75 74 6f 56 61 63 75 75 6d 3b 20 20 20 20 20 autoVacuum;
13ae9 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 75 /* True if au
13aea 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 to-vacuum is ena
13aeb 62 6c 65 64 20 2a 2f 0a 20 20 75 38 20 69 6e 63 bled */. u8 inc
13aec 72 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20 rVacuum;
13aed 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 63 72 2d /* True if incr-
13aee 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 vacuum is enable
13aef 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 d */.#endif. u1
13af0 36 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 6 pageSize;
13af1 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
13af2 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 ber of bytes on
13af3 61 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 a page */. u16
13af4 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 usableSize;
13af5 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 /* Number of u
13af6 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 65 sable bytes on e
13af7 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 ach page */. u1
13af8 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 6 maxLocal;
13af9 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c /* Maximum l
13afa 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 ocal payload in
13afb 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 non-LEAFDATA tab
13afc 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e les */. u16 min
13afd 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f Local; /
13afe 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 * Minimum local
13aff 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c payload in non-L
13b00 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a EAFDATA tables *
13b01 2f 0a 20 20 75 31 36 20 6d 61 78 4c 65 61 66 3b /. u16 maxLeaf;
13b02 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
13b03 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f imum local paylo
13b04 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 ad in a LEAFDATA
13b05 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20 table */. u16
13b06 6d 69 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20 minLeaf;
13b07 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 /* Minimum loc
13b08 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 al payload in a
13b09 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a LEAFDATA table *
13b0a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 61 63 /. u8 inTransac
13b0b 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 tion; /* Tra
13b0c 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 2a nsaction state *
13b0d 2f 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 /. int nTransac
13b0e 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d tion; /* Num
13b0f 62 65 72 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e ber of open tran
13b10 73 61 63 74 69 6f 6e 73 20 28 72 65 61 64 20 2b sactions (read +
13b11 20 77 72 69 74 65 29 20 2a 2f 0a 20 20 76 6f 69 write) */. voi
13b12 64 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 d *pSchema;
13b13 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
13b14 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 space allocated
13b15 20 62 79 20 73 71 6c 69 74 65 33 42 74 72 65 65 by sqlite3Btree
13b16 53 63 68 65 6d 61 28 29 20 2a 2f 0a 20 20 76 6f Schema() */. vo
13b17 69 64 20 28 2a 78 46 72 65 65 53 63 68 65 6d 61 id (*xFreeSchema
13b18 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 )(void*); /* De
13b19 73 74 72 75 63 74 6f 72 20 66 6f 72 20 42 74 53 structor for BtS
13b1a 68 61 72 65 64 2e 70 53 63 68 65 6d 61 20 2a 2f hared.pSchema */
13b1b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
13b1c 20 2a 6d 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e 2d *mutex; /* Non-
13b1d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 recursive mutex
13b1e 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 63 65 required to acce
13b1f 73 73 20 74 68 69 73 20 73 74 72 75 63 74 20 2a ss this struct *
13b20 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 48 61 73 /. Bitvec *pHas
13b21 43 6f 6e 74 65 6e 74 3b 20 20 2f 2a 20 53 65 74 Content; /* Set
13b22 20 6f 66 20 70 61 67 65 73 20 6d 6f 76 65 64 20 of pages moved
13b23 74 6f 20 66 72 65 65 2d 6c 69 73 74 20 74 68 69 to free-list thi
13b24 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f s transaction */
13b25 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
13b26 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
13b27 45 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 E. int nRef;
13b28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
13b29 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
13b2a 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 s to this struct
13b2b 75 72 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 ure */. BtShare
13b2c 64 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f d *pNext; /
13b2d 2a 20 4e 65 78 74 20 6f 6e 20 61 20 6c 69 73 74 * Next on a list
13b2e 20 6f 66 20 73 68 61 72 61 62 6c 65 20 42 74 53 of sharable BtS
13b2f 68 61 72 65 64 20 73 74 72 75 63 74 73 20 2a 2f hared structs */
13b30 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b . BtLock *pLock
13b31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 ; /* List
13b32 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f of locks held o
13b33 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74 n this shared-bt
13b34 72 65 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 ree struct */.
13b35 42 74 72 65 65 20 2a 70 57 72 69 74 65 72 3b 20 Btree *pWriter;
13b36 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 77 /* Btree w
13b37 69 74 68 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 ith currently op
13b38 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 en write transac
13b39 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 69 73 45 tion */. u8 isE
13b3a 78 63 6c 75 73 69 76 65 3b 20 20 20 20 20 20 20 xclusive;
13b3b 2f 2a 20 54 72 75 65 20 69 66 20 70 57 72 69 74 /* True if pWrit
13b3c 65 72 20 68 61 73 20 61 6e 20 45 58 43 4c 55 53 er has an EXCLUS
13b3d 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 IVE lock on the
13b3e 64 62 20 2a 2f 0a 20 20 75 38 20 69 73 50 65 6e db */. u8 isPen
13b3f 64 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a ding; /*
13b40 20 49 66 20 77 61 69 74 69 6e 67 20 66 6f 72 20 If waiting for
13b41 72 65 61 64 2d 6c 6f 63 6b 73 20 74 6f 20 63 6c read-locks to cl
13b42 65 61 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ear */.#endif.
13b43 75 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 u8 *pTmpSpace;
13b44 20 20 20 20 20 20 2f 2a 20 42 74 53 68 61 72 65 /* BtShare
13b45 64 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 d.pageSize bytes
13b46 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d of space for tm
13b47 70 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a p use */.};../*.
13b48 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
13b49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
13b4a 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 structure is use
13b4b 64 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d d to hold inform
13b4c 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61 ation.** about a
13b4d 20 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72 73 cell. The pars
13b4e 65 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63 74 eCellPtr() funct
13b4f 69 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68 69 ion fills in thi
13b50 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 s structure.** b
13b51 61 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74 ased on informat
13b52 69 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f 6d ion extract from
13b53 20 74 68 65 20 72 61 77 20 64 69 73 6b 20 70 61 the raw disk pa
13b54 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ge..*/.typedef s
13b55 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43 truct CellInfo C
13b56 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 ellInfo;.struct
13b57 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 75 38 20 CellInfo {. u8
13b58 2a 70 43 65 6c 6c 3b 20 20 20 20 20 2f 2a 20 50 *pCell; /* P
13b59 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 74 ointer to the st
13b5a 61 72 74 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 art of cell cont
13b5b 65 6e 74 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 ent */. i64 nKe
13b5c 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b y; /* The k
13b5d 65 79 20 66 6f 72 20 49 4e 54 4b 45 59 20 74 61 ey for INTKEY ta
13b5e 62 6c 65 73 2c 20 6f 72 20 6e 75 6d 62 65 72 20 bles, or number
13b5f 6f 66 20 62 79 74 65 73 20 69 6e 20 6b 65 79 20 of bytes in key
13b60 2a 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b 20 */. u32 nData;
13b61 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
13b62 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 2a bytes of data *
13b63 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 /. u32 nPayload
13b64 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75 ; /* Total amou
13b65 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f nt of payload */
13b66 0a 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b 20 . u16 nHeader;
13b67 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
13b68 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 cell content he
13b69 61 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f ader in bytes */
13b6a 0a 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20 . u16 nLocal;
13b6b 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70 /* Amount of p
13b6c 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 ayload held loca
13b6d 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76 lly */. u16 iOv
13b6e 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65 erflow; /* Offse
13b6f 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 t to overflow pa
13b70 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f ge number. Zero
13b71 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 if no overflow
13b72 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b 20 */. u16 nSize;
13b73 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
13b74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
13b75 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 on the main b-tr
13b76 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f ee page */.};../
13b77 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 *.** Maximum dep
13b78 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 th of an SQLite
13b79 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65 B-Tree structure
13b7a 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65 65 . Any B-Tree dee
13b7b 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69 73 per than.** this
13b7c 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72 65 will be declare
13b7d 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 20 d corrupt. This
13b7e 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c 61 value is calcula
13b7f 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a 2a ted based on a.*
13b80 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 * maximum databa
13b81 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31 20 se size of 2^31
13b82 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 pages a minimum
13b83 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72 20 fanout of 2 for
13b84 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 61 a.** root-node a
13b85 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74 68 nd 3 for all oth
13b86 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 er internal node
13b87 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 s..**.** If a tr
13b88 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ee that appears
13b89 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68 61 to be taller tha
13b8a 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75 6e n this is encoun
13b8b 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a 20 tered, it is.**
13b8c 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
13b8d 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 database is cor
13b8e 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rupt..*/.#define
13b8f 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 BTCURSOR_MAX_DE
13b90 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 20 PTH 20../*.** A
13b91 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e cursor is a poin
13b92 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 75 ter to a particu
13b93 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 6e lar entry within
13b94 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
13b95 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20 61 b-tree within a
13b96 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
13b97 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 20 **.** The entry
13b98 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 is identified by
13b99 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e 64 its MemPage and
13b9a 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a the index in.**
13b9b 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d MemPage.aCell[]
13b9c 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a of the entry..*
13b9d 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 69 6e 67 *.** When a sing
13b9e 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 le database file
13b9f 20 63 61 6e 20 73 68 61 72 65 64 20 62 79 20 74 can shared by t
13ba0 77 6f 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 wo more database
13ba1 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a connections,.**
13ba2 20 62 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e but cursors can
13ba3 6e 6f 74 20 62 65 20 73 68 61 72 65 64 2e 20 20 not be shared.
13ba4 45 61 63 68 20 63 75 72 73 6f 72 20 69 73 20 61 Each cursor is a
13ba5 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
13ba6 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64 .** particular d
13ba7 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
13ba8 6f 6e 20 69 64 65 6e 74 69 66 69 65 64 20 42 74 on identified Bt
13ba9 43 75 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62 Cursor.pBtree.db
13baa 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 ..**.** Fields i
13bab 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
13bac 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e are accessed un
13bad 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 der the BtShared
13bae 2e 6d 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 .mutex.** found
13baf 61 74 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 at self->pBt->mu
13bb0 74 65 78 2e 20 0a 2a 2f 0a 73 74 72 75 63 74 20 tex. .*/.struct
13bb1 42 74 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72 BtCursor {. Btr
13bb2 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 ee *pBtree;
13bb3 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 /* The Bt
13bb4 72 65 65 20 74 6f 20 77 68 69 63 68 20 74 68 69 ree to which thi
13bb5 73 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 s cursor belongs
13bb6 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a */. BtShared *
13bb7 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 pBt;
13bb8 2f 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 20 /* The BtShared
13bb9 74 68 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e this cursor poin
13bba 74 73 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72 ts to */. BtCur
13bbb 73 6f 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 sor *pNext, *pPr
13bbc 65 76 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20 ev; /* Forms a
13bbd 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 linked list of a
13bbe 6c 6c 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 ll cursors */.
13bbf 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a struct KeyInfo *
13bc0 70 4b 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 pKeyInfo; /* Arg
13bc1 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 ument passed to
13bc2 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 comparison funct
13bc3 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ion */. Pgno pg
13bc4 6e 6f 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20 noRoot;
13bc5 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 /* The root p
13bc6 61 67 65 20 6f 66 20 74 68 69 73 20 74 72 65 65 age of this tree
13bc7 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e */. sqlite3_in
13bc8 74 36 34 20 63 61 63 68 65 64 52 6f 77 69 64 3b t64 cachedRowid;
13bc9 20 2f 2a 20 4e 65 78 74 20 72 6f 77 69 64 20 63 /* Next rowid c
13bca 61 63 68 65 2e 20 20 30 20 6d 65 61 6e 73 20 6e ache. 0 means n
13bcb 6f 74 20 76 61 6c 69 64 20 2a 2f 0a 20 20 43 65 ot valid */. Ce
13bcc 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 20 20 llInfo info;
13bcd 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 /* A par
13bce 73 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 77 se of the cell w
13bcf 65 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 e are pointing a
13bd0 74 20 2a 2f 0a 20 20 75 38 20 77 72 46 6c 61 67 t */. u8 wrFlag
13bd1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
13bd2 20 2f 2a 20 54 72 75 65 20 69 66 20 77 72 69 74 /* True if writ
13bd3 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 61 74 4c able */. u8 atL
13bd4 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ast;
13bd5 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f /* Cursor po
13bd6 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 inting to the la
13bd7 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 38 st entry */. u8
13bd8 20 76 61 6c 69 64 4e 4b 65 79 3b 20 20 20 20 20 validNKey;
13bd9 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
13bda 69 66 20 69 6e 66 6f 2e 6e 4b 65 79 20 69 73 20 if info.nKey is
13bdb 76 61 6c 69 64 20 2a 2f 0a 20 20 75 38 20 65 53 valid */. u8 eS
13bdc 74 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 tate;
13bdd 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 /* One of t
13bde 68 65 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f he CURSOR_XXX co
13bdf 6e 73 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c nstants (see bel
13be0 6f 77 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ow) */. void *p
13be1 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 Key; /* Sav
13be2 65 64 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 ed key that was
13be3 63 75 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e cursor's last kn
13be4 6f 77 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a own position */.
13be5 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 i64 nKey;
13be6 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b /* Size of pK
13be7 65 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 65 ey, or last inte
13be8 67 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 ger key */. int
13be9 20 73 6b 69 70 3b 20 20 20 20 20 20 20 20 2f 2a skip; /*
13bea 20 28 73 6b 69 70 3c 30 29 20 2d 3e 20 50 72 65 (skip<0) -> Pre
13beb 76 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 v() is a no-op.
13bec 28 73 6b 69 70 3e 30 29 20 2d 3e 20 4e 65 78 74 (skip>0) -> Next
13bed 28 29 20 69 73 20 2a 2f 0a 23 69 66 6e 64 65 66 () is */.#ifndef
13bee 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
13bef 52 42 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e 63 RBLOB. u8 isInc
13bf0 72 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 20 rblobHandle;
13bf1 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
13bf2 73 20 63 75 72 73 6f 72 20 69 73 20 61 6e 20 69 s cursor is an i
13bf3 6e 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 2a ncr. io handle *
13bf4 2f 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66 /. Pgno *aOverf
13bf5 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a low; /*
13bf6 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c Cache of overfl
13bf7 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e ow page location
13bf8 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e s */.#endif.#ifn
13bf9 64 65 66 20 4e 44 45 42 55 47 0a 20 20 75 38 20 def NDEBUG. u8
13bfa 70 61 67 65 73 53 68 75 66 66 6c 65 64 3b 20 20 pagesShuffled;
13bfb 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
13bfc 66 20 42 74 72 65 65 20 70 61 67 65 73 20 61 72 f Btree pages ar
13bfd 65 20 72 65 61 72 72 61 6e 67 65 64 20 62 79 20 e rearranged by
13bfe 62 61 6c 61 6e 63 65 28 29 2a 2f 0a 23 65 6e 64 balance()*/.#end
13bff 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b 20 if. i16 iPage;
13c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c01 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
13c02 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 70 dex of current p
13c03 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a 2f age in apPage */
13c04 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 61 . MemPage *apPa
13c05 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f ge[BTCURSOR_MAX_
13c06 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 65 DEPTH]; /* Page
13c07 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 63 s from root to c
13c08 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 urrent page */.
13c09 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 52 u16 aiIdx[BTCUR
13c0a 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 SOR_MAX_DEPTH];
13c0b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
13c0c 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 67 t index in apPag
13c0d 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a e[i] */.};../*.*
13c0e 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75 * Potential valu
13c0f 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 2e es for BtCursor.
13c10 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 55 eState..**.** CU
13c11 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20 RSOR_VALID:.**
13c12 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 Cursor points t
13c13 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e o a valid entry.
13c14 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65 74 getPayload() et
13c15 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 c. may be called
13c16 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 ..**.** CURSOR_I
13c17 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 NVALID:.** Cur
13c18 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f 69 sor does not poi
13c19 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e nt to a valid en
13c1a 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68 61 try. This can ha
13c1b 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 6c ppen (for exampl
13c1c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 65 e) .** because
13c1d 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
13c1e 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 20 42 pty or because B
13c1f 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 74 28 treeCursorFirst(
13c20 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a ) has not been.*
13c21 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a * called..**.*
13c22 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 * CURSOR_REQUIRE
13c23 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 74 SEEK:.** The t
13c24 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 63 able that this c
13c25 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 ursor was opened
13c26 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 73 on still exists
13c27 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20 0a , but has been .
13c28 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73 69 ** modified si
13c29 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 77 nce the cursor w
13c2a 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54 68 as last used. Th
13c2b 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f e cursor positio
13c2c 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 20 n is saved.**
13c2d 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 in variables BtC
13c2e 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 42 ursor.pKey and B
13c2f 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 68 tCursor.nKey. Wh
13c30 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20 69 en a cursor is i
13c31 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 61 n .** this sta
13c32 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73 6f te, restoreCurso
13c33 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 rPosition() can
13c34 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 be called to att
13c35 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65 empt to.** see
13c36 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 k the cursor to
13c37 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 69 the saved positi
13c38 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 on..**.** CURSOR
13c39 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75 _FAULT:.** A u
13c3a 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72 72 nrecoverable err
13c3b 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72 or (an I/O error
13c3c 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 or a malloc fai
13c3d 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72 72 lure) has occurr
13c3e 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66 ed.** on a dif
13c3f 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f ferent connectio
13c40 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68 n that shares th
13c41 65 20 42 74 53 68 61 72 65 64 20 63 61 63 68 65 e BtShared cache
13c42 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 20 with this.**
13c43 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72 72 cursor. The err
13c44 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 65 20 or has left the
13c45 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f cache in an inco
13c46 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a nsistent state..
13c47 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20 ** Do nothing
13c48 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 20 63 else with this c
13c49 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 65 ursor. Any atte
13c4a 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 63 mpt to use the c
13c4b 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c ursor.** shoul
13c4c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 d return the err
13c4d 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 or code stored i
13c4e 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a n BtCursor.skip.
13c4f 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f */.#define CURSO
13c50 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 20 R_INVALID
13c51 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 55 0.#define CU
13c52 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 20 RSOR_VALID
13c53 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
13c54 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
13c55 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65 66 EEK 2.#def
13c56 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 ine CURSOR_FAULT
13c57 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 0a 3..
13c58 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 /* .** The datab
13c59 61 73 65 20 70 61 67 65 20 74 68 65 20 50 45 4e ase page the PEN
13c5a 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 69 DING_BYTE occupi
13c5b 65 73 2e 20 54 68 69 73 20 70 61 67 65 20 69 73 es. This page is
13c5c 20 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f 0a never used..*/.
13c5d 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 # define PENDING
13c5e 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
13c5f 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 42 PAGER_MJ_PGNO(pB
13c60 74 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b t)../*.** A link
13c61 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66 ed list of the f
13c62 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
13c63 72 65 73 20 69 73 20 73 74 6f 72 65 64 20 61 74 res is stored at
13c64 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e BtShared.pLock.
13c65 0a 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 61 64 .** Locks are ad
13c66 64 65 64 20 28 6f 72 20 75 70 67 72 61 64 65 64 ded (or upgraded
13c67 20 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 from READ_LOCK
13c68 74 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77 to WRITE_LOCK) w
13c69 68 65 6e 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a hen a cursor .**
13c6a 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 is opened on th
13c6b 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f e table with roo
13c6c 74 20 70 61 67 65 20 42 74 53 68 61 72 65 64 2e t page BtShared.
13c6d 69 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 iTable. Locks ar
13c6e 65 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f e removed.** fro
13c6f 6d 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e m this list when
13c70 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
13c71 73 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 s committed or r
13c72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 20 77 olled back, or w
13c73 68 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 20 68 hen.** a btree h
13c74 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e andle is closed.
13c75 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c 6f 63 .*/.struct BtLoc
13c76 6b 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 k {. Btree *pBt
13c77 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 ree; /* B
13c78 74 72 65 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64 tree handle hold
13c79 69 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f ing this lock */
13c7a 0a 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20 . Pgno iTable;
13c7b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
13c7c 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 2a page of table *
13c7d 2f 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 /. u8 eLock;
13c7e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 /* REA
13c7f 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f D_LOCK or WRITE_
13c80 4c 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b LOCK */. BtLock
13c81 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 *pNext;
13c82 2f 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 68 61 /* Next in BtSha
13c83 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a red.pLock list *
13c84 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 /.};../* Candida
13c85 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 te values for Bt
13c86 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 Lock.eLock */.#d
13c87 65 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20 efine READ_LOCK
13c88 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 52 1.#define WR
13c89 49 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f ITE_LOCK 2../
13c8a 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f *.** These macro
13c8b 73 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 s define the loc
13c8c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 69 ation of the poi
13c8d 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 nter-map entry f
13c8e 6f 72 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 or a .** databas
13c8f 65 20 70 61 67 65 2e 20 54 68 65 20 66 69 72 73 e page. The firs
13c90 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 65 61 t argument to ea
13c91 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ch is the number
13c92 20 6f 66 20 75 73 61 62 6c 65 0a 2a 2a 20 62 79 of usable.** by
13c93 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 tes on each page
13c94 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
13c95 20 28 6f 66 74 65 6e 20 31 30 32 34 29 2e 20 54 (often 1024). T
13c96 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 he second is the
13c97 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 .** page number
13c98 74 6f 20 6c 6f 6f 6b 20 75 70 20 69 6e 20 74 68 to look up in th
13c99 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a e pointer map..*
13c9a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 50 41 47 45 *.** PTRMAP_PAGE
13c9b 4e 4f 20 72 65 74 75 72 6e 73 20 74 68 65 20 64 NO returns the d
13c9c 61 74 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d atabase page num
13c9d 62 65 72 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 ber of the point
13c9e 65 72 2d 6d 61 70 0a 2a 2a 20 70 61 67 65 20 74 er-map.** page t
13c9f 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 72 hat stores the r
13ca0 65 71 75 69 72 65 64 20 70 6f 69 6e 74 65 72 2e equired pointer.
13ca1 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 PTRMAP_PTROFFSE
13ca2 54 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 T returns.** the
13ca3 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 72 offset of the r
13ca4 65 71 75 65 73 74 65 64 20 6d 61 70 20 65 6e 74 equested map ent
13ca5 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ry..**.** If the
13ca6 20 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70 pgno argument p
13ca7 61 73 73 65 64 20 74 6f 20 50 54 52 4d 41 50 5f assed to PTRMAP_
13ca8 50 41 47 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e PAGENO is a poin
13ca9 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 0a 2a 2a ter-map page,.**
13caa 20 74 68 65 6e 20 70 67 6e 6f 20 69 73 20 72 65 then pgno is re
13cab 74 75 72 6e 65 64 2e 20 53 6f 20 28 70 67 6e 6f turned. So (pgno
13cac 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 ==PTRMAP_PAGENO(
13cad 70 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63 61 6e pgsz, pgno)) can
13cae 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 74 be.** used to t
13caf 65 73 74 20 69 66 20 70 67 6e 6f 20 69 73 20 61 est if pgno is a
13cb0 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
13cb1 65 2e 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 e. PTRMAP_ISPAGE
13cb2 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 implements.** t
13cb3 68 69 73 20 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 his test..*/.#de
13cb4 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 41 47 45 fine PTRMAP_PAGE
13cb5 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 29 20 70 74 NO(pBt, pgno) pt
13cb6 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20 rmapPageno(pBt,
13cb7 70 67 6e 6f 29 0a 23 64 65 66 69 6e 65 20 50 54 pgno).#define PT
13cb8 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 70 RMAP_PTROFFSET(p
13cb9 67 70 74 72 6d 61 70 2c 20 70 67 6e 6f 29 20 28 gptrmap, pgno) (
13cba 35 2a 28 70 67 6e 6f 2d 70 67 70 74 72 6d 61 70 5*(pgno-pgptrmap
13cbb 2d 31 29 29 0a 23 64 65 66 69 6e 65 20 50 54 52 -1)).#define PTR
13cbc 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
13cbd 70 67 6e 6f 29 20 28 50 54 52 4d 41 50 5f 50 41 pgno) (PTRMAP_PA
13cbe 47 45 4e 4f 28 28 70 42 74 29 2c 28 70 67 6e 6f GENO((pBt),(pgno
13cbf 29 29 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a ))==(pgno))../*.
13cc0 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d ** The pointer m
13cc1 61 70 20 69 73 20 61 20 6c 6f 6f 6b 75 70 20 74 ap is a lookup t
13cc2 61 62 6c 65 20 74 68 61 74 20 69 64 65 6e 74 69 able that identi
13cc3 66 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 fies the parent
13cc4 70 61 67 65 20 66 6f 72 0a 2a 2a 20 65 61 63 68 page for.** each
13cc5 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 20 74 child page in t
13cc6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13cc7 2e 20 20 54 68 65 20 70 61 72 65 6e 74 20 70 61 . The parent pa
13cc8 67 65 20 69 73 20 74 68 65 20 70 61 67 65 20 74 ge is the page t
13cc9 68 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 hat.** contains
13cca 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
13ccb 20 63 68 69 6c 64 2e 20 20 45 76 65 72 79 20 70 child. Every p
13ccc 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
13ccd 61 73 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 ase contains.**
13cce 30 20 6f 72 20 31 20 70 61 72 65 6e 74 20 70 61 0 or 1 parent pa
13ccf 67 65 73 2e 20 20 28 49 6e 20 74 68 69 73 20 63 ges. (In this c
13cd0 6f 6e 74 65 78 74 20 27 64 61 74 61 62 61 73 65 ontext 'database
13cd1 20 70 61 67 65 27 20 72 65 66 65 72 73 0a 2a 2a page' refers.**
13cd2 20 74 6f 20 61 6e 79 20 70 61 67 65 20 74 68 61 to any page tha
13cd3 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 t is not part of
13cd4 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
13cd5 20 69 74 73 65 6c 66 2e 29 20 20 45 61 63 68 20 itself.) Each
13cd6 70 6f 69 6e 74 65 72 20 6d 61 70 0a 2a 2a 20 65 pointer map.** e
13cd7 6e 74 72 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 ntry consists of
13cd8 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 27 a single byte '
13cd9 74 79 70 65 27 20 61 6e 64 20 61 20 34 20 62 79 type' and a 4 by
13cda 74 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e te parent page n
13cdb 75 6d 62 65 72 2e 0a 2a 2a 20 54 68 65 20 50 54 umber..** The PT
13cdc 52 4d 41 50 5f 58 58 58 20 69 64 65 6e 74 69 66 RMAP_XXX identif
13cdd 69 65 72 73 20 62 65 6c 6f 77 20 61 72 65 20 74 iers below are t
13cde 68 65 20 76 61 6c 69 64 20 74 79 70 65 73 2e 0a he valid types..
13cdf 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 **.** The purpos
13ce0 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 e of the pointer
13ce1 20 6d 61 70 20 69 73 20 74 6f 20 66 61 63 69 6c map is to facil
13ce2 69 74 79 20 6d 6f 76 69 6e 67 20 70 61 67 65 73 ity moving pages
13ce3 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 from one.** pos
13ce4 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c ition in the fil
13ce5 65 20 74 6f 20 61 6e 6f 74 68 65 72 20 61 73 20 e to another as
13ce6 70 61 72 74 20 6f 66 20 61 75 74 6f 76 61 63 75 part of autovacu
13ce7 75 6d 2e 20 20 57 68 65 6e 20 61 20 70 61 67 65 um. When a page
13ce8 0a 2a 2a 20 69 73 20 6d 6f 76 65 64 2c 20 74 68 .** is moved, th
13ce9 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 69 74 73 e pointer in its
13cea 20 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20 parent must be
13ceb 75 70 64 61 74 65 64 20 74 6f 20 70 6f 69 6e 74 updated to point
13cec 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c to the.** new l
13ced 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 6f ocation. The po
13cee 69 6e 74 65 72 20 6d 61 70 20 69 73 20 75 73 65 inter map is use
13cef 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 d to locate the
13cf0 70 61 72 65 6e 74 20 70 61 67 65 20 71 75 69 63 parent page quic
13cf1 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 kly..**.** PTRMA
13cf2 50 5f 52 4f 4f 54 50 41 47 45 3a 20 54 68 65 20 P_ROOTPAGE: The
13cf3 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
13cf4 20 61 20 72 6f 6f 74 2d 70 61 67 65 2e 20 54 68 a root-page. Th
13cf5 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 e page-number is
13cf6 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 not.**
13cf7 20 20 20 20 20 20 20 20 20 75 73 65 64 20 69 6e used in
13cf8 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a this case..**.*
13cf9 2a 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 * PTRMAP_FREEPAG
13cfa 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 E: The database
13cfb 70 61 67 65 20 69 73 20 61 6e 20 75 6e 75 73 65 page is an unuse
13cfc 64 20 28 66 72 65 65 29 20 70 61 67 65 2e 20 54 d (free) page. T
13cfd 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 0a he page-number .
13cfe 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
13cff 20 20 20 20 69 73 20 6e 6f 74 20 75 73 65 64 20 is not used
13d00 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a in this case..**
13d01 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 .** PTRMAP_OVERF
13d02 4c 4f 57 31 3a 20 54 68 65 20 64 61 74 61 62 61 LOW1: The databa
13d03 73 65 20 70 61 67 65 20 69 73 20 74 68 65 20 66 se page is the f
13d04 69 72 73 74 20 70 61 67 65 20 69 6e 20 61 20 6c irst page in a l
13d05 69 73 74 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 ist of .**
13d06 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 ove
13d07 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 rflow pages. The
13d08 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 64 65 page number ide
13d09 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 67 65 ntifies the page
13d0a 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 that.**
13d0b 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61 conta
13d0c 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 77 69 74 ins the cell wit
13d0d 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 h a pointer to t
13d0e 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 his overflow pag
13d0f 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f e..**.** PTRMAP_
13d10 4f 56 45 52 46 4c 4f 57 32 3a 20 54 68 65 20 64 OVERFLOW2: The d
13d11 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 atabase page is
13d12 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 20 6c 61 the second or la
13d13 74 65 72 20 70 61 67 65 20 69 6e 20 61 20 6c 69 ter page in a li
13d14 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 st of.**
13d15 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 overf
13d16 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 low pages. The p
13d17 61 67 65 2d 6e 75 6d 62 65 72 20 69 64 65 6e 74 age-number ident
13d18 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f ifies the previo
13d19 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 us.**
13d1a 20 20 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 page in
13d1b 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
13d1c 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 e list..**.** PT
13d1d 52 4d 41 50 5f 42 54 52 45 45 3a 20 54 68 65 20 RMAP_BTREE: The
13d1e 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
13d1f 20 61 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 a non-root btre
13d20 65 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 e page. The page
13d21 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 number.**
13d22 20 20 20 20 20 20 20 20 20 69 64 65 6e 74 69 66 identif
13d23 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 ies the parent p
13d24 61 67 65 20 69 6e 20 74 68 65 20 62 74 72 65 65 age in the btree
13d25 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 ..*/.#define PTR
13d26 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 31 0a 23 MAP_ROOTPAGE 1.#
13d27 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 46 52 define PTRMAP_FR
13d28 45 45 50 41 47 45 20 32 0a 23 64 65 66 69 6e 65 EEPAGE 2.#define
13d29 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
13d2a 31 20 33 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 1 3.#define PTRM
13d2b 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 34 0a 23 AP_OVERFLOW2 4.#
13d2c 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 42 54 define PTRMAP_BT
13d2d 52 45 45 20 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 REE 5../* A bunc
13d2e 68 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 h of assert() st
13d2f 61 74 65 6d 65 6e 74 73 20 74 6f 20 63 68 65 63 atements to chec
13d30 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f k the transactio
13d31 6e 20 73 74 61 74 65 20 76 61 72 69 61 62 6c 65 n state variable
13d32 73 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 20 70 s.** of handle p
13d33 20 28 74 79 70 65 20 42 74 72 65 65 2a 29 20 61 (type Btree*) a
13d34 72 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f re internally co
13d35 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 nsistent..*/.#de
13d36 66 69 6e 65 20 62 74 72 65 65 49 6e 74 65 67 72 fine btreeIntegr
13d37 69 74 79 28 70 29 20 5c 0a 20 20 61 73 73 65 72 ity(p) \. asser
13d38 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 t( p->pBt->inTra
13d39 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f nsaction!=TRANS_
13d3a 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e NONE || p->pBt->
13d3b 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 nTransaction==0
13d3c 29 3b 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 ); \. assert( p
13d3d 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ->pBt->inTransac
13d3e 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 tion>=p->inTrans
13d3f 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ); .../*.** The
13d40 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 ISAUTOVACUUM ma
13d41 63 72 6f 20 69 73 20 75 73 65 64 20 77 69 74 68 cro is used with
13d42 69 6e 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f in balance_nonro
13d43 6f 74 28 29 20 74 6f 20 64 65 74 65 72 6d 69 6e ot() to determin
13d44 65 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 e.** if the data
13d45 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 base supports au
13d46 74 6f 2d 76 61 63 75 75 6d 20 6f 72 20 6e 6f 74 to-vacuum or not
13d47 2e 20 42 65 63 61 75 73 65 20 69 74 20 69 73 20 . Because it is
13d48 75 73 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61 used.** within a
13d49 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 n expression tha
13d4a 74 20 69 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 t is an argument
13d4b 20 74 6f 20 61 6e 6f 74 68 65 72 20 6d 61 63 72 to another macr
13d4c 6f 20 0a 2a 2a 20 28 73 71 6c 69 74 65 4d 61 6c o .** (sqliteMal
13d4d 6c 6f 63 52 61 77 29 2c 20 69 74 20 69 73 20 6e locRaw), it is n
13d4e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 ot possible to u
13d4f 73 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 se conditional c
13d50 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 ompilation..** S
13d51 6f 2c 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 o, this macro is
13d52 20 64 65 66 69 6e 65 64 20 69 6e 73 74 65 61 64 defined instead
13d53 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
13d54 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
13d55 55 55 4d 0a 23 64 65 66 69 6e 65 20 49 53 41 55 UUM.#define ISAU
13d56 54 4f 56 41 43 55 55 4d 20 28 70 42 74 2d 3e 61 TOVACUUM (pBt->a
13d57 75 74 6f 56 61 63 75 75 6d 29 0a 23 65 6c 73 65 utoVacuum).#else
13d58 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 .#define ISAUTOV
13d59 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a ACUUM 0.#endif..
13d5a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 ./*.** This stru
13d5b 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20 cture is passed
13d5c 61 72 6f 75 6e 64 20 74 68 72 6f 75 67 68 20 61 around through a
13d5d 6c 6c 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 ll the sanity ch
13d5e 65 63 6b 69 6e 67 20 72 6f 75 74 69 6e 65 73 0a ecking routines.
13d5f 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b ** in order to k
13d60 65 65 70 20 74 72 61 63 6b 20 6f 66 20 73 6f 6d eep track of som
13d61 65 20 67 6c 6f 62 61 6c 20 73 74 61 74 65 20 69 e global state i
13d62 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 nformation..*/.t
13d63 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e ypedef struct In
13d64 74 65 67 72 69 74 79 43 6b 20 49 6e 74 65 67 72 tegrityCk Integr
13d65 69 74 79 43 6b 3b 0a 73 74 72 75 63 74 20 49 6e ityCk;.struct In
13d66 74 65 67 72 69 74 79 43 6b 20 7b 0a 20 20 42 74 tegrityCk {. Bt
13d67 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 Shared *pBt;
13d68 2f 2a 20 54 68 65 20 74 72 65 65 20 62 65 69 6e /* The tree bein
13d69 67 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f g checked out */
13d6a 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
13d6b 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f ; /* The asso
13d6c 63 69 61 74 65 64 20 70 61 67 65 72 2e 20 20 41 ciated pager. A
13d6d 6c 73 6f 20 61 63 63 65 73 73 69 62 6c 65 20 62 lso accessible b
13d6e 79 20 70 42 74 2d 3e 70 50 61 67 65 72 20 2a 2f y pBt->pPager */
13d6f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 . Pgno nPage;
13d70 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
13d71 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
13d72 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 atabase */. int
13d73 20 2a 61 6e 52 65 66 3b 20 20 20 20 20 20 20 2f *anRef; /
13d74 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 * Number of time
13d75 73 20 65 61 63 68 20 70 61 67 65 20 69 73 20 72 s each page is r
13d76 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69 eferenced */. i
13d77 6e 74 20 6d 78 45 72 72 3b 20 20 20 20 20 20 20 nt mxErr;
13d78 20 2f 2a 20 53 74 6f 70 20 61 63 63 75 6d 75 6c /* Stop accumul
13d79 61 74 69 6e 67 20 65 72 72 6f 72 73 20 77 68 65 ating errors whe
13d7a 6e 20 74 68 69 73 20 72 65 61 63 68 65 73 20 7a n this reaches z
13d7b 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 ero */. int nEr
13d7c 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 r; /* Nu
13d7d 6d 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65 73 mber of messages
13d7e 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72 72 written to zErr
13d7f 4d 73 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 Msg so far */.
13d80 69 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 int mallocFailed
13d81 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c ; /* A memory al
13d82 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 location error h
13d83 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 20 as occurred */.
13d84 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73 67 StrAccum errMsg
13d85 3b 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 ; /* Accumulate
13d86 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
13d87 67 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f 0a ge text here */.
13d88 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f };../*.** Read o
13d89 72 20 77 72 69 74 65 20 61 20 74 77 6f 2d 20 61 r write a two- a
13d8a 6e 64 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67 nd four-byte big
13d8b 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
13d8c 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 values..*/.#defi
13d8d 6e 65 20 67 65 74 32 62 79 74 65 28 78 29 20 20 ne get2byte(x)
13d8e 20 28 28 78 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 ((x)[0]<<8 | (x
13d8f 29 5b 31 5d 29 0a 23 64 65 66 69 6e 65 20 70 75 )[1]).#define pu
13d90 74 32 62 79 74 65 28 70 2c 76 29 20 28 28 70 29 t2byte(p,v) ((p)
13d91 5b 30 5d 20 3d 20 28 75 38 29 28 28 76 29 3e 3e [0] = (u8)((v)>>
13d92 38 29 2c 20 28 70 29 5b 31 5d 20 3d 20 28 75 38 8), (p)[1] = (u8
13d93 29 28 76 29 29 0a 23 64 65 66 69 6e 65 20 67 65 )(v)).#define ge
13d94 74 34 62 79 74 65 20 73 71 6c 69 74 65 33 47 65 t4byte sqlite3Ge
13d95 74 34 62 79 74 65 0a 23 64 65 66 69 6e 65 20 70 t4byte.#define p
13d96 75 74 34 62 79 74 65 20 73 71 6c 69 74 65 33 50 ut4byte sqlite3P
13d97 75 74 34 62 79 74 65 0a 0a 2f 2a 0a 2a 2a 20 49 ut4byte../*.** I
13d98 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e 65 73 nternal routines
13d99 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
13d9a 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20 accessed by the
13d9b 62 74 72 65 65 20 6c 61 79 65 72 20 6f 6e 6c 79 btree layer only
13d9c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
13d9d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
13d9e 74 72 65 65 47 65 74 50 61 67 65 28 42 74 53 68 treeGetPage(BtSh
13d9f 61 72 65 64 2a 2c 20 50 67 6e 6f 2c 20 4d 65 6d ared*, Pgno, Mem
13da0 50 61 67 65 2a 2a 2c 20 69 6e 74 29 3b 0a 53 51 Page**, int);.SQ
13da1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13da2 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 sqlite3BtreeIni
13da3 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 tPage(MemPage *p
13da4 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Page);.SQLITE_PR
13da5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
13da6 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
13da7 50 74 72 28 4d 65 6d 50 61 67 65 2a 2c 20 75 38 Ptr(MemPage*, u8
13da8 2a 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53 *, CellInfo*);.S
13da9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
13daa 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 id sqlite3BtreeP
13dab 61 72 73 65 43 65 6c 6c 28 4d 65 6d 50 61 67 65 arseCell(MemPage
13dac 2a 2c 20 69 6e 74 2c 20 43 65 6c 6c 49 6e 66 6f *, int, CellInfo
13dad 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
13dae 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
13daf 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 reeRestoreCursor
13db0 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f Position(BtCurso
13db1 72 20 2a 70 43 75 72 29 3b 0a 53 51 4c 49 54 45 r *pCur);.SQLITE
13db2 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
13db3 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 65 6d lite3BtreeGetTem
13db4 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 pCursor(BtCursor
13db5 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 6f 72 *pCur, BtCursor
13db6 20 2a 70 54 65 6d 70 43 75 72 29 3b 0a 53 51 4c *pTempCur);.SQL
13db7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
13db8 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 6c sqlite3BtreeRel
13db9 65 61 73 65 54 65 6d 70 43 75 72 73 6f 72 28 42 easeTempCursor(B
13dba 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a tCursor *pCur);.
13dbb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
13dbc 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
13dbd 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 MoveToParent(BtC
13dbe 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a 0a 2f ursor *pCur);../
13dbf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
13dc0 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68 nd of btreeInt.h
13dc1 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
13dc2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13dc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
13dc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
13dc5 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
13dc6 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62 we left off in b
13dc7 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a tmutex.c *******
13dc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 *************/.#
13dc9 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
13dca 53 41 46 45 20 26 26 20 21 64 65 66 69 6e 65 64 SAFE && !defined
13dcb 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 (SQLITE_OMIT_SHA
13dcc 52 45 44 5f 43 41 43 48 45 29 0a 0a 0a 2f 2a 0a RED_CACHE).../*.
13dcd 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78 ** Enter a mutex
13dce 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54 on the given BT
13dcf 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a ree object..**.*
13dd0 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20 * If the object
13dd1 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c is not sharable,
13dd2 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69 then no mutex i
13dd3 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 0a s ever required.
13dd4 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 ** and this rout
13dd5 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 ine is a no-op.
13dd6 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 The underlying
13dd7 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63 mutex is non-rec
13dd8 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77 ursive..** But w
13dd9 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e e keep a referen
13dda 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65 ce count in Btre
13ddb 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 e.wantToLock so
13ddc 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 the behavior.**
13ddd 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 of this interfac
13dde 65 20 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a e is recursive..
13ddf 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64 **.** To avoid d
13de0 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 eadlocks, multip
13de1 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f le Btrees are lo
13de2 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 cked in the same
13de3 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c order.** by all
13de4 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
13de5 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70 tions. The p->p
13de6 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f Next is a list o
13de7 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65 f other.** Btree
13de8 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 s belonging to t
13de9 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
13dea 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 connection as t
13deb 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68 he p Btree.** wh
13dec 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c ich need to be l
13ded 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20 ocked after p.
13dee 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74 If we cannot get
13def 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c a lock on.** p,
13df0 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f then first unlo
13df1 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 ck all of the ot
13df2 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 hers on p->pNext
13df3 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66 , then wait.** f
13df4 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62 or the lock to b
13df5 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20 ecome available
13df6 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63 on p, then reloc
13df7 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 k all of the.**
13df8 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 65 subsequent Btree
13df9 73 20 74 68 61 74 20 64 65 73 69 72 65 20 61 20 s that desire a
13dfa 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lock..*/.SQLITE_
13dfb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
13dfc 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 ite3BtreeEnter(B
13dfd 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65 tree *p){. Btre
13dfe 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a e *pLater;.. /*
13dff 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 Some basic sani
13e00 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 ty checking on t
13e01 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c he Btree. The l
13e02 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20 ist of Btrees.
13e03 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 ** connected by
13e04 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 20 pNext and pPrev
13e05 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72 should be in sor
13e06 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a ted order by. *
13e07 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75 * Btree.pBt valu
13e08 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 e. All elements
13e09 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75 of the list shou
13e0a 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a ld belong to. *
13e0b 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 * the same conne
13e0c 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 ction. Only shar
13e0d 65 64 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e ed Btrees are on
13e0e 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 the list. */.
13e0f 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 assert( p->pNext
13e10 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d ==0 || p->pNext-
13e11 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 >pBt>p->pBt );.
13e12 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 assert( p->pPre
13e13 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 v==0 || p->pPrev
13e14 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a ->pBt<p->pBt );.
13e15 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 assert( p->pNe
13e16 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 xt==0 || p->pNex
13e17 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a t->db==p->db );.
13e18 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 assert( p->pPr
13e19 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 ev==0 || p->pPre
13e1a 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a v->db==p->db );.
13e1b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 assert( p->sha
13e1c 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 rable || (p->pNe
13e1d 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65 xt==0 && p->pPre
13e1e 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 v==0) );.. /* C
13e1f 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 heck for locking
13e20 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a consistency */.
13e21 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f assert( !p->lo
13e22 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 cked || p->wantT
13e23 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 oLock>0 );. ass
13e24 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 ert( p->sharable
13e25 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 || p->wantToLoc
13e26 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 k==0 );.. /* We
13e27 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 should already
13e28 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 hold a lock on t
13e29 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
13e2a 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 ection */. asse
13e2b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
13e2c 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
13e2d 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 28 20 21 tex) );.. if( !
13e2e 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 20 72 65 p->sharable ) re
13e2f 74 75 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 turn;. p->wantT
13e30 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 oLock++;. if( p
13e31 2d 3e 6c 6f 63 6b 65 64 20 29 20 72 65 74 75 72 ->locked ) retur
13e32 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 n;.. /* In most
13e33 20 63 61 73 65 73 2c 20 77 65 20 73 68 6f 75 6c cases, we shoul
13e34 64 20 62 65 20 61 62 6c 65 20 74 6f 20 61 63 71 d be able to acq
13e35 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 77 65 uire the lock we
13e36 0a 20 20 2a 2a 20 77 61 6e 74 20 77 69 74 68 6f . ** want witho
13e37 75 74 20 68 61 76 69 6e 67 20 74 6f 20 67 6f 20 ut having to go
13e38 74 68 72 6f 75 67 68 74 20 74 68 65 20 61 73 63 throught the asc
13e39 65 6e 64 69 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a ending lock. **
13e3a 20 70 72 6f 63 65 64 75 72 65 20 74 68 61 74 20 procedure that
13e3b 66 6f 6c 6c 6f 77 73 2e 20 20 4a 75 73 74 20 62 follows. Just b
13e3c 65 20 73 75 72 65 20 6e 6f 74 20 74 6f 20 62 6c e sure not to bl
13e3d 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ock.. */. if(
13e3e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
13e3f 79 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 y(p->pBt->mutex)
13e40 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13e41 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 p->locked = 1
13e42 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
13e43 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64 }.. /* To avoid
13e44 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74 deadlock, first
13e45 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63 release all loc
13e46 6b 73 20 77 69 74 68 20 61 20 6c 61 72 67 65 72 ks with a larger
13e47 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61 . ** BtShared a
13e48 64 64 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63 ddress. Then ac
13e49 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 quire our lock.
13e4a 20 54 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a Then reacquire.
13e4b 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42 ** the other B
13e4c 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68 tShared locks th
13e4d 61 74 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f at we used to ho
13e4e 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a ld in ascending.
13e4f 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f ** order.. */
13e50 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d . for(pLater=p-
13e51 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 >pNext; pLater;
13e52 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 pLater=pLater->p
13e53 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 Next){. asser
13e54 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61 t( pLater->shara
13e55 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ble );. asser
13e56 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 t( pLater->pNext
13e57 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 ==0 || pLater->p
13e58 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72 Next->pBt>pLater
13e59 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 ->pBt );. ass
13e5a 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f ert( !pLater->lo
13e5b 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e cked || pLater->
13e5c 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a wantToLock>0 );.
13e5d 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e if( pLater->
13e5e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 locked ){.
13e5f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
13e60 61 76 65 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d ave(pLater->pBt-
13e61 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 >mutex);. p
13e62 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20 Later->locked =
13e63 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 0;. }. }. s
13e64 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
13e65 65 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 er(p->pBt->mutex
13e66 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d );. p->locked =
13e67 20 31 3b 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 1;. for(pLater
13e68 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 =p->pNext; pLate
13e69 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 r; pLater=pLater
13e6a 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
13e6b 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f ( pLater->wantTo
13e6c 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 Lock ){. sq
13e6d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
13e6e 72 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d 3e 6d r(pLater->pBt->m
13e6f 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 4c 61 utex);. pLa
13e70 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b ter->locked = 1;
13e71 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
13e72 0a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 65 63 .** Exit the rec
13e73 75 72 73 69 76 65 20 6d 75 74 65 78 20 6f 6e 20 ursive mutex on
13e74 61 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 a Btree..*/.SQLI
13e75 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
13e76 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
13e77 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 e(Btree *p){. i
13e78 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 f( p->sharable )
13e79 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d {. assert( p-
13e7a 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b >wantToLock>0 );
13e7b 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f . p->wantToLo
13e7c 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d ck--;. if( p-
13e7d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 >wantToLock==0 )
13e7e 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
13e7f 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 p->locked );.
13e80 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
13e81 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d _leave(p->pBt->m
13e82 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 2d 3e utex);. p->
13e83 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 locked = 0;.
13e84 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 }. }.}..#ifndef
13e85 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 NDEBUG./*.** Re
13e86 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
13e87 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 BtShared mutex
13e88 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 62 is held on the b
13e89 74 72 65 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 tree. .**.** Th
13e8a 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73 is routine makes
13e8b 20 6e 6f 20 64 65 74 65 72 6d 69 6e 61 74 69 6f no determinatio
13e8c 6e 20 6f 6e 65 20 77 61 79 20 6f 72 20 61 6e 6f n one way or ano
13e8d 74 68 65 72 20 69 66 20 74 68 65 0a 2a 2a 20 64 ther if the.** d
13e8e 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
13e8f 6f 6e 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 on mutex is held
13e90 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
13e91 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c tine is used onl
13e92 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 73 y from within as
13e93 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
13e94 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
13e95 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
13e96 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
13e97 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 Btree *p){. ret
13e98 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65 urn (p->sharable
13e99 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 ==0 ||.
13e9a 20 20 20 20 28 70 2d 3e 6c 6f 63 6b 65 64 20 26 (p->locked &
13e9b 26 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 & p->wantToLock
13e9c 26 26 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 && sqlite3_mutex
13e9d 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 _held(p->pBt->mu
13e9e 74 65 78 29 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 tex)));.}.#endif
13e9f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
13ea0 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
13ea1 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 /*.** Enter and
13ea2 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20 6f 6e leave a mutex on
13ea3 20 61 20 42 74 72 65 65 20 67 69 76 65 6e 20 61 a Btree given a
13ea4 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 79 cursor owned by
13ea5 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20 that.** Btree.
13ea6 20 54 68 65 73 65 20 65 6e 74 72 79 20 70 6f 69 These entry poi
13ea7 6e 74 73 20 61 72 65 20 75 73 65 64 20 62 79 20 nts are used by
13ea8 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 incremental I/O
13ea9 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d and can be.** om
13eaa 69 74 74 65 64 20 69 66 20 74 68 61 74 20 6d 6f itted if that mo
13eab 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 64 dule is not used
13eac 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
13ead 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
13eae 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 BtreeEnterCursor
13eaf 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
13eb0 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 {. sqlite3Btree
13eb1 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72 Enter(pCur->pBtr
13eb2 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ee);.}.SQLITE_PR
13eb3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
13eb4 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
13eb5 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
13eb6 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 r){. sqlite3Btr
13eb7 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 42 eeLeave(pCur->pB
13eb8 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 tree);.}.#endif
13eb9 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 /* SQLITE_OMIT_I
13eba 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a NCRBLOB */.../*.
13ebb 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 ** Enter the mut
13ebc 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74 72 65 ex on every Btre
13ebd 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
13ebe 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 h a database.**
13ebf 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 connection. Thi
13ec0 73 20 69 73 20 6e 65 65 64 65 64 20 28 66 6f 72 s is needed (for
13ec1 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 20 example) prior
13ec2 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20 to parsing.** a
13ec3 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 20 statement since
13ec4 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61 we will be compa
13ec5 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 63 ring table and c
13ec6 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61 olumn names.** a
13ec7 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d gainst all schem
13ec8 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 as and we do not
13ec9 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63 68 65 want those sche
13eca 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73 mas being.** res
13ecb 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 et out from unde
13ecc 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 r us..**.** Ther
13ecd 65 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e e is a correspon
13ece 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 ding leave-all p
13ecf 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a rocedures..**.**
13ed0 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 Enter the mutex
13ed1 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 es in accending
13ed2 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 72 65 order by BtShare
13ed3 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65 73 d pointer addres
13ed4 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 68 s.** to avoid th
13ed5 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 e possibility of
13ed6 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74 deadlock when t
13ed7 77 6f 20 74 68 72 65 61 64 73 20 77 69 74 68 0a wo threads with.
13ed8 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 ** two or more b
13ed9 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 trees in common
13eda 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b both try to lock
13edb 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72 65 65 all their btree
13edc 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65 s.** at the same
13edd 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c instant..*/.SQL
13ede 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
13edf 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
13ee0 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 erAll(sqlite3 *d
13ee1 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 b){. int i;. B
13ee2 74 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72 tree *p, *pLater
13ee3 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
13ee4 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
13ee5 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 b->mutex) );. f
13ee6 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
13ee7 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d b; i++){. p =
13ee8 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
13ee9 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d . if( p && p-
13eea 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
13eeb 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b p->wantToLock
13eec 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 ++;. if( !p
13eed 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 ->locked ){.
13eee 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 assert( p->w
13eef 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b 0a antToLock==1 );.
13ef0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
13ef1 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70 2d ->pPrev ) p = p-
13ef2 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20 20 >pPrev;.
13ef3 77 68 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 while( p->locked
13ef4 20 26 26 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 && p->pNext ) p
13ef5 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 = p->pNext;.
13ef6 20 20 20 20 20 66 6f 72 28 70 4c 61 74 65 72 20 for(pLater
13ef7 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 = p->pNext; pLat
13ef8 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 er; pLater=pLate
13ef9 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 r->pNext){.
13efa 20 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d if( pLater-
13efb 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 >locked ){.
13efc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d sqlite3_m
13efd 75 74 65 78 5f 6c 65 61 76 65 28 70 4c 61 74 65 utex_leave(pLate
13efe 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a r->pBt->mutex);.
13eff 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 61 74 pLat
13f00 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a er->locked = 0;.
13f01 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
13f02 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68 }. wh
13f03 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20 ile( p ){.
13f04 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
13f05 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 2d 3e x_enter(p->pBt->
13f06 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 mutex);.
13f07 20 20 70 2d 3e 6c 6f 63 6b 65 64 2b 2b 3b 0a 20 p->locked++;.
13f08 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e p = p->
13f09 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d pNext;. }
13f0a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
13f0b 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 }.}.SQLITE_PRIV
13f0c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
13f0d 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 BtreeLeaveAll(sq
13f0e 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e lite3 *db){. in
13f0f 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b t i;. Btree *p;
13f10 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
13f11 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
13f12 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f ->mutex) );. fo
13f13 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 r(i=0; i<db->nDb
13f14 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 ; i++){. p =
13f15 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
13f16 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e if( p && p->
13f17 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
13f18 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e assert( p->wan
13f19 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 tToLock>0 );.
13f1a 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b p->wantToLock
13f1b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d --;. if( p-
13f1c 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 >wantToLock==0 )
13f1d 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
13f1e 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 ( p->locked );.
13f1f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d sqlite3_m
13f20 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 utex_leave(p->pB
13f21 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 t->mutex);.
13f22 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 p->locked = 0
13f23 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
13f24 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e }.}..#ifndef N
13f25 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 DEBUG./*.** Retu
13f26 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 63 rn true if the c
13f27 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f urrent thread ho
13f28 6c 64 73 20 74 68 65 20 64 61 74 61 62 61 73 65 lds the database
13f29 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d connection.** m
13f2a 75 74 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71 utex and all req
13f2b 75 69 72 65 64 20 42 74 53 68 61 72 65 64 20 6d uired BtShared m
13f2c 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 utexes..**.** Th
13f2d 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
13f2e 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ed inside assert
13f2f 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e () statements on
13f30 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
13f31 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
13f32 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 3BtreeHoldsAllMu
13f33 74 65 78 65 73 28 73 71 6c 69 74 65 33 20 2a 64 texes(sqlite3 *d
13f34 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 b){. int i;. i
13f35 66 28 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 f( !sqlite3_mute
13f36 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 x_held(db->mutex
13f37 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
13f38 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 0;. }. for(i=0
13f39 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
13f3a 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 3b ){. Btree *p;
13f3b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 . p = db->aDb
13f3c 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 [i].pBt;. if(
13f3d 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c p && p->sharabl
13f3e 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 28 70 e &&. (p
13f3f 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
13f40 7c 7c 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 || !sqlite3_mute
13f41 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d x_held(p->pBt->m
13f42 75 74 65 78 29 29 20 29 7b 0a 20 20 20 20 20 20 utex)) ){.
13f43 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
13f44 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a }. return 1;.
13f45 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 }.#endif /* NDEB
13f46 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 UG */../*.** Add
13f47 20 61 20 6e 65 77 20 42 74 72 65 65 20 70 6f 69 a new Btree poi
13f48 6e 74 65 72 20 74 6f 20 61 20 42 74 72 65 65 4d nter to a BtreeM
13f49 75 74 65 78 41 72 72 61 79 2e 20 0a 2a 2a 20 69 utexArray. .** i
13f4a 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 63 61 f the pointer ca
13f4b 6e 20 70 6f 73 73 69 62 6c 79 20 62 65 20 73 68 n possibly be sh
13f4c 61 72 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 6f ared with.** ano
13f4d 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f ther database co
13f4e 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nnection..**.**
13f4f 54 68 65 20 70 6f 69 6e 74 65 72 73 20 61 72 65 The pointers are
13f50 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 65 64 20 kept in sorted
13f51 6f 72 64 65 72 20 62 79 20 70 42 74 72 65 65 2d order by pBtree-
13f52 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a 2a 20 77 >pBt. That.** w
13f53 61 79 20 77 68 65 6e 20 77 65 20 67 6f 20 74 6f ay when we go to
13f54 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 65 20 6d enter all the m
13f55 75 74 65 78 65 73 2c 20 77 65 20 63 61 6e 20 65 utexes, we can e
13f56 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 nter them.** in
13f57 6f 72 64 65 72 20 77 69 74 68 6f 75 74 20 65 76 order without ev
13f58 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 62 61 ery having to ba
13f59 63 6b 75 70 20 61 6e 64 20 72 65 74 72 79 20 61 ckup and retry a
13f5a 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20 77 6f nd without.** wo
13f5b 72 72 79 69 6e 67 20 61 62 6f 75 74 20 64 65 61 rrying about dea
13f5c 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 dlock..**.** The
13f5d 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 61 72 65 number of share
13f5e 64 20 62 74 72 65 65 73 20 77 69 6c 6c 20 61 6c d btrees will al
13f5f 77 61 79 73 20 62 65 20 73 6d 61 6c 6c 20 28 75 ways be small (u
13f60 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 29 0a 2a sually 0 or 1).*
13f61 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 74 69 6f * so an insertio
13f62 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 61 64 65 n sort is an ade
13f63 71 75 61 74 65 20 61 6c 67 6f 72 69 74 68 6d 20 quate algorithm
13f64 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f here..*/.SQLITE_
13f65 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
13f66 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
13f67 72 61 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d rayInsert(BtreeM
13f68 75 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 utexArray *pArra
13f69 79 2c 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 y, Btree *pBtree
13f6a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 ){. int i, j;.
13f6b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a BtShared *pBt;.
13f6c 20 20 69 66 28 20 70 42 74 72 65 65 3d 3d 30 20 if( pBtree==0
13f6d 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 61 72 61 || pBtree->shara
13f6e 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ble==0 ) return;
13f6f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
13f70 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b {. for(i=0;
13f71 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 i<pArray->nMute
13f72 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 x; i++){. a
13f73 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61 ssert( pArray->a
13f74 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65 Btree[i]!=pBtree
13f75 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 );. }. }.#e
13f76 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 ndif. assert( p
13f77 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 Array->nMutex>=0
13f78 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41 );. assert( pA
13f79 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 rray->nMutex<Arr
13f7a 61 79 53 69 7a 65 28 70 41 72 72 61 79 2d 3e 61 aySize(pArray->a
13f7b 42 74 72 65 65 29 2d 31 20 29 3b 0a 20 20 70 42 Btree)-1 );. pB
13f7c 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b t = pBtree->pBt;
13f7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 . for(i=0; i<pA
13f7e 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b rray->nMutex; i+
13f7f 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 +){. assert(
13f80 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 pArray->aBtree[i
13f81 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20 ]!=pBtree );.
13f82 20 69 66 28 20 70 41 72 72 61 79 2d 3e 61 42 74 if( pArray->aBt
13f83 72 65 65 5b 69 5d 2d 3e 70 42 74 3e 70 42 74 20 ree[i]->pBt>pBt
13f84 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 ){. for(j=p
13f85 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a Array->nMutex; j
13f86 3e 69 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 >i; j--){.
13f87 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 pArray->aBtree
13f88 5b 6a 5d 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 [j] = pArray->aB
13f89 74 72 65 65 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 tree[j-1];.
13f8a 20 7d 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d }. pArray-
13f8b 3e 61 42 74 72 65 65 5b 69 5d 20 3d 20 70 42 74 >aBtree[i] = pBt
13f8c 72 65 65 3b 0a 20 20 20 20 20 20 70 41 72 72 61 ree;. pArra
13f8d 79 2d 3e 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 y->nMutex++;.
13f8e 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
13f8f 0a 20 20 7d 0a 20 20 70 41 72 72 61 79 2d 3e 61 . }. pArray->a
13f90 42 74 72 65 65 5b 70 41 72 72 61 79 2d 3e 6e 4d Btree[pArray->nM
13f91 75 74 65 78 2b 2b 5d 20 3d 20 70 42 74 72 65 65 utex++] = pBtree
13f92 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 ;.}../*.** Enter
13f93 20 74 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76 the mutex of ev
13f94 65 72 79 20 62 74 72 65 65 20 69 6e 20 74 68 65 ery btree in the
13f95 20 61 72 72 61 79 2e 20 20 54 68 69 73 20 72 6f array. This ro
13f96 75 74 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c utine is.** call
13f97 65 64 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e ed at the beginn
13f98 69 6e 67 20 6f 66 20 73 71 6c 69 74 65 33 56 64 ing of sqlite3Vd
13f99 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 6d beExec(). The m
13f9a 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 65 78 utexes are.** ex
13f9b 69 74 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 ited at the end
13f9c 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 of the same func
13f9d 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
13f9e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
13f9f 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
13fa0 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 rayEnter(BtreeMu
13fa1 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 texArray *pArray
13fa2 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f ){. int i;. fo
13fa3 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d r(i=0; i<pArray-
13fa4 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 >nMutex; i++){.
13fa5 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41 Btree *p = pA
13fa6 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b rray->aBtree[i];
13fa7 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 . /* Some bas
13fa8 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 ic sanity checki
13fa9 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 ng */. assert
13faa 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79 ( i==0 || pArray
13fab 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 ->aBtree[i-1]->p
13fac 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20 Bt<p->pBt );.
13fad 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 assert( !p->loc
13fae 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f ked || p->wantTo
13faf 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f Lock>0 );.. /
13fb0 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 * We should alre
13fb1 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 ady hold a lock
13fb2 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
13fb3 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
13fb4 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
13fb5 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
13fb6 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
13fb7 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b p->wantToLock
13fb8 2b 2b 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e ++;. if( !p->
13fb9 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 73 68 61 locked && p->sha
13fba 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 rable ){. s
13fbb 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
13fbc 65 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 er(p->pBt->mutex
13fbd 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b );. p->lock
13fbe 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ed = 1;. }.
13fbf 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 61 76 65 }.}../*.** Leave
13fc0 20 74 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76 the mutex of ev
13fc1 65 72 79 20 62 74 72 65 65 20 69 6e 20 74 68 65 ery btree in the
13fc2 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53 51 4c 49 54 group..*/.SQLIT
13fc3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
13fc4 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
13fc5 41 72 72 61 79 4c 65 61 76 65 28 42 74 72 65 65 ArrayLeave(Btree
13fc6 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 MutexArray *pArr
13fc7 61 79 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 ay){. int i;.
13fc8 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 for(i=0; i<pArra
13fc9 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b y->nMutex; i++){
13fca 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 . Btree *p =
13fcb 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 pArray->aBtree[i
13fcc 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 ];. /* Some b
13fcd 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 asic sanity chec
13fce 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73 65 king */. asse
13fcf 72 74 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72 rt( i==0 || pArr
13fd0 61 79 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d ay->aBtree[i-1]-
13fd1 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 >pBt<p->pBt );.
13fd2 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f assert( p->lo
13fd3 63 6b 65 64 20 7c 7c 20 21 70 2d 3e 73 68 61 72 cked || !p->shar
13fd4 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 able );. asse
13fd5 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
13fd6 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 k>0 );.. /* W
13fd7 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 e should already
13fd8 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 hold a lock on
13fd9 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
13fda 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 nection */. a
13fdb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
13fdc 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
13fdd 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 >mutex) );..
13fde 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b p->wantToLock--;
13fdf 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 . if( p->want
13fe0 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d 3e ToLock==0 && p->
13fe1 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 locked ){.
13fe2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
13fe3 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 ave(p->pBt->mute
13fe4 78 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 6f 63 x);. p->loc
13fe5 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 ked = 0;. }.
13fe6 20 7d 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 20 2f }.}...#endif /
13fe7 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 * SQLITE_THREADS
13fe8 41 46 45 20 26 26 20 21 53 51 4c 49 54 45 5f 4f AFE && !SQLITE_O
13fe9 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
13fea 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
13feb 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d 75 **** End of btmu
13fec 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tex.c **********
13fed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13fee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13fef 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
13ff0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
13ff1 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a btree.c ********
13ff2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13ff3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13ff4 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
13ff5 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 April 6.**.** Th
13ff6 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
13ff7 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
13ff8 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
13ff9 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
13ffa 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
13ffb 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
13ffc 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
13ffd 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
13ffe 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
13fff 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
14000 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
14001 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
14002 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
14003 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
14004 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
14005 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
14006 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
14007 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14008 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14009 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1400a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1400b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 ******.** $Id: b
1400c 74 72 65 65 2e 63 2c 76 20 31 2e 35 38 32 20 32 tree.c,v 1.582 2
1400d 30 30 39 2f 30 33 2f 33 30 20 31 38 3a 35 30 3a 009/03/30 18:50:
1400e 30 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 05 danielk1977 E
1400f 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 xp $.**.** This
14010 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 file implements
14011 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b a external (disk
14012 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 73 65 -based) database
14013 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a using BTrees..*
14014 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 * See the header
14015 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 74 72 comment on "btr
14016 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 64 64 eeInt.h" for add
14017 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
14018 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e ion..** Includin
14019 67 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 g a description
1401a 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61 of file format a
1401b 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 20 6f nd an overview o
1401c 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a f operation..*/.
1401d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 ./*.** The heade
1401e 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 r string that ap
1401f 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 pears at the beg
14020 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a inning of every.
14021 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 ** SQLite databa
14022 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f se..*/.static co
14023 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 48 nst char zMagicH
14024 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 45 eader[] = SQLITE
14025 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a 2f _FILE_HEADER;../
14026 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 6c *.** Set this gl
14027 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 6f obal variable to
14028 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 1 to enable tra
14029 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 54 cing using the T
1402a 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a RACE.** macro..*
1402b 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 /.#if 0.int sqli
1402c 74 65 33 42 74 72 65 65 54 72 61 63 65 3d 30 3b te3BtreeTrace=0;
1402d 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
1402e 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 ble tracing */.#
1402f 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29 define TRACE(X)
14030 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 65 if(sqlite3Btre
14031 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 58 eTrace){printf X
14032 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b ;fflush(stdout);
14033 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
14034 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 TRACE(X).#endif
14035 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ....#ifndef SQLI
14036 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
14037 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 ACHE./*.** A lis
14038 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 t of BtShared ob
14039 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 jects that are e
1403a 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 ligible for part
1403b 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 icipation.** in
1403c 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 shared cache. T
1403d 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 his variable has
1403e 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 file scope duri
1403f 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 ng normal builds
14040 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 ,.** but the tes
14041 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 t harness needs
14042 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 to access it so
14043 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 we make it globa
14044 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 l for .** test b
14045 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 uilds..**.** Acc
14046 65 73 73 20 74 6f 20 74 68 69 73 20 76 61 72 69 ess to this vari
14047 61 62 6c 65 20 69 73 20 70 72 6f 74 65 63 74 65 able is protecte
14048 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 d by SQLITE_MUTE
14049 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 2e X_STATIC_MASTER.
1404a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1404b 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
1404c 49 56 41 54 45 20 42 74 53 68 61 72 65 64 20 2a IVATE BtShared *
1404d 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 SQLITE_WSD sqlit
1404e 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
1404f 74 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 t = 0;.#else.sta
14050 74 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 51 tic BtShared *SQ
14051 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33 LITE_WSD sqlite3
14052 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20 SharedCacheList
14053 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 = 0;.#endif.#end
14054 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
14055 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a T_SHARED_CACHE *
14056 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
14057 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
14058 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 CHE./*.** Enable
14059 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 or disable the
1405a 73 68 61 72 65 64 20 70 61 67 65 72 20 61 6e 64 shared pager and
1405b 20 73 63 68 65 6d 61 20 66 65 61 74 75 72 65 73 schema features
1405c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1405d 74 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 66 65 tine has no effe
1405e 63 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 20 64 ct on existing d
1405f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14060 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 ons..** The shar
14061 65 64 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 ed cache setting
14062 20 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 66 75 effects only fu
14063 74 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a ture calls to.**
14064 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2c sqlite3_open(),
14065 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
14066 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 ), or sqlite3_op
14067 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 53 51 4c 49 en_v2()..*/.SQLI
14068 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
14069 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 e3_enable_shared
1406a 5f 63 61 63 68 65 28 69 6e 74 20 65 6e 61 62 6c _cache(int enabl
1406b 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f e){. sqlite3Glo
1406c 62 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 balConfig.shared
1406d 43 61 63 68 65 45 6e 61 62 6c 65 64 20 3d 20 65 CacheEnabled = e
1406e 6e 61 62 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 nable;. return
1406f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e SQLITE_OK;.}.#en
14070 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 dif.../*.** Forw
14071 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a ard declaration.
14072 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 */.static int ch
14073 65 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69 eckForReadConfli
14074 63 74 73 28 42 74 72 65 65 2a 2c 20 50 67 6e 6f cts(Btree*, Pgno
14075 2c 20 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 , BtCursor*, i64
14076 29 3b 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 );...#ifdef SQLI
14077 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
14078 41 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 ACHE. /*. ** T
14079 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 he functions que
1407a 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
1407b 6c 65 4c 6f 63 6b 28 29 2c 20 73 65 74 53 68 61 leLock(), setSha
1407c 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
1407d 6b 28 29 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c k(),. ** and cl
1407e 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 earAllSharedCach
1407f 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 eTableLocks().
14080 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e ** manipulate en
14081 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 74 53 tries in the BtS
14082 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b hared.pLock link
14083 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 6f 20 ed list used to
14084 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 72 65 store. ** share
14085 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 6c 65 d-cache table le
14086 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68 vel locks. If th
14087 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d e library is com
14088 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 20 piled with the.
14089 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 ** shared-cache
1408a 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c 65 feature disable
1408b 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 d, then there is
1408c 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 20 75 only ever one u
1408d 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 ser. ** of each
1408e 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 BtShared struct
1408f 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 73 20 ure and so this
14090 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e locking is not n
14091 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20 ecessary. . **
14092 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f So define the lo
14093 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e 63 74 ck related funct
14094 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a ions as no-ops..
14095 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 71 */. #define q
14096 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 uerySharedCacheT
14097 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 ableLock(a,b,c)
14098 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 SQLITE_OK. #def
14099 69 6e 65 20 73 65 74 53 68 61 72 65 64 43 61 63 ine setSharedCac
1409a 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c heTableLock(a,b,
1409b 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 c) SQLITE_OK. #
1409c 64 65 66 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53 define clearAllS
1409d 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
1409e 6f 63 6b 73 28 61 29 0a 23 65 6e 64 69 66 0a 0a ocks(a).#endif..
1409f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
140a0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
140a1 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 ./*.** Query to
140a2 73 65 65 20 69 66 20 62 74 72 65 65 20 68 61 6e see if btree han
140a3 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 61 69 6e dle p may obtain
140a4 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 a lock of type
140a5 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f eLock .** (READ_
140a6 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f LOCK or WRITE_LO
140a7 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 CK) on the table
140a8 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 with root-page
140a9 69 54 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 iTab. Return.**
140aa 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 SQLITE_OK if the
140ab 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 lock may be obt
140ac 61 69 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e ained (by callin
140ad 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 64 43 61 g.** setSharedCa
140ae 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c cheTableLock()),
140af 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 or SQLITE_LOCKE
140b0 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 D if not..*/.sta
140b1 74 69 63 20 69 6e 74 20 71 75 65 72 79 53 68 61 tic int querySha
140b2 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
140b3 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f k(Btree *p, Pgno
140b4 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29 iTab, u8 eLock)
140b5 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
140b6 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 t = p->pBt;. Bt
140b7 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 Lock *pIter;..
140b8 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
140b9 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
140ba 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 ) );. assert( e
140bb 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 Lock==READ_LOCK
140bc 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f || eLock==WRITE_
140bd 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
140be 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 ( p->db!=0 );.
140bf 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 . /* This is a
140c0 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 no-op if the sha
140c1 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 red-cache is not
140c2 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 enabled */. if
140c3 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 ( !p->sharable )
140c4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
140c5 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f ITE_OK;. }.. /
140c6 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 * If some other
140c7 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f connection is ho
140c8 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 lding an exclusi
140c9 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a ve lock, the. *
140ca 2a 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b * requested lock
140cb 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 may not be obta
140cc 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ined.. */. if(
140cd 20 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 pBt->pWriter!=p
140ce 20 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 && pBt->isExclu
140cf 73 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 sive ){. sqli
140d0 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f te3ConnectionBlo
140d1 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d cked(p->db, pBt-
140d2 3e 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 >pWriter->db);.
140d3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
140d4 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
140d5 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 CHE;. }.. /* T
140d6 68 69 73 20 28 61 6c 6f 6e 67 20 77 69 74 68 20 his (along with
140d7 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 setSharedCacheTa
140d8 62 6c 65 4c 6f 63 6b 28 29 29 20 69 73 20 77 68 bleLock()) is wh
140d9 65 72 65 0a 20 20 2a 2a 20 74 68 65 20 52 65 61 ere. ** the Rea
140da 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 dUncommitted fla
140db 67 20 69 73 20 64 65 61 6c 74 20 77 69 74 68 2e g is dealt with.
140dc 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 63 61 6c . ** If the cal
140dd 6c 65 72 20 69 73 20 71 75 65 72 79 69 6e 67 20 ler is querying
140de 66 6f 72 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 for a read-lock
140df 6f 6e 20 61 6e 79 20 74 61 62 6c 65 0a 20 20 2a on any table. *
140e0 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 * other than the
140e1 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
140e2 61 62 6c 65 20 28 74 61 62 6c 65 20 31 29 20 61 able (table 1) a
140e3 6e 64 20 69 66 20 74 68 65 20 52 65 61 64 55 6e nd if the ReadUn
140e4 63 6f 6d 6d 69 74 74 65 64 0a 20 20 2a 2a 20 66 committed. ** f
140e5 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e lag is set, then
140e6 20 74 68 65 20 6c 6f 63 6b 20 67 72 61 6e 74 65 the lock grante
140e7 64 20 65 76 65 6e 20 69 66 20 74 68 65 72 65 20 d even if there
140e8 61 72 65 20 77 72 69 74 65 2d 6c 6f 63 6b 73 0a are write-locks.
140e9 20 20 2a 2a 20 6f 6e 20 74 68 65 20 74 61 62 6c ** on the tabl
140ea 65 2e 20 49 66 20 61 20 77 72 69 74 65 2d 6c 6f e. If a write-lo
140eb 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 2c ck is requested,
140ec 20 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 the ReadUncommi
140ed 74 74 65 64 20 66 6c 61 67 0a 20 20 2a 2a 20 69 tted flag. ** i
140ee 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 s not considered
140ef 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 66 .. **. ** In f
140f0 75 6e 63 74 69 6f 6e 20 73 65 74 53 68 61 72 65 unction setShare
140f1 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
140f2 29 2c 20 69 66 20 61 20 72 65 61 64 2d 6c 6f 63 ), if a read-loc
140f3 6b 20 69 73 20 64 65 6d 61 6e 64 65 64 20 61 6e k is demanded an
140f4 64 20 74 68 65 20 0a 20 20 2a 2a 20 52 65 61 64 d the . ** Read
140f5 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 Uncommitted flag
140f6 20 69 73 20 73 65 74 2c 20 6e 6f 20 65 6e 74 72 is set, no entr
140f7 79 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 y is added to th
140f8 65 20 6c 6f 63 6b 73 20 6c 69 73 74 20 0a 20 20 e locks list .
140f9 2a 2a 20 28 42 74 53 68 61 72 65 64 2e 70 4c 6f ** (BtShared.pLo
140fa 63 6b 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ck).. **. ** T
140fb 6f 20 73 75 6d 6d 61 72 69 7a 65 3a 20 49 66 20 o summarize: If
140fc 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 the ReadUncommit
140fd 74 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 2c ted flag is set,
140fe 20 74 68 65 6e 20 72 65 61 64 20 63 75 72 73 6f then read curso
140ff 72 73 0a 20 20 2a 2a 20 6f 6e 20 6e 6f 6e 2d 73 rs. ** on non-s
14100 63 68 65 6d 61 20 74 61 62 6c 65 73 20 64 6f 20 chema tables do
14101 6e 6f 74 20 63 72 65 61 74 65 20 6f 72 20 72 65 not create or re
14102 73 70 65 63 74 20 74 61 62 6c 65 20 6c 6f 63 6b spect table lock
14103 73 2e 20 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 20 s. The locking.
14104 20 2a 2a 20 70 72 6f 63 65 64 75 72 65 20 66 6f ** procedure fo
14105 72 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 r a write-cursor
14106 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 does not change
14107 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20 20 .. */. if( .
14108 20 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 0==(p->db->fla
14109 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e gs&SQLITE_ReadUn
1410a 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20 0a 20 committed) || .
1410b 20 20 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f eLock==WRITE_
1410c 4c 4f 43 4b 20 7c 7c 0a 20 20 20 20 69 54 61 62 LOCK ||. iTab
1410d 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a 20 20 ==MASTER_ROOT.
1410e 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 ){. for(pIter
1410f 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 =pBt->pLock; pIt
14110 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d er; pIter=pIter-
14111 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 >pNext){. i
14112 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 f( pIter->pBtree
14113 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 54 !=p && pIter->iT
14114 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 0a 20 able==iTab && .
14115 20 20 20 20 20 20 20 20 20 28 70 49 74 65 72 2d (pIter-
14116 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20 7c 7c >eLock!=eLock ||
14117 20 65 4c 6f 63 6b 21 3d 52 45 41 44 5f 4c 4f 43 eLock!=READ_LOC
14118 4b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 K) ){. sq
14119 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
1411a 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 locked(p->db, pI
1411b 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 ter->pBtree->db)
1411c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 4c ;. if( eL
1411d 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
1411e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 ){. ass
1411f 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72 ert( p==pBt->pWr
14120 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20 iter );.
14121 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 pBt->isPending
14122 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
14123 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
14124 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
14125 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 20 20 REDCACHE;.
14126 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
14127 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
14128 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
14129 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1412a 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
1412b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
1412c 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
1412d 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 Add a lock on t
1412e 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f he table with ro
1412f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 ot-page iTable t
14130 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 o the shared-btr
14131 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 ee used.** by Bt
14132 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 ree handle p. Pa
14133 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 rameter eLock mu
14134 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41 st be either REA
14135 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 D_LOCK or .** WR
14136 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 ITE_LOCK..**.**
14137 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
14138 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 urned if the loc
14139 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65 k is added succe
1413a 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f ssfully. SQLITE_
1413b 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 BUSY and.** SQLI
1413c 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73 TE_NOMEM may als
1413d 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a o be returned..*
1413e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 /.static int set
1413f 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
14140 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 Lock(Btree *p, P
14141 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 gno iTable, u8 e
14142 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 Lock){. BtShare
14143 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
14144 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b . BtLock *pLock
14145 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a = 0;. BtLock *
14146 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 pIter;.. assert
14147 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
14148 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
14149 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
1414a 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f READ_LOCK || eLo
1414b 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 ck==WRITE_LOCK )
1414c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 ;. assert( p->d
1414d 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 b!=0 );.. /* Th
1414e 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 is is a no-op if
1414f 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 the shared-cach
14150 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 e is not enabled
14151 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 */. if( !p->sh
14152 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 arable ){. re
14153 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
14154 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 53 }.. assert( S
14155 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 QLITE_OK==queryS
14156 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
14157 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 ock(p, iTable, e
14158 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 Lock) );.. /* I
14159 66 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d f the read-uncom
1415a 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 20 73 mitted flag is s
1415b 65 74 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f et and a read-lo
1415c 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 20 ck is requested
1415d 6f 6e 0a 20 20 2a 2a 20 61 20 6e 6f 6e 2d 73 63 on. ** a non-sc
1415e 68 65 6d 61 20 74 61 62 6c 65 2c 20 74 68 65 6e hema table, then
1415f 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 6c 77 the lock is alw
14160 61 79 73 20 67 72 61 6e 74 65 64 2e 20 20 52 65 ays granted. Re
14161 74 75 72 6e 20 65 61 72 6c 79 0a 20 20 2a 2a 20 turn early. **
14162 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20 61 without adding a
14163 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 42 n entry to the B
14164 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 tShared.pLock li
14165 73 74 2e 20 53 65 65 0a 20 20 2a 2a 20 63 6f 6d st. See. ** com
14166 6d 65 6e 74 20 69 6e 20 66 75 6e 63 74 69 6f 6e ment in function
14167 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 querySharedCach
14168 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66 6f 72 eTableLock() for
14169 20 6d 6f 72 65 20 69 6e 66 6f 0a 20 20 2a 2a 20 more info. **
1416a 6f 6e 20 68 61 6e 64 6c 69 6e 67 20 74 68 65 20 on handling the
1416b 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 ReadUncommitted
1416c 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 flag.. */. if(
1416d 20 0a 20 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c . (p->db->fl
1416e 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 ags&SQLITE_ReadU
1416f 6e 63 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a ncommitted) && .
14170 20 20 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 (eLock==READ
14171 5f 4c 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54 _LOCK) &&. iT
14172 61 62 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f able!=MASTER_ROO
14173 54 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 T. ){. retur
14174 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
14175 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 .. /* First sea
14176 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 rch the list for
14177 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 an existing loc
14178 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e k on this table.
14179 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d */. for(pIter=
1417a 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 pBt->pLock; pIte
1417b 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e r; pIter=pIter->
1417c 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
1417d 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 pIter->iTable==i
1417e 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e Table && pIter->
1417f 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 pBtree==p ){.
14180 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 pLock = pIter
14181 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
14182 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
14183 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 f the above sear
14184 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 ch did not find
14185 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 a BtLock struct
14186 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 associating Btre
14187 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 e p. ** with ta
14188 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f ble iTable, allo
14189 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e cate one and lin
1418a 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 k it into the li
1418b 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 st.. */. if( !
1418c 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f pLock ){. pLo
1418d 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 ck = (BtLock *)s
1418e 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
1418f 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 (sizeof(BtLock))
14190 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b ;. if( !pLock
14191 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
14192 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
14193 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e }. pLock->
14194 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b iTable = iTable;
14195 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 . pLock->pBtr
14196 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 ee = p;. pLoc
14197 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e k->pNext = pBt->
14198 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e pLock;. pBt->
14199 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 pLock = pLock;.
1419a 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 }.. /* Set the
1419b 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 BtLock.eLock va
1419c 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 riable to the ma
1419d 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 ximum of the cur
1419e 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 rent lock. ** a
1419f 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 65 64 nd the requested
141a0 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e lock. This mean
141a1 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 s if a write-loc
141a2 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 k was already he
141a3 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 ld. ** and a re
141a4 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 ad-lock requeste
141a5 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f d, we don't inco
141a6 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 rrectly downgrad
141a7 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f e the lock.. */
141a8 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 54 45 . assert( WRITE
141a9 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 _LOCK>READ_LOCK
141aa 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 );. if( eLock>p
141ab 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 Lock->eLock ){.
141ac 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 pLock->eLock
141ad 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 = eLock;. }..
141ae 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
141af 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 ;.}.#endif /* !S
141b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
141b1 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e D_CACHE */..#ifn
141b2 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
141b3 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a SHARED_CACHE./*.
141b4 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 ** Release all t
141b5 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 he table locks (
141b6 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 locks obtained v
141b7 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 ia calls to.** t
141b8 68 65 20 73 65 74 53 68 61 72 65 64 43 61 63 68 he setSharedCach
141b9 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f eTableLock() pro
141ba 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 cedure) held by
141bb 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a Btree handle p..
141bc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
141bd 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 learAllSharedCac
141be 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 heTableLocks(Btr
141bf 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 ee *p){. BtShar
141c0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
141c1 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 ;. BtLock **ppI
141c2 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 ter = &pBt->pLoc
141c3 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 k;.. assert( sq
141c4 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
141c5 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 utex(p) );. ass
141c6 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 ert( p->sharable
141c7 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 || 0==*ppIter )
141c8 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 70 70 49 ;.. while( *ppI
141c9 74 65 72 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 ter ){. BtLoc
141ca 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 74 k *pLock = *ppIt
141cb 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 er;. assert(
141cc 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 pBt->isExclusive
141cd 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 ==0 || pBt->pWri
141ce 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 74 72 ter==pLock->pBtr
141cf 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c ee );. if( pL
141d0 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 ock->pBtree==p )
141d1 7b 0a 20 20 20 20 20 20 2a 70 70 49 74 65 72 20 {. *ppIter
141d2 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a = pLock->pNext;.
141d3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
141d4 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d ee(pLock);. }
141d5 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 70 49 74 else{. ppIt
141d6 65 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e 70 4e 65 er = &pLock->pNe
141d7 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 xt;. }. }..
141d8 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73 assert( pBt->is
141d9 50 65 6e 64 69 6e 67 3d 3d 30 20 7c 7c 20 70 42 Pending==0 || pB
141da 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20 t->pWriter );.
141db 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 if( pBt->pWriter
141dc 3d 3d 70 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e ==p ){. pBt->
141dd 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 pWriter = 0;.
141de 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 pBt->isExclusiv
141df 65 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e e = 0;. pBt->
141e0 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 isPending = 0;.
141e1 20 7d 65 6c 73 65 20 69 66 28 20 70 42 74 2d 3e }else if( pBt->
141e2 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 32 20 nTransaction==2
141e3 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66 ){. /* This f
141e4 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
141e5 64 20 77 68 65 6e 20 63 6f 6e 6e 65 63 74 69 6f d when connectio
141e6 6e 20 70 20 69 73 20 63 6f 6e 63 6c 75 64 69 6e n p is concludin
141e7 67 20 69 74 73 20 0a 20 20 20 20 2a 2a 20 74 72 g its . ** tr
141e8 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 74 68 ansaction. If th
141e9 65 72 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 ere currently ex
141ea 69 73 74 73 20 61 20 77 72 69 74 65 72 2c 20 61 ists a writer, a
141eb 6e 64 20 70 20 69 73 20 6e 6f 74 0a 20 20 20 20 nd p is not.
141ec 2a 2a 20 74 68 61 74 20 77 72 69 74 65 72 2c 20 ** that writer,
141ed 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 then the number
141ee 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 of locks held by
141ef 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 74 68 connections oth
141f0 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 er. ** than t
141f1 68 65 20 77 72 69 74 65 72 20 6d 75 73 74 20 62 he writer must b
141f2 65 20 61 62 6f 75 74 20 74 6f 20 64 72 6f 70 20 e about to drop
141f3 74 6f 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 to zero. In this
141f4 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 73 65 74 case. ** set
141f5 20 74 68 65 20 69 73 50 65 6e 64 69 6e 67 20 66 the isPending f
141f6 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20 20 2a 2a lag to 0.. **
141f7 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 . ** If there
141f8 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
141f9 79 20 61 20 77 72 69 74 65 72 2c 20 74 68 65 6e y a writer, then
141fa 20 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e 64 BtShared.isPend
141fb 69 6e 67 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 ing must. **
141fc 62 65 20 7a 65 72 6f 20 61 6c 72 65 61 64 79 2e be zero already.
141fd 20 53 6f 20 74 68 69 73 20 6e 65 78 74 20 6c 69 So this next li
141fe 6e 65 20 69 73 20 68 61 72 6d 6c 65 73 73 20 69 ne is harmless i
141ff 6e 20 74 68 61 74 20 63 61 73 65 2e 0a 20 20 20 n that case..
14200 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 69 73 50 */. pBt->isP
14201 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 7d 0a ending = 0;. }.
14202 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
14203 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
14204 41 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 ACHE */..static
14205 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 void releasePage
14206 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
14207 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 ; /* Forward re
14208 66 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a ference */../*.*
14209 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 * Verify that th
1420a 65 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 61 e cursor holds a
1420b 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 mutex on the Bt
1420c 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 6e 64 65 Shared.*/.#ifnde
1420d 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 f NDEBUG.static
1420e 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d int cursorHoldsM
1420f 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 utex(BtCursor *p
14210 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
14211 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14212 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d ->pBt->mutex);.}
14213 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 .#endif...#ifnde
14214 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
14215 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 CRBLOB./*.** Inv
14216 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 alidate the over
14217 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 flow page-list c
14218 61 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 20 ache for cursor
14219 70 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f pCur, if any..*/
1421a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 .static void inv
1421b 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 alidateOverflowC
1421c 61 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a 70 ache(BtCursor *p
1421d 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Cur){. assert(
1421e 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
1421f 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 (pCur) );. sqli
14220 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 te3_free(pCur->a
14221 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 Overflow);. pCu
14222 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 r->aOverflow = 0
14223 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c ;.}../*.** Inval
14224 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c idate the overfl
14225 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 ow page-list cac
14226 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f he for all curso
14227 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 rs opened.** on
14228 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 the shared btree
14229 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a structure pBt..
1422a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 */.static void i
1422b 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 nvalidateAllOver
1422c 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 72 flowCache(BtShar
1422d 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 ed *pBt){. BtCu
1422e 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 rsor *p;. asser
1422f 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14230 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
14231 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 ) );. for(p=pBt
14232 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d ->pCursor; p; p=
14233 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 p->pNext){. i
14234 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f nvalidateOverflo
14235 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d wCache(p);. }.}
14236 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
14237 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 invalidateOverf
14238 6c 6f 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 lowCache(x). #d
14239 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 efine invalidate
1423a 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 AllOverflowCache
1423b 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (x).#endif../*.*
1423c 2a 20 53 65 74 20 62 69 74 20 70 67 6e 6f 20 6f * Set bit pgno o
1423d 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 f the BtShared.p
1423e 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 HasContent bitve
1423f 63 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 c. This is calle
14240 64 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70 61 67 d .** when a pag
14241 65 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 6c e that previousl
14242 79 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 74 61 y contained data
14243 20 62 65 63 6f 6d 65 73 20 61 20 66 72 65 65 2d becomes a free-
14244 6c 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20 70 61 list leaf .** pa
14245 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 74 ge..**.** The Bt
14246 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 Shared.pHasConte
14247 6e 74 20 62 69 74 76 65 63 20 65 78 69 73 74 73 nt bitvec exists
14248 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 to work around
14249 61 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20 62 75 an obscure.** bu
1424a 67 20 63 61 75 73 65 64 20 62 79 20 74 68 65 20 g caused by the
1424b 69 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66 20 74 interaction of t
1424c 77 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f 70 74 wo useful IO opt
1424d 69 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72 72 6f imizations surro
1424e 75 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65 2d 6c unding.** free-l
1424f 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 3a 0a ist leaf pages:.
14250 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 **.** 1) When
14251 61 6c 6c 20 64 61 74 61 20 69 73 20 64 65 6c 65 all data is dele
14252 74 65 64 20 66 72 6f 6d 20 61 20 70 61 67 65 20 ted from a page
14253 61 6e 64 20 74 68 65 20 70 61 67 65 20 62 65 63 and the page bec
14254 6f 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61 20 66 omes.** a f
14255 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 ree-list leaf pa
14256 67 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 20 ge, the page is
14257 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 not written to t
14258 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 he database.**
14259 20 20 20 20 28 61 73 20 66 72 65 65 2d 6c 69 73 (as free-lis
1425a 74 20 6c 65 61 66 20 70 61 67 65 73 20 63 6f 6e t leaf pages con
1425b 74 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 66 tain no meaningf
1425c 75 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65 74 69 ul data). Someti
1425d 6d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75 63 68 mes.** such
1425e 20 61 20 70 61 67 65 20 69 73 20 6e 6f 74 20 65 a page is not e
1425f 76 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 28 ven journalled (
14260 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 as it will not b
14261 65 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 e modified,.**
14262 20 20 20 20 77 68 79 20 62 6f 74 68 65 72 20 6a why bother j
14263 6f 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 2e ournalling it?).
14264 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e .**.** 2) When
14265 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 a free-list lea
14266 66 20 70 61 67 65 20 69 73 20 72 65 75 73 65 64 f page is reused
14267 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 69 73 , its content is
14268 20 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20 20 20 not read.**
14269 20 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 from the datab
1426a 61 73 65 20 6f 72 20 77 72 69 74 74 65 6e 20 74 ase or written t
1426b 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
1426c 6c 65 20 28 77 68 79 20 73 68 6f 75 6c 64 20 69 le (why should i
1426d 74 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 69 66 t.** be, if
1426e 20 69 74 20 69 73 20 6e 6f 74 20 61 74 20 61 6c it is not at al
1426f 6c 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0a l meaningful?)..
14270 2a 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73 65 6c **.** By themsel
14271 76 65 73 2c 20 74 68 65 73 65 20 6f 70 74 69 6d ves, these optim
14272 69 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 66 69 izations work fi
14273 6e 65 20 61 6e 64 20 70 72 6f 76 69 64 65 20 61 ne and provide a
14274 20 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66 6f 72 handy.** perfor
14275 6d 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f 20 62 mance boost to b
14276 75 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20 69 6e ulk delete or in
14277 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e sert operations.
14278 20 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a 2a 20 However, if.**
14279 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 20 a page is moved
1427a 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 to the free-list
1427b 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64 and then reused
1427c 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 within the same
1427d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c .** transaction,
1427e 20 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 73 a problem comes
1427f 20 75 70 2e 20 49 66 20 74 68 65 20 70 61 67 65 up. If the page
14280 20 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c is not journall
14281 65 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20 69 73 ed when.** it is
14282 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 moved to the fr
14283 65 65 2d 6c 69 73 74 20 61 6e 64 20 69 74 20 69 ee-list and it i
14284 73 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 6e s also not journ
14285 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a 2a 2a alled when it.**
14286 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72 is extracted fr
14287 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 om the free-list
14288 20 61 6e 64 20 72 65 75 73 65 64 2c 20 74 68 65 and reused, the
14289 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 n the original d
1428a 61 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f ata.** may be lo
1428b 73 74 2e 20 49 6e 20 74 68 65 20 65 76 65 6e 74 st. In the event
1428c 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 of a rollback,
1428d 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 6f it may not be po
1428e 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 73 ssible.** to res
1428f 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 tore the databas
14290 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 e to its origina
14291 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e l configuration.
14292 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 74 .**.** The solut
14293 69 6f 6e 20 69 73 20 74 68 65 20 42 74 53 68 61 ion is the BtSha
14294 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 red.pHasContent
14295 62 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72 bitvec. Whenever
14296 20 61 20 70 61 67 65 20 69 73 20 0a 2a 2a 20 6d a page is .** m
14297 6f 76 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 oved to become a
14298 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
14299 70 61 67 65 2c 20 74 68 65 20 63 6f 72 72 65 73 page, the corres
1429a 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 0a 2a ponding bit is.*
1429b 2a 20 73 65 74 20 69 6e 20 74 68 65 20 62 69 74 * set in the bit
1429c 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 vec. Whenever a
1429d 6c 65 61 66 20 70 61 67 65 20 69 73 20 65 78 74 leaf page is ext
1429e 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 racted from the
1429f 66 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f 70 free-list,.** op
142a0 74 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f timization 2 abo
142a1 76 65 20 69 73 20 6f 6d 6d 69 74 74 65 64 20 69 ve is ommitted i
142a2 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 f the correspond
142a3 69 6e 67 20 62 69 74 20 69 73 20 61 6c 72 65 61 ing bit is alrea
142a4 64 79 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74 53 dy.** set in BtS
142a5 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e hared.pHasConten
142a6 74 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 t. The contents
142a7 6f 66 20 74 68 65 20 62 69 74 76 65 63 20 61 72 of the bitvec ar
142a8 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74 20 e cleared.** at
142a9 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 the end of every
142aa 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f transaction..*/
142ab 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
142ac 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 eSetHasContent(B
142ad 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 tShared *pBt, Pg
142ae 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 no pgno){. int
142af 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
142b0 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 if( !pBt->pHas
142b1 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 69 Content ){. i
142b2 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 72 63 nt nPage;. rc
142b3 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 = sqlite3PagerP
142b4 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 agecount(pBt->pP
142b5 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 ager, &nPage);.
142b6 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
142b7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 42 E_OK ){. pB
142b8 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d t->pHasContent =
142b9 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 sqlite3BitvecCr
142ba 65 61 74 65 28 28 75 33 32 29 6e 50 61 67 65 29 eate((u32)nPage)
142bb 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 42 74 ;. if( !pBt
142bc 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 29 7b ->pHasContent ){
142bd 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
142be 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
142bf 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
142c0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
142c1 4b 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 K && pgno<=sqlit
142c2 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 74 e3BitvecSize(pBt
142c3 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 ->pHasContent) )
142c4 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
142c5 65 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d e3BitvecSet(pBt-
142c6 3e 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 >pHasContent, pg
142c7 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 no);. }. retur
142c8 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 n rc;.}../*.** Q
142c9 75 65 72 79 20 74 68 65 20 42 74 53 68 61 72 65 uery the BtShare
142ca 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 d.pHasContent ve
142cb 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ctor..**.** This
142cc 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
142cd 6c 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 2d led when a free-
142ce 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69 list leaf page i
142cf 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 s removed from t
142d0 68 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 he.** free-list
142d1 66 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 65 for reuse. It re
142d2 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 69 turns false if i
142d3 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 74 t is safe to ret
142d4 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 rieve the.** pag
142d5 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 e from the pager
142d6 20 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20 layer with the
142d7 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 'no-content' fla
142d8 67 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 65 g set. True othe
142d9 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 rwise..*/.static
142da 20 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 73 int btreeGetHas
142db 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 Content(BtShared
142dc 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f *pBt, Pgno pgno
142dd 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 3d ){. Bitvec *p =
142de 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e pBt->pHasConten
142df 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 t;. return (p &
142e0 26 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 & (pgno>sqlite3B
142e1 69 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 itvecSize(p) ||
142e2 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
142e3 74 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a t(p, pgno)));.}.
142e4 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 ./*.** Clear (de
142e5 73 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 61 stroy) the BtSha
142e6 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 red.pHasContent
142e7 62 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 6f bitvec. This sho
142e8 75 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 uld be.** invoke
142e9 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 d at the conclus
142ea 69 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 74 ion of each writ
142eb 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e-transaction..*
142ec 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 /.static void bt
142ed 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 reeClearHasConte
142ee 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 nt(BtShared *pBt
142ef 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 ){. sqlite3Bitv
142f0 65 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 ecDestroy(pBt->p
142f1 48 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 HasContent);. p
142f2 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 Bt->pHasContent
142f3 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 = 0;.}../*.** Sa
142f4 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 ve the current c
142f5 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 ursor position i
142f6 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 n the variables
142f7 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a BtCursor.nKey .*
142f8 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 * and BtCursor.p
142f9 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 Key. The cursor'
142fa 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 s state is set t
142fb 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 o CURSOR_REQUIRE
142fc 53 45 45 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 SEEK..*/.static
142fd 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f int saveCursorPo
142fe 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 sition(BtCursor
142ff 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 *pCur){. int rc
14300 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 ;.. assert( CUR
14301 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d SOR_VALID==pCur-
14302 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73 >eState );. ass
14303 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b ert( 0==pCur->pK
14304 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ey );. assert(
14305 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
14306 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 20 (pCur) );.. rc
14307 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 = sqlite3BtreeKe
14308 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 75 ySize(pCur, &pCu
14309 72 2d 3e 6e 4b 65 79 29 3b 0a 0a 20 20 2f 2a 20 r->nKey);.. /*
1430a 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e If this is an in
1430b 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e tKey table, then
1430c 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c 6c 20 the above call
1430d 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a 65 28 to BtreeKeySize(
1430e 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 ). ** stores th
1430f 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69 6e e integer key in
14310 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 pCur->nKey. In
14311 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20 76 this case this v
14312 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c alue is. ** all
14313 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 that is require
14314 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 d. Otherwise, if
14315 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 65 pCur is not ope
14316 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20 n on an intKey.
14317 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 ** table, then
14318 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f 72 malloc space for
14319 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 70 and store the p
1431a 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20 Cur->nKey bytes
1431b 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74 of key . ** dat
1431c 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 a.. */. if( rc
1431d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 ==SQLITE_OK && 0
1431e 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 ==pCur->apPage[0
1431f 5d 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20 20 20 ]->intKey){.
14320 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c void *pKey = sql
14321 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 ite3Malloc( (int
14322 29 70 43 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 )pCur->nKey );.
14323 20 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 if( pKey ){.
14324 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
14325 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 3BtreeKey(pCur,
14326 30 2c 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 0, (int)pCur->nK
14327 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 20 ey, pKey);.
14328 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
14329 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 OK ){. pC
1432a 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b ur->pKey = pKey;
1432b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1432c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
1432d 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 ee(pKey);.
1432e 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1432f 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
14330 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a OMEM;. }. }.
14331 20 20 61 73 73 65 72 74 28 20 21 70 43 75 72 2d assert( !pCur-
14332 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b >apPage[0]->intK
14333 65 79 20 7c 7c 20 21 70 43 75 72 2d 3e 70 4b 65 ey || !pCur->pKe
14334 79 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d y );.. if( rc==
14335 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
14336 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 int i;. for(
14337 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 i=0; i<=pCur->iP
14338 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 age; i++){.
14339 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 releasePage(pCu
1433a 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 r->apPage[i]);.
1433b 20 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 pCur->apPag
1433c 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a e[i] = 0;. }.
1433d 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 pCur->iPage
1433e 3d 20 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e = -1;. pCur->
1433f 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
14340 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20 20 7d REQUIRESEEK;. }
14341 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 .. invalidateOv
14342 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 erflowCache(pCur
14343 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
14344 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 }../*.** Save th
14345 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 e positions of a
14346 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 63 65 70 ll cursors excep
14347 74 20 70 45 78 63 65 70 74 20 6f 70 65 6e 20 6f t pExcept open o
14348 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20 n the table .**
14349 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 with root-page i
1434a 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79 2c 20 74 Root. Usually, t
1434b 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6a 75 his is called ju
1434c 73 74 20 62 65 66 6f 72 65 20 63 75 72 73 6f 72 st before cursor
1434d 0a 2a 2a 20 70 45 78 63 65 70 74 20 69 73 20 75 .** pExcept is u
1434e 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 sed to modify th
1434f 65 20 74 61 62 6c 65 20 28 42 74 72 65 65 44 65 e table (BtreeDe
14350 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65 65 49 lete() or BtreeI
14351 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a 73 74 61 nsert())..*/.sta
14352 74 69 63 20 69 6e 74 20 73 61 76 65 41 6c 6c 43 tic int saveAllC
14353 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 20 ursors(BtShared
14354 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 *pBt, Pgno iRoot
14355 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 45 78 63 , BtCursor *pExc
14356 65 70 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 ept){. BtCursor
14357 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 *p;. assert( s
14358 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14359 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1435a 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 63 65 . assert( pExce
1435b 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 65 70 74 pt==0 || pExcept
1435c 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 20 20 ->pBt==pBt );.
1435d 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 for(p=pBt->pCurs
1435e 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 or; p; p=p->pNex
1435f 74 29 7b 0a 20 20 20 20 69 66 28 20 70 21 3d 70 t){. if( p!=p
14360 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d 69 52 Except && (0==iR
14361 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f oot || p->pgnoRo
14362 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26 20 0a 20 ot==iRoot) && .
14363 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 p->eState
14364 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
14365 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d {. int rc =
14366 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 saveCursorPosit
14367 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20 69 66 ion(p);. if
14368 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 ( SQLITE_OK!=rc
14369 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
1436a 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
1436b 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1436c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1436d 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 *.** Clear the c
1436e 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f urrent cursor po
1436f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 sition..*/.SQLIT
14370 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
14371 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
14372 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 Cursor(BtCursor
14373 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 *pCur){. assert
14374 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
14375 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 ex(pCur) );. sq
14376 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d lite3_free(pCur-
14377 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75 72 2d 3e >pKey);. pCur->
14378 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70 43 75 72 pKey = 0;. pCur
14379 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
1437a 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a R_INVALID;.}../*
1437b 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 .** Restore the
1437c 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 70 6f cursor to the po
1437d 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 69 6e sition it was in
1437e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 74 6f (or as close to
1437f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a 2a as possible).**
14380 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 6f 72 when saveCursor
14381 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 20 63 Position() was c
14382 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 alled. Note that
14383 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c 65 74 this call delet
14384 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 65 64 es the .** saved
14385 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20 73 position info s
14386 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 75 72 tored by saveCur
14387 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20 73 sorPosition(), s
14388 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a o there can be.*
14389 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 66 * at most one ef
1438a 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 65 43 fective restoreC
1438b 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 ursorPosition()
1438c 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 68 20 call after each
1438d 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 50 6f .** saveCursorPo
1438e 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 53 51 4c sition()..*/.SQL
1438f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
14390 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 73 74 sqlite3BtreeRest
14391 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
14392 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 n(BtCursor *pCur
14393 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 ){. int rc;. a
14394 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
14395 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
14396 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
14397 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f >eState>=CURSOR_
14398 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 REQUIRESEEK );.
14399 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
1439a 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e==CURSOR_FAULT
1439b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43 ){. return pC
1439c 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a 20 20 ur->skip;. }.
1439d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
1439e 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 URSOR_INVALID;.
1439f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
143a0 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 eeMoveto(pCur, p
143a1 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d Cur->pKey, pCur-
143a2 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d >nKey, 0, &pCur-
143a3 3e 73 6b 69 70 29 3b 0a 20 20 69 66 28 20 72 63 >skip);. if( rc
143a4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
143a5 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
143a6 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 pCur->pKey);.
143a7 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b pCur->pKey = 0;
143a8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
143a9 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
143aa 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d R_VALID || pCur-
143ab 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
143ac 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20 INVALID );. }.
143ad 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
143ae 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65 43 75 define restoreCu
143af 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 rsorPosition(p)
143b0 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 65 3e 3d \. (p->eState>=
143b1 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
143b2 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 20 20 20 EK ? \.
143b3 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 73 74 sqlite3BtreeRest
143b4 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
143b5 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 n(p) : \.
143b6 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a SQLITE_OK)../*
143b7 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 .** Determine wh
143b8 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 ether or not a c
143b9 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 ursor has moved
143ba 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 69 6f from the positio
143bb 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 n it.** was last
143bc 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43 75 72 placed at. Cur
143bd 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 sors can move wh
143be 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 20 en the row they
143bf 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 are pointing.**
143c0 61 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 75 at is deleted ou
143c1 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 t from under the
143c2 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f m..**.** This ro
143c3 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 6e utine returns an
143c4 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 73 error code if s
143c5 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 omething goes wr
143c6 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74 ong. The.** int
143c7 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 eger *pHasMoved
143c8 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 is set to one if
143c9 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 the cursor has
143ca 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 66 20 6e moved and 0 if n
143cb 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ot..*/.SQLITE_PR
143cc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
143cd 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 3BtreeCursorHasM
143ce 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20 2a 70 oved(BtCursor *p
143cf 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f Cur, int *pHasMo
143d0 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ved){. int rc;.
143d1 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 . rc = restoreC
143d2 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 ursorPosition(pC
143d3 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b ur);. if( rc ){
143d4 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 . *pHasMoved
143d5 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
143d6 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 rc;. }. if( pC
143d7 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 ur->eState!=CURS
143d8 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 OR_VALID || pCur
143d9 2d 3e 73 6b 69 70 21 3d 30 20 29 7b 0a 20 20 20 ->skip!=0 ){.
143da 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b *pHasMoved = 1;
143db 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 . }else{. *p
143dc 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 20 HasMoved = 0;.
143dd 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
143de 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 E_OK;.}..#ifndef
143df 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
143e0 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 OVACUUM./*.** Gi
143e1 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65 ven a page numbe
143e2 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 64 r of a regular d
143e3 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 65 atabase page, re
143e4 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a turn the page.**
143e5 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
143e6 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
143e7 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
143e8 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 he entry for the
143e9 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 6e .** input page n
143ea 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 umber..*/.static
143eb 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 65 Pgno ptrmapPage
143ec 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 no(BtShared *pBt
143ed 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 , Pgno pgno){.
143ee 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 70 int nPagesPerMap
143ef 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 Page;. Pgno iPt
143f0 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73 73 rMap, ret;. ass
143f1 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
143f2 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
143f3 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73 50 ex) );. nPagesP
143f4 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42 74 erMapPage = (pBt
143f5 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b ->usableSize/5)+
143f6 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 28 1;. iPtrMap = (
143f7 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65 pgno-2)/nPagesPe
143f8 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74 20 rMapPage;. ret
143f9 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 65 = (iPtrMap*nPage
143fa 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20 32 sPerMapPage) + 2
143fb 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 45 ; . if( ret==PE
143fc 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
143fd 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 2b pBt) ){. ret+
143fe 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 +;. }. return
143ff 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 ret;.}../*.** Wr
14400 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 ite an entry int
14401 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 o the pointer ma
14402 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f p..**.** This ro
14403 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74 68 utine updates th
14404 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e e pointer map en
14405 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75 6d try for page num
14406 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 ber 'key'.** so
14407 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f 20 that it maps to
14408 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e 64 type 'eType' and
14409 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d parent page num
1440a 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 20 41 ber 'pgno'..** A
1440b 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 n error code is
1440c 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65 returned if some
1440d 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
1440e 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49 , otherwise SQLI
1440f 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 TE_OK..*/.static
14410 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74 28 42 int ptrmapPut(B
14411 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 tShared *pBt, Pg
14412 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 70 65 no key, u8 eType
14413 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 29 7b 0a , Pgno parent){.
14414 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
14415 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 e; /* The point
14416 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 er map page */.
14417 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 u8 *pPtrmap;
14418 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 /* The pointe
14419 72 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 r map data */.
1441a 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 Pgno iPtrmap;
1441b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
1441c 20 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 map page number
1441d 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 */. int offset
1441e 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 ; /* Offse
1441f 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 t in pointer map
14420 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 page */. int r
14421 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 c;.. assert( sq
14422 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14423 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
14424 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 72 2d /* The master-
14425 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d journal page num
14426 62 65 72 20 6d 75 73 74 20 6e 65 76 65 72 20 62 ber must never b
14427 65 20 75 73 65 64 20 61 73 20 61 20 70 6f 69 6e e used as a poin
14428 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a ter map page */.
14429 20 20 61 73 73 65 72 74 28 20 30 3d 3d 50 54 52 assert( 0==PTR
1442a 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
1442b 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1442c 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 61 73 E(pBt)) );.. as
1442d 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 sert( pBt->autoV
1442e 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 20 6b acuum );. if( k
1442f 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ey==0 ){. ret
14430 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
14431 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 PT_BKPT;. }. i
14432 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f Ptrmap = PTRMAP_
14433 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 PAGENO(pBt, key)
14434 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
14435 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 PagerGet(pBt->pP
14436 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 ager, iPtrmap, &
14437 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 pDbPage);. if(
14438 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
14439 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
1443a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 }. offset = P
1443b 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 TRMAP_PTROFFSET(
1443c 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 iPtrmap, key);.
1443d 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a pPtrmap = (u8 *
1443e 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 )sqlite3PagerGet
1443f 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a Data(pDbPage);..
14440 20 20 69 66 28 20 65 54 79 70 65 21 3d 70 50 74 if( eType!=pPt
14441 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 7c 7c 20 rmap[offset] ||
14442 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 get4byte(&pPtrma
14443 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21 3d 70 61 p[offset+1])!=pa
14444 72 65 6e 74 20 29 7b 0a 20 20 20 20 54 52 41 43 rent ){. TRAC
14445 45 28 28 22 50 54 52 4d 41 50 5f 55 50 44 41 54 E(("PTRMAP_UPDAT
14446 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64 29 5c 6e E: %d->(%d,%d)\n
14447 22 2c 20 6b 65 79 2c 20 65 54 79 70 65 2c 20 70 ", key, eType, p
14448 61 72 65 6e 74 29 29 3b 0a 20 20 20 20 72 63 20 arent));. rc
14449 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1444a 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ite(pDbPage);.
1444b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1444c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 74 _OK ){. pPt
1444d 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65 rmap[offset] = e
1444e 54 79 70 65 3b 0a 20 20 20 20 20 20 70 75 74 34 Type;. put4
1444f 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 byte(&pPtrmap[of
14450 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29 fset+1], parent)
14451 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 ;. }. }.. s
14452 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
14453 28 70 44 62 50 61 67 65 29 3b 0a 20 20 72 65 74 (pDbPage);. ret
14454 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
14455 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79 20 66 Read an entry f
14456 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65 72 20 rom the pointer
14457 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 map..**.** This
14458 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65 76 65 routine retrieve
14459 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 s the pointer ma
1445a 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65 p entry for page
1445b 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e 67 0a 'key', writing.
1445c 2a 2a 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 ** the type and
1445d 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 parent page numb
1445e 65 72 20 74 6f 20 2a 70 45 54 79 70 65 20 61 6e er to *pEType an
1445f 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 65 63 74 d *pPgno respect
14460 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 ively..** An err
14461 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
14462 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 ned if something
14463 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 goes wrong, oth
14464 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b erwise SQLITE_OK
14465 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
14466 70 74 72 6d 61 70 47 65 74 28 42 74 53 68 61 72 ptrmapGet(BtShar
14467 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 ed *pBt, Pgno ke
14468 79 2c 20 75 38 20 2a 70 45 54 79 70 65 2c 20 50 y, u8 *pEType, P
14469 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 gno *pPgno){. D
1446a 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 bPage *pDbPage;
1446b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
1446c 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 map page */. i
1446d 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 nt iPtrmap;
1446e 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 /* Pointer map
1446f 20 70 61 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20 page index */.
14470 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 u8 *pPtrmap;
14471 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d /* Pointer m
14472 61 70 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a ap page data */.
14473 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 int offset;
14474 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f /* Offset o
14475 66 20 65 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74 f entry in point
14476 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20 er map */. int
14477 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 rc;.. assert( s
14478 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14479 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1447a 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54 .. iPtrmap = PT
1447b 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c RMAP_PAGENO(pBt,
1447c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 key);. rc = sq
1447d 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 lite3PagerGet(pB
1447e 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d t->pPager, iPtrm
1447f 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 ap, &pDbPage);.
14480 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 if( rc!=0 ){.
14481 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
14482 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 . pPtrmap = (u8
14483 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
14484 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b etData(pDbPage);
14485 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 .. offset = PTR
14486 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 MAP_PTROFFSET(iP
14487 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61 trmap, key);. a
14488 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d 30 ssert( pEType!=0
14489 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 );. *pEType =
1448a 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b pPtrmap[offset];
1448b 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20 2a . if( pPgno ) *
1448c 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 pPgno = get4byte
1448d 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 (&pPtrmap[offset
1448e 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 +1]);.. sqlite3
1448f 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 PagerUnref(pDbPa
14490 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 ge);. if( *pETy
14491 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e pe<1 || *pEType>
14492 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 5 ) return SQLIT
14493 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
14494 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
14495 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 OK;.}..#else /*
14496 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c 49 54 if defined SQLIT
14497 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
14498 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 M */. #define p
14499 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a trmapPut(w,x,y,z
1449a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 ) SQLITE_OK. #d
1449b 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65 74 28 efine ptrmapGet(
1449c 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f w,x,y,z) SQLITE_
1449d 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 OK. #define ptr
1449e 6d 61 70 50 75 74 4f 76 66 6c 28 79 2c 7a 29 20 mapPutOvfl(y,z)
1449f 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 SQLITE_OK.#endif
144a0 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 ../*.** Given a
144a1 62 74 72 65 65 20 70 61 67 65 20 61 6e 64 20 61 btree page and a
144a2 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d cell index (0 m
144a3 65 61 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 eans the first c
144a4 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 ell on.** the pa
144a5 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20 ge, 1 means the
144a6 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 second cell, and
144a7 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72 so forth) retur
144a8 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 n a pointer.** t
144a9 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 o the cell conte
144aa 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 nt..**.** This r
144ab 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c outine works onl
144ac 79 20 66 6f 72 20 70 61 67 65 73 20 74 68 61 74 y for pages that
144ad 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 do not contain
144ae 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a overflow cells..
144af 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 */.#define findC
144b0 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28 28 50 ell(P,I) \. ((P
144b1 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d )->aData + ((P)-
144b2 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65 74 32 >maskPage & get2
144b3 62 79 74 65 28 26 28 50 29 2d 3e 61 44 61 74 61 byte(&(P)->aData
144b4 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 [(P)->cellOffset
144b5 2b 32 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a 0a 2a +2*(I)])))../*.*
144b6 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 63 6f * This a more co
144b7 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f 66 mplex version of
144b8 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68 61 74 findCell() that
144b9 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 works for.** pa
144ba 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f 6e 74 ges that do cont
144bb 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ain overflow cel
144bc 6c 73 2e 20 20 53 65 65 20 69 6e 73 65 72 74 0a ls. See insert.
144bd 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2a 66 69 */.static u8 *fi
144be 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 4d ndOverflowCell(M
144bf 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
144c0 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 69 6e 74 nt iCell){. int
144c1 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 i;. assert( sq
144c2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
144c3 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
144c4 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 70 ex) );. for(i=p
144c5 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2d Page->nOverflow-
144c6 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 1; i>=0; i--){.
144c7 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 73 74 int k;. st
144c8 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 2a ruct _OvflCell *
144c9 70 4f 76 66 6c 3b 0a 20 20 20 20 70 4f 76 66 6c pOvfl;. pOvfl
144ca 20 3d 20 26 70 50 61 67 65 2d 3e 61 4f 76 66 6c = &pPage->aOvfl
144cb 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d 20 70 4f 76 [i];. k = pOv
144cc 66 6c 2d 3e 69 64 78 3b 0a 20 20 20 20 69 66 28 fl->idx;. if(
144cd 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 k<=iCell ){.
144ce 20 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c 6c 20 if( k==iCell
144cf 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
144d0 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 6c 6c 3b 0a n pOvfl->pCell;.
144d1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 43 }. iC
144d2 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d ell--;. }. }
144d3 0a 20 20 72 65 74 75 72 6e 20 66 69 6e 64 43 65 . return findCe
144d4 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 ll(pPage, iCell)
144d5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 ;.}../*.** Parse
144d6 20 61 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 a cell content
144d7 62 6c 6f 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 block and fill i
144d8 6e 20 74 68 65 20 43 65 6c 6c 49 6e 66 6f 20 73 n the CellInfo s
144d9 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 tructure. There
144da 0a 2a 2a 20 61 72 65 20 74 77 6f 20 76 65 72 73 .** are two vers
144db 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 66 75 6e ions of this fun
144dc 63 74 69 6f 6e 2e 20 20 73 71 6c 69 74 65 33 42 ction. sqlite3B
144dd 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 treeParseCell()
144de 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c 6c takes a .** cell
144df 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 65 index as the se
144e0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 6e cond argument an
144e1 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 d sqlite3BtreePa
144e2 72 73 65 43 65 6c 6c 50 74 72 28 29 20 0a 2a 2a rseCellPtr() .**
144e3 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 takes a pointer
144e4 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 to the body of
144e5 74 68 65 20 63 65 6c 6c 20 61 73 20 69 74 73 20 the cell as its
144e6 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e second argument.
144e7 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 .**.** Within th
144e8 69 73 20 66 69 6c 65 2c 20 74 68 65 20 70 61 72 is file, the par
144e9 73 65 43 65 6c 6c 28 29 20 6d 61 63 72 6f 20 63 seCell() macro c
144ea 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 73 an be called ins
144eb 74 65 61 64 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 tead of.** sqlit
144ec 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
144ed 50 74 72 28 29 2e 20 55 73 69 6e 67 20 73 6f 6d Ptr(). Using som
144ee 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 74 68 69 e compilers, thi
144ef 73 20 77 69 6c 6c 20 62 65 20 66 61 73 74 65 72 s will be faster
144f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
144f1 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
144f2 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 BtreeParseCellPt
144f3 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 r(. MemPage *pP
144f4 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 age, /*
144f5 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 Page containing
144f6 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 the cell */. u8
144f7 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 *pCell,
144f8 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
144f9 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74 65 78 to the cell tex
144fa 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f t. */. CellInfo
144fb 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 *pInfo
144fc 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 /* Fill in this
144fd 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a structure */.){.
144fe 20 20 75 31 36 20 6e 3b 20 20 20 20 20 20 20 20 u16 n;
144ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
14500 62 65 72 20 62 79 74 65 73 20 69 6e 20 63 65 6c ber bytes in cel
14501 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 l content header
14502 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f */. u32 nPaylo
14503 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ad; /*
14504 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
14505 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f 61 64 of cell payload
14506 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
14507 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14508 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
14509 74 65 78 29 20 29 3b 0a 0a 20 20 70 49 6e 66 6f tex) );.. pInfo
1450a 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b ->pCell = pCell;
1450b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1450c 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 50 61 ->leaf==0 || pPa
1450d 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b 0a 20 ge->leaf==1 );.
1450e 20 6e 20 3d 20 70 50 61 67 65 2d 3e 63 68 69 6c n = pPage->chil
1450f 64 50 74 72 53 69 7a 65 3b 0a 20 20 61 73 73 65 dPtrSize;. asse
14510 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 50 61 67 65 rt( n==4-4*pPage
14511 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 69 66 28 20 ->leaf );. if(
14512 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
14513 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
14514 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 hasData ){.
14515 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 n += getVarint3
14516 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 2(&pCell[n], nPa
14517 79 6c 6f 61 64 29 3b 0a 20 20 20 20 7d 65 6c 73 yload);. }els
14518 65 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c 6f 61 e{. nPayloa
14519 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 d = 0;. }.
1451a 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 28 n += getVarint(
1451b 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a &pCell[n], (u64*
1451c 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a )&pInfo->nKey);.
1451d 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 pInfo->nData
1451e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d = nPayload;. }
1451f 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 2d else{. pInfo-
14520 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 >nData = 0;.
14521 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 n += getVarint32
14522 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 (&pCell[n], nPay
14523 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 6e 66 6f load);. pInfo
14524 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 79 6c 6f 61 ->nKey = nPayloa
14525 64 3b 0a 20 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e d;. }. pInfo->
14526 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61 79 6c nPayload = nPayl
14527 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48 oad;. pInfo->nH
14528 65 61 64 65 72 20 3d 20 6e 3b 0a 20 20 69 66 28 eader = n;. if(
14529 20 6c 69 6b 65 6c 79 28 6e 50 61 79 6c 6f 61 64 likely(nPayload
1452a 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 <=pPage->maxLoca
1452b 6c 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 l) ){. /* Thi
1452c 73 20 69 73 20 74 68 65 20 28 65 61 73 79 29 20 s is the (easy)
1452d 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 common case wher
1452e 65 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 79 e the entire pay
1452f 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 20 2a 2a load fits. **
14530 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 on the local pa
14531 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 ge. No overflow
14532 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 is required..
14533 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 69 */. int nSi
14534 7a 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ze; /*
14535 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 63 65 Total size of ce
14536 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 62 79 ll content in by
14537 74 65 73 20 2a 2f 0a 20 20 20 20 6e 53 69 7a 65 tes */. nSize
14538 20 3d 20 6e 50 61 79 6c 6f 61 64 20 2b 20 6e 3b = nPayload + n;
14539 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 . pInfo->nLoc
1453a 61 6c 20 3d 20 28 75 31 36 29 6e 50 61 79 6c 6f al = (u16)nPaylo
1453b 61 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 ad;. pInfo->i
1453c 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 Overflow = 0;.
1453d 20 20 69 66 28 20 28 6e 53 69 7a 65 20 26 20 7e if( (nSize & ~
1453e 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 3)==0 ){. n
1453f 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20 20 20 Size = 4;
14540 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c /* Minimum cell
14541 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 size is 4 */.
14542 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e }. pInfo->n
14543 53 69 7a 65 20 3d 20 28 75 31 36 29 6e 53 69 7a Size = (u16)nSiz
14544 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
14545 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 /* If the payloa
14546 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 63 d will not fit c
14547 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74 68 65 ompletely on the
14548 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77 65 20 local page, we
14549 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64 have. ** to d
1454a 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68 20 74 ecide how much t
1454b 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 o store locally
1454c 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 and how much to
1454d 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20 20 2a spill onto. *
1454e 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 * overflow pages
1454f 2e 20 20 54 68 65 20 73 74 72 61 74 65 67 79 20 . The strategy
14550 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 is to minimize t
14551 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75 6e 75 he amount of unu
14552 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 sed. ** space
14553 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 on overflow pag
14554 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69 6e 67 es while keeping
14555 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6c the amount of l
14556 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20 20 20 ocal storage.
14557 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 6d ** in between m
14558 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61 78 4c inLocal and maxL
14559 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 ocal.. **.
1455a 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20 63 68 ** Warning: ch
1455b 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79 20 6f anging the way o
1455c 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 verflow payload
1455d 69 73 20 64 69 73 74 72 69 62 75 74 65 64 20 69 is distributed i
1455e 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77 61 79 n any. ** way
1455f 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 will result in
14560 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 an incompatible
14561 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20 20 20 file format..
14562 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69 6e 4c */. int minL
14563 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 ocal; /* Minimu
14564 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c m amount of payl
14565 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 oad held locally
14566 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 78 4c */. int maxL
14567 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69 6d 75 ocal; /* Maximu
14568 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c m amount of payl
14569 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 oad held locally
1456a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75 72 70 */. int surp
1456b 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72 66 6c lus; /* Overfl
1456c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61 69 6c ow payload avail
1456d 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c 20 73 able for local s
1456e 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20 20 6d torage */.. m
1456f 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d inLocal = pPage-
14570 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 6d >minLocal;. m
14571 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d axLocal = pPage-
14572 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 73 >maxLocal;. s
14573 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f 63 61 urplus = minLoca
14574 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20 2d 20 l + (nPayload -
14575 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61 67 65 minLocal)%(pPage
14576 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
14577 65 20 2d 20 34 29 3b 0a 20 20 20 20 69 66 28 20 e - 4);. if(
14578 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f surplus <= maxLo
14579 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e cal ){. pIn
1457a 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
1457b 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 6)surplus;. }
1457c 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 else{. pInf
1457d 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 o->nLocal = (u16
1457e 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d )minLocal;. }
1457f 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 . pInfo->iOve
14580 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49 rflow = (u16)(pI
14581 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 nfo->nLocal + n)
14582 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 ;. pInfo->nSi
14583 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 ze = pInfo->iOve
14584 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d rflow + 4;. }.}
14585 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65 .#define parseCe
14586 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c ll(pPage, iCell,
14587 20 70 49 6e 66 6f 29 20 5c 0a 20 20 73 71 6c 69 pInfo) \. sqli
14588 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
14589 6c 50 74 72 28 28 70 50 61 67 65 29 2c 20 66 69 lPtr((pPage), fi
1458a 6e 64 43 65 6c 6c 28 28 70 50 61 67 65 29 2c 20 ndCell((pPage),
1458b 28 69 43 65 6c 6c 29 29 2c 20 28 70 49 6e 66 6f (iCell)), (pInfo
1458c 29 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 )).SQLITE_PRIVAT
1458d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
1458e 72 65 65 50 61 72 73 65 43 65 6c 6c 28 0a 20 20 reeParseCell(.
1458f 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
14590 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
14591 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 containing the c
14592 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 ell */. int iCe
14593 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
14594 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69 6e 64 /* The cell ind
14595 65 78 2e 20 20 46 69 72 73 74 20 63 65 6c 6c 20 ex. First cell
14596 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e is 0 */. CellIn
14597 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 fo *pInfo
14598 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 /* Fill in thi
14599 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 s structure */.)
1459a 7b 0a 20 20 70 61 72 73 65 43 65 6c 6c 28 70 50 {. parseCell(pP
1459b 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 age, iCell, pInf
1459c 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d o);.}../*.** Com
1459d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20 6e pute the total n
1459e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
1459f 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65 64 73 hat a Cell needs
145a0 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 in the cell.**
145a1 64 61 74 61 20 61 72 65 61 20 6f 66 20 74 68 65 data area of the
145a2 20 62 74 72 65 65 2d 70 61 67 65 2e 20 20 54 68 btree-page. Th
145a3 65 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 e return number
145a4 69 6e 63 6c 75 64 65 73 20 74 68 65 20 63 65 6c includes the cel
145a5 6c 0a 2a 2a 20 64 61 74 61 20 68 65 61 64 65 72 l.** data header
145a6 20 61 6e 64 20 74 68 65 20 6c 6f 63 61 6c 20 70 and the local p
145a7 61 79 6c 6f 61 64 2c 20 62 75 74 20 6e 6f 74 20 ayload, but not
145a8 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 any overflow pag
145a9 65 20 6f 72 0a 2a 2a 20 74 68 65 20 73 70 61 63 e or.** the spac
145aa 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 65 e used by the ce
145ab 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 ll pointer..*/.#
145ac 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 ifndef NDEBUG.st
145ad 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a atic u16 cellSiz
145ae 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
145af 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 , int iCell){.
145b0 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
145b1 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
145b2 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 seCell(pPage, iC
145b3 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 72 ell, &info);. r
145b4 65 74 75 72 6e 20 69 6e 66 6f 2e 6e 53 69 7a 65 eturn info.nSize
145b5 3b 0a 7d 0a 23 65 6e 64 69 66 0a 73 74 61 74 69 ;.}.#endif.stati
145b6 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 c u16 cellSizePt
145b7 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 r(MemPage *pPage
145b8 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 , u8 *pCell){.
145b9 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
145ba 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
145bb 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
145bc 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
145bd 20 20 72 65 74 75 72 6e 20 69 6e 66 6f 2e 6e 53 return info.nS
145be 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 ize;.}..#ifndef
145bf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
145c0 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 VACUUM./*.** If
145c1 74 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 the cell pCell,
145c2 70 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 61 part of page pPa
145c3 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f ge contains a po
145c4 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f inter.** to an o
145c5 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e verflow page, in
145c6 73 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 6e sert an entry in
145c7 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d to the pointer-m
145c8 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 ap.** for the ov
145c9 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a erflow page..*/.
145ca 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 static int ptrma
145cb 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50 pPutOvflPtr(MemP
145cc 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a age *pPage, u8 *
145cd 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 6e pCell){. CellIn
145ce 66 6f 20 69 6e 66 6f 3b 0a 20 20 61 73 73 65 72 fo info;. asser
145cf 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 t( pCell!=0 );.
145d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
145d1 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
145d2 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
145d3 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f 2e assert( (info.
145d4 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e nData+(pPage->in
145d5 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 tKey?0:info.nKey
145d6 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 ))==info.nPayloa
145d7 64 20 29 3b 0a 20 20 69 66 28 20 28 69 6e 66 6f d );. if( (info
145d8 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 .nData+(pPage->i
145d9 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 ntKey?0:info.nKe
145da 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 y))>info.nLocal
145db 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c ){. Pgno ovfl
145dc 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 = get4byte(&pCe
145dd 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
145de 77 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 w]);. return
145df 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d ptrmapPut(pPage-
145e0 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d >pBt, ovfl, PTRM
145e1 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 AP_OVERFLOW1, pP
145e2 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a age->pgno);. }.
145e3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
145e4 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 OK;.}./*.** If t
145e5 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69 6e 64 he cell with ind
145e6 65 78 20 69 43 65 6c 6c 20 6f 6e 20 70 61 67 65 ex iCell on page
145e7 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 pPage contains
145e8 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 a pointer.** to
145e9 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
145ea 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 , insert an entr
145eb 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 y into the point
145ec 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 er-map.** for th
145ed 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e e overflow page.
145ee 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
145ef 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 4d 65 6d trmapPutOvfl(Mem
145f0 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 Page *pPage, int
145f1 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 iCell){. u8 *p
145f2 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 Cell;. assert(
145f3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
145f4 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
145f5 75 74 65 78 29 20 29 3b 0a 20 20 70 43 65 6c 6c utex) );. pCell
145f6 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 = findOverflowC
145f7 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c ell(pPage, iCell
145f8 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 74 72 6d );. return ptrm
145f9 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 apPutOvflPtr(pPa
145fa 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 7d 0a 23 65 ge, pCell);.}.#e
145fb 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 ndif.../*.** Def
145fc 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65 ragment the page
145fd 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c given. All Cel
145fe 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 ls are moved to
145ff 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 the.** end of th
14600 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 66 e page and all f
14601 72 65 65 20 73 70 61 63 65 20 69 73 20 63 6f 6c ree space is col
14602 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a lected into one.
14603 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b 20 74 ** big FreeBlk t
14604 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 62 65 hat occurs in be
14605 74 77 65 65 6e 20 74 68 65 20 68 65 61 64 65 72 tween the header
14606 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 and cell.** poi
14607 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64 20 74 nter array and t
14608 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
14609 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 area..*/.static
1460a 69 6e 74 20 64 65 66 72 61 67 6d 65 6e 74 50 61 int defragmentPa
1460b 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
1460c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 e){. int i;
1460d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1460e 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
1460f 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 */. int pc;
14610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14611 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 /* Address of a
14612 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 i-th cell */.
14613 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 int addr;
14614 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
14615 66 73 65 74 20 6f 66 20 66 69 72 73 74 20 62 79 fset of first by
14616 74 65 20 61 66 74 65 72 20 63 65 6c 6c 20 70 6f te after cell po
14617 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 inter array */.
14618 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 int hdr;
14619 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1461a 66 66 73 65 74 20 74 6f 20 74 68 65 20 70 61 67 ffset to the pag
1461b 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e e header */. in
1461c 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 t size;
1461d 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
1461e 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 of a cell */.
1461f 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 int usableSize;
14620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
14621 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62 mber of usable b
14622 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a ytes on a page *
14623 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 /. int cellOffs
14624 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f et; /
14625 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 * Offset to the
14626 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 cell pointer arr
14627 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b ay */. int cbrk
14628 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
14629 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 /* Offset to
1462a 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
1462b 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20 6e area */. int n
1462c 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Cell;
1462d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1462e 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 of cells on the
1462f 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e page */. unsign
14630 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 20 20 ed char *data;
14631 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 /* The page
14632 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 69 67 data */. unsig
14633 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 3b 20 ned char *temp;
14634 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 61 72 /* Temp ar
14635 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 ea for cell cont
14636 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ent */.. assert
14637 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
14638 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
14639 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
1463a 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 ssert( pPage->pB
1463b 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t!=0 );. assert
1463c 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 ( pPage->pBt->us
1463d 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c 49 ableSize <= SQLI
1463e 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
1463f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
14640 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
14641 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
14642 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14643 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
14644 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70 20 3d tex) );. temp =
14645 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d sqlite3PagerTem
14646 70 53 70 61 63 65 28 70 50 61 67 65 2d 3e 70 42 pSpace(pPage->pB
14647 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 64 61 t->pPager);. da
14648 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
14649 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 a;. hdr = pPage
1464a 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63 ->hdrOffset;. c
1464b 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 ellOffset = pPag
1464c 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 e->cellOffset;.
1464d 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e nCell = pPage->
1464e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 nCell;. assert(
1464f 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74 65 nCell==get2byte
14650 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 20 29 (&data[hdr+3]) )
14651 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d ;. usableSize =
14652 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 pPage->pBt->usa
14653 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 72 6b 20 bleSize;. cbrk
14654 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
14655 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63 [hdr+5]);. memc
14656 70 79 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c 20 py(&temp[cbrk],
14657 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61 &data[cbrk], usa
14658 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 6b 29 3b bleSize - cbrk);
14659 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62 6c 65 . cbrk = usable
1465a 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 3d 30 3b Size;. for(i=0;
1465b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
1465c 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20 20 u8 *pAddr;
1465d 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 /* The i-th c
1465e 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ell pointer */.
1465f 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 74 61 pAddr = &data
14660 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69 2a [cellOffset + i*
14661 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65 74 2];. pc = get
14662 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20 20 2byte(pAddr);.
14663 20 20 69 66 28 20 70 63 3e 3d 75 73 61 62 6c 65 if( pc>=usable
14664 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65 Size ){. re
14665 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
14666 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
14667 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 size = cellS
14668 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 74 izePtr(pPage, &t
14669 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 emp[pc]);. cb
1466a 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20 rk -= size;.
1466b 69 66 28 20 63 62 72 6b 3c 63 65 6c 6c 4f 66 66 if( cbrk<cellOff
1466c 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 7c 7c 20 70 set+2*nCell || p
1466d 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a c+size>usableSiz
1466e 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 e ){. retur
1466f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
14670 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
14671 20 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73 69 assert( cbrk+si
14672 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 26 ze<=usableSize &
14673 26 20 63 62 72 6b 3e 3d 30 20 29 3b 0a 20 20 20 & cbrk>=0 );.
14674 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 62 memcpy(&data[cb
14675 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d 2c 20 rk], &temp[pc],
14676 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 32 62 size);. put2b
14677 79 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29 yte(pAddr, cbrk)
14678 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
14679 63 62 72 6b 3e 3d 63 65 6c 6c 4f 66 66 73 65 74 cbrk>=cellOffset
1467a 2b 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20 70 75 +2*nCell );. pu
1467b 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
1467c 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 64 61 +5], cbrk);. da
1467d 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 3b 0a 20 ta[hdr+1] = 0;.
1467e 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 3d 20 30 data[hdr+2] = 0
1467f 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 ;. data[hdr+7]
14680 3d 20 30 3b 0a 20 20 61 64 64 72 20 3d 20 63 65 = 0;. addr = ce
14681 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c llOffset+2*nCell
14682 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 ;. memset(&data
14683 5b 61 64 64 72 5d 2c 20 30 2c 20 63 62 72 6b 2d [addr], 0, cbrk-
14684 61 64 64 72 29 3b 0a 20 20 61 73 73 65 72 74 28 addr);. assert(
14685 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
14686 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
14687 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 pDbPage) );. if
14688 28 20 63 62 72 6b 2d 61 64 64 72 21 3d 70 50 61 ( cbrk-addr!=pPa
14689 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 ge->nFree ){.
1468a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1468b 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
1468c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1468d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c _OK;.}../*.** Al
1468e 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 locate nByte byt
1468f 65 73 20 6f 66 20 73 70 61 63 65 20 6f 6e 20 61 es of space on a
14690 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 page..**.** Ret
14691 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e urn the index in
14692 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b to pPage->aData[
14693 5d 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 ] of the first b
14694 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 yte of.** the ne
14695 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 w allocation. T
14696 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e he caller guaran
14697 74 65 65 73 20 74 68 61 74 20 74 68 65 72 65 20 tees that there
14698 69 73 20 65 6e 6f 75 67 68 0a 2a 2a 20 73 70 61 is enough.** spa
14699 63 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ce. This routin
1469a 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 66 61 69 e will never fai
1469b 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 l..**.** If the
1469c 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 6e 42 page contains nB
1469d 79 74 65 73 20 6f 66 20 66 72 65 65 20 73 70 61 ytes of free spa
1469e 63 65 20 62 75 74 20 64 6f 65 73 20 6e 6f 74 20 ce but does not
1469f 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 42 79 74 65 contain.** nByte
146a0 73 20 6f 66 20 63 6f 6e 74 69 67 75 6f 75 73 20 s of contiguous
146a1 66 72 65 65 20 73 70 61 63 65 2c 20 74 68 65 6e free space, then
146a2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 75 this routine au
146a3 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 63 tomatically.** c
146a4 61 6c 6c 73 20 64 65 66 72 61 67 6d 65 6e 74 50 alls defragmentP
146a5 61 67 65 28 29 20 74 6f 20 63 6f 6e 73 6f 6c 69 age() to consoli
146a6 64 61 74 65 20 61 6c 6c 20 66 72 65 65 20 73 70 date all free sp
146a7 61 63 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 61 ace before .** a
146a8 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20 6e 65 llocating the ne
146a9 77 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 w chunk..*/.stat
146aa 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 53 ic int allocateS
146ab 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 pace(MemPage *pP
146ac 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b age, int nByte){
146ad 0a 20 20 69 6e 74 20 61 64 64 72 2c 20 70 63 2c . int addr, pc,
146ae 20 68 64 72 3b 0a 20 20 69 6e 74 20 73 69 7a 65 hdr;. int size
146af 3b 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b 0a 20 ;. int nFrag;.
146b0 20 69 6e 74 20 74 6f 70 3b 0a 20 20 69 6e 74 20 int top;. int
146b1 6e 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 63 65 6c nCell;. int cel
146b2 6c 4f 66 66 73 65 74 3b 0a 20 20 75 6e 73 69 67 lOffset;. unsig
146b3 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 0a ned char *data;.
146b4 20 20 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 . data = pPag
146b5 65 2d 3e 61 44 61 74 61 3b 0a 20 20 61 73 73 65 e->aData;. asse
146b6 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
146b7 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
146b8 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
146b9 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
146ba 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 pBt );. assert(
146bb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
146bc 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
146bd 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
146be 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29 3b 20 rt( nByte>=0 );
146bf 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c /* Minimum cell
146c0 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 size is 4 */.
146c1 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
146c2 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b 0a 20 Free>=nByte );.
146c3 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
146c4 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a nOverflow==0 );.
146c5 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d pPage->nFree -
146c6 3d 20 28 75 31 36 29 6e 42 79 74 65 3b 0a 20 20 = (u16)nByte;.
146c7 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
146c8 4f 66 66 73 65 74 3b 0a 0a 20 20 6e 46 72 61 67 Offset;.. nFrag
146c9 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a = data[hdr+7];.
146ca 20 20 69 66 28 20 6e 46 72 61 67 3c 36 30 20 29 if( nFrag<60 )
146cb 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 {. /* Search
146cc 74 68 65 20 66 72 65 65 6c 69 73 74 20 6c 6f 6f the freelist loo
146cd 6b 69 6e 67 20 66 6f 72 20 61 20 73 6c 6f 74 20 king for a slot
146ce 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 big enough to sa
146cf 74 69 73 66 79 20 74 68 65 0a 20 20 20 20 2a 2a tisfy the. **
146d0 20 73 70 61 63 65 20 72 65 71 75 65 73 74 2e 20 space request.
146d1 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 68 64 */. addr = hd
146d2 72 2b 31 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 r+1;. while(
146d3 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 (pc = get2byte(&
146d4 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 data[addr]))>0 )
146d5 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 {. size = g
146d6 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 et2byte(&data[pc
146d7 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +2]);. if(
146d8 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b 0a 20 size>=nByte ){.
146d9 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 73 int x = s
146da 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 ize - nByte;.
146db 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c 6e 42 if( size<nB
146dc 79 74 65 2b 34 20 29 7b 0a 20 20 20 20 20 20 20 yte+4 ){.
146dd 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b memcpy(&data[
146de 61 64 64 72 5d 2c 20 26 64 61 74 61 5b 70 63 5d addr], &data[pc]
146df 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 2);.
146e0 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 28 75 data[hdr+7] = (u
146e1 38 29 28 6e 46 72 61 67 20 2b 20 78 29 3b 0a 20 8)(nFrag + x);.
146e2 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
146e3 70 63 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 pc;. }els
146e4 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 e{. put
146e5 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 2byte(&data[pc+2
146e6 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 20 20 20 ], x);.
146e7 20 72 65 74 75 72 6e 20 70 63 20 2b 20 78 3b 0a return pc + x;.
146e8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
146e9 7d 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 70 }. addr = p
146ea 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 c;. }. }..
146eb 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f /* Allocate memo
146ec 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20 ry from the gap
146ed 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 in between the c
146ee 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 ell pointer arra
146ef 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63 y. ** and the c
146f0 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
146f1 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 3d 20 67 .. */. top = g
146f2 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
146f3 72 2b 35 5d 29 3b 0a 20 20 6e 43 65 6c 6c 20 3d r+5]);. nCell =
146f4 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
146f5 68 64 72 2b 33 5d 29 3b 0a 20 20 63 65 6c 6c 4f hdr+3]);. cellO
146f6 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 ffset = pPage->c
146f7 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 69 66 28 ellOffset;. if(
146f8 20 6e 46 72 61 67 3e 3d 36 30 20 7c 7c 20 63 65 nFrag>=60 || ce
146f9 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 65 llOffset + 2*nCe
146fa 6c 6c 20 3e 20 74 6f 70 20 2d 20 6e 42 79 74 65 ll > top - nByte
146fb 20 29 7b 0a 20 20 20 20 64 65 66 72 61 67 6d 65 ){. defragme
146fc 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 ntPage(pPage);.
146fd 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 top = get2byt
146fe 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b e(&data[hdr+5]);
146ff 0a 20 20 7d 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 . }. top -= nB
14700 79 74 65 3b 0a 20 20 61 73 73 65 72 74 28 20 63 yte;. assert( c
14701 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 ellOffset + 2*nC
14702 65 6c 6c 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 20 ell <= top );.
14703 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 put2byte(&data[h
14704 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 dr+5], top);. a
14705 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
14706 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
14707 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
14708 3b 0a 20 20 72 65 74 75 72 6e 20 74 6f 70 3b 0a ;. return top;.
14709 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1470a 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 a section of the
1470b 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f pPage->aData to
1470c 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a the freelist..*
1470d 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 * The first byte
1470e 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 of the new free
1470f 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d block is pPage-
14710 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a >aDisk[start].**
14711 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 and the size of
14712 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 the block is "s
14713 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a ize" bytes..**.*
14714 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 * Most of the ef
14715 66 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 fort here is inv
14716 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 olved in coalesi
14717 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 ng adjacent.** f
14718 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 ree blocks into
14719 61 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 a single big fre
1471a 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 e block..*/.stat
1471b 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 ic int freeSpace
1471c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
1471d 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 int start, int
1471e 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 size){. int add
1471f 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a r, pbegin, hdr;.
14720 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
14721 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 *data = pPage->a
14722 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 Data;.. assert(
14723 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 pPage->pBt!=0 )
14724 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
14725 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
14726 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
14727 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
14728 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68 start>=pPage->h
14729 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 50 61 67 drOffset+6+(pPag
1472a 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a e->leaf?0:4) );.
1472b 20 20 61 73 73 65 72 74 28 20 28 73 74 61 72 74 assert( (start
1472c 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d + size)<=pPage-
1472d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
1472e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
1472f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14730 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
14731 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
14732 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a size>=0 ); /*
14733 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 Minimum cell si
14734 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 ze is 4 */..#ifd
14735 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 ef SQLITE_SECURE
14736 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 _DELETE. /* Ove
14737 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 rwrite deleted i
14738 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 nformation with
14739 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53 zeros when the S
1473a 45 43 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 ECURE_DELETE .
1473b 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 ** option is ena
1473c 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d bled at compile-
1473d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 time */. memset
1473e 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 (&data[start], 0
1473f 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a , size);.#endif.
14740 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 . /* Add the sp
14741 61 63 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 ace back into th
14742 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 e linked list of
14743 20 66 72 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20 freeblocks */.
14744 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 hdr = pPage->hd
14745 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20 rOffset;. addr
14746 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 = hdr + 1;. whi
14747 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 le( (pbegin = ge
14748 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 t2byte(&data[add
14749 72 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 62 r]))<start && pb
1474a 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73 egin>0 ){. as
1474b 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 sert( pbegin<=pP
1474c 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
1474d 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 69 66 Size-4 );. if
1474e 28 20 70 62 65 67 69 6e 3c 3d 61 64 64 72 20 29 ( pbegin<=addr )
1474f 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 {. return
14750 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
14751 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 KPT;. }. a
14752 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 ddr = pbegin;.
14753 7d 0a 20 20 69 66 20 28 20 70 62 65 67 69 6e 3e }. if ( pbegin>
14754 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
14755 6c 65 53 69 7a 65 2d 34 20 29 20 7b 0a 20 20 20 leSize-4 ) {.
14756 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
14757 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
14758 0a 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 . assert( pbegi
14759 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67 69 6e n>addr || pbegin
1475a 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62 79 74 ==0 );. put2byt
1475b 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 73 e(&data[addr], s
1475c 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62 79 74 tart);. put2byt
1475d 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 e(&data[start],
1475e 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74 32 62 pbegin);. put2b
1475f 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 2b yte(&data[start+
14760 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50 61 2], size);. pPa
14761 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 28 75 31 ge->nFree += (u1
14762 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 6)size;.. /* Co
14763 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 alesce adjacent
14764 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 free blocks */.
14765 20 61 64 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 addr = pPage->h
14766 64 72 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20 20 drOffset + 1;.
14767 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d while( (pbegin =
14768 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
14769 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20 addr]))>0 ){.
1476a 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 69 7a int pnext, psiz
1476b 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 65 72 74 e, x;. assert
1476c 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b ( pbegin>addr );
1476d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65 . assert( pbe
1476e 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d gin<=pPage->pBt-
1476f 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b >usableSize-4 );
14770 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 . pnext = get
14771 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 2byte(&data[pbeg
14772 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 in]);. psize
14773 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
14774 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 [pbegin+2]);.
14775 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73 if( pbegin + ps
14776 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 ize + 3 >= pnext
14777 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 && pnext>0 ){.
14778 20 20 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20 int frag =
14779 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b pnext - (pbegin+
1477a 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 psize);. if
1477b 28 20 28 66 72 61 67 3c 30 29 20 7c 7c 20 28 66 ( (frag<0) || (f
1477c 72 61 67 3e 28 69 6e 74 29 64 61 74 61 5b 70 50 rag>(int)data[pP
1477d 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 age->hdrOffset+7
1477e 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 ]) ){. re
1477f 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
14780 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
14781 7d 0a 20 20 20 20 20 20 64 61 74 61 5b 70 50 61 }. data[pPa
14782 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d ge->hdrOffset+7]
14783 20 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 -= (u8)frag;.
14784 20 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 x = get2byte
14785 28 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a (&data[pnext]);.
14786 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 put2byte(&
14787 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 data[pbegin], x)
14788 3b 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 ;. x = pnex
14789 74 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 t + get2byte(&da
1478a 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 ta[pnext+2]) - p
1478b 62 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 begin;. put
1478c 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 2byte(&data[pbeg
1478d 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d in+2], x);. }
1478e 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 else{. addr
1478f 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d = pbegin;. }
14790 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
14791 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 e cell content a
14792 72 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 rea begins with
14793 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d a freeblock, rem
14794 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 ove it. */. if(
14795 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 data[hdr+1]==da
14796 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 ta[hdr+5] && dat
14797 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 a[hdr+2]==data[h
14798 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 dr+6] ){. int
14799 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e top;. pbegin
1479a 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
1479b 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d a[hdr+1]);. m
1479c 65 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b emcpy(&data[hdr+
1479d 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 1], &data[pbegin
1479e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d ], 2);. top =
1479f 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
147a0 68 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 hdr+5]) + get2by
147a1 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b te(&data[pbegin+
147a2 32 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 2]);. put2byt
147a3 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
147a4 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 top);. }. asse
147a5 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
147a6 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
147a7 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
147a8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
147a9 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f K;.}../*.** Deco
147aa 64 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 de the flags byt
147ab 65 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 e (the first byt
147ac 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 e of the header)
147ad 20 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 for a page.** a
147ae 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 nd initialize fi
147af 65 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 elds of the MemP
147b0 61 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63 age structure ac
147b1 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a cordingly..**.**
147b2 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 Only the follow
147b3 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 ing combinations
147b4 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 are supported.
147b5 20 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 Anything differ
147b6 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 ent.** indicates
147b7 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 a corrupt datab
147b8 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a ase files:.**.**
147b9 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 PTF_ZER
147ba 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 ODATA.**
147bb 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 PTF_ZERODATA |
147bc 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 PTF_LEAF.**
147bd 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 PTF_LEAFDATA
147be 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a | PTF_INTKEY.**
147bf 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 PTF_LEA
147c0 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b FDATA | PTF_INTK
147c1 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f EY | PTF_LEAF.*/
147c2 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f .static int deco
147c3 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 deFlags(MemPage
147c4 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 *pPage, int flag
147c5 42 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 Byte){. BtShare
147c6 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 d *pBt; /* A
147c7 20 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e copy of pPage->
147c8 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 pBt */.. assert
147c9 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 ( pPage->hdrOffs
147ca 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f et==(pPage->pgno
147cb 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 ==1 ? 100 : 0) )
147cc 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
147cd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
147ce 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
147cf 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 ) );. pPage->le
147d0 61 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 af = (u8)(flagBy
147d1 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 te>>3); assert(
147d2 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c PTF_LEAF == 1<<
147d3 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 3 );. flagByte
147d4 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 &= ~PTF_LEAF;.
147d5 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
147d6 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d ize = 4-4*pPage-
147d7 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 >leaf;. pBt = p
147d8 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 Page->pBt;. if(
147d9 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f flagByte==(PTF_
147da 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 LEAFDATA | PTF_I
147db 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 NTKEY) ){. pP
147dc 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b age->intKey = 1;
147dd 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 . pPage->hasD
147de 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 ata = pPage->lea
147df 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 f;. pPage->ma
147e0 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 xLocal = pBt->ma
147e1 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 xLeaf;. pPage
147e2 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 ->minLocal = pBt
147e3 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c ->minLeaf;. }el
147e4 73 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d se if( flagByte=
147e5 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b =PTF_ZERODATA ){
147e6 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b . pPage->intK
147e7 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 ey = 0;. pPag
147e8 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a e->hasData = 0;.
147e9 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f pPage->maxLo
147ea 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f cal = pBt->maxLo
147eb 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e cal;. pPage->
147ec 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e minLocal = pBt->
147ed 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 minLocal;. }els
147ee 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 e{. return SQ
147ef 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
147f0 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 T;. }. return
147f1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
147f2 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 .** Initialize t
147f3 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 he auxiliary inf
147f4 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 ormation for a d
147f5 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a isk block..**.**
147f6 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
147f7 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 K on success. I
147f8 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68 f we see that th
147f9 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e e page does.** n
147fa 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c ot contain a wel
147fb 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 l-formed databas
147fc 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 e page, then ret
147fd 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 urn .** SQLITE_C
147fe 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 ORRUPT. Note th
147ff 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 at a return of S
14800 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f QLITE_OK does no
14801 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 t.** guarantee t
14802 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 hat the page is
14803 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 well-formed. It
14804 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 only shows that
14805 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f .** we failed to
14806 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 detect any corr
14807 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 uption..*/.SQLIT
14808 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14809 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 lite3BtreeInitPa
1480a 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
1480b 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 e){.. assert( p
1480c 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a Page->pBt!=0 );.
1480d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1480e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
1480f 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
14810 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
14811 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 ge->pgno==sqlite
14812 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 3PagerPagenumber
14813 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
14814 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
14815 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 age == sqlite3Pa
14816 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 gerGetExtra(pPag
14817 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
14818 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
14819 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 aData == sqlite3
1481a 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 PagerGetData(pPa
1481b 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
1481c 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 . if( !pPage->i
1481d 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36 sInit ){. u16
1481e 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 pc;
1481f 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 /* Address of a
14820 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e freeblock within
14821 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20 pPage->aData[]
14822 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20 */. u8 hdr;
14823 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
14824 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 set to beginning
14825 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20 of page header
14826 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b */. u8 *data;
14827 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 /* Equ
14828 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 al to pPage->aDa
14829 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72 ta */. BtShar
1482a 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 ed *pBt;
1482b 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 /* The main btre
1482c 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
1482d 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a u16 usableSiz
1482e 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 e; /* Amount
1482f 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 of usable space
14830 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a on each page */.
14831 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 u16 cellOffs
14832 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 et; /* Offset
14833 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 from start of p
14834 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c age to first cel
14835 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 l pointer */.
14836 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 u16 nFree;
14837 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
14838 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e unused bytes on
14839 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 the page */.
1483a 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20 u16 top;
1483b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
1483c 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f e of the cell co
1483d 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 0a 20 ntent area */..
1483e 20 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e pBt = pPage->
1483f 70 42 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20 pBt;.. hdr =
14840 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
14841 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 ;. data = pPa
14842 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 ge->aData;. i
14843 66 28 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 f( decodeFlags(p
14844 50 61 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29 Page, data[hdr])
14845 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
14846 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
14847 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
14848 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 pageSize>=512 &&
14849 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d pBt->pageSize<=
1484a 33 32 37 36 38 20 29 3b 0a 20 20 20 20 70 50 61 32768 );. pPa
1484b 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70 ge->maskPage = p
1484c 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 Bt->pageSize - 1
1484d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 ;. pPage->nOv
1484e 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 erflow = 0;.
1484f 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 usableSize = pBt
14850 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 ->usableSize;.
14851 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 pPage->cellOff
14852 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 set = cellOffset
14853 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a = hdr + 12 - 4*
14854 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 pPage->leaf;.
14855 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 top = get2byte(
14856 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 &data[hdr+5]);.
14857 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 pPage->nCell
14858 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
14859 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66 [hdr+3]);. if
1485a 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d ( pPage->nCell>M
1485b 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20 X_CELL(pBt) ){.
1485c 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 /* To many
1485d 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 cells for a sing
1485e 6c 65 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 le page. The pa
1485f 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 ge must be corru
14860 70 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 pt */. retu
14861 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
14862 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 T_BKPT;. }.
14863 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 . /* Compute
14864 74 68 65 20 74 6f 74 61 6c 20 66 72 65 65 20 73 the total free s
14865 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65 pace on the page
14866 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 67 65 74 */. pc = get
14867 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
14868 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d 1]);. nFree =
14869 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74 data[hdr+7] + t
1486a 6f 70 20 2d 20 28 63 65 6c 6c 4f 66 66 73 65 74 op - (cellOffset
1486b 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c + 2*pPage->nCel
1486c 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 l);. while( p
1486d 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 31 36 c>0 ){. u16
1486e 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20 next, size;.
1486f 20 20 20 69 66 28 20 70 63 3e 75 73 61 62 6c 65 if( pc>usable
14870 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 Size-4 ){.
14871 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 20 /* Free block
14872 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20 is off the page
14873 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
14874 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
14875 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a _BKPT; . }.
14876 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 next = get
14877 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29 2byte(&data[pc])
14878 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 ;. size = g
14879 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 et2byte(&data[pc
1487a 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +2]);. if(
1487b 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c 3d next>0 && next<=
1487c 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a 20 20 20 pc+size+3 ){.
1487d 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f /* Free blo
1487e 63 6b 73 20 6d 75 73 74 20 62 65 20 69 6e 20 61 cks must be in a
1487f 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a ccending order *
14880 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e /. return
14881 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
14882 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 BKPT; . }.
14883 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 69 nFree += si
14884 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 6e ze;. pc = n
14885 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ext;. }. p
14886 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 Page->nFree = (u
14887 31 36 29 6e 46 72 65 65 3b 0a 20 20 20 20 69 66 16)nFree;. if
14888 28 20 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65 53 ( nFree>=usableS
14889 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ize ){. /*
1488a 46 72 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f Free space canno
1488b 74 20 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70 t exceed total p
1488c 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 age size */.
1488d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1488e 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 CORRUPT_BKPT; .
1488f 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 2f 2a }..#if 0. /*
14890 20 43 68 65 63 6b 20 74 68 61 74 20 61 6c 6c 20 Check that all
14891 74 68 65 20 6f 66 66 73 65 74 73 20 69 6e 20 74 the offsets in t
14892 68 65 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61 he cell offset a
14893 72 72 61 79 20 61 72 65 20 77 69 74 68 69 6e 20 rray are within
14894 72 61 6e 67 65 2e 20 0a 20 20 2a 2a 20 0a 20 20 range. . ** .
14895 2a 2a 20 4f 6d 69 74 74 69 6e 67 20 74 68 69 73 ** Omitting this
14896 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65 consistency che
14897 63 6b 20 61 6e 64 20 75 73 69 6e 67 20 74 68 65 ck and using the
14898 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 pPage->maskPage
14899 20 6d 61 73 6b 0a 20 20 2a 2a 20 74 6f 20 70 72 mask. ** to pr
1489a 65 76 65 6e 74 20 6f 76 65 72 72 75 6e 6e 69 6e event overrunnin
1489b 67 20 74 68 65 20 70 61 67 65 20 62 75 66 66 65 g the page buffe
1489c 72 20 69 6e 20 66 69 6e 64 43 65 6c 6c 28 29 20 r in findCell()
1489d 72 65 73 75 6c 74 73 20 69 6e 20 61 0a 20 20 2a results in a. *
1489e 2a 20 32 2e 35 25 20 70 65 72 66 6f 72 6d 61 6e * 2.5% performan
1489f 63 65 20 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20 ce gain.. */.
148a0 7b 0a 20 20 20 20 75 38 20 2a 70 4f 66 66 3b 20 {. u8 *pOff;
148a1 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
148a2 6f 72 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b or used to check
148a3 20 61 6c 6c 20 63 65 6c 6c 20 6f 66 66 73 65 74 all cell offset
148a4 73 20 61 72 65 20 69 6e 20 72 61 6e 67 65 20 2a s are in range *
148a5 2f 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 20 /. u8 *pEnd;
148a6 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
148a7 72 20 74 6f 20 65 6e 64 20 6f 66 20 63 65 6c 6c r to end of cell
148a8 20 6f 66 66 73 65 74 20 61 72 72 61 79 20 2a 2f offset array */
148a9 0a 20 20 20 20 75 38 20 6d 61 73 6b 3b 20 20 20 . u8 mask;
148aa 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 /* Mask of
148ab 20 62 69 74 73 20 74 68 61 74 20 6d 75 73 74 20 bits that must
148ac 62 65 20 7a 65 72 6f 20 69 6e 20 4d 53 42 20 6f be zero in MSB o
148ad 66 20 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 2a f cell offsets *
148ae 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28 /. mask = ~((
148af 28 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69 (u8)(pBt->pageSi
148b0 7a 65 3e 3e 38 29 29 2d 31 29 3b 0a 20 20 20 20 ze>>8))-1);.
148b1 70 45 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c pEnd = &data[cel
148b2 6c 4f 66 66 73 65 74 20 2b 20 70 50 61 67 65 2d lOffset + pPage-
148b3 3e 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 20 20 66 >nCell*2];. f
148b4 6f 72 28 70 4f 66 66 3d 26 64 61 74 61 5b 63 65 or(pOff=&data[ce
148b5 6c 6c 4f 66 66 73 65 74 5d 3b 20 70 4f 66 66 21 llOffset]; pOff!
148b6 3d 70 45 6e 64 20 26 26 20 21 28 28 2a 70 4f 66 =pEnd && !((*pOf
148b7 66 29 26 6d 61 73 6b 29 3b 20 70 4f 66 66 2b 3d f)&mask); pOff+=
148b8 32 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 66 66 2);. if( pOff
148b9 21 3d 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 !=pEnd ){.
148ba 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
148bb 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
148bc 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }. }.#endif..
148bd 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 pPage->isInit
148be 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 1;. }. retur
148bf 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
148c0 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72 /*.** Set up a r
148c1 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 aw page so that
148c2 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 it looks like a
148c3 64 61 74 61 62 61 73 65 20 70 61 67 65 20 68 6f database page ho
148c4 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 lding.** no entr
148c5 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ies..*/.static v
148c6 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d oid zeroPage(Mem
148c7 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 Page *pPage, int
148c8 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 flags){. unsig
148c9 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d ned char *data =
148ca 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 pPage->aData;.
148cb 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
148cc 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 pPage->pBt;. u
148cd 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 8 hdr = pPage->h
148ce 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 20 drOffset;. u16
148cf 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 first;.. assert
148d0 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 ( sqlite3PagerPa
148d1 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e genumber(pPage->
148d2 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d pDbPage)==pPage-
148d3 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 >pgno );. asser
148d4 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
148d5 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 etExtra(pPage->p
148d6 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 DbPage) == (void
148d7 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 *)pPage );. ass
148d8 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
148d9 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e rGetData(pPage->
148da 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61 pDbPage) == data
148db 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
148dc 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
148dd 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
148de 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
148df 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
148e0 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
148e1 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 ) );. /*memset(
148e2 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 &data[hdr], 0, p
148e3 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d Bt->usableSize -
148e4 20 68 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b hdr);*/. data[
148e5 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 hdr] = (char)fla
148e6 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 64 gs;. first = hd
148e7 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 67 r + 8 + 4*((flag
148e8 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f s&PTF_LEAF)==0 ?
148e9 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 1:0);. memset(&
148ea 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 data[hdr+1], 0,
148eb 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 4);. data[hdr+7
148ec 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 74 ] = 0;. put2byt
148ed 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
148ee 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 pBt->usableSize)
148ef 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 ;. pPage->nFree
148f0 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
148f1 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 ze - first;. de
148f2 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c codeFlags(pPage,
148f3 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65 flags);. pPage
148f4 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68 64 ->hdrOffset = hd
148f5 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c r;. pPage->cell
148f6 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a Offset = first;.
148f7 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c pPage->nOverfl
148f8 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 ow = 0;. assert
148f9 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e ( pBt->pageSize>
148fa 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 =512 && pBt->pag
148fb 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a eSize<=32768 );.
148fc 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 pPage->maskPag
148fd 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a e = pBt->pageSiz
148fe 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e e - 1;. pPage->
148ff 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 nCell = 0;. pPa
14900 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a ge->isInit = 1;.
14901 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 }.../*.** Conver
14902 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 69 t a DbPage obtai
14903 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 ned from the pag
14904 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 er into a MemPag
14905 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 e used by.** the
14906 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f btree layer..*/
14907 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 20 .static MemPage
14908 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 *btreePageFromDb
14909 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 62 Page(DbPage *pDb
1490a 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c Page, Pgno pgno,
1490b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b BtShared *pBt){
1490c 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
1490d 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 e = (MemPage*)sq
1490e 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
1490f 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 ra(pDbPage);. p
14910 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 Page->aData = sq
14911 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
14912 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 a(pDbPage);. pP
14913 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 age->pDbPage = p
14914 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 2d DbPage;. pPage-
14915 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 >pBt = pBt;. pP
14916 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f age->pgno = pgno
14917 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 ;. pPage->hdrOf
14918 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 fset = pPage->pg
14919 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b no==1 ? 100 : 0;
1491a 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b . return pPage;
1491b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 .}../*.** Get a
1491c 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 page from the p
1491d 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a ager. Initializ
1491e 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 e the MemPage.pB
1491f 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 t and.** MemPage
14920 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 .aData elements
14921 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a if needed..**.**
14922 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e If the noConten
14923 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69 t flag is set, i
14924 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 t means that we
14925 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 do not care abou
14926 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 t.** the content
14927 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 20 of the page at
14928 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 this time. So d
14929 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 o not go to the
1492a 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 disk.** to fetch
1492b 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a the content. J
1492c 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 ust fill in the
1492d 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 content with zer
1492e 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 os for now..** I
1492f 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 f in the future
14930 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 we call sqlite3P
14931 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74 agerWrite() on t
14932 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a 2a his page, that.*
14933 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 20 * means we have
14934 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 6f started to be co
14935 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 6f ncerned about co
14936 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 69 ntent and the di
14937 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c sk.** read shoul
14938 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 20 d occur at that
14939 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 point..*/.SQLITE
1493a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1493b 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
1493c 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
1493d 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 t, /* The
1493e 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 btree */. Pgno
1493f 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 pgno,
14940 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 /* Number of the
14941 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a page to fetch *
14942 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 /. MemPage **pp
14943 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 Page, /* Retu
14944 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 rn the page in t
14945 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f his parameter */
14946 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 . int noContent
14947 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
14948 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 t load page cont
14949 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 ent if true */.)
1494a 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 {. int rc;. Db
1494b 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a Page *pDbPage;..
1494c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1494d 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
1494e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 ->mutex) );. rc
1494f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 = sqlite3PagerA
14950 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 cquire(pBt->pPag
14951 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 er, pgno, (DbPag
14952 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f e**)&pDbPage, no
14953 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 Content);. if(
14954 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
14955 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 *ppPage = btre
14956 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 ePageFromDbPage(
14957 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 pDbPage, pgno, p
14958 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 Bt);. return SQ
14959 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1495a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 61 67 * Retrieve a pag
1495b 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 e from the pager
1495c 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 cache. If the r
1495d 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
1495e 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 not.** already
1495f 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 in the pager cac
14960 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 he return NULL.
14961 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d Initialize the M
14962 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a emPage.pBt and.*
14963 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 * MemPage.aData
14964 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 elements if need
14965 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 ed..*/.static Me
14966 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 mPage *btreePage
14967 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 Lookup(BtShared
14968 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
14969 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 {. DbPage *pDbP
1496a 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 age;. assert( s
1496b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1496c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1496d 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c . pDbPage = sql
1496e 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
1496f 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e pBt->pPager, pgn
14970 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 o);. if( pDbPag
14971 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
14972 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 btreePageFromDbP
14973 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e age(pDbPage, pgn
14974 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 o, pBt);. }. r
14975 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
14976 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a * Return the siz
14977 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
14978 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e e file in pages.
14979 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 If there is any
1497a 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f kind of.** erro
1497b 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 r, return ((unsi
1497c 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f gned int)-1)..*/
1497d 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67 .static Pgno pag
1497e 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 erPagecount(BtSh
1497f 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e ared *pBt){. in
14980 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 t nPage = -1;.
14981 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
14982 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b ( pBt->pPage1 );
14983 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
14984 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
14985 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 t->pPager, &nPag
14986 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 e);. assert( rc
14987 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e ==SQLITE_OK || n
14988 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 Page==-1 );. re
14989 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 turn (Pgno)nPage
1498a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 ;.}../*.** Get a
1498b 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 page from the p
1498c 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c ager and initial
1498d 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f ize it. This ro
1498e 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74 utine.** is just
1498f 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 a convenience w
14990 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65 rapper around se
14991 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a parate calls to.
14992 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 ** sqlite3BtreeG
14993 65 74 50 61 67 65 28 29 20 61 6e 64 20 73 71 6c etPage() and sql
14994 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
14995 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 e()..*/.static i
14996 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 nt getAndInitPag
14997 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
14998 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 Bt, /*
14999 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
1499a 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
1499b 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
1499c 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
1499d 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 e to get */. Me
1499e 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 mPage **ppPage
1499f 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
149a0 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 page pointer her
149a1 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
149a2 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
149a3 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ge;.. assert( s
149a4 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
149a5 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
149a6 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 . if( pgno==0 )
149a7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
149a8 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
149a9 3b 20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 ; . }.. /* It
149aa 69 73 20 6f 66 74 65 6e 20 74 68 65 20 63 61 73 is often the cas
149ab 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
149ac 77 65 20 77 61 6e 74 20 69 73 20 61 6c 72 65 61 we want is alrea
149ad 64 79 20 69 6e 20 63 61 63 68 65 2e 0a 20 20 2a dy in cache.. *
149ae 2a 20 49 66 20 73 6f 2c 20 67 65 74 20 69 74 20 * If so, get it
149af 64 69 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 directly. This
149b0 73 61 76 65 73 20 75 73 20 66 72 6f 6d 20 68 61 saves us from ha
149b1 76 69 6e 67 20 74 6f 20 63 61 6c 6c 0a 20 20 2a ving to call. *
149b2 2a 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 * pagerPagecount
149b3 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 () to make sure
149b4 70 67 6e 6f 20 69 73 20 77 69 74 68 69 6e 20 6c pgno is within l
149b5 69 6d 69 74 73 2c 20 77 68 69 63 68 20 72 65 73 imits, which res
149b6 75 6c 74 73 0a 20 20 2a 2a 20 69 6e 20 61 20 6d ults. ** in a m
149b7 65 61 73 75 72 65 61 62 6c 65 20 70 65 72 66 6f easureable perfo
149b8 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 rmance improveme
149b9 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 2a 70 70 50 nts.. */. *ppP
149ba 61 67 65 20 3d 20 70 50 61 67 65 20 3d 20 62 74 age = pPage = bt
149bb 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 reePageLookup(pB
149bc 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 t, pgno);. if(
149bd 70 50 61 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20 pPage ){. /*
149be 50 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 Page is already
149bf 69 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20 in cache */.
149c0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
149c1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
149c2 50 61 67 65 20 6e 6f 74 20 69 6e 20 63 61 63 68 Page not in cach
149c3 65 2e 20 20 41 63 71 75 69 72 65 20 69 74 2e 20 e. Acquire it.
149c4 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e */. if( pgno>
149c5 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
149c6 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 Bt) ){. ret
149c7 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
149c8 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a PT_BKPT; . }.
149c9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
149ca 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
149cb 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 , pgno, ppPage,
149cc 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 0);. if( rc )
149cd 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
149ce 70 50 61 67 65 20 3d 20 2a 70 70 50 61 67 65 3b pPage = *ppPage;
149cf 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 . }. if( !pPag
149d0 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 e->isInit ){.
149d1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
149d2 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 eeInitPage(pPage
149d3 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 );. }. if( rc!
149d4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
149d5 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
149d6 61 67 65 29 3b 0a 20 20 20 20 2a 70 70 50 61 67 age);. *ppPag
149d7 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 e = 0;. }. ret
149d8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
149d9 20 52 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 Release a MemPa
149da 67 65 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 ge. This should
149db 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 be called once
149dc 66 6f 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a for each prior.*
149dd 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 * call to sqlite
149de 33 42 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 3BtreeGetPage..*
149df 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
149e0 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 leasePage(MemPag
149e1 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 e *pPage){. if(
149e2 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 pPage ){. as
149e3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
149e4 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c erflow==0 || sql
149e5 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
149e6 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 count(pPage->pDb
149e7 50 61 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 Page)>1 );. a
149e8 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 ssert( pPage->aD
149e9 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ata );. asser
149ea 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b t( pPage->pBt );
149eb 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
149ec 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
149ed 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 a(pPage->pDbPage
149ee 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 ) == (void*)pPag
149ef 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 e );. assert(
149f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
149f1 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 Data(pPage->pDbP
149f2 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 age)==pPage->aDa
149f3 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ta );. assert
149f4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
149f5 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
149f6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 >mutex) );. s
149f7 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
149f8 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
149f9 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
149fa 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b uring a rollback
149fb 2c 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 , when the pager
149fc 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 reloads informa
149fd 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 tion into the ca
149fe 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 che.** so that t
149ff 68 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74 he cache is rest
14a00 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 ored to its orig
14a01 69 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 inal state at th
14a02 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 e start of.** th
14a03 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 e transaction, f
14a04 6f 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73 or each page res
14a05 74 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 tored this routi
14a06 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a ne is called..**
14a07 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
14a08 20 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 needs to reset
14a09 74 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73 the extra data s
14a0a 65 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e ection at the en
14a0b 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 d of the.** page
14a0c 20 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 to agree with t
14a0d 68 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 he restored data
14a0e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
14a0f 20 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 pageReinit(DbPa
14a10 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 ge *pData){. Me
14a11 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 mPage *pPage;.
14a12 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 pPage = (MemPage
14a13 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
14a14 65 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a etExtra(pData);.
14a15 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 if( pPage->isI
14a16 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 nit ){. asser
14a17 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14a18 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
14a19 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 ->mutex) );.
14a1a 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
14a1b 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 0;. if( sqlit
14a1c 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f e3PagerPageRefco
14a1d 75 6e 74 28 70 44 61 74 61 29 3e 30 20 29 7b 0a unt(pData)>0 ){.
14a1e 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d /* pPage m
14a1f 69 67 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74 ight not be a bt
14a20 72 65 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69 ree page; it mi
14a21 67 68 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c ght be an overfl
14a22 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a ow page. **
14a23 20 6f 72 20 70 74 72 6d 61 70 20 70 61 67 65 20 or ptrmap page
14a24 6f 72 20 61 20 66 72 65 65 20 70 61 67 65 2e 20 or a free page.
14a25 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c In those cases,
14a26 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 the following.
14a27 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 ** call to
14a28 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
14a29 50 61 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 Page() will like
14a2a 6c 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ly return SQLITE
14a2b 5f 43 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20 _CORRUPT..
14a2c 2a 2a 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 ** But no harm i
14a2d 73 20 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 s done by this.
14a2e 20 41 6e 64 20 69 74 20 69 73 20 76 65 72 79 20 And it is very
14a2f 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 important that.
14a30 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 ** sqlite3B
14a31 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62 treeInitPage() b
14a32 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 e called on ever
14a33 79 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20 y btree page so
14a34 77 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a we make. **
14a35 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 the call for ev
14a36 65 72 79 20 70 61 67 65 20 74 68 61 74 20 63 6f ery page that co
14a37 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e mes in for re-in
14a38 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 iting. */.
14a39 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
14a3a 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
14a3b 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
14a3c 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 Invoke the busy
14a3d 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74 handler for a bt
14a3e 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ree..*/.static i
14a3f 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 nt btreeInvokeBu
14a40 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a syHandler(void *
14a41 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65 pArg){. BtShare
14a42 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72 d *pBt = (BtShar
14a43 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65 ed*)pArg;. asse
14a44 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 rt( pBt->db );.
14a45 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14a46 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
14a47 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
14a48 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 return sqlite3I
14a49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
14a4a 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 (&pBt->db->busyH
14a4b 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a andler);.}../*.*
14a4c 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73 * Open a databas
14a4d 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a e file..** .** z
14a4e 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 Filename is the
14a4f 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
14a50 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a base file. If z
14a51 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c Filename is NULL
14a52 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61 .** a new databa
14a53 73 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d se with a random
14a54 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64 name is created
14a55 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 . This randomly
14a56 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 named.** databa
14a57 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 se file will be
14a58 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c deleted when sql
14a59 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29 ite3BtreeClose()
14a5a 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 is called..** I
14a5b 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 f zFilename is "
14a5c 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 :memory:" then a
14a5d 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
14a5e 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 0a base is created.
14a5f 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d ** that is autom
14a60 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 atically destroy
14a61 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c ed when it is cl
14a62 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f osed..*/.SQLITE_
14a63 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14a64 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 te3BtreeOpen(.
14a65 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
14a66 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 ename, /* Name
14a67 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 of the file cont
14a68 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 aining the BTree
14a69 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 database */. s
14a6a 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
14a6b 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 /* Associ
14a6c 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 61 ated database ha
14a6d 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 ndle */. Btree
14a6e 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 20 **ppBtree,
14a6f 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
14a70 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 new Btree object
14a71 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
14a72 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
14a73 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
14a74 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76 tions */. int v
14a75 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 fsFlags
14a76 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 /* Flags pass
14a77 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 ed through to sq
14a78 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
14a79 29 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 ) */.){. sqlite
14a7a 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 3_vfs *pVfs;
14a7b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
14a7c 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 74 VFS to use for t
14a7d 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 his btree */. B
14a7e 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30 tShared *pBt = 0
14a7f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
14a80 20 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 20 Shared part of
14a81 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 btree structure
14a82 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20 */. Btree *p;
14a83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a84 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f /* Handle to
14a85 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c return */. sql
14a86 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
14a87 78 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 xOpen = 0; /* P
14a88 72 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 63 revents a race c
14a89 6f 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74 ondition. Ticket
14a8a 20 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20 #3537 */. int
14a8b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
14a8c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
14a8d 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 sult code from t
14a8e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a his function */.
14a8f 20 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20 u8 nReserve;
14a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a91 20 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73 /* Byte of unus
14a92 65 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 ed space on each
14a93 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 page */. unsig
14a94 6e 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 ned char zDbHead
14a95 65 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 er[100]; /* Dat
14a96 61 62 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e abase header con
14a97 74 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 tent */.. /* Se
14a98 74 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 69 t the variable i
14a99 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66 sMemdb to true f
14a9a 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 or an in-memory
14a9b 64 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 database, or .
14a9c 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66 ** false for a f
14a9d 69 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62 61 ile-based databa
14a9e 73 65 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 se. This symbol
14a9f 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 is only required
14aa0 20 69 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 if. ** either
14aa1 6f 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 of the shared-da
14aa2 74 61 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d ta or autovacuum
14aa3 20 66 65 61 74 75 72 65 73 20 61 72 65 20 63 6f features are co
14aa4 6d 70 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 mpiled . ** int
14aa5 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 o the library..
14aa6 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 */.#if !defined
14aa7 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 (SQLITE_OMIT_SHA
14aa8 52 45 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 RED_CACHE) || !d
14aa9 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
14aaa 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 IT_AUTOVACUUM).
14aab 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifdef SQLITE_O
14aac 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 MIT_MEMORYDB.
14aad 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d const int isMem
14aae 64 62 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a db = 0;. #else.
14aaf 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 const int is
14ab0 4d 65 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d Memdb = zFilenam
14ab1 65 20 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69 e && !strcmp(zFi
14ab2 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 lename, ":memory
14ab3 3a 22 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 :");. #endif.#e
14ab4 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 ndif.. assert(
14ab5 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 db!=0 );. asser
14ab6 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14ab7 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
14ab8 20 29 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 );.. pVfs = db
14ab9 2d 3e 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 ->pVfs;. p = sq
14aba 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 lite3MallocZero(
14abb 73 69 7a 65 6f 66 28 42 74 72 65 65 29 29 3b 0a sizeof(Btree));.
14abc 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 if( !p ){.
14abd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
14abe 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e MEM;. }. p->in
14abf 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f Trans = TRANS_NO
14ac0 4e 45 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 NE;. p->db = db
14ac1 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 ;..#if !defined(
14ac2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
14ac3 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 ED_CACHE) && !de
14ac4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
14ac5 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 T_DISKIO). /*.
14ac6 20 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 ** If this Btre
14ac7 65 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 e is a candidate
14ac8 20 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 for shared cach
14ac9 65 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 e, try to find a
14aca 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 n. ** existing
14acb 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 BtShared object
14acc 74 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 that we can shar
14acd 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 e with. */. if
14ace 28 20 69 73 4d 65 6d 64 62 3d 3d 30 0a 20 20 20 ( isMemdb==0.
14acf 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 && (db->flags &
14ad0 53 51 4c 49 54 45 5f 56 74 61 62 29 3d 3d 30 0a SQLITE_Vtab)==0.
14ad1 20 20 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 && zFilename
14ad2 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 0a && zFilename[0].
14ad3 20 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
14ad4 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
14ad5 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 .sharedCacheEnab
14ad6 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 led ){. int
14ad7 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d nFullPathname =
14ad8 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
14ad9 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 e+1;. char
14ada 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d *zFullPathname =
14adb 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e sqlite3Malloc(n
14adc 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 FullPathname);.
14add 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
14ade 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b ex *mutexShared;
14adf 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 . p->sharab
14ae0 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 64 62 le = 1;. db
14ae1 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 ->flags |= SQLIT
14ae2 45 5f 53 68 61 72 65 64 43 61 63 68 65 3b 0a 20 E_SharedCache;.
14ae3 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 if( !zFullP
14ae4 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 athname ){.
14ae5 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
14ae6 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 p);. retu
14ae7 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
14ae8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
14ae9 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 qlite3OsFullPath
14aea 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 name(pVfs, zFile
14aeb 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e name, nFullPathn
14aec 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 ame, zFullPathna
14aed 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 me);. mutex
14aee 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75 Open = sqlite3Mu
14aef 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
14af0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 MUTEX_STATIC_OPE
14af1 4e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 N);. sqlite
14af2 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
14af3 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 texOpen);.
14af4 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 mutexShared = sq
14af5 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
14af6 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
14af7 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 TIC_MASTER);.
14af8 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
14af9 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 _enter(mutexShar
14afa 65 64 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 ed);. for(p
14afb 42 74 3d 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 Bt=GLOBAL(BtShar
14afc 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 ed*,sqlite3Share
14afd 64 43 61 63 68 65 4c 69 73 74 29 3b 20 70 42 74 dCacheList); pBt
14afe 3b 20 70 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 ; pBt=pBt->pNext
14aff 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
14b00 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 t( pBt->nRef>0 )
14b01 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d ;. if( 0=
14b02 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 =strcmp(zFullPat
14b03 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 hname, sqlite3Pa
14b04 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d gerFilename(pBt-
14b05 3e 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 >pPager)).
14b06 20 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71 && sq
14b07 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 70 42 lite3PagerVfs(pB
14b08 74 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73 t->pPager)==pVfs
14b09 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d ){. p-
14b0a 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 >pBt = pBt;.
14b0b 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b pBt->nRef+
14b0c 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 +;. bre
14b0d 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
14b0e 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
14b0f 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
14b10 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 mutexShared);.
14b11 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
14b12 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b (zFullPathname);
14b13 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 . }.#ifdef SQ
14b14 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 LITE_DEBUG. e
14b15 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e lse{. /* In
14b16 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 debug mode, we
14b17 6d 61 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 mark all persist
14b18 65 6e 74 20 64 61 74 61 62 61 73 65 73 20 61 73 ent databases as
14b19 20 73 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 sharable.
14b1a 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 ** even when the
14b1b 79 20 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 y are not. This
14b1c 20 65 78 65 72 63 69 73 65 73 20 74 68 65 20 6c exercises the l
14b1d 6f 63 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a ocking code and.
14b1e 20 20 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d ** gives m
14b1f 6f 72 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 ore opportunity
14b20 66 6f 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 for asserts(sqli
14b21 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
14b22 29 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 ). ** state
14b23 6d 65 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f ments to find lo
14b24 63 6b 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a cking problems..
14b25 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 */. p
14b26 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a ->sharable = 1;.
14b27 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
14b28 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 .#endif. if( pB
14b29 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 t==0 ){. /*.
14b2a 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 ** The follow
14b2b 69 6e 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 ing asserts make
14b2c 20 73 75 72 65 20 74 68 61 74 20 73 74 72 75 63 sure that struc
14b2d 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 tures used by th
14b2e 65 20 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 e btree are.
14b2f 2a 2a 20 74 68 65 20 72 69 67 68 74 20 73 69 7a ** the right siz
14b30 65 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 e. This is to g
14b31 75 61 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a uard against siz
14b32 65 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 e changes that r
14b33 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 esult. ** whe
14b34 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 n compiling on a
14b35 20 64 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 different archi
14b36 74 65 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a tecture.. */.
14b37 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
14b38 6f 66 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69 of(i64)==8 || si
14b39 7a 65 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a zeof(i64)==4 );.
14b3a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
14b3b 6f 66 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69 of(u64)==8 || si
14b3c 7a 65 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a zeof(u64)==4 );.
14b3d 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
14b3e 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 of(u32)==4 );.
14b3f 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
14b40 28 75 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20 (u16)==2 );.
14b41 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50 assert( sizeof(P
14b42 67 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 gno)==4 );. .
14b43 20 20 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d pBt = sqlite3M
14b44 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
14b45 66 28 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69 f(*pBt) );. i
14b46 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 f( pBt==0 ){.
14b47 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
14b48 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f OMEM;. goto
14b49 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b btree_open_out;
14b4a 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
14b4b 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
14b4c 28 70 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 (pVfs, &pBt->pPa
14b4d 67 65 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a ger, zFilename,.
14b4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14b4f 20 20 20 20 20 20 20 20 20 20 45 58 54 52 41 5f EXTRA_
14b50 53 49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 SIZE, flags, vfs
14b51 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 Flags);. if(
14b52 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
14b53 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
14b54 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 te3PagerReadFile
14b55 68 65 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 header(pBt->pPag
14b56 65 72 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 er,sizeof(zDbHea
14b57 64 65 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b der),zDbHeader);
14b58 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
14b59 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
14b5a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 goto btree
14b5b 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d _open_out;. }
14b5c 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
14b5d 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 rSetBusyhandler(
14b5e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 62 74 72 pBt->pPager, btr
14b5f 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 eeInvokeBusyHand
14b60 6c 65 72 2c 20 70 42 74 29 3b 0a 20 20 20 20 70 ler, pBt);. p
14b61 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a ->pBt = pBt;. .
14b62 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
14b63 53 65 74 52 65 69 6e 69 74 65 72 28 70 42 74 2d SetReiniter(pBt-
14b64 3e 70 50 61 67 65 72 2c 20 70 61 67 65 52 65 69 >pPager, pageRei
14b65 6e 69 74 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 nit);. pBt->p
14b66 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 Cursor = 0;.
14b67 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b pBt->pPage1 = 0;
14b68 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e . pBt->readOn
14b69 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ly = sqlite3Page
14b6a 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d rIsreadonly(pBt-
14b6b 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 >pPager);. pB
14b6c 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 t->pageSize = ge
14b6d 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 t2byte(&zDbHeade
14b6e 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 r[16]);. if(
14b6f 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 pBt->pageSize<51
14b70 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 2 || pBt->pageSi
14b71 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ze>SQLITE_MAX_PA
14b72 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 GE_SIZE.
14b73 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 || ((pBt->pageS
14b74 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 ize-1)&pBt->page
14b75 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Size)!=0 ){.
14b76 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
14b77 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 = 0;. sqlit
14b78 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 e3PagerSetPagesi
14b79 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ze(pBt->pPager,
14b7a 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b &pBt->pageSize);
14b7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14b7c 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
14b7d 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
14b7e 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d magic name ":mem
14b7f 6f 72 79 3a 22 20 77 69 6c 6c 20 63 72 65 61 74 ory:" will creat
14b80 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 e an in-memory d
14b81 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 20 20 atabase, then.
14b82 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 74 68 65 ** leave the
14b83 20 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65 autoVacuum mode
14b84 20 61 74 20 30 20 28 64 6f 20 6e 6f 74 20 61 75 at 0 (do not au
14b85 74 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76 65 6e to-vacuum), even
14b86 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c if. ** SQL
14b87 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
14b88 56 41 43 55 55 4d 20 69 73 20 74 72 75 65 2e 20 VACUUM is true.
14b89 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
14b8a 64 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 d, if. ** S
14b8b 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 QLITE_OMIT_MEMOR
14b8c 59 44 42 20 68 61 73 20 62 65 65 6e 20 64 65 66 YDB has been def
14b8d 69 6e 65 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d ined, then ":mem
14b8e 6f 72 79 3a 22 20 69 73 20 6a 75 73 74 20 61 0a ory:" is just a.
14b8f 20 20 20 20 20 20 2a 2a 20 72 65 67 75 6c 61 72 ** regular
14b90 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74 file-name. In t
14b91 68 69 73 20 63 61 73 65 20 74 68 65 20 61 75 74 his case the aut
14b92 6f 2d 76 61 63 75 75 6d 20 61 70 70 6c 69 65 73 o-vacuum applies
14b93 20 61 73 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a as per normal..
14b94 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
14b95 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 f( zFilename &&
14b96 21 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 !isMemdb ){.
14b97 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 pBt->autoVac
14b98 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 uum = (SQLITE_DE
14b99 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d FAULT_AUTOVACUUM
14b9a 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 ? 1 : 0);.
14b9b 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 pBt->incrVacu
14b9c 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 um = (SQLITE_DEF
14b9d 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d AULT_AUTOVACUUM=
14b9e 3d 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 =2 ? 1 : 0);.
14b9f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
14ba0 20 20 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0a nReserve = 0;.
14ba1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
14ba2 20 6e 52 65 73 65 72 76 65 20 3d 20 7a 44 62 48 nReserve = zDbH
14ba3 65 61 64 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20 eader[20];.
14ba4 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 pBt->pageSizeFi
14ba5 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 xed = 1;.#ifndef
14ba6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
14ba7 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 70 42 OVACUUM. pB
14ba8 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 t->autoVacuum =
14ba9 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 (get4byte(&zDbHe
14baa 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 5d 29 3f ader[36 + 4*4])?
14bab 31 3a 30 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 1:0);. pBt-
14bac 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 >incrVacuum = (g
14bad 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 et4byte(&zDbHead
14bae 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a er[36 + 7*4])?1:
14baf 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0);.#endif. }
14bb0 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 . pBt->usable
14bb1 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 Size = pBt->page
14bb2 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b Size - nReserve;
14bb3 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42 . assert( (pB
14bb4 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 t->pageSize & 7)
14bb5 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 ==0 ); /* 8-byt
14bb6 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 e alignment of p
14bb7 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 20 73 ageSize */. s
14bb8 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
14bb9 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 gesize(pBt->pPag
14bba 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 er, &pBt->pageSi
14bbb 7a 65 29 3b 0a 20 20 20 0a 23 69 66 20 21 64 65 ze);. .#if !de
14bbc 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
14bbd 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 T_SHARED_CACHE)
14bbe 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
14bbf 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a TE_OMIT_DISKIO).
14bc0 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e /* Add the n
14bc1 65 77 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 ew BtShared obje
14bc2 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 ct to the linked
14bc3 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65 20 42 list sharable B
14bc4 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f tShareds.. */
14bc5 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 . if( p->shar
14bc6 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 able ){. sq
14bc7 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
14bc8 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 exShared;.
14bc9 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 pBt->nRef = 1;.
14bca 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 mutexShared
14bcb 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
14bcc 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
14bcd 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
14bce 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 ;. if( SQLI
14bcf 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
14bd0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
14bd1 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 nfig.bCoreMutex
14bd2 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e ){. pBt->
14bd3 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
14bd4 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
14bd5 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 _MUTEX_FAST);.
14bd6 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d if( pBt->m
14bd7 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 utex==0 ){.
14bd8 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
14bd9 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
14bda 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
14bdb 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ed = 0;.
14bdc 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 goto btree_ope
14bdd 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d n_out;. }
14bde 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
14bdf 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
14be0 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b er(mutexShared);
14be1 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78 . pBt->pNex
14be2 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 t = GLOBAL(BtSha
14be3 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
14be4 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 edCacheList);.
14be5 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 GLOBAL(BtSha
14be6 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
14be7 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 edCacheList) = p
14be8 42 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 Bt;. sqlite
14be9 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
14bea 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 texShared);.
14beb 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 }.#endif. }..#i
14bec 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
14bed 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
14bee 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 CHE) && !defined
14bef 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 (SQLITE_OMIT_DIS
14bf0 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 KIO). /* If the
14bf1 20 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20 new Btree uses
14bf2 61 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 a sharable pBtSh
14bf3 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 ared, then link
14bf4 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 the new. ** Btr
14bf5 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 ee into the list
14bf6 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 of all sharable
14bf7 20 42 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 Btrees for the
14bf8 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e same connection.
14bf9 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 . ** The list i
14bfa 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 s kept in ascend
14bfb 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 ing order by pBt
14bfc 20 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 address.. */.
14bfd 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 if( p->sharable
14bfe 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
14bff 20 20 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a Btree *pSib;.
14c00 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 for(i=0; i<d
14c01 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
14c02 20 20 20 20 69 66 28 20 28 70 53 69 62 20 3d 20 if( (pSib =
14c03 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 db->aDb[i].pBt)!
14c04 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72 =0 && pSib->shar
14c05 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 able ){.
14c06 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72 while( pSib->pPr
14c07 65 76 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69 ev ){ pSib = pSi
14c08 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20 b->pPrev; }.
14c09 20 20 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70 if( p->pBt<p
14c0a 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 Sib->pBt ){.
14c0b 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d p->pNext =
14c0c 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 pSib;.
14c0d 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 p->pPrev = 0;.
14c0e 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 pSib->p
14c0f 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 Prev = p;.
14c10 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
14c11 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e while( pSib->
14c12 70 4e 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 pNext && pSib->p
14c13 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 Next->pBt<p->pBt
14c14 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
14c15 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 pSib = pSib->pNe
14c16 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a xt;. }.
14c17 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 p->pNe
14c18 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 xt = pSib->pNext
14c19 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 ;. p->p
14c1a 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 Prev = pSib;.
14c1b 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e if( p->pN
14c1c 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ext ){.
14c1d 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 p->pNext->pPr
14c1e 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 ev = p;.
14c1f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 }. pS
14c20 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 ib->pNext = p;.
14c21 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
14c22 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
14c23 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
14c24 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b . *ppBtree = p;
14c25 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 ..btree_open_out
14c26 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 :. if( rc!=SQLI
14c27 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
14c28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 pBt && pBt->pPa
14c29 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ger ){. sql
14c2a 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 ite3PagerClose(p
14c2b 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
14c2c 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 }. sqlite3_f
14c2d 72 65 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 ree(pBt);. sq
14c2e 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
14c2f 20 20 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b *ppBtree = 0;
14c30 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 74 65 78 . }. if( mutex
14c31 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 73 73 65 Open ){. asse
14c32 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14c33 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f 70 65 6e x_held(mutexOpen
14c34 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) );. sqlite3
14c35 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
14c36 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 20 72 exOpen);. }. r
14c37 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
14c38 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 ** Decrement the
14c39 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 BtShared.nRef c
14c3a 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 74 ounter. When it
14c3b 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a 2a reaches zero,.*
14c3c 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 74 53 * remove the BtS
14c3d 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 hared structure
14c3e 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 6e 67 from the sharing
14c3f 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 0a 2a list. Return.*
14c40 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 42 74 * true if the Bt
14c41 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e Shared.nRef coun
14c42 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 72 6f ter reaches zero
14c43 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 66 and return.** f
14c44 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 74 alse if it is st
14c45 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2f ill positive..*/
14c46 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f .static int remo
14c47 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 veFromSharingLis
14c48 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 t(BtShared *pBt)
14c49 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
14c4a 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
14c4b 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 HE. sqlite3_mut
14c4c 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 42 ex *pMaster;. B
14c4d 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a tShared *pList;.
14c4e 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20 int removed =
14c4f 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 0;.. assert( sq
14c50 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
14c51 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
14c52 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73 );. pMaster = s
14c53 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
14c54 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
14c55 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 ATIC_MASTER);.
14c56 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
14c57 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ter(pMaster);.
14c58 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 pBt->nRef--;. i
14c59 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 f( pBt->nRef<=0
14c5a 29 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41 ){. if( GLOBA
14c5b 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
14c5c 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
14c5d 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 st)==pBt ){.
14c5e 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 GLOBAL(BtShare
14c5f 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
14c60 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 CacheList) = pBt
14c61 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c ->pNext;. }el
14c62 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20 se{. pList
14c63 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 = GLOBAL(BtShare
14c64 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
14c65 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 CacheList);.
14c66 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 while( ALWAYS(
14c67 70 4c 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d pList) && pList-
14c68 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20 >pNext!=pBt ){.
14c69 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69 pList=pLi
14c6a 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 st->pNext;.
14c6b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 }. if( ALW
14c6c 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20 AYS(pList) ){.
14c6d 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65 pList->pNe
14c6e 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b xt = pBt->pNext;
14c6f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
14c70 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 if( SQLITE_TH
14c71 52 45 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20 READSAFE ){.
14c72 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
14c73 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29 free(pBt->mutex)
14c74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f ;. }. remo
14c75 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 ved = 1;. }. s
14c76 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
14c77 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 ve(pMaster);. r
14c78 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 eturn removed;.#
14c79 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b else. return 1;
14c7a 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
14c7b 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e Make sure pBt->
14c7c 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73 pTmpSpace points
14c7d 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f to an allocatio
14c7e 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c n of .** MX_CELL
14c7f 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74 65 73 _SIZE(pBt) bytes
14c80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
14c81 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 allocateTempSpa
14c82 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ce(BtShared *pBt
14c83 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 ){. if( !pBt->p
14c84 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 TmpSpace ){.
14c85 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d pBt->pTmpSpace =
14c86 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c sqlite3PageMall
14c87 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a oc( pBt->pageSiz
14c88 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a e );. }.}../*.*
14c89 2a 20 46 72 65 65 20 74 68 65 20 70 42 74 2d 3e * Free the pBt->
14c8a 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 61 pTmpSpace alloca
14c8b 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tion.*/.static v
14c8c 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70 61 63 oid freeTempSpac
14c8d 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 e(BtShared *pBt)
14c8e 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 {. sqlite3PageF
14c8f 72 65 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 ree( pBt->pTmpSp
14c90 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e 70 54 6d ace);. pBt->pTm
14c91 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f pSpace = 0;.}../
14c92 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 *.** Close an op
14c93 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 en database and
14c94 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 63 invalidate all c
14c95 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ursors..*/.SQLIT
14c96 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14c97 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 lite3BtreeClose(
14c98 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 Btree *p){. BtS
14c99 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
14c9a 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 pBt;. BtCursor
14c9b 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f *pCur;.. /* Clo
14c9c 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f se all cursors o
14c9d 70 65 6e 65 64 20 76 69 61 20 74 68 69 73 20 68 pened via this h
14c9e 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 andle. */. ass
14c9f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14ca0 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
14ca1 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
14ca2 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
14ca3 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e . pBt->db = p->
14ca4 64 62 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74 db;. pCur = pBt
14ca5 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69 ->pCursor;. whi
14ca6 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 le( pCur ){.
14ca7 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d BtCursor *pTmp =
14ca8 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20 pCur;. pCur
14ca9 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 = pCur->pNext;.
14caa 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 if( pTmp->pBt
14cab 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree==p ){.
14cac 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
14cad 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20 eCursor(pTmp);.
14cae 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 }. }.. /* R
14caf 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69 ollback any acti
14cb0 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 ve transaction a
14cb1 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64 nd free the hand
14cb2 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 le structure..
14cb3 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 ** The call to s
14cb4 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
14cb5 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20 ack() drops any
14cb6 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 table-locks held
14cb7 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 by. ** this ha
14cb8 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c ndle.. */. sql
14cb9 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 ite3BtreeRollbac
14cba 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 k(p);. sqlite3B
14cbb 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 treeLeave(p);..
14cbc 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 /* If there are
14cbd 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 still other out
14cbe 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e standing referen
14cbf 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65 ces to the share
14cc0 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 d-btree. ** str
14cc1 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e ucture, return n
14cc2 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65 ow. The remainde
14cc3 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 r of this proced
14cc4 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a ure cleans . **
14cc5 20 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62 up the shared-b
14cc6 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 tree.. */. ass
14cc7 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f ert( p->wantToLo
14cc8 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b ck==0 && p->lock
14cc9 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 ed==0 );. if( !
14cca 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 p->sharable || r
14ccb 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 emoveFromSharing
14ccc 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20 List(pBt) ){.
14ccd 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e /* The pBt is n
14cce 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 o longer on the
14ccf 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f sharing list, so
14cd0 20 77 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20 we can access.
14cd1 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 ** it without
14cd2 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 having to hold
14cd3 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a the mutex.. *
14cd4 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f *. ** Clean o
14cd5 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 ut and delete th
14cd6 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
14cd7 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 t.. */. as
14cd8 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72 sert( !pBt->pCur
14cd9 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 sor );. sqlit
14cda 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 e3PagerClose(pBt
14cdb 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 ->pPager);. i
14cdc 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 f( pBt->xFreeSch
14cdd 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68 ema && pBt->pSch
14cde 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ema ){. pBt
14cdf 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42 ->xFreeSchema(pB
14ce0 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 t->pSchema);.
14ce1 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 }. sqlite3_f
14ce2 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 ree(pBt->pSchema
14ce3 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 );. freeTempS
14ce4 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73 pace(pBt);. s
14ce5 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 qlite3_free(pBt)
14ce6 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ;. }..#ifndef S
14ce7 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
14ce8 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74 D_CACHE. assert
14ce9 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
14cea 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
14ceb 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a p->locked==0 );.
14cec 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 if( p->pPrev )
14ced 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 p->pPrev->pNext
14cee 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 = p->pNext;. i
14cef 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d f( p->pNext ) p-
14cf0 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 >pNext->pPrev =
14cf1 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 p->pPrev;.#endif
14cf2 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 .. sqlite3_free
14cf3 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
14cf4 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
14cf5 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d * Change the lim
14cf6 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 it on the number
14cf7 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 of pages allowe
14cf8 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a d in the cache..
14cf9 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 **.** The maximu
14cfa 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68 m number of cach
14cfb 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74 e pages is set t
14cfc 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a o the absolute.*
14cfd 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 * value of mxPag
14cfe 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73 e. If mxPage is
14cff 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70 negative, the p
14d00 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 ager will.** ope
14d01 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 rate asynchronou
14d02 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f sly - it will no
14d03 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 t stop to do fsy
14d04 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 nc()s.** to insu
14d05 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 re data is writt
14d06 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73 en to the disk s
14d07 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a urface before.**
14d08 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 continuing. Tr
14d09 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c ansactions still
14d0a 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f work if synchro
14d0b 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 nous is off,.**
14d0c 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 and the database
14d0d 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 cannot be corru
14d0e 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f pted if this pro
14d0f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e gram.** crashes.
14d10 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65 But if the ope
14d11 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72 rating system cr
14d12 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69 ashes or there i
14d13 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70 s.** an abrupt p
14d14 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65 ower failure whe
14d15 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 n synchronous is
14d16 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61 off, the databa
14d17 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c se.** could be l
14d18 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 eft in an incons
14d19 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 istent and unrec
14d1a 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a overable state..
14d1b 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 ** Synchronous i
14d1c 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20 s on by default
14d1d 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 so database corr
14d1e 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a uption is not.**
14d1f 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 normally a worr
14d20 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
14d21 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
14d22 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a BtreeSetCacheSiz
14d23 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 e(Btree *p, int
14d24 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61 mxPage){. BtSha
14d25 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
14d26 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c t;. assert( sql
14d27 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14d28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
14d29 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
14d2a 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 nter(p);. sqlit
14d2b 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 e3PagerSetCaches
14d2c 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ize(pBt->pPager,
14d2d 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 mxPage);. sqli
14d2e 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
14d2f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
14d30 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
14d31 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 61 hange the way da
14d32 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20 ta is synced to
14d33 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f disk in order to
14d34 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63 increase or dec
14d35 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c rease.** how wel
14d36 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 l the database r
14d37 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 75 esists damage du
14d38 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20 e to OS crashes
14d39 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 and power.** fai
14d3a 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20 lures. Level 1
14d3b 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 is the same as a
14d3c 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20 synchronous (no
14d3d 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e syncs() occur an
14d3e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 d.** there is a
14d3f 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 high probability
14d40 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76 of damage) Lev
14d41 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 61 el 2 is the defa
14d42 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69 ult. There.** i
14d43 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74 s a very low but
14d44 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 non-zero probab
14d45 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e ility of damage.
14d46 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65 Level 3 reduce
14d47 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69 s the.** probabi
14d48 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74 lity of damage t
14d49 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20 o near zero but
14d4a 77 69 74 68 20 61 20 77 72 69 74 65 20 70 65 72 with a write per
14d4b 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69 formance reducti
14d4c 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 on..*/.#ifndef S
14d4d 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 QLITE_OMIT_PAGER
14d4e 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f _PRAGMAS.SQLITE_
14d4f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14d50 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74 te3BtreeSetSafet
14d51 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c yLevel(Btree *p,
14d52 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 int level, int
14d53 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53 fullSync){. BtS
14d54 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
14d55 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
14d56 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14d57 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
14d58 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
14d59 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c eEnter(p);. sql
14d5a 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 ite3PagerSetSafe
14d5b 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 tyLevel(pBt->pPa
14d5c 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c ger, level, full
14d5d 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 Sync);. sqlite3
14d5e 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
14d5f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
14d60 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a K;.}.#endif../*.
14d61 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
14d62 66 20 74 68 65 20 67 69 76 65 6e 20 62 74 72 65 f the given btre
14d63 65 20 69 73 20 73 65 74 20 74 6f 20 73 61 66 65 e is set to safe
14d64 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 ty level 1. In
14d65 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 other.** words,
14d66 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e return TRUE if n
14d67 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20 o sync() occurs
14d68 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 on the disk file
14d69 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
14d6a 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
14d6b 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 BtreeSyncDisable
14d6c 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 d(Btree *p){. B
14d6d 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
14d6e 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b ->pBt;. int rc;
14d6f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
14d70 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
14d71 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 >db->mutex) );
14d72 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
14d73 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 nter(p);. asser
14d74 74 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 t( pBt && pBt->p
14d75 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 Pager );. rc =
14d76 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 sqlite3PagerNosy
14d77 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b nc(pBt->pPager);
14d78 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
14d79 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
14d7a 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 n rc;.}..#if !de
14d7b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
14d7c 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 T_PAGER_PRAGMAS)
14d7d 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
14d7e 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 ITE_OMIT_VACUUM)
14d7f 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
14d80 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 e default pages
14d81 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d size and the num
14d82 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 ber of reserved
14d83 62 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a bytes per page..
14d84 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 73 **.** The page s
14d85 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f ize must be a po
14d86 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e wer of 2 between
14d87 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e 20 512 and 65536.
14d88 20 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 If the page.**
14d89 73 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64 6f size supplied do
14d8a 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 73 es not meet this
14d8b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 6e constraint then
14d8c 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 the page size i
14d8d 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64 s not.** changed
14d8e 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a ..**.** Page siz
14d8f 65 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e es are constrain
14d90 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 ed to be a power
14d91 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20 of two so that
14d92 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 the region.** of
14d93 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
14d94 6c 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b le used for lock
14d95 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61 ing (beginning a
14d96 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a t PENDING_BYTE,.
14d97 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74 ** the first byt
14d98 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62 e past the 1GB b
14d99 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30 30 oundary, 0x40000
14d9a 30 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 63 000) needs to oc
14d9b 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 65 cur.** at the be
14d9c 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 67 ginning of a pag
14d9d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 e..**.** If para
14d9e 6d 65 74 65 72 20 6e 52 65 73 65 72 76 65 20 69 meter nReserve i
14d9f 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f s less than zero
14da0 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 , then the numbe
14da1 72 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a 2a r of reserved.**
14da2 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65 20 bytes per page
14da3 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 is left unchange
14da4 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
14da5 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
14da6 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 BtreeSetPageSize
14da7 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 (Btree *p, int p
14da8 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 ageSize, int nRe
14da9 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63 serve){. int rc
14daa 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
14dab 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
14dac 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 p->pBt;. assert
14dad 28 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 26 ( nReserve>=-1 &
14dae 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 & nReserve<=255
14daf 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
14db0 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 eEnter(p);. if(
14db1 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 pBt->pageSizeFi
14db2 78 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 xed ){. sqlit
14db3 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
14db4 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
14db5 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d TE_READONLY;. }
14db6 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c . if( nReserve<
14db7 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 72 76 0 ){. nReserv
14db8 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a e = pBt->pageSiz
14db9 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e - pBt->usableS
14dba 69 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 ize;. }. asser
14dbb 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 t( nReserve>=0 &
14dbc 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 & nReserve<=255
14dbd 29 3b 0a 20 20 69 66 28 20 70 61 67 65 53 69 7a );. if( pageSiz
14dbe 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 e>=512 && pageSi
14dbf 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 ze<=SQLITE_MAX_P
14dc0 41 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 20 20 AGE_SIZE &&.
14dc1 20 20 20 20 28 28 70 61 67 65 53 69 7a 65 2d 31 ((pageSize-1
14dc2 29 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 20 29 )&pageSize)==0 )
14dc3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 {. assert( (p
14dc4 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 ageSize & 7)==0
14dc5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
14dc6 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20 21 pBt->pPage1 && !
14dc7 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a pBt->pCursor );.
14dc8 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a pBt->pageSiz
14dc9 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a e = (u16)pageSiz
14dca 65 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 e;. freeTempS
14dcb 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 72 pace(pBt);. r
14dcc 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
14dcd 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d SetPagesize(pBt-
14dce 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 >pPager, &pBt->p
14dcf 61 67 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 ageSize);. }.
14dd0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
14dd1 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 = pBt->pageSize
14dd2 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b - (u16)nReserve;
14dd3 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
14dd4 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
14dd5 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
14dd6 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e eturn the curren
14dd7 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67 65 tly defined page
14dd8 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f size.*/.SQLITE_
14dd9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14dda 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 te3BtreeGetPageS
14ddb 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ize(Btree *p){.
14ddc 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e return p->pBt->
14ddd 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a pageSize;.}../*.
14dde 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
14ddf 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
14de0 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 6e space at the en
14de1 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 d of every page
14de2 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74 65 that.** are inte
14de3 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e 75 ntually left unu
14de4 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 sed. This is th
14de5 65 20 22 72 65 73 65 72 76 65 64 22 20 73 70 61 e "reserved" spa
14de6 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 73 6f ce that is.** so
14de7 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 79 20 metimes used by
14de8 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
14de9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14dea 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 t sqlite3BtreeGe
14deb 74 52 65 73 65 72 76 65 28 42 74 72 65 65 20 2a tReserve(Btree *
14dec 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 p){. int n;. s
14ded 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
14dee 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 (p);. n = p->pB
14def 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d t->pageSize - p-
14df0 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
14df1 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
14df2 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
14df3 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 rn n;.}../*.** S
14df4 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 et the maximum p
14df5 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 age count for a
14df6 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 database if mxPa
14df7 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a ge is positive..
14df8 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 ** No changes ar
14df9 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 e made if mxPage
14dfa 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 is 0 or negativ
14dfb 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 e..** Regardless
14dfc 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 of the value of
14dfd 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 mxPage, return
14dfe 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 the maximum page
14dff 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 count..*/.SQLIT
14e00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14e01 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 lite3BtreeMaxPag
14e02 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c eCount(Btree *p,
14e03 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 int mxPage){.
14e04 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 int n;. sqlite3
14e05 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
14e06 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 n = sqlite3Page
14e07 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d rMaxPageCount(p-
14e08 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 >pBt->pPager, mx
14e09 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 Page);. sqlite3
14e0a 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
14e0b 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e return n;.}.#en
14e0c 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
14e0d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 SQLITE_OMIT_PAGE
14e0e 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 R_PRAGMAS) || !d
14e0f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
14e10 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f IT_VACUUM) */../
14e11 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
14e12 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 'auto-vacuum' pr
14e13 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 operty of the da
14e14 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 tabase. If the '
14e15 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 autoVacuum'.** p
14e16 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d arameter is non-
14e17 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d zero, then auto-
14e18 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 vacuum mode is e
14e19 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c nabled. If zero,
14e1a 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c it.** is disabl
14e1b 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 ed. The default
14e1c 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 value for the au
14e1d 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 to-vacuum proper
14e1e 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d ty is .** determ
14e1f 69 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 ined by the SQLI
14e20 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 TE_DEFAULT_AUTOV
14e21 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a ACUUM macro..*/.
14e22 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
14e23 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
14e24 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 etAutoVacuum(Btr
14e25 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 ee *p, int autoV
14e26 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 acuum){.#ifdef S
14e27 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
14e28 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 ACUUM. return S
14e29 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
14e2a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 #else. BtShared
14e2b 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
14e2c 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
14e2d 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 E_OK;. u8 av =
14e2e 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a (u8)autoVacuum;.
14e2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
14e30 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 nter(p);. if( p
14e31 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 Bt->pageSizeFixe
14e32 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29 21 3d d && (av ?1:0)!=
14e33 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
14e34 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
14e35 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d TE_READONLY;. }
14e36 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61 else{. pBt->a
14e37 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 3f utoVacuum = av ?
14e38 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 1:0;. pBt->in
14e39 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d 32 crVacuum = av==2
14e3a 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 6c ?1:0;. }. sql
14e3b 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
14e3c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
14e3d 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
14e3e 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 Return the value
14e3f 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d 76 61 of the 'auto-va
14e40 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 2e 20 cuum' property.
14e41 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 If auto-vacuum i
14e42 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 20 s .** enabled 1
14e43 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 is returned. Oth
14e44 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51 4c erwise 0..*/.SQL
14e45 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
14e46 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 sqlite3BtreeGetA
14e47 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 utoVacuum(Btree
14e48 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 *p){.#ifdef SQLI
14e49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
14e4a 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 52 45 UM. return BTRE
14e4b 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e E_AUTOVACUUM_NON
14e4c 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 72 E;.#else. int r
14e4d 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 c;. sqlite3Btre
14e4e 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 eEnter(p);. rc
14e4f 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 = (. (!p->pBt
14e50 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 54 ->autoVacuum)?BT
14e51 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e REE_AUTOVACUUM_N
14e52 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 42 ONE:. (!p->pB
14e53 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f 42 t->incrVacuum)?B
14e54 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
14e55 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 5f FULL:. BTREE_
14e56 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 0a AUTOVACUUM_INCR.
14e57 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
14e58 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
14e59 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 eturn rc;.#endif
14e5a 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 .}.../*.** Get a
14e5b 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 50 reference to pP
14e5c 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 74 61 age1 of the data
14e5d 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 base file. This
14e5e 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 will.** also ac
14e5f 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f 63 6b quire a readlock
14e60 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a 2a on that file..*
14e61 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 *.** SQLITE_OK i
14e62 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 s returned on su
14e63 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 20 66 ccess. If the f
14e64 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 ile is not a.**
14e65 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 well-formed data
14e66 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 base file, then
14e67 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 SQLITE_CORRUPT i
14e68 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 s returned..** S
14e69 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 QLITE_BUSY is re
14e6a 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 64 61 turned if the da
14e6b 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 tabase is locked
14e6c 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a . SQLITE_NOMEM.
14e6d 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 ** is returned i
14e6e 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 f we run out of
14e6f 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 memory. .*/.stat
14e70 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65 ic int lockBtree
14e71 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b (BtShared *pBt){
14e72 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d . int rc;. Mem
14e73 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 Page *pPage1;.
14e74 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 int nPage;.. as
14e75 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14e76 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
14e77 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 42 tex) );. if( pB
14e78 74 2d 3e 70 50 61 67 65 31 20 29 20 72 65 74 75 t->pPage1 ) retu
14e79 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
14e7a 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
14e7b 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c eGetPage(pBt, 1,
14e7c 20 26 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 &pPage1, 0);.
14e7d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
14e7e 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a K ) return rc;..
14e7f 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65 /* Do some che
14e80 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e cking to help in
14e81 73 75 72 65 20 74 68 65 20 66 69 6c 65 20 77 65 sure the file we
14e82 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c 79 20 69 opened really i
14e83 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 64 s. ** a valid d
14e84 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 20 atabase file. .
14e85 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
14e86 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
14e87 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e (pBt->pPager, &n
14e88 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 Page);. if( rc!
14e89 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14e8a 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 goto page1_ini
14e8b 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 65 6c 73 t_failed;. }els
14e8c 65 20 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b e if( nPage>0 ){
14e8d 0a 20 20 20 20 69 6e 74 20 70 61 67 65 53 69 7a . int pageSiz
14e8e 65 3b 0a 20 20 20 20 69 6e 74 20 75 73 61 62 6c e;. int usabl
14e8f 65 53 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 eSize;. u8 *p
14e90 61 67 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 age1 = pPage1->a
14e91 44 61 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 Data;. rc = S
14e92 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 QLITE_NOTADB;.
14e93 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 if( memcmp(pag
14e94 65 31 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 e1, zMagicHeader
14e95 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 , 16)!=0 ){.
14e96 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 goto page1_ini
14e97 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a t_failed;. }.
14e98 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 38 if( page1[18
14e99 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ]>1 ){. pBt
14e9a 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a ->readOnly = 1;.
14e9b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 61 }. if( pa
14e9c 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 ge1[19]>1 ){.
14e9d 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e goto page1_in
14e9e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d it_failed;. }
14e9f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 .. /* The max
14ea0 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 20 66 72 imum embedded fr
14ea1 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 action must be e
14ea2 78 61 63 74 6c 79 20 32 35 25 2e 20 20 41 6e 64 xactly 25%. And
14ea3 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 20 the minimum.
14ea4 20 2a 2a 20 65 6d 62 65 64 64 65 64 20 66 72 61 ** embedded fra
14ea5 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 31 32 ction must be 12
14ea6 2e 35 25 20 66 6f 72 20 62 6f 74 68 20 6c 65 61 .5% for both lea
14ea7 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d 6c f-data and non-l
14ea8 65 61 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a 2a eaf-data.. **
14ea9 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 The original de
14eaa 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 sign allowed the
14eab 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 se amounts to va
14eac 72 79 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 20 ry, but as of.
14ead 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 ** version 3.6
14eae 2e 30 2c 20 77 65 20 72 65 71 75 69 72 65 20 74 .0, we require t
14eaf 68 65 6d 20 74 6f 20 62 65 20 66 69 78 65 64 2e hem to be fixed.
14eb0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
14eb1 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 31 memcmp(&page1[21
14eb2 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 ], "\100\040\040
14eb3 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ",3)!=0 ){.
14eb4 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 goto page1_init
14eb5 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 _failed;. }.
14eb6 20 20 20 70 61 67 65 53 69 7a 65 20 3d 20 67 65 pageSize = ge
14eb7 74 32 62 79 74 65 28 26 70 61 67 65 31 5b 31 36 t2byte(&page1[16
14eb8 5d 29 3b 0a 20 20 20 20 69 66 28 20 28 28 70 61 ]);. if( ((pa
14eb9 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 geSize-1)&pageSi
14eba 7a 65 29 21 3d 30 20 7c 7c 20 70 61 67 65 53 69 ze)!=0 || pageSi
14ebb 7a 65 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20 20 ze<512 ||.
14ebc 20 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 (SQLITE_MAX_PA
14ebd 47 45 5f 53 49 5a 45 3c 33 32 37 36 38 20 26 26 GE_SIZE<32768 &&
14ebe 20 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 pageSize>SQLITE
14ebf 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a _MAX_PAGE_SIZE).
14ec0 20 20 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 ){. got
14ec1 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 o page1_init_fai
14ec2 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 led;. }. a
14ec3 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 ssert( (pageSize
14ec4 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 & 7)==0 );.
14ec5 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 61 67 usableSize = pag
14ec6 65 53 69 7a 65 20 2d 20 70 61 67 65 31 5b 32 30 eSize - page1[20
14ec7 5d 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 ];. if( pageS
14ec8 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 69 ize!=pBt->pageSi
14ec9 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 ze ){. /* A
14eca 66 74 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 fter reading the
14ecb 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 first page of t
14ecc 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73 75 he database assu
14ecd 6d 69 6e 67 20 61 20 70 61 67 65 20 73 69 7a 65 ming a page size
14ece 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 . ** of BtS
14ecf 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 hared.pageSize,
14ed0 77 65 20 68 61 76 65 20 64 69 73 63 6f 76 65 72 we have discover
14ed1 65 64 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ed that the page
14ed2 2d 73 69 7a 65 20 69 73 0a 20 20 20 20 20 20 2a -size is. *
14ed3 2a 20 61 63 74 75 61 6c 6c 79 20 70 61 67 65 53 * actually pageS
14ed4 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 ize. Unlock the
14ed5 64 61 74 61 62 61 73 65 2c 20 6c 65 61 76 65 20 database, leave
14ed6 70 42 74 2d 3e 70 50 61 67 65 31 20 61 74 0a 20 pBt->pPage1 at.
14ed7 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 ** zero and
14ed8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
14ed9 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 K. The caller wi
14eda 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e ll call this fun
14edb 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61 ction. ** a
14edc 67 61 69 6e 20 77 69 74 68 20 74 68 65 20 63 6f gain with the co
14edd 72 72 65 63 74 20 70 61 67 65 2d 73 69 7a 65 2e rrect page-size.
14ede 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
14edf 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
14ee0 65 31 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e e1);. pBt->
14ee1 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 usableSize = (u1
14ee2 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 6)usableSize;.
14ee3 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a pBt->pageSiz
14ee4 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a e = (u16)pageSiz
14ee5 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 65 6d e;. freeTem
14ee6 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 pSpace(pBt);.
14ee7 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 sqlite3PagerS
14ee8 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e etPagesize(pBt->
14ee9 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 pPager, &pBt->pa
14eea 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 72 geSize);. r
14eeb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
14eec 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 . }. if( u
14eed 73 61 62 6c 65 53 69 7a 65 3c 35 30 30 20 29 7b sableSize<500 ){
14eee 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 . goto page
14eef 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 1_init_failed;.
14ef0 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61 }. pBt->pa
14ef1 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 geSize = (u16)pa
14ef2 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d geSize;. pBt-
14ef3 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 >usableSize = (u
14ef4 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 16)usableSize;.#
14ef5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14ef6 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
14ef7 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 pBt->autoVacuu
14ef8 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 m = (get4byte(&p
14ef9 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f age1[36 + 4*4])?
14efa 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 1:0);. pBt->i
14efb 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 ncrVacuum = (get
14efc 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 4byte(&page1[36
14efd 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 + 7*4])?1:0);.#e
14efe 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d ndif. }.. /* m
14eff 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d axLocal is the m
14f00 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 aximum amount of
14f01 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 payload to stor
14f02 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 e locally for.
14f03 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 ** a cell. Make
14f04 20 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c sure it is smal
14f05 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 l enough so that
14f06 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e at least minFan
14f07 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 out. ** cells c
14f08 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f an will fit on o
14f09 6e 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 ne page. We ass
14f0a 75 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 ume a 10-byte pa
14f0b 67 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 ge header.. **
14f0c 42 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c Besides the payl
14f0d 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 oad, the cell mu
14f0e 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 st store:. **
14f0f 20 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 2-byte pointe
14f10 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 r to the cell.
14f11 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 ** 4-byte ch
14f12 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a ild pointer. **
14f13 20 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 9-byte nKey
14f14 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 value. **
14f15 34 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 4-byte nData val
14f16 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 ue. ** 4-by
14f17 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 te overflow page
14f18 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f pointer. ** So
14f19 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 a cell consists
14f1a 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 of a 2-byte poi
14f1b 6e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 ner, a header wh
14f1c 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 ich is as much a
14f1d 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 s. ** 17 bytes
14f1e 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 long, 0 to N byt
14f1f 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 es of payload, a
14f20 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 nd an optional 4
14f21 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 byte overflow.
14f22 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 ** page pointer
14f23 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 .. */. pBt->ma
14f24 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 xLocal = (pBt->u
14f25 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34 sableSize-12)*64
14f26 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 /255 - 23;. pBt
14f27 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42 ->minLocal = (pB
14f28 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 t->usableSize-12
14f29 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 )*32/255 - 23;.
14f2a 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 pBt->maxLeaf =
14f2b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
14f2c 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e - 35;. pBt->min
14f2d 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61 Leaf = (pBt->usa
14f2e 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 bleSize-12)*32/2
14f2f 35 35 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72 55 - 23;. asser
14f30 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 t( pBt->maxLeaf
14f31 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f + 23 <= MX_CELL_
14f32 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70 SIZE(pBt) );. p
14f33 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 Bt->pPage1 = pPa
14f34 67 65 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ge1;. return SQ
14f35 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f LITE_OK;..page1_
14f36 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 init_failed:. r
14f37 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
14f38 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 1);. pBt->pPage
14f39 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 1 = 0;. return
14f3a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
14f3b 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 s routine works
14f3c 6c 69 6b 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 like lockBtree()
14f3d 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 except that it
14f3e 61 6c 73 6f 20 69 6e 76 6f 6b 65 73 20 74 68 65 also invokes the
14f3f 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61 63 .** busy callbac
14f40 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6c 6f k if there is lo
14f41 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 0a 2a ck contention..*
14f42 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 /.static int loc
14f43 6b 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28 kBtreeWithRetry(
14f44 42 74 72 65 65 20 2a 70 52 65 66 29 7b 0a 20 20 Btree *pRef){.
14f45 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
14f46 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 OK;.. assert( s
14f47 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
14f48 4d 75 74 65 78 28 70 52 65 66 29 20 29 3b 0a 20 Mutex(pRef) );.
14f49 20 69 66 28 20 70 52 65 66 2d 3e 69 6e 54 72 61 if( pRef->inTra
14f4a 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ns==TRANS_NONE )
14f4b 7b 0a 20 20 20 20 75 38 20 69 6e 54 72 61 6e 73 {. u8 inTrans
14f4c 61 63 74 69 6f 6e 20 3d 20 70 52 65 66 2d 3e 70 action = pRef->p
14f4d 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
14f4e 6e 3b 0a 20 20 20 20 62 74 72 65 65 49 6e 74 65 n;. btreeInte
14f4f 67 72 69 74 79 28 70 52 65 66 29 3b 0a 20 20 20 grity(pRef);.
14f50 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
14f51 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 52 65 eeBeginTrans(pRe
14f52 66 2c 20 30 29 3b 0a 20 20 20 20 70 52 65 66 2d f, 0);. pRef-
14f53 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
14f54 69 6f 6e 20 3d 20 69 6e 54 72 61 6e 73 61 63 74 ion = inTransact
14f55 69 6f 6e 3b 0a 20 20 20 20 70 52 65 66 2d 3e 69 ion;. pRef->i
14f56 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e nTrans = TRANS_N
14f57 4f 4e 45 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ONE;. if( rc=
14f58 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14f59 20 20 20 20 70 52 65 66 2d 3e 70 42 74 2d 3e 6e pRef->pBt->n
14f5a 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 Transaction--;.
14f5b 20 20 20 7d 0a 20 20 20 20 62 74 72 65 65 49 6e }. btreeIn
14f5c 74 65 67 72 69 74 79 28 70 52 65 66 29 3b 0a 20 tegrity(pRef);.
14f5d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
14f5e 7d 0a 20 20 20 20 20 20 20 0a 0a 2f 2a 0a 2a 2a }. ../*.**
14f5f 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
14f60 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 outstanding cur
14f61 73 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 sors and we are
14f62 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c not in the middl
14f63 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 e.** of a transa
14f64 63 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 ction but there
14f65 69 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f is a read lock o
14f66 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 n the database,
14f67 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 then.** this rou
14f68 74 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20 tine unrefs the
14f69 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 first page of th
14f6a 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
14f6b 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68 which .** has th
14f6c 65 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65 e effect of rele
14f6d 61 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c asing the read l
14f6e 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ock..**.** If th
14f6f 65 72 65 20 61 72 65 20 61 6e 79 20 6f 75 74 73 ere are any outs
14f70 74 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 2c tanding cursors,
14f71 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
14f72 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
14f73 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74 72 If there is a tr
14f74 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f ansaction in pro
14f75 67 72 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74 gress, this rout
14f76 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
14f77 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
14f78 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
14f79 65 64 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ed(BtShared *pBt
14f7a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
14f7b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14f7c 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
14f7d 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e if( pBt->inTran
14f7e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e saction==TRANS_N
14f7f 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70 43 75 72 ONE && pBt->pCur
14f80 73 6f 72 3d 3d 30 20 26 26 20 70 42 74 2d 3e 70 sor==0 && pBt->p
14f81 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20 Page1!=0 ){.
14f82 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 if( sqlite3Pager
14f83 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 Refcount(pBt->pP
14f84 61 67 65 72 29 3e 3d 31 20 29 7b 0a 20 20 20 20 ager)>=1 ){.
14f85 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 assert( pBt->p
14f86 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a Page1->aData );.
14f87 23 69 66 20 30 0a 20 20 20 20 20 20 69 66 28 20 #if 0. if(
14f88 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
14f89 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ta==0 ){.
14f8a 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
14f8b 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 = pBt->pPage1;.
14f8c 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 44 pPage->aD
14f8d 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ata = sqlite3Pag
14f8e 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d erGetData(pPage-
14f8f 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
14f90 20 20 20 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 pPage->pBt =
14f91 70 42 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61 pBt;. pPa
14f92 67 65 2d 3e 70 67 6e 6f 20 3d 20 31 3b 0a 20 20 ge->pgno = 1;.
14f93 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
14f94 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
14f95 42 74 2d 3e 70 50 61 67 65 31 29 3b 0a 20 20 20 Bt->pPage1);.
14f96 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 }. pBt->pPag
14f97 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f e1 = 0;. }.}../
14f98 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 *.** Create a ne
14f99 77 20 64 61 74 61 62 61 73 65 20 62 79 20 69 6e w database by in
14f9a 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 66 itializing the f
14f9b 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
14f9c 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 .** file..*/.sta
14f9d 74 69 63 20 69 6e 74 20 6e 65 77 44 61 74 61 62 tic int newDatab
14f9e 61 73 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 ase(BtShared *pB
14f9f 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 t){. MemPage *p
14fa0 50 31 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 P1;. unsigned c
14fa1 68 61 72 20 2a 64 61 74 61 3b 0a 20 20 69 6e 74 har *data;. int
14fa2 20 72 63 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 rc;. int nPage
14fa3 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
14fa4 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14fa5 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
14fa6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
14fa7 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
14fa8 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 >pPager, &nPage)
14fa9 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
14faa 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 67 65 3e 30 TE_OK || nPage>0
14fab 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
14fac 63 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20 70 c;. }. pP1 = p
14fad 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61 73 Bt->pPage1;. as
14fae 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 3b 0a sert( pP1!=0 );.
14faf 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e 61 44 data = pP1->aD
14fb0 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ata;. rc = sqli
14fb1 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
14fb2 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 1->pDbPage);. i
14fb3 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
14fb4 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 74 61 c;. memcpy(data
14fb5 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 , zMagicHeader,
14fb6 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 sizeof(zMagicHea
14fb7 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74 28 der));. assert(
14fb8 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 sizeof(zMagicHe
14fb9 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20 70 ader)==16 );. p
14fba 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 31 36 ut2byte(&data[16
14fbb 5d 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 ], pBt->pageSize
14fbc 29 3b 0a 20 20 64 61 74 61 5b 31 38 5d 20 3d 20 );. data[18] =
14fbd 31 3b 0a 20 20 64 61 74 61 5b 31 39 5d 20 3d 20 1;. data[19] =
14fbe 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 1;. assert( pBt
14fbf 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3c 3d 70 42 ->usableSize<=pB
14fc0 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 26 20 70 t->pageSize && p
14fc1 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2b 32 Bt->usableSize+2
14fc2 35 35 3e 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 55>=pBt->pageSiz
14fc3 65 29 3b 0a 20 20 64 61 74 61 5b 32 30 5d 20 3d e);. data[20] =
14fc4 20 28 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53 (u8)(pBt->pageS
14fc5 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c ize - pBt->usabl
14fc6 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32 eSize);. data[2
14fc7 31 5d 20 3d 20 36 34 3b 0a 20 20 64 61 74 61 5b 1] = 64;. data[
14fc8 32 32 5d 20 3d 20 33 32 3b 0a 20 20 64 61 74 61 22] = 32;. data
14fc9 5b 32 33 5d 20 3d 20 33 32 3b 0a 20 20 6d 65 6d [23] = 32;. mem
14fca 73 65 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 set(&data[24], 0
14fcb 2c 20 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 , 100-24);. zer
14fcc 6f 50 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49 oPage(pP1, PTF_I
14fcd 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 NTKEY|PTF_LEAF|P
14fce 54 46 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 TF_LEAFDATA );.
14fcf 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 pBt->pageSizeFi
14fd0 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 xed = 1;.#ifndef
14fd1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
14fd2 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74 OVACUUM. assert
14fd3 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
14fd4 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 m==1 || pBt->aut
14fd5 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 oVacuum==0 );.
14fd6 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 assert( pBt->inc
14fd7 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 rVacuum==1 || pB
14fd8 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 t->incrVacuum==0
14fd9 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 );. put4byte(&
14fda 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 data[36 + 4*4],
14fdb 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
14fdc 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61 ;. put4byte(&da
14fdd 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 ta[36 + 7*4], pB
14fde 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a t->incrVacuum);.
14fdf 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
14fe0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
14fe1 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 .** Attempt to s
14fe2 74 61 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73 tart a new trans
14fe3 61 63 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d action. A write-
14fe4 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 transaction.** i
14fe5 73 20 73 74 61 72 74 65 64 20 69 66 20 74 68 65 s started if the
14fe6 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
14fe7 20 69 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 is nonzero, oth
14fe8 65 72 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a erwise a read-.*
14fe9 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 * transaction.
14fea 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
14feb 67 75 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d gument is 2 or m
14fec 6f 72 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76 ore and exclusiv
14fed 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
14fee 20 69 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61 is started, mea
14fef 6e 69 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 ning that no oth
14ff0 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c er process is al
14ff1 6c 6f 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 lowed.** to acce
14ff2 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ss the database.
14ff3 20 20 41 20 70 72 65 65 78 69 73 74 69 6e 67 20 A preexisting
14ff4 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 transaction may
14ff5 6e 6f 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 not be.** upgrad
14ff6 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 ed to exclusive
14ff7 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 by calling this
14ff8 72 6f 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 routine a second
14ff9 20 74 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 time - the.** e
14ffa 78 63 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20 xclusivity flag
14ffb 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 only works for a
14ffc 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e new transaction
14ffd 2e 0a 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d ..**.** A write-
14ffe 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
14fff 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f be started befo
15000 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e re attempting an
15001 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f y .** changes to
15002 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
15003 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c None of the foll
15004 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a owing routines .
15005 2a 2a 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c ** will work unl
15006 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f ess a transactio
15007 6e 20 69 73 20 73 74 61 72 74 65 64 20 66 69 72 n is started fir
15008 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 st:.**.** s
15009 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
1500a 65 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 eTable().**
1500b 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 sqlite3BtreeCre
1500c 61 74 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 ateIndex().**
1500d 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
1500e 6c 65 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 learTable().**
1500f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15010 44 72 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 DropTable().**
15011 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15012 49 6e 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 Insert().**
15013 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c sqlite3BtreeDel
15014 65 74 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ete().** sq
15015 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
15016 4d 65 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 Meta().**.** If
15017 61 6e 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d an initial attem
15018 70 74 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 pt to acquire th
15019 65 20 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 e lock fails bec
1501a 61 75 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e ause of lock con
1501b 74 65 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 tention.** and t
1501c 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 he database was
1501d 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 previously unloc
1501e 6b 65 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 ked, then invoke
1501f 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
15020 72 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 r.** if there is
15021 20 6f 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68 one. But if th
15022 65 72 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 ere was previous
15023 6c 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 ly a read-lock,
15024 64 6f 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 do not.** invoke
15025 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
15026 72 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 r - just return
15027 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 SQLITE_BUSY. SQ
15028 4c 49 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a LITE_BUSY is .**
15029 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 returned when t
1502a 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
1502b 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f a read-lock in o
1502c 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 rder to avoid a
1502d 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 deadlock..**.**
1502e 53 75 70 70 6f 73 65 20 74 68 65 72 65 20 61 72 Suppose there ar
1502f 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 e two processes
15030 41 20 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20 A and B. A has
15031 61 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 a read lock and
15032 42 20 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 B has.** a reser
15033 76 65 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 ved lock. B tri
15034 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f es to promote to
15035 20 65 78 63 6c 75 73 69 76 65 20 62 75 74 20 69 exclusive but i
15036 73 20 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 s blocked becaus
15037 65 0a 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64 e.** of A's read
15038 20 6c 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20 lock. A tries
15039 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 to promote to re
1503a 73 65 72 76 65 64 20 62 75 74 20 69 73 20 62 6c served but is bl
1503b 6f 63 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f ocked by B..** O
1503c 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 ne or the other
1503d 6f 66 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65 of the two proce
1503e 73 73 65 73 20 6d 75 73 74 20 67 69 76 65 20 77 sses must give w
1503f 61 79 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20 ay or there can
15040 62 65 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 be.** no progres
15041 73 2e 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67 s. By returning
15042 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 SQLITE_BUSY and
15043 20 6e 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 not invoking th
15044 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a e busy callback.
15045 2a 2a 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64 ** when A alread
15046 79 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 y has a read loc
15047 6b 2c 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20 k, we encourage
15048 41 20 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64 A to give up and
15049 20 6c 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 let B.** procee
1504a 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1504b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1504c 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 BtreeBeginTrans(
1504d 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 Btree *p, int wr
1504e 66 6c 61 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 flag){. sqlite3
1504f 20 2a 70 42 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 *pBlock = 0;.
15050 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
15051 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 p->pBt;. int rc
15052 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
15053 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
15054 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 er(p);. pBt->db
15055 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 62 74 72 65 = p->db;. btre
15056 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a eIntegrity(p);..
15057 20 20 2f 2a 20 49 66 20 74 68 65 20 62 74 72 65 /* If the btre
15058 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 e is already in
15059 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 a write-transact
1505a 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 20 2a 2a 20 ion, or it. **
1505b 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20 is already in a
1505c 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e read-transaction
1505d 20 61 6e 64 20 61 20 72 65 61 64 2d 74 72 61 6e and a read-tran
1505e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 saction. ** is
1505f 72 65 71 75 65 73 74 65 64 2c 20 74 68 69 73 20 requested, this
15060 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f is a no-op.. */
15061 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
15062 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c s==TRANS_WRITE |
15063 7c 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 | (p->inTrans==T
15064 52 41 4e 53 5f 52 45 41 44 20 26 26 20 21 77 72 RANS_READ && !wr
15065 66 6c 61 67 29 20 29 7b 0a 20 20 20 20 67 6f 74 flag) ){. got
15066 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 o trans_begun;.
15067 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 }.. /* Write t
15068 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 ransactions are
15069 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 6f 6e 20 not possible on
1506a 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 a read-only data
1506b 62 61 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 42 base */. if( pB
1506c 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 77 t->readOnly && w
1506d 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 72 63 20 rflag ){. rc
1506e 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c = SQLITE_READONL
1506f 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e Y;. goto tran
15070 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69 s_begun;. }..#i
15071 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15072 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
15073 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 64 /* If another d
15074 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 68 atabase handle h
15075 61 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 as already opene
15076 64 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 d a write transa
15077 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74 ction . ** on t
15078 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 his shared-btree
15079 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61 structure and a
1507a 20 73 65 63 6f 6e 64 20 77 72 69 74 65 20 74 72 second write tr
1507b 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a ansaction is. *
1507c 2a 20 72 65 71 75 65 73 74 65 64 2c 20 72 65 74 * requested, ret
1507d 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 urn SQLITE_LOCKE
1507e 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 77 D.. */. if( (w
1507f 72 66 6c 61 67 20 26 26 20 70 42 74 2d 3e 69 6e rflag && pBt->in
15080 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
15081 4e 53 5f 57 52 49 54 45 29 20 7c 7c 20 70 42 74 NS_WRITE) || pBt
15082 2d 3e 69 73 50 65 6e 64 69 6e 67 20 29 7b 0a 20 ->isPending ){.
15083 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 42 74 2d pBlock = pBt-
15084 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b 0a 20 20 >pWriter->db;.
15085 7d 65 6c 73 65 20 69 66 28 20 77 72 66 6c 61 67 }else if( wrflag
15086 3e 31 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b >1 ){. BtLock
15087 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 66 6f 72 *pIter;. for
15088 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 (pIter=pBt->pLoc
15089 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d k; pIter; pIter=
1508a 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 pIter->pNext){.
1508b 20 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e if( pIter->
1508c 70 42 74 72 65 65 21 3d 70 20 29 7b 0a 20 20 20 pBtree!=p ){.
1508d 20 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 49 pBlock = pI
1508e 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3b ter->pBtree->db;
1508f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
15090 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
15091 7d 0a 20 20 69 66 28 20 70 42 6c 6f 63 6b 20 29 }. if( pBlock )
15092 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e {. sqlite3Con
15093 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 nectionBlocked(p
15094 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 3b 0a 20 ->db, pBlock);.
15095 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c rc = SQLITE_L
15096 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
15097 45 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e E;. goto tran
15098 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 23 65 6e s_begun;. }.#en
15099 64 69 66 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 dif.. do {.
1509a 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d if( pBt->pPage1=
1509b 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 7b 0a =0 ){. do{.
1509c 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 rc = loc
1509d 6b 42 74 72 65 65 28 70 42 74 29 3b 0a 20 20 20 kBtree(pBt);.
1509e 20 20 20 7d 77 68 69 6c 65 28 20 70 42 74 2d 3e }while( pBt->
1509f 70 50 61 67 65 31 3d 3d 30 20 26 26 20 72 63 3d pPage1==0 && rc=
150a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
150a1 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d }.. if( rc=
150a2 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 =SQLITE_OK && wr
150a3 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 69 66 flag ){. if
150a4 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 ( pBt->readOnly
150a5 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
150a6 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
150a7 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
150a8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
150a9 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 42 74 e3PagerBegin(pBt
150aa 2d 3e 70 50 61 67 65 72 2c 20 77 72 66 6c 61 67 ->pPager, wrflag
150ab 3e 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 >1);. if(
150ac 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
150ad 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
150ae 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 newDatabase(pBt
150af 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
150b0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
150b1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
150b2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c _OK ){. unl
150b3 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 ockBtreeIfUnused
150b4 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d (pBt);. }. }
150b5 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
150b6 45 5f 42 55 53 59 20 26 26 20 70 42 74 2d 3e 69 E_BUSY && pBt->i
150b7 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
150b8 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 ANS_NONE &&.
150b9 20 20 20 20 20 20 62 74 72 65 65 49 6e 76 6f 6b btreeInvok
150ba 65 42 75 73 79 48 61 6e 64 6c 65 72 28 70 42 74 eBusyHandler(pBt
150bb 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d ) );.. if( rc==
150bc 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
150bd 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d if( p->inTrans=
150be 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 =TRANS_NONE ){.
150bf 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 pBt->nTrans
150c0 61 63 74 69 6f 6e 2b 2b 3b 0a 20 20 20 20 7d 0a action++;. }.
150c1 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d p->inTrans =
150c2 20 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f 57 (wrflag?TRANS_W
150c3 52 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44 29 RITE:TRANS_READ)
150c4 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 ;. if( p->inT
150c5 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61 6e rans>pBt->inTran
150c6 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 saction ){.
150c7 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
150c8 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e 73 ion = p->inTrans
150c9 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
150ca 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
150cb 45 44 5f 43 41 43 48 45 0a 20 20 20 20 69 66 28 ED_CACHE. if(
150cc 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 wrflag ){.
150cd 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 assert( !pBt->p
150ce 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 Writer );.
150cf 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 70 pBt->pWriter = p
150d0 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 73 45 ;. pBt->isE
150d1 78 63 6c 75 73 69 76 65 20 3d 20 28 75 38 29 28 xclusive = (u8)(
150d2 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 7d wrflag>1);. }
150d3 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a 74 72 .#endif. }...tr
150d4 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66 28 ans_begun:. if(
150d5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
150d6 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 & wrflag ){.
150d7 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61 6b /* This call mak
150d8 65 73 20 73 75 72 65 20 74 68 61 74 20 74 68 65 es sure that the
150d9 20 70 61 67 65 72 20 68 61 73 20 74 68 65 20 63 pager has the c
150da 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 orrect number of
150db 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 76 . ** open sav
150dc 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 20 epoints. If the
150dd 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
150de 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
150df 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 0 and. ** th
150e0 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 e sub-journal is
150e1 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 not already ope
150e2 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 n, then it will
150e3 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e 0a be opened here..
150e4 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
150e5 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
150e6 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 Savepoint(pBt->p
150e7 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e 53 Pager, p->db->nS
150e8 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 0a avepoint);. }..
150e9 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 btreeIntegrity
150ea 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 (p);. sqlite3Bt
150eb 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
150ec 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 eturn rc;.}..#if
150ed 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
150ee 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a _AUTOVACUUM../*.
150ef 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 ** Set the point
150f0 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 er-map entries f
150f1 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 or all children
150f2 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 41 of page pPage. A
150f3 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65 lso, if.** pPage
150f4 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20 contains cells
150f5 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 that point to ov
150f6 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65 erflow pages, se
150f7 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a t the pointer.**
150f8 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 map entries for
150f9 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
150fa 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a ges as well..*/.
150fb 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 68 static int setCh
150fc 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61 ildPtrmaps(MemPa
150fd 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e ge *pPage){. in
150fe 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
150ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15100 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69 /* Counter vari
15101 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 able */. int nC
15102 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 ell;
15103 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15104 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 Number of cells
15105 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f in page pPage */
15106 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
15107 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15108 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
15109 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 code */. BtShar
1510a 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d ed *pBt = pPage-
1510b 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e 69 >pBt;. u8 isIni
1510c 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 tOrig = pPage->i
1510d 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 67 sInit;. Pgno pg
1510e 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f no = pPage->pgno
1510f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
15110 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15111 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
15112 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c x) );. rc = sql
15113 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
15114 65 28 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 e(pPage);. if(
15115 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
15116 0a 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 . goto set_ch
15117 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b ild_ptrmaps_out;
15118 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 . }. nCell = p
15119 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 Page->nCell;..
1511a 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c for(i=0; i<nCell
1511b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a ; i++){. u8 *
1511c 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
1511d 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 (pPage, i);..
1511e 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f rc = ptrmapPutO
1511f 76 66 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 vflPtr(pPage, pC
15120 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ell);. if( rc
15121 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
15122 20 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 goto set_ch
15123 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b ild_ptrmaps_out;
15124 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
15125 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
15126 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 Pgno child
15127 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 Pgno = get4byte(
15128 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 72 63 pCell);. rc
15129 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 = ptrmapPut(pBt
1512a 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 , childPgno, PTR
1512b 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 29 MAP_BTREE, pgno)
1512c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
1512d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
1512e 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 set_child_ptrma
1512f 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 ps_out;. }.
15130 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d }.. if( !pPage-
15131 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 6e >leaf ){. Pgn
15132 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 o childPgno = ge
15133 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
15134 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
15135 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 72 ffset+8]);. r
15136 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 c = ptrmapPut(pB
15137 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 t, childPgno, PT
15138 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f RMAP_BTREE, pgno
15139 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 69 6c );. }..set_chil
1513a 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a 0a 20 d_ptrmaps_out:.
1513b 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d pPage->isInit =
1513c 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 72 isInitOrig;. r
1513d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1513e 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e 20 ** Somewhere on
1513f 70 50 61 67 65 2c 20 77 68 69 63 68 20 69 73 20 pPage, which is
15140 67 75 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65 guarenteed to be
15141 20 61 20 62 74 72 65 65 20 70 61 67 65 2c 20 6e a btree page, n
15142 6f 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a ot an overflow.*
15143 2a 20 70 61 67 65 2c 20 69 73 20 61 20 70 6f 69 * page, is a poi
15144 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 72 nter to page iFr
15145 6f 6d 2e 20 4d 6f 64 69 66 79 20 74 68 69 73 20 om. Modify this
15146 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 20 pointer so that
15147 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 it points to.**
15148 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65 iTo. Parameter e
15149 54 79 70 65 20 64 65 73 63 72 69 62 65 73 20 74 Type describes t
1514a 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74 he type of point
1514b 65 72 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 er to be modifie
1514c 64 2c 20 61 73 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 d, as .** follow
1514d 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f s:.**.** PTRMAP_
1514e 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 67 65 BTREE: pPage
1514f 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 is a btree-page
15150 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f . The pointer po
15151 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c 64 20 ints at a child
15152 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
15153 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 70 50 page of pP
15154 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 age..**.** PTRMA
15155 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50 61 P_OVERFLOW1: pPa
15156 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 ge is a btree-pa
15157 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 ge. The pointer
15158 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f 76 65 points at an ove
15159 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 rflow.**
1515a 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 page
1515b 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6f 6e pointed to by on
1515c 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f e of the cells o
1515d 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 n pPage..**.** P
1515e 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a TRMAP_OVERFLOW2:
1515f 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 pPage is an ove
15160 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65 20 rflow-page. The
15161 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 pointer points a
15162 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 t the next.**
15163 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15164 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e overflow page in
15165 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 the list..*/.st
15166 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 79 50 atic int modifyP
15167 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50 61 agePointer(MemPa
15168 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f 20 ge *pPage, Pgno
15169 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c iFrom, Pgno iTo,
1516a 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 61 73 u8 eType){. as
1516b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1516c 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1516d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1516e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1516f 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
15170 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
15171 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d );. if( eType=
15172 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 =PTRMAP_OVERFLOW
15173 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 2 ){. /* The
15174 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79 pointer is alway
15175 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 s the first 4 by
15176 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 tes of the page
15177 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a in this case. *
15178 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 62 79 /. if( get4by
15179 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 te(pPage->aData)
1517a 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 !=iFrom ){.
1517b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1517c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1517d 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 }. put4byte(
1517e 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 pPage->aData, iT
1517f 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 o);. }else{.
15180 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d u8 isInitOrig =
15181 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a pPage->isInit;.
15182 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 int i;. i
15183 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 73 nt nCell;.. s
15184 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 qlite3BtreeInitP
15185 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
15186 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e nCell = pPage->n
15187 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 Cell;.. for(i
15188 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b =0; i<nCell; i++
15189 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 ){. u8 *pCe
1518a 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
1518b 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 age, i);. i
1518c 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 f( eType==PTRMAP
1518d 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 _OVERFLOW1 ){.
1518e 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 CellInfo i
1518f 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c nfo;. sql
15190 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
15191 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 llPtr(pPage, pCe
15192 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 ll, &info);.
15193 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 if( info.iOv
15194 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 erflow ){.
15195 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 67 if( iFrom==g
15196 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 et4byte(&pCell[i
15197 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 20 nfo.iOverflow])
15198 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
15199 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 ut4byte(&pCell[i
1519a 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 2c 20 nfo.iOverflow],
1519b 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 iTo);.
1519c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1519d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1519e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1519f 20 20 20 20 69 66 28 20 67 65 74 34 62 79 74 65 if( get4byte
151a0 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29 (pCell)==iFrom )
151a1 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 {. put4
151a2 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 6f 29 byte(pCell, iTo)
151a3 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
151a4 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
151a5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
151a6 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c 20 29 if( i==nCell )
151a7 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 {. if( eTyp
151a8 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 e!=PTRMAP_BTREE
151a9 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 67 65 || . ge
151aa 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
151ab 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
151ac 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72 6f 6d ffset+8])!=iFrom
151ad 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
151ae 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
151af 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a T_BKPT;. }.
151b0 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
151b1 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
151b2 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
151b3 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20 , iTo);. }..
151b4 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 pPage->isInit
151b5 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 = isInitOrig;.
151b6 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
151b7 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a TE_OK;.}.../*.**
151b8 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e 20 64 Move the open d
151b9 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62 atabase page pDb
151ba 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69 6f 6e Page to location
151bb 20 69 46 72 65 65 50 61 67 65 20 69 6e 20 74 68 iFreePage in th
151bc 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 e .** database.
151bd 54 68 65 20 70 44 62 50 61 67 65 20 72 65 66 65 The pDbPage refe
151be 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20 76 61 rence remains va
151bf 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 lid..*/.static i
151c0 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 nt relocatePage(
151c1 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
151c2 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 , /* B
151c3 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 tree */. MemPag
151c4 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20 e *pDbPage,
151c5 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 /* Open page
151c6 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 to move */. u8
151c7 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 eType,
151c8 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
151c9 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74 72 map 'type' entr
151ca 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f y for pDbPage */
151cb 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 . Pgno iPtrPage
151cc 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 , /* P
151cd 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65 ointer map 'page
151ce 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70 -no' entry for p
151cf 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f DbPage */. Pgno
151d0 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 20 20 iFreePage,
151d1 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 /* The loca
151d2 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 tion to move pDb
151d3 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 Page to */. int
151d4 20 69 73 43 6f 6d 6d 69 74 0a 29 7b 0a 20 20 4d isCommit.){. M
151d5 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67 65 emPage *pPtrPage
151d6 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 ; /* The page
151d7 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 that contains a
151d8 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50 61 pointer to pDbPa
151d9 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 ge */. Pgno iDb
151da 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d 3e Page = pDbPage->
151db 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 pgno;. Pager *p
151dc 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 Pager = pBt->pPa
151dd 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a ger;. int rc;..
151de 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d assert( eType=
151df 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 =PTRMAP_OVERFLOW
151e0 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 2 || eType==PTRM
151e1 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 AP_OVERFLOW1 ||
151e2 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 54 . eType==PT
151e3 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 RMAP_BTREE || eT
151e4 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 ype==PTRMAP_ROOT
151e5 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 PAGE );. assert
151e6 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
151e7 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
151e8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 );. assert( pD
151e9 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 bPage->pBt==pBt
151ea 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 );.. /* Move pa
151eb 67 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d 20 ge iDbPage from
151ec 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 its current loca
151ed 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d tion to page num
151ee 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a 2f ber iFreePage */
151ef 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f 56 . TRACE(("AUTOV
151f0 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 ACUUM: Moving %d
151f1 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25 64 to free page %d
151f2 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74 79 (ptr page %d ty
151f3 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20 pe %d)\n", .
151f4 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 iDbPage, iFree
151f5 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c 20 Page, iPtrPage,
151f6 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d 20 eType));. rc =
151f7 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 sqlite3PagerMove
151f8 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44 62 page(pPager, pDb
151f9 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 69 Page->pDbPage, i
151fa 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d FreePage, isComm
151fb 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 it);. if( rc!=S
151fc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
151fd 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
151fe 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d pDbPage->pgno =
151ff 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f iFreePage;.. /
15200 2a 20 49 66 20 70 44 62 50 61 67 65 20 77 61 73 * If pDbPage was
15201 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20 74 a btree-page, t
15202 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65 20 hen it may have
15203 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64 2f child pages and/
15204 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 or cells. ** th
15205 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 at point to over
15206 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 flow pages. The
15207 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 pointer map entr
15208 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 ies for all thes
15209 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 e. ** pages nee
1520a 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 2e d to be changed.
1520b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 . **. ** If pD
1520c 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72 bPage is an over
1520d 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 flow page, then
1520e 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65 the first 4 byte
1520f 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 20 s may store a.
15210 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 ** pointer to a
15211 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66 subsequent overf
15212 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68 69 low page. If thi
15213 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 s is the case, t
15214 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 hen. ** the poi
15215 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20 74 nter map needs t
15216 6f 20 62 65 20 75 70 64 61 74 65 64 20 66 6f 72 o be updated for
15217 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 the subsequent
15218 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 overflow page..
15219 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 3d */. if( eType=
1521a 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c =PTRMAP_BTREE ||
1521b 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 eType==PTRMAP_R
1521c 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 OOTPAGE ){. r
1521d 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d c = setChildPtrm
1521e 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20 20 aps(pDbPage);.
1521f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15220 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
15221 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
15222 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 }else{. Pgno
15223 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 nextOvfl = get4b
15224 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44 61 yte(pDbPage->aDa
15225 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 ta);. if( nex
15226 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 tOvfl!=0 ){.
15227 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 rc = ptrmapPut
15228 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c 2c 20 (pBt, nextOvfl,
15229 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 PTRMAP_OVERFLOW2
1522a 2c 20 69 46 72 65 65 50 61 67 65 29 3b 0a 20 20 , iFreePage);.
1522b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1522c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1522d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1522e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
1522f 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74 61 /* Fix the data
15230 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 base pointer on
15231 70 61 67 65 20 69 50 74 72 50 61 67 65 20 74 68 page iPtrPage th
15232 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69 44 at pointed at iD
15233 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 bPage so. ** th
15234 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 at it points at
15235 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 iFreePage. Also
15236 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 20 fix the pointer
15237 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 map entry for.
15238 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20 2a ** iPtrPage.. *
15239 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 /. if( eType!=P
1523a 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 TRMAP_ROOTPAGE )
1523b 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1523c 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 e3BtreeGetPage(p
1523d 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 70 Bt, iPtrPage, &p
1523e 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 PtrPage, 0);.
1523f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
15240 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
15241 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
15242 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
15243 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67 65 erWrite(pPtrPage
15244 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
15245 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15246 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 K ){. relea
15247 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65 29 sePage(pPtrPage)
15248 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
15249 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 c;. }. rc
1524a 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e = modifyPagePoin
1524b 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 44 ter(pPtrPage, iD
1524c 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 bPage, iFreePage
1524d 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 65 , eType);. re
1524e 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61 leasePage(pPtrPa
1524f 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ge);. if( rc=
15250 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15251 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 rc = ptrmapP
15252 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 67 ut(pBt, iFreePag
15253 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 e, eType, iPtrPa
15254 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ge);. }. }.
15255 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
15256 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 * Forward declar
15257 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 62 ation required b
15258 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 y incrVacuumStep
15259 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e (). */.static in
1525a 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 t allocateBtreeP
1525b 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c 20 age(BtShared *,
1525c 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e 6f MemPage **, Pgno
1525d 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a 0a *, Pgno, u8);..
1525e 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 /*.** Perform a
1525f 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 61 single step of a
15260 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 n incremental-va
15261 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 73 cuum. If success
15262 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 ful,.** return S
15263 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 QLITE_OK. If the
15264 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 6f re is no work to
15265 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 6f do (and therefo
15266 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20 69 re no.** point i
15267 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 n calling this f
15268 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c 20 unction again),
15269 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
1526a 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 73 NE..**.** More s
1526b 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73 20 pecificly, this
1526c 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 function attempt
1526d 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a 65 s to re-organize
1526e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 the .** databas
1526f 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c 61 e so that the la
15270 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 st page of the f
15271 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e ile currently in
15272 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f use.** is no lo
15273 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a nger in use..**.
15274 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 70 ** If the nFin p
15275 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d arameter is non-
15276 7a 65 72 6f 2c 20 74 68 65 20 69 6d 70 6c 65 6d zero, the implem
15277 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 entation assumes
15278 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c .** that the cal
15279 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61 ler will keep ca
1527a 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d lling incrVacuum
1527b 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 Step() until.**
1527c 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 it returns SQLIT
1527d 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72 E_DONE or an err
1527e 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69 or, and that nFi
1527f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 n is the.** numb
15280 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 20 er of pages the
15281 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 database file wi
15282 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72 ll contain after
15283 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73 this .** proces
15284 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a s is complete..*
15285 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 /.static int inc
15286 72 56 61 63 75 75 6d 53 74 65 70 28 42 74 53 68 rVacuumStep(BtSh
15287 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
15288 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74 nFin, Pgno iLast
15289 50 67 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 72 65 Pg){. Pgno nFre
1528a 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 eList;
1528b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
1528c 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 ges still on the
1528d 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 free-list */..
1528e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1528f 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
15290 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 >mutex) );.. if
15291 28 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 ( !PTRMAP_ISPAGE
15292 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 (pBt, iLastPg) &
15293 26 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 & iLastPg!=PENDI
15294 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
15295 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b ) ){. int rc;
15296 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 . u8 eType;.
15297 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 Pgno iPtrPage
15298 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 74 ;.. nFreeList
15299 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 = get4byte(&pBt
1529a 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
1529b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 36]);. if( nF
1529c 72 65 65 4c 69 73 74 3d 3d 30 20 7c 7c 20 6e 46 reeList==0 || nF
1529d 69 6e 3d 3d 69 4c 61 73 74 50 67 20 29 7b 0a 20 in==iLastPg ){.
1529e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1529f 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a TE_DONE;. }..
152a0 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
152a1 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c et(pBt, iLastPg,
152a2 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 &eType, &iPtrPa
152a3 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
152a4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
152a5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
152a6 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79 }. if( eTy
152a7 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe==PTRMAP_ROOTP
152a8 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 AGE ){. ret
152a9 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
152aa 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a PT_BKPT;. }..
152ab 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
152ac 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 TRMAP_FREEPAGE )
152ad 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e {. if( nFin
152ae 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ==0 ){. /
152af 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 * Remove the pag
152b0 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 e from the files
152b1 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 free-list. This
152b2 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
152b3 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e . ** if n
152b4 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e Fin is non-zero.
152b5 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 In that case, t
152b6 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c he free-list wil
152b7 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 l be. **
152b8 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 truncated to zer
152b9 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e o after this fun
152ba 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 ction returns, s
152bb 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 o it doesn't .
152bc 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 ** matter
152bd 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 if it still cont
152be 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 ains some garbag
152bf 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 e entries..
152c0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 */. Pg
152c1 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 no iFreePg;.
152c2 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 MemPage *pFr
152c3 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 eePg;. rc
152c4 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
152c5 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 Page(pBt, &pFree
152c6 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c Pg, &iFreePg, iL
152c7 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 astPg, 1);.
152c8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
152c9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
152ca 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
152cb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
152cc 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d ssert( iFreePg==
152cd 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 iLastPg );.
152ce 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
152cf 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d FreePg);. }
152d0 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 . } else {.
152d1 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 Pgno iFreePg
152d2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
152d3 20 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 Index of free p
152d4 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 age to move pLas
152d5 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 tPg to */.
152d6 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67 MemPage *pLastPg
152d7 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ;.. rc = sq
152d8 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
152d9 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 e(pBt, iLastPg,
152da 26 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 &pLastPg, 0);.
152db 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
152dc 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
152dd 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
152de 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 }.. /* If
152df 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 nFin is zero, t
152e0 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 his loop runs ex
152e1 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 actly once and p
152e2 61 67 65 20 70 4c 61 73 74 50 67 0a 20 20 20 20 age pLastPg.
152e3 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65 64 20 ** is swapped
152e4 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20 66 with the first f
152e5 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65 64 20 ree page pulled
152e6 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c 69 73 off the free lis
152e7 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 t.. **.
152e8 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 ** On the othe
152e9 72 20 68 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 r hand, if nFin
152ea 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
152eb 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a zero, then keep.
152ec 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 ** looping
152ed 20 75 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61 until a free-pa
152ee 67 65 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69 ge located withi
152ef 6e 20 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e n the first nFin
152f0 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 pages. **
152f1 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 66 of the file is f
152f2 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ound.. */.
152f3 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 do {.
152f4 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 MemPage *pFree
152f5 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d Pg;. rc =
152f6 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
152f7 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 ge(pBt, &pFreePg
152f8 2c 20 26 69 46 72 65 65 50 67 2c 20 30 2c 20 30 , &iFreePg, 0, 0
152f9 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
152fa 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
152fb 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
152fc 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a ePage(pLastPg);.
152fd 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
152fe 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
152ff 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
15300 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 ge(pFreePg);.
15301 20 20 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 }while( nFin!
15302 3d 30 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 =0 && iFreePg>nF
15303 69 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 in );. asse
15304 72 74 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73 rt( iFreePg<iLas
15305 74 50 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 tPg );. .
15306 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
15307 50 61 67 65 72 57 72 69 74 65 28 70 4c 61 73 74 PagerWrite(pLast
15308 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 Pg->pDbPage);.
15309 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1530a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1530b 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 rc = relocatePa
1530c 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c ge(pBt, pLastPg,
1530d 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 eType, iPtrPage
1530e 2c 20 69 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 , iFreePg, nFin!
1530f 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 =0);. }.
15310 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
15311 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69 LastPg);. i
15312 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
15313 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
15314 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
15315 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
15316 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 nFin==0 ){. i
15317 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 LastPg--;. wh
15318 69 6c 65 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 ile( iLastPg==PE
15319 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
1531a 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 pBt)||PTRMAP_ISP
1531b 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 AGE(pBt, iLastPg
1531c 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 50 ) ){. if( P
1531d 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
1531e 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 , iLastPg) ){.
1531f 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
15320 50 67 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 Pg;. int
15321 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
15322 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c eGetPage(pBt, iL
15323 61 73 74 50 67 2c 20 26 70 50 67 2c 20 30 29 3b astPg, &pPg, 0);
15324 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
15325 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15326 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
15327 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
15328 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
15329 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3PagerWrite(pPg-
1532a 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
1532b 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1532c 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 Pg);. if(
1532d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1532e 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
1532f 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
15330 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
15331 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 7d 0a LastPg--;. }.
15332 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
15333 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42 TruncateImage(pB
15334 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c 61 73 74 t->pPager, iLast
15335 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Pg);. }. retur
15336 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
15337 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 /*.** A write-tr
15338 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
15339 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 e opened before
1533a 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e calling this fun
1533b 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 70 65 72 ction..** It per
1533c 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c 65 20 75 forms a single u
1533d 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 6f 77 61 nit of work towa
1533e 72 64 73 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 rds an increment
1533f 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a al vacuum..**.**
15340 20 49 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e If the incremen
15341 74 61 6c 20 76 61 63 75 75 6d 20 69 73 20 66 69 tal vacuum is fi
15342 6e 69 73 68 65 64 20 61 66 74 65 72 20 74 68 69 nished after thi
15343 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 72 s function has r
15344 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 4f un,.** SQLITE_DO
15345 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 NE is returned.
15346 49 66 20 69 74 20 69 73 20 6e 6f 74 20 66 69 6e If it is not fin
15347 69 73 68 65 64 2c 20 62 75 74 20 6e 6f 20 65 72 ished, but no er
15348 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a ror occurred,.**
15349 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1534a 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 turned. Otherwis
1534b 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f e an SQLite erro
1534c 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51 4c 49 r code. .*/.SQLI
1534d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1534e 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56 qlite3BtreeIncrV
1534f 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b acuum(Btree *p){
15350 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 . int rc;. BtS
15351 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
15352 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 pBt;.. sqlite3B
15353 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
15354 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b pBt->db = p->db;
15355 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
15356 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
15357 52 41 4e 53 5f 57 52 49 54 45 20 26 26 20 70 2d RANS_WRITE && p-
15358 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
15359 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 21 WRITE );. if( !
1535a 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
1535b 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
1535c 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 TE_DONE;. }else
1535d 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 {. invalidate
1535e 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 AllOverflowCache
1535f 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 (pBt);. rc =
15360 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 incrVacuumStep(p
15361 42 74 2c 20 30 2c 20 70 61 67 65 72 50 61 67 65 Bt, 0, pagerPage
15362 63 6f 75 6e 74 28 70 42 74 29 29 3b 0a 20 20 7d count(pBt));. }
15363 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
15364 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
15365 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
15366 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
15367 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 alled prior to s
15368 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 qlite3PagerCommi
15369 74 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 t when a transac
1536a 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 tion.** is commi
1536b 74 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d ted for an auto-
1536c 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e vacuum database.
1536d 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 .**.** If SQLITE
1536e 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c _OK is returned,
1536f 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 then *pnTrunc i
15370 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d s set to the num
15371 62 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 ber of pages.**
15372 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
15373 65 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e e should be trun
15374 63 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 cated to during
15375 74 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 the commit proce
15376 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 ss. .** i.e. the
15377 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 database has be
15378 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 en reorganized s
15379 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 o that only the
1537a 66 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a first *pnTrunc.*
1537b 2a 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 * pages are in u
1537c 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e se..*/.static in
1537d 74 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d t autoVacuumComm
1537e 69 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 it(BtShared *pBt
1537f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
15380 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 LITE_OK;. Pager
15381 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e *pPager = pBt->
15382 70 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e pPager;. VVA_ON
15383 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 LY( int nRef = s
15384 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
15385 75 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 0a unt(pPager) );..
15386 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15387 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
15388 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e ->mutex) );. in
15389 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 validateAllOverf
1538a 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 lowCache(pBt);.
1538b 20 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75 74 assert(pBt->aut
1538c 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 oVacuum);. if(
1538d 21 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d !pBt->incrVacuum
1538e 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 ){. Pgno nFi
1538f 6e 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 n;. Pgno nFre
15390 65 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 50 74 72 e;. Pgno nPtr
15391 6d 61 70 3b 0a 20 20 20 20 50 67 6e 6f 20 69 46 map;. Pgno iF
15392 72 65 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 ree;. const i
15393 6e 74 20 70 67 73 7a 20 3d 20 70 42 74 2d 3e 70 nt pgsz = pBt->p
15394 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 50 67 6e ageSize;. Pgn
15395 6f 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 72 50 o nOrig = pagerP
15396 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 0a agecount(pBt);..
15397 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 if( PTRMAP_I
15398 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 SPAGE(pBt, nOrig
15399 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
1539a 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1539b 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
1539c 20 69 66 28 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 if( nOrig==PEND
1539d 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
1539e 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 4f 72 69 t) ){. nOri
1539f 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e g--;. }. n
153a0 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 Free = get4byte(
153a1 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 &pBt->pPage1->aD
153a2 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e 50 ata[36]);. nP
153a3 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e trmap = (nFree-n
153a4 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 Orig+PTRMAP_PAGE
153a5 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b 70 NO(pBt, nOrig)+p
153a6 67 73 7a 2f 35 29 2f 28 70 67 73 7a 2f 35 29 3b gsz/5)/(pgsz/5);
153a7 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 . nFin = nOri
153a8 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72 g - nFree - nPtr
153a9 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f 72 map;. if( nOr
153aa 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f ig>PENDING_BYTE_
153ab 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69 PAGE(pBt) && nFi
153ac 6e 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f n<=PENDING_BYTE_
153ad 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
153ae 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d nFin--;. }
153af 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d . while( PTRM
153b0 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e AP_ISPAGE(pBt, n
153b1 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45 Fin) || nFin==PE
153b2 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
153b3 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 pBt) ){. nF
153b4 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 in--;. }..
153b5 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 67 for(iFree=nOrig
153b6 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 20 ; iFree>nFin &&
153b7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 rc==SQLITE_OK; i
153b8 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 72 Free--){. r
153b9 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 c = incrVacuumSt
153ba 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 46 ep(pBt, nFin, iF
153bb 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ree);. }.
153bc 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f if( (rc==SQLITE_
153bd 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 DONE || rc==SQLI
153be 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 3e TE_OK) && nFree>
153bf 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
153c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
153c1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
153c2 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 erWrite(pBt->pPa
153c3 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
153c4 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
153c5 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
153c6 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20 a[32], 0);.
153c7 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e put4byte(&pBt->
153c8 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
153c9 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c ], 0);. sql
153ca 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
153cb 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67 eImage(pBt->pPag
153cc 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 7d er, nFin);. }
153cd 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
153ce 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
153cf 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c sqlite3PagerRoll
153d0 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 back(pPager);.
153d1 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 }. }.. asser
153d2 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 33 t( nRef==sqlite3
153d3 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 PagerRefcount(pP
153d4 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 ager) );. retur
153d5 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 n rc;.}..#endif
153d6 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /* ifndef SQLITE
153d7 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
153d8 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 */../*.** This
153d9 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 routine does the
153da 20 66 69 72 73 74 20 70 68 61 73 65 20 6f 66 20 first phase of
153db 61 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d 6d a two-phase comm
153dc 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e it. This routin
153dd 65 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72 6f e.** causes a ro
153de 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 llback journal t
153df 6f 20 62 65 20 63 72 65 61 74 65 64 20 28 69 66 o be created (if
153e0 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 it does not alr
153e1 65 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20 61 eady exist).** a
153e2 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 nd populated wit
153e3 68 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 h enough informa
153e4 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66 20 tion so that if
153e5 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63 63 a power loss occ
153e6 75 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 urs.** the datab
153e7 61 73 65 20 63 61 6e 20 62 65 20 72 65 73 74 6f ase can be resto
153e8 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 red to its origi
153e9 6e 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c 61 nal state by pla
153ea 79 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68 65 ying back.** the
153eb 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e 20 journal. Then
153ec 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
153ed 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 the journal are
153ee 66 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a 2a flushed out to.*
153ef 2a 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66 74 * the disk. Aft
153f0 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 er the journal i
153f1 73 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69 64 s safely on oxid
153f2 65 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20 74 e, the changes t
153f3 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 o the.** databas
153f4 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e e are written in
153f5 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
153f6 66 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65 64 file and flushed
153f7 20 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 to oxide..** At
153f8 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73 the end of this
153f9 20 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 call, the rollb
153fa 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69 6c ack journal stil
153fb 6c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65 0a l exists on the.
153fc 2a 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20 61 ** disk and we a
153fd 72 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 re still holding
153fe 20 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 all locks, so t
153ff 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 he transaction h
15400 61 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 as not.** commit
15401 74 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74 65 ted. See sqlite
15402 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 20 66 3BtreeCommit() f
15403 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 or the second ph
15404 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f ase of the.** co
15405 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a mmit process..**
15406 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 .** This call is
15407 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 a no-op if no w
15408 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
15409 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63 is currently ac
1540a 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a tive on pBt..**.
1540b 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79 ** Otherwise, sy
1540c 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nc the database
1540d 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72 file for the btr
1540e 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 ee pBt. zMaster
1540f 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 points to.** the
15410 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 name of a maste
15411 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 r journal file t
15412 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 hat should be wr
15413 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a itten into the.*
15414 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 * individual jou
15415 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 rnal file, or is
15416 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e NULL, indicatin
15417 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 g no master jour
15418 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 nal file .** (si
15419 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
1541a 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
1541b 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 * When this is c
1541c 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65 alled, the maste
1541d 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 r journal should
1541e 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65 already have be
1541f 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 en.** created, p
15420 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 opulated with th
15421 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 is journal point
15422 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f er and synced to
15423 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 disk..**.** Onc
15424 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e e this is routin
15425 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20 e has returned,
15426 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 the only thing r
15427 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 equired to commi
15428 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74 t.** the write-t
15429 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 ransaction for t
1542a 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c his database fil
1542b 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 e is to delete t
1542c 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53 he journal..*/.S
1542d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1542e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
1542f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 mmitPhaseOne(Btr
15430 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 ee *p, const cha
15431 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 r *zMaster){. i
15432 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
15433 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 K;. if( p->inTr
15434 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
15435 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 ){. BtShared
15436 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
15437 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15438 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 70 42 Enter(p);. pB
15439 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 23 t->db = p->db;.#
1543a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1543b 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
1543c 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
1543d 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 acuum ){. r
1543e 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f c = autoVacuumCo
1543f 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20 mmit(pBt);.
15440 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
15441 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 OK ){. sq
15442 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15443 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 p);. retu
15444 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
15445 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
15446 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15447 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
15448 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 pBt->pPager, zMa
15449 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 ster, 0);. sq
1544a 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
1544b 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p);. }. return
1544c 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f rc;.}../*.** Co
1544d 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 mmit the transac
1544e 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 tion currently i
1544f 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a n progress..**.*
15450 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
15451 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 mplements the se
15452 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 cond phase of a
15453 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 2-phase commit.
15454 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 The.** sqlite3B
15455 74 72 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69 treeSync() routi
15456 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 ne does the firs
15457 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75 t phase and shou
15458 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a ld be invoked.**
15459 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
1545a 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 g this routine.
1545b 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 The sqlite3Btre
1545c 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 20 eSync() routine
1545d 64 69 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 77 did.** all the w
1545e 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69 ork of writing i
1545f 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 nformation out t
15460 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 o disk and flush
15461 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 ing the.** conte
15462 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 nts so that they
15463 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 are written ont
15464 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74 o the disk platt
15465 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a er. All this.**
15466 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 routine has to
15467 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 do is delete or
15468 74 72 75 6e 63 61 74 65 20 74 68 65 20 72 6f 6c truncate the rol
15469 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a lback journal.**
1546a 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 74 (which causes t
1546b 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 he transaction t
1546c 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 72 o commit) and dr
1546d 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 op locks..**.**
1546e 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 This will releas
1546f 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b e the write lock
15470 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
15471 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 file. If there
15472 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 .** are no activ
15473 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c e cursors, it al
15474 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 so releases the
15475 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 read lock..*/.SQ
15476 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15477 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
15478 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 mitPhaseTwo(Btre
15479 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 e *p){. BtShare
1547a 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1547b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
1547c 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d Enter(p);. pBt-
1547d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 62 >db = p->db;. b
1547e 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 treeIntegrity(p)
1547f 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 ;.. /* If the h
15480 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 andle has a writ
15481 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 e-transaction op
15482 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 en, commit the s
15483 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20 hared-btrees .
15484 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 ** transaction a
15485 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72 65 nd set the share
15486 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 d state to TRANS
15487 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 _READ.. */. if
15488 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
15489 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
1548a 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 int rc;. ass
1548b 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
1548c 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
1548d 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65 RITE );. asse
1548e 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 rt( pBt->nTransa
1548f 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 ction>0 );. r
15490 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
15491 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
15492 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
15493 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
15494 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
15495 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
15496 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
15497 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 c;. }. pBt
15498 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
15499 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 = TRANS_READ;.
1549a 7d 0a 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 }. clearAllShar
1549b 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
1549c 73 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 s(p);.. /* If t
1549d 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6e he handle has an
1549e 79 20 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73 61 y kind of transa
1549f 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63 72 ction open, decr
154a0 65 6d 65 6e 74 20 74 68 65 20 74 72 61 6e 73 61 ement the transa
154a1 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e 74 ction. ** count
154a2 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 62 of the shared b
154a3 74 72 65 65 2e 20 49 66 20 74 68 65 20 74 72 61 tree. If the tra
154a4 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 72 nsaction count r
154a5 65 61 63 68 65 73 20 30 2c 20 73 65 74 0a 20 20 eaches 0, set.
154a6 2a 2a 20 74 68 65 20 73 68 61 72 65 64 20 73 74 ** the shared st
154a7 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e ate to TRANS_NON
154a8 45 2e 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 E. The unlockBtr
154a9 65 65 49 66 55 6e 75 73 65 64 28 29 20 63 61 6c eeIfUnused() cal
154aa 6c 20 62 65 6c 6f 77 0a 20 20 2a 2a 20 77 69 6c l below. ** wil
154ab 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 l unlock the pag
154ac 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 er.. */. if( p
154ad 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 ->inTrans!=TRANS
154ae 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 70 42 74 _NONE ){. pBt
154af 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d ->nTransaction--
154b0 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74 ;. if( 0==pBt
154b1 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 ->nTransaction )
154b2 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 {. pBt->inT
154b3 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 ransaction = TRA
154b4 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 NS_NONE;. }.
154b5 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 }.. /* Set the
154b6 20 68 61 6e 64 6c 65 73 20 63 75 72 72 65 6e 74 handles current
154b7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 transaction sta
154b8 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 te to TRANS_NONE
154b9 20 61 6e 64 20 75 6e 6c 6f 63 6b 0a 20 20 2a 2a and unlock. **
154ba 20 74 68 65 20 70 61 67 65 72 20 69 66 20 74 68 the pager if th
154bb 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74 is call closed t
154bc 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 he only read or
154bd 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
154be 6e 2e 0a 20 20 2a 2f 0a 20 20 62 74 72 65 65 43 n.. */. btreeC
154bf 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 70 learHasContent(p
154c0 42 74 29 3b 0a 20 20 70 2d 3e 69 6e 54 72 61 6e Bt);. p->inTran
154c1 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a s = TRANS_NONE;.
154c2 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 unlockBtreeIfU
154c3 6e 75 73 65 64 28 70 42 74 29 3b 0a 0a 20 20 62 nused(pBt);.. b
154c4 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 treeIntegrity(p)
154c5 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
154c6 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
154c7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
154c8 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 ./*.** Do both p
154c9 68 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 hases of a commi
154ca 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
154cb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
154cc 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 BtreeCommit(Btre
154cd 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b e *p){. int rc;
154ce 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
154cf 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
154d0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
154d1 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 itPhaseOne(p, 0)
154d2 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
154d3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
154d4 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
154d5 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29 3b mmitPhaseTwo(p);
154d6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
154d7 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
154d8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 eturn rc;.}..#if
154d9 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a ndef NDEBUG./*.*
154da 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
154db 62 65 72 20 6f 66 20 77 72 69 74 65 2d 63 75 72 ber of write-cur
154dc 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 sors open on thi
154dd 73 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 69 s handle. This i
154de 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20 s for use.** in
154df 61 73 73 65 72 74 28 29 20 65 78 70 72 65 73 73 assert() express
154e0 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 20 6f ions, so it is o
154e1 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 20 nly compiled if
154e2 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a NDEBUG is not.**
154e3 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 defined..**.**
154e4 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 For the purposes
154e5 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
154e6 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 , a write-cursor
154e7 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74 is any cursor t
154e8 68 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c hat.** is capabl
154e9 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20 e of writing to
154ea 74 68 65 20 64 61 74 61 62 73 65 2e 20 20 54 68 the databse. Th
154eb 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75 72 at means the cur
154ec 73 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 sor was.** origi
154ed 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 nally opened for
154ee 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74 68 65 writing and the
154ef 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f 74 20 cursor has not
154f0 62 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 62 be disabled.** b
154f1 79 20 68 61 76 69 6e 67 20 69 74 73 20 73 74 61 y having its sta
154f2 74 65 20 63 68 61 6e 67 65 64 20 74 6f 20 43 55 te changed to CU
154f3 52 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 RSOR_FAULT..*/.s
154f4 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 57 tatic int countW
154f5 72 69 74 65 43 75 72 73 6f 72 73 28 42 74 53 68 riteCursors(BtSh
154f6 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 ared *pBt){. Bt
154f7 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 Cursor *pCur;.
154f8 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 6f 72 int r = 0;. for
154f9 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 72 73 (pCur=pBt->pCurs
154fa 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72 3d 70 or; pCur; pCur=p
154fb 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Cur->pNext){.
154fc 20 69 66 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 if( pCur->wrFla
154fd 67 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 g && pCur->eStat
154fe 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e!=CURSOR_FAULT
154ff 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 ) r++; . }. re
15500 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 turn r;.}.#endif
15501 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
15502 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74 tine sets the st
15503 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 ate to CURSOR_FA
15504 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f ULT and the erro
15505 72 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 r.** code to err
15506 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 63 Code for every c
15507 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65 ursor on BtShare
15508 64 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a d that pBtree.**
15509 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a references..**.
1550a 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 ** Every cursor
1550b 69 73 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c is tripped, incl
1550c 75 64 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68 uding cursors th
1550d 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 at belong.** to
1550e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 other database c
1550f 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 onnections that
15510 68 61 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61 happen to be sha
15511 72 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68 ring.** the cach
15512 65 20 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a e with pBtree..*
15513 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
15514 65 20 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68 e gets called wh
15515 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 en a rollback oc
15516 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 curs..** All cur
15517 73 6f 72 73 20 75 73 69 6e 67 20 74 68 65 20 73 sors using the s
15518 61 6d 65 20 63 61 63 68 65 20 6d 75 73 74 20 62 ame cache must b
15519 65 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20 e tripped.** to
1551a 70 72 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f prevent them fro
1551b 6d 20 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20 m trying to use
1551c 74 68 65 20 62 74 72 65 65 20 61 66 74 65 72 0a the btree after.
1551d 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e ** the rollback.
1551e 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d The rollback m
1551f 61 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 ay have deleted
15520 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 tables.** or mov
15521 65 64 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73 ed root pages, s
15522 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 o it is not suff
15523 69 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 icient to.** sav
15524 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 e the state of t
15525 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 he cursor. The
15526 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a cursor must be.*
15527 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a * invalidated..*
15528 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
15529 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
1552a 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 eeTripAllCursors
1552b 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 (Btree *pBtree,
1552c 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 int errCode){.
1552d 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 73 BtCursor *p;. s
1552e 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1552f 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 28 (pBtree);. for(
15530 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 p=pBtree->pBt->p
15531 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e Cursor; p; p=p->
15532 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20 pNext){. int
15533 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 i;. sqlite3Bt
15534 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 reeClearCursor(p
15535 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 65 );. p->eState
15536 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b = CURSOR_FAULT;
15537 0a 20 20 20 20 70 2d 3e 73 6b 69 70 20 3d 20 65 . p->skip = e
15538 72 72 43 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28 rrCode;. for(
15539 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65 i=0; i<=p->iPage
1553a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 ; i++){. re
1553b 6c 65 61 73 65 50 61 67 65 28 70 2d 3e 61 70 50 leasePage(p->apP
1553c 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 age[i]);. p
1553d 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b ->apPage[i] = 0;
1553e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
1553f 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
15540 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Btree);.}../*.**
15541 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 Rollback the tr
15542 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f ansaction in pro
15543 67 72 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 gress. All curs
15544 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 ors will be.** i
15545 6e 76 61 6c 69 64 65 64 20 62 79 20 74 68 69 73 nvalided by this
15546 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 operation. Any
15547 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
15548 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 a cursor.** that
15549 20 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65 was open at the
1554a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
1554b 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c is operation wil
1554c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 l result.** in a
1554d 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 n error..**.** T
1554e 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 his will release
1554f 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 the write lock
15550 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
15551 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a file. If there.
15552 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 ** are no active
15553 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 cursors, it als
15554 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 o releases the r
15555 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c ead lock..*/.SQL
15556 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15557 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c sqlite3BtreeRoll
15558 62 61 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a back(Btree *p){.
15559 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 int rc;. BtSh
1555a 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
1555b 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 Bt;. MemPage *p
1555c 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65 Page1;.. sqlite
1555d 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
1555e 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 pBt->db = p->d
1555f 62 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c b;. rc = saveAl
15560 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c lCursors(pBt, 0,
15561 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 0);.#ifndef SQL
15562 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
15563 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21 3d CACHE. if( rc!=
15564 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15565 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68 6f /* This is a ho
15566 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f 6e rrible situation
15567 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f . An IO or mallo
15568 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 72 c() error occurr
15569 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a ed whilst. **
1556a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65 20 trying to save
1556b 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73 cursor positions
1556c 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 . If this is an
1556d 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 automatic rollba
1556e 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74 68 ck (as. ** th
1556f 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63 6f e result of a co
15570 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 nstraint, malloc
15571 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49 4f () failure or IO
15572 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20 20 error) then .
15573 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 6d ** the cache m
15574 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 ay be internally
15575 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e inconsistent (n
15576 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64 ot contain valid
15577 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20 2a trees) so. *
15578 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70 * we cannot simp
15579 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 ly return the er
1557a 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ror to the calle
1557b 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f 72 r. Instead, abor
1557c 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75 t . ** all qu
1557d 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20 62 eries that may b
1557e 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20 74 e using any of t
1557f 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 he cursors that
15580 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a failed to save..
15581 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
15582 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 e3BtreeTripAllCu
15583 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20 20 rsors(p, rc);.
15584 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65 65 }.#endif. btree
15585 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 Integrity(p);.
15586 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 clearAllSharedCa
15587 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 cheTableLocks(p)
15588 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 ;.. if( p->inTr
15589 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
1558a 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b ){. int rc2;
1558b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 52 .. assert( TR
1558c 41 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e ANS_WRITE==pBt->
1558d 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b inTransaction );
1558e 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 . rc2 = sqlit
1558f 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
15590 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
15591 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54 if( rc2!=SQLIT
15592 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 E_OK ){. rc
15593 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 = rc2;. }..
15594 20 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 /* The rollba
15595 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 74 ck may have dest
15596 72 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 31 royed the pPage1
15597 2d 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 20 ->aData value.
15598 53 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 73 So. ** call s
15599 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
1559a 67 65 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61 ge() on page 1 a
1559b 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 gain to make.
1559c 20 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d ** sure pPage1-
1559d 3e 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f >aData is set co
1559e 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 rrectly. */.
1559f 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 if( sqlite3Btree
155a0 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 GetPage(pBt, 1,
155a1 26 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c &pPage1, 0)==SQL
155a2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
155a3 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
155a4 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 e1);. }. a
155a5 73 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 ssert( countWrit
155a6 65 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 eCursors(pBt)==0
155a7 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 );. pBt->inT
155a8 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 ransaction = TRA
155a9 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 NS_READ;. }..
155aa 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d if( p->inTrans!=
155ab 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 TRANS_NONE ){.
155ac 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e assert( pBt->n
155ad 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b Transaction>0 );
155ae 0a 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 . pBt->nTrans
155af 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 action--;. if
155b0 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 ( 0==pBt->nTrans
155b1 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 action ){.
155b2 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
155b3 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b on = TRANS_NONE;
155b4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 74 . }. }.. bt
155b5 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 reeClearHasConte
155b6 6e 74 28 70 42 74 29 3b 0a 20 20 70 2d 3e 69 6e nt(pBt);. p->in
155b7 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f Trans = TRANS_NO
155b8 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 NE;. unlockBtre
155b9 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a eIfUnused(pBt);.
155ba 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 . btreeIntegrit
155bb 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 y(p);. sqlite3B
155bc 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
155bd 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
155be 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 74 .** Start a stat
155bf 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 ement subtransac
155c0 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 61 tion. The subtra
155c1 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 63 61 6e nsaction can can
155c2 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 be rolled.** ba
155c3 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 ck independently
155c4 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 of the main tra
155c5 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 nsaction. You mu
155c6 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 st start a trans
155c7 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72 action .** befor
155c8 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 62 e starting a sub
155c9 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 transaction. The
155ca 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 subtransaction
155cb 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 is ended automat
155cc 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68 ically .** if th
155cd 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 e main transacti
155ce 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f on commits or ro
155cf 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 lls back..**.**
155d0 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 Statement subtra
155d1 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73 nsactions are us
155d2 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 ed around indivi
155d3 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 dual SQL stateme
155d4 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 nts.** that are
155d5 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e contained within
155d6 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 a BEGIN...COMMI
155d7 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 T block. If a c
155d8 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 onstraint.** err
155d9 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e or occurs within
155da 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 the statement,
155db 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68 the effect of th
155dc 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 at one statement
155dd 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 .** can be rolle
155de 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 d back without h
155df 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 aving to rollbac
155e0 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 k the entire tra
155e1 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
155e2 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 2d A statement sub-
155e3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 transaction is i
155e4 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 6e mplemented as an
155e5 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 anonymous savep
155e6 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c oint. The.** val
155e7 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ue passed as the
155e8 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
155e9 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e r is the total n
155ea 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 umber of savepoi
155eb 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e nts,.** includin
155ec 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d g the new anonym
155ed 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f ous savepoint, o
155ee 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72 65 pen on the B-Tre
155ef 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72 65 e. i.e. if there
155f0 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 .** are no activ
155f1 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 e savepoints and
155f2 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d no other statem
155f3 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 ent-transactions
155f4 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65 open,.** iState
155f5 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73 20 ment is 1. This
155f6 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f anonymous savepo
155f7 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65 61 int can be relea
155f8 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 sed or rolled ba
155f9 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 ck.** using the
155fa 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 sqlite3BtreeSave
155fb 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e point() function
155fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
155fd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
155fe 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 treeBeginStmt(Bt
155ff 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74 61 ree *p, int iSta
15600 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72 tement){. int r
15601 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 c;. BtShared *p
15602 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 Bt = p->pBt;. s
15603 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
15604 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d (p);. pBt->db =
15605 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 p->db;. assert
15606 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
15607 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 ANS_WRITE );. a
15608 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64 ssert( pBt->read
15609 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Only==0 );. ass
1560a 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e ert( iStatement>
1560b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 0 );. assert( i
1560c 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d Statement>p->db-
1560d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 >nSavepoint );.
1560e 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e if( NEVER(p->in
1560f 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 Trans!=TRANS_WRI
15610 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f TE || pBt->readO
15611 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d nly) ){. rc =
15612 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c SQLITE_INTERNAL
15613 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
15614 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 ssert( pBt->inTr
15615 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
15616 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a _WRITE );. /*
15617 20 41 74 20 74 68 65 20 70 61 67 65 72 20 6c 65 At the pager le
15618 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 vel, a statement
15619 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1561a 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 a savepoint with
1561b 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 . ** an index
1561c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c greater than al
1561d 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 l savepoints cre
1561e 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 ated explicitly
1561f 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c using. ** SQL
15620 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 statements. It
15621 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 is illegal to op
15622 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 en, release or r
15623 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 ollback any.
15624 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e ** such savepoin
15625 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 74 61 ts while the sta
15626 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
15627 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 on savepoint is
15628 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 active.. */.
15629 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1562a 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e agerOpenSavepoin
1562b 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 t(pBt->pPager, i
1562c 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a Statement);. }.
1562d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1562e 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1562f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
15630 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
15631 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 t to this functi
15632 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 on, op, is alway
15633 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c s SAVEPOINT_ROLL
15634 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 BACK.** or SAVEP
15635 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 OINT_RELEASE. Th
15636 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 is function eith
15637 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72 er releases or r
15638 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a olls back the.**
15639 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 savepoint ident
1563a 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
1563b 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 er iSavepoint, d
1563c 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 epending on the
1563d 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e value .** of op.
1563e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c .**.** Normally,
1563f 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 iSavepoint is g
15640 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
15641 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f qual to zero. Ho
15642 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a wever, if op is.
15643 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c ** SAVEPOINT_ROL
15644 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 LBACK, then iSav
15645 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 epoint may also
15646 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 be -1. In this c
15647 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 ase the .** cont
15648 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69 ents of the enti
15649 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 re transaction a
1564a 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 re rolled back.
1564b 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e This is differen
1564c 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d t.** from a norm
1564d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 al transaction r
1564e 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c ollback, as no l
1564f 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65 ocks are release
15650 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 d and the.** tra
15651 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 nsaction remains
15652 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 open..*/.SQLITE
15653 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15654 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
15655 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 nt(Btree *p, int
15656 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f op, int iSavepo
15657 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d int){. int rc =
15658 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
15659 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e ( p && p->inTran
1565a 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
1565b 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
1565c 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1565d 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 assert( op==SA
1565e 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 VEPOINT_RELEASE
1565f 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 || op==SAVEPOINT
15660 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 _ROLLBACK );.
15661 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f assert( iSavepo
15662 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 int>=0 || (iSave
15663 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d point==-1 && op=
15664 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
15665 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 ACK) );. sqli
15666 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
15667 3b 0a 20 20 20 20 70 42 74 2d 3e 64 62 20 3d 20 ;. pBt->db =
15668 70 2d 3e 64 62 3b 0a 20 20 20 20 72 63 20 3d 20 p->db;. rc =
15669 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 sqlite3PagerSave
1566a 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 point(pBt->pPage
1566b 72 2c 20 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e r, op, iSavepoin
1566c 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d t);. if( rc==
1566d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1566e 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 62 rc = newDatab
1566f 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a ase(pBt);. }.
15670 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15671 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 Leave(p);. }.
15672 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
15673 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
15674 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 cursor for the
15675 42 54 72 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 BTree whose root
15676 20 69 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a is on the page.
15677 2a 2a 20 69 54 61 62 6c 65 2e 20 20 54 68 65 20 ** iTable. The
15678 61 63 74 20 6f 66 20 61 63 71 75 69 72 69 6e 67 act of acquiring
15679 20 61 20 63 75 72 73 6f 72 20 67 65 74 73 20 61 a cursor gets a
1567a 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a read lock on .*
1567b 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
1567c 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 ile..**.** If wr
1567d 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 Flag==0, then th
1567e 65 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c e cursor can onl
1567f 79 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 y be used for re
15680 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 ading..** If wrF
15681 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 lag==1, then the
15682 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 75 cursor can be u
15683 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 sed for reading
15684 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e or for.** writin
15685 67 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 g if other condi
15686 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 6e tions for writin
15687 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 g are also met.
15688 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 These.** are th
15689 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 e conditions tha
1568a 74 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e t must be met in
1568b 20 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 69 order for writi
1568c 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f ng to.** be allo
1568d 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 wed:.**.** 1: T
1568e 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 he cursor must h
1568f 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 ave been opened
15690 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a with wrFlag==1.*
15691 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 *.** 2: Other d
15692 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
15693 6f 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74 ons that share t
15694 68 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61 he same pager ca
15695 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 che.** but w
15696 68 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 hich are not in
15697 74 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 the READ_UNCOMMI
15698 54 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e TTED state may n
15699 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 ot have.** c
1569a 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 ursors open with
1569b 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 wrFlag==0 on th
1569c 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f e same table. O
1569d 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 therwise.**
1569e 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 the changes made
1569f 20 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63 by this write c
156a0 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 ursor would be v
156a1 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 isible to.**
156a2 20 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72 the read cursor
156a3 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64 s in the other d
156a4 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
156a5 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 on..**.** 3: Th
156a6 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 e database must
156a7 62 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 be writable (not
156a8 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 on read-only me
156a9 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 dia).**.** 4: T
156aa 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 here must be an
156ab 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 active transacti
156ac 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 on..**.** No che
156ad 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f cking is done to
156ae 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
156af 70 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 6c page iTable real
156b0 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f ly is the.** roo
156b1 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 t page of a b-tr
156b2 65 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f ee. If it is no
156b3 74 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 t, then the curs
156b4 6f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 or acquired.** w
156b5 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 ill not work cor
156b6 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 rectly..**.** It
156b7 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
156b8 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 the sqlite3Btre
156b9 65 43 75 72 73 6f 72 53 69 7a 65 28 29 20 62 79 eCursorSize() by
156ba 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a tes of memory .*
156bb 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 * pointed to by
156bc 70 43 75 72 20 68 61 76 65 20 62 65 65 6e 20 7a pCur have been z
156bd 65 72 6f 65 64 20 62 79 20 74 68 65 20 63 61 6c eroed by the cal
156be 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ler..*/.static i
156bf 6e 74 20 62 74 72 65 65 43 75 72 73 6f 72 28 0a nt btreeCursor(.
156c0 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 Btree *p,
156c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156c2 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
156c3 62 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 btree */. int i
156c4 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 Table,
156c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156c6 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f /* Root page o
156c7 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 f table to open
156c8 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c */. int wrFlag,
156c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
156cb 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 to write. 0 rea
156cc 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 d-only */. stru
156cd 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 ct KeyInfo *pKey
156ce 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 Info,
156cf 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 /* First arg
156d0 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 to comparison fu
156d1 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 nction */. BtCu
156d2 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 rsor *pCur
156d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156d4 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 /* Space for
156d5 6e 65 77 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b new cursor */.){
156d6 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e . int rc;. Pgn
156d7 6f 20 6e 50 61 67 65 3b 0a 20 20 42 74 53 68 61 o nPage;. BtSha
156d8 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
156d9 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 t;.. assert( sq
156da 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
156db 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 utex(p) );. ass
156dc 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c ert( wrFlag==0 |
156dd 7c 20 77 72 46 6c 61 67 3d 3d 31 20 29 3b 0a 20 | wrFlag==1 );.
156de 20 69 66 28 20 77 72 46 6c 61 67 20 29 7b 0a 20 if( wrFlag ){.
156df 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d assert( !pBt-
156e0 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 >readOnly );.
156e1 20 69 66 28 20 4e 45 56 45 52 28 70 42 74 2d 3e if( NEVER(pBt->
156e2 72 65 61 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 readOnly) ){.
156e3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
156e4 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d _READONLY;. }
156e5 0a 20 20 20 20 72 63 20 3d 20 63 68 65 63 6b 46 . rc = checkF
156e6 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 orReadConflicts(
156e7 70 2c 20 69 54 61 62 6c 65 2c 20 30 2c 20 30 29 p, iTable, 0, 0)
156e8 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
156e9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
156ea 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
156eb 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
156ec 44 43 41 43 48 45 20 29 3b 0a 20 20 20 20 20 20 DCACHE );.
156ed 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
156ee 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d . }.. if( pBt-
156ef 3e 70 50 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20 >pPage1==0 ){.
156f0 20 20 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 rc = lockBtree
156f1 57 69 74 68 52 65 74 72 79 28 70 29 3b 0a 20 20 WithRetry(p);.
156f2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
156f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
156f4 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
156f5 7d 0a 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f }. pCur->pgnoRo
156f6 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c ot = (Pgno)iTabl
156f7 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 e;. rc = sqlite
156f8 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
156f9 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 28 69 6e pBt->pPager, (in
156fa 74 20 2a 29 26 6e 50 61 67 65 29 3b 20 0a 20 20 t *)&nPage); .
156fb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
156fc 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
156fd 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 54 rc;. }. if( iT
156fe 61 62 6c 65 3d 3d 31 20 26 26 20 6e 50 61 67 65 able==1 && nPage
156ff 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
15700 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20 SQLITE_EMPTY;.
15701 20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 63 75 goto create_cu
15702 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3b 0a rsor_exception;.
15703 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 41 6e }. rc = getAn
15704 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 dInitPage(pBt, p
15705 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 Cur->pgnoRoot, &
15706 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 pCur->apPage[0])
15707 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
15708 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 TE_OK ){. got
15709 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f 72 5f o create_cursor_
1570a 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a exception;. }..
1570b 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f /* Now that no
1570c 20 6f 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 other errors ca
1570d 6e 20 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 n occur, finish
1570e 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 filling in the B
1570f 74 43 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 tCursor. ** var
15710 69 61 62 6c 65 73 2c 20 6c 69 6e 6b 20 74 68 65 iables, link the
15711 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 cursor into the
15712 20 42 74 53 68 61 72 65 64 20 6c 69 73 74 20 61 BtShared list a
15713 6e 64 20 73 65 74 20 2a 70 70 43 75 72 20 28 74 nd set *ppCur (t
15714 68 65 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 61 he. ** output a
15715 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 rgument to this
15716 66 75 6e 63 74 69 6f 6e 29 2e 0a 20 20 2a 2f 0a function).. */.
15717 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f pCur->pKeyInfo
15718 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 = pKeyInfo;. p
15719 43 75 72 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b Cur->pBtree = p;
1571a 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 . pCur->pBt = p
1571b 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c Bt;. pCur->wrFl
1571c 61 67 20 3d 20 28 75 38 29 77 72 46 6c 61 67 3b ag = (u8)wrFlag;
1571d 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d . pCur->pNext =
1571e 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 pBt->pCursor;.
1571f 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 if( pCur->pNext
15720 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e ){. pCur->pN
15721 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 ext->pPrev = pCu
15722 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43 r;. }. pBt->pC
15723 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20 ursor = pCur;.
15724 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
15725 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 URSOR_INVALID;.
15726 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 pCur->cachedRow
15727 69 64 20 3d 20 30 3b 0a 0a 20 20 72 65 74 75 72 id = 0;.. retur
15728 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72 n SQLITE_OK;..cr
15729 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 eate_cursor_exce
1572a 70 74 69 6f 6e 3a 0a 20 20 72 65 6c 65 61 73 65 ption:. release
1572b 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 Page(pCur->apPag
1572c 65 5b 30 5d 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42 e[0]);. unlockB
1572d 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 treeIfUnused(pBt
1572e 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1572f 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
15730 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
15731 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 eCursor(. Btree
15732 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
15733 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15734 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 /* The bt
15735 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 ree */. int iTa
15736 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ble,
15737 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15738 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
15739 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 e of table to op
1573a 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c en */. int wrFl
1573b 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ag,
1573c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1573d 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 /* 1 to writ
1573e 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a e. 0 read-only *
1573f 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e /. struct KeyIn
15740 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 fo *pKeyInfo,
15741 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15742 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 /* First arg to
15743 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 xCompare() */.
15744 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 BtCursor *pCur
15745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15746 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
15747 72 69 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20 rite new cursor
15748 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
15749 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
1574a 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 reeEnter(p);. p
1574b 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 ->pBt->db = p->d
1574c 62 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 b;. rc = btreeC
1574d 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c ursor(p, iTable,
1574e 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 wrFlag, pKeyInf
1574f 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 o, pCur);. sqli
15750 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
15751 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
15752 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
15753 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74 43 he size of a BtC
15754 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20 ursor object in
15755 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 bytes..**.** Thi
15756 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20 s interfaces is
15757 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 75 needed so that u
15758 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 20 sers of cursors
15759 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a can preallocate.
1575a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 74 ** sufficient st
1575b 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 20 orage to hold a
1575c 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 43 cursor. The BtC
1575d 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 20 ursor object is
1575e 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65 opaque.** to use
1575f 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f rs so they canno
15760 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66 28 t do the sizeof(
15761 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20 74 ) themselves - t
15762 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a hey must call.**
15763 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
15764 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
15765 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
15766 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 eCursorSize(void
15767 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 ){. return size
15768 6f 66 28 42 74 43 75 72 73 6f 72 29 3b 0a 7d 0a of(BtCursor);.}.
15769 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 ./*.** Set the c
1576a 61 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75 ached rowid valu
1576b 65 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f e of every curso
1576c 72 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 r in the same da
1576d 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 tabase file.** a
1576e 73 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e s pCur and havin
1576f 67 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 g the same root
15770 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70 page number as p
15771 43 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 Cur. The value
15772 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f is.** set to iRo
15773 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 wid..**.** Only
15774 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 76 positive rowid v
15775 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 alues are consid
15776 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74 ered valid for t
15777 68 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 his cache..** Th
15778 65 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 69 e cache is initi
15779 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 alized to zero,
1577a 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e indicating an in
1577b 76 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 valid cache..**
1577c 41 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 A btree will wor
1577d 6b 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f k fine with zero
1577e 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 or negative row
1577f 69 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61 ids. We just ca
15780 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 nnot.** cache ze
15781 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 ro or negative r
15782 6f 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61 owids, which mea
15783 6e 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 75 ns tables that u
15784 73 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 se zero or.** ne
15785 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69 gative rowids mi
15786 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 ght run a little
15787 20 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e slower. But in
15788 20 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a practice, zero.
15789 2a 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 ** or negative r
1578a 6f 77 69 64 73 20 61 72 65 20 76 65 72 79 20 75 owids are very u
1578b 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 ncommon so this
1578c 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 should not be a
1578d 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 problem..*/.SQLI
1578e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1578f 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 sqlite3BtreeSetC
15790 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 achedRowid(BtCur
15791 73 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 sor *pCur, sqlit
15792 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 e3_int64 iRowid)
15793 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
15794 0a 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 . for(p=pCur->p
15795 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
15796 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
15797 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 if( p->pgnoRoot
15798 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 ==pCur->pgnoRoot
15799 20 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 ) p->cachedRowi
1579a 64 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a d = iRowid;. }.
1579b 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1579c 63 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f cachedRowid==iRo
1579d 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 wid );.}../*.**
1579e 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 65 Return the cache
1579f 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 d rowid for the
157a0 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 given cursor. A
157a1 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 negative or zer
157a2 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 o.** return valu
157a3 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 e indicates that
157a4 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 the rowid cache
157a5 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 is invalid and
157a6 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e should be.** ign
157a7 6f 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f ored. If the ro
157a8 77 69 64 20 63 61 63 68 65 20 68 61 73 20 6e 65 wid cache has ne
157a9 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 ver before been
157aa 73 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a set, then a.** z
157ab 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e ero is returned.
157ac 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
157ad 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 TE sqlite3_int64
157ae 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
157af 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 CachedRowid(BtCu
157b0 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 rsor *pCur){. r
157b1 65 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 eturn pCur->cach
157b2 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a edRowid;.}../*.*
157b3 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 * Close a cursor
157b4 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b . The read lock
157b5 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
157b6 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 file is release
157b7 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 d.** when the la
157b8 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f st cursor is clo
157b9 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 sed..*/.SQLITE_P
157ba 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
157bb 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 e3BtreeCloseCurs
157bc 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
157bd 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 r){. Btree *pBt
157be 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 ree = pCur->pBtr
157bf 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 ee;. if( pBtree
157c0 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
157c1 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 BtShared *pBt
157c2 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 = pCur->pBt;.
157c3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
157c4 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 ter(pBtree);.
157c5 20 70 42 74 2d 3e 64 62 20 3d 20 70 42 74 72 65 pBt->db = pBtre
157c6 65 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 e->db;. sqlit
157c7 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
157c8 6f 72 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 or(pCur);. if
157c9 28 20 70 43 75 72 2d 3e 70 50 72 65 76 20 29 7b ( pCur->pPrev ){
157ca 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 50 72 . pCur->pPr
157cb 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 ev->pNext = pCur
157cc 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c ->pNext;. }el
157cd 73 65 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 se{. pBt->p
157ce 43 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 Cursor = pCur->p
157cf 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Next;. }.
157d0 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 if( pCur->pNext
157d1 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 ){. pCur->p
157d2 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 Next->pPrev = pC
157d3 75 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d ur->pPrev;. }
157d4 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
157d5 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b =pCur->iPage; i+
157d6 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 +){. releas
157d7 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 ePage(pCur->apPa
157d8 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 ge[i]);. }.
157d9 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 unlockBtreeIfU
157da 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 nused(pBt);.
157db 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c invalidateOverfl
157dc 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 owCache(pCur);.
157dd 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 /* sqlite3_fr
157de 65 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 ee(pCur); */.
157df 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
157e0 76 65 28 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a ve(pBtree);. }.
157e1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
157e2 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b OK;.}../*.** Mak
157e3 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75 e a temporary cu
157e4 72 73 6f 72 20 62 79 20 66 69 6c 6c 69 6e 67 20 rsor by filling
157e5 69 6e 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66 in the fields of
157e6 20 70 54 65 6d 70 43 75 72 2e 0a 2a 2a 20 54 68 pTempCur..** Th
157e7 65 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 e temporary curs
157e8 6f 72 20 69 73 20 6e 6f 74 20 6f 6e 20 74 68 65 or is not on the
157e9 20 63 75 72 73 6f 72 20 6c 69 73 74 20 66 6f 72 cursor list for
157ea 20 74 68 65 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 the Btree..*/.S
157eb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
157ec 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 id sqlite3BtreeG
157ed 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 etTempCursor(BtC
157ee 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 43 ursor *pCur, BtC
157ef 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 29 ursor *pTempCur)
157f0 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 {. int i;. ass
157f1 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
157f2 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
157f3 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 43 75 72 memcpy(pTempCur
157f4 2c 20 70 43 75 72 2c 20 73 69 7a 65 6f 66 28 42 , pCur, sizeof(B
157f5 74 43 75 72 73 6f 72 29 29 3b 0a 20 20 70 54 65 tCursor));. pTe
157f6 6d 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 30 mpCur->pNext = 0
157f7 3b 0a 20 20 70 54 65 6d 70 43 75 72 2d 3e 70 50 ;. pTempCur->pP
157f8 72 65 76 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 rev = 0;. for(i
157f9 3d 30 3b 20 69 3c 3d 70 54 65 6d 70 43 75 72 2d =0; i<=pTempCur-
157fa 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 >iPage; i++){.
157fb 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 sqlite3PagerRe
157fc 66 28 70 54 65 6d 70 43 75 72 2d 3e 61 70 50 61 f(pTempCur->apPa
157fd 67 65 5b 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b ge[i]->pDbPage);
157fe 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
157ff 54 65 6d 70 43 75 72 2d 3e 70 4b 65 79 3d 3d 30 TempCur->pKey==0
15800 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c );.}../*.** Del
15801 65 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ete a temporary
15802 63 75 72 73 6f 72 20 73 75 63 68 20 61 73 20 77 cursor such as w
15803 61 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 43 as made by the C
15804 72 65 61 74 65 54 65 6d 70 6f 72 61 72 79 43 75 reateTemporaryCu
15805 72 73 6f 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69 rsor().** functi
15806 6f 6e 20 61 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c on above..*/.SQL
15807 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
15808 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 6c sqlite3BtreeRel
15809 65 61 73 65 54 65 6d 70 43 75 72 73 6f 72 28 42 easeTempCursor(B
1580a 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
1580b 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 int i;. asser
1580c 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
1580d 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 66 tex(pCur) );. f
1580e 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d or(i=0; i<=pCur-
1580f 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 >iPage; i++){.
15810 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
15811 72 65 66 28 70 43 75 72 2d 3e 61 70 50 61 67 65 ref(pCur->apPage
15812 5b 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 [i]->pDbPage);.
15813 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
15814 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 7d e(pCur->pKey);.}
15815 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 ..../*.** Make s
15816 75 72 65 20 74 68 65 20 42 74 43 75 72 73 6f 72 ure the BtCursor
15817 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 * given in the a
15818 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20 76 61 rgument has a va
15819 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e lid.** BtCursor.
1581a 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 info structure.
1581b 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c If it is not al
1581c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63 61 6c ready valid, cal
1581d 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 l.** sqlite3Btre
1581e 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 eParseCell() to
1581f 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a fill it in..**.*
15820 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 * BtCursor.info
15821 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 68 is a cache of th
15822 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e e information in
15823 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c the current cel
15824 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 l..** Using this
15825 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20 74 cache reduces t
15826 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c he number of cal
15827 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 ls to sqlite3Btr
15828 65 65 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a eeParseCell()..*
15829 2a 0a 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a *.** 2007-06-25:
1582a 20 20 54 68 65 72 65 20 69 73 20 61 20 62 75 67 There is a bug
1582b 20 69 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e in some version
1582c 73 20 6f 66 20 4d 53 56 43 20 74 68 61 74 20 63 s of MSVC that c
1582d 61 75 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 ause the.** comp
1582e 69 6c 65 72 20 74 6f 20 63 72 61 73 68 20 77 68 iler to crash wh
1582f 65 6e 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 en getCellInfo()
15830 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
15831 61 73 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 as a macro..** B
15832 75 74 20 74 68 65 72 65 20 69 73 20 61 20 6d 65 ut there is a me
15833 61 73 75 72 65 61 62 6c 65 20 73 70 65 65 64 20 asureable speed
15834 61 64 76 61 6e 74 61 67 65 20 74 6f 20 75 73 69 advantage to usi
15835 6e 67 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 ng the macro on
15836 67 63 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 gcc.** (when les
15837 73 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d s compiler optim
15838 69 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f izations like -O
15839 73 20 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65 s or -O0 are use
1583a 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d d and the.** com
1583b 70 69 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 piler is not doi
1583c 6e 67 20 61 67 72 65 73 73 69 76 65 20 69 6e 6c ng agressive inl
1583d 69 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 ining.) So we u
1583e 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 se a real functi
1583f 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 on.** for MSVC a
15840 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 nd a macro for e
15841 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 verything else.
15842 20 54 69 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a Ticket #2457..*
15843 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 /.#ifndef NDEBUG
15844 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 61 . static void a
15845 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 ssertCellInfo(Bt
15846 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
15847 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f CellInfo info
15848 3b 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 ;. int iPage
15849 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 = pCur->iPage;.
1584a 20 20 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c memset(&info,
1584b 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 0, sizeof(info)
1584c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
1584d 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 reeParseCell(pCu
1584e 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d r->apPage[iPage]
1584f 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 , pCur->aiIdx[iP
15850 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 age], &info);.
15851 20 20 61 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 assert( memcmp
15852 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 (&info, &pCur->i
15853 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f nfo, sizeof(info
15854 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c ))==0 );. }.#el
15855 73 65 0a 20 20 23 64 65 66 69 6e 65 20 61 73 73 se. #define ass
15856 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 ertCellInfo(x).#
15857 65 6e 64 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 endif.#ifdef _MS
15858 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 C_VER. /* Use a
15859 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 real function i
1585a 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 n MSVC to work a
1585b 72 6f 75 6e 64 20 62 75 67 73 20 69 6e 20 74 68 round bugs in th
1585c 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a at compiler. */.
1585d 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 static void ge
1585e 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 tCellInfo(BtCurs
1585f 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 or *pCur){. i
15860 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 f( pCur->info.nS
15861 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ize==0 ){.
15862 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 int iPage = pCur
15863 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 20 20 73 ->iPage;. s
15864 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
15865 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 Cell(pCur->apPag
15866 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 e[iPage],pCur->a
15867 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 iIdx[iPage],&pCu
15868 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 r->info);.
15869 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
1586a 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 1;. }else{.
1586b 20 20 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c assertCell
1586c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 Info(pCur);.
1586d 7d 0a 20 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 }. }.#else /* i
1586e 66 20 6e 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a f not _MSC_VER *
1586f 2f 0a 20 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 /. /* Use a mac
15870 72 6f 20 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 ro in all other
15871 63 6f 6d 70 69 6c 65 72 73 20 73 6f 20 74 68 61 compilers so tha
15872 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 t the function i
15873 73 20 69 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 s inlined */.#de
15874 66 69 6e 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f fine getCellInfo
15875 28 70 43 75 72 29 20 20 20 20 20 20 20 20 20 20 (pCur)
15876 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15878 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
15879 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e if( pCur->info.n
1587a 53 69 7a 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 Size==0 ){
1587b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1587c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1587d 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
1587e 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 int iPage = p
1587f 43 75 72 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 Cur->iPage;
15880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15882 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
15883 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15884 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e ParseCell(pCur->
15885 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 apPage[iPage],pC
15886 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d ur->aiIdx[iPage]
15887 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c ,&pCur->info); \
15888 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 . pCur->valid
15889 4e 4b 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20 NKey = 1;
1588a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1588b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1588c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1588d 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 \. }else{
1588e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1588f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15891 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15892 20 5c 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c \. assertCel
15893 6c 49 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20 lInfo(pCur);
15894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15896 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15897 20 20 5c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f \. }.#endif /
15898 2a 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 2f * _MSC_VER */../
15899 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 *.** Set *pSize
1589a 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 to the size of t
1589b 68 65 20 62 75 66 66 65 72 20 6e 65 65 64 65 64 he buffer needed
1589c 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 61 6c to hold the val
1589d 75 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 ue of.** the key
1589e 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 for the current
1589f 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68 65 20 entry. If the
158a0 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f cursor is not po
158a1 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 inting.** to a v
158a2 61 6c 69 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 alid entry, *pSi
158a3 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 2e 20 ze is set to 0.
158a4 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 .**.** For a tab
158a5 6c 65 20 77 69 74 68 20 74 68 65 20 49 4e 54 4b le with the INTK
158a6 45 59 20 66 6c 61 67 20 73 65 74 2c 20 74 68 69 EY flag set, thi
158a7 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
158a8 73 20 74 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 s the key.** its
158a9 65 6c 66 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d elf, not the num
158aa 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
158ab 74 68 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 the key..*/.SQLI
158ac 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
158ad 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 qlite3BtreeKeySi
158ae 7a 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ze(BtCursor *pCu
158af 72 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a r, i64 *pSize){.
158b0 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 int rc;.. ass
158b1 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
158b2 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
158b3 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 rc = restoreCur
158b4 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 sorPosition(pCur
158b5 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
158b6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 ITE_OK ){. as
158b7 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
158b8 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c te==CURSOR_INVAL
158b9 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 ID || pCur->eSta
158ba 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
158bb 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 );. if( pCur
158bc 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
158bd 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 _INVALID ){.
158be 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 *pSize = 0;.
158bf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 }else{. g
158c0 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 etCellInfo(pCur)
158c1 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d ;. *pSize =
158c2 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 pCur->info.nKey
158c3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
158c4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
158c5 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 * Set *pSize to
158c6 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
158c7 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74 tes of data in t
158c8 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 he entry the.**
158c9 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 cursor currently
158ca 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77 points to. Alw
158cb 61 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ays return SQLIT
158cc 45 5f 4f 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65 E_OK..** Failure
158cd 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
158ce 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 . If the cursor
158cf 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
158d0 79 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f y.** pointing to
158d1 20 61 6e 20 65 6e 74 72 79 20 28 77 68 69 63 68 an entry (which
158d2 20 63 61 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 can happen, for
158d3 20 65 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 example, if.**
158d4 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
158d5 65 6d 70 74 79 29 20 74 68 65 6e 20 2a 70 53 69 empty) then *pSi
158d6 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a ze is set to 0..
158d7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
158d8 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
158d9 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 eeDataSize(BtCur
158da 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a sor *pCur, u32 *
158db 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 pSize){. int rc
158dc 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
158dd 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
158de 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 ur) );. rc = re
158df 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
158e0 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 ion(pCur);. if(
158e1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
158e2 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 {. assert( pC
158e3 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
158e4 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 OR_INVALID || pC
158e5 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
158e6 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 OR_VALID );.
158e7 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
158e8 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
158e9 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 ){. /* Not
158ea 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 76 pointing at a v
158eb 61 6c 69 64 20 65 6e 74 72 79 20 2d 20 73 65 74 alid entry - set
158ec 20 2a 70 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f *pSize to 0. */
158ed 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 . *pSize =
158ee 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
158ef 20 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 getCellInfo(
158f0 70 43 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 pCur);. *pS
158f1 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f ize = pCur->info
158f2 2e 6e 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 .nData;. }.
158f3 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
158f4 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 ../*.** Given th
158f5 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
158f6 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
158f7 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
158f8 65 20 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 e (parameter.**
158f9 6f 76 66 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 ovfl), this func
158fa 74 69 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 70 tion finds the p
158fb 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
158fc 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 e next page in t
158fd 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 he .** linked li
158fe 73 74 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 st of overflow p
158ff 61 67 65 73 2e 20 49 66 20 70 6f 73 73 69 62 6c ages. If possibl
15900 65 2c 20 69 74 20 75 73 65 73 20 74 68 65 20 61 e, it uses the a
15901 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f uto-vacuum.** po
15902 69 6e 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69 inter-map data i
15903 6e 73 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e nstead of readin
15904 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 g the content of
15905 20 70 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f page ovfl to do
15906 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 so. .**.** If a
15907 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 n error occurs a
15908 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
15909 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
1590a 20 4f 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a Otherwise:.**.*
1590b 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 * The page numbe
1590c 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76 r of the next ov
1590d 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 erflow page in t
1590e 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 he linked list i
1590f 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f s .** written to
15910 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 *pPgnoNext. If
15911 70 61 67 65 20 6f 76 66 6c 20 69 73 20 74 68 65 page ovfl is the
15912 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 69 74 last page in it
15913 73 20 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 73 s linked .** lis
15914 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 t, *pPgnoNext is
15915 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a set to zero. .*
15916 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69 *.** If ppPage i
15917 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 20 s not NULL, and
15918 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 a reference to t
15919 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 he MemPage objec
1591a 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a t corresponding.
1591b 2a 2a 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 ** to page numbe
1591c 72 20 70 4f 76 66 6c 20 77 61 73 20 6f 62 74 61 r pOvfl was obta
1591d 69 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70 50 61 ined, then *ppPa
1591e 67 65 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 ge is set to poi
1591f 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 72 65 nt to that.** re
15920 66 65 72 65 6e 63 65 2e 20 49 74 20 69 73 20 74 ference. It is t
15921 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 he responsibilit
15922 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 y of the caller
15923 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 to call releaseP
15924 61 67 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 50 age().** on *ppP
15925 61 67 65 20 74 6f 20 66 72 65 65 20 74 68 65 20 age to free the
15926 72 65 66 65 72 65 6e 63 65 2e 20 49 6e 20 6e 6f reference. In no
15927 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 20 6f reference was o
15928 62 74 61 69 6e 65 64 20 28 62 65 63 61 75 73 65 btained (because
15929 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d .** the pointer-
1592a 6d 61 70 20 77 61 73 20 75 73 65 64 20 74 6f 20 map was used to
1592b 6f 62 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65 obtain the value
1592c 20 66 6f 72 20 2a 70 50 67 6e 6f 4e 65 78 74 29 for *pPgnoNext)
1592d 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 67 , then.** *ppPag
1592e 65 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f e is set to zero
1592f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
15930 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 getOverflowPage(
15931 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
15932 2c 20 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 , . Pgno ovfl,
15933 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15934 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 /* Overflow pa
15935 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ge */. MemPage
15936 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 **ppPage,
15937 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d /* OUT: Mem
15938 50 61 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 Page handle (may
15939 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 be NULL) */. P
1593a 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 gno *pPgnoNext
1593b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1593c 55 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f UT: Next overflo
1593d 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f w page number */
1593e 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 .){. Pgno next
1593f 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a = 0;. MemPage *
15940 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 pPage = 0;. int
15941 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
15942 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
15943 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
15944 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15945 61 73 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 assert(pPgnoNext
15946 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
15947 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
15948 55 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 UM. /* Try to f
15949 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 ind the next pag
1594a 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f e in the overflo
1594b 77 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 w list using the
1594c 0a 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d . ** autovacuum
1594d 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
1594e 65 73 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 es. Guess that t
1594f 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
15950 0a 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c . ** the overfl
15951 6f 77 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 ow list is page
15952 6e 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e number (ovfl+1).
15953 20 49 66 20 74 68 61 74 20 67 75 65 73 73 20 74 If that guess t
15954 75 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 urns . ** out t
15955 6f 20 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c o be wrong, fall
15956 20 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 back to loading
15957 20 74 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 the data of pag
15958 65 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f e . ** number o
15959 76 66 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 vfl to determine
1595a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e the next page n
1595b 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 umber.. */. if
1595c 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
1595d 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 m ){. Pgno pg
1595e 6e 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 no;. Pgno iGu
1595f 65 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 ess = ovfl+1;.
15960 20 20 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 u8 eType;..
15961 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 while( PTRMAP_I
15962 53 50 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 SPAGE(pBt, iGues
15963 73 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 s) || iGuess==PE
15964 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
15965 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 pBt) ){. iG
15966 75 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 uess++;. }..
15967 20 20 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70 if( iGuess<=p
15968 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
15969 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d t) ){. rc =
1596a 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
1596b 69 47 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 iGuess, &eType,
1596c 26 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 &pgno);. if
1596d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1596e 26 26 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 && eType==PTRMAP
1596f 5f 4f 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67 _OVERFLOW2 && pg
15970 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 no==ovfl ){.
15971 20 20 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73 next = iGues
15972 73 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 s;. rc =
15973 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
15974 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
15975 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 72 63 3d endif.. if( rc=
15976 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15977 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
15978 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
15979 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 ovfl, &pPage, 0)
1597a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 72 63 3d ;. assert(rc=
1597b 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 =SQLITE_OK || pP
1597c 61 67 65 3d 3d 30 29 3b 0a 20 20 20 20 69 66 28 age==0);. if(
1597d 20 6e 65 78 74 3d 3d 30 20 26 26 20 72 63 3d 3d next==0 && rc==
1597e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1597f 20 20 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 next = get4by
15980 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 te(pPage->aData)
15981 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a ;. }. }.. *
15982 70 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 pPgnoNext = next
15983 3b 0a 20 20 69 66 28 20 70 70 50 61 67 65 20 29 ;. if( ppPage )
15984 7b 0a 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 {. *ppPage =
15985 70 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a pPage;. }else{.
15986 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
15987 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 pPage);. }. re
15988 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 turn (rc==SQLITE
15989 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f _DONE ? SQLITE_O
1598a 4b 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a K : rc);.}../*.*
1598b 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d * Copy data from
1598c 20 61 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 a buffer to a p
1598d 61 67 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 age, or from a p
1598e 61 67 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e age to a buffer.
1598f 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 .**.** pPayload
15990 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
15991 64 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 data stored on d
15992 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62 atabase page pDb
15993 50 61 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 Page..** If argu
15994 6d 65 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 ment eOp is fals
15995 65 2c 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 e, then nByte by
15996 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 tes of data are
15997 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 copied.** from p
15998 50 61 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 Payload to the b
15999 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 uffer pointed at
1599a 20 62 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 by pBuf. If eOp
1599b 20 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 is true,.** the
1599c 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 n sqlite3PagerWr
1599d 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 ite() is called
1599e 6f 6e 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e on pDbPage and n
1599f 42 79 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 Byte bytes.** of
159a0 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 data are copied
159a1 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 from the buffer
159a2 20 70 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 pBuf to pPayloa
159a3 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f d..**.** SQLITE_
159a4 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f OK is returned o
159a5 6e 20 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 n success, other
159a6 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f wise an error co
159a7 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e de..*/.static in
159a8 74 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 t copyPayload(.
159a9 20 76 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c void *pPayload,
159aa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
159ab 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 inter to page da
159ac 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 ta */. void *pB
159ad 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 uf,
159ae 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
159af 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 buffer */. int
159b0 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 nByte,
159b1 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
159b2 6f 66 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 of bytes to copy
159b3 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 */. int eOp,
159b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
159b5 2f 2a 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f /* 0 -> copy fro
159b6 6d 20 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 m page, 1 -> cop
159b7 79 20 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 y to page */. D
159b8 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 bPage *pDbPage
159b9 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
159ba 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 containing pPay
159bb 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 load */.){. if(
159bc 20 65 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 eOp ){. /* C
159bd 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 opy data from bu
159be 66 66 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 ffer to page (a
159bf 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 write operation)
159c0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d */. int rc =
159c1 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
159c2 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 te(pDbPage);.
159c3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
159c4 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
159c5 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
159c6 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 memcpy(pPayload
159c7 2c 20 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a , pBuf, nByte);.
159c8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
159c9 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 Copy data from p
159ca 61 67 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 age to buffer (a
159cb 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 read operation)
159cc 20 2a 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 */. memcpy(p
159cd 42 75 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e Buf, pPayload, n
159ce 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Byte);. }. ret
159cf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
159d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
159d1 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
159d2 20 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 read or overwri
159d3 74 65 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 te payload infor
159d4 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 mation.** for th
159d5 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 e entry that the
159d6 20 70 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 pCur cursor is
159d7 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 pointing to. If
159d8 74 68 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d the eOp.** param
159d9 65 74 65 72 20 69 73 20 30 2c 20 74 68 69 73 20 eter is 0, this
159da 69 73 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 is a read operat
159db 69 6f 6e 20 28 64 61 74 61 20 63 6f 70 69 65 64 ion (data copied
159dc 20 69 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 into.** buffer
159dd 70 42 75 66 29 2e 20 49 66 20 69 74 20 69 73 20 pBuf). If it is
159de 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 non-zero, a writ
159df 65 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 66 e (data copied f
159e0 72 6f 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 rom.** buffer pB
159e1 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 uf)..**.** A tot
159e2 61 6c 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65 al of "amt" byte
159e3 73 20 61 72 65 20 72 65 61 64 20 6f 72 20 77 72 s are read or wr
159e4 69 74 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 itten beginning
159e5 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 at "offset"..**
159e6 44 61 74 61 20 69 73 20 72 65 61 64 20 74 6f 20 Data is read to
159e7 6f 72 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 or from the buff
159e8 65 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 er pBuf..**.** T
159e9 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 his routine does
159ea 20 6e 6f 74 20 6d 61 6b 65 20 61 20 64 69 73 74 not make a dist
159eb 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 inction between
159ec 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a key and data..**
159ed 20 49 74 20 6a 75 73 74 20 72 65 61 64 73 20 6f It just reads o
159ee 72 20 77 72 69 74 65 73 20 62 79 74 65 73 20 66 r writes bytes f
159ef 72 6f 6d 20 74 68 65 20 70 61 79 6c 6f 61 64 20 rom the payload
159f0 61 72 65 61 2e 20 20 44 61 74 61 20 6d 69 67 68 area. Data migh
159f1 74 20 0a 2a 2a 20 61 70 70 65 61 72 20 6f 6e 20 t .** appear on
159f2 74 68 65 20 6d 61 69 6e 20 70 61 67 65 20 6f 72 the main page or
159f3 20 62 65 20 73 63 61 74 74 65 72 65 64 20 6f 75 be scattered ou
159f4 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 t on multiple ov
159f5 65 72 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 73 erflow .** pages
159f6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 ..**.** If the B
159f7 74 43 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c tCursor.isIncrbl
159f8 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 obHandle flag is
159f9 20 73 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75 set, and the cu
159fa 72 72 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 rrent.** cursor
159fb 65 6e 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f entry uses one o
159fc 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 r more overflow
159fd 70 61 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63 pages, this func
159fe 74 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 tion.** allocate
159ff 73 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 s space for and
15a00 6c 61 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 lazily popluates
15a01 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
15a02 67 65 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 ge-list .** cach
15a03 65 20 61 72 72 61 79 20 28 42 74 43 75 72 73 6f e array (BtCurso
15a04 72 2e 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 r.aOverflow). Su
15a05 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 bsequent calls u
15a06 73 65 20 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 se this.** cache
15a07 20 74 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 to make seeking
15a08 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 to the supplied
15a09 20 6f 66 66 73 65 74 20 6d 6f 72 65 20 65 66 66 offset more eff
15a0a 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e icient..**.** On
15a0b 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ce an overflow p
15a0c 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 age-list cache h
15a0d 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 as been allocate
15a0e 64 2c 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 d, it may be.**
15a0f 69 6e 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 invalidated if s
15a10 6f 6d 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 ome other cursor
15a11 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 writes to the s
15a12 61 6d 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 ame table, or if
15a13 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 .** the cursor i
15a14 73 20 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 s moved to a dif
15a15 66 65 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 ferent row. Addi
15a16 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 tionally, in aut
15a17 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 o-vacuum.** mode
15a18 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 , the following
15a19 65 76 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c events may inval
15a1a 69 64 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f idate an overflo
15a1b 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
15a1c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 e..**.** * An
15a1d 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
15a1e 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d um,.** * A com
15a1f 6d 69 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 mit in auto_vacu
15a20 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a um="full" mode,.
15a21 2a 2a 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 ** * Creating
15a22 61 20 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 a table (may req
15a23 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f uire moving an o
15a24 76 65 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a verflow page)..*
15a25 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 /.static int acc
15a26 65 73 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 essPayload(. Bt
15a27 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 Cursor *pCur,
15a28 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 /* Cursor poi
15a29 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 nting to entry t
15a2a 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 o read from */.
15a2b 20 75 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 u32 offset,
15a2c 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 /* Begin r
15a2d 65 61 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 eading this far
15a2e 69 6e 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a into payload */.
15a2f 20 20 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 u32 amt,
15a30 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 /* Read t
15a31 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a his many bytes *
15a32 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
15a33 72 20 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 r *pBuf, /* Writ
15a34 65 20 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f e the bytes into
15a35 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 this buffer */
15a36 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 2c 20 . int skipKey,
15a37 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 /* offse
15a38 74 20 62 65 67 69 6e 73 20 61 74 20 64 61 74 61 t begins at data
15a39 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 if this is true
15a3a 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 */. int eOp
15a3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 /* ze
15a3c 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d ro to read. non-
15a3d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a zero to write. *
15a3e 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 /.){. unsigned
15a3f 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a char *aPayload;.
15a40 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
15a41 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 E_OK;. u32 nKey
15a42 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30 ;. int iIdx = 0
15a43 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
15a44 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
15a45 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 e[pCur->iPage];
15a46 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20 6f 66 /* Btree page of
15a47 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20 2a current entry *
15a48 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
15a49 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20 20 t = pCur->pBt;
15a4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15a4b 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20 63 75 /* Btree this cu
15a4c 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 rsor belongs to
15a4d 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 */.. assert( pP
15a4e 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 age );. assert(
15a4f 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
15a50 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
15a51 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
15a52 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
15a53 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 ]<pPage->nCell )
15a54 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 ;. assert( curs
15a55 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
15a56 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c r) );.. getCell
15a57 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 Info(pCur);. aP
15a58 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 ayload = pCur->i
15a59 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 nfo.pCell + pCur
15a5a 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a ->info.nHeader;.
15a5b 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d nKey = (pPage-
15a5c 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 28 69 >intKey ? 0 : (i
15a5d 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b nt)pCur->info.nK
15a5e 65 79 29 3b 0a 0a 20 20 69 66 28 20 73 6b 69 70 ey);.. if( skip
15a5f 4b 65 79 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 Key ){. offse
15a60 74 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 7d 0a 20 t += nKey;. }.
15a61 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 20 if( offset+amt
15a62 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 > nKey+pCur->inf
15a63 6f 2e 6e 44 61 74 61 20 0a 20 20 20 7c 7c 20 26 o.nData . || &
15a64 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 aPayload[pCur->i
15a65 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 nfo.nLocal] > &p
15a66 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d Page->aData[pBt-
15a67 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29 >usableSize]. )
15a68 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 {. /* Trying
15a69 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 to read or write
15a6a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
15a6b 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e 20 the data is an
15a6c 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 error */. ret
15a6d 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15a6e 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 PT_BKPT;. }..
15a6f 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 61 /* Check if data
15a70 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 72 must be read/wr
15a71 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 itten to/from th
15a72 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74 73 e btree page its
15a73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 elf. */. if( of
15a74 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e fset<pCur->info.
15a75 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e nLocal ){. in
15a76 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 t a = amt;. i
15a77 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 f( a+offset>pCur
15a78 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b ->info.nLocal ){
15a79 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d . a = pCur-
15a7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f >info.nLocal - o
15a7b 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 ffset;. }.
15a7c 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 rc = copyPayloa
15a7d 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 d(&aPayload[offs
15a7e 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f et], pBuf, a, eO
15a7f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 p, pPage->pDbPag
15a80 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d e);. offset =
15a81 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 0;. pBuf +=
15a82 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b a;. amt -= a;
15a83 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 . }else{. of
15a84 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e fset -= pCur->in
15a85 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a fo.nLocal;. }..
15a86 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
15a87 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a _OK && amt>0 ){.
15a88 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 const u32 ov
15a89 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 flSize = pBt->us
15a8a 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f ableSize - 4; /
15a8b 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 * Bytes content
15a8c 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f per ovfl page */
15a8d 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 . Pgno nextPa
15a8e 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 ge;.. nextPag
15a8f 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 50 e = get4byte(&aP
15a90 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 ayload[pCur->inf
15a91 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 o.nLocal]);..#if
15a92 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15a93 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a _INCRBLOB. /*
15a94 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c If the isIncrbl
15a95 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 obHandle flag is
15a96 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 43 set and the BtC
15a97 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b ursor.aOverflow[
15a98 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 ]. ** has not
15a99 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c been allocated,
15a9a 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 allocate it now
15a9b 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20 73 . The array is s
15a9c 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f ized at. ** o
15a9d 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 ne entry for eac
15a9e 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 h overflow page
15a9f 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 in the overflow
15aa0 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a chain. The. *
15aa1 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 * page number of
15aa2 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 the first overf
15aa3 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72 low page is stor
15aa4 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b ed in aOverflow[
15aa5 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20 0],. ** etc.
15aa6 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 A value of 0 in
15aa7 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 the aOverflow[]
15aa8 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74 array means "not
15aa9 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 yet known".
15aaa 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 73 ** (the cache is
15aab 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 lazily populate
15aac 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 d).. */. i
15aad 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 f( pCur->isIncrb
15aae 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43 lobHandle && !pC
15aaf 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b ur->aOverflow ){
15ab0 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c . int nOvfl
15ab1 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e = (pCur->info.n
15ab2 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e Payload-pCur->in
15ab3 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 fo.nLocal+ovflSi
15ab4 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a ze-1)/ovflSize;.
15ab5 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 pCur->aOve
15ab6 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29 rflow = (Pgno *)
15ab7 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
15ab8 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e o(sizeof(Pgno)*n
15ab9 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 Ovfl);. if(
15aba 20 6e 4f 76 66 6c 20 26 26 20 21 70 43 75 72 2d nOvfl && !pCur-
15abb 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 >aOverflow ){.
15abc 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
15abd 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d E_NOMEM;. }
15abe 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 . }.. /* I
15abf 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 f the overflow p
15ac0 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 age-list cache h
15ac1 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 as been allocate
15ac2 64 20 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a d and the. **
15ac3 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 entry for the f
15ac4 69 72 73 74 20 72 65 71 75 69 72 65 64 20 6f 76 irst required ov
15ac5 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 76 erflow page is v
15ac6 61 6c 69 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a alid, skip. *
15ac7 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 69 74 * directly to it
15ac8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
15ac9 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
15aca 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 && pCur->aOverf
15acb 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 low[offset/ovflS
15acc 69 7a 65 5d 20 29 7b 0a 20 20 20 20 20 20 69 49 ize] ){. iI
15acd 64 78 20 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66 dx = (offset/ovf
15ace 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65 lSize);. ne
15acf 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 xtPage = pCur->a
15ad0 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a Overflow[iIdx];.
15ad1 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 28 offset = (
15ad2 6f 66 66 73 65 74 25 6f 76 66 6c 53 69 7a 65 29 offset%ovflSize)
15ad3 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a ;. }.#endif..
15ad4 20 20 20 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53 for( ; rc==S
15ad5 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e QLITE_OK && amt>
15ad6 30 20 26 26 20 6e 65 78 74 50 61 67 65 3b 20 69 0 && nextPage; i
15ad7 49 64 78 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66 Idx++){..#ifndef
15ad8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
15ad9 52 42 4c 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49 RBLOB. /* I
15ada 66 20 72 65 71 75 69 72 65 64 2c 20 70 6f 70 75 f required, popu
15adb 6c 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f late the overflo
15adc 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
15add 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 e. */. if(
15ade 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
15adf 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
15ae0 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c t(!pCur->aOverfl
15ae1 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 70 43 75 72 ow[iIdx] || pCur
15ae2 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
15ae3 5d 3d 3d 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 ]==nextPage);.
15ae4 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 pCur->aOve
15ae5 72 66 6c 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65 rflow[iIdx] = ne
15ae6 78 74 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a xtPage;. }.
15ae7 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 69 66 #endif.. if
15ae8 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69 ( offset>=ovflSi
15ae9 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ze ){. /*
15aea 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e The only reason
15aeb 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 to read this pa
15aec 67 65 20 69 73 20 74 6f 20 6f 62 74 61 69 6e 20 ge is to obtain
15aed 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 the page.
15aee 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 ** number for t
15aef 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
15af0 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 the overflow cha
15af1 69 6e 2e 20 54 68 65 20 70 61 67 65 0a 20 20 20 in. The page.
15af2 20 20 20 20 20 2a 2a 20 64 61 74 61 20 69 73 20 ** data is
15af3 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 20 53 6f not required. So
15af4 20 66 69 72 73 74 20 74 72 79 20 74 6f 20 6c 6f first try to lo
15af5 6f 6b 75 70 20 74 68 65 20 6f 76 65 72 66 6c 6f okup the overflo
15af6 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 w. ** pag
15af7 65 2d 6c 69 73 74 20 63 61 63 68 65 2c 20 69 66 e-list cache, if
15af8 20 61 6e 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20 any, then fall
15af9 62 61 63 6b 20 74 6f 20 74 68 65 20 67 65 74 4f back to the getO
15afa 76 65 72 66 6c 6f 77 50 61 67 65 28 29 0a 20 20 verflowPage().
15afb 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f ** functio
15afc 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 n.. */.#i
15afd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15afe 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 T_INCRBLOB.
15aff 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 if( pCur->aOv
15b00 65 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e erflow && pCur->
15b01 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 aOverflow[iIdx+1
15b02 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e ] ){. n
15b03 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e extPage = pCur->
15b04 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 aOverflow[iIdx+1
15b05 5d 3b 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73 ];. } els
15b06 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 e .#endif.
15b07 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 rc = getOver
15b08 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6e 65 flowPage(pBt, ne
15b09 78 74 50 61 67 65 2c 20 30 2c 20 26 6e 65 78 74 xtPage, 0, &next
15b0a 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 6f Page);. o
15b0b 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a ffset -= ovflSiz
15b0c 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a e;. }else{.
15b0d 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 /* Need
15b0e 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 67 to read this pag
15b0f 65 20 70 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 e properly. It c
15b10 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 ontains some of
15b11 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 the. ** r
15b12 61 6e 67 65 20 6f 66 20 64 61 74 61 20 74 68 61 ange of data tha
15b13 74 20 69 73 20 62 65 69 6e 67 20 72 65 61 64 20 t is being read
15b14 28 65 4f 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 (eOp==0) or writ
15b15 74 65 6e 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 ten (eOp!=0)..
15b16 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
15b17 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 DbPage *pDbPage
15b18 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 20 ;. int a
15b19 3d 20 61 6d 74 3b 0a 20 20 20 20 20 20 20 20 72 = amt;. r
15b1a 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
15b1b 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c Get(pBt->pPager,
15b1c 20 6e 65 78 74 50 61 67 65 2c 20 26 70 44 62 50 nextPage, &pDbP
15b1d 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 age);. if
15b1e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15b1f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 50 61 ){. aPa
15b20 79 6c 6f 61 64 20 3d 20 73 71 6c 69 74 65 33 50 yload = sqlite3P
15b21 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 agerGetData(pDbP
15b22 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
15b23 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 34 62 nextPage = get4b
15b24 79 74 65 28 61 50 61 79 6c 6f 61 64 29 3b 0a 20 yte(aPayload);.
15b25 20 20 20 20 20 20 20 20 20 69 66 28 20 61 20 2b if( a +
15b26 20 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 offset > ovflSi
15b27 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ze ){.
15b28 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d a = ovflSize -
15b29 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 offset;.
15b2a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 }. r
15b2b 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 c = copyPayload(
15b2c 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 &aPayload[offset
15b2d 2b 34 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f +4], pBuf, a, eO
15b2e 70 2c 20 70 44 62 50 61 67 65 29 3b 0a 20 20 20 p, pDbPage);.
15b2f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
15b30 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 gerUnref(pDbPage
15b31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 66 66 );. off
15b32 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 set = 0;.
15b33 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 amt -= a;.
15b34 20 20 20 20 20 20 20 70 42 75 66 20 2b 3d 20 61 pBuf += a
15b35 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
15b36 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
15b37 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
15b38 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 OK && amt>0 ){.
15b39 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15b3a 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
15b3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
15b3c 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61 }../*.** Read pa
15b3d 72 74 20 6f 66 20 74 68 65 20 6b 65 79 20 61 73 rt of the key as
15b3e 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 75 sociated with cu
15b3f 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78 61 63 rsor pCur. Exac
15b40 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 tly.** "amt" byt
15b41 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 es will be trans
15b42 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75 66 5b fered into pBuf[
15b43 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 65 72 ]. The transfer
15b44 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 22 6f .** begins at "o
15b45 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 ffset"..**.** Re
15b46 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
15b47 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 n success or an
15b48 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e error code if an
15b49 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 ything goes.** w
15b4a 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 rong. An error
15b4b 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 22 is returned if "
15b4c 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c offset+amt" is l
15b4d 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 arger than.** th
15b4e 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c e available payl
15b4f 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 oad..*/.SQLITE_P
15b50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15b51 65 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 e3BtreeKey(BtCur
15b52 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f sor *pCur, u32 o
15b53 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 ffset, u32 amt,
15b54 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 void *pBuf){. i
15b55 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
15b56 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
15b57 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
15b58 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
15b59 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
15b5a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
15b5b 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
15b5c 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
15b5d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
15b5e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
15b5f 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 r->iPage>=0 && p
15b60 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
15b61 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 ->iPage] );.
15b62 69 66 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 if( pCur->apPage
15b63 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 [0]->intKey ){.
15b64 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
15b65 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
15b66 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
15b67 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 t( pCur->aiIdx[p
15b68 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 Cur->iPage]<pCur
15b69 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
15b6a 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a Page]->nCell );.
15b6b 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 50 rc = accessP
15b6c 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 ayload(pCur, off
15b6d 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 set, amt, (unsig
15b6e 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 ned char*)pBuf,
15b6f 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 0, 0);. }. ret
15b70 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
15b71 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 68 Read part of th
15b72 65 20 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 e data associate
15b73 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 d with cursor pC
15b74 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 ur. Exactly.**
15b75 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c "amt" bytes will
15b76 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 be transfered i
15b77 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 nto pBuf[]. The
15b78 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 transfer.** beg
15b79 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e ins at "offset".
15b7a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
15b7b 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
15b7c 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 ss or an error c
15b7d 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ode if anything
15b7e 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 goes.** wrong.
15b7f 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 An error is retu
15b80 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b rned if "offset+
15b81 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 amt" is larger t
15b82 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c han.** the avail
15b83 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f able payload..*/
15b84 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15b85 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
15b86 44 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70 Data(BtCursor *p
15b87 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c Cur, u32 offset,
15b88 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a u32 amt, void *
15b89 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b pBuf){. int rc;
15b8a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
15b8b 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
15b8c 20 69 66 20 28 20 70 43 75 72 2d 3e 65 53 74 61 if ( pCur->eSta
15b8d 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c te==CURSOR_INVAL
15b8e 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ID ){. return
15b8f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 SQLITE_ABORT;.
15b90 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 }.#endif.. ass
15b91 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
15b92 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
15b93 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 rc = restoreCur
15b94 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 sorPosition(pCur
15b95 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
15b96 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 ITE_OK ){. as
15b97 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
15b98 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
15b99 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15b9a 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 pCur->iPage>=0 &
15b9b 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 & pCur->apPage[p
15b9c 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 Cur->iPage] );.
15b9d 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
15b9e 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
15b9f 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 ge]<pCur->apPage
15ba0 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
15ba1 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d Cell );. rc =
15ba2 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 accessPayload(p
15ba3 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 Cur, offset, amt
15ba4 2c 20 70 42 75 66 2c 20 31 2c 20 30 29 3b 0a 20 , pBuf, 1, 0);.
15ba5 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
15ba6 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
15ba7 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 a pointer to pay
15ba8 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e load information
15ba9 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 from the entry
15baa 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 70 43 75 that the .** pCu
15bab 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e r cursor is poin
15bac 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 70 6f ting to. The po
15bad 69 6e 74 65 72 20 69 73 20 74 6f 20 74 68 65 20 inter is to the
15bae 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 beginning of.**
15baf 74 68 65 20 6b 65 79 20 69 66 20 73 6b 69 70 4b the key if skipK
15bb0 65 79 3d 3d 30 20 61 6e 64 20 69 74 20 70 6f 69 ey==0 and it poi
15bb1 6e 74 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e nts to the begin
15bb2 6e 69 6e 67 20 6f 66 20 64 61 74 61 20 69 66 0a ning of data if.
15bb3 2a 2a 20 73 6b 69 70 4b 65 79 3d 3d 31 2e 20 20 ** skipKey==1.
15bb4 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 The number of by
15bb5 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 tes of available
15bb6 20 6b 65 79 2f 64 61 74 61 20 69 73 20 77 72 69 key/data is wri
15bb7 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 41 tten.** into *pA
15bb8 6d 74 2e 20 20 49 66 20 2a 70 41 6d 74 3d 3d 30 mt. If *pAmt==0
15bb9 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 , then the value
15bba 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e returned will n
15bbb 6f 74 20 62 65 0a 2a 2a 20 61 20 76 61 6c 69 64 ot be.** a valid
15bbc 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
15bbd 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
15bbe 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e an optimization.
15bbf 20 20 49 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 66 It is common f
15bc0 6f 72 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 or the entire ke
15bc1 79 0a 2a 2a 20 61 6e 64 20 64 61 74 61 20 74 6f y.** and data to
15bc2 20 66 69 74 20 6f 6e 20 74 68 65 20 6c 6f 63 61 fit on the loca
15bc3 6c 20 70 61 67 65 20 61 6e 64 20 66 6f 72 20 74 l page and for t
15bc4 68 65 72 65 20 74 6f 20 62 65 20 6e 6f 20 6f 76 here to be no ov
15bc5 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 73 2e erflow.** pages.
15bc6 20 20 57 68 65 6e 20 74 68 61 74 20 69 73 20 73 When that is s
15bc7 6f 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 o, this routine
15bc8 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 61 can be used to a
15bc9 63 63 65 73 73 20 74 68 65 0a 2a 2a 20 6b 65 79 ccess the.** key
15bca 20 61 6e 64 20 64 61 74 61 20 77 69 74 68 6f 75 and data withou
15bcb 74 20 6d 61 6b 69 6e 67 20 61 20 63 6f 70 79 2e t making a copy.
15bcc 20 20 49 66 20 74 68 65 20 6b 65 79 20 61 6e 64 If the key and
15bcd 2f 6f 72 20 64 61 74 61 20 73 70 69 6c 6c 73 0a /or data spills.
15bce 2a 2a 20 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 ** onto overflow
15bcf 20 70 61 67 65 73 2c 20 74 68 65 6e 20 61 63 63 pages, then acc
15bd0 65 73 73 50 61 79 6c 6f 61 64 28 29 20 6d 75 73 essPayload() mus
15bd1 74 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 t be used to rea
15bd2 73 73 65 6d 62 6c 65 0a 2a 2a 20 74 68 65 20 6b ssemble.** the k
15bd3 65 79 2f 64 61 74 61 20 61 6e 64 20 63 6f 70 79 ey/data and copy
15bd4 20 69 74 20 69 6e 74 6f 20 61 20 70 72 65 61 6c it into a preal
15bd5 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e 0a located buffer..
15bd6 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 **.** The pointe
15bd7 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 r returned by th
15bd8 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 is routine looks
15bd9 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 74 directly into t
15bda 68 65 20 63 61 63 68 65 64 0a 2a 2a 20 70 61 67 he cached.** pag
15bdb 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
15bdc 65 2e 20 20 54 68 65 20 64 61 74 61 20 6d 69 67 e. The data mig
15bdd 68 74 20 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76 ht change or mov
15bde 65 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 0a e the next time.
15bdf 2a 2a 20 61 6e 79 20 62 74 72 65 65 20 72 6f 75 ** any btree rou
15be0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a tine is called..
15be1 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
15be2 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 66 unsigned char *f
15be3 65 74 63 68 50 61 79 6c 6f 61 64 28 0a 20 20 42 etchPayload(. B
15be4 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 tCursor *pCur,
15be5 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f /* Cursor po
15be6 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 inting to entry
15be7 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a to read from */.
15be8 20 20 69 6e 74 20 2a 70 41 6d 74 2c 20 20 20 20 int *pAmt,
15be9 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
15bea 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 the number of av
15beb 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 68 65 ailable bytes he
15bec 72 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 re */. int skip
15bed 4b 65 79 20 20 20 20 20 20 20 20 20 20 2f 2a 20 Key /*
15bee 72 65 61 64 20 62 65 67 69 6e 6e 69 6e 67 20 61 read beginning a
15bef 74 20 64 61 74 61 20 69 66 20 74 68 69 73 20 69 t data if this i
15bf0 73 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 75 s true */.){. u
15bf1 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 50 nsigned char *aP
15bf2 61 79 6c 6f 61 64 3b 0a 20 20 4d 65 6d 50 61 67 ayload;. MemPag
15bf3 65 20 2a 70 50 61 67 65 3b 0a 20 20 75 33 32 20 e *pPage;. u32
15bf4 6e 4b 65 79 3b 0a 20 20 75 33 32 20 6e 4c 6f 63 nKey;. u32 nLoc
15bf5 61 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 al;.. assert( p
15bf6 43 75 72 21 3d 30 20 26 26 20 70 43 75 72 2d 3e Cur!=0 && pCur->
15bf7 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 iPage>=0 && pCur
15bf8 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
15bf9 50 61 67 65 5d 29 3b 0a 20 20 61 73 73 65 72 74 Page]);. assert
15bfa 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
15bfb 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
15bfc 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
15bfd 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
15bfe 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 );. pPage = pC
15bff 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
15c00 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 >iPage];. asser
15c01 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 t( pCur->aiIdx[p
15c02 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 Cur->iPage]<pPag
15c03 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 67 65 e->nCell );. ge
15c04 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b tCellInfo(pCur);
15c05 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 . aPayload = pC
15c06 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 3b 0a ur->info.pCell;.
15c07 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 70 43 aPayload += pC
15c08 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 ur->info.nHeader
15c09 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 ;. if( pPage->i
15c0a 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 6e 4b 65 ntKey ){. nKe
15c0b 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a y = 0;. }else{.
15c0c 20 20 20 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 nKey = (int)
15c0d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b pCur->info.nKey;
15c0e 0a 20 20 7d 0a 20 20 69 66 28 20 73 6b 69 70 4b . }. if( skipK
15c0f 65 79 20 29 7b 0a 20 20 20 20 61 50 61 79 6c 6f ey ){. aPaylo
15c10 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 ad += nKey;.
15c11 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 nLocal = pCur->i
15c12 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6e 4b 65 nfo.nLocal - nKe
15c13 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 y;. }else{.
15c14 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 nLocal = pCur->i
15c15 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 nfo.nLocal;.
15c16 69 66 28 20 6e 4c 6f 63 61 6c 3e 6e 4b 65 79 20 if( nLocal>nKey
15c17 29 7b 0a 20 20 20 20 20 20 6e 4c 6f 63 61 6c 20 ){. nLocal
15c18 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 7d 0a 20 20 = nKey;. }.
15c19 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 }. *pAmt = nLoc
15c1a 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61 al;. return aPa
15c1b 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a yload;.}.../*.**
15c1c 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 For the entry t
15c1d 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20 hat cursor pCur
15c1e 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 is point to, ret
15c1f 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 urn as.** many b
15c20 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 ytes of the key
15c21 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61 or data as are a
15c22 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 vailable on the
15c23 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 local.** b-tree
15c24 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 page. Write the
15c25 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c number of avail
15c26 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 able bytes into
15c27 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 *pAmt..**.** The
15c28 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 pointer returne
15c29 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 d is ephemeral.
15c2a 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 The key/data ma
15c2b 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 y move.** or be
15c2c 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 destroyed on the
15c2d 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e next call to an
15c2e 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c y Btree routine,
15c2f 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 .** including ca
15c30 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 lls from other t
15c31 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74 hreads against t
15c32 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a he same cache..*
15c33 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 * Hence, a mutex
15c34 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 on the BtShared
15c35 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 should be held
15c36 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 prior to calling
15c37 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
15c38 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
15c39 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74 utines is used t
15c3a 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65 o get quick acce
15c3b 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 ss to key and da
15c3c 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d ta.** in the com
15c3d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e mon case where n
15c3e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
15c3f 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 are used..*/.SQ
15c40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
15c41 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
15c42 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 BtreeKeyFetch(Bt
15c43 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
15c44 74 20 2a 70 41 6d 74 29 7b 0a 20 20 61 73 73 65 t *pAmt){. asse
15c45 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
15c46 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
15c47 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
15c48 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
15c49 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 63 6f {. return (co
15c4a 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 nst void*)fetchP
15c4b 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d ayload(pCur, pAm
15c4c 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 t, 0);. }. ret
15c4d 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f urn 0;.}.SQLITE_
15c4e 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f PRIVATE const vo
15c4f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 id *sqlite3Btree
15c50 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73 DataFetch(BtCurs
15c51 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
15c52 41 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Amt){. assert(
15c53 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
15c54 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 (pCur) );. if(
15c55 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
15c56 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 RSOR_VALID ){.
15c57 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 return (const
15c58 76 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f void*)fetchPaylo
15c59 61 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 31 ad(pCur, pAmt, 1
15c5a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
15c5b 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 0;.}.../*.** Mov
15c5c 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 e the cursor dow
15c5d 6e 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 n to a new child
15c5e 20 70 61 67 65 2e 20 20 54 68 65 20 6e 65 77 50 page. The newP
15c5f 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 69 73 20 gno argument is
15c60 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 the.** page numb
15c61 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 er of the child
15c62 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e page to move to.
15c63 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
15c64 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75 72 oveToChild(BtCur
15c65 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6e sor *pCur, u32 n
15c66 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 ewPgno){. int r
15c67 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43 75 c;. int i = pCu
15c68 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50 r->iPage;. MemP
15c69 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20 age *pNewPage;.
15c6a 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15c6b 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61 pCur->pBt;.. a
15c6c 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
15c6d 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
15c6e 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
15c6f 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15c70 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
15c71 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 42 t( pCur->iPage<B
15c72 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 TCURSOR_MAX_DEPT
15c73 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d H );. if( pCur-
15c74 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 53 4f >iPage>=(BTCURSO
15c75 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 20 29 R_MAX_DEPTH-1) )
15c76 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
15c77 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15c78 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 ;. }. rc = get
15c79 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c AndInitPage(pBt,
15c7a 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50 newPgno, &pNewP
15c7b 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 age);. if( rc )
15c7c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 43 return rc;. pC
15c7d 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 5d 20 ur->apPage[i+1]
15c7e 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 70 43 = pNewPage;. pC
15c7f 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d 20 3d ur->aiIdx[i+1] =
15c80 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 0;. pCur->iPag
15c81 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e e++;.. pCur->in
15c82 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
15c83 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
15c84 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 77 50 = 0;. if( pNewP
15c85 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 29 7b 0a age->nCell<1 ){.
15c86 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
15c87 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
15c88 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
15c89 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 ITE_OK;.}..#ifnd
15c8a 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ef NDEBUG./*.**
15c8b 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 20 Page pParent is
15c8c 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e an internal (non
15c8d 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 65 -leaf) tree page
15c8e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
15c8f 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 74 .** asserts that
15c90 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 page number iCh
15c91 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d ild is the left-
15c92 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 64 child if the iId
15c93 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 x'th.** cell in
15c94 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 page pParent. Or
15c95 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 71 75 , if iIdx is equ
15c96 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 al to the total
15c97 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c number of.** cel
15c98 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 ls in pParent, t
15c99 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 hat page number
15c9a 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 69 iChild is the ri
15c9b 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 ght-child of.**
15c9c 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 the page..*/.sta
15c9d 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50 tic void assertP
15c9e 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 arentIndex(MemPa
15c9f 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 ge *pParent, int
15ca0 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 iIdx, Pgno iChi
15ca1 6c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 ld){. assert( i
15ca2 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 Idx<=pParent->nC
15ca3 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64 ell );. if( iId
15ca4 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c x==pParent->nCel
15ca5 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 l ){. assert(
15ca6 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 get4byte(&pPare
15ca7 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e nt->aData[pParen
15ca8 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 t->hdrOffset+8])
15ca9 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 ==iChild );. }e
15caa 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
15cab 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 get4byte(findCe
15cac 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 ll(pParent, iIdx
15cad 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 ))==iChild );.
15cae 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 }.}.#else.# def
15caf 69 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e 74 ine assertParent
15cb0 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 Index(x,y,z) .#e
15cb1 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 ndif../*.** Move
15cb2 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 the cursor up t
15cb3 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 o the parent pag
15cb4 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 e..**.** pCur->i
15cb5 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 dx is set to the
15cb6 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 cell index that
15cb7 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f contains the po
15cb8 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 inter.** to the
15cb9 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69 page we are comi
15cba 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 ng from. If we
15cbb 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 are coming from
15cbc 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 the.** right-mos
15cbd 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 65 t child page the
15cbe 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 n pCur->idx is s
15cbf 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 et to one more t
15cc0 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 han.** the large
15cc1 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a st cell index..*
15cc2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
15cc3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
15cc4 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 eeMoveToParent(B
15cc5 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
15cc6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
15cc7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
15cc8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
15cc9 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
15cca 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 OR_VALID );. as
15ccb 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 sert( pCur->iPag
15ccc 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 e>0 );. assert(
15ccd 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
15cce 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 ur->iPage] );.
15ccf 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 assertParentInde
15cd0 78 28 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 x(. pCur->apP
15cd1 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d age[pCur->iPage-
15cd2 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 1], . pCur->a
15cd3 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
15cd4 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e -1], . pCur->
15cd5 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
15cd6 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 ge]->pgno. );.
15cd7 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 releasePage(pCu
15cd8 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
15cd9 69 50 61 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d iPage]);. pCur-
15cda 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 >iPage--;. pCur
15cdb 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
15cdc 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e ;. pCur->validN
15cdd 4b 65 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Key = 0;.}../*.*
15cde 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
15cdf 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 r to the root pa
15ce0 67 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 ge.*/.static int
15ce1 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 42 74 43 75 moveToRoot(BtCu
15ce2 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d rsor *pCur){. M
15ce3 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 emPage *pRoot;.
15ce4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
15ce5 5f 4f 4b 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 _OK;. Btree *p
15ce6 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a = pCur->pBtree;.
15ce7 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
15ce8 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 = p->pBt;.. ass
15ce9 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
15cea 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
15ceb 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f assert( CURSOR_
15cec 49 4e 56 41 4c 49 44 20 3c 20 43 55 52 53 4f 52 INVALID < CURSOR
15ced 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a _REQUIRESEEK );.
15cee 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 assert( CURSOR
15cef 5f 56 41 4c 49 44 20 20 20 3c 20 43 55 52 53 4f _VALID < CURSO
15cf0 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b R_REQUIRESEEK );
15cf1 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f . assert( CURSO
15cf2 52 5f 46 41 55 4c 54 20 20 20 3e 20 43 55 52 53 R_FAULT > CURS
15cf3 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
15cf4 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
15cf5 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 tate>=CURSOR_REQ
15cf6 55 49 52 45 53 45 45 4b 20 29 7b 0a 20 20 20 20 UIRESEEK ){.
15cf7 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
15cf8 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 ==CURSOR_FAULT )
15cf9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 {. return p
15cfa 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 20 20 7d Cur->skip;. }
15cfb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
15cfc 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75 eClearCursor(pCu
15cfd 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 r);. }.. if( p
15cfe 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 29 7b Cur->iPage>=0 ){
15cff 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
15d00 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 43 75 72 for(i=1; i<=pCur
15d01 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 ->iPage; i++){.
15d02 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
15d03 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d (pCur->apPage[i]
15d04 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
15d05 7b 0a 20 20 20 20 69 66 28 20 0a 20 20 20 20 20 {. if( .
15d06 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
15d07 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 = getAndInitPage
15d08 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f (pBt, pCur->pgno
15d09 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 Root, &pCur->apP
15d0a 61 67 65 5b 30 5d 29 29 0a 20 20 20 20 29 7b 0a age[0])). ){.
15d0b 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 pCur->eSta
15d0c 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
15d0d 4c 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 LID;. retur
15d0e 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n rc;. }. }.
15d0f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75 72 2d . pRoot = pCur-
15d10 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 61 73 >apPage[0];. as
15d11 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e sert( pRoot->pgn
15d12 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f o==pCur->pgnoRoo
15d13 74 20 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 t );. pCur->iPa
15d14 67 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ge = 0;. pCur->
15d15 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 aiIdx[0] = 0;.
15d16 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
15d17 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 74 = 0;. pCur->at
15d18 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 75 72 Last = 0;. pCur
15d19 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b ->validNKey = 0;
15d1a 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e .. if( pRoot->n
15d1b 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 6f 6f Cell==0 && !pRoo
15d1c 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 t->leaf ){. P
15d1d 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 20 20 gno subpage;.
15d1e 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e assert( pRoot->
15d1f 70 67 6e 6f 3d 3d 31 20 29 3b 0a 20 20 20 20 73 pgno==1 );. s
15d20 75 62 70 61 67 65 20 3d 20 67 65 74 34 62 79 74 ubpage = get4byt
15d21 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b e(&pRoot->aData[
15d22 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 pRoot->hdrOffset
15d23 2b 38 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 +8]);. assert
15d24 28 20 73 75 62 70 61 67 65 3e 30 20 29 3b 0a 20 ( subpage>0 );.
15d25 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
15d26 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a = CURSOR_VALID;.
15d27 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
15d28 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70 61 hild(pCur, subpa
15d29 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ge);. }else{.
15d2a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
15d2b 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e ((pRoot->nCell>
15d2c 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0)?CURSOR_VALID:
15d2d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b CURSOR_INVALID);
15d2e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
15d2f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 ;.}../*.** Move
15d30 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 the cursor down
15d31 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 to the left-most
15d32 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 leaf entry bene
15d33 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 ath the.** entry
15d34 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 to which it is
15d35 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
15d36 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 ng..**.** The le
15d37 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 ft-most leaf is
15d38 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 the one with the
15d39 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 smallest key -
15d3a 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 the first.** in
15d3b 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e ascending order.
15d3c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
15d3d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 oveToLeftmost(Bt
15d3e 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
15d3f 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e Pgno pgno;. in
15d40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
15d41 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
15d42 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 ge;.. assert( c
15d43 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
15d44 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
15d45 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
15d46 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
15d47 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 . while( rc==SQ
15d48 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 LITE_OK && !(pPa
15d49 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
15d4a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d e[pCur->iPage])-
15d4b 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73 73 >leaf ){. ass
15d4c 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
15d4d 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 [pCur->iPage]<pP
15d4e 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
15d4f 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 pgno = get4byt
15d50 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 e(findCell(pPage
15d51 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 , pCur->aiIdx[pC
15d52 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 ur->iPage]));.
15d53 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 rc = moveToChi
15d54 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a ld(pCur, pgno);.
15d55 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
15d56 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 .}../*.** Move t
15d57 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 he cursor down t
15d58 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 o the right-most
15d59 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 leaf entry bene
15d5a 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 ath the.** page
15d5b 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63 to which it is c
15d5c 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
15d5d 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20 64 g. Notice the d
15d5e 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 ifference.** bet
15d5f 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d ween moveToLeftm
15d60 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f ost() and moveTo
15d61 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f Rightmost(). mo
15d62 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a veToLeftmost().*
15d63 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74 * finds the left
15d64 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 -most entry bene
15d65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 ath the *entry*
15d66 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 whereas moveToRi
15d67 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e ghtmost().** fin
15d68 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 ds the right-mos
15d69 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 t entry beneath
15d6a 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a the *page*..**.*
15d6b 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 * The right-most
15d6c 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f 6e entry is the on
15d6d 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 e with the large
15d6e 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 st key - the las
15d6f 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 t.** key in asce
15d70 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a nding order..*/.
15d71 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 static int moveT
15d72 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75 72 oRightmost(BtCur
15d73 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 sor *pCur){. Pg
15d74 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 no pgno;. int r
15d75 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
15d76 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
15d77 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
15d78 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
15d79 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
15d7a 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
15d7b 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
15d7c 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 ;. while( rc==S
15d7d 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 QLITE_OK && !(pP
15d7e 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
15d7f 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
15d80 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 ->leaf ){. pg
15d81 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
15d82 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
15d83 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
15d84 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 ;. pCur->aiId
15d85 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d x[pCur->iPage] =
15d86 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 pPage->nCell;.
15d87 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
15d88 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b ild(pCur, pgno);
15d89 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
15d8a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
15d8b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
15d8c 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 ->iPage] = pPage
15d8d 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 ->nCell-1;. p
15d8e 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
15d8f 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 = 0;. pCur->v
15d90 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 alidNKey = 0;.
15d91 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
15d92 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 ../* Move the cu
15d93 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73 rsor to the firs
15d94 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 t entry in the t
15d95 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 able. Return SQ
15d96 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 LITE_OK.** on su
15d97 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 ccess. Set *pRe
15d98 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 s to 0 if the cu
15d99 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f rsor actually po
15d9a 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e ints to somethin
15d9b 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 g.** or set *pRe
15d9c 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 s to 1 if the ta
15d9d 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f ble is empty..*/
15d9e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15d9f 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
15da0 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a First(BtCursor *
15da1 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 pCur, int *pRes)
15da2 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 {. int rc;.. a
15da3 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
15da4 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
15da5 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15da6 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
15da7 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e ur->pBtree->db->
15da8 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
15da9 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 moveToRoot(pCur
15daa 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
15dab 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
15dac 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
15dad 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 CURSOR_INVALID )
15dae 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
15daf 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
15db0 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
15db1 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 ==0 );. *pR
15db2 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 es = 1;. rc
15db3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
15db4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
15db5 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
15db6 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
15db7 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 ->nCell>0 );.
15db8 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
15db9 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c rc = moveToL
15dba 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 eftmost(pCur);.
15dbb 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
15dbc 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 n rc;.}../* Move
15dbd 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 the cursor to t
15dbe 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
15dbf 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 the table. Ret
15dc0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a urn SQLITE_OK.**
15dc1 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 on success. Se
15dc2 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 t *pRes to 0 if
15dc3 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 the cursor actua
15dc4 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f lly points to so
15dc5 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 mething.** or se
15dc6 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 t *pRes to 1 if
15dc7 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 the table is emp
15dc8 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ty..*/.SQLITE_PR
15dc9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15dca 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 3BtreeLast(BtCur
15dcb 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
15dcc 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b pRes){. int rc;
15dcd 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 . . assert( cur
15dce 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
15dcf 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
15dd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15dd1 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 eld(pCur->pBtree
15dd2 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
15dd3 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f rc = moveToRoo
15dd4 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 t(pCur);. if( r
15dd5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
15dd6 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 if( CURSOR_I
15dd7 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
15dd8 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 tate ){. as
15dd9 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 sert( pCur->apPa
15dda 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
15ddb 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 >nCell==0 );.
15ddc 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 *pRes = 1;.
15ddd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
15dde 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
15ddf 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
15de0 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 D );. *pRes
15de1 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d = 0;. rc =
15de2 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
15de3 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 67 65 (pCur);. ge
15de4 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b tCellInfo(pCur);
15de5 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 74 4c . pCur->atL
15de6 61 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49 54 45 ast = rc==SQLITE
15de7 5f 4f 4b 20 3f 31 3a 30 3b 0a 20 20 20 20 7d 0a _OK ?1:0;. }.
15de8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
15de9 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 .}../* Move the
15dea 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69 cursor so that i
15deb 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 t points to an e
15dec 6e 74 72 79 20 6e 65 61 72 20 74 68 65 20 6b 65 ntry near the ke
15ded 79 20 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 y .** specified
15dee 62 79 20 70 49 64 78 4b 65 79 20 6f 72 20 69 6e by pIdxKey or in
15def 74 4b 65 79 2e 20 20 20 52 65 74 75 72 6e 20 61 tKey. Return a
15df0 20 73 75 63 63 65 73 73 20 63 6f 64 65 2e 0a 2a success code..*
15df1 2a 0a 2a 2a 20 46 6f 72 20 49 4e 54 4b 45 59 20 *.** For INTKEY
15df2 74 61 62 6c 65 73 2c 20 74 68 65 20 69 6e 74 4b tables, the intK
15df3 65 79 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 ey parameter is
15df4 75 73 65 64 2e 20 20 70 49 64 78 4b 65 79 20 0a used. pIdxKey .
15df5 2a 2a 20 6d 75 73 74 20 62 65 20 4e 55 4c 4c 2e ** must be NULL.
15df6 20 20 46 6f 72 20 69 6e 64 65 78 20 74 61 62 6c For index tabl
15df7 65 73 2c 20 70 49 64 78 4b 65 79 20 69 73 20 75 es, pIdxKey is u
15df8 73 65 64 20 61 6e 64 20 69 6e 74 4b 65 79 0a 2a sed and intKey.*
15df9 2a 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a * is ignored..**
15dfa 0a 2a 2a 20 49 66 20 61 6e 20 65 78 61 63 74 20 .** If an exact
15dfb 6d 61 74 63 68 20 69 73 20 6e 6f 74 20 66 6f 75 match is not fou
15dfc 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 nd, then the cur
15dfd 73 6f 72 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a sor is always.**
15dfe 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
15dff 74 20 61 20 6c 65 61 66 20 70 61 67 65 20 77 68 t a leaf page wh
15e00 69 63 68 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74 ich would hold t
15e01 68 65 20 65 6e 74 72 79 20 69 66 20 69 74 0a 2a he entry if it.*
15e02 2a 20 77 65 72 65 20 70 72 65 73 65 6e 74 2e 20 * were present.
15e03 20 54 68 65 20 63 75 72 73 6f 72 20 6d 69 67 68 The cursor migh
15e04 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e t point to an en
15e05 74 72 79 20 74 68 61 74 20 63 6f 6d 65 73 0a 2a try that comes.*
15e06 2a 20 62 65 66 6f 72 65 20 6f 72 20 61 66 74 65 * before or afte
15e07 72 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a r the key..**.**
15e08 20 41 6e 20 69 6e 74 65 67 65 72 20 69 73 20 77 An integer is w
15e09 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 52 65 ritten into *pRe
15e0a 73 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 s which is the r
15e0b 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 63 6f 6d 70 esult of.** comp
15e0c 61 72 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69 aring the key wi
15e0d 74 68 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 th the entry to
15e0e 77 68 69 63 68 20 74 68 65 20 63 75 72 73 6f 72 which the cursor
15e0f 20 69 73 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 is .** pointing
15e10 2e 20 20 54 68 65 20 6d 65 61 6e 69 6e 67 20 6f . The meaning o
15e11 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 77 72 f the integer wr
15e12 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 2a 70 itten into.** *p
15e13 52 65 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 Res is as follow
15e14 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 s:.**.** *pR
15e15 65 73 3c 30 20 20 20 20 20 20 54 68 65 20 63 75 es<0 The cu
15e16 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 rsor is left poi
15e17 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 nting at an entr
15e18 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 y that.**
15e19 20 20 20 20 20 20 20 20 20 20 20 69 73 20 73 6d is sm
15e1a 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e 74 4b 65 aller than intKe
15e1b 79 2f 70 49 64 78 4b 65 79 20 6f 72 20 69 66 20 y/pIdxKey or if
15e1c 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 the table is emp
15e1d 74 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ty.**
15e1e 20 20 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 and the c
15e1f 75 72 73 6f 72 20 69 73 20 74 68 65 72 65 66 6f ursor is therefo
15e20 72 65 20 6c 65 66 74 20 70 6f 69 6e 74 20 74 6f re left point to
15e21 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 nothing..**.**
15e22 20 20 20 20 2a 70 52 65 73 3d 3d 30 20 20 20 20 *pRes==0
15e23 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c The cursor is l
15e24 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 eft pointing at
15e25 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a an entry that.**
15e26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15e27 20 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 exactly matche
15e28 73 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 s intKey/pIdxKey
15e29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 ..**.** *pRe
15e2a 73 3e 30 20 20 20 20 20 20 54 68 65 20 63 75 72 s>0 The cur
15e2b 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e sor is left poin
15e2c 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 ting at an entry
15e2d 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 that.**
15e2e 20 20 20 20 20 20 20 20 20 20 69 73 20 6c 61 72 is lar
15e2f 67 65 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f ger than intKey/
15e30 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 pIdxKey..**.*/.S
15e31 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
15e32 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f t sqlite3BtreeMo
15e33 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 vetoUnpacked(.
15e34 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
15e35 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
15e36 63 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f 76 cursor to be mov
15e37 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 ed */. Unpacked
15e38 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 2c Record *pIdxKey,
15e39 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 /* Unpacked ind
15e3a 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 36 34 20 ex key */. i64
15e3b 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 20 20 20 intKey,
15e3c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c /* The tabl
15e3d 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 62 e key */. int b
15e3e 69 61 73 52 69 67 68 74 2c 20 20 20 20 20 20 20 iasRight,
15e3f 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 /* If true,
15e40 62 69 61 73 20 74 68 65 20 73 65 61 72 63 68 20 bias the search
15e41 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20 to the high end
15e42 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20 */. int *pRes
15e43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15e44 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 65 Write search re
15e45 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b sults here */.){
15e46 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
15e47 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
15e48 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
15e49 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15e4a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
15e4b 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
15e4c 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 utex) );.. /* I
15e4d 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
15e4e 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e already position
15e4f 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20 ed at the point
15e50 77 65 20 61 72 65 20 74 72 79 69 6e 67 0a 20 20 we are trying.
15e51 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 ** to move to, t
15e52 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20 hen just return
15e53 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e without doing an
15e54 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 66 28 20 y work */. if(
15e55 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
15e56 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 20 70 43 RSOR_VALID && pC
15e57 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 0a 20 ur->validNKey .
15e58 20 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 && pCur->apPag
15e59 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 e[0]->intKey .
15e5a 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d ){. if( pCur-
15e5b 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b >info.nKey==intK
15e5c 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 ey ){. *pRe
15e5d 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 s = 0;. ret
15e5e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
15e5f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75 }. if( pCu
15e60 72 2d 3e 61 74 4c 61 73 74 20 26 26 20 70 43 75 r->atLast && pCu
15e61 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 r->info.nKey<int
15e62 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 Key ){. *pR
15e63 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 es = -1;. r
15e64 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15e65 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 . }. }.. rc
15e66 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 = moveToRoot(pC
15e67 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b ur);. if( rc ){
15e68 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
15e69 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
15e6a 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
15e6b 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 >iPage] );. ass
15e6c 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
15e6d 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
15e6e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 isInit );. if(
15e6f 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
15e70 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a RSOR_INVALID ){.
15e71 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a *pRes = -1;.
15e72 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
15e73 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
15e74 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 Page]->nCell==0
15e75 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
15e76 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 LITE_OK;. }. a
15e77 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
15e78 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c age[0]->intKey |
15e79 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a 20 20 66 | pIdxKey );. f
15e7a 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69 6e 74 20 or(;;){. int
15e7b 6c 77 72 2c 20 75 70 72 3b 0a 20 20 20 20 50 67 lwr, upr;. Pg
15e7c 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d no chldPg;. M
15e7d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
15e7e 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
15e7f 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 r->iPage];. i
15e80 6e 74 20 63 20 3d 20 2d 31 3b 20 20 2f 2a 20 70 nt c = -1; /* p
15e81 52 65 73 20 72 65 74 75 72 6e 20 69 66 20 74 61 Res return if ta
15e82 62 6c 65 20 69 73 20 65 6d 70 74 79 20 6d 75 73 ble is empty mus
15e83 74 20 62 65 20 2d 31 20 2a 2f 0a 20 20 20 20 6c t be -1 */. l
15e84 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 20 wr = 0;. upr
15e85 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 = pPage->nCell-1
15e86 3b 0a 20 20 20 20 69 66 28 20 28 21 70 50 61 67 ;. if( (!pPag
15e87 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 70 49 64 e->intKey && pId
15e88 78 4b 65 79 3d 3d 30 29 20 7c 7c 20 75 70 72 3c xKey==0) || upr<
15e89 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
15e8a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
15e8b 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 KPT;. goto
15e8c 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
15e8d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 69 61 }. if( bia
15e8e 73 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 sRight ){.
15e8f 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
15e90 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 ->iPage] = (u16)
15e91 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a upr;. }else{.
15e92 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 pCur->aiId
15e93 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d x[pCur->iPage] =
15e94 20 28 75 31 36 29 28 28 75 70 72 2b 6c 77 72 29 (u16)((upr+lwr)
15e95 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 /2);. }. f
15e96 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 76 6f or(;;){. vo
15e97 69 64 20 2a 70 43 65 6c 6c 4b 65 79 3b 0a 20 20 id *pCellKey;.
15e98 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 i64 nCellKey
15e99 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 20 ;. int idx
15e9a 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 = pCur->aiIdx[pC
15e9b 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
15e9c 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
15e9d 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43 ze = 0;. pC
15e9e 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 ur->validNKey =
15e9f 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 1;. if( pPa
15ea0 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
15ea1 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b u8 *pCell;
15ea2 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d . pCell =
15ea3 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c findCell(pPage,
15ea4 20 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e 63 idx) + pPage->c
15ea5 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 20 hildPtrSize;.
15ea6 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e if( pPage->
15ea7 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 hasData ){.
15ea8 20 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a u32 dummy;.
15ea9 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 pCell
15eaa 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 += getVarint32(p
15eab 43 65 6c 6c 2c 20 64 75 6d 6d 79 29 3b 0a 20 20 Cell, dummy);.
15eac 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
15ead 67 65 74 56 61 72 69 6e 74 28 70 43 65 6c 6c 2c getVarint(pCell,
15eae 20 28 75 36 34 2a 29 26 6e 43 65 6c 6c 4b 65 79 (u64*)&nCellKey
15eaf 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e );. if( n
15eb0 43 65 6c 6c 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 CellKey==intKey
15eb1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d ){. c =
15eb2 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 0;. }els
15eb3 65 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 e if( nCellKey<i
15eb4 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 ntKey ){.
15eb5 20 20 20 63 20 3d 20 2d 31 3b 0a 20 20 20 20 20 c = -1;.
15eb6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
15eb7 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c assert( nCel
15eb8 6c 4b 65 79 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 lKey>intKey );.
15eb9 20 20 20 20 20 20 20 20 20 63 20 3d 20 2b 31 3b c = +1;
15eba 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
15ebb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
15ebc 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 3b 0a 20 int available;.
15ebd 20 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 20 pCellKey
15ebe 3d 20 28 76 6f 69 64 20 2a 29 66 65 74 63 68 50 = (void *)fetchP
15ebf 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 26 61 76 ayload(pCur, &av
15ec0 61 69 6c 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 ailable, 0);.
15ec1 20 20 20 20 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 nCellKey =
15ec2 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b pCur->info.nKey;
15ec3 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 76 61 . if( ava
15ec4 69 6c 61 62 6c 65 3e 3d 6e 43 65 6c 6c 4b 65 79 ilable>=nCellKey
15ec5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 ){. c
15ec6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 = sqlite3VdbeRec
15ec7 6f 72 64 43 6f 6d 70 61 72 65 28 28 69 6e 74 29 ordCompare((int)
15ec8 6e 43 65 6c 6c 4b 65 79 2c 20 70 43 65 6c 6c 4b nCellKey, pCellK
15ec9 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 ey, pIdxKey);.
15eca 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
15ecb 20 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 20 pCellKey
15ecc 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
15ecd 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 20 29 (int)nCellKey )
15ece 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
15ecf 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20 pCellKey==0 ){.
15ed0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
15ed1 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15ed2 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d goto m
15ed3 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
15ed4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15ed5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
15ed6 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 BtreeKey(pCur, 0
15ed7 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c , (int)nCellKey,
15ed8 20 28 76 6f 69 64 2a 29 70 43 65 6c 6c 4b 65 79 (void*)pCellKey
15ed9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d );. c =
15eda 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f sqlite3VdbeReco
15edb 72 64 43 6f 6d 70 61 72 65 28 28 69 6e 74 29 6e rdCompare((int)n
15edc 43 65 6c 6c 4b 65 79 2c 20 70 43 65 6c 6c 4b 65 CellKey, pCellKe
15edd 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 y, pIdxKey);.
15ede 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
15edf 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 ree(pCellKey);.
15ee0 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 if( rc
15ee1 29 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 ) goto moveto_fi
15ee2 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a nish;. }.
15ee3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
15ee4 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ( c==0 ){.
15ee5 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 pCur->info.nKe
15ee6 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 y = nCellKey;.
15ee7 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d if( pPage-
15ee8 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 >intKey && !pPag
15ee9 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
15eea 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 3b 0a lwr = idx;.
15eeb 20 20 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 upr =
15eec 6c 77 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 lwr - 1;.
15eed 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
15eee 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
15eef 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
15ef0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
15ef1 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 ITE_OK;.
15ef2 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 goto moveto_fi
15ef3 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a nish;. }.
15ef4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
15ef5 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 ( c<0 ){.
15ef6 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 20 lwr = idx+1;.
15ef7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
15ef8 20 20 20 75 70 72 20 3d 20 69 64 78 2d 31 3b 0a upr = idx-1;.
15ef9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
15efa 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a 20 20 20 ( lwr>upr ){.
15efb 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e pCur->info.
15efc 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 3b nKey = nCellKey;
15efd 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
15efe 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 }. pC
15eff 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
15f00 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 iPage] = (u16)((
15f01 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20 lwr+upr)/2);.
15f02 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c }. assert( l
15f03 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20 wr==upr+1 );.
15f04 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15f05 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 isInit );. if
15f06 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b ( pPage->leaf ){
15f07 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 . chldPg =
15f08 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
15f09 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 lwr>=pPage->nCe
15f0a 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 ll ){. chld
15f0b 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 Pg = get4byte(&p
15f0c 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
15f0d 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
15f0e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
15f0f 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 chldPg = get4
15f10 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 byte(findCell(pP
15f11 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 age, lwr));.
15f12 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67 }. if( chldPg
15f13 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ==0 ){. ass
15f14 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
15f15 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 [pCur->iPage]<pC
15f16 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
15f17 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
15f18 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 65 73 ;. if( pRes
15f19 20 29 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20 20 ) *pRes = c;.
15f1a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
15f1b 4f 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d OK;. goto m
15f1c 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
15f1d 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 }. pCur->ai
15f1e 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
15f1f 20 3d 20 28 75 31 36 29 6c 77 72 3b 0a 20 20 20 = (u16)lwr;.
15f20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
15f21 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d e = 0;. pCur-
15f22 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
15f23 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
15f24 68 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50 hild(pCur, chldP
15f25 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 g);. if( rc )
15f26 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
15f27 69 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f 5f ish;. }.moveto_
15f28 66 69 6e 69 73 68 3a 0a 20 20 72 65 74 75 72 6e finish:. return
15f29 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e rc;.}../*.** In
15f2a 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 this version of
15f2b 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b BtreeMoveto, pK
15f2c 65 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 ey is a packed i
15f2d 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 ndex record.** s
15f2e 75 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 uch as is genera
15f2f 74 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 ted by the OP_Ma
15f30 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e keRecord opcode.
15f31 20 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 Unpack the.**
15f32 72 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 record and then
15f33 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f call BtreeMoveto
15f34 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f Unpacked() to do
15f35 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 53 51 the work..*/.SQ
15f36 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15f37 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
15f38 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 eto(. BtCursor
15f39 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 *pCur, /* Cu
15f3a 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 rsor open on the
15f3b 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 btree to be sea
15f3c 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 rched */. const
15f3d 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f void *pKey, /
15f3e 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 * Packed key if
15f3f 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 the btree is an
15f40 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e index */. i64 n
15f41 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f Key, /
15f42 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f * Integer key fo
15f43 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 r tables. Size
15f44 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 of pKey for indi
15f45 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 ces */. int bia
15f46 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
15f47 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 Bias search to t
15f48 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 he high end */.
15f49 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 int *pRes
15f4a 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 /* Write se
15f4b 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 arch results her
15f4c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
15f4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
15f4e 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 /* Status c
15f4f 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 ode */. Unpacke
15f50 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 dRecord *pIdxKey
15f51 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 ; /* Unpacked
15f52 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 55 index key */. U
15f53 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 61 53 npackedRecord aS
15f54 70 61 63 65 5b 31 36 5d 3b 20 2f 2a 20 54 65 6d pace[16]; /* Tem
15f55 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 p space for pIdx
15f56 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 Key - to avoid a
15f57 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66 malloc */.. if
15f58 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 ( pKey ){. as
15f59 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34 sert( nKey==(i64
15f5a 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20 )(int)nKey );.
15f5b 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 pIdxKey = sqli
15f5c 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 te3VdbeRecordUnp
15f5d 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e ack(pCur->pKeyIn
15f5e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 fo, (int)nKey, p
15f5f 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Key,.
15f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15f61 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63 aSpac
15f62 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 e, sizeof(aSpace
15f63 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 ));. if( pIdx
15f64 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 Key==0 ) return
15f65 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15f66 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b }else{. pIdxK
15f67 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 ey = 0;. }. rc
15f68 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
15f69 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
15f6a 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 ur, pIdxKey, nKe
15f6b 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a y, bias, pRes);.
15f6c 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 if( pKey ){.
15f6d 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
15f6e 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
15f6f 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a d(pIdxKey);. }.
15f70 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
15f71 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
15f72 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 UE if the cursor
15f73 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 is not pointing
15f74 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 at an entry of
15f75 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a the table..**.**
15f76 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 TRUE will be re
15f77 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 turned after a c
15f78 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 all to sqlite3Bt
15f79 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a reeNext() moves.
15f7a 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 ** past the last
15f7b 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 entry in the ta
15f7c 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 ble or sqlite3Bt
15f7d 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 reePrev() moves
15f7e 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 past.** the firs
15f7f 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 t entry. TRUE i
15f80 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 s also returned
15f81 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
15f82 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 empty..*/.SQLITE
15f83 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15f84 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 ite3BtreeEof(BtC
15f85 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
15f86 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 /* TODO: What if
15f87 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 the cursor is i
15f88 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 n CURSOR_REQUIRE
15f89 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 SEEK but all tab
15f8a 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 le entries. **
15f8b 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 have been delete
15f8c 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c d? This API will
15f8d 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 need to change
15f8e 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 to return an err
15f8f 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 or code. ** as
15f90 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c well as the bool
15f91 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 ean result value
15f92 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 .. */. return
15f93 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 (CURSOR_VALID!=p
15f94 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a Cur->eState);.}.
15f95 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
15f96 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
15f97 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72 ction handle for
15f98 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51 a cursor..*/.SQ
15f99 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
15f9a 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42 74 72 ite3 *sqlite3Btr
15f9b 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e 73 74 eeCursorDb(const
15f9c 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 BtCursor *pCur)
15f9d 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
15f9e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
15f9f 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d Cur->pBtree->db-
15fa0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 >mutex) );. ret
15fa1 75 72 6e 20 70 43 75 72 2d 3e 70 42 74 72 65 65 urn pCur->pBtree
15fa2 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ->db;.}../*.** A
15fa3 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f dvance the curso
15fa4 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e r to the next en
15fa5 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
15fa6 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 ase. If.** succ
15fa7 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 essful then set
15fa8 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 *pRes=0. If the
15fa9 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 cursor.** was a
15faa 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 lready pointing
15fab 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 to the last entr
15fac 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
15fad 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 e before.** this
15fae 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c routine was cal
15faf 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 led, then set *p
15fb0 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Res=1..*/.SQLITE
15fb1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15fb2 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74 ite3BtreeNext(Bt
15fb3 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
15fb4 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 t *pRes){. int
15fb5 72 63 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 rc;. int idx;.
15fb6 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
15fb7 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
15fb8 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
15fb9 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 r) );. rc = res
15fba 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
15fbb 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 on(pCur);. if(
15fbc 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
15fbd 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
15fbe 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 52 }. assert( pR
15fbf 65 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 43 es!=0 );. if( C
15fc0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 URSOR_INVALID==p
15fc1 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 Cur->eState ){.
15fc2 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 *pRes = 1;.
15fc3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15fc4 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 OK;. }. if( pC
15fc5 75 72 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a 20 20 ur->skip>0 ){.
15fc6 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 pCur->skip = 0
15fc7 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b ;. *pRes = 0;
15fc8 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
15fc9 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 TE_OK;. }. pCu
15fca 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a 20 20 r->skip = 0;..
15fcb 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
15fcc 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
15fcd 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 ];. idx = ++pCu
15fce 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
15fcf 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 Page];. assert(
15fd0 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 pPage->isInit )
15fd1 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c ;. assert( idx<
15fd2 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b =pPage->nCell );
15fd3 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e .. pCur->info.n
15fd4 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 Size = 0;. pCur
15fd5 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b ->validNKey = 0;
15fd6 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67 . if( idx>=pPag
15fd7 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 e->nCell ){.
15fd8 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
15fd9 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d ){. rc = m
15fda 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
15fdb 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
15fdc 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
15fdd 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 drOffset+8]));.
15fde 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
15fdf 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 turn rc;. r
15fe0 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f c = moveToLeftmo
15fe1 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 st(pCur);.
15fe2 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
15fe3 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
15fe4 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 }. do{.
15fe5 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d if( pCur->iPage=
15fe6 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 =0 ){. *p
15fe7 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 Res = 1;.
15fe8 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
15fe9 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
15fea 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
15feb 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
15fec 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 }. sqlite3B
15fed 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 treeMoveToParent
15fee 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 (pCur);. pP
15fef 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
15ff0 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
15ff1 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 . }while( pCu
15ff2 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
15ff3 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 Page]>=pPage->nC
15ff4 65 6c 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73 ell );. *pRes
15ff5 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 = 0;. if( pP
15ff6 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
15ff7 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
15ff8 33 42 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 3BtreeNext(pCur,
15ff9 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 pRes);. }els
15ffa 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 e{. rc = SQ
15ffb 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
15ffc 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
15ffd 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 }. *pRes = 0;.
15ffe 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 if( pPage->leaf
15fff 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16000 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
16001 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d rc = moveToLeftm
16002 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74 ost(pCur);. ret
16003 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
16004 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 73 6f * Step the curso
16005 72 20 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f r to the back to
16006 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e the previous en
16007 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
16008 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 ase. If.** succ
16009 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 essful then set
1600a 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 *pRes=0. If the
1600b 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 cursor.** was a
1600c 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 lready pointing
1600d 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 to the first ent
1600e 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ry in the databa
1600f 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 se before.** thi
16010 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 s routine was ca
16011 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a lled, then set *
16012 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 pRes=1..*/.SQLIT
16013 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
16014 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f lite3BtreePrevio
16015 75 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 us(BtCursor *pCu
16016 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 r, int *pRes){.
16017 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 int rc;. MemPa
16018 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 ge *pPage;.. as
16019 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
1601a 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
1601b 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 rc = restoreCu
1601c 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 rsorPosition(pCu
1601d 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 r);. if( rc!=SQ
1601e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
1601f 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
16020 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 pCur->atLast = 0
16021 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 ;. if( CURSOR_I
16022 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
16023 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 tate ){. *pRe
16024 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 s = 1;. retur
16025 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
16026 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 . if( pCur->ski
16027 70 3c 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d p<0 ){. pCur-
16028 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a >skip = 0;. *
16029 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 pRes = 0;. re
1602a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1602b 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 }. pCur->skip
1602c 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d = 0;.. pPage =
1602d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1602e 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 ur->iPage];. as
1602f 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
16030 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50 nit );. if( !pP
16031 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
16032 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d int idx = pCur-
16033 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
16034 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f ge];. rc = mo
16035 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
16036 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c get4byte(findCel
16037 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b l(pPage, idx)));
16038 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
16039 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1603a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d }. rc = m
1603b 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 oveToRightmost(p
1603c 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Cur);. }else{.
1603d 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e while( pCur->
1603e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
1603f 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 e]==0 ){. i
16040 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d f( pCur->iPage==
16041 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 0 ){. pCu
16042 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
16043 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 OR_INVALID;.
16044 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 *pRes = 1;.
16045 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
16046 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
16047 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
16048 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 reeMoveToParent(
16049 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 pCur);. }.
1604a 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
1604b 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d e = 0;. pCur-
1604c 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
1604d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 . pCur->aiIdx
1604e 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b [pCur->iPage]--;
1604f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 . pPage = pCu
16050 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16051 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 iPage];. if(
16052 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 pPage->intKey &&
16053 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
16054 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
16055 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
16056 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 (pCur, pRes);.
16057 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
16058 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
16059 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 }. }. *pRes
1605a 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 = 0;. return r
1605b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f c;.}../*.** Allo
1605c 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 cate a new page
1605d 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1605e 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
1605f 65 20 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 e new page is ma
16060 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 rked as dirty.
16061 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c (In other words,
16062 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
16063 74 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 te().** has alre
16064 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 ady been called
16065 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e on the new page.
16066 29 20 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 ) The new page
16067 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e has also.** been
16068 20 72 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 referenced and
16069 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 the calling rout
1606a 69 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 ine is responsib
1606b 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a le for calling.*
1606c 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e * sqlite3PagerUn
1606d 72 65 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 ref() on the new
1606e 20 70 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 page when it is
1606f 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c done..**.** SQL
16070 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
16071 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 ed on success.
16072 41 6e 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e Any other return
16073 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 value indicates
16074 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a .** an error. *
16075 70 70 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e ppPage and *pPgn
16076 6f 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 o are undefined
16077 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 in the event of
16078 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 an error..** Do
16079 6e 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 not invoke sqlit
1607a 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f e3PagerUnref() o
1607b 6e 20 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 n *ppPage if an
1607c 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
1607d 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1607e 22 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 "nearby" paramet
1607f 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 er is not 0, the
16080 6e 20 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 n a (feeble) eff
16081 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a ort is made to .
16082 2a 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 ** locate a page
16083 20 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 close to the pa
16084 67 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 ge number "nearb
16085 79 22 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 y". This can be
16086 20 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 used in an.** a
16087 74 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 ttempt to keep r
16088 65 6c 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f elated pages clo
16089 73 65 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 se to each other
1608a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1608b 20 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 file,.** which
1608c 69 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 in turn can make
1608d 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 database access
1608e 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 faster..**.** I
1608f 66 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61 f the "exact" pa
16090 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 rameter is not 0
16091 2c 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e , and the page-n
16092 75 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 umber nearby exi
16093 73 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 sts .** anywhere
16094 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
16095 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 t, then it is gu
16096 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 arenteed to be r
16097 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a eturned. This.**
16098 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 is only used by
16099 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 auto-vacuum dat
1609a 61 62 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f abases when allo
1609b 63 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 cating a new tab
1609c 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
1609d 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 t allocateBtreeP
1609e 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 age(. BtShared
1609f 2a 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 *pBt, . MemPage
160a0 20 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 **ppPage, . Pg
160a1 6e 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 no *pPgno, . Pg
160a2 6e 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 no nearby,. u8
160a3 65 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 exact.){. MemPa
160a4 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e ge *pPage1;. in
160a5 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 t rc;. int n;
160a6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
160a7 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 pages on the fre
160a8 65 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6b elist */. int k
160a9 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ; /* Number
160aa 6f 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 of leaves on the
160ab 20 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 trunk of the fr
160ac 65 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 eelist */. MemP
160ad 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b age *pTrunk = 0;
160ae 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 . MemPage *pPre
160af 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61 vTrunk = 0;.. a
160b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
160b1 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
160b2 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 utex) );. pPage
160b3 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
160b4 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 . n = get4byte(
160b5 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
160b6 36 5d 29 3b 0a 20 20 69 66 28 20 6e 3e 30 20 29 6]);. if( n>0 )
160b7 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 {. /* There a
160b8 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 re pages on the
160b9 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 freelist. Reuse
160ba 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 one of those pa
160bb 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f ges. */. Pgno
160bc 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 iTrunk;. u8
160bd 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 searchList = 0;
160be 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c /* If the free-l
160bf 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 ist must be sear
160c0 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 ched for 'nearby
160c1 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a ' */. . /*
160c2 20 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20 If the 'exact'
160c3 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72 parameter was tr
160c4 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f ue and a query o
160c5 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 f the pointer-ma
160c6 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74 p. ** shows t
160c7 68 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65 hat the page 'ne
160c8 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65 arby' is somewhe
160c9 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c re on the free-l
160ca 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a ist, then. **
160cb 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 the entire-list
160cc 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65 will be searche
160cd 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e d for that page.
160ce 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 . */.#ifndef
160cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
160d0 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65 VACUUM. if( e
160d1 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d xact && nearby<=
160d2 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
160d3 42 74 29 20 29 7b 0a 20 20 20 20 20 20 75 38 20 Bt) ){. u8
160d4 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61 73 73 eType;. ass
160d5 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20 29 3b ert( nearby>0 );
160d6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
160d7 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
160d8 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 ;. rc = ptr
160d9 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65 61 72 mapGet(pBt, near
160da 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29 3b 0a by, &eType, 0);.
160db 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
160dc 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
160dd 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 if( eType==PTRMA
160de 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 P_FREEPAGE ){.
160df 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 searchList
160e0 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
160e1 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 *pPgno = nea
160e2 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 rby;. }.#endi
160e3 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d f.. /* Decrem
160e4 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73 ent the free-lis
160e5 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 t count by 1. Se
160e6 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 t iTrunk to the
160e7 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20 index of the.
160e8 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c ** first free-l
160e9 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 ist trunk page.
160ea 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e iPrevTrunk is in
160eb 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a itially 1.. *
160ec 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
160ed 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
160ee 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
160ef 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
160f0 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 34 62 rn rc;. put4b
160f1 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
160f2 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 ta[36], n-1);..
160f3 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 77 /* The code w
160f4 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 ithin this loop
160f5 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 is run only once
160f6 20 69 66 20 74 68 65 20 27 73 65 61 72 63 68 4c if the 'searchL
160f7 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a 20 20 ist' variable.
160f8 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 75 65 ** is not true
160f9 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 . Otherwise, it
160fa 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 runs once for ea
160fb 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e ch trunk-page on
160fc 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 65 65 the. ** free
160fd 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 65 20 -list until the
160fe 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 page 'nearby' is
160ff 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20 2a 2f located.. */
16100 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 . do {.
16101 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 54 72 pPrevTrunk = pTr
16102 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 unk;. if( p
16103 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 PrevTrunk ){.
16104 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 iTrunk = ge
16105 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75 t4byte(&pPrevTru
16106 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20 nk->aData[0]);.
16107 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
16108 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 iTrunk = get
16109 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
1610a 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20 Data[32]);.
1610b 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
1610c 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
1610d 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 e(pBt, iTrunk, &
1610e 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 pTrunk, 0);.
1610f 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
16110 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a pTrunk = 0;.
16111 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
16112 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
16113 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 6b }.. k
16114 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 = get4byte(&pTr
16115 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a unk->aData[4]);.
16116 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 26 if( k==0 &
16117 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 29 7b & !searchList ){
16118 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
16119 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 trunk has no lea
1611a 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 73 74 ves and the list
1611b 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 73 65 is not being se
1611c 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 20 20 arched. .
1611d 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 20 74 ** So extract t
1611e 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 74 he trunk page it
1611f 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 74 20 self and use it
16120 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a 20 20 as the newly .
16121 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 ** allocat
16122 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 ed page */.
16123 20 20 20 61 73 73 65 72 74 28 20 70 50 72 65 76 assert( pPrev
16124 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 Trunk==0 );.
16125 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16126 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
16127 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
16128 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
16129 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
1612a 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
1612b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1612c 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75 *pPgno = iTru
1612d 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 nk;. memc
1612e 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 py(&pPage1->aDat
1612f 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e a[32], &pTrunk->
16130 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
16131 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
16132 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 pTrunk;.
16133 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 pTrunk = 0;.
16134 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f TRACE(("ALLO
16135 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d CATE: %d trunk -
16136 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c %d free pages l
16137 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 eft\n", *pPgno,
16138 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c n-1));. }el
16139 73 65 20 69 66 28 20 6b 3e 70 42 74 2d 3e 75 73 se if( k>pBt->us
1613a 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 29 ableSize/4 - 2 )
1613b 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c {. /* Val
1613c 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 74 20 6f ue of k is out o
1613d 66 20 72 61 6e 67 65 2e 20 20 44 61 74 61 62 61 f range. Databa
1613e 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f se corruption */
1613f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
16140 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
16141 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 T;. goto
16142 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
16143 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 e;.#ifndef SQLIT
16144 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
16145 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 M. }else if
16146 28 20 73 65 61 72 63 68 4c 69 73 74 20 26 26 20 ( searchList &&
16147 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b 20 29 nearby==iTrunk )
16148 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
16149 20 6c 69 73 74 20 69 73 20 62 65 69 6e 67 20 73 list is being s
1614a 65 61 72 63 68 65 64 20 61 6e 64 20 74 68 69 73 earched and this
1614b 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 74 trunk page is t
1614c 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 he page.
1614d 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 ** to allocate,
1614e 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 regardless of wh
1614f 65 74 68 65 72 20 69 74 20 68 61 73 20 6c 65 61 ether it has lea
16150 76 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ves.. */.
16151 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
16152 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 *pPgno==iTrunk )
16153 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 ;. *ppPag
16154 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 e = pTrunk;.
16155 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d searchList =
16156 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 0;. rc =
16157 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
16158 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 te(pTrunk->pDbPa
16159 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ge);. if(
1615a 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
1615b 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
1615c 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
1615d 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6b }. if( k
1615e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1615f 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b if( !pPrevTrunk
16160 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
16161 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e memcpy(&pPage1->
16162 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 aData[32], &pTru
16163 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 nk->aData[0], 4)
16164 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
16165 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d e{. m
16166 65 6d 63 70 79 28 26 70 50 72 65 76 54 72 75 6e emcpy(&pPrevTrun
16167 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 k->aData[0], &pT
16168 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 runk->aData[0],
16169 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 4);. }.
1616a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1616b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1616c 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65 trunk page is re
1616d 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63 61 quired by the ca
1616e 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74 ller but it cont
1616f 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20 20 20 ains .
16170 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66 ** pointers to f
16171 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e ree-list leaves.
16172 20 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 20 The first leaf
16173 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0a becomes a trunk.
16174 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 ** pag
16175 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a e in this case..
16176 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
16177 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a MemPage *
16178 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 20 pNewTrunk;.
16179 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 72 Pgno iNewTr
1617a 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 unk = get4byte(&
1617b 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d pTrunk->aData[8]
1617c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
1617d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
1617e 74 50 61 67 65 28 70 42 74 2c 20 69 4e 65 77 54 tPage(pBt, iNewT
1617f 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b runk, &pNewTrunk
16180 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
16181 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16182 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
16183 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
16184 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
16185 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 }. r
16186 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
16187 57 72 69 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d Write(pNewTrunk-
16188 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
16189 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1618a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1618b 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
1618c 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 e(pNewTrunk);.
1618d 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
1618e 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
1618f 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
16190 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
16191 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 pNewTrunk->aData
16192 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 [0], &pTrunk->aD
16193 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 ata[0], 4);.
16194 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
16195 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 pNewTrunk->aData
16196 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 [4], k-1);.
16197 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 memcpy(&pNe
16198 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d wTrunk->aData[8]
16199 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 , &pTrunk->aData
1619a 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a [12], (k-1)*4);.
1619b 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
1619c 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 ePage(pNewTrunk)
1619d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1619e 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 !pPrevTrunk ){.
1619f 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
161a0 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
161a1 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
161a2 31 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 1->pDbPage) );.
161a3 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 put4b
161a4 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
161a5 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e ta[32], iNewTrun
161a6 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 k);. }e
161a7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
161a8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
161a9 65 72 57 72 69 74 65 28 70 50 72 65 76 54 72 75 erWrite(pPrevTru
161aa 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
161ab 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
161ac 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
161ad 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
161ae 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
161af 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
161b0 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 put4byte(&pP
161b1 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b revTrunk->aData[
161b2 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 0], iNewTrunk);.
161b3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
161b4 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 54 }. pT
161b5 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 runk = 0;.
161b6 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 TRACE(("ALLOCA
161b7 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 TE: %d trunk - %
161b8 64 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 d free pages lef
161b9 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d t\n", *pPgno, n-
161ba 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 1));.#endif.
161bb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
161bc 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 /* Extract a le
161bd 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e af from the trun
161be 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 k */. int
161bf 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 closest;.
161c0 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 Pgno iPage;.
161c1 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 unsigned c
161c2 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 har *aData = pTr
161c3 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 unk->aData;.
161c4 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
161c5 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
161c6 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
161c7 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
161c8 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
161c9 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
161ca 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
161cb 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 if( nearby>0
161cc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
161cd 20 69 2c 20 64 69 73 74 3b 0a 20 20 20 20 20 20 i, dist;.
161ce 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b closest = 0;
161cf 0a 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 . dist
161d0 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 = get4byte(&aDat
161d1 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a a[8]) - nearby;.
161d2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64 69 if( di
161d3 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 st<0 ) dist = -d
161d4 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 ist;. f
161d5 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b or(i=1; i<k; i++
161d6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
161d7 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 nt d2 = get4byte
161d8 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 (&aData[8+i*4])
161d9 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 - nearby;.
161da 20 20 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 if( d2<0 )
161db 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 d2 = -d2;.
161dc 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 if( d2<di
161dd 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 st ){.
161de 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b closest = i;
161df 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 . d
161e0 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 ist = d2;.
161e1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
161e2 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
161e3 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f e{. clo
161e4 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 sest = 0;.
161e5 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 }.. iPa
161e6 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 ge = get4byte(&a
161e7 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 Data[8+closest*4
161e8 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ]);. if(
161e9 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 !searchList || i
161ea 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a Page==nearby ){.
161eb 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f int no
161ec 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 Content;.
161ed 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 0a 20 Pgno nPage;.
161ee 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 *pPgno
161ef 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 = iPage;.
161f0 20 20 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 nPage = pager
161f1 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a Pagecount(pBt);.
161f2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 if( *p
161f3 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b 0a 20 20 Pgno>nPage ){.
161f4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 /* Fre
161f5 65 20 70 61 67 65 20 6f 66 66 20 74 68 65 20 65 e page off the e
161f6 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a nd of the file *
161f7 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 /. rc
161f8 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
161f9 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 T_BKPT;.
161fa 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
161fb 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
161fc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
161fd 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 TRACE(("ALLOCA
161fe 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 TE: %d was leaf
161ff 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e %d of %d on trun
16200 6b 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 k %d".
16201 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 ": %d mor
16202 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c e free pages\n",
16203 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
16204 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 *pPgno, closes
16205 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e t+1, k, pTrunk->
16206 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 pgno, n-1));.
16207 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 if( close
16208 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20 st<k-1 ){.
16209 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44 memcpy(&aD
1620a 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d ata[8+closest*4]
1620b 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c , &aData[4+k*4],
1620c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 4);. }
1620d 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 . put4b
1620e 79 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b yte(&aData[4], k
1620f 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 -1);. a
16210 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
16211 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
16212 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 20 Trunk->pDbPage)
16213 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43 );. noC
16214 6f 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47 ontent = !btreeG
16215 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 etHasContent(pBt
16216 2c 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 20 20 , *pPgno);.
16217 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
16218 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 3BtreeGetPage(pB
16219 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 t, *pPgno, ppPag
1621a 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 e, noContent);.
1621b 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d if( rc=
1621c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1621d 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
1621e 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1621f 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 ((*ppPage)->pDbP
16220 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
16221 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16222 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
16223 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
16224 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 (*ppPage);.
16225 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
16226 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 }. s
16227 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 earchList = 0;.
16228 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
16229 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1622a 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a ge(pPrevTrunk);.
1622b 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b pPrevTrunk
1622c 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65 = 0;. }while
1622d 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a ( searchList );.
1622e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1622f 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67 There are no pag
16230 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 es on the freeli
16231 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 20 st, so create a
16232 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65 0a new page at the.
16233 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68 ** end of th
16234 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 6e e file */. in
16235 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 t nPage = pagerP
16236 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 agecount(pBt);.
16237 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 61 67 *pPgno = nPag
16238 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66 28 20 e + 1;.. if(
16239 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f *pPgno==PENDING_
1623a 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
1623b 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29 {. (*pPgno)
1623c 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 ++;. }..#ifnd
1623d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1623e 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
1623f 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
16240 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50 41 m && PTRMAP_ISPA
16241 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29 20 GE(pBt, *pPgno)
16242 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 2a ){. /* If *
16243 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f 20 pPgno refers to
16244 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 a pointer-map pa
16245 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77 6f ge, allocate two
16246 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20 20 new pages.
16247 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f ** at the end o
16248 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74 65 f the file inste
16249 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20 66 ad of one. The f
1624a 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 70 irst allocated p
1624b 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 63 age. ** bec
1624c 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e 74 omes a new point
1624d 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68 65 er-map page, the
1624e 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64 20 second is used
1624f 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 by the caller..
16250 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4d 65 */. Me
16251 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b 0a mPage *pPg = 0;.
16252 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c TRACE(("AL
16253 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 LOCATE: %d from
16254 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f 69 end of file (poi
16255 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c 6e nter-map page)\n
16256 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 20 ", *pPgno));.
16257 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e assert( *pPgn
16258 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f o!=PENDING_BYTE_
16259 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 PAGE(pBt) );.
1625a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1625b 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
1625c 20 2a 70 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30 *pPgno, &pPg, 0
1625d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
1625e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1625f 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
16260 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 e3PagerWrite(pPg
16261 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
16262 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
16263 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 pPg);. }.
16264 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
16265 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 28 2a urn rc;. (*
16266 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 pPgno)++;.
16267 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 if( *pPgno==PEND
16268 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
16269 74 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b t) ){ (*pPgno)++
1626a 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ; }. }.#endif
1626b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 .. assert( *p
1626c 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno!=PENDING_BY
1626d 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a TE_PAGE(pBt) );.
1626e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1626f 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
16270 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 , *pPgno, ppPage
16271 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
16272 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
16273 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
16274 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 gerWrite((*ppPag
16275 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e)->pDbPage);.
16276 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16277 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c _OK ){. rel
16278 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 easePage(*ppPage
16279 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 52 41 );. }. TRA
1627a 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 CE(("ALLOCATE: %
1627b 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 d from end of fi
1627c 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b le\n", *pPgno));
1627d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
1627e 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f *pPgno!=PENDING_
1627f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
16280 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f ;..end_allocate_
16281 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73 65 50 page:. releaseP
16282 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 age(pTrunk);. r
16283 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76 eleasePage(pPrev
16284 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 20 72 63 Trunk);. if( rc
16285 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
16286 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 if( sqlite3Pa
16287 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
16288 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 (*ppPage)->pDbPa
16289 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 72 ge)>1 ){. r
1628a 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 eleasePage(*ppPa
1628b 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ge);. retur
1628c 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1628d 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
1628e 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 6e (*ppPage)->isIn
1628f 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 it = 0;. }. re
16290 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
16291 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
16292 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 is used to add p
16293 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 age iPage to the
16294 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
16295 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 ree-list. .** It
16296 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
16297 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
16298 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 already a part
16299 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 of the free-list
1629a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ..**.** The valu
1629b 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
1629c 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
1629d 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
1629e 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a is optional..**
1629f 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 If the caller h
162a0 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 appens to have a
162a1 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
162a2 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a MemPage object .
162a3 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
162a4 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 to page iPage h
162a5 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 andy, it may pas
162a6 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f s it as the seco
162a7 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 nd value. .** Ot
162a8 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 herwise, it may
162a9 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a pass NULL..**.**
162aa 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f If a pointer to
162ab 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 a MemPage objec
162ac 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 t is passed as t
162ad 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
162ae 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 nt,.** its refer
162af 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f ence count is no
162b0 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 t altered by thi
162b1 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 s function..*/.s
162b2 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 tatic int freePa
162b3 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 ge2(BtShared *pB
162b4 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d t, MemPage *pMem
162b5 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 Page, Pgno iPage
162b6 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 ){. MemPage *pT
162b7 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 runk = 0;
162b8 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 /* Free
162b9 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
162ba 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e */. Pgno iTrun
162bb 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 k = 0;
162bc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
162bd 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 e number of free
162be 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
162bf 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a */ . MemPage *
162c0 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 pPage1 = pBt->pP
162c1 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f age1; /* Lo
162c2 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f cal reference to
162c3 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d page 1 */. Mem
162c4 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 Page *pPage;
162c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
162c6 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 /* Page being f
162c7 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c reed. May be NUL
162c8 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 L. */. int rc;
162c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
162ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
162cb 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 eturn Code */.
162cc 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 int nFree;
162cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
162ce 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e /* Initial n
162cf 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
162d0 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a n free-list */..
162d1 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
162d2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
162d3 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
162d4 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b sert( iPage>1 );
162d5 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d . assert( !pMem
162d6 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 Page || pMemPage
162d7 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b ->pgno==iPage );
162d8 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 .. if( pMemPage
162d9 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 ){. pPage =
162da 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 pMemPage;. sq
162db 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50 lite3PagerRef(pP
162dc 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
162dd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 }else{. pPag
162de 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f e = btreePageLoo
162df 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b kup(pBt, iPage);
162e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 . }.. /* Incre
162e1 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 ment the free pa
162e2 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 ge count on pPag
162e3 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c e1 */. rc = sql
162e4 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
162e5 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b Page1->pDbPage);
162e6 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f . if( rc ) goto
162e7 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
162e8 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 nFree = get4byt
162e9 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
162ea 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 [36]);. put4byt
162eb 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
162ec 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a [36], nFree+1);.
162ed 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 .#ifdef SQLITE_S
162ee 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f ECURE_DELETE. /
162ef 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f * If the SQLITE_
162f0 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f SECURE_DELETE co
162f1 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f mpile-time optio
162f2 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 n is enabled, th
162f3 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 en. ** always f
162f4 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 ully overwrite d
162f5 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 eleted informati
162f6 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 on with zeros..
162f7 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67 */. if( (!pPag
162f8 65 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 e && (rc = sqlit
162f9 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 e3BtreeGetPage(p
162fa 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 67 Bt, iPage, &pPag
162fb 65 2c 20 30 29 29 29 0a 20 20 20 7c 7c 20 20 20 e, 0))). ||
162fc 20 20 20 20 20 20 20 20 20 28 72 63 20 3d 20 73 (rc = s
162fd 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
162fe 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
162ff 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ). ){. goto
16300 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
16301 7d 0a 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65 }. memset(pPage
16302 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 67 ->aData, 0, pPag
16303 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e->pBt->pageSize
16304 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 );.#endif.. /*
16305 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
16306 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 supports auto-va
16307 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e 20 65 cuum, write an e
16308 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e ntry in the poin
16309 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20 ter-map. ** to
1630a 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 indicate that th
1630b 65 20 70 61 67 65 20 69 73 20 66 72 65 65 2e 0a e page is free..
1630c 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 54 */. if( ISAUT
1630d 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 72 OVACUUM ){. r
1630e 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 c = ptrmapPut(pB
1630f 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50 t, iPage, PTRMAP
16310 5f 46 52 45 45 50 41 47 45 2c 20 30 29 3b 0a 20 _FREEPAGE, 0);.
16311 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
16312 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
16313 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e }.. /* Now man
16314 69 70 75 6c 61 74 65 20 74 68 65 20 61 63 74 75 ipulate the actu
16315 61 6c 20 64 61 74 61 62 61 73 65 20 66 72 65 65 al database free
16316 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72 65 2e -list structure.
16317 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 0a 20 There are two.
16318 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 69 65 ** possibilitie
16319 73 2e 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c s. If the free-l
1631a 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ist is currently
1631b 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20 74 68 empty, or if th
1631c 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74 72 75 e first. ** tru
1631d 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 nk page in the f
1631e 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c ree-list is full
1631f 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 , then this page
16320 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 0a 20 will become a.
16321 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 ** new free-lis
16322 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 4f 74 t trunk page. Ot
16323 68 65 72 77 69 73 65 2c 20 69 74 20 77 69 6c 6c herwise, it will
16324 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66 20 6f become a leaf o
16325 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 f the. ** first
16326 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 trunk page in t
16327 68 65 20 63 75 72 72 65 6e 74 20 66 72 65 65 2d he current free-
16328 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f 63 6b list. This block
16329 20 74 65 73 74 73 20 69 66 20 69 74 0a 20 20 2a tests if it. *
1632a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f * is possible to
1632b 20 61 64 64 20 74 68 65 20 70 61 67 65 20 61 73 add the page as
1632c 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 a new free-list
1632d 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 leaf.. */. if
1632e 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a 20 20 ( nFree!=0 ){.
1632f 20 20 69 6e 74 20 6e 4c 65 61 66 3b 20 20 20 20 int nLeaf;
16330 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
16331 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66 nitial number of
16332 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74 leaf cells on t
16333 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 20 runk page */..
16334 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 iTrunk = get4b
16335 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
16336 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72 63 20 ta[32]);. rc
16337 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
16338 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e tPage(pBt, iTrun
16339 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a k, &pTrunk, 0);.
1633a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1633b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 TE_OK ){. g
1633c 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 oto freepage_out
1633d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 65 ;. }.. nLe
1633e 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 af = get4byte(&p
1633f 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 Trunk->aData[4])
16340 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 3c ;. if( nLeaf<
16341 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
16342 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
16343 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 KPT;. goto
16344 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
16345 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 }. if( nLea
16346 66 3c 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a f<pBt->usableSiz
16347 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 20 20 20 e/4 - 8 ){.
16348 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 /* In this case
16349 20 74 68 65 72 65 20 69 73 20 72 6f 6f 6d 20 6f there is room o
1634a 6e 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 n the trunk page
1634b 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 70 to insert the p
1634c 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 age. ** bei
1634d 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6e 65 ng freed as a ne
1634e 77 20 6c 65 61 66 2e 0a 20 20 20 20 20 20 2a 2a w leaf.. **
1634f 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 . ** Note t
16350 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 hat the trunk pa
16351 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 ge is not really
16352 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63 full until it c
16353 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a ontains. **
16354 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
16355 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75 2 entries, not u
16356 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 sableSize/4 - 8
16357 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68 61 entries as we ha
16358 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 ve. ** code
16359 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20 61 d. But due to a
1635a 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 6e coding error in
1635b 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
1635c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 20 ite prior to.
1635d 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 74 ** 3.6.0, dat
1635e 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65 65 abases with free
1635f 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 73 list trunk pages
16360 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 68 holding more th
16361 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 an. ** usab
16362 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 leSize/4 - 8 ent
16363 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65 70 ries will be rep
16364 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70 74 orted as corrupt
16365 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 20 . In order.
16366 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 6e ** to maintain
16367 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
16368 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f 6c tibility with ol
16369 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 der versions of
1636a 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a 2a SQLite,. **
1636b 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e we will contain
1636c 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 65 to restrict the
1636d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 number of entri
1636e 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a 65 es to usableSize
1636f 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a 20 /4 - 8. **
16370 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f 6d for now. At som
16371 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 e point in the f
16372 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 72 uture (once ever
16373 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 65 yone has upgrade
16374 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 2e d. ** to 3.
16375 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 65 6.0 or later) we
16376 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 72 should consider
16377 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 64 fixing the cond
16378 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 20 itional above.
16379 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 22 ** to read "
1637a 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 20 usableSize/4-2"
1637b 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 62 instead of "usab
1637c 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 20 leSize/4-8"..
1637d 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d */. rc =
1637e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1637f 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 te(pTrunk->pDbPa
16380 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
16381 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16382 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
16383 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b (&pTrunk->aData[
16384 34 5d 2c 20 6e 4c 65 61 66 2b 31 29 3b 0a 20 20 4], nLeaf+1);.
16385 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
16386 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b pTrunk->aData[8+
16387 6e 4c 65 61 66 2a 34 5d 2c 20 69 50 61 67 65 29 nLeaf*4], iPage)
16388 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
16389 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 _SECURE_DELETE.
1638a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
1638b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
1638c 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
1638d 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
1638e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 ge);. }.#
1638f 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 63 endif. rc
16390 20 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f = btreeSetHasCo
16391 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65 ntent(pBt, iPage
16392 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
16393 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 TRACE(("FREE-PA
16394 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 GE: %d leaf on t
16395 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c runk page %d\n",
16396 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 pPage->pgno,pTru
16397 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 nk->pgno));.
16398 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
16399 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a out;. }. }..
1639a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 /* If control
1639b 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f flows to this po
1639c 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73 int, then it was
1639d 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
1639e 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68 add the. ** th
1639f 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 e page being fre
163a0 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67 ed as a leaf pag
163a1 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 e of the first t
163a2 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 runk in the free
163a3 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 -list.. ** Poss
163a4 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 ibly because the
163a5 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d free-list is em
163a6 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79 pty, or possibly
163a7 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20 because the .
163a8 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69 ** first trunk i
163a9 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
163aa 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20 is full. Either
163ab 77 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65 way, the page be
163ac 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77 ing freed. ** w
163ad 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e ill become the n
163ae 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 ew first trunk p
163af 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d age in the free-
163b0 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 list.. */. if(
163b1 20 20 20 28 28 21 70 50 61 67 65 29 20 26 26 20 ((!pPage) &&
163b2 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 (0 != (rc = sqli
163b3 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
163b4 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 pBt, iPage, &pPa
163b5 67 65 2c 20 30 29 29 29 29 0a 20 20 20 20 20 7c ge, 0)))). |
163b6 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 | (0 != (rc = sq
163b7 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
163b8 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 pPage->pDbPage))
163b9 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ). ){. goto
163ba 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
163bb 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 61 }. put4byte(pPa
163bc 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e ge->aData, iTrun
163bd 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 k);. put4byte(&
163be 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c pPage->aData[4],
163bf 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 0);. put4byte(
163c0 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
163c1 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52 2], iPage);. TR
163c2 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a ACE(("FREE-PAGE:
163c3 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61 %d new trunk pa
163c4 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c ge replacing %d\
163c5 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c n", pPage->pgno,
163c6 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65 iTrunk));..free
163c7 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 page_out:. if(
163c8 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 pPage ){. pPa
163c9 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a ge->isInit = 0;.
163ca 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 }. releasePag
163cb 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65 e(pPage);. rele
163cc 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b asePage(pTrunk);
163cd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
163ce 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 static int freeP
163cf 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 age(MemPage *pPa
163d0 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 66 72 ge){. return fr
163d1 65 65 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70 eePage2(pPage->p
163d2 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65 Bt, pPage, pPage
163d3 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ->pgno);.}../*.*
163d4 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 * Free any overf
163d5 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 low pages associ
163d6 61 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 ated with the gi
163d7 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 ven Cell..*/.sta
163d8 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c tic int clearCel
163d9 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 l(MemPage *pPage
163da 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 , unsigned char
163db 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 *pCell){. BtSha
163dc 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
163dd 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 ->pBt;. CellInf
163de 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f o info;. Pgno o
163df 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 vflPgno;. int r
163e0 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a c;. int nOvfl;.
163e1 20 20 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69 u16 ovflPageSi
163e2 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ze;.. assert( s
163e3 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
163e4 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
163e5 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
163e6 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 3BtreeParseCellP
163e7 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
163e8 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 &info);. if( i
163e9 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 nfo.iOverflow==0
163ea 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
163eb 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f QLITE_OK; /* No
163ec 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
163ed 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 Return without
163ee 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a doing anything *
163ef 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f /. }. ovflPgno
163f0 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 = get4byte(&pCe
163f1 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
163f2 77 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 w]);. assert( p
163f3 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3e Bt->usableSize >
163f4 20 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 4 );. ovflPage
163f5 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Size = pBt->usab
163f6 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f leSize - 4;. nO
163f7 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79 vfl = (info.nPay
163f8 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 load - info.nLoc
163f9 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a al + ovflPageSiz
163fa 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53 e - 1)/ovflPageS
163fb 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f ize;. assert( o
163fc 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f vflPgno==0 || nO
163fd 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 vfl>0 );. while
163fe 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 ( nOvfl-- ){.
163ff 20 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 30 3b Pgno iNext = 0;
16400 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f . MemPage *pO
16401 76 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 vfl = 0;. if(
16402 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 ovflPgno==0 ||
16403 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65 72 50 61 ovflPgno>pagerPa
16404 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a gecount(pBt) ){.
16405 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
16406 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
16407 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
16408 6e 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 72 nOvfl ){. r
16409 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 c = getOverflowP
1640a 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e age(pBt, ovflPgn
1640b 6f 2c 20 26 70 4f 76 66 6c 2c 20 26 69 4e 65 78 o, &pOvfl, &iNex
1640c 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 t);. if( rc
1640d 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
1640e 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 72 65 }. rc = fre
1640f 65 50 61 67 65 32 28 70 42 74 2c 20 70 4f 76 66 ePage2(pBt, pOvf
16410 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 l, ovflPgno);.
16411 20 20 69 66 28 20 70 4f 76 66 6c 20 29 7b 0a 20 if( pOvfl ){.
16412 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
16413 72 55 6e 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 rUnref(pOvfl->pD
16414 62 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 bPage);. }.
16415 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
16416 6e 20 72 63 3b 0a 20 20 20 20 6f 76 66 6c 50 67 n rc;. ovflPg
16417 6e 6f 20 3d 20 69 4e 65 78 74 3b 0a 20 20 7d 0a no = iNext;. }.
16418 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16419 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 OK;.}../*.** Cre
1641a 61 74 65 20 74 68 65 20 62 79 74 65 20 73 65 71 ate the byte seq
1641b 75 65 6e 63 65 20 75 73 65 64 20 74 6f 20 72 65 uence used to re
1641c 70 72 65 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f present a cell o
1641d 6e 20 70 61 67 65 20 70 50 61 67 65 0a 2a 2a 20 n page pPage.**
1641e 61 6e 64 20 77 72 69 74 65 20 74 68 61 74 20 62 and write that b
1641f 79 74 65 20 73 65 71 75 65 6e 63 65 20 69 6e 74 yte sequence int
16420 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 o pCell[]. Over
16421 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 0a 2a flow pages are.*
16422 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 * allocated and
16423 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65 63 filled in as nec
16424 65 73 73 61 72 79 2e 20 20 54 68 65 20 63 61 6c essary. The cal
16425 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 0a 2a ling procedure.*
16426 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 * is responsible
16427 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 for making sure
16428 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 sufficient spac
16429 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 e has been alloc
1642a 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c ated.** for pCel
1642b 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 l[]..**.** Note
1642c 74 68 61 74 20 70 43 65 6c 6c 20 64 6f 65 73 20 that pCell does
1642d 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 6e 65 not necessary ne
1642e 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 ed to point to t
1642f 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a he pPage->aData.
16430 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20 ** area. pCell
16431 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73 might point to s
16432 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74 ome temporary st
16433 6f 72 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c orage. The cell
16434 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 will.** be cons
16435 74 72 75 63 74 65 64 20 69 6e 20 74 68 69 73 20 tructed in this
16436 74 65 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74 temporary area t
16437 68 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 hen copied into
16438 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 pPage->aData.**
16439 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 later..*/.static
1643a 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 int fillInCell(
1643b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
1643c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
1643d 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 /* The page th
1643e 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
1643f 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e cell */. unsign
16440 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 ed char *pCell,
16441 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 /* Comp
16442 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 lete text of the
16443 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 cell */. const
16444 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 void *pKey, i64
16445 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 nKey, /* The
16446 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 key */. const
16447 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 6e 74 20 void *pData,int
16448 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 68 65 20 nData, /* The
16449 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a data */. int nZ
1644a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 ero,
1644b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
1644c 61 20 7a 65 72 6f 20 62 79 74 65 73 20 74 6f 20 a zero bytes to
1644d 61 70 70 65 6e 64 20 74 6f 20 70 44 61 74 61 20 append to pData
1644e 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65 */. int *pnSize
1644f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16450 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 65 6c /* Write cel
16451 6c 20 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29 l size here */.)
16452 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64 {. int nPayload
16453 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 70 53 ;. const u8 *pS
16454 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20 rc;. int nSrc,
16455 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 73 70 61 n, rc;. int spa
16456 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67 ceLeft;. MemPag
16457 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 e *pOvfl = 0;.
16458 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c 65 MemPage *pToRele
16459 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 ase = 0;. unsig
1645a 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72 ned char *pPrior
1645b 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
1645c 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42 r *pPayload;. B
1645d 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1645e 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e Page->pBt;. Pgn
1645f 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a o pgnoOvfl = 0;.
16460 20 20 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20 int nHeader;.
16461 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
16462 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16463 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
16464 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
16465 20 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 67 65 20 );.. /* pPage
16466 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
16467 6c 79 20 77 72 69 74 65 61 62 6c 65 20 73 69 6e ly writeable sin
16468 63 65 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 62 ce pCell might b
16469 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a e auxiliary. **
1646a 20 62 75 66 66 65 72 20 73 70 61 63 65 20 74 68 buffer space th
1646b 61 74 20 69 73 20 73 65 70 61 72 61 74 65 20 66 at is separate f
1646c 72 6f 6d 20 74 68 65 20 70 50 61 67 65 20 62 75 rom the pPage bu
1646d 66 66 65 72 20 61 72 65 61 20 2a 2f 0a 20 20 61 ffer area */. a
1646e 73 73 65 72 74 28 20 70 43 65 6c 6c 3c 70 50 61 ssert( pCell<pPa
1646f 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 43 65 ge->aData || pCe
16470 6c 6c 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 ll>=&pPage->aDat
16471 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d a[pBt->pageSize]
16472 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
16473 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
16474 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
16475 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a DbPage) );.. /*
16476 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 68 65 61 Fill in the hea
16477 64 65 72 2e 20 2a 2f 0a 20 20 6e 48 65 61 64 65 der. */. nHeade
16478 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 21 70 50 r = 0;. if( !pP
16479 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
1647a 20 6e 48 65 61 64 65 72 20 2b 3d 20 34 3b 0a 20 nHeader += 4;.
1647b 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e }. if( pPage->
1647c 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 6e hasData ){. n
1647d 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 Header += putVar
1647e 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 int(&pCell[nHead
1647f 65 72 5d 2c 20 6e 44 61 74 61 2b 6e 5a 65 72 6f er], nData+nZero
16480 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
16481 6e 44 61 74 61 20 3d 20 6e 5a 65 72 6f 20 3d 20 nData = nZero =
16482 30 3b 0a 20 20 7d 0a 20 20 6e 48 65 61 64 65 72 0;. }. nHeader
16483 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70 += putVarint(&p
16484 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 2a Cell[nHeader], *
16485 28 75 36 34 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 (u64*)&nKey);.
16486 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 sqlite3BtreePars
16487 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 eCellPtr(pPage,
16488 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 pCell, &info);.
16489 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 48 assert( info.nH
1648a 65 61 64 65 72 3d 3d 6e 48 65 61 64 65 72 20 29 eader==nHeader )
1648b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f ;. assert( info
1648c 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 29 3b 0a 20 .nKey==nKey );.
1648d 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 44 assert( info.nD
1648e 61 74 61 3d 3d 28 75 33 32 29 28 6e 44 61 74 61 ata==(u32)(nData
1648f 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 20 0a 20 20 +nZero) );. .
16490 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 /* Fill in the p
16491 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 6e 50 61 79 ayload */. nPay
16492 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20 2b 20 6e load = nData + n
16493 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70 50 61 67 Zero;. if( pPag
16494 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 e->intKey ){.
16495 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 pSrc = pData;.
16496 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b nSrc = nData;
16497 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a . nData = 0;.
16498 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20 69 66 }else{ . if
16499 28 20 6e 4b 65 79 3e 30 78 37 66 66 66 66 66 66 ( nKey>0x7ffffff
1649a 66 20 7c 7c 20 70 4b 65 79 3d 3d 30 20 29 7b 0a f || pKey==0 ){.
1649b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1649c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 ITE_CORRUPT;.
1649d 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 }. nPayload
1649e 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 += (int)nKey;.
1649f 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 pSrc = pKey;.
164a0 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e nSrc = (int)n
164a1 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 Key;. }. *pnSi
164a2 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b ze = info.nSize;
164a3 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 . spaceLeft = i
164a4 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 nfo.nLocal;. pP
164a5 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b ayload = &pCell[
164a6 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 nHeader];. pPri
164a7 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f or = &pCell[info
164a8 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 .iOverflow];..
164a9 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e while( nPayload>
164aa 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 0 ){. if( spa
164ab 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 ceLeft==0 ){.#if
164ac 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
164ad 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
164ae 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 Pgno pgnoPtrma
164af 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a p = pgnoOvfl; /*
164b0 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 Overflow page p
164b1 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 ointer-map entry
164b2 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 page */. i
164b3 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
164b4 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f um ){. do
164b5 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f {. pgno
164b6 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 Ovfl++;.
164b7 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 } while( .
164b8 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 PTRMAP_ISPAG
164b9 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 E(pBt, pgnoOvfl)
164ba 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 || pgnoOvfl==PE
164bb 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
164bc 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b pBt) . );
164bd 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
164be 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 rc = alloc
164bf 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
164c0 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f , &pOvfl, &pgnoO
164c1 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 vfl, pgnoOvfl, 0
164c2 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
164c3 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
164c4 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 M. /* If th
164c5 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
164c6 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c rts auto-vacuum,
164c7 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 and the second
164c8 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 or subsequent.
164c9 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 ** overflow
164ca 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c page is being al
164cb 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 located, add an
164cc 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 entry to the poi
164cd 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a nter-map. *
164ce 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 * for that page
164cf 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 now. . **.
164d0 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 ** If this
164d1 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 is the first ove
164d2 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e rflow page, then
164d3 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 6c write a partial
164d4 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a entry . **
164d5 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d to the pointer-
164d6 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 map. If we write
164d7 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 nothing to this
164d8 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f pointer-map slo
164d9 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e t,. ** then
164da 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 the optimistic
164db 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 overflow chain p
164dc 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 rocessing in cle
164dd 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a arCell(). *
164de 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 * may misinterpr
164df 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c et the uninitial
164e0 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 ised values and
164e1 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 delete the.
164e2 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 ** wrong pages
164e3 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
164e4 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
164e5 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
164e6 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c acuum && rc==SQL
164e7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
164e8 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 u8 eType = (pg
164e9 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f noPtrmap?PTRMAP_
164ea 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 OVERFLOW2:PTRMAP
164eb 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 _OVERFLOW1);.
164ec 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 rc = ptrmap
164ed 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 Put(pBt, pgnoOvf
164ee 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 l, eType, pgnoPt
164ef 72 6d 61 70 29 3b 0a 20 20 20 20 20 20 20 20 69 rmap);. i
164f0 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
164f1 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
164f2 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d Ovfl);. }
164f3 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
164f4 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
164f5 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
164f6 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b age(pToRelease);
164f7 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
164f8 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 rc;. }..
164f9 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 /* If pToRele
164fa 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 ase is not zero
164fb 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e than pPrior poin
164fc 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 ts into the data
164fd 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f area. ** o
164fe 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d f pToRelease. M
164ff 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 ake sure pToRele
16500 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 ase is still wri
16501 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 teable. */.
16502 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 assert( pToRele
16503 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ase==0 || sqlite
16504 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
16505 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 e(pToRelease->pD
16506 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 bPage) );..
16507 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 /* If pPrior is
16508 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 part of the dat
16509 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c a area of pPage,
1650a 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 then make sure
1650b 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 pPage. ** i
1650c 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c s still writeabl
1650d 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 e */. asser
1650e 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d t( pPrior<pPage-
1650f 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 >aData || pPrior
16510 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b >=&pPage->aData[
16511 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 pBt->pageSize].
16512 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 || sq
16513 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
16514 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
16515 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 Page) );..
16516 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c put4byte(pPrior,
16517 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 pgnoOvfl);.
16518 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
16519 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 oRelease);.
1651a 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f pToRelease = pO
1651b 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f vfl;. pPrio
1651c 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 r = pOvfl->aData
1651d 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
1651e 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 (pPrior, 0);.
1651f 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 pPayload = &p
16520 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a Ovfl->aData[4];.
16521 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 spaceLeft
16522 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
16523 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 e - 4;. }.
16524 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 n = nPayload;.
16525 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 if( n>spaceLe
16526 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 ft ) n = spaceLe
16527 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 ft;.. /* If p
16528 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 ToRelease is not
16529 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c zero than pPayl
1652a 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 oad points into
1652b 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 the data area.
1652c 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 ** of pToRelea
1652d 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 se. Make sure p
1652e 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 ToRelease is sti
1652f 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f ll writeable. */
16530 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f . assert( pTo
16531 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 Release==0 || sq
16532 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
16533 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 eable(pToRelease
16534 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
16535 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 /* If pPayloa
16536 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 d is part of the
16537 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 data area of pP
16538 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 age, then make s
16539 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a ure pPage. **
1653a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 is still writea
1653b 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 ble */. asser
1653c 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 t( pPayload<pPag
1653d 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 e->aData || pPay
1653e 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 load>=&pPage->aD
1653f 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a ata[pBt->pageSiz
16540 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c e]. |
16541 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 | sqlite3PagerIs
16542 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
16543 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 >pDbPage) );..
16544 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a if( nSrc>0 ){.
16545 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 if( n>nSrc
16546 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 ) n = nSrc;.
16547 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20 assert( pSrc
16548 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 );. memcpy(
16549 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 pPayload, pSrc,
1654a 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 n);. }else{.
1654b 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 memset(pPay
1654c 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 load, 0, n);.
1654d 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 }. nPayload
1654e 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f -= n;. pPaylo
1654f 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 ad += n;. pSr
16550 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 c += n;. nSrc
16551 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 -= n;. space
16552 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 Left -= n;. i
16553 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 f( nSrc==0 ){.
16554 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 nSrc = nData
16555 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 ;. pSrc = p
16556 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Data;. }. }.
16557 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
16558 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 oRelease);. ret
16559 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1655a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 ../*.** Remove t
1655b 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f he i-th cell fro
1655c 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 m pPage. This r
1655d 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 outine effects p
1655e 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 Page only..** Th
1655f 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 e cell content i
16560 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 s not freed or d
16561 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 eallocated. It
16562 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a is assumed that.
16563 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 ** the cell cont
16564 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 ent has been cop
16565 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c ied someplace el
16566 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e se. This routin
16567 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 e just.** remove
16568 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 s the reference
16569 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d to the cell from
1656a 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 pPage..**.** "s
1656b 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e z" must be the n
1656c 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1656d 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 n the cell..*/.s
1656e 74 61 74 69 63 20 69 6e 74 20 64 72 6f 70 43 65 tatic int dropCe
1656f 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ll(MemPage *pPag
16570 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 e, int idx, int
16571 73 7a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 sz){. int i;
16572 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
16573 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
16574 70 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f pc; /* O
16575 66 66 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f ffset to cell co
16576 6e 74 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 ntent of cell be
16577 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 ing deleted */.
16578 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 u8 *data;
16579 20 2f 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61 /* pPage->aData
1657a 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 */. u8 *ptr;
1657b 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
1657c 20 6d 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75 move bytes arou
1657d 6e 64 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d nd within data[]
1657e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
1657f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 /* The ret
16580 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 61 urn code */.. a
16581 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 ssert( idx>=0 &&
16582 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c idx<pPage->nCel
16583 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 l );. assert( s
16584 7a 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 z==cellSize(pPag
16585 65 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 e, idx) );. ass
16586 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
16587 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
16588 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
16589 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1658a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
1658b 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
1658c 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 );. data = pPag
1658d 65 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 e->aData;. ptr
1658e 3d 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 = &data[pPage->c
1658f 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 ellOffset + 2*id
16590 78 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 x];. pc = get2b
16591 79 74 65 28 70 74 72 29 3b 0a 20 20 69 66 28 20 yte(ptr);. if(
16592 28 70 63 3c 70 50 61 67 65 2d 3e 68 64 72 4f 66 (pc<pPage->hdrOf
16593 66 73 65 74 2b 36 2b 28 70 50 61 67 65 2d 3e 6c fset+6+(pPage->l
16594 65 61 66 3f 30 3a 34 29 29 0a 20 20 20 20 20 7c eaf?0:4)). |
16595 7c 20 28 70 63 2b 73 7a 3e 70 50 61 67 65 2d 3e | (pc+sz>pPage->
16596 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 pBt->usableSize)
16597 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16598 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
16599 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66 PT;. }. rc = f
1659a 72 65 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 reeSpace(pPage,
1659b 70 63 2c 20 73 7a 29 3b 0a 20 20 69 66 28 20 72 pc, sz);. if( r
1659c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1659d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1659e 20 7d 0a 20 20 66 6f 72 28 69 3d 69 64 78 2b 31 }. for(i=idx+1
1659f 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ; i<pPage->nCell
165a0 3b 20 69 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a ; i++, ptr+=2){.
165a1 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 ptr[0] = ptr
165a2 5b 32 5d 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20 [2];. ptr[1]
165a3 3d 20 70 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20 = ptr[3];. }.
165a4 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a pPage->nCell--;.
165a5 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
165a6 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
165a7 74 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 t+3], pPage->nCe
165a8 6c 6c 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 ll);. pPage->nF
165a9 72 65 65 20 2b 3d 20 32 3b 0a 20 20 72 65 74 75 ree += 2;. retu
165aa 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
165ab 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 ./*.** Insert a
165ac 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 new cell on pPag
165ad 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20 e at cell index
165ae 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e "i". pCell poin
165af 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e ts to the.** con
165b0 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c tent of the cell
165b1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
165b2 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c ell content will
165b3 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 fit on the page
165b4 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74 68 , then put it th
165b5 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77 ere. If it.** w
165b6 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65 ill not fit, the
165b7 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 n make a copy of
165b8 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
165b9 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a t into pTemp if.
165ba 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20 ** pTemp is not
165bb 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73 null. Regardles
165bc 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f s of pTemp, allo
165bd 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 cate a new entry
165be 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 4f .** in pPage->aO
165bf 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69 vfl[] and make i
165c0 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63 t point to the c
165c1 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74 ell content (eit
165c2 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20 her.** in pTemp
165c3 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 or the original
165c4 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20 pCell) and also
165c5 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78 record its index
165c6 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 . .** Allocating
165c7 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 a new entry in
165c8 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69 pPage->aCell[] i
165c9 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20 mplies that .**
165ca 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
165cb 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e is incremented.
165cc 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 20 .**.** If nSkip
165cd 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
165ce 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 n do not copy th
165cf 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79 e first nSkip by
165d0 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 65 tes of the.** ce
165d1 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 ll. The caller w
165d2 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 ill overwrite th
165d3 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 66 75 em after this fu
165d4 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20 nction returns.
165d5 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 6e If.** nSkip is n
165d6 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 43 on-zero, then pC
165d7 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 6e ell may not poin
165d8 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 t to an invalid
165d9 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 memory location
165da 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e .** (but pCell+n
165db 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 20 76 Skip is always v
165dc 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 alid)..*/.static
165dd 20 69 6e 74 20 69 6e 73 65 72 74 43 65 6c 6c 28 int insertCell(
165de 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
165df 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 6e 74 e, /* Page int
165e0 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 63 o which we are c
165e1 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 opying */. int
165e2 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a i, /*
165e3 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f 6d 65 New cell become
165e4 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 s the i-th cell
165e5 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 of the page */.
165e6 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 u8 *pCell,
165e7 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 /* Content of
165e8 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 2a 2f the new cell */
165e9 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20 20 20 . int sz,
165ea 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 /* Bytes of
165eb 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43 65 6c content in pCel
165ec 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65 6d 70 l */. u8 *pTemp
165ed 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 , /* Temp
165ee 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 66 storage space f
165ef 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e 65 65 or pCell, if nee
165f0 64 65 64 20 2a 2f 0a 20 20 75 38 20 6e 53 6b 69 ded */. u8 nSki
165f1 70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f p /* Do
165f2 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20 66 not write the f
165f3 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74 65 73 irst nSkip bytes
165f4 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a of the cell */.
165f5 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20 ){. int idx;
165f6 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 /* Where
165f7 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63 65 6c to write new cel
165f8 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74 l content in dat
165f9 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 a[] */. int j;
165fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
165fb 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
165fc 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 20 int top;
165fd 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
165fe 6f 66 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61 of content for a
165ff 6e 79 20 63 65 6c 6c 20 69 6e 20 64 61 74 61 5b ny cell in data[
16600 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20 ] */. int end;
16601 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
16602 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 t byte past the
16603 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 last cell pointe
16604 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 r in data[] */.
16605 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20 int ins;
16606 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64 /* Index in d
16607 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20 ata[] where new
16608 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 cell pointer is
16609 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e inserted */. in
1660a 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 t hdr;
1660b 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 64 /* Offset into d
1660c 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 70 61 67 ata[] of the pag
1660d 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e e header */. in
1660e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 t cellOffset;
1660f 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 66 69 /* Address of fi
16610 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 rst cell pointer
16611 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 in data[] */.
16612 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 u8 *data;
16613 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 /* The content
16614 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61 of the whole pa
16615 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b ge */. u8 *ptr;
16616 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
16617 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66 d for moving inf
16618 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20 ormation around
16619 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20 in data[] */..
1661a 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
1661b 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b i<=pPage->nCell+
1661c 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
1661d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
1661e 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 age->nCell<=MX_C
1661f 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 ELL(pPage->pBt)
16620 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 && MX_CELL(pPage
16621 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a ->pBt)<=5460 );.
16622 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
16623 3e 6e 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61 >nOverflow<=Arra
16624 79 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 4f 76 ySize(pPage->aOv
16625 66 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 fl) );. assert(
16626 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 sz==cellSizePtr
16627 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 (pPage, pCell) )
16628 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
16629 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1662a 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
1662b 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 ) );. if( pPage
1662c 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 ->nOverflow || s
1662d 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 z+2>pPage->nFree
1662e 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d ){. if( pTem
1662f 70 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 p ){. memcp
16630 79 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 y(pTemp+nSkip, p
16631 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e Cell+nSkip, sz-n
16632 53 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 Skip);. pCe
16633 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 ll = pTemp;.
16634 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d }. j = pPage-
16635 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 >nOverflow++;.
16636 20 20 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74 assert( j<(int
16637 29 28 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e )(sizeof(pPage->
16638 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50 aOvfl)/sizeof(pP
16639 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20 age->aOvfl[0]))
1663a 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f );. pPage->aO
1663b 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70 vfl[j].pCell = p
1663c 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d Cell;. pPage-
1663d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20 >aOvfl[j].idx =
1663e 28 75 31 36 29 69 3b 0a 20 20 20 20 70 50 61 67 (u16)i;. pPag
1663f 65 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 e->nFree = 0;.
16640 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 }else{. int r
16641 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
16642 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
16643 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
16644 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
16645 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
16646 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
16647 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
16648 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
16649 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
1664a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e data = pPage->
1664b 61 44 61 74 61 3b 0a 20 20 20 20 68 64 72 20 3d aData;. hdr =
1664c 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
1664d 74 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 t;. top = get
1664e 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
1664f 35 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 5]);. cellOff
16650 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c set = pPage->cel
16651 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 lOffset;. end
16652 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 = cellOffset +
16653 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 2b 2*pPage->nCell +
16654 20 32 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63 65 2;. ins = ce
16655 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a llOffset + 2*i;.
16656 20 20 20 20 69 66 28 20 65 6e 64 20 3e 20 74 6f if( end > to
16657 70 20 2d 20 73 7a 20 29 7b 0a 20 20 20 20 20 20 p - sz ){.
16658 72 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 rc = defragmentP
16659 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
1665a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1665b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
1665c 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
1665d 7d 0a 20 20 20 20 20 20 74 6f 70 20 3d 20 67 65 }. top = ge
1665e 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
1665f 2b 35 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 +5]);. asse
16660 72 74 28 20 65 6e 64 20 2b 20 73 7a 20 3c 3d 20 rt( end + sz <=
16661 74 6f 70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 top );. }.
16662 20 69 64 78 20 3d 20 61 6c 6c 6f 63 61 74 65 53 idx = allocateS
16663 70 61 63 65 28 70 50 61 67 65 2c 20 73 7a 29 3b pace(pPage, sz);
16664 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 . assert( idx
16665 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 >0 );. assert
16666 28 20 65 6e 64 20 3c 3d 20 67 65 74 32 62 79 74 ( end <= get2byt
16667 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 e(&data[hdr+5])
16668 29 3b 0a 20 20 20 20 69 66 20 28 69 64 78 2b 73 );. if (idx+s
16669 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e z > pPage->pBt->
1666a 75 73 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 20 usableSize) {.
1666b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1666c 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1666d 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d }. pPage-
1666e 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 >nCell++;. pP
1666f 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 3b age->nFree -= 2;
16670 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 . memcpy(&dat
16671 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 a[idx+nSkip], pC
16672 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 ell+nSkip, sz-nS
16673 6b 69 70 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d kip);. for(j=
16674 65 6e 64 2d 32 2c 20 70 74 72 3d 26 64 61 74 61 end-2, ptr=&data
16675 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 [j]; j>ins; j-=2
16676 2c 20 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 , ptr-=2){.
16677 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 ptr[0] = ptr[-2
16678 5d 3b 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20 ];. ptr[1]
16679 3d 20 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d = ptr[-1];. }
1667a 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 . put2byte(&d
1667b 61 74 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a ata[ins], idx);.
1667c 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
1667d 74 61 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 ta[hdr+3], pPage
1667e 2d 3e 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 ->nCell);.#ifnde
1667f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
16680 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
16681 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74 pPage->pBt->aut
16682 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
16683 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61 79 /* The cell may
16684 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 contain a point
16685 65 72 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f er to an overflo
16686 77 20 70 61 67 65 2e 20 49 66 20 73 6f 2c 20 77 w page. If so, w
16687 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 rite. ** th
16688 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 e entry for the
16689 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e overflow page in
1668a 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d to the pointer m
1668b 61 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ap.. */.
1668c 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f CellInfo info
1668d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ;. sqlite3B
1668e 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
1668f 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 (pPage, pCell, &
16690 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 61 73 73 info);. ass
16691 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 ert( (info.nData
16692 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f +(pPage->intKey?
16693 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 0:info.nKey))==i
16694 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a nfo.nPayload );.
16695 20 20 20 20 20 20 69 66 28 20 28 69 6e 66 6f 2e if( (info.
16696 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e nData+(pPage->in
16697 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 tKey?0:info.nKey
16698 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 ))>info.nLocal )
16699 7b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 70 {. Pgno p
1669a 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 gnoOvfl = get4by
1669b 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 te(&pCell[info.i
1669c 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 Overflow]);.
1669d 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 rc = ptrmapP
1669e 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 ut(pPage->pBt, p
1669f 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f gnoOvfl, PTRMAP_
166a0 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 OVERFLOW1, pPage
166a1 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
166a2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
166a3 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a OK ) return rc;.
166a4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 }. }.#e
166a5 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ndif. }.. retu
166a6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
166a7 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 73 ./*.** Add a lis
166a8 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 20 t of cells to a
166a9 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 page. The page
166aa 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 should be initia
166ab 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 lly empty..** Th
166ac 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61 72 e cells are guar
166ad 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f 6e anteed to fit on
166ae 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 the page..*/.st
166af 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62 atic void assemb
166b0 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 lePage(. MemPag
166b1 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54 e *pPage, /* T
166b2 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61 73 he page to be as
166b3 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 6e semblied */. in
166b4 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 t nCell,
166b5 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 /* The number of
166b6 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f cells to add to
166b7 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 this page */.
166b8 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20 u8 **apCell,
166b9 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f /* Pointers to
166ba 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f 0a cell bodies */.
166bb 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20 20 u16 *aSize
166bc 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 /* Sizes of
166bd 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a the cells */.){.
166be 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
166bf 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
166c0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 74 ter */. int tot
166c1 61 6c 53 69 7a 65 3b 20 20 20 20 2f 2a 20 54 6f alSize; /* To
166c2 74 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 tal size of all
166c3 63 65 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 68 cells */. int h
166c4 64 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 dr; /*
166c5 49 6e 64 65 78 20 6f 66 20 70 61 67 65 20 68 65 Index of page he
166c6 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 ader */. int ce
166c7 6c 6c 70 74 72 3b 20 20 20 20 20 20 2f 2a 20 41 llptr; /* A
166c8 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 ddress of next c
166c9 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ell pointer */.
166ca 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 int cellbody;
166cb 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
166cc 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 next cell body
166cd 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 */. u8 *data;
166ce 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 /* Data f
166cf 6f 72 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a or the page */..
166d0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
166d1 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b >nOverflow==0 );
166d2 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
166d3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
166d4 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
166d5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 );. assert( nC
166d6 65 6c 6c 3e 3d 30 20 26 26 20 6e 43 65 6c 6c 3c ell>=0 && nCell<
166d7 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e =MX_CELL(pPage->
166d8 70 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 pBt) && MX_CELL(
166d9 70 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36 pPage->pBt)<=546
166da 30 20 29 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 0 );. totalSize
166db 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b = 0;. for(i=0;
166dc 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
166dd 20 20 20 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d totalSize +=
166de 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 7d 0a 20 aSize[i];. }.
166df 20 61 73 73 65 72 74 28 20 74 6f 74 61 6c 53 69 assert( totalSi
166e0 7a 65 2b 32 2a 6e 43 65 6c 6c 3c 3d 70 50 61 67 ze+2*nCell<=pPag
166e1 65 2d 3e 6e 46 72 65 65 20 29 3b 0a 20 20 61 73 e->nFree );. as
166e2 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 sert( pPage->nCe
166e3 6c 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ll==0 );. asser
166e4 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
166e5 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
166e6 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
166e7 63 65 6c 6c 70 74 72 20 3d 20 70 50 61 67 65 2d cellptr = pPage-
166e8 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 64 >cellOffset;. d
166e9 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ata = pPage->aDa
166ea 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 ta;. hdr = pPag
166eb 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
166ec 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 put2byte(&data[h
166ed 64 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 dr+3], nCell);.
166ee 20 69 66 28 20 6e 43 65 6c 6c 20 29 7b 0a 20 20 if( nCell ){.
166ef 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 61 6c 6c cellbody = all
166f0 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65 ocateSpace(pPage
166f1 2c 20 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 , totalSize);.
166f2 20 20 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f assert( cellbo
166f3 64 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 dy>0 );. asse
166f4 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 rt( pPage->nFree
166f5 20 3e 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 >= 2*nCell );.
166f6 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 pPage->nFree
166f7 2d 3d 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 20 20 -= 2*nCell;.
166f8 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c for(i=0; i<nCell
166f9 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 75 ; i++){. pu
166fa 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c t2byte(&data[cel
166fb 6c 70 74 72 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 lptr], cellbody)
166fc 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 ;. memcpy(&
166fd 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 data[cellbody],
166fe 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 apCell[i], aSize
166ff 5b 69 5d 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c [i]);. cell
16700 70 74 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 ptr += 2;.
16701 63 65 6c 6c 62 6f 64 79 20 2b 3d 20 61 53 69 7a cellbody += aSiz
16702 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 e[i];. }.
16703 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79 assert( cellbody
16704 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 ==pPage->pBt->us
16705 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a ableSize );. }.
16706 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d pPage->nCell =
16707 20 28 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a (u16)nCell;.}..
16708 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
16709 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 64 ing parameters d
1670a 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e etermine how man
1670b 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73 y adjacent pages
1670c 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a get involved.**
1670d 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 in a balancing
1670e 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 operation. NN i
1670f 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
16710 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 neighbors on eit
16711 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 her side.** of t
16712 68 65 20 70 61 67 65 20 74 68 61 74 20 70 61 72 he page that par
16713 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 ticipate in the
16714 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 balancing operat
16715 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a ion. NB is the.
16716 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 ** total number
16717 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 70 61 of pages that pa
16718 72 74 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 rticipate, inclu
16719 64 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 ding the target
1671a 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e page and.** NN n
1671b 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 eighbors on eith
1671c 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 er side..**.** T
1671d 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 he minimum value
1671e 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 of NN is 1 (of
1671f 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61 course). Increa
16720 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a sing NN above 1.
16721 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67 ** (to 2 or 3) g
16722 69 76 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d ives a modest im
16723 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c provement in SEL
16724 45 43 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70 ECT and DELETE p
16725 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e erformance.** in
16726 20 65 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20 exchange for a
16727 6c 61 72 67 65 72 20 64 65 67 72 61 64 61 74 69 larger degradati
16728 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 on in INSERT and
16729 20 55 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61 UPDATE performa
1672a 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 nce..** The valu
1672b 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20 e of NN appears
1672c 74 6f 20 67 69 76 65 20 74 68 65 20 62 65 73 74 to give the best
1672d 20 72 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c results overall
1672e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 ..*/.#define NN
1672f 31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 1 /*
16730 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 Number of neigh
16731 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 bors on either s
16732 69 64 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a ide of pPage */.
16733 23 64 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 #define NB (NN*2
16734 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 +1) /* Tota
16735 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 l pages involved
16736 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20 in the balance
16737 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 */../* Forward r
16738 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 eference */.stat
16739 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 ic int balance(B
1673a 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 29 3b 0a tCursor*, int);.
1673b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1673c 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 OMIT_QUICKBALANC
1673d 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 E./*.** This ver
1673e 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 28 sion of balance(
1673f 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63 6f ) handles the co
16740 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 73 mmon special cas
16741 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 77 e where.** a new
16742 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67 20 entry is being
16743 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65 20 inserted on the
16744 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65 6e extreme right-en
16745 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 65 d of the.** tree
16746 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 , in other words
16747 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20 65 , when the new e
16748 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d 65 ntry will become
16749 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 the largest.**
1674a 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 65 entry in the tre
1674b 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 e..**.** Instead
1674c 20 6f 66 20 74 72 79 69 6e 67 20 62 61 6c 61 6e of trying balan
1674d 63 65 20 74 68 65 20 33 20 72 69 67 68 74 2d 6d ce the 3 right-m
1674e 6f 73 74 20 6c 65 61 66 20 70 61 67 65 73 2c 20 ost leaf pages,
1674f 6a 75 73 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65 just add.** a ne
16750 77 20 70 61 67 65 20 74 6f 20 74 68 65 20 72 69 w page to the ri
16751 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 61 6e ght-hand side an
16752 64 20 70 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 d put the one ne
16753 77 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 w entry in.** th
16754 61 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 6c at page. This l
16755 65 61 76 65 73 20 74 68 65 20 72 69 67 68 74 20 eaves the right
16756 73 69 64 65 20 6f 66 20 74 68 65 20 74 72 65 65 side of the tree
16757 20 73 6f 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62 somewhat.** unb
16758 61 6c 61 6e 63 65 64 2e 20 20 42 75 74 20 6f 64 alanced. But od
16759 64 73 20 61 72 65 20 74 68 61 74 20 77 65 20 77 ds are that we w
1675a 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67 ill be inserting
1675b 20 6e 65 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20 new entries.**
1675c 61 74 20 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20 at the end soon
1675d 61 66 74 65 72 77 61 72 64 73 20 73 6f 20 74 68 afterwards so th
1675e 65 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 20 70 e nearly empty p
1675f 61 67 65 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79 age will quickly
16760 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e .** fill up. On
16761 20 61 76 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 average..**.**
16762 70 50 61 67 65 20 69 73 20 74 68 65 20 6c 65 61 pPage is the lea
16763 66 20 70 61 67 65 20 77 68 69 63 68 20 69 73 20 f page which is
16764 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 70 the right-most p
16765 61 67 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e age in the tree.
16766 0a 2a 2a 20 70 50 61 72 65 6e 74 20 69 73 20 69 .** pParent is i
16767 74 73 20 70 61 72 65 6e 74 2e 20 20 70 50 61 67 ts parent. pPag
16768 65 20 6d 75 73 74 20 68 61 76 65 20 61 20 73 69 e must have a si
16769 6e 67 6c 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e ngle overflow en
1676a 74 72 79 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 try.** which is
1676b 61 6c 73 6f 20 74 68 65 20 72 69 67 68 74 2d 6d also the right-m
1676c 6f 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 ost entry on the
1676d 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 page..*/.static
1676e 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 int balance_qui
1676f 63 6b 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ck(BtCursor *pCu
16770 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 r){. int rc;.
16771 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d 20 MemPage *pNew =
16772 30 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 0;. Pgno pgnoNe
16773 77 3b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a w;. u8 *pCell;.
16774 20 20 75 31 36 20 73 7a 43 65 6c 6c 3b 0a 20 20 u16 szCell;.
16775 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
16776 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
16777 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
16778 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 4d Cur->iPage];. M
16779 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 20 emPage *pParent
1677a 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
1677b 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 Cur->iPage-1];.
1677c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1677d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 pPage->pBt;. i
1677e 6e 74 20 70 61 72 65 6e 74 49 64 78 20 3d 20 70 nt parentIdx = p
1677f 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 20 20 Parent->nCell;
16780 20 2f 2a 20 70 50 61 72 65 6e 74 20 6e 65 77 20 /* pParent new
16781 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 64 divider cell ind
16782 65 78 20 2a 2f 0a 20 20 69 6e 74 20 70 61 72 65 ex */. int pare
16783 6e 74 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 ntSize;
16784 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
16785 65 20 6f 66 20 6e 65 77 20 64 69 76 69 64 65 72 e of new divider
16786 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 70 61 cell */. u8 pa
16787 72 65 6e 74 43 65 6c 6c 5b 36 34 5d 3b 20 20 20 rentCell[64];
16788 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16789 53 70 61 63 65 20 66 6f 72 20 74 68 65 20 6e 65 Space for the ne
1678a 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a w divider cell *
1678b 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c /.. assert( sql
1678c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1678d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
1678e 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f x) );.. /* Allo
1678f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e cate a new page.
16790 20 49 6e 73 65 72 74 20 74 68 65 20 6f 76 65 72 Insert the over
16791 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f 6d 20 70 flow cell from p
16792 50 61 67 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 69 Page. ** into i
16793 74 2e 20 54 68 65 6e 20 72 65 6d 6f 76 65 20 74 t. Then remove t
16794 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c he overflow cell
16795 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 20 20 2a from pPage.. *
16796 2f 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 /. rc = allocat
16797 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
16798 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c &pNew, &pgnoNew,
16799 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 0, 0);. if( rc
1679a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1679b 20 20 20 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 pCell = pPage
1679c 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c ->aOvfl[0].pCell
1679d 3b 0a 20 20 20 20 73 7a 43 65 6c 6c 20 3d 20 63 ;. szCell = c
1679e 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
1679f 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73 , pCell);. as
167a0 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
167a1 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e erIswriteable(pN
167a2 65 77 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ew->pDbPage) );.
167a3 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 zeroPage(pNe
167a4 77 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b w, pPage->aData[
167a5 30 5d 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 0]);. assembl
167a6 65 50 61 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 ePage(pNew, 1, &
167a7 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b pCell, &szCell);
167a8 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 . pPage->nOve
167a9 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 0a 20 20 rflow = 0;. .
167aa 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 63 75 /* pPage is cu
167ab 72 72 65 6e 74 6c 79 20 74 68 65 20 72 69 67 68 rrently the righ
167ac 74 2d 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65 t-child of pPare
167ad 6e 74 2e 20 43 68 61 6e 67 65 20 74 68 69 73 0a nt. Change this.
167ae 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 74 ** so that t
167af 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69 he right-child i
167b0 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 61 s the new page a
167b1 6c 6c 6f 63 61 74 65 64 20 61 62 6f 76 65 20 61 llocated above a
167b2 6e 64 0a 20 20 20 20 2a 2a 20 70 50 61 67 65 20 nd. ** pPage
167b3 69 73 20 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72 is the next-to-r
167b4 69 67 68 74 20 63 68 69 6c 64 2e 20 0a 20 20 20 ight child. .
167b5 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 67 6e 6f 72 **. ** Ignor
167b6 65 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c e the return val
167b7 75 65 20 6f 66 20 74 68 65 20 63 61 6c 6c 20 74 ue of the call t
167b8 6f 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 2e 20 o fillInCell().
167b9 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 0a 20 20 20 fillInCell().
167ba 20 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 72 65 74 ** may only ret
167bb 75 72 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 53 urn other than S
167bc 51 4c 49 54 45 5f 4f 4b 20 69 66 20 69 74 20 69 QLITE_OK if it i
167bd 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 6c s required to al
167be 6c 6f 63 61 74 65 0a 20 20 20 20 2a 2a 20 6f 6e locate. ** on
167bf 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c e or more overfl
167c0 6f 77 20 70 61 67 65 73 2e 20 53 69 6e 63 65 20 ow pages. Since
167c1 61 6e 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c an internal tabl
167c2 65 20 42 2d 54 72 65 65 20 63 65 6c 6c 20 0a 20 e B-Tree cell .
167c3 20 20 20 2a 2a 20 6d 61 79 20 6e 65 76 65 72 20 ** may never
167c4 73 70 69 6c 6c 20 6f 76 65 72 20 6f 6e 74 6f 20 spill over onto
167c5 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
167c6 20 28 69 74 20 69 73 20 61 20 6d 61 78 69 6d 75 (it is a maximu
167c7 6d 20 6f 66 20 0a 20 20 20 20 2a 2a 20 31 33 20 m of . ** 13
167c8 62 79 74 65 73 20 69 6e 20 73 69 7a 65 29 2c 20 bytes in size),
167c9 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 63 65 73 it is not necces
167ca 73 61 72 79 20 74 6f 20 63 68 65 63 6b 20 74 68 sary to check th
167cb 65 20 72 65 74 75 72 6e 20 63 6f 64 65 2e 0a 20 e return code..
167cc 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d **. ** Sim
167cd 69 6c 61 72 6c 79 2c 20 74 68 65 20 69 6e 73 65 ilarly, the inse
167ce 72 74 43 65 6c 6c 28 29 20 66 75 6e 63 74 69 6f rtCell() functio
167cf 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 69 66 n cannot fail if
167d0 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 2a 2a the page. **
167d1 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 being inserted
167d2 69 6e 74 6f 20 69 73 20 61 6c 72 65 61 64 79 20 into is already
167d3 77 72 69 74 61 62 6c 65 20 61 6e 64 20 74 68 65 writable and the
167d4 20 63 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 0a cell does not .
167d5 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 ** contain a
167d6 6e 20 6f 76 65 72 66 6c 6f 77 20 70 6f 69 6e 74 n overflow point
167d7 65 72 2e 20 53 6f 20 69 67 6e 6f 72 65 20 74 68 er. So ignore th
167d8 69 73 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 is return code t
167d9 6f 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 oo.. */. a
167da 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 ssert( pPage->nC
167db 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 70 43 65 ell>0 );. pCe
167dc 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
167dd 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c age, pPage->nCel
167de 6c 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 l-1);. sqlite
167df 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 3BtreeParseCellP
167e0 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
167e1 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 66 69 6c &info);. fil
167e2 6c 49 6e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c lInCell(pParent,
167e3 20 70 61 72 65 6e 74 43 65 6c 6c 2c 20 30 2c 20 parentCell, 0,
167e4 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c info.nKey, 0, 0,
167e5 20 30 2c 20 26 70 61 72 65 6e 74 53 69 7a 65 29 0, &parentSize)
167e6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 ;. assert( pa
167e7 72 65 6e 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20 rentSize<64 );.
167e8 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
167e9 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
167ea 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 le(pParent->pDbP
167eb 61 67 65 29 20 29 3b 0a 20 20 20 20 69 6e 73 65 age) );. inse
167ec 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 rtCell(pParent,
167ed 70 61 72 65 6e 74 49 64 78 2c 20 70 61 72 65 6e parentIdx, paren
167ee 74 43 65 6c 6c 2c 20 70 61 72 65 6e 74 53 69 7a tCell, parentSiz
167ef 65 2c 20 30 2c 20 34 29 3b 0a 20 20 20 20 70 75 e, 0, 4);. pu
167f0 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 t4byte(findOverf
167f1 6c 6f 77 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c lowCell(pParent,
167f2 70 61 72 65 6e 74 49 64 78 29 2c 20 70 50 61 67 parentIdx), pPag
167f3 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75 e->pgno);. pu
167f4 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d t4byte(&pParent-
167f5 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e >aData[pParent->
167f6 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 hdrOffset+8], pg
167f7 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f noNew);. . /
167f8 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
167f9 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
167fa 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 base, update the
167fb 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 pointer map.
167fc 20 2a 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73 ** with entries
167fd 20 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 for the new pag
167fe 65 2c 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 e, and any point
167ff 65 72 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 er from the .
16800 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 ** cell on the
16801 70 61 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 page to an overf
16802 6c 6f 77 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f low page.. */
16803 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 . if( ISAUTOV
16804 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 ACUUM ){. r
16805 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 c = ptrmapPut(pB
16806 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d t, pgnoNew, PTRM
16807 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e AP_BTREE, pParen
16808 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 t->pgno);.
16809 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1680a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 K ){. rc
1680b 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 = ptrmapPutOvfl(
1680c 70 4e 65 77 2c 20 30 29 3b 0a 20 20 20 20 20 20 pNew, 0);.
1680d 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
1680e 52 65 6c 65 61 73 65 20 74 68 65 20 72 65 66 65 Release the refe
1680f 72 65 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 77 rence to the new
16810 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 page. */. re
16811 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 29 3b leasePage(pNew);
16812 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 . }.. /* At th
16813 69 73 20 70 6f 69 6e 74 20 74 68 65 20 70 50 61 is point the pPa
16814 67 65 2d 3e 6e 46 72 65 65 20 76 61 72 69 61 62 ge->nFree variab
16815 6c 65 20 69 73 20 6e 6f 74 20 73 65 74 20 63 6f le is not set co
16816 72 72 65 63 74 6c 79 20 77 69 74 68 0a 20 20 2a rrectly with. *
16817 2a 20 72 65 73 70 65 63 74 20 74 6f 20 74 68 65 * respect to the
16818 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 content of the
16819 70 61 67 65 20 28 62 65 63 61 75 73 65 20 69 74 page (because it
1681a 20 77 61 73 20 73 65 74 20 74 6f 20 30 20 62 79 was set to 0 by
1681b 20 0a 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c . ** insertCel
1681c 6c 29 2e 20 53 6f 20 63 61 6c 6c 20 73 71 6c 69 l). So call sqli
1681d 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 te3BtreeInitPage
1681e 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 () to make sure
1681f 69 74 20 69 73 0a 20 20 2a 2a 20 63 6f 72 72 65 it is. ** corre
16820 63 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 ct.. **. ** Th
16821 69 73 20 68 61 73 20 74 6f 20 62 65 20 64 6f 6e is has to be don
16822 65 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 e even if an err
16823 6f 72 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 or will be retur
16824 6e 65 64 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 ned. Normally, i
16825 66 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 f. ** an error
16826 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 72 occurs during tr
16827 65 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 ee balancing, th
16828 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 e contents of Me
16829 6d 50 61 67 65 20 61 72 65 0a 20 20 2a 2a 20 6e mPage are. ** n
1682a 6f 74 20 69 6d 70 6f 72 74 61 6e 74 2c 20 61 73 ot important, as
1682b 20 74 68 65 79 20 77 69 6c 6c 20 62 65 20 72 65 they will be re
1682c 63 61 6c 63 75 6c 61 74 65 64 20 77 68 65 6e 20 calculated when
1682d 74 68 65 20 70 61 67 65 20 69 73 20 72 6f 6c 6c the page is roll
1682e 65 64 0a 20 20 2a 2a 20 62 61 63 6b 2e 20 42 75 ed. ** back. Bu
1682f 74 20 68 65 72 65 2c 20 69 6e 20 62 61 6c 61 6e t here, in balan
16830 63 65 5f 71 75 69 63 6b 28 29 2c 20 69 74 20 69 ce_quick(), it i
16831 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 s possible that
16832 70 50 61 67 65 20 68 61 73 20 0a 20 20 2a 2a 20 pPage has . **
16833 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6d 61 72 not yet been mar
16834 6b 65 64 20 64 69 72 74 79 20 6f 72 20 77 72 69 ked dirty or wri
16835 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f tten into the jo
16836 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 urnal file. Ther
16837 65 66 6f 72 65 0a 20 20 2a 2a 20 69 74 20 77 69 efore. ** it wi
16838 6c 6c 20 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 ll not be rolled
16839 20 62 61 63 6b 20 61 6e 64 20 73 6f 20 69 74 20 back and so it
1683a 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 is important to
1683b 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20 make sure that.
1683c 20 2a 2a 20 74 68 65 20 70 61 67 65 20 64 61 74 ** the page dat
1683d 61 20 61 6e 64 20 63 6f 6e 74 65 6e 74 73 20 6f a and contents o
1683e 66 20 4d 65 6d 50 61 67 65 20 61 72 65 20 63 6f f MemPage are co
1683f 6e 73 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 nsistent.. */.
16840 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d pPage->isInit =
16841 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 0;. sqlite3Btr
16842 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 eeInitPage(pPage
16843 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
16844 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 ge->nOverflow==0
16845 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65 );.. /* If eve
16846 72 79 74 68 69 6e 67 20 65 6c 73 65 20 73 75 63 rything else suc
16847 63 65 65 64 65 64 2c 20 62 61 6c 61 6e 63 65 20 ceeded, balance
16848 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c the parent page,
16849 20 69 6e 20 0a 20 20 2a 2a 20 63 61 73 65 20 74 in . ** case t
1684a 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 he divider cell
1684b 69 6e 73 65 72 74 65 64 20 63 61 75 73 65 64 20 inserted caused
1684c 69 74 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 it to become ove
1684d 72 66 75 6c 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 rfull.. */. if
1684e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1684f 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 ){. releasePa
16850 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 ge(pPage);. p
16851 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 Cur->iPage--;.
16852 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 rc = balance(p
16853 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 Cur, 0);. }. r
16854 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
16855 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
16856 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a T_QUICKBALANCE *
16857 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f /../*.** This ro
16858 75 74 69 6e 65 20 72 65 64 69 73 74 72 69 62 75 utine redistribu
16859 74 65 73 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 tes Cells on pPa
1685a 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 4e 2a ge and up to NN*
1685b 32 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66 2 siblings.** of
1685c 20 70 50 61 67 65 20 73 6f 20 74 68 61 74 20 61 pPage so that a
1685d 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 61 62 ll pages have ab
1685e 6f 75 74 20 74 68 65 20 73 61 6d 65 20 61 6d 6f out the same amo
1685f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 unt of free spac
16860 65 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 4e 4e e..** Usually NN
16861 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74 siblings on eit
16862 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 her side of pPag
16863 65 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 e is used in the
16864 20 62 61 6c 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74 balancing,.** t
16865 68 6f 75 67 68 20 6d 6f 72 65 20 73 69 62 6c 69 hough more sibli
16866 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 ngs might come f
16867 72 6f 6d 20 6f 6e 65 20 73 69 64 65 20 69 66 20 rom one side if
16868 70 50 61 67 65 20 69 73 20 74 68 65 20 66 69 72 pPage is the fir
16869 73 74 0a 2a 2a 20 6f 72 20 6c 61 73 74 20 63 68 st.** or last ch
1686a 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65 6e ild of its paren
1686b 74 2e 20 20 49 66 20 70 50 61 67 65 20 68 61 73 t. If pPage has
1686c 20 66 65 77 65 72 20 74 68 61 6e 20 32 2a 4e 4e fewer than 2*NN
1686d 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f siblings.** (so
1686e 6d 65 74 68 69 6e 67 20 77 68 69 63 68 20 63 61 mething which ca
1686f 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 n only happen if
16870 20 70 50 61 67 65 20 69 73 20 74 68 65 20 72 6f pPage is the ro
16871 6f 74 20 70 61 67 65 20 6f 72 20 61 20 0a 2a 2a ot page or a .**
16872 20 63 68 69 6c 64 20 6f 66 20 72 6f 6f 74 29 20 child of root)
16873 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 then all availab
16874 6c 65 20 73 69 62 6c 69 6e 67 73 20 70 61 72 74 le siblings part
16875 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 icipate in the b
16876 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 alancing..**.**
16877 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 The number of si
16878 62 6c 69 6e 67 73 20 6f 66 20 70 50 61 67 65 20 blings of pPage
16879 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 might be increas
1687a 65 64 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 ed or decreased
1687b 62 79 20 6f 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f by one or.** two
1687c 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f in an effort to
1687d 20 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 72 keep pages near
1687e 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 ly full but not
1687f 6f 76 65 72 20 66 75 6c 6c 2e 20 54 68 65 20 72 over full. The r
16880 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 73 oot page.** is s
16881 70 65 63 69 61 6c 20 61 6e 64 20 69 73 20 61 6c pecial and is al
16882 6c 6f 77 65 64 20 74 6f 20 62 65 20 6e 65 61 72 lowed to be near
16883 6c 79 20 65 6d 70 74 79 2e 20 49 66 20 70 50 61 ly empty. If pPa
16884 67 65 20 69 73 20 0a 2a 2a 20 74 68 65 20 72 6f ge is .** the ro
16885 6f 74 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 ot page, then th
16886 65 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 74 e depth of the t
16887 72 65 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 ree might be inc
16888 72 65 61 73 65 64 0a 2a 2a 20 6f 72 20 64 65 63 reased.** or dec
16889 72 65 61 73 65 64 20 62 79 20 6f 6e 65 2c 20 61 reased by one, a
1688a 73 20 6e 65 63 65 73 73 61 72 79 2c 20 74 6f 20 s necessary, to
1688b 6b 65 65 70 20 74 68 65 20 72 6f 6f 74 20 70 61 keep the root pa
1688c 67 65 20 66 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a ge from being.**
1688d 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 63 6f 6d overfull or com
1688e 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 0a 2a pletely empty..*
1688f 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 *.** Note that w
16890 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
16891 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 is called, some
16892 20 6f 66 20 74 68 65 20 43 65 6c 6c 73 20 6f 6e of the Cells on
16893 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 pPage.** might
16894 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 not actually be
16895 73 74 6f 72 65 64 20 69 6e 20 70 50 61 67 65 2d stored in pPage-
16896 3e 61 44 61 74 61 5b 5d 2e 20 20 54 68 69 73 20 >aData[]. This
16897 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 can happen.** if
16898 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 76 65 the page is ove
16899 72 66 75 6c 6c 2e 20 20 50 61 72 74 20 6f 66 20 rfull. Part of
1689a 74 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 the job of this
1689b 72 6f 75 74 69 6e 65 20 69 73 20 74 6f 0a 2a 2a routine is to.**
1689c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 43 make sure all C
1689d 65 6c 6c 73 20 66 6f 72 20 70 50 61 67 65 20 6f ells for pPage o
1689e 6e 63 65 20 61 67 61 69 6e 20 66 69 74 20 69 6e nce again fit in
1689f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 2e pPage->aData[].
168a0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f .**.** In the co
168a1 75 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e urse of balancin
168a2 67 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 6f g the siblings o
168a3 66 20 70 50 61 67 65 2c 20 74 68 65 20 70 61 72 f pPage, the par
168a4 65 6e 74 20 6f 66 20 70 50 61 67 65 0a 2a 2a 20 ent of pPage.**
168a5 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 6f 76 65 might become ove
168a6 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 rfull or underfu
168a7 6c 6c 2e 20 20 49 66 20 74 68 61 74 20 68 61 70 ll. If that hap
168a8 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 69 73 20 pens, then this
168a9 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 routine.** is ca
168aa 6c 6c 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 lled recursively
168ab 20 6f 6e 20 74 68 65 20 70 61 72 65 6e 74 2e 0a on the parent..
168ac 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f **.** If this ro
168ad 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 utine fails for
168ae 61 6e 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d any reason, it m
168af 69 67 68 74 20 6c 65 61 76 65 20 74 68 65 20 64 ight leave the d
168b0 61 74 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 atabase.** in a
168b1 63 6f 72 72 75 70 74 65 64 20 73 74 61 74 65 2e corrupted state.
168b2 20 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 So if this rou
168b3 74 69 6e 65 20 66 61 69 6c 73 2c 20 74 68 65 20 tine fails, the
168b4 64 61 74 61 62 61 73 65 20 73 68 6f 75 6c 64 0a database should.
168b5 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 ** be rolled bac
168b6 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 k..*/.static int
168b7 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 balance_nonroot
168b8 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
168b9 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
168ba 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
168bb 20 2f 2a 20 54 68 65 20 6f 76 65 72 20 6f 72 20 /* The over or
168bc 75 6e 64 65 72 66 75 6c 6c 20 70 61 67 65 20 74 underfull page t
168bd 6f 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 4d o balance */. M
168be 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 3b emPage *pParent;
168bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
168c0 68 65 20 70 61 72 65 6e 74 20 6f 66 20 70 50 61 he parent of pPa
168c1 67 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 ge */. BtShared
168c2 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 *pBt;
168c3 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c /* The whol
168c4 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
168c5 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 int nCell = 0;
168c6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
168c7 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 Number of cells
168c8 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 in apCell[] */.
168c9 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d int nMaxCells =
168ca 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
168cb 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 Allocated size
168cc 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c of apCell, szCel
168cd 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 l, aFrom. */. i
168ce 6e 74 20 6e 4f 6c 64 20 3d 20 30 3b 20 20 20 20 nt nOld = 0;
168cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
168d0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
168d1 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 n apOld[] */. i
168d2 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 nt nNew = 0;
168d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
168d4 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
168d5 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 n apNew[] */. i
168d6 6e 74 20 6e 44 69 76 3b 20 20 20 20 20 20 20 20 nt nDiv;
168d7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
168d8 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 umber of cells i
168d9 6e 20 61 70 44 69 76 5b 5d 20 2a 2f 0a 20 20 69 n apDiv[] */. i
168da 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 nt i, j, k;
168db 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
168dc 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a oop counters */.
168dd 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 int idx;
168de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
168df 2a 20 49 6e 64 65 78 20 6f 66 20 70 50 61 67 65 * Index of pPage
168e0 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 in pParent->aCe
168e1 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 ll[] */. int nx
168e2 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 Div;
168e3 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 /* Next d
168e4 69 76 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70 ivider slot in p
168e5 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 Parent->aCell[]
168e6 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
168e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
168e8 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 /* The return
168e9 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 code */. int le
168ea 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20 afCorrection;
168eb 20 20 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70 /* 4 if p
168ec 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20 Page is a leaf.
168ed 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 0 if not */. i
168ee 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20 nt leafData;
168ef 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
168f0 72 75 65 20 69 66 20 70 50 61 67 65 20 69 73 20 rue if pPage is
168f1 61 20 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46 a leaf of a LEAF
168f2 44 41 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69 DATA tree */. i
168f3 6e 74 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20 nt usableSpace;
168f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
168f5 79 74 65 73 20 69 6e 20 70 50 61 67 65 20 62 65 ytes in pPage be
168f6 79 6f 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 yond the header
168f7 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61 */. int pageFla
168f8 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
168f9 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50 /* Value of pP
168fa 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f age->aData[0] */
168fb 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b . int subtotal;
168fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
168fd 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62 /* Subtotal of b
168fe 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e ytes in cells on
168ff 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 one page */. i
16900 6e 74 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20 nt iSpace1 = 0;
16901 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
16902 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 irst unused byte
16903 20 6f 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f of aSpace1[] */
16904 0a 20 20 69 6e 74 20 69 53 70 61 63 65 32 20 3d . int iSpace2 =
16905 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
16906 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 /* First unused
16907 62 79 74 65 20 6f 66 20 61 53 70 61 63 65 32 5b byte of aSpace2[
16908 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 ] */. int szScr
16909 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
1690a 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 /* Size of s
1690b 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 72 65 cratch memory re
1690c 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d quested */. Mem
1690d 50 61 67 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b Page *apOld[NB];
1690e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 /* pPa
1690f 67 65 20 61 6e 64 20 75 70 20 74 6f 20 74 77 6f ge and up to two
16910 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 50 siblings */. P
16911 67 6e 6f 20 70 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b gno pgnoOld[NB];
16912 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
16913 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 age numbers for
16914 65 61 63 68 20 70 61 67 65 20 69 6e 20 61 70 4f each page in apO
16915 6c 64 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ld[] */. MemPag
16916 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 e *apCopy[NB];
16917 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74 /* Privat
16918 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c e copies of apOl
16919 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d d[] pages */. M
1691a 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 emPage *apNew[NB
1691b 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 +2]; /* p
1691c 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e Page and up to N
1691d 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72 B siblings after
1691e 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 balancing */.
1691f 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 5b 4e 42 2b Pgno pgnoNew[NB+
16920 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2]; /*
16921 50 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 Page numbers for
16922 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 61 70 each page in ap
16923 4e 65 77 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 New[] */. u8 *a
16924 70 44 69 76 5b 4e 42 5d 3b 20 20 20 20 20 20 20 pDiv[NB];
16925 20 20 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 /* Divid
16926 65 72 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 er cells in pPar
16927 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 ent */. int cnt
16928 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 New[NB+2];
16929 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
1692a 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c n aCell[] of cel
1692b 6c 20 61 66 74 65 72 20 69 2d 74 68 20 70 61 67 l after i-th pag
1692c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 e */. int szNew
1692d 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 [NB+2];
1692e 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 /* Combined
1692f 73 69 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c size of cells pl
16930 61 63 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 ace on i-th page
16931 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c */. u8 **apCel
16932 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 l = 0;
16933 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 /* All cells
16934 62 65 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a begin balanced *
16935 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b /. u16 *szCell;
16936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16937 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f /* Local size o
16938 66 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 f all cells in a
16939 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 pCell[] */. u8
1693a 2a 61 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 *aCopy[NB];
1693b 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 /* Space for
1693c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 6f 66 holding data of
1693d 20 61 70 43 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75 apCopy[] */. u
1693e 38 20 2a 61 53 70 61 63 65 31 3b 20 20 20 20 20 8 *aSpace1;
1693f 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 /* Space f
16940 6f 72 20 63 6f 70 69 65 73 20 6f 66 20 64 69 76 or copies of div
16941 69 64 65 72 73 20 63 65 6c 6c 73 20 62 65 66 6f iders cells befo
16942 72 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 re balance */.
16943 75 38 20 2a 61 53 70 61 63 65 32 20 3d 20 30 3b u8 *aSpace2 = 0;
16944 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
16945 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 64 69 76 for overflow div
16946 69 64 65 72 73 20 63 65 6c 6c 73 20 61 66 74 65 iders cells afte
16947 72 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 r balance */. u
16948 38 20 2a 61 46 72 6f 6d 20 3d 20 30 3b 0a 0a 20 8 *aFrom = 0;..
16949 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
1694a 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
1694b 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 e];. assert( sq
1694c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1694d 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
1694e 65 78 29 20 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c ex) );. VVA_ONL
1694f 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68 Y( pCur->pagesSh
16950 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 0a 20 uffled = 1 );..
16951 20 2f 2a 20 0a 20 20 2a 2a 20 46 69 6e 64 20 74 /* . ** Find t
16952 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a he parent page..
16953 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
16954 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a Cur->iPage>0 );.
16955 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
16956 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 >isInit );. ass
16957 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
16958 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
16959 67 65 2d 3e 70 44 62 50 61 67 65 29 20 7c 7c 20 ge->pDbPage) ||
1695a 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
1695b 3d 3d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 ==1 );. pBt = p
1695c 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 70 50 61 Page->pBt;. pPa
1695d 72 65 6e 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 rent = pCur->apP
1695e 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d age[pCur->iPage-
1695f 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 1];. assert( pP
16960 61 72 65 6e 74 20 29 3b 0a 20 20 69 66 28 20 53 arent );. if( S
16961 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 QLITE_OK!=(rc =
16962 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
16963 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 e(pParent->pDbPa
16964 67 65 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f ge)) ){. goto
16965 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 balance_cleanup
16966 3b 0a 20 20 7d 0a 0a 20 20 54 52 41 43 45 28 28 ;. }.. TRACE((
16967 22 42 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20 "BALANCE: begin
16968 70 61 67 65 20 25 64 20 63 68 69 6c 64 20 6f 66 page %d child of
16969 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 %d\n", pPage->p
1696a 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 gno, pParent->pg
1696b 6e 6f 29 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 no));..#ifndef S
1696c 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b QLITE_OMIT_QUICK
1696d 42 41 4c 41 4e 43 45 0a 20 20 2f 2a 0a 20 20 2a BALANCE. /*. *
1696e 2a 20 41 20 73 70 65 63 69 61 6c 20 63 61 73 65 * A special case
1696f 3a 20 20 49 66 20 61 20 6e 65 77 20 65 6e 74 72 : If a new entr
16970 79 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 y has just been
16971 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 0a inserted into a.
16972 20 20 2a 2a 20 74 61 62 6c 65 20 28 74 68 61 74 ** table (that
16973 20 69 73 2c 20 61 20 62 74 72 65 65 20 77 69 74 is, a btree wit
16974 68 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 61 h integer keys a
16975 6e 64 20 61 6c 6c 20 64 61 74 61 20 61 74 20 74 nd all data at t
16976 68 65 20 6c 65 61 76 65 73 29 0a 20 20 2a 2a 20 he leaves). **
16977 61 6e 64 20 74 68 65 20 6e 65 77 20 65 6e 74 72 and the new entr
16978 79 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d y is the right-m
16979 6f 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ost entry in the
1697a 20 74 72 65 65 20 28 69 74 20 68 61 73 20 74 68 tree (it has th
1697b 65 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b e. ** largest k
1697c 65 79 29 20 74 68 65 6e 20 75 73 65 20 74 68 65 ey) then use the
1697d 20 73 70 65 63 69 61 6c 20 62 61 6c 61 6e 63 65 special balance
1697e 5f 71 75 69 63 6b 28 29 20 72 6f 75 74 69 6e 65 _quick() routine
1697f 20 66 6f 72 0a 20 20 2a 2a 20 62 61 6c 61 6e 63 for. ** balanc
16980 69 6e 67 2e 20 20 62 61 6c 61 6e 63 65 5f 71 75 ing. balance_qu
16981 69 63 6b 28 29 20 69 73 20 6d 75 63 68 20 66 61 ick() is much fa
16982 73 74 65 72 20 61 6e 64 20 72 65 73 75 6c 74 73 ster and results
16983 20 69 6e 20 61 20 74 69 67 68 74 65 72 0a 20 20 in a tighter.
16984 2a 2a 20 70 61 63 6b 69 6e 67 20 6f 66 20 64 61 ** packing of da
16985 74 61 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e ta in the common
16986 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 case.. */. if
16987 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 26 26 ( pPage->leaf &&
16988 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69 6e . pPage->in
16989 74 4b 65 79 20 26 26 0a 20 20 20 20 20 20 70 50 tKey &&. pP
1698a 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
1698b 31 20 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 1 &&. pPage
1698c 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d ->aOvfl[0].idx==
1698d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 0a pPage->nCell &&.
1698e 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 pParent->p
1698f 67 6e 6f 21 3d 31 20 26 26 0a 20 20 20 20 20 20 gno!=1 &&.
16990 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e get4byte(&pParen
16991 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 t->aData[pParent
16992 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d ->hdrOffset+8])=
16993 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 0a 20 20 29 =pPage->pgno. )
16994 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
16995 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 age->intKey );.
16996 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 4f 44 /*. ** TOD
16997 4f 3a 20 43 68 65 63 6b 20 74 68 65 20 73 69 62 O: Check the sib
16998 6c 69 6e 67 73 20 74 6f 20 74 68 65 20 6c 65 66 lings to the lef
16999 74 20 6f 66 20 70 50 61 67 65 2e 20 49 74 20 6d t of pPage. It m
1699a 61 79 20 62 65 20 74 68 61 74 0a 20 20 20 20 2a ay be that. *
1699b 2a 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 66 * they are not f
1699c 75 6c 6c 20 61 6e 64 20 6e 6f 20 6e 65 77 20 70 ull and no new p
1699d 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 2e age is required.
1699e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 . */. retu
1699f 72 6e 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b rn balance_quick
169a0 28 70 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 (pCur);. }.#end
169a1 69 66 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 if.. if( SQLITE
169a2 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
169a3 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
169a4 67 65 2d 3e 70 44 62 50 61 67 65 29 29 20 29 7b ge->pDbPage)) ){
169a5 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 . goto balanc
169a6 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a e_cleanup;. }..
169a7 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 /*. ** Find t
169a8 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 he cell in the p
169a9 61 72 65 6e 74 20 70 61 67 65 20 77 68 6f 73 65 arent page whose
169aa 20 6c 65 66 74 20 63 68 69 6c 64 20 70 6f 69 6e left child poin
169ab 74 73 20 62 61 63 6b 0a 20 20 2a 2a 20 74 6f 20 ts back. ** to
169ac 70 50 61 67 65 2e 20 20 54 68 65 20 22 69 64 78 pPage. The "idx
169ad 22 20 76 61 72 69 61 62 6c 65 20 69 73 20 74 68 " variable is th
169ae 65 20 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20 e index of that
169af 63 65 6c 6c 2e 20 20 49 66 20 70 50 61 67 65 0a cell. If pPage.
169b0 20 20 2a 2a 20 69 73 20 74 68 65 20 72 69 67 68 ** is the righ
169b1 74 6d 6f 73 74 20 63 68 69 6c 64 20 6f 66 20 70 tmost child of p
169b2 50 61 72 65 6e 74 20 74 68 65 6e 20 73 65 74 20 Parent then set
169b3 69 64 78 20 74 6f 20 70 50 61 72 65 6e 74 2d 3e idx to pParent->
169b4 6e 43 65 6c 6c 20 0a 20 20 2a 2f 0a 20 20 69 64 nCell . */. id
169b5 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b x = pCur->aiIdx[
169b6 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a pCur->iPage-1];.
169b7 20 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e assertParentIn
169b8 64 65 78 28 70 50 61 72 65 6e 74 2c 20 69 64 78 dex(pParent, idx
169b9 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a , pPage->pgno);.
169ba 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 . /*. ** Find
169bb 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f sibling pages to
169bc 20 70 50 61 67 65 20 61 6e 64 20 74 68 65 20 63 pPage and the c
169bd 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 ells in pParent
169be 74 68 61 74 20 64 69 76 69 64 65 0a 20 20 2a 2a that divide. **
169bf 20 74 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 20 the siblings.
169c0 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 An attempt is ma
169c1 64 65 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 de to find NN si
169c2 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 blings on either
169c3 0a 20 20 2a 2a 20 73 69 64 65 20 6f 66 20 70 50 . ** side of pP
169c4 61 67 65 2e 20 20 4d 6f 72 65 20 73 69 62 6c 69 age. More sibli
169c5 6e 67 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 ngs are taken fr
169c6 6f 6d 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 om one side, how
169c7 65 76 65 72 2c 20 69 66 0a 20 20 2a 2a 20 70 50 ever, if. ** pP
169c8 61 67 65 20 74 68 65 72 65 20 61 72 65 20 66 65 age there are fe
169c9 77 65 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c wer than NN sibl
169ca 69 6e 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 ings on the othe
169cb 72 20 73 69 64 65 2e 20 20 49 66 20 70 50 61 72 r side. If pPar
169cc 65 6e 74 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 ent. ** has NB
169cd 6f 72 20 66 65 77 65 72 20 63 68 69 6c 64 72 65 or fewer childre
169ce 6e 20 74 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 n then all child
169cf 72 65 6e 20 6f 66 20 70 50 61 72 65 6e 74 20 61 ren of pParent a
169d0 72 65 20 74 61 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 re taken.. */.
169d1 20 6e 78 44 69 76 20 3d 20 69 64 78 20 2d 20 4e nxDiv = idx - N
169d2 4e 3b 0a 20 20 69 66 28 20 6e 78 44 69 76 20 2b N;. if( nxDiv +
169d3 20 4e 42 20 3e 20 70 50 61 72 65 6e 74 2d 3e 6e NB > pParent->n
169d4 43 65 6c 6c 20 29 7b 0a 20 20 20 20 6e 78 44 69 Cell ){. nxDi
169d5 76 20 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 v = pParent->nCe
169d6 6c 6c 20 2d 20 4e 42 20 2b 20 31 3b 0a 20 20 7d ll - NB + 1;. }
169d7 0a 20 20 69 66 28 20 6e 78 44 69 76 3c 30 20 29 . if( nxDiv<0 )
169d8 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b {. nxDiv = 0;
169d9 0a 20 20 7d 0a 20 20 6e 44 69 76 20 3d 20 30 3b . }. nDiv = 0;
169da 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6b 3d 6e 78 . for(i=0, k=nx
169db 44 69 76 3b 20 69 3c 4e 42 3b 20 69 2b 2b 2c 20 Div; i<NB; i++,
169dc 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6b 3c k++){. if( k<
169dd 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 pParent->nCell )
169de 7b 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d {. apDiv[i]
169df 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 = findCell(pPar
169e0 65 6e 74 2c 20 6b 29 3b 0a 20 20 20 20 20 20 6e ent, k);. n
169e1 44 69 76 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 Div++;. ass
169e2 65 72 74 28 20 21 70 50 61 72 65 6e 74 2d 3e 6c ert( !pParent->l
169e3 65 61 66 20 29 3b 0a 20 20 20 20 20 20 70 67 6e eaf );. pgn
169e4 6f 4f 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 oOld[i] = get4by
169e5 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 te(apDiv[i]);.
169e6 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3d 3d 70 }else if( k==p
169e7 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b Parent->nCell ){
169e8 0a 20 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 . pgnoOld[i
169e9 5d 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 ] = get4byte(&pP
169ea 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 arent->aData[pPa
169eb 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b rent->hdrOffset+
169ec 38 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 8]);. }else{.
169ed 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
169ee 20 7d 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 }. rc = getA
169ef 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 ndInitPage(pBt,
169f0 70 67 6e 6f 4f 6c 64 5b 69 5d 2c 20 26 61 70 4f pgnoOld[i], &apO
169f1 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 ld[i]);. if(
169f2 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 rc ) goto balanc
169f3 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 2f e_cleanup;. /
169f4 2a 20 61 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 * apOld[i]->idxP
169f5 61 72 65 6e 74 20 3d 20 6b 3b 20 2a 2f 0a 20 20 arent = k; */.
169f6 20 20 61 70 43 6f 70 79 5b 69 5d 20 3d 20 30 3b apCopy[i] = 0;
169f7 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d . assert( i==
169f8 6e 4f 6c 64 20 29 3b 0a 20 20 20 20 6e 4f 6c 64 nOld );. nOld
169f9 2b 2b 3b 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c ++;. nMaxCell
169fa 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d s += 1+apOld[i]-
169fb 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d >nCell+apOld[i]-
169fc 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a >nOverflow;. }.
169fd 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 . /* Make nMaxC
169fe 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 ells a multiple
169ff 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f of 4 in order to
16a00 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 preserve 8-byte
16a01 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 . ** alignment
16a02 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d */. nMaxCells =
16a03 20 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 (nMaxCells + 3)
16a04 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 &~3;.. /*. **
16a05 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 Allocate space f
16a06 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 or memory struct
16a07 75 72 65 73 0a 20 20 2a 2f 0a 20 20 73 7a 53 63 ures. */. szSc
16a08 72 61 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e ratch =. n
16a09 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 MaxCells*sizeof(
16a0a 75 38 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 u8*)
16a0b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 /* ap
16a0c 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e Cell */. + n
16a0d 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 MaxCells*sizeof(
16a0e 75 31 36 29 20 20 20 20 20 20 20 20 20 20 20 20 u16)
16a0f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a /* sz
16a10 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 28 Cell */. + (
16a11 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 ROUND8(sizeof(Me
16a12 6d 50 61 67 65 29 29 2b 70 42 74 2d 3e 70 61 67 mPage))+pBt->pag
16a13 65 53 69 7a 65 29 2a 4e 42 20 20 2f 2a 20 61 43 eSize)*NB /* aC
16a14 6f 70 79 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 opy */. + pB
16a15 74 2d 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 t->pageSize
16a16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a17 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 /* aSp
16a18 61 63 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 28 ace1 */. + (
16a19 49 53 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 6e ISAUTOVACUUM ? n
16a1a 4d 61 78 43 65 6c 6c 73 20 3a 20 30 29 3b 20 20 MaxCells : 0);
16a1b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 46 /* aF
16a1c 72 6f 6d 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 rom */. apCell
16a1d 3d 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 = sqlite3Scratch
16a1e 4d 61 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 Malloc( szScratc
16a1f 68 20 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 h ); . if( apCe
16a20 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 ll==0 ){. rc
16a21 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
16a22 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 goto balance
16a23 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 _cleanup;. }.
16a24 73 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 szCell = (u16*)&
16a25 61 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 apCell[nMaxCells
16a26 5d 3b 0a 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20 ];. aCopy[0] =
16a27 28 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 (u8*)&szCell[nMa
16a28 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 xCells];. asser
16a29 74 28 20 28 28 61 43 6f 70 79 5b 30 5d 20 2d 20 t( ((aCopy[0] -
16a2a 28 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 (u8*)0) & 7)==0
16a2b 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 ); /* 8-byte ali
16a2c 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 64 20 gnment required
16a2d 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c */. for(i=1; i<
16a2e 4e 42 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 43 NB; i++){. aC
16a2f 6f 70 79 5b 69 5d 20 3d 20 26 61 43 6f 70 79 5b opy[i] = &aCopy[
16a30 69 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 i-1][pBt->pageSi
16a31 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 ze+ROUND8(sizeof
16a32 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 20 (MemPage))];.
16a33 20 61 73 73 65 72 74 28 20 28 28 61 43 6f 70 79 assert( ((aCopy
16a34 5b 69 5d 20 2d 20 28 75 38 2a 29 30 29 20 26 20 [i] - (u8*)0) &
16a35 37 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 7)==0 ); /* 8-by
16a36 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 te alignment req
16a37 75 69 72 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 uired */. }. a
16a38 53 70 61 63 65 31 20 3d 20 26 61 43 6f 70 79 5b Space1 = &aCopy[
16a39 4e 42 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 NB-1][pBt->pageS
16a3a 69 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f ize+ROUND8(sizeo
16a3b 66 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 f(MemPage))];.
16a3c 61 73 73 65 72 74 28 20 28 28 61 53 70 61 63 65 assert( ((aSpace
16a3d 31 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 1 - (u8*)0) & 7)
16a3e 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 ==0 ); /* 8-byte
16a3f 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 alignment requi
16a40 72 65 64 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 red */. if( ISA
16a41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
16a42 20 61 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 65 aFrom = &aSpace
16a43 31 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 1[pBt->pageSize]
16a44 3b 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 32 20 ;. }. aSpace2
16a45 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c = sqlite3PageMal
16a46 6c 6f 63 28 70 42 74 2d 3e 70 61 67 65 53 69 7a loc(pBt->pageSiz
16a47 65 29 3b 0a 20 20 69 66 28 20 61 53 70 61 63 65 e);. if( aSpace
16a48 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 2==0 ){. rc =
16a49 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
16a4a 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f goto balance_
16a4b 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 0a cleanup;. }. .
16a4c 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63 /*. ** Make c
16a4d 6f 70 69 65 73 20 6f 66 20 74 68 65 20 63 6f 6e opies of the con
16a4e 74 65 6e 74 20 6f 66 20 70 50 61 67 65 20 61 6e tent of pPage an
16a4f 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20 69 d its siblings i
16a50 6e 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a nto aOld[].. **
16a51 20 54 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 The rest of thi
16a52 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 s function will
16a53 75 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 use data from th
16a54 65 20 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a e copies rather.
16a55 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72 ** that the or
16a56 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e iginal pages sin
16a57 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 ce the original
16a58 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e pages will be in
16a59 20 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 the. ** proces
16a5a 73 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 s of being overw
16a5b 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66 ritten.. */. f
16a5c 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 or(i=0; i<nOld;
16a5d 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 i++){. MemPag
16a5e 65 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d e *p = apCopy[i]
16a5f 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f = (MemPage*)aCo
16a60 70 79 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 py[i];. memcp
16a61 79 28 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 y(p, apOld[i], s
16a62 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b izeof(MemPage));
16a63 0a 20 20 20 20 70 2d 3e 61 44 61 74 61 20 3d 20 . p->aData =
16a64 28 76 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20 (void*)&p[1];.
16a65 20 20 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61 74 memcpy(p->aDat
16a66 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 a, apOld[i]->aDa
16a67 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a ta, pBt->pageSiz
16a68 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 e);. }.. /*.
16a69 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 ** Load pointers
16a6a 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e to all cells on
16a6b 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 sibling pages a
16a6c 6e 64 20 74 68 65 20 64 69 76 69 64 65 72 20 63 nd the divider c
16a6d 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 ells. ** into t
16a6e 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b he local apCell[
16a6f 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 ] array. Make c
16a70 6f 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 opies of the div
16a71 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 ider cells. **
16a72 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 into space obtai
16a73 6e 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65 31 ned form aSpace1
16a74 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 [] and remove th
16a75 65 20 74 68 65 20 64 69 76 69 64 65 72 20 43 65 e the divider Ce
16a76 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 lls. ** from pP
16a77 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a arent.. **. **
16a78 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 If the siblings
16a79 20 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 are on leaf pag
16a7a 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 es, then the chi
16a7b 6c 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 ld pointers of t
16a7c 68 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 he. ** divider
16a7d 63 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 cells are stripp
16a7e 65 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c ed from the cell
16a7f 73 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 s before they ar
16a80 65 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e e copied. ** in
16a81 74 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 to aSpace1[]. I
16a82 6e 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 n this way, all
16a83 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b cells in apCell[
16a84 5d 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 ] are without.
16a85 2a 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 ** child pointer
16a86 73 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 s. If siblings
16a87 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 are not leaves,
16a88 74 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e then all cell in
16a89 0a 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 . ** apCell[] i
16a8a 6e 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 nclude child poi
16a8b 6e 74 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 nters. Either w
16a8c 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e ay, all cells in
16a8d 20 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 apCell[]. ** a
16a8e 72 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 re alike.. **.
16a8f 20 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 ** leafCorrecti
16a90 6f 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 on: 4 if pPage
16a91 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 is a leaf. 0 if
16a92 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 pPage is not a
16a93 6c 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 leaf.. **
16a94 20 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 leafData: 1 if
16a95 20 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 pPage holds key
16a96 2b 64 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e +data and pParen
16a97 74 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 t holds only key
16a98 73 2e 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 s.. */. nCell
16a99 3d 20 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 = 0;. leafCorre
16a9a 63 74 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c ction = pPage->l
16a9b 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 eaf*4;. leafDat
16a9c 61 20 3d 20 70 50 61 67 65 2d 3e 68 61 73 44 61 a = pPage->hasDa
16a9d 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 ta;. for(i=0; i
16a9e 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nOld; i++){.
16a9f 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d MemPage *pOld =
16aa0 20 61 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 apCopy[i];.
16aa1 69 6e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 int limit = pOld
16aa2 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f ->nCell+pOld->nO
16aa3 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 verflow;. for
16aa4 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a (j=0; j<limit; j
16aa5 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ++){. asser
16aa6 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c t( nCell<nMaxCel
16aa7 6c 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43 65 ls );. apCe
16aa8 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 ll[nCell] = find
16aa9 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c OverflowCell(pOl
16aaa 64 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 d, j);. szC
16aab 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c ell[nCell] = cel
16aac 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 lSizePtr(pOld, a
16aad 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 pCell[nCell]);.
16aae 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 if( ISAUTOV
16aaf 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 ACUUM ){.
16ab0 20 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 20 int a;.
16ab1 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 28 aFrom[nCell] = (
16ab2 75 38 29 69 3b 20 20 20 61 73 73 65 72 74 28 20 u8)i; assert(
16ab3 69 3e 3d 30 20 26 26 20 69 3c 36 20 29 3b 0a 20 i>=0 && i<6 );.
16ab4 20 20 20 20 20 20 20 66 6f 72 28 61 3d 30 3b 20 for(a=0;
16ab5 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f a<pOld->nOverflo
16ab6 77 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 20 w; a++){.
16ab7 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f 76 if( pOld->aOv
16ab8 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 43 fl[a].pCell==apC
16ab9 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 20 ell[nCell] ){.
16aba 20 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b aFrom[
16abb 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 nCell] = 0xFF;.
16abc 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
16abd 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
16abe 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
16abf 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 nCell++;.
16ac0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e }. if( i<n
16ac1 4f 6c 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 75 Old-1 ){. u
16ac2 31 36 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 16 sz = cellSize
16ac3 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 Ptr(pParent, apD
16ac4 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 iv[i]);. if
16ac5 28 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 ( leafData ){.
16ac6 20 20 20 20 20 20 2f 2a 20 57 69 74 68 20 74 68 /* With th
16ac7 65 20 4c 45 41 46 44 41 54 41 20 66 6c 61 67 2c e LEAFDATA flag,
16ac8 20 70 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 68 pParent cells h
16ac9 6f 6c 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 73 old only INTKEYs
16aca 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a that. **
16acb 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 20 are duplicates
16acc 6f 66 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 63 of keys on the c
16acd 68 69 6c 64 20 70 61 67 65 73 2e 20 20 57 65 20 hild pages. We
16ace 6e 65 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a 20 need to remove.
16acf 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 69 ** the di
16ad0 76 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d vider cells from
16ad1 20 70 50 61 72 65 6e 74 2c 20 62 75 74 20 74 68 pParent, but th
16ad2 65 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 e dividers cells
16ad3 20 61 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 20 are not.
16ad4 20 2a 2a 20 61 64 64 65 64 20 74 6f 20 61 70 43 ** added to apC
16ad5 65 6c 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 68 ell[] because th
16ad6 65 79 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 ey are duplicate
16ad7 73 20 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c 73 s of child cells
16ad8 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
16ad9 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 dropCell(pP
16ada 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a arent, nxDiv, sz
16adb 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
16adc 20 20 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d u8 *pTem
16add 70 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 p;. asser
16ade 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c t( nCell<nMaxCel
16adf 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a ls );. sz
16ae0 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a Cell[nCell] = sz
16ae1 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 20 ;. pTemp
16ae2 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 = &aSpace1[iSpac
16ae3 65 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 70 e1];. iSp
16ae4 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 ace1 += sz;.
16ae5 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d assert( sz<=
16ae6 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 pBt->pageSize/4
16ae7 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
16ae8 74 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d t( iSpace1<=pBt-
16ae9 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 >pageSize );.
16aea 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d memcpy(pTem
16aeb 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 p, apDiv[i], sz)
16aec 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c ;. apCell
16aed 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b [nCell] = pTemp+
16aee 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a leafCorrection;.
16aef 20 20 20 20 20 20 20 20 69 66 28 20 49 53 41 55 if( ISAU
16af0 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
16af1 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c aFrom[nCel
16af2 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 20 l] = 0xFF;.
16af3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 72 6f }. dro
16af4 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e pCell(pParent, n
16af5 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20 xDiv, sz);.
16af6 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 assert( leafC
16af7 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 orrection==0 ||
16af8 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d leafCorrection==
16af9 34 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 4 );. szC
16afa 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 28 75 ell[nCell] -= (u
16afb 31 36 29 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 16)leafCorrectio
16afc 6e 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 n;. asser
16afd 74 28 20 67 65 74 34 62 79 74 65 28 70 54 65 6d t( get4byte(pTem
16afe 70 29 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20 29 p)==pgnoOld[i] )
16aff 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 ;. if( !p
16b00 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 Old->leaf ){.
16b01 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c assert( l
16b02 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 eafCorrection==0
16b03 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a );. /*
16b04 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e 74 The right point
16b05 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 er of the child
16b06 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65 page pOld become
16b07 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20 20 s the left.
16b08 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 ** pointer
16b09 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63 of the divider c
16b0a 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 ell */.
16b0b 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e memcpy(apCell[n
16b0c 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 Cell], &pOld->aD
16b0d 61 74 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 ata[pOld->hdrOff
16b0e 73 65 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 set+8], 4);.
16b0f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
16b10 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 assert( lea
16b11 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 fCorrection==4 )
16b12 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
16b13 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 szCell[nCell]<4
16b14 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f ){. /
16b15 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 * Do not allow a
16b16 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 ny cells smaller
16b17 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20 2a than 4 bytes. *
16b18 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 7a /. sz
16b19 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b Cell[nCell] = 4;
16b1a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
16b1b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e }. n
16b1c 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a Cell++;. }.
16b1d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a }. }.. /*.
16b1e 20 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 ** Figure out
16b1f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
16b20 67 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68 6f ges needed to ho
16b21 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c ld all nCell cel
16b22 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 74 ls.. ** Store t
16b23 68 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 6b his number in "k
16b24 22 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 65 ". Also compute
16b25 20 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 szNew[] which i
16b26 73 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a 2a s the total. **
16b27 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c size of all cel
16b28 6c 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 70 ls on the i-th p
16b29 61 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b 5d age and cntNew[]
16b2a 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 6e which is the in
16b2b 64 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65 dex. ** in apCe
16b2c 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c ll[] of the cell
16b2d 20 74 68 61 74 20 64 69 76 69 64 65 73 20 70 61 that divides pa
16b2e 67 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20 69 ge i from page i
16b2f 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 +1. . ** cntNe
16b30 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 61 w[k] should equa
16b31 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 l nCell.. **.
16b32 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 74 ** Values comput
16b33 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 6b ed by this block
16b34 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 :. **. **
16b35 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f 74 k: The tot
16b36 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 al number of sib
16b37 6c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a 20 ling pages. **
16b38 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 szNew[i]: Spa
16b39 63 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65 20 ced used on the
16b3a 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 i-th sibling pag
16b3b 65 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 77 e.. ** cntNew
16b3c 5b 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 70 [i]: Index in ap
16b3d 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 6c Cell[] and szCel
16b3e 6c 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 l[] for the firs
16b3f 74 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 t cell to. **
16b40 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 the
16b41 72 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d 74 right of the i-t
16b42 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a h sibling page..
16b43 20 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 65 ** usableSpace
16b44 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 : Number of byte
16b45 73 20 6f 66 20 73 70 61 63 65 20 61 76 61 69 6c s of space avail
16b46 61 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 62 able on each sib
16b47 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f ling.. ** . */
16b48 0a 20 20 75 73 61 62 6c 65 53 70 61 63 65 20 3d . usableSpace =
16b49 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
16b4a 20 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 72 - 12 + leafCorr
16b4b 65 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 ection;. for(su
16b4c 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c btotal=k=i=0; i<
16b4d 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 nCell; i++){.
16b4e 20 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 43 assert( i<nMaxC
16b4f 65 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 74 ells );. subt
16b50 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 otal += szCell[i
16b51 5d 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 73 ] + 2;. if( s
16b52 75 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65 ubtotal > usable
16b53 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20 73 Space ){. s
16b54 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 zNew[k] = subtot
16b55 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a al - szCell[i];.
16b56 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 cntNew[k]
16b57 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20 6c = i;. if( l
16b58 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20 eafData ){ i--;
16b59 7d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61 6c }. subtotal
16b5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b = 0;. k++;
16b5b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e . }. }. szN
16b5c 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c ew[k] = subtotal
16b5d 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 ;. cntNew[k] =
16b5e 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 nCell;. k++;..
16b5f 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63 /*. ** The pac
16b60 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79 king computed by
16b61 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c the previous bl
16b62 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f ock is biased to
16b63 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67 ward the sibling
16b64 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 s. ** on the le
16b65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65 ft side. The le
16b66 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 ft siblings are
16b67 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75 always nearly fu
16b68 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20 ll, while the.
16b69 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 ** right-most si
16b6a 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e bling might be n
16b6b 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 early empty. Th
16b6c 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 is block of code
16b6d 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74 attempts. ** t
16b6e 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70 61 63 o adjust the pac
16b6f 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73 king of siblings
16b70 20 74 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 to get a better
16b71 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 balance.. **.
16b72 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d ** This adjustm
16b73 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e ent is more than
16b74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e an optimization
16b75 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61 . The packing a
16b76 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20 bove might. **
16b77 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c be so out of bal
16b78 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c ance as to be il
16b79 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d legal. For exam
16b7a 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d ple, the right-m
16b7b 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67 ost. ** sibling
16b7c 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65 might be comple
16b7d 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 tely empty. Thi
16b7e 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 s adjustment is
16b7f 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 not optional..
16b80 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 */. for(i=k-1;
16b81 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 i>0; i--){. i
16b82 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e nt szRight = szN
16b83 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 ew[i]; /* Size
16b84 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 of sibling on th
16b85 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69 e right */. i
16b86 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 nt szLeft = szNe
16b87 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 w[i-1]; /* Size
16b88 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 of sibling on th
16b89 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e e left */. in
16b8a 74 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 t r;
16b8b 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 /* Index of ri
16b8c 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e ght-most cell in
16b8d 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f left sibling */
16b8e 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 . int d;
16b8f 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
16b90 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 x of first cell
16b91 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 to the left of r
16b92 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a ight sibling */.
16b93 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b . r = cntNew[
16b94 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20 i-1] - 1;. d
16b95 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 = r + 1 - leafDa
16b96 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ta;. assert(
16b97 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 d<nMaxCells );.
16b98 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61 assert( r<nMa
16b99 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68 xCells );. wh
16b9a 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20 ile( szRight==0
16b9b 7c 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 6c || szRight+szCel
16b9c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28 l[d]+2<=szLeft-(
16b9d 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a szCell[r]+2) ){.
16b9e 20 20 20 20 20 20 73 7a 52 69 67 68 74 20 2b 3d szRight +=
16b9f 20 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a szCell[d] + 2;.
16ba0 20 20 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d 20 szLeft -=
16ba1 73 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20 szCell[r] + 2;.
16ba2 20 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 5d cntNew[i-1]
16ba3 2d 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 6e --;. r = cn
16ba4 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 tNew[i-1] - 1;.
16ba5 20 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d d = r + 1 -
16ba6 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 7d leafData;. }
16ba7 0a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 . szNew[i] =
16ba8 73 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 7a 4e szRight;. szN
16ba9 65 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 ew[i-1] = szLeft
16baa 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 68 ;. }.. /* Eith
16bab 65 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20 er we found one
16bac 6f 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63 or more cells (c
16bad 6e 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20 ntnew[0])>0) or
16bae 77 65 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20 we are the. **
16baf 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 a virtual root p
16bb0 61 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20 age. A virtual
16bb1 72 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68 65 root page is whe
16bb2 6e 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0a n the real root.
16bb3 20 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67 ** page is pag
16bb4 65 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 74 e 1 and we are t
16bb5 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66 he only child of
16bb6 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a 2f that page.. */
16bb7 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e 65 . assert( cntNe
16bb8 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 65 w[0]>0 || (pPare
16bb9 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 70 nt->pgno==1 && p
16bba 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 Parent->nCell==0
16bbb 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ) );.. /*. **
16bbc 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 Allocate k new p
16bbd 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 ages. Reuse old
16bbe 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f 73 pages where pos
16bbf 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 sible.. */. as
16bc0 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e sert( pPage->pgn
16bc1 6f 3e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c 61 o>1 );. pageFla
16bc2 67 73 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 gs = pPage->aDat
16bc3 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b a[0];. for(i=0;
16bc4 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<k; i++){.
16bc5 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 MemPage *pNew;.
16bc6 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b if( i<nOld ){
16bc7 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 . pNew = ap
16bc8 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 New[i] = apOld[i
16bc9 5d 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 ];. pgnoNew
16bca 5b 69 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69 5d [i] = pgnoOld[i]
16bcb 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d ;. apOld[i]
16bcc 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d = 0;. rc =
16bcd 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
16bce 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 te(pNew->pDbPage
16bcf 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b );. nNew++;
16bd0 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
16bd1 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
16bd2 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b anup;. }else{
16bd3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
16bd4 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d >0 );. rc =
16bd5 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
16bd6 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26 ge(pBt, &pNew, &
16bd7 70 67 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e 6f pgnoNew[i], pgno
16bd8 4e 65 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20 20 New[i-1], 0);.
16bd9 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
16bda 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
16bdb 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 p;. apNew[i
16bdc 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 ] = pNew;.
16bdd 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 nNew++;. }.
16bde 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 }.. /* Free any
16bdf 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74 20 old pages that
16be0 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64 20 were not reused
16be1 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 as new pages..
16be2 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f */. while( i<nO
16be3 6c 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 ld ){. rc = f
16be4 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d reePage(apOld[i]
16be5 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
16be6 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
16be7 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 73 anup;. releas
16be8 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b ePage(apOld[i]);
16be9 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 . apOld[i] =
16bea 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0;. i++;. }.
16beb 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74 . /*. ** Put t
16bec 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e 20 he new pages in
16bed 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e accending order.
16bee 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f 0a This helps to.
16bef 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 65 ** keep entrie
16bf0 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69 s in the disk fi
16bf1 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 74 le in order so t
16bf2 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20 hat a scan. **
16bf3 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 of the table is
16bf4 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68 a linear scan th
16bf5 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e 20 rough the file.
16bf6 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75 That. ** in tu
16bf7 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70 65 rn helps the ope
16bf8 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74 6f rating system to
16bf9 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a 20 deliver pages.
16bfa 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 73 ** from the dis
16bfb 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a k more rapidly..
16bfc 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e **. ** An O(n
16bfd 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f ^2) insertion so
16bfe 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 rt algorithm is
16bff 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65 0a used, but since.
16c00 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 20 ** n is never
16c01 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61 20 more than NB (a
16c02 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c small constant),
16c03 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a that should. *
16c04 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c * not be a probl
16c05 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 em.. **. ** Wh
16c06 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f en NB==3, this o
16c07 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 ne optimization
16c08 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 61 makes the databa
16c09 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 35 se. ** about 25
16c0a 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 72 % faster for lar
16c0b 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e ge insertions an
16c0c 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a d deletions.. *
16c0d 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b /. for(i=0; i<k
16c0e 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e -1; i++){. in
16c0f 74 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 t minV = pgnoNew
16c10 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e [i];. int min
16c11 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a I = i;. for(j
16c12 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b =i+1; j<k; j++){
16c13 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 4e . if( pgnoN
16c14 65 77 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64 29 ew[j]<(unsigned)
16c15 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 minV ){.
16c16 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 minI = j;.
16c17 20 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 minV = pgnoNew
16c18 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 [j];. }.
16c19 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 3e }. if( minI>
16c1a 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 i ){. int t
16c1b 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 ;. MemPage
16c1c 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 70 *pT;. t = p
16c1d 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 gnoNew[i];.
16c1e 20 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a pT = apNew[i];.
16c1f 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d pgnoNew[i]
16c20 20 3d 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d = pgnoNew[minI]
16c21 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d ;. apNew[i]
16c22 20 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b 0a = apNew[minI];.
16c23 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d 69 pgnoNew[mi
16c24 6e 49 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20 61 nI] = t;. a
16c25 70 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b pNew[minI] = pT;
16c26 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 . }. }. TRA
16c27 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c CE(("BALANCE: ol
16c28 64 3a 20 25 64 20 25 64 20 25 64 20 20 6e 65 77 d: %d %d %d new
16c29 3a 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 : %d(%d) %d(%d)
16c2a 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 %d(%d) %d(%d) %d
16c2b 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67 6e (%d)\n",. pgn
16c2c 6f 4f 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e 4f oOld[0], . nO
16c2d 6c 64 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64 5b ld>=2 ? pgnoOld[
16c2e 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 1] : 0,. nOld
16c2f 3e 3d 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32 5d >=3 ? pgnoOld[2]
16c30 20 3a 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e 65 : 0,. pgnoNe
16c31 77 5b 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a w[0], szNew[0],.
16c32 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70 67 nNew>=2 ? pg
16c33 6e 6f 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e 4e noNew[1] : 0, nN
16c34 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d ew>=2 ? szNew[1]
16c35 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d : 0,. nNew>=
16c36 33 20 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20 3a 3 ? pgnoNew[2] :
16c37 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a 0, nNew>=3 ? sz
16c38 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20 New[2] : 0,.
16c39 6e 4e 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e 65 nNew>=4 ? pgnoNe
16c3a 77 5b 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d w[3] : 0, nNew>=
16c3b 34 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30 4 ? szNew[3] : 0
16c3c 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20 ,. nNew>=5 ?
16c3d 70 67 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c 20 pgnoNew[4] : 0,
16c3e 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b nNew>=5 ? szNew[
16c3f 34 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a 0a 4] : 0));.. /*.
16c40 20 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 ** Evenly dist
16c41 72 69 62 75 74 65 20 74 68 65 20 64 61 74 61 20 ribute the data
16c42 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f in apCell[] acro
16c43 73 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 ss the new pages
16c44 2e 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 .. ** Insert di
16c45 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f vider cells into
16c46 20 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 pParent as nece
16c47 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 ssary.. */. j
16c48 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 = 0;. for(i=0;
16c49 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 i<nNew; i++){.
16c4a 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 /* Assemble th
16c4b 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 e new sibling pa
16c4c 67 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 ge. */. MemPa
16c4d 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 ge *pNew = apNew
16c4e 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 [i];. assert(
16c4f 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a j<nMaxCells );.
16c50 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 assert( pNew
16c51 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77 5b ->pgno==pgnoNew[
16c52 69 5d 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 i] );. zeroPa
16c53 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c 61 ge(pNew, pageFla
16c54 67 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c gs);. assembl
16c55 65 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e ePage(pNew, cntN
16c56 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c ew[i]-j, &apCell
16c57 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 [j], &szCell[j])
16c58 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e ;. assert( pN
16c59 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 ew->nCell>0 || (
16c5a 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65 nNew==1 && cntNe
16c5b 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 20 w[0]==0) );.
16c5c 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f assert( pNew->nO
16c5d 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20 verflow==0 );..
16c5e 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 /* If this is
16c5f 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 an auto-vacuum
16c60 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 database, update
16c61 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
16c62 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 entries. **
16c63 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 that point to th
16c64 65 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20 e siblings that
16c65 77 65 72 65 20 72 65 61 72 72 61 6e 67 65 64 2e were rearranged.
16c66 20 54 68 65 73 65 20 63 61 6e 20 62 65 3a 20 6c These can be: l
16c67 65 66 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 eft. ** child
16c68 72 65 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74 68 ren of cells, th
16c69 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 e right-child of
16c6a 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 6f 76 the page, or ov
16c6b 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 erflow pages.
16c6c 20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 ** pointed to b
16c6d 79 20 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f 0a y cells.. */.
16c6e 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
16c6f 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 66 6f CUUM ){. fo
16c70 72 28 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b r(k=j; k<cntNew[
16c71 69 5d 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 i]; k++){.
16c72 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 78 assert( k<nMax
16c73 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 Cells );.
16c74 20 69 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30 if( aFrom[k]==0
16c75 78 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 46 xFF || apCopy[aF
16c76 72 6f 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70 rom[k]]->pgno!=p
16c77 4e 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 New->pgno ){.
16c78 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d rc = ptrm
16c79 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c 20 apPutOvfl(pNew,
16c7a 6b 2d 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 k-j);.
16c7b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16c7c 4b 20 26 26 20 6c 65 61 66 43 6f 72 72 65 63 74 K && leafCorrect
16c7d 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ion==0 ){.
16c7e 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
16c7f 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62 79 pPut(pBt, get4by
16c80 74 65 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20 50 te(apCell[k]), P
16c81 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 TRMAP_BTREE, pNe
16c82 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 w->pgno);.
16c83 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
16c84 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16c85 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
16c86 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
16c87 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 20 eanup;.
16c88 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
16c89 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
16c8a 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a j = cntNew[i];..
16c8b 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 69 /* If the si
16c8c 62 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65 6d bling page assem
16c8d 62 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20 6e bled above was n
16c8e 6f 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 ot the right-mos
16c8f 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 2a t sibling,. *
16c90 2a 20 69 6e 73 65 72 74 20 61 20 64 69 76 69 64 * insert a divid
16c91 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 er cell into the
16c92 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 parent page..
16c93 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c 6e */. if( i<n
16c94 4e 65 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c 6c New-1 && j<nCell
16c95 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 ){. u8 *pC
16c96 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 ell;. u8 *p
16c97 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 Temp;. int
16c98 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 sz;.. asser
16c99 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 t( j<nMaxCells )
16c9a 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 ;. pCell =
16c9b 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 apCell[j];.
16c9c 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20 sz = szCell[j]
16c9d 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e + leafCorrection
16c9e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 ;. pTemp =
16c9f 26 61 53 70 61 63 65 32 5b 69 53 70 61 63 65 32 &aSpace2[iSpace2
16ca0 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e ];. if( !pN
16ca1 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ew->leaf ){.
16ca2 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 memcpy(&pNew
16ca3 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 6c ->aData[8], pCel
16ca4 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 69 l, 4);. i
16ca5 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
16ca6 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 61 46 . && (aF
16ca7 72 6f 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c 20 rom[j]==0xFF ||
16ca8 61 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d 5d apCopy[aFrom[j]]
16ca9 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 ->pgno!=pNew->pg
16caa 6e 6f 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 no). ){.
16cab 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 rc = pt
16cac 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 rmapPut(pBt, get
16cad 34 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 50 54 4byte(pCell), PT
16cae 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 RMAP_BTREE, pNew
16caf 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
16cb0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
16cb1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
16cb2 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 goto balance
16cb3 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 _cleanup;.
16cb4 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
16cb5 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
16cb6 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 leafData ){.
16cb7 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 /* If the tr
16cb8 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 ee is a leaf-dat
16cb9 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 a tree, and the
16cba 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 siblings are lea
16cbb 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a ves, . **
16cbc 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e then there is n
16cbd 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 o divider cell i
16cbe 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 n apCell[]. Inst
16cbf 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 ead, the divider
16cc0 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c . ** cel
16cc1 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 l consists of th
16cc2 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f e integer key fo
16cc3 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 r the right-most
16cc4 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 cell of .
16cc5 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 ** the sibling
16cc6 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 -page assembled
16cc7 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 above only..
16cc8 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 */. C
16cc9 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
16cca 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 j--;.
16ccb 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 sqlite3BtreeP
16ccc 61 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 77 arseCellPtr(pNew
16ccd 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e , apCell[j], &in
16cce 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43 65 fo);. pCe
16ccf 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 ll = pTemp;.
16cd0 20 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 rc = fillInC
16cd1 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 43 65 ell(pParent, pCe
16cd2 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79 ll, 0, info.nKey
16cd3 2c 20 30 2c 20 30 2c 20 30 2c 20 26 73 7a 29 3b , 0, 0, 0, &sz);
16cd4 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
16cd5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16cd6 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c goto bal
16cd7 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
16cd8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
16cd9 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 pTemp = 0;.
16cda 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
16cdb 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 pCell -= 4;.
16cdc 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 /* Obscure c
16cdd 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 ase for non-leaf
16cde 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 -data trees: If
16cdf 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c the cell at pCel
16ce0 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a l was. **
16ce1 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 previously stor
16ce2 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 ed on a leaf nod
16ce3 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 e, and its repor
16ce4 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 ted size was 4.
16ce5 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c ** bytes,
16ce6 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 then it may act
16ce7 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 ually be smaller
16ce8 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 than this .
16ce9 20 20 20 20 2a 2a 20 28 73 65 65 20 73 71 6c 69 ** (see sqli
16cea 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
16ceb 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65 73 20 lPtr(), 4 bytes
16cec 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 73 is the minimum s
16ced 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20 2a ize of. *
16cee 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75 74 * any cell). But
16cef 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 it is important
16cf0 20 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f 72 to pass the cor
16cf1 72 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20 20 rect size to .
16cf2 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 43 ** insertC
16cf3 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72 73 ell(), so repars
16cf4 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a e the cell now..
16cf5 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
16cf6 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 ** Note that
16cf7 74 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20 68 this can never h
16cf8 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c 69 appen in an SQLi
16cf9 74 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61 73 te data file, as
16cfa 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 all. **
16cfb 63 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65 61 cells are at lea
16cfc 73 74 20 34 20 62 79 74 65 73 2e 20 49 74 20 6f st 4 bytes. It o
16cfd 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20 62 nly happens in b
16cfe 2d 74 72 65 65 73 20 75 73 65 64 0a 20 20 20 20 -trees used.
16cff 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75 61 ** to evalua
16d00 74 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e te "IN (SELECT .
16d01 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61 72 ..)" and similar
16d02 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20 20 clauses..
16d03 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 */. if(
16d04 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b szCell[j]==4 ){
16d05 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
16d06 74 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e t(leafCorrection
16d07 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ==4);.
16d08 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 sz = cellSizePtr
16d09 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29 (pParent, pCell)
16d0a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
16d0b 20 20 7d 0a 20 20 20 20 20 20 69 53 70 61 63 65 }. iSpace
16d0c 32 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 2 += sz;. a
16d0d 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e ssert( sz<=pBt->
16d0e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 20 pageSize/4 );.
16d0f 20 20 20 20 61 73 73 65 72 74 28 20 69 53 70 61 assert( iSpa
16d10 63 65 32 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 ce2<=pBt->pageSi
16d11 7a 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d ze );. rc =
16d12 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 insertCell(pPar
16d13 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c ent, nxDiv, pCel
16d14 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 34 29 l, sz, pTemp, 4)
16d15 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
16d16 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
16d17 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 balance_cleanup
16d18 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
16d19 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
16d1a 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d iteable(pParent-
16d1b 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 >pDbPage) );.
16d1c 20 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 put4byte(find
16d1d 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 OverflowCell(pPa
16d1e 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e 65 rent,nxDiv), pNe
16d1f 77 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 20 w->pgno);..
16d20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
16d21 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 n auto-vacuum da
16d22 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74 20 tabase, and not
16d23 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 a leaf-data tree
16d24 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 ,. ** then
16d25 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 update the point
16d26 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20 65 er map with an e
16d27 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65 ntry for the ove
16d28 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 rflow page.
16d29 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65 6c ** that the cel
16d2a 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20 l just inserted
16d2b 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61 6e points to (if an
16d2c 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 y).. */.
16d2d 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
16d2e 55 55 4d 20 26 26 20 21 6c 65 61 66 44 61 74 61 UUM && !leafData
16d2f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
16d30 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 ptrmapPutOvfl(p
16d31 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b 0a Parent, nxDiv);.
16d32 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
16d33 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16d34 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 goto bala
16d35 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
16d36 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
16d37 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 j++;.
16d38 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a 0a nxDiv++;. }..
16d39 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 70 /* Set the p
16d3a 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 ointer-map entry
16d3b 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 62 for the new sib
16d3c 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 20 ling page. */.
16d3d 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
16d3e 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d UM ){. rc =
16d3f 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
16d40 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d pNew->pgno, PTRM
16d41 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e AP_BTREE, pParen
16d42 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 t->pgno);.
16d43 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16d44 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
16d45 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
16d46 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d p;. }. }
16d47 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a . }. assert( j
16d48 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 ==nCell );. ass
16d49 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 ert( nOld>0 );.
16d4a 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 20 assert( nNew>0
16d4b 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 6c );. if( (pageFl
16d4c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 3d ags & PTF_LEAF)=
16d4d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 =0 ){. u8 *zC
16d4e 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b 6e hild = &apCopy[n
16d4f 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d Old-1]->aData[8]
16d50 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 70 ;. memcpy(&ap
16d51 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 New[nNew-1]->aDa
16d52 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 ta[8], zChild, 4
16d53 29 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 );. if( ISAUT
16d54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 OVACUUM ){.
16d55 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 rc = ptrmapPut(
16d56 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 7a 43 pBt, get4byte(zC
16d57 68 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f 42 54 hild), PTRMAP_BT
16d58 52 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d REE, apNew[nNew-
16d59 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 1]->pgno);.
16d5a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16d5b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
16d5c 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
16d5d 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 up;. }.
16d5e 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 }. }. assert(
16d5f 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
16d60 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d iteable(pParent-
16d61 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 >pDbPage) );. i
16d62 66 28 20 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e f( nxDiv==pParen
16d63 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65 6e 74 t->nCell+pParent
16d64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 ->nOverflow ){.
16d65 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 /* Right-most
16d66 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 sibling is the
16d67 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 right-most child
16d68 20 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 of pParent */.
16d69 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 put4byte(&pPa
16d6a 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 rent->aData[pPar
16d6b 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 ent->hdrOffset+8
16d6c 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d ], pgnoNew[nNew-
16d6d 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 1]);. }else{.
16d6e 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 /* Right-most
16d6f 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 6c sibling is the l
16d70 65 66 74 20 63 68 69 6c 64 20 6f 66 20 74 68 65 eft child of the
16d71 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 first entry in
16d72 70 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a 20 70 pParent. ** p
16d73 61 73 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f ast the right-mo
16d74 73 74 20 64 69 76 69 64 65 72 20 65 6e 74 72 79 st divider entry
16d75 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65 */. put4byte
16d76 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c (findOverflowCel
16d77 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 l(pParent, nxDiv
16d78 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d ), pgnoNew[nNew-
16d79 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 1]);. }.. /*.
16d7a 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68 65 20 ** Balance the
16d7b 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20 4e 6f parent page. No
16d7c 74 65 20 74 68 61 74 20 74 68 65 20 63 75 72 72 te that the curr
16d7d 65 6e 74 20 70 61 67 65 20 28 70 50 61 67 65 29 ent page (pPage)
16d7e 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61 76 65 might. ** have
16d7f 20 62 65 65 6e 20 61 64 64 65 64 20 74 6f 20 74 been added to t
16d80 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f 20 69 he freelist so i
16d81 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e 67 65 t might no longe
16d82 72 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 r be initialized
16d83 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 70 .. ** But the p
16d84 61 72 65 6e 74 20 70 61 67 65 20 77 69 6c 6c 20 arent page will
16d85 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74 69 61 always be initia
16d86 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 lized.. */. as
16d87 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 69 sert( pParent->i
16d88 73 49 6e 69 74 20 29 3b 0a 20 20 73 71 6c 69 74 sInit );. sqlit
16d89 65 33 53 63 72 61 74 63 68 46 72 65 65 28 61 70 e3ScratchFree(ap
16d8a 43 65 6c 6c 29 3b 0a 20 20 61 70 43 65 6c 6c 20 Cell);. apCell
16d8b 3d 20 30 3b 0a 20 20 54 52 41 43 45 28 28 22 42 = 0;. TRACE(("B
16d8c 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 64 ALANCE: finished
16d8d 20 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d 25 64 with %d: old=%d
16d8e 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 new=%d cells=%d
16d8f 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 \n",. p
16d90 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c 64 Page->pgno, nOld
16d91 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b , nNew, nCell));
16d92 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 . pPage->nOverf
16d93 6c 6f 77 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61 low = 0;. relea
16d94 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
16d95 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a pCur->iPage--;.
16d96 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 rc = balance(p
16d97 43 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a Cur, 0);. . /*
16d98 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65 . ** Cleanup be
16d99 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a fore returning..
16d9a 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 */.balance_cle
16d9b 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 50 anup:. sqlite3P
16d9c 61 67 65 46 72 65 65 28 61 53 70 61 63 65 32 29 ageFree(aSpace2)
16d9d 3b 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 74 ;. sqlite3Scrat
16d9e 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a chFree(apCell);.
16d9f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c for(i=0; i<nOl
16da0 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c d; i++){. rel
16da1 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 easePage(apOld[i
16da2 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d ]);. }. for(i=
16da3 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0; i<nNew; i++){
16da4 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
16da5 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a (apNew[i]);. }.
16da6 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 pCur->apPage[p
16da7 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 Cur->iPage]->nOv
16da8 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 72 erflow = 0;.. r
16da9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
16daa 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
16dab 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 68 is called for th
16dac 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 e root page of a
16dad 20 62 74 72 65 65 20 77 68 65 6e 20 74 68 65 20 btree when the
16dae 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e root.** page con
16daf 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 tains no cells.
16db0 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 70 6f This is an oppo
16db1 72 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 rtunity to make
16db2 74 68 65 20 74 72 65 65 0a 2a 2a 20 73 68 61 6c the tree.** shal
16db3 6c 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c 65 76 lower by one lev
16db4 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e el..*/.static in
16db5 74 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f t balance_shallo
16db6 77 65 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 wer(BtCursor *pC
16db7 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a ur){. MemPage *
16db8 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 pPage;
16db9 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 /* Root page
16dba 20 6f 66 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 of B-Tree */.
16dbb 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b MemPage *pChild;
16dbc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16dbd 54 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 70 The only child p
16dbe 61 67 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a age of pPage */.
16dbf 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 Pgno pgnoChild
16dc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
16dc1 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f * Page number fo
16dc2 72 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69 6e r pChild */. in
16dc3 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
16dc4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 ; /* Re
16dc5 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 turn code from s
16dc6 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a ubprocedures */.
16dc7 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b BtShared *pBt;
16dc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16dc9 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42 54 /* The main BT
16dca 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f ree structure */
16dcb 0a 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65 72 . int mxCellPer
16dcc 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
16dcd 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 /* Maximum numbe
16dce 72 20 6f 66 20 63 65 6c 6c 73 20 70 65 72 20 70 r of cells per p
16dcf 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 age */. u8 **ap
16dd0 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Cell;
16dd1 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c /* All cel
16dd2 6c 73 20 66 72 6f 6d 20 70 61 67 65 73 20 62 65 ls from pages be
16dd3 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a ing balanced */.
16dd4 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 u16 *szCell;
16dd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16dd6 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 * Local size of
16dd7 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20 20 all cells */..
16dd8 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 assert( pCur->iP
16dd9 61 67 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67 age==0 );. pPag
16dda 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
16ddb 5b 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 [0];.. assert(
16ddc 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 pPage->nCell==0
16ddd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
16dde 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
16ddf 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
16de0 78 29 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 x) );. pBt = pP
16de1 61 67 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 43 65 age->pBt;. mxCe
16de2 6c 6c 50 65 72 50 61 67 65 20 3d 20 4d 58 5f 43 llPerPage = MX_C
16de3 45 4c 4c 28 70 42 74 29 3b 0a 20 20 61 70 43 65 ELL(pBt);. apCe
16de4 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ll = sqlite3Mall
16de5 6f 63 28 20 6d 78 43 65 6c 6c 50 65 72 50 61 67 oc( mxCellPerPag
16de6 65 2a 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b 73 e*(sizeof(u8*)+s
16de7 69 7a 65 6f 66 28 75 31 36 29 29 20 29 3b 0a 20 izeof(u16)) );.
16de8 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 if( apCell==0 )
16de9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
16dea 4f 4d 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20 3d OMEM;. szCell =
16deb 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6d (u16*)&apCell[m
16dec 78 43 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a 20 xCellPerPage];.
16ded 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 if( pPage->leaf
16dee 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 ){. /* The t
16def 61 62 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74 65 able is complete
16df0 6c 79 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 ly empty */.
16df1 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a TRACE(("BALANCE:
16df2 20 65 6d 70 74 79 20 74 61 62 6c 65 20 25 64 5c empty table %d\
16df3 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 n", pPage->pgno)
16df4 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
16df5 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 /* The root page
16df6 20 69 73 20 65 6d 70 74 79 20 62 75 74 20 68 61 is empty but ha
16df7 73 20 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54 72 s one child. Tr
16df8 61 6e 73 66 65 72 20 74 68 65 0a 20 20 20 20 2a ansfer the. *
16df9 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 * information fr
16dfa 6f 6d 20 74 68 61 74 20 6f 6e 65 20 63 68 69 6c om that one chil
16dfb 64 20 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74 20 d into the root
16dfc 70 61 67 65 20 69 66 20 69 74 20 0a 20 20 20 20 page if it .
16dfd 2a 2a 20 77 69 6c 6c 20 66 69 74 2e 20 20 54 68 ** will fit. Th
16dfe 69 73 20 72 65 64 75 63 65 73 20 74 68 65 20 64 is reduces the d
16dff 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 epth of the tree
16e00 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a by one.. **.
16e01 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 72 6f ** If the ro
16e02 6f 74 20 70 61 67 65 20 69 73 20 70 61 67 65 20 ot page is page
16e03 31 2c 20 69 74 20 68 61 73 20 6c 65 73 73 20 73 1, it has less s
16e04 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 74 pace available t
16e05 68 61 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20 63 han. ** its c
16e06 68 69 6c 64 20 28 64 75 65 20 74 6f 20 74 68 65 hild (due to the
16e07 20 31 30 30 20 62 79 74 65 20 68 65 61 64 65 72 100 byte header
16e08 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 20 that occurs at
16e09 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 the beginning.
16e0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 ** of the data
16e0b 62 61 73 65 20 66 6c 65 29 2c 20 73 6f 20 69 74 base fle), so it
16e0c 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 62 might not be ab
16e0d 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6f le to hold all o
16e0e 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 69 6e f the . ** in
16e0f 66 6f 72 6d 61 74 69 6f 6e 20 63 75 72 72 65 6e formation curren
16e10 74 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e tly contained in
16e11 20 74 68 65 20 63 68 69 6c 64 2e 20 20 49 66 20 the child. If
16e12 74 68 69 73 20 69 73 20 74 68 65 20 0a 20 20 20 this is the .
16e13 20 2a 2a 20 63 61 73 65 2c 20 74 68 65 6e 20 64 ** case, then d
16e14 6f 20 6e 6f 74 20 64 6f 20 74 68 65 20 74 72 61 o not do the tra
16e15 6e 73 66 65 72 2e 20 20 4c 65 61 76 65 20 70 61 nsfer. Leave pa
16e16 67 65 20 31 20 65 6d 70 74 79 20 65 78 63 65 70 ge 1 empty excep
16e17 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 t. ** for the
16e18 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 74 right-pointer t
16e19 6f 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 o the child page
16e1a 2e 20 20 54 68 65 20 63 68 69 6c 64 20 70 61 67 . The child pag
16e1b 65 20 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a 2a e becomes. **
16e1c 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f 6f the virtual roo
16e1d 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 20 t of the tree..
16e1e 20 20 20 2a 2f 0a 20 20 20 20 56 56 41 5f 4f 4e */. VVA_ON
16e1f 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 LY( pCur->pagesS
16e20 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 20 huffled = 1 );.
16e21 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 67 pgnoChild = g
16e22 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
16e23 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
16e24 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
16e25 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c assert( pgnoChil
16e26 64 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 d>0 );. asser
16e27 74 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 70 61 t( pgnoChild<=pa
16e28 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 gerPagecount(pPa
16e29 67 65 2d 3e 70 42 74 29 20 29 3b 0a 20 20 20 20 ge->pBt) );.
16e2a 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
16e2b 65 47 65 74 50 61 67 65 28 70 50 61 67 65 2d 3e eGetPage(pPage->
16e2c 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 pBt, pgnoChild,
16e2d 26 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20 &pChild, 0);.
16e2e 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 if( rc ) goto e
16e2f 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e nd_shallow_balan
16e30 63 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ce;. if( pPag
16e31 65 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 e->pgno==1 ){.
16e32 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16e33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 43 BtreeInitPage(pC
16e34 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 69 66 28 hild);. if(
16e35 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73 rc ) goto end_s
16e36 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a hallow_balance;.
16e37 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
16e38 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d hild->nOverflow=
16e39 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 =0 );. if(
16e3a 70 43 68 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d 31 pChild->nFree>=1
16e3b 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 00 ){. /*
16e3c 20 54 68 65 20 63 68 69 6c 64 20 69 6e 66 6f 72 The child infor
16e3d 6d 61 74 69 6f 6e 20 77 69 6c 6c 20 66 69 74 20 mation will fit
16e3e 6f 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 on the root page
16e3f 2c 20 73 6f 20 64 6f 20 74 68 65 0a 20 20 20 20 , so do the.
16e40 20 20 20 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a 20 ** copy */.
16e41 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 int i;.
16e42 20 20 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 zeroPage(p
16e43 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 Page, pChild->aD
16e44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 ata[0]);.
16e45 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 69 for(i=0; i<pChi
16e46 6c 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b ld->nCell; i++){
16e47 0a 20 20 20 20 20 20 20 20 20 20 61 70 43 65 6c . apCel
16e48 6c 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 l[i] = findCell(
16e49 70 43 68 69 6c 64 2c 69 29 3b 0a 20 20 20 20 20 pChild,i);.
16e4a 20 20 20 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 3d szCell[i] =
16e4b 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 43 68 cellSizePtr(pCh
16e4c 69 6c 64 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 3b ild, apCell[i]);
16e4d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
16e4e 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 assemblePage(
16e4f 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 6e pPage, pChild->n
16e50 43 65 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 7a Cell, apCell, sz
16e51 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 2f Cell);. /
16e52 2a 20 43 6f 70 79 20 74 68 65 20 72 69 67 68 74 * Copy the right
16e53 2d 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 -pointer of the
16e54 63 68 69 6c 64 20 74 6f 20 74 68 65 20 70 61 72 child to the par
16e55 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ent. */.
16e56 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
16e57 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
16e58 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
16e59 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 );. put4b
16e5a 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
16e5b 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
16e5c 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 20 20 et+8], .
16e5d 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70 43 get4byte(&pC
16e5e 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 68 69 hild->aData[pChi
16e5f 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ld->hdrOffset+8]
16e60 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ));. rc =
16e61 20 66 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 freePage(pChild
16e62 29 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 );. TRACE
16e63 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c (("BALANCE: chil
16e64 64 20 25 64 20 74 72 61 6e 73 66 65 72 20 74 6f d %d transfer to
16e65 20 70 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 69 page 1\n", pChi
16e66 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 ld->pgno));.
16e67 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
16e68 20 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 68 61 /* The child ha
16e69 73 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 s more informati
16e6a 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 on that will fit
16e6b 20 6f 6e 20 74 68 65 20 72 6f 6f 74 2e 0a 20 20 on the root..
16e6c 20 20 20 20 20 20 2a 2a 20 54 68 65 20 74 72 65 ** The tre
16e6d 65 20 69 73 20 61 6c 72 65 61 64 79 20 62 61 6c e is already bal
16e6e 61 6e 63 65 64 2e 20 20 44 6f 20 6e 6f 74 68 69 anced. Do nothi
16e6f 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 54 ng. */. T
16e70 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 RACE(("BALANCE:
16e71 63 68 69 6c 64 20 25 64 20 77 69 6c 6c 20 6e 6f child %d will no
16e72 74 20 66 69 74 20 6f 6e 20 70 61 67 65 20 31 5c t fit on page 1\
16e73 6e 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f n", pChild->pgno
16e74 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
16e75 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
16e76 63 70 79 28 70 50 61 67 65 2d 3e 61 44 61 74 61 cpy(pPage->aData
16e77 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 2c , pChild->aData,
16e78 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 pPage->pBt->usa
16e79 62 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 bleSize);.
16e7a 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
16e7b 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 0;. rc = sq
16e7c 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 lite3BtreeInitPa
16e7d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ge(pPage);.
16e7e 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
16e7f 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 ITE_OK );.
16e80 66 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 freePage(pChild)
16e81 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 ;. TRACE(("
16e82 42 41 4c 41 4e 43 45 3a 20 74 72 61 6e 73 66 65 BALANCE: transfe
16e83 72 20 63 68 69 6c 64 20 25 64 20 69 6e 74 6f 20 r child %d into
16e84 72 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 20 20 root %d\n",.
16e85 20 20 20 20 20 20 20 20 20 20 70 43 68 69 6c 64 pChild
16e86 2d 3e 70 67 6e 6f 2c 20 70 50 61 67 65 2d 3e 70 ->pgno, pPage->p
16e87 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 gno));. }.
16e88 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
16e89 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a nOverflow==0 );.
16e8a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
16e8b 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
16e8c 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
16e8d 55 55 4d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 UUM && rc==SQLIT
16e8e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 E_OK ){. rc
16e8f 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 = setChildPtrma
16e90 70 73 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d ps(pPage);. }
16e91 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 6c 65 .#endif. rele
16e92 61 73 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b asePage(pChild);
16e93 0a 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c 6f 77 . }.end_shallow
16e94 5f 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71 6c 69 _balance:. sqli
16e95 74 65 33 5f 66 72 65 65 28 61 70 43 65 6c 6c 29 te3_free(apCell)
16e96 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
16e97 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f .../*.** The roo
16e98 74 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 t page is overfu
16e99 6c 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 ll.**.** When th
16e9a 69 73 20 68 61 70 70 65 6e 73 2c 20 43 72 65 61 is happens, Crea
16e9b 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 te a new child p
16e9c 61 67 65 20 61 6e 64 20 63 6f 70 79 20 74 68 65 age and copy the
16e9d 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 .** contents of
16e9e 74 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20 74 68 the root into th
16e9f 65 20 63 68 69 6c 64 2e 20 20 54 68 65 6e 20 6d e child. Then m
16ea0 61 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 ake the root.**
16ea1 70 61 67 65 20 61 6e 20 65 6d 70 74 79 20 70 61 page an empty pa
16ea2 67 65 20 77 69 74 68 20 72 69 67 68 74 43 68 69 ge with rightChi
16ea3 6c 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 ld pointing to t
16ea4 68 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c 64 2e he new.** child.
16ea5 20 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61 6c 6c Finally, call
16ea6 20 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72 6e 61 balance_interna
16ea7 6c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 63 l() on the new c
16ea8 68 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75 73 65 hild.** to cause
16ea9 20 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a 2a 2f it to split..*/
16eaa 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 .static int bala
16eab 6e 63 65 5f 64 65 65 70 65 72 28 42 74 43 75 72 nce_deeper(BtCur
16eac 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e sor *pCur){. in
16ead 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
16eae 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 /* Return valu
16eaf 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 e from subproced
16eb0 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ures */. MemPag
16eb1 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 2f 2a e *pPage; /*
16eb2 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
16eb3 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 4d root page */. M
16eb4 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 emPage *pChild;
16eb5 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
16eb6 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 a new child pag
16eb7 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
16eb8 43 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20 50 61 Child; /* Pa
16eb9 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
16eba 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 new child page
16ebb 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 */. BtShared *p
16ebc 42 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 Bt; /* T
16ebd 68 65 20 42 54 72 65 65 20 2a 2f 0a 20 20 69 6e he BTree */. in
16ebe 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 t usableSize;
16ebf 20 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61 62 6c /* Total usabl
16ec0 65 20 73 69 7a 65 20 6f 66 20 61 20 70 61 67 65 e size of a page
16ec1 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 */. u8 *data;
16ec2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e /* Con
16ec3 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 65 tent of the pare
16ec4 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 nt page */. u8
16ec5 2a 63 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 *cdata;
16ec6 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 /* Content of t
16ec7 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f he child page */
16ec8 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 . int hdr;
16ec9 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
16eca 20 74 6f 20 70 61 67 65 20 68 65 61 64 65 72 20 to page header
16ecb 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 69 in parent */. i
16ecc 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20 nt cbrk;
16ecd 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 /* Offset to
16ece 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 content of first
16ecf 20 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e 74 20 cell in parent
16ed0 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 */.. assert( pC
16ed1 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 3b 0a ur->iPage==0 );.
16ed2 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16ed3 61 70 50 61 67 65 5b 30 5d 2d 3e 6e 4f 76 65 72 apPage[0]->nOver
16ed4 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 56 56 41 flow>0 );.. VVA
16ed5 5f 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 _ONLY( pCur->pag
16ed6 65 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 esShuffled = 1 )
16ed7 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 ;. pPage = pCur
16ed8 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 70 ->apPage[0];. p
16ed9 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b Bt = pPage->pBt;
16eda 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16edb 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
16edc 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
16edd 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
16ede 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
16edf 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
16ee0 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 ;. rc = allocat
16ee1 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
16ee2 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43 68 &pChild, &pgnoCh
16ee3 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f ild, pPage->pgno
16ee4 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 , 0);. if( rc )
16ee5 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 return rc;. as
16ee6 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
16ee7 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 erIswriteable(pC
16ee8 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 hild->pDbPage) )
16ee9 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d ;. usableSize =
16eea 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
16eeb 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 ;. data = pPage
16eec 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d ->aData;. hdr =
16eed 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
16eee 74 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32 t;. cbrk = get2
16eef 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
16ef0 5d 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70 43 ]);. cdata = pC
16ef1 68 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20 6d hild->aData;. m
16ef2 65 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64 61 emcpy(cdata, &da
16ef3 74 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d 3e ta[hdr], pPage->
16ef4 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50 61 cellOffset+2*pPa
16ef5 67 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b 0a ge->nCell-hdr);.
16ef6 20 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61 5b memcpy(&cdata[
16ef7 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72 cbrk], &data[cbr
16ef8 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 k], usableSize-c
16ef9 62 72 6b 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 brk);.. assert(
16efa 20 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69 74 3d pChild->isInit=
16efb 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c =0 );. rc = sql
16efc 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
16efd 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 e(pChild);. if(
16efe 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16eff 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20 {. int nCopy
16f00 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c = pPage->nOverfl
16f01 6f 77 2a 73 69 7a 65 6f 66 28 70 50 61 67 65 2d ow*sizeof(pPage-
16f02 3e 61 4f 76 66 6c 5b 30 5d 29 3b 0a 20 20 20 20 >aOvfl[0]);.
16f03 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 memcpy(pChild->a
16f04 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61 4f 76 Ovfl, pPage->aOv
16f05 66 6c 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 fl, nCopy);.
16f06 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f pChild->nOverflo
16f07 77 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 w = pPage->nOver
16f08 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 70 43 flow;. if( pC
16f09 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 hild->nOverflow
16f0a 29 7b 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d ){. pChild-
16f0b 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 >nFree = 0;.
16f0c 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
16f0d 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 50 61 hild->nCell==pPa
16f0e 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 ge->nCell );.
16f0f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16f10 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
16f11 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
16f12 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 );. zeroPage
16f13 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e (pPage, pChild->
16f14 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f aData[0] & ~PTF_
16f15 4c 45 41 46 29 3b 0a 20 20 20 20 70 75 74 34 62 LEAF);. put4b
16f16 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
16f17 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
16f18 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 et+8], pgnoChild
16f19 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22 42 );. TRACE(("B
16f1a 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f ALANCE: copy roo
16f1b 74 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c t %d into %d\n",
16f1c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 43 pPage->pgno, pC
16f1d 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 hild->pgno));.
16f1e 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
16f1f 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d UM ){. rc =
16f20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
16f21 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 54 pChild->pgno, PT
16f22 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 67 RMAP_BTREE, pPag
16f23 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 65 e->pgno);.#ifnde
16f24 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
16f25 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 TOVACUUM. i
16f26 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
16f27 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
16f28 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 setChildPtrmaps
16f29 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 (pChild);.
16f2a 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 }. if( rc )
16f2b 7b 0a 20 20 20 20 20 20 20 20 70 43 68 69 6c 64 {. pChild
16f2c 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b ->nOverflow = 0;
16f2d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
16f2e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
16f2f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16f30 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 {. pCur->iPag
16f31 65 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 e++;. pCur->a
16f32 70 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 69 6c pPage[1] = pChil
16f33 64 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 d;. pCur->aiI
16f34 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 72 dx[0] = 0;. r
16f35 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 c = balance_nonr
16f36 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c oot(pCur);. }el
16f37 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 se{. releaseP
16f38 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 7d age(pChild);. }
16f39 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
16f3a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 ../*.** The page
16f3b 20 74 68 61 74 20 70 43 75 72 20 63 75 72 72 65 that pCur curre
16f3c 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 68 ntly points to h
16f3d 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64 as just been mod
16f3e 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65 ified in.** some
16f3f 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74 way. This funct
16f40 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20 ion figures out
16f41 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61 if this modifica
16f42 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a 2a tion means the.*
16f43 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20 * tree needs to
16f44 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64 be balanced, and
16f45 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65 if so calls the
16f46 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c appropriate bal
16f47 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 69 ancing .** routi
16f48 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 61 6d ne..** .** Param
16f49 65 74 65 72 20 69 73 49 6e 73 65 72 74 20 69 73 eter isInsert is
16f4a 20 74 72 75 65 20 69 66 20 61 20 6e 65 77 20 63 true if a new c
16f4b 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73 ell was just ins
16f4c 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a erted into the.*
16f4d 2a 20 70 61 67 65 2c 20 6f 72 20 66 61 6c 73 65 * page, or false
16f4e 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 otherwise..*/.s
16f4f 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 tatic int balanc
16f50 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 e(BtCursor *pCur
16f51 2c 20 69 6e 74 20 69 73 49 6e 73 65 72 74 29 7b , int isInsert){
16f52 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
16f53 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 TE_OK;. MemPage
16f54 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e *pPage = pCur->
16f55 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
16f56 67 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ge];.. assert(
16f57 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
16f58 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
16f59 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
16f5a 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b Cur->iPage==0 ){
16f5b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
16f5c 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
16f5d 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
16f5e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16f5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 OK && pPage->nOv
16f60 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20 20 erflow>0 ){.
16f61 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64 rc = balance_d
16f62 65 65 70 65 72 28 70 43 75 72 29 3b 0a 20 20 20 eeper(pCur);.
16f63 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
16f64 3e 61 70 50 61 67 65 5b 30 5d 3d 3d 70 50 61 67 >apPage[0]==pPag
16f65 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 e );. asser
16f66 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
16f67 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 3d 53 51 low==0 || rc!=SQ
16f68 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d LITE_OK );. }
16f69 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
16f6a 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d ITE_OK && pPage-
16f6b 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 >nCell==0 ){.
16f6c 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f rc = balance_
16f6d 73 68 61 6c 6c 6f 77 65 72 28 70 43 75 72 29 3b shallower(pCur);
16f6e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
16f6f 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3d 3d Cur->apPage[0]==
16f70 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 61 pPage );. a
16f71 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f ssert( pPage->nO
16f72 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 verflow==0 || rc
16f73 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 !=SQLITE_OK );.
16f74 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
16f75 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 if( pPage->nOv
16f76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20 20 erflow>0 || .
16f77 20 20 20 20 20 28 21 69 73 49 6e 73 65 72 74 20 (!isInsert
16f78 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e && pPage->nFree>
16f79 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
16f7a 6c 65 53 69 7a 65 2a 32 2f 33 29 20 29 7b 0a 20 leSize*2/3) ){.
16f7b 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 rc = balanc
16f7c 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 29 3b e_nonroot(pCur);
16f7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
16f7e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
16f7f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
16f80 65 63 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72 73 ecks all cursors
16f81 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 that point to t
16f82 61 62 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a able pgnoRoot..*
16f83 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 6f 73 * If any of thos
16f84 65 20 63 75 72 73 6f 72 73 20 77 65 72 65 20 6f e cursors were o
16f85 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 pened with wrFla
16f86 67 3d 3d 30 20 69 6e 20 61 20 64 69 66 66 65 72 g==0 in a differ
16f87 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 ent.** database
16f88 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64 61 connection (a da
16f89 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
16f8a 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68 n that shares th
16f8b 65 20 70 61 67 65 72 0a 2a 2a 20 63 61 63 68 65 e pager.** cache
16f8c 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e with the curren
16f8d 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61 6e t connection) an
16f8e 64 20 74 68 61 74 20 6f 74 68 65 72 20 63 6f 6e d that other con
16f8f 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 6e nection .** is n
16f90 6f 74 20 69 6e 20 74 68 65 20 52 65 61 64 55 6e ot in the ReadUn
16f91 63 6f 6d 6d 6d 69 74 74 65 64 20 73 74 61 74 65 commmitted state
16f92 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
16f93 69 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 ine returns .**
16f94 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a SQLITE_LOCKED..*
16f95 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 *.** As well as
16f96 63 75 72 73 6f 72 73 20 77 69 74 68 20 77 72 46 cursors with wrF
16f97 6c 61 67 3d 3d 30 2c 20 63 75 72 73 6f 72 73 20 lag==0, cursors
16f98 77 69 74 68 20 0a 2a 2a 20 69 73 49 6e 63 72 62 with .** isIncrb
16f99 6c 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 72 65 lobHandle==1 are
16f9a 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 also considered
16f9b 20 27 72 65 61 64 27 20 63 75 72 73 6f 72 73 20 'read' cursors
16f9c 62 65 63 61 75 73 65 0a 2a 2a 20 69 6e 63 72 65 because.** incre
16f9d 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 mental blob curs
16f9e 6f 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 ors are used for
16f9f 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e both reading an
16fa0 64 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a d writing..**.**
16fa1 20 57 68 65 6e 20 70 67 6e 6f 52 6f 6f 74 20 69 When pgnoRoot i
16fa2 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 s the root page
16fa3 6f 66 20 61 6e 20 69 6e 74 6b 65 79 20 74 61 62 of an intkey tab
16fa4 6c 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f le, this functio
16fa5 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 n is also.** res
16fa6 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76 ponsible for inv
16fa7 61 6c 69 64 61 74 69 6e 67 20 69 6e 63 72 65 6d alidating increm
16fa8 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f ental blob curso
16fa9 72 73 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c rs when the tabl
16faa 65 20 72 6f 77 0a 2a 2a 20 6f 6e 20 77 68 69 63 e row.** on whic
16fab 68 20 74 68 65 79 20 61 72 65 20 6f 70 65 6e 65 h they are opene
16fac 64 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20 d is deleted or
16fad 6d 6f 64 69 66 69 65 64 2e 20 43 75 72 73 6f 72 modified. Cursor
16fae 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 s are invalidate
16faf 64 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74 d.** according t
16fb0 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
16fb1 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 rules:.**.** 1
16fb2 29 20 57 68 65 6e 20 42 74 72 65 65 43 6c 65 61 ) When BtreeClea
16fb3 72 54 61 62 6c 65 28 29 20 69 73 20 63 61 6c 6c rTable() is call
16fb4 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 ed to completely
16fb5 20 64 65 6c 65 74 65 20 74 68 65 20 63 6f 6e 74 delete the cont
16fb6 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 6f 66 20 ents.** of
16fb7 61 20 42 2d 54 72 65 65 20 74 61 62 6c 65 2c 20 a B-Tree table,
16fb8 70 45 78 63 6c 75 64 65 20 69 73 20 73 65 74 20 pExclude is set
16fb9 74 6f 20 7a 65 72 6f 20 61 6e 64 20 70 61 72 61 to zero and para
16fba 6d 65 74 65 72 20 69 52 6f 77 20 69 73 20 0a 2a meter iRow is .*
16fbb 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 6e 6f * set to no
16fbc 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 n-zero. In this
16fbd 63 61 73 65 20 61 6c 6c 20 69 6e 63 72 65 6d 65 case all increme
16fbe 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 ntal blob cursor
16fbf 73 20 6f 70 65 6e 0a 2a 2a 20 20 20 20 20 20 6f s open.** o
16fc0 6e 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 n the table root
16fc1 65 64 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 61 ed at pgnoRoot a
16fc2 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a re invalidated..
16fc3 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 **.** 2) When
16fc4 42 74 72 65 65 49 6e 73 65 72 74 28 29 2c 20 42 BtreeInsert(), B
16fc5 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 treeDelete() or
16fc6 42 74 72 65 65 50 75 74 44 61 74 61 28 29 20 69 BtreePutData() i
16fc7 73 20 63 61 6c 6c 65 64 20 74 6f 20 0a 2a 2a 20 s called to .**
16fc8 20 20 20 20 20 6d 6f 64 69 66 79 20 61 20 74 61 modify a ta
16fc9 62 6c 65 20 72 6f 77 20 76 69 61 20 61 6e 20 53 ble row via an S
16fca 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 70 45 QL statement, pE
16fcb 78 63 6c 75 64 65 20 69 73 20 73 65 74 20 74 6f xclude is set to
16fcc 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 77 72 the .** wr
16fcd 69 74 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 ite cursor used
16fce 74 6f 20 64 6f 20 74 68 65 20 6d 6f 64 69 66 69 to do the modifi
16fcf 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 72 61 6d cation and param
16fd0 65 74 65 72 20 69 52 6f 77 20 69 73 20 73 65 74 eter iRow is set
16fd1 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 .** to the
16fd2 69 6e 74 65 67 65 72 20 72 6f 77 20 69 64 20 6f integer row id o
16fd3 66 20 74 68 65 20 42 2d 54 72 65 65 20 65 6e 74 f the B-Tree ent
16fd4 72 79 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 ry being modifie
16fd5 64 2e 20 55 6e 6c 65 73 73 0a 2a 2a 20 20 20 20 d. Unless.**
16fd6 20 20 70 45 78 63 6c 75 64 65 20 69 73 20 69 74 pExclude is it
16fd7 73 65 6c 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e self an incremen
16fd8 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 2c tal blob cursor,
16fd9 20 74 68 65 6e 20 61 6c 6c 20 69 6e 63 72 65 6d then all increm
16fda 65 6e 74 61 6c 0a 2a 2a 20 20 20 20 20 20 62 6c ental.** bl
16fdb 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 ob cursors open
16fdc 6f 6e 20 72 6f 77 20 69 52 6f 77 20 6f 66 20 74 on row iRow of t
16fdd 68 65 20 42 2d 54 72 65 65 20 61 72 65 20 69 6e he B-Tree are in
16fde 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a validated..**.**
16fdf 20 20 20 33 29 20 49 66 20 62 6f 74 68 20 70 45 3) If both pE
16fe0 78 63 6c 75 64 65 20 61 6e 64 20 69 52 6f 77 20 xclude and iRow
16fe1 61 72 65 20 73 65 74 20 74 6f 20 7a 65 72 6f 2c are set to zero,
16fe2 20 6e 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 no incremental
16fe3 62 6c 6f 62 20 0a 2a 2a 20 20 20 20 20 20 63 75 blob .** cu
16fe4 72 73 6f 72 73 20 61 72 65 20 69 6e 76 61 6c 69 rsors are invali
16fe5 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 dated..*/.static
16fe6 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 52 65 61 int checkForRea
16fe7 64 43 6f 6e 66 6c 69 63 74 73 28 0a 20 20 42 74 dConflicts(. Bt
16fe8 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 ree *pBtree,
16fe9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
16fea 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68 abase file to ch
16feb 65 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 eck */. Pgno pg
16fec 6e 6f 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 noRoot,
16fed 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 65 61 /* Look for rea
16fee 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 69 d cursors on thi
16fef 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 43 s btree */. BtC
16ff0 75 72 73 6f 72 20 2a 70 45 78 63 6c 75 64 65 2c ursor *pExclude,
16ff1 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 /* Ignore t
16ff2 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 his cursor */.
16ff3 69 36 34 20 69 52 6f 77 20 20 20 20 20 20 20 20 i64 iRow
16ff4 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
16ff5 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 20 owid that might
16ff6 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 29 be changing */.)
16ff7 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
16ff8 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
16ff9 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a = pBtree->pBt;.
16ffa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
16ffb 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 61 73 pBtree->db;. as
16ffc 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
16ffd 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 eeHoldsMutex(pBt
16ffe 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d ree) );. for(p=
16fff 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b pBt->pCursor; p;
17000 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
17001 20 20 69 66 28 20 70 3d 3d 70 45 78 63 6c 75 64 if( p==pExclud
17002 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 e ) continue;.
17003 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f if( p->pgnoRoo
17004 74 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63 6f t!=pgnoRoot ) co
17005 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 ntinue;.#ifndef
17006 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
17007 42 4c 4f 42 0a 20 20 20 20 69 66 28 20 70 2d 3e BLOB. if( p->
17008 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 isIncrblobHandle
17009 20 26 26 20 28 20 0a 20 20 20 20 20 20 20 20 20 && ( .
1700a 28 21 70 45 78 63 6c 75 64 65 20 26 26 20 69 52 (!pExclude && iR
1700b 6f 77 29 0a 20 20 20 20 20 20 7c 7c 20 28 70 45 ow). || (pE
1700c 78 63 6c 75 64 65 20 26 26 20 21 70 45 78 63 6c xclude && !pExcl
1700d 75 64 65 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 ude->isIncrblobH
1700e 61 6e 64 6c 65 20 26 26 20 70 2d 3e 69 6e 66 6f andle && p->info
1700f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20 20 .nKey==iRow).
17010 20 29 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53 )){. p->eS
17011 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
17012 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 23 65 6e VALID;. }.#en
17013 64 69 66 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 dif. if( p->e
17014 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 State!=CURSOR_VA
17015 4c 49 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a LID ) continue;.
17016 20 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c 61 if( p->wrFla
17017 67 3d 3d 30 20 0a 23 69 66 6e 64 65 66 20 53 51 g==0 .#ifndef SQ
17018 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
17019 4f 42 0a 20 20 20 20 20 7c 7c 20 70 2d 3e 69 73 OB. || p->is
1701a 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 0a 23 IncrblobHandle.#
1701b 65 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20 20 20 endif. ){.
1701c 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f 74 sqlite3 *dbOt
1701d 68 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65 2d her = p->pBtree-
1701e 3e 64 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72 >db;. asser
1701f 74 28 64 62 4f 74 68 65 72 29 3b 0a 20 20 20 20 t(dbOther);.
17020 20 20 69 66 28 20 64 62 4f 74 68 65 72 21 3d 64 if( dbOther!=d
17021 62 20 26 26 20 28 64 62 4f 74 68 65 72 2d 3e 66 b && (dbOther->f
17022 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 lags & SQLITE_Re
17023 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 3d 3d adUncommitted)==
17024 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 0 ){. sql
17025 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c ite3ConnectionBl
17026 6f 63 6b 65 64 28 64 62 2c 20 64 62 4f 74 68 65 ocked(db, dbOthe
17027 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 r);. retu
17028 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 rn SQLITE_LOCKED
17029 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 _SHAREDCACHE;.
1702a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1702b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1702c 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 OK;.}../*.** Ins
1702d 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 ert a new record
1702e 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e into the BTree.
1702f 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 The key is giv
17030 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 en by (pKey,nKey
17031 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 ).** and the dat
17032 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 a is given by (p
17033 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 Data,nData). Th
17034 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 e cursor is used
17035 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 only to.** defi
17036 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 ne what table th
17037 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 e record should
17038 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f be inserted into
17039 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a . The cursor.**
1703a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
1703b 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f g at a random lo
1703c 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f cation..**.** Fo
1703d 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c r an INTKEY tabl
1703e 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 e, only the nKey
1703f 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 value of the ke
17040 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 y is used. pKey
17041 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 is.** ignored.
17042 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 For a ZERODATA
17043 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 table, the pData
17044 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 and nData are b
17045 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a oth ignored..*/.
17046 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
17047 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
17048 6e 73 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f nsert(. BtCurso
17049 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 r *pCur,
1704a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 /* Inser
1704b 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 t data into the
1704c 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 table of this cu
1704d 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rsor */. const
1704e 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 void *pKey, i64
1704f 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 nKey, /* The
17050 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 key of the new r
17051 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 ecord */. const
17052 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e void *pData, in
17053 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 t nData, /* The
17054 20 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 data of the new
17055 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 record */. int
17056 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 nZero,
17057 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17058 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 umber of extra 0
17059 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 bytes to append
1705a 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e to data */. in
1705b 74 20 61 70 70 65 6e 64 42 69 61 73 20 20 20 20 t appendBias
1705c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1705d 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 True if this is
1705e 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 likely an append
1705f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
17060 0a 20 20 69 6e 74 20 6c 6f 63 3b 0a 20 20 69 6e . int loc;. in
17061 74 20 73 7a 4e 65 77 3b 0a 20 20 69 6e 74 20 69 t szNew;. int i
17062 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 dx;. MemPage *p
17063 50 61 67 65 3b 0a 20 20 42 74 72 65 65 20 2a 70 Page;. Btree *p
17064 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b = pCur->pBtree;
17065 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
17066 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 = p->pBt;. uns
17067 69 67 6e 65 64 20 63 68 61 72 20 2a 6f 6c 64 43 igned char *oldC
17068 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 ell;. unsigned
17069 63 68 61 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 char *newCell =
1706a 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 0;.. assert( cu
1706b 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
1706c 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
1706d 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ( pBt->inTransac
1706e 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
1706f 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 E );. assert( !
17070 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b pBt->readOnly );
17071 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
17072 3e 77 72 46 6c 61 67 20 29 3b 0a 20 20 72 63 20 >wrFlag );. rc
17073 3d 20 63 68 65 63 6b 46 6f 72 52 65 61 64 43 6f = checkForReadCo
17074 6e 66 6c 69 63 74 73 28 70 43 75 72 2d 3e 70 42 nflicts(pCur->pB
17075 74 72 65 65 2c 20 70 43 75 72 2d 3e 70 67 6e 6f tree, pCur->pgno
17076 52 6f 6f 74 2c 20 70 43 75 72 2c 20 6e 4b 65 79 Root, pCur, nKey
17077 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 20 20 );. if( rc ){
17078 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 .
17079 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 /* The table pCu
1707a 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 r points to has
1707b 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 a read lock */.
1707c 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
1707d 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
1707e 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20 20 20 REDCACHE );.
1707f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
17080 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
17081 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e==CURSOR_FAULT
17082 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43 ){. return pC
17083 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a 0a 20 ur->skip;. }..
17084 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 /* Save the pos
17085 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 itions of any ot
17086 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e her cursors open
17087 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a on this table *
17088 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 /. sqlite3Btree
17089 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75 72 ClearCursor(pCur
1708a 29 3b 0a 20 20 69 66 28 20 0a 20 20 20 20 53 51 );. if( . SQ
1708b 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 LITE_OK!=(rc = s
1708c 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 aveAllCursors(pB
1708d 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f t, pCur->pgnoRoo
1708e 74 2c 20 70 43 75 72 29 29 20 7c 7c 0a 20 20 20 t, pCur)) ||.
1708f 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
17090 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
17091 76 65 74 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c veto(pCur, pKey,
17092 20 6e 4b 65 79 2c 20 61 70 70 65 6e 64 42 69 61 nKey, appendBia
17093 73 2c 20 26 6c 6f 63 29 29 0a 20 20 29 7b 0a 20 s, &loc)). ){.
17094 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
17095 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 }.. pPage = pCu
17096 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
17097 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 iPage];. assert
17098 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
17099 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 || nKey>=0 );.
1709a 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c assert( pPage->l
1709b 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 eaf || !pPage->i
1709c 6e 74 4b 65 79 20 29 3b 0a 20 20 54 52 41 43 45 ntKey );. TRACE
1709d 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 (("INSERT: table
1709e 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 =%d nkey=%lld nd
1709f 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 ata=%d page=%d %
170a0 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
170a1 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 pCur->pgnoRoot,
170a2 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 nKey, nData, pPa
170a3 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 ge->pgno,.
170a4 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 loc==0 ? "ov
170a5 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 erwrite" : "new
170a6 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 entry"));. asse
170a7 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
170a8 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 t );. allocateT
170a9 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 empSpace(pBt);.
170aa 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e newCell = pBt->
170ab 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 pTmpSpace;. if(
170ac 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 newCell==0 ) re
170ad 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
170ae 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e M;. rc = fillIn
170af 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 Cell(pPage, newC
170b0 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c ell, pKey, nKey,
170b1 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e pData, nData, n
170b2 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 Zero, &szNew);.
170b3 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 if( rc ) goto e
170b4 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 nd_insert;. ass
170b5 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c ert( szNew==cell
170b6 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e SizePtr(pPage, n
170b7 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 ewCell) );. ass
170b8 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 ert( szNew<=MX_C
170b9 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b ELL_SIZE(pBt) );
170ba 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 . idx = pCur->a
170bb 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
170bc 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 ];. if( loc==0
170bd 26 26 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d && CURSOR_VALID=
170be 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b =pCur->eState ){
170bf 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a . u16 szOld;.
170c0 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c assert( idx<
170c1 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
170c2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
170c3 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
170c4 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
170c5 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
170c6 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b goto end_insert;
170c7 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 . }. oldCe
170c8 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
170c9 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 age, idx);. i
170ca 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
170cb 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
170cc 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c newCell, oldCell
170cd 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 4);. }.
170ce 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 szOld = cellSize
170cf 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 Ptr(pPage, oldCe
170d0 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c ll);. rc = cl
170d1 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f earCell(pPage, o
170d2 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 ldCell);. if(
170d3 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 rc ) goto end_i
170d4 6e 73 65 72 74 3b 0a 20 20 20 20 72 63 20 3d 20 nsert;. rc =
170d5 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 dropCell(pPage,
170d6 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20 20 20 idx, szOld);.
170d7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
170d8 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 OK ) {. got
170d9 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 o end_insert;.
170da 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
170db 6c 6f 63 3c 30 20 26 26 20 70 50 61 67 65 2d 3e loc<0 && pPage->
170dc 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 61 nCell>0 ){. a
170dd 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 ssert( pPage->le
170de 61 66 20 29 3b 0a 20 20 20 20 69 64 78 20 3d 20 af );. idx =
170df 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ++pCur->aiIdx[pC
170e0 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
170e1 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
170e2 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e = 0;. pCur->
170e3 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 validNKey = 0;.
170e4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
170e5 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 rt( pPage->leaf
170e6 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 69 6e );. }. rc = in
170e7 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 sertCell(pPage,
170e8 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a idx, newCell, sz
170e9 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 New, 0, 0);. if
170ea 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
170eb 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 ){. rc = bala
170ec 6e 63 65 28 70 43 75 72 2c 20 31 29 3b 0a 20 20 nce(pCur, 1);.
170ed 7d 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b }.. /* Must mak
170ee 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 e sure nOverflow
170ef 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 is reset to zer
170f0 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 o even if the ba
170f1 6c 61 6e 63 65 28 29 0a 20 20 2a 2a 20 66 61 69 lance(). ** fai
170f2 6c 73 2e 20 20 49 6e 74 65 72 6e 61 6c 20 64 61 ls. Internal da
170f3 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 ta structure cor
170f4 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 ruption will res
170f5 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 2a ult otherwise. *
170f6 2f 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 /. pCur->apPage
170f7 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
170f8 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 Overflow = 0;..
170f9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
170fa 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f OK ){. moveTo
170fb 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a Root(pCur);. }.
170fc 65 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 end_insert:. re
170fd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
170fe 2a 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74 * Delete the ent
170ff 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 ry that the curs
17100 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
17101 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a o. The cursor.*
17102 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 * is left pointi
17103 6e 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72 ng at a arbitrar
17104 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 y location..*/.S
17105 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
17106 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 t sqlite3BtreeDe
17107 6c 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70 lete(BtCursor *p
17108 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 Cur){. MemPage
17109 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 *pPage = pCur->a
1710a 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
1710b 65 5d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 e];. int idx;.
1710c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
1710d 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63 3b pCell;. int rc;
1710e 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c . Pgno pgnoChil
1710f 64 20 3d 20 30 3b 0a 20 20 42 74 72 65 65 20 2a d = 0;. Btree *
17110 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 p = pCur->pBtree
17111 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
17112 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 t = p->pBt;.. a
17113 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
17114 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
17115 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
17116 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 ->isInit );. as
17117 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 sert( pBt->inTra
17118 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
17119 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 WRITE );. asser
1711a 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c t( !pBt->readOnl
1711b 79 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d y );. if( pCur-
1711c 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1711d 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 FAULT ){. ret
1711e 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a urn pCur->skip;.
1711f 20 20 7d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 }. if( NEVER(
17120 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
17121 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d ->iPage]>=pPage-
17122 3e 6e 43 65 6c 6c 29 20 29 7b 0a 20 20 20 20 72 >nCell) ){. r
17123 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
17124 4f 52 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73 OR; /* The curs
17125 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 or is not pointi
17126 6e 67 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 2a ng to anything *
17127 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 /. }. assert(
17128 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b 0a pCur->wrFlag );.
17129 20 20 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 rc = checkForR
1712a 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 eadConflicts(p,
1712b 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 pCur->pgnoRoot,
1712c 70 43 75 72 2c 20 70 43 75 72 2d 3e 69 6e 66 6f pCur, pCur->info
1712d 2e 6e 4b 65 79 29 3b 0a 20 20 69 66 28 20 72 63 .nKey);. if( rc
1712e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1712f 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
17130 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 pCur points to h
17131 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a as a read lock *
17132 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 /. assert( rc
17133 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f ==SQLITE_LOCKED_
17134 53 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a 20 SHAREDCACHE );.
17135 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
17136 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 }.. /* Restore
17137 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 the current curs
17138 6f 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20 6e or position (a n
17139 6f 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72 73 o-op if the curs
1713a 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20 20 or is not in .
1713b 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 ** CURSOR_REQUIR
1713c 45 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e 64 ESEEK state) and
1713d 20 73 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 save the positi
1713e 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 ons of any other
1713f 20 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20 6f cursors . ** o
17140 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 pen on the same
17141 74 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c 6c table. Then call
17142 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
17143 74 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67 65 te() on the page
17144 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 65 . ** that the e
17145 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65 6c ntry will be del
17146 65 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f 0a eted from.. */.
17147 20 20 69 66 28 20 0a 20 20 20 20 28 72 63 20 3d if( . (rc =
17148 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
17149 73 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d 30 sition(pCur))!=0
1714a 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 61 ||. (rc = sa
1714b 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 veAllCursors(pBt
1714c 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 , pCur->pgnoRoot
1714d 2c 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a 20 , pCur))!=0 ||.
1714e 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 (rc = sqlite3
1714f 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
17150 2d 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a 20 ->pDbPage))!=0.
17151 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
17152 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 c;. }.. /* Loc
17153 61 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74 ate the cell wit
17154 68 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e 64 hin its page and
17155 20 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f 69 leave pCell poi
17156 6e 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 2a nting to the. *
17157 2a 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 61 * data. The clea
17158 72 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 65 rCell() call fre
17159 65 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 es any overflow
1715a 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 pages associated
1715b 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 63 with the. ** c
1715c 65 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 74 ell. The cell it
1715d 73 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 6e self is still in
1715e 74 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64 78 tact.. */. idx
1715f 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 = pCur->aiIdx[p
17160 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 70 Cur->iPage];. p
17161 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
17162 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 69 pPage, idx);. i
17163 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
17164 29 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 ){. pgnoChild
17165 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c = get4byte(pCel
17166 6c 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 l);. }. rc = c
17167 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 learCell(pPage,
17168 70 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 63 pCell);. if( rc
17169 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
1716a 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 c;. }.. if( !p
1716b 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
1716c 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 /*. ** The
1716d 65 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 6f entry we are abo
1716e 75 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 ut to delete is
1716f 6e 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 66 not a leaf so if
17170 20 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 2a we do not. *
17171 2a 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 * do something w
17172 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 68 e will leave a h
17173 6f 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e ole on an intern
17174 61 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 20 al page.. **
17175 57 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c 20 We have to fill
17176 74 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 69 the hole by movi
17177 6e 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 6f ng in a cell fro
17178 6d 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a 20 m a leaf. The.
17179 20 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c 20 ** next Cell
1717a 61 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 6f after the one to
1717b 20 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 67 be deleted is g
1717c 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 uaranteed to exi
1717d 73 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 6f st and. ** to
1717e 20 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 65 be a leaf so we
1717f 20 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 can use it..
17180 20 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f 72 */. BtCursor
17181 20 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d 65 leafCur;. Me
17182 6d 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67 65 mPage *pLeafPage
17183 20 3d 20 30 3b 0a 0a 20 20 20 20 75 6e 73 69 67 = 0;.. unsig
17184 6e 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74 3b ned char *pNext;
17185 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 . int notUsed
17186 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 ;. unsigned c
17187 68 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20 3d 20 har *tempCell =
17188 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 0;. assert( !
17189 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b pPage->intKey );
1718a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1718b 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 70 eGetTempCursor(p
1718c 43 75 72 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a Cur, &leafCur);.
1718d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1718e 42 74 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43 BtreeNext(&leafC
1718f 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 ur, ¬Used);.
17190 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
17191 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 E_OK ){. as
17192 73 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61 69 sert( leafCur.ai
17193 49 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 Idx[leafCur.iPag
17194 65 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 e]==0 );. p
17195 4c 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 43 LeafPage = leafC
17196 75 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 75 ur.apPage[leafCu
17197 72 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 r.iPage];.
17198 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
17199 72 57 72 69 74 65 28 70 4c 65 61 66 50 61 67 65 rWrite(pLeafPage
1719a 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
1719b 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
1719c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1719d 20 69 6e 74 20 6c 65 61 66 43 75 72 73 6f 72 49 int leafCursorI
1719e 6e 76 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 nvalid = 0;.
1719f 20 20 75 31 36 20 73 7a 4e 65 78 74 3b 0a 20 20 u16 szNext;.
171a0 20 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45 TRACE(("DELE
171a1 54 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c TE: table=%d del
171a2 65 74 65 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f ete internal fro
171a3 6d 20 25 64 20 72 65 70 6c 61 63 65 20 66 72 6f m %d replace fro
171a4 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 m leaf %d\n",.
171a5 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e pCur->pgn
171a6 6f 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 oRoot, pPage->pg
171a7 6e 6f 2c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 no, pLeafPage->p
171a8 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72 6f gno));. dro
171a9 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 pCell(pPage, idx
171aa 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 , cellSizePtr(pP
171ab 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 age, pCell));.
171ac 20 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e 64 pNext = find
171ad 43 65 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 Cell(pLeafPage,
171ae 30 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 0);. szNext
171af 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
171b0 4c 65 61 66 50 61 67 65 2c 20 70 4e 65 78 74 29 LeafPage, pNext)
171b1 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
171b2 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 MX_CELL_SIZE(pBt
171b3 29 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a 20 )>=szNext+4 );.
171b4 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d allocateTem
171b5 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 pSpace(pBt);.
171b6 20 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70 42 tempCell = pB
171b7 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 t->pTmpSpace;.
171b8 20 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c 6c if( tempCell
171b9 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
171ba 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
171bb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
171bc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
171bd 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 K ){. rc
171be 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 = insertCell(pPa
171bf 67 65 2c 20 69 64 78 2c 20 70 4e 65 78 74 2d 34 ge, idx, pNext-4
171c0 2c 20 73 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 70 , szNext+4, temp
171c1 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 Cell, 0);.
171c2 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 }... /* The
171c3 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 20 "if" statement
171c4 69 6e 20 74 68 65 20 6e 65 78 74 20 63 6f 64 65 in the next code
171c5 20 62 6c 6f 63 6b 20 69 73 20 63 72 69 74 69 63 block is critic
171c6 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a al. The. *
171c7 2a 20 73 6c 69 67 68 74 65 73 74 20 65 72 72 6f * slightest erro
171c8 72 20 69 6e 20 74 68 61 74 20 73 74 61 74 65 6d r in that statem
171c9 65 6e 74 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 20 ent would allow
171ca 53 51 4c 69 74 65 20 74 6f 20 6f 70 65 72 61 74 SQLite to operat
171cb 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 e. ** corre
171cc 63 74 6c 79 20 6d 6f 73 74 20 6f 66 20 74 68 65 ctly most of the
171cd 20 74 69 6d 65 20 62 75 74 20 70 72 6f 64 75 63 time but produc
171ce 65 20 76 65 72 79 20 72 61 72 65 20 66 61 69 6c e very rare fail
171cf 75 72 65 73 2e 20 20 54 6f 0a 20 20 20 20 20 20 ures. To.
171d0 2a 2a 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 ** guard against
171d1 20 74 68 69 73 2c 20 74 68 65 20 66 6f 6c 6c 6f this, the follo
171d2 77 69 6e 67 20 6d 61 63 72 6f 73 20 68 65 6c 70 wing macros help
171d3 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 0a to verify that.
171d4 20 20 20 20 20 20 2a 2a 20 74 68 65 20 22 69 66 ** the "if
171d5 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 77 " statement is w
171d6 65 6c 6c 20 74 65 73 74 65 64 2e 0a 20 20 20 20 ell tested..
171d7 20 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 */. testc
171d8 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ase( pPage->nOve
171d9 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 rflow==0 && pPag
171da 65 2d 3e 6e 46 72 65 65 3c 70 42 74 2d 3e 75 73 e->nFree<pBt->us
171db 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 ableSize*2/3 .
171dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
171dd 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 & pLeafPage->nFr
171de 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 ee+2+szNext > pB
171df 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f t->usableSize*2/
171e0 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 3 );. testc
171e1 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ase( pPage->nOve
171e2 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 rflow==0 && pPag
171e3 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 e->nFree==pBt->u
171e4 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 sableSize*2/3 .
171e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
171e6 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 && pLeafPage->nF
171e7 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 ree+2+szNext > p
171e8 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
171e9 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 /3 );. test
171ea 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 case( pPage->nOv
171eb 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 erflow==0 && pPa
171ec 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e ge->nFree==pBt->
171ed 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 usableSize*2/3+1
171ee 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
171ef 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d && pLeafPage-
171f0 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 >nFree+2+szNext
171f1 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a > pBt->usableSiz
171f2 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 e*2/3 );. t
171f3 65 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e estcase( pPage->
171f4 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 26 26 20 70 nOverflow>0 && p
171f5 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 74 Page->nFree<=pBt
171f6 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 ->usableSize*2/3
171f7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
171f8 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e && pLeafPage->
171f9 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e nFree+2+szNext >
171fa 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
171fb 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 *2/3 );. te
171fc 73 74 63 61 73 65 28 20 28 70 50 61 67 65 2d 3e stcase( (pPage->
171fd 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 nOverflow>0 || (
171fe 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 pPage->nFree > p
171ff 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
17200 2f 33 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 /3)).
17201 20 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 && pLeafPa
17202 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 ge->nFree+2+szNe
17203 78 74 20 3d 3d 20 70 42 74 2d 3e 75 73 61 62 6c xt == pBt->usabl
17204 65 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a 20 eSize*2/3 );...
17205 20 20 20 20 20 69 66 28 20 28 70 50 61 67 65 2d if( (pPage-
17206 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 >nOverflow>0 ||
17207 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 (pPage->nFree >
17208 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a pBt->usableSize*
17209 32 2f 33 29 29 20 26 26 0a 20 20 20 20 20 20 20 2/3)) &&.
1720a 20 20 20 28 70 4c 65 61 66 50 61 67 65 2d 3e 6e (pLeafPage->n
1720b 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 Free+2+szNext >
1720c 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a pBt->usableSize*
1720d 32 2f 33 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 2/3). ){.
1720e 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 /* This br
1720f 61 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 69 66 anch is taken if
17210 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f the internal no
17211 64 65 20 69 73 20 6e 6f 77 20 65 69 74 68 65 72 de is now either
17212 20 6f 76 65 72 66 6c 6f 77 69 6e 67 0a 20 20 20 overflowing.
17213 20 20 20 20 20 2a 2a 20 6f 72 20 75 6e 64 65 72 ** or under
17214 66 75 6c 6c 20 61 6e 64 20 74 68 65 20 6c 65 61 full and the lea
17215 66 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65 20 75 f node will be u
17216 6e 64 65 72 66 75 6c 6c 20 61 66 74 65 72 20 74 nderfull after t
17217 68 65 20 6a 75 73 74 20 63 65 6c 6c 20 0a 20 20 he just cell .
17218 20 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 ** copied
17219 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 to the internal
1721a 6e 6f 64 65 20 69 73 20 64 65 6c 65 74 65 64 20 node is deleted
1721b 66 72 6f 6d 20 69 74 2e 20 54 68 69 73 20 69 73 from it. This is
1721c 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 a special.
1721d 20 20 20 2a 2a 20 63 61 73 65 20 62 65 63 61 75 ** case becau
1721e 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 se the call to b
1721f 61 6c 61 6e 63 65 28 29 20 74 6f 20 63 6f 72 72 alance() to corr
17220 65 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ect the internal
17221 20 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a 2a node. **
17222 20 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 may change the
17223 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 tree structure a
17224 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 nd invalidate th
17225 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 20 e contents of.
17226 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 ** the lea
17227 66 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e fCur.apPage[] an
17228 64 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b d leafCur.aiIdx[
17229 5d 20 61 72 72 61 79 73 2c 20 77 68 69 63 68 20 ] arrays, which
1722a 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20 will be.
1722b 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 62 ** used by the b
1722c 61 6c 61 6e 63 65 28 29 20 72 65 71 75 69 72 65 alance() require
1722d 64 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 65 d to correct the
1722e 20 75 6e 64 65 72 66 75 6c 6c 20 6c 65 61 66 0a underfull leaf.
1722f 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 2e ** node.
17230 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
17231 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 72 6d 75 ** The formu
17232 6c 61 20 75 73 65 64 20 69 6e 20 74 68 65 20 65 la used in the e
17233 78 70 72 65 73 73 69 6f 6e 20 61 62 6f 76 65 20 xpression above
17234 61 72 65 20 62 61 73 65 64 20 6f 6e 20 66 61 63 are based on fac
17235 65 74 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a ets of. *
17236 2a 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c * the SQLite fil
17237 65 2d 66 6f 72 6d 61 74 20 74 68 61 74 20 64 6f e-format that do
17238 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72 not change over
17239 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 2a time.. *
1723a 2f 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 /. testca
1723b 73 65 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 se( pPage->nFree
1723c 3d 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ==pBt->usableSiz
1723d 65 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 20 20 20 e*2/3+1 );.
1723e 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 testcase( pLe
1723f 61 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b afPage->nFree+2+
17240 73 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e 75 73 61 szNext==pBt->usa
17241 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b bleSize*2/3+1 );
17242 0a 20 20 20 20 20 20 20 20 6c 65 61 66 43 75 72 . leafCur
17243 73 6f 72 49 6e 76 61 6c 69 64 20 3d 20 31 3b 0a sorInvalid = 1;.
17244 20 20 20 20 20 20 7d 20 20 20 20 20 20 20 20 0a } .
17245 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
17246 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17247 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
17248 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
17249 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
1724a 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 ge) );. p
1724b 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 ut4byte(findOver
1724c 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 flowCell(pPage,
1724d 69 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64 29 idx), pgnoChild)
1724e 3b 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e ;. VVA_ON
1724f 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 LY( pCur->pagesS
17250 68 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a 20 huffled = 0 );.
17251 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 rc = bala
17252 6e 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 nce(pCur, 0);.
17253 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 }.. if(
17254 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
17255 26 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61 & leafCursorInva
17256 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 2f lid ){. /
17257 2a 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 65 20 * The leaf-node
17258 69 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 6c 6c is now underfull
17259 20 61 6e 64 20 73 6f 20 74 68 65 20 74 72 65 65 and so the tree
1725a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20 needs to be .
1725b 20 20 20 20 20 20 2a 2a 20 72 65 62 61 6c 61 6e ** rebalan
1725c 63 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68 ced. However, th
1725d 65 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 65 72 e balance() oper
1725e 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e 74 ation on the int
1725f 65 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a ernal. **
17260 20 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 79 20 node above may
17261 68 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74 68 have modified th
17262 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 e structure of t
17263 68 65 20 42 2d 54 72 65 65 20 61 6e 64 0a 20 20 he B-Tree and.
17264 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 65 20 ** so the
17265 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 current contents
17266 20 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 50 61 of leafCur.apPa
17267 67 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 75 72 ge[] and leafCur
17268 2e 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 .aiIdx[].
17269 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 ** may not be t
1726a 72 75 73 74 65 64 2e 0a 20 20 20 20 20 20 20 20 rusted..
1726b 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 **. ** It
1726c 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1726d 20 74 6f 20 63 6f 70 79 20 74 68 65 20 61 6e 63 to copy the anc
1726e 65 73 74 72 79 20 66 72 6f 6d 20 70 43 75 72 2c estry from pCur,
1726f 20 61 73 20 74 68 65 20 73 61 6d 65 0a 20 20 20 as the same.
17270 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 28 ** balance(
17271 29 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 61 6c ) call has inval
17272 69 64 61 74 65 64 20 74 68 65 20 70 43 75 72 2d idated the pCur-
17273 3e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 >apPage[] and ai
17274 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 2a 2a Idx[]. **
17275 20 61 72 72 61 79 73 2e 20 0a 20 20 20 20 20 20 arrays. .
17276 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 **. **
17277 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 76 65 The call to save
17278 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
17279 20 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 6c 6c below internall
1727a 79 20 73 61 76 65 73 20 74 68 65 20 0a 20 20 20 y saves the .
1727b 20 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 61 74 ** key that
1727c 20 6c 65 61 66 43 75 72 20 69 73 20 63 75 72 72 leafCur is curr
1727d 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 ently pointing t
1727e 6f 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 o. Currently, th
1727f 65 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 ere. ** a
17280 72 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66 re two copies of
17281 20 74 68 61 74 20 6b 65 79 20 69 6e 20 74 68 65 that key in the
17282 20 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 72 65 tree - one here
17283 20 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 20 20 on the leaf.
17284 20 20 20 20 20 2a 2a 20 70 61 67 65 20 61 6e 64 ** page and
17285 20 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 6e 74 one on some int
17286 65 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 74 68 ernal node in th
17287 65 20 74 72 65 65 2e 20 54 68 65 20 63 6f 70 79 e tree. The copy
17288 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 on. ** t
17289 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 20 he leaf node is
1728a 61 6c 77 61 79 73 20 74 68 65 20 6e 65 78 74 20 always the next
1728b 6b 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 64 65 key in tree-orde
1728c 72 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 20 r after the .
1728d 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 6e 20 ** copy on
1728e 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 the internal nod
1728f 65 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c 6c 20 e. So, the call
17290 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e to sqlite3BtreeN
17291 65 78 74 28 29 0a 20 20 20 20 20 20 20 20 2a 2a ext(). **
17292 20 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 43 75 calls restoreCu
17293 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 74 rsorPosition() t
17294 6f 20 70 6f 69 6e 74 20 74 68 65 20 63 75 72 73 o point the curs
17295 6f 72 20 74 6f 20 74 68 65 20 63 6f 70 79 0a 20 or to the copy.
17296 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 ** stored
17297 20 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c on the internal
17298 20 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 76 61 node, then adva
17299 6e 63 65 73 20 74 6f 20 74 68 65 20 6e 65 78 74 nces to the next
1729a 20 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 20 20 entry,.
1729b 2a 2a 20 77 68 69 63 68 20 68 61 70 70 65 6e 73 ** which happens
1729c 20 74 6f 20 62 65 20 74 68 65 20 63 6f 70 79 20 to be the copy
1729d 6f 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 74 68 of the key on th
1729e 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e e internal node.
1729f 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 74 20 . ** Net
172a0 65 66 66 65 63 74 3a 20 6c 65 61 66 43 75 72 20 effect: leafCur
172a1 69 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 63 6b is pointing back
172a2 20 74 6f 20 74 68 65 20 64 75 70 6c 69 63 61 74 to the duplicat
172a3 65 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 2a e cell. *
172a4 2a 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 * that needs to
172a5 62 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e 64 20 be removed, and
172a6 74 68 65 20 6c 65 61 66 43 75 72 2e 61 70 50 61 the leafCur.apPa
172a7 67 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 20 20 ge[] and.
172a8 20 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 49 64 ** leafCur.aiId
172a9 78 5b 5d 20 61 72 72 61 79 73 20 61 72 65 20 63 x[] arrays are c
172aa 6f 72 72 65 63 74 2e 0a 20 20 20 20 20 20 20 20 orrect..
172ab 2a 2f 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f */. VVA_O
172ac 4e 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 50 67 NLY( Pgno leafPg
172ad 6e 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 2d 3e no = pLeafPage->
172ae 70 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 20 pgno );.
172af 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 rc = saveCursorP
172b0 6f 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 75 72 osition(&leafCur
172b1 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
172b2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
172b3 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
172b4 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 qlite3BtreeNext(
172b5 26 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 55 73 &leafCur, ¬Us
172b6 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ed);. }.
172b7 20 20 20 20 20 20 20 70 4c 65 61 66 50 61 67 65 pLeafPage
172b8 20 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 = leafCur.apPag
172b9 65 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d e[leafCur.iPage]
172ba 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
172bb 28 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 6e ( pLeafPage->pgn
172bc 6f 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b 0a 20 o==leafPgno );.
172bd 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c assert( l
172be 65 61 66 43 75 72 2e 61 69 49 64 78 5b 6c 65 61 eafCur.aiIdx[lea
172bf 66 43 75 72 2e 69 50 61 67 65 5d 3d 3d 30 20 29 fCur.iPage]==0 )
172c0 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
172c1 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
172c2 72 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c rc. && SQL
172c3 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 ITE_OK==(rc = sq
172c4 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
172c5 70 4c 65 61 66 50 61 67 65 2d 3e 70 44 62 50 61 pLeafPage->pDbPa
172c6 67 65 29 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 ge)) . ){.
172c7 20 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 dropCell(
172c8 70 4c 65 61 66 50 61 67 65 2c 20 30 2c 20 73 7a pLeafPage, 0, sz
172c9 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 56 Next);. V
172ca 56 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 43 75 72 VA_ONLY( leafCur
172cb 2e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d .pagesShuffled =
172cc 20 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 0 );. rc
172cd 20 3d 20 62 61 6c 61 6e 63 65 28 26 6c 65 61 66 = balance(&leaf
172ce 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 Cur, 0);.
172cf 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 assert( leafCur
172d0 73 6f 72 49 6e 76 61 6c 69 64 20 7c 7c 20 21 6c sorInvalid || !l
172d1 65 61 66 43 75 72 2e 70 61 67 65 73 53 68 75 66 eafCur.pagesShuf
172d2 66 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 fled.
172d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
172d4 20 20 20 20 20 20 20 20 7c 7c 20 21 70 43 75 72 || !pCur
172d5 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 ->pagesShuffled
172d6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
172d7 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
172d8 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 eReleaseTempCurs
172d9 6f 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 or(&leafCur);.
172da 7d 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43 45 }else{. TRACE
172db 28 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c 65 (("DELETE: table
172dc 3d 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 =%d delete from
172dd 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20 leaf %d\n",.
172de 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f pCur->pgnoRoo
172df 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 t, pPage->pgno))
172e0 3b 0a 20 20 20 20 72 63 20 3d 20 64 72 6f 70 43 ;. rc = dropC
172e1 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 ell(pPage, idx,
172e2 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
172e3 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 20 e, pCell));.
172e4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
172e5 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
172e6 62 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 29 balance(pCur, 0)
172e7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
172e8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
172e9 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f ){. moveToRoo
172ea 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72 t(pCur);. }. r
172eb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
172ec 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
172ed 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 72 BTree table. Wr
172ee 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c ite into *piTabl
172ef 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 e the page.** nu
172f0 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f mber for the roo
172f1 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 t page of the ne
172f2 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 w table..**.** T
172f3 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 20 he type of type
172f4 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 is determined by
172f5 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d the flags param
172f6 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a eter. Only the.
172f7 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c ** following val
172f8 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 65 ues of flags are
172f9 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 currently in us
172fa 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 73 e. Other values
172fb 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69 for.** flags mi
172fc 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a ght not work:.**
172fd 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 4e .** BTREE_IN
172fe 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 44 TKEY|BTREE_LEAFD
172ff 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f 72 ATA Used for
17300 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 68 SQL tables with
17301 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20 rowid keys.**
17302 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 BTREE_ZERODAT
17303 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 A
17304 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20 Used for SQL
17305 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69 indices.*/.stati
17306 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 74 c int btreeCreat
17307 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c eTable(Btree *p,
17308 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 int *piTable, i
17309 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 53 nt flags){. BtS
1730a 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
1730b 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a pBt;. MemPage *
1730c 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67 pRoot;. Pgno pg
1730d 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 noRoot;. int rc
1730e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1730f 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
17310 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
17311 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 rt( pBt->inTrans
17312 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
17313 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ITE );. assert(
17314 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 !pBt->readOnly
17315 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
17316 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
17317 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 M. rc = allocat
17318 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
17319 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f &pRoot, &pgnoRoo
1731a 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 t, 1, 0);. if(
1731b 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e rc ){. return
1731c 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 rc;. }.#else.
1731d 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
1731e 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f cuum ){. Pgno
1731f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20 pgnoMove;
17320 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 68 /* Move a page h
17321 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d ere to make room
17322 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 for the root-pa
17323 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 ge */. MemPag
17324 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a e *pPageMove; /*
17325 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 The page to mov
17326 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a e to. */.. /*
17327 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 Creating a new
17328 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 62 table may probab
17329 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e ly require movin
1732a 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 61 g an existing da
1732b 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f tabase. ** to
1732c 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74 make room for t
1732d 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 6f he new tables ro
1732e 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 65 ot page. In case
1732f 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e 73 this page turns
17330 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 . ** out to b
17331 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 e an overflow pa
17332 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f ge, delete all o
17333 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70 verflow page-map
17334 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 68 caches. ** h
17335 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73 eld by open curs
17336 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ors.. */.
17337 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 invalidateAllOve
17338 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b rflowCache(pBt);
17339 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 .. /* Read th
1733a 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b e value of meta[
1733b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 3] from the data
1733c 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 6e base to determin
1733d 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 20 e where the.
1733e 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 ** root page of
1733f 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 68 the new table sh
17340 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d ould go. meta[3]
17341 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 is the largest
17342 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a root-page. **
17343 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 2c created so far,
17344 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 so the new root
17345 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b 33 -page is (meta[3
17346 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ]+1).. */.
17347 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
17348 65 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20 eeGetMeta(p, 4,
17349 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 &pgnoRoot);.
1734a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1734b 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
1734c 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
1734d 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20 pgnoRoot++;..
1734e 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f 74 /* The new root
1734f 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 -page may not be
17350 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 allocated on a
17351 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
17352 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 , or the. **
17353 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 67 PENDING_BYTE pag
17354 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 e.. */. wh
17355 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 ile( pgnoRoot==P
17356 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
17357 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20 , pgnoRoot) ||.
17358 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d pgnoRoot=
17359 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
1735a 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 GE(pBt) ){.
1735b 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20 pgnoRoot++;.
1735c 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
1735d 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20 gnoRoot>=3 );..
1735e 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /* Allocate a
1735f 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20 page. The page
17360 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 72 that currently r
17361 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52 6f esides at pgnoRo
17362 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 ot will. ** b
17363 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 61 e moved to the a
17364 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28 75 llocated page (u
17365 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 61 nless the alloca
17366 74 65 64 20 70 61 67 65 20 68 61 70 70 65 6e 73 ted page happens
17367 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 64 . ** to resid
17368 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a e at pgnoRoot)..
17369 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
1736a 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
1736b 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f 76 e(pBt, &pPageMov
1736c 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67 e, &pgnoMove, pg
1736d 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20 noRoot, 1);.
1736e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1736f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
17370 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 n rc;. }..
17371 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 if( pgnoMove!=p
17372 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 gnoRoot ){.
17373 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 /* pgnoRoot is
17374 74 68 65 20 70 61 67 65 20 74 68 61 74 20 77 69 the page that wi
17375 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 ll be used for t
17376 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a he root-page of.
17377 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 ** the new
17378 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 table (assuming
17379 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f an error did no
1737a 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65 t occur). But we
1737b 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 were. ** a
1737c 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 llocated pgnoMov
1737d 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 28 e. If required (
1737e 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e i.e. if it was n
1737f 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 ot allocated.
17380 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69 ** by extendi
17381 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68 ng the file), th
17382 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 61 e current page a
17383 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d t position pgnoM
17384 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 ove. ** is
17385 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65 already journale
17386 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 d.. */.
17387 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 u8 eType;.
17388 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b Pgno iPtrPage;
17389 0a 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 .. releaseP
1738a 61 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a age(pPageMove);.
1738b 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 . /* Move t
1738c 68 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c he page currentl
1738d 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f y at pgnoRoot to
1738e 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 pgnoMove. */.
1738f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17390 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
17391 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f , pgnoRoot, &pRo
17392 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 ot, 0);. if
17393 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
17394 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
17395 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
17396 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
17397 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 et(pBt, pgnoRoot
17398 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 , &eType, &iPtrP
17399 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 age);. if(
1739a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
1739b 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 eType==PTRMAP_R
1739c 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65 OOTPAGE || eType
1739d 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 ==PTRMAP_FREEPAG
1739e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c E ){. rel
1739f 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
173a0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
173a1 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
173a2 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 assert( eType!
173a3 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 =PTRMAP_ROOTPAGE
173a4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
173a5 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f ( eType!=PTRMAP_
173a6 46 52 45 45 50 41 47 45 20 29 3b 0a 20 20 20 20 FREEPAGE );.
173a7 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 rc = relocateP
173a8 61 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 2c 20 age(pBt, pRoot,
173a9 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c eType, iPtrPage,
173aa 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b 0a 20 pgnoMove, 0);.
173ab 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
173ac 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20 20 (pRoot);..
173ad 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 /* Obtain the pa
173ae 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 2a ge at pgnoRoot *
173af 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d /. if( rc!=
173b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
173b1 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
173b2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
173b3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
173b4 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f etPage(pBt, pgno
173b5 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 Root, &pRoot, 0)
173b6 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
173b7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
173b8 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
173b9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
173ba 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
173bb 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 rite(pRoot->pDbP
173bc 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 age);. if(
173bd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
173be 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
173bf 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 Page(pRoot);.
173c0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
173c1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
173c2 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d e{. pRoot =
173c3 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20 pPageMove;.
173c4 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 } .. /* Updat
173c5 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 e the pointer-ma
173c6 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20 p and meta-data
173c7 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f with the new roo
173c8 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a t-page number. *
173c9 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 /. rc = ptrma
173ca 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f pPut(pBt, pgnoRo
173cb 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 ot, PTRMAP_ROOTP
173cc 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 AGE, 0);. if(
173cd 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c rc ){. rel
173ce 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
173cf 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
173d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
173d1 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
173d2 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67 ateMeta(p, 4, pg
173d3 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 noRoot);. if(
173d4 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c rc ){. rel
173d5 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
173d6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
173d7 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 ;. }.. }else
173d8 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 {. rc = alloc
173d9 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
173da 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 , &pRoot, &pgnoR
173db 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 oot, 1, 0);.
173dc 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
173dd 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 rc;. }.#endif.
173de 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
173df 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
173e0 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 (pRoot->pDbPage)
173e1 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 );. zeroPage(p
173e2 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54 Root, flags | PT
173e3 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74 F_LEAF);. sqlit
173e4 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f e3PagerUnref(pRo
173e5 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ot->pDbPage);.
173e6 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 *piTable = (int)
173e7 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 pgnoRoot;. retu
173e8 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
173e9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
173ea 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
173eb 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 reateTable(Btree
173ec 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c *p, int *piTabl
173ed 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 e, int flags){.
173ee 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
173ef 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
173f0 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 . p->pBt->db =
173f1 70 2d 3e 64 62 3b 0a 20 20 72 63 20 3d 20 62 74 p->db;. rc = bt
173f2 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 reeCreateTable(p
173f3 2c 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 , piTable, flags
173f4 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
173f5 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
173f6 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
173f7 20 45 72 61 73 65 20 74 68 65 20 67 69 76 65 6e Erase the given
173f8 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 61 database page a
173f9 6e 64 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 nd all its child
173fa 72 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 ren. Return.**
173fb 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 the page to the
173fc 66 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 freelist..*/.sta
173fd 74 69 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 tic int clearDat
173fe 61 62 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 abasePage(. BtS
173ff 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 hared *pBt,
17400 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 /* The BTr
17401 65 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ee that contains
17402 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
17403 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 Pgno pgno,
17404 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
17405 6d 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f mber to clear */
17406 0a 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 . int freePageF
17407 6c 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c lag, /* Deal
17408 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74 locate page if t
17409 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e rue */. int *pn
1740a 43 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 Change.){. MemP
1740b 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a age *pPage = 0;.
1740c 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 int rc;. unsi
1740d 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c gned char *pCell
1740e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 ;. int i;.. as
1740f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
17410 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
17411 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67 tex) );. if( pg
17412 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 6e no>pagerPagecoun
17413 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 t(pBt) ){. re
17414 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
17415 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 UPT_BKPT;. }..
17416 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 rc = getAndInit
17417 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 Page(pBt, pgno,
17418 26 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 &pPage);. if( r
17419 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 c ) goto clearda
1741a 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a tabasepage_out;.
1741b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
1741c 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b ge->nCell; i++){
1741d 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e . pCell = fin
1741e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b dCell(pPage, i);
1741f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
17420 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 >leaf ){. r
17421 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 c = clearDatabas
17422 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 ePage(pBt, get4b
17423 79 74 65 28 70 43 65 6c 6c 29 2c 20 31 2c 20 70 yte(pCell), 1, p
17424 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 nChange);.
17425 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c if( rc ) goto cl
17426 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f eardatabasepage_
17427 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 out;. }. r
17428 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 c = clearCell(pP
17429 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 age, pCell);.
1742a 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 if( rc ) goto c
1742b 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 leardatabasepage
1742c 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 _out;. }. if(
1742d 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
1742e 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 rc = clearDa
1742f 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 tabasePage(pBt,
17430 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
17431 3e 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70 >aData[8]), 1, p
17432 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 69 66 nChange);. if
17433 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 ( rc ) goto clea
17434 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 rdatabasepage_ou
17435 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 t;. }else if( p
17436 6e 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 61 nChange ){. a
17437 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e ssert( pPage->in
17438 74 4b 65 79 20 29 3b 0a 20 20 20 20 2a 70 6e 43 tKey );. *pnC
17439 68 61 6e 67 65 20 2b 3d 20 70 50 61 67 65 2d 3e hange += pPage->
1743a 6e 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 nCell;. }. if(
1743b 20 66 72 65 65 50 61 67 65 46 6c 61 67 20 29 7b freePageFlag ){
1743c 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 . rc = freePa
1743d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c ge(pPage);. }el
1743e 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c se if( (rc = sql
1743f 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
17440 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 3d Page->pDbPage))=
17441 3d 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61 =0 ){. zeroPa
17442 67 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d ge(pPage, pPage-
17443 3e 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f >aData[0] | PTF_
17444 4c 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61 LEAF);. }..clea
17445 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 rdatabasepage_ou
17446 74 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 t:. releasePage
17447 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 (pPage);. retur
17448 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 n rc;.}../*.** D
17449 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d elete all inform
1744a 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e ation from a sin
1744b 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 gle table in the
1744c 20 64 61 74 61 62 61 73 65 2e 20 20 69 54 61 62 database. iTab
1744d 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61 67 le is.** the pag
1744e 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
1744f 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c root of the tabl
17450 65 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 e. After this r
17451 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a outine returns,.
17452 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 ** the root page
17453 20 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20 73 is empty, but s
17454 74 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a till exists..**.
17455 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
17456 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 will fail with S
17457 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 QLITE_LOCKED if
17458 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 there are any op
17459 65 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73 6f en.** read curso
1745a 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e rs on the table.
1745b 20 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75 72 Open write cur
1745c 73 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20 74 sors are moved t
1745d 6f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66 o the.** root of
1745e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a the table..**.*
1745f 2a 20 49 66 20 70 6e 43 68 61 6e 67 65 20 69 73 * If pnChange is
17460 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
17461 74 61 62 6c 65 20 69 54 61 62 6c 65 20 6d 75 73 table iTable mus
17462 74 20 62 65 20 61 6e 20 69 6e 74 6b 65 79 20 74 t be an intkey t
17463 61 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 able. The.** int
17464 65 67 65 72 20 76 61 6c 75 65 20 70 6f 69 6e 74 eger value point
17465 65 64 20 74 6f 20 62 79 20 70 6e 43 68 61 6e 67 ed to by pnChang
17466 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 e is incremented
17467 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f by the number o
17468 66 0a 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e 20 f.** entries in
17469 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 the table..*/.SQ
1746a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1746b 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
1746c 61 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 arTable(Btree *p
1746d 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e , int iTable, in
1746e 74 20 2a 70 6e 43 68 61 6e 67 65 29 7b 0a 20 20 t *pnChange){.
1746f 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 int rc;. BtShar
17470 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
17471 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
17472 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d Enter(p);. pBt-
17473 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 >db = p->db;. a
17474 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
17475 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
17476 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 63 68 ;. if( (rc = ch
17477 65 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69 eckForReadConfli
17478 63 74 73 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 cts(p, iTable, 0
17479 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b , 1))!=SQLITE_OK
1747a 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f 74 68 69 ){. /* nothi
1747b 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65 ng to do */. }e
1747c 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f lse if( SQLITE_O
1747d 4b 21 3d 28 72 63 20 3d 20 73 61 76 65 41 6c 6c K!=(rc = saveAll
1747e 43 75 72 73 6f 72 73 28 70 42 74 2c 20 69 54 61 Cursors(pBt, iTa
1747f 62 6c 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 ble, 0)) ){.
17480 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f /* nothing to do
17481 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 */. }else{.
17482 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 rc = clearDatab
17483 61 73 65 50 61 67 65 28 70 42 74 2c 20 28 50 67 asePage(pBt, (Pg
17484 6e 6f 29 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e no)iTable, 0, pn
17485 43 68 61 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 Change);. }. s
17486 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
17487 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
17488 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 ;.}../*.** Erase
17489 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e all information
1748a 20 69 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 in a table and
1748b 61 64 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 add the root of
1748c 74 68 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 the table to.**
1748d 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45 the freelist. E
1748e 78 63 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 xcept, the root
1748f 6f 66 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65 of the principle
17490 20 74 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 table (the one
17491 6f 6e 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73 on.** page 1) is
17492 20 6e 65 76 65 72 20 61 64 64 65 64 20 74 6f 20 never added to
17493 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a the freelist..**
17494 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
17495 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 will fail with
17496 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 SQLITE_LOCKED if
17497 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f there are any o
17498 70 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f pen.** cursors o
17499 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a n the table..**.
1749a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d ** If AUTOVACUUM
1749b 20 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 is enabled and
1749c 74 68 65 20 70 61 67 65 20 61 74 20 69 54 61 62 the page at iTab
1749d 6c 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 le is not the la
1749e 73 74 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 st.** root page
1749f 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
174a0 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c file, then the l
174a1 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a ast root page .*
174a2 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 * in the databas
174a3 65 20 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 e file is moved
174a4 69 6e 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f into the slot fo
174a5 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 rmerly occupied
174a6 62 79 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 by.** iTable and
174a7 20 74 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 that last slot
174a8 66 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 formerly occupie
174a9 64 20 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f d by the last ro
174aa 6f 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 ot page.** is ad
174ab 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c ded to the freel
174ac 69 73 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 ist instead of i
174ad 54 61 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 Table. In this
174ae 73 61 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 say, all.** root
174af 20 70 61 67 65 73 20 61 72 65 20 6b 65 70 74 20 pages are kept
174b0 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
174b1 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
174b2 20 66 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 file, which.**
174b3 69 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 is necessary for
174b4 20 41 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 AUTOVACUUM to w
174b5 6f 72 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d ork right. *piM
174b6 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 74 oved is set to t
174b7 68 65 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 he .** page numb
174b8 65 72 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 er that used to
174b9 62 65 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 be the last root
174ba 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c page in the fil
174bb 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 e before.** the
174bc 6d 6f 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 move. If no pag
174bd 65 20 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 e gets moved, *p
174be 69 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f iMoved is set to
174bf 20 30 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 0..** The last
174c0 72 6f 6f 74 20 70 61 67 65 20 69 73 20 72 65 63 root page is rec
174c1 6f 72 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d orded in meta[3]
174c2 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f and the value o
174c3 66 0a 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 f.** meta[3] is
174c4 75 70 64 61 74 65 64 20 62 79 20 74 68 69 73 20 updated by this
174c5 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 procedure..*/.st
174c6 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 44 72 atic int btreeDr
174c7 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 opTable(Btree *p
174c8 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 69 , Pgno iTable, i
174c9 6e 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 nt *piMoved){.
174ca 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 int rc;. MemPag
174cb 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 e *pPage = 0;.
174cc 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
174cd 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 p->pBt;.. asser
174ce 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
174cf 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
174d0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 assert( p->inT
174d1 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
174d2 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 E );.. /* It is
174d3 20 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 illegal to drop
174d4 20 61 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20 a table if any
174d5 63 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e cursors are open
174d6 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 on the. ** dat
174d7 61 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 62 abase. This is b
174d8 65 63 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 ecause in auto-v
174d9 61 63 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 acuum mode the b
174da 61 63 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 ackend may. **
174db 6e 65 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f need to move ano
174dc 74 68 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 ther root-page t
174dd 6f 20 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 o fill a gap lef
174de 74 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 t by the deleted
174df 0a 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e . ** root page.
174e0 20 49 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 If an open curs
174e1 6f 72 20 77 61 73 20 75 73 69 6e 67 20 74 68 69 or was using thi
174e2 73 20 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d s page a problem
174e3 20 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 would . ** occ
174e4 75 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ur.. */. if( p
174e5 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 Bt->pCursor ){.
174e6 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 sqlite3Connec
174e7 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 tionBlocked(p->d
174e8 62 2c 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 2d b, pBt->pCursor-
174e9 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 >pBtree->db);.
174ea 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
174eb 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 LOCKED_SHAREDCAC
174ec 48 45 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 HE;. }.. rc =
174ed 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
174ee 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 age(pBt, (Pgno)i
174ef 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c 20 30 Table, &pPage, 0
174f0 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
174f1 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 turn rc;. rc =
174f2 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
174f3 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 rTable(p, iTable
174f4 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 , 0);. if( rc )
174f5 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 {. releasePag
174f6 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 e(pPage);. re
174f7 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
174f8 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 *piMoved = 0;..
174f9 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29 7b if( iTable>1 ){
174fa 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
174fb 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
174fc 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 rc = freePage
174fd 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 6c (pPage);. rel
174fe 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
174ff 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 70 .#else. if( p
17500 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
17501 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61 78 {. Pgno max
17502 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 RootPgno;.
17503 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
17504 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20 26 eGetMeta(p, 4, &
17505 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 maxRootPgno);.
17506 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
17507 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
17508 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
17509 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 ge);. ret
1750a 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
1750b 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 6c . if( iTabl
1750c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29 e==maxRootPgno )
1750d 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 {. /* If
1750e 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
1750f 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 74 dropped is the t
17510 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61 able with the la
17511 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a rgest root-page.
17512 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 ** numbe
17513 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 r in the databas
17514 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20 e, put the root
17515 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65 page on the free
17516 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20 list. .
17517 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
17518 66 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b freePage(pPage);
17519 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
1751a 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
1751b 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1751c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1751d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1751e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1751f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
17520 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 The table being
17521 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f dropped does no
17522 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 65 t have the large
17523 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 st root-page.
17524 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 ** number i
17525 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
17526 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65 So move the page
17527 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20 that does into
17528 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
17529 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 gap left by the
1752a 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67 deleted root-pag
1752b 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
1752c 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
1752d 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65 Move;. re
1752e 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
1752f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
17530 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
17531 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 ge(pBt, maxRootP
17532 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b gno, &pMove, 0);
17533 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
17534 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17535 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
17536 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
17537 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 rc = reloca
17538 74 65 50 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 tePage(pBt, pMov
17539 65 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 e, PTRMAP_ROOTPA
1753a 47 45 2c 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 GE, 0, iTable, 0
1753b 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 );. relea
1753c 73 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 sePage(pMove);.
1753d 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1753e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1753f 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
17540 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
17541 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
17542 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
17543 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 maxRootPgno, &p
17544 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Move, 0);.
17545 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
17546 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
17547 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
17548 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 }. rc
17549 20 3d 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76 = freePage(pMov
1754a 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 e);. rele
1754b 61 73 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a asePage(pMove);.
1754c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1754d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1754e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1754f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
17550 20 20 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d *piMoved = m
17551 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 axRootPgno;.
17552 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 }.. /* Se
17553 74 20 74 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 t the new 'max-r
17554 6f 6f 74 2d 70 61 67 65 27 20 76 61 6c 75 65 20 oot-page' value
17555 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
17556 68 65 61 64 65 72 2e 20 54 68 69 73 0a 20 20 20 header. This.
17557 20 20 20 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 ** is the old
17558 20 76 61 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c value less one,
17559 20 6c 65 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 less one more i
1755a 66 20 74 68 61 74 20 68 61 70 70 65 6e 73 20 74 f that happens t
1755b 6f 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 61 20 o. ** be a
1755c 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 root-page number
1755d 2c 20 6c 65 73 73 20 6f 6e 65 20 61 67 61 69 6e , less one again
1755e 20 69 66 20 74 68 61 74 20 69 73 20 74 68 65 0a if that is the.
1755f 20 20 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 ** PENDING
17560 5f 42 59 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 _BYTE_PAGE..
17561 20 20 2a 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f */. maxRo
17562 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 otPgno--;.
17563 69 66 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d if( maxRootPgno=
17564 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
17565 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 GE(pBt) ){.
17566 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d maxRootPgno--
17567 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17568 69 66 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d if( maxRootPgno=
17569 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 =PTRMAP_PAGENO(p
1756a 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 Bt, maxRootPgno)
1756b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52 ){. maxR
1756c 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 ootPgno--;.
1756d 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1756e 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 maxRootPgno!=PE
1756f 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
17570 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 pBt) );.. r
17571 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
17572 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c UpdateMeta(p, 4,
17573 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 maxRootPgno);.
17574 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17575 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 rc = freePage(pP
17576 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 age);. rele
17577 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
17578 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
17579 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 else{. /* If
1757a 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 sqlite3BtreeDrop
1757b 54 61 62 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 Table was called
1757c 20 6f 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20 on page 1. */.
1757d 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 zeroPage(pPag
1757e 65 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 e, PTF_INTKEY|PT
1757f 46 5f 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65 F_LEAF );. re
17580 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
17581 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
17582 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 c; .}.SQLITE_PR
17583 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
17584 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 3BtreeDropTable(
17585 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 Btree *p, int iT
17586 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 able, int *piMov
17587 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ed){. int rc;.
17588 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
17589 65 72 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d er(p);. p->pBt-
1758a 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 >db = p->db;. r
1758b 63 20 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 c = btreeDropTab
1758c 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 le(p, iTable, pi
1758d 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 Moved);. sqlite
1758e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1758f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
17590 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 ./*.** Read the
17591 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e meta-information
17592 20 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 out of a databa
17593 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 se file. Meta[0
17594 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 ].** is the numb
17595 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 er of free pages
17596 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 currently in th
17597 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 e database. Met
17598 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 a[1].** through
17599 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 meta[15] are ava
1759a 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 ilable for use b
1759b 79 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e y higher layers.
1759c 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 Meta[0].** is
1759d 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f read-only, the o
1759e 74 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 thers are read/w
1759f 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 rite..** .** The
175a0 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 schema layer nu
175a1 6d 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 mbers meta value
175a2 73 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 s differently.
175a3 41 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a At the schema.**
175a4 20 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 layer (and the
175a5 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 SetCookie and Re
175a6 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 adCookie opcodes
175a7 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a ) the number of.
175a8 2a 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73 ** free pages is
175a9 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 not visible. S
175aa 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 o Cookie[0] is t
175ab 68 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b he same as Meta[
175ac 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 1]..*/.SQLITE_PR
175ad 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
175ae 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 3BtreeGetMeta(Bt
175af 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c ree *p, int idx,
175b0 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20 u32 *pMeta){.
175b1 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 DbPage *pDbPage
175b2 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 = 0;. int rc;.
175b3 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
175b4 70 50 31 3b 0a 20 20 42 74 53 68 61 72 65 64 20 pP1;. BtShared
175b5 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a *pBt = p->pBt;..
175b6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
175b7 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 ter(p);. pBt->d
175b8 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a b = p->db;.. /*
175b9 20 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61 2d Reading a meta-
175ba 64 61 74 61 20 76 61 6c 75 65 20 72 65 71 75 69 data value requi
175bb 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 res a read-lock
175bc 6f 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20 68 on page 1 (and h
175bd 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73 71 ence. ** the sq
175be 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c lite_master tabl
175bf 65 2e 20 57 65 20 67 72 61 62 20 74 68 69 73 20 e. We grab this
175c0 6c 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73 20 lock regardless
175c1 6f 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20 20 of whether or.
175c2 2a 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49 54 ** not the SQLIT
175c3 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 E_ReadUncommitte
175c4 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 28 74 d flag is set (t
175c5 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 he table rooted
175c6 61 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20 69 at page. ** 1 i
175c7 73 20 74 72 65 61 74 65 64 20 61 73 20 61 20 73 s treated as a s
175c8 70 65 63 69 61 6c 20 63 61 73 65 20 62 79 20 71 pecial case by q
175c9 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 uerySharedCacheT
175ca 61 62 6c 65 4c 6f 63 6b 28 29 0a 20 20 2a 2a 20 ableLock(). **
175cb 61 6e 64 20 73 65 74 53 68 61 72 65 64 43 61 63 and setSharedCac
175cc 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a heTableLock())..
175cd 20 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 */. rc = quer
175ce 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
175cf 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41 44 eLock(p, 1, READ
175d0 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 63 _LOCK);. if( rc
175d1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
175d2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
175d3 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 eave(p);. ret
175d4 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 urn rc;. }.. a
175d5 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 ssert( idx>=0 &&
175d6 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 69 66 idx<=15 );. if
175d7 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 7b ( pBt->pPage1 ){
175d8 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74 72 . /* The b-tr
175d9 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 6f ee is already ho
175da 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63 lding a referenc
175db 65 20 74 6f 20 70 61 67 65 20 31 20 6f 66 20 74 e to page 1 of t
175dc 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
175dd 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69 73 ** file. In this
175de 20 63 61 73 65 20 74 68 65 20 72 65 71 75 69 72 case the requir
175df 65 64 20 6d 65 74 61 2d 64 61 74 61 20 76 61 6c ed meta-data val
175e0 75 65 20 63 61 6e 20 62 65 20 72 65 61 64 20 64 ue can be read d
175e1 69 72 65 63 74 6c 79 0a 20 20 20 20 2a 2a 20 66 irectly. ** f
175e2 72 6f 6d 20 74 68 65 20 70 61 67 65 20 64 61 74 rom the page dat
175e3 61 20 6f 66 20 74 68 69 73 20 72 65 66 65 72 65 a of this refere
175e4 6e 63 65 2e 20 54 68 69 73 20 69 73 20 73 6c 69 nce. This is sli
175e5 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 61 ghtly faster tha
175e6 6e 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 73 74 n. ** request
175e7 69 6e 67 20 61 20 6e 65 77 20 72 65 66 65 72 65 ing a new refere
175e8 6e 63 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 nce from the pag
175e9 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 2a 2f er layer.. */
175ea 0a 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73 69 . pP1 = (unsi
175eb 67 6e 65 64 20 63 68 61 72 20 2a 29 70 42 74 2d gned char *)pBt-
175ec 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a >pPage1->aData;.
175ed 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
175ee 54 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73 20 The b-tree does
175ef 6e 6f 74 20 68 61 76 65 20 61 20 72 65 66 65 72 not have a refer
175f0 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 6f ence to page 1 o
175f1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
175f2 69 6c 65 2e 0a 20 20 20 20 2a 2a 20 4f 62 74 61 ile.. ** Obta
175f3 69 6e 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 20 in one from the
175f4 70 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 pager layer..
175f5 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
175f6 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 ite3PagerGet(pBt
175f7 2d 3e 70 50 61 67 65 72 2c 20 31 2c 20 26 70 44 ->pPager, 1, &pD
175f8 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
175f9 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 rc ){. sqli
175fa 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
175fb 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
175fc 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 31 c;. }. pP1
175fd 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
175fe 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 r *)sqlite3Pager
175ff 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 GetData(pDbPage)
17600 3b 0a 20 20 7d 0a 20 20 2a 70 4d 65 74 61 20 3d ;. }. *pMeta =
17601 20 67 65 74 34 62 79 74 65 28 26 70 50 31 5b 33 get4byte(&pP1[3
17602 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 20 6 + idx*4]);..
17603 2f 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 /* If the b-tree
17604 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69 6e 67 20 is not holding
17605 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 a reference to p
17606 61 67 65 20 31 2c 20 74 68 65 6e 20 6f 6e 65 20 age 1, then one
17607 77 61 73 20 0a 20 20 2a 2a 20 72 65 71 75 65 73 was . ** reques
17608 74 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 ted from the pag
17609 65 72 20 6c 61 79 65 72 20 69 6e 20 74 68 65 20 er layer in the
1760a 61 62 6f 76 65 20 62 6c 6f 63 6b 2e 20 52 65 6c above block. Rel
1760b 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a ease it now.. *
1760c 2f 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 50 /. if( !pBt->pP
1760d 61 67 65 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 age1 ){. sqli
1760e 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 te3PagerUnref(pD
1760f 62 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f bPage);. }.. /
17610 2a 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d 65 * If autovacuume
17611 64 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e d is disabled in
17612 20 74 68 69 73 20 62 75 69 6c 64 20 62 75 74 20 this build but
17613 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f we are trying to
17614 20 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61 6e . ** access an
17615 20 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64 61 autovacuumed da
17616 74 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61 6b tabase, then mak
17617 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 e the database r
17618 65 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a 23 eadonly. . */.#
17619 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1761a 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 T_AUTOVACUUM. i
1761b 66 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70 4d f( idx==4 && *pM
1761c 65 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 61 eta>0 ) pBt->rea
1761d 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 69 dOnly = 1;.#endi
1761e 66 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65 f.. /* Grab the
1761f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70 61 read-lock on pa
17620 67 65 20 31 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 ge 1. */. rc =
17621 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 setSharedCacheTa
17622 62 6c 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 bleLock(p, 1, RE
17623 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 73 71 6c 69 AD_LOCK);. sqli
17624 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
17625 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
17626 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65 ../*.** Write me
17627 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 ta-information b
17628 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ack into the dat
17629 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20 abase. Meta[0]
1762a 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 is.** read-only
1762b 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77 and may not be w
1762c 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ritten..*/.SQLIT
1762d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1762e 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
1762f 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69 Meta(Btree *p, i
17630 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74 nt idx, u32 iMet
17631 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a a){. BtShared *
17632 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
17633 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
17634 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 P1;. int rc;.
17635 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26 assert( idx>=1 &
17636 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73 & idx<=15 );. s
17637 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
17638 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d (p);. pBt->db =
17639 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 p->db;. assert
1763a 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
1763b 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 ANS_WRITE );. a
1763c 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 ssert( pBt->pPag
1763d 65 31 21 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d e1!=0 );. pP1 =
1763e 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 pBt->pPage1->aD
1763f 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ata;. rc = sqli
17640 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 te3PagerWrite(pB
17641 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 t->pPage1->pDbPa
17642 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ge);. if( rc==S
17643 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17644 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33 36 put4byte(&pP1[36
17645 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 + idx*4], iMeta
17646 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
17647 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
17648 4d 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d 37 M. if( idx==7
17649 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
1764a 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
1764b 6d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b m || iMeta==0 );
1764c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
1764d 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 Meta==0 || iMeta
1764e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 42 74 ==1 );. pBt
1764f 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 ->incrVacuum = (
17650 75 38 29 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a u8)iMeta;. }.
17651 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c #endif. }. sql
17652 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
17653 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
17654 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
17655 74 68 65 20 66 6c 61 67 20 62 79 74 65 20 61 74 the flag byte at
17656 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
17657 66 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 f the page that
17658 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 the cursor.** is
17659 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1765a 69 6e 67 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 ing to..*/.SQLIT
1765b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1765c 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 lite3BtreeFlags(
1765d 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
1765e 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 . /* TODO: What
1765f 20 61 62 6f 75 74 20 43 55 52 53 4f 52 5f 52 45 about CURSOR_RE
17660 51 55 49 52 45 53 45 45 4b 20 73 74 61 74 65 3f QUIRESEEK state?
17661 20 50 72 6f 62 61 62 6c 79 20 6e 65 65 64 20 74 Probably need t
17662 6f 20 63 61 6c 6c 0a 20 20 2a 2a 20 72 65 73 74 o call. ** rest
17663 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
17664 6e 28 29 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 n() here.. */.
17665 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
17666 0a 20 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 . restoreCursor
17667 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
17668 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
17669 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1766a 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 63 ge];. assert( c
1766b 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
1766c 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
1766d 74 28 20 70 50 61 67 65 21 3d 30 20 29 3b 0a 20 t( pPage!=0 );.
1766e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1766f 70 42 74 3d 3d 70 43 75 72 2d 3e 70 42 74 20 29 pBt==pCur->pBt )
17670 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 ;. return pPage
17671 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
17672 64 72 4f 66 66 73 65 74 5d 3b 0a 7d 0a 0a 23 69 drOffset];.}..#i
17673 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
17674 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f 2a 0a T_BTREECOUNT./*.
17675 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 ** The first arg
17676 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69 73 20 ument, pCur, is
17677 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 a cursor opened
17678 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65 2e 20 on some b-tree.
17679 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e 75 6d Count the.** num
1767a 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
1767b 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61 6e 64 n the b-tree and
1767c 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c write the resul
1767d 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e 0a 2a t to *pnEntry..*
1767e 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 *.** SQLITE_OK i
1767f 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 s returned if th
17680 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 73 e operation is s
17681 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78 65 63 uccessfully exec
17682 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 uted. .** Otherw
17683 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 ise, if an error
17684 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 is encountered
17685 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72 72 6f (i.e. an IO erro
17686 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a 2a 2a r or database.**
17687 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61 6e 20 corruption) an
17688 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 SQLite error cod
17689 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
1768a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1768b 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1768c 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20 eCount(BtCursor
1768d 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e 45 6e *pCur, i64 *pnEn
1768e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45 6e 74 try){. i64 nEnt
1768f 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ry = 0;
17690 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17691 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 Value to return
17692 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f 0a 20 in *pnEntry */.
17693 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
17694 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17695 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
17696 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 3d 20 6d code */. rc = m
17697 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b oveToRoot(pCur);
17698 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 61 6e .. /* Unless an
17699 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 error occurs, t
1769a 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f he following loo
1769b 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65 72 61 p runs one itera
1769c 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a 20 20 tion for each.
1769d 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 42 ** page in the B
1769e 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 -Tree structure
1769f 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 6f (not including o
176a0 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29 2e 20 verflow pages).
176a1 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 . */. while( r
176a2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
176a3 20 20 20 20 69 6e 74 20 69 49 64 78 3b 20 20 20 int iIdx;
176a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
176a5 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
176a6 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20 69 6e of child node in
176a7 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 20 20 4d parent */. M
176a8 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 emPage *pPage;
176a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
176aa 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70 61 67 /* Current pag
176ab 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 e of the b-tree
176ac 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 */.. /* If th
176ad 69 73 20 69 73 20 61 20 6c 65 61 66 20 70 61 67 is is a leaf pag
176ae 65 20 6f 72 20 74 68 65 20 74 72 65 65 20 69 73 e or the tree is
176af 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b 65 79 20 not an int-key
176b0 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 20 20 20 tree, then .
176b1 2a 2a 20 74 68 69 73 20 70 61 67 65 20 63 6f 6e ** this page con
176b2 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 6c 65 20 tains countable
176b3 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 65 6d 65 entries. Increme
176b4 6e 74 20 74 68 65 20 65 6e 74 72 79 20 63 6f 75 nt the entry cou
176b5 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 63 63 6f nter. ** acco
176b6 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 2a 2f 0a rdingly.. */.
176b7 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 pPage = pCur
176b8 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
176b9 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 Page];. if( p
176ba 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 Page->leaf || !p
176bb 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a Page->intKey ){.
176bc 20 20 20 20 20 20 6e 45 6e 74 72 79 20 2b 3d 20 nEntry +=
176bd 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 pPage->nCell;.
176be 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 }.. /* pPag
176bf 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65 e is a leaf node
176c0 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61 76 69 . This loop navi
176c1 67 61 74 65 73 20 74 68 65 20 63 75 72 73 6f 72 gates the cursor
176c2 20 73 6f 20 74 68 61 74 20 69 74 20 0a 20 20 20 so that it .
176c3 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ** points to th
176c4 65 20 66 69 72 73 74 20 69 6e 74 65 72 69 6f 72 e first interior
176c5 20 63 65 6c 6c 20 74 68 61 74 20 69 74 20 70 6f cell that it po
176c6 69 6e 74 73 20 74 6f 20 74 68 65 20 70 61 72 65 ints to the pare
176c7 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 nt of. ** the
176c8 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 next page in th
176c9 65 20 74 72 65 65 20 74 68 61 74 20 68 61 73 20 e tree that has
176ca 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 76 69 73 not yet been vis
176cb 69 74 65 64 2e 20 54 68 65 0a 20 20 20 20 2a 2a ited. The. **
176cc 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
176cd 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c 75 65 20 r->iPage] value
176ce 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e is set to the in
176cf 64 65 78 20 6f 66 20 74 68 65 20 70 61 72 65 6e dex of the paren
176d0 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 6f 66 t cell. ** of
176d1 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 74 6f the page, or to
176d2 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
176d3 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61 67 65 ells in the page
176d4 20 69 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 if the next pag
176d5 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 69 73 69 e. ** to visi
176d6 74 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 t is the right-c
176d7 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65 hild of its pare
176d8 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a nt.. **. *
176d9 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 73 20 69 * If all pages i
176da 6e 20 74 68 65 20 74 72 65 65 20 68 61 76 65 20 n the tree have
176db 62 65 65 6e 20 76 69 73 69 74 65 64 2c 20 72 65 been visited, re
176dc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 74 turn SQLITE_OK t
176dd 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c o the. ** cal
176de 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ler.. */.
176df 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 if( pPage->leaf
176e0 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 ){. do {.
176e1 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e if( pCur->
176e2 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 iPage==0 ){.
176e3 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70 61 67 /* All pag
176e4 65 73 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 es of the b-tree
176e5 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 have been visit
176e6 65 64 2e 20 52 65 74 75 72 6e 20 73 75 63 63 65 ed. Return succe
176e7 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20 20 20 ssfully. */.
176e8 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79 20 3d *pnEntry =
176e9 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 20 20 20 nEntry;.
176ea 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
176eb 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 _OK;. }.
176ec 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
176ed 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 reeMoveToParent(
176ee 70 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 77 68 pCur);. }wh
176ef 69 6c 65 20 28 20 70 43 75 72 2d 3e 61 69 49 64 ile ( pCur->aiId
176f0 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d x[pCur->iPage]>=
176f1 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
176f2 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
176f3 20 29 3b 0a 0a 20 20 20 20 20 20 70 43 75 72 2d );.. pCur-
176f4 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
176f5 67 65 5d 2b 2b 3b 0a 20 20 20 20 20 20 70 50 61 ge]++;. pPa
176f6 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
176f7 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
176f8 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 }.. /* De
176f9 73 63 65 6e 64 20 74 6f 20 74 68 65 20 63 68 69 scend to the chi
176fa 6c 64 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 63 ld node of the c
176fb 65 6c 6c 20 74 68 61 74 20 74 68 65 20 63 75 72 ell that the cur
176fc 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 0a 20 sor currently .
176fd 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 61 74 2e ** points at.
176fe 20 54 68 69 73 20 69 73 20 74 68 65 20 72 69 67 This is the rig
176ff 68 74 2d 63 68 69 6c 64 20 69 66 20 28 69 49 64 ht-child if (iId
17700 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 x==pPage->nCell)
17701 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 49 64 .. */. iId
17702 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b x = pCur->aiIdx[
17703 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
17704 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50 61 67 if( iIdx==pPag
17705 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 e->nCell ){.
17706 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 rc = moveToChi
17707 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 ld(pCur, get4byt
17708 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b e(&pPage->aData[
17709 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
1770a 2b 38 5d 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 +8]));. }else
1770b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 {. rc = mov
1770c 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 eToChild(pCur, g
1770d 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c et4byte(findCell
1770e 28 70 50 61 67 65 2c 20 69 49 64 78 29 29 29 3b (pPage, iIdx)));
1770f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
17710 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 An error has oc
17711 63 75 72 72 65 64 2e 20 52 65 74 75 72 6e 20 61 curred. Return a
17712 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f n error code. */
17713 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
17714 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 #endif../*.** Re
17715 74 75 72 6e 20 74 68 65 20 70 61 67 65 72 20 61 turn the pager a
17716 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
17717 20 42 54 72 65 65 2e 20 20 54 68 69 73 20 72 6f BTree. This ro
17718 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f utine is used fo
17719 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 r.** testing and
1771a 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e debugging only.
1771b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1771c 54 45 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 TE Pager *sqlite
1771d 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 65 3BtreePager(Btre
1771e 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 e *p){. return
1771f 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a p->pBt->pPager;.
17720 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
17721 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 E_OMIT_INTEGRITY
17722 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 70 70 _CHECK./*.** App
17723 65 6e 64 20 61 20 6d 65 73 73 61 67 65 20 74 6f end a message to
17724 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
17725 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 ge string..*/.st
17726 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 atic void checkA
17727 70 70 65 6e 64 4d 73 67 28 0a 20 20 49 6e 74 65 ppendMsg(. Inte
17728 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c grityCk *pCheck,
17729 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 31 2c 0a . char *zMsg1,.
1772a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
1772b 6f 72 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a ormat,. ....){.
1772c 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
1772d 69 66 28 20 21 70 43 68 65 63 6b 2d 3e 6d 78 45 if( !pCheck->mxE
1772e 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 rr ) return;. p
1772f 43 68 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b 0a Check->mxErr--;.
17730 20 20 70 43 68 65 63 6b 2d 3e 6e 45 72 72 2b 2b pCheck->nErr++
17731 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c ;. va_start(ap,
17732 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 69 66 28 zFormat);. if(
17733 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e pCheck->errMsg.
17734 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 73 71 6c nChar ){. sql
17735 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
17736 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d nd(&pCheck->errM
17737 73 67 2c 20 22 5c 6e 22 2c 20 31 29 3b 0a 20 20 sg, "\n", 1);.
17738 7d 0a 20 20 69 66 28 20 7a 4d 73 67 31 20 29 7b }. if( zMsg1 ){
17739 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 . sqlite3StrA
1773a 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 ccumAppend(&pChe
1773b 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 7a 4d 73 67 ck->errMsg, zMsg
1773c 31 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 1, -1);. }. sq
1773d 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 70 lite3VXPrintf(&p
1773e 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 31 Check->errMsg, 1
1773f 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
17740 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
17741 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d if( pCheck->errM
17742 73 67 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 sg.mallocFailed
17743 29 7b 0a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d ){. pCheck->m
17744 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
17745 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.}.#endif /*
17746 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
17747 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a EGRITY_CHECK */.
17748 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
17749 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 OMIT_INTEGRITY_C
1774a 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 64 64 20 31 HECK./*.** Add 1
1774b 20 74 6f 20 74 68 65 20 72 65 66 65 72 65 6e 63 to the referenc
1774c 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 e count for page
1774d 20 69 50 61 67 65 2e 20 20 49 66 20 74 68 69 73 iPage. If this
1774e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a is the second.*
1774f 2a 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 * reference to t
17750 68 65 20 70 61 67 65 2c 20 61 64 64 20 61 6e 20 he page, add an
17751 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f error message to
17752 20 70 43 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67 pCheck->zErrMsg
17753 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 ..** Return 1 if
17754 20 74 68 65 72 65 20 61 72 65 20 32 20 6f 72 65 there are 2 ore
17755 20 6d 6f 72 65 20 72 65 66 65 72 65 6e 63 65 73 more references
17756 20 74 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64 to the page and
17757 20 30 20 69 66 0a 2a 2a 20 69 66 20 74 68 69 73 0 if.** if this
17758 20 69 73 20 74 68 65 20 66 69 72 73 74 20 72 65 is the first re
17759 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70 ference to the p
1775a 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 age..**.** Also
1775b 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 check that the p
1775c 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 69 6e age number is in
1775d 20 62 6f 75 6e 64 73 2e 0a 2a 2f 0a 73 74 61 74 bounds..*/.stat
1775e 69 63 20 69 6e 74 20 63 68 65 63 6b 52 65 66 28 ic int checkRef(
1775f 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 IntegrityCk *pCh
17760 65 63 6b 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c eck, Pgno iPage,
17761 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 29 char *zContext)
17762 7b 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 {. if( iPage==0
17763 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 ) return 1;. i
17764 66 28 20 69 50 61 67 65 3e 70 43 68 65 63 6b 2d f( iPage>pCheck-
17765 3e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 63 68 >nPage ){. ch
17766 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
17767 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 eck, zContext, "
17768 69 6e 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d invalid page num
17769 62 65 72 20 25 64 22 2c 20 69 50 61 67 65 29 3b ber %d", iPage);
1776a 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
1776b 20 7d 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d }. if( pCheck-
1776c 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 3d 3d 31 >anRef[iPage]==1
1776d 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 ){. checkApp
1776e 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
1776f 43 6f 6e 74 65 78 74 2c 20 22 32 6e 64 20 72 65 Context, "2nd re
17770 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 ference to page
17771 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 %d", iPage);.
17772 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
17773 20 72 65 74 75 72 6e 20 20 28 70 43 68 65 63 6b return (pCheck
17774 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 2b 2b ->anRef[iPage]++
17775 29 3e 31 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 )>1;.}..#ifndef
17776 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
17777 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 68 65 VACUUM./*.** Che
17778 63 6b 20 74 68 61 74 20 74 68 65 20 65 6e 74 72 ck that the entr
17779 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 y in the pointer
1777a 2d 6d 61 70 20 66 6f 72 20 70 61 67 65 20 69 43 -map for page iC
1777b 68 69 6c 64 20 6d 61 70 73 20 74 6f 20 0a 2a 2a hild maps to .**
1777c 20 70 61 67 65 20 69 50 61 72 65 6e 74 2c 20 70 page iParent, p
1777d 6f 69 6e 74 65 72 20 74 79 70 65 20 70 74 72 54 ointer type ptrT
1777e 79 70 65 2e 20 49 66 20 6e 6f 74 2c 20 61 70 70 ype. If not, app
1777f 65 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 end an error mes
17780 73 61 67 65 0a 2a 2a 20 74 6f 20 70 43 68 65 63 sage.** to pChec
17781 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 k..*/.static voi
17782 64 20 63 68 65 63 6b 50 74 72 6d 61 70 28 0a 20 d checkPtrmap(.
17783 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 IntegrityCk *pC
17784 68 65 63 6b 2c 20 20 20 2f 2a 20 49 6e 74 65 67 heck, /* Integ
17785 72 69 74 79 20 63 68 65 63 6b 20 63 6f 6e 74 65 rity check conte
17786 78 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 43 68 xt */. Pgno iCh
17787 69 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f ild, /
17788 2a 20 43 68 69 6c 64 20 70 61 67 65 20 6e 75 6d * Child page num
17789 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 ber */. u8 eTyp
1778a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
1778b 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e /* Expected poin
1778c 74 65 72 20 6d 61 70 20 74 79 70 65 20 2a 2f 0a ter map type */.
1778d 20 20 50 67 6e 6f 20 69 50 61 72 65 6e 74 2c 20 Pgno iParent,
1778e 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 /* Expe
1778f 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 cted pointer map
17790 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d parent page num
17791 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ber */. char *z
17792 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20 20 Context
17793 2f 2a 20 43 6f 6e 74 65 78 74 20 64 65 73 63 72 /* Context descr
17794 69 70 74 69 6f 6e 20 28 75 73 65 64 20 66 6f 72 iption (used for
17795 20 65 72 72 6f 72 20 6d 73 67 29 20 2a 2f 0a 29 error msg) */.)
17796 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 38 {. int rc;. u8
17797 20 65 50 74 72 6d 61 70 54 79 70 65 3b 0a 20 20 ePtrmapType;.
17798 50 67 6e 6f 20 69 50 74 72 6d 61 70 50 61 72 65 Pgno iPtrmapPare
17799 6e 74 3b 0a 0a 20 20 72 63 20 3d 20 70 74 72 6d nt;.. rc = ptrm
1779a 61 70 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 42 apGet(pCheck->pB
1779b 74 2c 20 69 43 68 69 6c 64 2c 20 26 65 50 74 72 t, iChild, &ePtr
1779c 6d 61 70 54 79 70 65 2c 20 26 69 50 74 72 6d 61 mapType, &iPtrma
1779d 70 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 pParent);. if(
1779e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1779f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
177a0 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65 ITE_NOMEM ) pChe
177a1 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 ck->mallocFailed
177a2 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41 = 1;. checkA
177a3 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
177a4 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c zContext, "Fail
177a5 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61 ed to read ptrma
177a6 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c p key=%d", iChil
177a7 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a d);. return;.
177a8 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d }.. if( ePtrm
177a9 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c apType!=eType ||
177aa 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d iPtrmapParent!=
177ab 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 iParent ){. c
177ac 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
177ad 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
177ae 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20 . "Bad ptr
177af 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 map entry key=%d
177b0 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64 expected=(%d,%d
177b1 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 ) got=(%d,%d)",
177b2 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65 . iChild, e
177b3 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 Type, iParent, e
177b4 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72 PtrmapType, iPtr
177b5 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a mapParent);. }.
177b6 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
177b7 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 Check the integr
177b8 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c ity of the freel
177b9 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 ist or of an ove
177ba 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e rflow page list.
177bb 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 .** Verify that
177bc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
177bd 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 ges on the list
177be 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 is N..*/.static
177bf 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a void checkList(.
177c0 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 IntegrityCk *p
177c1 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 Check, /* Integ
177c2 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f rity checking co
177c3 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ntext */. int i
177c4 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20 sFreeList,
177c5 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66 /* True for a f
177c6 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 reelist. False
177c7 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 for overflow pag
177c8 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 e list */. int
177c9 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 iPage,
177ca 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
177cb 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20 for first page
177cc 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 in the list */.
177cd 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 int N,
177ce 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 /* Expect
177cf 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 ed number of pag
177d0 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a es in the list *
177d1 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 /. char *zConte
177d2 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e xt /* Con
177d3 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d text for error m
177d4 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 essages */.){.
177d5 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 int i;. int exp
177d6 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 ected = N;. int
177d7 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b iFirst = iPage;
177d8 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 . while( N-- >
177d9 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 0 && pCheck->mxE
177da 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 rr ){. DbPage
177db 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 *pOvflPage;.
177dc 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
177dd 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 pOvflData;. i
177de 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 f( iPage<1 ){.
177df 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
177e0 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
177e1 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25 ext,. "%
177e2 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69 d of %d pages mi
177e3 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 ssing from overf
177e4 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e low list startin
177e5 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20 g at %d",.
177e6 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 N+1, expecte
177e7 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20 d, iFirst);.
177e8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
177e9 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 if( checkRef(
177ea 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a pCheck, iPage, z
177eb 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b Context) ) break
177ec 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
177ed 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b 3PagerGet(pCheck
177ee 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 ->pPager, (Pgno)
177ef 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 iPage, &pOvflPag
177f0 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 e) ){. chec
177f1 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
177f2 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 k, zContext, "fa
177f3 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65 iled to get page
177f4 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 %d", iPage);.
177f5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
177f6 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d . pOvflData =
177f7 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
177f8 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
177f9 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29 tData(pOvflPage)
177fa 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65 ;. if( isFree
177fb 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e List ){. in
177fc 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 t n = get4byte(&
177fd 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 pOvflData[4]);.#
177fe 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
177ff 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
17800 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e if( pCheck->
17801 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
17802 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
17803 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 Ptrmap(pCheck, i
17804 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
17805 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 EPAGE, 0, zConte
17806 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
17807 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e dif. if( n>
17808 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 pCheck->pBt->usa
17809 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 bleSize/4-2 ){.
1780a 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 checkAppe
1780b 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
1780c 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 ontext,.
1780d 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61 "freelist lea
1780e 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 f count too big
1780f 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 on page %d", iPa
17810 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d ge);. N--
17811 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
17812 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 for(i=0;
17813 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 i<n; i++){.
17814 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 Pgno iFreeP
17815 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 age = get4byte(&
17816 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d pOvflData[8+i*4]
17817 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
17818 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
17819 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 M. if(
1781a 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 pCheck->pBt->aut
1781b 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
1781c 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
1781d 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 ap(pCheck, iFree
1781e 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
1781f 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 EPAGE, 0, zConte
17820 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d xt);. }
17821 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
17822 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 checkRef(pChec
17823 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43 k, iFreePage, zC
17824 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 ontext);.
17825 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 }. N -=
17826 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d n;. }. }
17827 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
17828 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
17829 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 else{.
1782a 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 /* If this datab
1782b 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 ase supports aut
1782c 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61 o-vacuum and iPa
1782d 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 ge is not the la
1782e 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 st. ** page
1782f 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f in this overflo
17830 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68 w list, check th
17831 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d at the pointer-m
17832 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 ap entry for.
17833 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 ** the follow
17834 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73 ing page matches
17835 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f iPage.. */
17836 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63 . if( pChec
17837 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 k->pBt->autoVacu
17838 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 um && N>0 ){.
17839 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74 i = get4byt
1783a 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 e(pOvflData);.
1783b 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
1783c 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 p(pCheck, i, PTR
1783d 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 MAP_OVERFLOW2, i
1783e 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b Page, zContext);
1783f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
17840 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20 endif. iPage
17841 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c = get4byte(pOvfl
17842 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 Data);. sqlit
17843 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 e3PagerUnref(pOv
17844 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 flPage);. }.}.#
17845 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
17846 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 OMIT_INTEGRITY_C
17847 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 HECK */..#ifndef
17848 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
17849 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a EGRITY_CHECK./*.
1784a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61 ** Do various sa
1784b 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 nity checks on a
1784c 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 single page of
1784d 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a a tree. Return.
1784e 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74 ** the tree dept
1784f 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72 h. Root pages r
17850 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 eturn 0. Parent
17851 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a s of root pages.
17852 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 ** return 1, and
17853 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a so forth..** .*
17854 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61 * These checks a
17855 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 re done:.**.**
17856 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 1. Make sur
17857 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 e that cells and
17858 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e freeblocks do n
17859 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 ot overlap.**
1785a 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69 but combi
1785b 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 ne to completely
1785c 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e cover the page.
1785d 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b .** NO 2. Mak
1785e 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 e sure cell keys
1785f 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a are in order..*
17860 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 * NO 3. Make
17861 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c sure no key is l
17862 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
17863 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 l to zLowerBound
17864 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 ..** NO 4. Ma
17865 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 ke sure no key i
17866 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
17867 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 r equal to zUppe
17868 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 rBound..**
17869 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e 5. Check the in
1786a 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66 tegrity of overf
1786b 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 low pages..**
1786c 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65 6. Recursive
1786d 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 ly call checkTre
1786e 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 ePage on all chi
1786f 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 ldren..** 7
17870 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74 . Verify that t
17871 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 he depth of all
17872 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20 children is the
17873 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e same..** 8.
17874 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 Make sure this
17875 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73 page is at leas
17876 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c t 33% full or el
17877 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 se it is.**
17878 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 the root of
17879 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 the tree..*/.st
1787a 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72 atic int checkTr
1787b 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72 eePage(. Integr
1787c 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 ityCk *pCheck,
1787d 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 /* Context for t
1787e 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 he sanity check
1787f 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20 */. int iPage,
17880 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
17881 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
17882 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a page to check *
17883 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e /. char *zParen
17884 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 tContext /* Par
17885 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 ent context */.)
17886 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
17887 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c ge;. int i, rc,
17888 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f depth, d2, pgno
17889 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 , cnt;. int hdr
1788a 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 , cellStart;. i
1788b 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a nt nCell;. u8 *
1788c 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 data;. BtShared
1788d 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 *pBt;. int usa
1788e 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 bleSize;. char
1788f 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 zContext[100];.
17890 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a char *hit = 0;.
17891 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
17892 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 ntf(sizeof(zCont
17893 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 ext), zContext,
17894 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61 "Page %d: ", iPa
17895 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ge);.. /* Check
17896 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65 that the page e
17897 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 xists. */. pBt
17898 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a = pCheck->pBt;.
17899 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 usableSize = p
1789a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
1789b 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 if( iPage==0 )
1789c 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
1789d 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b checkRef(pCheck
1789e 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 , iPage, zParent
1789f 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72 Context) ) retur
178a0 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d n 0;. if( (rc =
178a1 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
178a2 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 Page(pBt, (Pgno)
178a3 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 iPage, &pPage, 0
178a4 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 ))!=0 ){. if(
178a5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc==SQLITE_NOME
178a6 4d 20 29 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c M ) pCheck->mall
178a7 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
178a8 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
178a9 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
178aa 74 2c 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c t,. "unabl
178ab 65 20 74 6f 20 67 65 74 20 74 68 65 20 70 61 67 e to get the pag
178ac 65 2e 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 e. error code=%d
178ad 22 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75 ", rc);. retu
178ae 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
178af 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 (rc = sqlite3Btr
178b0 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 eeInitPage(pPage
178b1 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 ))!=0 ){. ass
178b2 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
178b3 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a 20 54 CORRUPT ); /* T
178b4 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 he only possible
178b5 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e 69 74 error from Init
178b6 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 65 63 Page */. chec
178b7 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
178b8 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 k, zContext, .
178b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
178ba 20 22 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e "sqlite3BtreeIn
178bb 69 74 50 61 67 65 28 29 20 72 65 74 75 72 6e 73 itPage() returns
178bc 20 65 72 72 6f 72 20 63 6f 64 65 20 25 64 22 2c error code %d",
178bd 20 72 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 rc);. releas
178be 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
178bf 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
178c0 0a 20 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 . /* Check out
178c1 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 all the cells..
178c2 20 2a 2f 0a 20 20 64 65 70 74 68 20 3d 20 30 3b */. depth = 0;
178c3 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 . for(i=0; i<pP
178c4 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 age->nCell && pC
178c5 68 65 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b heck->mxErr; i++
178c6 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c ){. u8 *pCell
178c7 3b 0a 20 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 ;. u32 sz;.
178c8 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b CellInfo info;
178c9 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 .. /* Check p
178ca 61 79 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 ayload overflow
178cb 70 61 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 pages. */.
178cc 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
178cd 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 f(sizeof(zContex
178ce 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 t), zContext,.
178cf 20 20 20 20 20 20 20 20 20 20 20 22 4f 6e 20 74 "On t
178d0 72 65 65 20 70 61 67 65 20 25 64 20 63 65 6c 6c ree page %d cell
178d1 20 25 64 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 %d: ", iPage, i
178d2 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 );. pCell = f
178d3 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 indCell(pPage,i)
178d4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
178d5 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
178d6 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
178d7 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e fo);. sz = in
178d8 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 20 69 66 fo.nData;. if
178d9 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 ( !pPage->intKey
178da 20 29 20 73 7a 20 2b 3d 20 28 69 6e 74 29 69 6e ) sz += (int)in
178db 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 61 73 73 fo.nKey;. ass
178dc 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 ert( sz==info.nP
178dd 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20 20 69 66 ayload );. if
178de 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 ( (sz>info.nLoca
178df 6c 29 20 0a 20 20 20 20 20 26 26 20 28 26 70 43 l) . && (&pC
178e0 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c ell[info.iOverfl
178e1 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e 61 44 61 ow]<=&pPage->aDa
178e2 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 ta[pBt->usableSi
178e3 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 ze]). ){.
178e4 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 28 73 int nPage = (s
178e5 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 z - info.nLocal
178e6 2b 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 35 + usableSize - 5
178e7 29 2f 28 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 )/(usableSize -
178e8 34 29 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 4);. Pgno p
178e9 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 gnoOvfl = get4by
178ea 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 te(&pCell[info.i
178eb 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66 6e Overflow]);.#ifn
178ec 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
178ed 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
178ee 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
178ef 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 cuum ){.
178f0 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 checkPtrmap(pChe
178f1 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 ck, pgnoOvfl, PT
178f2 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 RMAP_OVERFLOW1,
178f3 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 iPage, zContext)
178f4 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
178f5 0a 20 20 20 20 20 20 63 68 65 63 6b 4c 69 73 74 . checkList
178f6 28 70 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e 6f (pCheck, 0, pgno
178f7 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43 6f Ovfl, nPage, zCo
178f8 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 ntext);. }..
178f9 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e 69 /* Check sani
178fa 74 79 20 6f 66 20 6c 65 66 74 20 63 68 69 6c 64 ty of left child
178fb 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
178fc 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
178fd 61 66 20 29 7b 0a 20 20 20 20 20 20 70 67 6e 6f af ){. pgno
178fe 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c = get4byte(pCel
178ff 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 l);.#ifndef SQLI
17900 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
17901 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 UM. if( pBt
17902 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
17903 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 checkPtr
17904 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f map(pCheck, pgno
17905 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 , PTRMAP_BTREE,
17906 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 iPage, zContext)
17907 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
17908 0a 20 20 20 20 20 20 64 32 20 3d 20 63 68 65 63 . d2 = chec
17909 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 63 6b kTreePage(pCheck
1790a 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 74 , pgno, zContext
1790b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3e 30 );. if( i>0
1790c 20 26 26 20 64 32 21 3d 64 65 70 74 68 20 29 7b && d2!=depth ){
1790d 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 . checkAp
1790e 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
1790f 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 69 6c 64 zContext, "Child
17910 20 70 61 67 65 20 64 65 70 74 68 20 64 69 66 66 page depth diff
17911 65 72 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ers");. }.
17912 20 20 20 20 20 64 65 70 74 68 20 3d 20 64 32 3b depth = d2;
17913 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
17914 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
17915 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 . pgno = get4
17916 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
17917 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
17918 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 73 71 6c set+8]);. sql
17919 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
1791a 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 zeof(zContext),
1791b 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 zContext, .
1791c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1791d 22 4f 6e 20 70 61 67 65 20 25 64 20 61 74 20 72 "On page %d at r
1791e 69 67 68 74 20 63 68 69 6c 64 3a 20 22 2c 20 69 ight child: ", i
1791f 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 Page);.#ifndef S
17920 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
17921 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 ACUUM. if( pB
17922 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
17923 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d . checkPtrm
17924 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c ap(pCheck, pgno,
17925 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69 PTRMAP_BTREE, i
17926 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a Page, 0);. }.
17927 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b #endif. check
17928 54 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c TreePage(pCheck,
17929 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 74 29 pgno, zContext)
1792a 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 43 68 65 ;. }. . /* Che
1792b 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 20 ck for complete
1792c 63 6f 76 65 72 61 67 65 20 6f 66 20 74 68 65 20 coverage of the
1792d 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64 61 74 61 page. */. data
1792e 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
1792f 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e . hdr = pPage->
17930 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 68 69 74 hdrOffset;. hit
17931 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 = sqlite3PageMa
17932 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 lloc( pBt->pageS
17933 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 68 69 74 ize );. if( hit
17934 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 68 65 63 ==0 ){. pChec
17935 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 k->mallocFailed
17936 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 1;. }else{.
17937 20 20 75 31 36 20 63 6f 6e 74 65 6e 74 4f 66 66 u16 contentOff
17938 73 65 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 set = get2byte(&
17939 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 data[hdr+5]);.
1793a 20 20 69 66 20 28 63 6f 6e 74 65 6e 74 4f 66 66 if (contentOff
1793b 73 65 74 20 3e 20 75 73 61 62 6c 65 53 69 7a 65 set > usableSize
1793c 29 20 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 ) {. checkA
1793d 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
1793e 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 0, .
1793f 20 20 20 20 20 20 20 20 20 20 22 43 6f 72 72 75 "Corru
17940 70 74 69 6f 6e 20 64 65 74 65 63 74 65 64 20 69 ption detected i
17941 6e 20 68 65 61 64 65 72 20 6f 6e 20 70 61 67 65 n header on page
17942 20 25 64 22 2c 69 50 61 67 65 2c 30 29 3b 0a 20 %d",iPage,0);.
17943 20 20 20 20 20 67 6f 74 6f 20 63 68 65 63 6b 5f goto check_
17944 70 61 67 65 5f 61 62 6f 72 74 3b 0a 20 20 20 20 page_abort;.
17945 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 }. memset(hit
17946 2b 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20 +contentOffset,
17947 30 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f 0, usableSize-co
17948 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 ntentOffset);.
17949 20 20 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c memset(hit, 1,
1794a 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b contentOffset);
1794b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 . nCell = get
1794c 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
1794d 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61 3]);. cellSta
1794e 72 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 rt = hdr + 12 -
1794f 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 4*pPage->leaf;.
17950 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 for(i=0; i<nC
17951 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 ell; i++){.
17952 20 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 int pc = get2by
17953 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 te(&data[cellSta
17954 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 rt+i*2]);.
17955 75 31 36 20 73 69 7a 65 20 3d 20 31 30 32 34 3b u16 size = 1024;
17956 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 . int j;.
17957 20 20 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62 if( pc<=usab
17958 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
17959 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a size = cellSiz
1795a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 ePtr(pPage, &dat
1795b 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a a[pc]);. }.
1795c 20 20 20 20 20 20 69 66 28 20 28 70 63 2b 73 69 if( (pc+si
1795d 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a ze-1)>=usableSiz
1795e 65 20 7c 7c 20 70 63 3c 30 20 29 7b 0a 20 20 20 e || pc<0 ){.
1795f 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
17960 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a Msg(pCheck, 0, .
17961 20 20 20 20 20 20 20 20 20 20 20 20 22 43 6f 72 "Cor
17962 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 65 64 ruption detected
17963 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f 6e 20 70 in cell %d on p
17964 61 67 65 20 25 64 22 2c 69 2c 69 50 61 67 65 2c age %d",i,iPage,
17965 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0);. }else{
17966 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 . for(j=p
17967 63 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 70 63 3b c+size-1; j>=pc;
17968 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a j--) hit[j]++;.
17969 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1796a 20 20 66 6f 72 28 63 6e 74 3d 30 2c 20 69 3d 67 for(cnt=0, i=g
1796b 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
1796c 72 2b 31 5d 29 3b 20 69 3e 30 20 26 26 20 69 3c r+1]); i>0 && i<
1796d 75 73 61 62 6c 65 53 69 7a 65 20 26 26 20 63 6e usableSize && cn
1796e 74 3c 31 30 30 30 30 3b 20 0a 20 20 20 20 20 20 t<10000; .
1796f 20 20 20 20 20 63 6e 74 2b 2b 29 7b 0a 20 20 20 cnt++){.
17970 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 67 65 int size = ge
17971 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 t2byte(&data[i+2
17972 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b ]);. int j;
17973 0a 20 20 20 20 20 20 69 66 28 20 28 69 2b 73 69 . if( (i+si
17974 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a ze-1)>=usableSiz
17975 65 20 7c 7c 20 69 3c 30 20 29 7b 0a 20 20 20 20 e || i<0 ){.
17976 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
17977 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 20 0a sg(pCheck, 0, .
17978 20 20 20 20 20 20 20 20 20 20 20 20 22 43 6f 72 "Cor
17979 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 65 64 ruption detected
1797a 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f 6e 20 70 in cell %d on p
1797b 61 67 65 20 25 64 22 2c 69 2c 69 50 61 67 65 2c age %d",i,iPage,
1797c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0);. }else{
1797d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 69 . for(j=i
1797e 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 69 3b 20 6a +size-1; j>=i; j
1797f 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 --) hit[j]++;.
17980 20 20 20 20 7d 0a 20 20 20 20 20 20 69 20 3d 20 }. i =
17981 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 get2byte(&data[i
17982 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f ]);. }. fo
17983 72 28 69 3d 63 6e 74 3d 30 3b 20 69 3c 75 73 61 r(i=cnt=0; i<usa
17984 62 6c 65 53 69 7a 65 3b 20 69 2b 2b 29 7b 0a 20 bleSize; i++){.
17985 20 20 20 20 20 69 66 28 20 68 69 74 5b 69 5d 3d if( hit[i]=
17986 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6e =0 ){. cn
17987 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 t++;. }else
17988 20 69 66 28 20 68 69 74 5b 69 5d 3e 31 20 29 7b if( hit[i]>1 ){
17989 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 . checkAp
1798a 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
1798b 30 2c 0a 20 20 20 20 20 20 20 20 20 20 22 4d 75 0,. "Mu
1798c 6c 74 69 70 6c 65 20 75 73 65 73 20 66 6f 72 20 ltiple uses for
1798d 62 79 74 65 20 25 64 20 6f 66 20 70 61 67 65 20 byte %d of page
1798e 25 64 22 2c 20 69 2c 20 69 50 61 67 65 29 3b 0a %d", i, iPage);.
1798f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
17990 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
17991 20 69 66 28 20 63 6e 74 21 3d 64 61 74 61 5b 68 if( cnt!=data[h
17992 64 72 2b 37 5d 20 29 7b 0a 20 20 20 20 20 20 63 dr+7] ){. c
17993 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
17994 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 heck, 0, .
17995 20 20 20 20 22 46 72 61 67 6d 65 6e 74 65 64 20 "Fragmented
17996 73 70 61 63 65 20 69 73 20 25 64 20 62 79 74 65 space is %d byte
17997 20 72 65 70 6f 72 74 65 64 20 61 73 20 25 64 20 reported as %d
17998 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a 20 20 20 on page %d",.
17999 20 20 20 20 20 20 20 63 6e 74 2c 20 64 61 74 61 cnt, data
1799a 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67 65 29 3b [hdr+7], iPage);
1799b 0a 20 20 20 20 7d 0a 20 20 7d 0a 63 68 65 63 6b . }. }.check
1799c 5f 70 61 67 65 5f 61 62 6f 72 74 3a 0a 20 20 69 _page_abort:. i
1799d 66 20 28 68 69 74 29 20 73 71 6c 69 74 65 33 50 f (hit) sqlite3P
1799e 61 67 65 46 72 65 65 28 68 69 74 29 3b 0a 0a 20 ageFree(hit);..
1799f 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
179a0 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 65 ge);. return de
179a1 70 74 68 2b 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 pth+1;.}.#endif
179a2 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 /* SQLITE_OMIT_I
179a3 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a NTEGRITY_CHECK *
179a4 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
179a5 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 E_OMIT_INTEGRITY
179a6 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 _CHECK./*.** Thi
179a7 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 s routine does a
179a8 20 63 6f 6d 70 6c 65 74 65 20 63 68 65 63 6b 20 complete check
179a9 6f 66 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 of the given BTr
179aa 65 65 20 66 69 6c 65 2e 20 20 61 52 6f 6f 74 5b ee file. aRoot[
179ab 5d 20 69 73 0a 2a 2a 20 61 6e 20 61 72 72 61 79 ] is.** an array
179ac 20 6f 66 20 70 61 67 65 73 20 6e 75 6d 62 65 72 of pages number
179ad 73 20 77 65 72 65 20 65 61 63 68 20 70 61 67 65 s were each page
179ae 20 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 72 number is the r
179af 6f 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20 61 oot page of.** a
179b0 20 74 61 62 6c 65 2e 20 20 6e 52 6f 6f 74 20 69 table. nRoot i
179b1 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
179b2 65 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 entries in aRoot
179b3 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 ..**.** Write th
179b4 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f e number of erro
179b5 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e 45 72 72 r seen in *pnErr
179b6 2e 20 20 45 78 63 65 70 74 20 66 6f 72 20 73 6f . Except for so
179b7 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c me memory.** all
179b8 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 2c 20 ocation errors,
179b9 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
179ba 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 e held in memory
179bb 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
179bc 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 72 65 74 75 * malloc is retu
179bd 72 6e 65 64 20 69 66 20 2a 70 6e 45 72 72 20 69 rned if *pnErr i
179be 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 s non-zero. If
179bf 2a 70 6e 45 72 72 3d 3d 30 20 74 68 65 6e 20 4e *pnErr==0 then N
179c0 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e ULL is.** return
179c1 65 64 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 ed. If a memory
179c2 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
179c3 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 r occurs, NULL i
179c4 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 s returned..*/.S
179c5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
179c6 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 ar *sqlite3Btree
179c7 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 0a IntegrityCheck(.
179c8 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 Btree *p,
179c9 2f 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20 /* The btree to
179ca 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 be checked */.
179cb 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20 2f 2a int *aRoot, /*
179cc 20 41 6e 20 61 72 72 61 79 20 6f 66 20 72 6f 6f An array of roo
179cd 74 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 t pages numbers
179ce 66 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74 for individual t
179cf 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 rees */. int nR
179d0 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 oot, /* Numbe
179d1 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
179d2 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 aRoot[] */. int
179d3 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20 53 74 mxErr, /* St
179d4 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 65 72 72 op reporting err
179d5 6f 72 73 20 61 66 74 65 72 20 74 68 69 73 20 6d ors after this m
179d6 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e any */. int *pn
179d7 45 72 72 20 20 20 20 2f 2a 20 57 72 69 74 65 20 Err /* Write
179d8 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 number of errors
179d9 20 73 65 65 6e 20 74 6f 20 74 68 69 73 20 76 61 seen to this va
179da 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 50 riable */.){. P
179db 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65 gno i;. int nRe
179dc 66 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b f;. IntegrityCk
179dd 20 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68 61 sCheck;. BtSha
179de 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
179df 74 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 t;. char zErr[1
179e0 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 00];.. sqlite3B
179e1 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
179e2 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b pBt->db = p->db;
179e3 0a 20 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 . nRef = sqlite
179e4 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 3PagerRefcount(p
179e5 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69 Bt->pPager);. i
179e6 66 28 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 f( lockBtreeWith
179e7 52 65 74 72 79 28 70 29 21 3d 53 51 4c 49 54 45 Retry(p)!=SQLITE
179e8 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 6e 45 72 _OK ){. *pnEr
179e9 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 r = 1;. sqlit
179ea 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
179eb 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
179ec 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 22 te3DbStrDup(0, "
179ed 63 61 6e 6e 6f 74 20 61 63 71 75 69 72 65 20 61 cannot acquire a
179ee 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 read lock on th
179ef 65 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 e database");.
179f0 7d 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d }. sCheck.pBt =
179f1 20 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 pBt;. sCheck.p
179f2 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 Pager = pBt->pPa
179f3 67 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 ger;. sCheck.nP
179f4 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 age = pagerPagec
179f5 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 ount(sCheck.pBt)
179f6 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 ;. sCheck.mxErr
179f7 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 = mxErr;. sChe
179f8 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 ck.nErr = 0;. s
179f9 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c Check.mallocFail
179fa 65 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 ed = 0;. *pnErr
179fb 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65 = 0;. if( sChe
179fc 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 ck.nPage==0 ){.
179fd 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 unlockBtreeIf
179fe 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 Unused(pBt);.
179ff 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
17a00 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 ve(p);. retur
17a01 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 n 0;. }. sChec
17a02 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 k.anRef = sqlite
17a03 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63 6b 3Malloc( (sCheck
17a04 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f 66 .nPage+1)*sizeof
17a05 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 5d (sCheck.anRef[0]
17a06 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68 65 ) );. if( !sChe
17a07 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20 20 ck.anRef ){.
17a08 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 unlockBtreeIfUnu
17a09 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 2a 70 sed(pBt);. *p
17a0a 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 nErr = 1;. sq
17a0b 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
17a0c 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 p);. return 0
17a0d 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b ;. }. for(i=0;
17a0e 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 i<=sCheck.nPage
17a0f 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63 6b 2e 61 ; i++){ sCheck.a
17a10 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20 7d 0a 20 nRef[i] = 0; }.
17a11 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 i = PENDING_BYT
17a12 45 5f 50 41 47 45 28 70 42 74 29 3b 0a 20 20 69 E_PAGE(pBt);. i
17a13 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 f( i<=sCheck.nPa
17a14 67 65 20 29 7b 0a 20 20 20 20 73 43 68 65 63 6b ge ){. sCheck
17a15 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31 3b 0a 20 .anRef[i] = 1;.
17a16 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 }. sqlite3StrA
17a17 63 63 75 6d 49 6e 69 74 28 26 73 43 68 65 63 6b ccumInit(&sCheck
17a18 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72 2c 20 73 .errMsg, zErr, s
17a19 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 32 30 30 izeof(zErr), 200
17a1a 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 00);.. /* Check
17a1b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f the integrity o
17a1c 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 0a 20 f the freelist.
17a1d 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 73 74 28 */. checkList(
17a1e 26 73 43 68 65 63 6b 2c 20 31 2c 20 67 65 74 34 &sCheck, 1, get4
17a1f 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 byte(&pBt->pPage
17a20 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 2c 0a 20 1->aData[32]),.
17a21 20 20 20 20 20 20 20 20 20 20 20 67 65 74 34 62 get4b
17a22 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 yte(&pBt->pPage1
17a23 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c 20 22 4d ->aData[36]), "M
17a24 61 69 6e 20 66 72 65 65 6c 69 73 74 3a 20 22 29 ain freelist: ")
17a25 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 61 6c ;.. /* Check al
17a26 6c 20 74 68 65 20 74 61 62 6c 65 73 2e 0a 20 20 l the tables..
17a27 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 69 */. for(i=0; (i
17a28 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26 20 73 43 nt)i<nRoot && sC
17a29 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29 heck.mxErr; i++)
17a2a 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f 74 5b {. if( aRoot[
17a2b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 i]==0 ) continue
17a2c 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
17a2d 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
17a2e 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 . if( pBt->au
17a2f 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52 6f 6f toVacuum && aRoo
17a30 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 t[i]>1 ){.
17a31 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73 43 68 checkPtrmap(&sCh
17a32 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 50 eck, aRoot[i], P
17a33 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 TRMAP_ROOTPAGE,
17a34 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 0, 0);. }.#en
17a35 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65 dif. checkTre
17a36 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c 20 61 ePage(&sCheck, a
17a37 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 74 20 6f Root[i], "List o
17a38 66 20 74 72 65 65 20 72 6f 6f 74 73 3a 20 22 29 f tree roots: ")
17a39 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
17a3a 20 73 75 72 65 20 65 76 65 72 79 20 70 61 67 65 sure every page
17a3b 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 in the file is
17a3c 72 65 66 65 72 65 6e 63 65 64 0a 20 20 2a 2f 0a referenced. */.
17a3d 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 43 for(i=1; i<=sC
17a3e 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 20 73 43 heck.nPage && sC
17a3f 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29 heck.mxErr; i++)
17a40 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
17a41 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
17a42 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 if( sCheck.a
17a43 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 nRef[i]==0 ){.
17a44 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
17a45 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 sg(&sCheck, 0, "
17a46 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 Page %d is never
17a47 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 used", i);.
17a48 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 49 }.#else. /* I
17a49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 f the database s
17a4a 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 upports auto-vac
17a4b 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72 65 20 6e uum, make sure n
17a4c 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 6e o tables contain
17a4d 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 . ** referenc
17a4e 65 73 20 74 6f 20 70 6f 69 6e 74 65 72 2d 6d 61 es to pointer-ma
17a4f 70 20 70 61 67 65 73 2e 0a 20 20 20 20 2a 2f 0a p pages.. */.
17a50 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 if( sCheck.a
17a51 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26 20 0a 20 nRef[i]==0 && .
17a52 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 (PTRMAP_PA
17a53 47 45 4e 4f 28 70 42 74 2c 20 69 29 21 3d 69 20 GENO(pBt, i)!=i
17a54 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 || !pBt->autoVac
17a55 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 uum) ){. ch
17a56 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 eckAppendMsg(&sC
17a57 68 65 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 heck, 0, "Page %
17a58 64 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 22 d is never used"
17a59 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , i);. }.
17a5a 69 66 28 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 if( sCheck.anRef
17a5b 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20 20 20 20 [i]!=0 && .
17a5c 20 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f (PTRMAP_PAGENO
17a5d 28 70 42 74 2c 20 69 29 3d 3d 69 20 26 26 20 70 (pBt, i)==i && p
17a5e 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 20 Bt->autoVacuum)
17a5f 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 ){. checkAp
17a60 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c pendMsg(&sCheck,
17a61 20 30 2c 20 22 50 6f 69 6e 74 65 72 20 6d 61 70 0, "Pointer map
17a62 20 70 61 67 65 20 25 64 20 69 73 20 72 65 66 65 page %d is refe
17a63 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a 20 20 20 renced", i);.
17a64 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 }.#endif. }..
17a65 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 /* Make sure th
17a66 69 73 20 61 6e 61 6c 79 73 69 73 20 64 69 64 20 is analysis did
17a67 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79 20 75 6e not leave any un
17a68 72 65 66 28 29 20 70 61 67 65 73 2e 0a 20 20 2a ref() pages.. *
17a69 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 * This is an int
17a6a 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74 65 6e 63 ernal consistenc
17a6b 79 20 63 68 65 63 6b 3b 20 61 6e 20 69 6e 74 65 y check; an inte
17a6c 67 72 69 74 79 20 63 68 65 63 6b 0a 20 20 2a 2a grity check. **
17a6d 20 6f 66 20 74 68 65 20 69 6e 74 65 67 72 69 74 of the integrit
17a6e 79 20 63 68 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20 y check.. */.
17a6f 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 unlockBtreeIfUnu
17a70 73 65 64 28 70 42 74 29 3b 0a 20 20 69 66 28 20 sed(pBt);. if(
17a71 4e 45 56 45 52 28 6e 52 65 66 20 21 3d 20 73 71 NEVER(nRef != sq
17a72 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
17a73 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29 nt(pBt->pPager))
17a74 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 ){. checkApp
17a75 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 endMsg(&sCheck,
17a76 30 2c 20 0a 20 20 20 20 20 20 22 4f 75 74 73 74 0, . "Outst
17a77 61 6e 64 69 6e 67 20 70 61 67 65 20 63 6f 75 6e anding page coun
17a78 74 20 67 6f 65 73 20 66 72 6f 6d 20 25 64 20 74 t goes from %d t
17a79 6f 20 25 64 20 64 75 72 69 6e 67 20 74 68 69 73 o %d during this
17a7a 20 61 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 analysis",.
17a7b 20 20 6e 52 65 66 2c 20 73 71 6c 69 74 65 33 50 nRef, sqlite3P
17a7c 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 agerRefcount(pBt
17a7d 2d 3e 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b ->pPager). );
17a7e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e . }.. /* Clean
17a7f 20 20 75 70 20 61 6e 64 20 72 65 70 6f 72 74 20 up and report
17a80 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 73 errors.. */. s
17a81 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
17a82 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 (p);. sqlite3_f
17a83 72 65 65 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 ree(sCheck.anRef
17a84 29 3b 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e );. if( sCheck.
17a85 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
17a86 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
17a87 63 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b cumReset(&sCheck
17a88 2e 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 2a 70 .errMsg);. *p
17a89 6e 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 nErr = sCheck.nE
17a8a 72 72 2b 31 3b 0a 20 20 20 20 72 65 74 75 72 6e rr+1;. return
17a8b 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 6e 45 72 72 0;. }. *pnErr
17a8c 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 3b 0a = sCheck.nErr;.
17a8d 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 45 72 if( sCheck.nEr
17a8e 72 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 53 74 r==0 ) sqlite3St
17a8f 72 41 63 63 75 6d 52 65 73 65 74 28 26 73 43 68 rAccumReset(&sCh
17a90 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 72 eck.errMsg);. r
17a91 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 eturn sqlite3Str
17a92 41 63 63 75 6d 46 69 6e 69 73 68 28 26 73 43 68 AccumFinish(&sCh
17a93 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 7d 0a 23 eck.errMsg);.}.#
17a94 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
17a95 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 OMIT_INTEGRITY_C
17a96 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 HECK */../*.** R
17a97 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 eturn the full p
17a98 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 75 athname of the u
17a99 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 nderlying databa
17a9a 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 se file..**.** T
17a9b 68 65 20 70 61 67 65 72 20 66 69 6c 65 6e 61 6d he pager filenam
17a9c 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 e is invariant a
17a9d 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 s long as the pa
17a9e 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 ger is.** open s
17a9f 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 o it is safe to
17aa0 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 access without t
17aa1 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 he BtShared mute
17aa2 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 x..*/.SQLITE_PRI
17aa3 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
17aa4 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 *sqlite3BtreeGet
17aa5 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a Filename(Btree *
17aa6 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
17aa7 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 >pBt->pPager!=0
17aa8 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 );. return sqli
17aa9 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 te3PagerFilename
17aaa 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 (p->pBt->pPager)
17aab 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
17aac 6e 20 74 68 65 20 70 61 74 68 6e 61 6d 65 20 6f n the pathname o
17aad 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
17aae 6c 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 le for this data
17aaf 62 61 73 65 2e 20 54 68 65 20 72 65 74 75 72 6e base. The return
17ab0 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 69 .** value of thi
17ab1 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 s routine is the
17ab2 20 73 61 6d 65 20 72 65 67 61 72 64 6c 65 73 73 same regardless
17ab3 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 65 20 of whether the
17ab4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 journal file.**
17ab5 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 has been created
17ab6 20 6f 72 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 or not..**.** T
17ab7 68 65 20 70 61 67 65 72 20 6a 6f 75 72 6e 61 6c he pager journal
17ab8 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 filename is inv
17ab9 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 ariant as long a
17aba 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a s the pager is.*
17abb 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 * open so it is
17abc 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 safe to access w
17abd 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 ithout the BtSha
17abe 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 red mutex..*/.SQ
17abf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
17ac0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
17ac1 42 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e BtreeGetJournaln
17ac2 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ame(Btree *p){.
17ac3 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d assert( p->pBt-
17ac4 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 >pPager!=0 );.
17ac5 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 return sqlite3Pa
17ac6 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 gerJournalname(p
17ac7 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a ->pBt->pPager);.
17ac8 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
17ac9 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 74 72 non-zero if a tr
17aca 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
17acb 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ive..*/.SQLITE_P
17acc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
17acd 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 e3BtreeIsInTrans
17ace 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 (Btree *p){. as
17acf 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 73 71 sert( p==0 || sq
17ad0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
17ad1 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
17ad2 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 ;. return (p &&
17ad3 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 (p->inTrans==TR
17ad4 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a 7d 0a 0a ANS_WRITE));.}..
17ad5 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e /*.** Return non
17ad6 2d 7a 65 72 6f 20 69 66 20 61 20 72 65 61 64 20 -zero if a read
17ad7 28 6f 72 20 77 72 69 74 65 29 20 74 72 61 6e 73 (or write) trans
17ad8 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
17ad9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
17ada 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
17adb 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e treeIsInReadTran
17adc 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 s(Btree *p){. a
17add 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 ssert( p );. as
17ade 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
17adf 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
17ae0 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 mutex) );. retu
17ae1 72 6e 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 rn p->inTrans!=T
17ae2 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 7d 0a 0a 53 51 RANS_NONE;.}..SQ
17ae3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
17ae4 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
17ae5 6e 42 61 63 6b 75 70 28 42 74 72 65 65 20 2a 70 nBackup(Btree *p
17ae6 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 ){. assert( p )
17ae7 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
17ae8 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
17ae9 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
17aea 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 42 61 63 return p->nBac
17aeb 6b 75 70 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a kup!=0;.}../*.**
17aec 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
17aed 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
17aee 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 6d 65 to a blob of me
17aef 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 mory associated
17af0 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 with.** a single
17af1 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e 20 54 shared-btree. T
17af2 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 75 73 65 he memory is use
17af3 64 20 62 79 20 63 6c 69 65 6e 74 20 63 6f 64 65 d by client code
17af4 20 66 6f 72 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 for its own.**
17af5 70 75 72 70 6f 73 65 73 20 28 66 6f 72 20 65 78 purposes (for ex
17af6 61 6d 70 6c 65 2c 20 74 6f 20 73 74 6f 72 65 20 ample, to store
17af7 61 20 68 69 67 68 2d 6c 65 76 65 6c 20 73 63 68 a high-level sch
17af8 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ema associated w
17af9 69 74 68 20 0a 2a 2a 20 74 68 65 20 73 68 61 72 ith .** the shar
17afa 65 64 2d 62 74 72 65 65 29 2e 20 54 68 65 20 62 ed-btree). The b
17afb 74 72 65 65 20 6c 61 79 65 72 20 6d 61 6e 61 67 tree layer manag
17afc 65 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 es reference cou
17afd 6e 74 69 6e 67 20 69 73 73 75 65 73 2e 0a 2a 2a nting issues..**
17afe 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 .** The first ti
17aff 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 me this is calle
17b00 64 20 6f 6e 20 61 20 73 68 61 72 65 64 2d 62 74 d on a shared-bt
17b01 72 65 65 2c 20 6e 42 79 74 65 73 20 62 79 74 65 ree, nBytes byte
17b02 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 s of memory.** a
17b03 72 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 7a 65 re allocated, ze
17b04 72 6f 65 64 2c 20 61 6e 64 20 72 65 74 75 72 6e roed, and return
17b05 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
17b06 2e 20 46 6f 72 20 65 61 63 68 20 73 75 62 73 65 . For each subse
17b07 71 75 65 6e 74 20 0a 2a 2a 20 63 61 6c 6c 20 74 quent .** call t
17b08 68 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65 he nBytes parame
17b09 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 61 ter is ignored a
17b0a 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 nd a pointer to
17b0b 74 68 65 20 73 61 6d 65 20 62 6c 6f 62 0a 2a 2a the same blob.**
17b0c 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 74 75 72 of memory retur
17b0d 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ned. .**.** If t
17b0e 68 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65 he nBytes parame
17b0f 74 65 72 20 69 73 20 30 20 61 6e 64 20 74 68 65 ter is 0 and the
17b10 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 blob of memory
17b11 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
17b12 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 .** allocated, a
17b13 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 null pointer is
17b14 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68 returned. If th
17b15 65 20 62 6c 6f 62 20 68 61 73 20 61 6c 72 65 61 e blob has alrea
17b16 64 79 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 dy been.** alloc
17b17 61 74 65 64 2c 20 69 74 20 69 73 20 72 65 74 75 ated, it is retu
17b18 72 6e 65 64 20 61 73 20 6e 6f 72 6d 61 6c 2e 0a rned as normal..
17b19 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62 65 66 6f 72 **.** Just befor
17b1a 65 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 e the shared-btr
17b1b 65 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 68 ee is closed, th
17b1c 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65 e function passe
17b1d 64 20 61 73 20 74 68 65 20 0a 2a 2a 20 78 46 72 d as the .** xFr
17b1e 65 65 20 61 72 67 75 6d 65 6e 74 20 77 68 65 6e ee argument when
17b1f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
17b20 63 61 74 69 6f 6e 20 77 61 73 20 6d 61 64 65 20 cation was made
17b21 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 is invoked on th
17b22 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 6c e .** blob of al
17b23 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 located memory.
17b24 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 This function sh
17b25 6f 75 6c 64 20 6e 6f 74 20 63 61 6c 6c 20 73 71 ould not call sq
17b26 6c 69 74 65 33 5f 66 72 65 65 28 29 0a 2a 2a 20 lite3_free().**
17b27 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74 on the memory, t
17b28 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 64 he btree layer d
17b29 6f 65 73 20 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c oes that..*/.SQL
17b2a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
17b2b 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 *sqlite3BtreeSc
17b2c 68 65 6d 61 28 42 74 72 65 65 20 2a 70 2c 20 69 hema(Btree *p, i
17b2d 6e 74 20 6e 42 79 74 65 73 2c 20 76 6f 69 64 28 nt nBytes, void(
17b2e 2a 78 46 72 65 65 29 28 76 6f 69 64 20 2a 29 29 *xFree)(void *))
17b2f 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
17b30 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 t = p->pBt;. sq
17b31 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
17b32 70 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e p);. if( !pBt->
17b33 70 53 63 68 65 6d 61 20 26 26 20 6e 42 79 74 65 pSchema && nByte
17b34 73 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 53 s ){. pBt->pS
17b35 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 4d chema = sqlite3M
17b36 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 79 74 65 73 allocZero(nBytes
17b37 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78 46 72 65 );. pBt->xFre
17b38 65 53 63 68 65 6d 61 20 3d 20 78 46 72 65 65 3b eSchema = xFree;
17b39 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
17b3a 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
17b3b 65 74 75 72 6e 20 70 42 74 2d 3e 70 53 63 68 65 eturn pBt->pSche
17b3c 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ma;.}../*.** Ret
17b3d 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 urn SQLITE_LOCKE
17b3e 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 69 66 D_SHAREDCACHE if
17b3f 20 61 6e 6f 74 68 65 72 20 75 73 65 72 20 6f 66 another user of
17b40 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64 the same shared
17b41 20 0a 2a 2a 20 62 74 72 65 65 20 61 73 20 74 68 .** btree as th
17b42 65 20 61 72 67 75 6d 65 6e 74 20 68 61 6e 64 6c e argument handl
17b43 65 20 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75 e holds an exclu
17b44 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 sive lock on the
17b45 20 0a 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 .** sqlite_mast
17b46 65 72 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77 er table. Otherw
17b47 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a ise SQLITE_OK..*
17b48 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17b49 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
17b4a 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 eSchemaLocked(Bt
17b4b 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 ree *p){. int r
17b4c 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c c;. assert( sql
17b4d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17b4e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
17b4f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
17b50 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
17b51 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 querySharedCache
17b52 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 TableLock(p, MAS
17b53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c TER_ROOT, READ_L
17b54 4f 43 4b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 OCK);. assert(
17b55 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
17b56 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b rc==SQLITE_LOCK
17b57 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 ED_SHAREDCACHE )
17b58 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
17b59 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
17b5a 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 rn rc;.}...#ifnd
17b5b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
17b5c 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a HARED_CACHE./*.*
17b5d 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 * Obtain a lock
17b5e 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 68 6f on the table who
17b5f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 se root page is
17b60 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a 20 6c 6f iTab. The.** lo
17b61 63 6b 20 69 73 20 61 20 77 72 69 74 65 20 6c 6f ck is a write lo
17b62 63 6b 20 69 66 20 69 73 57 72 69 74 65 6c 6f 63 ck if isWriteloc
17b63 6b 20 69 73 20 74 72 75 65 20 6f 72 20 61 20 72 k is true or a r
17b64 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69 66 20 69 ead lock.** if i
17b65 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 t is false..*/.S
17b66 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
17b67 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f t sqlite3BtreeLo
17b68 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 ckTable(Btree *p
17b69 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 38 20 69 , int iTab, u8 i
17b6a 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a 20 20 69 sWriteLock){. i
17b6b 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
17b6c 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 K;. if( p->shar
17b6d 61 62 6c 65 20 29 7b 0a 20 20 20 20 75 38 20 6c able ){. u8 l
17b6e 6f 63 6b 54 79 70 65 20 3d 20 52 45 41 44 5f 4c ockType = READ_L
17b6f 4f 43 4b 20 2b 20 69 73 57 72 69 74 65 4c 6f 63 OCK + isWriteLoc
17b70 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 52 k;. assert( R
17b71 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54 EAD_LOCK+1==WRIT
17b72 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 E_LOCK );. as
17b73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 sert( isWriteLoc
17b74 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c k==0 || isWriteL
17b75 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71 ock==1 );. sq
17b76 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
17b77 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 75 65 p);. rc = que
17b78 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
17b79 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 leLock(p, iTab,
17b7a 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 69 lockType);. i
17b7b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
17b7c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
17b7d 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 etSharedCacheTab
17b7e 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 leLock(p, iTab,
17b7f 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d lockType);. }
17b80 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
17b81 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 eLeave(p);. }.
17b82 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
17b83 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
17b84 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
17b85 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e OB./*.** Argumen
17b86 74 20 70 43 73 72 20 6d 75 73 74 20 62 65 20 61 t pCsr must be a
17b87 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66 cursor opened f
17b88 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e or writing on an
17b89 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c .** INTKEY tabl
17b8a 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e e currently poin
17b8b 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20 ting at a valid
17b8c 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a table entry. .**
17b8d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
17b8e 6f 64 69 66 69 65 73 20 74 68 65 20 64 61 74 61 odifies the data
17b8f 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 stored as part
17b90 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a of that entry..*
17b91 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 * Only the data
17b92 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 content may only
17b93 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 be modified, it
17b94 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
17b95 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 .** to change th
17b96 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
17b97 64 61 74 61 20 73 74 6f 72 65 64 2e 0a 2a 2f 0a data stored..*/.
17b98 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
17b99 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 nt sqlite3BtreeP
17b9a 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 20 utData(BtCursor
17b9b 2a 70 43 73 72 2c 20 75 33 32 20 6f 66 66 73 65 *pCsr, u32 offse
17b9c 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 t, u32 amt, void
17b9d 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *z){. int rc;.
17b9e 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
17b9f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 73 72 rHoldsMutex(pCsr
17ba0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
17ba1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
17ba2 64 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2d 3e d(pCsr->pBtree->
17ba3 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
17ba4 61 73 73 65 72 74 28 70 43 73 72 2d 3e 69 73 49 assert(pCsr->isI
17ba5 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a ncrblobHandle);.
17ba6 0a 20 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 . restoreCursor
17ba7 50 6f 73 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a Position(pCsr);.
17ba8 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e assert( pCsr->
17ba9 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 52 eState!=CURSOR_R
17baa 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 EQUIRESEEK );.
17bab 69 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 if( pCsr->eState
17bac 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 !=CURSOR_VALID )
17bad 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
17bae 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 0a ITE_ABORT;. }..
17baf 20 20 2f 2a 20 43 68 65 63 6b 20 73 6f 6d 65 20 /* Check some
17bb0 70 72 65 63 6f 6e 64 69 74 69 6f 6e 73 3a 20 0a preconditions: .
17bb1 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65 20 63 ** (a) the c
17bb2 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20 66 6f ursor is open fo
17bb3 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a 2a 20 r writing,. **
17bb4 20 20 28 62 29 20 74 68 65 72 65 20 69 73 20 6e (b) there is n
17bb5 6f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 o read-lock on t
17bb6 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6d he table being m
17bb7 6f 64 69 66 69 65 64 20 61 6e 64 0a 20 20 2a 2a odified and. **
17bb8 20 20 20 28 63 29 20 74 68 65 20 63 75 72 73 6f (c) the curso
17bb9 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76 61 r points at a va
17bba 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69 6e lid row of an in
17bbb 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a 2f tKey table.. */
17bbc 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77 72 . if( !pCsr->wr
17bbd 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75 Flag ){. retu
17bbe 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e rn SQLITE_READON
17bbf 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 LY;. }. assert
17bc0 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72 65 ( !pCsr->pBt->re
17bc1 61 64 4f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20 adOnly .
17bc2 20 20 26 26 20 70 43 73 72 2d 3e 70 42 74 2d 3e && pCsr->pBt->
17bc3 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
17bc4 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
17bc5 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 65 61 rc = checkForRea
17bc6 64 43 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d dConflicts(pCsr-
17bc7 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 >pBtree, pCsr->p
17bc8 67 6e 6f 52 6f 6f 74 2c 20 70 43 73 72 2c 20 30 gnoRoot, pCsr, 0
17bc9 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
17bca 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a ITE_OK ){. /*
17bcb 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20 The table pCur
17bcc 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20 points to has a
17bcd 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 read lock */.
17bce 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
17bcf 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
17bd0 44 43 41 43 48 45 20 29 3b 0a 20 20 20 20 72 65 DCACHE );. re
17bd1 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 turn rc;. }. i
17bd2 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 3d f( pCsr->eState=
17bd3 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
17bd4 7c 7c 20 21 70 43 73 72 2d 3e 61 70 50 61 67 65 || !pCsr->apPage
17bd5 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 [pCsr->iPage]->i
17bd6 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74 ntKey ){. ret
17bd7 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
17bd8 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
17bd9 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 accessPayload(pC
17bda 73 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c sr, offset, amt,
17bdb 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
17bdc 2a 29 7a 2c 20 30 2c 20 31 29 3b 0a 7d 0a 0a 2f *)z, 0, 1);.}../
17bdd 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 * .** Set a flag
17bde 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 on this cursor
17bdf 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63 to cache the loc
17be0 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20 ations of pages
17be1 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65 from the .** ove
17be2 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74 rflow list for t
17be3 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 he current row.
17be4 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
17be5 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a cursors opened.*
17be6 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61 * for incrementa
17be7 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a l blob IO only..
17be8 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
17be9 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20 ion sets a flag
17bea 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c only. The actual
17beb 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63 page location c
17bec 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20 ache.** (stored
17bed 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 in BtCursor.aOve
17bee 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f rflow[]) is allo
17bef 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62 cated and used b
17bf0 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63 y function.** ac
17bf1 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74 cessPayload() (t
17bf2 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69 he worker functi
17bf3 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74 on for sqlite3Bt
17bf4 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a reeData() and.**
17bf5 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
17bf6 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 Data())..*/.SQLI
17bf7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
17bf8 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 sqlite3BtreeCach
17bf9 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 eOverflow(BtCurs
17bfa 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
17bfb 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
17bfc 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
17bfd 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17bfe 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
17bff 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
17c00 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
17c01 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c (!pCur->isIncrbl
17c02 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73 obHandle);. ass
17c03 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 ert(!pCur->aOver
17c04 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69 flow);. pCur->i
17c05 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 sIncrblobHandle
17c06 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f = 1;.}.#endif../
17c07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
17c08 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a nd of btree.c **
17c09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
17c0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
17c0d 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70 egin file backup
17c0e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
17c0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
17c11 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72 *.** 2009 Januar
17c12 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 28.**.** The a
17c13 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
17c14 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
17c15 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
17c16 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
17c17 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
17c18 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
17c19 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
17c1a 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
17c1b 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
17c1c 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
17c1d 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
17c1e 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
17c1f 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
17c20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
17c21 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
17c22 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
17c23 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
17c24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c28 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
17c29 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d contains the im
17c2a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
17c2b 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
17c2c 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49 up_XXX() .** API
17c2d 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74 functions and t
17c2e 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75 he related featu
17c2f 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 res..**.** $Id:
17c30 62 61 63 6b 75 70 2e 63 2c 76 20 31 2e 31 33 20 backup.c,v 1.13
17c31 32 30 30 39 2f 30 33 2f 31 36 20 31 33 3a 31 39 2009/03/16 13:19
17c32 3a 33 36 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :36 danielk1977
17c33 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 4d 61 63 Exp $.*/../* Mac
17c34 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d ro to find the m
17c35 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 inimum of two nu
17c36 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f meric values..*/
17c37 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 .#ifndef MIN.# d
17c38 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 efine MIN(x,y) (
17c39 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 (x)<(y)?(x):(y))
17c3a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 .#endif../*.** S
17c3b 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74 tructure allocat
17c3c 65 64 20 66 6f 72 20 65 61 63 68 20 62 61 63 6b ed for each back
17c3d 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f up operation..*/
17c3e 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
17c3f 62 61 63 6b 75 70 20 7b 0a 20 20 73 71 6c 69 74 backup {. sqlit
17c40 65 33 2a 20 70 44 65 73 74 44 62 3b 20 20 20 20 e3* pDestDb;
17c41 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 /* Destinati
17c42 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 on database hand
17c43 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 le */. Btree *p
17c44 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 Dest;
17c45 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 /* Destination
17c46 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 20 b-tree file */.
17c47 20 75 33 32 20 69 44 65 73 74 53 63 68 65 6d 61 u32 iDestSchema
17c48 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 ; /* Ori
17c49 67 69 6e 61 6c 20 73 63 68 65 6d 61 20 63 6f 6f ginal schema coo
17c4a 6b 69 65 20 69 6e 20 64 65 73 74 69 6e 61 74 69 kie in destinati
17c4b 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 44 65 73 on */. int bDes
17c4c 74 4c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 tLocked;
17c4d 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 61 20 /* True once a
17c4e 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
17c4f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e 20 70 44 65 n is open on pDe
17c50 73 74 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 69 4e st */.. Pgno iN
17c51 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
17c52 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
17c53 20 6f 66 20 74 68 65 20 6e 65 78 74 20 73 6f 75 of the next sou
17c54 72 63 65 20 70 61 67 65 20 74 6f 20 63 6f 70 79 rce page to copy
17c55 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 */. sqlite3* p
17c56 53 72 63 44 62 3b 20 20 20 20 20 20 20 20 20 2f SrcDb; /
17c57 2a 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 * Source databas
17c58 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 e handle */. Bt
17c59 72 65 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 ree *pSrc;
17c5a 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 /* Source
17c5b 20 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a b-tree file */.
17c5c 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
17c5d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
17c5e 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 65 72 ackup process er
17c5f 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f ror code */.. /
17c60 2a 20 54 68 65 73 65 20 74 77 6f 20 76 61 72 69 * These two vari
17c61 61 62 6c 65 73 20 61 72 65 20 73 65 74 20 62 79 ables are set by
17c62 20 65 76 65 72 79 20 63 61 6c 6c 20 74 6f 20 62 every call to b
17c63 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68 ackup_step(). Th
17c64 65 79 20 61 72 65 0a 20 20 2a 2a 20 72 65 61 64 ey are. ** read
17c65 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 62 61 63 by calls to bac
17c66 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 kup_remaining()
17c67 61 6e 64 20 62 61 63 6b 75 70 5f 70 61 67 65 63 and backup_pagec
17c68 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f 0a 20 20 50 ount().. */. P
17c69 67 6e 6f 20 6e 52 65 6d 61 69 6e 69 6e 67 3b 20 gno nRemaining;
17c6a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
17c6b 72 20 6f 66 20 70 61 67 65 73 20 6c 65 66 74 20 r of pages left
17c6c 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 50 67 6e to copy */. Pgn
17c6d 6f 20 6e 50 61 67 65 63 6f 75 6e 74 3b 20 20 20 o nPagecount;
17c6e 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
17c6f 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 umber of pages t
17c70 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 73 71 6c o copy */.. sql
17c71 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 4e 65 ite3_backup *pNe
17c72 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 62 61 xt; /* Next ba
17c73 63 6b 75 70 20 61 73 73 6f 63 69 61 74 65 64 20 ckup associated
17c74 77 69 74 68 20 73 6f 75 72 63 65 20 70 61 67 65 with source page
17c75 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 r */.};../*.** T
17c76 48 52 45 41 44 20 53 41 46 45 54 59 20 4e 4f 54 HREAD SAFETY NOT
17c77 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 6e 63 65 ES:.**.** Once
17c78 20 69 74 20 68 61 73 20 62 65 65 6e 20 63 72 65 it has been cre
17c79 61 74 65 64 20 75 73 69 6e 67 20 62 61 63 6b 75 ated using backu
17c7a 70 5f 69 6e 69 74 28 29 2c 20 61 20 73 69 6e 67 p_init(), a sing
17c7b 6c 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 le sqlite3_backu
17c7c 70 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65 p.** structure
17c7d 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64 may be accessed
17c7e 20 76 69 61 20 74 77 6f 20 67 72 6f 75 70 73 20 via two groups
17c7f 6f 66 20 74 68 72 65 61 64 2d 73 61 66 65 20 65 of thread-safe e
17c80 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0a 2a 2a 0a ntry points:.**.
17c81 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 ** * Via the
17c82 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
17c83 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 69 XXX() API functi
17c84 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 on backup_step()
17c85 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20 62 and .** b
17c86 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 ackup_finish().
17c87 42 6f 74 68 20 74 68 65 73 65 20 66 75 6e 63 74 Both these funct
17c88 69 6f 6e 73 20 6f 62 74 61 69 6e 20 74 68 65 20 ions obtain the
17c89 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 0a source database.
17c8a 2a 2a 20 20 20 20 20 20 20 68 61 6e 64 6c 65 20 ** handle
17c8b 6d 75 74 65 78 20 61 6e 64 20 74 68 65 20 6d 75 mutex and the mu
17c8c 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 tex associated w
17c8d 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 42 ith the source B
17c8e 74 53 68 61 72 65 64 20 0a 2a 2a 20 20 20 20 20 tShared .**
17c8f 20 20 73 74 72 75 63 74 75 72 65 2c 20 69 6e 20 structure, in
17c90 74 68 61 74 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a that order..**.*
17c91 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 20 * * Via the
17c92 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 61 BackupUpdate() a
17c93 6e 64 20 42 61 63 6b 75 70 52 65 73 74 61 72 74 nd BackupRestart
17c94 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 68 () functions, wh
17c95 69 63 68 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 ich are.**
17c96 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 invoked by the
17c97 70 61 67 65 72 20 6c 61 79 65 72 20 74 6f 20 72 pager layer to r
17c98 65 70 6f 72 74 20 76 61 72 69 6f 75 73 20 73 74 eport various st
17c99 61 74 65 20 63 68 61 6e 67 65 73 20 69 6e 0a 2a ate changes in.*
17c9a 2a 20 20 20 20 20 20 20 74 68 65 20 70 61 67 65 * the page
17c9b 20 63 61 63 68 65 20 61 73 73 6f 63 69 61 74 65 cache associate
17c9c 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 63 d with the sourc
17c9d 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 e database. The
17c9e 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 20 61 mutex.** a
17c9f 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
17ca0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
17ca1 73 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 se BtShared stru
17ca2 63 74 75 72 65 20 77 69 6c 6c 20 61 6c 77 61 79 cture will alway
17ca3 73 20 0a 2a 2a 20 20 20 20 20 20 20 62 65 20 68 s .** be h
17ca4 65 6c 64 20 77 68 65 6e 20 65 69 74 68 65 72 20 eld when either
17ca5 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f of these functio
17ca6 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 0a ns are invoked..
17ca7 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 6f 74 68 65 **.** The othe
17ca8 72 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 r sqlite3_backup
17ca9 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 _XXX() API funct
17caa 69 6f 6e 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d ions, backup_rem
17cab 61 69 6e 69 6e 67 28 29 20 61 6e 64 0a 2a 2a 20 aining() and.**
17cac 20 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 backup_pagecou
17cad 6e 74 28 29 20 61 72 65 20 6e 6f 74 20 74 68 72 nt() are not thr
17cae 65 61 64 2d 73 61 66 65 20 66 75 6e 63 74 69 6f ead-safe functio
17caf 6e 73 2e 20 49 66 20 74 68 65 79 20 61 72 65 20 ns. If they are
17cb0 63 61 6c 6c 65 64 0a 2a 2a 20 20 20 77 68 69 6c called.** whil
17cb1 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 e some other thr
17cb2 65 61 64 20 69 73 20 63 61 6c 6c 69 6e 67 20 62 ead is calling b
17cb3 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 72 20 ackup_step() or
17cb4 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c backup_finish(),
17cb5 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c 75 65 73 .** the values
17cb6 20 72 65 74 75 72 6e 65 64 20 6d 61 79 20 62 65 returned may be
17cb7 20 69 6e 76 61 6c 69 64 2e 20 54 68 65 72 65 20 invalid. There
17cb8 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20 is no way for a
17cb9 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 42 61 63 call to.** Bac
17cba 6b 75 70 55 70 64 61 74 65 28 29 20 6f 72 20 42 kupUpdate() or B
17cbb 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 74 ackupRestart() t
17cbc 6f 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68 o interfere with
17cbd 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e backup_remainin
17cbe 67 28 29 0a 2a 2a 20 20 20 6f 72 20 62 61 63 6b g().** or back
17cbf 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a up_pagecount()..
17cc0 2a 2a 0a 2a 2a 20 20 20 44 65 70 65 6e 64 69 6e **.** Dependin
17cc1 67 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 g on the SQLite
17cc2 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 74 configuration, t
17cc3 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
17cc4 6c 65 73 20 61 6e 64 2f 6f 72 0a 2a 2a 20 20 20 les and/or.**
17cc5 74 68 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 the Btree object
17cc6 73 20 6d 61 79 20 68 61 76 65 20 74 68 65 69 72 s may have their
17cc7 20 6f 77 6e 20 6d 75 74 65 78 65 73 20 74 68 61 own mutexes tha
17cc8 74 20 72 65 71 75 69 72 65 20 6c 6f 63 6b 69 6e t require lockin
17cc9 67 2e 0a 2a 2a 20 20 20 4e 6f 6e 2d 73 68 61 72 g..** Non-shar
17cca 61 62 6c 65 20 42 74 72 65 65 73 20 28 69 6e 2d able Btrees (in-
17ccb 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 memory databases
17ccc 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 2c 20 64 for example), d
17ccd 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 o not have.**
17cce 61 73 73 6f 63 69 61 74 65 64 20 6d 75 74 65 78 associated mutex
17ccf 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 es..*/../*.** Re
17cd0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 63 turn a pointer c
17cd1 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
17cd2 64 61 74 61 62 61 73 65 20 7a 44 62 20 28 69 2e database zDb (i.
17cd3 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 e. "main", "temp
17cd4 22 29 0a 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74 ").** in connect
17cd5 69 6f 6e 20 68 61 6e 64 6c 65 20 70 44 62 2e 20 ion handle pDb.
17cd6 49 66 20 73 75 63 68 20 61 20 64 61 74 61 62 61 If such a databa
17cd7 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 se cannot be fou
17cd8 6e 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 nd, return.** a
17cd9 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64 NULL pointer and
17cda 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 write an error
17cdb 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f message to pErro
17cdc 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 rDb..**.** If th
17cdd 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 73 e "temp" databas
17cde 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 e is requested,
17cdf 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 it may need to b
17ce0 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 e opened by this
17ce1 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49 .** function. I
17ce2 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
17ce3 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f s while doing so
17ce4 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 77 , return 0 and w
17ce5 72 69 74 65 20 61 6e 20 0a 2a 2a 20 65 72 72 6f rite an .** erro
17ce6 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 r message to pEr
17ce7 72 6f 72 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 63 rorDb..*/.static
17ce8 20 42 74 72 65 65 20 2a 66 69 6e 64 42 74 72 65 Btree *findBtre
17ce9 65 28 73 71 6c 69 74 65 33 20 2a 70 45 72 72 6f e(sqlite3 *pErro
17cea 72 44 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 44 rDb, sqlite3 *pD
17ceb 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
17cec 44 62 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 73 Db){. int i = s
17ced 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 qlite3FindDbName
17cee 28 70 44 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 69 (pDb, zDb);.. i
17cef 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 50 f( i==1 ){. P
17cf0 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 20 arse sParse;.
17cf1 20 6d 65 6d 73 65 74 28 26 73 50 61 72 73 65 2c memset(&sParse,
17cf2 20 30 2c 20 73 69 7a 65 6f 66 28 73 50 61 72 73 0, sizeof(sPars
17cf3 65 29 29 3b 0a 20 20 20 20 73 50 61 72 73 65 2e e));. sParse.
17cf4 64 62 20 3d 20 70 44 62 3b 0a 20 20 20 20 69 66 db = pDb;. if
17cf5 28 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d ( sqlite3OpenTem
17cf6 70 44 61 74 61 62 61 73 65 28 26 73 50 61 72 73 pDatabase(&sPars
17cf7 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 e) ){. sqli
17cf8 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 26 73 te3ErrorClear(&s
17cf9 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 Parse);. sq
17cfa 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f lite3Error(pErro
17cfb 72 44 62 2c 20 73 50 61 72 73 65 2e 72 63 2c 20 rDb, sParse.rc,
17cfc 22 25 73 22 2c 20 73 50 61 72 73 65 2e 7a 45 72 "%s", sParse.zEr
17cfd 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 72 65 74 rMsg);. ret
17cfe 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 0;. }.
17cff 20 61 73 73 65 72 74 28 20 73 50 61 72 73 65 2e assert( sParse.
17d00 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20 zErrMsg==0 );.
17d01 7d 0a 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a }.. if( i<0 ){.
17d02 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
17d03 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 (pErrorDb, SQLIT
17d04 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77 E_ERROR, "unknow
17d05 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 n database %s",
17d06 7a 44 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e zDb);. return
17d07 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 0;. }.. retur
17d08 6e 20 70 44 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 n pDb->aDb[i].pB
17d09 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 t;.}../*.** Crea
17d0a 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 te an sqlite3_ba
17d0b 63 6b 75 70 20 70 72 6f 63 65 73 73 20 74 6f 20 ckup process to
17d0c 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 copy the content
17d0d 73 20 6f 66 20 7a 53 72 63 44 62 20 66 72 6f 6d s of zSrcDb from
17d0e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 .** connection h
17d0f 61 6e 64 6c 65 20 70 53 72 63 44 62 20 74 6f 20 andle pSrcDb to
17d10 7a 44 65 73 74 44 62 20 69 6e 20 70 44 65 73 74 zDestDb in pDest
17d11 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 Db. If successfu
17d12 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 l, return.** a p
17d13 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
17d14 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 w sqlite3_backup
17d15 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 object..**.** I
17d16 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
17d17 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 s, NULL is retur
17d18 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 ned and an error
17d19 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 code and error
17d1a 6d 65 73 73 61 67 65 0a 2a 2a 20 73 74 6f 72 65 message.** store
17d1b 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 68 61 d in database ha
17d1c 6e 64 6c 65 20 70 44 65 73 74 44 62 2e 0a 2a 2f ndle pDestDb..*/
17d1d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
17d1e 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 te3_backup *sqli
17d1f 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
17d20 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73 . sqlite3* pDes
17d21 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 tDb,
17d22 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
17d23 62 61 73 65 20 74 6f 20 77 72 69 74 65 20 74 6f base to write to
17d24 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
17d25 20 2a 7a 44 65 73 74 44 62 2c 20 20 20 20 20 20 *zDestDb,
17d26 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17d27 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 ame of database
17d28 77 69 74 68 69 6e 20 70 44 65 73 74 44 62 20 2a within pDestDb *
17d29 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72 /. sqlite3* pSr
17d2a 63 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 cDb,
17d2b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
17d2c 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
17d2d 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f to read from */
17d2e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
17d2f 53 72 63 44 62 20 20 20 20 20 20 20 20 20 20 20 SrcDb
17d30 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
17d31 20 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74 of database wit
17d32 68 69 6e 20 70 53 72 63 44 62 20 2a 2f 0a 29 7b hin pSrcDb */.){
17d33 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
17d34 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 p *p;
17d35 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
17d36 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a e to return */..
17d37 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 73 6f /* Lock the so
17d38 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 urce database ha
17d39 6e 64 6c 65 2e 20 54 68 65 20 64 65 73 74 69 6e ndle. The destin
17d3a 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 20 ation database.
17d3b 20 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f ** handle is no
17d3c 74 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 69 73 t locked in this
17d3d 20 72 6f 75 74 69 6e 65 2c 20 62 75 74 20 69 74 routine, but it
17d3e 20 69 73 20 6c 6f 63 6b 65 64 20 69 6e 0a 20 20 is locked in.
17d3f 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
17d40 70 5f 73 74 65 70 28 29 2e 20 54 68 65 20 75 73 p_step(). The us
17d41 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20 74 er is required t
17d42 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 6e 6f o ensure that no
17d43 0a 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 72 65 . ** other thre
17d44 61 64 20 61 63 63 65 73 73 65 73 20 74 68 65 20 ad accesses the
17d45 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 6e 64 destination hand
17d46 6c 65 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 le for the durat
17d47 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 ion. ** of the
17d48 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
17d49 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 . Any attempt t
17d4a 6f 20 75 73 65 20 74 68 65 20 64 65 73 74 69 6e o use the destin
17d4b 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 61 74 61 62 ation. ** datab
17d4c 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 ase connection w
17d4d 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73 hile a backup is
17d4e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 61 79 in progress may
17d4f 20 63 61 75 73 65 0a 20 20 2a 2a 20 61 20 6d 61 cause. ** a ma
17d50 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20 64 lfunction or a d
17d51 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 eadlock.. */.
17d52 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
17d53 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 ter(pSrcDb->mute
17d54 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 x);. sqlite3_mu
17d55 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73 74 44 tex_enter(pDestD
17d56 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69 66 b->mutex);.. if
17d57 28 20 70 53 72 63 44 62 3d 3d 70 44 65 73 74 44 ( pSrcDb==pDestD
17d58 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
17d59 45 72 72 6f 72 28 0a 20 20 20 20 20 20 20 20 70 Error(. p
17d5a 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 45 DestDb, SQLITE_E
17d5b 52 52 4f 52 2c 20 22 73 6f 75 72 63 65 20 61 6e RROR, "source an
17d5c 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 75 d destination mu
17d5d 73 74 20 62 65 20 64 69 73 74 69 6e 63 74 22 0a st be distinct".
17d5e 20 20 20 20 29 3b 0a 20 20 20 20 70 20 3d 20 30 );. p = 0
17d5f 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 ;. }else {.
17d60 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 /* Allocate spac
17d61 65 20 66 6f 72 20 61 20 6e 65 77 20 73 71 6c 69 e for a new sqli
17d62 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 te3_backup objec
17d63 74 20 2a 2f 0a 20 20 20 20 70 20 3d 20 28 73 71 t */. p = (sq
17d64 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 73 lite3_backup *)s
17d65 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
17d66 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 zeof(sqlite3_bac
17d67 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 21 kup));. if( !
17d68 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 p ){. sqlit
17d69 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 2c e3Error(pDestDb,
17d6a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 SQLITE_NOMEM, 0
17d6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
17d6c 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 /* If the alloca
17d6d 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c 20 tion succeeded,
17d6e 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77 populate the new
17d6f 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 66 object. */. if
17d70 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 ( p ){. memse
17d71 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 t(p, 0, sizeof(s
17d72 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 3b qlite3_backup));
17d73 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 66 . p->pSrc = f
17d74 69 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62 indBtree(pDestDb
17d75 2c 20 70 53 72 63 44 62 2c 20 7a 53 72 63 44 62 , pSrcDb, zSrcDb
17d76 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 20 );. p->pDest
17d77 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65 73 = findBtree(pDes
17d78 74 44 62 2c 20 70 44 65 73 74 44 62 2c 20 7a 44 tDb, pDestDb, zD
17d79 65 73 74 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70 estDb);. p->p
17d7a 44 65 73 74 44 62 20 3d 20 70 44 65 73 74 44 62 DestDb = pDestDb
17d7b 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 44 62 20 ;. p->pSrcDb
17d7c 3d 20 70 53 72 63 44 62 3b 0a 20 20 20 20 70 2d = pSrcDb;. p-
17d7d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a 20 20 20 >iNext = 1;..
17d7e 20 69 66 28 20 30 3d 3d 70 2d 3e 70 53 72 63 20 if( 0==p->pSrc
17d7f 7c 7c 20 30 3d 3d 70 2d 3e 70 44 65 73 74 20 29 || 0==p->pDest )
17d80 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 28 {. /* One (
17d81 6f 72 20 62 6f 74 68 29 20 6f 66 20 74 68 65 20 or both) of the
17d82 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 73 20 named databases
17d83 64 69 64 20 6e 6f 74 20 65 78 69 73 74 2e 20 41 did not exist. A
17d84 6e 20 65 72 72 6f 72 20 68 61 73 0a 20 20 20 20 n error has.
17d85 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 ** already bee
17d86 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 n written into t
17d87 68 65 20 70 44 65 73 74 44 62 20 68 61 6e 64 6c he pDestDb handl
17d88 65 2e 20 41 6c 6c 20 74 68 61 74 20 69 73 20 6c e. All that is l
17d89 65 66 74 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 eft. ** to
17d8a 64 6f 20 68 65 72 65 20 69 73 20 66 72 65 65 20 do here is free
17d8b 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
17d8c 75 70 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 up structure..
17d8d 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
17d8e 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
17d8f 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 20 20 7d p = 0;. }
17d90 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 65 76 . }.. /* If ev
17d91 65 72 79 74 68 69 6e 67 20 68 61 73 20 67 6f 6e erything has gon
17d92 65 20 61 73 20 70 6c 61 6e 6e 65 64 2c 20 61 74 e as planned, at
17d93 74 61 63 68 20 74 68 65 20 62 61 63 6b 75 70 20 tach the backup
17d94 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 0a 20 20 object to the.
17d95 2a 2a 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e ** source pager.
17d96 20 54 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 The source page
17d97 72 20 63 61 6c 6c 73 20 42 61 63 6b 75 70 55 70 r calls BackupUp
17d98 64 61 74 65 28 29 20 61 6e 64 20 42 61 63 6b 75 date() and Backu
17d99 70 52 65 73 74 61 72 74 28 29 0a 20 20 2a 2a 20 pRestart(). **
17d9a 74 6f 20 6e 6f 74 69 66 79 20 74 68 69 73 20 6d to notify this m
17d9b 6f 64 75 6c 65 20 69 66 20 74 68 65 20 73 6f 75 odule if the sou
17d9c 72 63 65 20 66 69 6c 65 20 69 73 20 6d 6f 64 69 rce file is modi
17d9d 66 69 65 64 20 6d 69 64 2d 62 61 63 6b 75 70 2e fied mid-backup.
17d9e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b . */. if( p ){
17d9f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 61 63 . sqlite3_bac
17da0 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 kup **pp;
17da1 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
17da2 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61 67 65 to head of page
17da3 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74 20 2a rs backup list *
17da4 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 /. sqlite3Btr
17da5 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 eeEnter(p->pSrc)
17da6 3b 0a 20 20 20 20 70 70 20 3d 20 73 71 6c 69 74 ;. pp = sqlit
17da7 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 e3PagerBackupPtr
17da8 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 (sqlite3BtreePag
17da9 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 er(p->pSrc));.
17daa 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 70 p->pNext = *pp
17dab 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20 ;. *pp = p;.
17dac 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
17dad 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 eave(p->pSrc);.
17dae 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 p->pSrc->nBac
17daf 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 73 71 kup++;. }.. sq
17db0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
17db1 65 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 e(pDestDb->mutex
17db2 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
17db3 65 78 5f 6c 65 61 76 65 28 70 53 72 63 44 62 2d ex_leave(pSrcDb-
17db4 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 >mutex);. retur
17db5 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 n p;.}../*.** Ar
17db6 67 75 6d 65 6e 74 20 72 63 20 69 73 20 61 6e 20 gument rc is an
17db7 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 SQLite error cod
17db8 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 e. Return true i
17db9 66 20 74 68 69 73 20 65 72 72 6f 72 20 69 73 20 f this error is
17dba 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 66 .** considered f
17dbb 61 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e 74 65 atal if encounte
17dbc 72 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 red during a bac
17dbd 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 41 kup operation. A
17dbe 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a 20 61 72 65 ll errors.** are
17dbf 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 considered fata
17dc0 6c 20 65 78 63 65 70 74 20 66 6f 72 20 53 51 4c l except for SQL
17dc1 49 54 45 5f 42 55 53 59 20 61 6e 64 20 53 51 4c ITE_BUSY and SQL
17dc2 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 73 ITE_LOCKED..*/.s
17dc3 74 61 74 69 63 20 69 6e 74 20 69 73 46 61 74 61 tatic int isFata
17dc4 6c 45 72 72 6f 72 28 69 6e 74 20 72 63 29 7b 0a lError(int rc){.
17dc5 20 20 72 65 74 75 72 6e 20 28 72 63 21 3d 53 51 return (rc!=SQ
17dc6 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 LITE_OK && rc!=S
17dc7 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72 63 QLITE_BUSY && rc
17dc8 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 !=SQLITE_LOCKED)
17dc9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d ;.}../*.** Param
17dca 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70 6f eter zSrcData po
17dcb 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 ints to a buffer
17dcc 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
17dcd 64 61 74 61 20 66 6f 72 20 0a 2a 2a 20 70 61 67 data for .** pag
17dce 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20 74 68 e iSrcPg from th
17dcf 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
17dd0 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64 61 74 e. Copy this dat
17dd1 61 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 64 a into the .** d
17dd2 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
17dd3 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ase..*/.static i
17dd4 6e 74 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 nt backupOnePage
17dd5 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
17dd6 2a 70 2c 20 50 67 6e 6f 20 69 53 72 63 50 67 2c *p, Pgno iSrcPg,
17dd7 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 72 63 44 const u8 *zSrcD
17dd8 61 74 61 29 7b 0a 20 20 50 61 67 65 72 20 2a 20 ata){. Pager *
17dd9 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72 const pDestPager
17dda 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
17ddb 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 0a ager(p->pDest);.
17ddc 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63 const int nSrc
17ddd 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 Pgsz = sqlite3Bt
17dde 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 reeGetPageSize(p
17ddf 2d 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e ->pSrc);. int n
17de0 44 65 73 74 50 67 73 7a 20 3d 20 73 71 6c 69 74 DestPgsz = sqlit
17de1 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 e3BtreeGetPageSi
17de2 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 ze(p->pDest);.
17de3 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f 70 79 20 const int nCopy
17de4 3d 20 4d 49 4e 28 6e 53 72 63 50 67 73 7a 2c 20 = MIN(nSrcPgsz,
17de5 6e 44 65 73 74 50 67 73 7a 29 3b 0a 20 20 63 6f nDestPgsz);. co
17de6 6e 73 74 20 69 36 34 20 69 45 6e 64 20 3d 20 28 nst i64 iEnd = (
17de7 69 36 34 29 69 53 72 63 50 67 2a 28 69 36 34 29 i64)iSrcPg*(i64)
17de8 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 nSrcPgsz;.. int
17de9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
17dea 0a 20 20 69 36 34 20 69 4f 66 66 3b 0a 0a 20 20 . i64 iOff;..
17deb 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 73 74 assert( p->bDest
17dec 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 73 65 Locked );. asse
17ded 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f rt( !isFatalErro
17dee 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 61 73 r(p->rc) );. as
17def 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50 45 sert( iSrcPg!=PE
17df0 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
17df1 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 3b p->pSrc->pBt) );
17df2 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63 44 . assert( zSrcD
17df3 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 ata );.. /* Cat
17df4 63 68 20 74 68 65 20 63 61 73 65 20 77 68 65 72 ch the case wher
17df5 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f e the destinatio
17df6 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 n is an in-memor
17df7 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 y database and t
17df8 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 69 7a he. ** page siz
17df9 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 es of the source
17dfa 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e and destination
17dfb 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 differ. . */.
17dfc 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 3d 6e if( nSrcPgsz!=n
17dfd 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 6c 69 DestPgsz && sqli
17dfe 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 te3PagerIsMemdb(
17dff 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
17e00 72 28 70 2d 3e 70 44 65 73 74 29 29 20 29 7b 0a r(p->pDest)) ){.
17e01 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
17e02 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 READONLY;. }..
17e03 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72 75 /* This loop ru
17e04 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ns once for each
17e05 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 destination pag
17e06 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68 65 e spanned by the
17e07 20 73 6f 75 72 63 65 20 0a 20 20 2a 2a 20 70 61 source . ** pa
17e08 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69 74 65 ge. For each ite
17e09 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62 6c 65 ration, variable
17e0a 20 69 4f 66 66 20 69 73 20 73 65 74 20 74 6f 20 iOff is set to
17e0b 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a the byte offset.
17e0c 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 73 74 ** of the dest
17e0d 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 ination page..
17e0e 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 66 3d 69 45 */. for(iOff=iE
17e0f 6e 64 2d 28 69 36 34 29 6e 53 72 63 50 67 73 7a nd-(i64)nSrcPgsz
17e10 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ; rc==SQLITE_OK
17e11 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 69 4f && iOff<iEnd; iO
17e12 66 66 2b 3d 6e 44 65 73 74 50 67 73 7a 29 7b 0a ff+=nDestPgsz){.
17e13 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 65 73 DbPage *pDes
17e14 74 50 67 20 3d 20 30 3b 0a 20 20 20 20 50 67 6e tPg = 0;. Pgn
17e15 6f 20 69 44 65 73 74 20 3d 20 28 50 67 6e 6f 29 o iDest = (Pgno)
17e16 28 69 4f 66 66 2f 6e 44 65 73 74 50 67 73 7a 29 (iOff/nDestPgsz)
17e17 2b 31 3b 0a 20 20 20 20 69 66 28 20 69 44 65 73 +1;. if( iDes
17e18 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t==PENDING_BYTE_
17e19 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 PAGE(p->pDest->p
17e1a 42 74 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a Bt) ) continue;.
17e1b 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
17e1c 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K==(rc = sqlite3
17e1d 50 61 67 65 72 47 65 74 28 70 44 65 73 74 50 61 PagerGet(pDestPa
17e1e 67 65 72 2c 20 69 44 65 73 74 2c 20 26 70 44 65 ger, iDest, &pDe
17e1f 73 74 50 67 29 29 0a 20 20 20 20 20 26 26 20 53 stPg)). && S
17e20 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
17e21 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
17e22 65 28 70 44 65 73 74 50 67 29 29 0a 20 20 20 20 e(pDestPg)).
17e23 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 ){. const u
17e24 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 72 63 44 61 8 *zIn = &zSrcDa
17e25 74 61 5b 69 4f 66 66 25 6e 53 72 63 50 67 73 7a ta[iOff%nSrcPgsz
17e26 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 44 65 ];. u8 *zDe
17e27 73 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 stData = sqlite3
17e28 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 65 PagerGetData(pDe
17e29 73 74 50 67 29 3b 0a 20 20 20 20 20 20 75 38 20 stPg);. u8
17e2a 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 73 74 44 61 *zOut = &zDestDa
17e2b 74 61 5b 69 4f 66 66 25 6e 44 65 73 74 50 67 73 ta[iOff%nDestPgs
17e2c 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f z];.. /* Co
17e2d 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d py the data from
17e2e 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 the source page
17e2f 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e into the destin
17e30 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 20 20 ation page..
17e31 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72 20 ** Then clear
17e32 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 20 the Btree layer
17e33 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 66 MemPage.isInit f
17e34 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20 6d lag. Both this m
17e35 6f 64 75 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 odule. ** a
17e36 6e 64 20 74 68 65 20 70 61 67 65 72 20 63 6f 64 nd the pager cod
17e37 65 20 75 73 65 20 74 68 69 73 20 74 72 69 63 6b e use this trick
17e38 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65 20 66 (clearing the f
17e39 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 20 20 irst byte.
17e3a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 27 ** of the page '
17e3b 65 78 74 72 61 27 20 73 70 61 63 65 20 74 6f 20 extra' space to
17e3c 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 42 invalidate the B
17e3d 74 72 65 65 20 6c 61 79 65 72 73 0a 20 20 20 20 tree layers.
17e3e 20 20 2a 2a 20 63 61 63 68 65 64 20 70 61 72 73 ** cached pars
17e3f 65 20 6f 66 20 74 68 65 20 70 61 67 65 29 2e 20 e of the page).
17e40 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 69 MemPage.isInit i
17e41 73 20 6d 61 72 6b 65 64 20 0a 20 20 20 20 20 20 s marked .
17e42 2a 2a 20 22 4d 55 53 54 20 42 45 20 46 49 52 53 ** "MUST BE FIRS
17e43 54 22 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 T" for this purp
17e44 6f 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ose.. */.
17e45 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c memcpy(zOut,
17e46 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 zIn, nCopy);.
17e47 20 20 20 20 28 28 75 38 20 2a 29 73 71 6c 69 74 ((u8 *)sqlit
17e48 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
17e49 70 44 65 73 74 50 67 29 29 5b 30 5d 20 3d 20 30 pDestPg))[0] = 0
17e4a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
17e4b 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 te3PagerUnref(pD
17e4c 65 73 74 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 estPg);. }.. r
17e4d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
17e4e 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20 63 ** If pFile is c
17e4f 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 urrently larger
17e50 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 73 than iSize bytes
17e51 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 , then truncate
17e52 69 74 20 74 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 it to.** exactly
17e53 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20 49 66 iSize bytes. If
17e54 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20 6c 61 pFile is not la
17e55 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20 rger than iSize
17e56 62 79 74 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 bytes, then.** t
17e57 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
17e58 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 a no-op..**.** R
17e59 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
17e5a 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 if everything is
17e5b 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 successful, or
17e5c 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
17e5d 0a 2a 2a 20 63 6f 64 65 20 69 66 20 61 6e 20 65 .** code if an e
17e5e 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a rror occurs..*/.
17e5f 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75 static int backu
17e60 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 73 71 pTruncateFile(sq
17e61 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c lite3_file *pFil
17e62 65 2c 20 69 36 34 20 69 53 69 7a 65 29 7b 0a 20 e, i64 iSize){.
17e63 20 69 36 34 20 69 43 75 72 72 65 6e 74 3b 0a 20 i64 iCurrent;.
17e64 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
17e65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 3OsFileSize(pFil
17e66 65 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20 e, &iCurrent);.
17e67 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
17e68 4f 4b 20 26 26 20 69 43 75 72 72 65 6e 74 3e 69 OK && iCurrent>i
17e69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 20 3d Size ){. rc =
17e6a 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
17e6b 74 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 te(pFile, iSize)
17e6c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
17e6d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 c;.}../*.** Copy
17e6e 20 6e 50 61 67 65 20 70 61 67 65 73 20 66 72 6f nPage pages fro
17e6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74 m the source b-t
17e70 72 65 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 ree to the desti
17e71 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 nation..*/.SQLIT
17e72 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
17e73 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 3_backup_step(sq
17e74 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c lite3_backup *p,
17e75 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 int nPage){. i
17e76 6e 74 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74 65 nt rc;.. sqlite
17e77 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3_mutex_enter(p-
17e78 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b >pSrcDb->mutex);
17e79 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
17e7a 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 nter(p->pSrc);.
17e7b 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 if( p->pDestDb
17e7c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
17e7d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 utex_enter(p->pD
17e7e 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 estDb->mutex);.
17e7f 20 7d 0a 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63 }.. rc = p->rc
17e80 3b 0a 20 20 69 66 28 20 21 69 73 46 61 74 61 6c ;. if( !isFatal
17e81 45 72 72 6f 72 28 72 63 29 20 29 7b 0a 20 20 20 Error(rc) ){.
17e82 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 Pager * const p
17e83 53 72 63 50 61 67 65 72 20 3d 20 73 71 6c 69 74 SrcPager = sqlit
17e84 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e e3BtreePager(p->
17e85 70 53 72 63 29 3b 20 20 20 20 20 2f 2a 20 53 6f pSrc); /* So
17e86 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 20 20 urce pager */.
17e87 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 Pager * const
17e88 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c pDestPager = sql
17e89 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 ite3BtreePager(p
17e8a 2d 3e 70 44 65 73 74 29 3b 20 20 20 2f 2a 20 44 ->pDest); /* D
17e8b 65 73 74 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 est pager */.
17e8c 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 int ii;
17e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e8e 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 /* Iterator
17e8f 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 variable */.
17e90 69 6e 74 20 6e 53 72 63 50 61 67 65 20 3d 20 2d int nSrcPage = -
17e91 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
17e92 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 6f /* Size of so
17e93 75 72 63 65 20 64 62 20 69 6e 20 70 61 67 65 73 urce db in pages
17e94 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 43 6c 6f */. int bClo
17e95 73 65 54 72 61 6e 73 20 3d 20 30 3b 20 20 20 20 seTrans = 0;
17e96 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
17e97 75 65 20 69 66 20 73 72 63 20 64 62 20 72 65 71 ue if src db req
17e98 75 69 72 65 73 20 75 6e 6c 6f 63 6b 69 6e 67 20 uires unlocking
17e99 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 */.. /* If th
17e9a 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 69 e source pager i
17e9b 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 s currently in a
17e9c 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
17e9d 6f 6e 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 2a on, return. *
17e9e 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 6d * SQLITE_BUSY im
17e9f 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a mediately.. *
17ea0 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65 /. if( p->pDe
17ea1 73 74 44 62 20 26 26 20 70 2d 3e 70 53 72 63 2d stDb && p->pSrc-
17ea2 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
17ea3 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
17ea4 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
17ea5 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
17ea6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
17ea7 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
17ea8 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 }.. /* Lock
17ea9 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
17eaa 64 61 74 61 62 61 73 65 2c 20 69 66 20 69 74 20 database, if it
17eab 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c is not locked al
17eac 72 65 61 64 79 2e 20 2a 2f 0a 20 20 20 20 69 66 ready. */. if
17ead 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 ( SQLITE_OK==rc
17eae 26 26 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 && p->bDestLocke
17eaf 64 3d 3d 30 0a 20 20 20 20 20 26 26 20 53 51 4c d==0. && SQL
17eb0 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 ITE_OK==(rc = sq
17eb1 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
17eb2 72 61 6e 73 28 70 2d 3e 70 44 65 73 74 2c 20 32 rans(p->pDest, 2
17eb3 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 )) . ){.
17eb4 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 p->bDestLocked
17eb5 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 1;. rc =
17eb6 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
17eb7 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 20 31 2c eta(p->pDest, 1,
17eb8 20 26 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 &p->iDestSchema
17eb9 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a );. }.. /*
17eba 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
17ebb 6f 70 65 6e 20 72 65 61 64 2d 74 72 61 6e 73 61 open read-transa
17ebc 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 ction on the sou
17ebd 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 6f 70 rce database, op
17ebe 65 6e 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f en. ** one no
17ebf 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 w. If a transact
17ec0 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 68 65 ion is opened he
17ec1 72 65 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c re, then it will
17ec2 20 62 65 20 63 6c 6f 73 65 64 0a 20 20 20 20 2a be closed. *
17ec3 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 * before this fu
17ec4 6e 63 74 69 6f 6e 20 65 78 69 74 73 2e 0a 20 20 nction exits..
17ec5 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
17ec6 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d =SQLITE_OK && 0=
17ec7 3d 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 =sqlite3BtreeIsI
17ec8 6e 52 65 61 64 54 72 61 6e 73 28 70 2d 3e 70 53 nReadTrans(p->pS
17ec9 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 rc) ){. rc
17eca 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 = sqlite3BtreeBe
17ecb 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 ginTrans(p->pSrc
17ecc 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 43 6c 6f , 0);. bClo
17ecd 73 65 54 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20 seTrans = 1;.
17ece 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 }. . /* Now
17ecf 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 that there is a
17ed0 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 read-lock on th
17ed1 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
17ed2 65 2c 20 71 75 65 72 79 20 74 68 65 0a 20 20 20 e, query the.
17ed3 20 2a 2a 20 73 6f 75 72 63 65 20 70 61 67 65 72 ** source pager
17ed4 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20 for the number
17ed5 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
17ed6 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f database.. */
17ed7 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
17ed8 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
17ed9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
17eda 72 50 61 67 65 63 6f 75 6e 74 28 70 53 72 63 50 rPagecount(pSrcP
17edb 61 67 65 72 2c 20 26 6e 53 72 63 50 61 67 65 29 ager, &nSrcPage)
17edc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
17edd 69 69 3d 30 3b 20 28 6e 50 61 67 65 3c 30 20 7c ii=0; (nPage<0 |
17ede 7c 20 69 69 3c 6e 50 61 67 65 29 20 26 26 20 70 | ii<nPage) && p
17edf 2d 3e 69 4e 65 78 74 3c 3d 28 50 67 6e 6f 29 6e ->iNext<=(Pgno)n
17ee0 53 72 63 50 61 67 65 20 26 26 20 21 72 63 3b 20 SrcPage && !rc;
17ee1 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e ii++){. con
17ee2 73 74 20 50 67 6e 6f 20 69 53 72 63 50 67 20 3d st Pgno iSrcPg =
17ee3 20 70 2d 3e 69 4e 65 78 74 3b 20 20 20 20 20 20 p->iNext;
17ee4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
17ee5 75 72 63 65 20 70 61 67 65 20 6e 75 6d 62 65 72 urce page number
17ee6 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 53 */. if( iS
17ee7 72 63 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 rcPg!=PENDING_BY
17ee8 54 45 5f 50 41 47 45 28 70 2d 3e 70 53 72 63 2d TE_PAGE(p->pSrc-
17ee9 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 >pBt) ){.
17eea 20 44 62 50 61 67 65 20 2a 70 53 72 63 50 67 3b DbPage *pSrcPg;
17eeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17eec 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17eed 53 6f 75 72 63 65 20 70 61 67 65 20 6f 62 6a 65 Source page obje
17eee 63 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 ct */. rc
17eef 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
17ef0 65 74 28 70 53 72 63 50 61 67 65 72 2c 20 69 53 et(pSrcPager, iS
17ef1 72 63 50 67 2c 20 26 70 53 72 63 50 67 29 3b 0a rcPg, &pSrcPg);.
17ef2 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
17ef3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17ef4 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 63 6b rc = back
17ef5 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 53 72 upOnePage(p, iSr
17ef6 63 50 67 2c 20 73 71 6c 69 74 65 33 50 61 67 65 cPg, sqlite3Page
17ef7 72 47 65 74 44 61 74 61 28 70 53 72 63 50 67 29 rGetData(pSrcPg)
17ef8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
17ef9 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
17efa 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 SrcPg);.
17efb 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
17efc 70 2d 3e 69 4e 65 78 74 2b 2b 3b 0a 20 20 20 20 p->iNext++;.
17efd 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
17efe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17eff 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 20 3d p->nPagecount =
17f00 20 6e 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20 nSrcPage;.
17f01 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 20 3d p->nRemaining =
17f02 20 6e 53 72 63 50 61 67 65 2b 31 2d 70 2d 3e 69 nSrcPage+1-p->i
17f03 4e 65 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20 Next;. if(
17f04 70 2d 3e 69 4e 65 78 74 3e 28 50 67 6e 6f 29 6e p->iNext>(Pgno)n
17f05 53 72 63 50 61 67 65 20 29 7b 0a 20 20 20 20 20 SrcPage ){.
17f06 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 rc = SQLITE_D
17f07 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ONE;. }.
17f08 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63 }. . if( rc
17f09 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
17f0a 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 . const int
17f0b 20 6e 53 72 63 50 61 67 65 73 69 7a 65 20 3d 20 nSrcPagesize =
17f0c 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
17f0d 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 29 ageSize(p->pSrc)
17f0e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e ;. const in
17f0f 74 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 t nDestPagesize
17f10 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
17f11 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65 tPageSize(p->pDe
17f12 73 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e st);. int n
17f13 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20 20 DestTruncate;.
17f14 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 . /* Update
17f15 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 the schema vers
17f16 69 6f 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 65 ion field in the
17f17 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
17f18 61 62 61 73 65 2e 20 54 68 69 73 0a 20 20 20 20 abase. This.
17f19 20 20 2a 2a 20 69 73 20 74 6f 20 6d 61 6b 65 20 ** is to make
17f1a 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 63 sure that the sc
17f1b 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 72 65 61 hema-version rea
17f1c 6c 6c 79 20 64 6f 65 73 20 63 68 61 6e 67 65 20 lly does change
17f1d 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 in. ** the
17f1e 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 73 case where the s
17f1f 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e ource and destin
17f20 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 20 ation databases
17f21 68 61 76 65 20 74 68 65 0a 20 20 20 20 20 20 2a have the. *
17f22 2a 20 73 61 6d 65 20 73 63 68 65 6d 61 20 76 65 * same schema ve
17f23 72 73 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a rsion.. */.
17f24 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
17f25 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2d 3e eeUpdateMeta(p->
17f26 70 44 65 73 74 2c 20 31 2c 20 70 2d 3e 69 44 65 pDest, 1, p->iDe
17f27 73 74 53 63 68 65 6d 61 2b 31 29 3b 0a 20 20 20 stSchema+1);.
17f28 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 if( p->pDestD
17f29 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c b ){. sql
17f2a 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
17f2b 6c 53 63 68 65 6d 61 28 70 2d 3e 70 44 65 73 74 lSchema(p->pDest
17f2c 44 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a Db, 0);. }.
17f2d 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 44 . /* Set nD
17f2e 65 73 74 54 72 75 6e 63 61 74 65 20 74 6f 20 74 estTruncate to t
17f2f 68 65 20 66 69 6e 61 6c 20 6e 75 6d 62 65 72 20 he final number
17f30 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
17f31 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 destination.
17f32 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 ** database. T
17f33 68 65 20 63 6f 6d 70 6c 69 63 61 74 69 6f 6e 20 he complication
17f34 68 65 72 65 20 69 73 20 74 68 61 74 20 74 68 65 here is that the
17f35 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 destination pag
17f36 65 0a 20 20 20 20 20 20 2a 2a 20 73 69 7a 65 20 e. ** size
17f37 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74 may be different
17f38 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 70 to the source p
17f39 61 67 65 20 73 69 7a 65 2e 20 0a 20 20 20 20 20 age size. .
17f3a 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 **. ** If
17f3b 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 20 the source page
17f3c 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 size is smaller
17f3d 74 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 than the destina
17f3e 74 69 6f 6e 20 70 61 67 65 20 73 69 7a 65 2c 20 tion page size,
17f3f 0a 20 20 20 20 20 20 2a 2a 20 72 6f 75 6e 64 20 . ** round
17f40 75 70 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 up. In this case
17f41 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c the call to sql
17f42 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 29 ite3OsTruncate()
17f43 20 62 65 6c 6f 77 20 77 69 6c 6c 0a 20 20 20 20 below will.
17f44 20 20 2a 2a 20 66 69 78 20 74 68 65 20 73 69 7a ** fix the siz
17f45 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 48 e of the file. H
17f46 6f 77 65 76 65 72 20 69 74 20 69 73 20 69 6d 70 owever it is imp
17f47 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 0a 20 ortant to call.
17f48 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 ** sqlite3P
17f49 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 agerTruncateImag
17f4a 65 28 29 20 68 65 72 65 20 73 6f 20 74 68 61 74 e() here so that
17f4b 20 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74 68 any pages in th
17f4c 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 74 e . ** dest
17f4d 69 6e 61 74 69 6f 6e 20 66 69 6c 65 20 74 68 61 ination file tha
17f4e 74 20 6c 69 65 20 62 65 79 6f 6e 64 20 74 68 65 t lie beyond the
17f4f 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 70 nDestTruncate p
17f50 61 67 65 20 6d 61 72 6b 20 61 72 65 0a 20 20 20 age mark are.
17f51 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 ** journalled
17f52 20 62 79 20 50 61 67 65 72 43 6f 6d 6d 69 74 50 by PagerCommitP
17f53 68 61 73 65 4f 6e 65 28 29 20 62 65 66 6f 72 65 haseOne() before
17f54 20 74 68 65 79 20 61 72 65 20 64 65 73 74 72 6f they are destro
17f55 79 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 yed. ** by
17f56 74 68 65 20 66 69 6c 65 20 74 72 75 6e 63 61 74 the file truncat
17f57 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion.. */.
17f58 20 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65 if( nSrcPage
17f59 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69 size<nDestPagesi
17f5a 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e ze ){. in
17f5b 74 20 72 61 74 69 6f 20 3d 20 6e 44 65 73 74 50 t ratio = nDestP
17f5c 61 67 65 73 69 7a 65 2f 6e 53 72 63 50 61 67 65 agesize/nSrcPage
17f5d 73 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 6e 44 size;. nD
17f5e 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 28 6e estTruncate = (n
17f5f 53 72 63 50 61 67 65 2b 72 61 74 69 6f 2d 31 29 SrcPage+ratio-1)
17f60 2f 72 61 74 69 6f 3b 0a 20 20 20 20 20 20 20 20 /ratio;.
17f61 69 66 28 20 6e 44 65 73 74 54 72 75 6e 63 61 74 if( nDestTruncat
17f62 65 3d 3d 28 69 6e 74 29 50 45 4e 44 49 4e 47 5f e==(int)PENDING_
17f63 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 BYTE_PAGE(p->pDe
17f64 73 74 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 st->pBt) ){.
17f65 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63 nDestTrunc
17f66 61 74 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d ate--;. }
17f67 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
17f68 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63 nDestTrunc
17f69 61 74 65 20 3d 20 6e 53 72 63 50 61 67 65 20 2a ate = nSrcPage *
17f6a 20 28 6e 53 72 63 50 61 67 65 73 69 7a 65 2f 6e (nSrcPagesize/n
17f6b 44 65 73 74 50 61 67 65 73 69 7a 65 29 3b 0a 20 DestPagesize);.
17f6c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
17f6d 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
17f6e 65 49 6d 61 67 65 28 70 44 65 73 74 50 61 67 65 eImage(pDestPage
17f6f 72 2c 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 r, nDestTruncate
17f70 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 6e 53 );.. if( nS
17f71 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 rcPagesize<nDest
17f72 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 Pagesize ){.
17f73 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f /* If the so
17f74 75 72 63 65 20 70 61 67 65 2d 73 69 7a 65 20 69 urce page-size i
17f75 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 s smaller than t
17f76 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 he destination p
17f77 61 67 65 2d 73 69 7a 65 2c 0a 20 20 20 20 20 20 age-size,.
17f78 20 20 2a 2a 20 74 77 6f 20 65 78 74 72 61 20 74 ** two extra t
17f79 68 69 6e 67 73 20 6d 61 79 20 6e 65 65 64 20 74 hings may need t
17f7a 6f 20 68 61 70 70 65 6e 3a 0a 20 20 20 20 20 20 o happen:.
17f7b 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 **. **
17f7c 20 20 2a 20 54 68 65 20 64 65 73 74 69 6e 61 74 * The destinat
17f7d 69 6f 6e 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 ion may need to
17f7e 62 65 20 74 72 75 6e 63 61 74 65 64 2c 20 61 6e be truncated, an
17f7f 64 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 d. **.
17f80 20 20 20 20 20 2a 2a 20 20 20 2a 20 44 61 74 61 ** * Data
17f81 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 70 stored on the p
17f82 61 67 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 ages immediately
17f83 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 0a following the .
17f84 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 70 ** p
17f85 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 ending-byte page
17f86 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 in the source d
17f87 61 74 61 62 61 73 65 20 6d 61 79 20 6e 65 65 64 atabase may need
17f88 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20 2a to be. *
17f89 2a 20 20 20 20 20 63 6f 70 69 65 64 20 69 6e 74 * copied int
17f8a 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
17f8b 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 n database..
17f8c 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 */. c
17f8d 6f 6e 73 74 20 69 36 34 20 69 53 69 7a 65 20 3d onst i64 iSize =
17f8e 20 28 69 36 34 29 6e 53 72 63 50 61 67 65 73 69 (i64)nSrcPagesi
17f8f 7a 65 20 2a 20 28 69 36 34 29 6e 53 72 63 50 61 ze * (i64)nSrcPa
17f90 67 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ge;. sqli
17f91 74 65 33 5f 66 69 6c 65 20 2a 20 63 6f 6e 73 74 te3_file * const
17f92 20 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 pFile = sqlite3
17f93 50 61 67 65 72 46 69 6c 65 28 70 44 65 73 74 50 PagerFile(pDestP
17f94 61 67 65 72 29 3b 0a 0a 20 20 20 20 20 20 20 20 ager);..
17f95 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
17f96 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
17f97 20 28 69 36 34 29 6e 44 65 73 74 54 72 75 6e 63 (i64)nDestTrunc
17f98 61 74 65 2a 28 69 36 34 29 6e 44 65 73 74 50 61 ate*(i64)nDestPa
17f99 67 65 73 69 7a 65 20 3e 3d 20 69 53 69 7a 65 20 gesize >= iSize
17f9a 7c 7c 20 28 0a 20 20 20 20 20 20 20 20 20 20 20 || (.
17f9b 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 nDestTruncate
17f9c 3d 3d 28 69 6e 74 29 28 50 45 4e 44 49 4e 47 5f ==(int)(PENDING_
17f9d 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 BYTE_PAGE(p->pDe
17f9e 73 74 2d 3e 70 42 74 29 2d 31 29 0a 20 20 20 20 st->pBt)-1).
17f9f 20 20 20 20 20 20 20 26 26 20 69 53 69 7a 65 3e && iSize>
17fa0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 =PENDING_BYTE &&
17fa1 20 69 53 69 7a 65 3c 3d 50 45 4e 44 49 4e 47 5f iSize<=PENDING_
17fa2 42 59 54 45 2b 6e 44 65 73 74 50 61 67 65 73 69 BYTE+nDestPagesi
17fa3 7a 65 0a 20 20 20 20 20 20 20 20 29 29 3b 0a 20 ze. ));.
17fa4 20 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 if( SQLIT
17fa5 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
17fa6 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 te3PagerCommitPh
17fa7 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 aseOne(pDestPage
17fa8 72 2c 20 30 2c 20 31 29 29 0a 20 20 20 20 20 20 r, 0, 1)).
17fa9 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d && SQLITE_OK=
17faa 3d 28 72 63 20 3d 20 62 61 63 6b 75 70 54 72 75 =(rc = backupTru
17fab 6e 63 61 74 65 46 69 6c 65 28 70 46 69 6c 65 2c ncateFile(pFile,
17fac 20 69 53 69 7a 65 29 29 0a 20 20 20 20 20 20 20 iSize)).
17fad 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d && SQLITE_OK==
17fae 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
17faf 65 72 53 79 6e 63 28 70 44 65 73 74 50 61 67 65 erSync(pDestPage
17fb0 72 29 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 r)). ){.
17fb1 20 20 20 20 20 20 20 20 20 69 36 34 20 69 4f 66 i64 iOf
17fb2 66 3b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34 f;. i64
17fb3 20 69 45 6e 64 20 3d 20 4d 49 4e 28 50 45 4e 44 iEnd = MIN(PEND
17fb4 49 4e 47 5f 42 59 54 45 20 2b 20 6e 44 65 73 74 ING_BYTE + nDest
17fb5 50 61 67 65 73 69 7a 65 2c 20 69 53 69 7a 65 29 Pagesize, iSize)
17fb6 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 ;. for(
17fb7 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66 . iOf
17fb8 66 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e f=PENDING_BYTE+n
17fb9 53 72 63 50 61 67 65 73 69 7a 65 3b 20 0a 20 20 SrcPagesize; .
17fba 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 rc==SQ
17fbb 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c LITE_OK && iOff<
17fbc 69 45 6e 64 3b 20 0a 20 20 20 20 20 20 20 20 20 iEnd; .
17fbd 20 20 20 69 4f 66 66 2b 3d 6e 53 72 63 50 61 67 iOff+=nSrcPag
17fbe 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20 20 20 esize.
17fbf 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 50 ){. P
17fc0 67 48 64 72 20 2a 70 53 72 63 50 67 20 3d 20 30 gHdr *pSrcPg = 0
17fc1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6f ;. co
17fc2 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50 67 20 nst Pgno iSrcPg
17fc3 3d 20 28 50 67 6e 6f 29 28 28 69 4f 66 66 2f 6e = (Pgno)((iOff/n
17fc4 53 72 63 50 61 67 65 73 69 7a 65 29 2b 31 29 3b SrcPagesize)+1);
17fc5 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
17fc6 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
17fc7 74 28 70 53 72 63 50 61 67 65 72 2c 20 69 53 72 t(pSrcPager, iSr
17fc8 63 50 67 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 cPg, &pSrcPg);.
17fc9 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
17fca 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
17fcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 38 u8
17fcc 20 2a 7a 44 61 74 61 20 3d 20 73 71 6c 69 74 65 *zData = sqlite
17fcd 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 53 3PagerGetData(pS
17fce 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 rcPg);.
17fcf 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
17fd0 33 4f 73 57 72 69 74 65 28 70 46 69 6c 65 2c 20 3OsWrite(pFile,
17fd1 7a 44 61 74 61 2c 20 6e 53 72 63 50 61 67 65 73 zData, nSrcPages
17fd2 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20 ize, iOff);.
17fd3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17fd4 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
17fd5 65 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b erUnref(pSrcPg);
17fd6 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
17fd7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
17fd8 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d se{. rc =
17fd9 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
17fda 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73 mitPhaseOne(pDes
17fdb 74 50 61 67 65 72 2c 20 30 2c 20 30 29 3b 0a 20 tPager, 0, 0);.
17fdc 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 }. .
17fdd 2f 2a 20 46 69 6e 69 73 68 20 63 6f 6d 6d 69 74 /* Finish commit
17fde 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 ting the transac
17fdf 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 65 73 74 tion to the dest
17fe0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
17fe1 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 53 . */. if( S
17fe2 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20 QLITE_OK==rc.
17fe3 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b && SQLITE_OK
17fe4 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 ==(rc = sqlite3B
17fe5 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
17fe6 77 6f 28 70 2d 3e 70 44 65 73 74 29 29 0a 20 20 wo(p->pDest)).
17fe7 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ){. r
17fe8 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
17fe9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
17fea 20 0a 20 20 20 20 2f 2a 20 49 66 20 62 43 6c 6f . /* If bClo
17feb 73 65 54 72 61 6e 73 20 69 73 20 74 72 75 65 2c seTrans is true,
17fec 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
17fed 69 6f 6e 20 6f 70 65 6e 65 64 20 61 20 72 65 61 ion opened a rea
17fee 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 d transaction.
17fef 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 6f 75 72 ** on the sour
17ff0 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 6c 6f ce database. Clo
17ff1 73 65 20 74 68 65 20 72 65 61 64 20 74 72 61 6e se the read tran
17ff2 73 61 63 74 69 6f 6e 20 68 65 72 65 2e 20 54 68 saction here. Th
17ff3 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f ere is. ** no
17ff4 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 74 need to check t
17ff5 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 he return values
17ff6 20 6f 66 20 74 68 65 20 62 74 72 65 65 20 6d 65 of the btree me
17ff7 74 68 6f 64 73 20 68 65 72 65 2c 20 61 73 0a 20 thods here, as.
17ff8 20 20 20 2a 2a 20 22 63 6f 6d 6d 69 74 74 69 6e ** "committin
17ff9 67 22 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 g" a read-only t
17ffa 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f ransaction canno
17ffb 74 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20 t fail.. */.
17ffc 20 20 20 69 66 28 20 62 43 6c 6f 73 65 54 72 61 if( bCloseTra
17ffd 6e 73 20 29 7b 0a 20 20 20 20 20 20 54 45 53 54 ns ){. TEST
17ffe 4f 4e 4c 59 28 20 69 6e 74 20 72 63 32 20 29 3b ONLY( int rc2 );
17fff 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 . TESTONLY(
18000 20 72 63 32 20 20 3d 20 29 20 73 71 6c 69 74 65 rc2 = ) sqlite
18001 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
18002 65 4f 6e 65 28 70 2d 3e 70 53 72 63 2c 20 30 29 eOne(p->pSrc, 0)
18003 3b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 ;. TESTONLY
18004 28 20 72 63 32 20 7c 3d 20 29 20 73 71 6c 69 74 ( rc2 |= ) sqlit
18005 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
18006 73 65 54 77 6f 28 70 2d 3e 70 53 72 63 29 3b 0a seTwo(p->pSrc);.
18007 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
18008 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 2==SQLITE_OK );.
18009 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 70 2d 3e }. . p->
1800a 72 63 20 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 69 rc = rc;. }. i
1800b 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b f( p->pDestDb ){
1800c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1800d 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73 ex_leave(p->pDes
1800e 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d tDb->mutex);. }
1800f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
18010 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 eave(p->pSrc);.
18011 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
18012 65 61 76 65 28 70 2d 3e 70 53 72 63 44 62 2d 3e eave(p->pSrcDb->
18013 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
18014 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
18015 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 lease all resour
18016 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ces associated w
18017 69 74 68 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 ith an sqlite3_b
18018 61 63 6b 75 70 2a 20 68 61 6e 64 6c 65 2e 0a 2a ackup* handle..*
18019 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1801a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
1801b 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62 finish(sqlite3_b
1801c 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c ackup *p){. sql
1801d 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 ite3_backup **pp
1801e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1801f 20 20 2f 2a 20 50 74 72 20 74 6f 20 68 65 61 64 /* Ptr to head
18020 20 6f 66 20 70 61 67 65 72 73 20 62 61 63 6b 75 of pagers backu
18021 70 20 6c 69 73 74 20 2a 2f 0a 20 20 73 71 6c 69 p list */. sqli
18022 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
18023 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
18024 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 70 72 6f /* Mutex to pro
18025 74 65 63 74 20 73 6f 75 72 63 65 20 64 61 74 61 tect source data
18026 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 base */. int rc
18027 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
18028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18029 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
1802a 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 45 6e 74 65 72 n */.. /* Enter
1802b 20 74 68 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a the mutexes */.
1802c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1802d 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d enter(p->pSrcDb-
1802e 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 >mutex);. sqlit
1802f 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e e3BtreeEnter(p->
18030 70 53 72 63 29 3b 0a 20 20 6d 75 74 65 78 20 3d pSrc);. mutex =
18031 20 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 p->pSrcDb->mute
18032 78 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 x;. if( p->pDes
18033 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 tDb ){. sqlit
18034 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
18035 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 ->pDestDb->mutex
18036 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 );. }.. /* Det
18037 61 63 68 20 74 68 69 73 20 62 61 63 6b 75 70 20 ach this backup
18038 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 from the source
18039 70 61 67 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 pager. */. if(
1803a 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 p->pDestDb ){.
1803b 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 61 pp = sqlite3Pa
1803c 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 6c gerBackupPtr(sql
1803d 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 ite3BtreePager(p
1803e 2d 3e 70 53 72 63 29 29 3b 0a 20 20 20 20 77 68 ->pSrc));. wh
1803f 69 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b 0a 20 ile( *pp!=p ){.
18040 20 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 pp = &(*pp)
18041 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 ->pNext;. }.
18042 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 *pp = p->pNex
18043 74 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e t;. p->pSrc->
18044 6e 42 61 63 6b 75 70 2d 2d 3b 0a 20 20 7d 0a 0a nBackup--;. }..
18045 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61 /* If a transa
18046 63 74 69 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f ction is still o
18047 70 65 6e 20 6f 6e 20 74 68 65 20 42 74 72 65 65 pen on the Btree
18048 2c 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 , roll it back.
18049 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 */. sqlite3Btre
1804a 65 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e 70 44 65 eRollback(p->pDe
1804b 73 74 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 st);.. /* Set t
1804c 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 he error code of
1804d 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
1804e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1804f 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e . */. rc = (p->
18050 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 rc==SQLITE_DONE)
18051 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70 ? SQLITE_OK : p
18052 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 45 ->rc;. sqlite3E
18053 72 72 6f 72 28 70 2d 3e 70 44 65 73 74 44 62 2c rror(p->pDestDb,
18054 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 rc, 0);.. /* E
18055 78 69 74 20 74 68 65 20 6d 75 74 65 78 65 73 20 xit the mutexes
18056 61 6e 64 20 66 72 65 65 20 74 68 65 20 62 61 63 and free the bac
18057 6b 75 70 20 63 6f 6e 74 65 78 74 20 73 74 72 75 kup context stru
18058 63 74 75 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20 cture. */. if(
18059 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 p->pDestDb ){.
1805a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1805b 6c 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62 leave(p->pDestDb
1805c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
1805d 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1805e 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 e(p->pSrc);. if
1805f 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a ( p->pDestDb ){.
18060 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
18061 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 (p);. }. sqlit
18062 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
18063 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
18064 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
18065 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
18066 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f f pages still to
18067 20 62 65 20 62 61 63 6b 65 64 20 75 70 20 61 73 be backed up as
18068 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
18069 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 ent.** call to s
1806a 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
1806b 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ep()..*/.SQLITE_
1806c 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1806d 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 backup_remaining
1806e 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
1806f 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
18070 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a 7d 0a 0a >nRemaining;.}..
18071 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
18072 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
18073 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f pages in the so
18074 75 72 63 65 20 64 61 74 61 62 61 73 65 20 61 73 urce database as
18075 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 0a 2a 2a of the most .**
18076 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 recent call to
18077 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
18078 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tep()..*/.SQLITE
18079 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1807a 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e _backup_pagecoun
1807b 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 t(sqlite3_backup
1807c 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
1807d 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 3b 0a 7d 0a ->nPagecount;.}.
1807e 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1807f 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 tion is called a
18080 66 74 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 fter the content
18081 73 20 6f 66 20 70 61 67 65 20 69 50 61 67 65 20 s of page iPage
18082 6f 66 20 74 68 65 0a 2a 2a 20 73 6f 75 72 63 65 of the.** source
18083 20 64 61 74 61 62 61 73 65 20 68 61 76 65 20 62 database have b
18084 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 49 66 een modified. If
18085 20 70 61 67 65 20 69 50 61 67 65 20 68 61 73 20 page iPage has
18086 61 6c 72 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a already been .**
18087 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 copied into the
18088 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
18089 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 abase, then the
1808a 64 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f 20 data written to
1808b 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 the.** destinati
1808c 6f 6e 20 69 73 20 6e 6f 77 20 69 6e 76 61 6c 69 on is now invali
1808d 64 61 74 65 64 2e 20 54 68 65 20 64 65 73 74 69 dated. The desti
1808e 6e 61 74 69 6f 6e 20 63 6f 70 79 20 6f 66 20 69 nation copy of i
1808f 50 61 67 65 20 6e 65 65 64 73 0a 2a 2a 20 74 6f Page needs.** to
18090 20 62 65 20 75 70 64 61 74 65 64 20 77 69 74 68 be updated with
18091 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 62 65 the new data be
18092 66 6f 72 65 20 74 68 65 20 62 61 63 6b 75 70 20 fore the backup
18093 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 operation is.**
18094 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 complete..**.**
18095 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 It is assumed th
18096 61 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 at the mutex ass
18097 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
18098 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
18099 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e .** correspondin
1809a 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 g to the source
1809b 64 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 database is held
1809c 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
1809d 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 ion is.** called
1809e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1809f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
180a0 42 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c BackupUpdate(sql
180a1 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 ite3_backup *pBa
180a2 63 6b 75 70 2c 20 50 67 6e 6f 20 69 50 61 67 65 ckup, Pgno iPage
180a3 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 , const u8 *aDat
180a4 61 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 a){. sqlite3_ba
180a5 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 ckup *p;
180a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 /* It
180a7 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 erator variable
180a8 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b */. for(p=pBack
180a9 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 up; p; p=p->pNex
180aa 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 t){. assert(
180ab 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
180ac 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d ld(p->pSrc->pBt-
180ad 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 69 >mutex) );. i
180ae 66 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 f( !isFatalError
180af 28 70 2d 3e 72 63 29 20 26 26 20 69 50 61 67 65 (p->rc) && iPage
180b0 3c 70 2d 3e 69 4e 65 78 74 20 29 7b 0a 20 20 20 <p->iNext ){.
180b1 20 20 20 2f 2a 20 54 68 65 20 62 61 63 6b 75 70 /* The backup
180b2 20 70 72 6f 63 65 73 73 20 70 20 68 61 73 20 61 process p has a
180b3 6c 72 65 61 64 79 20 63 6f 70 69 65 64 20 70 61 lready copied pa
180b4 67 65 20 69 50 61 67 65 2e 20 42 75 74 20 6e 6f ge iPage. But no
180b5 77 20 69 74 0a 20 20 20 20 20 20 2a 2a 20 68 61 w it. ** ha
180b6 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 s been modified
180b7 62 79 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e by a transaction
180b8 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 70 on the source p
180b9 61 67 65 72 2e 20 43 6f 70 79 0a 20 20 20 20 20 ager. Copy.
180ba 20 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 ** the new data
180bb 20 69 6e 74 6f 20 74 68 65 20 62 61 63 6b 75 70 into the backup
180bc 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
180bd 20 69 6e 74 20 72 63 20 3d 20 62 61 63 6b 75 70 int rc = backup
180be 4f 6e 65 50 61 67 65 28 70 2c 20 69 50 61 67 65 OnePage(p, iPage
180bf 2c 20 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 , aData);.
180c0 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
180c1 54 45 5f 42 55 53 59 20 26 26 20 72 63 21 3d 53 TE_BUSY && rc!=S
180c2 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 3b 0a QLITE_LOCKED );.
180c3 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
180c4 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
180c5 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 p->rc = rc;.
180c6 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
180c7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72 .}../*.** Restar
180c8 74 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f t the backup pro
180c9 63 65 73 73 2e 20 54 68 69 73 20 69 73 20 63 61 cess. This is ca
180ca 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 lled when the pa
180cb 67 65 72 20 6c 61 79 65 72 0a 2a 2a 20 64 65 74 ger layer.** det
180cc 65 63 74 73 20 74 68 61 74 20 74 68 65 20 64 61 ects that the da
180cd 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 tabase has been
180ce 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 65 modified by an e
180cf 78 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 xternal database
180d0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 .** connection.
180d1 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
180d2 72 65 20 69 73 20 6e 6f 20 77 61 79 20 6f 66 20 re is no way of
180d3 6b 6e 6f 77 69 6e 67 20 77 68 69 63 68 20 6f 66 knowing which of
180d4 20 74 68 65 0a 2a 2a 20 70 61 67 65 73 20 74 68 the.** pages th
180d5 61 74 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70 at have been cop
180d6 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 ied into the des
180d7 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
180d8 65 20 61 72 65 20 73 74 69 6c 6c 20 0a 2a 2a 20 e are still .**
180d9 76 61 6c 69 64 20 61 6e 64 20 77 68 69 63 68 20 valid and which
180da 61 72 65 20 6e 6f 74 2c 20 73 6f 20 74 68 65 20 are not, so the
180db 65 6e 74 69 72 65 20 70 72 6f 63 65 73 73 20 6e entire process n
180dc 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 eeds to be resta
180dd 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 rted..**.** It i
180de 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 s assumed that t
180df 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 he mutex associa
180e0 74 65 64 20 77 69 74 68 20 74 68 65 20 42 74 53 ted with the BtS
180e1 68 61 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 hared object.**
180e2 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
180e3 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
180e4 62 61 73 65 20 69 73 20 68 65 6c 64 20 77 68 65 base is held whe
180e5 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
180e6 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f is.** called..*/
180e7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
180e8 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b void sqlite3Back
180e9 75 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 upRestart(sqlite
180ea 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 3_backup *pBacku
180eb 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 p){. sqlite3_ba
180ec 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 ckup *p;
180ed 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 /* It
180ee 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 erator variable
180ef 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b */. for(p=pBack
180f0 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 up; p; p=p->pNex
180f1 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 t){. assert(
180f2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
180f3 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d ld(p->pSrc->pBt-
180f4 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 >mutex) );. p
180f5 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 7d ->iNext = 1;. }
180f6 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
180f7 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f TE_OMIT_VACUUM./
180f8 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 6f *.** Copy the co
180f9 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f mplete content o
180fa 66 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70 f pBtFrom into p
180fb 42 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61 63 BtTo. A transac
180fc 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 tion.** must be
180fd 61 63 74 69 76 65 20 66 6f 72 20 62 6f 74 68 20 active for both
180fe 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 files..**.** The
180ff 20 73 69 7a 65 20 6f 66 20 66 69 6c 65 20 70 54 size of file pT
18100 6f 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64 o may be reduced
18101 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 by this operati
18102 6f 6e 2e 20 49 66 20 61 6e 79 74 68 69 6e 67 20 on. If anything
18103 0a 2a 2a 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 .** goes wrong,
18104 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
18105 6f 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 on pTo is rolled
18106 20 62 61 63 6b 2e 20 49 66 20 73 75 63 63 65 73 back. If succes
18107 73 66 75 6c 2c 20 74 68 65 20 0a 2a 2a 20 74 72 sful, the .** tr
18108 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d ansaction is com
18109 6d 69 74 74 65 64 20 62 65 66 6f 72 65 20 72 65 mitted before re
1810a 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 turning..*/.SQLI
1810b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1810c 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 qlite3BtreeCopyF
1810d 69 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 ile(Btree *pTo,
1810e 42 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 Btree *pFrom){.
1810f 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
18110 65 33 5f 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 e3_backup b;. s
18111 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
18112 28 70 54 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 (pTo);. sqlite3
18113 42 74 72 65 65 45 6e 74 65 72 28 70 46 72 6f 6d BtreeEnter(pFrom
18114 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 );.. /* Set up
18115 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 an sqlite3_backu
18116 70 20 6f 62 6a 65 63 74 2e 20 73 71 6c 69 74 65 p object. sqlite
18117 33 5f 62 61 63 6b 75 70 2e 70 44 65 73 74 44 62 3_backup.pDestDb
18118 20 6d 75 73 74 20 62 65 20 73 65 74 0a 20 20 2a must be set. *
18119 2a 20 74 6f 20 30 2e 20 54 68 69 73 20 69 73 20 * to 0. This is
1811a 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c used by the impl
1811b 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 ementations of s
1811c 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
1811d 65 70 28 29 0a 20 20 2a 2a 20 61 6e 64 20 73 71 ep(). ** and sq
1811e 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
1811f 69 73 68 28 29 20 74 6f 20 64 65 74 65 63 74 20 ish() to detect
18120 74 68 61 74 20 74 68 65 79 20 61 72 65 20 62 65 that they are be
18121 69 6e 67 20 63 61 6c 6c 65 64 0a 20 20 2a 2a 20 ing called. **
18122 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 from this functi
18123 6f 6e 2c 20 6e 6f 74 20 64 69 72 65 63 74 6c 79 on, not directly
18124 20 62 79 20 74 68 65 20 75 73 65 72 2e 0a 20 20 by the user..
18125 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 62 2c 20 */. memset(&b,
18126 30 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 0a 20 0, sizeof(b));.
18127 20 62 2e 70 53 72 63 44 62 20 3d 20 70 46 72 6f b.pSrcDb = pFro
18128 6d 2d 3e 64 62 3b 0a 20 20 62 2e 70 53 72 63 20 m->db;. b.pSrc
18129 3d 20 70 46 72 6f 6d 3b 0a 20 20 62 2e 70 44 65 = pFrom;. b.pDe
1812a 73 74 20 3d 20 70 54 6f 3b 0a 20 20 62 2e 69 4e st = pTo;. b.iN
1812b 65 78 74 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 30 ext = 1;.. /* 0
1812c 78 37 46 46 46 46 46 46 46 20 69 73 20 74 68 65 x7FFFFFFF is the
1812d 20 68 61 72 64 20 6c 69 6d 69 74 20 66 6f 72 20 hard limit for
1812e 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
1812f 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 ges in a databas
18130 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 42 79 20 e. ** file. By
18131 70 61 73 73 69 6e 67 20 74 68 69 73 20 61 73 20 passing this as
18132 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
18133 67 65 73 20 74 6f 20 63 6f 70 79 20 74 6f 0a 20 ges to copy to.
18134 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ** sqlite3_back
18135 75 70 5f 73 74 65 70 28 29 2c 20 77 65 20 63 61 up_step(), we ca
18136 6e 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 n guarantee that
18137 20 74 68 65 20 63 6f 70 79 20 66 69 6e 69 73 68 the copy finish
18138 65 73 20 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 es . ** within
18139 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 28 75 a single call (u
1813a 6e 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f nless an error o
1813b 63 63 75 72 73 29 2e 20 54 68 65 20 61 73 73 65 ccurs). The asse
1813c 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 0a 20 rt() statement.
1813d 20 2a 2a 20 63 68 65 63 6b 73 20 74 68 69 73 20 ** checks this
1813e 61 73 73 75 6d 70 74 69 6f 6e 20 2d 20 28 70 2d assumption - (p-
1813f 3e 72 63 29 20 73 68 6f 75 6c 64 20 62 65 20 73 >rc) should be s
18140 65 74 20 74 6f 20 65 69 74 68 65 72 20 53 51 4c et to either SQL
18141 49 54 45 5f 44 4f 4e 45 20 0a 20 20 2a 2a 20 6f ITE_DONE . ** o
18142 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e r an error code.
18143 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f . */. sqlite3_
18144 62 61 63 6b 75 70 5f 73 74 65 70 28 26 62 2c 20 backup_step(&b,
18145 30 78 37 46 46 46 46 46 46 46 29 3b 0a 20 20 61 0x7FFFFFFF);. a
18146 73 73 65 72 74 28 20 62 2e 72 63 21 3d 53 51 4c ssert( b.rc!=SQL
18147 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d ITE_OK );. rc =
18148 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
18149 66 69 6e 69 73 68 28 26 62 29 3b 0a 20 20 69 66 finish(&b);. if
1814a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1814b 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 70 42 74 2d ){. pTo->pBt-
1814c 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d >pageSizeFixed =
1814d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 0;. }.. sqlit
1814e 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 46 72 e3BtreeLeave(pFr
1814f 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 om);. sqlite3Bt
18150 72 65 65 4c 65 61 76 65 28 70 54 6f 29 3b 0a 20 reeLeave(pTo);.
18151 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
18152 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
18153 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f MIT_VACUUM */../
18154 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
18155 6e 64 20 6f 66 20 62 61 63 6b 75 70 2e 63 20 2a nd of backup.c *
18156 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18157 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
18159 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1815a 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 6d 65 egin file vdbeme
1815b 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m.c ************
1815c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1815d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1815e 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 *.** 2004 May 26
1815f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
18160 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
18161 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
18162 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
18163 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
18164 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
18165 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
18166 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
18167 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
18168 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
18169 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1816a 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1816b 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1816c 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1816d 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1816e 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1816f 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
18170 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
18171 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18172 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18173 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
18175 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
18176 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 contains code us
18177 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20 e to manipulate
18178 22 4d 65 6d 22 20 73 74 72 75 63 74 75 72 65 2e "Mem" structure.
18179 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f A "Mem".** sto
1817a 72 65 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c res a single val
1817b 75 65 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 ue in the VDBE.
1817c 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 71 75 Mem is an opaqu
1817d 65 20 73 74 72 75 63 74 75 72 65 20 76 69 73 69 e structure visi
1817e 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 ble.** only with
1817f 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 49 6e in the VDBE. In
18180 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 terface routines
18181 20 72 65 66 65 72 20 74 6f 20 61 20 4d 65 6d 20 refer to a Mem
18182 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d using the.** nam
18183 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a e sqlite_value.*
18184 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 6d 65 *.** $Id: vdbeme
18185 6d 2e 63 2c 76 20 31 2e 31 33 39 20 32 30 30 39 m.c,v 1.139 2009
18186 2f 30 33 2f 32 39 20 31 35 3a 31 32 3a 31 30 20 /03/29 15:12:10
18187 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
18188 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 .** Call sqlite3
18189 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
1818a 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c b() on the suppl
1818b 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 ied value (type
1818c 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 Mem*).** P if re
1818d 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 quired..*/.#defi
1818e 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50 29 ne expandBlob(P)
1818f 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 (((P)->flags&ME
18190 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 M_Zero)?sqlite3V
18191 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
18192 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 (P):0)../*.** If
18193 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65 pMem is an obje
18194 63 74 20 77 69 74 68 20 61 20 76 61 6c 69 64 20 ct with a valid
18195 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 string represent
18196 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74 ation, this rout
18197 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74 ine.** ensures t
18198 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f he internal enco
18199 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74 72 ding for the str
1819a 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
1819b 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72 65 on is.** 'desire
1819c 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51 dEnc', one of SQ
1819d 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
1819e 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c E_UTF16LE or SQL
1819f 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a ITE_UTF16BE..**.
181a0 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f ** If pMem is no
181a1 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65 63 t a string objec
181a2 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 t, or the encodi
181a3 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 ng of the string
181a4 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 .** representati
181a5 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73 74 on is already st
181a6 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 ored using the r
181a7 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69 6e equested encodin
181a8 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 g, then this.**
181a9 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
181aa 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 op..**.** SQLITE
181ab 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
181ac 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f if the conversio
181ad 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20 n is successful
181ae 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64 (or not required
181af 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d )..** SQLITE_NOM
181b0 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e EM may be return
181b1 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 ed if a malloc()
181b2 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f fails during co
181b3 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77 nversion.** betw
181b4 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a een formats..*/.
181b5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
181b6 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 nt sqlite3VdbeCh
181b7 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d angeEncoding(Mem
181b8 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69 *pMem, int desi
181b9 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72 redEnc){. int r
181ba 63 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d c;. assert( (pM
181bb 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f em->flags&MEM_Ro
181bc 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 wSet)==0 );. as
181bd 73 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 sert( desiredEnc
181be 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c ==SQLITE_UTF8 ||
181bf 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
181c0 49 54 45 5f 55 54 46 31 36 4c 45 0a 20 20 20 20 ITE_UTF16LE.
181c1 20 20 20 20 20 20 20 7c 7c 20 64 65 73 69 72 65 || desire
181c2 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
181c3 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20 21 28 16BE );. if( !(
181c4 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
181c5 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e Str) || pMem->en
181c6 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 20 29 7b c==desiredEnc ){
181c7 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
181c8 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 TE_OK;. }. ass
181c9 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 ert( pMem->db==0
181ca 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
181cb 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d x_held(pMem->db-
181cc 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 64 65 >mutex) );.#ifde
181cd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
181ce 46 31 36 0a 20 20 72 65 74 75 72 6e 20 53 51 4c F16. return SQL
181cf 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 ITE_ERROR;.#else
181d0 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c .. /* MemTransl
181d1 61 74 65 28 29 20 6d 61 79 20 72 65 74 75 72 6e ate() may return
181d2 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 SQLITE_OK or SQ
181d3 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e LITE_NOMEM. If N
181d4 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
181d5 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 ,. ** then the
181d6 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 encoding of the
181d7 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 value may not ha
181d8 76 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f ve changed.. */
181d9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 . rc = sqlite3V
181da 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 dbeMemTranslate(
181db 70 4d 65 6d 2c 20 28 75 38 29 64 65 73 69 72 65 pMem, (u8)desire
181dc 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 dEnc);. assert(
181dd 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 rc==SQLITE_OK
181de 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e || rc==SQLITE_N
181df 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72 74 28 OMEM);. assert(
181e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 rc==SQLITE_OK
181e1 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 || pMem->enc!=d
181e2 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 esiredEnc);. as
181e3 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f sert(rc==SQLITE_
181e4 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65 NOMEM || pMem->e
181e5 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 29 3b nc==desiredEnc);
181e6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 . return rc;.#e
181e7 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 ndif.}../*.** Ma
181e8 6b 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 ke sure pMem->z
181e9 70 6f 69 6e 74 73 20 74 6f 20 61 20 77 72 69 74 points to a writ
181ea 61 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 able allocation
181eb 6f 66 20 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20 of at least .**
181ec 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 n bytes..**.** I
181ed 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c f the memory cel
181ee 6c 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 l currently cont
181ef 61 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 20 62 ains string or b
181f0 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20 lob data.** and
181f1 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 the third argume
181f2 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 nt passed to thi
181f3 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 s function is tr
181f4 75 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75 72 72 ue, the .** curr
181f5 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 ent content of t
181f6 68 65 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 he cell is prese
181f7 72 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c rved. Otherwise,
181f8 20 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20 64 69 it may.** be di
181f9 73 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a 2a 2a scarded. .**.**
181fa 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
181fb 65 74 73 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20 ets the MEM_Dyn
181fc 66 6c 61 67 20 61 6e 64 20 63 6c 65 61 72 73 20 flag and clears
181fd 61 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62 61 63 any xDel callbac
181fe 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c k..** It also cl
181ff 65 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61 ears MEM_Ephem a
18200 6e 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49 nd MEM_Static. I
18201 66 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66 f the preserve f
18202 6c 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73 lag is .** not s
18203 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72 et, Mem.n is zer
18204 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 oed..*/.SQLITE_P
18205 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18206 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 e3VdbeMemGrow(Me
18207 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 m *pMem, int n,
18208 69 6e 74 20 70 72 65 73 65 72 76 65 29 7b 0a 20 int preserve){.
18209 20 61 73 73 65 72 74 28 20 31 20 3e 3d 0a 20 20 assert( 1 >=.
1820a 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f ((pMem->zMallo
1820b 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c c && pMem->zMall
1820c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31 oc==pMem->z) ? 1
1820d 20 3a 20 30 29 20 2b 0a 20 20 20 20 28 28 28 70 : 0) +. (((p
1820e 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 Mem->flags&MEM_D
1820f 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29 yn)&&pMem->xDel)
18210 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 ? 1 : 0) + .
18211 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ((pMem->flags&M
18212 45 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20 3a 20 EM_Ephem) ? 1 :
18213 30 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 0) + . ((pMem
18214 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 ->flags&MEM_Stat
18215 69 63 29 20 3f 20 31 20 3a 20 30 29 0a 20 20 29 ic) ? 1 : 0). )
18216 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 ;. assert( (pMe
18217 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 m->flags&MEM_Row
18218 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 Set)==0 );.. if
18219 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b ( n<32 ) n = 32;
1821a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 44 62 . if( sqlite3Db
1821b 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d MallocSize(pMem-
1821c 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c >db, pMem->zMall
1821d 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28 oc)<n ){. if(
1821e 20 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 preserve && pMe
1821f 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c m->z==pMem->zMal
18220 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 loc ){. pMe
18221 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 m->z = pMem->zMa
18222 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 lloc = sqlite3Db
18223 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d ReallocOrFree(pM
18224 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c em->db, pMem->z,
18225 20 6e 29 3b 0a 20 20 20 20 20 20 70 72 65 73 65 n);. prese
18226 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c rve = 0;. }el
18227 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
18228 33 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 3DbFree(pMem->db
18229 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 , pMem->zMalloc)
1822a 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d ;. pMem->zM
1822b 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 alloc = sqlite3D
1822c 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d bMallocRaw(pMem-
1822d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 >db, n);. }.
1822e 20 7d 0a 0a 20 20 69 66 28 20 70 72 65 73 65 72 }.. if( preser
1822f 76 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 20 26 26 ve && pMem->z &&
18230 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 pMem->zMalloc &
18231 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d & pMem->z!=pMem-
18232 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 >zMalloc ){.
18233 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 memcpy(pMem->zMa
18234 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 lloc, pMem->z, p
18235 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 Mem->n);. }. i
18236 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d f( pMem->flags&M
18237 45 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e EM_Dyn && pMem->
18238 78 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d xDel ){. pMem
18239 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 ->xDel((void *)(
1823a 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a pMem->z));. }..
1823b 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d pMem->z = pMem
1823c 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 ->zMalloc;. if(
1823d 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 pMem->z==0 ){.
1823e 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1823f 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c MEM_Null;. }el
18240 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c se{. pMem->fl
18241 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 ags &= ~(MEM_Eph
18242 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a em|MEM_Static);.
18243 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c }. pMem->xDel
18244 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 = 0;. return (
18245 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 pMem->z ? SQLITE
18246 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d _OK : SQLITE_NOM
18247 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 EM);.}../*.** Ma
18248 6b 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d ke the given Mem
18249 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e object MEM_Dyn.
1824a 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
1824b 2c 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 , make it so.**
1824c 74 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 that any TEXT or
1824d 20 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 BLOB content is
1824e 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 stored in memor
1824f 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a y obtained from.
18250 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e ** malloc(). In
18251 20 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e this way, we kn
18252 6f 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f ow that the memo
18253 72 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 ry is safe to be
18254 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 .** overwritten
18255 6f 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a or altered..**.*
18256 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
18257 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 OK on success or
18258 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 SQLITE_NOMEM if
18259 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a malloc fails..*
1825a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1825b 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1825c 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 MemMakeWriteable
1825d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 (Mem *pMem){. i
1825e 6e 74 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 nt f;. assert(
1825f 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
18260 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
18261 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
18262 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
18263 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
18264 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
18265 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d expandBlob(pMem
18266 29 3b 0a 20 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 );. f = pMem->f
18267 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 66 26 28 lags;. if( (f&(
18268 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
18269 29 29 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 )) && pMem->z!=p
1826a 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a Mem->zMalloc ){.
1826b 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
1826c 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c dbeMemGrow(pMem,
1826d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29 pMem->n + 2, 1)
1826e 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1826f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
18270 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a }. pMem->z
18271 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 [pMem->n] = 0;.
18272 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d pMem->z[pMem-
18273 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 70 >n+1] = 0;. p
18274 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
18275 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 20 72 M_Term;. }.. r
18276 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
18277 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
18278 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61 73 20 given Mem* has
18279 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 a zero-filled ta
1827a 69 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e 74 6f il, turn it into
1827b 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a 2a 2a 20 an ordinary.**
1827c 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e 20 64 blob stored in d
1827d 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
1827e 61 74 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 23 ated space..*/.#
1827f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18280 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 IT_INCRBLOB.SQLI
18281 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18282 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
18283 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 andBlob(Mem *pMe
18284 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e m){. if( pMem->
18285 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f flags & MEM_Zero
18286 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 ){. int nByt
18287 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e;. assert( p
18288 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 Mem->flags&MEM_B
18289 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 lob );. asser
1828a 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
1828b 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
1828c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d ;. assert( pM
1828d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
1828e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1828f 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
18290 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 );.. /* Set
18291 6e 42 79 74 65 20 74 6f 20 74 68 65 20 6e 75 6d nByte to the num
18292 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 ber of bytes req
18293 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 uired to store t
18294 68 65 20 65 78 70 61 6e 64 65 64 20 62 6c 6f 62 he expanded blob
18295 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74 65 20 3d . */. nByte =
18296 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d pMem->n + pMem-
18297 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 69 66 >u.nZero;. if
18298 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b 0a 20 20 ( nByte<=0 ){.
18299 20 20 20 20 6e 42 79 74 65 20 3d 20 31 3b 0a 20 nByte = 1;.
1829a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
1829b 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1829c 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31 29 20 pMem, nByte, 1)
1829d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1829e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1829f 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 }.. memset(
182a0 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e &pMem->z[pMem->n
182a1 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 2e 6e 5a ], 0, pMem->u.nZ
182a2 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e ero);. pMem->
182a3 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 n += pMem->u.nZe
182a4 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ro;. pMem->fl
182a5 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 ags &= ~(MEM_Zer
182a6 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d o|MEM_Term);. }
182a7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
182a8 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a _OK;.}.#endif...
182a9 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
182aa 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 the given Mem is
182ab 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 \u0000 terminat
182ac 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
182ad 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
182ae 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 3VdbeMemNulTermi
182af 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b nate(Mem *pMem){
182b0 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
182b1 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
182b2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 3_mutex_held(pMe
182b3 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b m->db->mutex) );
182b4 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c . if( (pMem->fl
182b5 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 ags & MEM_Term)!
182b6 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 =0 || (pMem->fla
182b7 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 gs & MEM_Str)==0
182b8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
182b9 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e QLITE_OK; /* N
182ba 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a othing to do */.
182bb 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
182bc 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
182bd 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 m, pMem->n+2, 1)
182be 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
182bf 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
182c0 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d . pMem->z[pMem-
182c1 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d >n] = 0;. pMem-
182c2 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 >z[pMem->n+1] =
182c3 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 0;. pMem->flags
182c4 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 |= MEM_Term;.
182c5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
182c6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d ;.}../*.** Add M
182c7 45 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 EM_Str to the se
182c8 74 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 t of representat
182c9 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 ions for the giv
182ca 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 en Mem. Numbers
182cb 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 .** are converte
182cc 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f d using sqlite3_
182cd 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e snprintf(). Con
182ce 76 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 verting a BLOB t
182cf 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 o a string.** is
182d0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
182d1 45 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 Existing represe
182d2 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 ntations MEM_Int
182d3 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 and MEM_Real ar
182d4 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 e *not* invalida
182d5 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d ted..**.** A MEM
182d6 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c _Null value will
182d7 20 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64 never be passed
182d8 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
182d9 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e n. This function
182da 20 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 is.** used for
182db 63 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 converting value
182dc 73 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 s to text for re
182dd 74 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 turning to the u
182de 73 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a ser (i.e. via.**
182df 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
182e0 65 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 ext()), or for e
182e1 6e 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c nsuring that val
182e2 75 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 ues to be used a
182e3 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 s btree.** keys
182e4 61 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 are strings. In
182e5 74 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 the former case
182e6 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 a NULL pointer i
182e7 73 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a s returned the.*
182e8 2a 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c * user and the l
182e9 61 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 ater is an inter
182ea 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 nal programming
182eb 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 error..*/.SQLITE
182ec 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
182ed 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e ite3VdbeMemStrin
182ee 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 gify(Mem *pMem,
182ef 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 int enc){. int
182f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
182f1 20 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d int fg = pMem-
182f2 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 >flags;. const
182f3 69 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a int nByte = 32;.
182f4 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
182f5 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
182f6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 3_mutex_held(pMe
182f7 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b m->db->mutex) );
182f8 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 . assert( !(fg&
182f9 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 MEM_Zero) );. a
182fa 73 73 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d ssert( !(fg&(MEM
182fb 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 _Str|MEM_Blob))
182fc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 );. assert( fg&
182fd 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 (MEM_Int|MEM_Rea
182fe 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 l) );. assert(
182ff 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
18300 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a _RowSet)==0 );..
18301 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
18302 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 beMemGrow(pMem,
18303 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 nByte, 0) ){.
18304 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
18305 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 OMEM;. }.. /*
18306 46 6f 72 20 61 20 52 65 61 6c 20 6f 72 20 49 6e For a Real or In
18307 74 65 67 65 72 2c 20 75 73 65 20 73 71 6c 69 74 teger, use sqlit
18308 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20 e3_mprintf() to
18309 70 72 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d produce the UTF-
1830a 38 0a 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65 8. ** string re
1830b 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
1830c 74 68 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c the value. Then,
1830d 20 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 if the required
1830e 20 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 encoding. ** i
1830f 73 20 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54 s UTF-16le or UT
18310 46 2d 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e F-16be do a tran
18311 73 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 slation.. ** .
18312 20 2a 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77 ** FIX ME: It w
18313 6f 75 6c 64 20 62 65 20 62 65 74 74 65 72 20 69 ould be better i
18314 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e f sqlite3_snprin
18315 74 66 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54 tf() could do UT
18316 46 2d 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 F-16.. */. if(
18317 20 66 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b fg & MEM_Int ){
18318 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
18319 72 69 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 rintf(nByte, pMe
1831a 6d 2d 3e 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d m->z, "%lld", pM
1831b 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 em->u.i);. }els
1831c 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 66 e{. assert( f
1831d 67 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a g & MEM_Real );.
1831e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1831f 69 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d intf(nByte, pMem
18320 2d 3e 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 ->z, "%!.15g", p
18321 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 Mem->r);. }. p
18322 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 Mem->n = sqlite3
18323 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a Strlen30(pMem->z
18324 29 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d );. pMem->enc =
18325 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 SQLITE_UTF8;.
18326 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pMem->flags |= M
18327 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b EM_Str|MEM_Term;
18328 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
18329 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 angeEncoding(pMe
1832a 6d 2c 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 m, enc);. retur
1832b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
1832c 65 6d 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 emory cell pMem
1832d 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e contains the con
1832e 74 65 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 text of an aggre
1832f 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a gate function..*
18330 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
18331 61 6c 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a alls the finaliz
18332 65 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 e method for tha
18333 74 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 t function. The
18334 0a 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 .** result of th
18335 65 20 61 67 67 72 65 67 61 74 65 20 69 73 20 73 e aggregate is s
18336 74 6f 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 tored back into
18337 70 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 pMem..**.** Retu
18338 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 rn SQLITE_ERROR
18339 69 66 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 if the finalizer
1833a 20 72 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f reports an erro
1833b 72 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a r. SQLITE_OK.**
1833c 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 otherwise..*/.S
1833d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1833e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
1833f 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d Finalize(Mem *pM
18340 65 6d 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 em, FuncDef *pFu
18341 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 nc){. int rc =
18342 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 SQLITE_OK;. if(
18343 20 70 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d pFunc && pFunc-
18344 3e 78 46 69 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 >xFinalize ){.
18345 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
18346 74 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 t ctx;. asser
18347 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 t( (pMem->flags
18348 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c & MEM_Null)!=0 |
18349 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 | pFunc==pMem->u
1834a 2e 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 .pDef );. ass
1834b 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 ert( pMem->db==0
1834c 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
1834d 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d x_held(pMem->db-
1834e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d >mutex) );. m
1834f 65 6d 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 emset(&ctx, 0, s
18350 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 izeof(ctx));.
18351 20 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d ctx.s.flags = M
18352 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 EM_Null;. ctx
18353 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 .s.db = pMem->db
18354 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d ;. ctx.pMem =
18355 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 pMem;. ctx.p
18356 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 Func = pFunc;.
18357 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 pFunc->xFinali
18358 7a 65 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 ze(&ctx);. as
18359 73 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e sert( 0==(pMem->
1835a 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 flags&MEM_Dyn) &
1835b 26 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b & !pMem->xDel );
1835c 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1835d 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 ee(pMem->db, pMe
1835e 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 m->zMalloc);.
1835f 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 memcpy(pMem, &c
18360 74 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 tx.s, sizeof(ctx
18361 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 28 .s));. rc = (
18362 63 74 78 2e 69 73 45 72 72 6f 72 3f 53 51 4c 49 ctx.isError?SQLI
18363 54 45 5f 45 52 52 4f 52 3a 53 51 4c 49 54 45 5f TE_ERROR:SQLITE_
18364 4f 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 OK);. }. retur
18365 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 n rc;.}../*.** I
18366 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c f the memory cel
18367 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 74 72 l contains a str
18368 69 6e 67 20 76 61 6c 75 65 20 74 68 61 74 20 6d ing value that m
18369 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a ust be freed by.
1836a 2a 2a 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65 ** invoking an e
1836b 78 74 65 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b xternal callback
1836c 2c 20 66 72 65 65 20 69 74 20 6e 6f 77 2e 20 43 , free it now. C
1836d 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 alling this func
1836e 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 tion.** does not
1836f 20 66 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d free any Mem.zM
18370 61 6c 6c 6f 63 20 62 75 66 66 65 72 2e 0a 2a 2f alloc buffer..*/
18371 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18372 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
18373 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
18374 61 6c 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 al(Mem *p){. as
18375 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c sert( p->db==0 |
18376 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
18377 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
18378 78 29 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 x) );. if( p->f
18379 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a lags&MEM_Agg ){.
1837a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1837b 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 70 2d emFinalize(p, p-
1837c 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 61 73 >u.pDef);. as
1837d 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 20 sert( (p->flags
1837e 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 3b & MEM_Agg)==0 );
1837f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
18380 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 MemRelease(p);.
18381 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c }else if( p->fl
18382 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 ags&MEM_Dyn && p
18383 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 61 73 ->xDel ){. as
18384 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 sert( (p->flags&
18385 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
18386 3b 0a 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 ;. p->xDel((v
18387 6f 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 oid *)p->z);.
18388 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 p->xDel = 0;.
18389 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 }else if( p->fla
1838a 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b gs&MEM_RowSet ){
1838b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 . sqlite3RowS
1838c 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 52 6f etClear(p->u.pRo
1838d 77 53 65 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a wSet);. }.}../*
1838e 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 .** Release any
1838f 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 memory held by t
18390 68 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 he Mem. This may
18391 20 6c 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 leave the Mem i
18392 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 n an.** inconsis
18393 74 65 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 tent state, for
18394 65 78 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 example with (Me
18395 6d 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 m.z==0) and.** (
18396 4d 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 Mem.type==SQLITE
18397 5f 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 _TEXT)..*/.SQLIT
18398 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
18399 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1839a 65 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 ease(Mem *p){.
1839b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1839c 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 leaseExternal(p)
1839d 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1839e 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c e(p->db, p->zMal
1839f 6c 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 loc);. p->z = 0
183a0 3b 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d ;. p->zMalloc =
183a1 20 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 0;. p->xDel =
183a2 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 0;.}../*.** Conv
183a3 65 72 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 ert a 64-bit IEE
183a4 45 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 E double into a
183a5 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
183a6 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 teger..** If the
183a7 20 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c double is too l
183a8 61 72 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 arge, return 0x8
183a9 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 000000000000000.
183aa 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 .**.** Most syst
183ab 65 6d 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f ems appear to do
183ac 20 74 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 this simply by
183ad 61 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 assigning.** var
183ae 69 61 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f iables and witho
183af 75 74 20 74 68 65 20 65 78 74 72 61 20 72 61 6e ut the extra ran
183b0 67 65 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a ge tests. But.*
183b1 2a 20 74 68 65 72 65 20 61 72 65 20 72 65 70 6f * there are repo
183b2 72 74 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 rts that windows
183b3 20 74 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 throws an expec
183b4 74 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 tion.** if the f
183b5 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
183b6 6c 75 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 lue is out of ra
183b7 6e 67 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 nge. (See ticket
183b8 20 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 #2880.).** Beca
183b9 75 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f use we do not co
183ba 6d 70 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 mpletely underst
183bb 61 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c and the problem,
183bc 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 we will.** take
183bd 20 74 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 the conservativ
183be 65 20 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 e approach and a
183bf 6c 77 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 lways do range t
183c0 65 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 ests.** before a
183c1 74 74 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f ttempting the co
183c2 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 nversion..*/.sta
183c3 74 69 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f tic i64 doubleTo
183c4 49 6e 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b Int64(double r){
183c5 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 . /*. ** Many
183c6 63 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 compilers we enc
183c7 6f 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 ounter do not de
183c8 66 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 fine constants f
183c9 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 or the. ** mini
183ca 6d 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 mum and maximum
183cb 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 64-bit integers,
183cc 20 6f 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 or they define
183cd 74 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 them. ** incons
183ce 69 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d istently. And m
183cf 61 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 any do not under
183d0 73 74 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e stand the "LL" n
183d1 6f 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f otation.. ** So
183d2 20 77 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f we define our o
183d3 77 6e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 wn static consta
183d4 6e 74 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e nts here using n
183d5 6f 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 othing. ** larg
183d6 65 72 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 er than a 32-bit
183d7 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
183d8 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 t.. */. static
183d9 20 63 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e const i64 maxIn
183da 74 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 t = LARGEST_INT6
183db 34 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 4;. static cons
183dc 74 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 t i64 minInt = S
183dd 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a MALLEST_INT64;..
183de 20 20 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 if( r<(double)
183df 6d 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 minInt ){. re
183e0 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d turn minInt;. }
183e1 65 6c 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 else if( r>(doub
183e2 6c 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 le)maxInt ){.
183e3 20 2f 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f /* minInt is co
183e4 72 72 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 rrect here - not
183e5 20 6d 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 maxInt. It tur
183e6 6e 73 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 ns out that assi
183e7 67 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 gning. ** a v
183e8 65 72 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 ery large positi
183e9 76 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 ve number to an
183ea 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 integer results
183eb 69 6e 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a in a very large.
183ec 20 20 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 ** negative
183ed 69 6e 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d integer. This m
183ee 61 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 akes no sense, b
183ef 75 74 20 69 74 20 69 73 20 77 68 61 74 20 78 38 ut it is what x8
183f0 36 20 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 6 hardware. *
183f1 2a 20 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f * does so for co
183f2 6d 70 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 mpatibility we w
183f3 69 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 ill do the same
183f4 69 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a in software. */.
183f5 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e return minIn
183f6 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
183f7 72 65 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 return (i64)r;.
183f8 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
183f9 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 rn some kind of
183fa 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 integer value wh
183fb 69 63 68 20 69 73 20 74 68 65 20 62 65 73 74 20 ich is the best
183fc 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 we can do.** at
183fd 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 representing the
183fe 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 value that *pMe
183ff 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 20 61 m describes as a
18400 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 n integer..** If
18401 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 pMem is an inte
18402 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 ger, then the va
18403 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 lue is exact. I
18404 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 f pMem is.** a f
18405 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 loating-point th
18406 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 en the value ret
18407 75 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 urned is the int
18408 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 eger part..** If
18409 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e pMem is a strin
1840a 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 g or blob, then
1840b 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d we make an attem
1840c 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a pt to convert.**
1840d 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 it into a integ
1840e 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 er and return th
1840f 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 at. If pMem rep
18410 72 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e resents an.** an
18411 20 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c SQL-NULL value,
18412 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a return 0..**.**
18413 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 If pMem represe
18414 6e 74 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c nts a string val
18415 75 65 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 ue, its encoding
18416 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 might be change
18417 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
18418 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 VATE i64 sqlite3
18419 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d VdbeIntValue(Mem
1841a 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 *pMem){. int f
1841b 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 lags;. assert(
1841c 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
1841d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1841e 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
1841f 78 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20 x) );. flags =
18420 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 pMem->flags;. i
18421 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 f( flags & MEM_I
18422 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e nt ){. return
18423 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 pMem->u.i;. }e
18424 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 lse if( flags &
18425 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
18426 72 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 6f 49 return doubleToI
18427 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 20 nt64(pMem->r);.
18428 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 }else if( flags
18429 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f & (MEM_Str|MEM_
1842a 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 36 34 Blob) ){. i64
1842b 20 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 65 6d value;. pMem
1842c 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 ->flags |= MEM_S
1842d 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 tr;. if( sqli
1842e 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1842f 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 oding(pMem, SQLI
18430 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 TE_UTF8).
18431 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 || sqlite3VdbeMe
18432 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d mNulTerminate(pM
18433 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 em) ){. ret
18434 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 0;. }.
18435 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a assert( pMem->z
18436 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 );. sqlite3A
18437 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 20 26 toi64(pMem->z, &
18438 76 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 74 75 value);. retu
18439 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 6c 73 rn value;. }els
1843a 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b e{. return 0;
1843b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
1843c 74 75 72 6e 20 74 68 65 20 62 65 73 74 20 72 65 turn the best re
1843d 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
1843e 70 4d 65 6d 20 74 68 61 74 20 77 65 20 63 61 6e pMem that we can
1843f 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a 20 64 get into a.** d
18440 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 6d 20 ouble. If pMem
18441 69 73 20 61 6c 72 65 61 64 79 20 61 20 64 6f 75 is already a dou
18442 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 67 65 ble or an intege
18443 72 2c 20 72 65 74 75 72 6e 20 69 74 73 0a 2a 2a r, return its.**
18444 20 76 61 6c 75 65 2e 20 20 49 66 20 69 74 20 69 value. If it i
18445 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c s a string or bl
18446 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 ob, try to conve
18447 72 74 20 69 74 20 74 6f 20 61 20 64 6f 75 62 6c rt it to a doubl
18448 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 61 e..** If it is a
18449 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e NULL, return 0.
1844a 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 0..*/.SQLITE_PRI
1844b 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 VATE double sqli
1844c 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 te3VdbeRealValue
1844d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 (Mem *pMem){. a
1844e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
1844f 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
18450 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
18451 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 b->mutex) );. i
18452 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 f( pMem->flags &
18453 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 MEM_Real ){.
18454 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b return pMem->r;
18455 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 . }else if( pMe
18456 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 m->flags & MEM_I
18457 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e nt ){. return
18458 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 (double)pMem->u
18459 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 .i;. }else if(
1845a 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d pMem->flags & (M
1845b 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 EM_Str|MEM_Blob)
1845c 20 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 ){. /* (doub
1845d 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 le)0 In case of
1845e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
1845f 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f TING_POINT... */
18460 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 . double val
18461 3d 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 = (double)0;.
18462 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 pMem->flags |=
18463 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 MEM_Str;. if(
18464 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
18465 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c geEncoding(pMem,
18466 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 SQLITE_UTF8).
18467 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 || sqlite3V
18468 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
18469 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 te(pMem) ){.
1846a 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 /* (double)0 I
1846b 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 n case of SQLITE
1846c 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
1846d 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 OINT... */.
1846e 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 return (double)
1846f 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 0;. }. ass
18470 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a ert( pMem->z );.
18471 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 sqlite3AtoF(
18472 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a pMem->z, &val);.
18473 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
18474 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
18475 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 (double)0 In cas
18476 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 e of SQLITE_OMIT
18477 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e _FLOATING_POINT.
18478 2e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e .. */. return
18479 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a (double)0;. }.
1847a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d }../*.** The MEM
1847b 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c structure is al
1847c 72 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c ready a MEM_Real
1847d 2e 20 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d . Try to also m
1847e 61 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f ake it a.** MEM_
1847f 49 6e 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a Int if we can..*
18480 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18481 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
18482 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 eIntegerAffinity
18483 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 (Mem *pMem){. a
18484 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 ssert( pMem->fla
18485 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b gs & MEM_Real );
18486 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
18487 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
18488 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 wSet)==0 );. as
18489 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
1848a 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
1848b 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
1848c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 ->mutex) );.. p
1848d 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 62 6c Mem->u.i = doubl
1848e 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 eToInt64(pMem->r
1848f 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 );. if( pMem->r
18490 3d 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e ==(double)pMem->
18491 75 2e 69 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d u.i ){. pMem-
18492 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e >flags |= MEM_In
18493 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 t;. }.}../*.**
18494 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20 Convert pMem to
18495 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49 type integer. I
18496 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 nvalidate any pr
18497 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ior representati
18498 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ons..*/.SQLITE_P
18499 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1849a 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1849b 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ify(Mem *pMem){.
1849c 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1849d 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
1849e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
1849f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
184a0 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
184a1 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
184a2 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 70 4d 65 Set)==0 );. pMe
184a3 6d 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65 33 m->u.i = sqlite3
184a4 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 4d 65 VdbeIntValue(pMe
184a5 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 m);. MemSetType
184a6 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 Flag(pMem, MEM_I
184a7 6e 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 nt);. return SQ
184a8 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
184a9 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 * Convert pMem s
184aa 6f 20 74 68 61 74 20 69 74 20 69 73 20 6f 66 20 o that it is of
184ab 74 79 70 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a type MEM_Real..*
184ac 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 * Invalidate any
184ad 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 prior represent
184ae 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ations..*/.SQLIT
184af 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
184b0 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c lite3VdbeMemReal
184b1 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ify(Mem *pMem){.
184b2 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
184b3 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
184b4 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
184b5 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
184b6 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 pMem->r = sqli
184b7 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 te3VdbeRealValue
184b8 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 (pMem);. MemSet
184b9 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d TypeFlag(pMem, M
184ba 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 75 EM_Real);. retu
184bb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
184bc 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 ./*.** Convert p
184bd 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 68 Mem so that it h
184be 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 61 as types MEM_Rea
184bf 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 20 l or MEM_Int or
184c0 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 both..** Invalid
184c1 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 ate any prior re
184c2 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a presentations..*
184c3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
184c4 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
184c5 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 20 MemNumerify(Mem
184c6 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c 65 *pMem){. double
184c7 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 20 69 r1, r2;. i64 i
184c8 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 ;. assert( (pMe
184c9 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f m->flags & (MEM_
184ca 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d Int|MEM_Real|MEM
184cb 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20 20 _Null))==0 );.
184cc 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
184cd 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 lags & (MEM_Blob
184ce 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 29 3b |MEM_Str))!=0 );
184cf 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
184d0 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
184d1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 3_mutex_held(pMe
184d2 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b m->db->mutex) );
184d3 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 56 . r1 = sqlite3V
184d4 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d 65 dbeRealValue(pMe
184d5 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c 65 m);. i = double
184d6 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 20 72 ToInt64(r1);. r
184d7 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a 20 2 = (double)i;.
184d8 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a 20 if( r1==r2 ){.
184d9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
184da 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d mIntegerify(pMem
184db 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
184dc 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20 20 pMem->r = r1;.
184dd 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
184de 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 (pMem, MEM_Real)
184df 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
184e0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
184e1 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 ** Delete any pr
184e2 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 evious value and
184e3 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 set the value s
184e4 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 tored in *pMem t
184e5 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 o NULL..*/.SQLIT
184e6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
184e7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
184e8 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b Null(Mem *pMem){
184e9 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 . if( pMem->fla
184ea 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 gs & MEM_RowSet
184eb 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f ){. sqlite3Ro
184ec 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e wSetClear(pMem->
184ed 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 7d 0a u.pRowSet);. }.
184ee 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
184ef 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 (pMem, MEM_Null)
184f0 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d ;. pMem->type =
184f1 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d 0a SQLITE_NULL;.}.
184f2 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e ./*.** Delete an
184f3 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 y previous value
184f4 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c and set the val
184f5 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 ue to be a BLOB
184f6 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20 63 of length.** n c
184f7 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 ontaining all ze
184f8 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ros..*/.SQLITE_P
184f9 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
184fa 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 te3VdbeMemSetZer
184fb 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 2c oBlob(Mem *pMem,
184fc 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 int n){. sqlit
184fd 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
184fe 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e (pMem);. pMem->
184ff 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 flags = MEM_Blob
18500 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d 65 |MEM_Zero;. pMe
18501 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
18502 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e 6e _BLOB;. pMem->n
18503 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30 20 = 0;. if( n<0
18504 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d ) n = 0;. pMem-
18505 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a 20 20 >u.nZero = n;.
18506 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
18507 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a 0a 2a TE_UTF8;.}../*.*
18508 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 * Delete any pre
18509 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 vious value and
1850a 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 set the value st
1850b 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f ored in *pMem to
1850c 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 val,.** manifes
1850d 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e 0a t type INTEGER..
1850e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1850f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
18510 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 beMemSetInt64(Me
18511 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 6c m *pMem, i64 val
18512 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ){. sqlite3Vdbe
18513 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
18514 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 ;. pMem->u.i =
18515 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 val;. pMem->fla
18516 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
18517 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
18518 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a 0a ITE_INTEGER;.}..
18519 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 /*.** Delete any
1851a 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
1851b 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 and set the valu
1851c 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 e stored in *pMe
1851d 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e m to val,.** man
1851e 69 66 65 73 74 20 74 79 70 65 20 52 45 41 4c 2e ifest type REAL.
1851f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18520 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
18521 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 dbeMemSetDouble(
18522 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62 6c Mem *pMem, doubl
18523 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 73 71 e val){. if( sq
18524 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 29 20 lite3IsNaN(val)
18525 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
18526 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 beMemSetNull(pMe
18527 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 m);. }else{.
18528 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
18529 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
1852a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c 3b pMem->r = val;
1852b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
1852c 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 = MEM_Real;.
1852d 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
1852e 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a LITE_FLOAT;. }.
1852f 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
18530 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c any previous val
18531 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 ue and set the v
18532 61 6c 75 65 20 6f 66 20 70 4d 65 6d 20 74 6f 20 alue of pMem to
18533 62 65 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 20 62 be an.** empty b
18534 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 2e 0a 2a 2f oolean index..*/
18535 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18536 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
18537 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 6d MemSetRowSet(Mem
18538 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 71 6c 69 74 *pMem){. sqlit
18539 65 33 20 2a 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 e3 *db = pMem->d
1853a 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 b;. assert( db!
1853b 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d =0 );. if( pMem
1853c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
1853d 77 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 wSet ){. sqli
1853e 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 te3RowSetClear(p
1853f 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b Mem->u.pRowSet);
18540 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
18541 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
18542 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 ase(pMem);. p
18543 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 Mem->zMalloc = s
18544 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
18545 77 28 64 62 2c 20 36 34 29 3b 0a 20 20 7d 0a 20 w(db, 64);. }.
18546 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
18547 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 4d 65 ailed ){. pMe
18548 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e m->flags = MEM_N
18549 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ull;. }else{.
1854a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1854b 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 70 zMalloc );. p
1854c 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 20 3d Mem->u.pRowSet =
1854d 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e sqlite3RowSetIn
1854e 69 74 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 it(db, pMem->zMa
1854f 6c 6c 6f 63 2c 20 0a 20 20 20 20 20 20 20 20 20 lloc, .
18550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
18552 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
18553 65 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c e(db, pMem->zMal
18554 6c 6f 63 29 29 3b 0a 20 20 20 20 61 73 73 65 72 loc));. asser
18555 74 28 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 t( pMem->u.pRowS
18556 65 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 4d 65 et!=0 );. pMe
18557 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 m->flags = MEM_R
18558 6f 77 53 65 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a owSet;. }.}../*
18559 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
1855a 69 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a 65 63 if the Mem objec
1855b 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 54 45 58 t contains a TEX
1855c 54 20 6f 72 20 42 4c 4f 42 20 74 68 61 74 20 69 T or BLOB that i
1855d 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65 20 2d s.** too large -
1855e 20 77 68 6f 73 65 20 73 69 7a 65 20 65 78 63 65 whose size exce
1855f 65 64 73 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c eds SQLITE_MAX_L
18560 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ENGTH..*/.SQLITE
18561 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
18562 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 ite3VdbeMemTooBi
18563 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73 g(Mem *p){. ass
18564 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b ert( p->db!=0 );
18565 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 . if( p->flags
18566 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 & (MEM_Str|MEM_B
18567 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 lob) ){. int
18568 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 20 20 69 66 n = p->n;. if
18569 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
1856a 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 6e _Zero ){. n
1856b 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a += p->u.nZero;.
1856c 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
1856d 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 n>p->db->aLimit
1856e 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
1856f 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 NGTH];. }. ret
18570 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a urn 0; .}../*.**
18571 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63 74 20 Size of struct
18572 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e Mem not includin
18573 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f g the Mem.zMallo
18574 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 c member..*/.#de
18575 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 fine MEMCELLSIZE
18576 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28 4d 65 (size_t)(&(((Me
18577 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 29 m *)0)->zMalloc)
18578 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e )../*.** Make an
18579 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 shallow copy of
1857a 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e pFrom into pTo.
1857b 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 Prior contents
1857c 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20 66 of.** pTo are f
1857d 72 65 65 64 2e 20 20 54 68 65 20 70 46 72 6f 6d reed. The pFrom
1857e 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 ->z field is not
1857f 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20 49 66 duplicated. If
18580 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 20 .** pFrom->z is
18581 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d 3e used, then pTo->
18582 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 z points to the
18583 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 70 46 same thing as pF
18584 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66 6c rom->z.** and fl
18585 61 67 73 20 67 65 74 73 20 73 72 63 54 79 70 65 ags gets srcType
18586 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 70 68 (either MEM_Eph
18587 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 em or MEM_Static
18588 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
18589 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1858a 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 3VdbeMemShallowC
1858b 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f opy(Mem *pTo, co
1858c 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c 20 nst Mem *pFrom,
1858d 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a 20 20 int srcType){.
1858e 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e assert( (pFrom->
1858f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
18590 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 et)==0 );. sqli
18591 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
18592 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a eExternal(pTo);.
18593 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 memcpy(pTo, pF
18594 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 rom, MEMCELLSIZE
18595 29 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c 20 3d );. pTo->xDel =
18596 20 30 3b 0a 20 20 69 66 28 20 28 70 46 72 6f 6d 0;. if( (pFrom
18597 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 ->flags&MEM_Dyn)
18598 21 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 7a 3d !=0 || pFrom->z=
18599 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 =pFrom->zMalloc
1859a 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 ){. pTo->flag
1859b 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e 7c 4d s &= ~(MEM_Dyn|M
1859c 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 EM_Static|MEM_Ep
1859d 68 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 hem);. assert
1859e 28 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 45 ( srcType==MEM_E
1859f 70 68 65 6d 20 7c 7c 20 73 72 63 54 79 70 65 3d phem || srcType=
185a0 3d 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b 0a 20 =MEM_Static );.
185a1 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d pTo->flags |=
185a2 20 73 72 63 54 79 70 65 3b 0a 20 20 7d 0a 7d 0a srcType;. }.}.
185a3 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 66 75 ./*.** Make a fu
185a4 6c 6c 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d ll copy of pFrom
185a5 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f into pTo. Prio
185a6 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 54 r contents of pT
185a7 6f 20 61 72 65 0a 2a 2a 20 66 72 65 65 64 20 62 o are.** freed b
185a8 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79 20 69 efore the copy i
185a9 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 s made..*/.SQLIT
185aa 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
185ab 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 lite3VdbeMemCopy
185ac 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 (Mem *pTo, const
185ad 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 Mem *pFrom){.
185ae 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
185af 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 28 OK;.. assert( (
185b0 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pFrom->flags & M
185b1 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
185b2 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
185b3 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c mReleaseExternal
185b4 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 (pTo);. memcpy(
185b5 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 pTo, pFrom, MEMC
185b6 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d ELLSIZE);. pTo-
185b7 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 >flags &= ~MEM_D
185b8 79 6e 3b 0a 0a 20 20 69 66 28 20 70 54 6f 2d 3e yn;.. if( pTo->
185b9 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d flags&(MEM_Str|M
185ba 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 EM_Blob) ){.
185bb 69 66 28 20 30 3d 3d 28 70 46 72 6f 6d 2d 3e 66 if( 0==(pFrom->f
185bc 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 29 lags&MEM_Static)
185bd 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 2d 3e 66 ){. pTo->f
185be 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 lags |= MEM_Ephe
185bf 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 m;. rc = sq
185c0 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 lite3VdbeMemMake
185c1 57 72 69 74 65 61 62 6c 65 28 70 54 6f 29 3b 0a Writeable(pTo);.
185c2 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
185c3 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
185c4 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 63 6f Transfer the co
185c5 6e 74 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 ntents of pFrom
185c6 74 6f 20 70 54 6f 2e 20 41 6e 79 20 65 78 69 73 to pTo. Any exis
185c7 74 69 6e 67 20 76 61 6c 75 65 20 69 6e 20 70 54 ting value in pT
185c8 6f 20 69 73 0a 2a 2a 20 66 72 65 65 64 2e 20 49 o is.** freed. I
185c9 66 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 f pFrom contains
185ca 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 2c ephemeral data,
185cb 20 61 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e a copy is made.
185cc 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d 20 63 6f 6e .**.** pFrom con
185cd 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c tains an SQL NUL
185ce 4c 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 L when this rout
185cf 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a ine returns..*/.
185d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
185d1 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
185d2 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a 70 54 6f 2c emMove(Mem *pTo,
185d3 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 Mem *pFrom){.
185d4 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 64 assert( pFrom->d
185d5 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
185d6 6d 75 74 65 78 5f 68 65 6c 64 28 70 46 72 6f 6d mutex_held(pFrom
185d7 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
185d8 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 64 assert( pTo->d
185d9 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
185da 6d 75 74 65 78 5f 68 65 6c 64 28 70 54 6f 2d 3e mutex_held(pTo->
185db 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
185dc 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 64 assert( pFrom->d
185dd 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d 3e 64 62 3d b==0 || pTo->db=
185de 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 64 62 3d =0 || pFrom->db=
185df 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 =pTo->db );.. s
185e0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
185e1 65 61 73 65 28 70 54 6f 29 3b 0a 20 20 6d 65 6d ease(pTo);. mem
185e2 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 cpy(pTo, pFrom,
185e3 73 69 7a 65 6f 66 28 4d 65 6d 29 29 3b 0a 20 20 sizeof(Mem));.
185e4 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pFrom->flags = M
185e5 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70 46 72 6f 6d EM_Null;. pFrom
185e6 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 70 46 ->xDel = 0;. pF
185e7 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 rom->zMalloc = 0
185e8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
185e9 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 e the value of a
185ea 20 4d 65 6d 20 74 6f 20 62 65 20 61 20 73 74 72 Mem to be a str
185eb 69 6e 67 20 6f 72 20 61 20 42 4c 4f 42 2e 0a 2a ing or a BLOB..*
185ec 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 *.** The memory
185ed 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 61 74 management strat
185ee 65 67 79 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 egy depends on t
185ef 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
185f0 78 44 65 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 xDel.** paramete
185f1 72 2e 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 r. If the value
185f2 70 61 73 73 65 64 20 69 73 20 53 51 4c 49 54 45 passed is SQLITE
185f3 5f 54 52 41 4e 53 49 45 4e 54 2c 20 74 68 65 6e _TRANSIENT, then
185f4 20 74 68 65 20 0a 2a 2a 20 73 74 72 69 6e 67 20 the .** string
185f5 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 61 is copied into a
185f6 20 28 70 6f 73 73 69 62 6c 79 20 65 78 69 73 74 (possibly exist
185f7 69 6e 67 29 20 62 75 66 66 65 72 20 6d 61 6e 61 ing) buffer mana
185f8 67 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 4d ged by the .** M
185f9 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 4f 74 em structure. Ot
185fa 68 65 72 77 69 73 65 2c 20 61 6e 79 20 65 78 69 herwise, any exi
185fb 73 74 69 6e 67 20 62 75 66 66 65 72 20 69 73 20 sting buffer is
185fc 66 72 65 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a freed and the.**
185fd 20 70 6f 69 6e 74 65 72 20 63 6f 70 69 65 64 2e pointer copied.
185fe 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
185ff 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
18600 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 20 4d beMemSetStr(. M
18601 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 20 20 em *pMem,
18602 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c /* Memory cel
18603 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 72 69 l to set to stri
18604 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 6f ng value */. co
18605 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20 20 nst char *z,
18606 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69 6e /* String poin
18607 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 ter */. int n,
18608 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18609 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67 2c Bytes in string,
1860a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a or negative */.
1860b 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 u8 enc,
1860c 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e /* Encodin
1860d 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 g of z. 0 for B
1860e 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 LOBs */. void (
1860f 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f 2a *xDel)(void*) /*
18610 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 Destructor func
18611 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 tion */.){. int
18612 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20 20 nByte = n;
18613 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66 6f /* New value fo
18614 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 20 69 r pMem->n */. i
18615 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20 20 nt iLimit;
18616 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c /* Maximum al
18617 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72 20 lowed string or
18618 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 20 75 blob size */. u
18619 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 20 20 16 flags = 0;
1861a 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 /* New value
1861b 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 for pMem->flags
1861c 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d */.. assert( pM
1861d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
1861e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1861f 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
18620 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
18621 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
18622 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a _RowSet)==0 );..
18623 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 4e /* If z is a N
18624 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 74 ULL pointer, set
18625 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 6e pMem to contain
18626 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a 2f an SQL NULL. */
18627 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 . if( !z ){.
18628 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
18629 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 etNull(pMem);.
1862a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1862b 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 OK;. }.. if( p
1862c 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 69 Mem->db ){. i
1862d 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 62 Limit = pMem->db
1862e 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1862f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 LIMIT_LENGTH];.
18630 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d }else{. iLim
18631 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f it = SQLITE_MAX_
18632 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 6c LENGTH;. }. fl
18633 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d 45 ags = (enc==0?ME
18634 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 3b M_Blob:MEM_Str);
18635 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20 29 . if( nByte<0 )
18636 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 6e {. assert( en
18637 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 c!=0 );. if(
18638 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 enc==SQLITE_UTF8
18639 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 42 ){. for(nB
1863a 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 4c yte=0; nByte<=iL
1863b 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 5d imit && z[nByte]
1863c 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 20 ; nByte++){}.
1863d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f }else{. fo
1863e 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 r(nByte=0; nByte
1863f 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b 6e <=iLimit && (z[n
18640 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 2b Byte] | z[nByte+
18641 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b 7d 1]); nByte+=2){}
18642 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 73 . }. flags
18643 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 |= MEM_Term;.
18644 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c }.. /* The foll
18645 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 73 owing block sets
18646 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 20 the new values
18647 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 6d of Mem.z and Mem
18648 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 61 .xDel. It. ** a
18649 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67 20 lso sets a flag
1864a 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c in local variabl
1864b 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e 64 e "flags" to ind
1864c 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 icate the memory
1864d 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e 74 . ** management
1864e 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 6e (one of MEM_Dyn
1864f 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e or MEM_Static).
18650 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 6c . */. if( xDel
18651 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 ==SQLITE_TRANSIE
18652 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 NT ){. int nA
18653 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 20 lloc = nByte;.
18654 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f if( flags&MEM_
18655 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e 41 Term ){. nA
18656 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 51 lloc += (enc==SQ
18657 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a LITE_UTF8?1:2);.
18658 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 42 }. if( nB
18659 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 yte>iLimit ){.
1865a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1865b 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d 0a E_TOOBIG;. }.
1865c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
1865d 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c dbeMemGrow(pMem,
1865e 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a 20 nAlloc, 0) ){.
1865f 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
18660 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
18661 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d memcpy(pMem-
18662 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b 0a >z, z, nAlloc);.
18663 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65 6c }else if( xDel
18664 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 ==SQLITE_DYNAMIC
18665 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
18666 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d dbeMemRelease(pM
18667 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a em);. pMem->z
18668 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a Malloc = pMem->z
18669 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 = (char *)z;.
1866a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 pMem->xDel = 0
1866b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
1866c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1866d 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 ease(pMem);.
1866e 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 pMem->z = (char
1866f 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 *)z;. pMem->x
18670 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 Del = xDel;.
18671 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c 3d flags |= ((xDel=
18672 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3f =SQLITE_STATIC)?
18673 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f 44 MEM_Static:MEM_D
18674 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e yn);. }. if( n
18675 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 Byte>iLimit ){.
18676 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
18677 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 _TOOBIG;. }..
18678 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b pMem->n = nByte;
18679 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d . pMem->flags =
1867a 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e flags;. pMem->
1867b 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 enc = (enc==0 ?
1867c 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e SQLITE_UTF8 : en
1867d 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 c);. pMem->type
1867e 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c = (enc==0 ? SQL
1867f 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 ITE_BLOB : SQLIT
18680 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 E_TEXT);..#ifnde
18681 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
18682 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e F16. if( pMem->
18683 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 enc!=SQLITE_UTF8
18684 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d && sqlite3VdbeM
18685 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d emHandleBom(pMem
18686 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
18687 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
18688 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 }.#endif.. retu
18689 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1868a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 ./*.** Compare t
1868b 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 61 69 he values contai
1868c 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f 20 6d ned by the two m
1868d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 65 74 emory cells, ret
1868e 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 74 69 urning.** negati
1868f 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 ve, zero or posi
18690 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20 69 73 tive if pMem1 is
18691 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 less than, equa
18692 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 l to, or greater
18693 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e 20 .** than pMem2.
18694 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 69 73 Sorting order is
18695 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c 20 66 NULL's first, f
18696 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62 65 ollowed by numbe
18697 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a 2a 20 rs (integers.**
18698 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 74 65 and reals) sorte
18699 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20 66 d numerically, f
1869a 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 74 20 ollowed by text
1869b 6f 72 64 65 72 65 64 20 62 79 20 74 68 65 20 63 ordered by the c
1869c 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 ollating.** sequ
1869d 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20 66 ence pColl and f
1869e 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f 72 inally blob's or
1869f 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d 70 28 dered by memcmp(
186a0 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e 55 4c )..**.** Two NUL
186a1 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e L values are con
186a2 73 69 64 65 72 65 64 20 65 71 75 61 6c 20 62 79 sidered equal by
186a3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
186a4 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
186a5 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d E int sqlite3Mem
186a6 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 Compare(const Me
186a7 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 74 20 m *pMem1, const
186a8 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f 6e 73 Mem *pMem2, cons
186a9 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c t CollSeq *pColl
186aa 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 ){. int rc;. i
186ab 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 6e 74 nt f1, f2;. int
186ac 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 3b combined_flags;
186ad 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68 61 6e .. /* Interchan
186ae 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70 4d 65 ge pMem1 and pMe
186af 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 m2 if the collat
186b0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70 65 ing sequence spe
186b1 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45 53 43 cifies. ** DESC
186b2 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 order.. */. f
186b3 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67 73 1 = pMem1->flags
186b4 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d 3e ;. f2 = pMem2->
186b5 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69 6e 65 flags;. combine
186b6 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 32 3b d_flags = f1|f2;
186b7 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f 6d 62 . assert( (comb
186b8 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d 45 4d ined_flags & MEM
186b9 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
186ba 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c . /* If one val
186bb 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20 69 ue is NULL, it i
186bc 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 s less than the
186bd 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 20 76 other. If both v
186be 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 20 4e alues. ** are N
186bf 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a 20 ULL, return 0..
186c0 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e */. if( combin
186c1 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c ed_flags&MEM_Nul
186c2 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 l ){. return
186c3 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 2d 20 (f2&MEM_Null) -
186c4 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 (f1&MEM_Null);.
186c5 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 }.. /* If one
186c6 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d 62 65 value is a numbe
186c7 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 r and the other
186c8 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 6d 62 is not, the numb
186c9 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a er is less.. **
186ca 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e 75 6d If both are num
186cb 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 61 73 bers, compare as
186cc 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 69 73 reals if one is
186cd 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 20 69 a real, or as i
186ce 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 66 20 ntegers. ** if
186cf 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 20 both values are
186d0 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 integers.. */.
186d1 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c if( combined_fl
186d2 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d ags&(MEM_Int|MEM
186d3 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 69 66 _Real) ){. if
186d4 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74 7c ( !(f1&(MEM_Int|
186d5 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20 MEM_Real)) ){.
186d6 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
186d7 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 66 32 }. if( !(f2
186d8 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 &(MEM_Int|MEM_Re
186d9 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 al)) ){. re
186da 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 turn -1;. }.
186db 20 20 20 69 66 28 20 28 66 31 20 26 20 66 32 20 if( (f1 & f2
186dc 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b & MEM_Int)==0 ){
186dd 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 31 . double r1
186de 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 28 20 , r2;. if(
186df 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 (f1&MEM_Real)==0
186e0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d ){. r1 =
186e1 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 31 2d 3e (double)pMem1->
186e2 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 u.i;. }else
186e3 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 70 {. r1 = p
186e4 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20 20 7d Mem1->r;. }
186e5 0a 20 20 20 20 20 20 69 66 28 20 28 66 32 26 4d . if( (f2&M
186e6 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 EM_Real)==0 ){.
186e7 20 20 20 20 20 20 20 72 32 20 3d 20 28 64 6f 75 r2 = (dou
186e8 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 2e 69 3b 0a ble)pMem2->u.i;.
186e9 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
186ea 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32 2d r2 = pMem2-
186eb 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 >r;. }.
186ec 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72 65 if( r1<r2 ) re
186ed 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 turn -1;. i
186ee 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 75 72 f( r1>r2 ) retur
186ef 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 n 1;. retur
186f0 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a n 0;. }else{.
186f1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 31 assert( f1
186f2 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 &MEM_Int );.
186f3 20 20 61 73 73 65 72 74 28 20 66 32 26 4d 45 4d assert( f2&MEM
186f4 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 69 66 _Int );. if
186f5 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c 20 70 ( pMem1->u.i < p
186f6 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75 Mem2->u.i ) retu
186f7 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 66 28 rn -1;. if(
186f8 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 70 4d pMem1->u.i > pM
186f9 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75 72 em2->u.i ) retur
186fa 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 n 1;. retur
186fb 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a n 0;. }. }..
186fc 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 /* If one valu
186fd 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 61 6e e is a string an
186fe 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20 61 d the other is a
186ff 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 69 6e blob, the strin
18700 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a 20 g is less.. **
18701 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 72 69 If both are stri
18702 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 73 69 ngs, compare usi
18703 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 ng the collating
18704 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f functions.. */
18705 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f . if( combined_
18706 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b flags&MEM_Str ){
18707 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 4d . if( (f1 & M
18708 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 EM_Str)==0 ){.
18709 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
1870a 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 32 20 }. if( (f2
1870b 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b & MEM_Str)==0 ){
1870c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 . return -1
1870d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 ;. }.. ass
1870e 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d ert( pMem1->enc=
1870f 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b 0a 20 =pMem2->enc );.
18710 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31 assert( pMem1
18711 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 ->enc==SQLITE_UT
18712 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 F8 || .
18713 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 pMem1->enc==S
18714 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c QLITE_UTF16LE ||
18715 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c pMem1->enc==SQL
18716 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 0a ITE_UTF16BE );..
18717 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 /* The colla
18718 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6d 75 tion sequence mu
18719 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 74 st be defined at
1871a 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 76 65 this point, eve
1871b 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 n if. ** the
1871c 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 68 65 user deletes the
1871d 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
1871e 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 76 64 nce after the vd
1871f 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a 20 20 be program is.
18720 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28 74 ** compiled (t
18721 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c 77 61 his was not alwa
18722 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a 20 20 ys the case)..
18723 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
18724 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c 6c !pColl || pColl
18725 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 20 69 ->xCmp );.. i
18726 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 f( pColl ){.
18727 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 if( pMem1->enc
18728 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a ==pColl->enc ){.
18729 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 /* The s
1872a 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 65 61 trings are alrea
1872b 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 dy in the correc
1872c 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 61 6c t encoding. Cal
1872d 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a l the. **
1872e 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 comparison func
1872f 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 2a 2f tion directly */
18730 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
18731 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c pColl->xCmp(pCol
18732 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 2d 3e l->pUser,pMem1->
18733 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 6d 32 n,pMem1->z,pMem2
18734 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b 0a 20 ->n,pMem2->z);.
18735 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
18736 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a const void *
18737 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 20 20 v1, *v2;.
18738 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 20 20 int n1, n2;.
18739 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 20 20 Mem c1;.
1873a 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 20 20 Mem c2;.
1873b 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 31 2c memset(&c1,
1873c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 29 3b 0, sizeof(c1));
1873d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 . memset(
1873e 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 &c2, 0, sizeof(c
1873f 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 2));. sql
18740 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c ite3VdbeMemShall
18741 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d 65 6d owCopy(&c1, pMem
18742 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 1, MEM_Ephem);.
18743 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
18744 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
18745 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45 4d (&c2, pMem2, MEM
18746 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 _Ephem);.
18747 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61 6c v1 = sqlite3Val
18748 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f ueText((sqlite3_
18749 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f 6c value*)&c1, pCol
1874a 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 l->enc);.
1874b 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30 20 n1 = v1==0 ? 0
1874c 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 20 20 : c1.n;.
1874d 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 v2 = sqlite3Valu
1874e 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f 76 eText((sqlite3_v
1874f 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f 6c 6c alue*)&c2, pColl
18750 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 ->enc);.
18751 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20 3a n2 = v2==0 ? 0 :
18752 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 20 72 c2.n;. r
18753 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 c = pColl->xCmp(
18754 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e 31 pColl->pUser, n1
18755 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a 20 , v1, n2, v2);.
18756 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
18757 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 31 beMemRelease(&c1
18758 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
18759 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1875a 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 20 72 (&c2);. r
1875b 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
1875c 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 49 }. }. /* I
1875d 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 f a NULL pointer
1875e 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 was passed as t
1875f 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63 74 he collate funct
18760 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 ion, fall throug
18761 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 h. ** to the
18762 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75 73 blob case and us
18763 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a e memcmp(). */.
18764 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68 20 }. . /* Both
18765 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 62 values must be b
18766 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20 75 lobs. Compare u
18767 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 20 sing memcmp().
18768 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70 */. rc = memcmp
18769 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d 32 (pMem1->z, pMem2
1876a 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e 70 ->z, (pMem1->n>p
1876b 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d 3e Mem2->n)?pMem2->
1876c 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20 69 n:pMem1->n);. i
1876d 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 f( rc==0 ){.
1876e 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d 20 rc = pMem1->n -
1876f 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 20 pMem2->n;. }.
18770 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
18771 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f 75 .** Move data ou
18772 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65 79 t of a btree key
18773 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20 61 or data field a
18774 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73 74 nd into a Mem st
18775 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 ructure..** The
18776 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20 74 data or key is t
18777 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e aken from the en
18778 74 72 79 20 74 68 61 74 20 70 43 75 72 20 69 73 try that pCur is
18779 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1877a 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66 73 ing.** to. offs
1877b 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65 72 et and amt deter
1877c 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69 6f mine what portio
1877d 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f 72 n of the data or
1877e 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76 65 key to retrieve
1877f 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75 65 ..** key is true
18780 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79 20 to get the key
18781 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74 20 or false to get
18782 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 75 6c data. The resul
18783 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 t is written.**
18784 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65 6c into the pMem el
18785 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ement..**.** The
18786 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 pMem structure
18787 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 is assumed to be
18788 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 uninitialized.
18789 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74 65 Any prior conte
1878a 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72 69 nt.** is overwri
1878b 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65 69 tten without bei
1878c 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 ng freed..**.**
1878d 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 If this routine
1878e 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65 fails for any re
1878f 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65 74 ason (malloc ret
18790 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e 61 urns NULL or una
18791 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20 66 ble.** to read f
18792 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74 68 rom the disk) th
18793 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20 6c en the pMem is l
18794 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 eft in an incons
18795 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2f istent state..*/
18796 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18797 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
18798 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20 42 emFromBtree(. B
18799 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 tCursor *pCur,
1879a 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 /* Cursor point
1879b 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74 6f ing at record to
1879c 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20 20 retrieve. */.
1879d 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20 20 int offset,
1879e 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d /* Offset from
1879f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 64 61 the start of da
187a0 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79 74 ta to return byt
187a1 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e es from. */. in
187a2 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 t amt,
187a3 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
187a4 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a 2f es to return. */
187a5 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20 20 . int key,
187a6 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c /* If true,
187a7 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20 74 retrieve from t
187a8 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e 6f he btree key, no
187a9 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65 6d t data. */. Mem
187aa 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 2f *pMem /
187ab 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64 61 * OUT: Return da
187ac 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20 73 ta in this Mem s
187ad 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b 0a tructure. */.){.
187ae 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 char *zData;
187af 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72 6f /* Data fro
187b0 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 m the btree laye
187b1 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 6c r */. int avail
187b2 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75 6d able = 0; /* Num
187b3 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 ber of bytes ava
187b4 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f ilable on the lo
187b5 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 20 2a cal btree page *
187b6 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b /. sqlite3 *db;
187b7 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
187b8 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
187b9 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
187ba 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20 73 TE_OK;.. db = s
187bb 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
187bc 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73 73 rDb(pCur);. ass
187bd 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
187be 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
187bf 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
187c0 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d (pMem->flags & M
187c1 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
187c2 0a 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20 . if( key ){.
187c3 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20 zData = (char
187c4 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 *)sqlite3BtreeKe
187c5 79 46 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 yFetch(pCur, &av
187c6 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 ailable);. }els
187c7 65 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 e{. zData = (
187c8 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 char *)sqlite3Bt
187c9 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43 75 reeDataFetch(pCu
187ca 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a r, &available);.
187cb 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 44 }. assert( zD
187cc 61 74 61 21 3d 30 20 29 3b 0a 0a 20 20 69 66 28 ata!=0 );.. if(
187cd 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 61 76 61 offset+amt<=ava
187ce 69 6c 61 62 6c 65 20 26 26 20 28 28 70 4d 65 6d ilable && ((pMem
187cf 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 ->flags&MEM_Dyn)
187d0 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d 3e 78 44 65 ==0 || pMem->xDe
187d1 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 l) ){. sqlite
187d2 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
187d3 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d pMem);. pMem-
187d4 3e 7a 20 3d 20 26 7a 44 61 74 61 5b 6f 66 66 73 >z = &zData[offs
187d5 65 74 5d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 et];. pMem->f
187d6 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c lags = MEM_Blob|
187d7 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 6c MEM_Ephem;. }el
187d8 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b se if( SQLITE_OK
187d9 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 ==(rc = sqlite3V
187da 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c dbeMemGrow(pMem,
187db 20 61 6d 74 2b 32 2c 20 30 29 29 20 29 7b 0a 20 amt+2, 0)) ){.
187dc 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
187dd 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79 MEM_Blob|MEM_Dy
187de 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 n|MEM_Term;.
187df 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20 pMem->enc = 0;.
187e0 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
187e1 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 20 SQLITE_BLOB;.
187e2 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 if( key ){.
187e3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
187e4 72 65 65 4b 65 79 28 70 43 75 72 2c 20 6f 66 66 reeKey(pCur, off
187e5 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e set, amt, pMem->
187e6 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 z);. }else{.
187e7 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
187e8 33 42 74 72 65 65 44 61 74 61 28 70 43 75 72 2c 3BtreeData(pCur,
187e9 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d offset, amt, pM
187ea 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 em->z);. }.
187eb 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d pMem->z[amt] =
187ec 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 0;. pMem->z[
187ed 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 amt+1] = 0;.
187ee 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
187ef 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
187f0 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
187f1 28 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 (pMem);. }.
187f2 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d }. pMem->n = am
187f3 74 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b t;.. return rc;
187f4 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 .}../* This func
187f5 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 76 61 tion is only ava
187f6 69 6c 61 62 6c 65 20 69 6e 74 65 72 6e 61 6c 6c ilable internall
187f7 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 y, it is not par
187f8 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 74 65 t of the.** exte
187f9 72 6e 61 6c 20 41 50 49 2e 20 49 74 20 77 6f 72 rnal API. It wor
187fa 6b 73 20 69 6e 20 61 20 73 69 6d 69 6c 61 72 20 ks in a similar
187fb 77 61 79 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 way to sqlite3_v
187fc 61 6c 75 65 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 alue_text(),.**
187fd 65 78 63 65 70 74 20 74 68 65 20 64 61 74 61 20 except the data
187fe 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e 20 74 returned is in t
187ff 68 65 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 he encoding spec
18800 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 ified by the sec
18801 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 ond.** parameter
18802 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62 65 20 , which must be
18803 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 one of SQLITE_UT
18804 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 F16BE, SQLITE_UT
18805 46 31 36 4c 45 20 6f 72 0a 2a 2a 20 53 51 4c 49 F16LE or.** SQLI
18806 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 TE_UTF8..**.** (
18807 32 30 30 36 2d 30 32 2d 31 36 3a 29 20 20 54 68 2006-02-16:) Th
18808 65 20 65 6e 63 20 76 61 6c 75 65 20 63 61 6e 20 e enc value can
18809 62 65 20 6f 72 2d 65 64 20 77 69 74 68 20 53 51 be or-ed with SQ
1880a 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e LITE_UTF16_ALIGN
1880b 45 44 2e 0a 2a 2a 20 49 66 20 74 68 61 74 20 69 ED..** If that i
1880c 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e s the case, then
1880d 20 74 68 65 20 72 65 73 75 6c 74 20 6d 75 73 74 the result must
1880e 20 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61 be aligned on a
1880f 6e 20 65 76 65 6e 20 62 79 74 65 0a 2a 2a 20 62 n even byte.** b
18810 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 oundary..*/.SQLI
18811 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
18812 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 void *sqlite3Va
18813 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f lueText(sqlite3_
18814 76 61 6c 75 65 2a 20 70 56 61 6c 2c 20 75 38 20 value* pVal, u8
18815 65 6e 63 29 7b 0a 20 20 69 66 28 20 21 70 56 61 enc){. if( !pVa
18816 6c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 l ) return 0;..
18817 20 61 73 73 65 72 74 28 20 70 56 61 6c 2d 3e 64 assert( pVal->d
18818 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
18819 6d 75 74 65 78 5f 68 65 6c 64 28 70 56 61 6c 2d mutex_held(pVal-
1881a 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
1881b 20 61 73 73 65 72 74 28 20 28 65 6e 63 26 33 29 assert( (enc&3)
1881c 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 54 45 5f 55 ==(enc&~SQLITE_U
1881d 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 3b TF16_ALIGNED) );
1881e 0a 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c . assert( (pVal
1881f 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
18820 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 wSet)==0 );.. i
18821 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d f( pVal->flags&M
18822 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 EM_Null ){. r
18823 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 eturn 0;. }. a
18824 73 73 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 ssert( (MEM_Blob
18825 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 >>3) == MEM_Str
18826 29 3b 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67 73 );. pVal->flags
18827 20 7c 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 |= (pVal->flags
18828 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b & MEM_Blob)>>3;
18829 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 56 . expandBlob(pV
1882a 61 6c 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d al);. if( pVal-
1882b 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 >flags&MEM_Str )
1882c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1882d 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1882e 70 56 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51 4c pVal, enc & ~SQL
1882f 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 ITE_UTF16_ALIGNE
18830 44 29 3b 0a 20 20 20 20 69 66 28 20 28 65 6e 63 D);. if( (enc
18831 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f & SQLITE_UTF16_
18832 41 4c 49 47 4e 45 44 29 21 3d 30 20 26 26 20 31 ALIGNED)!=0 && 1
18833 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f ==(1&SQLITE_PTR_
18834 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 TO_INT(pVal->z))
18835 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
18836 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 ( (pVal->flags &
18837 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f (MEM_Ephem|MEM_
18838 53 74 61 74 69 63 29 29 21 3d 30 20 29 3b 0a 20 Static))!=0 );.
18839 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1883a 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 VdbeMemMakeWrite
1883b 61 62 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 able(pVal)!=SQLI
1883c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1883d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 return 0;.
1883e 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c }. }. sql
1883f 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
18840 72 6d 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20 rminate(pVal);.
18841 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
18842 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 rt( (pVal->flags
18843 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b &MEM_Blob)==0 );
18844 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
18845 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 56 61 MemStringify(pVa
18846 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 61 73 73 l, enc);. ass
18847 65 72 74 28 20 30 3d 3d 28 31 26 53 51 4c 49 54 ert( 0==(1&SQLIT
18848 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 61 E_PTR_TO_INT(pVa
18849 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20 7d 0a 20 20 l->z)) );. }.
1884a 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63 assert(pVal->enc
1884b 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 ==(enc & ~SQLITE
1884c 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 _UTF16_ALIGNED)
1884d 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 || pVal->db==0.
1884e 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
1884f 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 pVal->db->malloc
18850 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 Failed );. if(
18851 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 pVal->enc==(enc
18852 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f & ~SQLITE_UTF16_
18853 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20 ALIGNED) ){.
18854 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a return pVal->z;.
18855 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
18856 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a urn 0;. }.}../*
18857 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
18858 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
18859 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 bject..*/.SQLITE
1885a 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
1885b 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 _value *sqlite3V
1885c 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 alueNew(sqlite3
1885d 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d *db){. Mem *p =
1885e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1885f 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
18860 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b *p));. if( p ){
18861 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 . p->flags =
18862 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d MEM_Null;. p-
18863 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e >type = SQLITE_N
18864 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d ULL;. p->db =
18865 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 db;. }. retur
18866 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 n p;.}../*.** Cr
18867 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 eate a new sqlit
18868 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c e3_value object,
18869 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1886a 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a value of pExpr..
1886b 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 **.** This only
1886c 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73 works for very s
1886d 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e imple expression
1886e 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f s that consist o
1886f 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a f one constant.*
18870 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35 * token (i.e. "5
18871 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74 ", "5.1", "'a st
18872 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20 ring'"). If the
18873 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a expression can.*
18874 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64 * be converted d
18875 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76 irectly into a v
18876 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 alue, then the v
18877 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 alue is allocate
18878 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 d and.** a point
18879 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 er written to *p
1887a 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 pVal. The caller
1887b 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
1887c 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 for deallocating
1887d 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79 .** the value by
1887e 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 passing it to s
1887f 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 qlite3ValueFree(
18880 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74 ) later on. If t
18881 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a he expression.**
18882 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 cannot be conve
18883 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c rted to a value,
18884 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20 then *ppVal is
18885 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a set to NULL..*/.
18886 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
18887 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 nt sqlite3ValueF
18888 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74 romExpr(. sqlit
18889 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
1888a 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1888b 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1888c 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 */. Expr *pExpr
1888d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1888e 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
1888f 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a to evaluate */.
18890 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 u8 enc,
18891 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
18892 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a ncoding to use *
18893 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c /. u8 affinity,
18894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18895 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 Affinity to use
18896 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 */. sqlite3_va
18897 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20 lue **ppVal
18898 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 /* Write the new
18899 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29 value here */.)
1889a 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68 {. int op;. ch
1889b 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 ar *zVal = 0;.
1889c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1889d 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 Val = 0;.. if(
1889e 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70 !pExpr ){. *p
1889f 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 pVal = 0;. re
188a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
188a1 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 }. op = pExpr
188a2 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70 3d ->op;.. if( op=
188a3 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 =TK_STRING || op
188a4 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 ==TK_FLOAT || op
188a5 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a ==TK_INTEGER ){.
188a6 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 zVal = sqlit
188a7 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 e3DbStrNDup(db,
188a8 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f (char*)pExpr->to
188a9 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f ken.z, pExpr->to
188aa 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61 6c ken.n);. pVal
188ab 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e = sqlite3ValueN
188ac 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 ew(db);. if(
188ad 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 29 !zVal || !pVal )
188ae 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
188af 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 sqlite3Dequote
188b0 28 7a 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 (zVal);. sqli
188b1 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 te3ValueSetStr(p
188b2 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 Val, -1, zVal, S
188b3 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 QLITE_UTF8, SQLI
188b4 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 TE_DYNAMIC);.
188b5 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54 if( (op==TK_INT
188b6 45 47 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 EGER || op==TK_F
188b7 4c 4f 41 54 20 29 20 26 26 20 61 66 66 69 6e 69 LOAT ) && affini
188b8 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ty==SQLITE_AFF_N
188b9 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ONE ){. sql
188ba 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 ite3ValueApplyAf
188bb 66 69 6e 69 74 79 28 70 56 61 6c 2c 20 53 51 4c finity(pVal, SQL
188bc 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c ITE_AFF_NUMERIC,
188bd 20 65 6e 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 enc);. }else
188be 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
188bf 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 alueApplyAffinit
188c0 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 y(pVal, affinity
188c1 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 , enc);. }.
188c2 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b }else if( op==TK
188c3 5f 55 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20 _UMINUS ) {.
188c4 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 if( SQLITE_OK==s
188c5 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 qlite3ValueFromE
188c6 78 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c xpr(db,pExpr->pL
188c7 65 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79 eft,enc,affinity
188c8 2c 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 ,&pVal) ){.
188c9 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20 pVal->u.i = -1
188ca 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 * pVal->u.i;.
188cb 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 2d 31 /* (double)-1
188cc 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 In case of SQLI
188cd 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
188ce 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 _POINT... */.
188cf 20 20 20 70 56 61 6c 2d 3e 72 20 3d 20 28 64 6f pVal->r = (do
188d0 75 62 6c 65 29 2d 31 20 2a 20 70 56 61 6c 2d 3e uble)-1 * pVal->
188d1 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 r;. }. }.#if
188d2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
188d3 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 _BLOB_LITERAL.
188d4 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f else if( op==TK_
188d5 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69 6e 74 20 BLOB ){. int
188d6 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 nVal;. assert
188d7 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e ( pExpr->token.n
188d8 3e 3d 33 20 29 3b 0a 20 20 20 20 61 73 73 65 72 >=3 );. asser
188d9 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e t( pExpr->token.
188da 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 z[0]=='x' || pEx
188db 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d pr->token.z[0]==
188dc 27 58 27 20 29 3b 0a 20 20 20 20 61 73 73 65 72 'X' );. asser
188dd 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e t( pExpr->token.
188de 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 z[1]=='\'' );.
188df 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
188e0 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78 70 72 2d 3e >token.z[pExpr->
188e1 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d 27 5c 27 27 token.n-1]=='\''
188e2 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 );. pVal = s
188e3 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 qlite3ValueNew(d
188e4 62 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 61 b);. if( !pVa
188e5 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b l ) goto no_mem;
188e6 0a 20 20 20 20 6e 56 61 6c 20 3d 20 70 45 78 70 . nVal = pExp
188e7 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20 33 3b 0a r->token.n - 3;.
188e8 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 68 61 72 zVal = (char
188e9 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a *)pExpr->token.z
188ea 20 2b 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 + 2;. sqlite
188eb 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
188ec 56 61 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 54 Val, sqlite3HexT
188ed 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c 20 oBlob(db, zVal,
188ee 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a 20 nVal), nVal/2,.
188ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
188f0 20 20 20 20 20 20 20 20 30 2c 20 53 51 4c 49 54 0, SQLIT
188f1 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a E_DYNAMIC);. }.
188f2 23 65 6e 64 69 66 0a 0a 20 20 2a 70 70 56 61 6c #endif.. *ppVal
188f3 20 3d 20 70 56 61 6c 3b 0a 20 20 72 65 74 75 72 = pVal;. retur
188f4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f n SQLITE_OK;..no
188f5 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c _mem:. db->mall
188f6 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
188f7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
188f8 2c 20 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74 , zVal);. sqlit
188f9 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c e3ValueFree(pVal
188fa 29 3b 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b );. *ppVal = 0;
188fb 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
188fc 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _NOMEM;.}../*.**
188fd 20 43 68 61 6e 67 65 20 74 68 65 20 73 74 72 69 Change the stri
188fe 6e 67 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73 ng value of an s
188ff 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
18900 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ect.*/.SQLITE_PR
18901 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
18902 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20 e3ValueSetStr(.
18903 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
18904 76 2c 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 v, /* Value
18905 74 6f 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69 to be set */. i
18906 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 nt n,
18907 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f /* Length o
18908 66 20 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20 f string z */.
18909 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20 const void *z,
1890a 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 /* Text of
1890b 20 74 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20 the new string
1890c 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 */. u8 enc,
1890d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e /* En
1890e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f coding to use */
1890f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
18910 76 6f 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74 void*) /* Dest
18911 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73 ructor for the s
18912 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 tring */.){. if
18913 28 20 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 ( v ) sqlite3Vdb
18914 65 4d 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 eMemSetStr((Mem
18915 2a 29 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 *)v, z, n, enc,
18916 78 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 xDel);.}../*.**
18917 46 72 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f Free an sqlite3_
18918 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a value object.*/.
18919 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1891a 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 oid sqlite3Value
1891b 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c Free(sqlite3_val
1891c 75 65 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 ue *v){. if( !v
1891d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c ) return;. sql
1891e 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1891f 73 65 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20 se((Mem *)v);.
18920 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 28 28 sqlite3DbFree(((
18921 4d 65 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 29 3b Mem*)v)->db, v);
18922 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
18923 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
18924 79 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 ytes in the sqli
18925 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
18926 20 61 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 assuming.** tha
18927 74 20 69 74 20 75 73 65 73 20 74 68 65 20 65 6e t it uses the en
18928 63 6f 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a coding "enc".*/.
18929 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1892a 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 nt sqlite3ValueB
1892b 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c ytes(sqlite3_val
1892c 75 65 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 ue *pVal, u8 enc
1892d 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d ){. Mem *p = (M
1892e 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 em*)pVal;. if(
1892f 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f (p->flags & MEM_
18930 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 Blob)!=0 || sqli
18931 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 te3ValueText(pVa
18932 6c 2c 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 l, enc) ){. i
18933 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 f( p->flags & ME
18934 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
18935 72 65 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 70 2d return p->n + p-
18936 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 >u.nZero;. }e
18937 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
18938 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 n p->n;. }.
18939 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
1893a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1893b 20 45 6e 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e End of vdbemem.
1893c 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1893d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1893e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1893f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
18940 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 Begin file vdbe
18941 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a aux.c **********
18942 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18943 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
18944 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 ./*.** 2003 Sept
18945 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 ember 6.**.** Th
18946 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
18947 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
18948 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
18949 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1894a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1894b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1894c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1894d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1894e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1894f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
18950 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
18951 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
18952 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
18953 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
18954 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
18955 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
18956 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
18957 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18958 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18959 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1895a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1895b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
1895c 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
1895d 65 20 75 73 65 64 20 66 6f 72 20 63 72 65 61 74 e used for creat
1895e 69 6e 67 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c ing, destroying,
1895f 20 61 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a and populating.
18960 2a 2a 20 61 20 56 44 42 45 20 28 6f 72 20 61 6e ** a VDBE (or an
18961 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 "sqlite3_stmt"
18962 61 73 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 as it is known t
18963 6f 20 74 68 65 20 6f 75 74 73 69 64 65 20 77 6f o the outside wo
18964 72 6c 64 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20 rld.) Prior.**
18965 74 6f 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37 to version 2.8.7
18966 2c 20 61 6c 6c 20 74 68 69 73 20 63 6f 64 65 20 , all this code
18967 77 61 73 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 was combined int
18968 6f 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 o the vdbe.c sou
18969 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74 rce file..** But
1896a 20 74 68 61 74 20 66 69 6c 65 20 77 61 73 20 67 that file was g
1896b 65 74 74 69 6e 67 20 74 6f 6f 20 62 69 67 20 73 etting too big s
1896c 6f 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e o this subroutin
1896d 65 73 20 77 65 72 65 20 73 70 6c 69 74 20 6f 75 es were split ou
1896e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 t..**.** $Id: vd
1896f 62 65 61 75 78 2e 63 2c 76 20 31 2e 34 34 36 20 beaux.c,v 1.446
18970 32 30 30 39 2f 30 33 2f 32 35 20 31 35 3a 34 33 2009/03/25 15:43
18971 3a 30 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :09 danielk1977
18972 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a Exp $.*/..../*.*
18973 2a 20 57 68 65 6e 20 64 65 62 75 67 67 69 6e 67 * When debugging
18974 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 the code genera
18975 74 6f 72 20 69 6e 20 61 20 73 79 6d 62 6f 6c 69 tor in a symboli
18976 63 20 64 65 62 75 67 67 65 72 2c 20 6f 6e 65 20 c debugger, one
18977 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68 65 20 73 can.** set the s
18978 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 qlite3VdbeAddopT
18979 72 61 63 65 20 74 6f 20 31 20 61 6e 64 20 61 6c race to 1 and al
1897a 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c 6c 20 62 l opcodes will b
1897b 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20 61 73 20 e printed.** as
1897c 74 68 65 79 20 61 72 65 20 61 64 64 65 64 20 74 they are added t
1897d 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f o the instructio
1897e 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 n stream..*/.#if
1897f 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
18980 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18981 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
18982 64 64 6f 70 54 72 61 63 65 20 3d 20 30 3b 0a 23 ddopTrace = 0;.#
18983 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 endif.../*.** Cr
18984 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 eate a new virtu
18985 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 al database engi
18986 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ne..*/.SQLITE_PR
18987 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 IVATE Vdbe *sqli
18988 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71 te3VdbeCreate(sq
18989 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 lite3 *db){. Vd
1898a 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c be *p;. p = sql
1898b 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
1898c 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 64 62 65 (db, sizeof(Vdbe
1898d 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 ) );. if( p==0
1898e 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 2d ) return 0;. p-
1898f 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20 >db = db;. if(
18990 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 db->pVdbe ){.
18991 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70 50 72 65 db->pVdbe->pPre
18992 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e v = p;. }. p->
18993 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 56 64 62 pNext = db->pVdb
18994 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 e;. p->pPrev =
18995 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 0;. db->pVdbe =
18996 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d p;. p->magic =
18997 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 VDBE_MAGIC_INIT
18998 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
18999 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 ./*.** Remember
1899a 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 66 the SQL string f
1899b 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 or a prepared st
1899c 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 atement..*/.SQLI
1899d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1899e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71 sqlite3VdbeSetSq
1899f 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 l(Vdbe *p, const
189a0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c char *z, int n,
189a1 20 69 6e 74 20 69 73 50 72 65 70 61 72 65 56 32 int isPrepareV2
189a2 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 ){. if( p==0 )
189a3 72 65 74 75 72 6e 3b 0a 23 69 66 64 65 66 20 53 return;.#ifdef S
189a4 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
189a5 0a 20 20 69 66 28 20 21 69 73 50 72 65 70 61 72 . if( !isPrepar
189a6 65 56 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 65 eV2 ) return;.#e
189a7 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 ndif. assert( p
189a8 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70 ->zSql==0 );. p
189a9 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 ->zSql = sqlite3
189aa 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c DbStrNDup(p->db,
189ab 20 7a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 50 z, n);. p->isP
189ac 72 65 70 61 72 65 56 32 20 3d 20 69 73 50 72 65 repareV2 = isPre
189ad 70 61 72 65 56 32 20 3f 20 31 20 3a 20 30 3b 0a pareV2 ? 1 : 0;.
189ae 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
189af 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74 the SQL associat
189b0 65 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72 ed with a prepar
189b1 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a ed statement.*/.
189b2 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
189b3 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 char *sqlite3_s
189b4 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ql(sqlite3_stmt
189b5 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
189b6 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
189b7 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d mt;. return (p-
189b8 3e 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 70 >isPrepareV2 ? p
189b9 2d 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a ->zSql : 0);.}..
189ba 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 /*.** Swap all c
189bb 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74 ontent between t
189bc 77 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72 wo VDBE structur
189bd 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
189be 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
189bf 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20 e3VdbeSwap(Vdbe
189c0 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a *pA, Vdbe *pB){.
189c1 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d Vdbe tmp, *pTm
189c2 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b p;. char *zTmp;
189c3 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 . tmp = *pA;.
189c4 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 *pA = *pB;. *pB
189c5 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d = tmp;. pTmp =
189c6 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 pA->pNext;. pA
189c7 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e ->pNext = pB->pN
189c8 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 ext;. pB->pNext
189c9 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 = pTmp;. pTmp
189ca 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 = pA->pPrev;. p
189cb 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 A->pPrev = pB->p
189cc 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 Prev;. pB->pPre
189cd 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 v = pTmp;. zTmp
189ce 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 = pA->zSql;. p
189cf 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 A->zSql = pB->zS
189d0 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d ql;. pB->zSql =
189d1 20 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 zTmp;. pB->isP
189d2 72 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 repareV2 = pA->i
189d3 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 sPrepareV2;.}..#
189d4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
189d5 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 UG./*.** Turn tr
189d6 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a acing on or off.
189d7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
189d8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
189d9 62 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c beTrace(Vdbe *p,
189da 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 FILE *trace){.
189db 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63 p->trace = trac
189dc 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a e;.}.#endif../*.
189dd 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64 ** Resize the Vd
189de 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20 be.aOp array so
189df 74 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65 that it is at le
189e0 61 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 ast one op large
189e1 72 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 r than .** it wa
189e2 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f s..**.** If an o
189e3 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 ut-of-memory err
189e4 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
189e5 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 resizing the arr
189e6 61 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 ay, return.** SQ
189e7 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 LITE_NOMEM. In t
189e8 68 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f his case Vdbe.aO
189e9 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c p and Vdbe.nOpAl
189ea 6c 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 loc remain .** u
189eb 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69 nchanged (this i
189ec 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 s so that any op
189ed 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c codes already al
189ee 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a located can be .
189ef 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61 ** correctly dea
189f0 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 llocated along w
189f1 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 ith the rest of
189f2 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 the Vdbe)..*/.st
189f3 61 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 atic int growOpA
189f4 72 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 rray(Vdbe *p){.
189f5 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 VdbeOp *pNew;.
189f6 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e int nNew = (p->
189f7 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f nOpAlloc ? p->nO
189f8 70 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 pAlloc*2 : (int)
189f9 28 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 (1024/sizeof(Op)
189fa 29 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c ));. pNew = sql
189fb 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d ite3DbRealloc(p-
189fc 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 >db, p->aOp, nNe
189fd 77 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 w*sizeof(Op));.
189fe 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 if( pNew ){.
189ff 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 p->nOpAlloc = s
18a00 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
18a01 7a 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f ze(p->db, pNew)/
18a02 73 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 sizeof(Op);.
18a03 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 p->aOp = pNew;.
18a04 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65 }. return (pNe
18a05 77 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 w ? SQLITE_OK :
18a06 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d SQLITE_NOMEM);.}
18a07 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 ../*.** Add a ne
18a08 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f w instruction to
18a09 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 the list of ins
18a0a 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e tructions curren
18a0b 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 t in the.** VDBE
18a0c 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 . Return the ad
18a0d 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77 dress of the new
18a0e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a instruction..**
18a0f 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a .** Parameters:.
18a10 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20 **.** p
18a11 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 Pointer
18a12 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a to the VDBE.**.
18a13 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20 ** op
18a14 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65 The opcode
18a15 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75 for this instru
18a16 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 ction.**.** p
18a17 31 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f 1, p2, p3 O
18a18 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 perands.**.** Us
18a19 65 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 e the sqlite3Vdb
18a1a 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 eResolveLabel()
18a1b 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 function to fix
18a1c 61 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a an address and.*
18a1d 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 * the sqlite3Vdb
18a1e 65 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63 eChangeP4() func
18a1f 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 tion to change t
18a20 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
18a21 50 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a P4.** operand..*
18a22 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18a23 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
18a24 41 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 AddOp3(Vdbe *p,
18a25 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 int op, int p1,
18a26 69 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b int p2, int p3){
18a27 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 . int i;. Vdbe
18a28 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 Op *pOp;.. i =
18a29 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 p->nOp;. assert
18a2a 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
18a2b 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
18a2c 20 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26 assert( op>0 &&
18a2d 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 op<0xff );. if
18a2e 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 ( p->nOpAlloc<=i
18a2f 20 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 ){. if( grow
18a30 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 OpArray(p) ){.
18a31 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
18a32 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 }. }. p->nOp
18a33 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e ++;. pOp = &p->
18a34 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f aOp[i];. pOp->o
18a35 70 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a pcode = (u8)op;.
18a36 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 pOp->p5 = 0;.
18a37 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 pOp->p1 = p1;.
18a38 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 pOp->p2 = p2;.
18a39 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 pOp->p3 = p3;.
18a3a 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a pOp->p4.p = 0;.
18a3b 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
18a3c 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d P4_NOTUSED;. p-
18a3d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 >expired = 0;.#i
18a3e 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
18a3f 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e G. pOp->zCommen
18a40 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c t = 0;. if( sql
18a41 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 ite3VdbeAddopTra
18a42 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 ce ) sqlite3Vdbe
18a43 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 PrintOp(0, i, &p
18a44 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 ->aOp[i]);.#endi
18a45 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 f.#ifdef VDBE_PR
18a46 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 OFILE. pOp->cyc
18a47 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e les = 0;. pOp->
18a48 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a cnt = 0;.#endif.
18a49 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 return i;.}.SQ
18a4a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18a4b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
18a4c 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 p0(Vdbe *p, int
18a4d 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 op){. return sq
18a4e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
18a4f 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b p, op, 0, 0, 0);
18a50 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
18a51 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
18a52 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70 2c eAddOp1(Vdbe *p,
18a53 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 int op, int p1)
18a54 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
18a55 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
18a56 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d op, p1, 0, 0);.}
18a57 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18a58 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
18a59 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20 69 ddOp2(Vdbe *p, i
18a5a 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 nt op, int p1, i
18a5b 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72 6e nt p2){. return
18a5c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
18a5d 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 p3(p, op, p1, p2
18a5e 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 , 0);.}.../*.**
18a5f 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 Add an opcode th
18a60 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 at includes the
18a61 70 34 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f p4 value as a po
18a62 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 inter..*/.SQLITE
18a63 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
18a64 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a ite3VdbeAddOp4(.
18a65 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 Vdbe *p,
18a66 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 /* Add the
18a67 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73 20 opcode to this
18a68 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 VM */. int op,
18a69 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
18a6a 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f he new opcode */
18a6b 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20 . int p1,
18a6c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31 /* The P1
18a6d 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
18a6e 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20 t p2,
18a6f 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72 /* The P2 oper
18a70 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c and */. int p3,
18a71 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18a72 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 2a The P3 operand *
18a73 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
18a74 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 zP4, /* The P
18a75 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 4 operand */. i
18a76 6e 74 20 70 34 74 79 70 65 20 20 20 20 20 20 20 nt p4type
18a77 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 /* P4 operand
18a78 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e type */.){. in
18a79 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 t addr = sqlite3
18a7a 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 VdbeAddOp3(p, op
18a7b 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 , p1, p2, p3);.
18a7c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
18a7d 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 geP4(p, addr, zP
18a7e 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72 65 4, p4type);. re
18a7f 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a turn addr;.}../*
18a80 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
18a81 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 symbolic label
18a82 66 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 for an instructi
18a83 6f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20 on that has yet
18a84 74 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 to be.** coded.
18a85 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 The symbolic la
18a86 62 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 bel is really ju
18a87 73 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 st a negative nu
18a88 6d 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 mber. The.** la
18a89 62 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 bel can be used
18a8a 61 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 as the P2 value
18a8b 6f 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e of an operation.
18a8c 20 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a Later, when.**
18a8d 20 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 the label is re
18a8e 73 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 solved to a spec
18a8f 69 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 ific address, th
18a90 65 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e e VDBE will scan
18a91 0a 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 .** through its
18a92 6f 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 operation list a
18a93 6e 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 nd change all va
18a94 6c 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 lues of P2 which
18a95 20 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 match.** the la
18a96 62 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 bel into the res
18a97 6f 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a olved address..*
18a98 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e *.** The VDBE kn
18a99 6f 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61 ows that a P2 va
18a9a 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 lue is a label b
18a9b 65 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 ecause labels ar
18a9c 65 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 e.** always nega
18a9d 74 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 tive and P2 valu
18a9e 65 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 es are suppose t
18a9f 6f 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 o be non-negativ
18aa0 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e e..** Hence, a n
18aa1 65 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 egative P2 value
18aa2 20 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 is a label that
18aa3 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 has yet to be r
18aa4 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a esolved..**.** Z
18aa5 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 ero is returned
18aa6 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 if a malloc() fa
18aa7 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ils..*/.SQLITE_P
18aa8 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18aa9 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
18aaa 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
18aab 69 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 i;. i = p->nLab
18aac 65 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 el++;. assert(
18aad 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
18aae 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
18aaf 66 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 f( i>=p->nLabelA
18ab0 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 lloc ){. int
18ab1 6e 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c n = p->nLabelAll
18ab2 6f 63 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d oc*2 + 5;. p-
18ab3 3e 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 >aLabel = sqlite
18ab4 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
18ab5 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 (p->db, p->aLabe
18ab6 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 l,.
18ab7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18ab8 20 20 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 n*size
18ab9 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 of(p->aLabel[0])
18aba 29 3b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c );. p->nLabel
18abb 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 Alloc = sqlite3D
18abc 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 bMallocSize(p->d
18abd 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 b, p->aLabel)/si
18abe 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 zeof(p->aLabel[0
18abf 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d ]);. }. if( p-
18ac0 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 >aLabel ){. p
18ac1 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 ->aLabel[i] = -1
18ac2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d ;. }. return -
18ac3 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 1-i;.}../*.** Re
18ac4 73 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 solve label "x"
18ac5 74 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73 to be the addres
18ac6 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e s of the next in
18ac7 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 struction to.**
18ac8 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 be inserted. Th
18ac9 65 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 e parameter "x"
18aca 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f must have been o
18acb 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
18acc 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 a prior call to
18acd 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
18ace 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 abel()..*/.SQLIT
18acf 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
18ad0 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
18ad1 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 eLabel(Vdbe *p,
18ad2 69 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 int x){. int j
18ad3 3d 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 = -1-x;. assert
18ad4 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
18ad5 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
18ad6 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 assert( j>=0 &&
18ad7 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a j<p->nLabel );.
18ad8 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 if( p->aLabel
18ad9 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c ){. p->aLabel
18ada 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 [j] = p->nOp;.
18adb 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 }.}../*.** Loop
18adc 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67 through the prog
18add 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 ram looking for
18ade 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 P2 values that a
18adf 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f re negative.** o
18ae0 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 n jump instructi
18ae1 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20 ons. Each such
18ae2 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c value is a label
18ae3 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a . Resolve the.*
18ae4 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 * label by setti
18ae5 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 ng the P2 value
18ae6 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e to its correct n
18ae7 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a on-zero value..*
18ae8 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
18ae9 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 e is called once
18aea 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 after all opcod
18aeb 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73 es have been ins
18aec 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 erted..**.** Var
18aed 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 iable *pMaxFuncA
18aee 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 rgs is set to th
18aef 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 e maximum value
18af0 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 of any P2 argume
18af1 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f nt .** to an OP_
18af2 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 Function, OP_Agg
18af3 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 Step or OP_VFilt
18af4 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 er opcode. This
18af5 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 is used by .** s
18af6 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 qlite3VdbeMakeRe
18af7 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68 ady() to size th
18af8 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 e Vdbe.apArg[] a
18af9 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rray..**.** This
18afa 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f routine also do
18afb 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 es the following
18afc 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20 optimization:
18afd 49 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20 It scans for.**
18afe 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 instructions tha
18aff 74 20 6d 69 67 68 74 20 63 61 75 73 65 20 61 20 t might cause a
18b00 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 statement rollba
18b01 63 6b 2e 20 20 53 75 63 68 20 69 6e 73 74 72 75 ck. Such instru
18b02 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 3a 0a 2a ctions.** are:.*
18b03 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c *.** * OP_Hal
18b04 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 t with P1=SQLITE
18b05 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 _CONSTRAINT and
18b06 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 P2=OE_Abort..**
18b07 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a * OP_Destroy.
18b08 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 ** * OP_VUpda
18b09 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 te.** * OP_VR
18b0a 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 49 66 20 6e ename.**.** If n
18b0b 6f 20 73 75 63 68 20 69 6e 73 74 72 75 63 74 69 o such instructi
18b0c 6f 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 on is found, the
18b0d 6e 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e n every Statemen
18b0e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 2a t instruction .*
18b0f 2a 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 * is changed to
18b10 61 20 4e 6f 6f 70 2e 20 20 49 6e 20 74 68 69 73 a Noop. In this
18b11 20 77 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63 way, we avoid c
18b12 72 65 61 74 69 6e 67 20 74 68 65 20 73 74 61 74 reating the stat
18b13 65 6d 65 6e 74 20 0a 2a 2a 20 6a 6f 75 72 6e 61 ement .** journa
18b14 6c 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61 l file unnecessa
18b15 72 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rily..*/.static
18b16 76 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 void resolveP2Va
18b17 6c 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e lues(Vdbe *p, in
18b18 74 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 t *pMaxFuncArgs)
18b19 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
18b1a 20 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20 nMaxArgs = 0;.
18b1b 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20 Op *pOp;. int
18b1c 2a 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61 *aLabel = p->aLa
18b1d 62 65 6c 3b 0a 20 20 69 6e 74 20 64 6f 65 73 53 bel;. int doesS
18b1e 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b tatementRollback
18b1f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 53 = 0;. int hasS
18b20 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20 tatementBegin =
18b21 30 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 0;. p->readOnly
18b22 20 3d 20 31 3b 0a 20 20 70 2d 3e 75 73 65 73 53 = 1;. p->usesS
18b23 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a tmtJournal = 0;.
18b24 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 for(pOp=p->aOp
18b25 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e , i=p->nOp-1; i>
18b26 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b =0; i--, pOp++){
18b27 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d . u8 opcode =
18b28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 pOp->opcode;..
18b29 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f if( opcode==O
18b2a 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 P_Function || op
18b2b 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70 code==OP_AggStep
18b2c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f ){. if( pO
18b2d 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29 p->p5>nMaxArgs )
18b2e 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d nMaxArgs = pOp-
18b2f 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c >p5;.#ifndef SQL
18b30 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
18b31 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 TABLE. }else
18b32 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 if( opcode==OP_V
18b33 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 Update ){.
18b34 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 if( pOp->p2>nMax
18b35 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 Args ) nMaxArgs
18b36 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 = pOp->p2;.#endi
18b37 66 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 f. }. if(
18b38 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 opcode==OP_Halt
18b39 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 ){. if( pOp
18b3a 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e ->p1==SQLITE_CON
18b3b 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e STRAINT && pOp->
18b3c 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a p2==OE_Abort ){.
18b3d 20 20 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 doesStat
18b3e 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 ementRollback =
18b3f 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
18b40 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d else if( opcode=
18b41 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b =OP_Statement ){
18b42 0a 20 20 20 20 20 20 68 61 73 53 74 61 74 65 6d . hasStatem
18b43 65 6e 74 42 65 67 69 6e 20 3d 20 31 3b 0a 20 20 entBegin = 1;.
18b44 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a p->usesStmtJ
18b45 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20 ournal = 1;.
18b46 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 }else if( opcode
18b47 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 29 7b 0a ==OP_Destroy ){.
18b48 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d doesStatem
18b49 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b entRollback = 1;
18b4a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f . }else if( o
18b4b 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 pcode==OP_Transa
18b4c 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 ction && pOp->p2
18b4d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e !=0 ){. p->
18b4e 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 readOnly = 0;.#i
18b4f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18b50 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
18b51 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 }else if( opc
18b52 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 ode==OP_VUpdate
18b53 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 || opcode==OP_VR
18b54 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 64 ename ){. d
18b55 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c oesStatementRoll
18b56 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 back = 1;. }e
18b57 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d lse if( opcode==
18b58 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a 20 20 OP_VFilter ){.
18b59 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 int n;.
18b5a 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 20 assert( p->nOp
18b5b 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 20 20 - i >= 3 );.
18b5c 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 assert( pOp[-1
18b5d 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 74 ].opcode==OP_Int
18b5e 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20 6e 20 eger );. n
18b5f 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20 = pOp[-1].p1;.
18b60 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 41 72 if( n>nMaxAr
18b61 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 gs ) nMaxArgs =
18b62 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a n;.#endif. }.
18b63 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
18b64 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f VdbeOpcodeHasPro
18b65 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 4f 50 perty(opcode, OP
18b66 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 4f 70 FLG_JUMP) && pOp
18b67 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 ->p2<0 ){.
18b68 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d 3e assert( -1-pOp->
18b69 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a p2<p->nLabel );.
18b6a 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 pOp->p2 =
18b6b 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70 aLabel[-1-pOp->p
18b6c 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2];. }. }.
18b6d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d sqlite3DbFree(p-
18b6e 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b >db, p->aLabel);
18b6f 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 30 . p->aLabel = 0
18b70 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 72 ;.. *pMaxFuncAr
18b71 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a 0a gs = nMaxArgs;..
18b72 20 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72 /* If we never
18b73 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74 rollback a stat
18b74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
18b75 6e 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e n, then statemen
18b76 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 t. ** transacti
18b77 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64 ons are not need
18b78 65 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65 ed. So change e
18b79 76 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e very OP_Statemen
18b7a 74 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e t. ** opcode in
18b7b 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20 to an OP_Noop.
18b7c 54 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c This avoid a cal
18b7d 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70 l to sqlite3OsOp
18b7e 65 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20 enExclusive().
18b7f 2a 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 ** which can be
18b80 65 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d expensive on som
18b81 65 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a e platforms.. *
18b82 2f 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65 /. if( hasState
18b83 6d 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f mentBegin && !do
18b84 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 esStatementRollb
18b85 61 63 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 75 73 ack ){. p->us
18b86 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 esStmtJournal =
18b87 30 3b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70 0;. for(pOp=p
18b88 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d ->aOp, i=p->nOp-
18b89 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 1; i>=0; i--, pO
18b8a 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 p++){. if(
18b8b 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
18b8c 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 Statement ){.
18b8d 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 pOp->opcode
18b8e 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 = OP_Noop;.
18b8f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
18b90 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
18b91 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
18b92 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
18b93 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 n to be inserted
18b94 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18b95 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
18b96 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 dbeCurrentAddr(V
18b97 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 dbe *p){. asser
18b98 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 t( p->magic==VDB
18b99 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a E_MAGIC_INIT );.
18b9a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b return p->nOp;
18b9b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 .}../*.** Add a
18b9c 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 whole list of op
18b9d 65 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 erations to the
18b9e 6f 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e operation stack.
18b9f 20 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 Return the.**
18ba0 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66 address of the f
18ba1 69 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 irst operation a
18ba2 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f dded..*/.SQLITE_
18ba3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18ba4 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
18ba5 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f (Vdbe *p, int nO
18ba6 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f p, VdbeOpList co
18ba7 6e 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 nst *aOp){. int
18ba8 20 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28 addr;. assert(
18ba9 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
18baa 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
18bab 69 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 if( p->nOp + nOp
18bac 20 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 > p->nOpAlloc &
18bad 26 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 & growOpArray(p)
18bae 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
18baf 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 ;. }. addr = p
18bb0 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 ->nOp;. if( nOp
18bb1 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b >0 ){. int i;
18bb2 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 . VdbeOpList
18bb3 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 const *pIn = aOp
18bb4 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
18bb5 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b <nOp; i++, pIn++
18bb6 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 ){. int p2
18bb7 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 = pIn->p2;.
18bb8 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 VdbeOp *pOut =
18bb9 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b &p->aOp[i+addr];
18bba 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 . pOut->opc
18bbb 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 ode = pIn->opcod
18bbc 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 e;. pOut->p
18bbd 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 1 = pIn->p1;.
18bbe 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73 if( p2<0 && s
18bbf 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 qlite3VdbeOpcode
18bc0 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74 HasProperty(pOut
18bc1 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f ->opcode, OPFLG_
18bc2 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20 JUMP) ){.
18bc3 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 pOut->p2 = addr
18bc4 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20 + ADDR(p2);.
18bc5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
18bc6 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b pOut->p2 = p2;
18bc7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
18bc8 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70 Out->p3 = pIn->p
18bc9 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 3;. pOut->p
18bca 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 4type = P4_NOTUS
18bcb 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e ED;. pOut->
18bcc 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 p4.p = 0;.
18bcd 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69 pOut->p5 = 0;.#i
18bce 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
18bcf 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43 G. pOut->zC
18bd0 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 omment = 0;.
18bd1 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
18bd2 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20 eAddopTrace ){.
18bd3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
18bd4 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 bePrintOp(0, i+a
18bd5 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 ddr, &p->aOp[i+a
18bd6 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 ddr]);. }.#
18bd7 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 endif. }.
18bd8 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20 p->nOp += nOp;.
18bd9 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 }. return addr
18bda 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
18bdb 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 e the value of t
18bdc 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f he P1 operand fo
18bdd 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 r a specific ins
18bde 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 truction..** Thi
18bdf 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
18be0 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 ful when a large
18be1 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 program is load
18be2 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 ed from a.** sta
18be3 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 tic array using
18be4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
18be5 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 List but we want
18be6 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 to make a.** fe
18be7 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 w minor changes
18be8 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a to the program..
18be9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18bea 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
18beb 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20 beChangeP1(Vdbe
18bec 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e *p, int addr, in
18bed 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 t val){. assert
18bee 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 ( p==0 || p->mag
18bef 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
18bf0 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26 NIT );. if( p &
18bf1 26 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e & addr>=0 && p->
18bf2 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 nOp>addr && p->a
18bf3 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 Op ){. p->aOp
18bf4 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b [addr].p1 = val;
18bf5 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 . }.}../*.** Ch
18bf6 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
18bf7 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 f the P2 operand
18bf8 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 for a specific
18bf9 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 instruction..**
18bfa 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
18bfb 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 useful for setti
18bfc 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e ng a jump destin
18bfd 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ation..*/.SQLITE
18bfe 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
18bff 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
18c00 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 2(Vdbe *p, int a
18c01 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 ddr, int val){.
18c02 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c assert( p==0 ||
18c03 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
18c04 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
18c05 69 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 if( p && addr>=0
18c06 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 && p->nOp>addr
18c07 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 && p->aOp ){.
18c08 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 p->aOp[addr].p2
18c09 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f = val;. }.}../
18c0a 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
18c0b 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20 value of the P3
18c0c 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 operand for a sp
18c0d 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 ecific instructi
18c0e 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
18c0f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
18c10 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56 e3VdbeChangeP3(V
18c11 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 dbe *p, int addr
18c12 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 , int val){. as
18c13 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d sert( p==0 || p-
18c14 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
18c15 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
18c16 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20 26 26 p && addr>=0 &&
18c17 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26 20 p->nOp>addr &&
18c18 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70 2d p->aOp ){. p-
18c19 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 >aOp[addr].p3 =
18c1a 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a val;. }.}../*.*
18c1b 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c * Change the val
18c1c 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 ue of the P5 ope
18c1d 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 rand for the mos
18c1e 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 t recently.** ad
18c1f 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a ded operation..*
18c20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18c21 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
18c22 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a eChangeP5(Vdbe *
18c23 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 p, u8 val){. as
18c24 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d sert( p==0 || p-
18c25 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
18c26 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
18c27 20 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a p && p->aOp ){.
18c28 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e assert( p->n
18c29 4f 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 Op>0 );. p->a
18c2a 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 Op[p->nOp-1].p5
18c2b 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a = val;. }.}../*
18c2c 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 .** Change the P
18c2d 32 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 2 operand of ins
18c2e 74 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f truction addr so
18c2f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
18c30 74 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 to.** the addres
18c31 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e s of the next in
18c32 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 struction to be
18c33 63 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 coded..*/.SQLITE
18c34 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
18c35 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
18c36 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 e(Vdbe *p, int a
18c37 64 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 ddr){. sqlite3V
18c38 64 62 65 43 68 61 6e 67 65 50 32 28 70 2c 20 61 dbeChangeP2(p, a
18c39 64 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a ddr, p->nOp);.}.
18c3a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 ../*.** If the i
18c3b 6e 70 75 74 20 46 75 6e 63 44 65 66 20 73 74 72 nput FuncDef str
18c3c 75 63 74 75 72 65 20 69 73 20 65 70 68 65 6d 65 ucture is epheme
18c3d 72 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 20 69 ral, then free i
18c3e 74 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 46 75 t. If.** the Fu
18c3f 6e 63 44 65 66 20 69 73 20 6e 6f 74 20 65 70 68 ncDef is not eph
18c40 65 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e ermal, then do n
18c41 6f 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 othing..*/.stati
18c42 63 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d c void freeEphem
18c43 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c eralFunction(sql
18c44 69 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 ite3 *db, FuncDe
18c45 66 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 28 20 f *pDef){. if(
18c46 70 44 65 66 20 26 26 20 28 70 44 65 66 2d 3e 66 pDef && (pDef->f
18c47 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
18c48 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a NC_EPHEM)!=0 ){.
18c49 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
18c4a 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d e(db, pDef);. }
18c4b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
18c4c 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e a P4 value if n
18c4d 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 ecessary..*/.sta
18c4e 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28 tic void freeP4(
18c4f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
18c50 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70 p4type, void *p
18c51 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a 4){. if( p4 ){.
18c52 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79 switch( p4ty
18c53 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 pe ){. case
18c54 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20 P4_REAL:.
18c55 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20 case P4_INT64:.
18c56 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52 case P4_MPR
18c57 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65 INTF:. case
18c58 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 P4_DYNAMIC:.
18c59 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
18c5a 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 FO:. case P
18c5b 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 4_INTARRAY:.
18c5c 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 case P4_KEYINF
18c5d 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 O_HANDOFF: {.
18c5e 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
18c5f 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 ee(db, p4);.
18c60 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
18c61 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 }. case P4
18c62 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20 _VDBEFUNC: {.
18c63 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 VdbeFunc *p
18c64 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 VdbeFunc = (Vdbe
18c65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20 Func *)p4;.
18c66 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c freeEphemeral
18c67 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64 Function(db, pVd
18c68 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a beFunc->pFunc);.
18c69 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
18c6a 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 dbeDeleteAuxData
18c6b 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a (pVdbeFunc, 0);.
18c6c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
18c6d 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 bFree(db, pVdbeF
18c6e 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 unc);. br
18c6f 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
18c70 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 case P4_FUNCD
18c71 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 EF: {. fr
18c72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 eeEphemeralFunct
18c73 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 ion(db, (FuncDef
18c74 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 *)p4);. b
18c75 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
18c76 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a case P4_MEM:
18c77 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 {. sqlit
18c78 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c e3ValueFree((sql
18c79 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b ite3_value*)p4);
18c7a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
18c7b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
18c7c 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}.../*.** Chan
18c7d 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 ge N opcodes sta
18c7e 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f rting at addr to
18c7f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 No-ops..*/.SQLI
18c80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
18c81 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
18c82 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c eToNoop(Vdbe *p,
18c83 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e int addr, int N
18c84 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d ){. if( p && p-
18c85 3e 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65 >aOp ){. Vdbe
18c86 4f 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f Op *pOp = &p->aO
18c87 70 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c p[addr];. sql
18c88 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
18c89 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d ;. while( N--
18c8a 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 ){. freeP4
18c8b 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 (db, pOp->p4type
18c8c 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 , pOp->p4.p);.
18c8d 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 memset(pOp,
18c8e 30 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 0, sizeof(pOp[0]
18c8f 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f ));. pOp->o
18c90 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b pcode = OP_Noop;
18c91 0a 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 . pOp++;.
18c92 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
18c93 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 Change the valu
18c94 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72 e of the P4 oper
18c95 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 and for a specif
18c96 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ic instruction..
18c97 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
18c98 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 is useful when a
18c99 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 large program i
18c9a 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a s loaded from a.
18c9b 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 ** static array
18c9c 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 using sqlite3Vdb
18c9d 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 eAddOpList but w
18c9e 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 e want to make a
18c9f 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 .** few minor ch
18ca0 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f anges to the pro
18ca1 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e gram..**.** If n
18ca2 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 20 >=0 then the P4
18ca3 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 6d operand is dynam
18ca4 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 ic, meaning that
18ca5 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 a copy of.** th
18ca6 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 65 e string is made
18ca7 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 into memory obt
18ca8 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
18ca9 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 e3_malloc()..**
18caa 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 A value of n==0
18cab 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 73 means copy bytes
18cac 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 6e of zP4 up to an
18cad 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a d including the.
18cae 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 79 ** first null by
18caf 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 6e te. If n>0 then
18cb0 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 20 copy n+1 bytes
18cb1 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 of zP4..**.** If
18cb2 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69 n==P4_KEYINFO i
18cb3 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 34 t means that zP4
18cb4 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
18cb5 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
18cb6 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 ture..** A copy
18cb7 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 4b is made of the K
18cb8 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
18cb9 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 into memory obt
18cba 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 ained from.** sq
18cbb 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f lite3_malloc, to
18cbc 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 74 be freed when t
18cbd 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c he Vdbe is final
18cbe 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b ized..** n==P4_K
18cbf 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 EYINFO_HANDOFF i
18cc0 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a 50 ndicates that zP
18cc1 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 4 points to a Ke
18cc2 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a yInfo structure.
18cc3 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d ** stored in mem
18cc4 6f 72 79 20 74 68 61 74 20 74 68 65 20 63 61 6c ory that the cal
18cc5 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 ler has obtained
18cc6 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 from sqlite3_ma
18cc7 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61 lloc. The .** ca
18cc8 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ller should not
18cc9 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 free the allocat
18cca 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 ion, it will be
18ccb 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 freed when the V
18ccc 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 dbe is.** finali
18ccd 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 zed..** .** Othe
18cce 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50 r values of n (P
18ccf 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4_STATIC, P4_COL
18cd0 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63 LSEQ etc.) indic
18cd1 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69 ate that zP4 poi
18cd2 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69 nts.** to a stri
18cd3 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20 ng or structure
18cd4 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65 that is guarante
18cd5 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20 ed to exist for
18cd6 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a the lifetime of.
18cd7 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20 ** the Vdbe. In
18cd8 74 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63 these cases we c
18cd9 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65 an just copy the
18cda 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
18cdb 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63 If addr<0 then c
18cdc 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 20 hange P4 on the
18cdd 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e most recently in
18cde 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69 serted instructi
18cdf 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
18ce0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
18ce1 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 e3VdbeChangeP4(V
18ce2 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 dbe *p, int addr
18ce3 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 , const char *zP
18ce4 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 4, int n){. Op
18ce5 2a 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 *pOp;. sqlite3
18ce6 2a 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 *db;. assert( p
18ce7 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d !=0 );. db = p-
18ce8 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 >db;. assert( p
18ce9 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
18cea 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 GIC_INIT );. if
18ceb 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 ( p->aOp==0 || d
18cec 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
18ced 29 7b 0a 20 20 20 20 69 66 20 28 6e 20 21 3d 20 ){. if (n !=
18cee 50 34 5f 4b 45 59 49 4e 46 4f 29 20 7b 0a 20 20 P4_KEYINFO) {.
18cef 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 6e freeP4(db, n
18cf0 2c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a , (void*)*(char*
18cf1 2a 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20 *)&zP4);. }.
18cf2 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
18cf3 20 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d assert( addr<p-
18cf4 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64 >nOp );. if( ad
18cf5 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72 dr<0 ){. addr
18cf6 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 = p->nOp - 1;.
18cf7 20 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 20 if( addr<0 )
18cf8 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 4f return;. }. pO
18cf9 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 p = &p->aOp[addr
18cfa 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 62 2c 20 ];. freeP4(db,
18cfb 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 pOp->p4type, pOp
18cfc 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d 3e ->p4.p);. pOp->
18cfd 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 p4.p = 0;. if(
18cfe 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 n==P4_INT32 ){.
18cff 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69 73 /* Note: this
18d00 20 63 61 73 74 20 69 73 20 73 61 66 65 2c 20 62 cast is safe, b
18d01 65 63 61 75 73 65 20 74 68 65 20 6f 72 69 67 69 ecause the origi
18d02 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61 73 n data point was
18d03 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20 74 an int. ** t
18d04 68 61 74 20 77 61 73 20 63 61 73 74 20 74 6f 20 hat was cast to
18d05 61 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 a (const char *)
18d06 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70 34 . */. pOp->p4
18d07 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f .i = SQLITE_PTR_
18d08 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 20 20 TO_INT(zP4);.
18d09 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
18d0a 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 6c 73 65 4_INT32;. }else
18d0b 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 if( zP4==0 ){.
18d0c 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 pOp->p4.p = 0
18d0d 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 ;. pOp->p4typ
18d0e 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a e = P4_NOTUSED;.
18d0f 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 }else if( n==P
18d10 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 4_KEYINFO ){.
18d11 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
18d12 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 65 fo;. int nFie
18d13 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 20 ld, nByte;..
18d14 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 6e nField = ((KeyIn
18d15 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c 64 fo*)zP4)->nField
18d16 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69 ;. nByte = si
18d17 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 20 zeof(*pKeyInfo)
18d18 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 7a + (nField-1)*siz
18d19 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 eof(pKeyInfo->aC
18d1a 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c 64 oll[0]) + nField
18d1b 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d ;. pKeyInfo =
18d1c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 sqlite3Malloc(
18d1d 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f 70 nByte );. pOp
18d1e 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 ->p4.pKeyInfo =
18d1f 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66 pKeyInfo;. if
18d20 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 ( pKeyInfo ){.
18d21 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 u8 *aSortOrd
18d22 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 er;. memcpy
18d23 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c 20 (pKeyInfo, zP4,
18d24 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 53 nByte);. aS
18d25 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49 ortOrder = pKeyI
18d26 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b nfo->aSortOrder;
18d27 0a 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 74 . if( aSort
18d28 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20 Order ){.
18d29 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 pKeyInfo->aSort
18d2a 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e 65 Order = (unsigne
18d2b 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e 66 d char*)&pKeyInf
18d2c 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 5d o->aColl[nField]
18d2d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 ;. memcpy
18d2e 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 (pKeyInfo->aSort
18d2f 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64 65 Order, aSortOrde
18d30 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20 r, nField);.
18d31 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 }. pOp->p
18d32 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 4type = P4_KEYIN
18d33 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 FO;. }else{.
18d34 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c p->db->mall
18d35 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
18d36 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
18d37 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 = P4_NOTUSED;.
18d38 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
18d39 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 n==P4_KEYINFO_HA
18d3a 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f 70 NDOFF ){. pOp
18d3b 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 ->p4.p = (void*)
18d3c 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 zP4;. pOp->p4
18d3d 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 46 type = P4_KEYINF
18d3e 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e O;. }else if( n
18d3f 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 <0 ){. pOp->p
18d40 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 4.p = (void*)zP4
18d41 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 ;. pOp->p4typ
18d42 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 e = (signed char
18d43 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 )n;. }else{.
18d44 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20 if( n==0 ) n =
18d45 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
18d46 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 zP4);. pOp->p
18d47 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 4.z = sqlite3DbS
18d48 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50 trNDup(p->db, zP
18d49 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 4, n);. pOp->
18d4a 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41 p4type = P4_DYNA
18d4b 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e MIC;. }.}..#ifn
18d4c 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a def NDEBUG./*.**
18d4d 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d Change the comm
18d4e 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 20 6d ent on the the m
18d4f 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 ost recently cod
18d50 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 ed instruction.
18d51 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20 Or.** insert a
18d52 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68 No-op and add th
18d53 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61 e comment to tha
18d54 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f t new instructio
18d55 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 6b 65 n. This.** make
18d56 73 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65 s the code easie
18d57 72 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67 r to read during
18d58 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e debugging. Non
18d59 65 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e e of this happen
18d5a 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63 s.** in a produc
18d5b 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f 0a 53 tion build..*/.S
18d5c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
18d5d 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f id sqlite3VdbeCo
18d5e 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 mment(Vdbe *p, c
18d5f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
18d60 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
18d61 69 73 74 20 61 70 3b 0a 20 20 61 73 73 65 72 74 ist ap;. assert
18d62 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d ( p->nOp>0 || p-
18d63 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 >aOp==0 );. ass
18d64 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c ert( p->aOp==0 |
18d65 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d | p->aOp[p->nOp-
18d66 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 1].zComment==0 |
18d67 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 | p->db->mallocF
18d68 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 ailed );. if( p
18d69 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 ->nOp ){. cha
18d6a 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 r **pz = &p->aOp
18d6b 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d [p->nOp-1].zComm
18d6c 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 ent;. va_star
18d6d 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
18d6e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
18d6f 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 e(p->db, *pz);.
18d70 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33 *pz = sqlite3
18d71 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 VMPrintf(p->db,
18d72 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
18d73 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
18d74 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 }.}.SQLITE_PRIVA
18d75 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
18d76 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 dbeNoopComment(V
18d77 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 dbe *p, const ch
18d78 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
18d79 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
18d7a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
18d7b 64 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 dOp0(p, OP_Noop)
18d7c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
18d7d 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d Op>0 || p->aOp==
18d7e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
18d7f 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 ->aOp==0 || p->a
18d80 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f Op[p->nOp-1].zCo
18d81 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 mment==0 || p->d
18d82 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
18d83 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 );. if( p->nOp
18d84 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a ){. char **pz
18d85 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f = &p->aOp[p->nO
18d86 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 p-1].zComment;.
18d87 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 va_start(ap,
18d88 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 zFormat);. sq
18d89 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 lite3DbFree(p->d
18d8a 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a b, *pz);. *pz
18d8b 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
18d8c 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 tf(p->db, zForma
18d8d 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 t, ap);. va_e
18d8e 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 nd(ap);. }.}.#e
18d8f 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 ndif /* NDEBUG
18d90 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e */../*.** Return
18d91 20 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 the opcode for
18d92 61 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e a given address.
18d93 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18d94 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 TE VdbeOp *sqlit
18d95 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 e3VdbeGetOp(Vdbe
18d96 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a *p, int addr){.
18d97 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
18d98 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
18d99 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 NIT );. assert(
18d9a 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64 (addr>=0 && add
18d9b 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e r<p->nOp) || p->
18d9c 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
18d9d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 61 );. return ((a
18d9e 64 64 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70 ddr>=0 && addr<p
18d9f 2d 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61 4f 70 5b ->nOp)?(&p->aOp[
18da0 61 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a 0a 23 69 addr]):0);.}..#i
18da1 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
18da2 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 E_OMIT_EXPLAIN)
18da3 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 || !defined(NDEB
18da4 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65 UG) \. || de
18da5 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 fined(VDBE_PROFI
18da6 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 LE) || defined(S
18da7 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a QLITE_DEBUG)./*.
18da8 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 ** Compute a str
18da9 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62 ing that describ
18daa 65 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 es the P4 parame
18dab 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64 ter for an opcod
18dac 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20 e..** Use zTemp
18dad 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64 for any required
18dae 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 temporary buffe
18daf 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 r space..*/.stat
18db0 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79 ic char *display
18db1 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72 P4(Op *pOp, char
18db2 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65 *zTemp, int nTe
18db3 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 mp){. char *zP4
18db4 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65 = zTemp;. asse
18db5 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b rt( nTemp>=20 );
18db6 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e . switch( pOp->
18db7 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 p4type ){. ca
18db8 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 se P4_KEYINFO_ST
18db9 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50 ATIC:. case P
18dba 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 4_KEYINFO: {.
18dbb 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 int i, j;.
18dbc 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 KeyInfo *pKey
18dbd 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Info = pOp->p4.p
18dbe 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 KeyInfo;. s
18dbf 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
18dc0 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b nTemp, zTemp, "k
18dc1 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79 eyinfo(%d", pKey
18dc2 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20 Info->nField);.
18dc3 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33 i = sqlite3
18dc4 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b Strlen30(zTemp);
18dc5 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 . for(j=0;
18dc6 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 j<pKeyInfo->nFie
18dc7 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ld; j++){.
18dc8 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
18dc9 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f = pKeyInfo->aCo
18dca 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 ll[j];. i
18dcb 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 f( pColl ){.
18dcc 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 int n = sq
18dcd 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43 lite3Strlen30(pC
18dce 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 oll->zName);.
18dcf 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e if( i+n>n
18dd0 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 Temp-6 ){.
18dd1 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 memcpy(&zT
18dd2 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29 emp[i],",...",4)
18dd3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 ;. br
18dd4 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d eak;. }
18dd5 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 . zTemp
18dd6 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20 [i++] = ',';.
18dd7 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 if( pKeyI
18dd8 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 nfo->aSortOrder
18dd9 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f && pKeyInfo->aSo
18dda 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 rtOrder[j] ){.
18ddb 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b zTemp[
18ddc 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 i++] = '-';.
18ddd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
18dde 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b memcpy(&zTemp[
18ddf 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 i], pColl->zName
18de0 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 ,n+1);.
18de1 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 i += n;.
18de2 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e }else if( i+4<n
18de3 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 Temp-6 ){.
18de4 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d memcpy(&zTem
18de5 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a p[i],",nil",4);.
18de6 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 i += 4
18de7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
18de8 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b }. zTemp[
18de9 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 i++] = ')';.
18dea 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a zTemp[i] = 0;.
18deb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c assert( i<
18dec 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62 nTemp );. b
18ded 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
18dee 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a case P4_COLLSEQ:
18def 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 {. CollSeq
18df0 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70 *pColl = pOp->p
18df1 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73 4.pColl;. s
18df2 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
18df3 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63 nTemp, zTemp, "c
18df4 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20 ollseq(%.20s)",
18df5 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pColl->zName);.
18df6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
18df7 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 }. case P4_FU
18df8 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46 NCDEF: {. F
18df9 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70 uncDef *pDef = p
18dfa 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 Op->p4.pFunc;.
18dfb 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
18dfc 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
18dfd 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65 p, "%s(%d)", pDe
18dfe 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e f->zName, pDef->
18dff 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 nArg);. bre
18e00 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
18e01 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20 se P4_INT64: {.
18e02 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
18e03 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
18e04 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70 mp, "%lld", *pOp
18e05 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20 ->p4.pI64);.
18e06 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
18e07 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32 case P4_INT32
18e08 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
18e09 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
18e0a 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70 , zTemp, "%d", p
18e0b 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20 Op->p4.i);.
18e0c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
18e0d 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20 case P4_REAL:
18e0e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
18e0f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
18e10 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 zTemp, "%.16g",
18e11 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b *pOp->p4.pReal);
18e12 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
18e13 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
18e14 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d MEM: {. Mem
18e15 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34 *pMem = pOp->p4
18e16 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 .pMem;. ass
18e17 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 ert( (pMem->flag
18e18 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
18e19 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d );. if( pM
18e1a 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
18e1b 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 7a Str ){. z
18e1c 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 P4 = pMem->z;.
18e1d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d }else if( pM
18e1e 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
18e1f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Int ){. s
18e20 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
18e21 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 nTemp, zTemp, "%
18e22 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 lld", pMem->u.i)
18e23 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
18e24 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
18e25 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
18e26 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
18e27 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
18e28 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65 6d p, "%.16g", pMem
18e29 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ->r);. }.
18e2a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
18e2b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18e2c 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
18e2d 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 54 E. case P4_VT
18e2e 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 AB: {. sqli
18e2f 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 te3_vtab *pVtab
18e30 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b = pOp->p4.pVtab;
18e31 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
18e32 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
18e33 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a 25 Temp, "vtab:%p:%
18e34 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61 62 p", pVtab, pVtab
18e35 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 ->pModule);.
18e36 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
18e37 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 50 endif. case P
18e38 34 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20 4_INTARRAY: {.
18e39 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
18e3a 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
18e3b 70 2c 20 22 69 6e 74 61 72 72 61 79 22 29 3b 0a p, "intarray");.
18e3c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
18e3d 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
18e3e 7b 0a 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4f {. zP4 = pO
18e3f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69 p->p4.z;. i
18e40 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 f( zP4==0 ){.
18e41 20 20 20 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70 zP4 = zTemp
18e42 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b ;. zTemp[
18e43 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0] = 0;. }.
18e44 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 }. }. asse
18e45 72 74 28 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20 rt( zP4!=0 );.
18e46 72 65 74 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65 return zP4;.}.#e
18e47 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c ndif../*.** Decl
18e48 61 72 65 20 74 6f 20 74 68 65 20 56 64 62 65 20 are to the Vdbe
18e49 74 68 61 74 20 74 68 65 20 42 54 72 65 65 20 6f that the BTree o
18e4a 62 6a 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62 bject at db->aDb
18e4b 5b 69 5d 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a [i] is used..**.
18e4c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18e4d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
18e4e 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 beUsesBtree(Vdbe
18e4f 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 *p, int i){. i
18e50 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 73 65 72 nt mask;. asser
18e51 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e t( i>=0 && i<p->
18e52 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 db->nDb );. ass
18e53 65 72 74 28 20 69 3c 28 69 6e 74 29 73 69 7a 65 ert( i<(int)size
18e54 6f 66 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 29 of(p->btreeMask)
18e55 2a 38 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 31 *8 );. mask = 1
18e56 3c 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 <<i;. if( (p->b
18e57 74 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 treeMask & mask)
18e58 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 ==0 ){. p->bt
18e59 72 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b reeMask |= mask;
18e5a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
18e5b 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 eMutexArrayInser
18e5c 74 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d t(&p->aMutex, p-
18e5d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 >db->aDb[i].pBt)
18e5e 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 ;. }.}...#if de
18e5f 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 fined(VDBE_PROFI
18e60 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 LE) || defined(S
18e61 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a QLITE_DEBUG)./*.
18e62 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c ** Print a singl
18e63 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20 e opcode. This
18e64 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
18e65 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e for debugging on
18e66 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
18e67 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
18e68 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 e3VdbePrintOp(FI
18e69 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 LE *pOut, int pc
18e6a 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 , Op *pOp){. ch
18e6b 61 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 ar *zP4;. char
18e6c 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 zPtr[50];. stat
18e6d 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a ic const char *z
18e6e 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25 Format1 = "%4d %
18e6f 2d 31 33 73 20 25 34 64 20 25 34 64 20 25 34 64 -13s %4d %4d %4d
18e70 20 25 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 %-4s %.2X %s\n"
18e71 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 ;. if( pOut==0
18e72 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b ) pOut = stdout;
18e73 0a 20 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 . zP4 = display
18e74 50 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 P4(pOp, zPtr, si
18e75 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 zeof(zPtr));. f
18e76 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f printf(pOut, zFo
18e77 72 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 rmat1, pc, .
18e78 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e sqlite3OpcodeN
18e79 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 ame(pOp->opcode)
18e7a 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e , pOp->p1, pOp->
18e7b 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 p2, pOp->p3, zP4
18e7c 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 , pOp->p5,.#ifde
18e7d 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
18e7e 20 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 pOp->zComme
18e7f 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 nt ? pOp->zComme
18e80 6e 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 nt : "".#else.
18e81 20 20 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 "".#endif.
18e82 29 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74 );. fflush(pOut
18e83 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a );.}.#endif../*.
18e84 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72 ** Release an ar
18e85 72 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 ray of N Mem ele
18e86 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 ments.*/.static
18e87 76 6f 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 void releaseMemA
18e88 72 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 rray(Mem *p, int
18e89 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 N){. if( p &&
18e8a 4e 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 N ){. Mem *pE
18e8b 6e 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 nd;. sqlite3
18e8c 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 *db = p->db;.
18e8d 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 u8 malloc_faile
18e8e 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 d = db->mallocFa
18e8f 69 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 iled;. for(pE
18e90 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 nd=&p[N]; p<pEnd
18e91 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 ; p++){. as
18e92 73 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 sert( (&p[1])==p
18e93 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d End || p[0].db==
18e94 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 p[1].db );..
18e95 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 /* This block
18e96 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c is really an inl
18e97 69 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 ined version of
18e98 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
18e99 6c 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a lease(). **
18e9a 20 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61 that takes adva
18e9b 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63 ntage of the fac
18e9c 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 t that the memor
18e9d 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 y cell value is
18e9e 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 . ** being
18e9f 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 set to NULL afte
18ea0 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 r releasing any
18ea1 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 dynamic resource
18ea2 73 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 s.. **.
18ea3 20 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69 ** The justifi
18ea4 63 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 cation for dupli
18ea5 63 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74 cating code is t
18ea6 68 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f hat according to
18ea7 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 . ** callg
18ea8 72 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65 rind, this cause
18ea9 73 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 74 s a certain test
18eaa 20 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 65 case to hit the
18eab 20 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20 CPU 4.7 .
18eac 2a 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20 ** percent less
18ead 28 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 (x86 linux, gcc
18eae 76 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d version 4.1.2, -
18eaf 4f 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20 O6) than if .
18eb0 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d ** sqlite3Mem
18eb1 52 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 63 Release() were c
18eb2 61 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e alled from here.
18eb3 20 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 With -O2, this
18eb4 6a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 jumps. ** t
18eb5 6f 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 o 6.6 percent. T
18eb6 68 65 20 74 65 73 74 20 63 61 73 65 20 69 73 20 he test case is
18eb7 69 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 72 inserting 1000 r
18eb8 6f 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 ows into a table
18eb9 20 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 . ** with
18eba 6e 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67 no indexes using
18ebb 20 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 a single prepar
18ebc 65 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d ed INSERT statem
18ebd 65 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 ent, bind() .
18ebe 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 ** and reset(
18ebf 29 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 67 ). Inserts are g
18ec0 72 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72 rouped into a tr
18ec1 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 ansaction..
18ec2 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d */. if( p-
18ec3 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c >flags&(MEM_Agg|
18ec4 4d 45 4d 5f 44 79 6e 29 20 29 7b 0a 20 20 20 20 MEM_Dyn) ){.
18ec5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
18ec6 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 20 emRelease(p);.
18ec7 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d }else if( p-
18ec8 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 >zMalloc ){.
18ec9 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
18eca 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 e(db, p->zMalloc
18ecb 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 4d );. p->zM
18ecc 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 alloc = 0;.
18ecd 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66 6c 61 }.. p->fla
18ece 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
18ecf 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c }. db->mal
18ed0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 6c 6c locFailed = mall
18ed1 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 0a 7d oc_failed;. }.}
18ed2 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
18ed3 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
18ed4 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f NAGEMENT.SQLITE_
18ed5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18ed6 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 te3VdbeReleaseBu
18ed7 66 66 65 72 73 28 56 64 62 65 20 2a 70 29 7b 0a ffers(Vdbe *p){.
18ed8 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 int ii;. int
18ed9 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 61 73 73 nFree = 0;. ass
18eda 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
18edb 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
18edc 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 utex) );. for(i
18edd 69 3d 31 3b 20 69 69 3c 3d 70 2d 3e 6e 4d 65 6d i=1; ii<=p->nMem
18ede 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d ; ii++){. Mem
18edf 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 *pMem = &p->aMe
18ee0 6d 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 m[ii];. if( p
18ee1 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
18ee2 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 20 _RowSet ){.
18ee3 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c sqlite3RowSetCl
18ee4 65 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 ear(pMem->u.pRow
18ee5 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Set);. }.
18ee6 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 if( pMem->z && p
18ee7 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 Mem->flags&MEM_D
18ee8 79 6e 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 yn ){. asse
18ee9 72 74 28 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 rt( !pMem->xDel
18eea 29 3b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b );. nFree +
18eeb 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
18eec 63 53 69 7a 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 cSize(pMem->db,
18eed 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 pMem->z);.
18eee 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
18eef 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 lease(pMem);.
18ef0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
18ef1 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a nFree;.}.#endif.
18ef2 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18ef3 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a OMIT_EXPLAIN./*.
18ef4 2a 2a 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e ** Give a listin
18ef5 67 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d g of the program
18ef6 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 in the virtual
18ef7 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 machine..**.** T
18ef8 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 he interface is
18ef9 74 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 the same as sqli
18efa 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 te3VdbeExec().
18efb 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a But instead of.*
18efc 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f * running the co
18efd 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20 74 de, it invokes t
18efe 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 he callback once
18eff 20 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72 75 for each instru
18f00 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 ction..** This f
18f01 65 61 74 75 72 65 20 69 73 20 75 73 65 64 20 74 eature is used t
18f02 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50 o implement "EXP
18f03 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 LAIN"..**.** Whe
18f04 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c n p->explain==1,
18f05 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f each instructio
18f06 6e 20 69 73 20 6c 69 73 74 65 64 2e 20 20 57 68 n is listed. Wh
18f07 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e en.** p->explain
18f08 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70 ==2, only OP_Exp
18f09 6c 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e lain instruction
18f0a 73 20 61 72 65 20 6c 69 73 74 65 64 20 61 6e 64 s are listed and
18f0b 20 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 73 68 these.** are sh
18f0c 6f 77 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 own in a differe
18f0d 6e 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65 nt format. p->e
18f0e 78 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 73 65 xplain==2 is use
18f0f 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a d to implement.*
18f10 2a 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 * EXPLAIN QUERY
18f11 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f PLAN..*/.SQLITE_
18f12 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18f13 74 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56 te3VdbeList(. V
18f14 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 dbe *p
18f15 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
18f16 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c VDBE */.){. sql
18f17 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
18f18 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 ;. int i;. int
18f19 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
18f1a 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 . Mem *pMem = p
18f1b 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 ->pResultSet = &
18f1c 70 2d 3e 61 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61 p->aMem[1];.. a
18f1d 73 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 ssert( p->explai
18f1e 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 n );. if( p->ma
18f1f 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic!=VDBE_MAGIC_
18f20 52 55 4e 20 29 20 72 65 74 75 72 6e 20 53 51 4c RUN ) return SQL
18f21 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 61 73 ITE_MISUSE;. as
18f22 73 65 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d sert( db->magic=
18f23 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 =SQLITE_MAGIC_BU
18f24 53 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 SY );. assert(
18f25 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->rc==SQLITE_OK
18f26 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 || p->rc==SQLIT
18f27 45 5f 42 55 53 59 20 7c 7c 20 70 2d 3e 72 63 3d E_BUSY || p->rc=
18f28 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
18f29 0a 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 .. /* Even thou
18f2a 67 68 20 74 68 69 73 20 6f 70 63 6f 64 65 20 64 gh this opcode d
18f2b 6f 65 73 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 oes not use dyna
18f2c 6d 69 63 20 73 74 72 69 6e 67 73 20 66 6f 72 0a mic strings for.
18f2d 20 20 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c ** the result,
18f2e 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 result columns
18f2f 6d 61 79 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d may become dynam
18f30 69 63 20 69 66 20 74 68 65 20 75 73 65 72 20 63 ic if the user c
18f31 61 6c 6c 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 alls. ** sqlite
18f32 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
18f33 29 2c 20 63 61 75 73 69 6e 67 20 61 20 74 72 61 ), causing a tra
18f34 6e 73 6c 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d nslation to UTF-
18f35 31 36 20 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 16 encoding.. *
18f36 2f 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 /. releaseMemAr
18f37 72 61 79 28 70 4d 65 6d 2c 20 70 2d 3e 6e 4d 65 ray(pMem, p->nMe
18f38 6d 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 63 m);.. if( p->rc
18f39 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
18f3a 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 {. /* This ha
18f3b 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f ppens if a mallo
18f3c 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c c() inside a cal
18f3d 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c l to sqlite3_col
18f3e 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 umn_text() or.
18f3f 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c ** sqlite3_col
18f40 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 umn_text16() fai
18f41 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 64 62 2d led. */. db-
18f42 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
18f43 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 1;. return SQ
18f44 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
18f45 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20 3d 20 70 . do{. i = p
18f46 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 ->pc++;. }while
18f47 28 20 69 3c 70 2d 3e 6e 4f 70 20 26 26 20 70 2d ( i<p->nOp && p-
18f48 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 20 70 >explain==2 && p
18f49 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 21 ->aOp[i].opcode!
18f4a 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b 0a 20 =OP_Explain );.
18f4b 20 69 66 28 20 69 3e 3d 70 2d 3e 6e 4f 70 20 29 if( i>=p->nOp )
18f4c 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
18f4d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 LITE_OK;. rc
18f4e 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 = SQLITE_DONE;.
18f4f 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 }else if( db->u
18f50 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 1.isInterrupted
18f51 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 ){. p->rc = S
18f52 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b QLITE_INTERRUPT;
18f53 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
18f54 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 _ERROR;. sqli
18f55 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
18f56 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
18f57 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 s", sqlite3ErrSt
18f58 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c r(p->rc));. }el
18f59 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b se{. char *z;
18f5a 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d 20 26 . Op *pOp = &
18f5b 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 69 p->aOp[i];. i
18f5c 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 f( p->explain==1
18f5d 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e ){. pMem->
18f5e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
18f5f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 . pMem->typ
18f60 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 e = SQLITE_INTEG
18f61 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e ER;. pMem->
18f62 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20 20 20 u.i = i;
18f63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18f64 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 /* Progr
18f65 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 am counter */.
18f66 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20 pMem++;. .
18f67 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
18f68 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 = MEM_Static|ME
18f69 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
18f6a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 pMem->z =
18f6b 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 4f 70 (char*)sqlite3Op
18f6c 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 codeName(pOp->op
18f6d 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64 code); /* Opcod
18f6e 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 e */. asser
18f6f 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b t( pMem->z!=0 );
18f70 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d . pMem->n =
18f71 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
18f72 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 (pMem->z);.
18f73 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
18f74 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 LITE_TEXT;.
18f75 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
18f76 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 ITE_UTF8;.
18f77 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 pMem++;. }..
18f78 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
18f79 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d MEM_Int;. pM
18f7a 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 em->u.i = pOp->p
18f7b 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
18f7c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
18f7d 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 1 */. pMem->t
18f7e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 ype = SQLITE_INT
18f7f 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b EGER;. pMem++
18f80 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ;.. pMem->fla
18f81 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
18f82 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f pMem->u.i = pO
18f83 70 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20 p->p2;
18f84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18f85 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 /* P2 */. pMe
18f86 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
18f87 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d _INTEGER;. pM
18f88 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 em++;.. if( p
18f89 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a ->explain==1 ){.
18f8a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
18f8b 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
18f8c 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 pMem->u.i = p
18f8d 4f 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20 Op->p3;
18f8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18f8f 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 /* P3 */.
18f90 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
18f91 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 ITE_INTEGER;.
18f92 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d pMem++;. }
18f93 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 .. if( sqlite
18f94 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
18f95 6d 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 m, 32, 0) ){
18f96 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f /* P4 */
18f97 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
18f98 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
18f99 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ed );. retu
18f9a 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
18f9b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
18f9c 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e >flags = MEM_Dyn
18f9d 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 |MEM_Str|MEM_Ter
18f9e 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c m;. z = displ
18f9f 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e ayP4(pOp, pMem->
18fa0 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 z, 32);. if(
18fa1 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 z!=pMem->z ){.
18fa2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
18fa3 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a emSetStr(pMem, z
18fa4 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 , -1, SQLITE_UTF
18fa5 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 8, 0);. }else
18fa6 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
18fa7 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 pMem->z!=0 );.
18fa8 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 pMem->n = sq
18fa9 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d lite3Strlen30(pM
18faa 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d em->z);. pM
18fab 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 em->enc = SQLITE
18fac 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 _UTF8;. }.
18fad 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
18fae 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 LITE_TEXT;. p
18faf 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 Mem++;.. if(
18fb0 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b p->explain==1 ){
18fb1 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
18fb2 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
18fb3 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 em, 4, 0) ){.
18fb4 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
18fb5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
18fb6 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 );. retu
18fb7 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
18fb8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
18fb9 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
18fba 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d _Dyn|MEM_Str|MEM
18fbb 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 _Term;. pMe
18fbc 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 m->n = 2;.
18fbd 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
18fbe 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e (3, pMem->z, "%.
18fbf 32 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 2x", pOp->p5);
18fc0 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 /* P5 */.
18fc1 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
18fc2 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 ITE_TEXT;.
18fc3 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
18fc4 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 TE_UTF8;. p
18fc5 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 Mem++;. .#ifdef
18fc6 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
18fc7 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f if( pOp->zCo
18fc8 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 mment ){.
18fc9 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
18fca 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b EM_Str|MEM_Term;
18fcb 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a . pMem->z
18fcc 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 = pOp->zComment
18fcd 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e ;. pMem->
18fce 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
18fcf 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 n30(pMem->z);.
18fd0 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 pMem->enc
18fd1 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 = SQLITE_UTF8;.
18fd2 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 pMem->typ
18fd3 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b e = SQLITE_TEXT;
18fd4 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e . }else.#en
18fd5 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 dif. {.
18fd6 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
18fd7 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 = MEM_Null;
18fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18fd9 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a /* Comment */.
18fda 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 pMem->ty
18fdb 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c pe = SQLITE_NULL
18fdc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
18fdd 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 . p->nResColu
18fde 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 mn = 8 - 5*(p->e
18fdf 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 xplain-1);. p
18fe0 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b ->rc = SQLITE_OK
18fe1 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
18fe2 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 E_ROW;. }. ret
18fe3 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
18fe4 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
18fe5 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 EXPLAIN */..#ifd
18fe6 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
18fe7 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 /*.** Print the
18fe8 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65 SQL that was use
18fe9 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 d to generate a
18fea 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f VDBE program..*/
18feb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18fec 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
18fed 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 PrintSql(Vdbe *p
18fee 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 ){. int nOp = p
18fef 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 ->nOp;. VdbeOp
18ff0 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c *pOp;. if( nOp<
18ff1 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 1 ) return;. pO
18ff2 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a p = &p->aOp[0];.
18ff3 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
18ff4 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 e==OP_Trace && p
18ff5 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 Op->p4.z!=0 ){.
18ff6 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
18ff7 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 = pOp->p4.z;.
18ff8 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
18ff9 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b Isspace(*z) ) z+
18ffa 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 +;. printf("S
18ffb 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b QL: [%s]\n", z);
18ffc 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 . }.}.#endif..#
18ffd 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
18ffe 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 TE_OMIT_TRACE) &
18fff 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
19000 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 _ENABLE_IOTRACE)
19001 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 ./*.** Print an
19002 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 IOTRACE message
19003 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 showing SQL cont
19004 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ent..*/.SQLITE_P
19005 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
19006 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 te3VdbeIOTraceSq
19007 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e l(Vdbe *p){. in
19008 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a t nOp = p->nOp;.
19009 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 VdbeOp *pOp;.
1900a 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 if( sqlite3IoTr
1900b 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ace==0 ) return;
1900c 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 . if( nOp<1 ) r
1900d 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 eturn;. pOp = &
1900e 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 p->aOp[0];. if(
1900f 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
19010 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 _Trace && pOp->p
19011 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 4.z!=0 ){. in
19012 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 t i, j;. char
19013 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 z[1000];. sq
19014 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
19015 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 izeof(z), z, "%s
19016 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 ", pOp->p4.z);.
19017 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 for(i=0; sqli
19018 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 te3Isspace(z[i])
19019 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 ; i++){}. for
1901a 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 (j=0; z[i]; i++)
1901b 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 {. if( sqli
1901c 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 te3Isspace(z[i])
1901d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1901e 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 z[i-1]!=' ' ){.
1901f 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 z[j++]
19020 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d = ' ';. }
19021 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19022 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a z[j++] = z
19023 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 [i];. }.
19024 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b }. z[j] = 0;
19025 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 . sqlite3IoTr
19026 61 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 ace("SQL %s\n",
19027 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 z);. }.}.#endif
19028 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 /* !SQLITE_OMIT
19029 5f 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 _TRACE && SQLITE
1902a 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 _ENABLE_IOTRACE
1902b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 */../*.** Alloca
1902c 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20 te space from a
1902d 66 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65 fixed size buffe
1902e 72 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69 r. Make *pp poi
1902f 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c nt to the.** all
19030 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20 20 28 ocated space. (
19031 4e 6f 74 65 3a 20 20 70 70 20 69 73 20 61 20 63 Note: pp is a c
19032 68 61 72 2a 20 72 61 74 68 65 72 20 74 68 61 6e har* rather than
19033 20 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20 a void** to.**
19034 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 work around the
19035 70 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 6e 67 pointer aliasing
19036 20 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a rules of C.) *
19037 70 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 pp should initia
19038 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20 lly.** be zero.
19039 20 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a If *pp is not z
1903a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 ero, that means
1903b 74 68 61 74 20 74 68 65 20 73 70 61 63 65 20 68 that the space h
1903c 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 as already.** be
1903d 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 en allocated and
1903e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1903f 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e a noop..**.** n
19040 42 79 74 65 20 69 73 20 74 68 65 20 6e 75 6d 62 Byte is the numb
19041 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
19042 70 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a pace needed..**.
19043 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74 ** *ppFrom point
19044 20 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 73 70 to available sp
19045 61 63 65 20 61 6e 64 20 70 45 6e 64 20 70 6f 69 ace and pEnd poi
19046 6e 74 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f nts to the end o
19047 66 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62 f the.** availab
19048 6c 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 le space..**.**
19049 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75 *pnByte is a cou
1904a 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 nter of the numb
1904b 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
1904c 70 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66 pace that have f
1904d 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f ailed.** to allo
1904e 63 61 74 65 2e 20 20 49 66 20 74 68 65 72 65 20 cate. If there
1904f 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 20 is insufficient
19050 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d space in *ppFrom
19051 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a to satisfy the.
19052 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68 65 6e ** request, then
19053 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 increment *pnBy
19054 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75 6e 74 te by the amount
19055 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 2e of the request.
19056 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
19057 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 63 68 allocSpace(. ch
19058 61 72 20 2a 70 70 2c 20 20 20 20 20 20 20 20 20 ar *pp,
19059 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 /* IN/OUT: Se
1905a 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 t *pp to point t
1905b 6f 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 o allocated buff
1905c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 er */. int nByt
1905d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e, /*
1905e 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1905f 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 to allocate */.
19060 20 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 u8 **ppFrom,
19061 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a /* IN/OUT:
19062 20 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a Allocate from *
19063 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a ppFrom */. u8 *
19064 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 pEnd,
19065 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31 /* Pointer to 1
19066 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 byte past the e
19067 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75 nd of *ppFrom bu
19068 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 ffer */. int *p
19069 6e 42 79 74 65 20 20 20 20 20 20 20 20 20 20 2f nByte /
1906a 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 * If allocation
1906b 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20 cannot be made,
1906c 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 increment *pnByt
1906d 65 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 28 2a e */.){. if( (*
1906e 28 76 6f 69 64 2a 2a 29 70 70 29 3d 3d 30 20 29 (void**)pp)==0 )
1906f 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 52 4f {. nByte = RO
19070 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 20 UND8(nByte);.
19071 20 69 66 28 20 28 70 45 6e 64 20 2d 20 2a 70 70 if( (pEnd - *pp
19072 46 72 6f 6d 29 3e 3d 6e 42 79 74 65 20 29 7b 0a From)>=nByte ){.
19073 20 20 20 20 20 20 2a 28 76 6f 69 64 2a 2a 29 70 *(void**)p
19074 70 20 3d 20 28 76 6f 69 64 20 2a 29 2a 70 70 46 p = (void *)*ppF
19075 72 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 70 46 72 rom;. *ppFr
19076 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 om += nByte;.
19077 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 }else{. *p
19078 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74 65 3b 0a nByte += nByte;.
19079 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1907a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72 ** Prepare a vir
1907b 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72 tual machine for
1907c 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69 execution. Thi
1907d 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67 s involves thing
1907e 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c s such.** as all
1907f 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70 ocating stack sp
19080 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ace and initiali
19081 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d zing the program
19082 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 counter..** Aft
19083 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20 er the VDBE has
19084 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63 be prepped, it c
19085 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62 an be executed b
19086 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a y one or more.**
19087 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
19088 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a 3VdbeExec(). .*
19089 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 *.** This is the
1908a 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 only way to mov
1908b 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44 e a VDBE from VD
1908c 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f BE_MAGIC_INIT to
1908d 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 .** VDBE_MAGIC_R
1908e 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 UN..**.** This f
1908f 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 unction may be c
19090 61 6c 6c 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 alled more than
19091 6f 6e 63 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 once on a single
19092 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
19093 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 63 ..** The first c
19094 61 6c 6c 20 69 73 20 6d 61 64 65 20 77 68 69 6c all is made whil
19095 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 e compiling the
19096 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 53 SQL statement. S
19097 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c ubsequent.** cal
19098 6c 73 20 61 72 65 20 6d 61 64 65 20 61 73 20 70 ls are made as p
19099 61 72 74 20 6f 66 20 74 68 65 20 70 72 6f 63 65 art of the proce
1909a 73 73 20 6f 66 20 72 65 73 65 74 74 69 6e 67 20 ss of resetting
1909b 61 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 a statement to b
1909c 65 0a 2a 2a 20 72 65 2d 65 78 65 63 75 74 65 64 e.** re-executed
1909d 20 28 66 72 6f 6d 20 61 20 63 61 6c 6c 20 74 6f (from a call to
1909e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 sqlite3_reset()
1909f 29 2e 20 54 68 65 20 6e 56 61 72 2c 20 6e 4d 65 ). The nVar, nMe
190a0 6d 2c 20 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 61 m, nCursor .** a
190a1 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 70 61 72 nd isExplain par
190a2 61 6d 65 74 65 72 73 20 61 72 65 20 6f 6e 6c 79 ameters are only
190a3 20 70 61 73 73 65 64 20 63 6f 72 72 65 63 74 20 passed correct
190a4 76 61 6c 75 65 73 20 74 68 65 20 66 69 72 73 74 values the first
190a5 20 74 69 6d 65 0a 2a 2a 20 74 68 65 20 66 75 6e time.** the fun
190a6 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
190a7 20 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 On subsequent c
190a8 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 6c 69 74 alls, from sqlit
190a9 65 33 5f 72 65 73 65 74 28 29 2c 20 6e 56 61 72 e3_reset(), nVar
190aa 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 2d 31 .** is passed -1
190ab 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 and nMem, nCurs
190ac 6f 72 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e or and isExplain
190ad 20 61 72 65 20 61 6c 6c 20 70 61 73 73 65 64 20 are all passed
190ae 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f zero..*/.SQLITE_
190af 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
190b0 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 ite3VdbeMakeRead
190b1 79 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 y(. Vdbe *p,
190b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
190b3 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 /* The VDBE
190b4 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 */. int nVar,
190b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
190b6 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
190b7 20 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 '?' see in the
190b8 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f SQL statement */
190b9 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 . int nMem,
190ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
190bb 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d /* Number of m
190bc 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 emory cells to a
190bd 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 llocate */. int
190be 20 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 nCursor,
190bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
190c0 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 umber of cursors
190c1 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a to allocate */.
190c2 20 20 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 20 int isExplain
190c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
190c4 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
190c5 45 58 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 EXPLAIN keywords
190c6 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29 is present */.)
190c7 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c {. int n;. sql
190c8 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
190c9 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d ;.. assert( p!=
190ca 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
190cb 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
190cc 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f GIC_INIT );.. /
190cd 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 * There should b
190ce 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f e at least one o
190cf 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 pcode.. */. as
190d0 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 sert( p->nOp>0 )
190d1 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 ;.. /* Set the
190d2 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 magic to VDBE_MA
190d3 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 GIC_RUN sooner r
190d4 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 ather than later
190d5 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 . */. p->magic
190d6 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e = VDBE_MAGIC_RUN
190d7 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 ;.. /* For each
190d8 20 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 cursor required
190d9 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 , also allocate
190da 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d a memory cell. M
190db 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 emory. ** cells
190dc 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 (nMem+1-nCursor
190dd 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 )..nMem, inclusi
190de 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 ve, will never b
190df 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 e used by. ** t
190e0 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e he vdbe program.
190e1 20 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 Instead they ar
190e2 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 e used to alloca
190e3 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a te space for. *
190e4 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 * VdbeCursor/BtC
190e5 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 ursor structures
190e6 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 . The blob of me
190e7 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 mory associated
190e8 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f with . ** curso
190e9 72 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e r 0 is stored in
190ea 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 memory cell nMe
190eb 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 m. Memory cell (
190ec 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f nMem-1). ** sto
190ed 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 res the blob of
190ee 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 memory associate
190ef 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c d with cursor 1,
190f0 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 etc.. **. **
190f1 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 See also: alloca
190f2 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f teCursor().. */
190f3 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 . nMem += nCurs
190f4 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 or;.. /* Alloca
190f5 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d te space for mem
190f6 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c 20 53 ory registers, S
190f7 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 56 44 QL variables, VD
190f8 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64 20 0a BE cursors and .
190f9 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 74 6f ** an array to
190fa 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 75 6e marshal SQL fun
190fb 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 ction arguments
190fc 69 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 in. This is only
190fd 20 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a 20 66 done the. ** f
190fe 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 66 irst time this f
190ff 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
19100 64 20 66 6f 72 20 61 20 67 69 76 65 6e 20 56 44 d for a given VD
19101 42 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69 74 20 BE, not when it
19102 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 63 61 is. ** being ca
19103 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 lled from sqlite
19104 33 5f 72 65 73 65 74 28 29 20 74 6f 20 72 65 73 3_reset() to res
19105 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d et the virtual m
19106 61 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 achine.. */. i
19107 66 28 20 6e 56 61 72 3e 3d 30 20 26 26 20 21 64 f( nVar>=0 && !d
19108 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
19109 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 73 72 20 ){. u8 *zCsr
1910a 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b = (u8 *)&p->aOp[
1910b 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 38 20 p->nOp];. u8
1910c 2a 7a 45 6e 64 20 3d 20 28 75 38 20 2a 29 26 70 *zEnd = (u8 *)&p
1910d 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f ->aOp[p->nOpAllo
1910e 63 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 c];. int nByt
1910f 65 3b 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b e;. int nArg;
19110 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
19111 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 73 m number of args
19112 20 70 61 73 73 65 64 20 74 6f 20 61 20 75 73 65 passed to a use
19113 72 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20 r function. */.
19114 20 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 resolveP2Valu
19115 65 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 es(p, &nArg);.
19116 20 20 69 66 28 20 69 73 45 78 70 6c 61 69 6e 20 if( isExplain
19117 26 26 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 20 && nMem<10 ){.
19118 20 20 20 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 nMem = 10;.
19119 20 20 20 7d 0a 0a 20 20 20 20 64 6f 20 7b 0a 20 }.. do {.
1911a 20 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 memset(zCsr
1911b 2c 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b , 0, zEnd-zCsr);
1911c 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 30 . nByte = 0
1911d 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 ;. allocSpa
1911e 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 4d ce((char*)&p->aM
1911f 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 28 em, nMem*sizeof(
19120 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e Mem), &zCsr, zEn
19121 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 d, &nByte);.
19122 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 allocSpace((ch
19123 61 72 2a 29 26 70 2d 3e 61 56 61 72 2c 20 6e 56 ar*)&p->aVar, nV
19124 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 ar*sizeof(Mem),
19125 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 &zCsr, zEnd, &nB
19126 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f yte);. allo
19127 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 cSpace((char*)&p
19128 2d 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a 73 69 ->apArg, nArg*si
19129 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a 43 73 zeof(Mem*), &zCs
1912a 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 r, zEnd, &nByte)
1912b 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 ;. allocSpa
1912c 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 7a ce((char*)&p->az
1912d 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 Var, nVar*sizeof
1912e 28 63 68 61 72 2a 29 2c 20 26 7a 43 73 72 2c 20 (char*), &zCsr,
1912f 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 zEnd, &nByte);.
19130 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 allocSpace(
19131 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 43 73 72 (char*)&p->apCsr
19132 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
19133 20 20 20 20 6e 43 75 72 73 6f 72 2a 73 69 7a 65 nCursor*size
19134 6f 66 28 56 64 62 65 43 75 72 73 6f 72 2a 29 2c of(VdbeCursor*),
19135 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e &zCsr, zEnd, &n
19136 42 79 74 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 Byte. );.
19137 20 20 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b if( nByte ){
19138 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 72 65 . p->pFre
19139 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c e = sqlite3DbMal
1913a 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 locRaw(db, nByte
1913b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1913c 20 7a 43 73 72 20 3d 20 70 2d 3e 70 46 72 65 65 zCsr = p->pFree
1913d 3b 0a 20 20 20 20 20 20 7a 45 6e 64 20 3d 20 26 ;. zEnd = &
1913e 7a 43 73 72 5b 6e 42 79 74 65 5d 3b 0a 20 20 20 zCsr[nByte];.
1913f 20 7d 77 68 69 6c 65 28 20 6e 42 79 74 65 20 26 }while( nByte &
19140 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 & !db->mallocFai
19141 6c 65 64 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 6e led );.. p->n
19142 43 75 72 73 6f 72 20 3d 20 6e 43 75 72 73 6f 72 Cursor = nCursor
19143 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 ;. if( p->aVa
19144 72 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 r ){. p->nV
19145 61 72 20 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 ar = nVar;.
19146 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 for(n=0; n<nVar
19147 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; n++){.
19148 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 p->aVar[n].flags
19149 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1914a 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e p->aVar[n].
1914b 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d db = db;. }
1914c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1914d 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 ->aMem ){.
1914e 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 p->aMem--;
1914f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19150 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 /* aMem[] goes f
19151 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 rom 1..nMem */.
19152 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e p->nMem = n
19153 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
19154 20 20 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f /* no
19155 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 t from 0..nMem-1
19156 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d */. for(n=
19157 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 1; n<=nMem; n++)
19158 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 {. p->aMe
19159 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d m[n].flags = MEM
1915a 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 _Null;. p
1915b 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 ->aMem[n].db = d
1915c 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d b;. }. }
1915d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 . }.#ifdef SQLI
1915e 54 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 6e TE_DEBUG. for(n
1915f 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e =1; n<p->nMem; n
19160 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ++){. assert(
19161 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d p->aMem[n].db==
19162 64 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 db );. }.#endif
19163 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a .. p->pc = -1;.
19164 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 p->rc = SQLITE
19165 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 _OK;. p->errorA
19166 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 ction = OE_Abort
19167 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c ;. p->explain |
19168 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 = isExplain;. p
19169 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d ->magic = VDBE_M
1916a 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e AGIC_RUN;. p->n
1916b 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d Change = 0;. p-
1916c 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 >cacheCtr = 1;.
1916d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 p->minWriteFile
1916e 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 Format = 255;.
1916f 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 p->iStatement =
19170 30 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 0;.#ifdef VDBE_P
19171 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69 ROFILE. {. i
19172 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
19173 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 0; i<p->nOp; i++
19174 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b ){. p->aOp[
19175 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 i].cnt = 0;.
19176 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c p->aOp[i].cycl
19177 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 es = 0;. }.
19178 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a }.#endif.}../*.*
19179 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 45 20 63 * Close a VDBE c
1917a 75 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 73 ursor and releas
1917b 65 20 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 72 e all the resour
1917c 63 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 ces that cursor
1917d 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 68 .** happens to h
1917e 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 old..*/.SQLITE_P
1917f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
19180 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f te3VdbeFreeCurso
19181 72 28 56 64 62 65 20 2a 70 2c 20 56 64 62 65 43 r(Vdbe *p, VdbeC
19182 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69 ursor *pCx){. i
19183 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20 f( pCx==0 ){.
19184 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
19185 66 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 f( pCx->pBt ){.
19186 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
19187 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a lose(pCx->pBt);.
19188 20 20 20 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e /* The pCx->
19189 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 pCursor will be
1918a 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 61 close automatica
1918b 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 69 73 74 lly, if it exist
1918c 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 s, by. ** the
1918d 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a call above. */.
1918e 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 78 2d }else if( pCx-
1918f 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 >pCursor ){.
19190 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
19191 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 eCursor(pCx->pCu
19192 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 rsor);. }.#ifnd
19193 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
19194 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 IRTUALTABLE. if
19195 28 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 ( pCx->pVtabCurs
19196 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 or ){. sqlite
19197 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 3_vtab_cursor *p
19198 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43 78 VtabCursor = pCx
19199 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 ->pVtabCursor;.
1919a 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 const sqlite3
1919b 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 _module *pModule
1919c 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b = pCx->pModule;
1919d 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 . p->inVtabMe
1919e 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 76 thod = 1;. (v
1919f 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
191a0 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 20 yOff(p->db);.
191a1 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 pModule->xClose
191a2 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 (pVtabCursor);.
191a3 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
191a4 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3b SafetyOn(p->db);
191a5 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 . p->inVtabMe
191a6 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 thod = 0;. }.#e
191a7 6e 64 69 66 0a 20 20 69 66 28 20 21 70 43 78 2d ndif. if( !pCx-
191a8 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c >ephemPseudoTabl
191a9 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
191aa 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 DbFree(p->db, pC
191ab 78 2d 3e 70 44 61 74 61 29 3b 0a 20 20 7d 0a 7d x->pData);. }.}
191ac 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c ../*.** Close al
191ad 6c 20 63 75 72 73 6f 72 73 20 65 78 63 65 70 74 l cursors except
191ae 20 66 6f 72 20 56 54 61 62 20 63 75 72 73 6f 72 for VTab cursor
191af 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 s that are curre
191b0 6e 74 6c 79 0a 2a 2a 20 69 6e 20 75 73 65 2e 0a ntly.** in use..
191b1 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
191b2 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 loseAllCursorsEx
191b3 63 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28 ceptActiveVtabs(
191b4 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
191b5 69 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 i;. if( p->apCs
191b6 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 r==0 ) return;.
191b7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
191b8 43 75 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 Cursor; i++){.
191b9 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
191ba 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a = p->apCsr[i];.
191bb 20 20 20 20 69 66 28 20 70 43 20 26 26 20 28 21 if( pC && (!
191bc 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 p->inVtabMethod
191bd 7c 7c 20 21 70 43 2d 3e 70 56 74 61 62 43 75 72 || !pC->pVtabCur
191be 73 6f 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 sor) ){. sq
191bf 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 lite3VdbeFreeCur
191c0 73 6f 72 28 70 2c 20 70 43 29 3b 0a 20 20 20 20 sor(p, pC);.
191c1 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 p->apCsr[i] =
191c2 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
191c3 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74 /*.** Clean up t
191c4 68 65 20 56 4d 20 61 66 74 65 72 20 65 78 65 63 he VM after exec
191c5 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ution..**.** Thi
191c6 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 s routine will a
191c7 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f utomatically clo
191c8 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20 se any cursors,
191c9 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a lists, and/or.**
191ca 20 73 6f 72 74 65 72 73 20 74 68 61 74 20 77 65 sorters that we
191cb 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49 re left open. I
191cc 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74 t also deletes t
191cd 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 he values of.**
191ce 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 variables in the
191cf 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a aVar[] array..*
191d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c /.static void Cl
191d1 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a eanup(Vdbe *p){.
191d2 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 int i;. sqlit
191d3 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a e3 *db = p->db;.
191d4 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 63 Mem *pMem;. c
191d5 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 loseAllCursorsEx
191d6 63 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28 ceptActiveVtabs(
191d7 70 29 3b 0a 20 20 66 6f 72 28 70 4d 65 6d 3d 26 p);. for(pMem=&
191d8 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 69 3d 31 3b p->aMem[1], i=1;
191d9 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b i<=p->nMem; i++
191da 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 , pMem++){. i
191db 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 f( pMem->flags &
191dc 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 MEM_RowSet ){.
191dd 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 sqlite3RowS
191de 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e etClear(pMem->u.
191df 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a pRowSet);. }.
191e0 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
191e1 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c ag(pMem, MEM_Nul
191e2 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 l);. }. releas
191e3 65 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d eMemArray(&p->aM
191e4 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b em[1], p->nMem);
191e5 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 . if( p->contex
191e6 74 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 tStack ){. sq
191e7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
191e8 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 29 p->contextStack)
191e9 3b 0a 20 20 7d 0a 20 20 70 2d 3e 63 6f 6e 74 65 ;. }. p->conte
191ea 78 74 53 74 61 63 6b 20 3d 20 30 3b 0a 20 20 70 xtStack = 0;. p
191eb 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 ->contextStackDe
191ec 70 74 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f pth = 0;. p->co
191ed 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 20 3d 20 ntextStackTop =
191ee 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 0;. sqlite3DbFr
191ef 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
191f0 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 g);. p->zErrMsg
191f1 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75 = 0;. p->pResu
191f2 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ltSet = 0;.}../*
191f3 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 .** Set the numb
191f4 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c er of result col
191f5 75 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 umns that will b
191f6 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 e returned by th
191f7 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d is SQL.** statem
191f8 65 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 ent. This is now
191f9 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 set at compile
191fa 74 69 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61 time, rather tha
191fb 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63 n during.** exec
191fc 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 ution of the vdb
191fd 65 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 e program so tha
191fe 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
191ff 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 _count() can.**
19200 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 be called on an
19201 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 SQL statement be
19202 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65 fore sqlite3_ste
19203 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 p()..*/.SQLITE_P
19204 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
19205 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c te3VdbeSetNumCol
19206 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e s(Vdbe *p, int n
19207 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 ResColumn){. Me
19208 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 m *pColName;. i
19209 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 nt n;. sqlite3
1920a 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 *db = p->db;..
1920b 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 releaseMemArray(
1920c 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e p->aColName, p->
1920d 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 nResColumn*COLNA
1920e 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 ME_N);. sqlite3
1920f 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 DbFree(db, p->aC
19210 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e olName);. n = n
19211 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d ResColumn*COLNAM
19212 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 6f E_N;. p->nResCo
19213 6c 75 6d 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d lumn = nResColum
19214 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 n;. p->aColName
19215 20 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28 4d = pColName = (M
19216 65 6d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c em*)sqlite3DbMal
19217 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 locZero(db, size
19218 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b 0a 20 20 69 of(Mem)*n );. i
19219 66 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d 3d f( p->aColName==
1921a 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 0 ) return;. wh
1921b 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b 0a ile( n-- > 0 ){.
1921c 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c pColName->fl
1921d 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
1921e 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 64 62 pColName->db
1921f 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70 43 = p->db;. pC
19220 6f 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 7d 0a 7d 0a olName++;. }.}.
19221 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e ./*.** Set the n
19222 61 6d 65 20 6f 66 20 74 68 65 20 69 64 78 27 74 ame of the idx't
19223 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 72 h column to be r
19224 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 53 eturned by the S
19225 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a QL statement..**
19226 20 7a 4e 61 6d 65 20 6d 75 73 74 20 62 65 20 61 zName must be a
19227 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 75 pointer to a nu
19228 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 l terminated str
19229 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ing..**.** This
1922a 63 61 6c 6c 20 6d 75 73 74 20 62 65 20 6d 61 64 call must be mad
1922b 65 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 e after a call t
1922c 6f 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 o sqlite3VdbeSet
1922d 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a 2a 0a 2a 2a NumCols()..**.**
1922e 20 54 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d The final param
1922f 65 74 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73 74 eter, xDel, must
19230 20 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 be one of SQLIT
19231 45 5f 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49 54 E_DYNAMIC, SQLIT
19232 45 5f 53 54 41 54 49 43 0a 2a 2a 20 6f 72 20 53 E_STATIC.** or S
19233 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e QLITE_TRANSIENT.
19234 20 49 66 20 69 74 20 69 73 20 53 51 4c 49 54 45 If it is SQLITE
19235 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 74 _DYNAMIC, then t
19236 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 he buffer pointe
19237 64 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d 65 d.** to by zName
19238 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 62 will be freed b
19239 79 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 y sqlite3DbFree(
1923a 29 20 77 68 65 6e 20 74 68 65 20 76 64 62 65 20 ) when the vdbe
1923b 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2f is destroyed..*/
1923c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1923d 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 int sqlite3VdbeS
1923e 65 74 43 6f 6c 4e 61 6d 65 28 0a 20 20 56 64 62 etColName(. Vdb
1923f 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 e *p,
19240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19241 20 56 64 62 65 20 62 65 69 6e 67 20 63 6f 6e 66 Vdbe being conf
19242 69 67 75 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 igured */. int
19243 69 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 idx,
19244 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19245 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 Index of column
19246 7a 4e 61 6d 65 20 61 70 70 6c 69 65 73 20 74 6f zName applies to
19247 20 2a 2f 0a 20 20 69 6e 74 20 76 61 72 2c 20 20 */. int var,
19248 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19249 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
1924a 20 74 68 65 20 43 4f 4c 4e 41 4d 45 5f 2a 20 63 the COLNAME_* c
1924b 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 63 6f onstants */. co
1924c 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
1924d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1924e 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 * Pointer to buf
1924f 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e fer containing n
19250 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a ame */. void (*
19251 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 xDel)(void*)
19252 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
19253 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 ory management s
19254 74 72 61 74 65 67 79 20 66 6f 72 20 7a 4e 61 6d trategy for zNam
19255 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
19256 3b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d ;. Mem *pColNam
19257 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 e;. assert( idx
19258 3c 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 29 <p->nResColumn )
19259 3b 0a 20 20 61 73 73 65 72 74 28 20 76 61 72 3c ;. assert( var<
1925a 43 4f 4c 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20 69 COLNAME_N );. i
1925b 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 f( p->db->malloc
1925c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 61 73 Failed ){. as
1925d 73 65 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 sert( !zName ||
1925e 78 44 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e xDel!=SQLITE_DYN
1925f 41 4d 49 43 20 29 3b 0a 20 20 20 20 72 65 74 75 AMIC );. retu
19260 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
19261 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
19262 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29 3b ->aColName!=0 );
19263 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26 28 . pColName = &(
19264 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78 2b p->aColName[idx+
19265 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d var*p->nResColum
19266 6e 5d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 n]);. rc = sqli
19267 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
19268 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 (pColName, zName
19269 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 , -1, SQLITE_UTF
1926a 38 2c 20 78 44 65 6c 29 3b 0a 20 20 61 73 73 65 8, xDel);. asse
1926b 72 74 28 20 72 63 21 3d 30 20 7c 7c 20 21 7a 4e rt( rc!=0 || !zN
1926c 61 6d 65 20 7c 7c 20 28 70 43 6f 6c 4e 61 6d 65 ame || (pColName
1926d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 54 65 72 6d ->flags&MEM_Term
1926e 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e )!=0 );. return
1926f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 rc;.}../*.** A
19270 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 read or write tr
19271 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6f 72 ansaction may or
19272 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 63 74 69 may not be acti
19273 76 65 20 6f 6e 20 64 61 74 61 62 61 73 65 20 68 ve on database h
19274 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 49 66 20 andle.** db. If
19275 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
19276 20 61 63 74 69 76 65 2c 20 63 6f 6d 6d 69 74 20 active, commit
19277 69 74 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 it. If there is
19278 61 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e 73 a.** write-trans
19279 61 63 74 69 6f 6e 20 73 70 61 6e 6e 69 6e 67 20 action spanning
1927a 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 more than one da
1927b 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69 tabase file, thi
1927c 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 61 6b s routine.** tak
1927d 65 73 20 63 61 72 65 20 6f 66 20 74 68 65 20 6d es care of the m
1927e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 74 72 aster journal tr
1927f 69 63 6b 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 ickery..*/.stati
19280 63 20 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69 74 c int vdbeCommit
19281 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64 (sqlite3 *db, Vd
19282 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b be *p){. int i;
19283 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d 20 . int nTrans =
19284 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 0; /* Number of
19285 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 20 databases with
19286 61 6e 20 61 63 74 69 76 65 20 77 72 69 74 65 2d an active write-
19287 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 transaction */.
19288 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
19289 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65 65 64 58 _OK;. int needX
1928a 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a 20 20 2f commit = 0;.. /
1928b 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 * Before doing a
1928c 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 nything else, ca
1928d 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 ll the xSync() c
1928e 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a allback for any.
1928f 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 ** virtual mod
19290 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 ule tables writt
19291 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 en in this trans
19292 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 action. This has
19293 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 to. ** be done
19294 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e before determin
19295 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 ing whether a ma
19296 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
19297 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 e is . ** requi
19298 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 red, as an xSync
19299 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 () callback may
1929a 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 add an attached
1929b 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f database. ** to
1929c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1929d 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 .. */. rc = sq
1929e 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 lite3VtabSync(db
1929f 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a , &p->zErrMsg);.
192a0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
192a1 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
192a2 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 n rc;. }.. /*
192a3 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d This loop determ
192a4 69 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 20 ines (a) if the
192a5 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 commit hook shou
192a6 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e ld be invoked an
192a7 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d d. ** (b) how m
192a8 61 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 6c any database fil
192a9 65 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 es have open wri
192aa 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c te transactions,
192ab 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 not . ** inclu
192ac 64 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 ding the temp da
192ad 74 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 tabase. (b) is i
192ae 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 mportant because
192af 20 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 if more than .
192b0 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 ** one database
192b1 20 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 file has an ope
192b2 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 n write transact
192b3 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f ion, a master jo
192b4 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 urnal. ** file
192b5 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 is required for
192b6 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 an atomic commit
192b7 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d .. */ . for(i=
192b8 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
192b9 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a +){ . Btree *
192ba 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d pBt = db->aDb[i]
192bb 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 .pBt;. if( sq
192bc 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 lite3BtreeIsInTr
192bd 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 ans(pBt) ){.
192be 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 needXcommit =
192bf 31 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 1;. if( i!=
192c0 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 1 ) nTrans++;.
192c1 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
192c2 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 there are any w
192c3 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
192c4 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 s at all, invoke
192c5 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b the commit hook
192c6 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 */. if( needXc
192c7 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f ommit && db->xCo
192c8 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a mmitCallback ){.
192c9 20 20 20 20 61 73 73 65 72 74 28 20 28 64 62 2d assert( (db-
192ca 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
192cb 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 20 29 CommitBusy)==0 )
192cc 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 ;. db->flags
192cd 7c 3d 20 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 74 |= SQLITE_Commit
192ce 42 75 73 79 3b 0a 20 20 20 20 28 76 6f 69 64 29 Busy;. (void)
192cf 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
192d0 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 (db);. rc = d
192d1 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 b->xCommitCallba
192d2 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 ck(db->pCommitAr
192d3 67 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 g);. (void)sq
192d4 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
192d5 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 );. db->flags
192d6 20 26 3d 20 7e 53 51 4c 49 54 45 5f 43 6f 6d 6d &= ~SQLITE_Comm
192d7 69 74 42 75 73 79 3b 0a 20 20 20 20 69 66 28 20 itBusy;. if(
192d8 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 rc ){. retu
192d9 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 rn SQLITE_CONSTR
192da 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a AINT;. }. }.
192db 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 . /* The simple
192dc 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 case - no more
192dd 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 than one databas
192de 65 20 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e e file (not coun
192df 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 ting the. ** TE
192e0 4d 50 20 64 61 74 61 62 61 73 65 29 20 68 61 73 MP database) has
192e1 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 a transaction a
192e2 63 74 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 ctive. There i
192e3 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 s no need for th
192e4 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f e. ** master-jo
192e5 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a urnal.. **. **
192e6 20 49 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 If the return v
192e7 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 alue of sqlite3B
192e8 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 treeGetFilename(
192e9 29 20 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 ) is a zero leng
192ea 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 th. ** string,
192eb 69 74 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 it means the mai
192ec 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d n database is :m
192ed 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 emory: or a temp
192ee 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a file. In . **
192ef 20 74 68 61 74 20 63 61 73 65 20 77 65 20 64 6f that case we do
192f0 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f not support ato
192f1 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 mic multi-file c
192f2 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 ommits, so use t
192f3 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 he . ** simple
192f4 63 61 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 case then too..
192f5 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c */. if( 0==sql
192f6 69 74 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c ite3Strlen30(sql
192f7 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 ite3BtreeGetFile
192f8 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e name(db->aDb[0].
192f9 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 61 pBt)). || nTra
192fa 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 66 ns<=1. ){. f
192fb 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 or(i=0; rc==SQLI
192fc 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e TE_OK && i<db->n
192fd 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 Db; i++){.
192fe 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d Btree *pBt = db-
192ff 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
19300 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 if( pBt ){.
19301 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
19302 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
19303 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20 seOne(pBt, 0);.
19304 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
19305 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d /* Do the comm
19306 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 it only if all d
19307 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73 73 atabases success
19308 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 fully complete p
19309 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20 hase 1. . **
1930a 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 If one of the Bt
1930b 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e reeCommitPhaseOn
1930c 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c e() calls fails,
1930d 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 this indicates
1930e 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72 an. ** IO err
1930f 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e or while deletin
19310 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 g or truncating
19311 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 a journal file.
19312 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a It is unlikely,.
19313 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64 ** but could
19314 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73 happen. In this
19315 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72 case abandon pr
19316 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 ocessing and ret
19317 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 urn the error..
19318 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d */. for(i=
19319 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0; rc==SQLITE_OK
1931a 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 && i<db->nDb; i
1931b 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 ++){. Btree
1931c 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b *pBt = db->aDb[
1931d 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 i].pBt;. if
1931e 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ( pBt ){.
1931f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
19320 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
19321 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 (pBt);. }.
19322 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
19323 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19324 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 sqlite3VtabC
19325 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d ommit(db);. }
19326 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 . }.. /* The c
19327 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68 omplex case - Th
19328 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66 ere is a multi-f
19329 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 ile write-transa
1932a 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20 ction active..
1932b 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65 73 ** This requires
1932c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
1932d 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65 l file to ensure
1932e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1932f 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 is. ** committ
19330 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a ed atomicly.. *
19331 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
19332 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65 _OMIT_DISKIO. e
19333 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
19334 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d _vfs *pVfs = db-
19335 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e >pVfs;. int n
19336 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 eedSync = 0;.
19337 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d char *zMaster =
19338 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61 0; /* File-na
19339 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65 me for the maste
1933a 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 r journal */.
1933b 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 char const *zMa
1933c 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 inFile = sqlite3
1933d 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 BtreeGetFilename
1933e 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 (db->aDb[0].pBt)
1933f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 ;. sqlite3_fi
19340 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b le *pMaster = 0;
19341 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 . i64 offset
19342 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 = 0;. int res
19343 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 ;.. /* Select
19344 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
19345 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 l file name */.
19346 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33 do {. u3
19347 32 20 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 2 iRandom;.
19348 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
19349 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 b, zMaster);.
1934a 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f sqlite3_rando
1934b 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 mness(sizeof(iRa
1934c 6e 64 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 ndom), &iRandom)
1934d 3b 0a 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 ;. zMaster
1934e 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
1934f 28 64 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 (db, "%s-mj%08X"
19350 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 61 , zMainFile, iRa
19351 6e 64 6f 6d 26 30 78 37 66 66 66 66 66 66 66 29 ndom&0x7fffffff)
19352 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4d 61 ;. if( !zMa
19353 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 ster ){.
19354 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
19355 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 MEM;. }.
19356 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
19357 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d sAccess(pVfs, zM
19358 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 aster, SQLITE_AC
19359 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 CESS_EXISTS, &re
1935a 73 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 s);. }while(
1935b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1935c 20 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 res );. if(
1935d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1935e 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 . /* Open t
1935f 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
19360 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d l. */. rc =
19361 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 sqlite3OsOpenMa
19362 6c 6c 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 lloc(pVfs, zMast
19363 65 72 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 er, &pMaster, .
19364 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
19365 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 OPEN_READWRITE|S
19366 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
19367 45 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c E|. SQL
19368 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
19369 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d VE|SQLITE_OPEN_M
1936a 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 ASTER_JOURNAL, 0
1936b 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a . );. }.
1936c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1936d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
1936e 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1936f 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 zMaster);.
19370 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
19371 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 }. . /* Write
19372 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 the name of eac
19373 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 h database file
19374 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 in the transacti
19375 6f 6e 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a on into the new.
19376 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f ** master jo
19377 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 urnal file. If a
19378 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 n error occurs a
19379 74 20 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f t this point clo
1937a 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 se. ** and de
1937b 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 lete the master
1937c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c journal file. Al
1937d 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c l the individual
1937e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 journal files.
1937f 20 20 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 ** still have
19380 20 27 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 'null' as the m
19381 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f aster journal po
19382 69 6e 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 inter, so they w
19383 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 ill roll. **
19384 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 back independent
19385 6c 79 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 ly if a failure
19386 6f 63 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 occurs.. */.
19387 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 for(i=0; i<db
19388 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
19389 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 Btree *pBt =
1938a 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1938b 20 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29 if( i==1 )
1938c 20 63 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 continue; /*
1938d 49 67 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 20 Ignore the TEMP
1938e 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 database */.
1938f 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 if( sqlite3Btr
19390 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 eeIsInTrans(pBt)
19391 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ){. char
19392 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 const *zFile =
19393 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a sqlite3BtreeGetJ
19394 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b ournalname(pBt);
19395 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 69 . if( zFi
19396 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 le[0]==0 ) conti
19397 6e 75 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 nue; /* Ignore
19398 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 :memory: databas
19399 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 es */. if
1939a 28 20 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21 ( !needSync && !
1939b 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 sqlite3BtreeSync
1939c 44 69 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b Disabled(pBt) ){
1939d 0a 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 . needS
1939e 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ync = 1;.
1939f 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
193a0 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
193a1 4d 61 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 Master, zFile, s
193a2 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
193a3 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29 File)+1, offset)
193a4 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 ;. offset
193a5 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 += sqlite3Strle
193a6 6e 33 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 n30(zFile)+1;.
193a7 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
193a8 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
193a9 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c sqlite3OsCl
193aa 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 oseFree(pMaster)
193ab 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
193ac 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 te3OsDelete(pVfs
193ad 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 , zMaster, 0);.
193ae 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
193af 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 DbFree(db, zMast
193b0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 er);. r
193b1 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
193b2 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
193b3 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 }.. /* Sync t
193b4 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
193b5 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 49 l file. If the I
193b6 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 OCAP_SEQUENTIAL
193b7 64 65 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 6c device. ** fl
193b8 61 67 20 69 73 20 73 65 74 20 74 68 69 73 20 69 ag is set this i
193b9 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a s not required..
193ba 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e */. if( n
193bb 65 65 64 53 79 6e 63 20 0a 20 20 20 20 20 26 26 eedSync . &&
193bc 20 30 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 0==(sqlite3OsDe
193bd 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
193be 69 63 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c ics(pMaster)&SQL
193bf 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e ITE_IOCAP_SEQUEN
193c0 54 49 41 4c 29 0a 20 20 20 20 20 26 26 20 53 51 TIAL). && SQ
193c1 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 LITE_OK!=(rc = s
193c2 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61 qlite3OsSync(pMa
193c3 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e ster, SQLITE_SYN
193c4 43 5f 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 29 C_NORMAL)). )
193c5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f {. sqlite3O
193c6 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 sCloseFree(pMast
193c7 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 er);. sqlit
193c8 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c e3OsDelete(pVfs,
193c9 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 zMaster, 0);.
193ca 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
193cb 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a e(db, zMaster);.
193cc 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
193cd 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 . }.. /* S
193ce 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66 ync all the db f
193cf 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e iles involved in
193d0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
193d1 2e 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a . The same call.
193d2 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 ** sets the
193d3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 master journal p
193d4 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69 ointer in each i
193d5 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 ndividual journa
193d6 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 l. If. ** an
193d7 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72 error occurs her
193d8 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 e, do not delete
193d9 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
193da 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a nal file.. **
193db 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65 . ** If the e
193dc 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
193dd 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c ng the first cal
193de 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 l to. ** sqli
193df 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
193e0 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 aseOne(), then t
193e1 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 here is a chance
193e2 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a that the. **
193e3 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
193e4 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 file will be orp
193e5 68 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61 haned. But we ca
193e6 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a nnot delete it,.
193e7 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 ** in case t
193e8 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
193e9 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 l file name was
193ea 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
193eb 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 journal. **
193ec 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20 file before the
193ed 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 failure occurred
193ee 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
193ef 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 (i=0; rc==SQLITE
193f0 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 _OK && i<db->nDb
193f1 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 ; i++){ . B
193f2 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e tree *pBt = db->
193f3 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
193f4 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 if( pBt ){.
193f5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
193f6 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
193f7 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 eOne(pBt, zMaste
193f8 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 r);. }.
193f9 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 }. sqlite3OsC
193fa 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 loseFree(pMaster
193fb 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
193fc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
193fd 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
193fe 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 db, zMaster);.
193ff 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
19400 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c }.. /* Del
19401 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ete the master j
19402 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 ournal file. Thi
19403 73 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 s commits the tr
19404 61 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 ansaction. After
19405 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 . ** doing th
19406 69 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 is the directory
19407 20 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e is synced again
19408 20 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 before any indi
19409 76 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 vidual. ** tr
1940a 61 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 ansaction files
1940b 61 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 are deleted..
1940c 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
1940d 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 ite3OsDelete(pVf
1940e 73 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a s, zMaster, 1);.
1940f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
19410 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a e(db, zMaster);.
19411 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b zMaster = 0;
19412 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
19413 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
19414 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c }.. /* Al
19415 6c 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 l files and dire
19416 63 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 ctories have alr
19417 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 eady been synced
19418 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 , so the followi
19419 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 ng. ** calls
1941a 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 to sqlite3BtreeC
1941b 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 ommitPhaseTwo()
1941c 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 are only closing
1941d 20 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a files and. *
1941e 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 * deleting or tr
1941f 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c uncating journal
19420 73 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 s. If something
19421 67 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 goes wrong while
19422 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 . ** this is
19423 68 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e happening we don
19424 27 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 't really care.
19425 54 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 The integrity of
19426 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e the. ** tran
19427 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 saction is alrea
19428 64 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 dy guaranteed, b
19429 75 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 ut some stray 'c
1942a 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 old' journals.
1942b 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e ** may be lyin
1942c 67 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e g around. Return
1942d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 ing an error cod
1942e 65 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 e won't help mat
1942f 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ters.. */.
19430 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 disable_simulat
19431 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a ed_io_errors();.
19432 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e sqlite3Begin
19433 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
19434 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 for(i=0; i<d
19435 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 b->nDb; i++){ .
19436 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 Btree *pBt
19437 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
19438 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 ;. if( pBt
19439 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1943a 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
1943b 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 seTwo(pBt);.
1943c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
1943d 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
1943e 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 lloc();. enab
1943f 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f le_simulated_io_
19440 65 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 errors();.. s
19441 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 qlite3VtabCommit
19442 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 (db);. }.#endif
19443 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
19444 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f ../* .** This ro
19445 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 utine checks tha
19446 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 t the sqlite3.ac
19447 74 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e tiveVdbeCnt coun
19448 74 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 t variable.** ma
19449 74 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 tches the number
1944a 20 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 of vdbe's in th
1944b 65 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 e list sqlite3.p
1944c 56 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a Vdbe that are.**
1944d 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 currently activ
1944e 65 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 e. An assertion
1944f 66 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f fails if the two
19450 20 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d counts do not m
19451 61 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 atch..** This is
19452 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c an internal sel
19453 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 f-check only - i
19454 74 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 t is not an esse
19455 6e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 ntial processing
19456 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 .** step..**.**
19457 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 This is a no-op
19458 69 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 if NDEBUG is def
19459 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 ined..*/.#ifndef
1945a 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 NDEBUG.static v
1945b 6f 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 oid checkActiveV
1945c 64 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a dbeCnt(sqlite3 *
1945d 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a db){. Vdbe *p;.
1945e 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 int cnt = 0;.
1945f 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b int nWrite = 0;
19460 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 . p = db->pVdbe
19461 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a ;. while( p ){.
19462 20 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 if( p->magic
19463 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e ==VDBE_MAGIC_RUN
19464 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a && p->pc>=0 ){.
19465 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 cnt++;.
19466 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e if( p->readOn
19467 6c 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b ly==0 ) nWrite++
19468 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 ;. }. p =
19469 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 p->pNext;. }.
1946a 61 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d assert( cnt==db-
1946b 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 >activeVdbeCnt )
1946c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 ;. assert( nWri
1946d 74 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 te==db->writeVdb
1946e 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a eCnt );.}.#else.
1946f 23 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 #define checkAct
19470 69 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 iveVdbeCnt(x).#e
19471 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 ndif../*.** For
19472 65 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74 every Btree that
19473 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e in database con
19474 6e 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 nection db which
19475 20 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f .** has been mo
19476 64 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f dified, "trip" o
19477 72 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 r invalidate eac
19478 68 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 h cursor in.** t
19479 68 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 hat Btree might
1947a 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 have been modifi
1947b 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 ed so that the c
1947c 75 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 ursor.** can nev
1947d 65 72 20 62 65 20 75 73 65 64 20 61 67 61 69 6e er be used again
1947e 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 . This happens
1947f 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a when a rollback.
19480 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 *** occurs. We
19481 68 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c have to trip all
19482 20 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f the other curso
19483 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 rs, even.** curs
19484 6f 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d or from other VM
19485 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 s in different d
19486 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
19487 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 ons,.** so that
19488 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 none of them try
19489 20 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 to use the data
1948a 20 61 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a at which they.*
1948b 2a 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 * were pointing
1948c 61 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 and which now ma
1948d 79 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e y have been chan
1948e 67 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 ged due.** to th
1948f 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a e rollback..**.*
19490 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 * Remember that
19491 61 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 a rollback can d
19492 65 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d elete tables com
19493 70 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f plete and.** reo
19494 72 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 rder rootpages.
19495 20 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 So it is not su
19496 66 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f fficient just to
19497 20 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 save.** the sta
19498 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 te of the cursor
19499 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e . We have to in
1949a 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 validate the cur
1949b 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 sor.** so that i
1949c 74 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 t is never used
1949d 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 again..*/.static
1949e 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 void invalidate
1949f 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 CursorsOnModifie
194a0 64 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 dBtrees(sqlite3
194a1 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *db){. int i;.
194a2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
194a3 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 nDb; i++){. B
194a4 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 tree *p = db->aD
194a5 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 b[i].pBt;. if
194a6 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 ( p && sqlite3Bt
194a7 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 reeIsInTrans(p)
194a8 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
194a9 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 BtreeTripAllCurs
194aa 6f 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 ors(p, SQLITE_AB
194ab 4f 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ORT);. }. }.
194ac 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
194ad 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 74 Vdbe passed as t
194ae 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
194af 74 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 t opened a state
194b0 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e ment-transaction
194b1 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e 6f ,.** close it no
194b2 77 2e 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 20 w. Argument eOp
194b3 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 53 must be either S
194b4 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
194b5 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 4e K or.** SAVEPOIN
194b6 54 5f 52 45 4c 45 41 53 45 2e 20 49 66 20 69 74 T_RELEASE. If it
194b7 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f is SAVEPOINT_RO
194b8 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 65 LLBACK, then the
194b9 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 statement.** tr
194ba 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c ansaction is rol
194bb 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 65 4f 70 led back. If eOp
194bc 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 is SAVEPOINT_RE
194bd 4c 45 41 53 45 2c 20 74 68 65 6e 20 74 68 65 20 LEASE, then the
194be 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 .** statement tr
194bf 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d ansaction is com
194c0 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 mtted..**.** If
194c1 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 an IO error occu
194c2 72 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f rs, an SQLITE_IO
194c3 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f ERR_XXX error co
194c4 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 de is returned.
194c5 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53 51 .** Otherwise SQ
194c6 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 LITE_OK..*/.SQLI
194c7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
194c8 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 qlite3VdbeCloseS
194c9 74 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 70 tatement(Vdbe *p
194ca 2c 20 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 71 , int eOp){. sq
194cb 6c 69 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 20 lite3 *const db
194cc 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 = p->db;. int r
194cd 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
194ce 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 if( p->iStateme
194cf 6e 74 20 26 26 20 64 62 2d 3e 6e 53 74 61 74 65 nt && db->nState
194d0 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 ment ){. int
194d1 69 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 i;. const int
194d2 20 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d iSavepoint = p-
194d3 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0a 0a >iStatement-1;..
194d4 20 20 20 20 61 73 73 65 72 74 28 20 65 4f 70 3d assert( eOp=
194d5 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
194d6 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41 56 45 ACK || eOp==SAVE
194d7 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a POINT_RELEASE);.
194d8 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
194d9 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a nStatement>0 );.
194da 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 assert( p->i
194db 53 74 61 74 65 6d 65 6e 74 3d 3d 28 64 62 2d 3e Statement==(db->
194dc 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e nStatement+db->n
194dd 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 0a 20 Savepoint) );..
194de 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 for(i=0; i<db
194df 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
194e0 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 int rc2 = SQ
194e1 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 42 LITE_OK;. B
194e2 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e tree *pBt = db->
194e3 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
194e4 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 if( pBt ){.
194e5 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 if( eOp==SA
194e6 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
194e7 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
194e8 32 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 2 = sqlite3Btree
194e9 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53 Savepoint(pBt, S
194ea 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
194eb 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a K, iSavepoint);.
194ec 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
194ed 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 if( rc2==SQLIT
194ee 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
194ef 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 rc2 = sqlite3B
194f0 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 treeSavepoint(pB
194f1 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c t, SAVEPOINT_REL
194f2 45 41 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74 EASE, iSavepoint
194f3 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
194f4 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
194f5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
194f6 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 rc = rc2;.
194f7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
194f8 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6e 53 }. db->nS
194f9 74 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20 20 20 20 tatement--;.
194fa 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 p->iStatement =
194fb 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
194fc 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
194fd 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
194fe 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20 56 led the when a V
194ff 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c DBE tries to hal
19500 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45 0a t. If the VDBE.
19501 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61 6e ** has made chan
19502 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 75 ges and is in au
19503 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 74 tocommit mode, t
19504 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 65 hen commit those
19505 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 66 .** changes. If
19506 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6e a rollback is n
19507 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 74 eeded, then do t
19508 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a he rollback..**.
19509 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1950a 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 is the only way
1950b 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 61 74 to move the stat
1950c 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a 2a e of a VM from.*
1950d 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 52 * SQLITE_MAGIC_R
1950e 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 UN to SQLITE_MAG
1950f 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 20 IC_HALT. It is
19510 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 63 harmless to.** c
19511 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 4d all this on a VM
19512 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 that is in the
19513 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c SQLITE_MAGIC_HAL
19514 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 T state..**.** R
19515 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
19516 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 6f 6d ode. If the com
19517 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 6f mit could not co
19518 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 20 6f mplete because o
19519 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e f.** lock conten
1951a 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 4c tion, return SQL
1951b 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 53 51 ITE_BUSY. If SQ
1951c 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 LITE_BUSY is ret
1951d 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 61 urned, it.** mea
1951e 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 69 64 ns the close did
1951f 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 20 not happen and
19520 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 70 65 needs to be repe
19521 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ated..*/.SQLITE_
19522 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19523 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 65 te3VdbeHalt(Vdbe
19524 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 *p){. int rc;
19525 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19526 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
19527 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 69 65 to store transie
19528 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 73 20 nt return codes
19529 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
1952a 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 = p->db;.. /*
1952b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f This function co
1952c 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63 ntains the logic
1952d 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 that determines
1952e 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 if a statement
1952f 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 or. ** transact
19530 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d ion will be comm
19531 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 itted or rolled
19532 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 back as a result
19533 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 65 of the. ** exe
19534 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 76 cution of this v
19535 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 irtual machine.
19536 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6e . **. ** If an
19537 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 y of the followi
19538 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72 3a ng errors occur:
19539 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 . **. ** S
1953a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a QLITE_NOMEM. **
1953b 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 SQLITE_IOER
1953c 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 R. ** SQLIT
1953d 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 E_FULL. **
1953e 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
1953f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 . **. ** Then
19540 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63 the internal cac
19541 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 he might have be
19542 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e en left in an in
19543 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20 consistent. **
19544 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64 20 state. We need
19545 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 to rollback the
19546 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
19547 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 20 ction, if there
19548 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20 is. ** one, or
19549 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 61 the complete tra
1954a 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 72 nsaction if ther
1954b 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e e is no statemen
1954c 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 t transaction..
1954d 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 62 */.. if( p->db
1954e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1954f 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
19550 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
19551 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 closeAllCursor
19552 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74 61 sExceptActiveVta
19553 62 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e bs(p);. if( p->
19554 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 magic!=VDBE_MAGI
19555 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 C_RUN ){. ret
19556 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
19557 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 }. checkActive
19558 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20 20 VdbeCnt(db);..
19559 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 20 /* No commit or
1955a 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64 20 rollback needed
1955b 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 6e if the program n
1955c 65 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f 0a ever started */.
1955d 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 if( p->pc>=0 )
1955e 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20 20 {. int mrc;
1955f 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72 6f /* Primary erro
19560 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e 72 r code from p->r
19561 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 53 74 c */. int eSt
19562 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a 20 atementOp = 0;.
19563 20 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c int isSpecial
19564 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 Error;
19565 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 /* Set to true
19566 20 69 66 20 61 20 27 73 70 65 63 69 61 6c 27 20 if a 'special'
19567 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a error */.. /*
19568 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 Lock all btrees
19569 20 75 73 65 64 20 62 79 20 74 68 65 20 73 74 61 used by the sta
1956a 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 tement */. sq
1956b 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
1956c 72 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d rrayEnter(&p->aM
1956d 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 utex);.. /* C
1956e 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 heck for one of
1956f 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f the special erro
19570 72 73 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 rs */. mrc =
19571 70 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20 p->rc & 0xff;.
19572 20 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 isSpecialError
19573 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e = mrc==SQLITE_N
19574 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c OMEM || mrc==SQL
19575 49 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 ITE_IOERR.
19576 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
19577 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e | mrc==SQLITE_IN
19578 54 45 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d TERRUPT || mrc==
19579 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 SQLITE_FULL;.
1957a 20 69 66 28 20 69 73 53 70 65 63 69 61 6c 45 72 if( isSpecialEr
1957b 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ror ){. /*
1957c 49 66 20 74 68 65 20 71 75 65 72 79 20 77 61 73 If the query was
1957d 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e read-only, we n
1957e 65 65 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 eed do no rollba
1957f 63 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 ck at all. Other
19580 77 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 wise,. ** p
19581 72 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20 roceed with the
19582 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 special handling
19583 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
19584 20 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c if( !p->readOnl
19585 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 y || mrc!=SQLITE
19586 5f 49 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 _INTERRUPT ){.
19587 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d if( p->rc=
19588 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c =SQLITE_IOERR_BL
19589 4f 43 4b 45 44 20 26 26 20 70 2d 3e 75 73 65 73 OCKED && p->uses
1958a 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 StmtJournal ){.
1958b 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d eStatem
1958c 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e entOp = SAVEPOIN
1958d 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 T_ROLLBACK;.
1958e 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 p->rc = SQ
1958f 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 LITE_BUSY;.
19590 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6d 72 }else if( (mr
19591 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
19592 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 || mrc==SQLITE_F
19593 55 4c 4c 29 0a 20 20 20 20 20 20 20 20 20 20 20 ULL).
19594 20 20 20 20 20 20 20 20 26 26 20 70 2d 3e 75 73 && p->us
19595 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b esStmtJournal ){
19596 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 . eStat
19597 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f ementOp = SAVEPO
19598 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 INT_ROLLBACK;.
19599 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1959a 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 /* We are
1959b 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 forced to roll
1959c 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 back the active
1959d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 transaction. Bef
1959e 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 ore doing.
1959f 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 ** so, abort
195a0 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 any other state
195a1 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c ments this handl
195a2 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 e currently has
195a3 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 active..
195a4 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 */. i
195a5 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 nvalidateCursors
195a6 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 OnModifiedBtrees
195a7 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (db);.
195a8 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 sqlite3RollbackA
195a9 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 ll(db);.
195aa 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 sqlite3CloseSa
195ab 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 vepoints(db);.
195ac 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f db->auto
195ad 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 Commit = 1;.
195ae 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
195af 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
195b0 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 the auto-commit
195b1 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 flag is set and
195b2 20 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c this is the onl
195b3 79 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20 y active writer
195b4 0a 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e . ** VM, then
195b5 20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 we do either a
195b6 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 commit or rollba
195b7 63 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ck of the curren
195b8 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a t transaction. .
195b9 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f **. ** No
195ba 74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 te: This block a
195bb 6c 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 lso runs if one
195bc 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 of the special e
195bd 72 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 rrors handled .
195be 20 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 ** above has
195bf 6f 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a occurred. . *
195c0 2f 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 /. if( !sqlit
195c1 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 e3VtabInSync(db)
195c2 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 . && db->au
195c3 74 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 toCommit . &
195c4 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 & db->writeVdbeC
195c5 6e 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 nt==(p->readOnly
195c6 3d 3d 30 29 20 0a 20 20 20 20 20 26 26 20 28 64 ==0) . && (d
195c7 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 b->flags & SQLIT
195c8 45 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 E_CommitBusy)==0
195c9 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 . ){. if
195ca 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f ( p->rc==SQLITE_
195cb 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 OK || (p->errorA
195cc 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 ction==OE_Fail &
195cd 26 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f & !isSpecialErro
195ce 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a r) ){. /*
195cf 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 The auto-commit
195d0 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61 flag is true, a
195d1 6e 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 nd the vdbe prog
195d2 72 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20 ram was .
195d3 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f ** successful o
195d4 72 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 r hit an 'OR FAI
195d5 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 L' constraint. T
195d6 68 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d his means a comm
195d7 69 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 it . ** i
195d8 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 s required..
195d9 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 */. r
195da 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 c = vdbeCommit(d
195db 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 b, p);. i
195dc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 f( rc==SQLITE_BU
195dd 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 SY ){.
195de 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
195df 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e xArrayLeave(&p->
195e0 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 aMutex);.
195e1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
195e2 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d _BUSY;. }
195e3 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c else if( rc!=SQL
195e4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
195e5 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a p->rc = rc;.
195e6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
195e7 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3RollbackAll(db)
195e8 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
195e9 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
195ea 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c e3CommitInternal
195eb 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 Changes(db);.
195ec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
195ed 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
195ee 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 te3RollbackAll(d
195ef 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 b);. }.
195f0 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 db->nStatement
195f1 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
195f2 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 if( eStatementOp
195f3 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
195f4 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f p->rc==SQLITE_O
195f5 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 K || p->errorAct
195f6 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a ion==OE_Fail ){.
195f7 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 eStateme
195f8 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 ntOp = SAVEPOINT
195f9 5f 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 _RELEASE;.
195fa 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 }else if( p->err
195fb 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f orAction==OE_Abo
195fc 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 rt ){. eS
195fd 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 tatementOp = SAV
195fe 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b EPOINT_ROLLBACK;
195ff 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19600 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 invalidate
19601 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 CursorsOnModifie
19602 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 dBtrees(db);.
19603 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c sqlite3Roll
19604 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 backAll(db);.
19605 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 sqlite3Clos
19606 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b eSavepoints(db);
19607 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 . db->aut
19608 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 oCommit = 1;.
19609 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
1960a 20 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 /* If eStateme
1960b 6e 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f ntOp is non-zero
1960c 2c 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 , then a stateme
1960d 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e nt transaction n
1960e 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 eeds to. ** b
1960f 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 e committed or r
19610 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c olled back. Call
19611 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 sqlite3VdbeClos
19612 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a eStatement() to.
19613 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 ** do so. If
19614 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
19615 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 returns an error
19616 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e , and the curren
19617 74 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 t statement.
19618 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ** error code is
19619 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 SQLITE_OK or SQ
1961a 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c LITE_CONSTRAINT,
1961b 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 then set the er
1961c 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 ror. ** code
1961d 74 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 to the new value
1961e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1961f 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b eStatementOp ){
19620 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
19621 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 te3VdbeCloseStat
19622 65 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d ement(p, eStatem
19623 65 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 entOp);. if
19624 28 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d ( rc && (p->rc==
19625 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e SQLITE_OK || p->
19626 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 rc==SQLITE_CONST
19627 52 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 RAINT) ){.
19628 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
19629 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1962a 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1962b 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e sg);. p->
1962c 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
1962d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
1962e 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 /* If this was
1962f 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 an INSERT, UPDA
19630 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 TE or DELETE and
19631 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 no statement tr
19632 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a ansaction. **
19633 20 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 has been rolled
19634 20 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 back, update th
19635 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
19636 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 ction change-cou
19637 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 nter. . */.
19638 20 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 if( p->changeC
19639 6e 74 4f 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 ntOn && p->pc>=0
1963a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53 ){. if( eS
1963b 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45 tatementOp!=SAVE
1963c 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 POINT_ROLLBACK )
1963d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1963e 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
1963f 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b db, p->nChange);
19640 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19641 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
19642 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 eSetChanges(db,
19643 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
19644 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 p->nChange = 0
19645 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
19646 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 6f * Rollback or co
19647 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 20 mmit any schema
19648 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 63 changes that occ
19649 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 urred. */. if
1964a 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f ( p->rc!=SQLITE_
1964b 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 OK && db->flags&
1964c 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
1964d 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71 nges ){. sq
1964e 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e lite3ResetIntern
1964f 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b alSchema(db, 0);
19650 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 . db->flags
19651 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20 = (db->flags |
19652 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
19653 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 nges);. }..
19654 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 /* Release the
19655 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 71 locks */. sq
19656 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
19657 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d rrayLeave(&p->aM
19658 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a utex);. }.. /*
19659 20 57 65 20 68 61 76 65 20 73 75 63 63 65 73 73 We have success
1965a 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e 64 fully halted and
1965b 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e 20 closed the VM.
1965c 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 63 Record this fac
1965d 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 t. */. if( p->p
1965e 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e c>=0 ){. db->
1965f 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d 3b activeVdbeCnt--;
19660 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 . if( !p->rea
19661 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 64 dOnly ){. d
19662 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2d b->writeVdbeCnt-
19663 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 -;. }. ass
19664 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 ert( db->activeV
19665 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74 dbeCnt>=db->writ
19666 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d 0a eVdbeCnt );. }.
19667 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
19668 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 20 E_MAGIC_HALT;.
19669 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 checkActiveVdbeC
1966a 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 2d nt(db);. if( p-
1966b 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1966c 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d d ){. p->rc =
1966d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1966e 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
1966f 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 auto-commit flag
19670 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 2c is set to true,
19671 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 20 then any locks
19672 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a 20 that were held.
19673 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f ** by connectio
19674 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 65 n db have now be
19675 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 6c en released. Cal
19676 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 l sqlite3Connect
19677 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a 20 ionUnlocked() .
19678 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 6e ** to invoke an
19679 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f 63 y required unloc
1967a 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
1967b 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 ks.. */. if( d
1967c 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b b->autoCommit ){
1967d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e . sqlite3Conn
1967e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 ectionUnlocked(d
1967f 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 b);. }.. asser
19680 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 t( db->activeVdb
19681 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 eCnt>0 || db->au
19682 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 toCommit==0 || d
19683 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 b->nStatement==0
19684 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c );. return SQL
19685 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ITE_OK;.}.../*.*
19686 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64 * Each VDBE hold
19687 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 s the result of
19688 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
19689 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63 sqlite3_step() c
1968a 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e all.** in p->rc.
1968b 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 This routine s
1968c 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20 ets that result
1968d 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f back to SQLITE_O
1968e 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 K..*/.SQLITE_PRI
1968f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
19690 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 3VdbeResetStepRe
19691 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 sult(Vdbe *p){.
19692 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
19693 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 OK;.}../*.** Cle
19694 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74 an up a VDBE aft
19695 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74 er execution but
19696 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 do not delete t
19697 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74 he VDBE just yet
19698 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65 ..** Write any e
19699 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e rror messages in
1969a 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52 to *pzErrMsg. R
1969b 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 eturn the result
1969c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 code..**.** Aft
1969d 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 er this routine
1969e 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45 is run, the VDBE
1969f 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79 should be ready
196a0 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a to be executed.
196a1 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 ** again..**.**
196a2 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e To look at it an
196a3 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20 other way, this
196a4 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74 routine resets t
196a5 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a he state of the.
196a6 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 ** virtual machi
196a7 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47 ne from VDBE_MAG
196a8 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d IC_RUN or VDBE_M
196a9 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74 AGIC_HALT back t
196aa 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f o.** VDBE_MAGIC_
196ab 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f INIT..*/.SQLITE_
196ac 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
196ad 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62 te3VdbeReset(Vdb
196ae 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 e *p){. sqlite3
196af 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e *db;. db = p->
196b0 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 db;.. /* If the
196b1 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20 VM did not run
196b2 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72 to completion or
196b3 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72 if it encounter
196b4 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 ed an. ** error
196b5 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 , then it might
196b6 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61 not have been ha
196b7 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20 lted properly.
196b8 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20 So halt. ** it
196b9 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 now.. */. (voi
196ba 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
196bb 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 n(db);. sqlite3
196bc 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 28 VdbeHalt(p);. (
196bd 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
196be 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a tyOff(db);.. /*
196bf 20 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73 If the VDBE has
196c0 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72 be run even par
196c1 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 tially, then tra
196c2 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20 nsfer the error
196c3 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 code. ** and er
196c4 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d ror message from
196c5 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74 the VDBE into t
196c6 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
196c7 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74 structure. But
196c8 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42 . ** if the VDB
196c9 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 E has just been
196ca 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68 set to run but h
196cb 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 as not actually
196cc 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a executed any. *
196cd 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 * instructions y
196ce 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61 et, leave the ma
196cf 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f in database erro
196d0 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e r information un
196d1 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 changed.. */.
196d2 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a if( p->pc>=0 ){.
196d3 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d if( p->zErrM
196d4 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 sg ){. sqli
196d5 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
196d6 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71 lloc();. sq
196d7 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
196d8 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e (db->pErr,-1,p->
196d9 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55 zErrMsg,SQLITE_U
196da 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 TF8,SQLITE_TRANS
196db 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 6c IENT);. sql
196dc 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
196dd 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 2d loc();. db-
196de 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 >errCode = p->rc
196df 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 ;. sqlite3D
196e0 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
196e1 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d 3e rMsg);. p->
196e2 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
196e3 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 }else if( p->rc
196e4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
196e5 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 3Error(db, p->rc
196e6 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , 0);. }else{
196e7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
196e8 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f ror(db, SQLITE_O
196e9 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d K, 0);. }. }
196ea 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 26 else if( p->rc &
196eb 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a & p->expired ){.
196ec 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 72 /* The expir
196ed 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 20 ed flag was set
196ee 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 6f on the VDBE befo
196ef 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61 6c re the first cal
196f0 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 l. ** to sqli
196f1 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 20 te3_step(). For
196f2 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 6e consistency (sin
196f3 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 ce sqlite3_step(
196f4 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c ) was. ** cal
196f5 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 61 led), set the da
196f6 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 20 tabase error in
196f7 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65 6c this case as wel
196f8 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 l.. */. sq
196f9 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 lite3Error(db, p
196fa 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 ->rc, 0);. sq
196fb 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
196fc 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 70 (db->pErr, -1, p
196fd 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 54 ->zErrMsg, SQLIT
196fe 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 E_UTF8, SQLITE_T
196ff 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 RANSIENT);. s
19700 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
19701 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
19702 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 p->zErrMsg = 0
19703 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c ;. }.. /* Recl
19704 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 aim all memory u
19705 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45 0a sed by the VDBE.
19706 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 70 */. Cleanup(p
19707 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 72 );.. /* Save pr
19708 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 74 ofiling informat
19709 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 44 ion from this VD
1970a 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 66 BE run.. */.#if
1970b 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 def VDBE_PROFILE
1970c 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a 6f . {. FILE *o
1970d 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 65 ut = fopen("vdbe
1970e 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 22 _profile.out", "
1970f 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 a");. if( out
19710 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b ){. int i;
19711 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
19712 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 20 ut, "---- ");.
19713 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
19714 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nOp; i++){.
19715 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
19716 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f 70 , "%02x", p->aOp
19717 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 20 [i].opcode);.
19718 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 6e }. fprin
19719 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 tf(out, "\n");.
1971a 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
1971b 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 p->nOp; i++){.
1971c 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
1971d 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 25 t, "%6d %10lld %
1971e 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 20 8lld ",.
1971f 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 p->aOp[i].cnt
19720 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e ,. p->
19721 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a 20 aOp[i].cycles,.
19722 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 p->aOp
19723 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e 61 [i].cnt>0 ? p->a
19724 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d 3e Op[i].cycles/p->
19725 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a 20 aOp[i].cnt : 0.
19726 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
19727 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
19728 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 2d ntOp(out, i, &p-
19729 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 20 >aOp[i]);.
1972a 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f }. fclose(o
1972b 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 ut);. }. }.#
1972c 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 63 endif. p->magic
1972d 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e = VDBE_MAGIC_IN
1972e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e IT;. return p->
1972f 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b rc & db->errMask
19730 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 ;.}. ./*.** Clea
19731 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20 n up and delete
19732 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 a VDBE after exe
19733 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 cution. Return
19734 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 an integer which
19735 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c is.** the resul
19736 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61 t code. Write a
19737 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 ny error message
19738 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72 text into *pzEr
19739 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rMsg..*/.SQLITE_
1973a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1973b 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 te3VdbeFinalize(
1973c 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
1973d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1973e 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d if( p->magic==
1973f 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c VDBE_MAGIC_RUN |
19740 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 | p->magic==VDBE
19741 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20 _MAGIC_HALT ){.
19742 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
19743 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20 dbeReset(p);.
19744 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70 assert( (rc & p
19745 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d ->db->errMask)==
19746 72 63 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 rc );. }else if
19747 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 ( p->magic!=VDBE
19748 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 7b 0a 20 _MAGIC_INIT ){.
19749 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1974a 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 _MISUSE;. }. s
1974b 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
1974c 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
1974d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 ;.}../*.** Call
1974e 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 the destructor f
1974f 6f 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20 or each auxdata
19750 65 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 entry in pVdbeFu
19751 6e 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 nc for which.**
19752 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
19753 67 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 g bit in mask is
19754 20 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 clear. Auxdata
19755 20 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 entries beyond
19756 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 31.** are always
19757 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 destroyed. To
19758 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 destroy all auxd
19759 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c ata entries, cal
1975a 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e l this.** routin
1975b 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a e with mask==0..
1975c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1975d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1975e 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 beDeleteAuxData(
1975f 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 VdbeFunc *pVdbeF
19760 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a unc, int mask){.
19761 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
19762 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d =0; i<pVdbeFunc-
19763 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nAux; i++){.
19764 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 struct AuxData
19765 2a 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 *pAux = &pVdbeFu
19766 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 nc->apAux[i];.
19767 20 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 if( (i>31 || !
19768 28 6d 61 73 6b 26 28 31 3c 3c 69 29 29 29 20 26 (mask&(1<<i))) &
19769 26 20 70 41 75 78 2d 3e 70 41 75 78 20 29 7b 0a & pAux->pAux ){.
1976a 20 20 20 20 20 20 69 66 28 20 70 41 75 78 2d 3e if( pAux->
1976b 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 xDelete ){.
1976c 20 20 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 pAux->xDelete
1976d 28 70 41 75 78 2d 3e 70 41 75 78 29 3b 0a 20 20 (pAux->pAux);.
1976e 20 20 20 20 7d 0a 20 20 20 20 20 20 70 41 75 78 }. pAux
1976f 2d 3e 70 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 ->pAux = 0;.
19770 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 }. }.}../*.** D
19771 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 elete an entire
19772 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f VDBE..*/.SQLITE_
19773 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19774 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 56 ite3VdbeDelete(V
19775 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 dbe *p){. int i
19776 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
19777 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 .. if( p==0 ) r
19778 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 2d eturn;. db = p-
19779 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 >db;. if( p->pP
1977a 72 65 76 20 29 7b 0a 20 20 20 20 70 2d 3e 70 50 rev ){. p->pP
1977b 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e rev->pNext = p->
1977c 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a pNext;. }else{.
1977d 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1977e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 20 20 20 pVdbe==p );.
1977f 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 2d 3e 70 db->pVdbe = p->p
19780 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Next;. }. if(
19781 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 p->pNext ){.
19782 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 p->pNext->pPrev
19783 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a = p->pPrev;. }.
19784 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a if( p->aOp ){.
19785 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d 20 70 2d Op *pOp = p-
19786 3e 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d >aOp;. for(i=
19787 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 0; i<p->nOp; i++
19788 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 , pOp++){.
19789 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e freeP4(db, pOp->
1978a 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e p4type, pOp->p4.
1978b 70 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 p);.#ifdef SQLIT
1978c 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 73 71 E_DEBUG. sq
1978d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1978e 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b 0a pOp->zComment);.
1978f 23 65 6e 64 69 66 20 20 20 20 20 0a 20 20 20 20 #endif .
19790 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d }. }. releaseM
19791 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c emArray(p->aVar,
19792 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 73 71 6c p->nVar);. sql
19793 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
19794 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 72 65 6c ->aLabel);. rel
19795 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e easeMemArray(p->
19796 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 aColName, p->nRe
19797 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f sColumn*COLNAME_
19798 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 N);. sqlite3DbF
19799 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e ree(db, p->aColN
1979a 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 ame);. sqlite3D
1979b 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 53 71 bFree(db, p->zSq
1979c 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d l);. p->magic =
1979d 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 44 VDBE_MAGIC_DEAD
1979e 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1979f 65 28 64 62 2c 20 70 2d 3e 61 4f 70 29 3b 0a 20 e(db, p->aOp);.
197a0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
197a1 62 2c 20 70 2d 3e 70 46 72 65 65 29 3b 0a 20 20 b, p->pFree);.
197a2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
197a3 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 , p);.}../*.** I
197a4 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61 f a MoveTo opera
197a5 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20 tion is pending
197a6 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 on the given cur
197a7 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61 sor, then do tha
197a8 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e t.** MoveTo now.
197a9 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f Return an erro
197aa 72 20 63 6f 64 65 2e 20 20 49 66 20 6e 6f 20 4d r code. If no M
197ab 6f 76 65 54 6f 20 69 73 20 70 65 6e 64 69 6e 67 oveTo is pending
197ac 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e , this.** routin
197ad 65 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67 20 61 e does nothing a
197ae 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 nd returns SQLIT
197af 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
197b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
197b1 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
197b2 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 2a eto(VdbeCursor *
197b3 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 66 p){. if( p->def
197b4 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 erredMoveto ){.
197b5 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b 0a int res, rc;.
197b6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
197b7 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e ST. extern in
197b8 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 t sqlite3_search
197b9 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 _count;.#endif.
197ba 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 assert( p->is
197bb 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 20 Table );. rc
197bc 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
197bd 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e vetoUnpacked(p->
197be 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d pCursor, 0, p->m
197bf 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20 ovetoTarget, 0,
197c0 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 &res);. if( r
197c1 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
197c2 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20 p->lastRowid
197c3 3d 20 6b 65 79 54 6f 49 6e 74 28 70 2d 3e 6d 6f = keyToInt(p->mo
197c4 76 65 74 6f 54 61 72 67 65 74 29 3b 0a 20 20 20 vetoTarget);.
197c5 20 70 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 p->rowidIsValid
197c6 20 3d 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a = res==0 ?1:0;.
197c7 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 29 7b if( res<0 ){
197c8 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
197c9 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 2d 3e te3BtreeNext(p->
197ca 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a pCursor, &res);.
197cb 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
197cc 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
197cd 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
197ce 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ST. sqlite3_s
197cf 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 earch_count++;.#
197d0 65 6e 64 69 66 0a 20 20 20 20 70 2d 3e 64 65 66 endif. p->def
197d1 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b erredMoveto = 0;
197d2 0a 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 . p->cacheSta
197d3 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
197d4 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 E;. }else if( p
197d5 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 ->pCursor ){.
197d6 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 int hasMoved;.
197d7 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 int rc = sqli
197d8 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 te3BtreeCursorHa
197d9 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f sMoved(p->pCurso
197da 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 r, &hasMoved);.
197db 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
197dc 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 rn rc;. if( h
197dd 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 asMoved ){.
197de 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 p->cacheStatus
197df 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
197e0 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 p->nullRow
197e1 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 1;. }. }.
197e2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
197e3 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K;.}../*.** The
197e4 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 following functi
197e5 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 ons:.**.** sqlit
197e6 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
197e7 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 ().** sqlite3Vdb
197e8 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 eSerialTypeLen()
197e9 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 .** sqlite3VdbeS
197ea 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 erialLen().** sq
197eb 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 lite3VdbeSerialP
197ec 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 ut().** sqlite3V
197ed 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a dbeSerialGet().*
197ee 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 *.** encapsulate
197ef 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 the code that s
197f0 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 erializes values
197f1 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 for storage in
197f2 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 SQLite.** data a
197f3 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 nd index records
197f4 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 . Each serialize
197f5 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 d value consists
197f6 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c of a.** 'serial
197f7 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f -type' and a blo
197f8 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 b of data. The s
197f9 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e erial type is an
197fa 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 8-byte unsigned
197fb 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f .** integer, sto
197fc 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e red as a varint.
197fd 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c .**.** In an SQL
197fe 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 ite index record
197ff 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 , the serial typ
19800 65 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 e is stored dire
19801 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 ctly before.** t
19802 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 he blob of data
19803 74 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f that it correspo
19804 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 nds to. In a tab
19805 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 le record, all s
19806 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 erial.** types a
19807 72 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 re stored at the
19808 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 start of the re
19809 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c cord, and the bl
1980a 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a obs of data at.*
1980b 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 * the end. Hence
1980c 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
1980d 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 allow the calle
1980e 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a r to handle the.
1980f 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 ** serial-type a
19810 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 nd data blob sep
19811 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 erately..**.** T
19812 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 he following tab
19813 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 le describes the
19814 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 various storage
19815 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 classes for dat
19816 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 a:.**.** seria
19817 6c 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79 l type by
19818 74 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 tes of data
19819 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d type.** -----
1981a 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d --------- --
1981b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 -------------
1981c 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
1981d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 .** 0
1981e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
1981f 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c NULL
19820 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 .** 1
19821 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
19822 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e sign
19823 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 ed integer.**
19824 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 2
19825 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 2
19826 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 signed int
19827 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 eger.** 3
19828 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19829 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 3
1982a 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a signed integer.*
1982b 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 * 4
1982c 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20 4
1982d 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 signed
1982e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
1982f 20 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5
19830 20 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 6
19831 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 signed integ
19832 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 er.** 6
19833 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19834 20 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69 8 si
19835 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 gned integer.**
19836 20 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20 7
19837 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 8
19838 20 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f IEEE flo
19839 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 at.** 8
1983a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1983b 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 0 In
1983c 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 teger constant 0
1983d 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 .** 9
1983e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
1983f 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 Inte
19840 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a ger constant 1.*
19841 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 * 10,11
19842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19843 20 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76 reserv
19844 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e ed for expansion
19845 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 .** N>=12 and
19846 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 even (N-1
19847 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 2)/2 BLOB
19848 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 .** N>=13 and
19849 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 odd (N-1
1984a 33 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 3)/2 text
1984b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 .**.** The 8 and
1984c 20 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64 9 types were ad
1984d 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 ded in 3.3.0, fi
1984e 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 le format 4. Pr
1984f 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 ior versions.**
19850 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e of SQLite will n
19851 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 ot understand th
19852 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 ose serial types
19853 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ..*/../*.** Retu
19854 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 rn the serial-ty
19855 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 pe for the value
19856 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e stored in pMem.
19857 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19858 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 TE u32 sqlite3Vd
19859 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d beSerialType(Mem
1985a 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 *pMem, int file
1985b 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 _format){. int
1985c 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c flags = pMem->fl
1985d 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 ags;. int n;..
1985e 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e if( flags&MEM_N
1985f 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ull ){. retur
19860 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 n 0;. }. if( f
19861 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a lags&MEM_Int ){.
19862 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 /* Figure ou
19863 74 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 t whether to use
19864 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 1, 2, 4, 6 or 8
19865 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 bytes. */.# d
19866 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 efine MAX_6BYTE
19867 28 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30 ((((i64)0x000080
19868 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 00)<<32)-1).
19869 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e i64 i = pMem->u.
1986a 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 i;. u64 u;.
1986b 20 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 if( file_forma
1986c 74 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 t>=4 && (i&1)==i
1986d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1986e 20 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 8+(u32)i;. }
1986f 0a 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d . u = i<0 ? -
19870 69 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 i : i;. if( u
19871 3c 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 <=127 ) return 1
19872 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 ;. if( u<=327
19873 36 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 67 ) return 2;.
19874 20 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 if( u<=838860
19875 37 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7 ) return 3;.
19876 20 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 if( u<=2147483
19877 36 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 647 ) return 4;.
19878 20 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 if( u<=MAX_6
19879 42 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b BYTE ) return 5;
1987a 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 . return 6;.
1987b 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d }. if( flags&M
1987c 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 EM_Real ){. r
1987d 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 eturn 7;. }. a
1987e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d ssert( pMem->db-
1987f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c >mallocFailed ||
19880 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c flags&(MEM_Str|
19881 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e MEM_Blob) );. n
19882 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 = pMem->n;. if
19883 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 ( flags & MEM_Ze
19884 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 ro ){. n += p
19885 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 Mem->u.nZero;.
19886 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 }. assert( n>=0
19887 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e );. return ((n
19888 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 *2) + 12 + ((fla
19889 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 gs&MEM_Str)!=0))
1988a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1988b 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 n the length of
1988c 74 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 the data corresp
1988d 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 onding to the su
1988e 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 pplied serial-ty
1988f 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 pe..*/.SQLITE_PR
19890 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19891 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 3VdbeSerialTypeL
19892 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 en(u32 serial_ty
19893 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 pe){. if( seria
19894 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 l_type>=12 ){.
19895 20 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c return (serial
19896 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d _type-12)/2;. }
19897 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 else{. static
19898 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b const u8 aSize[
19899 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 ] = { 0, 1, 2, 3
1989a 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c , 4, 6, 8, 8, 0,
1989b 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 0, 0, 0 };.
1989c 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 return aSize[ser
1989d 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d ial_type];. }.}
1989e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 ../*.** If we ar
1989f 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 e on an architec
198a0 74 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d ture with mixed-
198a1 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 endian floating
198a2 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 .** points (ex:
198a3 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 ARM7) then swap
198a4 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 the lower 4 byte
198a5 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 s with the .** u
198a6 70 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 pper 4 bytes. R
198a7 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 eturn the result
198a8 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 ..**.** For most
198a9 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 architectures,
198aa 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e this is a no-op.
198ab 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 .**.** (later):
198ac 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 It is reported
198ad 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d to me that the m
198ae 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 ixed-endian prob
198af 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 lem.** on ARM7 i
198b0 73 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 s an issue with
198b1 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 GCC, not with th
198b2 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 e ARM7 chip. It
198b3 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 seems.** that e
198b4 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 arly versions of
198b5 20 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 GCC stored the
198b6 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 two words of a 6
198b7 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 4-bit.** float i
198b8 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 n the wrong orde
198b9 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 r. And that err
198ba 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 or has been prop
198bb 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 agated.** ever s
198bc 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 ince. The blame
198bd 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
198be 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 ily with GCC, th
198bf 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 ough..** GCC mig
198c0 68 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 ht have just cop
198c1 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d ying the problem
198c2 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f from a prior co
198c3 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 mpiler..** I am
198c4 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e also told that n
198c5 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 ewer versions of
198c6 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 GCC that follow
198c7 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 a different.**
198c8 41 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65 ABI get the byte
198c9 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a order right..**
198ca 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 .** Developers u
198cb 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 sing SQLite on a
198cc 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f n ARM7 should co
198cd 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 mpile and run th
198ce 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 eir.** applicati
198cf 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 on using -DSQLIT
198d0 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 E_DEBUG=1 at lea
198d1 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 st once. With D
198d2 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c EBUG.** enabled,
198d3 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 some asserts be
198d4 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 low will ensure
198d5 74 68 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 that the byte or
198d6 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 der of.** floati
198d7 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 ng point values
198d8 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a is correct..**.*
198d9 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 * (2007-08-30)
198da 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 Frank van Vugt h
198db 61 73 20 73 74 75 64 69 65 64 20 74 68 69 73 20 as studied this
198dc 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a problem closely.
198dd 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 ** and has send
198de 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 his findings to
198df 74 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c the SQLite devel
198e0 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a opers. Frank.**
198e1 20 77 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d writes that som
198e2 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 e Linux kernels
198e3 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 offer floating p
198e4 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a oint hardware.**
198e5 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 emulation that
198e6 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 uses only 32-bit
198e7 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 mantissas inste
198e8 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a ad of a full .**
198e9 20 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 48-bits as requ
198ea 69 72 65 64 20 62 79 20 74 68 65 20 49 45 45 45 ired by the IEEE
198eb 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 standard. (Thi
198ec 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 s is the.** CONF
198ed 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f IG_FPE_FASTFPE o
198ee 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 ption.) On such
198ef 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 systems, floati
198f0 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 ng point.** byte
198f1 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 swapping become
198f2 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 s very complicat
198f3 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 ed. To avoid pr
198f4 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e oblems,.** the n
198f5 65 63 65 73 73 61 72 79 20 62 79 74 65 20 73 77 ecessary byte sw
198f6 61 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 apping is carrie
198f7 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 d out using a 64
198f8 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 -bit integer.**
198f9 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 rather than a 64
198fa 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 -bit float. Fra
198fb 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68 nk assures us th
198fc 61 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 at the code here
198fd 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 .** works for hi
198fe 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 m. We, the deve
198ff 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 lopers, have no
19900 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 way to independe
19901 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 ntly.** verify t
19902 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 his, but Frank s
19903 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 eems to know wha
19904 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 t he is talking
19905 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 about.** so we t
19906 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 rust him..*/.#if
19907 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 def SQLITE_MIXED
19908 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c _ENDIAN_64BIT_FL
19909 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 OAT.static u64 f
1990a 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 loatSwap(u64 in)
1990b 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 {. union {.
1990c 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 u64 r;. u32 i
1990d 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 [2];. } u;. u3
1990e 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 2 t;.. u.r = in
1990f 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a ;. t = u.i[0];.
19910 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 u.i[0] = u.i[1
19911 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b ];. u.i[1] = t;
19912 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d . return u.r;.}
19913 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 .# define swapMi
19914 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 xedEndianFloat(X
19915 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 ) X = floatSwap
19916 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 (X).#else.# defi
19917 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 ne swapMixedEndi
19918 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 anFloat(X).#endi
19919 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 f../*.** Write t
1991a 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 he serialized da
1991b 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 ta blob for the
1991c 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 value stored in
1991d 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 pMem into .** bu
1991e 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 f. It is assumed
1991f 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 that the caller
19920 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 has allocated s
19921 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e ufficient space.
19922 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
19923 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 umber of bytes w
19924 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 ritten..**.** nB
19925 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 uf is the amount
19926 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 of space left i
19927 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d n buf[]. nBuf m
19928 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a ust always be.**
19929 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f large enough to
1992a 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 hold the entire
1992b 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c field. Except,
1992c 20 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 if the field is
1992d 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 .** a blob with
1992e 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 a zero-filled ta
1992f 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d il, then buf[] m
19930 69 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 ight be just the
19931 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 right.** size t
19932 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e o hold everythin
19933 67 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 g except for the
19934 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 zero-filled tai
19935 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 l. If buf[].**
19936 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 is only big enou
19937 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e gh to hold the n
19938 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 on-zero prefix,
19939 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 then only write
1993a 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 that.** prefix i
1993b 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 nto buf[]. But
1993c 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 if buf[] is larg
1993d 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 e enough to hold
1993e 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 both the.** pre
1993f 66 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c fix and the tail
19940 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 then write the
19941 70 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 prefix and set t
19942 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a he tail to all.*
19943 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 * zeros..**.** R
19944 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
19945 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c of bytes actual
19946 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 ly written into
19947 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 buf[]. The numb
19948 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 er.** of bytes i
19949 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 n the zero-fille
1994a 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 d tail is includ
1994b 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e ed in the return
1994c 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 value only.** i
1994d 66 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 f those bytes we
1994e 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 re zeroed in buf
1994f 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 []..*/ .SQLITE_P
19950 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19951 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 e3VdbeSerialPut(
19952 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 u8 *buf, int nBu
19953 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e f, Mem *pMem, in
19954 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a t file_format){.
19955 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
19956 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 e = sqlite3VdbeS
19957 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 erialType(pMem,
19958 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 file_format);.
19959 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 int len;.. /* I
1995a 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 nteger and Real
1995b 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f */. if( serial_
1995c 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 type<=7 && seria
1995d 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 l_type>0 ){.
1995e 75 36 34 20 76 3b 0a 20 20 20 20 69 6e 74 20 69 u64 v;. int i
1995f 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 6c ;. if( serial
19960 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 _type==7 ){.
19961 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
19962 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d (v)==sizeof(pMem
19963 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 ->r) );. me
19964 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e mcpy(&v, &pMem->
19965 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 r, sizeof(v));.
19966 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e swapMixedEn
19967 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 dianFloat(v);.
19968 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 }else{. v
19969 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 = pMem->u.i;.
1996a 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 }. len = i
1996b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
1996c 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 ialTypeLen(seria
1996d 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 l_type);. ass
1996e 65 72 74 28 20 6c 65 6e 3c 3d 6e 42 75 66 20 29 ert( len<=nBuf )
1996f 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d ;. while( i--
19970 20 29 7b 0a 20 20 20 20 20 20 62 75 66 5b 69 5d ){. buf[i]
19971 20 3d 20 28 75 38 29 28 76 26 30 78 46 46 29 3b = (u8)(v&0xFF);
19972 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a . v >>= 8;.
19973 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
19974 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 len;. }.. /*
19975 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 2a String or blob *
19976 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 /. if( serial_t
19977 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 61 ype>=12 ){. a
19978 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 20 2b ssert( pMem->n +
19979 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ((pMem->flags &
1997a 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65 6d 2d MEM_Zero)?pMem-
1997b 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20 20 20 20 >u.nZero:0).
1997c 20 20 20 20 20 20 20 20 20 3d 3d 20 73 71 6c 69 == sqli
1997d 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1997e 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 eLen(serial_type
1997f 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ) );. assert(
19980 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 pMem->n<=nBuf )
19981 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d ;. len = pMem
19982 2d 3e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 ->n;. memcpy(
19983 62 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 buf, pMem->z, le
19984 6e 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d n);. if( pMem
19985 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 ->flags & MEM_Ze
19986 72 6f 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20 ro ){. len
19987 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f += pMem->u.nZero
19988 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3e ;. if( len>
19989 6e 42 75 66 20 29 7b 0a 20 20 20 20 20 20 20 20 nBuf ){.
1998a 6c 65 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 20 20 len = nBuf;.
1998b 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 }. memset
1998c 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 (&buf[pMem->n],
1998d 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0, len-pMem->n);
1998e 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
1998f 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a n len;. }.. /*
19990 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e NULL or constan
19991 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 ts 0 or 1 */. r
19992 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
19993 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68 * Deserialize th
19994 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e e data blob poin
19995 74 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73 ted to by buf as
19996 20 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72 serial type ser
19997 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 ial_type.** and
19998 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
19999 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 in pMem. Retur
1999a 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1999b 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a bytes read..*/ .
1999c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1999d 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 nt sqlite3VdbeSe
1999e 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 rialGet(. const
1999f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
199a0 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 buf, /* Buff
199a1 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a er to deserializ
199a2 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 e from */. u32
199a3 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 serial_type,
199a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72 /* Ser
199a5 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65 ial type to dese
199a6 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d rialize */. Mem
199a7 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20 *pMem
199a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 /* Me
199a9 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 mory cell to wri
199aa 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f te value into */
199ab 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65 .){. switch( se
199ac 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 rial_type ){.
199ad 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 case 10: /* R
199ae 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 eserved for futu
199af 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 re use */. ca
199b0 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 se 11: /* Rese
199b1 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 rved for future
199b2 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 use */. case
199b3 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0: { /* NULL */
199b4 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
199b5 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
199b6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
199b7 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20 }. case 1: {
199b8 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 1-byte signed
199b9 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 integer */.
199ba 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 pMem->u.i = (s
199bb 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 igned char)buf[0
199bc 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 ];. pMem->f
199bd 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
199be 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
199bf 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32 }. case 2
199c0 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 : { /* 2-byte si
199c1 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
199c2 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 pMem->u.i
199c3 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 = (((signed char
199c4 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 )buf[0])<<8) | b
199c5 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 uf[1];. pMe
199c6 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
199c7 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e nt;. return
199c8 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 2;. }. ca
199c9 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 se 3: { /* 3-byt
199ca 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 e signed integer
199cb 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e */. pMem->
199cc 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 u.i = (((signed
199cd 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 char)buf[0])<<16
199ce 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 ) | (buf[1]<<8)
199cf 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 | buf[2];.
199d0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
199d1 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 M_Int;. ret
199d2 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 3;. }.
199d3 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d case 4: { /* 4-
199d4 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 byte signed inte
199d5 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 ger */. pMe
199d6 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d m->u.i = (buf[0]
199d7 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c <<24) | (buf[1]<
199d8 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c <16) | (buf[2]<<
199d9 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 8) | buf[3];.
199da 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
199db 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
199dc 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a return 4;. }.
199dd 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a case 5: { /*
199de 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6-byte signed i
199df 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 nteger */.
199e0 75 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65 u64 x = (((signe
199e1 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c d char)buf[0])<<
199e2 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 8) | buf[1];.
199e3 20 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b u32 y = (buf[
199e4 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 2]<<24) | (buf[3
199e5 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d ]<<16) | (buf[4]
199e6 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 <<8) | buf[5];.
199e7 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 x = (x<<32)
199e8 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d | y;. pMem
199e9 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 ->u.i = *(i64*)&
199ea 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 x;. pMem->f
199eb 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
199ec 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a return 6;.
199ed 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36 }. case 6
199ee 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 : /* 8-byte si
199ef 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
199f0 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a case 7: { /*
199f1 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 IEEE floating p
199f2 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 oint */. u6
199f3 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79 4 x;. u32 y
199f4 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e ;.#if !defined(N
199f5 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e DEBUG) && !defin
199f6 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ed(SQLITE_OMIT_F
199f7 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 LOATING_POINT).
199f8 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 /* Verify t
199f9 68 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64 hat integers and
199fa 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
199fb 76 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73 values use the s
199fc 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 ame. ** byt
199fd 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 e order. Or, th
199fe 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 at if SQLITE_MIX
199ff 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f ED_ENDIAN_64BIT_
19a00 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a FLOAT is. *
19a01 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36 * defined that 6
19a02 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 4-bit floating p
19a03 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c oint values real
19a04 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20 ly are mixed.
19a05 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 ** endian..
19a06 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 */. sta
19a07 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31 tic const u64 t1
19a08 20 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30 = ((u64)0x3ff00
19a09 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 000)<<32;.
19a0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 static const dou
19a0b 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 ble r1 = 1.0;.
19a0c 20 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b u64 t2 = t1;
19a0d 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 . swapMixed
19a0e 45 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b EndianFloat(t2);
19a0f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
19a10 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f izeof(r1)==sizeo
19a11 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 f(t2) && memcmp(
19a12 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 &r1, &t2, sizeof
19a13 28 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 (r1))==0 );.#end
19a14 69 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62 if.. x = (b
19a15 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 uf[0]<<24) | (bu
19a16 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 f[1]<<16) | (buf
19a17 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d [2]<<8) | buf[3]
19a18 3b 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66 ;. y = (buf
19a19 5b 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b [4]<<24) | (buf[
19a1a 35 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5]<<16) | (buf[6
19a1b 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a ]<<8) | buf[7];.
19a1c 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 x = (x<<32
19a1d 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28 ) | y;. if(
19a1e 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 serial_type==6
19a1f 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d ){. pMem-
19a20 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 >u.i = *(i64*)&x
19a21 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e ;. pMem->
19a22 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
19a23 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19a24 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 assert( si
19a25 7a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 zeof(x)==8 && si
19a26 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 zeof(pMem->r)==8
19a27 20 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70 );. swap
19a28 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 MixedEndianFloat
19a29 28 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d (x);. mem
19a2a 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 cpy(&pMem->r, &x
19a2b 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 , sizeof(x));.
19a2c 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
19a2d 73 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e s = sqlite3IsNaN
19a2e 28 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f (pMem->r) ? MEM_
19a2f 4e 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b Null : MEM_Real;
19a30 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
19a31 65 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 eturn 8;. }.
19a32 20 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a case 8: /*
19a33 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 Integer 0 */.
19a34 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 case 9: { /*
19a35 49 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 Integer 1 */.
19a36 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 pMem->u.i = s
19a37 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 erial_type-8;.
19a38 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
19a39 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 = MEM_Int;.
19a3a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
19a3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
19a3c 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 int len =
19a3d 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 (serial_type-12)
19a3e 2f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e /2;. pMem->
19a3f 7a 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b z = (char *)buf;
19a40 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d . pMem->n =
19a41 20 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d len;. pMem
19a42 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 ->xDel = 0;.
19a43 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 if( serial_typ
19a44 65 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20 e&0x01 ){.
19a45 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19a46 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 MEM_Str | MEM_Ep
19a47 68 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 hem;. }else
19a48 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e {. pMem->
19a49 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 flags = MEM_Blob
19a4a 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 | MEM_Ephem;.
19a4b 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
19a4c 72 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 rn len;. }.
19a4d 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
19a4e 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 ../*.** Given th
19a4f 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f e nKey-byte enco
19a50 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64 ding of a record
19a51 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 in pKey[], pars
19a52 65 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 e the.** record
19a53 69 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 into a UnpackedR
19a54 65 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e ecord structure.
19a55 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 Return a point
19a56 65 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 er to.** that st
19a57 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
19a58 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
19a59 69 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64 ion might provid
19a5a 65 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20 e szSpace bytes
19a5b 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 of memory.** spa
19a5c 63 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 ce at pSpace. T
19a5d 68 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65 his space can be
19a5e 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 used to hold th
19a5f 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 e returned.** VD
19a60 62 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73 beParsedRecord s
19a61 74 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69 tructure if it i
19a62 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 s large enough.
19a63 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 If it is.** not
19a64 20 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 big enough, spa
19a65 63 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 ce is obtained f
19a66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
19a67 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 oc()..**.** The
19a68 72 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75 returned structu
19a69 72 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f re should be clo
19a6a 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f sed by a call to
19a6b 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44 .** sqlite3VdbeD
19a6c 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
19a6d 6f 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 ord()..*/ .SQLIT
19a6e 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b E_PRIVATE Unpack
19a6f 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 edRecord *sqlite
19a70 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 3VdbeRecordUnpac
19a71 6b 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b k(. KeyInfo *pK
19a72 65 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 eyInfo, /* I
19a73 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
19a74 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d the record form
19a75 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 at */. int nKey
19a76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
19a77 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69 * Size of the bi
19a78 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 nary record */.
19a79 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
19a7a 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 y, /* The b
19a7b 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a inary record */.
19a7c 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
19a7d 20 2a 70 53 70 61 63 65 2c 2f 2a 20 53 70 61 63 *pSpace,/* Spac
19a7e 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68 e available to h
19a7f 6f 6c 64 20 72 65 73 75 6c 74 69 6e 67 20 6f 62 old resulting ob
19a80 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 7a ject */. int sz
19a81 53 70 61 63 65 20 20 20 20 20 20 20 20 20 20 20 Space
19a82 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 61 /* Size of pSpa
19a83 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f ce[] in bytes */
19a84 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 .){. const unsi
19a85 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 20 gned char *aKey
19a86 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 = (const unsigne
19a87 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 d char *)pKey;.
19a88 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
19a89 2a 70 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c *p;. int nByte,
19a8a 20 64 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 d;. u32 idx;.
19a8b 20 75 31 36 20 75 3b 20 20 20 20 20 20 20 20 20 u16 u;
19a8c 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 /* Unsig
19a8d 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 ned loop counter
19a8e 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 3b */. u32 szHdr;
19a8f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 . Mem *pMem;.
19a90 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f . assert( sizeo
19a91 66 28 4d 65 6d 29 3e 73 69 7a 65 6f 66 28 2a 70 f(Mem)>sizeof(*p
19a92 29 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 ) );. nByte = s
19a93 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 4b 65 79 izeof(Mem)*(pKey
19a94 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 32 29 3b Info->nField+2);
19a95 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 73 7a 53 . if( nByte>szS
19a96 70 61 63 65 20 29 7b 0a 20 20 20 20 70 20 3d 20 pace ){. p =
19a97 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
19a98 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c aw(pKeyInfo->db,
19a99 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 nByte);. if(
19a9a 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
19a9b 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d ;. p->flags =
19a9c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 46 UNPACKED_NEED_F
19a9d 52 45 45 20 7c 20 55 4e 50 41 43 4b 45 44 5f 4e REE | UNPACKED_N
19a9e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 7d EED_DESTROY;. }
19a9f 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 70 53 else{. p = pS
19aa0 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 pace;. p->fla
19aa1 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 gs = UNPACKED_NE
19aa2 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 7d 0a ED_DESTROY;. }.
19aa3 20 20 70 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 p->pKeyInfo =
19aa4 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e pKeyInfo;. p->n
19aa5 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f Field = pKeyInfo
19aa6 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20 ->nField + 1;.
19aa7 70 2d 3e 61 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d p->aMem = pMem =
19aa8 20 26 28 28 4d 65 6d 2a 29 70 29 5b 31 5d 3b 0a &((Mem*)p)[1];.
19aa9 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e idx = getVarin
19aaa 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29 t32(aKey, szHdr)
19aab 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20 ;. d = szHdr;.
19aac 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 u = 0;. while(
19aad 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c idx<szHdr && u<
19aae 70 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 p->nField ){.
19aaf 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 u32 serial_type
19ab0 3b 0a 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65 ;.. idx += ge
19ab1 74 56 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b tVarint32(&aKey[
19ab2 69 64 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 idx], serial_typ
19ab3 65 29 3b 0a 20 20 20 20 69 66 28 20 64 3e 3d 6e e);. if( d>=n
19ab4 4b 65 79 20 26 26 20 73 71 6c 69 74 65 33 56 64 Key && sqlite3Vd
19ab5 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
19ab6 73 65 72 69 61 6c 5f 74 79 70 65 29 3e 30 20 29 serial_type)>0 )
19ab7 20 62 72 65 61 6b 3b 0a 20 20 20 20 70 4d 65 6d break;. pMem
19ab8 2d 3e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f ->enc = pKeyInfo
19ab9 2d 3e 65 6e 63 3b 0a 20 20 20 20 70 4d 65 6d 2d ->enc;. pMem-
19aba 3e 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e >db = pKeyInfo->
19abb 64 62 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c db;. pMem->fl
19abc 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 ags = 0;. pMe
19abd 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a m->zMalloc = 0;.
19abe 20 20 20 20 64 20 2b 3d 20 73 71 6c 69 74 65 33 d += sqlite3
19abf 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 VdbeSerialGet(&a
19ac0 4b 65 79 5b 64 5d 2c 20 73 65 72 69 61 6c 5f 74 Key[d], serial_t
19ac1 79 70 65 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 ype, pMem);.
19ac2 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b pMem++;. u++;
19ac3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75 . }. assert( u
19ac4 3c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 <=pKeyInfo->nFie
19ac5 6c 64 20 2b 20 31 20 29 3b 0a 20 20 70 2d 3e 6e ld + 1 );. p->n
19ac6 46 69 65 6c 64 20 3d 20 75 3b 0a 20 20 72 65 74 Field = u;. ret
19ac7 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a urn (void*)p;.}.
19ac8 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
19ac9 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 55 ine destroys a U
19aca 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 6f 62 npackedRecord ob
19acb 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ject.*/.SQLITE_P
19acc 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
19acd 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 te3VdbeDeleteUnp
19ace 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70 61 ackedRecord(Unpa
19acf 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 29 7b 0a ckedRecord *p){.
19ad0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 if( p ){. i
19ad1 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e f( p->flags & UN
19ad2 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 PACKED_NEED_DEST
19ad3 52 4f 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ROY ){. int
19ad4 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 i;. Mem *p
19ad5 4d 65 6d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 Mem;. for(i
19ad6 3d 30 2c 20 70 4d 65 6d 3d 70 2d 3e 61 4d 65 6d =0, pMem=p->aMem
19ad7 3b 20 69 3c 70 2d 3e 6e 46 69 65 6c 64 3b 20 69 ; i<p->nField; i
19ad8 2b 2b 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 ++, pMem++){.
19ad9 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a if( pMem->z
19ada 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 Malloc ){.
19adb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19adc 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
19add 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
19ade 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
19adf 20 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 p->flags & UNPA
19ae0 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 29 CKED_NEED_FREE )
19ae1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
19ae2 62 46 72 65 65 28 70 2d 3e 70 4b 65 79 49 6e 66 bFree(p->pKeyInf
19ae3 6f 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d o->db, p);. }
19ae4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
19ae5 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 is function comp
19ae6 61 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 ares the two tab
19ae7 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 le rows or index
19ae8 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 records.** spec
19ae9 69 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c ified by {nKey1,
19aea 20 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 pKey1} and pPKe
19aeb 79 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 y2. It returns
19aec 61 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f a negative, zero
19aed 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 .** or positive
19aee 69 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 integer if key1
19aef 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 is less than, eq
19af0 75 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 ual to or .** gr
19af1 65 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e eater than key2.
19af2 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b The {nKey1, pK
19af3 65 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 ey1} key must be
19af4 20 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 a blob.** creat
19af5 65 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 ed by th OP_Make
19af6 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 Record opcode of
19af7 20 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 the VDBE. The
19af8 70 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 pPKey2.** key mu
19af9 73 74 20 62 65 20 61 20 70 61 72 73 65 64 20 6b st be a parsed k
19afa 65 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 ey such as obtai
19afb 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 ned from.** sqli
19afc 74 65 33 56 64 62 65 50 61 72 73 65 52 65 63 6f te3VdbeParseReco
19afd 72 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 rd..**.** Key1 a
19afe 6e 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 nd Key2 do not h
19aff 61 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 ave to contain t
19b00 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f he same number o
19b01 66 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 f fields..** The
19b02 20 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 20 key with fewer
19b03 66 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c fields is usuall
19b04 79 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 y compares less
19b05 74 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e than the .** lon
19b06 67 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 ger key. Howeve
19b07 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 r if the UNPACKE
19b08 44 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 D_INCRKEY flags
19b09 69 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 in pPKey2 is set
19b0a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d .** and the comm
19b0b 6f 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 20 on prefixes are
19b0c 65 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 equal, then key1
19b0d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 is less than ke
19b0e 79 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 y2..** Or if the
19b0f 20 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f UNPACKED_MATCH_
19b10 50 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 PREFIX flag is s
19b11 65 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 et and the prefi
19b12 78 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c xes are.** equal
19b13 2c 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 , then the keys
19b14 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 are considered t
19b15 6f 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a o be equal and.*
19b16 2a 20 74 68 65 20 70 61 72 74 73 20 62 65 79 6f * the parts beyo
19b17 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 nd the common pr
19b18 65 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 efix are ignored
19b19 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 ..**.** If the U
19b1a 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 NPACKED_IGNORE_R
19b1b 4f 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 OWID flag is set
19b1c 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 , then the last
19b1d 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 byte of.** the h
19b1e 65 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 eader of pKey1 i
19b1f 73 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 s ignored. It i
19b20 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 s assumed that p
19b21 4b 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e Key1 is.** an in
19b22 64 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 dex key, and thu
19b23 73 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f s ends with a ro
19b24 77 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 wid value. The
19b25 6c 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 last byte.** of
19b26 74 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 the header will
19b27 74 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 therefore be the
19b28 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 serial type of
19b29 74 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e the rowid:.** on
19b2a 65 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c e of 1, 2, 3, 4,
19b2b 20 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 5, 6, 8, or 9 -
19b2c 20 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 the integer ser
19b2d 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 ial types..** Th
19b2e 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 e serial type of
19b2f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 the final rowid
19b30 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 will always be
19b31 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a a single byte..*
19b32 2a 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 * By ignoring th
19b33 69 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 is last byte of
19b34 74 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 the header, we f
19b35 6f 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 orce the compari
19b36 73 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 son.** to ignore
19b37 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 the rowid at th
19b38 65 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a e end of key1..*
19b39 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19b3a 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
19b3b 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 RecordCompare(.
19b3c 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 int nKey1, cons
19b3d 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f t void *pKey1, /
19b3e 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 * Left key */.
19b3f 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
19b40 70 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a pPKey2 /*
19b41 20 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b Right key */.){
19b42 0a 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 . int d1;
19b43 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
19b44 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e into aKey[] of n
19b45 65 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 ext data element
19b46 20 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 */. u32 idx1;
19b47 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
19b48 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f et into aKey[] o
19b49 66 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c f next header el
19b4a 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 ement */. u32 s
19b4b 7a 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a zHdr1; /*
19b4c 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
19b4d 20 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 in header */.
19b4e 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 int i = 0;. int
19b4f 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 nField;. int r
19b50 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 c = 0;. const u
19b51 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b nsigned char *aK
19b52 65 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 ey1 = (const uns
19b53 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 igned char *)pKe
19b54 79 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 y1;. KeyInfo *p
19b55 4b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d KeyInfo;. Mem m
19b56 65 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f em1;.. pKeyInfo
19b57 20 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 = pPKey2->pKeyI
19b58 6e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 nfo;. mem1.enc
19b59 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b = pKeyInfo->enc;
19b5a 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 . mem1.db = pKe
19b5b 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d yInfo->db;. mem
19b5c 31 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 1.flags = 0;. m
19b5d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b em1.zMalloc = 0;
19b5e 0a 20 20 0a 20 20 69 64 78 31 20 3d 20 67 65 74 . . idx1 = get
19b5f 56 61 72 69 6e 74 33 32 28 61 4b 65 79 31 2c 20 Varint32(aKey1,
19b60 73 7a 48 64 72 31 29 3b 0a 20 20 64 31 20 3d 20 szHdr1);. d1 =
19b61 73 7a 48 64 72 31 3b 0a 20 20 69 66 28 20 70 50 szHdr1;. if( pP
19b62 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e Key2->flags & UN
19b63 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f PACKED_IGNORE_RO
19b64 57 49 44 20 29 7b 0a 20 20 20 20 73 7a 48 64 72 WID ){. szHdr
19b65 31 2d 2d 3b 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 1--;. }. nFiel
19b66 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 d = pKeyInfo->nF
19b67 69 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28 20 69 ield;. while( i
19b68 64 78 31 3c 73 7a 48 64 72 31 20 26 26 20 69 3c dx1<szHdr1 && i<
19b69 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 20 29 pPKey2->nField )
19b6a 7b 0a 20 20 20 20 75 33 32 20 73 65 72 69 61 6c {. u32 serial
19b6b 5f 74 79 70 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 _type1;.. /*
19b6c 52 65 61 64 20 74 68 65 20 73 65 72 69 61 6c 20 Read the serial
19b6d 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 6e 65 types for the ne
19b6e 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 65 61 xt element in ea
19b6f 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 69 ch key. */. i
19b70 64 78 31 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 dx1 += getVarint
19b71 33 32 28 20 61 4b 65 79 31 2b 69 64 78 31 2c 20 32( aKey1+idx1,
19b72 73 65 72 69 61 6c 5f 74 79 70 65 31 20 29 3b 0a serial_type1 );.
19b73 20 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b 65 79 if( d1>=nKey
19b74 31 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 1 && sqlite3Vdbe
19b75 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 SerialTypeLen(se
19b76 72 69 61 6c 5f 74 79 70 65 31 29 3e 30 20 29 20 rial_type1)>0 )
19b77 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 break;.. /* E
19b78 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 xtract the value
19b79 73 20 74 6f 20 62 65 20 63 6f 6d 70 61 72 65 64 s to be compared
19b7a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 31 20 .. */. d1
19b7b 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 += sqlite3VdbeSe
19b7c 72 69 61 6c 47 65 74 28 26 61 4b 65 79 31 5b 64 rialGet(&aKey1[d
19b7d 31 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31 1], serial_type1
19b7e 2c 20 26 6d 65 6d 31 29 3b 0a 0a 20 20 20 20 2f , &mem1);.. /
19b7f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 * Do the compari
19b80 73 6f 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 son. */. r
19b81 63 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f c = sqlite3MemCo
19b82 6d 70 61 72 65 28 26 6d 65 6d 31 2c 20 26 70 50 mpare(&mem1, &pP
19b83 4b 65 79 32 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 Key2->aMem[i],.
19b84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19b85 20 20 20 20 20 20 20 20 20 20 69 3c 6e 46 69 65 i<nFie
19b86 6c 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 ld ? pKeyInfo->a
19b87 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 Coll[i] : 0);.
19b88 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 if( rc!=0 ){.
19b89 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
19b8a 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 }. i++;. }.
19b8b 20 69 66 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f if( mem1.zMallo
19b8c 63 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d c ) sqlite3VdbeM
19b8d 65 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 emRelease(&mem1)
19b8e 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 ;.. if( rc==0 )
19b8f 7b 0a 20 20 20 20 2f 2a 20 72 63 3d 3d 30 20 68 {. /* rc==0 h
19b90 65 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20 6f ere means that o
19b91 6e 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 72 ne of the keys r
19b92 61 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 73 an out of fields
19b93 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 and. ** all
19b94 74 68 65 20 66 69 65 6c 64 73 20 75 70 20 74 6f the fields up to
19b95 20 74 68 61 74 20 70 6f 69 6e 74 20 77 65 72 65 that point were
19b96 20 65 71 75 61 6c 2e 20 49 66 20 74 68 65 20 55 equal. If the U
19b97 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a NPACKED_INCRKEY.
19b98 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 ** flag is s
19b99 65 74 2c 20 74 68 65 6e 20 62 72 65 61 6b 20 74 et, then break t
19b9a 68 65 20 74 69 65 20 62 79 20 74 72 65 61 74 69 he tie by treati
19b9b 6e 67 20 6b 65 79 32 20 61 73 20 6c 61 72 67 65 ng key2 as large
19b9c 72 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 r.. ** If the
19b9d 20 55 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f UPACKED_PREFIX_
19b9e 4d 41 54 43 48 20 66 6c 61 67 20 69 73 20 73 65 MATCH flag is se
19b9f 74 2c 20 74 68 65 6e 20 6b 65 79 73 20 77 69 74 t, then keys wit
19ba0 68 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 h common prefixe
19ba1 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 63 6f 6e s. ** are con
19ba2 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71 sidered to be eq
19ba3 75 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c ual. Otherwise,
19ba4 20 74 68 65 20 6c 6f 6e 67 65 72 20 6b 65 79 20 the longer key
19ba5 69 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 6c is the . ** l
19ba6 61 72 67 65 72 2e 20 20 41 73 20 69 74 20 68 61 arger. As it ha
19ba7 70 70 65 6e 73 2c 20 74 68 65 20 70 50 4b 65 79 ppens, the pPKey
19ba8 32 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 2 will always be
19ba9 20 74 68 65 20 6c 6f 6e 67 65 72 0a 20 20 20 20 the longer.
19baa 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ** if there is a
19bab 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20 20 difference..
19bac 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 4b 65 */. if( pPKe
19bad 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 y2->flags & UNPA
19bae 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 7b 0a CKED_INCRKEY ){.
19baf 20 20 20 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20 rc = -1;.
19bb0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 4b }else if( pPK
19bb1 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 ey2->flags & UNP
19bb2 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 ACKED_PREFIX_MAT
19bb3 43 48 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c CH ){. /* L
19bb4 65 61 76 65 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 eave rc==0 */.
19bb5 20 20 7d 65 6c 73 65 20 69 66 28 20 69 64 78 31 }else if( idx1
19bb6 3c 73 7a 48 64 72 31 20 29 7b 0a 20 20 20 20 20 <szHdr1 ){.
19bb7 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 rc = 1;. }.
19bb8 20 7d 65 6c 73 65 20 69 66 28 20 70 4b 65 79 49 }else if( pKeyI
19bb9 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 nfo->aSortOrder
19bba 26 26 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e && i<pKeyInfo->n
19bbb 46 69 65 6c 64 0a 20 20 20 20 20 20 20 20 20 20 Field.
19bbc 20 20 20 20 20 26 26 20 70 4b 65 79 49 6e 66 6f && pKeyInfo
19bbd 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 ->aSortOrder[i]
19bbe 29 7b 0a 20 20 20 20 72 63 20 3d 20 2d 72 63 3b ){. rc = -rc;
19bbf 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
19bc0 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 c;.}. ../*.** pC
19bc1 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 ur points at an
19bc2 69 6e 64 65 78 20 65 6e 74 72 79 20 63 72 65 61 index entry crea
19bc3 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f 50 ted using the OP
19bc4 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f _MakeRecord opco
19bc5 64 65 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 de..** Read the
19bc6 72 6f 77 69 64 20 28 74 68 65 20 6c 61 73 74 20 rowid (the last
19bc7 66 69 65 6c 64 20 69 6e 20 74 68 65 20 72 65 63 field in the rec
19bc8 6f 72 64 29 20 61 6e 64 20 73 74 6f 72 65 20 69 ord) and store i
19bc9 74 20 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 t in *rowid..**
19bca 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
19bcb 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 if everything w
19bcc 6f 72 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f orks, or an erro
19bcd 72 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65 r code otherwise
19bce 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 67 ..**.** pCur mig
19bcf 68 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 ht be pointing t
19bd0 6f 20 74 65 78 74 20 6f 62 74 61 69 6e 65 64 20 o text obtained
19bd1 66 72 6f 6d 20 61 20 63 6f 72 72 75 70 74 20 64 from a corrupt d
19bd2 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
19bd3 20 53 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 So the content
19bd4 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 cannot be truste
19bd5 64 2e 20 20 44 6f 20 61 70 70 72 6f 70 72 69 61 d. Do appropria
19bd6 74 65 20 63 68 65 63 6b 73 20 6f 6e 20 74 68 65 te checks on the
19bd7 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c content..*/.SQL
19bd8 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19bd9 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f sqlite3VdbeIdxRo
19bda 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 wid(BtCursor *pC
19bdb 75 72 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b ur, i64 *rowid){
19bdc 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 . i64 nCellKey
19bdd 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 = 0;. int rc;.
19bde 20 75 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20 u32 szHdr;
19bdf 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
19be0 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 e header */. u3
19be1 32 20 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20 2 typeRowid;
19be2 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f /* Serial type o
19be3 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 f the rowid */.
19be4 20 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20 u32 lenRowid;
19be5 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
19be6 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d e rowid */. Mem
19be7 20 6d 2c 20 76 3b 0a 0a 20 20 2f 2a 20 47 65 74 m, v;.. /* Get
19be8 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
19be9 20 69 6e 64 65 78 20 65 6e 74 72 79 2e 20 20 4f index entry. O
19bea 6e 6c 79 20 69 6e 64 69 63 65 73 20 65 6e 74 72 nly indices entr
19beb 69 65 73 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a ies of less. **
19bec 20 74 68 61 6e 20 32 47 69 42 20 61 72 65 20 73 than 2GiB are s
19bed 75 70 70 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e upport - anythin
19bee 67 20 6c 61 72 67 65 20 6d 75 73 74 20 62 65 20 g large must be
19bef 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
19bf0 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ion */. sqlite3
19bf1 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 BtreeKeySize(pCu
19bf2 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 r, &nCellKey);.
19bf3 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 6e 43 if( unlikely(nC
19bf4 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 ellKey<=0 || nCe
19bf5 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66 llKey>0x7fffffff
19bf6 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
19bf7 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
19bf8 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 KPT;. }.. /* R
19bf9 65 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70 6c ead in the compl
19bfa 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 ete content of t
19bfb 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a he index entry *
19bfc 2f 0a 20 20 6d 2e 66 6c 61 67 73 20 3d 20 30 3b /. m.flags = 0;
19bfd 0a 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 6d . m.db = 0;. m
19bfe 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 .zMalloc = 0;.
19bff 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
19c00 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 75 MemFromBtree(pCu
19c01 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c r, 0, (int)nCell
19c02 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 Key, 1, &m);. i
19c03 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 f( rc ){. ret
19c04 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f urn rc;. }.. /
19c05 2a 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74 72 * The index entr
19c06 79 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69 74 y must begin wit
19c07 68 20 61 20 68 65 61 64 65 72 20 73 69 7a 65 20 h a header size
19c08 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 */. (void)getVa
19c09 72 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c rint32((u8*)m.z,
19c0a 20 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74 63 szHdr);. testc
19c0b 61 73 65 28 20 73 7a 48 64 72 3d 3d 32 20 29 3b ase( szHdr==2 );
19c0c 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 48 . testcase( szH
19c0d 64 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 dr==m.n );. if(
19c0e 20 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 3c unlikely(szHdr<
19c0f 32 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72 3e 2 || (int)szHdr>
19c10 6d 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f m.n) ){. goto
19c11 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 idx_rowid_corru
19c12 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a ption;. }.. /*
19c13 20 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 The last field
19c14 6f 66 20 74 68 65 20 69 6e 64 65 78 20 73 68 6f of the index sho
19c15 75 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67 65 uld be an intege
19c16 72 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a 20 r - the ROWID..
19c17 20 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 ** Verify that
19c18 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 72 the last entry r
19c19 65 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74 65 eally is an inte
19c1a 67 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 29 ger. */. (void)
19c1b 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a getVarint32((u8*
19c1c 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 )&m.z[szHdr-1],
19c1d 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 typeRowid);. te
19c1e 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 stcase( typeRowi
19c1f 64 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63 61 d==1 );. testca
19c20 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 32 se( typeRowid==2
19c21 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
19c22 74 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0a typeRowid==3 );.
19c23 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 testcase( type
19c24 52 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74 65 Rowid==4 );. te
19c25 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 stcase( typeRowi
19c26 64 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63 61 d==5 );. testca
19c27 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 36 se( typeRowid==6
19c28 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
19c29 74 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b 0a typeRowid==8 );.
19c2a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 testcase( type
19c2b 52 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69 66 Rowid==9 );. if
19c2c 28 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65 52 ( unlikely(typeR
19c2d 6f 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52 6f owid<1 || typeRo
19c2e 77 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f 77 wid>9 || typeRow
19c2f 69 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67 6f id==7) ){. go
19c30 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 to idx_rowid_cor
19c31 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c ruption;. }. l
19c32 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 enRowid = sqlite
19c33 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 3VdbeSerialTypeL
19c34 65 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 en(typeRowid);.
19c35 20 74 65 73 74 63 61 73 65 28 20 6d 2e 6e 2d 6c testcase( m.n-l
19c36 65 6e 52 6f 77 69 64 3d 3d 73 7a 48 64 72 20 29 enRowid==szHdr )
19c37 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 ;. if( unlikely
19c38 28 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 3c 73 7a (m.n-lenRowid<sz
19c39 48 64 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f Hdr) ){. goto
19c3a 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 idx_rowid_corru
19c3b 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a ption;. }.. /*
19c3c 20 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 67 Fetch the integ
19c3d 65 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f er off the end o
19c3e 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f f the index reco
19c3f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 rd */. sqlite3V
19c40 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 dbeSerialGet((u8
19c41 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f *)&m.z[m.n-lenRo
19c42 77 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c wid], typeRowid,
19c43 20 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d &v);. *rowid =
19c44 20 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65 v.u.i;. sqlite
19c45 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
19c46 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 &m);. return SQ
19c47 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a LITE_OK;.. /* J
19c48 75 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 61 ump here if data
19c49 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 base corruption
19c4a 69 73 20 64 65 74 65 63 74 65 64 20 61 66 74 65 is detected afte
19c4b 72 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 2a r m has been. *
19c4c 2a 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72 * allocated. Fr
19c4d 65 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 20 ee the m object
19c4e 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 and return SQLIT
19c4f 45 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 64 E_CORRUPT. */.id
19c50 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 x_rowid_corrupti
19c51 6f 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 20 on:. testcase(
19c52 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0a m.zMalloc!=0 );.
19c53 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19c54 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 Release(&m);. r
19c55 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
19c56 52 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a RUPT_BKPT;.}../*
19c57 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 .** Compare the
19c58 6b 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 key of the index
19c59 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 entry that curs
19c5a 6f 72 20 70 43 20 69 73 20 70 6f 69 6e 74 20 74 or pC is point t
19c5b 6f 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 o against.** the
19c5c 20 6b 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70 key string in p
19c5d 4b 65 79 20 28 6f 66 20 6c 65 6e 67 74 68 20 6e Key (of length n
19c5e 4b 65 79 29 2e 20 20 57 72 69 74 65 20 69 6e 74 Key). Write int
19c5f 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72 o *pRes a number
19c60 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 .** that is nega
19c61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 tive, zero, or p
19c62 6f 73 69 74 69 76 65 20 69 66 20 70 43 20 69 73 ositive if pC is
19c63 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 less than, equa
19c64 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 l to,.** or grea
19c65 74 65 72 20 74 68 61 6e 20 70 4b 65 79 2e 20 20 ter than pKey.
19c66 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
19c67 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a on success..**.
19c68 2a 2a 20 70 4b 65 79 20 69 73 20 65 69 74 68 65 ** pKey is eithe
19c69 72 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75 r created withou
19c6a 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20 t a rowid or is
19c6b 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61 truncated so tha
19c6c 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 t it.** omits th
19c6d 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 e rowid at the e
19c6e 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61 nd. The rowid a
19c6f 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
19c70 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20 index entry.**
19c71 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65 is ignored as we
19c72 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73 ll. Hence, this
19c73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f routine only co
19c74 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69 mpares the prefi
19c75 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b xes .** of the k
19c76 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 eys prior to the
19c77 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f final rowid, no
19c78 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 t the entire key
19c79 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 ..**.** pUnpacke
19c7a 64 20 6d 61 79 20 62 65 20 61 6e 20 75 6e 70 61 d may be an unpa
19c7b 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 cked version of
19c7c 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 49 66 20 70 pKey,nKey. If p
19c7d 55 6e 70 61 63 6b 65 64 20 69 73 0a 2a 2a 20 73 Unpacked is.** s
19c7e 75 70 70 6c 69 65 64 20 69 74 20 69 73 20 75 73 upplied it is us
19c7f 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 70 ed in place of p
19c80 4b 65 79 2c 6e 4b 65 79 2e 0a 2a 2f 0a 53 51 4c Key,nKey..*/.SQL
19c81 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19c82 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 sqlite3VdbeIdxKe
19c83 79 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65 yCompare(. Vdbe
19c84 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 Cursor *pC,
19c85 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
19c86 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 ursor to compare
19c87 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e against */. Un
19c88 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 packedRecord *pU
19c89 6e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 npacked, /* Unp
19c8a 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 acked version of
19c8b 20 70 4b 65 79 20 61 6e 64 20 6e 4b 65 79 20 2a pKey and nKey *
19c8c 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 20 /. int *res
19c8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19c8e 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d /* Write the com
19c8f 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 68 parison result h
19c90 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20 ere */.){. i64
19c91 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 nCellKey = 0;.
19c92 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 73 int rc;. BtCurs
19c93 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70 or *pCur = pC->p
19c94 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b Cursor;. Mem m;
19c95 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
19c96 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e KeySize(pCur, &n
19c97 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 69 66 28 20 CellKey);. if(
19c98 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e nCellKey<=0 || n
19c99 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 CellKey>0x7fffff
19c9a 66 66 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d ff ){. *res =
19c9b 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 0;. return S
19c9c 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
19c9d 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 6d 2e 66 6c m.db = 0;. m.fl
19c9e 61 67 73 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d 61 ags = 0;. m.zMa
19c9f 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20 3d lloc = 0;. rc =
19ca0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
19ca1 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75 romBtree(pC->pCu
19ca2 72 73 6f 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 rsor, 0, (int)nC
19ca3 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a ellKey, 1, &m);.
19ca4 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
19ca5 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
19ca6 20 61 73 73 65 72 74 28 20 70 55 6e 70 61 63 6b assert( pUnpack
19ca7 65 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 ed->flags & UNPA
19ca8 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 CKED_IGNORE_ROWI
19ca9 44 20 29 3b 0a 20 20 2a 72 65 73 20 3d 20 73 71 D );. *res = sq
19caa 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 lite3VdbeRecordC
19cab 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c ompare(m.n, m.z,
19cac 20 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20 20 73 pUnpacked);. s
19cad 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
19cae 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 ease(&m);. retu
19caf 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
19cb0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
19cb1 69 6e 65 20 73 65 74 73 20 74 68 65 20 76 61 6c ine sets the val
19cb2 75 65 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 ue to be returne
19cb3 64 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 d by subsequent
19cb4 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 calls to.** sqli
19cb5 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e te3_changes() on
19cb6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
19cb7 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53 ndle 'db'. .*/.S
19cb8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
19cb9 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
19cba 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 tChanges(sqlite3
19cbb 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67 *db, int nChang
19cbc 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 e){. assert( sq
19cbd 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
19cbe 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
19cbf 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e db->nChange = n
19cc0 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 Change;. db->nT
19cc1 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43 otalChange += nC
19cc2 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 hange;.}../*.**
19cc3 53 65 74 20 61 20 66 6c 61 67 20 69 6e 20 74 68 Set a flag in th
19cc4 65 20 76 64 62 65 20 74 6f 20 75 70 64 61 74 65 e vdbe to update
19cc5 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e the change coun
19cc6 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 20 66 ter when it is f
19cc7 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72 20 72 inalised.** or r
19cc8 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eset..*/.SQLITE_
19cc9 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19cca 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 ite3VdbeCountCha
19ccb 6e 67 65 73 28 56 64 62 65 20 2a 76 29 7b 0a 20 nges(Vdbe *v){.
19ccc 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20 v->changeCntOn
19ccd 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 = 1;.}../*.** Ma
19cce 72 6b 20 65 76 65 72 79 20 70 72 65 70 61 72 65 rk every prepare
19ccf 64 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 6f d statement asso
19cd0 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 ciated with a da
19cd1 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
19cd2 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72 65 64 2e n.** as expired.
19cd3 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65 .**.** An expire
19cd4 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e d statement mean
19cd5 73 20 74 68 61 74 20 72 65 63 6f 6d 70 69 6c 61 s that recompila
19cd6 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 tion of the stat
19cd7 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65 63 6f ement is.** reco
19cd8 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e mmend. Statemen
19cd9 74 73 20 65 78 70 69 72 65 20 77 68 65 6e 20 74 ts expire when t
19cda 68 69 6e 67 73 20 68 61 70 70 65 6e 20 74 68 61 hings happen tha
19cdb 74 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a 2a 20 t make their.**
19cdc 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74 programs obsolet
19cdd 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75 73 65 e. Removing use
19cde 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 r-defined functi
19cdf 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67 ons or collating
19ce0 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c 20 6f .** sequences, o
19ce1 72 20 63 68 61 6e 67 69 6e 67 20 61 6e 20 61 75 r changing an au
19ce2 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 thorization func
19ce3 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 79 70 tion are the typ
19ce4 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67 73 20 es of.** things
19ce5 74 68 61 74 20 6d 61 6b 65 20 70 72 65 70 61 72 that make prepar
19ce6 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 62 ed statements ob
19ce7 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 solete..*/.SQLIT
19ce8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
19ce9 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 qlite3ExpirePrep
19cea 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 aredStatements(s
19ceb 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 qlite3 *db){. V
19cec 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 70 20 dbe *p;. for(p
19ced 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20 70 3b 20 = db->pVdbe; p;
19cee 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
19cef 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b p->expired = 1;
19cf0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
19cf1 74 75 72 6e 20 74 68 65 20 64 61 74 61 62 61 73 turn the databas
19cf2 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
19cf3 68 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f 0a 53 h the Vdbe..*/.S
19cf4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
19cf5 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64 lite3 *sqlite3Vd
19cf6 62 65 44 62 28 56 64 62 65 20 2a 76 29 7b 0a 20 beDb(Vdbe *v){.
19cf7 20 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b 0a 7d return v->db;.}
19cf8 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
19cf9 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 75 78 * End of vdbeaux
19cfa 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
19cfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19cfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19cfd 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
19cfe 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
19cff 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eapi.c *********
19d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d02 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
19d03 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 26.**.** The au
19d04 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
19d05 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
19d06 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
19d07 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
19d08 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
19d09 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
19d0a 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
19d0b 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
19d0c 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
19d0d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
19d0e 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
19d0f 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
19d10 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
19d11 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
19d12 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
19d13 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
19d14 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
19d15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d19 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
19d1a 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
19d1b 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e use to implemen
19d1c 74 20 41 50 49 73 20 74 68 61 74 20 61 72 65 20 t APIs that are
19d1d 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56 part of the.** V
19d1e 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 DBE..**.** $Id:
19d1f 76 64 62 65 61 70 69 2e 63 2c 76 20 31 2e 31 35 vdbeapi.c,v 1.15
19d20 36 20 32 30 30 39 2f 30 33 2f 32 35 20 31 35 3a 6 2009/03/25 15:
19d21 34 33 3a 30 39 20 64 61 6e 69 65 6c 6b 31 39 37 43:09 danielk197
19d22 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 7 Exp $.*/..#if
19d23 30 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 0 && defined(SQL
19d24 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ITE_ENABLE_MEMOR
19d25 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 29 0a 2f 2a Y_MANAGEMENT)./*
19d26 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
19d27 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 g structure cont
19d28 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f ains pointers to
19d29 20 74 68 65 20 65 6e 64 20 70 6f 69 6e 74 73 20 the end points
19d2a 6f 66 20 61 0a 2a 2a 20 64 6f 75 62 6c 79 2d 6c of a.** doubly-l
19d2b 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c inked list of al
19d2c 6c 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 l compiled SQL s
19d2d 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 6d tatements that m
19d2e 61 79 20 62 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a ay be holding.**
19d2f 20 62 75 66 66 65 72 73 20 65 6c 69 67 69 62 6c buffers eligibl
19d30 65 20 66 6f 72 20 72 65 6c 65 61 73 65 20 77 68 e for release wh
19d31 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 en the sqlite3_r
19d32 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 elease_memory()
19d33 69 6e 74 65 72 66 61 63 65 20 69 73 0a 2a 2a 20 interface is.**
19d34 69 6e 76 6f 6b 65 64 2e 20 41 63 63 65 73 73 20 invoked. Access
19d35 74 6f 20 74 68 69 73 20 6c 69 73 74 20 69 73 20 to this list is
19d36 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 protected by the
19d37 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
19d38 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 6d 75 74 ATIC_LRU2.** mut
19d39 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d ex..**.** Statem
19d3a 65 6e 74 73 20 61 72 65 20 61 64 64 65 64 20 74 ents are added t
19d3b 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 o the end of thi
19d3c 73 20 6c 69 73 74 20 77 68 65 6e 20 73 71 6c 69 s list when sqli
19d3d 74 65 33 5f 72 65 73 65 74 28 29 20 69 73 0a 2a te3_reset() is.*
19d3e 2a 20 63 61 6c 6c 65 64 2e 20 54 68 65 79 20 61 * called. They a
19d3f 72 65 20 72 65 6d 6f 76 65 64 20 65 69 74 68 65 re removed eithe
19d40 72 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 73 r when sqlite3_s
19d41 74 65 70 28 29 20 6f 72 20 73 71 6c 69 74 65 33 tep() or sqlite3
19d42 5f 66 69 6e 61 6c 69 7a 65 28 29 0a 2a 2a 20 69 _finalize().** i
19d43 73 20 63 61 6c 6c 65 64 2e 20 57 68 65 6e 20 73 s called. When s
19d44 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 61 64 tatements are ad
19d45 64 65 64 20 74 6f 20 74 68 69 73 20 6c 69 73 74 ded to this list
19d46 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 , the associated
19d47 20 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 72 .** register ar
19d48 72 61 79 20 28 70 2d 3e 61 4d 65 6d 5b 31 2e 2e ray (p->aMem[1..
19d49 70 2d 3e 6e 4d 65 6d 5d 29 20 6d 61 79 20 63 6f p->nMem]) may co
19d4a 6e 74 61 69 6e 20 64 79 6e 61 6d 69 63 20 62 75 ntain dynamic bu
19d4b 66 66 65 72 73 20 74 68 61 74 0a 2a 2a 20 63 61 ffers that.** ca
19d4c 6e 20 62 65 20 66 72 65 65 64 20 75 73 69 6e 67 n be freed using
19d4d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 sqlite3VdbeRele
19d4e 61 73 65 4d 65 6d 6f 72 79 28 29 2e 0a 2a 2a 0a aseMemory()..**.
19d4f 2a 2a 20 57 68 65 6e 20 73 74 61 74 65 6d 65 6e ** When statemen
19d50 74 73 20 61 72 65 20 61 64 64 65 64 20 6f 72 20 ts are added or
19d51 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 69 removed from thi
19d52 73 20 6c 69 73 74 2c 20 74 68 65 20 6d 75 74 65 s list, the mute
19d53 78 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 x.** associated
19d54 77 69 74 68 20 74 68 65 20 56 64 62 65 20 62 65 with the Vdbe be
19d55 69 6e 67 20 61 64 64 65 64 20 6f 72 20 72 65 6d ing added or rem
19d56 6f 76 65 64 20 28 56 64 62 65 2e 64 62 2d 3e 6d oved (Vdbe.db->m
19d57 75 74 65 78 29 20 69 73 0a 2a 2a 20 61 6c 72 65 utex) is.** alre
19d58 61 64 79 20 68 65 6c 64 2e 20 54 68 65 20 4c 52 ady held. The LR
19d59 55 32 20 6d 75 74 65 78 20 69 73 20 74 68 65 6e U2 mutex is then
19d5a 20 6f 62 74 61 69 6e 65 64 2c 20 62 6c 6f 63 6b obtained, block
19d5b 69 6e 67 20 69 66 20 6e 65 63 65 73 73 61 72 79 ing if necessary
19d5c 2c 0a 2a 2a 20 74 68 65 20 6c 69 6e 6b 65 64 2d ,.** the linked-
19d5d 6c 69 73 74 20 70 6f 69 6e 74 65 72 73 20 6d 61 list pointers ma
19d5e 6e 69 70 75 6c 61 74 65 64 20 61 6e 64 20 74 68 nipulated and th
19d5f 65 20 4c 52 55 32 20 6d 75 74 65 78 20 72 65 6c e LRU2 mutex rel
19d60 69 6e 71 75 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 inquished..*/.st
19d61 72 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c 72 ruct StatementLr
19d62 75 4c 69 73 74 20 7b 0a 20 20 56 64 62 65 20 2a uList {. Vdbe *
19d63 70 46 69 72 73 74 3b 0a 20 20 56 64 62 65 20 2a pFirst;. Vdbe *
19d64 70 4c 61 73 74 3b 0a 7d 3b 0a 73 74 61 74 69 63 pLast;.};.static
19d65 20 73 74 72 75 63 74 20 53 74 61 74 65 6d 65 6e struct Statemen
19d66 74 4c 72 75 4c 69 73 74 20 73 71 6c 69 74 65 33 tLruList sqlite3
19d67 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 3b 0a 0a LruStatements;..
19d68 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 /*.** Check that
19d69 20 74 68 65 20 6c 69 73 74 20 6c 6f 6f 6b 73 20 the list looks
19d6a 74 6f 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 to be internally
19d6b 20 63 6f 6e 73 69 73 74 65 6e 74 2e 20 54 68 69 consistent. Thi
19d6c 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 s is used.** as
19d6d 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 72 part of an asser
19d6e 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73 t() statement as
19d6f 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
19d70 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 assert( stmtLr
19d71 75 43 68 65 63 6b 28 29 20 29 3b 0a 2a 2f 0a 23 uCheck() );.*/.#
19d72 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 ifndef NDEBUG.st
19d73 61 74 69 63 20 69 6e 74 20 73 74 6d 74 4c 72 75 atic int stmtLru
19d74 43 68 65 63 6b 28 29 7b 0a 20 20 56 64 62 65 20 Check(){. Vdbe
19d75 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 *p;. for(p=sqli
19d76 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
19d77 2e 70 46 69 72 73 74 3b 20 70 3b 20 70 3d 70 2d .pFirst; p; p=p-
19d78 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 >pLruNext){.
19d79 61 73 73 65 72 74 28 70 2d 3e 70 4c 72 75 4e 65 assert(p->pLruNe
19d7a 78 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 xt || p==sqlite3
19d7b 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c LruStatements.pL
19d7c 61 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ast);. assert
19d7d 28 21 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c (!p->pLruNext ||
19d7e 20 70 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c p->pLruNext->pL
19d7f 72 75 50 72 65 76 3d 3d 70 29 3b 0a 20 20 20 20 ruPrev==p);.
19d80 61 73 73 65 72 74 28 70 2d 3e 70 4c 72 75 50 72 assert(p->pLruPr
19d81 65 76 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 ev || p==sqlite3
19d82 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 LruStatements.pF
19d83 69 72 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72 irst);. asser
19d84 74 28 21 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c t(!p->pLruPrev |
19d85 7c 20 70 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 | p->pLruPrev->p
19d86 4c 72 75 4e 65 78 74 3d 3d 70 29 3b 0a 20 20 7d LruNext==p);. }
19d87 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 . return 1;.}.#
19d88 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 endif../*.** Add
19d89 20 76 64 62 65 20 70 20 74 6f 20 74 68 65 20 65 vdbe p to the e
19d8a 6e 64 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d nd of the statem
19d8b 65 6e 74 20 6c 72 75 20 6c 69 73 74 2e 20 49 74 ent lru list. It
19d8c 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
19d8d 0a 2a 2a 20 70 20 69 73 20 6e 6f 74 20 61 6c 72 .** p is not alr
19d8e 65 61 64 79 20 70 61 72 74 20 6f 66 20 74 68 65 eady part of the
19d8f 20 6c 69 73 74 20 77 68 65 6e 20 74 68 69 73 20 list when this
19d90 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20 6c is called. The l
19d91 72 75 20 6c 69 73 74 0a 2a 2a 20 69 73 20 70 72 ru list.** is pr
19d92 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53 otected by the S
19d93 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
19d94 49 43 5f 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f IC_LRU mutex..*/
19d95 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 6d .static void stm
19d96 74 4c 72 75 41 64 64 28 56 64 62 65 20 2a 70 29 tLruAdd(Vdbe *p)
19d97 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
19d98 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d x_enter(sqlite3M
19d99 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
19d9a 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
19d9b 55 32 29 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e U2));.. if( p->
19d9c 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 pLruPrev || p->p
19d9d 4c 72 75 4e 65 78 74 20 7c 7c 20 73 71 6c 69 74 LruNext || sqlit
19d9e 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e e3LruStatements.
19d9f 70 46 69 72 73 74 3d 3d 70 20 29 7b 0a 20 20 20 pFirst==p ){.
19da0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
19da1 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 eave(sqlite3Mute
19da2 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
19da3 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 TEX_STATIC_LRU2)
19da4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
19da5 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 73 74 }.. assert( st
19da6 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a mtLruCheck() );.
19da7 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 4c . if( !sqlite3L
19da8 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 ruStatements.pFi
19da9 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 rst ){. asser
19daa 74 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53 74 t( !sqlite3LruSt
19dab 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20 29 atements.pLast )
19dac 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 75 ;. sqlite3Lru
19dad 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 Statements.pFirs
19dae 74 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 t = p;. sqlit
19daf 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e e3LruStatements.
19db0 70 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d 65 6c pLast = p;. }el
19db1 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
19db2 21 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 !sqlite3LruState
19db3 6d 65 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c 72 ments.pLast->pLr
19db4 75 4e 65 78 74 20 29 3b 0a 20 20 20 20 70 2d 3e uNext );. p->
19db5 70 4c 72 75 50 72 65 76 20 3d 20 73 71 6c 69 74 pLruPrev = sqlit
19db6 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e e3LruStatements.
19db7 70 4c 61 73 74 3b 0a 20 20 20 20 73 71 6c 69 74 pLast;. sqlit
19db8 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e e3LruStatements.
19db9 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65 78 74 20 pLast->pLruNext
19dba 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = p;. sqlite3
19dbb 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c LruStatements.pL
19dbc 61 73 74 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 ast = p;. }..
19dbd 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 assert( stmtLruC
19dbe 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20 73 71 6c heck() );.. sql
19dbf 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
19dc0 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
19dc1 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
19dc2 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 7d STATIC_LRU2));.}
19dc3 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 ../*.** Assuming
19dc4 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 the SQLITE_MUTE
19dc5 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 6d 75 X_STATIC_LRU2 mu
19dc6 74 65 78 74 20 69 73 20 61 6c 72 65 61 64 79 20 text is already
19dc7 68 65 6c 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 held, remove.**
19dc8 73 74 61 74 65 6d 65 6e 74 20 70 20 66 72 6f 6d statement p from
19dc9 20 74 68 65 20 6c 65 61 73 74 2d 72 65 63 65 6e the least-recen
19dca 74 6c 79 2d 75 73 65 64 20 73 74 61 74 65 6d 65 tly-used stateme
19dcb 6e 74 20 6c 69 73 74 2e 20 49 66 20 74 68 65 20 nt list. If the
19dcc 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 .** statement is
19dcd 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 not currently p
19dce 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2c art of the list,
19dcf 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 this call is a
19dd0 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
19dd1 20 76 6f 69 64 20 73 74 6d 74 4c 72 75 52 65 6d void stmtLruRem
19dd2 6f 76 65 4e 6f 6d 75 74 65 78 28 56 64 62 65 20 oveNomutex(Vdbe
19dd3 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4c *p){. if( p->pL
19dd4 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 ruPrev || p->pLr
19dd5 75 4e 65 78 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 uNext || p==sqli
19dd6 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
19dd7 2e 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20 61 .pFirst ){. a
19dd8 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 ssert( stmtLruCh
19dd9 65 63 6b 28 29 20 29 3b 0a 20 20 20 20 69 66 28 eck() );. if(
19dda 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a p->pLruNext ){.
19ddb 20 20 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65 78 p->pLruNex
19ddc 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 2d t->pLruPrev = p-
19ddd 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d >pLruPrev;. }
19dde 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
19ddf 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
19de0 2e 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 72 75 .pLast = p->pLru
19de1 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Prev;. }.
19de2 69 66 28 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 if( p->pLruPrev
19de3 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72 75 ){. p->pLru
19de4 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 20 3d Prev->pLruNext =
19de5 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 p->pLruNext;.
19de6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
19de7 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 qlite3LruStateme
19de8 6e 74 73 2e 70 46 69 72 73 74 20 3d 20 70 2d 3e nts.pFirst = p->
19de9 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a pLruNext;. }.
19dea 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 p->pLruNext
19deb 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 72 75 = 0;. p->pLru
19dec 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 61 73 Prev = 0;. as
19ded 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 sert( stmtLruChe
19dee 63 6b 28 29 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ck() );. }.}../
19def 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 *.** Assuming th
19df0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
19df1 54 41 54 49 43 5f 4c 52 55 32 20 6d 75 74 65 78 TATIC_LRU2 mutex
19df2 74 20 69 73 20 6e 6f 74 20 68 65 6c 64 2c 20 72 t is not held, r
19df3 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 74 65 6d 65 emove.** stateme
19df4 6e 74 20 70 20 66 72 6f 6d 20 74 68 65 20 6c 65 nt p from the le
19df5 61 73 74 2d 72 65 63 65 6e 74 6c 79 2d 75 73 65 ast-recently-use
19df6 64 20 73 74 61 74 65 6d 65 6e 74 20 6c 69 73 74 d statement list
19df7 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 73 74 61 . If the .** sta
19df8 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 63 75 tement is not cu
19df9 72 72 65 6e 74 6c 79 20 70 61 72 74 20 6f 66 20 rrently part of
19dfa 74 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20 63 the list, this c
19dfb 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a all is a no-op..
19dfc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
19dfd 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 56 64 62 tmtLruRemove(Vdb
19dfe 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 e *p){. sqlite3
19dff 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c _mutex_enter(sql
19e00 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
19e01 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
19e02 49 43 5f 4c 52 55 32 29 29 3b 0a 20 20 73 74 6d IC_LRU2));. stm
19e03 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 tLruRemoveNomute
19e04 78 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f x(p);. sqlite3_
19e05 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 mutex_leave(sqli
19e06 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
19e07 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
19e08 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a 0a 2f 2a 0a C_LRU2));.}../*.
19e09 2a 2a 20 54 72 79 20 74 6f 20 72 65 6c 65 61 73 ** Try to releas
19e0a 65 20 6e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d e n bytes of mem
19e0b 6f 72 79 20 62 79 20 66 72 65 65 69 6e 67 20 62 ory by freeing b
19e0c 75 66 66 65 72 73 20 61 73 73 6f 63 69 61 74 65 uffers associate
19e0d 64 20 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6d d .** with the m
19e0e 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 emory registers
19e0f 6f 66 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 75 of currently unu
19e10 73 65 64 20 76 64 62 65 73 2e 0a 2a 2f 0a 53 51 sed vdbes..*/.SQ
19e11 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19e12 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 sqlite3VdbeRele
19e13 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 aseMemory(int n)
19e14 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 56 {. Vdbe *p;. V
19e15 64 62 65 20 2a 70 4e 65 78 74 3b 0a 20 20 69 6e dbe *pNext;. in
19e16 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20 t nFree = 0;..
19e17 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
19e18 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ter(sqlite3Mutex
19e19 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
19e1a 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 EX_STATIC_LRU2))
19e1b 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 ;. for(p=sqlite
19e1c 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 3LruStatements.p
19e1d 46 69 72 73 74 3b 20 70 20 26 26 20 6e 46 72 65 First; p && nFre
19e1e 65 3c 6e 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 e<n; p=pNext){.
19e1f 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4c pNext = p->pL
19e20 72 75 4e 65 78 74 3b 0a 0a 20 20 20 20 2f 2a 20 ruNext;.. /*
19e21 46 6f 72 20 65 61 63 68 20 73 74 61 74 65 6d 65 For each stateme
19e22 6e 74 20 68 61 6e 64 6c 65 20 69 6e 20 74 68 65 nt handle in the
19e23 20 6c 72 75 20 6c 69 73 74 2c 20 61 74 74 65 6d lru list, attem
19e24 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 pt to obtain the
19e25 0a 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 . ** associat
19e26 65 64 20 64 61 74 61 62 61 73 65 20 6d 75 74 65 ed database mute
19e27 78 2e 20 49 66 20 69 74 20 63 61 6e 6e 6f 74 20 x. If it cannot
19e28 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 63 6f 6e be obtained, con
19e29 74 69 6e 75 65 0a 20 20 20 20 2a 2a 20 74 6f 20 tinue. ** to
19e2a 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65 the next stateme
19e2b 6e 74 20 68 61 6e 64 6c 65 2e 20 49 74 20 69 73 nt handle. It is
19e2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
19e2d 20 62 6c 6f 63 6b 20 6f 6e 0a 20 20 20 20 2a 2a block on. **
19e2e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 the database mu
19e2f 74 65 78 20 2d 20 74 68 61 74 20 63 6f 75 6c 64 tex - that could
19e30 20 63 61 75 73 65 20 64 65 61 64 6c 6f 63 6b 2e cause deadlock.
19e31 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
19e32 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 SQLITE_OK==sqlit
19e33 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e e3_mutex_try(p->
19e34 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 db->mutex) ){.
19e35 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c nFree += sql
19e36 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 ite3VdbeReleaseB
19e37 75 66 66 65 72 73 28 70 29 3b 0a 20 20 20 20 20 uffers(p);.
19e38 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e 6f stmtLruRemoveNo
19e39 6d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 20 20 mutex(p);.
19e3a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
19e3b 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
19e3c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 );. }. }. s
19e3d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
19e3e 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ve(sqlite3MutexA
19e3f 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
19e40 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b X_STATIC_LRU2));
19e41 0a 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 .. return nFree
19e42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 ;.}../*.** Call
19e43 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 sqlite3Reprepare
19e44 28 29 20 6f 6e 20 74 68 65 20 73 74 61 74 65 6d () on the statem
19e45 65 6e 74 2e 20 52 65 6d 6f 76 65 20 69 74 20 66 ent. Remove it f
19e46 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c 72 75 20 6c rom the.** lru l
19e47 69 73 74 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 ist before doing
19e48 20 73 6f 2c 20 61 73 20 52 65 70 72 65 70 61 72 so, as Reprepar
19e49 65 28 29 20 77 69 6c 6c 20 66 72 65 65 20 61 6c e() will free al
19e4a 6c 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 l the.** memory
19e4b 72 65 67 69 73 74 65 72 20 62 75 66 66 65 72 73 register buffers
19e4c 20 61 6e 79 77 61 79 2e 0a 2a 2f 0a 69 6e 74 20 anyway..*/.int
19e4d 76 64 62 65 52 65 70 72 65 70 61 72 65 28 56 64 vdbeReprepare(Vd
19e4e 62 65 20 2a 70 29 7b 0a 20 20 73 74 6d 74 4c 72 be *p){. stmtLr
19e4f 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 72 65 uRemove(p);. re
19e50 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 70 72 turn sqlite3Repr
19e51 65 70 61 72 65 28 70 29 3b 0a 7d 0a 0a 23 65 6c epare(p);.}..#el
19e52 73 65 20 20 20 20 20 20 20 2f 2a 20 21 53 51 4c se /* !SQL
19e53 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ITE_ENABLE_MEMOR
19e54 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a Y_MANAGEMENT */.
19e55 20 20 23 64 65 66 69 6e 65 20 73 74 6d 74 4c 72 #define stmtLr
19e56 75 52 65 6d 6f 76 65 28 78 29 0a 20 20 23 64 65 uRemove(x). #de
19e57 66 69 6e 65 20 73 74 6d 74 4c 72 75 41 64 64 28 fine stmtLruAdd(
19e58 78 29 0a 20 20 23 64 65 66 69 6e 65 20 76 64 62 x). #define vdb
19e59 65 52 65 70 72 65 70 61 72 65 28 78 29 20 73 71 eReprepare(x) sq
19e5a 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 78 lite3Reprepare(x
19e5b 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 ).#endif...#ifnd
19e5c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
19e5d 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 EPRECATED./*.**
19e5e 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e Return TRUE (non
19e5f 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20 73 74 -zero) of the st
19e60 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69 65 64 atement supplied
19e61 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 as an argument
19e62 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 72 needs.** to be r
19e63 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41 20 73 74 ecompiled. A st
19e64 61 74 65 6d 65 6e 74 20 6e 65 65 64 73 20 74 6f atement needs to
19e65 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 77 be recompiled w
19e66 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 65 henever the.** e
19e67 78 65 63 75 74 69 6f 6e 20 65 6e 76 69 72 6f 6e xecution environ
19e68 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 69 6e 20 ment changes in
19e69 61 20 77 61 79 20 74 68 61 74 20 77 6f 75 6c 64 a way that would
19e6a 20 61 6c 74 65 72 20 74 68 65 20 70 72 6f 67 72 alter the progr
19e6b 61 6d 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 am.** that sqlit
19e6c 65 33 5f 70 72 65 70 61 72 65 28 29 20 67 65 6e e3_prepare() gen
19e6d 65 72 61 74 65 73 2e 20 20 46 6f 72 20 65 78 61 erates. For exa
19e6e 6d 70 6c 65 2c 20 69 66 20 6e 65 77 20 66 75 6e mple, if new fun
19e6f 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c ctions or.** col
19e70 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 lating sequences
19e71 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 20 are registered
19e72 6f 72 20 69 66 20 61 6e 20 61 75 74 68 6f 72 69 or if an authori
19e73 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a zer function is.
19e74 2a 2a 20 61 64 64 65 64 20 6f 72 20 63 68 61 6e ** added or chan
19e75 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ged..*/.SQLITE_A
19e76 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 PI int sqlite3_e
19e77 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f 73 xpired(sqlite3_s
19e78 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 tmt *pStmt){. V
19e79 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 dbe *p = (Vdbe*)
19e7a 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 pStmt;. return
19e7b 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 78 70 69 72 p==0 || p->expir
19e7c 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a ed;.}.#endif../*
19e7d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
19e7e 67 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f g routine destro
19e7f 79 73 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 ys a virtual mac
19e80 68 69 6e 65 20 74 68 61 74 20 69 73 20 63 72 65 hine that is cre
19e81 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 ated by.** the s
19e82 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 qlite3_compile()
19e83 20 72 6f 75 74 69 6e 65 2e 20 54 68 65 20 69 6e routine. The in
19e84 74 65 67 65 72 20 72 65 74 75 72 6e 65 64 20 69 teger returned i
19e85 73 20 61 6e 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 s an SQLITE_.**
19e86 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65 20 success/failure
19e87 63 6f 64 65 20 74 68 61 74 20 64 65 73 63 72 69 code that descri
19e88 62 65 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f bes the result o
19e89 66 20 65 78 65 63 75 74 69 6e 67 20 74 68 65 20 f executing the
19e8a 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 virtual.** machi
19e8b 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ne..**.** This r
19e8c 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 outine sets the
19e8d 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 error code and s
19e8e 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 62 tring returned b
19e8f 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 y.** sqlite3_err
19e90 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 33 5f code(), sqlite3_
19e91 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c errmsg() and sql
19e92 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 2e ite3_errmsg16().
19e93 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
19e94 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c nt sqlite3_final
19e95 69 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ize(sqlite3_stmt
19e96 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 *pStmt){. int
19e97 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d rc;. if( pStmt=
19e98 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 =0 ){. rc = S
19e99 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
19e9a 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d e{. Vdbe *v =
19e9b 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
19e9c 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d sqlite3 *db =
19e9d 20 76 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 v->db;.#if SQLI
19e9e 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 TE_THREADSAFE.
19e9f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
19ea0 2a 6d 75 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e *mutex = v->db->
19ea1 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 mutex;.#endif.
19ea2 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
19ea3 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 enter(mutex);.
19ea4 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 stmtLruRemove(
19ea5 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c v);. rc = sql
19ea6 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 ite3VdbeFinalize
19ea7 28 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 (v);. rc = sq
19ea8 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c lite3ApiExit(db,
19ea9 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 rc);. sqlite
19eaa 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
19eab 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
19eac 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
19ead 54 65 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75 Terminate the cu
19eae 72 72 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 rrent execution
19eaf 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d of an SQL statem
19eb0 65 6e 74 20 61 6e 64 20 72 65 73 65 74 20 69 74 ent and reset it
19eb1 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20 .** back to its
19eb2 73 74 61 72 74 69 6e 67 20 73 74 61 74 65 20 73 starting state s
19eb3 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 o that it can be
19eb4 20 72 65 75 73 65 64 2e 20 41 20 73 75 63 63 65 reused. A succe
19eb5 73 73 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 ss code from.**
19eb6 74 68 65 20 70 72 69 6f 72 20 65 78 65 63 75 74 the prior execut
19eb7 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e ion is returned.
19eb8 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
19eb9 69 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 ine sets the err
19eba 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 or code and stri
19ebb 6e 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a ng returned by.*
19ebc 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 * sqlite3_errcod
19ebd 65 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 e(), sqlite3_err
19ebe 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 msg() and sqlite
19ebf 33 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 3_errmsg16()..*/
19ec0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
19ec1 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 sqlite3_reset(sq
19ec2 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
19ec3 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 t){. int rc;.
19ec4 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a if( pStmt==0 ){.
19ec5 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
19ec6 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
19ec7 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 Vdbe *v = (Vdbe
19ec8 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c *)pStmt;. sql
19ec9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
19eca 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (v->db->mutex);.
19ecb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
19ecc 56 64 62 65 52 65 73 65 74 28 76 29 3b 0a 20 20 VdbeReset(v);.
19ecd 20 20 73 74 6d 74 4c 72 75 41 64 64 28 76 29 3b stmtLruAdd(v);
19ece 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
19ecf 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 2d 31 2c MakeReady(v, -1,
19ed0 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 0, 0, 0);. a
19ed1 73 73 65 72 74 28 20 28 72 63 20 26 20 28 76 2d ssert( (rc & (v-
19ed2 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d >db->errMask))==
19ed3 72 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 rc );. rc = s
19ed4 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 76 2d qlite3ApiExit(v-
19ed5 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 >db, rc);. sq
19ed6 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
19ed7 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(v->db->mutex);
19ed8 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
19ed9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 ;.}../*.** Set a
19eda 6c 6c 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 ll the parameter
19edb 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 s in the compile
19edc 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 d SQL statement
19edd 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 to NULL..*/.SQLI
19ede 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
19edf 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 e3_clear_binding
19ee0 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a s(sqlite3_stmt *
19ee1 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 69 3b pStmt){. int i;
19ee2 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
19ee3 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 65 20 2a 70 TE_OK;. Vdbe *p
19ee4 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
19ee5 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
19ee6 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 ADSAFE. sqlite3
19ee7 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 _mutex *mutex =
19ee8 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e ((Vdbe*)pStmt)->
19ee9 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 69 db->mutex;.#endi
19eea 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 f. sqlite3_mute
19eeb 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a x_enter(mutex);.
19eec 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e for(i=0; i<p->
19eed 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nVar; i++){.
19eee 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
19eef 6c 65 61 73 65 28 26 70 2d 3e 61 56 61 72 5b 69 lease(&p->aVar[i
19ef0 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56 61 72 5b ]);. p->aVar[
19ef1 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e i].flags = MEM_N
19ef2 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ull;. }. sqlit
19ef3 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
19ef4 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
19ef5 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a rc;.}.../*******
19ef6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ef7 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 ***** sqlite3_va
19ef8 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a lue_ **********
19ef9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19efa 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c *****.** The fol
19efb 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 lowing routines
19efc 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 extract informat
19efd 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 6d 20 6f ion from a Mem o
19efe 72 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 0a r sqlite3_value.
19eff 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f ** structure..*/
19f00 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
19f01 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
19f02 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 value_blob(sqlit
19f03 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b e3_value *pVal){
19f04 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d . Mem *p = (Mem
19f05 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 70 2d *)pVal;. if( p-
19f06 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c >flags & (MEM_Bl
19f07 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20 ob|MEM_Str) ){.
19f08 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19f09 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 29 3b 0a mExpandBlob(p);.
19f0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 p->flags &=
19f0b 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 70 2d ~MEM_Str;. p-
19f0c 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c >flags |= MEM_Bl
19f0d 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ob;. return p
19f0e 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ->z;. }else{.
19f0f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
19f10 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56 61 6c _value_text(pVal
19f11 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f );. }.}.SQLITE_
19f12 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
19f13 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71 6c 69 value_bytes(sqli
19f14 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 te3_value *pVal)
19f15 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
19f16 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 e3ValueBytes(pVa
19f17 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b l, SQLITE_UTF8);
19f18 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
19f19 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
19f1a 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f bytes16(sqlite3_
19f1b 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
19f1c 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 return sqlite3Va
19f1d 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53 lueBytes(pVal, S
19f1e 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 QLITE_UTF16NATIV
19f1f 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 E);.}.SQLITE_API
19f20 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f double sqlite3_
19f21 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c value_double(sql
19f22 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
19f23 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
19f24 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 te3VdbeRealValue
19f25 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a ((Mem*)pVal);.}.
19f26 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
19f27 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
19f28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
19f29 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
19f2a 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 (int)sqlite3Vdbe
19f2b 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 IntValue((Mem*)p
19f2c 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 Val);.}.SQLITE_A
19f2d 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 PI sqlite_int64
19f2e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
19f2f 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 t64(sqlite3_valu
19f30 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e *pVal){. retu
19f31 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e rn sqlite3VdbeIn
19f32 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 tValue((Mem*)pVa
19f33 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 l);.}.SQLITE_API
19f34 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
19f35 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 char *sqlite3_va
19f36 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 lue_text(sqlite3
19f37 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 _value *pVal){.
19f38 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 75 return (const u
19f39 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73 nsigned char *)s
19f3a 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
19f3b 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 pVal, SQLITE_UTF
19f3c 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 8);.}.#ifndef SQ
19f3d 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
19f3e 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
19f3f 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 void *sqlite3_v
19f40 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c 69 alue_text16(sqli
19f41 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 te3_value* pVal)
19f42 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
19f43 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c e3ValueText(pVal
19f44 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 , SQLITE_UTF16NA
19f45 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f TIVE);.}.SQLITE_
19f46 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
19f47 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
19f48 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 xt16be(sqlite3_v
19f49 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 alue *pVal){. r
19f4a 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c eturn sqlite3Val
19f4b 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c ueText(pVal, SQL
19f4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b 0a 7d 0a ITE_UTF16BE);.}.
19f4d 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
19f4e 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 void *sqlite3_v
19f4f 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 alue_text16le(sq
19f50 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
19f51 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c l){. return sql
19f52 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 ite3ValueText(pV
19f53 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 al, SQLITE_UTF16
19f54 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a LE);.}.#endif /*
19f55 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
19f56 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 16 */.SQLITE_API
19f57 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
19f58 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f ue_type(sqlite3_
19f59 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 value* pVal){.
19f5a 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 74 79 70 return pVal->typ
19f5b 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a e;.}../*********
19f5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f5d 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 *** sqlite3_resu
19f5e 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a lt_ ***********
19f5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f60 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ****.** The foll
19f61 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 owing routines a
19f62 72 65 20 75 73 65 64 20 62 79 20 75 73 65 72 2d re used by user-
19f63 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
19f64 73 20 74 6f 20 73 70 65 63 69 66 79 0a 2a 2a 20 s to specify.**
19f65 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 the function res
19f66 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ult..*/.SQLITE_A
19f67 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
19f68 72 65 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 result_blob(. s
19f69 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
19f6a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 pCtx, . const v
19f6b 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e oid *z, . int n
19f6c 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c , . void (*xDel
19f6d 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 )(void *).){. a
19f6e 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 ssert( n>=0 );.
19f6f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
19f70 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
19f71 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
19f72 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
19f73 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e emSetStr(&pCtx->
19f74 73 2c 20 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c s, z, n, 0, xDel
19f75 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
19f76 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
19f77 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 ult_double(sqlit
19f78 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
19f79 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a , double rVal){.
19f7a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
19f7b 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
19f7c 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
19f7d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
19f7e 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 43 MemSetDouble(&pC
19f7f 74 78 2d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a tx->s, rVal);.}.
19f80 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
19f81 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
19f82 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e rror(sqlite3_con
19f83 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 text *pCtx, cons
19f84 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e t char *z, int n
19f85 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
19f86 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19f87 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
19f88 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 x) );. pCtx->is
19f89 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 Error = SQLITE_E
19f8a 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 RROR;. sqlite3V
19f8b 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 dbeMemSetStr(&pC
19f8c 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c tx->s, z, n, SQL
19f8d 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
19f8e 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 _TRANSIENT);.}.#
19f8f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
19f90 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
19f91 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
19f92 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
19f93 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
19f94 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 *pCtx, const voi
19f95 64 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 d *z, int n){.
19f96 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
19f97 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
19f98 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
19f99 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 . pCtx->isError
19f9a 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
19f9b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
19f9c 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 mSetStr(&pCtx->s
19f9d 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 , z, n, SQLITE_U
19f9e 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 TF16NATIVE, SQLI
19f9f 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d TE_TRANSIENT);.}
19fa0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 .#endif.SQLITE_A
19fa1 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
19fa2 72 65 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 result_int(sqlit
19fa3 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
19fa4 2c 20 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61 , int iVal){. a
19fa5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
19fa6 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
19fa7 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
19fa8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19fa9 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e SetInt64(&pCtx->
19faa 73 2c 20 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d s, (i64)iVal);.}
19fab 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
19fac 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
19fad 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f int64(sqlite3_co
19fae 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 36 34 ntext *pCtx, i64
19faf 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 iVal){. assert
19fb0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
19fb1 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
19fb2 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
19fb3 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e ite3VdbeMemSetIn
19fb4 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 t64(&pCtx->s, iV
19fb5 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 al);.}.SQLITE_AP
19fb6 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
19fb7 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 esult_null(sqlit
19fb8 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
19fb9 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
19fba 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19fbb 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
19fbc 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 x) );. sqlite3V
19fbd 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 dbeMemSetNull(&p
19fbe 43 74 78 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 Ctx->s);.}.SQLIT
19fbf 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
19fc0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 0a e3_result_text(.
19fc1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
19fc2 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 t *pCtx, . cons
19fc3 74 20 63 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e t char *z, . in
19fc4 74 20 6e 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 t n,. void (*xD
19fc5 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 el)(void *).){.
19fc6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
19fc7 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
19fc8 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
19fc9 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
19fca 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e emSetStr(&pCtx->
19fcb 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f s, z, n, SQLITE_
19fcc 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 UTF8, xDel);.}.#
19fcd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
19fce 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
19fcf 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
19fd0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0a _result_text16(.
19fd1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
19fd2 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 t *pCtx, . cons
19fd3 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e t void *z, . in
19fd4 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 t n, . void (*x
19fd5 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a Del)(void *).){.
19fd6 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
19fd7 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
19fd8 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
19fd9 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
19fda 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d MemSetStr(&pCtx-
19fdb 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 >s, z, n, SQLITE
19fdc 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44 _UTF16NATIVE, xD
19fdd 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 el);.}.SQLITE_AP
19fde 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
19fdf 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a esult_text16be(.
19fe0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
19fe1 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 t *pCtx, . cons
19fe2 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e t void *z, . in
19fe3 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 t n, . void (*x
19fe4 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a Del)(void *).){.
19fe5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
19fe6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
19fe7 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
19fe8 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
19fe9 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d MemSetStr(&pCtx-
19fea 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 >s, z, n, SQLITE
19feb 5f 55 54 46 31 36 42 45 2c 20 78 44 65 6c 29 3b _UTF16BE, xDel);
19fec 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
19fed 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
19fee 74 5f 74 65 78 74 31 36 6c 65 28 0a 20 20 73 71 t_text16le(. sq
19fef 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
19ff0 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f Ctx, . const vo
19ff1 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c id *z, . int n,
19ff2 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 . void (*xDel)
19ff3 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 (void *).){. as
19ff4 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
19ff5 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
19ff6 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
19ff7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19ff8 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
19ff9 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
19ffa 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 16LE, xDel);.}.#
19ffb 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
19ffc 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 OMIT_UTF16 */.SQ
19ffd 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
19ffe 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
19fff 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ue(sqlite3_conte
1a000 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 74 65 xt *pCtx, sqlite
1a001 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 3_value *pValue)
1a002 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1a003 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1a004 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1a005 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 ) );. sqlite3Vd
1a006 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 2d beMemCopy(&pCtx-
1a007 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 >s, pValue);.}.S
1a008 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1a009 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 qlite3_result_ze
1a00a 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 roblob(sqlite3_c
1a00b 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e ontext *pCtx, in
1a00c 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 t n){. assert(
1a00d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1a00e 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1a00f 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1a010 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f e3VdbeMemSetZero
1a011 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e Blob(&pCtx->s, n
1a012 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1a013 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1a014 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 ult_error_code(s
1a015 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1a016 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 6f 64 pCtx, int errCod
1a017 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 e){. pCtx->isEr
1a018 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a 7d ror = errCode;.}
1a019 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 ../* Force an SQ
1a01a 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f LITE_TOOBIG erro
1a01b 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r. */.SQLITE_API
1a01c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1a01d 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 sult_error_toobi
1a01e 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 g(sqlite3_contex
1a01f 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 t *pCtx){. asse
1a020 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1a021 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1a022 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
1a023 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 Ctx->isError = S
1a024 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 QLITE_TOOBIG;.
1a025 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1a026 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 tStr(&pCtx->s, "
1a027 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 string or blob t
1a028 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 oo big", -1, .
1a029 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a02a 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 SQLITE_UTF8
1a02b 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 , SQLITE_STATIC)
1a02c 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 ;.}../* An SQLIT
1a02d 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a E_NOMEM error. *
1a02e 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
1a02f 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1a030 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c _error_nomem(sql
1a031 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1a032 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 tx){. assert( s
1a033 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1a034 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1a035 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
1a036 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1a037 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 &pCtx->s);. pCt
1a038 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c x->isError = SQL
1a039 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 ITE_NOMEM;. pCt
1a03a 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 x->s.db->mallocF
1a03b 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a ailed = 1;.}../*
1a03c 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 .** Execute the
1a03d 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c statement pStmt,
1a03e 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61 20 either until a
1a03f 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 row of data is r
1a040 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 eady, the.** sta
1a041 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 tement is comple
1a042 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72 tely executed or
1a043 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1a044 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1a045 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 tine implements
1a046 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 the bulk of the
1a047 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68 65 logic behind the
1a048 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a sqlite_step().*
1a049 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 * API. The only
1a04a 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 thing omitted i
1a04b 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 s the automatic
1a04c 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a recompile if a .
1a04d 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 ** schema change
1a04e 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 has occurred.
1a04f 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20 68 That detail is h
1a050 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a andled by the.**
1a051 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f 73 outer sqlite3_s
1a052 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70 72 tep() wrapper pr
1a053 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 ocedure..*/.stat
1a054 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 ic int sqlite3St
1a055 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 ep(Vdbe *p){. s
1a056 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e qlite3 *db;. in
1a057 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
1a058 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 p);. if( p->mag
1a059 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 ic!=VDBE_MAGIC_R
1a05a 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e UN ){. return
1a05b 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
1a05c 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 }.. /* Assert
1a05d 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 that malloc() h
1a05e 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f as not failed */
1a05f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 . db = p->db;.
1a060 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1a061 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 ailed ){. ret
1a062 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1a063 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e ;. }.. if( p->
1a064 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 69 pc<=0 && p->expi
1a065 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 20 70 red ){. if( p
1a066 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
1a067 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d ){. p->rc =
1a068 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a SQLITE_SCHEMA;.
1a069 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 }. rc = S
1a06a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1a06b 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 goto end_of_ste
1a06c 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c p;. }. if( sql
1a06d 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1a06e 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1a06f 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1a070 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1a071 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 _MISUSE;. }. i
1a072 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 f( p->pc<0 ){.
1a073 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
1a074 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 e no other state
1a075 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 ments currently
1a076 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20 running, then.
1a077 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69 ** reset the i
1a078 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20 nterrupt flag.
1a079 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 This prevents a
1a07a 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
1a07b 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a 2a interrupt. **
1a07c 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69 from interrupti
1a07d 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 ng a statement t
1a07e 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 hat has not yet
1a07f 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a started.. */.
1a080 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 if( db->acti
1a081 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a veVdbeCnt==0 ){.
1a082 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 db->u1.isI
1a083 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a nterrupted = 0;.
1a084 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 }..#ifndef S
1a085 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
1a086 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 . if( db->xPr
1a087 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e ofile && !db->in
1a088 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 it.busy ){.
1a089 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 double rNow;.
1a08a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 sqlite3OsCur
1a08b 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 rentTime(db->pVf
1a08c 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20 s, &rNow);.
1a08d 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d 20 p->startTime =
1a08e 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 (u64)((rNow - (i
1a08f 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a nt)rNow)*3600.0*
1a090 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 24.0*1000000000.
1a091 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0);. }.#endif
1a092 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65 .. db->active
1a093 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 69 VdbeCnt++;. i
1a094 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d f( p->readOnly==
1a095 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 62 0 ) db->writeVdb
1a096 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70 eCnt++;. p->p
1a097 63 20 3d 20 30 3b 0a 20 20 20 20 73 74 6d 74 4c c = 0;. stmtL
1a098 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 7d ruRemove(p);. }
1a099 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a09a 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 OMIT_EXPLAIN. i
1a09b 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b f( p->explain ){
1a09c 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1a09d 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20 3VdbeList(p);.
1a09e 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 }else.#endif /*
1a09f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
1a0a0 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72 AIN */. {. r
1a0a1 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 c = sqlite3VdbeE
1a0a2 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 xec(p);. }.. i
1a0a3 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1a0a4 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 Off(db) ){. r
1a0a5 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 c = SQLITE_MISUS
1a0a6 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 E;. }..#ifndef
1a0a7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1a0a8 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 E. /* Invoke th
1a0a9 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 e profile callba
1a0aa 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ck if there is o
1a0ab 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 ne. */. if( rc
1a0ac 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 !=SQLITE_ROW &&
1a0ad 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 db->xProfile &&
1a0ae 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 !db->init.busy &
1a0af 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20 & p->zSql ){.
1a0b0 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 double rNow;.
1a0b1 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65 u64 elapseTime
1a0b2 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ;.. sqlite3Os
1a0b3 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e CurrentTime(db->
1a0b4 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 pVfs, &rNow);.
1a0b5 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28 elapseTime = (
1a0b6 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e u64)((rNow - (in
1a0b7 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 t)rNow)*3600.0*2
1a0b8 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 4.0*1000000000.0
1a0b9 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d );. elapseTim
1a0ba 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d e -= p->startTim
1a0bb 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66 e;. db->xProf
1a0bc 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65 ile(db->pProfile
1a0bd 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c Arg, p->zSql, el
1a0be 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 apseTime);. }.#
1a0bf 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72 endif.. db->err
1a0c0 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28 Code = rc;. if(
1a0c1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 SQLITE_NOMEM==s
1a0c2 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d qlite3ApiExit(p-
1a0c3 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20 >db, p->rc) ){.
1a0c4 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 p->rc = SQLIT
1a0c5 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64 E_NOMEM;. }.end
1a0c6 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41 _of_step:. /* A
1a0c7 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63 t this point loc
1a0c8 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68 al variable rc h
1a0c9 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74 olds the value t
1a0ca 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 hat should be .
1a0cb 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 ** returned if
1a0cc 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77 this statement w
1a0cd 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e as compiled usin
1a0ce 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20 g the legacy .
1a0cf 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 ** sqlite3_prepa
1a0d0 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 re() interface.
1a0d1 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 According to the
1a0d2 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20 docs, this can
1a0d3 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 only. ** be one
1a0d4 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 of the values i
1a0d5 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 65 n the first asse
1a0d6 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69 rt() below. Vari
1a0d7 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a able p->rc . **
1a0d8 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 contains the va
1a0d9 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 lue that would b
1a0da 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 71 e returned if sq
1a0db 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
1a0dc 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c . ** were call
1a0dd 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20 ed on statement
1a0de 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 p.. */. assert
1a0df 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 ( rc==SQLITE_ROW
1a0e0 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f || rc==SQLITE_
1a0e1 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51 DONE || rc==SQ
1a0e2 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20 LITE_ERROR .
1a0e3 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 || rc==SQLITE
1a0e4 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c _BUSY || rc==SQL
1a0e5 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a ITE_MISUSE. );.
1a0e6 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21 assert( p->rc!
1a0e7 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70 =SQLITE_ROW && p
1a0e8 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e ->rc!=SQLITE_DON
1a0e9 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73 E );. if( p->is
1a0ea 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 21 PrepareV2 && rc!
1a0eb 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 =SQLITE_ROW && r
1a0ec 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 c!=SQLITE_DONE )
1a0ed 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
1a0ee 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 statement was p
1a0ef 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 71 repared using sq
1a0f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1a0f1 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a (), and an. *
1a0f2 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 * error has occu
1a0f3 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e red, then return
1a0f4 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
1a0f5 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a in p->rc to the.
1a0f6 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 ** caller. S
1a0f7 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 et the error cod
1a0f8 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
1a0f9 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 e handle to the
1a0fa 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20 same value..
1a0fb 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d */ . rc = db-
1a0fc 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 >errCode = p->rc
1a0fd 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
1a0fe 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b rc&db->errMask);
1a0ff 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 .}../*.** This i
1a100 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 s the top-level
1a101 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1a102 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 f sqlite3_step()
1a103 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 . Call.** sqlit
1a104 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d e3Step() to do m
1a105 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e ost of the work.
1a106 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 If a schema er
1a107 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 ror occurs,.** c
1a108 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 all sqlite3Repre
1a109 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 pare() and try a
1a10a 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 gain..*/.#ifdef
1a10b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 SQLITE_OMIT_PARS
1a10c 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ER.SQLITE_API in
1a10d 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 t sqlite3_step(s
1a10e 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1a10f 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 mt){. int rc =
1a110 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1a111 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 if( pStmt ){.
1a112 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 76 Vdbe *v;. v
1a113 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
1a114 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1a115 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e ex_enter(v->db->
1a116 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d mutex);. rc =
1a117 20 73 71 6c 69 74 65 33 53 74 65 70 28 76 29 3b sqlite3Step(v);
1a118 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1a119 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e ex_leave(v->db->
1a11a 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 mutex);. }. re
1a11b 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65 turn rc;.}.#else
1a11c 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1a11d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c sqlite3_step(sql
1a11e 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1a11f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1a120 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 LITE_MISUSE;. i
1a121 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 f( pStmt ){.
1a122 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 int cnt = 0;.
1a123 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 Vdbe *v = (Vdbe
1a124 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c *)pStmt;. sql
1a125 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 ite3 *db = v->db
1a126 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1a127 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 tex_enter(db->mu
1a128 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 tex);. while(
1a129 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 (rc = sqlite3St
1a12a 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 ep(v))==SQLITE_S
1a12b 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 CHEMA.
1a12c 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 && cnt++ < 5.
1a12d 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63 20 && (rc
1a12e 3d 20 76 64 62 65 52 65 70 72 65 70 61 72 65 28 = vdbeReprepare(
1a12f 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 v))==SQLITE_OK )
1a130 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1a131 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 reset(pStmt);.
1a132 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20 3d v->expired =
1a133 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0;. }. if
1a134 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 ( rc==SQLITE_SCH
1a135 45 4d 41 20 26 26 20 76 2d 3e 69 73 50 72 65 70 EMA && v->isPrep
1a136 61 72 65 56 32 20 26 26 20 64 62 2d 3e 70 45 72 areV2 && db->pEr
1a137 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 r ){. /* Th
1a138 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 61 is case occurs a
1a139 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f 20 fter failing to
1a13a 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71 6c recompile an sql
1a13b 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20 20 statement. .
1a13c 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 ** The error
1a13d 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 message from the
1a13e 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 61 SQL compiler ha
1a13f 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c s already been l
1a140 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 oaded . **
1a141 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
1a142 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 62 e handle. This b
1a143 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 20 lock copies the
1a144 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a 20 error message .
1a145 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 ** from the
1a146 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1a147 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65 6d into the statem
1a148 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68 65 ent and sets the
1a149 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 statement.
1a14a 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e ** program coun
1a14b 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73 75 ter to 0 to ensu
1a14c 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 re that when the
1a14d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a 20 statement is .
1a14e 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a 65 ** finalize
1a14f 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 70 d or reset the p
1a150 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73 73 arser error mess
1a151 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 age is available
1a152 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73 71 via. ** sq
1a153 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 lite3_errmsg() a
1a154 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f nd sqlite3_errco
1a155 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 de().. */.
1a156 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1a157 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20 63 *zErr = (const c
1a158 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 har *)sqlite3_va
1a159 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72 lue_text(db->pEr
1a15a 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69 74 r); . sqlit
1a15b 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d 3e e3DbFree(db, v->
1a15c 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 zErrMsg);.
1a15d 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( !db->mallocF
1a15e 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 ailed ){.
1a15f 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 v->zErrMsg = sq
1a160 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
1a161 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d , zErr);. }
1a162 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 else {.
1a163 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a v->zErrMsg = 0;.
1a164 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 v->rc =
1a165 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1a166 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1a167 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 rc = sqlite3ApiE
1a168 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 xit(db, rc);.
1a169 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1a16a 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b eave(db->mutex);
1a16b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1a16c 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1a16d 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73 * Extract the us
1a16e 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 er data from a s
1a16f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 qlite3_context s
1a170 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 tructure and ret
1a171 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 urn a.** pointer
1a172 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 to it..*/.SQLIT
1a173 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
1a174 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 te3_user_data(sq
1a175 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1a176 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 ){. assert( p &
1a177 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 & p->pFunc );.
1a178 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d return p->pFunc-
1a179 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f >pUserData;.}../
1a17a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 *.** Extract the
1a17b 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 user data from
1a17c 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a sqlite3_contex
1a17d 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 t structure and
1a17e 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e return a.** poin
1a17f 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 ter to it..*/.SQ
1a180 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
1a181 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 *sqlite3_contex
1a182 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 t_db_handle(sqli
1a183 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b te3_context *p){
1a184 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 . assert( p &&
1a185 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 p->pFunc );. re
1a186 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a turn p->s.db;.}.
1a187 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
1a188 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c wing is the impl
1a189 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e ementation of an
1a18a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 SQL function th
1a18b 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 at always.** fai
1a18c 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 ls with an error
1a18d 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 message stating
1a18e 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 that the functi
1a18f 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 on is used in th
1a190 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 e.** wrong conte
1a191 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 xt. The sqlite3
1a192 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 _overload_functi
1a193 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 on() API might c
1a194 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 onstruct.** SQL
1a195 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 function that us
1a196 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 e this routine s
1a197 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 o that the funct
1a198 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a ions will exist.
1a199 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f ** for name reso
1a19a 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 lution but are a
1a19b 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 ctually overload
1a19c 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 ed by the xFindF
1a19d 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f unction.** metho
1a19e 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 d of virtual tab
1a19f 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 les..*/.SQLITE_P
1a1a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a1a1 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 te3InvalidFuncti
1a1a2 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f on(. sqlite3_co
1a1a3 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
1a1a4 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e /* The function
1a1a5 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 calling context
1a1a6 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 */. int NotUse
1a1a7 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
1a1a8 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 /* Number of ar
1a1a9 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 guments to the f
1a1aa 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c unction */. sql
1a1ab 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 ite3_value **Not
1a1ac 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 Used2 /* Value
1a1ad 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e of each argumen
1a1ae 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 t */.){. const
1a1af 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f char *zName = co
1a1b0 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e ntext->pFunc->zN
1a1b1 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 ame;. char *zEr
1a1b2 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 r;. UNUSED_PARA
1a1b3 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 METER2(NotUsed,
1a1b4 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 NotUsed2);. zEr
1a1b5 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e r = sqlite3MPrin
1a1b6 74 66 28 30 2c 0a 20 20 20 20 20 20 22 75 6e 61 tf(0,. "una
1a1b7 62 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 ble to use funct
1a1b8 69 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 ion %s in the re
1a1b9 71 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 quested context"
1a1ba 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 , zName);. sqli
1a1bb 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
1a1bc 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 (context, zErr,
1a1bd 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 -1);. sqlite3_f
1a1be 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a ree(zErr);.}../*
1a1bf 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 .** Allocate or
1a1c0 72 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 return the aggre
1a1c1 67 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 gate context for
1a1c2 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e a user function
1a1c3 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 . A new.** cont
1a1c4 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ext is allocated
1a1c5 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 on the first ca
1a1c6 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 ll. Subsequent
1a1c7 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 calls return the
1a1c8 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 .** same context
1a1c9 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e that was return
1a1ca 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c ed on prior call
1a1cb 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
1a1cc 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 void *sqlite3_a
1a1cd 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 ggregate_context
1a1ce 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1a1cf 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b *p, int nByte){
1a1d0 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 . Mem *pMem;.
1a1d1 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e assert( p && p->
1a1d2 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e pFunc && p->pFun
1a1d3 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 c->xStep );. as
1a1d4 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1a1d5 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 tex_held(p->s.db
1a1d6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d ->mutex) );. pM
1a1d7 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 em = p->pMem;.
1a1d8 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( (pMem->flags
1a1d9 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 & MEM_Agg)==0 )
1a1da 7b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d {. if( nByte=
1a1db 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
1a1dc 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1a1dd 65 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b eExternal(pMem);
1a1de 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
1a1df 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1a1e0 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 pMem->z = 0
1a1e1 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1a1e2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1a1e3 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 mGrow(pMem, nByt
1a1e4 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 e, 0);. pMe
1a1e5 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 m->flags = MEM_A
1a1e6 67 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e gg;. pMem->
1a1e7 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e u.pDef = p->pFun
1a1e8 63 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 c;. if( pMe
1a1e9 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 m->z ){.
1a1ea 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 memset(pMem->z,
1a1eb 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 0, nByte);.
1a1ec 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
1a1ed 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 eturn (void*)pMe
1a1ee 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 m->z;.}../*.** R
1a1ef 65 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 eturn the auxila
1a1f0 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c ry data pointer,
1a1f1 20 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 if any, for the
1a1f2 20 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e iArg'th argumen
1a1f3 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 t to.** the user
1a1f4 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 -function define
1a1f5 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 d by pCtx..*/.SQ
1a1f6 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
1a1f7 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 qlite3_get_auxda
1a1f8 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ta(sqlite3_conte
1a1f9 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 xt *pCtx, int iA
1a1fa 72 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 rg){. VdbeFunc
1a1fb 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 *pVdbeFunc;.. a
1a1fc 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1a1fd 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1a1fe 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1a1ff 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 pVdbeFunc = pC
1a200 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 tx->pVdbeFunc;.
1a201 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 if( !pVdbeFunc
1a202 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 || iArg>=pVdbeFu
1a203 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 nc->nAux || iArg
1a204 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e <0 ){. return
1a205 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
1a206 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 pVdbeFunc->apAu
1a207 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a x[iArg].pAux;.}.
1a208 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 ./*.** Set the a
1a209 75 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 uxilary data poi
1a20a 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 nter and delete
1a20b 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 function, for th
1a20c 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 e iArg'th.** arg
1a20d 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 ument to the use
1a20e 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e r-function defin
1a20f 65 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 ed by pCtx. Any
1a210 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 previous value i
1a211 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 s.** deleted by
1a212 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 calling the dele
1a213 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 te function spec
1a214 69 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 ified when it wa
1a215 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 s set..*/.SQLITE
1a216 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1a217 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 3_set_auxdata(.
1a218 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1a219 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 *pCtx, . int i
1a21a 41 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 Arg, . void *pA
1a21b 75 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 ux, . void (*xD
1a21c 65 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b elete)(void*).){
1a21d 0a 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 . struct AuxDat
1a21e 61 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 a *pAuxData;. V
1a21f 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 dbeFunc *pVdbeFu
1a220 6e 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 nc;. if( iArg<0
1a221 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a ) goto failed;.
1a222 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1a223 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1a224 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1a225 20 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 );. pVdbeFunc
1a226 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e = pCtx->pVdbeFun
1a227 63 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 c;. if( !pVdbeF
1a228 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 unc || pVdbeFunc
1a229 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a ->nAux<=iArg ){.
1a22a 20 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 int nAux = (
1a22b 70 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 pVdbeFunc ? pVdb
1a22c 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 eFunc->nAux : 0)
1a22d 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f ;. int nMallo
1a22e 63 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 c = sizeof(VdbeF
1a22f 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 unc) + sizeof(st
1a230 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 ruct AuxData)*iA
1a231 72 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e rg;. pVdbeFun
1a232 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 c = sqlite3DbRea
1a233 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c lloc(pCtx->s.db,
1a234 20 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c pVdbeFunc, nMal
1a235 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 loc);. if( !p
1a236 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 VdbeFunc ){.
1a237 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 goto failed;.
1a238 20 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 }. pCtx->p
1a239 56 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 VdbeFunc = pVdbe
1a23a 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 Func;. memset
1a23b 28 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 (&pVdbeFunc->apA
1a23c 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a ux[nAux], 0, siz
1a23d 65 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61 eof(struct AuxDa
1a23e 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 ta)*(iArg+1-nAux
1a23f 29 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e ));. pVdbeFun
1a240 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 c->nAux = iArg+1
1a241 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d ;. pVdbeFunc-
1a242 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 >pFunc = pCtx->p
1a243 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 Func;. }.. pAu
1a244 78 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 xData = &pVdbeFu
1a245 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b nc->apAux[iArg];
1a246 0a 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d . if( pAuxData-
1a247 3e 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74 >pAux && pAuxDat
1a248 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 a->xDelete ){.
1a249 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c pAuxData->xDel
1a24a 65 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 ete(pAuxData->pA
1a24b 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 ux);. }. pAuxD
1a24c 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 ata->pAux = pAux
1a24d 3b 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 ;. pAuxData->xD
1a24e 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b elete = xDelete;
1a24f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c . return;..fail
1a250 65 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 ed:. if( xDelet
1a251 65 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 e ){. xDelete
1a252 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 (pAux);. }.}..#
1a253 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1a254 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a IT_DEPRECATED./*
1a255 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1a256 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 umber of times t
1a257 68 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e he Step function
1a258 20 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 of a aggregate
1a259 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c has been .** cal
1a25a 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 led..**.** This
1a25b 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 function is depr
1a25c 65 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 ecated. Do not
1a25d 75 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 use it for new c
1a25e 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 ode. It is.** p
1a25f 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 rovide only to a
1a260 76 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 void breaking le
1a261 67 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 gacy code. New
1a262 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
1a263 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 on.** implementa
1a264 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 tions should kee
1a265 70 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e p their own coun
1a266 74 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 ts within their
1a267 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e aggregate.** con
1a268 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f text..*/.SQLITE_
1a269 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1a26a 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 aggregate_count(
1a26b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1a26c 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
1a26d 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 && p->pMem && p
1a26e 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 ->pFunc && p->pF
1a26f 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 unc->xStep );.
1a270 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e return p->pMem->
1a271 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a n;.}.#endif../*.
1a272 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1a273 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1a274 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
1a275 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d t for the statem
1a276 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 ent pStmt..*/.SQ
1a277 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1a278 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e ite3_column_coun
1a279 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
1a27a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a pStmt){. Vdbe *
1a27b 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 pVm = (Vdbe *)pS
1a27c 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 tmt;. return pV
1a27d 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c m ? pVm->nResCol
1a27e 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a umn : 0;.}../*.*
1a27f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1a280 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 ber of values av
1a281 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 ailable from the
1a282 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 current row of
1a283 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 the.** currently
1a284 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 executing state
1a285 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 ment pStmt..*/.S
1a286 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1a287 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 lite3_data_count
1a288 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1a289 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 Stmt){. Vdbe *p
1a28a 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 Vm = (Vdbe *)pSt
1a28b 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 mt;. if( pVm==0
1a28c 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 || pVm->pResult
1a28d 53 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 Set==0 ) return
1a28e 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 0;. return pVm-
1a28f 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a >nResColumn;.}..
1a290 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
1a291 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 see if column iC
1a292 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 ol of the given
1a293 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c statement is val
1a294 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 id. If.** it is
1a295 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 , return a point
1a296 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f er to the Mem fo
1a297 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 r the value of t
1a298 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 hat column..** I
1a299 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 f iCol is not va
1a29a 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f lid, return a po
1a29b 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 inter to a Mem w
1a29c 68 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 65 hich has a value
1a29d 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a .** of NULL..*/.
1a29e 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 static Mem *colu
1a29f 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 mnMem(sqlite3_st
1a2a0 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1a2a1 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a ){. Vdbe *pVm;.
1a2a2 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 int vals;. Me
1a2a3 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 m *pOut;.. pVm
1a2a4 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
1a2a5 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 . if( pVm && pV
1a2a6 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 m->pResultSet!=0
1a2a7 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 && i<pVm->nResC
1a2a8 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b olumn && i>=0 ){
1a2a9 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1a2aa 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 ex_enter(pVm->db
1a2ab 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61 ->mutex);. va
1a2ac 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 ls = sqlite3_dat
1a2ad 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a a_count(pStmt);.
1a2ae 20 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d pOut = &pVm-
1a2af 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a >pResultSet[i];.
1a2b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1a2b1 28 28 64 6f 75 62 6c 65 29 30 29 20 49 6e 20 63 ((double)0) In c
1a2b2 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d ase of SQLITE_OM
1a2b3 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
1a2b4 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 73 74 61 74 T... */. stat
1a2b5 69 63 20 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c ic const Mem nul
1a2b6 6c 4d 65 6d 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f lMem = {{0}, (do
1a2b7 75 62 6c 65 29 30 2c 20 30 2c 20 22 22 2c 20 30 uble)0, 0, "", 0
1a2b8 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 , MEM_Null, SQLI
1a2b9 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 TE_NULL, 0, 0, 0
1a2ba 20 7d 3b 0a 20 20 20 20 69 66 28 20 70 56 6d 20 };. if( pVm
1a2bb 26 26 20 70 56 6d 2d 3e 64 62 20 29 7b 0a 20 20 && pVm->db ){.
1a2bc 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1a2bd 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d x_enter(pVm->db-
1a2be 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 73 >mutex);. s
1a2bf 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d 2d qlite3Error(pVm-
1a2c0 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 >db, SQLITE_RANG
1a2c1 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 E, 0);. }.
1a2c2 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26 6e pOut = (Mem*)&n
1a2c3 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 ullMem;. }. re
1a2c4 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a turn pOut;.}../*
1a2c5 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1a2c6 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 n is called afte
1a2c7 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73 71 r invoking an sq
1a2c8 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58 20 lite3_value_XXX
1a2c9 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a function on a .*
1a2ca 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 28 * column value (
1a2cb 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65 74 i.e. a value ret
1a2cc 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61 74 urned by evaluat
1a2cd 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72 65 ing an SQL expre
1a2ce 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 ssion in the.**
1a2cf 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20 61 select list of a
1a2d0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1a2d1 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75 73 t) that may caus
1a2d2 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 e a malloc() fai
1a2d3 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c lure. If .** mal
1a2d4 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 loc() has failed
1a2d5 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d 61 , the threads ma
1a2d6 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
1a2d7 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20 74 is cleared and t
1a2d8 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 he result.** cod
1a2d9 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 70 e of statement p
1a2da 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c 49 Stmt set to SQLI
1a2db 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20 TE_NOMEM..**.**
1a2dc 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 68 Specifically, th
1a2dd 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f is is called fro
1a2de 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20 m within:.**.**
1a2df 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 sqlite3_colu
1a2e0 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20 20 mn_int().**
1a2e1 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 sqlite3_column_i
1a2e2 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73 71 nt64().** sq
1a2e3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
1a2e4 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 t().** sqlit
1a2e5 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
1a2e6 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
1a2e7 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a 3_column_real().
1a2e8 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1a2e9 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a olumn_bytes().**
1a2ea 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
1a2eb 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a 2a umn_bytes16().**
1a2ec 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f 72 20 .** But not for
1a2ed 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
1a2ee 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e 65 76 lob(), which nev
1a2ef 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28 er calls malloc(
1a2f0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
1a2f1 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 d columnMallocFa
1a2f2 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f 73 74 ilure(sqlite3_st
1a2f3 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 20 2f mt *pStmt).{. /
1a2f4 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 * If malloc() fa
1a2f5 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e 20 65 iled during an e
1a2f6 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 ncoding conversi
1a2f7 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 20 2a on within an. *
1a2f8 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
1a2f9 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20 73 _XXX API, then s
1a2fa 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f et the return co
1a2fb 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d de of the statem
1a2fc 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 4c 49 ent to. ** SQLI
1a2fd 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e 65 TE_NOMEM. The ne
1a2fe 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65 70 xt call to _step
1a2ff 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c 6c () (if any) will
1a300 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
1a301 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 5f 66 RROR. ** and _f
1a302 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 72 inalize() will r
1a303 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a eturn NOMEM.. *
1a304 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 /. Vdbe *p = (V
1a305 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 dbe *)pStmt;. i
1a306 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72 f( p ){. p->r
1a307 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
1a308 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 it(p->db, p->rc)
1a309 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1a30a 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
1a30b 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a >mutex);. }.}..
1a30c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
1a30d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 ************* sq
1a30e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a lite3_column_ *
1a30f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1a311 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1a312 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 routines are use
1a313 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c 65 6d d to access elem
1a314 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 ents of the curr
1a315 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 ent row.** in th
1a316 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f e result set..*/
1a317 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1a318 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
1a319 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 column_blob(sqli
1a31a 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1a31b 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 int i){. const
1a31c 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76 61 void *val;. va
1a31d 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
1a31e 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65 e_blob( columnMe
1a31f 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 m(pStmt,i) );.
1a320 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 /* Even though t
1a321 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f 64 here is no encod
1a322 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 20 ing conversion,
1a323 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69 67 value_blob() mig
1a324 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 ht. ** need to
1a325 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 6f call malloc() to
1a326 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 73 75 expand the resu
1a327 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f 62 lt of a zeroblob
1a328 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 () . ** express
1a329 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c ion. . */. col
1a32a 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1a32b 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1a32c 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1a32d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1a32e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c column_bytes(sql
1a32f 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1a330 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 , int i){. int
1a331 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
1a332 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c 75 6d lue_bytes( colum
1a333 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1a334 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1a335 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1a336 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1a337 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1a338 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
1a339 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d es16(sqlite3_stm
1a33a 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1a33b 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 {. int val = sq
1a33c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
1a33d 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 s16( columnMem(p
1a33e 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c Stmt,i) );. col
1a33f 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1a340 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1a341 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1a342 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 API double sqlit
1a343 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 e3_column_double
1a344 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1a345 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
1a346 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 71 6c double val = sql
1a347 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c ite3_value_doubl
1a348 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 e( columnMem(pSt
1a349 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1a34a 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1a34b 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1a34c 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
1a34d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
1a34e 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 lumn_int(sqlite3
1a34f 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1a350 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 t i){. int val
1a351 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1a352 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 int( columnMem(p
1a353 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c Stmt,i) );. col
1a354 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1a355 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1a356 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1a357 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 API sqlite_int64
1a358 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1a359 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 int64(sqlite3_st
1a35a 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1a35b 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 ){. sqlite_int6
1a35c 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 4 val = sqlite3_
1a35d 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 6f 6c value_int64( col
1a35e 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
1a35f 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
1a360 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
1a361 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
1a362 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1a363 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1a364 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1a365 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d text(sqlite3_stm
1a366 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1a367 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e {. const unsign
1a368 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d 20 73 ed char *val = s
1a369 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1a36a 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 t( columnMem(pSt
1a36b 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1a36c 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1a36d 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1a36e 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
1a36f 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 I sqlite3_value
1a370 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1a371 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74 value(sqlite3_st
1a372 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1a373 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d ){. Mem *pOut =
1a374 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1a375 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f 75 74 , i);. if( pOut
1a376 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 ->flags&MEM_Stat
1a377 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e ic ){. pOut->
1a378 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 flags &= ~MEM_St
1a379 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e atic;. pOut->
1a37a 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 flags |= MEM_Eph
1a37b 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d 6e em;. }. column
1a37c 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 MallocFailure(pS
1a37d 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 tmt);. return (
1a37e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 sqlite3_value *)
1a37f 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 pOut;.}.#ifndef
1a380 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1a381 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 6.SQLITE_API con
1a382 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1a383 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 73 _column_text16(s
1a384 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1a385 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f mt, int i){. co
1a386 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d 20 nst void *val =
1a387 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1a388 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 xt16( columnMem(
1a389 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f pStmt,i) );. co
1a38a 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
1a38b 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
1a38c 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69 66 rn val;.}.#endif
1a38d 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1a38e 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f UTF16 */.SQLITE_
1a38f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1a390 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c 69 column_type(sqli
1a391 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1a392 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 69 int i){. int i
1a393 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 Type = sqlite3_v
1a394 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75 6d alue_type( colum
1a395 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1a396 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1a397 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1a398 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b 0a 7d return iType;.}
1a399 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ../* The followi
1a39a 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65 ng function is e
1a39b 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 xperimental and
1a39c 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
1a39d 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 e or.** removal
1a39e 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65 33 */./*int sqlite3
1a39f 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f _column_numeric_
1a3a0 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d type(sqlite3_stm
1a3a1 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1a3a2 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71 6c {.** return sql
1a3a3 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 ite3_value_numer
1a3a4 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d ic_type( columnM
1a3a5 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 2a em(pStmt,i) );.*
1a3a6 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e *}.*/../*.** Con
1a3a7 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 65 6c vert the N-th el
1a3a8 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 2d 3e ement of pStmt->
1a3a9 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f 20 pColName[] into
1a3aa 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 0a 2a a string using.*
1a3ab 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e 20 72 * xFunc() then r
1a3ac 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 69 6e eturn that strin
1a3ad 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 74 20 g. If N is out
1a3ae 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e of range, return
1a3af 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 0..**.** There
1a3b0 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 6d 65 are up to 5 name
1a3b1 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d s for each colum
1a3b2 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 74 65 n. useType dete
1a3b3 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a 2a 20 rmines which.**
1a3b4 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e 65 64 name is returned
1a3b5 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 65 20 . Here are the
1a3b6 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 names:.**.**
1a3b7 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 75 6d 0 The colum
1a3b8 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 68 6f n name as it sho
1a3b9 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 65 64 uld be displayed
1a3ba 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 for output.**
1a3bb 20 20 31 20 20 20 20 20 20 54 68 65 20 64 61 74 1 The dat
1a3bc 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 20 74 atype name for t
1a3bd 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 he column.**
1a3be 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 2 The name
1a3bf 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1a3c0 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 that the column
1a3c1 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 derives from.**
1a3c2 20 20 20 33 20 20 20 20 20 20 54 68 65 20 6e 61 3 The na
1a3c3 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1a3c4 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 that the column
1a3c5 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 derives from.**
1a3c6 20 20 20 34 20 20 20 20 20 20 54 68 65 20 6e 61 4 The na
1a3c7 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1a3c8 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20 column that the
1a3c9 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 result column de
1a3ca 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a rives from.**.**
1a3cb 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 If the result i
1a3cc 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 63 s not a simple c
1a3cd 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 olumn reference
1a3ce 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78 70 (if it is an exp
1a3cf 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61 20 ression.** or a
1a3d0 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20 75 constant) then u
1a3d1 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61 6e seTypes 2, 3, an
1a3d2 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e d 4 return NULL.
1a3d3 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .*/.static const
1a3d4 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61 6d void *columnNam
1a3d5 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d e(. sqlite3_stm
1a3d6 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e 74 20 t *pStmt,. int
1a3d7 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 N,. const void
1a3d8 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a 29 2c *(*xFunc)(Mem*),
1a3d9 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 0a 29 . int useType.)
1a3da 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a {. const void *
1a3db 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 65 20 ret = 0;. Vdbe
1a3dc 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
1a3dd 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 0a mt;. int n;. .
1a3de 0a 20 20 69 66 28 20 70 21 3d 30 20 29 7b 0a 20 . if( p!=0 ){.
1a3df 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 n = sqlite3_c
1a3e0 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d olumn_count(pStm
1a3e1 74 29 3b 0a 20 20 20 20 69 66 28 20 4e 3c 6e 20 t);. if( N<n
1a3e2 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 && N>=0 ){.
1a3e3 20 4e 20 2b 3d 20 75 73 65 54 79 70 65 2a 6e 3b N += useType*n;
1a3e4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
1a3e5 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 utex_enter(p->db
1a3e6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
1a3e7 72 65 74 20 3d 20 78 46 75 6e 63 28 26 70 2d 3e ret = xFunc(&p->
1a3e8 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 0a 20 aColName[N]);..
1a3e9 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 /* A malloc
1a3ea 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 64 may have failed
1a3eb 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 78 inside of the x
1a3ec 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66 20 Func() call. If
1a3ed 74 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 73 this. ** is
1a3ee 20 74 68 65 20 63 61 73 65 2c 20 63 6c 65 61 72 the case, clear
1a3ef 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 the mallocFaile
1a3f0 64 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 d flag and retur
1a3f1 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a 2f n NULL.. */
1a3f2 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 64 62 . if( p->db
1a3f3 20 26 26 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f && p->db->mallo
1a3f4 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 cFailed ){.
1a3f5 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 p->db->malloc
1a3f6 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 Failed = 0;.
1a3f7 20 20 20 20 72 65 74 20 3d 20 30 3b 0a 20 20 20 ret = 0;.
1a3f8 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
1a3f9 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
1a3fa 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1a3fb 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1a3fc 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ret;.}../*.** R
1a3fd 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f eturn the name o
1a3fe 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
1a3ff 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
1a400 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 et returned by S
1a401 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 QL.** statement
1a402 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pStmt..*/.SQLITE
1a403 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
1a404 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1a405 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
1a406 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 t *pStmt, int N)
1a407 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d {. return colum
1a408 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 nName(. pSt
1a409 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f mt, N, (const vo
1a40a 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c id*(*)(Mem*))sql
1a40b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c ite3_value_text,
1a40c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a COLNAME_NAME);.
1a40d 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1a40e 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 _OMIT_UTF16.SQLI
1a40f 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1a410 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
1a411 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 n_name16(sqlite3
1a412 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1a413 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 t N){. return c
1a414 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 olumnName(.
1a415 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 pStmt, N, (cons
1a416 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 t void*(*)(Mem*)
1a417 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
1a418 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e ext16, COLNAME_N
1a419 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a AME);.}.#endif..
1a41a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74 /*.** Constraint
1a41b 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 45 : If you have E
1a41c 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 NABLE_COLUMN_MET
1a41d 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 6d ADATA then you m
1a41e 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e ust.** not defin
1a41f 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e e OMIT_DECLTYPE.
1a420 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
1a421 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
1a422 54 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 64 TYPE) && defined
1a423 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 (SQLITE_ENABLE_C
1a424 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a OLUMN_METADATA).
1a425 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f # error "Must no
1a426 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51 t define both SQ
1a427 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 LITE_OMIT_DECLTY
1a428 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 6e PE \. an
1a429 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d SQLITE_ENABLE_
1a42a 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 COLUMN_METADATA"
1a42b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
1a42c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 SQLITE_OMIT_DEC
1a42d 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75 LTYPE./*.** Retu
1a42e 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 rn the column de
1a42f 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 28 claration type (
1a430 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 6f if applicable) o
1a431 66 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c 75 f the 'i'th colu
1a432 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 mn.** of the res
1a433 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 73 ult set of SQL s
1a434 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a tatement pStmt..
1a435 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1a436 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1a437 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 3_column_decltyp
1a438 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1a439 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1a43a 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1a43b 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1a43c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1a43d 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1a43e 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 3_value_text, CO
1a43f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b LNAME_DECLTYPE);
1a440 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1a441 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1a442 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1a443 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
1a444 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71 mn_decltype16(sq
1a445 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1a446 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1a447 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1a448 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1a449 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1a44a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1a44b 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e lue_text16, COLN
1a44c 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d AME_DECLTYPE);.}
1a44d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1a44e 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
1a44f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1a450 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a _OMIT_DECLTYPE *
1a451 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
1a452 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d _ENABLE_COLUMN_M
1a453 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 ETADATA./*.** Re
1a454 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
1a455 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 the database fr
1a456 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c om which a resul
1a457 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 t column derives
1a458 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 ..** NULL is ret
1a459 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 urned if the res
1a45a 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e ult column is an
1a45b 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 expression or c
1a45c 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e onstant or.** an
1a45d 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 ything else whic
1a45e 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 h is not an unab
1a45f 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 iguous reference
1a460 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 to a database c
1a461 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 olumn..*/.SQLITE
1a462 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
1a463 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1a464 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 database_name(sq
1a465 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1a466 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1a467 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1a468 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1a469 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1a46a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1a46b 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d lue_text, COLNAM
1a46c 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 E_DATABASE);.}.#
1a46d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1a46e 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1a46f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1a470 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 sqlite3_column_d
1a471 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 atabase_name16(s
1a472 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1a473 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1a474 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1a475 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1a476 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1a477 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1a478 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c alue_text16, COL
1a479 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a NAME_DATABASE);.
1a47a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1a47b 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
1a47c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1a47d 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
1a47e 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 able from which
1a47f 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 a result column
1a480 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c derives..** NULL
1a481 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
1a482 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d the result colum
1a483 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 n is an expressi
1a484 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f on or constant o
1a485 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c r.** anything el
1a486 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 se which is not
1a487 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 an unabiguous re
1a488 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 ference to a dat
1a489 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f abase column..*/
1a48a 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1a48b 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
1a48c 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d column_table_nam
1a48d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1a48e 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1a48f 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1a490 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1a491 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1a492 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1a493 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 3_value_text, CO
1a494 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a LNAME_TABLE);.}.
1a495 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a496 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
1a497 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
1a498 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1a499 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c table_name16(sql
1a49a 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1a49b 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1a49c 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1a49d 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1a49e 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1a49f 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1a4a0 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 ue_text16, COLNA
1a4a1 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e ME_TABLE);.}.#en
1a4a2 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1a4a3 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a IT_UTF16 */../*.
1a4a4 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 ** Return the na
1a4a5 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1a4a6 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 column from whic
1a4a7 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d h a result colum
1a4a8 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 n derives..** NU
1a4a9 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 LL is returned i
1a4aa 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c f the result col
1a4ab 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 umn is an expres
1a4ac 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 sion or constant
1a4ad 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 or.** anything
1a4ae 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f else which is no
1a4af 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 t an unabiguous
1a4b0 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 reference to a d
1a4b1 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a atabase column..
1a4b2 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1a4b3 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1a4b4 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 3_column_origin_
1a4b5 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
1a4b6 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 t *pStmt, int N)
1a4b7 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d {. return colum
1a4b8 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 nName(. pSt
1a4b9 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f mt, N, (const vo
1a4ba 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c id*(*)(Mem*))sql
1a4bb 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c ite3_value_text,
1a4bc 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 COLNAME_COLUMN)
1a4bd 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1a4be 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1a4bf 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1a4c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
1a4c1 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 umn_origin_name1
1a4c2 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 6(sqlite3_stmt *
1a4c3 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1a4c4 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1a4c5 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1a4c6 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1a4c7 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1a4c8 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 3_value_text16,
1a4c9 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b COLNAME_COLUMN);
1a4ca 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1a4cb 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
1a4cc 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 /.#endif /* SQLI
1a4cd 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
1a4ce 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f _METADATA */.../
1a4cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
1a4d1 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a sqlite3_bind_ *
1a4d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a4d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a **********.** .*
1a4d4 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 * Routines used
1a4d5 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 73 to attach values
1a4d6 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 6e to wildcards in
1a4d7 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 a compiled SQL
1a4d8 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a statement..*/./*
1a4d9 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 76 .** Unbind the v
1a4da 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 alue bound to va
1a4db 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 74 riable i in virt
1a4dc 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 ual machine p. T
1a4dd 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 his is the .** t
1a4de 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 69 he same as bindi
1a4df 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 ng a NULL value
1a4e0 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 to the column. I
1a4e1 66 20 74 68 65 20 22 69 22 20 70 61 72 61 6d 65 f the "i" parame
1a4e2 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 ter is.** out of
1a4e3 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c range, then SQL
1a4e4 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 74 ITE_RANGE is ret
1a4e5 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 20 urned. Othewise
1a4e6 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a SQLITE_OK..**.**
1a4e7 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 A successful ev
1a4e8 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 aluation of this
1a4e9 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 routine acquire
1a4ea 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 s the mutex on p
1a4eb 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69 ..** the mutex i
1a4ec 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e s released if an
1a4ed 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 y kind of error
1a4ee 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 occurs..**.** Th
1a4ef 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f e error code sto
1a4f0 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 red in database
1a4f1 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69 p->db is overwri
1a4f2 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65 tten with the re
1a4f3 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e turn.** value in
1a4f4 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 any case..*/.st
1a4f5 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62 atic int vdbeUnb
1a4f6 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ind(Vdbe *p, int
1a4f7 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 i){. Mem *pVar
1a4f8 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
1a4f9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1a4fa 55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d USE;. sqlite3_m
1a4fb 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 utex_enter(p->db
1a4fc 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 ->mutex);. if(
1a4fd 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d p->magic!=VDBE_M
1a4fe 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70 AGIC_RUN || p->p
1a4ff 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 c>=0 ){. sqli
1a500 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 te3Error(p->db,
1a501 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 SQLITE_MISUSE, 0
1a502 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
1a503 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 utex_leave(p->db
1a504 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 ->mutex);. re
1a505 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
1a506 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c SE;. }. if( i<
1a507 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 1 || i>p->nVar )
1a508 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
1a509 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 or(p->db, SQLITE
1a50a 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 _RANGE, 0);.
1a50b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1a50c 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
1a50d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
1a50e 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a LITE_RANGE;. }.
1a50f 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20 i--;. pVar =
1a510 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73 &p->aVar[i];. s
1a511 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1a512 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56 ease(pVar);. pV
1a513 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ar->flags = MEM_
1a514 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45 Null;. sqlite3E
1a515 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 rror(p->db, SQLI
1a516 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 TE_OK, 0);. ret
1a517 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1a518 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74 ../*.** Bind a t
1a519 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 ext or BLOB valu
1a51a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1a51b 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 6c bindText(. sql
1a51c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1a51d 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65 , /* The state
1a51e 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 61 ment to bind aga
1a51f 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c inst */. int i,
1a520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a521 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 /* Index of the
1a522 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69 parameter to bi
1a523 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f nd */. const vo
1a524 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f id *zData, /
1a525 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
1a526 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e data to be boun
1a527 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 d */. int nData
1a528 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
1a529 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1a52a 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62 of data to be b
1a52b 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 ound */. void (
1a52c 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20 *xDel)(void*),
1a52d 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 /* Destructor f
1a52e 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 or the data */.
1a52f 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 20 20 u8 encoding
1a530 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 /* Encod
1a531 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 ing for the data
1a532 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70 */.){. Vdbe *p
1a533 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 = (Vdbe *)pStmt
1a534 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 ;. Mem *pVar;.
1a535 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d int rc;.. rc =
1a536 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 vdbeUnbind(p, i
1a537 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1a538 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
1a539 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 ( zData!=0 ){.
1a53a 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 pVar = &p->a
1a53b 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 Var[i-1];.
1a53c 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1a53d 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c 20 MemSetStr(pVar,
1a53e 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e zData, nData, en
1a53f 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20 coding, xDel);.
1a540 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1a541 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69 ITE_OK && encodi
1a542 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ng!=0 ){.
1a543 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1a544 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1a545 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 pVar, ENC(p->db)
1a546 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1a547 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d sqlite3Error(p-
1a548 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 >db, rc, 0);.
1a549 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 rc = sqlite3A
1a54a 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72 63 piExit(p->db, rc
1a54b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
1a54c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1a54d 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1a54e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1a54f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 .}.../*.** Bind
1a550 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f 20 a blob value to
1a551 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 an SQL statement
1a552 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 51 variable..*/.SQ
1a553 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1a554 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 0a ite3_bind_blob(.
1a555 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1a556 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c pStmt, . int i,
1a557 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
1a558 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 zData, . int nD
1a559 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 ata, . void (*x
1a55a 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 Del)(void*).){.
1a55b 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 return bindText
1a55c 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 (pStmt, i, zData
1a55d 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 30 , nData, xDel, 0
1a55e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1a55f 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1a560 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f _double(sqlite3_
1a561 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1a562 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 i, double rValu
1a563 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 e){. int rc;.
1a564 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 Vdbe *p = (Vdbe
1a565 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 *)pStmt;. rc =
1a566 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 vdbeUnbind(p, i)
1a567 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1a568 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c TE_OK ){. sql
1a569 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f ite3VdbeMemSetDo
1a56a 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69 2d uble(&p->aVar[i-
1a56b 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20 20 1], rValue);.
1a56c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1a56d 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
1a56e 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e x);. }. return
1a56f 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 rc;.}.SQLITE_AP
1a570 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1a571 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 nd_int(sqlite3_s
1a572 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20 69 tmt *p, int i, i
1a573 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72 65 nt iValue){. re
1a574 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e turn sqlite3_bin
1a575 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28 69 d_int64(p, i, (i
1a576 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 64)iValue);.}.SQ
1a577 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1a578 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 ite3_bind_int64(
1a579 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1a57a 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69 tmt, int i, sqli
1a57b 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 29 te_int64 iValue)
1a57c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 {. int rc;. Vd
1a57d 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 be *p = (Vdbe *)
1a57e 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 pStmt;. rc = vd
1a57f 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a beUnbind(p, i);.
1a580 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1a581 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
1a582 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1a583 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 4(&p->aVar[i-1],
1a584 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 iValue);. sq
1a585 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1a586 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1a587 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1a588 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
1a589 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1a58a 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d null(sqlite3_stm
1a58b 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1a58c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 {. int rc;. Vd
1a58d 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 be *p = (Vdbe*)p
1a58e 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 Stmt;. rc = vdb
1a58f 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 eUnbind(p, i);.
1a590 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1a591 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
1a592 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
1a593 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
1a594 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1a595 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1a596 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
1a597 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d ( . sqlite3_stm
1a598 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 t *pStmt, . int
1a599 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 i, . const cha
1a59a 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 r *zData, . int
1a59b 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 nData, . void
1a59c 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 (*xDel)(void*).)
1a59d 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 {. return bindT
1a59e 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 ext(pStmt, i, zD
1a59f 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c ata, nData, xDel
1a5a0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a , SQLITE_UTF8);.
1a5a1 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1a5a2 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 _OMIT_UTF16.SQLI
1a5a3 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1a5a4 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 0a e3_bind_text16(.
1a5a5 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1a5a6 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c pStmt, . int i,
1a5a7 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
1a5a8 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 zData, . int nD
1a5a9 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 ata, . void (*x
1a5aa 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 Del)(void*).){.
1a5ab 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 return bindText
1a5ac 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 (pStmt, i, zData
1a5ad 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 , nData, xDel, S
1a5ae 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 QLITE_UTF16NATIV
1a5af 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 E);.}.#endif /*
1a5b0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1a5b1 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 6 */.SQLITE_API
1a5b2 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1a5b3 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 _value(sqlite3_s
1a5b4 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1a5b5 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 i, const sqlite3
1a5b6 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b _value *pValue){
1a5b7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 . int rc;. Vdb
1a5b8 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 e *p = (Vdbe *)p
1a5b9 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 Stmt;. rc = vdb
1a5ba 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 eUnbind(p, i);.
1a5bb 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1a5bc 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 OK ){. rc = s
1a5bd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 qlite3VdbeMemCop
1a5be 79 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c y(&p->aVar[i-1],
1a5bf 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 pValue);. if
1a5c0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1a5c1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1a5c2 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
1a5c3 6e 63 6f 64 69 6e 67 28 26 70 2d 3e 61 56 61 72 ncoding(&p->aVar
1a5c4 5b 69 2d 31 5d 2c 20 45 4e 43 28 70 2d 3e 64 62 [i-1], ENC(p->db
1a5c5 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 ));. }. sq
1a5c6 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1a5c7 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1a5c8 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 . }. rc = sqli
1a5c9 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 te3ApiExit(p->db
1a5ca 2c 20 72 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 , rc);. return
1a5cb 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 rc;.}.SQLITE_API
1a5cc 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1a5cd 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 d_zeroblob(sqlit
1a5ce 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1a5cf 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b 0a 20 int i, int n){.
1a5d0 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 int rc;. Vdbe
1a5d1 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
1a5d2 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 mt;. rc = vdbeU
1a5d3 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 nbind(p, i);. i
1a5d4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1a5d5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1a5d6 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f dbeMemSetZeroBlo
1a5d7 62 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c b(&p->aVar[i-1],
1a5d8 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 n);. sqlite3
1a5d9 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
1a5da 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a db->mutex);. }.
1a5db 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1a5dc 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1a5dd 20 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64 63 number of wildc
1a5de 61 72 64 73 20 74 68 61 74 20 63 61 6e 20 62 65 ards that can be
1a5df 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f 75 potentially bou
1a5e0 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20 72 nd to..** This r
1a5e1 6f 75 74 69 6e 65 20 69 73 20 61 64 64 65 64 20 outine is added
1a5e2 74 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a 3a to support DBD::
1a5e3 53 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 53 51 4c SQLite. .*/.SQL
1a5e4 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1a5e5 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
1a5e6 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 er_count(sqlite3
1a5e7 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 _stmt *pStmt){.
1a5e8 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
1a5e9 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 *)pStmt;. retur
1a5ea 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 72 20 3a 20 n p ? p->nVar :
1a5eb 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 0;.}../*.** Crea
1a5ec 74 65 20 61 20 6d 61 70 70 69 6e 67 20 66 72 6f te a mapping fro
1a5ed 6d 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 m variable numbe
1a5ee 72 73 20 74 6f 20 76 61 72 69 61 62 6c 65 20 6e rs to variable n
1a5ef 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 ames.** in the V
1a5f0 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 61 dbe.azVar[] arra
1a5f1 79 2c 20 69 66 20 73 75 63 68 20 61 20 6d 61 70 y, if such a map
1a5f2 70 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61 6c ping does not al
1a5f3 72 65 61 64 79 0a 2a 2a 20 65 78 69 73 74 2e 0a ready.** exist..
1a5f4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
1a5f5 72 65 61 74 65 56 61 72 4d 61 70 28 56 64 62 65 reateVarMap(Vdbe
1a5f6 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e *p){. if( !p->
1a5f7 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c okVar ){. sql
1a5f8 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1a5f9 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1a5fa 20 20 20 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 if( !p->okVa
1a5fb 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a r ){. int j
1a5fc 3b 0a 20 20 20 20 20 20 4f 70 20 2a 70 4f 70 3b ;. Op *pOp;
1a5fd 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 . for(j=0,
1a5fe 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70 2d pOp=p->aOp; j<p-
1a5ff 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b 2b >nOp; j++, pOp++
1a600 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
1a601 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 Op->opcode==OP_V
1a602 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 ariable ){.
1a603 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1a604 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p1>0 && pOp->p
1a605 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 1<=p->nVar );.
1a606 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 72 p->azVar
1a607 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f [pOp->p1-1] = pO
1a608 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 20 p->p4.z;.
1a609 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1a60a 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31 3b 0a 20 p->okVar = 1;.
1a60b 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1a60c 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
1a60d 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a db->mutex);. }.
1a60e 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1a60f 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77 69 the name of a wi
1a610 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 ldcard parameter
1a611 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 . Return NULL i
1a612 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 69 f the index.** i
1a613 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f s out of range o
1a614 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 61 72 r if the wildcar
1a615 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a 2a d is unnamed..**
1a616 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 .** The result i
1a617 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e 0a s always UTF-8..
1a618 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1a619 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1a61a 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
1a61b 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
1a61c 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1a61d 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 ){. Vdbe *p = (
1a61e 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 Vdbe*)pStmt;. i
1a61f 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c f( p==0 || i<1 |
1a620 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 | i>p->nVar ){.
1a621 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1a622 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 . createVarMap(
1a623 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p);. return p->
1a624 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f azVar[i-1];.}../
1a625 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69 6c *.** Given a wil
1a626 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 dcard parameter
1a627 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68 65 name, return the
1a628 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 61 index of the va
1a629 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20 74 riable.** with t
1a62a 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74 68 hat name. If th
1a62b 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61 62 ere is no variab
1a62c 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76 65 le with the give
1a62d 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 72 n name,.** retur
1a62e 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 n 0..*/.SQLITE_A
1a62f 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1a630 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e ind_parameter_in
1a631 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 dex(sqlite3_stmt
1a632 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 *pStmt, const c
1a633 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 har *zName){. V
1a634 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 dbe *p = (Vdbe*)
1a635 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a pStmt;. int i;.
1a636 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 if( p==0 ){.
1a637 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1a638 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 70 createVarMap(p
1a639 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 ); . if( zName
1a63a 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ){. for(i=0;
1a63b 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b i<p->nVar; i++){
1a63c 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
1a63d 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 5b r *z = p->azVar[
1a63e 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 i];. if( z
1a63f 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 6d && strcmp(z,zNam
1a640 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 e)==0 ){.
1a641 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 return i+1;.
1a642 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1a643 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
1a644 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c .** Transfer all
1a645 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74 bindings from t
1a646 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 he first stateme
1a647 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 73 nt over to the s
1a648 65 63 6f 6e 64 2e 0a 2a 2a 20 49 66 20 74 68 65 econd..** If the
1a649 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73 20 two statements
1a64a 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65 72 contain a differ
1a64b 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 ent number of bi
1a64c 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a 20 ndings, then.**
1a64d 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 an SQLITE_ERROR
1a64e 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
1a64f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1a650 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 nt sqlite3Transf
1a651 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 erBindings(sqlit
1a652 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 e3_stmt *pFromSt
1a653 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 mt, sqlite3_stmt
1a654 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 *pToStmt){. Vd
1a655 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 be *pFrom = (Vdb
1a656 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 e*)pFromStmt;.
1a657 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 Vdbe *pTo = (Vdb
1a658 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 6e e*)pToStmt;. in
1a659 74 20 69 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 t i, rc = SQLITE
1a65a 5f 4f 4b 3b 0a 20 20 69 66 28 20 28 70 46 72 6f _OK;. if( (pFro
1a65b 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d m->magic!=VDBE_M
1a65c 41 47 49 43 5f 52 55 4e 20 26 26 20 70 46 72 6f AGIC_RUN && pFro
1a65d 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d m->magic!=VDBE_M
1a65e 41 47 49 43 5f 48 41 4c 54 29 0a 20 20 20 20 7c AGIC_HALT). |
1a65f 7c 20 28 70 54 6f 2d 3e 6d 61 67 69 63 21 3d 56 | (pTo->magic!=V
1a660 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 DBE_MAGIC_RUN &&
1a661 20 70 54 6f 2d 3e 6d 61 67 69 63 21 3d 56 44 42 pTo->magic!=VDB
1a662 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20 20 E_MAGIC_HALT).
1a663 20 20 7c 7c 20 70 54 6f 2d 3e 64 62 21 3d 70 46 || pTo->db!=pF
1a664 72 6f 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 72 rom->db ){. r
1a665 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1a666 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 USE;. }. if( p
1a667 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d From->nVar!=pTo-
1a668 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65 74 >nVar ){. ret
1a669 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
1a66a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
1a66b 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54 6f 2d mutex_enter(pTo-
1a66c 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66 >db->mutex);. f
1a66d 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 or(i=0; rc==SQLI
1a66e 54 45 5f 4f 4b 20 26 26 20 69 3c 70 46 72 6f 6d TE_OK && i<pFrom
1a66f 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 ->nVar; i++){.
1a670 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1a671 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 72 5b Move(&pTo->aVar[
1a672 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 61 72 i], &pFrom->aVar
1a673 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 [i]);. }. sqli
1a674 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1a675 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b pTo->db->mutex);
1a676 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
1a677 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d QLITE_OK || rc==
1a678 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a SQLITE_NOMEM );.
1a679 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1a67a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a67b 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f MIT_DEPRECATED./
1a67c 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 20 *.** Deprecated
1a67d 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 external interfa
1a67e 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f ce. Internal/co
1a67f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a 2a re SQLite code.*
1a680 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 * should call sq
1a681 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e lite3TransferBin
1a682 64 69 6e 67 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 dings..*/.SQLITE
1a683 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1a684 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e _transfer_bindin
1a685 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 gs(sqlite3_stmt
1a686 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 *pFromStmt, sqli
1a687 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d te3_stmt *pToStm
1a688 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c t){. return sql
1a689 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 ite3TransferBind
1a68a 69 6e 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 ings(pFromStmt,
1a68b 70 54 6f 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 pToStmt);.}.#end
1a68c 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
1a68d 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61 the sqlite3* da
1a68e 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 6f tabase handle to
1a68f 20 77 68 69 63 68 20 74 68 65 20 70 72 65 70 61 which the prepa
1a690 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67 69 red statement gi
1a691 76 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72 ven.** in the ar
1a692 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 gument belongs.
1a693 20 54 68 69 73 20 69 73 20 74 68 65 20 73 61 6d This is the sam
1a694 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1a695 65 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 74 68 e that was.** th
1a696 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
1a697 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f to the sqlite3_
1a698 70 72 65 70 61 72 65 28 29 20 74 68 61 74 20 77 prepare() that w
1a699 61 73 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 as used to creat
1a69a 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 e.** the stateme
1a69b 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 nt in the first
1a69c 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 place..*/.SQLITE
1a69d 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 _API sqlite3 *sq
1a69e 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 lite3_db_handle(
1a69f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1a6a0 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 tmt){. return p
1a6a1 53 74 6d 74 20 3f 20 28 28 56 64 62 65 2a 29 70 Stmt ? ((Vdbe*)p
1a6a2 53 74 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d Stmt)->db : 0;.}
1a6a3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
1a6a4 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1a6a5 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 next prepared st
1a6a6 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 70 53 atement after pS
1a6a7 74 6d 74 20 61 73 73 6f 63 69 61 74 65 64 0a 2a tmt associated.*
1a6a8 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20 * with database
1a6a9 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62 2e 20 connection pDb.
1a6aa 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c If pStmt is NUL
1a6ab 4c 2c 20 72 65 74 75 72 6e 20 74 68 65 20 66 69 L, return the fi
1a6ac 72 73 74 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 rst.** prepared
1a6ad 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 statement for th
1a6ae 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1a6af 63 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 4e ction. Return N
1a6b0 55 4c 4c 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 ULL if there.**
1a6b1 61 72 65 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a are no more..*/.
1a6b2 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
1a6b3 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 e3_stmt *sqlite3
1a6b4 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 _next_stmt(sqlit
1a6b5 65 33 20 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 e3 *pDb, sqlite3
1a6b6 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 _stmt *pStmt){.
1a6b7 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
1a6b8 4e 65 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f Next;. sqlite3_
1a6b9 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 62 2d mutex_enter(pDb-
1a6ba 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 >mutex);. if( p
1a6bb 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 Stmt==0 ){. p
1a6bc 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33 5f Next = (sqlite3_
1a6bd 73 74 6d 74 2a 29 70 44 62 2d 3e 70 56 64 62 65 stmt*)pDb->pVdbe
1a6be 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1a6bf 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33 5f Next = (sqlite3_
1a6c0 73 74 6d 74 2a 29 28 28 56 64 62 65 2a 29 70 53 stmt*)((Vdbe*)pS
1a6c1 74 6d 74 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d tmt)->pNext;. }
1a6c2 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
1a6c3 5f 6c 65 61 76 65 28 70 44 62 2d 3e 6d 75 74 65 _leave(pDb->mute
1a6c4 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 x);. return pNe
1a6c5 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 xt;.}../*.** Ret
1a6c6 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 urn the value of
1a6c7 20 61 20 73 74 61 74 75 73 20 63 6f 75 6e 74 65 a status counte
1a6c8 72 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 64 r for a prepared
1a6c9 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 statement.*/.SQ
1a6ca 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1a6cb 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 ite3_stmt_status
1a6cc 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1a6cd 53 74 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e Stmt, int op, in
1a6ce 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 t resetFlag){.
1a6cf 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 28 56 Vdbe *pVdbe = (V
1a6d0 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e dbe*)pStmt;. in
1a6d1 74 20 76 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f t v = pVdbe->aCo
1a6d2 75 6e 74 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 unter[op-1];. i
1a6d3 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 20 70 f( resetFlag ) p
1a6d4 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f Vdbe->aCounter[o
1a6d5 70 2d 31 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 p-1] = 0;. retu
1a6d6 72 6e 20 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a rn v;.}../******
1a6d7 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1a6d8 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a vdbeapi.c ******
1a6d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a6da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a6db 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1a6dc 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1a6dd 69 6c 65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a ile vdbe.c *****
1a6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a6df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a6e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1a6e1 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
1a6e2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1a6e3 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1a6e4 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1a6e5 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1a6e6 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1a6e7 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1a6e8 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1a6e9 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1a6ea 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1a6eb 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1a6ec 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1a6ed 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1a6ee 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1a6ef 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1a6f0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1a6f1 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1a6f2 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1a6f3 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1a6f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a6f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a6f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a6f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1a6f8 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 ** The code in t
1a6f9 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 his file impleme
1a6fa 6e 74 73 20 65 78 65 63 75 74 69 6f 6e 20 6d 65 nts execution me
1a6fb 74 68 6f 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20 thod of the .**
1a6fc 56 69 72 74 75 61 6c 20 44 61 74 61 62 61 73 65 Virtual Database
1a6fd 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 2e 20 Engine (VDBE).
1a6fe 20 41 20 73 65 70 61 72 61 74 65 20 66 69 6c 65 A separate file
1a6ff 20 28 22 76 64 62 65 61 75 78 2e 63 22 29 0a 2a ("vdbeaux.c").*
1a700 2a 20 68 61 6e 64 6c 65 73 20 68 6f 75 73 65 6b * handles housek
1a701 65 65 70 69 6e 67 20 64 65 74 61 69 6c 73 20 73 eeping details s
1a702 75 63 68 20 61 73 20 63 72 65 61 74 69 6e 67 20 uch as creating
1a703 61 6e 64 20 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 and deleting.**
1a704 56 44 42 45 20 69 6e 73 74 61 6e 63 65 73 2e 20 VDBE instances.
1a705 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 73 6f This file is so
1a706 6c 65 6c 79 20 69 6e 74 65 72 65 73 74 65 64 20 lely interested
1a707 69 6e 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 in executing.**
1a708 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d the VDBE program
1a709 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 ..**.** In the e
1a70a 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 xternal interfac
1a70b 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 e, an "sqlite3_s
1a70c 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70 61 71 tmt*" is an opaq
1a70d 75 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f ue pointer.** to
1a70e 20 61 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 a VDBE..**.** T
1a70f 68 65 20 53 51 4c 20 70 61 72 73 65 72 20 67 65 he SQL parser ge
1a710 6e 65 72 61 74 65 73 20 61 20 70 72 6f 67 72 61 nerates a progra
1a711 6d 20 77 68 69 63 68 20 69 73 20 74 68 65 6e 20 m which is then
1a712 65 78 65 63 75 74 65 64 20 62 79 0a 2a 2a 20 74 executed by.** t
1a713 68 65 20 56 44 42 45 20 74 6f 20 64 6f 20 74 68 he VDBE to do th
1a714 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 20 53 51 e work of the SQ
1a715 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 56 44 L statement. VD
1a716 42 45 20 70 72 6f 67 72 61 6d 73 20 61 72 65 20 BE programs are
1a717 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e 20 66 .** similar in f
1a718 6f 72 6d 20 74 6f 20 61 73 73 65 6d 62 6c 79 20 orm to assembly
1a719 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 20 70 language. The p
1a71a 72 6f 67 72 61 6d 20 63 6f 6e 73 69 73 74 73 20 rogram consists
1a71b 6f 66 0a 2a 2a 20 61 20 6c 69 6e 65 61 72 20 73 of.** a linear s
1a71c 65 71 75 65 6e 63 65 20 6f 66 20 6f 70 65 72 61 equence of opera
1a71d 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 6f 70 65 tions. Each ope
1a71e 72 61 74 69 6f 6e 20 68 61 73 20 61 6e 20 6f 70 ration has an op
1a71f 63 6f 64 65 20 0a 2a 2a 20 61 6e 64 20 35 20 6f code .** and 5 o
1a720 70 65 72 61 6e 64 73 2e 20 20 4f 70 65 72 61 6e perands. Operan
1a721 64 73 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 ds P1, P2, and P
1a722 33 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e 20 3 are integers.
1a723 20 4f 70 65 72 61 6e 64 20 50 34 20 0a 2a 2a 20 Operand P4 .**
1a724 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e is a null-termin
1a725 61 74 65 64 20 73 74 72 69 6e 67 2e 20 20 4f 70 ated string. Op
1a726 65 72 61 6e 64 20 50 35 20 69 73 20 61 6e 20 75 erand P5 is an u
1a727 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 nsigned characte
1a728 72 2e 0a 2a 2a 20 46 65 77 20 6f 70 63 6f 64 65 r..** Few opcode
1a729 73 20 75 73 65 20 61 6c 6c 20 35 20 6f 70 65 72 s use all 5 oper
1a72a 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 ands..**.** Comp
1a72b 75 74 61 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 utation results
1a72c 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 are stored on a
1a72d 73 65 74 20 6f 66 20 72 65 67 69 73 74 65 72 73 set of registers
1a72e 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e numbered beginn
1a72f 69 6e 67 0a 2a 2a 20 77 69 74 68 20 31 20 61 6e ing.** with 1 an
1a730 64 20 67 6f 69 6e 67 20 75 70 20 74 6f 20 56 64 d going up to Vd
1a731 62 65 2e 6e 4d 65 6d 2e 20 20 45 61 63 68 20 72 be.nMem. Each r
1a732 65 67 69 73 74 65 72 20 63 61 6e 20 73 74 6f 72 egister can stor
1a733 65 0a 2a 2a 20 65 69 74 68 65 72 20 61 6e 20 69 e.** either an i
1a734 6e 74 65 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 nteger, a null-t
1a735 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 erminated string
1a736 2c 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 , a floating poi
1a737 6e 74 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 nt.** number, or
1a738 20 74 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 the SQL "NULL"
1a739 76 61 6c 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69 value. An impli
1a73a 63 69 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 cit conversion f
1a73b 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 rom one.** type
1a73c 74 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63 63 to the other occ
1a73d 75 72 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 urs as necessary
1a73e 2e 0a 2a 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 ..** .** Most of
1a73f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 the code in thi
1a740 73 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 s file is taken
1a741 75 70 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 up by the sqlite
1a742 33 56 64 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 3VdbeExec().** f
1a743 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 64 6f unction which do
1a744 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 es the work of i
1a745 6e 74 65 72 70 72 65 74 69 6e 67 20 61 20 56 44 nterpreting a VD
1a746 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 BE program..** B
1a747 75 74 20 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 ut other routine
1a748 73 20 61 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 s are also provi
1a749 64 65 64 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 ded to help in b
1a74a 75 69 6c 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 uilding up.** a
1a74b 70 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 program instruct
1a74c 69 6f 6e 20 62 79 20 69 6e 73 74 72 75 63 74 69 ion by instructi
1a74d 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 on..**.** Variou
1a74e 73 20 73 63 72 69 70 74 73 20 73 63 61 6e 20 74 s scripts scan t
1a74f 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 his source file
1a750 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 in order to gene
1a751 72 61 74 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 rate HTML.** doc
1a752 75 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 umentation, head
1a753 65 72 73 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 ers files, or ot
1a754 68 65 72 20 64 65 72 69 76 65 64 20 66 69 6c 65 her derived file
1a755 73 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 74 69 s. The formatti
1a756 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 ng.** of the cod
1a757 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 e in this file i
1a758 73 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 69 6d s, therefore, im
1a759 70 6f 72 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 portant. See ot
1a75a 68 65 72 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 her comments.**
1a75b 69 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 in this file for
1a75c 20 64 65 74 61 69 6c 73 2e 20 20 49 66 20 69 6e details. If in
1a75d 20 64 6f 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 doubt, do not d
1a75e 65 76 69 61 74 65 20 66 72 6f 6d 20 65 78 69 73 eviate from exis
1a75f 74 69 6e 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 ting.** commenti
1a760 6e 67 20 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 ng and indentati
1a761 6f 6e 20 70 72 61 63 74 69 63 65 73 20 77 68 65 on practices whe
1a762 6e 20 63 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 n changing or ad
1a763 64 69 6e 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a ding code..**.**
1a764 20 24 49 64 3a 20 76 64 62 65 2e 63 2c 76 20 31 $Id: vdbe.c,v 1
1a765 2e 38 32 38 20 32 30 30 39 2f 30 33 2f 32 33 20 .828 2009/03/23
1a766 31 37 3a 31 31 3a 32 37 20 64 61 6e 69 65 6c 6b 17:11:27 danielk
1a767 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 1977 Exp $.*/../
1a768 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1a769 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 ng global variab
1a76a 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 le is incremente
1a76b 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20 63 d every time a c
1a76c 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 ursor.** moves,
1a76d 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f 50 either by the OP
1a76e 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 _SeekXX, OP_Next
1a76f 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 , or OP_Prev opc
1a770 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74 0a odes. The test.
1a771 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75 73 ** procedures us
1a772 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 e this informati
1a773 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 on to make sure
1a774 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 65 that indices are
1a775 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 .** working corr
1a776 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61 72 ectly. This var
1a777 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e iable has no fun
1a778 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e ction other than
1a779 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 to.** help veri
1a77a 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f fy the correct o
1a77b 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 peration of the
1a77c 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 library..*/.#ifd
1a77d 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
1a77e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1a77f 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 lite3_search_cou
1a780 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
1a781 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 /*.** When this
1a782 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1a783 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74 20 is positive, it
1a784 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65 64 gets decremented
1a785 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 once before.**
1a786 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e each instruction
1a787 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 57 in the VDBE. W
1a788 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 72 6f hen reaches zero
1a789 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 65 72 , the u1.isInter
1a78a 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 rupted.** field
1a78b 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 20 73 of the sqlite3 s
1a78c 74 72 75 63 74 75 72 65 20 69 73 20 73 65 74 20 tructure is set
1a78d 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 in order to simu
1a78e 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 72 75 late and interru
1a78f 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 pt..**.** This f
1a790 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 64 20 acility is used
1a791 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 for testing purp
1a792 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 oses only. It d
1a793 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e oes not function
1a794 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 .** in an ordina
1a795 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 ry build..*/.#if
1a796 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
1a797 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1a798 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1a799 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 _count = 0;.#end
1a79a 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 if../*.** The ne
1a79b 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 xt global variab
1a79c 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 le is incremente
1a79d 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65 20 d each type the
1a79e 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a OP_Sort opcode.*
1a79f 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 20 * is executed.
1a7a0 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 The test procedu
1a7a1 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 res use this inf
1a7a2 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 ormation to make
1a7a3 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f sure that.** so
1a7a4 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72 69 rting is occurri
1a7a5 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 ng or not occurr
1a7a6 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69 61 ing at appropria
1a7a7 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69 73 te times. This
1a7a8 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 variable.** has
1a7a9 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 no function oth
1a7aa 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 er than to help
1a7ab 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 verify the corre
1a7ac 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 ct operation of
1a7ad 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a the.** library..
1a7ae 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1a7af 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
1a7b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 int sqlite3_sor
1a7b1 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e t_count = 0;.#en
1a7b2 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e dif../*.** The n
1a7b3 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 ext global varia
1a7b4 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 20 ble records the
1a7b5 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 size of the larg
1a7b6 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 est MEM_Blob.**
1a7b7 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 or MEM_Str that
1a7b8 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 79 has been used by
1a7b9 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 a VDBE opcode.
1a7ba 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 The test proced
1a7bb 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 ures.** use this
1a7bc 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 information to
1a7bd 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
1a7be 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e he zero-blob fun
1a7bf 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 ctionality.** is
1a7c0 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 working correct
1a7c1 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69 61 ly. This varia
1a7c2 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 ble has no funct
1a7c3 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 ion other than t
1a7c4 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 o.** help verify
1a7c5 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 the correct ope
1a7c6 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 ration of the li
1a7c7 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 brary..*/.#ifdef
1a7c8 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
1a7c9 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1a7ca 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 te3_max_blobsize
1a7cb 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 = 0;.static voi
1a7cc 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 d updateMaxBlobs
1a7cd 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 ize(Mem *p){. i
1a7ce 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 f( (p->flags & (
1a7cf 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
1a7d0 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 ))!=0 && p->n>sq
1a7d1 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 lite3_max_blobsi
1a7d2 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ze ){. sqlite
1a7d3 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 3_max_blobsize =
1a7d4 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e p->n;. }.}.#en
1a7d5 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 dif../*.** Test
1a7d6 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 73 65 a register to se
1a7d7 65 20 69 66 20 69 74 20 65 78 63 65 65 64 73 20 e if it exceeds
1a7d8 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 the current maxi
1a7d9 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a mum blob size..*
1a7da 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65 * If it does, re
1a7db 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78 cord the new max
1a7dc 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a imum blob size..
1a7dd 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
1a7de 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 21 QLITE_TEST) && !
1a7df 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1a7e0 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
1a7e1 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 ).# define UPDAT
1a7e2 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 E_MAX_BLOBSIZE(P
1a7e3 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 ) updateMaxBlob
1a7e4 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 size(P).#else.#
1a7e5 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 define UPDATE_MA
1a7e6 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 X_BLOBSIZE(P).#e
1a7e7 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 ndif../*.** Conv
1a7e8 65 72 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 ert the given re
1a7e9 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 gister into a st
1a7ea 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 ring if it isn't
1a7eb 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e one.** already.
1a7ec 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f Return non-zero
1a7ed 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 if a malloc() f
1a7ee 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ails..*/.#define
1a7ef 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e Stringify(P, en
1a7f0 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d c) \. if(((P)-
1a7f1 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c >flags&(MEM_Str|
1a7f2 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 MEM_Blob))==0 &&
1a7f3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a7f4 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 tringify(P,enc))
1a7f5 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e \. { goto n
1a7f6 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 o_mem; }../*.**
1a7f7 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 An ephemeral str
1a7f8 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 ing value (signi
1a7f9 66 69 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f fied by the MEM_
1a7fa 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 Ephem flag) cont
1a7fb 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 ains.** a pointe
1a7fc 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c r to a dynamical
1a7fd 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 ly allocated str
1a7fe 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f ing where some o
1a7ff 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 ther entity.** i
1a800 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
1a801 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 r deallocating t
1a802 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 hat string. Bec
1a803 61 75 73 65 20 74 68 65 20 72 65 67 69 73 74 65 ause the registe
1a804 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f r.** does not co
1a805 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 ntrol the string
1a806 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 , it might be de
1a807 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 leted without th
1a808 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e e register.** kn
1a809 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 owing it..**.**
1a80a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e This routine con
1a80b 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 verts an ephemer
1a80c 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 al string into a
1a80d 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c dynamically all
1a80e 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 ocated.** string
1a80f 20 74 68 61 74 20 74 68 65 20 72 65 67 69 73 74 that the regist
1a810 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f er itself contro
1a811 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f ls. In other wo
1a812 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 rds, it.** conve
1a813 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d rts an MEM_Ephem
1a814 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 string into an
1a815 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a MEM_Dyn string..
1a816 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 */.#define Deeph
1a817 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 emeralize(P) \.
1a818 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 if( ((P)->flag
1a819 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 s&MEM_Ephem)!=0
1a81a 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 \. && sqli
1a81b 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 te3VdbeMemMakeWr
1a81c 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f iteable(P) ){ go
1a81d 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a to no_mem;}../*.
1a81e 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 ** Call sqlite3V
1a81f 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
1a820 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 () on the suppli
1a821 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d ed value (type M
1a822 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 em*).** P if req
1a823 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e uired..*/.#defin
1a824 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 e ExpandBlob(P)
1a825 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d (((P)->flags&MEM
1a826 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 _Zero)?sqlite3Vd
1a827 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 beMemExpandBlob(
1a828 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 P):0)../*.** Arg
1a829 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 ument pMem point
1a82a 73 20 61 74 20 61 20 72 65 67 69 73 74 65 72 20 s at a register
1a82b 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 that will be pas
1a82c 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 sed to a.** user
1a82d 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
1a82e 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f n or returned to
1a82f 20 74 68 65 20 75 73 65 72 20 61 73 20 74 68 65 the user as the
1a830 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 result of a que
1a831 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e ry..** The secon
1a832 64 20 61 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f d argument, 'db_
1a833 65 6e 63 27 20 69 73 20 74 68 65 20 74 65 78 74 enc' is the text
1a834 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 encoding used b
1a835 79 20 74 68 65 20 76 64 62 65 20 66 6f 72 0a 2a y the vdbe for.*
1a836 2a 20 72 65 67 69 73 74 65 72 20 76 61 72 69 61 * register varia
1a837 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f 75 74 bles. This rout
1a838 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 ine sets the pMe
1a839 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d m->enc and pMem-
1a83a 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c >type.** variabl
1a83b 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 es used by the s
1a83c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 qlite3_value_*()
1a83d 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64 routines..*/.#d
1a83e 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70 65 49 efine storeTypeI
1a83f 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65 54 nfo(A,B) _storeT
1a840 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61 74 69 ypeInfo(A).stati
1a841 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54 79 70 c void _storeTyp
1a842 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29 eInfo(Mem *pMem)
1a843 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 {. int flags =
1a844 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 pMem->flags;. i
1a845 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e f( flags & MEM_N
1a846 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d ull ){. pMem-
1a847 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e >type = SQLITE_N
1a848 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 ULL;. }. else
1a849 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f if( flags & MEM_
1a84a 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d Int ){. pMem-
1a84b 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 >type = SQLITE_I
1a84c 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c NTEGER;. }. el
1a84d 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d se if( flags & M
1a84e 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 70 EM_Real ){. p
1a84f 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a850 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20 TE_FLOAT;. }.
1a851 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 else if( flags &
1a852 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 MEM_Str ){.
1a853 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a854 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73 ITE_TEXT;. }els
1a855 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 e{. pMem->typ
1a856 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b e = SQLITE_BLOB;
1a857 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 . }.}../*.** Pr
1a858 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f operties of opco
1a859 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f des. The OPFLG_
1a85a 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72 INITIALIZER macr
1a85b 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 o is.** created
1a85c 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b by mkopcodeh.awk
1a85d 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 during compilat
1a85e 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62 ion. Data is ob
1a85f 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 tained.** from t
1a860 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c he comments foll
1a861 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 20 owing the "case
1a862 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d OP_xxxx:" statem
1a863 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20 ents in.** this
1a864 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 file. .*/.stati
1a865 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
1a866 20 63 68 61 72 20 6f 70 63 6f 64 65 50 72 6f 70 char opcodeProp
1a867 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49 erty[] = OPFLG_I
1a868 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 0a NITIALIZER;../*.
1a869 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 ** Return true i
1a86a 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 61 73 20 f an opcode has
1a86b 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 46 4c 47 any of the OPFLG
1a86c 5f 78 78 78 20 70 72 6f 70 65 72 74 69 65 73 0a _xxx properties.
1a86d 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ** specified by
1a86e 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mask..*/.SQLITE_
1a86f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1a870 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73 te3VdbeOpcodeHas
1a871 50 72 6f 70 65 72 74 79 28 69 6e 74 20 6f 70 63 Property(int opc
1a872 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a ode, int mask){.
1a873 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65 assert( opcode
1a874 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c 28 69 6e >0 && opcode<(in
1a875 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f 64 65 50 t)sizeof(opcodeP
1a876 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 20 72 65 roperty) );. re
1a877 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 72 6f 70 turn (opcodeProp
1a878 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 6d 61 73 erty[opcode]&mas
1a879 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 k)!=0;.}../*.**
1a87a 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 75 72 Allocate VdbeCur
1a87b 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2e sor number iCur.
1a87c 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 Return a point
1a87d 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75 72 er to it. Retur
1a87e 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 n NULL.** if we
1a87f 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 run out of memor
1a880 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 64 62 y..*/.static Vdb
1a881 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 74 eCursor *allocat
1a882 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 65 20 eCursor(. Vdbe
1a883 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
1a884 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 /* The virtual
1a885 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 machine */. int
1a886 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 iCur,
1a887 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 /* Index of t
1a888 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 73 6f he new VdbeCurso
1a889 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c r */. int nFiel
1a88a 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 d, /*
1a88b 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 Number of fields
1a88c 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 in the table or
1a88d 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 index */. int
1a88e 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 iDb,
1a88f 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 61 62 61 /* When databa
1a890 73 65 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 se the cursor be
1a891 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d 31 20 longs to, or -1
1a892 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 65 65 */. int isBtree
1a893 43 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 2a 2f Cursor /* */
1a894 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 .){. /* Find th
1a895 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 e memory cell th
1a896 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 at will be used
1a897 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f to store the blo
1a898 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a b of memory. **
1a899 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 required for th
1a89a 69 73 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 is VdbeCursor st
1a89b 72 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 63 ructure. It is c
1a89c 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65 onvenient to use
1a89d 20 61 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65 a . ** vdbe me
1a89e 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e mory cell to man
1a89f 61 67 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 age the memory a
1a8a0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 llocation requir
1a8a1 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64 ed for a. ** Vd
1a8a2 62 65 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 beCursor structu
1a8a3 72 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f re for the follo
1a8a4 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20 wing reasons:.
1a8a5 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 **. ** * Some
1a8a6 74 69 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d times cursor num
1a8a7 62 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f bers are used fo
1a8a8 72 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 r a couple of di
1a8a9 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20 fferent. **
1a8aa 20 70 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 purposes in a v
1a8ab 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 dbe program. The
1a8ac 20 64 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 different uses
1a8ad 6d 69 67 68 74 20 72 65 71 75 69 72 65 0a 20 20 might require.
1a8ae 2a 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 ** different
1a8af 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f sized allocatio
1a8b0 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 ns. Memory cells
1a8b1 20 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c provide growabl
1a8b2 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63 e. ** alloc
1a8b3 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a ations.. **. *
1a8b4 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67 * * When using
1a8b5 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d ENABLE_MEMORY_M
1a8b6 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72 ANAGEMENT, memor
1a8b7 79 20 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63 y cell buffers c
1a8b8 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66 an. ** be f
1a8b9 72 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20 reed lazily via
1a8ba 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 the sqlite3_rele
1a8bb 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49 ase_memory() API
1a8bc 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20 . This. **
1a8bd 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75 minimizes the nu
1a8be 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63 mber of malloc c
1a8bf 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65 alls made by the
1a8c0 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20 system.. **.
1a8c1 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 ** Memory cells
1a8c2 66 6f 72 20 63 75 72 73 6f 72 73 20 61 72 65 20 for cursors are
1a8c3 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 allocated at the
1a8c4 20 74 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72 top of the addr
1a8c5 65 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20 ess. ** space.
1a8c6 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e Memory cell (p->
1a8c7 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64 nMem) correspond
1a8c8 73 20 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53 s to cursor 0. S
1a8c9 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75 pace for. ** cu
1a8ca 72 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65 rsor 1 is manage
1a8cb 64 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c d by memory cell
1a8cc 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74 (p->nMem-1), et
1a8cd 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 c.. */. Mem *p
1a8ce 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Mem = &p->aMem[p
1a8cf 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20 ->nMem-iCur];..
1a8d0 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64 int nByte;. Vd
1a8d1 62 65 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20 beCursor *pCx =
1a8d2 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 0a 20 20 0;. nByte = .
1a8d3 20 20 20 20 73 69 7a 65 6f 66 28 56 64 62 65 43 sizeof(VdbeC
1a8d4 75 72 73 6f 72 29 20 2b 20 0a 20 20 20 20 20 20 ursor) + .
1a8d5 28 69 73 42 74 72 65 65 43 75 72 73 6f 72 3f 73 (isBtreeCursor?s
1a8d6 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
1a8d7 72 53 69 7a 65 28 29 3a 30 29 20 2b 20 0a 20 20 rSize():0) + .
1a8d8 20 20 20 20 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 2*nField*siz
1a8d9 65 6f 66 28 75 33 32 29 3b 0a 0a 20 20 61 73 73 eof(u32);.. ass
1a8da 65 72 74 28 20 69 43 75 72 3c 70 2d 3e 6e 43 75 ert( iCur<p->nCu
1a8db 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 2d rsor );. if( p-
1a8dc 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 29 7b 0a >apCsr[iCur] ){.
1a8dd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 sqlite3VdbeF
1a8de 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e reeCursor(p, p->
1a8df 61 70 43 73 72 5b 69 43 75 72 5d 29 3b 0a 20 20 apCsr[iCur]);.
1a8e0 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d p->apCsr[iCur]
1a8e1 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
1a8e2 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 SQLITE_OK==sqlit
1a8e3 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
1a8e4 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 7b em, nByte, 0) ){
1a8e5 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 . p->apCsr[iC
1a8e6 75 72 5d 20 3d 20 70 43 78 20 3d 20 28 56 64 62 ur] = pCx = (Vdb
1a8e7 65 43 75 72 73 6f 72 2a 29 70 4d 65 6d 2d 3e 7a eCursor*)pMem->z
1a8e8 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 ;. memset(pMe
1a8e9 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b m->z, 0, nByte);
1a8ea 0a 20 20 20 20 70 43 78 2d 3e 69 44 62 20 3d 20 . pCx->iDb =
1a8eb 69 44 62 3b 0a 20 20 20 20 70 43 78 2d 3e 6e 46 iDb;. pCx->nF
1a8ec 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a 20 ield = nField;.
1a8ed 20 20 20 69 66 28 20 6e 46 69 65 6c 64 20 29 7b if( nField ){
1a8ee 0a 20 20 20 20 20 20 70 43 78 2d 3e 61 54 79 70 . pCx->aTyp
1a8ef 65 20 3d 20 28 75 33 32 20 2a 29 26 70 4d 65 6d e = (u32 *)&pMem
1a8f0 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 ->z[sizeof(VdbeC
1a8f1 75 72 73 6f 72 29 5d 3b 0a 20 20 20 20 7d 0a 20 ursor)];. }.
1a8f2 20 20 20 69 66 28 20 69 73 42 74 72 65 65 43 75 if( isBtreeCu
1a8f3 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 70 43 rsor ){. pC
1a8f4 78 2d 3e 70 43 75 72 73 6f 72 20 3d 20 28 42 74 x->pCursor = (Bt
1a8f5 43 75 72 73 6f 72 2a 29 0a 20 20 20 20 20 20 20 Cursor*).
1a8f6 20 20 20 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 &pMem->z[size
1a8f7 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 2b 32 of(VdbeCursor)+2
1a8f8 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 *nField*sizeof(u
1a8f9 33 32 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 32)];. }. }.
1a8fa 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a return pCx;.}.
1a8fb 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f ./*.** Try to co
1a8fc 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20 69 6e nvert a value in
1a8fd 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 to a numeric rep
1a8fe 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 77 resentation if w
1a8ff 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77 e can.** do so w
1a900 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 ithout loss of i
1a901 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20 nformation. In
1a902 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 other words, if
1a903 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f the string.** lo
1a904 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 oks like a numbe
1a905 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e r, convert it in
1a906 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 to a number. If
1a907 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 it does not.**
1a908 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62 look like a numb
1a909 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61 6c 6f er, leave it alo
1a90a 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ne..*/.static vo
1a90b 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 id applyNumericA
1a90c 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65 ffinity(Mem *pRe
1a90d 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65 63 2d c){. if( (pRec-
1a90e 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 >flags & (MEM_Re
1a90f 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 al|MEM_Int))==0
1a910 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61 6c 6e ){. int realn
1a911 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 um;. sqlite3V
1a912 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
1a913 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 te(pRec);. if
1a914 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d ( (pRec->flags&M
1a915 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20 20 20 EM_Str).
1a916 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e 75 6d && sqlite3IsNum
1a917 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65 ber(pRec->z, &re
1a918 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63 alnum, pRec->enc
1a919 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 ) ){. i64 v
1a91a 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 alue;. sqli
1a91b 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1a91c 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51 4c 49 oding(pRec, SQLI
1a91d 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20 TE_UTF8);.
1a91e 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26 26 20 if( !realnum &&
1a91f 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 52 sqlite3Atoi64(pR
1a920 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29 ec->z, &value) )
1a921 7b 0a 20 20 20 20 20 20 20 20 70 52 65 63 2d 3e {. pRec->
1a922 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20 u.i = value;.
1a923 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 MemSetTypeF
1a924 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e lag(pRec, MEM_In
1a925 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b t);. }else{
1a926 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a927 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 VdbeMemRealify(p
1a928 52 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Rec);. }.
1a929 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1a92a 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64 Processing is d
1a92b 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20 etermine by the
1a92c 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74 affinity paramet
1a92d 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 er:.**.** SQLITE
1a92e 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a _AFF_INTEGER:.**
1a92f 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
1a930 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f :.** SQLITE_AFF_
1a931 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 NUMERIC:.** T
1a932 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 ry to convert pR
1a933 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 ec to an integer
1a934 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1a935 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 or a .** floa
1a936 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 ting-point repre
1a937 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 sentation if an
1a938 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e integer represen
1a939 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 tation.** is
1a93a 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e not possible. N
1a93b 6f 74 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 ote that the int
1a93c 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 eger representat
1a93d 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 ion is.** alw
1a93e 61 79 73 20 70 72 65 66 65 72 72 65 64 2c 20 65 ays preferred, e
1a93f 76 65 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e ven if the affin
1a940 69 74 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63 ity is REAL, bec
1a941 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e ause.** an in
1a942 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 teger representa
1a943 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 tion is more spa
1a944 63 65 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 ce efficient on
1a945 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 disk..**.** SQLI
1a946 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 TE_AFF_TEXT:.**
1a947 20 20 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20 Convert pRec
1a948 74 6f 20 61 20 74 65 78 74 20 72 65 70 72 65 73 to a text repres
1a949 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 entation..**.**
1a94a 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a SQLITE_AFF_NONE:
1a94b 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 .** No-op. p
1a94c 52 65 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64 Rec is unchanged
1a94d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1a94e 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a applyAffinity(.
1a94f 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20 Mem *pRec,
1a950 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c /* The val
1a951 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 ue to apply affi
1a952 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61 nity to */. cha
1a953 72 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 r affinity,
1a954 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 /* The affinity
1a955 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a to be applied *
1a956 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 20 20 20 /. u8 enc
1a957 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 /* Use t
1a958 68 69 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e his text encodin
1a959 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66 g */.){. if( af
1a95a 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
1a95b 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f FF_TEXT ){. /
1a95c 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74 * Only attempt t
1a95d 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f he conversion to
1a95e 20 54 45 58 54 20 69 66 20 74 68 65 72 65 20 69 TEXT if there i
1a95f 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 s an integer or
1a960 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72 real. ** repr
1a961 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62 esentation (blob
1a962 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74 and NULL do not
1a963 20 67 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20 get converted)
1a964 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20 but no string.
1a965 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 ** representat
1a966 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
1a967 69 66 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c if( 0==(pRec->fl
1a968 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20 ags&MEM_Str) &&
1a969 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45 (pRec->flags&(ME
1a96a 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 M_Real|MEM_Int))
1a96b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1a96c 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 3VdbeMemStringif
1a96d 79 28 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20 y(pRec, enc);.
1a96e 20 20 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c }. pRec->fl
1a96f 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 ags &= ~(MEM_Rea
1a970 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 l|MEM_Int);. }e
1a971 6c 73 65 20 69 66 28 20 61 66 66 69 6e 69 74 79 lse if( affinity
1a972 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e !=SQLITE_AFF_NON
1a973 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 E ){. assert(
1a974 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
1a975 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c E_AFF_INTEGER ||
1a976 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
1a977 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 E_AFF_REAL.
1a978 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e || affin
1a979 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity==SQLITE_AFF_
1a97a 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61 NUMERIC );. a
1a97b 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e pplyNumericAffin
1a97c 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69 ity(pRec);. i
1a97d 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 f( pRec->flags &
1a97e 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 MEM_Real ){.
1a97f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e sqlite3VdbeIn
1a980 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 52 tegerAffinity(pR
1a981 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ec);. }. }.}
1a982 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 ../*.** Try to c
1a983 6f 6e 76 65 72 74 20 74 68 65 20 74 79 70 65 20 onvert the type
1a984 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72 of a function ar
1a985 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75 gument or a resu
1a986 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 lt column.** int
1a987 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 o a numeric repr
1a988 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65 esentation. Use
1a989 20 65 69 74 68 65 72 20 49 4e 54 45 47 45 52 20 either INTEGER
1a98a 6f 72 20 52 45 41 4c 20 77 68 69 63 68 65 76 65 or REAL whicheve
1a98b 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69 r.** is appropri
1a98c 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64 ate. But only d
1a98d 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e o the conversion
1a98e 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 if it is possib
1a98f 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f le without.** lo
1a990 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f ss of informatio
1a991 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 n and return the
1a992 20 72 65 76 69 73 65 64 20 74 79 70 65 20 6f 66 revised type of
1a993 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a the argument..*
1a994 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 *.** This is an
1a995 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 61 70 69 EXPERIMENTAL api
1a996 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 and is subject
1a997 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 72 65 6d to change or rem
1a998 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oval..*/.SQLITE_
1a999 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1a99a 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 value_numeric_ty
1a99b 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 pe(sqlite3_value
1a99c 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a *pVal){. Mem *
1a99d 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61 pMem = (Mem*)pVa
1a99e 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 l;. applyNumeri
1a99f 63 41 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b cAffinity(pMem);
1a9a0 0a 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f . storeTypeInfo
1a9a1 28 70 4d 65 6d 2c 20 30 29 3b 0a 20 20 72 65 74 (pMem, 0);. ret
1a9a2 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 3b 0a urn pMem->type;.
1a9a3 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72 74 65 }../*.** Exporte
1a9a4 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 70 70 d version of app
1a9a5 6c 79 41 66 66 69 6e 69 74 79 28 29 2e 20 54 68 lyAffinity(). Th
1a9a6 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20 6f 6e 20 is one works on
1a9a7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 sqlite3_value*,
1a9a8 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69 6e 74 65 .** not the inte
1a9a9 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70 65 2e 0a rnal Mem* type..
1a9aa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a9ab 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 E void sqlite3Va
1a9ac 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 lueApplyAffinity
1a9ad 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 (. sqlite3_valu
1a9ae 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75 38 20 61 e *pVal, . u8 a
1a9af 66 66 69 6e 69 74 79 2c 20 0a 20 20 75 38 20 65 ffinity, . u8 e
1a9b0 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79 41 66 66 nc.){. applyAff
1a9b1 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29 70 56 61 inity((Mem *)pVa
1a9b2 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 l, affinity, enc
1a9b3 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c );.}..#ifdef SQL
1a9b4 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ITE_DEBUG./*.**
1a9b5 57 72 69 74 65 20 61 20 6e 69 63 65 20 73 74 72 Write a nice str
1a9b6 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
1a9b7 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e on of the conten
1a9b8 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d 65 6d 0a ts of cell pMem.
1a9b9 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65 72 20 7a ** into buffer z
1a9ba 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e 42 75 66 Buf, length nBuf
1a9bb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a9bc 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a9bd 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 VdbeMemPrettyPri
1a9be 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 nt(Mem *pMem, ch
1a9bf 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 63 68 61 ar *zBuf){. cha
1a9c0 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75 66 3b 0a r *zCsr = zBuf;.
1a9c1 20 20 69 6e 74 20 66 20 3d 20 70 4d 65 6d 2d 3e int f = pMem->
1a9c2 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61 74 69 63 flags;.. static
1a9c3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e const char *con
1a9c4 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 st encnames[] =
1a9c5 7b 22 28 58 29 22 2c 20 22 28 38 29 22 2c 20 22 {"(X)", "(8)", "
1a9c6 28 31 36 4c 45 29 22 2c 20 22 28 31 36 42 45 29 (16LE)", "(16BE)
1a9c7 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26 4d 45 4d "};.. if( f&MEM
1a9c8 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20 69 6e 74 _Blob ){. int
1a9c9 20 69 3b 0a 20 20 20 20 63 68 61 72 20 63 3b 0a i;. char c;.
1a9ca 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f if( f & MEM_
1a9cb 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 63 20 3d Dyn ){. c =
1a9cc 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 65 'z';. asse
1a9cd 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 rt( (f & (MEM_St
1a9ce 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 atic|MEM_Ephem))
1a9cf 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ==0 );. }else
1a9d0 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 if( f & MEM_Sta
1a9d1 74 69 63 20 29 7b 0a 20 20 20 20 20 20 63 20 3d tic ){. c =
1a9d2 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 't';. asse
1a9d3 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 rt( (f & (MEM_Dy
1a9d4 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 n|MEM_Ephem))==0
1a9d5 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 );. }else if
1a9d6 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 ( f & MEM_Ephem
1a9d7 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 65 27 ){. c = 'e'
1a9d8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1a9d9 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 (f & (MEM_Static
1a9da 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b |MEM_Dyn))==0 );
1a9db 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1a9dc 20 20 63 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d c = 's';. }
1a9dd 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e .. sqlite3_sn
1a9de 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 printf(100, zCsr
1a9df 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20 20 20 20 , "%c", c);.
1a9e0 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 zCsr += sqlite3S
1a9e1 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 trlen30(zCsr);.
1a9e2 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1a9e3 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 ntf(100, zCsr, "
1a9e4 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a %d[", pMem->n);.
1a9e5 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 zCsr += sqli
1a9e6 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 te3Strlen30(zCsr
1a9e7 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
1a9e8 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e i<16 && i<pMem->
1a9e9 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 n; i++){. s
1a9ea 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a9eb 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30 32 58 100, zCsr, "%02X
1a9ec 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e 7a ", ((int)pMem->z
1a9ed 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a 20 20 [i] & 0xFF));.
1a9ee 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 zCsr += sqli
1a9ef 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 te3Strlen30(zCsr
1a9f0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
1a9f1 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c (i=0; i<16 && i<
1a9f2 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 pMem->n; i++){.
1a9f3 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d char z = pM
1a9f4 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 em->z[i];.
1a9f5 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32 if( z<32 || z>12
1a9f6 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e 6 ) *zCsr++ = '.
1a9f7 27 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a ';. else *z
1a9f8 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d Csr++ = z;. }
1a9f9 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e .. sqlite3_sn
1a9fa 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 printf(100, zCsr
1a9fb 2c 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65 , "]%s", encname
1a9fc 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 s[pMem->enc]);.
1a9fd 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 zCsr += sqlit
1a9fe 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 e3Strlen30(zCsr)
1a9ff 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 ;. if( f & ME
1aa00 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
1aa01 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1aa02 28 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 64 7a (100, zCsr,"+%dz
1aa03 22 2c 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 ",pMem->u.nZero)
1aa04 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 ;. zCsr +=
1aa05 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1aa06 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 zCsr);. }.
1aa07 20 2a 7a 43 73 72 20 3d 20 27 5c 30 27 3b 0a 20 *zCsr = '\0';.
1aa08 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d }else if( f & M
1aa09 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 6e EM_Str ){. in
1aa0a 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 7a 42 75 66 t j, k;. zBuf
1aa0b 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 69 [0] = ' ';. i
1aa0c 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 f( f & MEM_Dyn )
1aa0d 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 {. zBuf[1]
1aa0e 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 = 'z';. ass
1aa0f 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 ert( (f & (MEM_S
1aa10 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 tatic|MEM_Ephem)
1aa11 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 )==0 );. }els
1aa12 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 e if( f & MEM_St
1aa13 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 7a 42 atic ){. zB
1aa14 75 66 5b 31 5d 20 3d 20 27 74 27 3b 0a 20 20 20 uf[1] = 't';.
1aa15 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1aa16 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 (MEM_Dyn|MEM_Eph
1aa17 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d em))==0 );. }
1aa18 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d else if( f & MEM
1aa19 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 _Ephem ){.
1aa1a 7a 42 75 66 5b 31 5d 20 3d 20 27 65 27 3b 0a 20 zBuf[1] = 'e';.
1aa1b 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 assert( (f
1aa1c 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 & (MEM_Static|ME
1aa1d 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 M_Dyn))==0 );.
1aa1e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a }else{. z
1aa1f 42 75 66 5b 31 5d 20 3d 20 27 73 27 3b 0a 20 20 Buf[1] = 's';.
1aa20 20 20 7d 0a 20 20 20 20 6b 20 3d 20 32 3b 0a 20 }. k = 2;.
1aa21 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1aa22 6e 74 66 28 31 30 30 2c 20 26 7a 42 75 66 5b 6b ntf(100, &zBuf[k
1aa23 5d 2c 20 22 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e ], "%d", pMem->n
1aa24 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 );. k += sqli
1aa25 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 te3Strlen30(&zBu
1aa26 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b f[k]);. zBuf[
1aa27 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20 20 20 k++] = '[';.
1aa28 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 20 26 26 for(j=0; j<15 &&
1aa29 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 j<pMem->n; j++)
1aa2a 7b 0a 20 20 20 20 20 20 75 38 20 63 20 3d 20 70 {. u8 c = p
1aa2b 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20 20 20 Mem->z[j];.
1aa2c 20 69 66 28 20 63 3e 3d 30 78 32 30 20 26 26 20 if( c>=0x20 &&
1aa2d 63 3c 30 78 37 66 20 29 7b 0a 20 20 20 20 20 20 c<0x7f ){.
1aa2e 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 63 3b zBuf[k++] = c;
1aa2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1aa30 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 zBuf[k++]
1aa31 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 7d 0a 20 = '.';. }.
1aa32 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6b 2b }. zBuf[k+
1aa33 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20 20 73 71 +] = ']';. sq
1aa34 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 lite3_snprintf(1
1aa35 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65 6e 63 00,&zBuf[k], enc
1aa36 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d names[pMem->enc]
1aa37 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 );. k += sqli
1aa38 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 te3Strlen30(&zBu
1aa39 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b f[k]);. zBuf[
1aa3a 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a k++] = 0;. }.}.
1aa3b 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
1aa3c 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a QLITE_DEBUG./*.*
1aa3d 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61 6c 75 * Print the valu
1aa3e 65 20 6f 66 20 61 20 72 65 67 69 73 74 65 72 20 e of a register
1aa3f 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75 72 70 for tracing purp
1aa40 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 oses:.*/.static
1aa41 76 6f 69 64 20 6d 65 6d 54 72 61 63 65 50 72 69 void memTracePri
1aa42 6e 74 28 46 49 4c 45 20 2a 6f 75 74 2c 20 4d 65 nt(FILE *out, Me
1aa43 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e m *p){. if( p->
1aa44 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1aa45 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ){. fprintf(
1aa46 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 29 3b 0a 20 out, " NULL");.
1aa47 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d 3e 66 }else if( (p->f
1aa48 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c lags & (MEM_Int|
1aa49 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 4d 5f MEM_Str))==(MEM_
1aa4a 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a Int|MEM_Str) ){.
1aa4b 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
1aa4c 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e " si:%lld", p->
1aa4d 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 u.i);. }else if
1aa4e 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
1aa4f 5f 49 6e 74 20 29 7b 0a 20 20 20 20 66 70 72 69 _Int ){. fpri
1aa50 6e 74 66 28 6f 75 74 2c 20 22 20 69 3a 25 6c 6c ntf(out, " i:%ll
1aa51 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d d", p->u.i);. }
1aa52 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 else if( p->flag
1aa53 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a s & MEM_Real ){.
1aa54 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
1aa55 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72 29 3b " r:%g", p->r);
1aa56 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 . }else{. ch
1aa57 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a 20 20 ar zBuf[200];.
1aa58 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1aa59 50 72 65 74 74 79 50 72 69 6e 74 28 70 2c 20 7a PrettyPrint(p, z
1aa5a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 Buf);. fprint
1aa5b 66 28 6f 75 74 2c 20 22 20 22 29 3b 0a 20 20 20 f(out, " ");.
1aa5c 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
1aa5d 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 7d s", zBuf);. }.}
1aa5e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 67 .static void reg
1aa5f 69 73 74 65 72 54 72 61 63 65 28 46 49 4c 45 20 isterTrace(FILE
1aa60 2a 6f 75 74 2c 20 69 6e 74 20 69 52 65 67 2c 20 *out, int iReg,
1aa61 4d 65 6d 20 2a 70 29 7b 0a 20 20 66 70 72 69 6e Mem *p){. fprin
1aa62 74 66 28 6f 75 74 2c 20 22 52 45 47 5b 25 64 5d tf(out, "REG[%d]
1aa63 20 3d 20 22 2c 20 69 52 65 67 29 3b 0a 20 20 6d = ", iReg);. m
1aa64 65 6d 54 72 61 63 65 50 72 69 6e 74 28 6f 75 74 emTracePrint(out
1aa65 2c 20 70 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 , p);. fprintf(
1aa66 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 7d 0a 23 65 out, "\n");.}.#e
1aa67 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
1aa68 49 54 45 5f 44 45 42 55 47 0a 23 20 20 64 65 66 ITE_DEBUG.# def
1aa69 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 ine REGISTER_TRA
1aa6a 43 45 28 52 2c 4d 29 20 69 66 28 70 2d 3e 74 72 CE(R,M) if(p->tr
1aa6b 61 63 65 29 72 65 67 69 73 74 65 72 54 72 61 63 ace)registerTrac
1aa6c 65 28 70 2d 3e 74 72 61 63 65 2c 52 2c 4d 29 0a e(p->trace,R,M).
1aa6d 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 #else.# define
1aa6e 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 52 REGISTER_TRACE(R
1aa6f 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 ,M).#endif...#if
1aa70 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 def VDBE_PROFILE
1aa71 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e ../* .** hwtime.
1aa72 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e h contains inlin
1aa73 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 e assembler code
1aa74 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e for implementin
1aa75 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f g .** high-perfo
1aa76 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f rmance timing ro
1aa77 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a utines..*/./****
1aa78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
1aa79 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 de hwtime.h in t
1aa7a 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 64 62 he middle of vdb
1aa7b 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
1aa7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1aa7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
1aa7e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a file hwtime.h *
1aa7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
1aa82 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2008 May 27.**.
1aa83 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1aa84 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1aa85 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1aa86 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1aa87 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1aa88 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1aa89 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1aa8a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1aa8b 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1aa8c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1aa8d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1aa8e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1aa8f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1aa90 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1aa91 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1aa92 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1aa93 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1aa94 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1aa95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa99 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
1aa9a 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
1aa9b 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 asm code for re
1aa9c 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 trieving "high-p
1aa9d 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 erformance".** c
1aa9e 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 ounters for x86
1aa9f 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a class CPUs..**.*
1aaa0 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c * $Id: hwtime.h,
1aaa1 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 v 1.3 2008/08/01
1aaa2 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 14:33:15 shane
1aaa3 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
1aaa4 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 _HWTIME_H_.#def
1aaa5 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a ine _HWTIME_H_..
1aaa6 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
1aaa7 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 ing routine only
1aaa8 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 works on pentiu
1aaa9 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 m-class (or newe
1aaaa 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a r) processors..*
1aaab 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 * It uses the RD
1aaac 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 TSC opcode to re
1aaad 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 ad the cycle cou
1aaae 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 nt value out of
1aaaf 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 the.** processor
1aab0 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 and returns tha
1aab1 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 t value. This c
1aab2 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 an be used for h
1aab3 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 igh-res.** profi
1aab4 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 ling..*/.#if (de
1aab5 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
1aab6 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f || defined(_MSC_
1aab7 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 VER)) && \.
1aab8 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 (defined(i386)
1aab9 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 || defined(__i38
1aaba 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 6__) || defined(
1aabb 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 _M_IX86)).. #if
1aabc 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f defined(__GNUC_
1aabd 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f _).. __inline__
1aabe 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
1aabf 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
1aac0 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 d){. unsigne
1aac1 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 d int lo, hi;.
1aac2 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
1aac3 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
1aac4 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d : "=a" (lo), "=
1aac5 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 d" (hi));. r
1aac6 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 eturn (sqlite_ui
1aac7 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 nt64)hi << 32 |
1aac8 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 lo;. }.. #elif
1aac9 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
1aaca 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 R).. __declspec
1aacb 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 (naked) __inline
1aacc 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f sqlite_uint64 _
1aacd 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 _cdecl sqlite3Hw
1aace 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
1aacf 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 __asm {.
1aad0 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 rdtsc. r
1aad1 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 et ; retur
1aad2 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 n value at EDX:E
1aad3 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 AX. }. }..
1aad4 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 #endif..#elif (
1aad5 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
1aad6 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 ) && defined(__x
1aad7 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 86_64__)).. __i
1aad8 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
1aad9 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
1aada 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
1aadb 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 unsigned long v
1aadc 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f al;. __asm_
1aadd 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
1aade 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 "rdtsc" : "=A" (
1aadf 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 val));. ret
1aae0 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 urn val;. }. .#
1aae1 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
1aae2 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
1aae3 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 ed(__ppc__))..
1aae4 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
1aae5 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
1aae6 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
1aae7 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
1aae8 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 g long retval;.
1aae9 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
1aaea 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f ng junk;. _
1aaeb 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
1aaec 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 e__ ("\n\.
1aaed 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 1: mftb
1aaee 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 u %1\n\.
1aaef 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 mftb
1aaf0 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 %L0\n\.
1aaf1 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 mft
1aaf2 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 bu %0\n\.
1aaf3 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 cmp
1aaf4 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 w %0,%1\n\.
1aaf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aaf6 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 bne 1b".
1aaf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 :
1aaf8 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 "=r" (retval), "
1aaf9 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 =r" (junk));.
1aafa 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c return retval
1aafb 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 ;. }..#else..
1aafc 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c #error Need impl
1aafd 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
1aafe 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
1aaff 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e r your platform.
1ab00 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 .. /*. ** To c
1ab01 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 ompile without i
1ab02 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 mplementing sqli
1ab03 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
1ab04 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 your platform,.
1ab05 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f ** you can remo
1ab06 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 ve the above #er
1ab07 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 ror and use the
1ab08 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 following. ** s
1ab09 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 tub function. Y
1ab0a 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d ou will lose tim
1ab0b 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 ing support for
1ab0c 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 many. ** of the
1ab0d 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 debugging and t
1ab0e 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 esting utilities
1ab0f 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 , but it should
1ab10 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f at. ** least co
1ab11 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 mpile and run..
1ab12 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
1ab13 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 TE sqlite_uint
1ab14 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
1ab15 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 (void){ return (
1ab16 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
1ab17 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 ); }..#endif..#e
1ab18 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
1ab19 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a (_HWTIME_H_) */.
1ab1a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1ab1b 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 End of hwtime.h
1ab1c 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1ab1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ab1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1ab1f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1ab20 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
1ab21 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
1ab22 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.c ********
1ab23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1ab24 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ..#endif../*.**
1ab25 54 68 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e The CHECK_FOR_IN
1ab26 54 45 52 52 55 50 54 20 6d 61 63 72 6f 20 64 65 TERRUPT macro de
1ab27 66 69 6e 65 64 20 68 65 72 65 20 6c 6f 6f 6b 73 fined here looks
1ab28 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 0a 2a to see if the.*
1ab29 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 * sqlite3_interr
1ab2a 75 70 74 28 29 20 72 6f 75 74 69 6e 65 20 68 61 upt() routine ha
1ab2b 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 20 20 s been called.
1ab2c 49 66 20 69 74 20 68 61 73 20 62 65 65 6e 2c 20 If it has been,
1ab2d 74 68 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 69 then.** processi
1ab2e 6e 67 20 6f 66 20 74 68 65 20 56 44 42 45 20 70 ng of the VDBE p
1ab2f 72 6f 67 72 61 6d 20 69 73 20 69 6e 74 65 72 72 rogram is interr
1ab30 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 upted..**.** Thi
1ab31 73 20 6d 61 63 72 6f 20 61 64 64 65 64 20 74 6f s macro added to
1ab32 20 65 76 65 72 79 20 69 6e 73 74 72 75 63 74 69 every instructi
1ab33 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 61 20 6a on that does a j
1ab34 75 6d 70 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a ump in order to.
1ab35 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6c ** implement a l
1ab36 6f 6f 70 2e 20 20 54 68 69 73 20 74 65 73 74 20 oop. This test
1ab37 75 73 65 64 20 74 6f 20 62 65 20 6f 6e 20 65 76 used to be on ev
1ab38 65 72 79 20 73 69 6e 67 6c 65 20 69 6e 73 74 72 ery single instr
1ab39 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 74 uction,.** but t
1ab3a 68 61 74 20 6d 65 61 6e 74 20 77 65 20 6d 6f 72 hat meant we mor
1ab3b 65 20 74 65 73 74 69 6e 67 20 74 68 61 74 20 77 e testing that w
1ab3c 65 20 6e 65 65 64 65 64 2e 20 20 42 79 20 6f 6e e needed. By on
1ab3d 6c 79 20 74 65 73 74 69 6e 67 20 74 68 65 0a 2a ly testing the.*
1ab3e 2a 20 66 6c 61 67 20 6f 6e 20 6a 75 6d 70 20 69 * flag on jump i
1ab3f 6e 73 74 72 75 63 74 69 6f 6e 73 2c 20 77 65 20 nstructions, we
1ab40 67 65 74 20 61 20 28 73 6d 61 6c 6c 29 20 73 70 get a (small) sp
1ab41 65 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e eed improvement.
1ab42 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 .*/.#define CHEC
1ab43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 K_FOR_INTERRUPT
1ab44 5c 0a 20 20 20 69 66 28 20 64 62 2d 3e 75 31 2e \. if( db->u1.
1ab45 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 20 isInterrupted )
1ab46 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1ab47 6f 5f 69 6e 74 65 72 72 75 70 74 3b 0a 0a 23 69 o_interrupt;..#i
1ab48 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1ab49 47 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c G.static int fil
1ab4a 65 45 78 69 73 74 73 28 73 71 6c 69 74 65 33 20 eExists(sqlite3
1ab4b 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *db, const char
1ab4c 2a 7a 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 *zFile){. int r
1ab4d 65 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 es = 0;. int rc
1ab4e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 = SQLITE_OK;.#i
1ab4f 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1ab50 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 . /* If we are
1ab51 63 75 72 72 65 6e 74 6c 79 20 74 65 73 74 69 6e currently testin
1ab52 67 20 49 4f 20 65 72 72 6f 72 73 2c 20 74 68 65 g IO errors, the
1ab53 6e 20 64 6f 20 6e 6f 74 20 63 61 6c 6c 20 4f 73 n do not call Os
1ab54 41 63 63 65 73 73 28 29 20 74 6f 0a 20 20 2a 2a Access() to. **
1ab55 20 74 65 73 74 20 66 6f 72 20 74 68 65 20 70 72 test for the pr
1ab56 65 73 65 6e 63 65 20 6f 66 20 7a 46 69 6c 65 2e esence of zFile.
1ab57 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 This is because
1ab58 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 74 68 any IO error th
1ab59 61 74 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20 68 at. ** occurs h
1ab5a 65 72 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 ere will not be
1ab5b 72 65 70 6f 72 74 65 64 2c 20 63 61 75 73 69 6e reported, causin
1ab5c 67 20 74 68 65 20 74 65 73 74 20 74 6f 20 66 61 g the test to fa
1ab5d 69 6c 2e 0a 20 20 2a 2f 0a 20 20 65 78 74 65 72 il.. */. exter
1ab5e 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f n int sqlite3_io
1ab5f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a _error_pending;.
1ab60 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6f if( sqlite3_io
1ab61 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3c 3d _error_pending<=
1ab62 30 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 0 ).#endif. r
1ab63 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 c = sqlite3OsAcc
1ab64 65 73 73 28 64 62 2d 3e 70 56 66 73 2c 20 7a 46 ess(db->pVfs, zF
1ab65 69 6c 65 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 ile, SQLITE_ACCE
1ab66 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 SS_EXISTS, &res)
1ab67 3b 0a 20 20 72 65 74 75 72 6e 20 28 72 65 73 20 ;. return (res
1ab68 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b && rc==SQLITE_OK
1ab69 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 );.}.#endif..#if
1ab6a 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a ndef NDEBUG./*.*
1ab6b 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1ab6c 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 is only called f
1ab6d 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 61 73 rom within an as
1ab6e 73 65 72 74 28 29 20 65 78 70 72 65 73 73 69 6f sert() expressio
1ab6f 6e 2e 20 49 74 0a 2a 2a 20 63 68 65 63 6b 73 20 n. It.** checks
1ab70 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 that the sqlite3
1ab71 2e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 76 61 .nTransaction va
1ab72 72 69 61 62 6c 65 20 69 73 20 63 6f 72 72 65 63 riable is correc
1ab73 74 6c 79 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 tly set to.** th
1ab74 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d e number of non-
1ab75 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 transaction save
1ab76 70 6f 69 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 points currently
1ab77 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b in the .** link
1ab78 65 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 ed list starting
1ab79 20 61 74 20 73 71 6c 69 74 65 33 2e 70 53 61 76 at sqlite3.pSav
1ab7a 65 70 6f 69 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 epoint..** .** U
1ab7b 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 sage:.**.**
1ab7c 61 73 73 65 72 74 28 20 63 68 65 63 6b 53 61 76 assert( checkSav
1ab7d 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 epointCount(db)
1ab7e 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 );.*/.static int
1ab7f 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 checkSavepointC
1ab80 6f 75 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 ount(sqlite3 *db
1ab81 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a ){. int n = 0;.
1ab82 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 3b 0a Savepoint *p;.
1ab83 20 20 66 6f 72 28 70 3d 64 62 2d 3e 70 53 61 76 for(p=db->pSav
1ab84 65 70 6f 69 6e 74 3b 20 70 3b 20 70 3d 70 2d 3e epoint; p; p=p->
1ab85 70 4e 65 78 74 29 20 6e 2b 2b 3b 0a 20 20 61 73 pNext) n++;. as
1ab86 73 65 72 74 28 20 6e 3d 3d 28 64 62 2d 3e 6e 53 sert( n==(db->nS
1ab87 61 76 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 69 avepoint + db->i
1ab88 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 sTransactionSave
1ab89 70 6f 69 6e 74 29 20 29 3b 0a 20 20 72 65 74 75 point) );. retu
1ab8a 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a rn 1;.}.#endif..
1ab8b 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 73 /*.** Execute as
1ab8c 20 6d 75 63 68 20 6f 66 20 61 20 56 44 42 45 20 much of a VDBE
1ab8d 70 72 6f 67 72 61 6d 20 61 73 20 77 65 20 63 61 program as we ca
1ab8e 6e 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 2a n then return..*
1ab8f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 *.** sqlite3Vdbe
1ab90 4d 61 6b 65 52 65 61 64 79 28 29 20 6d 75 73 74 MakeReady() must
1ab91 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 be called befor
1ab92 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 e this routine i
1ab93 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6c n order to.** cl
1ab94 6f 73 65 20 74 68 65 20 70 72 6f 67 72 61 6d 20 ose the program
1ab95 77 69 74 68 20 61 20 66 69 6e 61 6c 20 4f 50 5f with a final OP_
1ab96 48 61 6c 74 20 61 6e 64 20 74 6f 20 73 65 74 20 Halt and to set
1ab97 75 70 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 up the callbacks
1ab98 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 72 72 6f .** and the erro
1ab99 72 20 6d 65 73 73 61 67 65 20 70 6f 69 6e 74 65 r message pointe
1ab9a 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65 76 65 r..**.** Wheneve
1ab9b 72 20 61 20 72 6f 77 20 6f 72 20 72 65 73 75 6c r a row or resul
1ab9c 74 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 t data is availa
1ab9d 62 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e ble, this routin
1ab9e 65 20 77 69 6c 6c 20 65 69 74 68 65 72 0a 2a 2a e will either.**
1ab9f 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 65 73 75 invoke the resu
1aba0 6c 74 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 lt callback (if
1aba1 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 there is one) or
1aba2 20 72 65 74 75 72 6e 20 77 69 74 68 0a 2a 2a 20 return with.**
1aba3 53 51 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a SQLITE_ROW..**.*
1aba4 2a 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 * If an attempt
1aba5 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 is made to open
1aba6 61 20 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73 a locked databas
1aba7 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 e, then this rou
1aba8 74 69 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65 69 74 tine.** will eit
1aba9 68 65 72 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 her invoke the b
1abaa 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 usy callback (if
1abab 20 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f there is one) o
1abac 72 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 r it will.** ret
1abad 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e urn SQLITE_BUSY.
1abae 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 .**.** If an err
1abaf 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 or occurs, an er
1abb0 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 77 ror message is w
1abb1 72 69 74 74 65 6e 20 74 6f 20 6d 65 6d 6f 72 79 ritten to memory
1abb2 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f obtained.** fro
1abb3 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
1abb4 28 29 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d 73 () and p->zErrMs
1abb5 67 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 g is made to poi
1abb6 6e 74 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f 72 nt to that memor
1abb7 79 2e 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 y..** The error
1abb8 63 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20 69 code is stored i
1abb9 6e 20 70 2d 3e 72 63 20 61 6e 64 20 74 68 69 73 n p->rc and this
1abba 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
1abbb 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a SQLITE_ERROR..*
1abbc 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c *.** If the call
1abbd 62 61 63 6b 20 65 76 65 72 20 72 65 74 75 72 6e back ever return
1abbe 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1abbf 20 74 68 65 20 70 72 6f 67 72 61 6d 20 65 78 69 the program exi
1abc0 74 73 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c ts.** immediatel
1abc1 79 2e 20 20 54 68 65 72 65 20 77 69 6c 6c 20 62 y. There will b
1abc2 65 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61 e no error messa
1abc3 67 65 20 62 75 74 20 74 68 65 20 70 2d 3e 72 63 ge but the p->rc
1abc4 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 field is.** set
1abc5 20 74 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 to SQLITE_ABORT
1abc6 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e and this routin
1abc7 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 e will return SQ
1abc8 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a LITE_ERROR..**.*
1abc9 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 * A memory alloc
1abca 61 74 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73 ation error caus
1abcb 65 73 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73 es p->rc to be s
1abcc 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d et to SQLITE_NOM
1abcd 45 4d 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 EM and this.** r
1abce 6f 75 74 69 6e 65 20 74 6f 20 72 65 74 75 72 6e outine to return
1abcf 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a SQLITE_ERROR..*
1abd0 2a 0a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61 6c *.** Other fatal
1abd1 20 65 72 72 6f 72 73 20 72 65 74 75 72 6e 20 53 errors return S
1abd2 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a QLITE_ERROR..**.
1abd3 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f ** After this ro
1abd4 75 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68 utine has finish
1abd5 65 64 2c 20 73 71 6c 69 74 65 33 56 64 62 65 46 ed, sqlite3VdbeF
1abd6 69 6e 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64 inalize() should
1abd7 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 be.** used to c
1abd8 6c 65 61 6e 20 75 70 20 74 68 65 20 6d 65 73 73 lean up the mess
1abd9 20 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 62 that was left b
1abda 65 68 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ehind..*/.SQLITE
1abdb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1abdc 69 74 65 33 56 64 62 65 45 78 65 63 28 0a 20 20 ite3VdbeExec(.
1abdd 56 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 Vdbe *p
1abde 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1abdf 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 e VDBE */.){. i
1abe0 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 nt pc;
1abe1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1abe2 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 program counter
1abe3 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 */. Op *pOp;
1abe4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1abe5 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72 /* Current oper
1abe6 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 ation */. int r
1abe7 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
1abe8 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 /* Value t
1abe9 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 o return */. sq
1abea 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 lite3 *db = p->d
1abeb 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 b; /* The
1abec 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 database */. u8
1abed 20 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28 encoding = ENC(
1abee 64 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 db); /* The
1abef 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e database encodin
1abf0 67 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 g */. Mem *pIn1
1abf1 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1abf2 20 20 2f 2a 20 31 73 74 20 69 6e 70 75 74 20 6f /* 1st input o
1abf3 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 perand */. Mem
1abf4 2a 70 49 6e 32 20 3d 20 30 3b 20 20 20 20 20 20 *pIn2 = 0;
1abf5 20 20 20 20 20 20 20 2f 2a 20 32 6e 64 20 69 6e /* 2nd in
1abf6 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 put operand */.
1abf7 20 4d 65 6d 20 2a 70 49 6e 33 20 3d 20 30 3b 20 Mem *pIn3 = 0;
1abf8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 /* 3
1abf9 72 64 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 rd input operand
1abfa 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 */. Mem *pOut
1abfb 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
1abfc 20 2f 2a 20 4f 75 74 70 75 74 20 6f 70 65 72 61 /* Output opera
1abfd 6e 64 20 2a 2f 0a 20 20 75 38 20 6f 70 50 72 6f nd */. u8 opPro
1abfe 70 65 72 74 79 3b 0a 20 20 69 6e 74 20 69 43 6f perty;. int iCo
1abff 6d 70 61 72 65 20 3d 20 30 3b 20 20 20 20 20 20 mpare = 0;
1ac00 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 /* Result of
1ac01 20 6c 61 73 74 20 4f 50 5f 43 6f 6d 70 61 72 65 last OP_Compare
1ac02 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 operation */.
1ac03 69 6e 74 20 2a 61 50 65 72 6d 75 74 65 20 3d 20 int *aPermute =
1ac04 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 65 0; /* Pe
1ac05 72 6d 75 74 61 74 69 6f 6e 20 6f 66 20 63 6f 6c rmutation of col
1ac06 75 6d 6e 73 20 66 6f 72 20 4f 50 5f 43 6f 6d 70 umns for OP_Comp
1ac07 61 72 65 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 are */.#ifdef VD
1ac08 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 75 36 34 BE_PROFILE. u64
1ac09 20 73 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 start;
1ac0a 20 20 20 20 20 20 20 20 2f 2a 20 43 50 55 20 63 /* CPU c
1ac0b 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 74 20 73 74 lock count at st
1ac0c 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f art of opcode */
1ac0d 0a 20 20 69 6e 74 20 6f 72 69 67 50 63 3b 20 20 . int origPc;
1ac0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ac0f 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 Program counter
1ac10 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63 at start of opc
1ac11 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ode */.#endif.#i
1ac12 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1ac13 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 T_PROGRESS_CALLB
1ac14 41 43 4b 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72 ACK. int nProgr
1ac15 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20 20 20 essOps = 0;
1ac16 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78 65 63 /* Opcodes exec
1ac17 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f 67 72 uted since progr
1ac18 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f ess callback. */
1ac19 0a 23 65 6e 64 69 66 0a 20 20 55 6e 70 61 63 6b .#endif. Unpack
1ac1a 65 64 52 65 63 6f 72 64 20 61 54 65 6d 70 52 65 edRecord aTempRe
1ac1b 63 5b 31 36 5d 3b 20 2f 2a 20 53 70 61 63 65 20 c[16]; /* Space
1ac1c 74 6f 20 68 6f 6c 64 20 61 20 74 72 61 6e 73 69 to hold a transi
1ac1d 65 6e 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f ent UnpackedReco
1ac1e 72 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 rd */.. assert(
1ac1f 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
1ac20 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20 20 2f 2a MAGIC_RUN ); /*
1ac21 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
1ac22 76 65 72 69 66 69 65 73 20 74 68 69 73 20 2a 2f verifies this */
1ac23 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d . assert( db->m
1ac24 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 agic==SQLITE_MAG
1ac25 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 73 71 6c IC_BUSY );. sql
1ac26 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
1ac27 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 rayEnter(&p->aMu
1ac28 74 65 78 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 tex);. if( p->r
1ac29 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
1ac2a 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 ){. /* This h
1ac2b 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c appens if a mall
1ac2c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 oc() inside a ca
1ac2d 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ll to sqlite3_co
1ac2e 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 lumn_text() or.
1ac2f 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f ** sqlite3_co
1ac30 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 lumn_text16() fa
1ac31 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f iled. */. go
1ac32 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 to no_mem;. }.
1ac33 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d assert( p->rc==
1ac34 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e SQLITE_OK || p->
1ac35 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
1ac36 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c );. p->rc = SQL
1ac37 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 ITE_OK;. assert
1ac38 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 ( p->explain==0
1ac39 29 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 );. p->pResultS
1ac3a 65 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 et = 0;. db->bu
1ac3b 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 syHandler.nBusy
1ac3c 3d 20 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 = 0;. CHECK_FOR
1ac3d 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 _INTERRUPT;. sq
1ac3e 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 lite3VdbeIOTrace
1ac3f 53 71 6c 28 70 29 3b 0a 23 69 66 64 65 66 20 53 Sql(p);.#ifdef S
1ac40 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 QLITE_DEBUG. sq
1ac41 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
1ac42 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 Malloc();. if(
1ac43 70 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 p->pc==0 . &&
1ac44 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 ((p->db->flags &
1ac45 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 SQLITE_VdbeList
1ac46 69 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 ing) || fileExis
1ac47 74 73 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 ts(db, "vdbe_exp
1ac48 6c 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 lain")). ){.
1ac49 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e int i;. prin
1ac4a 74 66 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d tf("VDBE Program
1ac4b 20 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 Listing:\n");.
1ac4c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 sqlite3VdbePr
1ac4d 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 intSql(p);. f
1ac4e 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 or(i=0; i<p->nOp
1ac4f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 ; i++){. sq
1ac50 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 lite3VdbePrintOp
1ac51 28 73 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d 3e (stdout, i, &p->
1ac52 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 aOp[i]);. }.
1ac53 20 7d 0a 20 20 69 66 28 20 66 69 6c 65 45 78 69 }. if( fileExi
1ac54 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f 74 72 sts(db, "vdbe_tr
1ac55 61 63 65 22 29 20 29 7b 0a 20 20 20 20 70 2d 3e ace") ){. p->
1ac56 74 72 61 63 65 20 3d 20 73 74 64 6f 75 74 3b 0a trace = stdout;.
1ac57 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 }. sqlite3End
1ac58 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
1ac59 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 70 63 3d #endif. for(pc=
1ac5a 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49 54 p->pc; rc==SQLIT
1ac5b 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 E_OK; pc++){.
1ac5c 20 61 73 73 65 72 74 28 20 70 63 3e 3d 30 20 26 assert( pc>=0 &
1ac5d 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 & pc<p->nOp );.
1ac5e 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f if( db->mallo
1ac5f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e cFailed ) goto n
1ac60 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56 44 o_mem;.#ifdef VD
1ac61 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 6f BE_PROFILE. o
1ac62 72 69 67 50 63 20 3d 20 70 63 3b 0a 20 20 20 20 rigPc = pc;.
1ac63 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 48 start = sqlite3H
1ac64 77 74 69 6d 65 28 29 3b 0a 23 65 6e 64 69 66 0a wtime();.#endif.
1ac65 20 20 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f pOp = &p->aO
1ac66 70 5b 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a 20 4f p[pc];.. /* O
1ac67 6e 6c 79 20 61 6c 6c 6f 77 20 74 72 61 63 69 6e nly allow tracin
1ac68 67 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 g if SQLITE_DEBU
1ac69 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 G is defined..
1ac6a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 */.#ifdef SQLI
1ac6b 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 TE_DEBUG. if(
1ac6c 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 p->trace ){.
1ac6d 20 20 20 69 66 28 20 70 63 3d 3d 30 20 29 7b 0a if( pc==0 ){.
1ac6e 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 printf("
1ac6f 56 44 42 45 20 45 78 65 63 75 74 69 6f 6e 20 54 VDBE Execution T
1ac70 72 61 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 20 20 race:\n");.
1ac71 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 sqlite3VdbePr
1ac72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 intSql(p);.
1ac73 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1ac74 56 64 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e 74 VdbePrintOp(p->t
1ac75 72 61 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b 0a race, pc, pOp);.
1ac76 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d }. if( p-
1ac77 3e 74 72 61 63 65 3d 3d 30 20 26 26 20 70 63 3d >trace==0 && pc=
1ac78 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
1ac79 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
1ac7a 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 69 66 lloc();. if
1ac7b 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c ( fileExists(db,
1ac7c 20 22 76 64 62 65 5f 73 71 6c 74 72 61 63 65 22 "vdbe_sqltrace"
1ac7d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c ) ){. sql
1ac7e 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c ite3VdbePrintSql
1ac7f 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 (p);. }.
1ac80 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e sqlite3EndBen
1ac81 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 ignMalloc();.
1ac82 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 }.#endif.
1ac83 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 .. /* Check t
1ac84 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 65 64 o see if we need
1ac85 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 to simulate an
1ac86 69 6e 74 65 72 72 75 70 74 2e 20 20 54 68 69 73 interrupt. This
1ac87 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 0a 20 20 only happens.
1ac88 20 20 2a 2a 20 69 66 20 77 65 20 68 61 76 65 20 ** if we have
1ac89 61 20 73 70 65 63 69 61 6c 20 74 65 73 74 20 62 a special test b
1ac8a 75 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 uild.. */.#if
1ac8b 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
1ac8c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f if( sqlite3_
1ac8d 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3e interrupt_count>
1ac8e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1ac8f 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 e3_interrupt_cou
1ac90 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 nt--;. if(
1ac91 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
1ac92 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 20 t_count==0 ){.
1ac93 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e sqlite3_in
1ac94 74 65 72 72 75 70 74 28 64 62 29 3b 0a 20 20 20 terrupt(db);.
1ac95 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
1ac96 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
1ac97 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f E_OMIT_PROGRESS_
1ac98 43 41 4c 4c 42 41 43 4b 0a 20 20 20 20 2f 2a 20 CALLBACK. /*
1ac99 43 61 6c 6c 20 74 68 65 20 70 72 6f 67 72 65 73 Call the progres
1ac9a 73 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 69 74 s callback if it
1ac9b 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 61 is configured a
1ac9c 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20 nd the required
1ac9d 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f 66 number. ** of
1ac9e 20 56 44 42 45 20 6f 70 73 20 68 61 76 65 20 62 VDBE ops have b
1ac9f 65 65 6e 20 65 78 65 63 75 74 65 64 20 28 65 69 een executed (ei
1aca0 74 68 65 72 20 73 69 6e 63 65 20 74 68 69 73 20 ther since this
1aca1 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a 20 20 invocation of.
1aca2 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 ** sqlite3Vdbe
1aca3 45 78 65 63 28 29 20 6f 72 20 73 69 6e 63 65 20 Exec() or since
1aca4 6c 61 73 74 20 74 69 6d 65 20 74 68 65 20 70 72 last time the pr
1aca5 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 ogress callback
1aca6 77 61 73 20 63 61 6c 6c 65 64 29 2e 0a 20 20 20 was called)..
1aca7 20 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 ** If the progr
1aca8 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 ess callback ret
1aca9 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 65 urns non-zero, e
1acaa 78 69 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 xit the virtual
1acab 6d 61 63 68 69 6e 65 20 77 69 74 68 0a 20 20 20 machine with.
1acac 20 2a 2a 20 61 20 72 65 74 75 72 6e 20 63 6f 64 ** a return cod
1acad 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0a e SQLITE_ABORT..
1acae 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 */. if( d
1acaf 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 29 7b 0a b->xProgress ){.
1acb0 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 50 if( db->nP
1acb1 72 6f 67 72 65 73 73 4f 70 73 3d 3d 6e 50 72 6f rogressOps==nPro
1acb2 67 72 65 73 73 4f 70 73 20 29 7b 0a 20 20 20 20 gressOps ){.
1acb3 20 20 20 20 69 6e 74 20 70 72 63 3b 0a 20 20 20 int prc;.
1acb4 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1acb5 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 SafetyOff(db) )
1acb6 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1acb7 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 o_misuse;.
1acb8 20 20 70 72 63 20 3d 64 62 2d 3e 78 50 72 6f 67 prc =db->xProg
1acb9 72 65 73 73 28 64 62 2d 3e 70 50 72 6f 67 72 65 ress(db->pProgre
1acba 73 73 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 ssArg);.
1acbb 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1acbc 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 yOn(db) ) goto a
1acbd 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1acbe 73 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 se;. if(
1acbf 70 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 prc!=0 ){.
1acc0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1acc1 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 20 INTERRUPT;.
1acc2 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 goto vdbe_e
1acc3 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 20 20 rror_halt;.
1acc4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 50 72 }. nPr
1acc5 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a 20 ogressOps = 0;.
1acc6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 50 72 }. nPr
1acc7 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20 20 ogressOps++;.
1acc8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f }.#endif.. /
1acc9 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 * Do common setu
1acca 70 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 p processing for
1accb 20 61 6e 79 20 6f 70 63 6f 64 65 20 74 68 61 74 any opcode that
1accc 20 69 73 20 6d 61 72 6b 65 64 0a 20 20 20 20 2a is marked. *
1accd 2a 20 77 69 74 68 20 74 68 65 20 22 6f 75 74 32 * with the "out2
1acce 2d 70 72 65 72 65 6c 65 61 73 65 22 20 74 61 67 -prerelease" tag
1accf 2e 20 20 53 75 63 68 20 6f 70 63 6f 64 65 73 20 . Such opcodes
1acd0 68 61 76 65 20 61 20 73 69 6e 67 6c 65 0a 20 20 have a single.
1acd1 20 20 2a 2a 20 6f 75 74 70 75 74 20 77 68 69 63 ** output whic
1acd2 68 20 69 73 20 73 70 65 63 69 66 69 65 64 20 62 h is specified b
1acd3 79 20 74 68 65 20 50 32 20 70 61 72 61 6d 65 74 y the P2 paramet
1acd4 65 72 2e 20 20 54 68 65 20 50 32 20 72 65 67 69 er. The P2 regi
1acd5 73 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 69 ster. ** is i
1acd6 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 nitialized to a
1acd7 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 NULL.. */.
1acd8 20 6f 70 50 72 6f 70 65 72 74 79 20 3d 20 6f 70 opProperty = op
1acd9 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 70 4f 70 codeProperty[pOp
1acda 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 69 ->opcode];. i
1acdb 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 f( (opProperty &
1acdc 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 OPFLG_OUT2_PRER
1acdd 45 4c 45 41 53 45 29 21 3d 30 20 29 7b 0a 20 20 ELEASE)!=0 ){.
1acde 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1acdf 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 >p2>0 );. a
1ace0 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d ssert( pOp->p2<=
1ace1 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 p->nMem );.
1ace2 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pOut = &p->aMem
1ace3 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 [pOp->p2];.
1ace4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1ace5 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 eleaseExternal(p
1ace6 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 Out);. pOut
1ace7 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 ->flags = MEM_Nu
1ace8 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 20 0a ll;. }else. .
1ace9 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e /* Do common
1acea 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63 6f 64 setup for opcod
1aceb 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 6f es marked with o
1acec 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
1aced 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d 62 69 ing. ** combi
1acee 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f 70 65 nations of prope
1acef 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 rties.. **.
1acf0 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 ** i
1acf1 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 n1. **
1acf2 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20 20 20 in1 in2.
1acf3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 ** in1
1acf4 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20 2a 2a in2 out3. **
1acf5 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 20 69 in1 i
1acf6 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a n3. **. **
1acf7 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e 31 2c Variables pIn1,
1acf8 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e 33 20 pIn2, and pIn3
1acf9 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e are made to poin
1acfa 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 t to appropriate
1acfb 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 . ** register
1acfc 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20 20 56 s for inputs. V
1acfd 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70 6f 69 ariable pOut poi
1acfe 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74 70 75 nts to the outpu
1acff 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20 20 20 t register..
1ad00 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70 50 72 */. if( (opPr
1ad01 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 operty & OPFLG_I
1ad02 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 N1)!=0 ){.
1ad03 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1ad04 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0 );. asser
1ad05 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e t( pOp->p1<=p->n
1ad06 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49 6e Mem );. pIn
1ad07 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 1 = &p->aMem[pOp
1ad08 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52 45 47 ->p1];. REG
1ad09 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1ad0a 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20 >p1, pIn1);.
1ad0b 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 if( (opPropert
1ad0c 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21 3d y & OPFLG_IN2)!=
1ad0d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 0 ){. ass
1ad0e 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 ert( pOp->p2>0 )
1ad0f 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1ad10 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d ( pOp->p2<=p->nM
1ad11 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 49 em );. pI
1ad12 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f n2 = &p->aMem[pO
1ad13 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 20 p->p2];.
1ad14 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1ad15 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a 20 Op->p2, pIn2);.
1ad16 20 20 20 20 20 20 20 69 66 28 20 28 6f 70 50 72 if( (opPr
1ad17 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f operty & OPFLG_O
1ad18 55 54 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 UT3)!=0 ){.
1ad19 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1ad1a 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 ->p3>0 );.
1ad1b 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1ad1c 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1ad1d 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 20 3d pOut =
1ad1e 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1ad1f 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 3];. }.
1ad20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f }else if( (o
1ad21 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c pProperty & OPFL
1ad22 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 G_IN3)!=0 ){.
1ad23 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1ad24 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 ->p3>0 );.
1ad25 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ad26 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1ad27 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 70 2d pIn3 = &p-
1ad28 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1ad29 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52 REGISTER
1ad2a 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 _TRACE(pOp->p3,
1ad2b 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 pIn3);. }.
1ad2c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 }else if( (op
1ad2d 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 Property & OPFLG
1ad2e 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 _IN2)!=0 ){.
1ad2f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ad30 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 2>0 );. ass
1ad31 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d ert( pOp->p2<=p-
1ad32 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 >nMem );. p
1ad33 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In2 = &p->aMem[p
1ad34 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 52 Op->p2];. R
1ad35 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1ad36 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a 20 20 p->p2, pIn2);.
1ad37 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 50 }else if( (opP
1ad38 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
1ad39 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 IN3)!=0 ){.
1ad3a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1ad3b 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 >0 );. asse
1ad3c 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e rt( pOp->p3<=p->
1ad3d 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49 nMem );. pI
1ad3e 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f n3 = &p->aMem[pO
1ad3f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 52 45 p->p3];. RE
1ad40 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1ad41 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 20 ->p3, pIn3);.
1ad42 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 }.. switch(
1ad43 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 0a pOp->opcode ){..
1ad44 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
1ad45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1ad49 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 * What follows i
1ad4a 73 20 61 20 6d 61 73 73 69 76 65 20 73 77 69 74 s a massive swit
1ad4b 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 ch statement whe
1ad4c 72 65 20 65 61 63 68 20 63 61 73 65 20 69 6d 70 re each case imp
1ad4d 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 70 lements a.** sep
1ad4e 61 72 61 74 65 20 69 6e 73 74 72 75 63 74 69 6f arate instructio
1ad4f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c n in the virtual
1ad50 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20 77 65 machine. If we
1ad51 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 61 follow the usua
1ad52 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f 6e l.** indentation
1ad53 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 61 conventions, ea
1ad54 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64 20 62 ch case should b
1ad55 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20 36 20 e indented by 6
1ad56 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a 20 spaces. But.**
1ad57 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20 6f 66 that is a lot of
1ad58 20 77 61 73 74 65 64 20 73 70 61 63 65 20 6f 6e wasted space on
1ad59 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67 69 6e the left margin
1ad5a 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65 20 77 . So the code w
1ad5b 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 69 ithin.** the swi
1ad5c 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 69 tch statement wi
1ad5d 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20 63 6f ll break with co
1ad5e 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 20 nvention and be
1ad5f 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f 74 flush-left. Anot
1ad60 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d 65 her.** big comme
1ad61 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 74 nt (similar to t
1ad62 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d 61 his one) will ma
1ad63 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 20 rk the point in
1ad64 74 68 65 20 63 6f 64 65 20 77 68 65 72 65 0a 2a the code where.*
1ad65 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 * we transition
1ad66 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 69 back to normal i
1ad67 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a ndentation..**.*
1ad68 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 * The formatting
1ad69 20 6f 66 20 65 61 63 68 20 63 61 73 65 20 69 73 of each case is
1ad6a 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 65 important. The
1ad6b 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 51 makefile for SQ
1ad6c 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 Lite.** generate
1ad6d 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20 22 6f s two C files "o
1ad6e 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 6f pcodes.h" and "o
1ad6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73 63 61 pcodes.c" by sca
1ad70 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 69 nning this.** fi
1ad71 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c le looking for l
1ad72 69 6e 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 ines that begin
1ad73 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f 22 2e with "case OP_".
1ad74 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 20 The opcodes.h
1ad75 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 files.** will be
1ad76 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65 filled with #de
1ad77 66 69 6e 65 73 20 74 68 61 74 20 67 69 76 65 20 fines that give
1ad78 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72 20 76 unique integer v
1ad79 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a 2a 2a alues to each.**
1ad7a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20 opcode and the
1ad7b 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69 opcodes.c file i
1ad7c 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e s filled with an
1ad7d 20 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 array of string
1ad7e 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 s where.** each
1ad7f 73 74 72 69 6e 67 20 69 73 20 74 68 65 20 73 79 string is the sy
1ad80 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20 mbolic name for
1ad81 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1ad82 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68 g opcode. If th
1ad83 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 65 6d e.** case statem
1ad84 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 ent is followed
1ad85 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 20 by a comment of
1ad86 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 6d the form "/# sam
1ad87 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 e as ... #/".**
1ad88 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 20 that comment is
1ad89 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
1ad8a 65 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 e the particular
1ad8b 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 value of the op
1ad8c 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 code..**.** Othe
1ad8d 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 68 r keywords in th
1ad8e 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 66 e comment that f
1ad8f 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61 73 65 ollows each case
1ad90 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 are used to.**
1ad91 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 4f 50 construct the OP
1ad92 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 FLG_INITIALIZER
1ad93 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69 74 69 value that initi
1ad94 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 6f alizes opcodePro
1ad95 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 perty[]..** Keyw
1ad96 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 6e ords include: in
1ad97 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 74 1, in2, in3, out
1ad98 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20 6f 75 2_prerelease, ou
1ad99 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a 2a t2, out3. See.*
1ad9a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e * the mkopcodeh.
1ad9b 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 61 awk script for a
1ad9c 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1ad9d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 ation..**.** Doc
1ad9e 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 umentation about
1ad9f 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 VDBE opcodes is
1ada0 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 generated by sc
1ada1 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 anning this file
1ada2 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 .** for lines of
1ada3 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f that contain "O
1ada4 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c pcode:". That l
1ada5 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 ine and all subs
1ada6 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e equent.** commen
1ada7 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 t lines are used
1ada8 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 in the generati
1ada9 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 on of the opcode
1adaa 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 .html documentat
1adab 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a ion.** file..**.
1adac 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a ** SUMMARY:.**.*
1adad 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 * Formatting
1adae 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f is important to
1adaf 20 73 63 72 69 70 74 73 20 74 68 61 74 20 73 63 scripts that sc
1adb0 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a an this file..**
1adb1 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 Do not devi
1adb2 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 ate from the for
1adb3 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 matting style cu
1adb4 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a rrently in use..
1adb5 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1adb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adba 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 /../* Opcode: G
1adbb 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a oto * P2 * * *.*
1adbc 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 74 *.** An uncondit
1adbd 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 64 ional jump to ad
1adbe 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 65 dress P2..** The
1adbf 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1adc0 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c 6c 20 n executed will
1adc1 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 61 be .** the one a
1adc2 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f 6d 20 t index P2 from
1adc3 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
1adc4 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e .** the program.
1adc5 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 6f .*/.case OP_Goto
1adc6 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1adc7 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 45 /* jump */. CHE
1adc8 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 CK_FOR_INTERRUPT
1adc9 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 ;. pc = pOp->p2
1adca 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d - 1;. break;.}
1adcb 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f ../* Opcode: Go
1adcc 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a sub P1 P2 * * *.
1adcd 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
1adce 63 75 72 72 65 6e 74 20 61 64 64 72 65 73 73 20 current address
1adcf 6f 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 31 onto register P1
1add0 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 6d .** and then jum
1add1 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e p to address P2.
1add2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 75 .*/.case OP_Gosu
1add3 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 b: {
1add4 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 73 73 /* jump */. ass
1add5 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 ert( pOp->p1>0 )
1add6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1add7 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p1<=p->nMem );.
1add8 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 pIn1 = &p->aMe
1add9 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 m[pOp->p1];. as
1adda 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61 sert( (pIn1->fla
1addb 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 gs & MEM_Dyn)==0
1addc 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 );. pIn1->flag
1addd 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 s = MEM_Int;. p
1adde 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 In1->u.i = pc;.
1addf 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1ade0 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a pOp->p1, pIn1);.
1ade1 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1ade2 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 1;. break;.}..
1ade3 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 74 75 /* Opcode: Retu
1ade4 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a rn P1 * * * *.**
1ade5 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 .** Jump to the
1ade6 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e next instruction
1ade7 20 61 66 74 65 72 20 74 68 65 20 61 64 64 72 65 after the addre
1ade8 73 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ss in register P
1ade9 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 1..*/.case OP_Re
1adea 74 75 72 6e 3a 20 7b 20 20 20 20 20 20 20 20 20 turn: {
1adeb 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73 /* in1 */. as
1adec 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1aded 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 s & MEM_Int );.
1adee 20 70 63 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d pc = (int)pIn1-
1adef 3e 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d >u.i;. break;.}
1adf0 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59 69 ../* Opcode: Yi
1adf1 65 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a eld P1 * * * *.*
1adf2 2a 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 70 72 *.** Swap the pr
1adf3 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 77 69 ogram counter wi
1adf4 74 68 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 th the value in
1adf5 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a register P1..*/.
1adf6 63 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20 7b case OP_Yield: {
1adf7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 /* i
1adf8 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 65 n1 */. int pcDe
1adf9 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 st;. assert( (p
1adfa 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1adfb 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49 _Dyn)==0 );. pI
1adfc 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f n1->flags = MEM_
1adfd 49 6e 74 3b 0a 20 20 70 63 44 65 73 74 20 3d 20 Int;. pcDest =
1adfe 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a (int)pIn1->u.i;.
1adff 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 pIn1->u.i = pc
1ae00 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 ;. REGISTER_TRA
1ae01 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 CE(pOp->p1, pIn1
1ae02 29 3b 0a 20 20 70 63 20 3d 20 70 63 44 65 73 74 );. pc = pcDest
1ae03 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1ae04 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 66 Opcode: HaltIf
1ae05 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 33 20 50 Null P1 P2 P3 P
1ae06 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 4 *.**.** Check
1ae07 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1ae08 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 69 73 ister P3. If is
1ae09 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 61 is NULL then Ha
1ae0a 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 61 lt using.** para
1ae0b 6d 65 74 65 72 20 50 31 2c 20 50 32 2c 20 61 6e meter P1, P2, an
1ae0c 64 20 50 34 20 61 73 20 69 66 20 74 68 69 73 20 d P4 as if this
1ae0d 77 65 72 65 20 61 20 48 61 6c 74 20 69 6e 73 74 were a Halt inst
1ae0e 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 ruction. If the
1ae0f 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 .** value in reg
1ae10 69 73 74 65 72 20 50 33 20 69 73 20 6e 6f 74 20 ister P3 is not
1ae11 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 NULL, then this
1ae12 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
1ae13 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 op..*/.case OP_H
1ae14 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 20 altIfNull: {
1ae15 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 69 66 /* in3 */. if
1ae16 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 ( (pIn3->flags &
1ae17 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 20 MEM_Null)==0 )
1ae18 62 72 65 61 6b 3b 0a 20 20 2f 2a 20 46 61 6c 6c break;. /* Fall
1ae19 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 through into OP
1ae1a 5f 48 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f _Halt */.}../* O
1ae1b 70 63 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 20 pcode: Halt P1
1ae1c 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * P4 *.**.**
1ae1d 45 78 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 Exit immediately
1ae1e 2e 20 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73 . All open curs
1ae1f 6f 72 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f ors, etc are clo
1ae20 73 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 sed.** automatic
1ae21 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 ally..**.** P1 i
1ae22 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 s the result cod
1ae23 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 e returned by sq
1ae24 6c 69 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71 lite3_exec(), sq
1ae25 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a lite3_reset(),.*
1ae26 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e * or sqlite3_fin
1ae27 61 6c 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20 alize(). For a
1ae28 6e 6f 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69 normal halt, thi
1ae29 73 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 s should be SQLI
1ae2a 54 45 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f TE_OK (0)..** Fo
1ae2b 72 20 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e r errors, it can
1ae2c 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76 be some other v
1ae2d 61 6c 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20 alue. If P1!=0
1ae2e 74 68 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74 then P2 will det
1ae2f 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 ermine.** whethe
1ae30 72 20 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c r or not to roll
1ae31 62 61 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74 back the current
1ae32 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44 transaction. D
1ae33 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a o not rollback.*
1ae34 2a 20 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c * if P2==OE_Fail
1ae35 2e 20 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 . Do the rollbac
1ae36 6b 20 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c k if P2==OE_Roll
1ae37 62 61 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45 back. If P2==OE
1ae38 5f 41 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20 _Abort,.** then
1ae39 62 61 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61 back out all cha
1ae3a 6e 67 65 73 20 74 68 61 74 20 68 61 76 65 20 6f nges that have o
1ae3b 63 63 75 72 72 65 64 20 64 75 72 69 6e 67 20 74 ccurred during t
1ae3c 68 69 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 his execution of
1ae3d 20 74 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75 the.** VDBE, bu
1ae3e 74 20 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 t do not rollbac
1ae3f 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f k the transactio
1ae40 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 n. .**.** If P4
1ae41 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e is not null then
1ae42 20 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 it is an error
1ae43 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a message string..
1ae44 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 **.** There is a
1ae45 6e 20 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20 n implied "Halt
1ae46 30 20 30 20 30 22 20 69 6e 73 74 72 75 63 74 69 0 0 0" instructi
1ae47 6f 6e 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 on inserted at t
1ae48 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a he very end of.*
1ae49 2a 20 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e * every program.
1ae4a 20 20 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74 So a jump past
1ae4b 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 the last instru
1ae4c 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f ction of the pro
1ae4d 67 72 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73 gram.** is the s
1ae4e 61 6d 65 20 61 73 20 65 78 65 63 75 74 69 6e 67 ame as executing
1ae4f 20 48 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f Halt..*/.case O
1ae50 50 5f 48 61 6c 74 3a 20 7b 0a 20 20 70 2d 3e 72 P_Halt: {. p->r
1ae51 63 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 c = pOp->p1;. p
1ae52 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 70 2d 3e ->pc = pc;. p->
1ae53 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 70 4f errorAction = pO
1ae54 70 2d 3e 70 32 3b 0a 20 20 69 66 28 20 70 4f 70 p->p2;. if( pOp
1ae55 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 ->p4.z ){. sq
1ae56 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1ae57 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1ae58 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 "%s", pOp->p4.z)
1ae59 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c ;. }. rc = sql
1ae5a 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b ite3VdbeHalt(p);
1ae5b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
1ae5c 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 QLITE_BUSY || rc
1ae5d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 ==SQLITE_OK );.
1ae5e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1ae5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72 BUSY ){. p->r
1ae60 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f c = rc = SQLITE_
1ae61 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 BUSY;. }else{.
1ae62 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 rc = p->rc ?
1ae63 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 SQLITE_ERROR : S
1ae64 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a QLITE_DONE;. }.
1ae65 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 goto vdbe_retu
1ae66 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 rn;.}../* Opcode
1ae67 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 : Integer P1 P2
1ae68 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 * * *.**.** The
1ae69 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 32-bit integer v
1ae6a 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 74 alue P1 is writt
1ae6b 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 en into register
1ae6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P2..*/.case OP_
1ae6d 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 Integer: {
1ae6e 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1ae6f 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d lease */. pOut-
1ae70 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1ae71 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 ;. pOut->u.i =
1ae72 70 4f 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b pOp->p1;. break
1ae73 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1ae74 49 6e 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20 Int64 * P2 * P4
1ae75 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 *.**.** P4 is a
1ae76 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d pointer to a 64-
1ae77 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 bit integer valu
1ae78 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 e..** Write that
1ae79 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 value into regi
1ae7a 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 ster P2..*/.case
1ae7b 20 4f 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 OP_Int64: {
1ae7c 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1ae7d 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 rerelease */. a
1ae7e 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 ssert( pOp->p4.p
1ae7f 49 36 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 I64!=0 );. pOut
1ae80 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1ae81 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d t;. pOut->u.i =
1ae82 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a *pOp->p4.pI64;.
1ae83 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1ae84 70 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 pcode: Real * P2
1ae85 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 * P4 *.**.** P4
1ae86 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1ae87 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 a 64-bit floati
1ae88 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a ng point value..
1ae89 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61 ** Write that va
1ae8a 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 lue into registe
1ae8b 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P2..*/.case OP
1ae8c 5f 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 _Real: {
1ae8d 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1ae8e 4b 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 K_FLOAT, out2-pr
1ae8f 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f erelease */. pO
1ae90 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
1ae91 52 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 Real;. assert(
1ae92 21 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 !sqlite3IsNaN(*p
1ae93 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b Op->p4.pReal) );
1ae94 0a 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f . pOut->r = *pO
1ae95 70 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 p->p4.pReal;. b
1ae96 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1ae97 64 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 de: String8 * P2
1ae98 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 * P4 *.**.** P4
1ae99 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c points to a nul
1ae9a 20 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d terminated UTF-
1ae9b 38 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 8 string. This o
1ae9c 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f pcode is transfo
1ae9d 72 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e rmed .** into an
1ae9e 20 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 OP_String befor
1ae9f 65 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 e it is executed
1aea0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 for the first t
1aea1 69 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ime..*/.case OP_
1aea2 53 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 String8: {
1aea3 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1aea4 5f 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 _STRING, out2-pr
1aea5 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 erelease */. as
1aea6 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 sert( pOp->p4.z!
1aea7 3d 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 =0 );. pOp->opc
1aea8 6f 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b ode = OP_String;
1aea9 0a 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c . pOp->p1 = sql
1aeaa 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 ite3Strlen30(pOp
1aeab 2d 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 ->p4.z);..#ifnde
1aeac 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
1aead 46 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 F16. if( encodi
1aeae 6e 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 ng!=SQLITE_UTF8
1aeaf 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1aeb0 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 beMemSetStr(pOut
1aeb1 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31 2c , pOp->p4.z, -1,
1aeb2 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
1aeb3 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
1aeb4 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
1aeb5 3d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e =sqlite3VdbeChan
1aeb6 67 65 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c geEncoding(pOut,
1aeb7 20 65 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 encoding) ) got
1aeb8 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 o no_mem;. if
1aeb9 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c ( SQLITE_OK!=sql
1aeba 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 ite3VdbeMemMakeW
1aebb 72 69 74 65 61 62 6c 65 28 70 4f 75 74 29 20 29 riteable(pOut) )
1aebc 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1aebd 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 pOut->zMalloc
1aebe 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 = 0;. pOut->f
1aebf 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 61 74 lags |= MEM_Stat
1aec0 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c ic;. pOut->fl
1aec1 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b ags &= ~MEM_Dyn;
1aec2 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 . if( pOp->p4
1aec3 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41 4d 49 43 type==P4_DYNAMIC
1aec4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1aec5 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3DbFree(db, pOp-
1aec6 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 >p4.z);. }.
1aec7 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
1aec8 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 20 20 P4_DYNAMIC;.
1aec9 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75 74 pOp->p4.z = pOut
1aeca 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 31 ->z;. pOp->p1
1aecb 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 20 20 = pOut->n;.
1aecc 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e if( pOp->p1>db->
1aecd 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1aece 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
1aecf 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 goto too_bi
1aed0 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 55 50 44 g;. }. UPD
1aed1 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1aed2 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 (pOut);. brea
1aed3 6b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 k;. }.#endif.
1aed4 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e if( pOp->p1>db->
1aed5 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1aed6 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
1aed7 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1aed8 0a 20 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 . }. /* Fall t
1aed9 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 hrough to the ne
1aeda 78 74 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 xt case, OP_Stri
1aedb 6e 67 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 ng */.}. ./* Op
1aedc 63 6f 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20 code: String P1
1aedd 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * P4 *.**.**
1aede 54 68 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 The string value
1aedf 20 50 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 P4 of length P1
1aee0 20 28 62 79 74 65 73 29 20 69 73 20 73 74 6f 72 (bytes) is stor
1aee1 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1aee2 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 2..*/.case OP_St
1aee3 72 69 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20 ring: {
1aee4 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1aee5 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ase */. assert(
1aee6 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b pOp->p4.z!=0 );
1aee7 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d . pOut->flags =
1aee8 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 MEM_Str|MEM_Sta
1aee9 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 tic|MEM_Term;.
1aeea 70 4f 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 pOut->z = pOp->p
1aeeb 34 2e 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 4.z;. pOut->n =
1aeec 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 pOp->p1;. pOut
1aeed 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 ->enc = encoding
1aeee 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 ;. UPDATE_MAX_B
1aeef 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 LOBSIZE(pOut);.
1aef0 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1aef1 63 6f 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 code: Null * P2
1aef2 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 * * *.**.** Writ
1aef3 65 20 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 e a NULL into re
1aef4 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 gister P2..*/.ca
1aef5 73 65 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 se OP_Null: {
1aef6 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1aef7 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1aef8 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 break;.}.../* Op
1aef9 63 6f 64 65 3a 20 42 6c 6f 62 20 50 31 20 50 32 code: Blob P1 P2
1aefa 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70 * P4.**.** P4 p
1aefb 6f 69 6e 74 73 20 74 6f 20 61 20 62 6c 6f 62 20 oints to a blob
1aefc 6f 66 20 64 61 74 61 20 50 31 20 62 79 74 65 73 of data P1 bytes
1aefd 20 6c 6f 6e 67 2e 20 20 53 74 6f 72 65 20 74 68 long. Store th
1aefe 69 73 0a 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65 is.** blob in re
1aeff 67 69 73 74 65 72 20 50 32 2e 20 54 68 69 73 20 gister P2. This
1af00 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6e instruction is n
1af01 6f 74 20 63 6f 64 65 64 20 64 69 72 65 63 74 6c ot coded directl
1af02 79 0a 2a 2a 20 62 79 20 74 68 65 20 63 6f 6d 70 y.** by the comp
1af03 69 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 74 iler. Instead, t
1af04 68 65 20 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65 he compiler laye
1af05 72 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 r specifies.** a
1af06 6e 20 4f 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63 n OP_HexBlob opc
1af07 6f 64 65 2c 20 77 69 74 68 20 74 68 65 20 68 65 ode, with the he
1af08 78 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 x string represe
1af09 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 ntation of.** th
1af0a 65 20 62 6c 6f 62 20 61 73 20 50 34 2e 20 54 68 e blob as P4. Th
1af0b 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72 61 is opcode is tra
1af0c 6e 73 66 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f nsformed to an O
1af0d 50 5f 42 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69 P_Blob.** the fi
1af0e 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 rst time it is e
1af0f 78 65 63 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 xecuted..*/.case
1af10 20 4f 50 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 OP_Blob: {
1af11 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 /* ou
1af12 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
1af13 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1af14 70 31 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 p1 <= SQLITE_MAX
1af15 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c _LENGTH );. sql
1af16 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
1af17 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e r(pOut, pOp->p4.
1af18 7a 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 z, pOp->p1, 0, 0
1af19 29 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d );. pOut->enc =
1af1a 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 encoding;. UPD
1af1b 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1af1c 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b (pOut);. break;
1af1d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 .}../* Opcode: V
1af1e 61 72 69 61 62 6c 65 20 50 31 20 50 32 20 50 33 ariable P1 P2 P3
1af1f 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 61 6e P4 *.**.** Tran
1af20 73 66 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 sfer the values
1af21 6f 66 20 62 6f 75 6e 64 20 70 61 72 61 6d 65 74 of bound paramet
1af22 65 72 73 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 ers P1..P1+P3-1
1af23 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a into registers.*
1af24 2a 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 0a 2a * P2..P2+P3-1..*
1af25 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61 *.** If the para
1af26 6d 65 74 65 72 20 69 73 20 6e 61 6d 65 64 2c 20 meter is named,
1af27 74 68 65 6e 20 69 74 73 20 6e 61 6d 65 20 61 70 then its name ap
1af28 70 65 61 72 73 20 69 6e 20 50 34 20 61 6e 64 20 pears in P4 and
1af29 50 33 3d 3d 31 2e 0a 2a 2a 20 54 68 65 20 50 34 P3==1..** The P4
1af2a 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 62 value is used b
1af2b 79 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 y sqlite3_bind_p
1af2c 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 2e arameter_name().
1af2d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 61 72 69 .*/.case OP_Vari
1af2e 61 62 6c 65 3a 20 7b 0a 20 20 69 6e 74 20 6a 20 able: {. int j
1af2f 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 = pOp->p1 - 1;.
1af30 20 69 6e 74 20 6b 20 3d 20 70 4f 70 2d 3e 70 32 int k = pOp->p2
1af31 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 ;. Mem *pVar;.
1af32 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 int n = pOp->p3
1af33 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 ;. assert( j>=0
1af34 20 26 26 20 6a 2b 6e 3c 3d 70 2d 3e 6e 56 61 72 && j+n<=p->nVar
1af35 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b 3e );. assert( k>
1af36 3d 31 20 26 26 20 6b 2b 6e 2d 31 3c 3d 70 2d 3e =1 && k+n-1<=p->
1af37 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 nMem );. assert
1af38 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c ( pOp->p4.z==0 |
1af39 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 29 3b 0a | pOp->p3==1 );.
1af3a 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e 20 . while( n-- >
1af3b 30 20 29 7b 0a 20 20 20 20 70 56 61 72 20 3d 20 0 ){. pVar =
1af3c 26 70 2d 3e 61 56 61 72 5b 6a 2b 2b 5d 3b 0a 20 &p->aVar[j++];.
1af3d 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
1af3e 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 56 61 72 beMemTooBig(pVar
1af3f 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ) ){. goto
1af40 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 too_big;. }.
1af41 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d pOut = &p->aM
1af42 65 6d 5b 6b 2b 2b 5d 3b 0a 20 20 20 20 73 71 6c em[k++];. sql
1af43 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1af44 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 seExternal(pOut)
1af45 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 ;. pOut->flag
1af46 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1af47 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1af48 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 ShallowCopy(pOut
1af49 2c 20 70 56 61 72 2c 20 4d 45 4d 5f 53 74 61 74 , pVar, MEM_Stat
1af4a 69 63 29 3b 0a 20 20 20 20 55 50 44 41 54 45 5f ic);. UPDATE_
1af4b 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 MAX_BLOBSIZE(pOu
1af4c 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b t);. }. break;
1af4d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d .}../* Opcode: M
1af4e 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a ove P1 P2 P3 * *
1af4f 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 .**.** Move the
1af50 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 values in regist
1af51 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 6f er P1..P1+P3-1 o
1af52 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 ver into.** regi
1af53 73 74 65 72 73 20 50 32 2e 2e 50 32 2b 50 33 2d sters P2..P2+P3-
1af54 31 2e 20 20 52 65 67 69 73 74 65 72 73 20 50 31 1. Registers P1
1af55 2e 2e 50 31 2b 50 31 2d 31 20 61 72 65 0a 2a 2a ..P1+P1-1 are.**
1af56 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 20 left holding a
1af57 4e 55 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e 20 NULL. It is an
1af58 65 72 72 6f 72 20 66 6f 72 20 72 65 67 69 73 74 error for regist
1af59 65 72 20 72 61 6e 67 65 73 0a 2a 2a 20 50 31 2e er ranges.** P1.
1af5a 2e 50 31 2b 50 33 2d 31 20 61 6e 64 20 50 32 2e .P1+P3-1 and P2.
1af5b 2e 50 32 2b 50 33 2d 31 20 74 6f 20 6f 76 65 72 .P2+P3-1 to over
1af5c 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f lap..*/.case OP_
1af5d 4d 6f 76 65 3a 20 7b 0a 20 20 63 68 61 72 20 2a Move: {. char *
1af5e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20 6e zMalloc;. int n
1af5f 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e = pOp->p3;. in
1af60 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a t p1 = pOp->p1;.
1af61 20 20 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e int p2 = pOp->
1af62 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e p2;. assert( n>
1af63 30 20 26 26 20 70 31 3e 30 20 26 26 20 70 32 3e 0 && p1>0 && p2>
1af64 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1af65 31 2b 6e 3c 3d 70 32 20 7c 7c 20 70 32 2b 6e 3c 1+n<=p2 || p2+n<
1af66 3d 70 31 20 29 3b 0a 0a 20 20 70 49 6e 31 20 3d =p1 );.. pIn1 =
1af67 20 26 70 2d 3e 61 4d 65 6d 5b 70 31 5d 3b 0a 20 &p->aMem[p1];.
1af68 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pOut = &p->aMem
1af69 5b 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 6e [p2];. while( n
1af6a 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 -- ){. assert
1af6b 28 20 70 4f 75 74 3c 3d 26 70 2d 3e 61 4d 65 6d ( pOut<=&p->aMem
1af6c 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 [p->nMem] );.
1af6d 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d 26 assert( pIn1<=&
1af6e 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d p->aMem[p->nMem]
1af6f 20 29 3b 0a 20 20 20 20 7a 4d 61 6c 6c 6f 63 20 );. zMalloc
1af70 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b = pOut->zMalloc;
1af71 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c . pOut->zMall
1af72 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 oc = 0;. sqli
1af73 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 te3VdbeMemMove(p
1af74 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20 Out, pIn1);.
1af75 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 pIn1->zMalloc =
1af76 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 47 zMalloc;. REG
1af77 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b 2b ISTER_TRACE(p2++
1af78 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e , pOut);. pIn
1af79 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 1++;. pOut++;
1af7a 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1af7b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 ./* Opcode: Copy
1af7c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1af7d 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f ** Make a copy o
1af7e 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 6e f register P1 in
1af7f 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1af80 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 **.** This instr
1af81 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 uction makes a d
1af82 65 65 70 20 63 6f 70 79 20 6f 66 20 74 68 65 20 eep copy of the
1af83 76 61 6c 75 65 2e 20 20 41 20 64 75 70 6c 69 63 value. A duplic
1af84 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f ate.** is made o
1af85 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 f any string or
1af86 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 blob constant.
1af87 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 See also OP_SCop
1af88 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f y..*/.case OP_Co
1af89 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 py: {
1af8a 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73 /* in1 */. as
1af8b 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1af8c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1af8d 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p2<=p->nMem );
1af8e 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1af8f 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 em[pOp->p2];. a
1af90 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e ssert( pOut!=pIn
1af91 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 1 );. sqlite3Vd
1af92 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
1af93 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d (pOut, pIn1, MEM
1af94 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 70 68 _Ephem);. Deeph
1af95 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29 3b emeralize(pOut);
1af96 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1af97 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 E(pOp->p2, pOut)
1af98 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1af99 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 20 50 Opcode: SCopy P
1af9a 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1af9b 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20 Make a shallow
1af9c 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65 72 copy of register
1af9d 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74 65 P1 into registe
1af9e 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 r P2..**.** This
1af9f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b instruction mak
1afa0 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 es a shallow cop
1afa1 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 y of the value.
1afa2 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a If the value.**
1afa3 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 is a string or
1afa4 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20 63 blob, then the c
1afa5 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f opy is only a po
1afa6 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 inter to the.**
1afa7 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e original and hen
1afa8 63 65 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e ce if the origin
1afa9 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77 69 al changes so wi
1afaa 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20 ll the copy..**
1afab 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f 72 Worse, if the or
1afac 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f iginal is deallo
1afad 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79 20 cated, the copy
1afae 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e becomes invalid.
1afaf 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70 72 6f .** Thus the pro
1afb0 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72 61 6e gram must guaran
1afb1 74 65 65 20 74 68 61 74 20 74 68 65 20 6f 72 69 tee that the ori
1afb2 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 ginal will not c
1afb3 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20 hange.** during
1afb4 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 the lifetime of
1afb5 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65 20 4f the copy. Use O
1afb6 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61 P_Copy to make a
1afb7 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70 complete.** cop
1afb8 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43 y..*/.case OP_SC
1afb9 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 opy: {
1afba 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 52 45 /* in1 */. RE
1afbb 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1afbc 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61 ->p1, pIn1);. a
1afbd 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 ssert( pOp->p2>0
1afbe 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1afbf 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p2<=p->nMem )
1afc0 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 ;. pOut = &p->a
1afc1 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1afc2 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 assert( pOut!=pI
1afc3 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 n1 );. sqlite3V
1afc4 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
1afc5 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 y(pOut, pIn1, ME
1afc6 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47 49 M_Ephem);. REGI
1afc7 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1afc8 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 p2, pOut);. bre
1afc9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1afca 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 31 20 50 : ResultRow P1 P
1afcb 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 2 * * *.**.** Th
1afcc 65 20 72 65 67 69 73 74 65 72 73 20 50 31 20 74 e registers P1 t
1afcd 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 20 63 hrough P1+P2-1 c
1afce 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 20 ontain a single
1afcf 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74 row of.** result
1afd0 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 63 s. This opcode c
1afd1 61 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 auses the sqlite
1afd2 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74 6f 3_step() call to
1afd3 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69 terminate.** wi
1afd4 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57 th an SQLITE_ROW
1afd5 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 return code and
1afd6 20 69 74 20 73 65 74 73 20 75 70 20 74 68 65 20 it sets up the
1afd7 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20 sqlite3_stmt.**
1afd8 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 72 6f structure to pro
1afd9 76 69 64 65 20 61 63 63 65 73 73 20 74 6f 20 74 vide access to t
1afda 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65 73 he top P1 values
1afdb 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a as the result.*
1afdc 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f * row..*/.case O
1afdd 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 20 P_ResultRow: {.
1afde 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e Mem *pMem;. in
1afdf 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t i;. assert( p
1afe0 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f ->nResColumn==pO
1afe1 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65 72 p->p2 );. asser
1afe2 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a t( pOp->p1>0 );.
1afe3 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1afe4 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 1+pOp->p2<=p->nM
1afe5 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 em+1 );.. /* If
1afe6 20 74 68 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e the SQLITE_Coun
1afe7 74 52 6f 77 73 20 66 6c 61 67 20 69 73 20 73 65 tRows flag is se
1afe8 74 20 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61 t in sqlite3.fla
1afe9 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 0a 20 gs mask, then .
1afea 20 2a 2a 20 44 4d 4c 20 73 74 61 74 65 6d 65 6e ** DML statemen
1afeb 74 73 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f ts invoke this o
1afec 70 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e 20 pcode to return
1afed 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f the number of ro
1afee 77 73 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 ws . ** modifie
1afef 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 54 d to the user. T
1aff0 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 his is the only
1aff1 77 61 79 20 74 68 61 74 20 61 20 56 4d 20 74 68 way that a VM th
1aff2 61 74 0a 20 20 2a 2a 20 6f 70 65 6e 73 20 61 20 at. ** opens a
1aff3 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1aff4 63 74 69 6f 6e 20 6d 61 79 20 69 6e 76 6f 6b 65 ction may invoke
1aff5 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 20 20 this opcode..
1aff6 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 61 73 65 20 **. ** In case
1aff7 74 68 69 73 20 69 73 20 73 75 63 68 20 61 20 73 this is such a s
1aff8 74 61 74 65 6d 65 6e 74 2c 20 63 6c 6f 73 65 20 tatement, close
1aff9 61 6e 79 20 73 74 61 74 65 6d 65 6e 74 20 74 72 any statement tr
1affa 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f ansaction. ** o
1affb 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 56 4d pened by this VM
1affc 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
1affd 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 g control to the
1affe 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20 74 user. This is t
1afff 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 65 20 74 68 o. ** ensure th
1b000 61 74 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 at statement-tra
1b001 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 61 6c nsactions are al
1b002 77 61 79 73 20 6e 65 73 74 65 64 2c 20 6e 6f 74 ways nested, not
1b003 20 6f 76 65 72 6c 61 70 70 69 6e 67 2e 0a 20 20 overlapping..
1b004 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 6e 20 73 ** If the open s
1b005 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 tatement-transac
1b006 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6c 6f 73 tion is not clos
1b007 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 74 68 ed here, then th
1b008 65 20 75 73 65 72 0a 20 20 2a 2a 20 6d 61 79 20 e user. ** may
1b009 73 74 65 70 20 61 6e 6f 74 68 65 72 20 56 4d 20 step another VM
1b00a 74 68 61 74 20 6f 70 65 6e 73 20 69 74 73 20 6f that opens its o
1b00b 77 6e 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 wn statement tra
1b00c 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 0a 20 nsaction. This.
1b00d 20 2a 2a 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 ** may lead to
1b00e 6f 76 65 72 6c 61 70 70 69 6e 67 20 73 74 61 74 overlapping stat
1b00f 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1b010 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ns.. */. asser
1b011 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 t( p->iStatement
1b012 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 6c 61 67 73 ==0 || db->flags
1b013 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 &SQLITE_CountRow
1b014 73 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 s );. if( SQLIT
1b015 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK!=(rc = sqli
1b016 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 te3VdbeCloseStat
1b017 65 6d 65 6e 74 28 70 2c 20 53 41 56 45 50 4f 49 ement(p, SAVEPOI
1b018 4e 54 5f 52 45 4c 45 41 53 45 29 29 20 29 7b 0a NT_RELEASE)) ){.
1b019 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a break;. }..
1b01a 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 /* Invalidate
1b01b 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75 all ephemeral cu
1b01c 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20 rsor row caches
1b01d 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 */. p->cacheCtr
1b01e 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20 = (p->cacheCtr
1b01f 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 + 2)|1;.. /* Ma
1b020 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75 ke sure the resu
1b021 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 lts of the curre
1b022 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20 nt row are \000
1b023 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 terminated. **
1b024 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69 and have an assi
1b025 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20 gned type. The
1b026 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65 results are de-e
1b027 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a phemeralized as.
1b028 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66 ** as side eff
1b029 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 70 4d 65 6d ect.. */. pMem
1b02a 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 = p->pResultSet
1b02b 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1b02c 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b >p1];. for(i=0;
1b02d 20 69 3c 70 4f 70 2d 3e 70 32 3b 20 69 2b 2b 29 i<pOp->p2; i++)
1b02e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1b02f 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 eMemNulTerminate
1b030 28 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 20 20 (&pMem[i]);.
1b031 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 26 70 storeTypeInfo(&p
1b032 4d 65 6d 5b 69 5d 2c 20 65 6e 63 6f 64 69 6e 67 Mem[i], encoding
1b033 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f );. REGISTER_
1b034 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 69 2c TRACE(pOp->p1+i,
1b035 20 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 7d 0a &pMem[i]);. }.
1b036 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
1b037 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f Failed ) goto no
1b038 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 _mem;.. /* Retu
1b039 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 20 rn SQLITE_ROW.
1b03a 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 20 */. p->pc = pc
1b03b 2b 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 + 1;. rc = SQLI
1b03c 54 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 76 TE_ROW;. goto v
1b03d 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f dbe_return;.}../
1b03e 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 74 * Opcode: Concat
1b03f 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1b040 0a 2a 2a 20 41 64 64 20 74 68 65 20 74 65 78 74 .** Add the text
1b041 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1b042 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 onto the end of
1b043 74 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 72 the text in.** r
1b044 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 egister P2 and s
1b045 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1b046 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a in register P3..
1b047 2a 2a 20 49 66 20 65 69 74 68 65 72 20 74 68 65 ** If either the
1b048 20 50 31 20 6f 72 20 50 32 20 74 65 78 74 20 61 P1 or P2 text a
1b049 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f re NULL then sto
1b04a 72 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a re NULL in P3..*
1b04b 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 7c *.** P3 = P2 |
1b04c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 | P1.**.** It is
1b04d 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 20 illegal for P1
1b04e 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68 65 and P3 to be the
1b04f 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e 20 same register.
1b050 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 66 Sometimes,.** if
1b051 20 50 33 20 69 73 20 74 68 65 20 73 61 6d 65 20 P3 is the same
1b052 72 65 67 69 73 74 65 72 20 61 73 20 50 32 2c 20 register as P2,
1b053 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
1b054 6f 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 6f on is able.** to
1b055 20 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 28 avoid a memcpy(
1b056 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f )..*/.case OP_Co
1b057 6e 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20 20 ncat: {
1b058 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1b059 43 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e 32 CONCAT, in1, in2
1b05a 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 36 34 20 , out3 */. i64
1b05b 6e 42 79 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 nByte;.. assert
1b05c 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b 0a ( pIn1!=pOut );.
1b05d 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1b05e 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 gs | pIn2->flags
1b05f 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a ) & MEM_Null ){.
1b060 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1b061 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b emSetNull(pOut);
1b062 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1b063 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e ExpandBlob(pIn
1b064 31 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 1);. Stringify(
1b065 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b pIn1, encoding);
1b066 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 . ExpandBlob(pI
1b067 6e 32 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 n2);. Stringify
1b068 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e 67 29 (pIn2, encoding)
1b069 3b 0a 20 20 6e 42 79 74 65 20 3d 20 70 49 6e 31 ;. nByte = pIn1
1b06a 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 ->n + pIn2->n;.
1b06b 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 if( nByte>db->a
1b06c 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1b06d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1b06e 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1b06f 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 }. MemSetType
1b070 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 53 Flag(pOut, MEM_S
1b071 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 tr);. if( sqlit
1b072 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f e3VdbeMemGrow(pO
1b073 75 74 2c 20 28 69 6e 74 29 6e 42 79 74 65 2b 32 ut, (int)nByte+2
1b074 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29 7b , pOut==pIn2) ){
1b075 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d . goto no_mem
1b076 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 ;. }. if( pOut
1b077 21 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d 65 !=pIn2 ){. me
1b078 6d 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70 49 mcpy(pOut->z, pI
1b079 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29 3b n2->z, pIn2->n);
1b07a 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 . }. memcpy(&p
1b07b 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d 2c Out->z[pIn2->n],
1b07c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d 3e pIn1->z, pIn1->
1b07d 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e 42 n);. pOut->z[nB
1b07e 79 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 yte] = 0;. pOut
1b07f 2d 3e 7a 5b 6e 42 79 74 65 2b 31 5d 20 3d 20 30 ->z[nByte+1] = 0
1b080 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 ;. pOut->flags
1b081 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 |= MEM_Term;. p
1b082 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 6e 42 Out->n = (int)nB
1b083 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 yte;. pOut->enc
1b084 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 = encoding;. U
1b085 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1b086 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 ZE(pOut);. brea
1b087 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1b088 20 41 64 64 20 50 31 20 50 32 20 50 33 20 2a 20 Add P1 P2 P3 *
1b089 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 *.**.** Add the
1b08a 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1b08b 72 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75 r P1 to the valu
1b08c 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 e in register P2
1b08d 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 .** and store th
1b08e 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
1b08f 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 ster P3..** If e
1b090 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e ither input is N
1b091 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 ULL, the result
1b092 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f is NULL..*/./* O
1b093 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20 pcode: Multiply
1b094 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1b095 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74 **.** Multiply t
1b096 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1b097 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 ster P1 by the v
1b098 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1b099 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 P2.** and store
1b09a 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1b09b 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1b09c 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1b09d 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1b09e 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1b09f 2a 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61 * Opcode: Subtra
1b0a0 63 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a ct P1 P2 P3 * *.
1b0a1 2a 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74 **.** Subtract t
1b0a2 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1b0a3 73 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65 ster P1 from the
1b0a4 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1b0a5 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f er P2.** and sto
1b0a6 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1b0a7 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
1b0a8 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 If either input
1b0a9 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
1b0aa 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1b0ab 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69 ./* Opcode: Divi
1b0ac 64 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a de P1 P2 P3 * *.
1b0ad 2a 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65 **.** Divide the
1b0ae 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1b0af 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c er P1 by the val
1b0b0 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1b0b1 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 2.** and store t
1b0b2 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 he result in reg
1b0b3 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 74 68 ister P3. If th
1b0b4 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1b0b5 74 65 72 20 50 32 0a 2a 2a 20 69 73 20 7a 65 72 ter P2.** is zer
1b0b6 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 o, then the resu
1b0b7 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 lt is NULL..** I
1b0b8 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1b0b9 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1b0ba 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1b0bb 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69 6e * Opcode: Remain
1b0bc 64 65 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a der P1 P2 P3 * *
1b0bd 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 .**.** Compute t
1b0be 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61 66 74 he remainder aft
1b0bf 65 72 20 69 6e 74 65 67 65 72 20 64 69 76 69 73 er integer divis
1b0c0 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ion of the value
1b0c1 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 in.** register
1b0c2 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 P1 by the value
1b0c3 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61 in register P2 a
1b0c4 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1b0c5 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a 20 49 ult in P3. .** I
1b0c6 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 f the value in r
1b0c7 65 67 69 73 74 65 72 20 50 32 20 69 73 20 7a 65 egister P2 is ze
1b0c8 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 ro the result is
1b0c9 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 NULL..** If eit
1b0ca 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e her operand is N
1b0cb 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 ULL, the result
1b0cc 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 is NULL..*/.case
1b0cd 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 OP_Add:
1b0ce 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1b0cf 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c 20 69 me as TK_PLUS, i
1b0d0 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1b0d1 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 .case OP_Subtrac
1b0d2 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t:
1b0d3 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 /* same as TK_MI
1b0d4 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f NUS, in1, in2, o
1b0d5 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d ut3 */.case OP_M
1b0d6 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 20 20 ultiply:
1b0d7 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1b0d8 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c 20 69 TK_STAR, in1, i
1b0d9 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1b0da 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 20 20 OP_Divide:
1b0db 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1b0dc 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 2c 20 me as TK_SLASH,
1b0dd 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1b0de 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 69 6e /.case OP_Remain
1b0df 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20 der: {
1b0e0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 /* same as TK_R
1b0e1 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 EM, in1, in2, ou
1b0e2 74 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 t3 */. int flag
1b0e3 73 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 s;. applyNumeri
1b0e4 63 41 66 66 69 6e 69 74 79 28 70 49 6e 31 29 3b cAffinity(pIn1);
1b0e5 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 . applyNumericA
1b0e6 66 66 69 6e 69 74 79 28 70 49 6e 32 29 3b 0a 20 ffinity(pIn2);.
1b0e7 20 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66 flags = pIn1->f
1b0e8 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 lags | pIn2->fla
1b0e9 67 73 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 73 gs;. if( (flags
1b0ea 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 & MEM_Null)!=0
1b0eb 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 ) goto arithmeti
1b0ec 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c c_result_is_null
1b0ed 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 ;. if( (pIn1->f
1b0ee 6c 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c 61 lags & pIn2->fla
1b0ef 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d gs & MEM_Int)==M
1b0f0 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 69 36 EM_Int ){. i6
1b0f1 34 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20 4 a, b;. a =
1b0f2 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 62 pIn1->u.i;. b
1b0f3 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 = pIn2->u.i;.
1b0f4 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f switch( pOp->o
1b0f5 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 63 pcode ){. c
1b0f6 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 ase OP_Add:
1b0f7 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20 20 b += a;
1b0f8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1b0f9 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a ase OP_Subtract:
1b0fa 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20 20 b -= a;
1b0fb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1b0fc 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a ase OP_Multiply:
1b0fd 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20 20 b *= a;
1b0fe 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1b0ff 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b ase OP_Divide: {
1b100 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d . if( a==
1b101 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 0 ) goto arithme
1b102 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 tic_result_is_nu
1b103 6c 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 ll;. /* D
1b104 69 76 69 64 69 6e 67 20 74 68 65 20 6c 61 72 67 ividing the larg
1b105 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 67 est possible neg
1b106 61 74 69 76 65 20 36 34 2d 62 69 74 20 69 6e 74 ative 64-bit int
1b107 65 67 65 72 20 28 31 3c 3c 36 33 29 20 62 79 20 eger (1<<63) by
1b108 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 72 . ** -1 r
1b109 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 eturns an intege
1b10a 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 73 r too large to s
1b10b 74 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69 74 tore in a 64-bit
1b10c 20 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a 20 data-type. On.
1b10d 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 61 ** some a
1b10e 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 rchitectures, th
1b10f 65 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f 77 e value overflow
1b110 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f 6e s to (1<<63). On
1b111 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20 20 others,.
1b112 20 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73 20 ** a SIGFPE is
1b113 69 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c 6c issued. The foll
1b114 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 owing statement
1b115 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 0a normalizes this.
1b116 20 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61 76 ** behav
1b117 69 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 ior so that all
1b118 61 72 63 68 69 74 65 63 74 75 72 65 73 20 62 65 architectures be
1b119 68 61 76 65 20 61 73 20 69 66 20 69 6e 74 65 67 have as if integ
1b11a 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f er . ** o
1b11b 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 64 verflow occurred
1b11c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
1b11d 20 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20 26 if( a==-1 &
1b11e 26 20 62 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e & b==SMALLEST_IN
1b11f 54 36 34 20 29 20 61 20 3d 20 31 3b 0a 20 20 20 T64 ) a = 1;.
1b120 20 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 20 20 b /= a;.
1b121 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1b122 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c }. defaul
1b123 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 t: {. if(
1b124 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 a==0 ) goto ari
1b125 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1b126 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 s_null;.
1b127 69 66 28 20 61 3d 3d 2d 31 20 29 20 61 20 3d 20 if( a==-1 ) a =
1b128 31 3b 0a 20 20 20 20 20 20 20 20 62 20 25 3d 20 1;. b %=
1b129 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b a;. break
1b12a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1b12b 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 pOut->u.i =
1b12c 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 b;. MemSetTyp
1b12d 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1b12e 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Int);. }else{.
1b12f 20 20 20 64 6f 75 62 6c 65 20 61 2c 20 62 3b 0a double a, b;.
1b130 20 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33 56 a = sqlite3V
1b131 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e dbeRealValue(pIn
1b132 31 29 3b 0a 20 20 20 20 62 20 3d 20 73 71 6c 69 1);. b = sqli
1b133 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 te3VdbeRealValue
1b134 28 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 (pIn2);. swit
1b135 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 ch( pOp->opcode
1b136 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 ){. case OP
1b137 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 62 20 _Add: b
1b138 2b 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 += a; brea
1b139 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 k;. case OP
1b13a 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 62 20 _Subtract: b
1b13b 2d 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 -= a; brea
1b13c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 k;. case OP
1b13d 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 62 20 _Multiply: b
1b13e 2a 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 *= a; brea
1b13f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 k;. case OP
1b140 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 _Divide: {.
1b141 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 /* (double)0
1b142 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 In case of SQLIT
1b143 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
1b144 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 POINT... */.
1b145 20 20 20 20 69 66 28 20 61 3d 3d 28 64 6f 75 62 if( a==(doub
1b146 6c 65 29 30 20 29 20 67 6f 74 6f 20 61 72 69 74 le)0 ) goto arit
1b147 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 hmetic_result_is
1b148 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 62 _null;. b
1b149 20 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62 /= a;. b
1b14a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1b14b 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
1b14c 20 20 20 20 20 20 20 69 36 34 20 69 61 20 3d 20 i64 ia =
1b14d 28 69 36 34 29 61 3b 0a 20 20 20 20 20 20 20 20 (i64)a;.
1b14e 69 36 34 20 69 62 20 3d 20 28 69 36 34 29 62 3b i64 ib = (i64)b;
1b14f 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 61 3d . if( ia=
1b150 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d =0 ) goto arithm
1b151 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e etic_result_is_n
1b152 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ull;. if(
1b153 20 69 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20 31 ia==-1 ) ia = 1
1b154 3b 0a 20 20 20 20 20 20 20 20 62 20 3d 20 28 64 ;. b = (d
1b155 6f 75 62 6c 65 29 28 69 62 20 25 20 69 61 29 3b ouble)(ib % ia);
1b156 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1b157 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1b158 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e if( sqlite3IsN
1b159 61 4e 28 62 29 20 29 7b 0a 20 20 20 20 20 20 67 aN(b) ){. g
1b15a 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 oto arithmetic_r
1b15b 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 esult_is_null;.
1b15c 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 72 }. pOut->r
1b15d 20 3d 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 = b;. MemSet
1b15e 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1b15f 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66 EM_Real);. if
1b160 28 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 ( (flags & MEM_R
1b161 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 eal)==0 ){.
1b162 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 sqlite3VdbeInte
1b163 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74 gerAffinity(pOut
1b164 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 );. }. }. b
1b165 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 reak;..arithmeti
1b166 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c c_result_is_null
1b167 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d :. sqlite3VdbeM
1b168 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b emSetNull(pOut);
1b169 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1b16a 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 Opcode: CollSeq
1b16b 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 * * P4.**.** P4
1b16c 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1b16d 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 a CollSeq struct
1b16e 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 61 . If the next ca
1b16f 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e ll to a user fun
1b170 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 ction.** or aggr
1b171 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 egate calls sqli
1b172 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 te3GetFuncCollSe
1b173 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 q(), this collat
1b174 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c ion sequence wil
1b175 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 64 l.** be returned
1b176 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 . This is used b
1b177 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d y the built-in m
1b178 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 in(), max() and
1b179 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 nullif().** func
1b17a 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tions..**.** The
1b17b 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 64 20 interface used
1b17c 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 by the implement
1b17d 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f ation of the afo
1b17e 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 rementioned func
1b17f 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 tions.** to retr
1b180 69 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 ieve the collati
1b181 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74 20 on sequence set
1b182 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 by this opcode i
1b183 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a s not available.
1b184 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c ** publicly, onl
1b185 79 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 y to user functi
1b186 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20 66 ons defined in f
1b187 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f unc.c..*/.case O
1b188 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 P_CollSeq: {. a
1b189 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1b18a 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 pe==P4_COLLSEQ )
1b18b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1b18c 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f Opcode: Functio
1b18d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 n P1 P2 P3 P4 P5
1b18e 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 .**.** Invoke a
1b18f 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 user function (P
1b190 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
1b191 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 o a Function str
1b192 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 ucture that.** d
1b193 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74 efines the funct
1b194 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67 ion) with P5 arg
1b195 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f uments taken fro
1b196 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e m register P2 an
1b197 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e d.** successors.
1b198 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 The result of
1b199 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 the function is
1b19a 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1b19b 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 er P3..** Regist
1b19c 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 er P3 must not b
1b19d 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 6e e one of the fun
1b19e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a ction inputs..**
1b19f 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 .** P1 is a 32-b
1b1a0 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 it bitmask indic
1b1a1 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 ating whether or
1b1a2 20 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 not each argume
1b1a3 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 nt to the .** fu
1b1a4 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 nction was deter
1b1a5 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 mined to be cons
1b1a6 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 tant at compile
1b1a7 74 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 time. If the fir
1b1a8 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 st.** argument w
1b1a9 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e as constant then
1b1aa 20 62 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 bit 0 of P1 is
1b1ab 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 set. This is use
1b1ac 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a d to determine.*
1b1ad 2a 20 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 * whether meta d
1b1ae 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
1b1af 69 74 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 ith a user funct
1b1b0 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 ion argument usi
1b1b1 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 ng the.** sqlite
1b1b2 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 3_set_auxdata()
1b1b3 41 50 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c API may be safel
1b1b4 79 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c y retained until
1b1b5 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 the next.** inv
1b1b6 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 ocation of this
1b1b7 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 opcode..**.** Se
1b1b8 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 e also: AggStep
1b1b9 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a and AggFinal.*/.
1b1ba 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e case OP_Function
1b1bb 3a 20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d : {. int i;. M
1b1bc 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 em *pArg;. sqli
1b1bd 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b te3_context ctx;
1b1be 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
1b1bf 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 **apVal;. int
1b1c0 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 0a 20 20 n = pOp->p5;..
1b1c1 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 apVal = p->apArg
1b1c2 3b 0a 20 20 61 73 73 65 72 74 28 20 61 70 56 61 ;. assert( apVa
1b1c3 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 20 l || n==0 );..
1b1c4 61 73 73 65 72 74 28 20 6e 3d 3d 30 20 7c 7c 20 assert( n==0 ||
1b1c5 28 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f (pOp->p2>0 && pO
1b1c6 70 2d 3e 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d p->p2+n<=p->nMem
1b1c7 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 +1) );. assert(
1b1c8 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 pOp->p3<pOp->p2
1b1c9 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 || pOp->p3>=pOp
1b1ca 2d 3e 70 32 2b 6e 20 29 3b 0a 20 20 70 41 72 67 ->p2+n );. pArg
1b1cb 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1b1cc 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b >p2];. for(i=0;
1b1cd 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 41 72 67 2b i<n; i++, pArg+
1b1ce 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d +){. apVal[i]
1b1cf 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 73 74 6f = pArg;. sto
1b1d0 72 65 54 79 70 65 49 6e 66 6f 28 70 41 72 67 2c reTypeInfo(pArg,
1b1d1 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 encoding);.
1b1d2 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1b1d3 4f 70 2d 3e 70 32 2c 20 70 41 72 67 29 3b 0a 20 Op->p2, pArg);.
1b1d4 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f }.. assert( pO
1b1d5 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 p->p4type==P4_FU
1b1d6 4e 43 44 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 NCDEF || pOp->p4
1b1d7 74 79 70 65 3d 3d 50 34 5f 56 44 42 45 46 55 4e type==P4_VDBEFUN
1b1d8 43 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e C );. if( pOp->
1b1d9 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 p4type==P4_FUNCD
1b1da 45 46 20 29 7b 0a 20 20 20 20 63 74 78 2e 70 46 EF ){. ctx.pF
1b1db 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 unc = pOp->p4.pF
1b1dc 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70 56 64 unc;. ctx.pVd
1b1dd 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 beFunc = 0;. }e
1b1de 6c 73 65 7b 0a 20 20 20 20 63 74 78 2e 70 56 64 lse{. ctx.pVd
1b1df 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 46 75 beFunc = (VdbeFu
1b1e0 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62 nc*)pOp->p4.pVdb
1b1e1 65 46 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70 eFunc;. ctx.p
1b1e2 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 64 62 65 Func = ctx.pVdbe
1b1e3 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d Func->pFunc;. }
1b1e4 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1b1e5 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 >p3>0 && pOp->p3
1b1e6 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
1b1e7 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
1b1e8 4f 70 2d 3e 70 33 5d 3b 0a 20 20 63 74 78 2e 73 Op->p3];. ctx.s
1b1e9 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c .flags = MEM_Nul
1b1ea 6c 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20 3d 20 l;. ctx.s.db =
1b1eb 64 62 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c db;. ctx.s.xDel
1b1ec 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d = 0;. ctx.s.zM
1b1ed 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a alloc = 0;.. /*
1b1ee 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c The output cell
1b1ef 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 76 may already hav
1b1f0 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 e a buffer alloc
1b1f1 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 ated. Move. **
1b1f2 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 63 the pointer to c
1b1f3 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 tx.s so in case
1b1f4 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f the user-functio
1b1f5 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 74 n can use. ** t
1b1f6 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 he already alloc
1b1f7 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 ated buffer inst
1b1f8 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e ead of allocatin
1b1f9 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a g a new one.. *
1b1fa 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d /. sqlite3VdbeM
1b1fb 65 6d 4d 6f 76 65 28 26 63 74 78 2e 73 2c 20 70 emMove(&ctx.s, p
1b1fc 4f 75 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 Out);. MemSetTy
1b1fd 70 65 46 6c 61 67 28 26 63 74 78 2e 73 2c 20 4d peFlag(&ctx.s, M
1b1fe 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 63 74 78 EM_Null);.. ctx
1b1ff 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 .isError = 0;.
1b200 69 66 28 20 63 74 78 2e 70 46 75 6e 63 2d 3e 66 if( ctx.pFunc->f
1b201 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
1b202 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 NC_NEEDCOLL ){.
1b203 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70 assert( pOp>p
1b204 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 ->aOp );. ass
1b205 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 ert( pOp[-1].p4t
1b206 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 ype==P4_COLLSEQ
1b207 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1b208 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f Op[-1].opcode==O
1b209 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 P_CollSeq );.
1b20a 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 ctx.pColl = pOp
1b20b 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 [-1].p4.pColl;.
1b20c 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1b20d 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 SafetyOff(db) )
1b20e 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1b20f 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 63 74 o_misuse;. (*ct
1b210 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 x.pFunc->xFunc)(
1b211 26 63 74 78 2c 20 6e 2c 20 61 70 56 61 6c 29 3b &ctx, n, apVal);
1b212 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1b213 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 fetyOn(db) ){.
1b214 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b215 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29 3b Release(&ctx.s);
1b216 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f . goto abort_
1b217 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1b218 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c }. if( db->mal
1b219 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1b21a 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 /* Even though
1b21b 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 a malloc() has f
1b21c 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 6c 65 ailed, the imple
1b21d 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
1b21e 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 75 6e . ** user fun
1b21f 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 20 63 ction may have c
1b220 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 65 33 alled an sqlite3
1b221 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 66 75 _result_XXX() fu
1b222 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f nction. ** to
1b223 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 65 2e return a value.
1b224 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
1b225 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 6e 79 all releases any
1b226 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 20 2a resources. *
1b227 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 * associated wit
1b228 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 2e 0a h such a value..
1b229 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f **. ** No
1b22a 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 65 6c te: Maybe MemRel
1b22b 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 62 65 ease() should be
1b22c 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c 69 74 called if sqlit
1b22d 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 20 20 e3SafetyOn().
1b22e 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f 20 28 ** fails also (
1b22f 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 61 74 the if(...) stat
1b230 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 42 75 ement above). Bu
1b231 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 65 0a t if people are.
1b232 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e 67 20 ** misusing
1b233 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 61 76 sqlite, they hav
1b234 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c 65 6d e bigger problem
1b235 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 64 20 s than a leaked
1b236 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 value.. */.
1b237 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b238 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29 3b Release(&ctx.s);
1b239 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d . goto no_mem
1b23a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 ;. }.. /* If a
1b23b 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 ny auxiliary dat
1b23c 61 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 a functions have
1b23d 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 62 79 20 been called by
1b23e 74 68 69 73 20 75 73 65 72 20 66 75 6e 63 74 69 this user functi
1b23f 6f 6e 2c 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 on,. ** immedia
1b240 74 65 6c 79 20 63 61 6c 6c 20 74 68 65 20 64 65 tely call the de
1b241 73 74 72 75 63 74 6f 72 20 66 6f 72 20 61 6e 79 structor for any
1b242 20 6e 6f 6e 2d 73 74 61 74 69 63 20 76 61 6c 75 non-static valu
1b243 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 es.. */. if( c
1b244 74 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b 0a tx.pVdbeFunc ){.
1b245 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 sqlite3VdbeD
1b246 65 6c 65 74 65 41 75 78 44 61 74 61 28 63 74 78 eleteAuxData(ctx
1b247 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 2d .pVdbeFunc, pOp-
1b248 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 >p1);. pOp->p
1b249 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 63 74 4.pVdbeFunc = ct
1b24a 78 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 x.pVdbeFunc;.
1b24b 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
1b24c 34 5f 56 44 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 4_VDBEFUNC;. }.
1b24d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 75 6e . /* If the fun
1b24e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61 ction returned a
1b24f 6e 20 65 72 72 6f 72 2c 20 74 68 72 6f 77 20 61 n error, throw a
1b250 6e 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 n exception */.
1b251 20 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f 72 if( ctx.isError
1b252 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
1b253 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1b254 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 rMsg, db, "%s",
1b255 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1b256 78 74 28 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 xt(&ctx.s));.
1b257 20 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72 6f rc = ctx.isErro
1b258 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 r;. }.. /* Cop
1b259 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 y the result of
1b25a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 the function int
1b25b 6f 20 72 65 67 69 73 74 65 72 20 50 33 20 2a 2f o register P3 */
1b25c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
1b25d 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 63 74 angeEncoding(&ct
1b25e 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a x.s, encoding);.
1b25f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b260 4d 6f 76 65 28 70 4f 75 74 2c 20 26 63 74 78 2e Move(pOut, &ctx.
1b261 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 s);. if( sqlite
1b262 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 3VdbeMemTooBig(p
1b263 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f Out) ){. goto
1b264 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 too_big;. }.
1b265 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1b266 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 Op->p3, pOut);.
1b267 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1b268 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 SIZE(pOut);. br
1b269 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1b26a 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20 e: BitAnd P1 P2
1b26b 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b P3 * *.**.** Tak
1b26c 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41 e the bit-wise A
1b26d 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 ND of the values
1b26e 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1b26f 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 and P2 and.** st
1b270 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1b271 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1b272 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1b273 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1b274 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1b275 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 /./* Opcode: Bit
1b276 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a Or P1 P2 P3 * *.
1b277 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 **.** Take the b
1b278 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68 it-wise OR of th
1b279 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1b27a 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 ster P1 and P2 a
1b27b 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 nd.** store the
1b27c 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1b27d 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1b27e 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1b27f 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1b280 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1b281 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50 ode: ShiftLeft P
1b282 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1b283 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 * Shift the inte
1b284 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 ger value in reg
1b285 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 ister P2 to the
1b286 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e left by the.** n
1b287 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70 umber of bits sp
1b288 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69 ecified by the i
1b289 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65 nteger in regise
1b28a 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 r P1..** Store t
1b28b 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 he result in reg
1b28c 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 ister P3..** If
1b28d 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 either input is
1b28e 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 NULL, the result
1b28f 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 is NULL..*/./*
1b290 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67 Opcode: ShiftRig
1b291 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a ht P1 P2 P3 * *.
1b292 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 **.** Shift the
1b293 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e integer value in
1b294 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 register P2 to
1b295 74 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65 the right by the
1b296 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 .** number of bi
1b297 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ts specified by
1b298 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 the integer in r
1b299 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 egister P1..** S
1b29a 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1b29b 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a in register P3..
1b29c 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 ** If either inp
1b29d 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 ut is NULL, the
1b29e 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a result is NULL..
1b29f 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e */.case OP_BitAn
1b2a0 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d:
1b2a1 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1b2a2 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e _BITAND, in1, in
1b2a3 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 2, out3 */.case
1b2a4 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 OP_BitOr:
1b2a5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1b2a6 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 me as TK_BITOR,
1b2a7 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1b2a8 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c /.case OP_ShiftL
1b2a9 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 eft:
1b2aa 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1b2ab 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 LSHIFT, in1, in2
1b2ac 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1b2ad 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20 P_ShiftRight: {
1b2ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1b2af 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 e as TK_RSHIFT,
1b2b0 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1b2b1 2f 0a 20 20 69 36 34 20 61 2c 20 62 3b 0a 0a 20 /. i64 a, b;..
1b2b2 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
1b2b3 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 s | pIn2->flags)
1b2b4 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1b2b5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1b2b6 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a mSetNull(pOut);.
1b2b7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 break;. }.
1b2b8 20 61 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 a = sqlite3Vdbe
1b2b9 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a IntValue(pIn2);.
1b2ba 20 20 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 b = sqlite3Vdb
1b2bb 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 3b eIntValue(pIn1);
1b2bc 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e . switch( pOp->
1b2bd 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 opcode ){. ca
1b2be 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20 se OP_BitAnd:
1b2bf 20 20 20 61 20 26 3d 20 62 3b 20 20 20 20 20 62 a &= b; b
1b2c0 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f reak;. case O
1b2c1 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 61 P_BitOr: a
1b2c2 20 7c 3d 20 62 3b 20 20 20 20 20 62 72 65 61 6b |= b; break
1b2c3 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 53 68 ;. case OP_Sh
1b2c4 69 66 74 4c 65 66 74 3a 20 20 20 61 20 3c 3c 3d iftLeft: a <<=
1b2c5 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 b; break;.
1b2c6 20 20 64 65 66 61 75 6c 74 3a 20 20 61 73 73 65 default: asse
1b2c7 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d rt( pOp->opcode=
1b2c8 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 =OP_ShiftRight )
1b2c9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1b2ca 20 20 20 20 20 20 20 20 20 20 20 61 20 3e 3e 3d a >>=
1b2cb 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 b; break;.
1b2cc 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 }. pOut->u.i =
1b2cd 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 a;. MemSetTypeF
1b2ce 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1b2cf 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1b2d0 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 49 6d /* Opcode: AddIm
1b2d1 6d 20 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a m P1 P2 * * *.*
1b2d2 2a 20 0a 2a 2a 20 41 64 64 20 74 68 65 20 63 6f * .** Add the co
1b2d3 6e 73 74 61 6e 74 20 50 32 20 74 6f 20 74 68 65 nstant P2 to the
1b2d4 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1b2d5 65 72 20 50 31 2e 0a 2a 2a 20 54 68 65 20 72 65 er P1..** The re
1b2d6 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 61 sult is always a
1b2d7 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a n integer..**.**
1b2d8 20 54 6f 20 66 6f 72 63 65 20 61 6e 79 20 72 65 To force any re
1b2d9 67 69 73 74 65 72 20 74 6f 20 62 65 20 61 6e 20 gister to be an
1b2da 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 61 64 integer, just ad
1b2db 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f d 0..*/.case OP_
1b2dc 41 64 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20 20 AddImm: {
1b2dd 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 /* in1 */.
1b2de 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1b2df 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b ntegerify(pIn1);
1b2e0 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 . pIn1->u.i +=
1b2e1 70 4f 70 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b pOp->p2;. break
1b2e2 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1b2e3 4d 75 73 74 42 65 49 6e 74 20 50 31 20 50 32 20 MustBeInt P1 P2
1b2e4 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 * * *.** .** For
1b2e5 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 ce the value in
1b2e6 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 register P1 to b
1b2e7 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 e an integer. I
1b2e8 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 f the value.** i
1b2e9 6e 20 50 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 n P1 is not an i
1b2ea 6e 74 65 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f nteger and canno
1b2eb 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 t be converted i
1b2ec 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a nto an integer.*
1b2ed 2a 20 77 69 74 68 6f 75 74 20 64 61 74 61 20 6c * without data l
1b2ee 6f 73 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 oss, then jump i
1b2ef 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 mmediately to P2
1b2f0 2c 20 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a , or if P2==0.**
1b2f1 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45 raise an SQLITE
1b2f2 5f 4d 49 53 4d 41 54 43 48 20 65 78 63 65 70 74 _MISMATCH except
1b2f3 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ion..*/.case OP_
1b2f4 4d 75 73 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 MustBeInt: {
1b2f5 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1b2f6 20 69 6e 31 20 2a 2f 0a 20 20 61 70 70 6c 79 41 in1 */. applyA
1b2f7 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 ffinity(pIn1, SQ
1b2f8 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
1b2f9 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69 , encoding);. i
1b2fa 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
1b2fb 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b & MEM_Int)==0 ){
1b2fc 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 . if( pOp->p2
1b2fd 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ==0 ){. rc
1b2fe 3d 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 = SQLITE_MISMATC
1b2ff 48 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 H;. goto ab
1b300 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1b301 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1b302 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1b303 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c - 1;. }. }el
1b304 73 65 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 se{. MemSetTy
1b305 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d peFlag(pIn1, MEM
1b306 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 _Int);. }. bre
1b307 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1b308 3a 20 52 65 61 6c 41 66 66 69 6e 69 74 79 20 50 : RealAffinity P
1b309 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1b30a 49 66 20 72 65 67 69 73 74 65 72 20 50 31 20 68 If register P1 h
1b30b 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72 20 olds an integer
1b30c 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 convert it to a
1b30d 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a real value..**.*
1b30e 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 * This opcode is
1b30f 20 75 73 65 64 20 77 68 65 6e 20 65 78 74 72 61 used when extra
1b310 63 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f cting informatio
1b311 6e 20 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 n from a column
1b312 74 68 61 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c that.** has REAL
1b313 20 61 66 66 69 6e 69 74 79 2e 20 20 53 75 63 68 affinity. Such
1b314 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d column values m
1b315 61 79 20 73 74 69 6c 6c 20 62 65 20 73 74 6f 72 ay still be stor
1b316 65 64 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72 ed as.** integer
1b317 73 2c 20 66 6f 72 20 73 70 61 63 65 20 65 66 66 s, for space eff
1b318 69 63 69 65 6e 63 79 2c 20 62 75 74 20 61 66 74 iciency, but aft
1b319 65 72 20 65 78 74 72 61 63 74 69 6f 6e 20 77 65 er extraction we
1b31a 20 77 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f want them.** to
1b31b 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 72 65 61 have only a rea
1b31c 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 l value..*/.case
1b31d 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 OP_RealAffinity
1b31e 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1b31f 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 /* in1 */.
1b320 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 if( pIn1->flags
1b321 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 & MEM_Int ){.
1b322 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b323 52 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 Realify(pIn1);.
1b324 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 }. break;.}..#
1b325 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1b326 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 IT_CAST./* Opcod
1b327 65 3a 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a e: ToText P1 * *
1b328 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 * *.**.** Force
1b329 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1b32a 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 gister P1 to be
1b32b 74 65 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 text..** If the
1b32c 76 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 value is numeric
1b32d 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 , convert it to
1b32e 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 a string using t
1b32f 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 he.** equivalent
1b330 20 6f 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 of printf(). B
1b331 6c 6f 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 lob values are u
1b332 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 nchanged and.**
1b333 61 72 65 20 61 66 74 65 72 77 61 72 64 73 20 73 are afterwards s
1b334 69 6d 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 imply interprete
1b335 64 20 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a d as text..**.**
1b336 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 A NULL value is
1b337 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 not changed by
1b338 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 this routine. I
1b339 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a t remains NULL..
1b33a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 */.case OP_ToTex
1b33b 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 t: {
1b33c 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1b33d 20 54 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 TK_TO_TEXT, in1
1b33e 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e */. if( pIn1->
1b33f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1b340 20 29 20 62 72 65 61 6b 3b 0a 20 20 61 73 73 65 ) break;. asse
1b341 72 74 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 rt( MEM_Str==(ME
1b342 4d 5f 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 M_Blob>>3) );.
1b343 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 pIn1->flags |= (
1b344 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pIn1->flags&MEM_
1b345 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c Blob)>>3;. appl
1b346 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 yAffinity(pIn1,
1b347 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c SQLITE_AFF_TEXT,
1b348 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63 encoding);. rc
1b349 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 = ExpandBlob(pI
1b34a 6e 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 n1);. assert( p
1b34b 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1b34c 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c _Str || db->mall
1b34d 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 70 49 ocFailed );. pI
1b34e 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d n1->flags &= ~(M
1b34f 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c EM_Int|MEM_Real|
1b350 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 MEM_Blob|MEM_Zer
1b351 6f 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 o);. UPDATE_MAX
1b352 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b _BLOBSIZE(pIn1);
1b353 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1b354 4f 70 63 6f 64 65 3a 20 54 6f 42 6c 6f 62 20 50 Opcode: ToBlob P
1b355 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1b356 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 Force the value
1b357 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 in register P1 t
1b358 6f 20 62 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 o be a BLOB..**
1b359 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 If the value is
1b35a 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 numeric, convert
1b35b 20 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 it to a string
1b35c 66 69 72 73 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 first..** String
1b35d 73 20 61 72 65 20 73 69 6d 70 6c 79 20 72 65 69 s are simply rei
1b35e 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 62 6c nterpreted as bl
1b35f 6f 62 73 20 77 69 74 68 20 6e 6f 20 63 68 61 6e obs with no chan
1b360 67 65 0a 2a 2a 20 74 6f 20 74 68 65 20 75 6e 64 ge.** to the und
1b361 65 72 6c 79 69 6e 67 20 64 61 74 61 2e 0a 2a 2a erlying data..**
1b362 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 .** A NULL value
1b363 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 is not changed
1b364 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e by this routine.
1b365 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c It remains NUL
1b366 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f L..*/.case OP_To
1b367 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 20 Blob: {
1b368 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1b369 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 as TK_TO_BLOB,
1b36a 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e in1 */. if( pIn
1b36b 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 1->flags & MEM_N
1b36c 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 69 ull ) break;. i
1b36d 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
1b36e 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 & MEM_Blob)==0 )
1b36f 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e {. applyAffin
1b370 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 ity(pIn1, SQLITE
1b371 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 _AFF_TEXT, encod
1b372 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ing);. assert
1b373 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 ( pIn1->flags &
1b374 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d MEM_Str || db->m
1b375 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
1b376 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1b377 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 g(pIn1, MEM_Blob
1b378 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1b379 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pIn1->flags &= ~
1b37a 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e 4d (MEM_TypeMask&~M
1b37b 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 EM_Blob);. }.
1b37c 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1b37d 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 IZE(pIn1);. bre
1b37e 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1b37f 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 2a : ToNumeric P1 *
1b380 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 * * *.**.** For
1b381 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 ce the value in
1b382 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 register P1 to b
1b383 65 20 6e 75 6d 65 72 69 63 20 28 65 69 74 68 65 e numeric (eithe
1b384 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 r an.** integer
1b385 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f or a floating-po
1b386 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 int number.).**
1b387 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 If the value is
1b388 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 text or blob, tr
1b389 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 y to convert it
1b38a 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 68 65 0a to an using the.
1b38b 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 ** equivalent of
1b38c 20 61 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 28 atoi() or atof(
1b38d 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 ) and store 0 if
1b38e 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 no such convers
1b38f 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 69 ion .** is possi
1b390 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c ble..**.** A NUL
1b391 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 L value is not c
1b392 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 hanged by this r
1b393 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 outine. It rema
1b394 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 ins NULL..*/.cas
1b395 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 e OP_ToNumeric:
1b396 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1b397 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1b398 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e 31 _TO_NUMERIC, in1
1b399 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d */. if( (pIn1-
1b39a 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 4e 75 >flags & (MEM_Nu
1b39b 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 ll|MEM_Int|MEM_R
1b39c 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 eal))==0 ){.
1b39d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
1b39e 6d 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 merify(pIn1);.
1b39f 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e }. break;.}.#en
1b3a0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1b3a1 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f IT_CAST */../* O
1b3a2 70 63 6f 64 65 3a 20 54 6f 49 6e 74 20 50 31 20 pcode: ToInt P1
1b3a3 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f * * * *.**.** Fo
1b3a4 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e rce the value in
1b3a5 20 72 65 67 69 73 74 65 72 20 50 31 20 62 65 20 register P1 be
1b3a6 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 0a an integer. If.
1b3a7 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 ** The value is
1b3a8 63 75 72 72 65 6e 74 6c 79 20 61 20 72 65 61 6c currently a real
1b3a9 20 6e 75 6d 62 65 72 2c 20 64 72 6f 70 20 69 74 number, drop it
1b3aa 73 20 66 72 61 63 74 69 6f 6e 61 6c 20 70 61 72 s fractional par
1b3ab 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c t..** If the val
1b3ac 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c ue is text or bl
1b3ad 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 ob, try to conve
1b3ae 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 rt it to an inte
1b3af 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a ger using the.**
1b3b0 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 equivalent of a
1b3b1 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20 toi() and store
1b3b2 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 0 if no such con
1b3b3 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 version is possi
1b3b4 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c ble..**.** A NUL
1b3b5 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 L value is not c
1b3b6 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 hanged by this r
1b3b7 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 outine. It rema
1b3b8 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 ins NULL..*/.cas
1b3b9 65 20 4f 50 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 e OP_ToInt: {
1b3ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1b3bb 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
1b3bc 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 INT, in1 */. if
1b3bd 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1b3be 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b MEM_Null)==0 ){
1b3bf 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1b3c0 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 MemIntegerify(pI
1b3c1 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b n1);. }. break
1b3c2 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
1b3c3 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a ITE_OMIT_CAST./*
1b3c4 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20 Opcode: ToReal
1b3c5 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1b3c6 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 Force the value
1b3c7 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1b3c8 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 to be a floating
1b3c9 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a point number..*
1b3ca 2a 20 49 66 20 54 68 65 20 76 61 6c 75 65 20 69 * If The value i
1b3cb 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 69 s currently an i
1b3cc 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74 20 nteger, convert
1b3cd 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 it..** If the va
1b3ce 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 lue is text or b
1b3cf 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 lob, try to conv
1b3d0 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 ert it to an int
1b3d1 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a eger using the.*
1b3d2 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 * equivalent of
1b3d3 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65 atoi() and store
1b3d4 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 63 68 20 0.0 if no such
1b3d5 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f conversion is po
1b3d6 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 ssible..**.** A
1b3d7 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f NULL value is no
1b3d8 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 t changed by thi
1b3d9 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 s routine. It r
1b3da 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a emains NULL..*/.
1b3db 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 case OP_ToReal:
1b3dc 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1b3dd 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1b3de 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a 2f _TO_REAL, in1 */
1b3df 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
1b3e0 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d ags & MEM_Null)=
1b3e1 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1b3e2 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 3VdbeMemRealify(
1b3e3 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 pIn1);. }. bre
1b3e4 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1b3e5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 SQLITE_OMIT_CAST
1b3e6 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 */../* Opcode:
1b3e7 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 Lt P1 P2 P3 P4 P
1b3e8 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 5.**.** Compare
1b3e9 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 the values in re
1b3ea 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 33 gister P1 and P3
1b3eb 2e 20 20 49 66 20 72 65 67 28 50 33 29 3c 72 65 . If reg(P3)<re
1b3ec 67 28 50 31 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 g(P1) then.** ju
1b3ed 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 mp to address P2
1b3ee 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 . .**.** If the
1b3ef 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 SQLITE_JUMPIFNU
1b3f0 4c 4c 20 62 69 74 20 6f 66 20 50 35 20 69 73 20 LL bit of P5 is
1b3f1 73 65 74 20 61 6e 64 20 65 69 74 68 65 72 20 72 set and either r
1b3f2 65 67 28 50 31 29 20 6f 72 0a 2a 2a 20 72 65 67 eg(P1) or.** reg
1b3f3 28 50 33 29 20 69 73 20 4e 55 4c 4c 20 74 68 65 (P3) is NULL the
1b3f4 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 2e n take the jump.
1b3f5 20 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f If the SQLITE_
1b3f6 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 JUMPIFNULL .** b
1b3f7 69 74 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e it is clear then
1b3f8 20 66 61 6c 6c 20 74 68 72 75 20 69 66 20 65 69 fall thru if ei
1b3f9 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
1b3fa 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 NULL..**.** The
1b3fb 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 SQLITE_AFF_MASK
1b3fc 70 6f 72 74 69 6f 6e 20 6f 66 20 50 35 20 6d 75 portion of P5 mu
1b3fd 73 74 20 62 65 20 61 6e 20 61 66 66 69 6e 69 74 st be an affinit
1b3fe 79 20 63 68 61 72 61 63 74 65 72 20 2d 0a 2a 2a y character -.**
1b3ff 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
1b400 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 , SQLITE_AFF_INT
1b401 45 47 45 52 2c 20 61 6e 64 20 73 6f 20 66 6f 72 EGER, and so for
1b402 74 68 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 69 th. An attempt i
1b403 73 20 6d 61 64 65 20 0a 2a 2a 20 74 6f 20 63 6f s made .** to co
1b404 65 72 63 65 20 62 6f 74 68 20 69 6e 70 75 74 73 erce both inputs
1b405 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 according to th
1b406 69 73 20 61 66 66 69 6e 69 74 79 20 62 65 66 6f is affinity befo
1b407 72 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 re the.** compar
1b408 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e 20 49 66 ison is made. If
1b409 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f the SQLITE_AFF_
1b40a 4d 41 53 4b 20 69 73 20 30 78 30 30 2c 20 74 68 MASK is 0x00, th
1b40b 65 6e 20 6e 75 6d 65 72 69 63 0a 2a 2a 20 61 66 en numeric.** af
1b40c 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 2e 20 finity is used.
1b40d 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 66 Note that the af
1b40e 66 69 6e 69 74 79 20 63 6f 6e 76 65 72 73 69 6f finity conversio
1b40f 6e 73 20 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a ns are stored.**
1b410 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 69 back into the i
1b411 6e 70 75 74 20 72 65 67 69 73 74 65 72 73 20 50 nput registers P
1b412 31 20 61 6e 64 20 50 33 2e 20 20 53 6f 20 74 68 1 and P3. So th
1b413 69 73 20 6f 70 63 6f 64 65 20 63 61 6e 20 63 61 is opcode can ca
1b414 75 73 65 0a 2a 2a 20 70 65 72 73 69 73 74 65 6e use.** persisten
1b415 74 20 63 68 61 6e 67 65 73 20 74 6f 20 72 65 67 t changes to reg
1b416 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 isters P1 and P3
1b417 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 ..**.** Once any
1b418 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 conversions hav
1b419 65 20 74 61 6b 65 6e 20 70 6c 61 63 65 2c 20 61 e taken place, a
1b41a 6e 64 20 6e 65 69 74 68 65 72 20 76 61 6c 75 65 nd neither value
1b41b 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 is NULL, .** th
1b41c 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6d e values are com
1b41d 70 61 72 65 64 2e 20 49 66 20 62 6f 74 68 20 76 pared. If both v
1b41e 61 6c 75 65 73 20 61 72 65 20 62 6c 6f 62 73 20 alues are blobs
1b41f 74 68 65 6e 20 6d 65 6d 63 6d 70 28 29 20 69 73 then memcmp() is
1b420 0a 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 .** used to dete
1b421 72 6d 69 6e 65 20 74 68 65 20 72 65 73 75 6c 74 rmine the result
1b422 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 s of the compari
1b423 73 6f 6e 2e 20 20 49 66 20 62 6f 74 68 20 76 61 son. If both va
1b424 6c 75 65 73 0a 2a 2a 20 61 72 65 20 74 65 78 74 lues.** are text
1b425 2c 20 74 68 65 6e 20 74 68 65 20 61 70 70 72 6f , then the appro
1b426 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 priate collating
1b427 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 function specif
1b428 69 65 64 20 69 6e 0a 2a 2a 20 50 34 20 69 73 20 ied in.** P4 is
1b429 20 75 73 65 64 20 74 6f 20 64 6f 20 74 68 65 20 used to do the
1b42a 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 comparison. If
1b42b 50 34 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 P4 is not specif
1b42c 69 65 64 20 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 ied then.** memc
1b42d 6d 70 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 mp() is used to
1b42e 63 6f 6d 70 61 72 65 20 74 65 78 74 20 73 74 72 compare text str
1b42f 69 6e 67 2e 20 20 49 66 20 62 6f 74 68 20 76 61 ing. If both va
1b430 6c 75 65 73 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 lues are.** nume
1b431 72 69 63 2c 20 74 68 65 6e 20 61 20 6e 75 6d 65 ric, then a nume
1b432 72 69 63 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 ric comparison i
1b433 73 20 75 73 65 64 2e 20 49 66 20 74 68 65 20 74 s used. If the t
1b434 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 wo values.** are
1b435 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 74 79 of different ty
1b436 70 65 73 2c 20 74 68 65 6e 20 6e 75 6d 62 65 72 pes, then number
1b437 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 s are considered
1b438 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 73 74 less than.** st
1b439 72 69 6e 67 73 20 61 6e 64 20 73 74 72 69 6e 67 rings and string
1b43a 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 s are considered
1b43b 20 6c 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 less than blobs
1b43c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 ..**.** If the S
1b43d 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 62 69 QLITE_STOREP2 bi
1b43e 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 t of P5 is set,
1b43f 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 then do not jump
1b440 2e 20 20 49 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 . Instead,.** s
1b441 74 6f 72 65 20 61 20 62 6f 6f 6c 65 61 6e 20 72 tore a boolean r
1b442 65 73 75 6c 74 20 28 65 69 74 68 65 72 20 30 2c esult (either 0,
1b443 20 6f 72 20 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 or 1, or NULL)
1b444 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a in register P2..
1b445 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 */./* Opcode: Ne
1b446 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1b447 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 **.** This works
1b448 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c just like the L
1b449 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 t opcode except
1b44a 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 that the jump is
1b44b 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 taken if.** the
1b44c 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 operands in reg
1b44d 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 isters P1 and P3
1b44e 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 2e 20 are not equal.
1b44f 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f See the Lt opco
1b450 64 65 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69 de for.** additi
1b451 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1b452 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1b453 45 71 20 50 31 20 50 32 20 50 33 20 50 34 20 50 Eq P1 P2 P3 P4 P
1b454 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 5.**.** This wor
1b455 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 ks just like the
1b456 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 Lt opcode excep
1b457 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 t that the jump
1b458 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 is taken if.** t
1b459 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 he operands in r
1b45a 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 egisters P1 and
1b45b 50 33 20 61 72 65 20 65 71 75 61 6c 2e 0a 2a 2a P3 are equal..**
1b45c 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f See the Lt opco
1b45d 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 de for additiona
1b45e 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
1b45f 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 /./* Opcode: Le
1b460 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1b461 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 *.** This works
1b462 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 just like the Lt
1b463 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 opcode except t
1b464 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 hat the jump is
1b465 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 taken if.** the
1b466 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 content of regis
1b467 74 65 72 20 50 33 20 69 73 20 6c 65 73 73 20 74 ter P3 is less t
1b468 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
1b469 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a the content of.*
1b46a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 * register P1.
1b46b 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 See the Lt opcod
1b46c 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c e for additional
1b46d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
1b46e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 ./* Opcode: Gt P
1b46f 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1b470 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a .** This works j
1b471 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 ust like the Lt
1b472 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 opcode except th
1b473 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 at the jump is t
1b474 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 aken if.** the c
1b475 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 ontent of regist
1b476 65 72 20 50 33 20 69 73 20 67 72 65 61 74 65 72 er P3 is greater
1b477 20 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e than the conten
1b478 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 t of.** register
1b479 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 P1. See the Lt
1b47a 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 opcode for addi
1b47b 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
1b47c 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 on..*/./* Opcode
1b47d 3a 20 47 65 20 50 31 20 50 32 20 50 33 20 50 34 : Ge P1 P2 P3 P4
1b47e 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 P5.**.** This w
1b47f 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 orks just like t
1b480 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 he Lt opcode exc
1b481 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d ept that the jum
1b482 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a p is taken if.**
1b483 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
1b484 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 67 register P3 is g
1b485 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
1b486 71 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 qual to the cont
1b487 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 ent of.** regist
1b488 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 er P1. See the
1b489 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 Lt opcode for ad
1b48a 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
1b48b 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 tion..*/.case OP
1b48c 5f 45 71 3a 20 20 20 20 20 20 20 20 20 20 20 20 _Eq:
1b48d 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1b48e 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 _EQ, jump, in1,
1b48f 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e in3 */.case OP_N
1b490 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e:
1b491 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
1b492 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e E, jump, in1, in
1b493 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 3 */.case OP_Lt:
1b494 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1b495 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c * same as TK_LT,
1b496 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
1b497 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 */.case OP_Le:
1b498 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b499 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a same as TK_LE, j
1b49a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f ump, in1, in3 */
1b49b 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 .case OP_Gt:
1b49c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1b49d 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d me as TK_GT, jum
1b49e 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 p, in1, in3 */.c
1b49f 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20 ase OP_Ge: {
1b4a0 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1b4a1 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c as TK_GE, jump,
1b4a2 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 in1, in3 */. i
1b4a3 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 nt flags;. int
1b4a4 72 65 73 3b 0a 20 20 63 68 61 72 20 61 66 66 69 res;. char affi
1b4a5 6e 69 74 79 3b 0a 0a 20 20 66 6c 61 67 73 20 3d nity;.. flags =
1b4a6 20 70 49 6e 31 2d 3e 66 6c 61 67 73 7c 70 49 6e pIn1->flags|pIn
1b4a7 33 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 69 66 28 3->flags;.. if(
1b4a8 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 flags&MEM_Null
1b4a9 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 65 69 74 ){. /* If eit
1b4aa 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e her operand is N
1b4ab 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65 73 ULL then the res
1b4ac 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 4e 55 ult is always NU
1b4ad 4c 4c 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6a LL.. ** The j
1b4ae 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 20 ump is taken if
1b4af 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 the SQLITE_JUMPI
1b4b0 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 73 65 74 FNULL bit is set
1b4b1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1b4b2 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 pOp->p5 & SQLIT
1b4b3 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 E_STOREP2 ){.
1b4b4 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d pOut = &p->aM
1b4b5 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 em[pOp->p2];.
1b4b6 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1b4b7 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c g(pOut, MEM_Null
1b4b8 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 );. REGISTE
1b4b9 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c R_TRACE(pOp->p2,
1b4ba 20 70 4f 75 74 29 3b 0a 20 20 20 20 7d 65 6c 73 pOut);. }els
1b4bb 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 e if( pOp->p5 &
1b4bc 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c SQLITE_JUMPIFNUL
1b4bd 4c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 L ){. pc =
1b4be 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 7d pOp->p2-1;. }
1b4bf 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1b4c0 0a 20 20 61 66 66 69 6e 69 74 79 20 3d 20 70 4f . affinity = pO
1b4c1 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 41 p->p5 & SQLITE_A
1b4c2 46 46 5f 4d 41 53 4b 3b 0a 20 20 69 66 28 20 61 FF_MASK;. if( a
1b4c3 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 61 ffinity ){. a
1b4c4 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e pplyAffinity(pIn
1b4c5 31 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 1, affinity, enc
1b4c6 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 70 70 6c oding);. appl
1b4c7 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c 20 yAffinity(pIn3,
1b4c8 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 affinity, encodi
1b4c9 6e 67 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d ng);. if( db-
1b4ca 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
1b4cb 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d goto no_mem;. }
1b4cc 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1b4cd 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c >p4type==P4_COLL
1b4ce 53 45 51 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e 70 SEQ || pOp->p4.p
1b4cf 43 6f 6c 6c 3d 3d 30 20 29 3b 0a 20 20 45 78 70 Coll==0 );. Exp
1b4d0 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 andBlob(pIn1);.
1b4d1 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 ExpandBlob(pIn3
1b4d2 29 3b 0a 20 20 72 65 73 20 3d 20 73 71 6c 69 74 );. res = sqlit
1b4d3 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 49 6e e3MemCompare(pIn
1b4d4 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e 70 34 3, pIn1, pOp->p4
1b4d5 2e 70 43 6f 6c 6c 29 3b 0a 20 20 73 77 69 74 63 .pColl);. switc
1b4d6 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 h( pOp->opcode )
1b4d7 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 45 71 {. case OP_Eq
1b4d8 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3d 3d : res = res==
1b4d9 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 0; break;.
1b4da 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 case OP_Ne:
1b4db 20 72 65 73 20 3d 20 72 65 73 21 3d 30 3b 20 20 res = res!=0;
1b4dc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
1b4dd 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20 72 65 73 se OP_Lt: res
1b4de 20 3d 20 72 65 73 3c 30 3b 20 20 20 20 20 20 62 = res<0; b
1b4df 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f reak;. case O
1b4e0 50 5f 4c 65 3a 20 20 20 20 72 65 73 20 3d 20 72 P_Le: res = r
1b4e1 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 65 61 6b es<=0; break
1b4e2 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 47 74 ;. case OP_Gt
1b4e3 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3e 30 : res = res>0
1b4e4 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 ; break;.
1b4e5 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 default:
1b4e6 20 72 65 73 20 3d 20 72 65 73 3e 3d 30 3b 20 20 res = res>=0;
1b4e7 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 break;. }..
1b4e8 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 if( pOp->p5 & S
1b4e9 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b QLITE_STOREP2 ){
1b4ea 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e . pOut = &p->
1b4eb 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1b4ec 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1b4ed 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1b4ee 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 ;. pOut->u.i
1b4ef 3d 20 72 65 73 3b 0a 20 20 20 20 52 45 47 49 53 = res;. REGIS
1b4f0 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1b4f1 32 2c 20 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 2, pOut);. }els
1b4f2 65 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 e if( res ){.
1b4f3 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b pc = pOp->p2-1;
1b4f4 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1b4f5 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d ./* Opcode: Perm
1b4f6 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 utation * * * P4
1b4f7 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 *.**.** Set the
1b4f8 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73 65 permutation use
1b4f9 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f 6d 70 d by the OP_Comp
1b4fa 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 are operator to
1b4fb 62 65 20 74 68 65 20 61 72 72 61 79 0a 2a 2a 20 be the array.**
1b4fc 6f 66 20 69 6e 74 65 67 65 72 73 20 69 6e 20 50 of integers in P
1b4fd 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 4..**.** The per
1b4fe 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 mutation is only
1b4ff 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 valid until the
1b500 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d 75 74 61 next OP_Permuta
1b501 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 tion, OP_Compare
1b502 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 ,.** OP_Halt, or
1b503 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 20 20 OP_ResultRow.
1b504 54 79 70 69 63 61 6c 6c 79 20 74 68 65 20 4f 50 Typically the OP
1b505 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 73 68 6f _Permutation sho
1b506 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d uld occur.** imm
1b507 65 64 69 61 74 65 6c 79 20 70 72 69 6f 72 20 74 ediately prior t
1b508 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 o the OP_Compare
1b509 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 65 72 ..*/.case OP_Per
1b50a 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 mutation: {. as
1b50b 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 sert( pOp->p4typ
1b50c 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 59 20 29 e==P4_INTARRAY )
1b50d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1b50e 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 50 65 72 >p4.ai );. aPer
1b50f 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 mute = pOp->p4.a
1b510 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f i;. break;.}../
1b511 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61 72 * Opcode: Compar
1b512 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a e P1 P2 P3 P4 *.
1b513 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 6f **.** Compare to
1b514 20 76 65 63 74 6f 72 73 20 6f 66 20 72 65 67 69 vectors of regi
1b515 73 74 65 72 73 20 69 6e 20 72 65 67 28 50 31 29 sters in reg(P1)
1b516 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 29 20 28 ..reg(P1+P3-1) (
1b517 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 all this.** one
1b518 22 41 22 29 20 61 6e 64 20 69 6e 20 72 65 67 28 "A") and in reg(
1b519 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 33 2d 31 P2)..reg(P2+P3-1
1b51a 29 20 28 22 42 22 29 2e 20 20 53 61 76 65 20 74 ) ("B"). Save t
1b51b 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 he result of.**
1b51c 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 the comparison f
1b51d 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 6e 65 or use by the ne
1b51e 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 72 xt OP_Jump instr
1b51f 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 uct..**.** P4 is
1b520 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
1b521 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e 65 ture that define
1b522 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 s collating sequ
1b523 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 74 0a 2a ences and sort.*
1b524 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 74 68 65 * orders for the
1b525 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 comparison. Th
1b526 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 70 e permutation ap
1b527 70 6c 69 65 73 20 74 6f 20 72 65 67 69 73 74 65 plies to registe
1b528 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 rs.** only. The
1b529 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 KeyInfo element
1b52a 73 20 61 72 65 20 75 73 65 64 20 73 65 71 75 65 s are used seque
1b52b 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 ntially..**.** T
1b52c 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 he comparison is
1b52d 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69 73 a sort comparis
1b52e 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d on, so NULLs com
1b52f 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e pare equal,.** N
1b530 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74 68 ULLs are less th
1b531 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d 62 an numbers, numb
1b532 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 ers are less tha
1b533 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e n strings,.** an
1b534 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c 65 d strings are le
1b535 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a ss than blobs..*
1b536 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d 70 61 72 /.case OP_Compar
1b537 65 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 70 e: {. int n = p
1b538 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 20 69 2c Op->p3;. int i,
1b539 20 70 31 2c 20 70 32 3b 0a 20 20 63 6f 6e 73 74 p1, p2;. const
1b53a 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
1b53b 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 fo = pOp->p4.pKe
1b53c 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72 74 28 yInfo;. assert(
1b53d 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 n>0 );. assert
1b53e 28 20 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b ( pKeyInfo!=0 );
1b53f 0a 20 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b . p1 = pOp->p1;
1b540 0a 20 20 61 73 73 65 72 74 28 20 70 31 3e 30 20 . assert( p1>0
1b541 26 26 20 70 31 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d && p1+n<=p->nMem
1b542 2b 31 20 29 3b 0a 20 20 70 32 20 3d 20 70 4f 70 +1 );. p2 = pOp
1b543 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 ->p2;. assert(
1b544 70 32 3e 30 20 26 26 20 70 32 2b 6e 3c 3d 70 2d p2>0 && p2+n<=p-
1b545 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 66 6f 72 >nMem+1 );. for
1b546 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b (i=0; i<n; i++){
1b547 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 61 . int idx = a
1b548 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d 75 Permute ? aPermu
1b549 74 65 5b 69 5d 20 3a 20 69 3b 0a 20 20 20 20 43 te[i] : i;. C
1b54a 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 ollSeq *pColl;
1b54b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 /* Collating s
1b54c 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 20 6f equence to use o
1b54d 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 n this term */.
1b54e 20 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20 int bRev;
1b54f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
1b550 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f 72 r DESCENDING sor
1b551 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 52 t order */. R
1b552 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 31 EGISTER_TRACE(p1
1b553 2b 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70 +idx, &p->aMem[p
1b554 31 2b 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 1+idx]);. REG
1b555 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b 69 ISTER_TRACE(p2+i
1b556 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 2b dx, &p->aMem[p2+
1b557 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 idx]);. asser
1b558 74 28 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e t( i<pKeyInfo->n
1b559 46 69 65 6c 64 20 29 3b 0a 20 20 20 20 70 43 6f Field );. pCo
1b55a 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 ll = pKeyInfo->a
1b55b 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 62 52 65 Coll[i];. bRe
1b55c 76 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 v = pKeyInfo->aS
1b55d 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20 ortOrder[i];.
1b55e 20 69 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c 69 iCompare = sqli
1b55f 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 70 te3MemCompare(&p
1b560 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d 2c 20 ->aMem[p1+idx],
1b561 26 70 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78 5d &p->aMem[p2+idx]
1b562 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 , pColl);. if
1b563 28 20 69 43 6f 6d 70 61 72 65 20 29 7b 0a 20 20 ( iCompare ){.
1b564 20 20 20 20 69 66 28 20 62 52 65 76 20 29 20 69 if( bRev ) i
1b565 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d 70 Compare = -iComp
1b566 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b are;. break
1b567 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 50 ;. }. }. aP
1b568 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 72 ermute = 0;. br
1b569 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1b56a 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50 33 e: Jump P1 P2 P3
1b56b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 * *.**.** Jump
1b56c 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 to the instructi
1b56d 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 31 on at address P1
1b56e 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 65 , P2, or P3 depe
1b56f 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 nding on whether
1b570 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 .** in the most
1b571 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 72 recent OP_Compar
1b572 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 e instruction th
1b573 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 20 e P1 vector was
1b574 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 75 less than.** equ
1b575 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 al to, or greate
1b576 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76 65 r than the P2 ve
1b577 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76 65 ctor, respective
1b578 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4a ly..*/.case OP_J
1b579 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20 20 ump: {
1b57a 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 /* jump */.
1b57b 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 29 if( iCompare<0 )
1b57c 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e {. pc = pOp->
1b57d 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 p1 - 1;. }else
1b57e 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 20 if( iCompare==0
1b57f 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1b580 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 >p2 - 1;. }else
1b581 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e {. pc = pOp->
1b582 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p3 - 1;. }. br
1b583 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1b584 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33 20 e: And P1 P2 P3
1b585 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 * *.**.** Take t
1b586 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 6f he logical AND o
1b587 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 f the values in
1b588 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 registers P1 and
1b589 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 65 P2 and.** write
1b58a 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f the result into
1b58b 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
1b58c 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 31 .** If either P1
1b58d 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61 6c or P2 is 0 (fal
1b58e 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 se) then the res
1b58f 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69 66 ult is 0 even if
1b590 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 6e .** the other in
1b591 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 put is NULL. A
1b592 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f 72 NULL and true or
1b593 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 0a two NULLs give.
1b594 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 ** a NULL output
1b595 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1b596 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a Or P1 P2 P3 * *.
1b597 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c **.** Take the l
1b598 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 65 ogical OR of the
1b599 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 values in regis
1b59a 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e ter P1 and P2 an
1b59b 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 61 d.** store the a
1b59c 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74 65 nswer in registe
1b59d 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 r P3..**.** If e
1b59e 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 69 ither P1 or P2 i
1b59f 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 29 s nonzero (true)
1b5a0 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 then the result
1b5a1 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a 20 is 1 (true).**
1b5a2 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68 65 even if the othe
1b5a3 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e r input is NULL.
1b5a4 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 6c A NULL and fal
1b5a5 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 0a se or two NULLs.
1b5a6 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 6f ** give a NULL o
1b5a7 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f utput..*/.case O
1b5a8 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 P_And:
1b5a9 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1b5aa 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c K_AND, in1, in2,
1b5ab 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 out3 */.case OP
1b5ac 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20 _Or: {
1b5ad 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1b5ae 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f _OR, in1, in2, o
1b5af 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 76 31 2c ut3 */. int v1,
1b5b0 20 76 32 3b 20 20 20 20 2f 2a 20 30 3d 3d 46 41 v2; /* 0==FA
1b5b1 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d LSE, 1==TRUE, 2=
1b5b2 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c =UNKNOWN or NULL
1b5b3 20 2a 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 2d */.. if( pIn1-
1b5b4 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1b5b5 6c 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 32 3b l ){. v1 = 2;
1b5b6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 31 . }else{. v1
1b5b7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e = sqlite3VdbeIn
1b5b8 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b tValue(pIn1)!=0;
1b5b9 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 2d . }. if( pIn2-
1b5ba 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1b5bb 6c 20 29 7b 0a 20 20 20 20 76 32 20 3d 20 32 3b l ){. v2 = 2;
1b5bc 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 32 . }else{. v2
1b5bd 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e = sqlite3VdbeIn
1b5be 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d 30 3b tValue(pIn2)!=0;
1b5bf 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e . }. if( pOp->
1b5c0 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 29 opcode==OP_And )
1b5c1 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e {. static con
1b5c2 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
1b5c3 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b and_logic[] = {
1b5c4 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 0, 0, 0, 0, 1,
1b5c5 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20 20 2, 0, 2, 2 };.
1b5c6 20 20 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63 v1 = and_logic
1b5c7 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 65 6c [v1*3+v2];. }el
1b5c8 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 se{. static c
1b5c9 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1b5ca 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d 20 ar or_logic[] =
1b5cb 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 2c { 0, 1, 2, 1, 1,
1b5cc 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a 20 1, 2, 1, 2 };.
1b5cd 20 20 20 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63 v1 = or_logic
1b5ce 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 0a 20 [v1*3+v2];. }.
1b5cf 20 69 66 28 20 76 31 3d 3d 32 20 29 7b 0a 20 20 if( v1==2 ){.
1b5d0 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1b5d1 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 (pOut, MEM_Null)
1b5d2 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1b5d3 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 31 3b 0a 20 Out->u.i = v1;.
1b5d4 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1b5d5 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1b5d6 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1b5d7 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 ../* Opcode: Not
1b5d8 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1b5d9 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 ** Interpret the
1b5da 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1b5db 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f 6c 65 er P1 as a boole
1b5dc 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f 72 65 an value. Store
1b5dd 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 20 the.** boolean
1b5de 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20 72 65 complement in re
1b5df 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 74 gister P2. If t
1b5e0 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1b5e1 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a 20 4e ster P1 is .** N
1b5e2 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c ULL, then a NULL
1b5e3 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 50 32 is stored in P2
1b5e4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 ..*/.case OP_Not
1b5e5 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1b5e6 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1b5e7 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 _NOT, in1 */. p
1b5e8 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
1b5e9 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 70 Op->p2];. if( p
1b5ea 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1b5eb 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c _Null ){. sql
1b5ec 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1b5ed 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 ll(pOut);. }els
1b5ee 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 e{. sqlite3Vd
1b5ef 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f beMemSetInt64(pO
1b5f0 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62 65 ut, !sqlite3Vdbe
1b5f1 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3b IntValue(pIn1));
1b5f2 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1b5f3 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4e ./* Opcode: BitN
1b5f4 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ot P1 P2 * * *.*
1b5f5 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 *.** Interpret t
1b5f6 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1b5f7 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e 20 gister P1 as an
1b5f8 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 65 20 integer. Store
1b5f9 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d 70 the.** ones-comp
1b5fa 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 50 31 lement of the P1
1b5fb 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 value into regi
1b5fc 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 31 20 ster P2. If P1
1b5fd 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 holds.** a NULL
1b5fe 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e 55 4c then store a NUL
1b5ff 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 L in P2..*/.case
1b600 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 20 OP_BitNot: {
1b601 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1b602 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c 20 e as TK_BITNOT,
1b603 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d 20 in1 */. pOut =
1b604 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1b605 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 ];. if( pIn1->f
1b606 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1b607 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1b608 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 beMemSetNull(pOu
1b609 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
1b60a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1b60b 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 7e 73 etInt64(pOut, ~s
1b60c 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1b60d 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 ue(pIn1));. }.
1b60e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1b60f 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 20 50 code: If P1 P2 P
1b610 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1b611 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 to P2 if the va
1b612 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1b613 50 31 20 69 73 20 74 72 75 65 2e 20 20 54 68 65 P1 is true. The
1b614 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 value is.** is
1b615 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 65 20 considered true
1b616 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72 69 63 if it is numeric
1b617 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 and non-zero.
1b618 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 If the value.**
1b619 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 in P1 is NULL th
1b61a 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 en take the jump
1b61b 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e 0a if P3 is true..
1b61c 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 */./* Opcode: If
1b61d 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a Not P1 P2 P3 * *
1b61e 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 .**.** Jump to P
1b61f 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 2 if the value i
1b620 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 n register P1 is
1b621 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76 61 6c False. The val
1b622 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 ue is.** is cons
1b623 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20 69 idered true if i
1b624 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69 63 20 t has a numeric
1b625 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e 20 20 value of zero.
1b626 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 If the value.**
1b627 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 in P1 is NULL th
1b628 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 en take the jump
1b629 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e 0a if P3 is true..
1b62a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a 20 20 */.case OP_If:
1b62b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1b62c 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 63 * jump, in1 */.c
1b62d 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20 ase OP_IfNot: {
1b62e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
1b62f 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 mp, in1 */. int
1b630 20 63 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e c;. if( pIn1->
1b631 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1b632 20 29 7b 0a 20 20 20 20 63 20 3d 20 70 4f 70 2d ){. c = pOp-
1b633 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 >p3;. }else{.#i
1b634 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
1b635 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
1b636 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 c = sqlite3V
1b637 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 dbeIntValue(pIn1
1b638 29 21 3d 30 3b 0a 23 65 6c 73 65 0a 20 20 20 20 )!=0;.#else.
1b639 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 c = sqlite3VdbeR
1b63a 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 3d ealValue(pIn1)!=
1b63b 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 0.0;.#endif.
1b63c 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1b63d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 63 20 3d 20 =OP_IfNot ) c =
1b63e 21 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 20 !c;. }. if( c
1b63f 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1b640 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 >p2-1;. }. bre
1b641 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1b642 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50 32 20 50 : IsNull P1 P2 P
1b643 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1b644 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 to P2 if the va
1b645 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1b646 50 31 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20 P1 is NULL. If
1b647 50 33 20 69 73 20 67 72 65 61 74 65 72 0a 2a 2a P3 is greater.**
1b648 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e than zero, then
1b649 20 63 68 65 63 6b 20 61 6c 6c 20 76 61 6c 75 65 check all value
1b64a 73 20 72 65 67 28 50 31 29 2c 20 72 65 67 28 50 s reg(P1), reg(P
1b64b 31 2b 31 29 2c 20 0a 2a 2a 20 72 65 67 28 50 31 1+1), .** reg(P1
1b64c 2b 32 29 2c 20 2e 2e 2e 2c 20 72 65 67 28 50 31 +2), ..., reg(P1
1b64d 2b 50 33 2d 31 29 2e 0a 2a 2f 0a 63 61 73 65 20 +P3-1)..*/.case
1b64e 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 OP_IsNull: {
1b64f 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1b650 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 as TK_ISNULL, ju
1b651 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 mp, in1 */. int
1b652 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 n = pOp->p3;.
1b653 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d assert( pOp->p3=
1b654 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 31 3e 30 20 =0 || pOp->p1>0
1b655 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 66 28 );. do{. if(
1b656 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1b657 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a MEM_Null)!=0 ){.
1b658 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1b659 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 p2 - 1;. br
1b65a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 eak;. }. p
1b65b 49 6e 31 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 In1++;. }while(
1b65c 20 2d 2d 6e 20 3e 20 30 20 29 3b 0a 20 20 62 72 --n > 0 );. br
1b65d 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1b65e 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 20 50 32 e: NotNull P1 P2
1b65f 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d * * *.**.** Jum
1b660 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 p to P2 if the v
1b661 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1b662 20 50 31 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e P1 is not NULL.
1b663 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f .*/.case OP_No
1b664 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 tNull: {
1b665 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1b666 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70 2c K_NOTNULL, jump,
1b667 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 in1 */. if( (p
1b668 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1b669 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 _Null)==0 ){.
1b66a 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1b66b 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1b66c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 }../* Opcode: Se
1b66d 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 2a 20 50 32 tNumColumns * P2
1b66e 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 * * *.**.** Thi
1b66f 73 20 6f 70 63 6f 64 65 20 73 65 74 73 20 74 68 s opcode sets th
1b670 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
1b671 6d 6e 73 20 66 6f 72 20 74 68 65 20 63 75 72 73 mns for the curs
1b672 6f 72 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 or opened by the
1b673 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e .** following in
1b674 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 50 32 2e struction to P2.
1b675 0a 2a 2a 0a 2a 2a 20 41 6e 20 4f 50 5f 53 65 74 .**.** An OP_Set
1b676 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 73 20 6f 6e NumColumns is on
1b677 6c 79 20 75 73 65 66 75 6c 20 69 66 20 69 74 20 ly useful if it
1b678 6f 63 63 75 72 73 20 69 6d 6d 65 64 69 61 74 65 occurs immediate
1b679 6c 79 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6f 6e ly before .** on
1b67a 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
1b67b 6e 67 20 6f 70 63 6f 64 65 73 3a 0a 2a 2a 0a 2a ng opcodes:.**.*
1b67c 2a 20 20 20 20 20 4f 70 65 6e 52 65 61 64 0a 2a * OpenRead.*
1b67d 2a 20 20 20 20 20 4f 70 65 6e 57 72 69 74 65 0a * OpenWrite.
1b67e 2a 2a 20 20 20 20 20 4f 70 65 6e 50 73 65 75 64 ** OpenPseud
1b67f 6f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f o.**.** If the O
1b680 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 20 P_Column opcode
1b681 69 73 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 is to be execute
1b682 64 20 6f 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 d on a cursor, t
1b683 68 65 6e 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f hen.** this opco
1b684 64 65 20 6d 75 73 74 20 62 65 20 70 72 65 73 65 de must be prese
1b685 6e 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 nt immediately b
1b686 65 66 6f 72 65 20 74 68 65 20 6f 70 63 6f 64 65 efore the opcode
1b687 20 74 68 61 74 0a 2a 2a 20 6f 70 65 6e 73 20 74 that.** opens t
1b688 68 65 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 23 69 he cursor..*/.#i
1b689 66 20 30 0a 63 61 73 65 20 4f 50 5f 53 65 74 4e f 0.case OP_SetN
1b68a 75 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20 62 umColumns: {. b
1b68b 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a reak;.}.#endif..
1b68c 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d /* Opcode: Colum
1b68d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a n P1 P2 P3 P4 *.
1b68e 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 **.** Interpret
1b68f 74 68 65 20 64 61 74 61 20 74 68 61 74 20 63 75 the data that cu
1b690 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74 rsor P1 points t
1b691 6f 20 61 73 20 61 20 73 74 72 75 63 74 75 72 65 o as a structure
1b692 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 built using.**
1b693 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 the MakeRecord i
1b694 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65 nstruction. (Se
1b695 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 e the MakeRecord
1b696 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 opcode for addi
1b697 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d tional.** inform
1b698 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
1b699 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61 format of the da
1b69a 74 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68 ta.) Extract th
1b69b 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a e P2-th column.*
1b69c 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f * from this reco
1b69d 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 rd. If there ar
1b69e 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b e less that (P2+
1b69f 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e 1) .** values in
1b6a0 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 74 the record, ext
1b6a1 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a ract a NULL..**.
1b6a2 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 74 ** The value ext
1b6a3 72 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 64 racted is stored
1b6a4 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1b6a5 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f .**.** If the co
1b6a6 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65 lumn contains fe
1b6a7 77 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 6c wer than P2 fiel
1b6a8 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 74 ds, then extract
1b6a9 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a a NULL. Or,.**
1b6aa 20 69 66 20 74 68 65 20 50 34 20 61 72 67 75 6d if the P4 argum
1b6ab 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d 20 ent is a P4_MEM
1b6ac 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 use the value of
1b6ad 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74 the P4 argument
1b6ae 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c as.** the resul
1b6af 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f t..*/.case OP_Co
1b6b0 6c 75 6d 6e 3a 20 7b 0a 20 20 69 6e 74 20 70 61 lumn: {. int pa
1b6b1 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 yloadSize; /*
1b6b2 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1b6b3 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f in the record */
1b6b4 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d . int p1 = pOp-
1b6b5 3e 70 31 3b 20 20 2f 2a 20 50 31 20 76 61 6c 75 >p1; /* P1 valu
1b6b6 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 e of the opcode
1b6b7 2a 2f 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 4f */. int p2 = pO
1b6b8 70 2d 3e 70 32 3b 20 20 2f 2a 20 63 6f 6c 75 6d p->p2; /* colum
1b6b9 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72 n number to retr
1b6ba 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 75 ieve */. VdbeCu
1b6bb 72 73 6f 72 20 2a 70 43 20 3d 20 30 3b 2f 2a 20 rsor *pC = 0;/*
1b6bc 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 The VDBE cursor
1b6bd 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 3b */. char *zRec;
1b6be 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
1b6bf 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 er to complete r
1b6c0 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 ecord-data */.
1b6c1 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1b6c2 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 /* The BTree
1b6c3 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 20 cursor */. u32
1b6c4 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f *aType; /
1b6c5 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73 * aType[i] holds
1b6c6 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 the numeric typ
1b6c7 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f e of the i-th co
1b6c8 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61 lumn */. u32 *a
1b6c9 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 Offset; /*
1b6ca 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 aOffset[i] is of
1b6cb 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 fset to start of
1b6cc 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 data for i-th c
1b6cd 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e olumn */. int n
1b6ce 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a Field; /*
1b6cf 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 number of field
1b6d0 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
1b6d1 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 */. int len;
1b6d2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c /* The l
1b6d3 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 72 ength of the ser
1b6d4 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72 ialized data for
1b6d5 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 the column */.
1b6d6 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1b6d7 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1b6d8 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ter */. char *z
1b6d9 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 Data; /* P
1b6da 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 art of the recor
1b6db 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 d being decoded
1b6dc 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b */. Mem *pDest;
1b6dd 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 /* Where
1b6de 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 78 to write the ex
1b6df 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f tracted value */
1b6e0 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 . Mem sMem;
1b6e1 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f /* For sto
1b6e2 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 ring the record
1b6e3 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f being decoded */
1b6e4 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4d 65 6d .. memset(&sMem
1b6e5 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4d 65 6d , 0, sizeof(sMem
1b6e6 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31 ));. assert( p1
1b6e7 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1b6e8 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1b6e9 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 >0 && pOp->p3<=p
1b6ea 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 44 65 73 ->nMem );. pDes
1b6eb 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
1b6ec 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53 65 74 54 ->p3];. MemSetT
1b6ed 79 70 65 46 6c 61 67 28 70 44 65 73 74 2c 20 4d ypeFlag(pDest, M
1b6ee 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 EM_Null);.. /*
1b6ef 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74 73 20 This block sets
1b6f0 74 68 65 20 76 61 72 69 61 62 6c 65 20 70 61 79 the variable pay
1b6f1 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74 loadSize to be t
1b6f2 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
1b6f3 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e of. ** bytes in
1b6f4 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a the record.. *
1b6f5 2a 0a 20 20 2a 2a 20 7a 52 65 63 20 69 73 20 73 *. ** zRec is s
1b6f6 65 74 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6d et to be the com
1b6f7 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 plete text of th
1b6f8 65 20 72 65 63 6f 72 64 20 69 66 20 69 74 20 69 e record if it i
1b6f9 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a s available.. *
1b6fa 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 * The complete r
1b6fb 65 63 6f 72 64 20 74 65 78 74 20 69 73 20 61 6c ecord text is al
1b6fc 77 61 79 73 20 61 76 61 69 6c 61 62 6c 65 20 66 ways available f
1b6fd 6f 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 or pseudo-tables
1b6fe 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 63 . ** If the rec
1b6ff 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e ord is stored in
1b700 20 61 20 63 75 72 73 6f 72 2c 20 74 68 65 20 63 a cursor, the c
1b701 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 20 74 omplete record t
1b702 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62 ext. ** might b
1b703 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 e available in t
1b704 68 65 20 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 he pC->aRow cac
1b705 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68 74 he. Or it might
1b706 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49 66 not be.. ** If
1b707 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e 61 the data is una
1b708 76 61 69 6c 61 62 6c 65 2c 20 20 7a 52 65 63 20 vailable, zRec
1b709 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a is set to NULL..
1b70a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 **. ** We als
1b70b 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 o compute the nu
1b70c 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1b70d 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20 20 in the record.
1b70e 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a For cursors,. *
1b70f 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
1b710 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 columns is store
1b711 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75 72 d in the VdbeCur
1b712 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 sor.nField eleme
1b713 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 20 3d 20 nt.. */. pC =
1b714 70 2d 3e 61 70 43 73 72 5b 70 31 5d 3b 0a 20 20 p->apCsr[p1];.
1b715 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b assert( pC!=0 );
1b716 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1b717 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1b718 45 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e E. assert( pC->
1b719 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20 29 pVtabCursor==0 )
1b71a 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 ;.#endif. if( p
1b71b 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b C->pCursor!=0 ){
1b71c 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f . /* The reco
1b71d 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 rd is stored in
1b71e 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20 20 a B-Tree */.
1b71f 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1b720 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 CursorMoveto(pC)
1b721 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
1b722 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1b723 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7a 52 65 63 _error;. zRec
1b724 20 3d 20 30 3b 0a 20 20 20 20 70 43 72 73 72 20 = 0;. pCrsr
1b725 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 = pC->pCursor;.
1b726 20 20 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 if( pC->nullR
1b727 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 61 79 6c ow ){. payl
1b728 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 oadSize = 0;.
1b729 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 63 }else if( pC->c
1b72a 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 acheStatus==p->c
1b72b 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20 acheCtr ){.
1b72c 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 payloadSize = p
1b72d 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a C->payloadSize;.
1b72e 20 20 20 20 20 20 7a 52 65 63 20 3d 20 28 63 68 zRec = (ch
1b72f 61 72 2a 29 70 43 2d 3e 61 52 6f 77 3b 0a 20 20 ar*)pC->aRow;.
1b730 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e }else if( pC->
1b731 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 isIndex ){.
1b732 20 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 i64 payloadSize
1b733 36 34 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 64;. sqlite
1b734 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 3BtreeKeySize(pC
1b735 72 73 72 2c 20 26 70 61 79 6c 6f 61 64 53 69 7a rsr, &payloadSiz
1b736 65 36 34 29 3b 0a 20 20 20 20 20 20 70 61 79 6c e64);. payl
1b737 6f 61 64 53 69 7a 65 20 3d 20 28 69 6e 74 29 70 oadSize = (int)p
1b738 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 ayloadSize64;.
1b739 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
1b73a 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53 qlite3BtreeDataS
1b73b 69 7a 65 28 70 43 72 73 72 2c 20 28 75 33 32 20 ize(pCrsr, (u32
1b73c 2a 29 26 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b *)&payloadSize);
1b73d 0a 20 20 20 20 7d 0a 20 20 20 20 6e 46 69 65 6c . }. nFiel
1b73e 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a d = pC->nField;.
1b73f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
1b740 65 72 74 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 ert( pC->pseudoT
1b741 61 62 6c 65 20 29 3b 0a 20 20 20 20 2f 2a 20 54 able );. /* T
1b742 68 65 20 72 65 63 6f 72 64 20 69 73 20 74 68 65 he record is the
1b743 20 73 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20 61 sole entry of a
1b744 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f pseudo-table */
1b745 0a 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 . payloadSize
1b746 20 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20 20 = pC->nData;.
1b747 20 20 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44 61 zRec = pC->pDa
1b748 74 61 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 ta;. pC->cach
1b749 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1b74a 53 54 41 4c 45 3b 0a 20 20 20 20 61 73 73 65 72 STALE;. asser
1b74b 74 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d t( payloadSize==
1b74c 30 20 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b 0a 0 || zRec!=0 );.
1b74d 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 2d nField = pC-
1b74e 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 43 72 >nField;. pCr
1b74f 73 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f sr = 0;. }.. /
1b750 2a 20 49 66 20 70 61 79 6c 6f 61 64 53 69 7a 65 * If payloadSize
1b751 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 74 is 0, then just
1b752 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a 2f store a NULL */
1b753 0a 20 20 69 66 28 20 70 61 79 6c 6f 61 64 53 69 . if( payloadSi
1b754 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 ze==0 ){. ass
1b755 65 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 ert( pDest->flag
1b756 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 s&MEM_Null );.
1b757 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e goto op_column
1b758 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 _out;. }. if(
1b759 70 61 79 6c 6f 61 64 53 69 7a 65 3e 64 62 2d 3e payloadSize>db->
1b75a 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1b75b 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
1b75c 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1b75d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
1b75e 70 32 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20 p2<nField );..
1b75f 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 73 /* Read and pars
1b760 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 61 64 e the table head
1b761 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72 er. Store the r
1b762 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 70 61 esults of the pa
1b763 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 rse. ** into th
1b764 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 e record header
1b765 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f 66 20 cache fields of
1b766 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f the cursor.. */
1b767 0a 20 20 61 54 79 70 65 20 3d 20 70 43 2d 3e 61 . aType = pC->a
1b768 54 79 70 65 3b 0a 20 20 69 66 28 20 70 43 2d 3e Type;. if( pC->
1b769 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e cacheStatus==p->
1b76a 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 cacheCtr ){.
1b76b 61 4f 66 66 73 65 74 20 3d 20 70 43 2d 3e 61 4f aOffset = pC->aO
1b76c 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a ffset;. }else{.
1b76d 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 u8 *zIdx;
1b76e 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
1b76f 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 to header */.
1b770 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 u8 *zEndHdr;
1b771 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
1b772 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 first byte after
1b773 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 the header */.
1b774 20 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 int offset;
1b775 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
1b776 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 to the data */.
1b777 20 20 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b 20 int szHdrSz;
1b778 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
1b779 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 he header size f
1b77a 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 ield at start of
1b77b 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 record */. i
1b77c 6e 74 20 61 76 61 69 6c 20 3d 20 30 3b 20 20 20 nt avail = 0;
1b77d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1b77e 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 es of available
1b77f 64 61 74 61 20 2a 2f 0a 0a 20 20 20 20 61 73 73 data */.. ass
1b780 65 72 74 28 61 54 79 70 65 29 3b 0a 20 20 20 20 ert(aType);.
1b781 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 4f pC->aOffset = aO
1b782 66 66 73 65 74 20 3d 20 26 61 54 79 70 65 5b 6e ffset = &aType[n
1b783 46 69 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d 3e Field];. pC->
1b784 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61 payloadSize = pa
1b785 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 70 yloadSize;. p
1b786 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1b787 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 p->cacheCtr;..
1b788 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 /* Figure out
1b789 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 how many bytes
1b78a 61 72 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 are in the heade
1b78b 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 65 r */. if( zRe
1b78c 63 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61 c ){. zData
1b78d 20 3d 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c = zRec;. }el
1b78e 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 se{. if( pC
1b78f 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 ->isIndex ){.
1b790 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 zData = (ch
1b791 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 ar*)sqlite3Btree
1b792 4b 65 79 46 65 74 63 68 28 70 43 72 73 72 2c 20 KeyFetch(pCrsr,
1b793 26 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d &avail);. }
1b794 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 44 else{. zD
1b795 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c ata = (char*)sql
1b796 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 ite3BtreeDataFet
1b797 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c ch(pCrsr, &avail
1b798 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1b799 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28 /* If KeyFetch(
1b79a 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61 )/DataFetch() ma
1b79b 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65 naged to get the
1b79c 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c entire payload,
1b79d 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74 . ** save t
1b79e 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68 he payload in th
1b79f 65 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 e pC->aRow cache
1b7a0 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76 . That will sav
1b7a1 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 e us from.
1b7a2 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b ** having to mak
1b7a3 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c e additional cal
1b7a4 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 ls to fetch the
1b7a5 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 content portion
1b7a6 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 of. ** the
1b7a7 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f record.. */
1b7a8 0a 20 20 20 20 20 20 69 66 28 20 61 76 61 69 6c . if( avail
1b7a9 3e 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b >=payloadSize ){
1b7aa 0a 20 20 20 20 20 20 20 20 7a 52 65 63 20 3d 20 . zRec =
1b7ab 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70 zData;. p
1b7ac 43 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 7a C->aRow = (u8*)z
1b7ad 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73 Data;. }els
1b7ae 65 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 e{. pC->a
1b7af 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d Row = 0;. }
1b7b0 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 . }. /* Th
1b7b1 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
1b7b2 72 74 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c rt is true in al
1b7b3 6c 20 63 61 73 65 73 20 61 63 63 65 70 74 20 77 l cases accept w
1b7b4 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 hen. ** the d
1b7b5 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 atabase file has
1b7b6 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 been corrupted
1b7b7 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 externally..
1b7b8 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 7a 52 ** assert( zR
1b7b9 65 63 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e 3d ec!=0 || avail>=
1b7ba 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 61 payloadSize || a
1b7bb 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20 vail>=9 ); */.
1b7bc 20 20 73 7a 48 64 72 53 7a 20 3d 20 67 65 74 56 szHdrSz = getV
1b7bd 61 72 69 6e 74 33 32 28 28 75 38 2a 29 7a 44 61 arint32((u8*)zDa
1b7be 74 61 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 ta, offset);..
1b7bf 20 20 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63 /* The KeyFetc
1b7c0 68 28 29 20 6f 72 20 44 61 74 61 46 65 74 63 68 h() or DataFetch
1b7c1 28 29 20 61 62 6f 76 65 20 61 72 65 20 66 61 73 () above are fas
1b7c2 74 20 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74 t and will get t
1b7c3 68 65 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a he entire. **
1b7c4 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 record header i
1b7c5 6e 20 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42 n most cases. B
1b7c6 75 74 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69 ut they will fai
1b7c7 6c 20 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d l to get the com
1b7c8 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63 plete. ** rec
1b7c9 6f 72 64 20 68 65 61 64 65 72 20 69 66 20 74 68 ord header if th
1b7ca 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 e record header
1b7cb 64 6f 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20 does not fit on
1b7cc 61 20 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20 a single page.
1b7cd 20 20 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72 ** in the B-Tr
1b7ce 65 65 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 ee. When that h
1b7cf 61 70 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69 appens, use sqli
1b7d0 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1b7d1 72 65 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 ree() to. **
1b7d2 61 63 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70 acquire the comp
1b7d3 6c 65 74 65 20 68 65 61 64 65 72 20 74 65 78 74 lete header text
1b7d4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1b7d5 20 21 7a 52 65 63 20 26 26 20 61 76 61 69 6c 3c !zRec && avail<
1b7d6 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 offset ){.
1b7d7 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a sMem.flags = 0;.
1b7d8 20 20 20 20 20 20 73 4d 65 6d 2e 64 62 20 3d 20 sMem.db =
1b7d9 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 0;. rc = sq
1b7da 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d lite3VdbeMemFrom
1b7db 42 74 72 65 65 28 70 43 72 73 72 2c 20 30 2c 20 Btree(pCrsr, 0,
1b7dc 6f 66 66 73 65 74 2c 20 70 43 2d 3e 69 73 49 6e offset, pC->isIn
1b7dd 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 dex, &sMem);.
1b7de 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1b7df 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1b7e0 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f goto op_column_o
1b7e1 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ut;. }.
1b7e2 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a zData = sMem.z
1b7e3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 6e 64 ;. }. zEnd
1b7e4 48 64 72 20 3d 20 28 75 38 20 2a 29 26 7a 44 61 Hdr = (u8 *)&zDa
1b7e5 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 ta[offset];.
1b7e6 7a 49 64 78 20 3d 20 28 75 38 20 2a 29 26 7a 44 zIdx = (u8 *)&zD
1b7e7 61 74 61 5b 73 7a 48 64 72 53 7a 5d 3b 0a 0a 20 ata[szHdrSz];..
1b7e8 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68 /* Scan the h
1b7e9 65 61 64 65 72 20 61 6e 64 20 75 73 65 20 69 74 eader and use it
1b7ea 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 to fill in the
1b7eb 61 54 79 70 65 5b 5d 20 61 6e 64 20 61 4f 66 66 aType[] and aOff
1b7ec 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 set[]. ** arr
1b7ed 61 79 73 2e 20 20 61 54 79 70 65 5b 69 5d 20 77 ays. aType[i] w
1b7ee 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 ill contain the
1b7ef 74 79 70 65 20 69 6e 74 65 67 65 72 20 66 6f 72 type integer for
1b7f0 20 74 68 65 20 69 2d 74 68 0a 20 20 20 20 2a 2a the i-th. **
1b7f1 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 61 4f 66 66 column and aOff
1b7f2 73 65 74 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 set[i] will cont
1b7f3 61 69 6e 20 74 68 65 20 6f 66 66 73 65 74 20 66 ain the offset f
1b7f4 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
1b7f5 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 g. ** of the
1b7f6 72 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 record to the st
1b7f7 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 art of the data
1b7f8 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 6f 6c for the i-th col
1b7f9 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 umn. */. f
1b7fa 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 69 65 6c 64 or(i=0; i<nField
1b7fb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
1b7fc 28 20 7a 49 64 78 3c 7a 45 6e 64 48 64 72 20 29 ( zIdx<zEndHdr )
1b7fd 7b 0a 20 20 20 20 20 20 20 20 61 4f 66 66 73 65 {. aOffse
1b7fe 74 5b 69 5d 20 3d 20 6f 66 66 73 65 74 3b 0a 20 t[i] = offset;.
1b7ff 20 20 20 20 20 20 20 7a 49 64 78 20 2b 3d 20 67 zIdx += g
1b800 65 74 56 61 72 69 6e 74 33 32 28 7a 49 64 78 2c etVarint32(zIdx,
1b801 20 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20 aType[i]);.
1b802 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 71 offset += sq
1b803 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1b804 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d 29 ypeLen(aType[i])
1b805 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1b806 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 20 69 /* If i i
1b807 73 20 6c 65 73 73 20 74 68 61 74 20 6e 46 69 65 s less that nFie
1b808 6c 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 ld, then there a
1b809 72 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69 re less fields i
1b80a 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a n this. *
1b80b 2a 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65 * record than Se
1b80c 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69 tNumColumns indi
1b80d 63 61 74 65 64 20 74 68 65 72 65 20 61 72 65 20 cated there are
1b80e 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 columns in the.
1b80f 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e ** table.
1b810 20 53 65 74 20 74 68 65 20 6f 66 66 73 65 74 20 Set the offset
1b811 66 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63 6f for any extra co
1b812 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 6e lumns not presen
1b813 74 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 t in. **
1b814 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30 2e the record to 0.
1b815 20 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 65 This tells code
1b816 20 62 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20 below to store
1b817 61 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 2a a NULL. *
1b818 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73 * instead of des
1b819 65 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c erializing a val
1b81a 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f ue from the reco
1b81b 72 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 rd.. */.
1b81c 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69 aOffset[i
1b81d 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 ] = 0;. }.
1b81e 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1b81f 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1b820 73 4d 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d 2e sMem);. sMem.
1b821 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1b822 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 ;.. /* If we
1b823 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68 have read more h
1b824 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20 eader data than
1b825 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e was contained in
1b826 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20 the header,.
1b827 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e ** or if the en
1b828 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 d of the last fi
1b829 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 eld appears to b
1b82a 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
1b82b 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 f the. ** rec
1b82c 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65 ord, or if the e
1b82d 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 nd of the last f
1b82e 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 ield appears to
1b82f 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e be before the en
1b830 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 d. ** of the
1b831 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c record (when all
1b832 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29 fields present)
1b833 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62 , then we must b
1b834 65 20 64 65 61 6c 69 6e 67 20 0a 20 20 20 20 2a e dealing . *
1b835 2a 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 * with a corrupt
1b836 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a database.. *
1b837 2f 0a 20 20 20 20 69 66 28 20 7a 49 64 78 3e 7a /. if( zIdx>z
1b838 45 6e 64 48 64 72 20 7c 7c 20 6f 66 66 73 65 74 EndHdr || offset
1b839 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 0a 20 20 >payloadSize .
1b83a 20 20 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45 6e || (zIdx==zEn
1b83b 64 48 64 72 20 26 26 20 6f 66 66 73 65 74 21 3d dHdr && offset!=
1b83c 70 61 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a payloadSize) ){.
1b83d 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1b83e 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1b83f 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f goto op_co
1b840 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a lumn_out;. }.
1b841 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 }.. /* Get th
1b842 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 e column informa
1b843 74 69 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65 74 tion. If aOffset
1b844 5b 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f [p2] is non-zero
1b845 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65 73 , then . ** des
1b846 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c erialize the val
1b847 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f ue from the reco
1b848 72 64 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70 rd. If aOffset[p
1b849 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 2] is zero,. **
1b84a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 then there are
1b84b 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64 not enough field
1b84c 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
1b84d 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 20 to satisfy the.
1b84e 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e ** request. In
1b84f 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 20 this case, set
1b850 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f the value NULL o
1b851 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69 73 r to P4 if P4 is
1b852 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 . ** a pointer
1b853 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e to a Mem object.
1b854 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66 66 . */. if( aOff
1b855 73 65 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20 61 set[p2] ){. a
1b856 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1b857 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 E_OK );. if(
1b858 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71 zRec ){. sq
1b859 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1b85a 61 73 65 45 78 74 65 72 6e 61 6c 28 70 44 65 73 aseExternal(pDes
1b85b 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 t);. sqlite
1b85c 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 3VdbeSerialGet((
1b85d 75 38 20 2a 29 26 7a 52 65 63 5b 61 4f 66 66 73 u8 *)&zRec[aOffs
1b85e 65 74 5b 70 32 5d 5d 2c 20 61 54 79 70 65 5b 70 et[p2]], aType[p
1b85f 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 2], pDest);.
1b860 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 65 6e }else{. len
1b861 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1b862 72 69 61 6c 54 79 70 65 4c 65 6e 28 61 54 79 70 rialTypeLen(aTyp
1b863 65 5b 70 32 5d 29 3b 0a 20 20 20 20 20 20 73 71 e[p2]);. sq
1b864 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
1b865 28 26 73 4d 65 6d 2c 20 70 44 65 73 74 29 3b 0a (&sMem, pDest);.
1b866 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1b867 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 e3VdbeMemFromBtr
1b868 65 65 28 70 43 72 73 72 2c 20 61 4f 66 66 73 65 ee(pCrsr, aOffse
1b869 74 5b 70 32 5d 2c 20 6c 65 6e 2c 20 70 43 2d 3e t[p2], len, pC->
1b86a 69 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b isIndex, &sMem);
1b86b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1b86c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1b86d 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 goto op_colu
1b86e 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a mn_out;. }.
1b86f 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 73 4d zData = sM
1b870 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 em.z;. sqli
1b871 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 te3VdbeSerialGet
1b872 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 61 54 79 ((u8*)zData, aTy
1b873 70 65 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a pe[p2], pDest);.
1b874 20 20 20 20 7d 0a 20 20 20 20 70 44 65 73 74 2d }. pDest-
1b875 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b >enc = encoding;
1b876 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
1b877 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1b878 34 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 4_MEM ){. s
1b879 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
1b87a 6c 6c 6f 77 43 6f 70 79 28 70 44 65 73 74 2c 20 llowCopy(pDest,
1b87b 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45 pOp->p4.pMem, ME
1b87c 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 7d M_Static);. }
1b87d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
1b87e 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 rt( pDest->flags
1b87f 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 &MEM_Null );.
1b880 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
1b881 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 we dynamically a
1b882 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 llocated space t
1b883 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 o hold the data
1b884 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c (in the. ** sql
1b885 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 ite3VdbeMemFromB
1b886 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 tree() call abov
1b887 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 e) then transfer
1b888 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 control of that
1b889 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c . ** dynamicall
1b88a 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 y allocated spac
1b88b 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20 70 44 e over to the pD
1b88c 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20 est structure..
1b88d 20 2a 2a 20 54 68 69 73 20 70 72 65 76 65 6e 74 ** This prevent
1b88e 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79 2e s a memory copy.
1b88f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 4d 65 6d . */. if( sMem
1b890 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 .zMalloc ){.
1b891 61 73 73 65 72 74 28 20 73 4d 65 6d 2e 7a 3d 3d assert( sMem.z==
1b892 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a sMem.zMalloc );.
1b893 20 20 20 20 61 73 73 65 72 74 28 20 21 28 70 44 assert( !(pD
1b894 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d est->flags & MEM
1b895 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73 _Dyn) );. ass
1b896 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e 66 6c ert( !(pDest->fl
1b897 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c ags & (MEM_Blob|
1b898 4d 45 4d 5f 53 74 72 29 29 20 7c 7c 20 70 44 65 MEM_Str)) || pDe
1b899 73 74 2d 3e 7a 3d 3d 73 4d 65 6d 2e 7a 20 29 3b st->z==sMem.z );
1b89a 0a 20 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 . pDest->flag
1b89b 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d s &= ~(MEM_Ephem
1b89c 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 |MEM_Static);.
1b89d 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c pDest->flags |
1b89e 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 = MEM_Term;.
1b89f 70 44 65 73 74 2d 3e 7a 20 3d 20 73 4d 65 6d 2e pDest->z = sMem.
1b8a0 7a 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 7a 4d z;. pDest->zM
1b8a1 61 6c 6c 6f 63 20 3d 20 73 4d 65 6d 2e 7a 4d 61 alloc = sMem.zMa
1b8a2 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 lloc;. }.. rc
1b8a3 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
1b8a4 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 44 MakeWriteable(pD
1b8a5 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e est);..op_column
1b8a6 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45 5f 4d _out:. UPDATE_M
1b8a7 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 73 AX_BLOBSIZE(pDes
1b8a8 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 t);. REGISTER_T
1b8a9 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44 RACE(pOp->p3, pD
1b8aa 65 73 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d est);. break;.}
1b8ab 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66 66 ../* Opcode: Aff
1b8ac 69 6e 69 74 79 20 50 31 20 50 32 20 2a 20 50 34 inity P1 P2 * P4
1b8ad 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 *.**.** Apply a
1b8ae 66 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20 72 ffinities to a r
1b8af 61 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69 73 ange of P2 regis
1b8b0 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 ters starting wi
1b8b1 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 th P1..**.** P4
1b8b2 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 is a string that
1b8b3 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 is P2 character
1b8b4 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68 20 s long. The nth
1b8b5 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 character of the
1b8b6 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 .** string indic
1b8b7 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 ates the column
1b8b8 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
1b8b9 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
1b8ba 20 74 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f the nth.** memo
1b8bb 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72 ry cell in the r
1b8bc 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 ange..*/.case OP
1b8bd 5f 41 66 66 69 6e 69 74 79 3a 20 7b 0a 20 20 63 _Affinity: {. c
1b8be 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 20 3d har *zAffinity =
1b8bf 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 4d 65 pOp->p4.z;. Me
1b8c0 6d 20 2a 70 44 61 74 61 30 20 3d 20 26 70 2d 3e m *pData0 = &p->
1b8c1 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1b8c2 20 4d 65 6d 20 2a 70 4c 61 73 74 20 3d 20 26 70 Mem *pLast = &p
1b8c3 44 61 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d Data0[pOp->p2-1]
1b8c4 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a 0a ;. Mem *pRec;..
1b8c5 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 for(pRec=pData
1b8c6 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 0; pRec<=pLast;
1b8c7 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 70 pRec++){. Exp
1b8c8 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 andBlob(pRec);.
1b8c9 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 applyAffinity
1b8ca 28 70 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 79 (pRec, zAffinity
1b8cb 5b 70 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 65 [pRec-pData0], e
1b8cc 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 ncoding);. }.
1b8cd 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1b8ce 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 20 ode: MakeRecord
1b8cf 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
1b8d0 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32 20 72 .** Convert P2 r
1b8d1 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 egisters beginni
1b8d2 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74 6f 20 ng with P1 into
1b8d3 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a a single entry.*
1b8d4 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 * suitable for u
1b8d5 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65 63 se as a data rec
1b8d6 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 ord in a databas
1b8d7 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61 20 e table or as a
1b8d8 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e 64 key.** in an ind
1b8d9 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 6c 73 ex. The details
1b8da 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 61 of the format a
1b8db 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20 61 73 re irrelevant as
1b8dc 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65 20 long as.** the
1b8dd 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 OP_Column opcode
1b8de 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65 20 can decode the
1b8df 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a 2a 2a record later..**
1b8e0 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72 63 65 Refer to source
1b8e1 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 code comments f
1b8e2 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f or the details o
1b8e3 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20 f the record.**
1b8e4 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 format..**.** P4
1b8e5 20 6d 61 79 20 62 65 20 61 20 73 74 72 69 6e 67 may be a string
1b8e6 20 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 that is P2 char
1b8e7 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 acters long. Th
1b8e8 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 e nth character
1b8e9 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 of the.** string
1b8ea 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 indicates the c
1b8eb 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 olumn affinity t
1b8ec 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 hat should be us
1b8ed 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a ed for the nth.*
1b8ee 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 * field of the i
1b8ef 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 ndex key..**.**
1b8f0 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d The mapping from
1b8f1 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 66 character to af
1b8f2 66 69 6e 69 74 79 20 69 73 20 67 69 76 65 6e 20 finity is given
1b8f3 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 by the SQLITE_AF
1b8f4 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65 66 F_.** macros def
1b8f5 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e ined in sqliteIn
1b8f6 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 t.h..**.** If P4
1b8f7 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 6c is NULL then al
1b8f8 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73 20 68 l index fields h
1b8f9 61 76 65 20 74 68 65 20 61 66 66 69 6e 69 74 79 ave the affinity
1b8fa 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f NONE..*/.case O
1b8fb 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b 0a P_MakeRecord: {.
1b8fc 20 20 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 /* Assuming th
1b8fd 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 61 69 6e e record contain
1b8fe 73 20 4e 20 66 69 65 6c 64 73 2c 20 74 68 65 20 s N fields, the
1b8ff 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c 6f record format lo
1b900 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74 68 oks. ** like th
1b901 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d is:. **. ** --
1b902 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b903 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b904 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b905 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b906 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 ------. ** | hd
1b907 72 2d 73 69 7a 65 20 7c 20 74 79 70 65 20 30 20 r-size | type 0
1b908 7c 20 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20 7c | type 1 | ... |
1b909 20 74 79 70 65 20 4e 2d 31 20 7c 20 64 61 74 61 type N-1 | data
1b90a 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 0 | ... | data N
1b90b 2d 31 20 7c 20 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d -1 | . ** -----
1b90c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b90d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b90e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b90f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b910 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 61 ---. **. ** Da
1b911 74 61 28 30 29 20 69 73 20 74 61 6b 65 6e 20 66 ta(0) is taken f
1b912 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 2e rom register P1.
1b913 20 20 44 61 74 61 28 31 29 20 63 6f 6d 65 73 20 Data(1) comes
1b914 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 from register P1
1b915 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 +1. ** and so f
1b916 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 roth.. **. **
1b917 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64 20 Each type field
1b918 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70 72 is a varint repr
1b919 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72 esenting the ser
1b91a 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 ial type of the
1b91b 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 . ** correspond
1b91c 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 ing data element
1b91d 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 64 62 (see sqlite3Vdb
1b91e 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20 eSerialType()).
1b91f 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a The. ** hdr-siz
1b920 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20 e field is also
1b921 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20 69 a varint which i
1b922 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f s the offset fro
1b923 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a m the beginning.
1b924 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f ** of the reco
1b925 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 2a rd to data0.. *
1b926 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f /. u8 *zNewReco
1b927 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 rd; /* A
1b928 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 buffer to hold t
1b929 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 he data for the
1b92a 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 new record */.
1b92b 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 Mem *pRec;
1b92c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 /* The ne
1b92d 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 w record */. u6
1b92e 34 20 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 20 4 nData = 0;
1b92f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1b930 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 f bytes of data
1b931 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e space */. int n
1b932 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Hdr = 0;
1b933 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1b934 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 ytes of header s
1b935 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 pace */. i64 nB
1b936 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 yte = 0;
1b937 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 /* Data space r
1b938 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 equired for this
1b939 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 record */. int
1b93a 20 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 nZero = 0;
1b93b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1b93c 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 zero bytes at t
1b93d 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 he end of the re
1b93e 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 cord */. int nV
1b93f 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 arint;
1b940 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1b941 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 tes in a varint
1b942 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f */. u32 serial_
1b943 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 type; /* T
1b944 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d ype field */. M
1b945 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 em *pData0;
1b946 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 /* First f
1b947 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 ield to be combi
1b948 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 ned into the rec
1b949 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c ord */. Mem *pL
1b94a 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ast;
1b94b 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 /* Last field of
1b94c 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1b94d 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 int nField;
1b94e 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1b94f 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
1b950 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 he record */. c
1b951 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 har *zAffinity;
1b952 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 /* The aff
1b953 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 inity string for
1b954 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1b955 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 int file_format
1b956 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 ; /* File
1b957 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f format to use fo
1b958 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 r encoding */.
1b959 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
1b95a 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
1b95b 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f used in zNewReco
1b95c 72 64 5b 5d 20 2a 2f 0a 0a 20 20 6e 46 69 65 6c rd[] */.. nFiel
1b95d 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 7a d = pOp->p1;. z
1b95e 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e Affinity = pOp->
1b95f 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 p4.z;. assert(
1b960 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d nField>0 && pOp-
1b961 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 >p2>0 && pOp->p2
1b962 2b 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d +nField<=p->nMem
1b963 2b 31 20 29 3b 0a 20 20 70 44 61 74 61 30 20 3d +1 );. pData0 =
1b964 20 26 70 2d 3e 61 4d 65 6d 5b 6e 46 69 65 6c 64 &p->aMem[nField
1b965 5d 3b 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f ];. nField = pO
1b966 70 2d 3e 70 32 3b 0a 20 20 70 4c 61 73 74 20 3d p->p2;. pLast =
1b967 20 26 70 44 61 74 61 30 5b 6e 46 69 65 6c 64 2d &pData0[nField-
1b968 31 5d 3b 0a 20 20 66 69 6c 65 5f 66 6f 72 6d 61 1];. file_forma
1b969 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 t = p->minWriteF
1b96a 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a ileFormat;.. /*
1b96b 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 Loop through th
1b96c 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 e elements that
1b96d 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68 65 will make up the
1b96e 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 75 72 record to figur
1b96f 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77 20 6d e. ** out how m
1b970 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 71 uch space is req
1b971 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 6e 65 uired for the ne
1b972 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 w record.. */.
1b973 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 for(pRec=pData0
1b974 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 ; pRec<=pLast; p
1b975 52 65 63 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 Rec++){. int
1b976 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20 7a 41 66 len;. if( zAf
1b977 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 finity ){.
1b978 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 applyAffinity(pR
1b979 65 63 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 52 ec, zAffinity[pR
1b97a 65 63 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f ec-pData0], enco
1b97b 64 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ding);. }.
1b97c 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 if( pRec->flags
1b97d 26 4d 45 4d 5f 5a 65 72 6f 20 26 26 20 70 52 65 &MEM_Zero && pRe
1b97e 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 c->n>0 ){.
1b97f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 sqlite3VdbeMemEx
1b980 70 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a pandBlob(pRec);.
1b981 20 20 20 20 7d 0a 20 20 20 20 73 65 72 69 61 6c }. serial
1b982 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 _type = sqlite3V
1b983 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 dbeSerialType(pR
1b984 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 ec, file_format)
1b985 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 ;. len = sqli
1b986 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1b987 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 eLen(serial_type
1b988 29 3b 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 );. nData +=
1b989 6c 65 6e 3b 0a 20 20 20 20 6e 48 64 72 20 2b 3d len;. nHdr +=
1b98a 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 sqlite3VarintLe
1b98b 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a n(serial_type);.
1b98c 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c if( pRec->fl
1b98d 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
1b98e 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 {. /* Only
1b98f 70 75 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 pure zero-filled
1b990 20 42 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 6e BLOBs can be in
1b991 70 75 74 20 74 6f 20 74 68 69 73 20 4f 70 63 6f put to this Opco
1b992 64 65 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 20 de.. ** We
1b993 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f do not allow blo
1b994 62 73 20 77 69 74 68 20 61 20 70 72 65 66 69 78 bs with a prefix
1b995 20 61 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c and a zero-fill
1b996 65 64 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 ed tail. */.
1b997 20 20 6e 5a 65 72 6f 20 2b 3d 20 70 52 65 63 2d nZero += pRec-
1b998 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 >u.nZero;. }e
1b999 6c 73 65 20 69 66 28 20 6c 65 6e 20 29 7b 0a 20 lse if( len ){.
1b99a 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a nZero = 0;.
1b99b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1b99c 41 64 64 20 74 68 65 20 69 6e 69 74 69 61 6c 20 Add the initial
1b99d 68 65 61 64 65 72 20 76 61 72 69 6e 74 20 61 6e header varint an
1b99e 64 20 74 6f 74 61 6c 20 74 68 65 20 73 69 7a 65 d total the size
1b99f 20 2a 2f 0a 20 20 6e 48 64 72 20 2b 3d 20 6e 56 */. nHdr += nV
1b9a0 61 72 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 arint = sqlite3V
1b9a1 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29 3b 0a arintLen(nHdr);.
1b9a2 20 20 69 66 28 20 6e 56 61 72 69 6e 74 3c 73 71 if( nVarint<sq
1b9a3 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 6e lite3VarintLen(n
1b9a4 48 64 72 29 20 29 7b 0a 20 20 20 20 6e 48 64 72 Hdr) ){. nHdr
1b9a5 2b 2b 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 ++;. }. nByte
1b9a6 3d 20 6e 48 64 72 2b 6e 44 61 74 61 2d 6e 5a 65 = nHdr+nData-nZe
1b9a7 72 6f 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e ro;. if( nByte>
1b9a8 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1b9a9 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
1b9aa 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f ){. goto too_
1b9ab 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d big;. }.. /* M
1b9ac 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 75 74 ake sure the out
1b9ad 70 75 74 20 72 65 67 69 73 74 65 72 20 68 61 73 put register has
1b9ae 20 61 20 62 75 66 66 65 72 20 6c 61 72 67 65 20 a buffer large
1b9af 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72 65 20 enough to store
1b9b0 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 72 65 . ** the new re
1b9b1 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70 75 74 cord. The output
1b9b2 20 72 65 67 69 73 74 65 72 20 28 70 4f 70 2d 3e register (pOp->
1b9b3 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 p3) is not allow
1b9b4 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20 6f 6e ed to. ** be on
1b9b5 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 72 e of the input r
1b9b6 65 67 69 73 74 65 72 73 20 28 62 65 63 61 75 73 egisters (becaus
1b9b7 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 e the following
1b9b8 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73 71 6c call to. ** sql
1b9b9 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1b9ba 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65 72 20 ) could clobber
1b9bb 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f 72 65 the value before
1b9bc 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a 20 20 it is used)..
1b9bd 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1b9be 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c 7c 20 ->p3<pOp->p1 ||
1b9bf 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 31 pOp->p3>=pOp->p1
1b9c0 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 70 4f +pOp->p2 );. pO
1b9c1 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1b9c2 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 73 71 p->p3];. if( sq
1b9c3 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1b9c4 28 70 4f 75 74 2c 20 28 69 6e 74 29 6e 42 79 74 (pOut, (int)nByt
1b9c5 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 e, 0) ){. got
1b9c6 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 o no_mem;. }.
1b9c7 7a 4e 65 77 52 65 63 6f 72 64 20 3d 20 28 75 38 zNewRecord = (u8
1b9c8 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 2f *)pOut->z;.. /
1b9c9 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 63 6f * Write the reco
1b9ca 72 64 20 2a 2f 0a 20 20 69 20 3d 20 70 75 74 56 rd */. i = putV
1b9cb 61 72 69 6e 74 33 32 28 7a 4e 65 77 52 65 63 6f arint32(zNewReco
1b9cc 72 64 2c 20 6e 48 64 72 29 3b 0a 20 20 66 6f 72 rd, nHdr);. for
1b9cd 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 (pRec=pData0; pR
1b9ce 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b ec<=pLast; pRec+
1b9cf 2b 29 7b 0a 20 20 20 20 73 65 72 69 61 6c 5f 74 +){. serial_t
1b9d0 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ype = sqlite3Vdb
1b9d1 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 65 63 eSerialType(pRec
1b9d2 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a , file_format);.
1b9d3 20 20 20 20 69 20 2b 3d 20 70 75 74 56 61 72 69 i += putVari
1b9d4 6e 74 33 32 28 26 7a 4e 65 77 52 65 63 6f 72 64 nt32(&zNewRecord
1b9d5 5b 69 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 [i], serial_type
1b9d6 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69 61 ); /* seria
1b9d7 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20 20 l type */. }.
1b9d8 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b for(pRec=pData0;
1b9d9 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 pRec<=pLast; pR
1b9da 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61 ec++){ /* seria
1b9db 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69 20 l data */. i
1b9dc 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 += sqlite3VdbeSe
1b9dd 72 69 61 6c 50 75 74 28 26 7a 4e 65 77 52 65 63 rialPut(&zNewRec
1b9de 6f 72 64 5b 69 5d 2c 20 28 69 6e 74 29 28 6e 42 ord[i], (int)(nB
1b9df 79 74 65 2d 69 29 2c 20 70 52 65 63 2c 66 69 6c yte-i), pRec,fil
1b9e0 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20 e_format);. }.
1b9e1 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 42 79 74 assert( i==nByt
1b9e2 65 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 e );.. assert(
1b9e3 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 pOp->p3>0 && pOp
1b9e4 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1b9e5 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e . pOut->n = (in
1b9e6 74 29 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d t)nByte;. pOut-
1b9e7 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f >flags = MEM_Blo
1b9e8 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70 b | MEM_Dyn;. p
1b9e9 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 Out->xDel = 0;.
1b9ea 20 69 66 28 20 6e 5a 65 72 6f 20 29 7b 0a 20 20 if( nZero ){.
1b9eb 20 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 6f 20 pOut->u.nZero
1b9ec 3d 20 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4f 75 = nZero;. pOu
1b9ed 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f t->flags |= MEM_
1b9ee 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 Zero;. }. pOut
1b9ef 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 ->enc = SQLITE_U
1b9f0 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65 TF8; /* In case
1b9f1 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 65 the blob is eve
1b9f2 72 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 74 r converted to t
1b9f3 65 78 74 20 2a 2f 0a 20 20 52 45 47 49 53 54 45 ext */. REGISTE
1b9f4 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c R_TRACE(pOp->p3,
1b9f5 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 41 54 45 pOut);. UPDATE
1b9f6 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f _MAX_BLOBSIZE(pO
1b9f7 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ut);. break;.}.
1b9f8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 75 6e ./* Opcode: Coun
1b9f9 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1b9fa 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 6e 75 .** Store the nu
1b9fb 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
1b9fc 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 (an integer valu
1b9fd 65 29 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 e) in the table
1b9fe 6f 72 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 70 65 or index .** ope
1b9ff 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 31 ned by cursor P1
1ba00 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a in register P2.
1ba01 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1ba02 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e E_OMIT_BTREECOUN
1ba03 54 0a 63 61 73 65 20 4f 50 5f 43 6f 75 6e 74 3a T.case OP_Count:
1ba04 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 { /* ou
1ba05 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
1ba06 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a 20 . i64 nEntry;.
1ba07 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1ba08 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1ba09 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 >p1]->pCursor;.
1ba0a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1ba0b 65 65 43 6f 75 6e 74 28 70 43 72 73 72 2c 20 26 eeCount(pCrsr, &
1ba0c 6e 45 6e 74 72 79 29 3b 0a 20 20 70 4f 75 74 2d nEntry);. pOut-
1ba0d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1ba0e 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 ;. pOut->u.i =
1ba0f 6e 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b 3b nEntry;. break;
1ba10 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 .}.#endif../* Op
1ba11 63 6f 64 65 3a 20 53 74 61 74 65 6d 65 6e 74 20 code: Statement
1ba12 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1ba13 20 42 65 67 69 6e 20 61 6e 20 69 6e 64 69 76 69 Begin an indivi
1ba14 64 75 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 dual statement t
1ba15 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 63 68 ransaction which
1ba16 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c 61 is part of a la
1ba17 72 67 65 72 0a 2a 2a 20 74 72 61 6e 73 61 63 74 rger.** transact
1ba18 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 ion. This is ne
1ba19 65 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 eded so that the
1ba1a 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 statement.** ca
1ba1b 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b n be rolled back
1ba1c 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 after an error
1ba1d 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 without having t
1ba1e 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a o roll back the.
1ba1f 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 ** entire transa
1ba20 63 74 69 6f 6e 2e 20 20 54 68 65 20 73 74 61 74 ction. The stat
1ba21 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1ba22 6e 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 n will automatic
1ba23 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 77 ally.** commit w
1ba24 68 65 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c hen the VDBE hal
1ba25 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ts..**.** If the
1ba26 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1ba27 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c tion is currentl
1ba28 79 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 y in autocommit
1ba29 6d 6f 64 65 20 28 74 68 61 74 20 0a 2a 2a 20 69 mode (that .** i
1ba2a 73 20 74 6f 20 73 61 79 2c 20 69 66 20 69 74 20 s to say, if it
1ba2b 69 73 20 69 6e 20 62 65 74 77 65 65 6e 20 42 45 is in between BE
1ba2c 47 49 4e 20 61 6e 64 20 43 4f 4d 4d 49 54 29 0a GIN and COMMIT).
1ba2d 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 72 65 20 ** and if there
1ba2e 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 61 63 74 are no other act
1ba2f 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 6f ive statements o
1ba30 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 n the same datab
1ba31 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f ase.** connectio
1ba32 6e 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 65 n, then this ope
1ba33 72 61 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f ration is a no-o
1ba34 70 2e 20 20 4e 6f 20 73 74 61 74 65 6d 65 6e 74 p. No statement
1ba35 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
1ba36 69 73 20 6e 65 65 64 65 64 20 73 69 6e 63 65 20 is needed since
1ba37 61 6e 79 20 65 72 72 6f 72 20 63 61 6e 20 75 73 any error can us
1ba38 65 20 74 68 65 20 6e 6f 72 6d 61 6c 20 52 4f 4c e the normal ROL
1ba39 4c 42 41 43 4b 20 70 72 6f 63 65 73 73 20 74 6f LBACK process to
1ba3a 0a 2a 2a 20 75 6e 64 6f 20 63 68 61 6e 67 65 73 .** undo changes
1ba3b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 74 61 ..**.** If a sta
1ba3c 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1ba3d 6f 6e 20 69 73 20 73 74 61 72 74 65 64 2c 20 74 on is started, t
1ba3e 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 hen a statement
1ba3f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 journal file.**
1ba40 77 69 6c 6c 20 62 65 20 61 6c 6c 6f 63 61 74 65 will be allocate
1ba41 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 d and initialize
1ba42 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61 d..**.** The sta
1ba43 74 65 6d 65 6e 74 20 69 73 20 62 65 67 75 6e 20 tement is begun
1ba44 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
1ba45 66 69 6c 65 20 77 69 74 68 20 69 6e 64 65 78 20 file with index
1ba46 50 31 2e 20 20 54 68 65 20 6d 61 69 6e 0a 2a 2a P1. The main.**
1ba47 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 database file h
1ba48 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 as an index of 0
1ba49 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 75 73 and the file us
1ba4a 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 ed for temporary
1ba4b 20 74 61 62 6c 65 73 0a 2a 2a 20 68 61 73 20 61 tables.** has a
1ba4c 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2f n index of 1..*/
1ba4d 0a 63 61 73 65 20 4f 50 5f 53 74 61 74 65 6d 65 .case OP_Stateme
1ba4e 6e 74 3a 20 7b 0a 20 20 69 66 28 20 64 62 2d 3e nt: {. if( db->
1ba4f 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c autoCommit==0 ||
1ba50 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 db->activeVdbeC
1ba51 6e 74 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 nt>1 ){. int
1ba52 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 i = pOp->p1;.
1ba53 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 Btree *pBt;.
1ba54 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
1ba55 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 i<db->nDb );.
1ba56 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 assert( db->aD
1ba57 62 5b 69 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 b[i].pBt!=0 );.
1ba58 20 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 pBt = db->aDb
1ba59 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 73 [i].pBt;. ass
1ba5a 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1ba5b 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 eIsInTrans(pBt)
1ba5c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
1ba5d 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1ba5e 31 3c 3c 69 29 29 21 3d 30 20 29 3b 0a 20 20 20 1<<i))!=0 );.
1ba5f 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 if( p->iStateme
1ba60 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 nt==0 ){. a
1ba61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 ssert( db->nStat
1ba62 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e ement>=0 && db->
1ba63 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b nSavepoint>=0 );
1ba64 0a 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 . db->nStat
1ba65 65 6d 65 6e 74 2b 2b 3b 20 0a 20 20 20 20 20 20 ement++; .
1ba66 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 p->iStatement =
1ba67 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b db->nSavepoint +
1ba68 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3b db->nStatement;
1ba69 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1ba6a 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
1ba6b 6e 53 74 6d 74 28 70 42 74 2c 20 70 2d 3e 69 53 nStmt(pBt, p->iS
1ba6c 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 tatement);. }.
1ba6d 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1ba6e 63 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 20 code: Savepoint
1ba6f 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a P1 * * P4 *.**.*
1ba70 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 * Open, release
1ba71 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 or rollback the
1ba72 73 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 20 savepoint named
1ba73 62 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 2c by parameter P4,
1ba74 20 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e depending.** on
1ba75 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 31 the value of P1
1ba76 2e 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 . To open a new
1ba77 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 savepoint, P1==0
1ba78 2e 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 6f . To release (co
1ba79 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 73 mmit) an.** exis
1ba7a 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c 20 ting savepoint,
1ba7b 50 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f 6c P1==1, or to rol
1ba7c 6c 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 6e lback an existin
1ba7d 67 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d 3d g savepoint P1==
1ba7e 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 61 2..*/.case OP_Sa
1ba7f 76 65 70 6f 69 6e 74 3a 20 7b 0a 20 20 69 6e 74 vepoint: {. int
1ba80 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 p1 = pOp->p1;.
1ba81 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 char *zName = p
1ba82 4f 70 2d 3e 70 34 2e 7a 3b 20 20 20 20 20 20 20 Op->p4.z;
1ba83 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 /* Name of sav
1ba84 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 epoint */.. /*
1ba85 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 Assert that the
1ba86 70 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 p1 parameter is
1ba87 76 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 valid. Also that
1ba88 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
1ba89 6f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 open. ** transa
1ba8a 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 ction, then ther
1ba8b 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 e cannot be any
1ba8c 73 61 76 65 70 6f 69 6e 74 73 2e 20 0a 20 20 2a savepoints. . *
1ba8d 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e /. assert( db->
1ba8e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c pSavepoint==0 ||
1ba8f 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d db->autoCommit=
1ba90 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1ba91 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 p1==SAVEPOINT_BE
1ba92 47 49 4e 7c 7c 70 31 3d 3d 53 41 56 45 50 4f 49 GIN||p1==SAVEPOI
1ba93 4e 54 5f 52 45 4c 45 41 53 45 7c 7c 70 31 3d 3d NT_RELEASE||p1==
1ba94 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
1ba95 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
1ba96 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c db->pSavepoint |
1ba97 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 | db->isTransact
1ba98 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 ionSavepoint==0
1ba99 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 65 );. assert( che
1ba9a 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 ckSavepointCount
1ba9b 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 70 (db) );.. if( p
1ba9c 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 1==SAVEPOINT_BEG
1ba9d 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 IN ){. if( db
1ba9e 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 ->writeVdbeCnt>0
1ba9f 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e ){. /* A n
1baa0 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e ew savepoint can
1baa1 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 69 not be created i
1baa2 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 f there are acti
1baa3 76 65 20 77 72 69 74 65 20 0a 20 20 20 20 20 20 ve write .
1baa4 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 69 ** statements (i
1baa5 2e 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 72 .e. open read/wr
1baa6 69 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 ite incremental
1baa7 62 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a 20 blob handles)..
1baa8 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
1baa9 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1baaa 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1baab 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 76 "cannot open sav
1baac 65 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 20 epoint - ".
1baad 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e "SQL statemen
1baae 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 ts in progress")
1baaf 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
1bab0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 ITE_BUSY;. }e
1bab1 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e lse{. int n
1bab2 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 Name = sqlite3St
1bab3 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 rlen30(zName);.
1bab4 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a Savepoint *
1bab5 70 4e 65 77 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 pNew;.. /*
1bab6 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 76 Create a new sav
1bab7 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 epoint structure
1bab8 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 20 . */. pNew
1bab9 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
1baba 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
1babb 53 61 76 65 70 6f 69 6e 74 29 2b 6e 4e 61 6d 65 Savepoint)+nName
1babc 2b 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 +1);. if( p
1babd 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 70 New ){. p
1babe 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 New->zName = (ch
1babf 61 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 ar *)&pNew[1];.
1bac0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e memcpy(pN
1bac1 65 77 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 ew->zName, zName
1bac2 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 , nName+1);.
1bac3 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 . /* If t
1bac4 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 here is no open
1bac5 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
1bac6 6e 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61 n mark this as a
1bac7 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 special.
1bac8 20 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e ** "transaction
1bac9 20 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0a savepoint". */.
1baca 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e if( db->
1bacb 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 autoCommit ){.
1bacc 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f db->auto
1bacd 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 Commit = 0;.
1bace 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 6e db->isTran
1bacf 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 sactionSavepoint
1bad0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 = 1;. }e
1bad1 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 lse{. d
1bad2 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b 3b b->nSavepoint++;
1bad3 0a 09 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 20 ..}. .
1bad4 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77 /* Link the new
1bad5 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74 6f 20 savepoint into
1bad6 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e the database han
1bad7 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f 0a 20 dle's list. */.
1bad8 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 pNew->pNe
1bad9 78 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f xt = db->pSavepo
1bada 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d int;. db-
1badb 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 4e >pSavepoint = pN
1badc 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ew;. }.
1badd 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 53 }. }else{. S
1bade 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 avepoint *pSavep
1badf 6f 69 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 53 oint;. int iS
1bae0 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20 avepoint = 0;..
1bae1 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e /* Find the n
1bae2 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 amed savepoint.
1bae3 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 If there is no s
1bae4 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 uch savepoint, t
1bae5 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e hen an. ** an
1bae6 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
1bae7 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ed to the user.
1bae8 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 */. for(.
1bae9 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 64 62 pSavepoint=db
1baea 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 0a 20 ->pSavepoint; .
1baeb 20 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 20 pSavepoint
1baec 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
1baed 70 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e p(pSavepoint->zN
1baee 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 ame, zName);.
1baef 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 70 53 pSavepoint=pS
1baf0 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a avepoint->pNext.
1baf1 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 53 61 ){. iSa
1baf2 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d vepoint++;. }
1baf3 0a 20 20 20 20 69 66 28 20 21 70 53 61 76 65 70 . if( !pSavep
1baf4 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 oint ){. sq
1baf5 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1baf6 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1baf7 22 6e 6f 20 73 75 63 68 20 73 61 76 65 70 6f 69 "no such savepoi
1baf8 6e 74 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b nt: %s", zName);
1baf9 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1bafa 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 TE_ERROR;. }e
1bafb 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20 20 20 lse if( .
1bafc 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e db->writeVdbeCn
1bafd 74 3e 30 20 7c 7c 20 28 70 31 3d 3d 53 41 56 45 t>0 || (p1==SAVE
1bafe 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 POINT_ROLLBACK &
1baff 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 & db->activeVdbe
1bb00 43 6e 74 3e 31 29 20 0a 20 20 20 20 29 7b 0a 20 Cnt>1) . ){.
1bb01 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f /* It is no
1bb02 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 t possible to re
1bb03 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 lease (commit) a
1bb04 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68 savepoint if th
1bb05 65 72 65 20 61 72 65 20 0a 20 20 20 20 20 20 2a ere are . *
1bb06 2a 20 61 63 74 69 76 65 20 77 72 69 74 65 20 73 * active write s
1bb07 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 tatements. It is
1bb08 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
1bb09 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 65 rollback a save
1bb0a 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69 point. ** i
1bb0b 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 f there are any
1bb0c 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 active statement
1bb0d 73 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20 s at all..
1bb0e 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1bb0f 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1bb10 72 72 4d 73 67 2c 20 64 62 2c 20 0a 20 20 20 20 rrMsg, db, .
1bb11 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73 "cannot %s s
1bb12 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73 avepoint - SQL s
1bb13 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f tatements in pro
1bb14 67 72 65 73 73 22 2c 0a 20 20 20 20 20 20 20 20 gress",.
1bb15 28 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 (p1==SAVEPOINT_R
1bb16 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62 OLLBACK ? "rollb
1bb17 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29 ack": "release")
1bb18 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 . );.
1bb19 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
1bb1a 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 ;. }else{..
1bb1b 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 /* Determine
1bb1c 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1bb1d 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61 this is a transa
1bb1e 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e ction savepoint.
1bb1f 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a If so,. **
1bb20 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 52 and this is a R
1bb21 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20 ELEASE command,
1bb22 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 then the current
1bb23 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 transaction .
1bb24 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 ** is commit
1bb25 74 65 64 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 ted. . */.
1bb26 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73 int isTrans
1bb27 61 63 74 69 6f 6e 20 3d 20 70 53 61 76 65 70 6f action = pSavepo
1bb28 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 int->pNext==0 &&
1bb29 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 db->isTransacti
1bb2a 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 onSavepoint;.
1bb2b 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61 63 if( isTransac
1bb2c 74 69 6f 6e 20 26 26 20 70 31 3d 3d 53 41 56 45 tion && p1==SAVE
1bb2d 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b POINT_RELEASE ){
1bb2e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 . db->aut
1bb2f 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 oCommit = 1;.
1bb30 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1bb31 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c VdbeHalt(p)==SQL
1bb32 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
1bb33 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 p->pc = pc
1bb34 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e ;. db->
1bb35 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a autoCommit = 0;.
1bb36 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 p->rc
1bb37 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 = rc = SQLITE_BU
1bb38 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f SY;. go
1bb39 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1bb3a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1bb3b 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 db->isTransact
1bb3c 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 ionSavepoint = 0
1bb3d 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 ;. rc = p
1bb3e 2d 3e 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ->rc;. }els
1bb3f 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 e{. int i
1bb40 69 3b 0a 20 20 20 20 20 20 20 20 69 53 61 76 65 i;. iSave
1bb41 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 point = db->nSav
1bb42 65 70 6f 69 6e 74 20 2d 20 69 53 61 76 65 70 6f epoint - iSavepo
1bb43 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 int - 1;.
1bb44 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 64 62 for(ii=0; ii<db
1bb45 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 ->nDb; ii++){.
1bb46 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1bb47 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
1bb48 6e 74 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 nt(db->aDb[ii].p
1bb49 42 74 2c 20 70 31 2c 20 69 53 61 76 65 70 6f 69 Bt, p1, iSavepoi
1bb4a 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 nt);. i
1bb4b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1bb4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1bb4d 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1bb4e 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1bb4f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1bb50 20 20 20 20 20 20 20 69 66 28 20 70 31 3d 3d 53 if( p1==S
1bb51 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
1bb52 4b 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 26 K && (db->flags&
1bb53 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
1bb54 6e 67 65 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 nges)!=0 ){.
1bb55 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1bb56 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
1bb57 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 20 ments(db);.
1bb58 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 sqlite3Rese
1bb59 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 tInternalSchema(
1bb5a 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 db, 0);.
1bb5b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
1bb5c 20 20 20 2f 2a 20 52 65 67 61 72 64 6c 65 73 73 /* Regardless
1bb5d 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 69 73 of whether this
1bb5e 20 69 73 20 61 20 52 45 4c 45 41 53 45 20 6f 72 is a RELEASE or
1bb5f 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65 73 74 72 ROLLBACK, destr
1bb60 6f 79 20 61 6c 6c 20 0a 20 20 20 20 20 20 2a 2a oy all . **
1bb61 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65 73 74 savepoints nest
1bb62 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 ed inside of the
1bb63 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 savepoint being
1bb64 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20 2a 2f operated on. */
1bb65 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 64 62 . while( db
1bb66 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d 70 53 ->pSavepoint!=pS
1bb67 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 avepoint ){.
1bb68 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 Savepoint *p
1bb69 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 Tmp = db->pSavep
1bb6a 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 oint;. db
1bb6b 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 ->pSavepoint = p
1bb6c 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Tmp->pNext;.
1bb6d 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1bb6e 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20 20 e(db, pTmp);.
1bb6f 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f db->nSavepo
1bb70 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a int--;. }..
1bb71 20 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 /* If it i
1bb72 73 20 61 20 52 45 4c 45 41 53 45 2c 20 74 68 65 s a RELEASE, the
1bb73 6e 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 n destroy the sa
1bb74 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 vepoint being op
1bb75 65 72 61 74 65 64 20 6f 6e 20 74 6f 6f 20 2a 2f erated on too */
1bb76 0a 20 20 20 20 20 20 69 66 28 20 70 31 3d 3d 53 . if( p1==S
1bb77 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
1bb78 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1bb79 72 74 28 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d rt( pSavepoint==
1bb7a 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 db->pSavepoint )
1bb7b 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 ;. db->pS
1bb7c 61 76 65 70 6f 69 6e 74 20 3d 20 70 53 61 76 65 avepoint = pSave
1bb7d 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 point->pNext;.
1bb7e 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1bb7f 72 65 65 28 64 62 2c 20 70 53 61 76 65 70 6f 69 ree(db, pSavepoi
1bb80 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 nt);. if(
1bb81 20 21 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 !isTransaction
1bb82 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d ){. db-
1bb83 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 >nSavepoint--;.
1bb84 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1bb85 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 . }. }.. br
1bb86 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1bb87 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 e: AutoCommit P1
1bb88 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1bb89 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 Set the database
1bb8a 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 auto-commit fla
1bb8b 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 g to P1 (1 or 0)
1bb8c 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c . If P2 is true,
1bb8d 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e roll.** back an
1bb8e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 y currently acti
1bb8f 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 ve btree transac
1bb90 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 tions. If there
1bb91 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a are any active.*
1bb92 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f * VMs (apart fro
1bb93 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 m this one), the
1bb94 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 n a ROLLBACK fai
1bb95 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 ls. A COMMIT fa
1bb96 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 ils if.** there
1bb97 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69 are active writi
1bb98 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65 ng VMs or active
1bb99 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68 VMs that use sh
1bb9a 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a ared cache..**.*
1bb9b 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 * This instructi
1bb9c 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d on causes the VM
1bb9d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 to halt..*/.cas
1bb9e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a e OP_AutoCommit:
1bb9f 20 7b 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 {. int desired
1bba0 41 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70 AutoCommit = pOp
1bba1 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 72 6f 6c 6c ->p1;. int roll
1bba2 62 61 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a back = pOp->p2;.
1bba3 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 20 3d int turnOnAC =
1bba4 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d desiredAutoComm
1bba5 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 it && !db->autoC
1bba6 6f 6d 6d 69 74 3b 0a 0a 20 20 61 73 73 65 72 74 ommit;.. assert
1bba7 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d ( desiredAutoCom
1bba8 6d 69 74 3d 3d 31 20 7c 7c 20 64 65 73 69 72 65 mit==1 || desire
1bba9 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 dAutoCommit==0 )
1bbaa 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69 ;. assert( desi
1bbab 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 redAutoCommit==1
1bbac 20 7c 7c 20 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20 || rollback==0
1bbad 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 );.. assert( db
1bbae 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
1bbaf 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 0 ); /* At leas
1bbb0 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 t this one VM is
1bbb1 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 active */.. if
1bbb2 28 20 74 75 72 6e 4f 6e 41 43 20 26 26 20 72 6f ( turnOnAC && ro
1bbb3 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 llback && db->ac
1bbb4 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b tiveVdbeCnt>1 ){
1bbb5 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
1bbb6 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c instruction impl
1bbb7 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 ements a ROLLBAC
1bbb8 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 K and other VMs
1bbb9 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c are. ** still
1bbba 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 running, and a
1bbbb 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
1bbbc 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e ctive, return an
1bbbd 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e error indicatin
1bbbe 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 g. ** that th
1bbbf 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 e other VMs must
1bbc0 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e complete first.
1bbc1 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c . */. sql
1bbc2 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1bbc3 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1bbc4 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 cannot rollback
1bbc5 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a transaction - ".
1bbc6 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 "SQL sta
1bbc7 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 tements in progr
1bbc8 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 ess");. rc =
1bbc9 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
1bbca 65 6c 73 65 20 69 66 28 20 74 75 72 6e 4f 6e 41 else if( turnOnA
1bbcb 43 20 26 26 20 21 72 6f 6c 6c 62 61 63 6b 20 26 C && !rollback &
1bbcc 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 & db->writeVdbeC
1bbcd 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 nt>1 ){. /* I
1bbce 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 f this instructi
1bbcf 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 on implements a
1bbd0 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 74 68 65 72 COMMIT and other
1bbd1 20 56 4d 73 20 61 72 65 20 77 72 69 74 69 6e 67 VMs are writing
1bbd2 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 . ** return a
1bbd3 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 n error indicati
1bbd4 6e 67 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 ng that the othe
1bbd5 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c r VMs must compl
1bbd6 65 74 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20 ete first. .
1bbd7 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 */. sqlite3Se
1bbd8 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1bbd9 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 Msg, db, "cannot
1bbda 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74 commit transact
1bbdb 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 ion - ".
1bbdc 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 "SQL statements
1bbdd 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 in progress");.
1bbde 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
1bbdf 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 USY;. }else if(
1bbe0 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d desiredAutoComm
1bbe1 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d it!=db->autoComm
1bbe2 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 72 6f it ){. if( ro
1bbe3 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 llback ){.
1bbe4 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 41 assert( desiredA
1bbe5 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a utoCommit==1 );.
1bbe6 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c sqlite3Rol
1bbe7 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 lbackAll(db);.
1bbe8 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1bbe9 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 it = 1;. }els
1bbea 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 e{. db->aut
1bbeb 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 64 65 oCommit = (u8)de
1bbec 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b siredAutoCommit;
1bbed 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1bbee 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 e3VdbeHalt(p)==S
1bbef 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
1bbf0 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 p->pc = pc
1bbf1 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 ;. db->au
1bbf2 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28 toCommit = (u8)(
1bbf3 31 2d 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 1-desiredAutoCom
1bbf4 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 70 2d mit);. p-
1bbf5 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 >rc = rc = SQLIT
1bbf6 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 E_BUSY;.
1bbf7 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1bbf8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1bbf9 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1bbfa 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 3b nStatement==0 );
1bbfb 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 . sqlite3Clos
1bbfc 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b eSavepoints(db);
1bbfd 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d . if( p->rc==
1bbfe 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1bbff 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 rc = SQLITE_D
1bc00 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ONE;. }else{.
1bc01 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1bc02 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 E_ERROR;. }.
1bc03 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 goto vdbe_ret
1bc04 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 urn;. }else{.
1bc05 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1bc06 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1bc07 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21 64 65 db,. (!de
1bc08 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 29 siredAutoCommit)
1bc09 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 74 20 61 ?"cannot start a
1bc0a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 74 transaction wit
1bc0b 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f hin a transactio
1bc0c 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 28 72 6f n":(. (ro
1bc0d 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 llback)?"cannot
1bc0e 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 rollback - no tr
1bc0f 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
1bc10 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20 ive":.
1bc11 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 "cannot
1bc12 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 commit - no tra
1bc13 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
1bc14 76 65 22 29 29 3b 0a 20 20 20 20 20 20 20 20 20 ve"));.
1bc15 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1bc16 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 62 72 _ERROR;. }. br
1bc17 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1bc18 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 50 e: Transaction P
1bc19 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1bc1a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 Begin a transac
1bc1b 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 6e 73 tion. The trans
1bc1c 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 65 6e action ends when
1bc1d 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 6f 6c a Commit or Rol
1bc1e 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64 65 20 lback.** opcode
1bc1f 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 is encountered.
1bc20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 Depending on th
1bc21 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 73 65 e ON CONFLICT se
1bc22 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20 74 72 tting, the.** tr
1bc23 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 ansaction might
1bc24 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 also be rolled b
1bc25 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ack if an error
1bc26 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a is encountered..
1bc27 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 **.** P1 is the
1bc28 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 index of the dat
1bc29 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 77 68 abase file on wh
1bc2a 69 63 68 20 74 68 65 20 74 72 61 6e 73 61 63 74 ich the transact
1bc2b 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72 74 65 ion is.** starte
1bc2c 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73 20 74 d. Index 0 is t
1bc2d 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1bc2e 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 file and index
1bc2f 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 1 is the.** file
1bc30 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 used for tempor
1bc31 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49 6e 64 ary tables. Ind
1bc32 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d 6f 72 ices of 2 or mor
1bc33 65 20 61 72 65 20 75 73 65 64 20 66 6f 72 0a 2a e are used for.*
1bc34 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 * attached datab
1bc35 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 ases..**.** If P
1bc36 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 2 is non-zero, t
1bc37 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e hen a write-tran
1bc38 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 saction is start
1bc39 65 64 2e 20 20 41 20 52 45 53 45 52 56 45 44 20 ed. A RESERVED
1bc3a 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 lock is.** obtai
1bc3b 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ned on the datab
1bc3c 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 61 20 ase file when a
1bc3d 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1bc3e 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 4e n is started. N
1bc3f 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 o.** other proce
1bc40 73 73 20 63 61 6e 20 73 74 61 72 74 20 61 6e 6f ss can start ano
1bc41 74 68 65 72 20 77 72 69 74 65 20 74 72 61 6e 73 ther write trans
1bc42 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69 action while thi
1bc43 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 s transaction is
1bc44 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20 20 53 .** underway. S
1bc45 74 61 72 74 69 6e 67 20 61 20 77 72 69 74 65 20 tarting a write
1bc46 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 73 6f transaction also
1bc47 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c 6c 62 creates a rollb
1bc48 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 0a 2a ack journal. A.*
1bc49 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 * write transact
1bc4a 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 ion must be star
1bc4b 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 ted before any c
1bc4c 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20 6d 61 hanges can be ma
1bc4d 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 de to the.** dat
1bc4e 61 62 61 73 65 2e 20 20 49 66 20 50 32 20 69 73 abase. If P2 is
1bc4f 20 32 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 2 or greater th
1bc50 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 en an EXCLUSIVE
1bc51 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f 62 74 lock is also obt
1bc52 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 ained.** on the
1bc53 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 file..**.** If P
1bc54 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 2 is zero, then
1bc55 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f a read-lock is o
1bc56 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 btained on the d
1bc57 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f atabase file..*/
1bc58 0a 63 61 73 65 20 4f 50 5f 54 72 61 6e 73 61 63 .case OP_Transac
1bc59 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74 20 69 20 tion: {. int i
1bc5a 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 72 = pOp->p1;. Btr
1bc5b 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73 73 65 ee *pBt;.. asse
1bc5c 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 rt( i>=0 && i<db
1bc5d 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1bc5e 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1bc5f 20 26 20 28 31 3c 3c 69 29 29 21 3d 30 20 29 3b & (1<<i))!=0 );
1bc60 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 . pBt = db->aDb
1bc61 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20 [i].pBt;.. if(
1bc62 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 pBt ){. rc =
1bc63 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
1bc64 6e 54 72 61 6e 73 28 70 42 74 2c 20 70 4f 70 2d nTrans(pBt, pOp-
1bc65 3e 70 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63 >p2);. if( rc
1bc66 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b ==SQLITE_BUSY ){
1bc67 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 . p->pc = p
1bc68 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d c;. p->rc =
1bc69 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1bc6a 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64 Y;. goto vd
1bc6b 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d be_return;. }
1bc6c 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1bc6d 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 ITE_OK && rc!=SQ
1bc6e 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 2f 2a LITE_READONLY /*
1bc6f 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 && rc!=SQLITE_B
1bc70 55 53 59 20 2a 2f 20 29 7b 0a 20 20 20 20 20 20 USY */ ){.
1bc71 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1bc72 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 o_error;. }.
1bc73 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1bc74 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 6f * Opcode: ReadCo
1bc75 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 okie P1 P2 P3 *
1bc76 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f *.**.** Read coo
1bc77 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72 kie number P3 fr
1bc78 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61 om database P1 a
1bc79 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f nd write it into
1bc7a 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a register P2..**
1bc7b 20 50 33 3d 3d 30 20 69 73 20 74 68 65 20 73 63 P3==0 is the sc
1bc7c 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 hema version. P
1bc7d 33 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61 3==1 is the data
1bc7e 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 base format..**
1bc7f 50 33 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63 P3==2 is the rec
1bc80 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 ommended pager c
1bc81 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 ache size, and s
1bc82 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 o forth. P1==0
1bc83 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 is.** the main d
1bc84 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1bc85 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 P1==1 is the da
1bc86 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 tabase file used
1bc87 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d to store.** tem
1bc88 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a porary tables..*
1bc89 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 65 *.** If P1 is ne
1bc8a 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 69 gative, then thi
1bc8b 73 20 69 73 20 61 20 72 65 71 75 65 73 74 20 74 s is a request t
1bc8c 6f 20 72 65 61 64 20 74 68 65 20 73 69 7a 65 20 o read the size
1bc8d 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 of a.** database
1bc8e 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 50 33 20 s free-list. P3
1bc8f 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 31 must be set to 1
1bc90 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 54 in this case. T
1bc91 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 64 61 74 he actual.** dat
1bc92 61 62 61 73 65 20 61 63 63 65 73 73 65 64 20 69 abase accessed i
1bc93 73 20 28 28 50 31 2b 31 29 2a 2d 31 29 2e 20 46 s ((P1+1)*-1). F
1bc94 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 20 50 31 or example, a P1
1bc95 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 2d 31 parameter of -1
1bc96 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 .** corresponds
1bc97 74 6f 20 64 61 74 61 62 61 73 65 20 30 20 28 22 to database 0 ("
1bc98 6d 61 69 6e 22 29 2c 20 61 20 50 31 20 6f 66 20 main"), a P1 of
1bc99 2d 32 20 69 73 20 64 61 74 61 62 61 73 65 20 31 -2 is database 1
1bc9a 20 28 22 74 65 6d 70 22 29 2e 0a 2a 2a 0a 2a 2a ("temp")..**.**
1bc9b 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 There must be a
1bc9c 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 read-lock on th
1bc9d 65 20 64 61 74 61 62 61 73 65 20 28 65 69 74 68 e database (eith
1bc9e 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e er a transaction
1bc9f 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 61 72 .** must be star
1bca0 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d 75 73 ted or there mus
1bca1 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 t be an open cur
1bca2 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a 20 65 sor) before.** e
1bca3 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e xecuting this in
1bca4 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 struction..*/.ca
1bca5 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 se OP_ReadCookie
1bca6 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1bca7 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1bca8 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d ease */. int iM
1bca9 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d eta;. int iDb =
1bcaa 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 pOp->p1;. int
1bcab 69 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 iCookie = pOp->p
1bcac 33 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 3;.. assert( pO
1bcad 70 2d 3e 70 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 p->p3<SQLITE_N_B
1bcae 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 69 TREE_META );. i
1bcaf 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 f( iDb<0 ){.
1bcb0 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62 2b 31 iDb = (-1*(iDb+1
1bcb1 29 29 3b 0a 20 20 20 20 69 43 6f 6f 6b 69 65 20 ));. iCookie
1bcb2 2a 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 61 73 73 *= -1;. }. ass
1bcb3 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 ert( iDb>=0 && i
1bcb4 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 Db<db->nDb );.
1bcb5 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b assert( db->aDb[
1bcb6 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 iDb].pBt!=0 );.
1bcb7 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1bcb8 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62 eeMask & (1<<iDb
1bcb9 29 29 21 3d 30 20 29 3b 0a 20 20 2f 2a 20 54 68 ))!=0 );. /* Th
1bcba 65 20 69 6e 64 65 78 69 6e 67 20 6f 66 20 6d 65 e indexing of me
1bcbb 74 61 20 76 61 6c 75 65 73 20 61 74 20 74 68 65 ta values at the
1bcbc 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 69 73 schema layer is
1bcbd 20 6f 66 66 20 62 79 20 6f 6e 65 20 66 72 6f 6d off by one from
1bcbe 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 69 . ** the indexi
1bcbf 6e 67 20 69 6e 20 74 68 65 20 62 74 72 65 65 20 ng in the btree
1bcc0 6c 61 79 65 72 2e 20 20 54 68 65 20 62 74 72 65 layer. The btre
1bcc1 65 20 63 6f 6e 73 69 64 65 72 73 20 6d 65 74 61 e considers meta
1bcc2 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20 62 65 20 74 [0] to. ** be t
1bcc3 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 he number of fre
1bcc4 65 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 e pages in the d
1bcc5 61 74 61 62 61 73 65 20 28 61 20 72 65 61 64 2d atabase (a read-
1bcc6 6f 6e 6c 79 20 76 61 6c 75 65 29 0a 20 20 2a 2a only value). **
1bcc7 20 61 6e 64 20 6d 65 74 61 5b 31 5d 20 74 6f 20 and meta[1] to
1bcc8 62 65 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f be the schema co
1bcc9 6f 6b 69 65 2e 20 20 54 68 65 20 73 63 68 65 6d okie. The schem
1bcca 61 20 6c 61 79 65 72 20 63 6f 6e 73 69 64 65 72 a layer consider
1bccb 73 0a 20 20 2a 2a 20 6d 65 74 61 5b 31 5d 20 74 s. ** meta[1] t
1bccc 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d 61 20 o be the schema
1bccd 63 6f 6f 6b 69 65 2e 20 20 53 6f 20 77 65 20 68 cookie. So we h
1bcce 61 76 65 20 74 6f 20 73 68 69 66 74 20 74 68 65 ave to shift the
1bccf 20 69 6e 64 65 78 0a 20 20 2a 2a 20 62 79 20 6f index. ** by o
1bcd0 6e 65 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 ne in the follow
1bcd1 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 ing statement..
1bcd2 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
1bcd3 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 64 e3BtreeGetMeta(d
1bcd4 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c b->aDb[iDb].pBt,
1bcd5 20 31 20 2b 20 69 43 6f 6f 6b 69 65 2c 20 28 75 1 + iCookie, (u
1bcd6 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20 20 32 *)&iMeta);.
1bcd7 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 69 4d 65 74 pOut->u.i = iMet
1bcd8 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 a;. MemSetTypeF
1bcd9 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1bcda 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1bcdb 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f /* Opcode: SetCo
1bcdc 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 okie P1 P2 P3 *
1bcdd 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 *.**.** Write th
1bcde 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 e content of reg
1bcdf 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 72 70 ister P3 (interp
1bce0 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65 reted as an inte
1bce1 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f ger).** into coo
1bce2 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66 kie number P2 of
1bce3 20 64 61 74 61 62 61 73 65 20 50 31 2e 0a 2a 2a database P1..**
1bce4 20 50 32 3d 3d 30 20 69 73 20 74 68 65 20 73 63 P2==0 is the sc
1bce5 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 hema version. P
1bce6 32 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61 2==1 is the data
1bce7 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 base format..**
1bce8 50 32 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63 P2==2 is the rec
1bce9 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 ommended pager c
1bcea 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 ache size, and s
1bceb 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 o forth. P1==0
1bcec 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 is.** the main d
1bced 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1bcee 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 P1==1 is the da
1bcef 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 tabase file used
1bcf0 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d to store.** tem
1bcf1 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a porary tables..*
1bcf2 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 *.** A transacti
1bcf3 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 on must be start
1bcf4 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 ed before execut
1bcf5 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e ing this opcode.
1bcf6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43 .*/.case OP_SetC
1bcf7 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f ookie: { /
1bcf8 2a 20 69 6e 33 20 2a 2f 0a 20 20 44 62 20 2a 70 * in3 */. Db *p
1bcf9 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f Db;. assert( pO
1bcfa 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 p->p2<SQLITE_N_B
1bcfb 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 TREE_META );. a
1bcfc 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1bcfd 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 0 && pOp->p1<db-
1bcfe 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1bcff 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1bd00 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 & (1<<pOp->p1))!
1bd01 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 =0 );. pDb = &d
1bd02 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b b->aDb[pOp->p1];
1bd03 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 2d 3e . assert( pDb->
1bd04 70 42 74 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 pBt!=0 );. sqli
1bd05 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
1bd06 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a rify(pIn3);. /*
1bd07 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 See note about
1bd08 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f index shifting o
1bd09 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 n OP_ReadCookie
1bd0a 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
1bd0b 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 3BtreeUpdateMeta
1bd0c 28 70 44 62 2d 3e 70 42 74 2c 20 31 2b 70 4f 70 (pDb->pBt, 1+pOp
1bd0d 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d ->p2, (int)pIn3-
1bd0e 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70 >u.i);. if( pOp
1bd0f 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 2f ->p2==0 ){. /
1bd10 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68 65 6d * When the schem
1bd11 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 a cookie changes
1bd12 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77 , record the new
1bd13 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e 61 6c cookie internal
1bd14 6c 79 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 ly */. pDb->p
1bd15 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 Schema->schema_c
1bd16 6f 6f 6b 69 65 20 3d 20 28 69 6e 74 29 70 49 6e ookie = (int)pIn
1bd17 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e 3->u.i;. db->
1bd18 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f flags |= SQLITE_
1bd19 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 InternChanges;.
1bd1a 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e }else if( pOp->
1bd1b 70 32 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 p2==1 ){. /*
1bd1c 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69 Record changes i
1bd1d 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 n the file forma
1bd1e 74 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53 t */. pDb->pS
1bd1f 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d chema->file_form
1bd20 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75 at = (u8)pIn3->u
1bd21 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f .i;. }. if( pO
1bd22 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 p->p1==1 ){.
1bd23 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c /* Invalidate al
1bd24 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 l prepared state
1bd25 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74 ments whenever t
1bd26 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
1bd27 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 . ** schema i
1bd28 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b s changed. Tick
1bd29 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 et #1644 */.
1bd2a 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
1bd2b 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
1bd2c 64 62 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b db);. }. break
1bd2d 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1bd2e 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20 VerifyCookie P1
1bd2f 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b P2 *.**.** Check
1bd30 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c the value of gl
1bd31 6f 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 obal database pa
1bd32 72 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30 rameter number 0
1bd33 20 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 (the.** schema
1bd34 76 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b version) and mak
1bd35 65 20 73 75 72 65 20 69 74 20 69 73 20 65 71 75 e sure it is equ
1bd36 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50 al to P2. .** P
1bd37 31 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 1 is the databas
1bd38 65 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 e number which i
1bd39 73 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e s 0 for the main
1bd3a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a database file.*
1bd3b 2a 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20 * and 1 for the
1bd3c 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d file holding tem
1bd3d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e porary tables an
1bd3e 64 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75 d some higher nu
1bd3f 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69 mber.** for auxi
1bd40 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e liary databases.
1bd41 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69 .**.** The cooki
1bd42 65 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 61 e changes its va
1bd43 6c 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 lue whenever the
1bd44 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
1bd45 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69 changes..** Thi
1bd46 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 s operation is u
1bd47 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 68 sed to detect wh
1bd48 65 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b en that the cook
1bd49 69 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a ie has changed.*
1bd4a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 * and that the c
1bd4b 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e urrent process n
1bd4c 65 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 74 eeds to reread t
1bd4d 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a he schema..**.**
1bd4e 20 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 Either a transa
1bd4f 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68 ction needs to h
1bd50 61 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 64 ave been started
1bd51 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e or an OP_Open n
1bd52 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78 eeds.** to be ex
1bd53 65 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 62 ecuted (to estab
1bd54 6c 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b lish a read lock
1bd55 29 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70 ) before this op
1bd56 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b code is.** invok
1bd57 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 ed..*/.case OP_V
1bd58 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 20 erifyCookie: {.
1bd59 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 74 int iMeta;. Bt
1bd5a 72 65 65 20 2a 70 42 74 3b 0a 20 20 61 73 73 65 ree *pBt;. asse
1bd5b 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1bd5c 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 & pOp->p1<db->nD
1bd5d 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1bd5e 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1bd5f 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 1<<pOp->p1))!=0
1bd60 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 );. pBt = db->a
1bd61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b Db[pOp->p1].pBt;
1bd62 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 . if( pBt ){.
1bd63 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1bd64 72 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c 20 reeGetMeta(pBt,
1bd65 31 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 74 61 1, (u32 *)&iMeta
1bd66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1bd67 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1bd68 20 20 20 20 69 4d 65 74 61 20 3d 20 30 3b 0a 20 iMeta = 0;.
1bd69 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
1bd6a 49 54 45 5f 4f 4b 20 26 26 20 69 4d 65 74 61 21 ITE_OK && iMeta!
1bd6b 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 =pOp->p2 ){.
1bd6c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1bd6d 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1bd6e 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 p->zErrMsg =
1bd6f 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1bd70 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 73 63 db, "database sc
1bd71 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 hema has changed
1bd72 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ");. /* If th
1bd73 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 e schema-cookie
1bd74 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1bd75 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74 e file matches t
1bd76 68 65 20 63 6f 6f 6b 69 65 20 0a 20 20 20 20 2a he cookie . *
1bd77 2a 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68 * stored with th
1bd78 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 e in-memory repr
1bd79 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 esentation of th
1bd7a 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20 e schema, do.
1bd7b 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74 ** not reload t
1bd7c 68 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74 he schema from t
1bd7d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1bd7e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1bd7f 49 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 If virtual-table
1bd80 73 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68 s are in use, th
1bd81 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 is is not just a
1bd82 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a n optimization..
1bd83 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d ** Often, v-
1bd84 74 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65 tables store the
1bd85 69 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72 ir data in other
1bd86 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 SQLite tables,
1bd87 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65 which. ** are
1bd88 20 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69 queried from wi
1bd89 74 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 thin xNext() and
1bd8a 20 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d other v-table m
1bd8b 65 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20 ethods using.
1bd8c 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65 ** prepared que
1bd8d 72 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20 ries. If such a
1bd8e 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d query is out-of-
1bd8f 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 date, we do not
1bd90 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 want to. ** d
1bd91 69 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62 iscard the datab
1bd92 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74 ase schema, as t
1bd93 68 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70 he user code imp
1bd94 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20 lementing the.
1bd95 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75 ** v-table wou
1bd96 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65 ld have to be re
1bd97 61 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69 ady for the sqli
1bd98 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 te3_vtab structu
1bd99 72 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a re itself. **
1bd9a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 to be invalidat
1bd9b 65 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69 ed whenever sqli
1bd9c 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 te3_step() is ca
1bd9d 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
1bd9e 20 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 . ** a v-tab
1bd9f 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a le method.. *
1bda0 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 /. if( db->aD
1bda1 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 b[pOp->p1].pSche
1bda2 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 ma->schema_cooki
1bda3 65 21 3d 69 4d 65 74 61 20 29 7b 0a 20 20 20 20 e!=iMeta ){.
1bda4 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
1bda5 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c ternalSchema(db,
1bda6 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d pOp->p1);. }
1bda7 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 .. sqlite3Exp
1bda8 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
1bda9 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 72 ments(db);. r
1bdaa 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d c = SQLITE_SCHEM
1bdab 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a A;. }. break;.
1bdac 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 }../* Opcode: Op
1bdad 65 6e 52 65 61 64 20 50 31 20 50 32 20 50 33 20 enRead P1 P2 P3
1bdae 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e P4 P5.**.** Open
1bdaf 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 a read-only cur
1bdb0 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 sor for the data
1bdb1 62 61 73 65 20 74 61 62 6c 65 20 77 68 6f 73 65 base table whose
1bdb2 20 72 6f 6f 74 20 70 61 67 65 20 69 73 0a 2a 2a root page is.**
1bdb3 20 50 32 20 69 6e 20 61 20 64 61 74 61 62 61 73 P2 in a databas
1bdb4 65 20 66 69 6c 65 2e 20 20 54 68 65 20 64 61 74 e file. The dat
1bdb5 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 64 65 abase file is de
1bdb6 74 65 72 6d 69 6e 65 64 20 62 79 20 50 33 2e 20 termined by P3.
1bdb7 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61 6e 73 20 .** P3==0 means
1bdb8 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1bdb9 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e 73 20 74 e, P3==1 means t
1bdba 68 65 20 64 61 74 61 62 61 73 65 20 75 73 65 64 he database used
1bdbb 20 66 6f 72 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 for .** tempora
1bdbc 72 79 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 50 ry tables, and P
1bdbd 33 3e 31 20 6d 65 61 6e 73 20 75 73 65 64 20 74 3>1 means used t
1bdbe 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
1bdbf 20 61 74 74 61 63 68 65 64 0a 2a 2a 20 64 61 74 attached.** dat
1bdc0 61 62 61 73 65 2e 20 20 47 69 76 65 20 74 68 65 abase. Give the
1bdc1 20 6e 65 77 20 63 75 72 73 6f 72 20 61 6e 20 69 new cursor an i
1bdc2 64 65 6e 74 69 66 69 65 72 20 6f 66 20 50 31 2e dentifier of P1.
1bdc3 20 20 54 68 65 20 50 31 0a 2a 2a 20 76 61 6c 75 The P1.** valu
1bdc4 65 73 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 63 es need not be c
1bdc5 6f 6e 74 69 67 75 6f 75 73 20 62 75 74 20 61 6c ontiguous but al
1bdc6 6c 20 50 31 20 76 61 6c 75 65 73 20 73 68 6f 75 l P1 values shou
1bdc7 6c 64 20 62 65 20 73 6d 61 6c 6c 20 69 6e 74 65 ld be small inte
1bdc8 67 65 72 73 2e 0a 2a 2a 20 49 74 20 69 73 20 61 gers..** It is a
1bdc9 6e 20 65 72 72 6f 72 20 66 6f 72 20 50 31 20 74 n error for P1 t
1bdca 6f 20 62 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a o be negative..*
1bdcb 2a 0a 2a 2a 20 49 66 20 50 35 21 3d 30 20 74 68 *.** If P5!=0 th
1bdcc 65 6e 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65 en use the conte
1bdcd 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 nt of register P
1bdce 32 20 61 73 20 74 68 65 20 72 6f 6f 74 20 70 61 2 as the root pa
1bdcf 67 65 2c 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 76 ge, not.** the v
1bdd0 61 6c 75 65 20 6f 66 20 50 32 20 69 74 73 65 6c alue of P2 itsel
1bdd1 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 77 f..**.** There w
1bdd2 69 6c 6c 20 62 65 20 61 20 72 65 61 64 20 6c 6f ill be a read lo
1bdd3 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
1bdd4 73 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 72 se whenever ther
1bdd5 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20 e is an.** open
1bdd6 63 75 72 73 6f 72 2e 20 20 49 66 20 74 68 65 20 cursor. If the
1bdd7 64 61 74 61 62 61 73 65 20 77 61 73 20 75 6e 6c database was unl
1bdd8 6f 63 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 74 ocked prior to t
1bdd9 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a his instruction.
1bdda 2a 2a 20 74 68 65 6e 20 61 20 72 65 61 64 20 6c ** then a read l
1bddb 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65 64 20 ock is acquired
1bddc 61 73 20 70 61 72 74 20 6f 66 20 74 68 69 73 20 as part of this
1bddd 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 41 20 instruction. A
1bdde 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c read.** lock all
1bddf 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ows other proces
1bde0 73 65 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 ses to read the
1bde1 64 61 74 61 62 61 73 65 20 62 75 74 20 70 72 6f database but pro
1bde2 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79 20 6f 74 hibits.** any ot
1bde3 68 65 72 20 70 72 6f 63 65 73 73 20 66 72 6f 6d her process from
1bde4 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 64 modifying the d
1bde5 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 72 65 atabase. The re
1bde6 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 72 65 ad lock is.** re
1bde7 6c 65 61 73 65 64 20 77 68 65 6e 20 61 6c 6c 20 leased when all
1bde8 63 75 72 73 6f 72 73 20 61 72 65 20 63 6c 6f 73 cursors are clos
1bde9 65 64 2e 20 20 49 66 20 74 68 69 73 20 69 6e 73 ed. If this ins
1bdea 74 72 75 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 truction attempt
1bdeb 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61 20 72 65 s.** to get a re
1bdec 61 64 20 6c 6f 63 6b 20 62 75 74 20 66 61 69 6c ad lock but fail
1bded 73 2c 20 74 68 65 20 73 63 72 69 70 74 20 74 65 s, the script te
1bdee 72 6d 69 6e 61 74 65 73 20 77 69 74 68 20 61 6e rminates with an
1bdef 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 .** SQLITE_BUSY
1bdf0 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a error code..**.*
1bdf1 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d * The P4 value m
1bdf2 61 79 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 ay be either an
1bdf3 69 6e 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33 integer (P4_INT3
1bdf4 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 2) or a pointer
1bdf5 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20 to.** a KeyInfo
1bdf6 73 74 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45 structure (P4_KE
1bdf7 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73 YINFO). If it is
1bdf8 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1bdf9 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 KeyInfo .** stru
1bdfa 63 74 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64 cture, then said
1bdfb 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69 6e structure defin
1bdfc 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 es the content a
1bdfd 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a nd collating .**
1bdfe 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65 sequence of the
1bdff 20 69 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 index being ope
1be00 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ned. Otherwise,
1be01 69 66 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65 if P4 is an inte
1be02 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 ger .** value, i
1be03 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 t is set to the
1be04 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
1be05 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a s in the table..
1be06 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f **.** See also O
1be07 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 penWrite..*/./*
1be08 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57 72 69 74 Opcode: OpenWrit
1be09 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 e P1 P2 P3 P4 P5
1be0a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 .**.** Open a re
1be0b 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 ad/write cursor
1be0c 6e 61 6d 65 64 20 50 31 20 6f 6e 20 74 68 65 20 named P1 on the
1be0d 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 table or index w
1be0e 68 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 hose root.** pag
1be0f 65 20 69 73 20 50 32 2e 20 20 4f 72 20 69 66 20 e is P2. Or if
1be10 50 35 21 3d 30 20 75 73 65 20 74 68 65 20 63 6f P5!=0 use the co
1be11 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 ntent of registe
1be12 72 20 50 32 20 74 6f 20 66 69 6e 64 20 74 68 65 r P2 to find the
1be13 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 0a 2a .** root page..*
1be14 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 *.** The P4 valu
1be15 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 e may be either
1be16 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49 an integer (P4_I
1be17 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 NT32) or a point
1be18 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e er to.** a KeyIn
1be19 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 50 34 fo structure (P4
1be1a 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 _KEYINFO). If it
1be1b 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1be1c 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 a KeyInfo .** s
1be1d 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73 tructure, then s
1be1e 61 69 64 20 73 74 72 75 63 74 75 72 65 20 64 65 aid structure de
1be1f 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e fines the conten
1be20 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 t and collating
1be21 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20 .** sequence of
1be22 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 the index being
1be23 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 opened. Otherwis
1be24 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e 20 69 e, if P4 is an i
1be25 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 nteger .** value
1be26 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 , it is set to t
1be27 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1be28 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c umns in the tabl
1be29 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e e..**.** This in
1be2a 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20 struction works
1be2b 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65 just like OpenRe
1be2c 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ad except that i
1be2d 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73 t opens the curs
1be2e 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72 or.** in read/wr
1be2f 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61 ite mode. For a
1be30 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68 given table, th
1be31 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f ere can be one o
1be32 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79 r more read-only
1be33 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61 .** cursors or a
1be34 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69 single read/wri
1be35 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f te cursor but no
1be36 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65 t both..**.** Se
1be37 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e e also OpenRead.
1be38 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e .*/.case OP_Open
1be39 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f 4f 70 Read:.case OP_Op
1be3a 65 6e 57 72 69 74 65 3a 20 7b 0a 20 20 69 6e 74 enWrite: {. int
1be3b 20 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 4b nField = 0;. K
1be3c 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
1be3d 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 20 3d 20 = 0;. int i =
1be3e 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70 pOp->p1;. int p
1be3f 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 2 = pOp->p2;. i
1be40 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 nt iDb = pOp->p3
1be41 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a ;. int wrFlag;.
1be42 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 Btree *pX;. V
1be43 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b dbeCursor *pCur;
1be44 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 0a 20 . Db *pDb;. .
1be45 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
1be46 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
1be47 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e ;. assert( (p->
1be48 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c btreeMask & (1<<
1be49 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 iDb))!=0 );. pD
1be4a 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 b = &db->aDb[iDb
1be4b 5d 3b 0a 20 20 70 58 20 3d 20 70 44 62 2d 3e 70 ];. pX = pDb->p
1be4c 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 58 Bt;. assert( pX
1be4d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 !=0 );. if( pOp
1be4e 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 ->opcode==OP_Ope
1be4f 6e 57 72 69 74 65 20 29 7b 0a 20 20 20 20 77 72 nWrite ){. wr
1be50 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 Flag = 1;. if
1be51 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e ( pDb->pSchema->
1be52 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d file_format < p-
1be53 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 >minWriteFileFor
1be54 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e mat ){. p->
1be55 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d minWriteFileForm
1be56 61 74 20 3d 20 70 44 62 2d 3e 70 53 63 68 65 6d at = pDb->pSchem
1be57 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a a->file_format;.
1be58 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1be59 20 20 20 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20 wrFlag = 0;.
1be5a 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 }. if( pOp->p5
1be5b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1be5c 70 32 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 p2>0 );. asse
1be5d 72 74 28 20 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 rt( p2<=p->nMem
1be5e 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 70 );. pIn2 = &p
1be5f 2d 3e 61 4d 65 6d 5b 70 32 5d 3b 0a 20 20 20 20 ->aMem[p2];.
1be60 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e sqlite3VdbeMemIn
1be61 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a tegerify(pIn2);.
1be62 20 20 20 20 70 32 20 3d 20 28 69 6e 74 29 70 49 p2 = (int)pI
1be63 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 69 66 28 n2->u.i;. if(
1be64 20 70 32 3c 32 20 29 20 7b 0a 20 20 20 20 20 20 p2<2 ) {.
1be65 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
1be66 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
1be67 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1be68 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 o_error;. }.
1be69 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d }. assert( i>=
1be6a 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 0 );. if( pOp->
1be6b 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e p4type==P4_KEYIN
1be6c 46 4f 20 29 7b 0a 20 20 20 20 70 4b 65 79 49 6e FO ){. pKeyIn
1be6d 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 fo = pOp->p4.pKe
1be6e 79 49 6e 66 6f 3b 0a 20 20 20 20 70 4b 65 79 49 yInfo;. pKeyI
1be6f 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 nfo->enc = ENC(p
1be70 2d 3e 64 62 29 3b 0a 20 20 20 20 6e 46 69 65 6c ->db);. nFiel
1be71 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 d = pKeyInfo->nF
1be72 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 ield+1;. }else
1be73 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d if( pOp->p4type=
1be74 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20 20 =P4_INT32 ){.
1be75 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 nField = pOp->p
1be76 34 2e 69 3b 0a 20 20 7d 0a 20 20 70 43 75 72 20 4.i;. }. pCur
1be77 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 = allocateCursor
1be78 28 70 2c 20 69 2c 20 6e 46 69 65 6c 64 2c 20 69 (p, i, nField, i
1be79 44 62 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 43 Db, 1);. if( pC
1be7a 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f ur==0 ) goto no_
1be7b 6d 65 6d 3b 0a 20 20 70 43 75 72 2d 3e 6e 75 6c mem;. pCur->nul
1be7c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d lRow = 1;. rc =
1be7d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
1be7e 73 6f 72 28 70 58 2c 20 70 32 2c 20 77 72 46 6c sor(pX, p2, wrFl
1be7f 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 ag, pKeyInfo, pC
1be80 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 ur->pCursor);.
1be81 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d pCur->pKeyInfo =
1be82 20 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 20 73 77 pKeyInfo;.. sw
1be83 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20 itch( rc ){.
1be84 63 61 73 65 20 53 51 4c 49 54 45 5f 42 55 53 59 case SQLITE_BUSY
1be85 3a 20 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 : {. p->pc
1be86 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 = pc;. p->r
1be87 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f c = rc = SQLITE_
1be88 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f BUSY;. goto
1be89 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 vdbe_return;.
1be8a 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
1be8b 49 54 45 5f 4f 4b 3a 20 7b 0a 20 20 20 20 20 20 ITE_OK: {.
1be8c 69 6e 74 20 66 6c 61 67 73 20 3d 20 73 71 6c 69 int flags = sqli
1be8d 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 te3BtreeFlags(pC
1be8e 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 ur->pCursor);.
1be8f 20 20 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 /* Sanity ch
1be90 65 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 74 68 ecking. Only th
1be91 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 e lower four bit
1be92 73 20 6f 66 20 74 68 65 20 66 6c 61 67 73 20 62 s of the flags b
1be93 79 74 65 20 73 68 6f 75 6c 64 0a 20 20 20 20 20 yte should.
1be94 20 2a 2a 20 62 65 20 75 73 65 64 2e 20 20 42 69 ** be used. Bi
1be95 74 20 33 20 28 6d 61 73 6b 20 30 78 30 38 29 20 t 3 (mask 0x08)
1be96 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 is unpredictable
1be97 2e 20 20 54 68 65 20 6c 6f 77 65 72 20 33 20 62 . The lower 3 b
1be98 69 74 73 0a 20 20 20 20 20 20 2a 2a 20 28 6d 61 its. ** (ma
1be99 73 6b 20 30 78 30 37 29 20 73 68 6f 75 6c 64 20 sk 0x07) should
1be9a 62 65 20 65 69 74 68 65 72 20 35 20 28 69 6e 74 be either 5 (int
1be9b 6b 65 79 2b 6c 65 61 66 64 61 74 61 20 66 6f 72 key+leafdata for
1be9c 20 74 61 62 6c 65 73 29 20 6f 72 0a 20 20 20 20 tables) or.
1be9d 20 20 2a 2a 20 32 20 28 7a 65 72 6f 64 61 74 61 ** 2 (zerodata
1be9e 20 66 6f 72 20 69 6e 64 69 63 65 73 29 2e 20 20 for indices).
1be9f 49 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69 If these conditi
1bea0 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6d 65 74 20 ons are not met
1bea1 69 74 20 63 61 6e 0a 20 20 20 20 20 20 2a 2a 20 it can. **
1bea2 6f 6e 6c 79 20 6d 65 61 6e 20 74 68 61 74 20 77 only mean that w
1bea3 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 e are dealing wi
1bea4 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 th a corrupt dat
1bea5 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 abase file.
1bea6 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 66 */. if( (f
1bea7 6c 61 67 73 20 26 20 30 78 66 30 29 21 3d 30 20 lags & 0xf0)!=0
1bea8 7c 7c 20 28 28 66 6c 61 67 73 20 26 20 30 78 30 || ((flags & 0x0
1bea9 37 29 21 3d 35 20 26 26 20 28 66 6c 61 67 73 20 7)!=5 && (flags
1beaa 26 20 30 78 30 37 29 21 3d 32 29 20 29 7b 0a 20 & 0x07)!=2) ){.
1beab 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
1beac 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1bead 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 . goto ab
1beae 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1beaf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1beb0 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 pCur->isTable =
1beb1 28 66 6c 61 67 73 20 26 20 42 54 52 45 45 5f 49 (flags & BTREE_I
1beb2 4e 54 4b 45 59 29 21 3d 30 20 3f 31 3a 30 3b 0a NTKEY)!=0 ?1:0;.
1beb3 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e pCur->isIn
1beb4 64 65 78 20 3d 20 28 66 6c 61 67 73 20 26 20 42 dex = (flags & B
1beb5 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 21 3d TREE_ZERODATA)!=
1beb6 30 20 3f 31 3a 30 3b 0a 20 20 20 20 20 20 2f 2a 0 ?1:0;. /*
1beb7 20 49 66 20 50 34 3d 3d 30 20 69 74 20 6d 65 61 If P4==0 it mea
1beb8 6e 73 20 77 65 20 61 72 65 20 65 78 70 65 63 74 ns we are expect
1beb9 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74 61 62 ed to open a tab
1beba 6c 65 2e 20 20 49 66 20 50 34 21 3d 30 20 74 68 le. If P4!=0 th
1bebb 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 65 en. ** we e
1bebc 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70 65 6e xpect to be open
1bebd 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 ing an index. I
1bebe 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 77 68 f this is not wh
1bebf 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20 20 20 at happened,.
1bec0 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 ** then the d
1bec1 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 atabase is corru
1bec2 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 pt. */.
1bec3 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69 73 54 if( (pCur->isT
1bec4 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70 34 74 able && pOp->p4t
1bec5 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 29 ype==P4_KEYINFO)
1bec6 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43 75 72 . || (pCur
1bec7 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70 4f 70 ->isIndex && pOp
1bec8 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 59 ->p4type!=P4_KEY
1bec9 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20 20 20 INFO) ){.
1beca 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
1becb 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
1becc 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1becd 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1bece 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
1becf 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1bed0 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20 7b 0a SQLITE_EMPTY: {.
1bed1 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 54 61 pCur->isTa
1bed2 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70 ble = pOp->p4typ
1bed3 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 e!=P4_KEYINFO;.
1bed4 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64 pCur->isInd
1bed5 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73 54 61 ex = !pCur->isTa
1bed6 62 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d ble;. pCur-
1bed7 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 >pCursor = 0;.
1bed8 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1bed9 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b OK;. break;
1beda 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
1bedb 6c 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f lt: {. goto
1bedc 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1bedd 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ror;. }. }.
1bede 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1bedf 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 code: OpenEpheme
1bee0 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a ral P1 P2 * P4 *
1bee1 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 .**.** Open a ne
1bee2 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61 w cursor P1 to a
1bee3 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 transient table
1bee4 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 ..** The cursor
1bee5 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 is always opened
1bee6 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e read/write even
1bee7 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e if .** the main
1bee8 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 61 database is rea
1bee9 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 d-only. The tra
1beea 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 75 61 nsient or virtua
1beeb 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65 l.** table is de
1beec 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 leted automatica
1beed 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75 72 lly when the cur
1beee 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a sor is closed..*
1beef 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e *.** P2 is the n
1bef0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
1bef1 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 in the virtual
1bef2 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 table..** The cu
1bef3 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 rsor points to a
1bef4 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 66 20 BTree table if
1bef5 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 P4==0 and to a B
1bef6 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 Tree index.** if
1bef7 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 P4 is not 0. I
1bef8 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c f P4 is not NULL
1bef9 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 , it points to a
1befa 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
1befb 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 6e re.** that defin
1befc 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 es the format of
1befd 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 6e 64 keys in the ind
1befe 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f ex..**.** This o
1beff 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 63 pcode was once c
1bf00 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 alled OpenTemp.
1bf01 20 42 75 74 20 74 68 61 74 20 63 72 65 61 74 65 But that create
1bf02 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 d.** confusion b
1bf03 65 63 61 75 73 65 20 74 68 65 20 74 65 72 6d 20 ecause the term
1bf04 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 "temp table", mi
1bf05 67 68 74 20 72 65 66 65 72 20 65 69 74 68 65 72 ght refer either
1bf06 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 61 .** to a TEMP ta
1bf07 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c 20 6c ble at the SQL l
1bf08 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 evel, or to a ta
1bf09 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a ble opened by.**
1bf0a 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 this opcode. T
1bf0b 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 hen this opcode
1bf0c 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 was call OpenVir
1bf0d 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 tual. But.** th
1bf0e 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e 66 75 at created confu
1bf0f 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 77 68 sion with the wh
1bf10 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c ole virtual-tabl
1bf11 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 e idea..*/.case
1bf12 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
1bf13 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f : {. int i = pO
1bf14 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 p->p1;. VdbeCur
1bf15 73 6f 72 20 2a 70 43 78 3b 0a 20 20 73 74 61 74 sor *pCx;. stat
1bf16 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65 ic const int ope
1bf17 6e 46 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20 nFlags = .
1bf18 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
1bf19 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51 WRITE |. SQ
1bf1a 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
1bf1b 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f |. SQLITE_
1bf1c 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c OPEN_EXCLUSIVE |
1bf1d 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 . SQLITE_OP
1bf1e 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
1bf1f 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f |. SQLITE_
1bf20 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 OPEN_TRANSIENT_D
1bf21 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e B;.. assert( i>
1bf22 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 6c =0 );. pCx = al
1bf23 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 locateCursor(p,
1bf24 69 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20 i, pOp->p2, -1,
1bf25 31 29 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 1);. if( pCx==0
1bf26 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1bf27 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d pCx->nullRow =
1bf28 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 1;. rc = sqlit
1bf29 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 e3BtreeFactory(d
1bf2a 62 2c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f b, 0, 1, SQLITE_
1bf2b 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 DEFAULT_TEMP_CAC
1bf2c 48 45 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 HE_SIZE, openFla
1bf2d 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 gs,.
1bf2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
1bf2f 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 pCx->pBt);. if(
1bf30 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1bf31 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1bf32 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e e3BtreeBeginTran
1bf33 73 28 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a s(pCx->pBt, 1);.
1bf34 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
1bf35 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f LITE_OK ){. /
1bf36 2a 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 * If a transient
1bf37 20 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72 index is requir
1bf38 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79 ed, create it by
1bf39 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 calling. **
1bf3a 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 sqlite3BtreeCrea
1bf3b 74 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74 teTable() with t
1bf3c 68 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 he BTREE_ZERODAT
1bf3d 41 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 A flag before.
1bf3e 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e ** opening it.
1bf3f 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 If a transient
1bf40 74 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65 table is require
1bf41 64 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a d, just use the.
1bf42 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 ** automatic
1bf43 61 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62 ally created tab
1bf44 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 le with root-pag
1bf45 65 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 e 1 (an INTKEY t
1bf46 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 able).. */.
1bf47 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b if( pOp->p4.pK
1bf48 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 eyInfo ){.
1bf49 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 int pgno;.
1bf4a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1bf4b 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 ype==P4_KEYINFO
1bf4c 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 );. rc = sq
1bf4d 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 lite3BtreeCreate
1bf4e 54 61 62 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20 Table(pCx->pBt,
1bf4f 26 70 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 &pgno, BTREE_ZER
1bf50 4f 44 41 54 41 29 3b 20 0a 20 20 20 20 20 20 69 ODATA); . i
1bf51 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1bf52 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1bf53 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 rt( pgno==MASTER
1bf54 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20 _ROOT+1 );.
1bf55 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1bf56 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e treeCursor(pCx->
1bf57 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20 pBt, pgno, 1, .
1bf58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bf59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
1bf5a 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 KeyInfo*)pOp->p4
1bf5b 2e 7a 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 .z, pCx->pCursor
1bf5c 29 3b 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e );. pCx->
1bf5d 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e pKeyInfo = pOp->
1bf5e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 p4.pKeyInfo;.
1bf5f 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e pCx->pKeyIn
1bf60 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d fo->enc = ENC(p-
1bf61 3e 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 >db);. }.
1bf62 20 20 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 pCx->isTable
1bf63 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
1bf64 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1bf65 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 70 te3BtreeCursor(p
1bf66 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f Cx->pBt, MASTER_
1bf67 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 70 43 78 2d ROOT, 1, 0, pCx-
1bf68 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 >pCursor);.
1bf69 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 pCx->isTable =
1bf6a 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 1;. }. }. p
1bf6b 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 Cx->isIndex = !p
1bf6c 43 78 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 Cx->isTable;. b
1bf6d 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1bf6e 64 65 3a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 de: OpenPseudo P
1bf6f 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1bf70 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72 * Open a new cur
1bf71 73 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 sor that points
1bf72 74 6f 20 61 20 66 61 6b 65 20 74 61 62 6c 65 20 to a fake table
1bf73 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 that contains a
1bf74 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 single.** row of
1bf75 20 64 61 74 61 2e 20 20 41 6e 79 20 61 74 74 65 data. Any atte
1bf76 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 73 mpt to write a s
1bf77 65 63 6f 6e 64 20 72 6f 77 20 6f 66 20 64 61 74 econd row of dat
1bf78 61 20 63 61 75 73 65 73 20 74 68 65 0a 2a 2a 20 a causes the.**
1bf79 66 69 72 73 74 20 72 6f 77 20 74 6f 20 62 65 20 first row to be
1bf7a 64 65 6c 65 74 65 64 2e 20 20 41 6c 6c 20 64 61 deleted. All da
1bf7b 74 61 20 69 73 20 64 65 6c 65 74 65 64 20 77 68 ta is deleted wh
1bf7c 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 en the cursor is
1bf7d 0a 2a 2a 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a .** closed..**.*
1bf7e 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 * A pseudo-table
1bf7f 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73 created by this
1bf80 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 66 75 opcode is usefu
1bf81 6c 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 l for holding th
1bf82 65 0a 2a 2a 20 4e 45 57 20 6f 72 20 4f 4c 44 20 e.** NEW or OLD
1bf83 74 61 62 6c 65 73 20 69 6e 20 61 20 74 72 69 67 tables in a trig
1bf84 67 65 72 2e 20 20 41 6c 73 6f 20 75 73 65 64 20 ger. Also used
1bf85 74 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73 69 to hold the a si
1bf86 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 ngle.** row outp
1bf87 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 ut from the sort
1bf88 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 er so that the r
1bf89 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 ow can be decomp
1bf8a 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 osed into.** ind
1bf8b 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 ividual columns
1bf8c 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c using the OP_Col
1bf8d 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a umn opcode..**.*
1bf8e 2a 20 57 68 65 6e 20 4f 50 5f 49 6e 73 65 72 74 * When OP_Insert
1bf8f 20 69 73 20 65 78 65 63 75 74 65 64 20 74 6f 20 is executed to
1bf90 69 6e 73 65 72 74 20 61 20 72 6f 77 20 69 6e 20 insert a row in
1bf91 74 6f 20 74 68 65 20 70 73 65 75 64 6f 20 74 61 to the pseudo ta
1bf92 62 6c 65 2c 0a 2a 2a 20 74 68 65 20 70 73 65 75 ble,.** the pseu
1bf93 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f 72 20 do-table cursor
1bf94 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 6d may or may not m
1bf95 61 6b 65 20 69 74 27 73 20 6f 77 6e 20 63 6f 70 ake it's own cop
1bf96 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 y of the.** orig
1bf97 69 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e 20 49 inal row data. I
1bf98 66 20 50 32 20 69 73 20 30 2c 20 74 68 65 6e 20 f P2 is 0, then
1bf99 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 the pseudo-table
1bf9a 20 77 69 6c 6c 20 63 6f 70 79 20 74 68 65 0a 2a will copy the.*
1bf9b 2a 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 20 64 * original row d
1bf9c 61 74 61 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ata. Otherwise,
1bf9d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1bf9e 20 6f 72 69 67 69 6e 61 6c 20 6d 65 6d 6f 72 79 original memory
1bf9f 20 63 65 6c 6c 0a 2a 2a 20 69 73 20 73 74 6f 72 cell.** is stor
1bfa0 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
1bfa1 2c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 , the vdbe progr
1bfa2 61 6d 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 am must ensure t
1bfa3 68 61 74 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f hat the .** memo
1bfa4 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 ry cell containi
1bfa5 6e 67 20 74 68 65 20 72 6f 77 20 64 61 74 61 20 ng the row data
1bfa6 69 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 74 is not overwritt
1bfa7 65 6e 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 en until the.**
1bfa8 70 73 65 75 64 6f 20 74 61 62 6c 65 20 69 73 20 pseudo table is
1bfa9 63 6c 6f 73 65 64 20 28 6f 72 20 61 20 6e 65 77 closed (or a new
1bfaa 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64 row is inserted
1bfab 20 69 6e 74 6f 20 69 74 29 2e 0a 2a 2a 0a 2a 2a into it)..**.**
1bfac 20 50 33 20 69 73 20 74 68 65 20 6e 75 6d 62 65 P3 is the numbe
1bfad 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
1bfae 68 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 he records that
1bfaf 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 62 will be stored b
1bfb0 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d y.** the pseudo-
1bfb1 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f table..*/.case O
1bfb2 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a P_OpenPseudo: {.
1bfb3 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1bfb4 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 1;. VdbeCursor
1bfb5 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28 20 *pCx;. assert(
1bfb6 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 i>=0 );. pCx =
1bfb7 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 allocateCursor(p
1bfb8 2c 20 69 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 , i, pOp->p3, -1
1bfb9 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 78 3d , 0);. if( pCx=
1bfba 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
1bfbb 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 ;. pCx->nullRow
1bfbc 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 70 73 65 = 1;. pCx->pse
1bfbd 75 64 6f 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 udoTable = 1;.
1bfbe 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f pCx->ephemPseudo
1bfbf 54 61 62 6c 65 20 3d 20 28 75 38 29 70 4f 70 2d Table = (u8)pOp-
1bfc0 3e 70 32 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 >p2;. pCx->isTa
1bfc1 62 6c 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e ble = 1;. pCx->
1bfc2 69 73 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 isIndex = 0;. b
1bfc3 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1bfc4 64 65 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a de: Close P1 * *
1bfc5 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 * *.**.** Close
1bfc6 20 61 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f a cursor previo
1bfc7 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50 usly opened as P
1bfc8 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74 1. If P1 is not
1bfc9 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 .** currently op
1bfca 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 en, this instruc
1bfcb 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
1bfcc 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 .*/.case OP_Clos
1bfcd 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 e: {. int i = p
1bfce 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 Op->p1;. assert
1bfcf 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e ( i>=0 && i<p->n
1bfd0 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 Cursor );. sqli
1bfd1 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f te3VdbeFreeCurso
1bfd2 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d r(p, p->apCsr[i]
1bfd3 29 3b 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d );. p->apCsr[i]
1bfd4 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1bfd5 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 ../* Opcode: See
1bfd6 6b 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20 kGe P1 P2 P3 P4
1bfd7 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f *.**.** If curso
1bfd8 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 r P1 refers to a
1bfd9 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 n SQL table (B-T
1bfda 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e ree that uses in
1bfdb 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a teger keys), .**
1bfdc 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 use the value i
1bfdd 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 n register P3 as
1bfde 20 74 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75 the key. If cu
1bfdf 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a rsor P1 refers .
1bfe0 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 ** to an SQL ind
1bfe1 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 ex, then P3 is t
1bfe2 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 he first in an a
1bfe3 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 rray of P4 regis
1bfe4 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 ters .** that ar
1bfe5 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 e used as an unp
1bfe6 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e acked index key.
1bfe7 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 .**.** Repositi
1bfe8 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 on cursor P1 so
1bfe9 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1bfea 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 to the smallest
1bfeb 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 entry that .** i
1bfec 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
1bfed 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b r equal to the k
1bfee 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 ey value. If the
1bfef 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 re are no record
1bff0 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 s .** greater th
1bff1 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
1bff2 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 he key and P2 is
1bff3 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 not zero, then
1bff4 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a jump to P2..**.*
1bff5 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e * See also: Foun
1bff6 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 d, NotFound, Dis
1bff7 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 tinct, SeekLt, S
1bff8 65 65 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f eekGt, SeekLe.*/
1bff9 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b ./* Opcode: Seek
1bffa 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a Gt P1 P2 P3 P4 *
1bffb 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 .**.** If cursor
1bffc 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e P1 refers to an
1bffd 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 SQL table (B-Tr
1bffe 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 ee that uses int
1bfff 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 eger keys), .**
1c000 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e use the value in
1c001 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 register P3 as
1c002 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 a key. If cursor
1c003 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 P1 refers .** t
1c004 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 o an SQL index,
1c005 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 then P3 is the f
1c006 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 irst in an array
1c007 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1c008 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 .** that are us
1c009 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 ed as an unpacke
1c00a 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a d index key. .**
1c00b 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 .** Reposition c
1c00c 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 ursor P1 so that
1c00d 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 it points to t
1c00e 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 he smallest entr
1c00f 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 y that .** is gr
1c010 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b eater than the k
1c011 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 ey value. If the
1c012 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 re are no record
1c013 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a s greater than .
1c014 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 ** the key and P
1c015 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 2 is not zero, t
1c016 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a hen jump to P2..
1c017 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1c018 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c Found, NotFound,
1c019 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c Distinct, SeekL
1c01a 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c t, SeekGe, SeekL
1c01b 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 e.*/./* Opcode:
1c01c 53 65 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20 SeekLt P1 P2 P3
1c01d 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 P4 * .**.** If c
1c01e 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 ursor P1 refers
1c01f 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 to an SQL table
1c020 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 (B-Tree that use
1c021 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c s integer keys),
1c022 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c .** use the val
1c023 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1c024 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 3 as a key. If c
1c025 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 ursor P1 refers
1c026 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e .** to an SQL in
1c027 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 dex, then P3 is
1c028 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 the first in an
1c029 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 array of P4 regi
1c02a 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 sters .** that a
1c02b 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e re used as an un
1c02c 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 packed index key
1c02d 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 . .**.** Reposit
1c02e 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f ion cursor P1 so
1c02f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 that it points
1c030 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 to the largest
1c031 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 entry that .** i
1c032 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 s less than the
1c033 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 key value. If th
1c034 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 ere are no recor
1c035 64 73 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a ds less than .**
1c036 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 the key and P2
1c037 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
1c038 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1c039 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1c03a 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 und, NotFound, D
1c03b 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c istinct, SeekGt,
1c03c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a SeekGe, SeekLe.
1c03d 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 */./* Opcode: Se
1c03e 65 6b 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 ekLe P1 P2 P3 P4
1c03f 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 *.**.** If curs
1c040 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 or P1 refers to
1c041 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d an SQL table (B-
1c042 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 Tree that uses i
1c043 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a nteger keys), .*
1c044 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 * use the value
1c045 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 in register P3 a
1c046 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 s a key. If curs
1c047 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a or P1 refers .**
1c048 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 to an SQL index
1c049 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 , then P3 is the
1c04a 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 first in an arr
1c04b 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 ay of P4 registe
1c04c 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 rs .** that are
1c04d 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 used as an unpac
1c04e 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a ked index key. .
1c04f 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e **.** Reposition
1c050 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 cursor P1 so th
1c051 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 at it points to
1c052 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 the largest entr
1c053 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 y that .** is le
1c054 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
1c055 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 to the key valu
1c056 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 e. If there are
1c057 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c no records .** l
1c058 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
1c059 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 l to the key and
1c05a 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c P2 is not zero,
1c05b 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 then jump to P2
1c05c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1c05d 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e : Found, NotFoun
1c05e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 d, Distinct, See
1c05f 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 kGt, SeekGe, See
1c060 6b 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 kLt.*/.case OP_S
1c061 65 65 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f eekLt: /
1c062 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 * jump, in3 */.c
1c063 61 73 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 ase OP_SeekLe:
1c064 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1c065 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 in3 */.case OP_S
1c066 65 65 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f eekGe: /
1c067 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 * jump, in3 */.c
1c068 61 73 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b ase OP_SeekGt: {
1c069 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1c06a 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d in3 */. int i =
1c06b 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 pOp->p1;. Vdbe
1c06c 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 Cursor *pC;.. a
1c06d 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
1c06e 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1c06f 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1c070 21 3d 30 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d !=0 );. pC = p-
1c071 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 >apCsr[i];. ass
1c072 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 ert( pC!=0 );.
1c073 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 if( pC->pCursor!
1c074 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 =0 ){. int re
1c075 73 2c 20 6f 63 3b 0a 20 20 20 20 6f 63 20 3d 20 s, oc;. oc =
1c076 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 pOp->opcode;.
1c077 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 pC->nullRow = 0
1c078 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73 ;. if( pC->is
1c079 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 Table ){. i
1c07a 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a 64 iKey; /*
1c07b 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61 72 The rowid we ar
1c07c 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a e to seek to */.
1c07d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e . /* The in
1c07e 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20 put value in P3
1c07f 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20 might be of any
1c080 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72 type: integer, r
1c081 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20 eal, string,.
1c082 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e ** blob, or N
1c083 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65 ULL. But it nee
1c084 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 ds to be an inte
1c085 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 61 ger before we ca
1c086 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 n do. ** th
1c087 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72 e seek, so cover
1c088 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 61 t it. */. a
1c089 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e pplyNumericAffin
1c08a 69 74 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 ity(pIn3);.
1c08b 20 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 iKey = sqlite3V
1c08c 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 dbeIntValue(pIn3
1c08d 29 3b 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 );. pC->row
1c08e 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 0a idIsValid = 0;..
1c08f 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
1c090 50 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 6e P3 value could n
1c091 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 ot be converted
1c092 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 into an integer
1c093 77 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a 2a without. **
1c094 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 loss of informa
1c095 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 69 tion, then speci
1c096 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 al processing is
1c097 20 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f 0a required... */.
1c098 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d if( (pIn3-
1c099 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1c09a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1c09b 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 if( (pIn3->flags
1c09c 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 & MEM_Real)==0
1c09d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
1c09e 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 If the P3 value
1c09f 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 cannot be conver
1c0a0 74 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 6e ted into any kin
1c0a1 64 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a 20 d of a number,.
1c0a2 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e ** then
1c0a3 20 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f 74 the seek is not
1c0a4 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a 75 possible, so ju
1c0a5 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 mp to P2 */.
1c0a6 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1c0a7 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 p2 - 1;.
1c0a8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1c0a9 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 }. /* If
1c0aa 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 we reach this p
1c0ab 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 50 oint, then the P
1c0ac 33 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 3 value must be
1c0ad 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 20 a floating.
1c0ae 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62 ** point numb
1c0af 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 er. */. a
1c0b0 73 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c ssert( (pIn3->fl
1c0b1 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21 ags & MEM_Real)!
1c0b2 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 =0 );.. i
1c0b3 66 28 20 69 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 f( iKey==SMALLES
1c0b4 54 5f 49 4e 54 36 34 20 26 26 20 28 70 49 6e 33 T_INT64 && (pIn3
1c0b5 2d 3e 72 3c 28 64 6f 75 62 6c 65 29 69 4b 65 79 ->r<(double)iKey
1c0b6 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 || pIn3->r>0) )
1c0b7 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
1c0b8 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 74 he P3 value is t
1c0b9 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 o large in magni
1c0ba 74 75 64 65 20 74 6f 20 62 65 20 65 78 70 72 65 tude to be expre
1c0bb 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 ssed as an.
1c0bc 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e ** integer.
1c0bd 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 */. re
1c0be 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 s = 1;.
1c0bf 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 29 if( pIn3->r<0 )
1c0c0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 {. if
1c0c1 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 ( oc==OP_SeekGt
1c0c2 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 || oc==OP_SeekGe
1c0c3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1c0c4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1c0c5 72 65 65 46 69 72 73 74 28 70 43 2d 3e 70 43 75 reeFirst(pC->pCu
1c0c6 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 rsor, &res);.
1c0c7 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
1c0c8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 c!=SQLITE_OK ) g
1c0c9 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1c0ca 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 _error;.
1c0cb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1c0cc 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1c0cd 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 if( oc==OP_Se
1c0ce 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 ekLt || oc==OP_S
1c0cf 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 eekLe ){.
1c0d0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1c0d1 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d te3BtreeLast(pC-
1c0d2 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b >pCursor, &res);
1c0d3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 . i
1c0d4 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1c0d5 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1c0d6 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1c0d7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1c0d8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1c0d9 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 if( res ){.
1c0da 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d pc = pOp-
1c0db 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 >p2 - 1;.
1c0dc 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 }. b
1c0dd 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 reak;. }e
1c0de 6c 73 65 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 lse if( oc==OP_S
1c0df 65 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f eekLt || oc==OP_
1c0e0 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 SeekGe ){.
1c0e1 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 63 /* Use the c
1c0e2 65 69 6c 69 6e 67 28 29 20 66 75 6e 63 74 69 6f eiling() functio
1c0e3 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 n to convert rea
1c0e4 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 l->int */.
1c0e5 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 if( pIn3->r
1c0e6 3e 20 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29 > (double)iKey )
1c0e7 20 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20 iKey++;.
1c0e8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1c0e9 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f /* Use the flo
1c0ea 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f or() function to
1c0eb 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 convert real->i
1c0ec 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 nt */.
1c0ed 61 73 73 65 72 74 28 20 6f 63 3d 3d 4f 50 5f 53 assert( oc==OP_S
1c0ee 65 65 6b 4c 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f eekLe || oc==OP_
1c0ef 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 SeekGt );.
1c0f0 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 if( pIn3->r
1c0f1 3c 20 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29 < (double)iKey )
1c0f2 20 69 4b 65 79 2d 2d 3b 0a 20 20 20 20 20 20 20 iKey--;.
1c0f3 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 }. } .
1c0f4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1c0f5 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1c0f6 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 d(pC->pCursor, 0
1c0f7 2c 20 28 75 36 34 29 69 4b 65 79 2c 20 30 2c 20 , (u64)iKey, 0,
1c0f8 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 &res);. if(
1c0f9 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1c0fa 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 {. goto a
1c0fb 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1c0fc 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 r;. }.
1c0fd 20 69 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20 if( res==0 ){.
1c0fe 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 pC->rowid
1c0ff 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 IsValid = 1;.
1c100 20 20 20 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77 pC->lastRow
1c101 69 64 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 20 id = iKey;.
1c102 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1c103 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f UnpackedReco
1c104 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 rd r;. int
1c105 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 nField = pOp->p4
1c106 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 .i;. assert
1c107 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1c108 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20 4_INT32 );.
1c109 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c 64 3e assert( nField>
1c10a 30 20 29 3b 0a 20 20 20 20 20 20 72 2e 70 4b 65 0 );. r.pKe
1c10b 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 yInfo = pC->pKey
1c10c 49 6e 66 6f 3b 0a 20 20 20 20 20 20 72 2e 6e 46 Info;. r.nF
1c10d 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 46 69 65 ield = (u16)nFie
1c10e 6c 64 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 63 ld;. if( oc
1c10f 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 6f ==OP_SeekGt || o
1c110 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a c==OP_SeekLe ){.
1c111 20 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 r.flags
1c112 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b = UNPACKED_INCRK
1c113 45 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b EY;. }else{
1c114 0a 20 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73 . r.flags
1c115 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
1c116 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d r.aMem = &p-
1c117 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1c118 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1c119 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1c11a 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f acked(pC->pCurso
1c11b 72 2c 20 26 72 2c 20 30 2c 20 30 2c 20 26 72 65 r, &r, 0, 0, &re
1c11c 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 s);. if( rc
1c11d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1c11e 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 goto abor
1c11f 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1c120 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 }. pC
1c121 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1c122 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 0;. }. pC
1c123 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1c124 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 = 0;. pC->ca
1c125 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1c126 45 5f 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 20 E_STALE;.#ifdef
1c127 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 SQLITE_TEST.
1c128 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 sqlite3_search_c
1c129 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 ount++;.#endif.
1c12a 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 if( oc==OP_Se
1c12b 65 6b 47 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 ekGe || oc==OP_S
1c12c 65 65 6b 47 74 20 29 7b 0a 20 20 20 20 20 20 69 eekGt ){. i
1c12d 66 28 20 72 65 73 3c 30 20 7c 7c 20 28 72 65 73 f( res<0 || (res
1c12e 3d 3d 30 20 26 26 20 6f 63 3d 3d 4f 50 5f 53 65 ==0 && oc==OP_Se
1c12f 65 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20 ekGt) ){.
1c130 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1c131 65 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73 eeNext(pC->pCurs
1c132 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 or, &res);.
1c133 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1c134 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 E_OK ) goto abor
1c135 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1c136 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 pC->rowi
1c137 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1c138 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1c139 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 res = 0;.
1c13a 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1c13b 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d assert( oc=
1c13c 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f 63 =OP_SeekLt || oc
1c13d 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 ==OP_SeekLe );.
1c13e 20 20 20 20 20 69 66 28 20 72 65 73 3e 30 20 7c if( res>0 |
1c13f 7c 20 28 72 65 73 3d 3d 30 20 26 26 20 6f 63 3d | (res==0 && oc=
1c140 3d 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20 =OP_SeekLt) ){.
1c141 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1c142 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
1c143 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 (pC->pCursor, &r
1c144 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 es);. if(
1c145 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1c146 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1c147 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1c148 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c pC->rowidIsVal
1c149 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 id = 0;. }e
1c14a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
1c14b 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67 res might be neg
1c14c 61 74 69 76 65 20 62 65 63 61 75 73 65 20 74 68 ative because th
1c14d 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 e table is empty
1c14e 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 . Check to.
1c14f 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68 ** see if th
1c150 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a is is the case..
1c151 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1c152 20 20 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33 res = sqlite3
1c153 42 74 72 65 65 45 6f 66 28 70 43 2d 3e 70 43 75 BtreeEof(pC->pCu
1c154 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 rsor);. }.
1c155 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1c156 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 pOp->p2>0 );.
1c157 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
1c158 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1c159 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c - 1;. }. }el
1c15a 73 65 20 69 66 28 20 21 70 43 2d 3e 70 73 65 75 se if( !pC->pseu
1c15b 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f doTable ){. /
1c15c 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 * This happens w
1c15d 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 hen attempting t
1c15e 6f 20 6f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 o open the sqlit
1c15f 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a e3_master table.
1c160 20 20 20 20 2a 2a 20 66 6f 72 20 72 65 61 64 20 ** for read
1c161 61 63 63 65 73 73 20 72 65 74 75 72 6e 73 20 53 access returns S
1c162 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20 QLITE_EMPTY. In
1c163 74 68 69 73 20 63 61 73 65 20 61 6c 77 61 79 73 this case always
1c164 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 . ** take the
1c165 20 6a 75 6d 70 20 28 73 69 6e 63 65 20 74 68 65 jump (since the
1c166 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 re are no record
1c167 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 29 2e s in the table).
1c168 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d . */. pc =
1c169 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1c16a 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1c16b 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 20 50 31 Opcode: Seek P1
1c16c 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1c16d 50 31 20 69 73 20 61 6e 20 6f 70 65 6e 20 74 61 P1 is an open ta
1c16e 62 6c 65 20 63 75 72 73 6f 72 20 61 6e 64 20 50 ble cursor and P
1c16f 32 20 69 73 20 61 20 72 6f 77 69 64 20 69 6e 74 2 is a rowid int
1c170 65 67 65 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a eger. Arrange.*
1c171 2a 20 66 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65 * for P1 to move
1c172 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e so that it poin
1c173 74 73 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 ts to the rowid
1c174 67 69 76 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a given by P2..**.
1c175 2a 2a 20 54 68 69 73 20 69 73 20 61 63 74 75 61 ** This is actua
1c176 6c 6c 79 20 61 20 64 65 66 65 72 72 65 64 20 73 lly a deferred s
1c177 65 65 6b 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63 eek. Nothing ac
1c178 74 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 75 tually happens u
1c179 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73 ntil.** the curs
1c17a 6f 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 or is used to re
1c17b 61 64 20 61 20 72 65 63 6f 72 64 2e 20 20 54 68 ad a record. Th
1c17c 61 74 20 77 61 79 2c 20 69 66 20 6e 6f 20 72 65 at way, if no re
1c17d 61 64 73 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f ads.** occur, no
1c17e 20 75 6e 6e 65 63 65 73 73 61 72 79 20 49 2f 4f unnecessary I/O
1c17f 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73 happens..*/.cas
1c180 65 20 4f 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 e OP_Seek: {
1c181 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20 /* in2 */. int
1c182 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i = pOp->p1;. V
1c183 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a dbeCursor *pC;..
1c184 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1c185 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1c186 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 ;. pC = p->apCs
1c187 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 r[i];. assert(
1c188 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 pC!=0 );. if( p
1c189 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b C->pCursor!=0 ){
1c18a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1c18b 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
1c18c 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b pC->nullRow = 0;
1c18d 0a 20 20 20 20 70 43 2d 3e 6d 6f 76 65 74 6f 54 . pC->movetoT
1c18e 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56 arget = sqlite3V
1c18f 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 dbeIntValue(pIn2
1c190 29 3b 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 );. pC->rowid
1c191 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 IsValid = 0;.
1c192 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 pC->deferredMov
1c193 65 74 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 eto = 1;. }. b
1c194 72 65 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f reak;.}. ../* O
1c195 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 pcode: Found P1
1c196 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1c197 52 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 Register P3 hold
1c198 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 s a blob constru
1c199 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f cted by MakeReco
1c19a 72 64 2e 20 20 50 31 20 69 73 20 61 6e 20 69 6e rd. P1 is an in
1c19b 64 65 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 6e dex..** If an en
1c19c 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 73 try that matches
1c19d 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1c19e 67 69 73 74 65 72 20 70 33 20 65 78 69 73 74 73 gister p3 exists
1c19f 20 69 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a in P1 then.** j
1c1a0 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74 ump to P2. If t
1c1a1 68 65 20 50 33 20 76 61 6c 75 65 20 64 6f 65 73 he P3 value does
1c1a2 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 65 not match any e
1c1a3 6e 74 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 68 ntry in P1.** th
1c1a4 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 en fall thru. T
1c1a5 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 he P1 cursor is
1c1a6 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
1c1a7 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e the matching en
1c1a8 74 72 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 69 try.** if it exi
1c1a9 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 sts..**.** This
1c1aa 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75 instruction is u
1c1ab 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
1c1ac 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 the IN operator
1c1ad 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c 65 where the.** le
1c1ae 66 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 ft-hand side is
1c1af 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
1c1b0 6e 74 2e 20 20 50 31 20 6d 61 79 20 62 65 20 61 nt. P1 may be a
1c1b1 20 74 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 20 true index, or
1c1b2 69 74 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 74 it.** may be a t
1c1b3 65 6d 70 6f 72 61 72 79 20 69 6e 64 65 78 20 74 emporary index t
1c1b4 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 72 65 hat holds the re
1c1b5 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c sults of the SEL
1c1b6 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ECT.** statement
1c1b7 2e 20 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 . This instruc
1c1b8 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 tion is also use
1c1b9 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 d to implement t
1c1ba 68 65 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 6b he.** DISTINCT k
1c1bb 65 79 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 54 eyword in SELECT
1c1bc 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
1c1bd 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 ** This instruct
1c1be 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 69 6e ion checks if in
1c1bf 64 65 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 dex P1 contains
1c1c0 61 20 72 65 63 6f 72 64 20 66 6f 72 20 77 68 69 a record for whi
1c1c1 63 68 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 ch .** the first
1c1c2 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 N serialized va
1c1c3 6c 75 65 73 20 65 78 61 63 74 6c 79 20 6d 61 74 lues exactly mat
1c1c4 63 68 20 74 68 65 20 4e 20 73 65 72 69 61 6c 69 ch the N seriali
1c1c5 7a 65 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e zed values.** in
1c1c6 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72 the record in r
1c1c7 65 67 69 73 74 65 72 20 50 33 2c 20 77 68 65 72 egister P3, wher
1c1c8 65 20 4e 20 69 73 20 74 68 65 20 74 6f 74 61 6c e N is the total
1c1c9 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 number of value
1c1ca 73 20 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 72 s in.** the P3 r
1c1cb 65 63 6f 72 64 20 28 74 68 65 20 50 33 20 72 65 ecord (the P3 re
1c1cc 63 6f 72 64 20 69 73 20 61 20 70 72 65 66 69 78 cord is a prefix
1c1cd 20 6f 66 20 74 68 65 20 50 31 20 72 65 63 6f 72 of the P1 recor
1c1ce 64 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 d). .**.** See a
1c1cf 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 lso: NotFound, I
1c1d0 73 55 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 73 sUnique, NotExis
1c1d1 74 73 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a ts.*/./* Opcode:
1c1d2 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20 NotFound P1 P2
1c1d3 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 P3 * *.**.** Reg
1c1d4 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 ister P3 holds a
1c1d5 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 blob constructe
1c1d6 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e d by MakeRecord.
1c1d7 20 20 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e P1 is.** an in
1c1d8 64 65 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72 dex. If no entr
1c1d9 79 20 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 y exists in P1 t
1c1da 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 hat matches the
1c1db 62 6c 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a blob then jump.*
1c1dc 2a 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20 * to P2. If an
1c1dd 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69 73 74 entry does exist
1c1de 69 6e 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 ing, fall throug
1c1df 68 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 h. The cursor i
1c1e0 73 20 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 s left.** pointi
1c1e1 6e 67 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20 ng to the entry
1c1e2 74 68 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a 2a that matches..**
1c1e3 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1c1e4 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 und, NotExists,
1c1e5 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 IsUnique.*/.case
1c1e6 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 OP_NotFound:
1c1e7 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 /* jump, in3
1c1e8 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e */.case OP_Foun
1c1e9 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a d: { /* j
1c1ea 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e ump, in3 */. in
1c1eb 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 t i = pOp->p1;.
1c1ec 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73 int alreadyExis
1c1ed 74 73 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75 ts = 0;. VdbeCu
1c1ee 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 rsor *pC;. asse
1c1ef 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1c1f0 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 >nCursor );. as
1c1f1 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 sert( p->apCsr[i
1c1f2 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 ]!=0 );. if( (p
1c1f3 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 C = p->apCsr[i])
1c1f4 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a ->pCursor!=0 ){.
1c1f5 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1c1f6 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1c1f7 2a 70 49 64 78 4b 65 79 3b 0a 0a 20 20 20 20 61 *pIdxKey;.. a
1c1f8 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 ssert( pC->isTab
1c1f9 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 le==0 );. ass
1c1fa 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 ert( pIn3->flags
1c1fb 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 & MEM_Blob );.
1c1fc 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c pIdxKey = sql
1c1fd 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e ite3VdbeRecordUn
1c1fe 70 61 63 6b 28 70 43 2d 3e 70 4b 65 79 49 6e 66 pack(pC->pKeyInf
1c1ff 6f 2c 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 o, pIn3->n, pIn3
1c200 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->z,.
1c201 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c202 20 20 20 20 20 20 20 20 20 20 20 61 54 65 6d 70 aTemp
1c203 52 65 63 2c 20 73 69 7a 65 6f 66 28 61 54 65 6d Rec, sizeof(aTem
1c204 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20 pRec));. if(
1c205 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 pIdxKey==0 ){.
1c206 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1c207 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1c208 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 Op->opcode==OP_F
1c209 6f 75 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 49 ound ){. pI
1c20a 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 dxKey->flags |=
1c20b 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f UNPACKED_PREFIX_
1c20c 4d 41 54 43 48 3b 0a 20 20 20 20 7d 0a 20 20 20 MATCH;. }.
1c20d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1c20e 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
1c20f 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 70 49 (pC->pCursor, pI
1c210 64 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 72 65 dxKey, 0, 0, &re
1c211 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 s);. sqlite3V
1c212 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 dbeDeleteUnpacke
1c213 64 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 dRecord(pIdxKey)
1c214 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1c215 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1c216 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1c217 20 20 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 alreadyExists
1c218 3d 20 28 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20 = (res==0);.
1c219 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1c21a 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e to = 0;. pC->
1c21b 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1c21c 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 CHE_STALE;. }.
1c21d 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1c21e 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 ==OP_Found ){.
1c21f 20 20 69 66 28 20 61 6c 72 65 61 64 79 45 78 69 if( alreadyExi
1c220 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e sts ) pc = pOp->
1c221 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b p2 - 1;. }else{
1c222 0a 20 20 20 20 69 66 28 20 21 61 6c 72 65 61 64 . if( !alread
1c223 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 yExists ) pc = p
1c224 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a Op->p2 - 1;. }.
1c225 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c226 70 63 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20 pcode: IsUnique
1c227 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
1c228 0a 2a 2a 20 54 68 65 20 50 33 20 72 65 67 69 73 .** The P3 regis
1c229 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 ter contains an
1c22a 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e integer record n
1c22b 75 6d 62 65 72 2e 20 20 43 61 6c 6c 20 74 68 69 umber. Call thi
1c22c 73 0a 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 s.** record numb
1c22d 65 72 20 52 2e 20 20 54 68 65 20 50 34 20 72 65 er R. The P4 re
1c22e 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 gister contains
1c22f 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 63 72 65 an index key cre
1c230 61 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4d 61 ated.** using Ma
1c231 6b 65 52 65 63 6f 72 64 2e 20 20 43 61 6c 6c 20 keRecord. Call
1c232 69 74 20 4b 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 it K..**.** P1 i
1c233 73 20 61 6e 20 69 6e 64 65 78 2e 20 20 53 6f 20 s an index. So
1c234 69 74 20 68 61 73 20 6e 6f 20 64 61 74 61 20 61 it has no data a
1c235 6e 64 20 69 74 73 20 6b 65 79 20 63 6f 6e 73 69 nd its key consi
1c236 73 74 73 20 6f 66 20 61 0a 2a 2a 20 72 65 63 6f sts of a.** reco
1c237 72 64 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 rd generated by
1c238 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68 OP_MakeRecord wh
1c239 65 72 65 20 74 68 65 20 6c 61 73 74 20 66 69 65 ere the last fie
1c23a 6c 64 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 6f ld is the .** ro
1c23b 77 69 64 20 6f 66 20 74 68 65 20 65 6e 74 72 79 wid of the entry
1c23c 20 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 20 that the index
1c23d 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 20 0a 2a refers to..** .*
1c23e 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 * This instructi
1c23f 6f 6e 20 61 73 6b 73 20 69 66 20 74 68 65 72 65 on asks if there
1c240 20 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 is an entry in
1c241 50 31 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 P1 where the.**
1c242 66 69 65 6c 64 73 20 6d 61 74 63 68 65 73 20 4b fields matches K
1c243 20 62 75 74 20 74 68 65 20 72 6f 77 69 64 20 69 but the rowid i
1c244 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d s different from
1c245 20 52 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 R..** If there
1c246 69 73 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 is no such entry
1c247 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 , then there is
1c248 61 6e 20 69 6d 6d 65 64 69 61 74 65 0a 2a 2a 20 an immediate.**
1c249 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20 jump to P2. If
1c24a 61 6e 79 20 65 6e 74 72 79 20 64 6f 65 73 20 65 any entry does e
1c24b 78 69 73 74 20 77 68 65 72 65 20 74 68 65 20 69 xist where the i
1c24c 6e 64 65 78 20 73 74 72 69 6e 67 0a 2a 2a 20 6d ndex string.** m
1c24d 61 74 63 68 65 73 20 4b 20 62 75 74 20 74 68 65 atches K but the
1c24e 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 record number i
1c24f 73 20 6e 6f 74 20 52 2c 20 74 68 65 6e 20 74 68 s not R, then th
1c250 65 20 72 65 63 6f 72 64 0a 2a 2a 20 6e 75 6d 62 e record.** numb
1c251 65 72 20 66 6f 72 20 74 68 61 74 20 65 6e 74 72 er for that entr
1c252 79 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 y is written int
1c253 6f 20 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c o P3 and control
1c254 0a 2a 2a 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 .** falls throug
1c255 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e h to the next in
1c256 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a struction..**.**
1c257 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f See also: NotFo
1c258 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 und, NotExists,
1c259 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 Found.*/.case OP
1c25a 5f 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 _IsUnique: {
1c25b 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 /* jump, in3
1c25c 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f */. int i = pO
1c25d 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 p->p1;. VdbeCur
1c25e 73 6f 72 20 2a 70 43 78 3b 0a 20 20 42 74 43 75 sor *pCx;. BtCu
1c25f 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 4d rsor *pCrsr;. M
1c260 65 6d 20 2a 70 4b 3b 0a 20 20 69 36 34 20 52 3b em *pK;. i64 R;
1c261 0a 0a 20 20 2f 2a 20 50 6f 70 20 74 68 65 20 76 .. /* Pop the v
1c262 61 6c 75 65 20 52 20 6f 66 66 20 74 68 65 20 74 alue R off the t
1c263 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 0a op of the stack.
1c264 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
1c265 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 Op->p4type==P4_I
1c266 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74 NT32 );. assert
1c267 28 20 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26 ( pOp->p4.i>0 &&
1c268 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e pOp->p4.i<=p->n
1c269 4d 65 6d 20 29 3b 0a 20 20 70 4b 20 3d 20 26 70 Mem );. pK = &p
1c26a 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69 ->aMem[pOp->p4.i
1c26b 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ];. sqlite3Vdbe
1c26c 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 MemIntegerify(pI
1c26d 6e 33 29 3b 0a 20 20 52 20 3d 20 70 49 6e 33 2d n3);. R = pIn3-
1c26e 3e 75 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 20 >u.i;. assert(
1c26f 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1c270 72 73 6f 72 20 29 3b 0a 20 20 70 43 78 20 3d 20 rsor );. pCx =
1c271 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 p->apCsr[i];. a
1c272 73 73 65 72 74 28 20 70 43 78 21 3d 30 20 29 3b ssert( pCx!=0 );
1c273 0a 20 20 70 43 72 73 72 20 3d 20 70 43 78 2d 3e . pCrsr = pCx->
1c274 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 pCursor;. if( p
1c275 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 Crsr!=0 ){. i
1c276 6e 74 20 72 65 73 3b 0a 20 20 20 20 69 36 34 20 nt res;. i64
1c277 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
1c278 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 /* The re
1c279 63 6f 72 64 20 6e 75 6d 62 65 72 20 74 68 61 74 cord number that
1c27a 20 6d 61 74 63 68 65 73 20 4b 20 2a 2f 0a 20 20 matches K */.
1c27b 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1c27c 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 *pIdxKey; /*
1c27d 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f 6e Unpacked version
1c27e 20 6f 66 20 50 34 20 2a 2f 0a 0a 20 20 20 20 2f of P4 */.. /
1c27f 2a 20 4d 61 6b 65 20 73 75 72 65 20 4b 20 69 73 * Make sure K is
1c280 20 61 20 73 74 72 69 6e 67 20 61 6e 64 20 6d 61 a string and ma
1c281 6b 65 20 7a 4b 65 79 20 70 6f 69 6e 74 20 74 6f ke zKey point to
1c282 20 4b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 K. */. as
1c283 73 65 72 74 28 20 70 4b 2d 3e 66 6c 61 67 73 20 sert( pK->flags
1c284 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 & MEM_Blob );.
1c285 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 pIdxKey = sqli
1c286 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 te3VdbeRecordUnp
1c287 61 63 6b 28 70 43 78 2d 3e 70 4b 65 79 49 6e 66 ack(pCx->pKeyInf
1c288 6f 2c 20 70 4b 2d 3e 6e 2c 20 70 4b 2d 3e 7a 2c o, pK->n, pK->z,
1c289 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1c28a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c28b 20 20 20 20 20 20 20 61 54 65 6d 70 52 65 63 2c aTempRec,
1c28c 20 73 69 7a 65 6f 66 28 61 54 65 6d 70 52 65 63 sizeof(aTempRec
1c28d 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 ));. if( pIdx
1c28e 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Key==0 ){.
1c28f 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 goto no_mem;.
1c290 20 7d 0a 20 20 20 20 70 49 64 78 4b 65 79 2d 3e }. pIdxKey->
1c291 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 flags |= UNPACKE
1c292 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a D_IGNORE_ROWID;.
1c293 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 . /* Search f
1c294 6f 72 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50 or an entry in P
1c295 31 20 77 68 65 72 65 20 61 6c 6c 20 62 75 74 20 1 where all but
1c296 74 68 65 20 6c 61 73 74 20 72 6f 77 69 64 20 6d the last rowid m
1c297 61 74 63 68 20 4b 0a 20 20 20 20 2a 2a 20 49 66 atch K. ** If
1c298 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 there is no suc
1c299 68 20 65 6e 74 72 79 2c 20 6a 75 6d 70 20 69 6d h entry, jump im
1c29a 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e mediately to P2.
1c29b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
1c29c 72 74 28 20 70 43 78 2d 3e 64 65 66 65 72 72 65 rt( pCx->deferre
1c29d 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 dMoveto==0 );.
1c29e 20 20 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74 pCx->cacheStat
1c29f 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1c2a0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1c2a1 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1c2a2 61 63 6b 65 64 28 70 43 72 73 72 2c 20 70 49 64 acked(pCrsr, pId
1c2a3 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 72 65 73 xKey, 0, 0, &res
1c2a4 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
1c2a5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1c2a6 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
1c2a7 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
1c2a8 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 d(pIdxKey);.
1c2a9 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1c2aa 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d _to_error;. }
1c2ab 0a 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 29 . if( res<0 )
1c2ac 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1c2ad 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 ite3BtreeNext(pC
1c2ae 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 rsr, &res);.
1c2af 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
1c2b0 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1c2b1 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 2 - 1;. s
1c2b2 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
1c2b3 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 UnpackedRecord(p
1c2b4 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 IdxKey);.
1c2b5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
1c2b6 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
1c2b7 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 qlite3VdbeIdxKey
1c2b8 43 6f 6d 70 61 72 65 28 70 43 78 2c 20 70 49 64 Compare(pCx, pId
1c2b9 78 4b 65 79 2c 20 26 72 65 73 29 3b 20 0a 20 20 xKey, &res); .
1c2ba 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
1c2bb 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
1c2bc 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 d(pIdxKey);.
1c2bd 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1c2be 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 K ) goto abort_d
1c2bf 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1c2c0 20 69 66 28 20 72 65 73 3e 30 20 29 7b 0a 20 20 if( res>0 ){.
1c2c1 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1c2c2 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 - 1;. brea
1c2c3 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a k;. }.. /*
1c2c4 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 At this point,
1c2c5 70 43 72 73 72 20 69 73 20 70 6f 69 6e 74 69 6e pCrsr is pointin
1c2c6 67 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 69 6e g to an entry in
1c2c7 20 50 31 20 77 68 65 72 65 20 61 6c 6c 20 62 75 P1 where all bu
1c2c8 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6e t. ** the fin
1c2c9 61 6c 20 65 6e 74 72 79 20 28 74 68 65 20 72 6f al entry (the ro
1c2ca 77 69 64 29 20 6d 61 74 63 68 65 73 20 4b 2e 20 wid) matches K.
1c2cb 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
1c2cc 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 6e 61 the. ** fina
1c2cd 6c 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69 l rowid column i
1c2ce 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d s different from
1c2cf 20 52 2e 20 20 49 66 20 69 74 20 65 71 75 61 6c R. If it equal
1c2d0 73 20 52 20 74 68 65 6e 20 6a 75 6d 70 0a 20 20 s R then jump.
1c2d1 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 ** immediately
1c2d2 20 74 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 to P2.. */.
1c2d3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1c2d4 64 62 65 49 64 78 52 6f 77 69 64 28 70 43 72 73 dbeIdxRowid(pCrs
1c2d5 72 2c 20 26 76 29 3b 0a 20 20 20 20 69 66 28 20 r, &v);. if(
1c2d6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1c2d7 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
1c2d8 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1c2d9 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 76 3d }. if( v=
1c2da 3d 52 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d =R ){. pc =
1c2db 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1c2dc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1c2dd 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6e .. /* The fin
1c2de 61 6c 20 76 61 72 69 6e 74 20 6f 66 20 74 68 65 al varint of the
1c2df 20 6b 65 79 20 69 73 20 64 69 66 66 65 72 65 6e key is differen
1c2e0 74 20 66 72 6f 6d 20 52 2e 20 20 53 74 6f 72 65 t from R. Store
1c2e1 20 69 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20 it back. **
1c2e2 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52 33 into register R3
1c2e3 2e 20 20 28 54 68 65 20 72 65 63 6f 72 64 20 6e . (The record n
1c2e4 75 6d 62 65 72 20 6f 66 20 61 6e 20 65 6e 74 72 umber of an entr
1c2e5 79 20 74 68 61 74 20 76 69 6f 6c 61 74 65 73 0a y that violates.
1c2e6 20 20 20 20 2a 2a 20 61 20 55 4e 49 51 55 45 20 ** a UNIQUE
1c2e7 63 6f 6e 73 74 72 61 69 6e 74 2e 29 0a 20 20 20 constraint.).
1c2e8 20 2a 2f 0a 20 20 20 20 70 49 6e 33 2d 3e 75 2e */. pIn3->u.
1c2e9 69 20 3d 20 76 3b 0a 20 20 20 20 61 73 73 65 72 i = v;. asser
1c2ea 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d t( pIn3->flags&M
1c2eb 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 7d 0a 20 20 EM_Int );. }.
1c2ec 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c2ed 6f 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 ode: NotExists P
1c2ee 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1c2ef 2a 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e * Use the conten
1c2f0 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 t of register P3
1c2f1 20 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 as a integer ke
1c2f2 79 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 y. If a record
1c2f3 0a 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 .** with that ke
1c2f4 79 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 y does not exist
1c2f5 20 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c in table of P1,
1c2f6 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 then jump to P2
1c2f7 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 . .** If the rec
1c2f8 6f 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 ord does exist,
1c2f9 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 then fall thru.
1c2fa 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c The cursor is l
1c2fb 65 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 eft .** pointing
1c2fc 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 to the record i
1c2fd 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a f it exists..**.
1c2fe 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 ** The differenc
1c2ff 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f e between this o
1c300 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 peration and Not
1c301 46 6f 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 Found is that th
1c302 69 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 is.** operation
1c303 61 73 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 assumes the key
1c304 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e is an integer an
1c305 64 20 74 68 61 74 20 50 31 20 69 73 20 61 20 74 d that P1 is a t
1c306 61 62 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 able whereas.**
1c307 4e 6f 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 NotFound assumes
1c308 20 6b 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 key is a blob c
1c309 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 onstructed from
1c30a 4d 61 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a MakeRecord and.*
1c30b 2a 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 * P1 is an index
1c30c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1c30d 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e : Found, NotFoun
1c30e 64 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 d, IsUnique.*/.c
1c30f 61 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 ase OP_NotExists
1c310 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1c311 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 mp, in3 */. int
1c312 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 i = pOp->p1;.
1c313 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c314 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1c315 72 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d r;. assert( i>=
1c316 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 0 && i<p->nCurso
1c317 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1c318 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b ->apCsr[i]!=0 );
1c319 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 . if( (pCrsr =
1c31a 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 (pC = p->apCsr[i
1c31b 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 ])->pCursor)!=0
1c31c 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d ){. int res =
1c31d 20 30 3b 0a 20 20 20 20 75 36 34 20 69 4b 65 79 0;. u64 iKey
1c31e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 ;. assert( pI
1c31f 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n3->flags & MEM_
1c320 49 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 Int );. asser
1c321 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d 3e t( p->apCsr[i]->
1c322 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 69 isTable );. i
1c323 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70 Key = intToKey(p
1c324 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 72 In3->u.i);. r
1c325 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1c326 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 MovetoUnpacked(p
1c327 43 72 73 72 2c 20 30 2c 20 69 4b 65 79 2c 20 30 Crsr, 0, iKey, 0
1c328 2c 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 3e ,&res);. pC->
1c329 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 lastRowid = pIn3
1c32a 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 43 2d 3e 72 ->u.i;. pC->r
1c32b 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 owidIsValid = re
1c32c 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70 s==0 ?1:0;. p
1c32d 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a C->nullRow = 0;.
1c32e 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 pC->cacheSta
1c32f 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1c330 45 3b 0a 20 20 20 20 69 66 28 20 72 65 73 21 3d E;. if( res!=
1c331 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 0 ){. pc =
1c332 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
1c333 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 assert( pC->r
1c334 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 owidIsValid==0 )
1c335 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
1c336 69 66 28 20 21 70 43 2d 3e 70 73 65 75 64 6f 54 if( !pC->pseudoT
1c337 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 able ){. /* T
1c338 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e his happens when
1c339 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f an attempt to o
1c33a 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f pen a read curso
1c33b 72 20 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a r on the . **
1c33c 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
1c33d 61 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c able returns SQL
1c33e 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a ITE_EMPTY.. *
1c33f 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 /. assert( pC
1c340 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 ->isTable );.
1c341 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1c342 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 1;. assert( p
1c343 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d C->rowidIsValid=
1c344 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 =0 );. }. brea
1c345 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1c346 20 53 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 Sequence P1 P2
1c347 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 * * *.**.** Find
1c348 20 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 the next availa
1c349 62 6c 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d ble sequence num
1c34a 62 65 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 ber for cursor P
1c34b 31 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 1..** Write the
1c34c 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 sequence number
1c34d 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 into register P2
1c34e 2e 0a 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 ..** The sequenc
1c34f 65 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 e number on the
1c350 63 75 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d cursor is increm
1c351 65 6e 74 65 64 20 61 66 74 65 72 20 74 68 69 73 ented after this
1c352 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e .** instruction.
1c353 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 .*/.case OP_Se
1c354 71 75 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 quence: {
1c355 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1c356 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 elease */. int
1c357 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 i = pOp->p1;. a
1c358 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
1c359 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1c35a 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 assert( p->apCs
1c35b 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 r[i]!=0 );. pOu
1c35c 74 2d 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 t->u.i = p->apCs
1c35d 72 5b 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b r[i]->seqCount++
1c35e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c ;. MemSetTypeFl
1c35f 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 ag(pOut, MEM_Int
1c360 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a );. break;.}...
1c361 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f /* Opcode: NewRo
1c362 77 69 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a wid P1 P2 P3 * *
1c363 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 .**.** Get a new
1c364 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 integer record
1c365 6e 75 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 number (a.k.a "r
1c366 6f 77 69 64 22 29 20 75 73 65 64 20 61 73 20 74 owid") used as t
1c367 68 65 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c he key to a tabl
1c368 65 2e 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 e..** The record
1c369 20 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 number is not p
1c36a 72 65 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 reviously used a
1c36b 73 20 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 s a key in the d
1c36c 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 atabase.** table
1c36d 20 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 that cursor P1
1c36e 70 6f 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 points to. The
1c36f 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 new record numbe
1c370 72 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 r is written.**
1c371 77 72 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 written to regis
1c372 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 ter P2..**.** If
1c373 20 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 P3>0 then P3 is
1c374 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 a register that
1c375 20 68 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 holds the large
1c376 73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a st previously.**
1c377 20 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 generated recor
1c378 64 20 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 d number. No ne
1c379 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 w record numbers
1c37a 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 are allowed to
1c37b 62 65 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 be less.** than
1c37c 74 68 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 this value. Whe
1c37d 6e 20 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 n this value rea
1c37e 63 68 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d ches its maximum
1c37f 2c 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a , a SQLITE_FULL.
1c380 2a 2a 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 ** error is gene
1c381 72 61 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 rated. The P3 r
1c382 65 67 69 73 74 65 72 20 69 73 20 75 70 64 61 74 egister is updat
1c383 65 64 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 ed with the gene
1c384 72 61 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 rated.** record
1c385 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 number. This P3
1c386 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 mechanism is us
1c387 65 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 ed to help imple
1c388 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f ment the.** AUTO
1c389 49 4e 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 INCREMENT featur
1c38a 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 e..*/.case OP_Ne
1c38b 77 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 wRowid: {
1c38c 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1c38d 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 elease */. int
1c38e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 i = pOp->p1;. i
1c38f 36 34 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 64 v = 0;. Vdbe
1c390 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 Cursor *pC;. as
1c391 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1c392 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1c393 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 assert( p->apCsr
1c394 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 [i]!=0 );. if(
1c395 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 (pC = p->apCsr[i
1c396 5d 29 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 ])->pCursor==0 )
1c397 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 {. /* The zer
1c398 6f 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e o initialization
1c399 20 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 above is all th
1c39a 61 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a at is needed */.
1c39b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1c39c 54 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f The next rowid o
1c39d 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 r record number
1c39e 28 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 (different terms
1c39f 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 for the same.
1c3a0 20 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f ** thing) is o
1c3a1 62 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f btained in a two
1c3a2 2d 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e -step algorithm.
1c3a3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 . **. ** F
1c3a4 69 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 irst we attempt
1c3a5 74 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 to find the larg
1c3a6 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 est existing row
1c3a7 69 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 id and add one.
1c3a8 20 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 ** to that.
1c3a9 42 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 But if the large
1c3aa 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 st existing rowi
1c3ab 64 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 d is already the
1c3ac 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 maximum. **
1c3ad 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 positive integer
1c3ae 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c , we have to fal
1c3af 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 l through to the
1c3b0 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 second. ** p
1c3b1 72 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 robabilistic alg
1c3b2 6f 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 orithm. **.
1c3b3 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ** The second
1c3b4 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 algorithm is to
1c3b5 73 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 select a rowid a
1c3b6 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 t random and see
1c3b7 20 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c if. ** it al
1c3b8 72 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 ready exists in
1c3b9 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 the table. If i
1c3ba 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 t does not exist
1c3bb 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a , we have. **
1c3bc 20 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 succeeded. If
1c3bd 74 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 the random rowid
1c3be 20 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 does exist, we
1c3bf 73 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 select a new one
1c3c0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 . ** and try
1c3c1 61 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 again, up to 100
1c3c2 30 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 0 times.. **.
1c3c3 20 20 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 ** For a tab
1c3c4 6c 65 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 le with less tha
1c3c5 6e 20 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 n 2 billion entr
1c3c6 69 65 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 ies, the probabi
1c3c7 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e lity. ** of n
1c3c8 6f 74 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 ot finding a unu
1c3c9 73 65 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f sed rowid is abo
1c3ca 75 74 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 ut 1.0e-300. Th
1c3cb 69 73 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 is is a . **
1c3cc 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 non-zero probabi
1c3cd 6c 69 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 lity, but it is
1c3ce 73 74 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c still vanishingl
1c3cf 79 20 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 y small and shou
1c3d0 6c 64 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 ld. ** never
1c3d1 63 61 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e cause a problem.
1c3d2 20 20 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 You are much,
1c3d3 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 much more likely
1c3d4 20 74 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a to have a. *
1c3d5 2a 20 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 * hardware failu
1c3d6 72 65 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 re than for this
1c3d7 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 algorithm to fa
1c3d8 69 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a il.. **. *
1c3d9 2a 20 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 * The analysis i
1c3da 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 n the previous p
1c3db 61 72 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 aragraph assumes
1c3dc 20 74 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 that you have a
1c3dd 20 67 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 good. ** sou
1c3de 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 rce of random nu
1c3df 6d 62 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 mbers. Is a lib
1c3e0 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 rary function li
1c3e1 6b 65 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 ke lrand48().
1c3e2 20 2a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f ** good enough?
1c3e3 20 20 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e Maybe. Maybe n
1c3e4 6f 74 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f ot. It's hard to
1c3e5 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 know whether th
1c3e6 65 72 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 ere. ** might
1c3e7 20 62 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 be subtle bugs
1c3e8 69 73 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e is some implemen
1c3e9 74 61 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 tations of lrand
1c3ea 34 38 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 48() that. **
1c3eb 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f could cause pro
1c3ec 62 6c 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 blems. To avoid
1c3ed 75 6e 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c uncertainty, SQL
1c3ee 69 74 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e ite uses its own
1c3ef 20 0a 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 . ** random
1c3f0 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
1c3f1 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 based on the RC
1c3f2 34 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 4 algorithm..
1c3f3 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 **. ** To pr
1c3f4 6f 6d 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f omote locality o
1c3f5 66 20 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 f reference for
1c3f6 72 65 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 repetitive inser
1c3f7 74 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 ts, the. ** f
1c3f8 69 72 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 irst few attempt
1c3f9 73 20 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 s at choosing a
1c3fa 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 random rowid pic
1c3fb 6b 20 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 k values just a
1c3fc 6c 69 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 little. ** la
1c3fd 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 rger than the pr
1c3fe 65 76 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 evious rowid. T
1c3ff 68 69 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f his has been sho
1c400 77 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c wn experimentall
1c401 79 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 y. ** to doub
1c402 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 le the speed of
1c403 74 68 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 the COPY operati
1c404 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 on.. */. i
1c405 6e 74 20 72 65 73 3d 30 2c 20 72 78 3d 53 51 4c nt res=0, rx=SQL
1c406 49 54 45 5f 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 ITE_OK, cnt;.
1c407 20 69 36 34 20 78 3b 0a 20 20 20 20 63 6e 74 20 i64 x;. cnt
1c408 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 73 71 = 0;. if( (sq
1c409 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 lite3BtreeFlags(
1c40a 70 43 2d 3e 70 43 75 72 73 6f 72 29 26 28 42 54 pC->pCursor)&(BT
1c40b 52 45 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 REE_INTKEY|BTREE
1c40c 5f 5a 45 52 4f 44 41 54 41 29 29 20 21 3d 0a 20 _ZERODATA)) !=.
1c40d 20 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 49 BTREE_I
1c40e 4e 54 4b 45 59 20 29 7b 0a 20 20 20 20 20 20 72 NTKEY ){. r
1c40f 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
1c410 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 PT_BKPT;. g
1c411 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1c412 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 _error;. }.
1c413 20 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 assert( (sqlit
1c414 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d e3BtreeFlags(pC-
1c415 3e 70 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 >pCursor) & BTRE
1c416 45 5f 49 4e 54 4b 45 59 29 21 3d 30 20 29 3b 0a E_INTKEY)!=0 );.
1c417 20 20 20 20 61 73 73 65 72 74 28 20 28 73 71 6c assert( (sql
1c418 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 ite3BtreeFlags(p
1c419 43 2d 3e 70 43 75 72 73 6f 72 29 20 26 20 42 54 C->pCursor) & BT
1c41a 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 REE_ZERODATA)==0
1c41b 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 );..#ifdef SQLI
1c41c 54 45 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 TE_32BIT_ROWID.#
1c41d 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f define MAX_RO
1c41e 57 49 44 20 30 78 37 66 66 66 66 66 66 66 0a 23 WID 0x7fffffff.#
1c41f 65 6c 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 else. /* Some
1c420 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c compilers compl
1c421 61 69 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 ain about consta
1c422 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 nts of the form
1c423 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 0x7fffffffffffff
1c424 66 66 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 ff.. ** Other
1c425 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 s complain about
1c426 20 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 0x7ffffffffffff
1c427 66 66 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c ffffLL. The fol
1c428 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 lowing macro see
1c429 6d 73 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f ms. ** to pro
1c42a 76 69 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e vide the constan
1c42b 74 20 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 t while making a
1c42c 6c 6c 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 ll compilers hap
1c42d 70 79 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 py.. */.# d
1c42e 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 efine MAX_ROWID
1c42f 20 28 69 36 34 29 28 20 28 28 28 75 36 34 29 30 (i64)( (((u64)0
1c430 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 20 x7fffffff)<<32)
1c431 7c 20 28 75 36 34 29 30 78 66 66 66 66 66 66 66 | (u64)0xfffffff
1c432 66 20 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 f ).#endif..
1c433 69 66 28 20 21 70 43 2d 3e 75 73 65 52 61 6e 64 if( !pC->useRand
1c434 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 omRowid ){.
1c435 20 76 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 v = sqlite3Btre
1c436 65 47 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 eGetCachedRowid(
1c437 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 pC->pCursor);.
1c438 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a if( v==0 ){.
1c439 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1c43a 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 ite3BtreeLast(pC
1c43b 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 ->pCursor, &res)
1c43c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
1c43d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1c43e 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 goto ab
1c43f 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1c440 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1c441 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 if( res ){.
1c442 20 20 20 20 20 20 20 20 20 76 20 3d 20 31 3b 0a v = 1;.
1c443 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1c444 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1c445 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d BtreeKeySize(pC-
1c446 3e 70 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 >pCursor, &v);.
1c447 20 20 20 20 20 20 20 20 20 76 20 3d 20 6b 65 79 v = key
1c448 54 6f 49 6e 74 28 76 29 3b 0a 20 20 20 20 20 20 ToInt(v);.
1c449 20 20 20 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52 if( v==MAX_R
1c44a 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 OWID ){.
1c44b 20 20 20 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f pC->useRando
1c44c 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 mRowid = 1;.
1c44d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1c44e 20 20 20 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20 v++;.
1c44f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1c450 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 }. }..#if
1c451 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1c452 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 _AUTOINCREMENT.
1c453 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 if( pOp->p3
1c454 20 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 ){. Mem
1c455 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 61 *pMem;. a
1c456 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1c457 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e && pOp->p3<=p->
1c458 6e 4d 65 6d 20 29 3b 20 2f 2a 20 50 33 20 69 73 nMem ); /* P3 is
1c459 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 a valid memory
1c45a 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 cell */.
1c45b 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pMem = &p->aMem[
1c45c 70 4f 70 2d 3e 70 33 5d 3b 0a 09 52 45 47 49 53 pOp->p3];..REGIS
1c45d 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1c45e 33 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 3, pMem);.
1c45f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1c460 49 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 Integerify(pMem)
1c461 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1c462 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
1c463 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 MEM_Int)!=0 );
1c464 20 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 /* mem(P3) hold
1c465 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a s an integer */.
1c466 20 20 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d if( pMem
1c467 2d 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 ->u.i==MAX_ROWID
1c468 20 7c 7c 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f || pC->useRando
1c469 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 mRowid ){.
1c46a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1c46b 46 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 FULL;.
1c46c 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1c46d 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1c46e 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 }. if( v
1c46f 3c 70 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a <pMem->u.i+1 ){.
1c470 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 70 4d v = pM
1c471 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 em->u.i + 1;.
1c472 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
1c473 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 Mem->u.i = v;.
1c474 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
1c475 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1c476 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 70 SetCachedRowid(p
1c477 43 2d 3e 70 43 75 72 73 6f 72 2c 20 76 3c 4d 41 C->pCursor, v<MA
1c478 58 5f 52 4f 57 49 44 20 3f 20 76 2b 31 20 3a 20 X_ROWID ? v+1 :
1c479 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0);. }. if
1c47a 28 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 ( pC->useRandomR
1c47b 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73 owid ){. as
1c47c 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 sert( pOp->p3==0
1c47d 20 29 3b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 ); /* SQLITE_F
1c47e 55 4c 4c 20 6d 75 73 74 20 68 61 76 65 20 6f 63 ULL must have oc
1c47f 63 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 curred prior to
1c480 74 68 69 73 20 2a 2f 0a 20 20 20 20 20 20 76 20 this */. v
1c481 3d 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f = db->priorNewRo
1c482 77 69 64 3b 0a 20 20 20 20 20 20 63 6e 74 20 3d wid;. cnt =
1c483 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20 0;. do{.
1c484 20 20 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 if( cnt==0
1c485 20 26 26 20 28 76 26 30 78 66 66 66 66 66 66 29 && (v&0xffffff)
1c486 3d 3d 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==v ){.
1c487 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 v++;. }e
1c488 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 lse{. s
1c489 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
1c48a 73 28 73 69 7a 65 6f 66 28 76 29 2c 20 26 76 29 s(sizeof(v), &v)
1c48b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1c48c 63 6e 74 3c 35 20 29 20 76 20 26 3d 20 30 78 66 cnt<5 ) v &= 0xf
1c48d 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 20 7d fffff;. }
1c48e 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d . if( v==
1c48f 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
1c490 20 20 20 20 20 20 78 20 3d 20 69 6e 74 54 6f 4b x = intToK
1c491 65 79 28 76 29 3b 0a 20 20 20 20 20 20 20 20 72 ey(v);. r
1c492 78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 x = sqlite3Btree
1c493 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 MovetoUnpacked(p
1c494 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 C->pCursor, 0, (
1c495 75 36 34 29 78 2c 20 30 2c 20 26 72 65 73 29 3b u64)x, 0, &res);
1c496 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a . cnt++;.
1c497 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 63 6e }while( cn
1c498 74 3c 31 30 30 20 26 26 20 72 78 3d 3d 53 51 4c t<100 && rx==SQL
1c499 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 ITE_OK && res==0
1c49a 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 72 );. db->pr
1c49b 69 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 76 3b iorNewRowid = v;
1c49c 0a 20 20 20 20 20 20 69 66 28 20 72 78 3d 3d 53 . if( rx==S
1c49d 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d QLITE_OK && res=
1c49e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 =0 ){. rc
1c49f 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a = SQLITE_FULL;.
1c4a0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f goto abo
1c4a1 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1c4a2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1c4a3 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 pC->rowidIsVa
1c4a4 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d lid = 0;. pC-
1c4a5 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1c4a6 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 = 0;. pC->cac
1c4a7 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1c4a8 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d 65 _STALE;. }. Me
1c4a9 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1c4aa 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70 t, MEM_Int);. p
1c4ab 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 Out->u.i = v;.
1c4ac 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c4ad 6f 64 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 ode: Insert P1 P
1c4ae 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
1c4af 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 Write an entry
1c4b0 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f into the table o
1c4b1 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 f cursor P1. A
1c4b2 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 new entry is.**
1c4b3 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f created if it do
1c4b4 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 esn't already ex
1c4b5 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 ist or the data
1c4b6 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a for an existing.
1c4b7 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 ** entry is over
1c4b8 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 written. The da
1c4b9 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 ta is the value
1c4ba 73 74 6f 72 65 64 20 72 65 67 69 73 74 65 72 0a stored register.
1c4bb 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20 54 68 ** number P2. Th
1c4bc 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65 64 20 e key is stored
1c4bd 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 in register P3.
1c4be 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a 20 The key must.**
1c4bf 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a be an integer..*
1c4c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c *.** If the OPFL
1c4c1 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 AG_NCHANGE flag
1c4c2 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 of P5 is set, th
1c4c3 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 en the row chang
1c4c4 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e e count is.** in
1c4c5 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 cremented (other
1c4c6 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74 wise not). If t
1c4c7 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f he OPFLAG_LASTRO
1c4c8 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69 WID flag of P5 i
1c4c9 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 s set,.** then r
1c4ca 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 66 owid is stored f
1c4cb 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 or subsequent re
1c4cc 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73 turn by the.** s
1c4cd 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
1c4ce 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74 rt_rowid() funct
1c4cf 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69 ion (otherwise i
1c4d0 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29 t is unmodified)
1c4d1 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 ..**.** Paramete
1c4d2 72 20 50 34 20 6d 61 79 20 70 6f 69 6e 74 20 74 r P4 may point t
1c4d3 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 o a string conta
1c4d4 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2d ining the table-
1c4d5 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20 name, or.** may
1c4d6 62 65 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 be NULL. If it i
1c4d7 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
1c4d8 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b the update-hook
1c4d9 20 0a 2a 2a 20 28 73 71 6c 69 74 65 33 2e 78 55 .** (sqlite3.xU
1c4da 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 20 69 pdateCallback) i
1c4db 73 20 69 6e 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77 s invoked follow
1c4dc 69 6e 67 20 61 20 73 75 63 63 65 73 73 66 75 6c ing a successful
1c4dd 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 insert..**.** (
1c4de 57 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 WARNING/TODO: If
1c4df 20 50 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d P1 is a pseudo-
1c4e0 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73 cursor and P2 is
1c4e1 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 dynamically.**
1c4e2 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20 allocated, then
1c4e3 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20 ownership of P2
1c4e4 69 73 20 74 72 61 6e 73 66 65 72 72 65 64 20 74 is transferred t
1c4e5 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72 o the pseudo-cur
1c4e6 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73 sor.** and regis
1c4e7 74 65 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65 ter P2 becomes e
1c4e8 70 68 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68 phemeral. If th
1c4e9 65 20 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e e cursor is chan
1c4ea 67 65 64 2c 20 74 68 65 0a 2a 2a 20 76 61 6c 75 ged, the.** valu
1c4eb 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 e of register P2
1c4ec 20 77 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67 will then chang
1c4ed 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 e. Make sure th
1c4ee 69 73 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 is does not.** c
1c4ef 61 75 73 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d ause any problem
1c4f0 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 s.).**.** This i
1c4f1 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 nstruction only
1c4f2 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e works on tables.
1c4f3 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 The equivalent
1c4f4 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 instruction.**
1c4f5 66 6f 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f for indices is O
1c4f6 50 5f 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a P_IdxInsert..*/.
1c4f7 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 case OP_Insert:
1c4f8 7b 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 20 3d {. Mem *pData =
1c4f9 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1c4fa 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 20 2];. Mem *pKey
1c4fb 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1c4fc 70 33 5d 3b 0a 0a 20 20 69 36 34 20 69 4b 65 79 p3];.. i64 iKey
1c4fd 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 ; /* The integ
1c4fe 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 er ROWID or key
1c4ff 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 for the record t
1c500 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f o be inserted */
1c501 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
1c502 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p1;. VdbeCursor
1c503 20 2a 70 43 3b 0a 20 20 61 73 73 65 72 74 28 20 *pC;. assert(
1c504 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1c505 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 rsor );. pC = p
1c506 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 ->apCsr[i];. as
1c507 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 sert( pC!=0 );.
1c508 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 assert( pC->pCu
1c509 72 73 6f 72 21 3d 30 20 7c 7c 20 70 43 2d 3e 70 rsor!=0 || pC->p
1c50a 73 65 75 64 6f 54 61 62 6c 65 20 29 3b 0a 20 20 seudoTable );.
1c50b 61 73 73 65 72 74 28 20 70 4b 65 79 2d 3e 66 6c assert( pKey->fl
1c50c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b ags & MEM_Int );
1c50d 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 . assert( pC->i
1c50e 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49 sTable );. REGI
1c50f 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1c510 70 32 2c 20 70 44 61 74 61 29 3b 0a 20 20 52 45 p2, pData);. RE
1c511 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1c512 2d 3e 70 33 2c 20 70 4b 65 79 29 3b 0a 0a 20 20 ->p3, pKey);..
1c513 69 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 iKey = intToKey(
1c514 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 pKey->u.i);. if
1c515 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c ( pOp->p5 & OPFL
1c516 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e AG_NCHANGE ) p->
1c517 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 nChange++;. if(
1c518 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 pOp->p5 & OPFLA
1c519 47 5f 4c 41 53 54 52 4f 57 49 44 20 29 20 64 62 G_LASTROWID ) db
1c51a 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 4b ->lastRowid = pK
1c51b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 70 ey->u.i;. if( p
1c51c 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 Data->flags & ME
1c51d 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 44 M_Null ){. pD
1c51e 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 ata->z = 0;.
1c51f 70 44 61 74 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 pData->n = 0;.
1c520 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
1c521 74 28 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 t( pData->flags
1c522 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f & (MEM_Blob|MEM_
1c523 53 74 72 29 20 29 3b 0a 20 20 7d 0a 20 20 69 66 Str) );. }. if
1c524 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c ( pC->pseudoTabl
1c525 65 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 43 e ){. if( !pC
1c526 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 ->ephemPseudoTab
1c527 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 le ){. sqli
1c528 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 te3DbFree(db, pC
1c529 2d 3e 70 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a ->pData);. }.
1c52a 20 20 20 20 70 43 2d 3e 69 4b 65 79 20 3d 20 69 pC->iKey = i
1c52b 4b 65 79 3b 0a 20 20 20 20 70 43 2d 3e 6e 44 61 Key;. pC->nDa
1c52c 74 61 20 3d 20 70 44 61 74 61 2d 3e 6e 3b 0a 20 ta = pData->n;.
1c52d 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e 7a 3d if( pData->z=
1c52e 3d 70 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 =pData->zMalloc
1c52f 7c 7c 20 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 || pC->ephemPseu
1c530 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 doTable ){.
1c531 20 70 43 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 pC->pData = pDa
1c532 74 61 2d 3e 7a 3b 0a 20 20 20 20 20 20 69 66 28 ta->z;. if(
1c533 20 21 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 !pC->ephemPseud
1c534 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 oTable ){.
1c535 20 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 pData->flags &
1c536 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 = ~MEM_Dyn;.
1c537 20 20 20 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 pData->flags
1c538 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 |= MEM_Ephem;.
1c539 20 20 20 20 20 20 20 70 44 61 74 61 2d 3e 7a 4d pData->zM
1c53a 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 alloc = 0;.
1c53b 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1c53c 20 20 20 20 70 43 2d 3e 70 44 61 74 61 20 3d 20 pC->pData =
1c53d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 70 sqlite3Malloc( p
1c53e 43 2d 3e 6e 44 61 74 61 2b 32 20 29 3b 0a 20 20 C->nData+2 );.
1c53f 20 20 20 20 69 66 28 20 21 70 43 2d 3e 70 44 61 if( !pC->pDa
1c540 74 61 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d ta ) goto no_mem
1c541 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 ;. memcpy(p
1c542 43 2d 3e 70 44 61 74 61 2c 20 70 44 61 74 61 2d C->pData, pData-
1c543 3e 7a 2c 20 70 43 2d 3e 6e 44 61 74 61 29 3b 0a >z, pC->nData);.
1c544 20 20 20 20 20 20 70 43 2d 3e 70 44 61 74 61 5b pC->pData[
1c545 70 43 2d 3e 6e 44 61 74 61 5d 20 3d 20 30 3b 0a pC->nData] = 0;.
1c546 20 20 20 20 20 20 70 43 2d 3e 70 44 61 74 61 5b pC->pData[
1c547 70 43 2d 3e 6e 44 61 74 61 2b 31 5d 20 3d 20 30 pC->nData+1] = 0
1c548 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e ;. }. pC->
1c549 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 7d nullRow = 0;. }
1c54a 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 5a else{. int nZ
1c54b 65 72 6f 3b 0a 20 20 20 20 69 66 28 20 70 44 61 ero;. if( pDa
1c54c 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ta->flags & MEM_
1c54d 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 6e 5a Zero ){. nZ
1c54e 65 72 6f 20 3d 20 70 44 61 74 61 2d 3e 75 2e 6e ero = pData->u.n
1c54f 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Zero;. }else{
1c550 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20 30 . nZero = 0
1c551 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
1c552 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 te3BtreeSetCache
1c553 64 52 6f 77 69 64 28 70 43 2d 3e 70 43 75 72 73 dRowid(pC->pCurs
1c554 6f 72 2c 20 30 29 3b 0a 20 20 20 20 72 63 20 3d or, 0);. rc =
1c555 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 sqlite3BtreeIns
1c556 65 72 74 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c ert(pC->pCursor,
1c557 20 30 2c 20 69 4b 65 79 2c 0a 20 20 20 20 20 20 0, iKey,.
1c558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c559 20 20 20 20 20 20 70 44 61 74 61 2d 3e 7a 2c 20 pData->z,
1c55a 70 44 61 74 61 2d 3e 6e 2c 20 6e 5a 65 72 6f 2c pData->n, nZero,
1c55b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1c55c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 pOp
1c55d 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 41 50 ->p5 & OPFLAG_AP
1c55e 50 45 4e 44 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 PEND);. }. .
1c55f 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1c560 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 64 65 66 65 = 0;. pC->defe
1c561 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a rredMoveto = 0;.
1c562 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 pC->cacheStatu
1c563 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1c564 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 .. /* Invoke th
1c565 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 e update-hook if
1c566 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 required. */.
1c567 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1c568 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 K && db->xUpdate
1c569 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d Callback && pOp-
1c56a 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e >p4.z ){. con
1c56b 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 st char *zDb = d
1c56c 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e b->aDb[pC->iDb].
1c56d 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 zName;. const
1c56e 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f char *zTbl = pO
1c56f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 69 6e 74 p->p4.z;. int
1c570 20 6f 70 20 3d 20 28 28 70 4f 70 2d 3e 70 35 20 op = ((pOp->p5
1c571 26 20 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 & OPFLAG_ISUPDAT
1c572 45 29 20 3f 20 53 51 4c 49 54 45 5f 55 50 44 41 E) ? SQLITE_UPDA
1c573 54 45 20 3a 20 53 51 4c 49 54 45 5f 49 4e 53 45 TE : SQLITE_INSE
1c574 52 54 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 RT);. assert(
1c575 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a pC->isTable );.
1c576 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 db->xUpdateC
1c577 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 allback(db->pUpd
1c578 61 74 65 41 72 67 2c 20 6f 70 2c 20 7a 44 62 2c ateArg, op, zDb,
1c579 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 zTbl, iKey);.
1c57a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 44 assert( pC->iD
1c57b 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72 b>=0 );. }. br
1c57c 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1c57d 65 3a 20 44 65 6c 65 74 65 20 50 31 20 50 32 20 e: Delete P1 P2
1c57e 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c * P4 *.**.** Del
1c57f 65 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 61 ete the record a
1c580 74 20 77 68 69 63 68 20 74 68 65 20 50 31 20 63 t which the P1 c
1c581 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 ursor is current
1c582 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a ly pointing..**.
1c583 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 77 69 ** The cursor wi
1c584 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f 69 6e 74 ll be left point
1c585 69 6e 67 20 61 74 20 65 69 74 68 65 72 20 74 68 ing at either th
1c586 65 20 6e 65 78 74 20 6f 72 20 74 68 65 20 70 72 e next or the pr
1c587 65 76 69 6f 75 73 0a 2a 2a 20 72 65 63 6f 72 64 evious.** record
1c588 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49 in the table. I
1c589 66 20 69 74 20 69 73 20 6c 65 66 74 20 70 6f 69 f it is left poi
1c58a 6e 74 69 6e 67 20 61 74 20 74 68 65 20 6e 65 78 nting at the nex
1c58b 74 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 0a 2a t record, then.*
1c58c 2a 20 74 68 65 20 6e 65 78 74 20 4e 65 78 74 20 * the next Next
1c58d 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c instruction will
1c58e 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 65 be a no-op. He
1c58f 6e 63 65 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 nce it is OK to
1c590 64 65 6c 65 74 65 0a 2a 2a 20 61 20 72 65 63 6f delete.** a reco
1c591 72 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 rd from within a
1c592 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a n Next loop..**.
1c593 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 ** If the OPFLAG
1c594 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 _NCHANGE flag of
1c595 20 50 32 20 69 73 20 73 65 74 2c 20 74 68 65 6e P2 is set, then
1c596 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 the row change
1c597 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 count is.** incr
1c598 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 77 69 emented (otherwi
1c599 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50 se not)..**.** P
1c59a 31 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 70 73 1 must not be ps
1c59b 65 75 64 6f 2d 74 61 62 6c 65 2e 20 20 49 74 20 eudo-table. It
1c59c 68 61 73 20 74 6f 20 62 65 20 61 20 72 65 61 6c has to be a real
1c59d 20 74 61 62 6c 65 20 77 69 74 68 0a 2a 2a 20 6d table with.** m
1c59e 75 6c 74 69 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a ultiple rows..**
1c59f 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 .** If P4 is not
1c5a0 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 NULL, then it i
1c5a1 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
1c5a2 65 20 74 61 62 6c 65 20 74 68 61 74 20 50 31 20 e table that P1
1c5a3 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 is.** pointing t
1c5a4 6f 2e 20 20 54 68 65 20 75 70 64 61 74 65 20 68 o. The update h
1c5a5 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f ook will be invo
1c5a6 6b 65 64 2c 20 69 66 20 69 74 20 65 78 69 73 74 ked, if it exist
1c5a7 73 2e 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e s..** If P4 is n
1c5a8 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 ot NULL then the
1c5a9 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 P1 cursor must
1c5aa 68 61 76 65 20 62 65 65 6e 20 70 6f 73 69 74 69 have been positi
1c5ab 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4f 50 oned.** using OP
1c5ac 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 69 6f 72 20 _NotFound prior
1c5ad 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 to invoking this
1c5ae 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 opcode..*/.case
1c5af 20 4f 50 5f 44 65 6c 65 74 65 3a 20 7b 0a 20 20 OP_Delete: {.
1c5b0 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b int i = pOp->p1;
1c5b1 0a 20 20 69 36 34 20 69 4b 65 79 20 3d 20 30 3b . i64 iKey = 0;
1c5b2 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1c5b3 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e C;.. assert( i>
1c5b4 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 =0 && i<p->nCurs
1c5b5 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e or );. pC = p->
1c5b6 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 apCsr[i];. asse
1c5b7 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 rt( pC!=0 );. a
1c5b8 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73 ssert( pC->pCurs
1c5b9 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c or!=0 ); /* Onl
1c5ba 79 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c y valid for real
1c5bb 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 tables, no pseu
1c5bc 64 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f dotables */.. /
1c5bd 2a 20 49 66 20 74 68 65 20 75 70 64 61 74 65 2d * If the update-
1c5be 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 hook will be inv
1c5bf 6f 6b 65 64 2c 20 73 65 74 20 69 4b 65 79 20 74 oked, set iKey t
1c5c0 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 o the rowid of t
1c5c1 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e he. ** row bein
1c5c2 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a g deleted.. */.
1c5c3 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74 if( db->xUpdat
1c5c4 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 eCallback && pOp
1c5c5 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 ->p4.z ){. as
1c5c6 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c sert( pC->isTabl
1c5c7 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 e );. assert(
1c5c8 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 pC->rowidIsVali
1c5c9 64 20 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 d ); /* lastRow
1c5ca 69 64 20 73 65 74 20 62 79 20 70 72 65 76 69 6f id set by previo
1c5cb 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a us OP_NotFound *
1c5cc 2f 0a 20 20 20 20 69 4b 65 79 20 3d 20 70 43 2d /. iKey = pC-
1c5cd 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a >lastRowid;. }.
1c5ce 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 . rc = sqlite3V
1c5cf 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 dbeCursorMoveto(
1c5d0 70 43 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 pC);. if( rc )
1c5d1 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1c5d2 6f 5f 65 72 72 6f 72 3b 0a 20 20 73 71 6c 69 74 o_error;. sqlit
1c5d3 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 e3BtreeSetCached
1c5d4 52 6f 77 69 64 28 70 43 2d 3e 70 43 75 72 73 6f Rowid(pC->pCurso
1c5d5 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 r, 0);. rc = sq
1c5d6 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 lite3BtreeDelete
1c5d7 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 (pC->pCursor);.
1c5d8 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 pC->cacheStatus
1c5d9 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1c5da 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 . /* Invoke the
1c5db 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 update-hook if
1c5dc 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 required. */. i
1c5dd 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1c5de 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 && db->xUpdateC
1c5df 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e allback && pOp->
1c5e0 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 p4.z ){. cons
1c5e1 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 t char *zDb = db
1c5e2 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a ->aDb[pC->iDb].z
1c5e3 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 Name;. const
1c5e4 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 char *zTbl = pOp
1c5e5 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e ->p4.z;. db->
1c5e6 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 xUpdateCallback(
1c5e7 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 db->pUpdateArg,
1c5e8 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a SQLITE_DELETE, z
1c5e9 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b Db, zTbl, iKey);
1c5ea 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1c5eb 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 >iDb>=0 );. }.
1c5ec 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f if( pOp->p2 & O
1c5ed 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 PFLAG_NCHANGE )
1c5ee 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 p->nChange++;.
1c5ef 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c5f0 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20 ode: ResetCount
1c5f1 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 P1 * *.**.** Thi
1c5f2 73 20 6f 70 63 6f 64 65 20 72 65 73 65 74 73 20 s opcode resets
1c5f3 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c the VMs internal
1c5f4 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1c5f5 74 6f 20 30 2e 20 49 66 20 50 31 20 69 73 20 74 to 0. If P1 is t
1c5f6 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 rue,.** then the
1c5f7 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68 value of the ch
1c5f8 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 ange counter is
1c5f9 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64 61 copied to the da
1c5fa 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a tabase handle.**
1c5fb 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1c5fc 28 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 (returned by sub
1c5fd 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f sequent calls to
1c5fe 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
1c5ff 28 29 29 0a 2a 2a 20 62 65 66 6f 72 65 20 69 74 ()).** before it
1c600 20 69 73 20 72 65 73 65 74 2e 20 54 68 69 73 20 is reset. This
1c601 69 73 20 75 73 65 64 20 62 79 20 74 72 69 67 67 is used by trigg
1c602 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a er programs..*/.
1c603 63 61 73 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 case OP_ResetCou
1c604 6e 74 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d nt: {. if( pOp-
1c605 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >p1 ){. sqlit
1c606 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 e3VdbeSetChanges
1c607 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 (db, p->nChange)
1c608 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e ;. }. p->nChan
1c609 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b ge = 0;. break;
1c60a 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .}../* Opcode: R
1c60b 6f 77 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a owData P1 P2 * *
1c60c 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 *.**.** Write i
1c60d 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 nto register P2
1c60e 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 the complete row
1c60f 20 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 data for cursor
1c610 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 P1..** There is
1c611 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 no interpretati
1c612 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 on of the data.
1c613 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 .** It is just
1c614 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 copied onto the
1c615 50 32 20 72 65 67 69 73 74 65 72 20 65 78 61 63 P2 register exac
1c616 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 tly as .** it is
1c617 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 found in the da
1c618 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1c619 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72 ** If the P1 cur
1c61a 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e sor must be poin
1c61b 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 ting to a valid
1c61c 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 row (not a NULL
1c61d 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 row).** of a rea
1c61e 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 l table, not a p
1c61f 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a seudo-table..*/.
1c620 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 /* Opcode: RowKe
1c621 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a y P1 P2 * * *.**
1c622 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 .** Write into r
1c623 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63 egister P2 the c
1c624 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 omplete row key
1c625 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a for cursor P1..*
1c626 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e * There is no in
1c627 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 terpretation of
1c628 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 the data. .** T
1c629 68 65 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 he key is copied
1c62a 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 onto the P3 reg
1c62b 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 ister exactly as
1c62c 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 .** it is found
1c62d 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1c62e 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
1c62f 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 the P1 cursor mu
1c630 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 st be pointing t
1c631 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e o a valid row (n
1c632 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a ot a NULL row).*
1c633 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c * of a real tabl
1c634 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d e, not a pseudo-
1c635 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f table..*/.case O
1c636 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f P_RowKey:.case O
1c637 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 20 20 69 P_RowData: {. i
1c638 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a nt i = pOp->p1;.
1c639 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c63a 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 ;. BtCursor *pC
1c63b 72 73 72 3b 0a 20 20 75 33 32 20 6e 3b 0a 0a 20 rsr;. u32 n;..
1c63c 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pOut = &p->aMem
1c63d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a [pOp->p2];.. /*
1c63e 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b 65 Note that RowKe
1c63f 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 72 y and RowData ar
1c640 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c 79 e really exactly
1c641 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 75 the same instru
1c642 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 ction */. asser
1c643 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e t( i>=0 && i<p->
1c644 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 nCursor );. pC
1c645 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 = p->apCsr[i];.
1c646 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 assert( pC->isT
1c647 61 62 6c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 able || pOp->opc
1c648 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 ode==OP_RowKey )
1c649 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e ;. assert( pC->
1c64a 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e isIndex || pOp->
1c64b 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 opcode==OP_RowDa
1c64c 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ta );. assert(
1c64d 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 pC!=0 );. asser
1c64e 74 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d t( pC->nullRow==
1c64f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1c650 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 3d 3d C->pseudoTable==
1c651 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1c652 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b C->pCursor!=0 );
1c653 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 . pCrsr = pC->p
1c654 43 75 72 73 6f 72 3b 0a 20 20 72 63 20 3d 20 73 Cursor;. rc = s
1c655 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
1c656 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 69 66 Moveto(pC);. if
1c657 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 ( rc ) goto abor
1c658 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1c659 20 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 if( pC->isInde
1c65a 78 20 29 7b 0a 20 20 20 20 69 36 34 20 6e 36 34 x ){. i64 n64
1c65b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 ;. assert( !p
1c65c 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1c65d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 sqlite3BtreeKe
1c65e 79 53 69 7a 65 28 70 43 72 73 72 2c 20 26 6e 36 ySize(pCrsr, &n6
1c65f 34 29 3b 0a 20 20 20 20 69 66 28 20 6e 36 34 3e 4);. if( n64>
1c660 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1c661 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
1c662 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f ){. goto to
1c663 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 o_big;. }.
1c664 20 6e 20 3d 20 28 69 6e 74 29 6e 36 34 3b 0a 20 n = (int)n64;.
1c665 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
1c666 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 te3BtreeDataSize
1c667 28 70 43 72 73 72 2c 20 26 6e 29 3b 0a 20 20 20 (pCrsr, &n);.
1c668 20 69 66 28 20 28 69 6e 74 29 6e 3e 64 62 2d 3e if( (int)n>db->
1c669 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1c66a 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
1c66b 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 goto too_bi
1c66c 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 g;. }. }. i
1c66d 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
1c66e 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e 2c 20 30 mGrow(pOut, n, 0
1c66f 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f ) ){. goto no
1c670 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 _mem;. }. pOut
1c671 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 4d 65 6d 53 65 ->n = n;. MemSe
1c672 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1c673 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 MEM_Blob);. if(
1c674 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a pC->isIndex ){.
1c675 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1c676 42 74 72 65 65 4b 65 79 28 70 43 72 73 72 2c 20 BtreeKey(pCrsr,
1c677 30 2c 20 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 0, n, pOut->z);.
1c678 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
1c679 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 = sqlite3BtreeDa
1c67a 74 61 28 70 43 72 73 72 2c 20 30 2c 20 6e 2c 20 ta(pCrsr, 0, n,
1c67b 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20 20 pOut->z);. }.
1c67c 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pOut->enc = SQLI
1c67d 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 TE_UTF8; /* In
1c67e 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73 case the blob is
1c67f 20 65 76 65 72 20 63 61 73 74 20 74 6f 20 74 65 ever cast to te
1c680 78 74 20 2a 2f 0a 20 20 55 50 44 41 54 45 5f 4d xt */. UPDATE_M
1c681 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 AX_BLOBSIZE(pOut
1c682 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1c683 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 69 64 20 * Opcode: Rowid
1c684 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1c685 2a 20 53 74 6f 72 65 20 69 6e 20 72 65 67 69 73 * Store in regis
1c686 74 65 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65 ter P2 an intege
1c687 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6b r which is the k
1c688 65 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 ey of the table
1c689 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 50 31 entry that.** P1
1c68a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
1c68b 69 6e 74 20 74 6f 2e 0a 2a 2f 0a 63 61 73 65 20 int to..*/.case
1c68c 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 OP_Rowid: {
1c68d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f /* o
1c68e 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1c68f 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d /. int i = pOp-
1c690 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f >p1;. VdbeCurso
1c691 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a r *pC;. i64 v;.
1c692 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
1c693 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 && i<p->nCursor
1c694 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 );. pC = p->apC
1c695 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 sr[i];. assert(
1c696 20 70 43 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d pC!=0 );. rc =
1c697 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 sqlite3VdbeCurs
1c698 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 orMoveto(pC);.
1c699 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 if( rc ) goto ab
1c69a 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1c69b 3b 0a 20 20 69 66 28 20 70 43 2d 3e 72 6f 77 69 ;. if( pC->rowi
1c69c 64 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 dIsValid ){.
1c69d 76 20 3d 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 v = pC->lastRowi
1c69e 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 d;. }else if( p
1c69f 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 C->pseudoTable )
1c6a0 7b 0a 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 {. v = keyToI
1c6a1 6e 74 28 70 43 2d 3e 69 4b 65 79 29 3b 0a 20 20 nt(pC->iKey);.
1c6a2 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 6e 75 }else if( pC->nu
1c6a3 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 2f 2a 20 llRow ){. /*
1c6a4 4c 65 61 76 65 20 74 68 65 20 72 6f 77 69 64 20 Leave the rowid
1c6a5 73 65 74 20 74 6f 20 61 20 4e 55 4c 4c 20 2a 2f set to a NULL */
1c6a6 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 65 . break;. }e
1c6a7 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
1c6a8 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 pC->pCursor!=0
1c6a9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
1c6aa 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e 70 reeKeySize(pC->p
1c6ab 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20 Cursor, &v);.
1c6ac 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28 76 29 v = keyToInt(v)
1c6ad 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e ;. }. pOut->u.
1c6ae 69 20 3d 20 76 3b 0a 20 20 4d 65 6d 53 65 74 54 i = v;. MemSetT
1c6af 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1c6b0 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b M_Int);. break;
1c6b1 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e .}../* Opcode: N
1c6b2 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a 20 2a 20 ullRow P1 * * *
1c6b3 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 *.**.** Move the
1c6b4 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 cursor P1 to a
1c6b5 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e 79 20 4f null row. Any O
1c6b6 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 61 74 69 P_Column operati
1c6b7 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 ons.** that occu
1c6b8 72 20 77 68 69 6c 65 20 74 68 65 20 63 75 72 73 r while the curs
1c6b9 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 6e 75 6c or is on the nul
1c6ba 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c 77 61 79 l row will alway
1c6bb 73 0a 2a 2a 20 77 72 69 74 65 20 61 20 4e 55 4c s.** write a NUL
1c6bc 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 L..*/.case OP_Nu
1c6bd 6c 6c 52 6f 77 3a 20 7b 0a 20 20 69 6e 74 20 69 llRow: {. int i
1c6be 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 = pOp->p1;. Vd
1c6bf 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 beCursor *pC;..
1c6c0 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
1c6c1 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b i<p->nCursor );
1c6c2 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 . pC = p->apCsr
1c6c3 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 [i];. assert( p
1c6c4 43 21 3d 30 20 29 3b 0a 20 20 70 43 2d 3e 6e 75 C!=0 );. pC->nu
1c6c5 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 70 43 2d llRow = 1;. pC-
1c6c6 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1c6c7 30 3b 0a 20 20 69 66 28 20 70 43 2d 3e 70 43 75 0;. if( pC->pCu
1c6c8 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 rsor ){. sqli
1c6c9 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 te3BtreeClearCur
1c6ca 73 6f 72 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 sor(pC->pCursor)
1c6cb 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1c6cc 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 61 73 ../* Opcode: Las
1c6cd 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1c6ce 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 .** The next use
1c6cf 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 of the Rowid or
1c6d0 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 Column or Next
1c6d1 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 instruction for
1c6d2 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 P1 .** will refe
1c6d3 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e r to the last en
1c6d4 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
1c6d5 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 ase table or ind
1c6d6 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 ex..** If the ta
1c6d7 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 ble or index is
1c6d8 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 empty and P2>0,
1c6d9 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 then jump immedi
1c6da 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 ately to P2..**
1c6db 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 If P2 is 0 or if
1c6dc 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e the table or in
1c6dd 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 dex is not empty
1c6de 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a , fall through.*
1c6df 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * to the followi
1c6e0 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ng instruction..
1c6e1 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 73 74 3a */.case OP_Last:
1c6e2 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1c6e3 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 p */. int i = p
1c6e4 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 Op->p1;. VdbeCu
1c6e5 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 rsor *pC;. BtCu
1c6e6 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 rsor *pCrsr;. i
1c6e7 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 nt res;.. asser
1c6e8 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e t( i>=0 && i<p->
1c6e9 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 nCursor );. pC
1c6ea 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 = p->apCsr[i];.
1c6eb 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 assert( pC!=0 )
1c6ec 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e ;. pCrsr = pC->
1c6ed 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 pCursor;. asser
1c6ee 74 28 20 70 43 72 73 72 21 3d 30 20 29 3b 0a 20 t( pCrsr!=0 );.
1c6ef 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1c6f0 65 65 4c 61 73 74 28 70 43 72 73 72 2c 20 26 72 eeLast(pCrsr, &r
1c6f1 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 es);. pC->nullR
1c6f2 6f 77 20 3d 20 28 75 38 29 72 65 73 3b 0a 20 20 ow = (u8)res;.
1c6f3 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1c6f4 74 6f 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 72 6f to = 0;. pC->ro
1c6f5 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1c6f6 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 pC->cacheStatu
1c6f7 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1c6f8 0a 20 20 69 66 28 20 72 65 73 20 26 26 20 70 4f . if( res && pO
1c6f9 70 2d 3e 70 32 3e 30 20 29 7b 0a 20 20 20 20 70 p->p2>0 ){. p
1c6fa 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1c6fb 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1c6fc 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 ../* Opcode: Sor
1c6fd 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1c6fe 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1c6ff 64 6f 65 73 20 65 78 61 63 74 6c 79 20 74 68 65 does exactly the
1c700 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 4f same thing as O
1c701 50 5f 52 65 77 69 6e 64 20 65 78 63 65 70 74 20 P_Rewind except
1c702 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65 that.** it incre
1c703 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d ments an undocum
1c704 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 61 72 ented global var
1c705 69 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 74 iable used for t
1c706 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f esting..**.** So
1c707 72 74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c rting is accompl
1c708 69 73 68 65 64 20 62 79 20 77 72 69 74 69 6e 67 ished by writing
1c709 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 records into a
1c70a 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a sorting index,.*
1c70b 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e 67 * then rewinding
1c70c 20 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64 20 that index and
1c70d 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b 20 playing it back
1c70e 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 from beginning t
1c70f 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 73 o.** end. We us
1c710 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 e the OP_Sort op
1c711 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66 20 code instead of
1c712 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f 20 OP_Rewind to do
1c713 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67 the.** rewinding
1c714 20 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c 6f so that the glo
1c715 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69 6c bal variable wil
1c716 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 64 l be incremented
1c717 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73 69 and.** regressi
1c718 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64 65 74 on tests can det
1c719 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
1c71a 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d 69 r not the optimi
1c71b 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63 zer is.** correc
1c71c 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f tly optimizing o
1c71d 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 ut sorts..*/.cas
1c71e 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20 e OP_Sort: {
1c71f 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 /* jump */.#
1c720 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1c721 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 T. sqlite3_sort
1c722 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 _count++;. sqli
1c723 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 te3_search_count
1c724 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e --;.#endif. p->
1c725 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f aCounter[SQLITE_
1c726 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 2d STMTSTATUS_SORT-
1c727 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 1]++;. /* Fall
1c728 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f through into OP_
1c729 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f Rewind */.}./* O
1c72a 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50 31 pcode: Rewind P1
1c72b 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1c72c 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 The next use of
1c72d 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c the Rowid or Col
1c72e 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 umn or Next inst
1c72f 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a ruction for P1 .
1c730 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f ** will refer to
1c731 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
1c732 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1c733 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e table or index.
1c734 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 .** If the table
1c735 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 or index is emp
1c736 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 ty and P2>0, the
1c737 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 n jump immediate
1c738 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 ly to P2..** If
1c739 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 P2 is 0 or if th
1c73a 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 e table or index
1c73b 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 is not empty, f
1c73c 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 all through.** t
1c73d 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
1c73e 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
1c73f 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20 case OP_Rewind:
1c740 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1c741 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f */. int i = pO
1c742 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 p->p1;. VdbeCur
1c743 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 sor *pC;. BtCur
1c744 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e sor *pCrsr;. in
1c745 74 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 74 t res;.. assert
1c746 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e ( i>=0 && i<p->n
1c747 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d Cursor );. pC =
1c748 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 p->apCsr[i];.
1c749 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b assert( pC!=0 );
1c74a 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 . if( (pCrsr =
1c74b 70 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 pC->pCursor)!=0
1c74c 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1c74d 74 65 33 42 74 72 65 65 46 69 72 73 74 28 70 43 te3BtreeFirst(pC
1c74e 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 rsr, &res);.
1c74f 70 43 2d 3e 61 74 46 69 72 73 74 20 3d 20 72 65 pC->atFirst = re
1c750 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70 s==0 ?1:0;. p
1c751 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1c752 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 o = 0;. pC->c
1c753 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 acheStatus = CAC
1c754 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 70 43 HE_STALE;. pC
1c755 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1c756 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
1c757 20 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 res = 1;. }.
1c758 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 pC->nullRow = (u
1c759 38 29 72 65 73 3b 0a 20 20 61 73 73 65 72 74 28 8)res;. assert(
1c75a 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f pOp->p2>0 && pO
1c75b 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a p->p2<p->nOp );.
1c75c 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
1c75d 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1c75e 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1c75f 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 }../* Opcode: Ne
1c760 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a xt P1 P2 * * *.*
1c761 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 63 75 72 *.** Advance cur
1c762 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 sor P1 so that i
1c763 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 t points to the
1c764 6e 65 78 74 20 6b 65 79 2f 64 61 74 61 20 70 61 next key/data pa
1c765 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 ir in its.** tab
1c766 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 le or index. If
1c767 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f there are no mo
1c768 72 65 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 re key/value pai
1c769 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 rs then fall thr
1c76a 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 ough.** to the f
1c76b 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 ollowing instruc
1c76c 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 tion. But if th
1c76d 65 20 63 75 72 73 6f 72 20 61 64 76 61 6e 63 65 e cursor advance
1c76e 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c was successful,
1c76f 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 .** jump immedia
1c770 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a tely to P2..**.*
1c771 2a 20 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 * The P1 cursor
1c772 6d 75 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 must be for a re
1c773 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 al table, not a
1c774 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a pseudo-table..**
1c775 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 50 72 .** See also: Pr
1c776 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a ev.*/./* Opcode:
1c777 20 50 72 65 76 20 50 31 20 50 32 20 2a 20 2a 20 Prev P1 P2 * *
1c778 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20 *.**.** Back up
1c779 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 cursor P1 so tha
1c77a 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 t it points to t
1c77b 68 65 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f he previous key/
1c77c 64 61 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 data pair in its
1c77d 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 .** table or ind
1c77e 65 78 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 ex. If there is
1c77f 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6b 65 79 no previous key
1c780 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 68 65 /value pairs the
1c781 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a n fall through.*
1c782 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * to the followi
1c783 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 ng instruction.
1c784 20 42 75 74 20 69 66 20 74 68 65 20 63 75 72 73 But if the curs
1c785 6f 72 20 62 61 63 6b 75 70 20 77 61 73 20 73 75 or backup was su
1c786 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d ccessful,.** jum
1c787 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f p immediately to
1c788 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 P2..**.** The P
1c789 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 1 cursor must be
1c78a 20 66 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c for a real tabl
1c78b 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d e, not a pseudo-
1c78c 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f table..*/.case O
1c78d 50 5f 50 72 65 76 3a 20 20 20 20 20 20 20 20 20 P_Prev:
1c78e 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 /* jump */.case
1c78f 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20 20 20 20 OP_Next: {
1c790 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 /* jump */.
1c791 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c792 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1c793 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 r;. int res;..
1c794 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 CHECK_FOR_INTER
1c795 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28 20 RUPT;. assert(
1c796 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1c797 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 p->p1<p->nCursor
1c798 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 );. pC = p->ap
1c799 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1c79a 69 66 28 20 70 43 3d 3d 30 20 29 7b 0a 20 20 20 if( pC==0 ){.
1c79b 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 65 65 20 break; /* See
1c79c 74 69 63 6b 65 74 20 23 32 32 37 33 20 2a 2f 0a ticket #2273 */.
1c79d 20 20 7d 0a 20 20 70 43 72 73 72 20 3d 20 70 43 }. pCrsr = pC
1c79e 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 ->pCursor;. ass
1c79f 65 72 74 28 20 70 43 72 73 72 20 29 3b 0a 20 20 ert( pCrsr );.
1c7a0 72 65 73 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 res = 1;. asser
1c7a1 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d t( pC->deferredM
1c7a2 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 oveto==0 );. rc
1c7a3 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d = pOp->opcode==
1c7a4 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c 69 74 65 OP_Next ? sqlite
1c7a5 33 42 74 72 65 65 4e 65 78 74 28 70 43 72 73 72 3BtreeNext(pCrsr
1c7a6 2c 20 26 72 65 73 29 20 3a 0a 20 20 20 20 20 20 , &res) :.
1c7a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c7a8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
1c7a9 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 72 treePrevious(pCr
1c7aa 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 70 43 2d sr, &res);. pC-
1c7ab 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72 >nullRow = (u8)r
1c7ac 65 73 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 es;. pC->cacheS
1c7ad 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1c7ae 41 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 3d 3d ALE;. if( res==
1c7af 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 0 ){. pc = pO
1c7b0 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 69 p->p2 - 1;. i
1c7b1 66 28 20 70 4f 70 2d 3e 70 35 20 29 20 70 2d 3e f( pOp->p5 ) p->
1c7b2 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 35 aCounter[pOp->p5
1c7b3 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 20 53 51 -1]++;.#ifdef SQ
1c7b4 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 LITE_TEST. sq
1c7b5 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 lite3_search_cou
1c7b6 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d nt++;.#endif. }
1c7b7 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 . pC->rowidIsVa
1c7b8 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b lid = 0;. break
1c7b9 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1c7ba 49 64 78 49 6e 73 65 72 74 20 50 31 20 50 32 20 IdxInsert P1 P2
1c7bb 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 P3 * *.**.** Reg
1c7bc 69 73 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61 ister P2 holds a
1c7bd 20 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d SQL index key m
1c7be 61 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a ade using the.**
1c7bf 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 MakeRecord inst
1c7c0 72 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 ructions. This
1c7c1 6f 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 68 opcode writes th
1c7c2 61 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 at key.** into t
1c7c3 68 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61 he index P1. Da
1c7c4 74 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 ta for the entry
1c7c5 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 is nil..**.** P
1c7c6 33 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74 3 is a flag that
1c7c7 20 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74 provides a hint
1c7c8 20 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c to the b-tree l
1c7c9 61 79 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a ayer that this.*
1c7ca 2a 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 * insert is like
1c7cb 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 ly to be an appe
1c7cc 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 nd..**.** This i
1c7cd 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 nstruction only
1c7ce 77 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 works for indice
1c7cf 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 s. The equivale
1c7d0 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a nt instruction.*
1c7d1 2a 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 * for tables is
1c7d2 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 OP_Insert..*/.ca
1c7d3 73 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a se OP_IdxInsert:
1c7d4 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 { /* in2
1c7d5 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f */. int i = pO
1c7d6 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 p->p1;. VdbeCur
1c7d7 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 sor *pC;. BtCur
1c7d8 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 sor *pCrsr;. as
1c7d9 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1c7da 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1c7db 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 assert( p->apCsr
1c7dc 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 [i]!=0 );. asse
1c7dd 72 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 rt( pIn2->flags
1c7de 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 & MEM_Blob );.
1c7df 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 43 if( (pCrsr = (pC
1c7e0 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d = p->apCsr[i])-
1c7e1 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a >pCursor)!=0 ){.
1c7e2 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e assert( pC->
1c7e3 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 isTable==0 );.
1c7e4 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f rc = ExpandBlo
1c7e5 62 28 70 49 6e 32 29 3b 0a 20 20 20 20 69 66 28 b(pIn2);. if(
1c7e6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1c7e7 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 {. int nKey
1c7e8 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 = pIn2->n;.
1c7e9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b const char *zK
1c7ea 65 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 ey = pIn2->z;.
1c7eb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1c7ec 42 74 72 65 65 49 6e 73 65 72 74 28 70 43 72 73 BtreeInsert(pCrs
1c7ed 72 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 22 r, zKey, nKey, "
1c7ee 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 ", 0, 0, pOp->p3
1c7ef 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1c7f0 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 pC->deferredMov
1c7f1 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 eto==0 );.
1c7f2 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1c7f3 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1c7f4 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1c7f5 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1c7f6 49 64 78 44 65 6c 65 74 65 20 50 31 20 50 32 20 IdxDelete P1 P2
1c7f7 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 P3 * *.**.** The
1c7f8 20 63 6f 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 content of P3 r
1c7f9 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e egisters startin
1c7fa 67 20 61 74 20 72 65 67 69 73 74 65 72 20 50 32 g at register P2
1c7fb 20 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 form.** an unpa
1c7fc 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 cked index key.
1c7fd 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f This opcode remo
1c7fe 76 65 73 20 74 68 61 74 20 65 6e 74 72 79 20 66 ves that entry f
1c7ff 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 rom the .** inde
1c800 78 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 x opened by curs
1c801 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f or P1..*/.case O
1c802 50 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b 0a 20 P_IdxDelete: {.
1c803 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 int i = pOp->p1
1c804 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
1c805 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a pC;. BtCursor *
1c806 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28 pCrsr;. assert(
1c807 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 pOp->p3>0 );.
1c808 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e assert( pOp->p2>
1c809 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 0 && pOp->p2+pOp
1c80a 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 ->p3<=p->nMem+1
1c80b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d );. assert( i>=
1c80c 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 0 && i<p->nCurso
1c80d 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1c80e 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b ->apCsr[i]!=0 );
1c80f 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 . if( (pCrsr =
1c810 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 (pC = p->apCsr[i
1c811 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 ])->pCursor)!=0
1c812 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a ){. int res;.
1c813 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f UnpackedReco
1c814 72 64 20 72 3b 0a 20 20 20 20 72 2e 70 4b 65 79 rd r;. r.pKey
1c815 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 Info = pC->pKeyI
1c816 6e 66 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c nfo;. r.nFiel
1c817 64 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 33 d = (u16)pOp->p3
1c818 3b 0a 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 ;. r.flags =
1c819 30 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 0;. r.aMem =
1c81a 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1c81b 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ];. rc = sqli
1c81c 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
1c81d 70 61 63 6b 65 64 28 70 43 72 73 72 2c 20 26 72 packed(pCrsr, &r
1c81e 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 , 0, 0, &res);.
1c81f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1c820 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20 29 E_OK && res==0 )
1c821 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1c822 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 ite3BtreeDelete(
1c823 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 pCrsr);. }.
1c824 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 assert( pC->de
1c825 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 ferredMoveto==0
1c826 29 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 );. pC->cache
1c827 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1c828 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61 TALE;. }. brea
1c829 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1c82a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 20 IdxRowid P1 P2
1c82b 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 * * *.**.** Writ
1c82c 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 e into register
1c82d 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 P2 an integer wh
1c82e 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 20 ich is the last
1c82f 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 63 entry in the rec
1c830 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e ord at.** the en
1c831 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b d of the index k
1c832 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ey pointed to by
1c833 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 69 cursor P1. Thi
1c834 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c 64 s integer should
1c835 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 be.** the rowid
1c836 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e of the table en
1c837 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 69 try to which thi
1c838 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 6f s index entry po
1c839 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ints..**.** See
1c83a 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b also: Rowid, Mak
1c83b 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 eRecord..*/.case
1c83c 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20 OP_IdxRowid: {
1c83d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c83e 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1c83f 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 */. int i = pOp
1c840 2d 3e 70 31 3b 0a 20 20 42 74 43 75 72 73 6f 72 ->p1;. BtCursor
1c841 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 62 65 43 *pCrsr;. VdbeC
1c842 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 ursor *pC;.. as
1c843 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1c844 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1c845 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 assert( p->apCsr
1c846 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 [i]!=0 );. if(
1c847 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20 70 (pCrsr = (pC = p
1c848 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 ->apCsr[i])->pCu
1c849 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 rsor)!=0 ){.
1c84a 69 36 34 20 72 6f 77 69 64 3b 0a 0a 20 20 20 20 i64 rowid;..
1c84b 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 assert( pC->defe
1c84c 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b rredMoveto==0 );
1c84d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1c84e 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 >isTable==0 );.
1c84f 20 20 20 69 66 28 20 21 70 43 2d 3e 6e 75 6c 6c if( !pC->null
1c850 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Row ){. rc
1c851 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 = sqlite3VdbeIdx
1c852 52 6f 77 69 64 28 70 43 72 73 72 2c 20 26 72 6f Rowid(pCrsr, &ro
1c853 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 wid);. if(
1c854 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1c855 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 . goto ab
1c856 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1c857 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1c858 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1c859 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Out, MEM_Int);.
1c85a 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d pOut->u.i =
1c85b 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 rowid;. }.
1c85c 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1c85d 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 20 50 Opcode: IdxGE P
1c85e 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1c85f 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 .** The P4 regis
1c860 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e ter values begin
1c861 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 ning with P3 for
1c862 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e m an unpacked in
1c863 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 dex .** key that
1c864 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 omits the ROWID
1c865 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 . Compare this
1c866 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 key value agains
1c867 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 t the index .**
1c868 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 that P1 is curre
1c869 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f ntly pointing to
1c86a 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 , ignoring the R
1c86b 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 OWID on the P1 i
1c86c 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ndex..**.** If t
1c86d 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 he P1 index entr
1c86e 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 y is greater tha
1c86f 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 n or equal to th
1c870 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20 74 e key value.** t
1c871 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 hen jump to P2.
1c872 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 Otherwise fall
1c873 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
1c874 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ext instruction.
1c875 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 .**.** If P5 is
1c876 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 non-zero then th
1c877 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 e key value is i
1c878 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 ncreased by an e
1c879 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f 72 psilon .** prior
1c87a 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 to the comparis
1c87b 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 20 74 on. This make t
1c87c 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c he opcode work l
1c87d 69 6b 65 20 49 64 78 47 54 20 65 78 63 65 70 74 ike IdxGT except
1c87e 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 68 65 20 .** that if the
1c87f 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 73 74 65 key from registe
1c880 72 20 50 33 20 69 73 20 61 20 70 72 65 66 69 78 r P3 is a prefix
1c881 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 20 74 of the key in t
1c882 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 he cursor,.** th
1c883 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73 e result is fals
1c884 65 20 77 68 65 72 65 61 73 20 69 74 20 77 6f 75 e whereas it wou
1c885 6c 64 20 62 65 20 74 72 75 65 20 77 69 74 68 20 ld be true with
1c886 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 IdxGT..*/./* Opc
1c887 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 20 50 32 ode: IdxLT P1 P2
1c888 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 54 P3 * P5.**.** T
1c889 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20 76 he P4 register v
1c88a 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 alues beginning
1c88b 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 with P3 form an
1c88c 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a unpacked index .
1c88d 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 ** key that omit
1c88e 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f s the ROWID. Co
1c88f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 mpare this key v
1c890 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 alue against the
1c891 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 index .** that
1c892 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 P1 is currently
1c893 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e pointing to, ign
1c894 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 oring the ROWID
1c895 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e on the P1 index.
1c896 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 .**.** If the P1
1c897 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 index entry is
1c898 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 less than the ke
1c899 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75 6d y value then jum
1c89a 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68 65 p to P2..** Othe
1c89b 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 rwise fall throu
1c89c 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 gh to the next i
1c89d 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
1c89e 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a * If P5 is non-z
1c89f 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 ero then the key
1c8a0 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 value is increa
1c8a1 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f sed by an epsilo
1c8a2 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 74 n prior .** to t
1c8a3 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 he comparison.
1c8a4 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20 6f This makes the o
1c8a5 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 pcode work like
1c8a6 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f IdxLE..*/.case O
1c8a7 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20 20 P_IdxLT:
1c8a8 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1c8a9 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45 3a /.case OP_IdxGE:
1c8aa 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1c8ab 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 p, in3 */. int
1c8ac 69 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 i= pOp->p1;. Vd
1c8ad 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 beCursor *pC;..
1c8ae 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
1c8af 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b i<p->nCursor );
1c8b0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 . assert( p->ap
1c8b1 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 Csr[i]!=0 );. i
1c8b2 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 f( (pC = p->apCs
1c8b3 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 3d r[i])->pCursor!=
1c8b4 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 0 ){. int res
1c8b5 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 ;. UnpackedRe
1c8b6 63 6f 72 64 20 72 3b 0a 20 20 20 20 61 73 73 65 cord r;. asse
1c8b7 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 rt( pC->deferred
1c8b8 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 Moveto==0 );.
1c8b9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 assert( pOp->p5
1c8ba 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d ==0 || pOp->p5==
1c8bb 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 1 );. assert(
1c8bc 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pOp->p4type==P4
1c8bd 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 72 2e _INT32 );. r.
1c8be 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 pKeyInfo = pC->p
1c8bf 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72 2e 6e KeyInfo;. r.n
1c8c0 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 Field = (u16)pOp
1c8c1 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 20 ->p4.i;. if(
1c8c2 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 20 pOp->p5 ){.
1c8c3 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 r.flags = UNPAC
1c8c4 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c 20 55 4e KED_INCRKEY | UN
1c8c5 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f PACKED_IGNORE_RO
1c8c6 57 49 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a WID;. }else{.
1c8c7 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 r.flags =
1c8c8 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f UNPACKED_IGNORE_
1c8c9 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 ROWID;. }.
1c8ca 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d r.aMem = &p->aM
1c8cb 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 em[pOp->p3];.
1c8cc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1c8cd 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 70 eIdxKeyCompare(p
1c8ce 43 2c 20 26 72 2c 20 26 72 65 73 29 3b 0a 20 20 C, &r, &res);.
1c8cf 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
1c8d0 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 e==OP_IdxLT ){.
1c8d1 20 20 20 20 20 72 65 73 20 3d 20 2d 72 65 73 3b res = -res;
1c8d2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1c8d3 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f assert( pOp->o
1c8d4 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47 45 20 pcode==OP_IdxGE
1c8d5 29 3b 0a 20 20 20 20 20 20 72 65 73 2b 2b 3b 0a );. res++;.
1c8d6 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65 }. if( re
1c8d7 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 s>0 ){. pc
1c8d8 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 3b 0a = pOp->p2 - 1 ;.
1c8d9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 }. }. brea
1c8da 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1c8db 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 20 50 Destroy P1 P2 P
1c8dc 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 3 * *.**.** Dele
1c8dd 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64 61 74 te an entire dat
1c8de 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 abase table or i
1c8df 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 ndex whose root
1c8e0 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 page in the data
1c8e1 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 base.** file is
1c8e2 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a given by P1..**.
1c8e3 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 ** The table bei
1c8e4 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69 73 20 ng destroyed is
1c8e5 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 in the main data
1c8e6 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 33 3d base file if P3=
1c8e7 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3d 31 =0. If.** P3==1
1c8e8 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 then the table
1c8e9 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 to be clear is i
1c8ea 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 n the auxiliary
1c8eb 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a database file.**
1c8ec 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f that is used to
1c8ed 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 store tables cr
1c8ee 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 eate using CREAT
1c8ef 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c E TEMPORARY TABL
1c8f0 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f E..**.** If AUTO
1c8f1 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 VACUUM is enable
1c8f2 64 20 74 68 65 6e 20 69 74 20 69 73 20 70 6f 73 d then it is pos
1c8f3 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 sible that anoth
1c8f4 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 er root page.**
1c8f5 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 20 69 might be moved i
1c8f6 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 64 65 nto the newly de
1c8f7 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67 65 20 leted root page
1c8f8 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 in order to keep
1c8f9 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 all.** root pag
1c8fa 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 74 es contiguous at
1c8fb 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
1c8fc 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 f the database.
1c8fd 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a 20 76 The former.** v
1c8fe 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 alue of the root
1c8ff 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76 65 64 page that moved
1c900 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62 65 66 - its value bef
1c901 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f 63 63 ore the move occ
1c902 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 73 74 urred -.** is st
1c903 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1c904 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 P2. If no page
1c905 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 61 .** movement wa
1c906 73 20 72 65 71 75 69 72 65 64 20 28 62 65 63 61 s required (beca
1c907 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 62 65 use the table be
1c908 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61 73 20 ing dropped was
1c909 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 65 20 already .** the
1c90a 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 65 20 last one in the
1c90b 64 61 74 61 62 61 73 65 29 20 74 68 65 6e 20 61 database) then a
1c90c 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 zero is stored
1c90d 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a in register P2..
1c90e 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d ** If AUTOVACUUM
1c90f 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 68 65 is disabled the
1c910 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 n a zero is stor
1c911 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1c912 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 2..**.** See als
1c913 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 73 65 o: Clear.*/.case
1c914 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b 20 20 OP_Destroy: {
1c915 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1c916 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 lease */. int i
1c917 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e Moved;. int iCn
1c918 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 t;.#ifndef SQLIT
1c919 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1c91a 42 4c 45 0a 20 20 56 64 62 65 20 2a 70 56 64 62 BLE. Vdbe *pVdb
1c91b 65 3b 0a 20 20 69 43 6e 74 20 3d 20 30 3b 0a 20 e;. iCnt = 0;.
1c91c 20 66 6f 72 28 70 56 64 62 65 3d 64 62 2d 3e 70 for(pVdbe=db->p
1c91d 56 64 62 65 3b 20 70 56 64 62 65 3b 20 70 56 64 Vdbe; pVdbe; pVd
1c91e 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65 78 74 29 be=pVdbe->pNext)
1c91f 7b 0a 20 20 20 20 69 66 28 20 70 56 64 62 65 2d {. if( pVdbe-
1c920 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1c921 49 43 5f 52 55 4e 20 26 26 20 70 56 64 62 65 2d IC_RUN && pVdbe-
1c922 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 3c 32 20 >inVtabMethod<2
1c923 26 26 20 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20 && pVdbe->pc>=0
1c924 29 7b 0a 20 20 20 20 20 20 69 43 6e 74 2b 2b 3b ){. iCnt++;
1c925 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 . }. }.#else
1c926 0a 20 20 69 43 6e 74 20 3d 20 64 62 2d 3e 61 63 . iCnt = db->ac
1c927 74 69 76 65 56 64 62 65 43 6e 74 3b 0a 23 65 6e tiveVdbeCnt;.#en
1c928 64 69 66 0a 20 20 69 66 28 20 69 43 6e 74 3e 31 dif. if( iCnt>1
1c929 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
1c92a 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 ITE_LOCKED;.
1c92b 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d p->errorAction =
1c92c 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c OE_Abort;. }el
1c92d 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20 se{. int iDb
1c92e 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 61 = pOp->p3;. a
1c92f 73 73 65 72 74 28 20 69 43 6e 74 3d 3d 31 20 29 ssert( iCnt==1 )
1c930 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1c931 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1c932 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 <<iDb))!=0 );.
1c933 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1c934 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64 62 2d reeDropTable(db-
1c935 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c 20 70 >aDb[iDb].pBt, p
1c936 4f 70 2d 3e 70 31 2c 20 26 69 4d 6f 76 65 64 29 Op->p1, &iMoved)
1c937 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
1c938 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1c939 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 nt);. pOut->u
1c93a 2e 69 20 3d 20 69 4d 6f 76 65 64 3b 0a 23 69 66 .i = iMoved;.#if
1c93b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1c93c 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
1c93d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1c93e 4b 20 26 26 20 69 4d 6f 76 65 64 21 3d 30 20 29 K && iMoved!=0 )
1c93f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 {. sqlite3R
1c940 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62 ootPageMoved(&db
1c941 2d 3e 61 44 62 5b 69 44 62 5d 2c 20 69 4d 6f 76 ->aDb[iDb], iMov
1c942 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 ed, pOp->p1);.
1c943 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
1c944 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1c945 63 6f 64 65 3a 20 43 6c 65 61 72 20 50 31 20 50 code: Clear P1 P
1c946 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 2 P3.**.** Delet
1c947 65 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 73 20 6f e all contents o
1c948 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 f the database t
1c949 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68 able or index wh
1c94a 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a ose root page.**
1c94b 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1c94c 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 file is given b
1c94d 79 20 50 31 2e 20 20 42 75 74 2c 20 75 6e 6c 69 y P1. But, unli
1c94e 6b 65 20 44 65 73 74 72 6f 79 2c 20 64 6f 20 6e ke Destroy, do n
1c94f 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 ot.** remove the
1c950 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1c951 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1c952 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
1c953 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c e table being cl
1c954 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 6d 61 ear is in the ma
1c955 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
1c956 20 69 66 20 50 32 3d 3d 30 2e 20 20 49 66 0a 2a if P2==0. If.*
1c957 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 74 68 65 * P2==1 then the
1c958 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 table to be cle
1c959 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78 ar is in the aux
1c95a 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 iliary database
1c95b 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 file.** that is
1c95c 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61 used to store ta
1c95d 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 69 6e bles create usin
1c95e 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41 g CREATE TEMPORA
1c95f 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 RY TABLE..**.**
1c960 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 If the P3 value
1c961 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1c962 6e 20 74 68 65 20 74 61 62 6c 65 20 72 65 66 65 n the table refe
1c963 72 72 65 64 20 74 6f 20 6d 75 73 74 20 62 65 20 rred to must be
1c964 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 74 61 62 an.** intkey tab
1c965 6c 65 20 28 61 6e 20 53 51 4c 20 74 61 62 6c 65 le (an SQL table
1c966 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 65 78 29 2e , not an index).
1c967 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
1c968 65 20 72 6f 77 20 63 68 61 6e 67 65 20 0a 2a 2a e row change .**
1c969 20 63 6f 75 6e 74 20 69 73 20 69 6e 63 72 65 6d count is increm
1c96a 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d ented by the num
1c96b 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 ber of rows in t
1c96c 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 he table being c
1c96d 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 49 66 20 50 leared. .** If P
1c96e 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 3 is greater tha
1c96f 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n zero, then the
1c970 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
1c971 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 0a register P3 is.
1c972 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 65 6d 65 6e ** also incremen
1c973 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 ted by the numbe
1c974 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 r of rows in the
1c975 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 table being cle
1c976 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ared..**.** See
1c977 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79 0a 2a 2f also: Destroy.*/
1c978 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61 72 3a 20 .case OP_Clear:
1c979 7b 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 20 {. int nChange
1c97a 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 = 0;. assert( (
1c97b 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1c97c 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30 20 1<<pOp->p2))!=0
1c97d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1c97e 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 3BtreeClearTable
1c97f 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b (. db->aDb[
1c980 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70 4f pOp->p2].pBt, pO
1c981 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33 20 p->p1, (pOp->p3
1c982 3f 20 26 6e 43 68 61 6e 67 65 20 3a 20 30 29 0a ? &nChange : 0).
1c983 20 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e );. if( pOp->
1c984 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 68 p3 ){. p->nCh
1c985 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65 3b ange += nChange;
1c986 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 . if( pOp->p3
1c987 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 >0 ){. p->a
1c988 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 Mem[pOp->p3].u.i
1c989 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 += nChange;.
1c98a 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
1c98b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 }../* Opcode: Cr
1c98c 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 32 20 eateTable P1 P2
1c98d 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f * * *.**.** Allo
1c98e 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 6c 65 cate a new table
1c98f 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 in the main dat
1c990 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 abase file if P1
1c991 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a ==0 or in the.**
1c992 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 auxiliary datab
1c993 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d ase file if P1==
1c994 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 61 63 1 or in an attac
1c995 68 65 64 20 64 61 74 61 62 61 73 65 20 69 66 0a hed database if.
1c996 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 65 20 ** P1>1. Write
1c997 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 the root page nu
1c998 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 mber of the new
1c999 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 table into.** re
1c99a 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a 20 gister P2.**.**
1c99b 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 The difference b
1c99c 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 20 61 etween a table a
1c99d 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 nd an index is t
1c99e 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 6d 75 his: A table mu
1c99f 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d 62 st.** have a 4-b
1c9a0 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 yte integer key
1c9a1 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 72 62 and can have arb
1c9a2 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 41 6e itrary data. An
1c9a3 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e index.** has an
1c9a4 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 20 62 arbitrary key b
1c9a5 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a ut no data..**.*
1c9a6 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 65 61 * See also: Crea
1c9a7 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 teIndex.*/./* Op
1c9a8 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e 64 65 code: CreateInde
1c9a9 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a x P1 P2 * * *.**
1c9aa 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
1c9ab 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20 ew index in the
1c9ac 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1c9ad 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 20 69 le if P1==0 or i
1c9ae 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 n the.** auxilia
1c9af 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ry database file
1c9b0 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20 if P1==1 or in
1c9b1 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 an attached data
1c9b2 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e base if.** P1>1.
1c9b3 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 6f 74 Write the root
1c9b4 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
1c9b5 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e the new table in
1c9b6 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 to.** register P
1c9b7 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f 63 2..**.** See doc
1c9b8 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50 umentation on OP
1c9b9 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 6f 72 _CreateTable for
1c9ba 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
1c9bb 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 rmation..*/.case
1c9bc 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 3a OP_CreateIndex:
1c9bd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f /* o
1c9be 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1c9bf 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 /.case OP_Create
1c9c0 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20 20 Table: {
1c9c1 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1c9c2 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 70 67 ease */. int pg
1c9c3 6e 6f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 66 6c no = 0;. int fl
1c9c4 61 67 73 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a ags;. Db *pDb;.
1c9c5 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c9c6 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1c9c7 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 db->nDb );. ass
1c9c8 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
1c9c9 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 sk & (1<<pOp->p1
1c9ca 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d ))!=0 );. pDb =
1c9cb 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 &db->aDb[pOp->p
1c9cc 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 1];. assert( pD
1c9cd 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69 b->pBt!=0 );. i
1c9ce 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1c9cf 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29 OP_CreateTable )
1c9d0 7b 0a 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d {. /* flags =
1c9d1 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20 2a BTREE_INTKEY; *
1c9d2 2f 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 42 54 /. flags = BT
1c9d3 52 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 REE_LEAFDATA|BTR
1c9d4 45 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c EE_INTKEY;. }el
1c9d5 73 65 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 se{. flags =
1c9d6 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b 0a BTREE_ZERODATA;.
1c9d7 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1c9d8 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 e3BtreeCreateTab
1c9d9 6c 65 28 70 44 62 2d 3e 70 42 74 2c 20 26 70 67 le(pDb->pBt, &pg
1c9da 6e 6f 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4f no, flags);. pO
1c9db 75 74 2d 3e 75 2e 69 20 3d 20 70 67 6e 6f 3b 0a ut->u.i = pgno;.
1c9dc 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1c9dd 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1c9de 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1c9df 4f 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63 68 Opcode: ParseSch
1c9e0 65 6d 61 20 50 31 20 50 32 20 2a 20 50 34 20 2a ema P1 P2 * P4 *
1c9e1 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64 20 .**.** Read and
1c9e2 70 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69 65 parse all entrie
1c9e3 73 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 s from the SQLIT
1c9e4 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 6f E_MASTER table o
1c9e5 66 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a 2a f database P1.**
1c9e6 20 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 20 that match the
1c9e7 57 48 45 52 45 20 63 6c 61 75 73 65 20 50 34 2e WHERE clause P4.
1c9e8 20 20 50 32 20 69 73 20 74 68 65 20 22 66 6f 72 P2 is the "for
1c9e9 63 65 22 20 66 6c 61 67 2e 20 20 20 41 6c 77 61 ce" flag. Alwa
1c9ea 79 73 20 64 6f 0a 2a 2a 20 74 68 65 20 70 61 72 ys do.** the par
1c9eb 73 69 6e 67 20 69 66 20 50 32 20 69 73 20 74 72 sing if P2 is tr
1c9ec 75 65 2e 20 20 49 66 20 50 32 20 69 73 20 66 61 ue. If P2 is fa
1c9ed 6c 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 lse, then this r
1c9ee 6f 75 74 69 6e 65 20 69 73 20 61 0a 2a 2a 20 6e outine is a.** n
1c9ef 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 63 68 65 o-op if the sche
1c9f0 6d 61 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e ma is not curren
1c9f1 74 6c 79 20 6c 6f 61 64 65 64 2e 20 20 49 6e 20 tly loaded. In
1c9f2 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 other words, if
1c9f3 50 32 0a 2a 2a 20 69 73 20 66 61 6c 73 65 2c 20 P2.** is false,
1c9f4 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 the SQLITE_MASTE
1c9f5 52 20 74 61 62 6c 65 20 69 73 20 6f 6e 6c 79 20 R table is only
1c9f6 70 61 72 73 65 64 20 69 66 20 74 68 65 20 72 65 parsed if the re
1c9f7 73 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 st of the.** sch
1c9f8 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 6c ema is already l
1c9f9 6f 61 64 65 64 20 69 6e 74 6f 20 74 68 65 20 73 oaded into the s
1c9fa 79 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a ymbol table..**.
1c9fb 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 ** This opcode i
1c9fc 6e 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73 65 nvokes the parse
1c9fd 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 r to create a ne
1c9fe 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e w virtual machin
1c9ff 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73 20 e,.** then runs
1ca00 74 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20 the new virtual
1ca01 6d 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73 20 machine. It is
1ca02 74 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61 6e thus a re-entran
1ca03 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 t opcode..*/.cas
1ca04 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 e OP_ParseSchema
1ca05 3a 20 7b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 : {. int iDb =
1ca06 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 pOp->p1;. asser
1ca07 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 t( iDb>=0 && iDb
1ca08 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f <db->nDb );.. /
1ca09 2a 20 49 66 20 70 4f 70 2d 3e 70 32 20 69 73 20 * If pOp->p2 is
1ca0a 30 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 0, then this opc
1ca0b 6f 64 65 20 69 73 20 62 65 69 6e 67 20 65 78 65 ode is being exe
1ca0c 63 75 74 65 64 20 74 6f 20 72 65 61 64 20 61 0a cuted to read a.
1ca0d 20 20 2a 2a 20 73 69 6e 67 6c 65 20 72 6f 77 2c ** single row,
1ca0e 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 for example the
1ca0f 20 72 6f 77 20 63 6f 72 72 65 73 70 6f 6e 64 69 row correspondi
1ca10 6e 67 20 74 6f 20 61 20 6e 65 77 20 69 6e 64 65 ng to a new inde
1ca11 78 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 x. ** created b
1ca12 79 20 74 68 69 73 20 56 44 42 45 2c 20 66 72 6f y this VDBE, fro
1ca13 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 m the sqlite_mas
1ca14 74 65 72 20 74 61 62 6c 65 2e 20 49 74 20 6f 6e ter table. It on
1ca15 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 ly. ** does thi
1ca16 73 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 s if the corresp
1ca17 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 onding in-memory
1ca18 20 73 63 68 65 6d 61 20 69 73 20 63 75 72 72 65 schema is curre
1ca19 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61 64 65 64 ntly. ** loaded
1ca1a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 . Otherwise, the
1ca1b 20 6e 65 77 20 69 6e 64 65 78 20 64 65 66 69 6e new index defin
1ca1c 69 74 69 6f 6e 20 63 61 6e 20 62 65 20 6c 6f 61 ition can be loa
1ca1d 64 65 64 20 61 6c 6f 6e 67 0a 20 20 2a 2a 20 77 ded along. ** w
1ca1e 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 ith the rest of
1ca1f 74 68 65 20 73 63 68 65 6d 61 20 77 68 65 6e 20 the schema when
1ca20 69 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a it is required..
1ca21 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74 68 6f 75 **. ** Althou
1ca22 67 68 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 gh the mutex on
1ca23 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
1ca24 65 63 74 20 74 68 61 74 20 63 6f 72 72 65 73 70 ect that corresp
1ca25 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a 20 64 61 74 onds to. ** dat
1ca26 61 62 61 73 65 20 69 44 62 20 28 74 68 65 20 64 abase iDb (the d
1ca27 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 atabase containi
1ca28 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 ng the sqlite_ma
1ca29 73 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 ster table. **
1ca2a 72 65 61 64 20 62 79 20 74 68 69 73 20 69 6e 73 read by this ins
1ca2b 74 72 75 63 74 69 6f 6e 29 20 69 73 20 63 75 72 truction) is cur
1ca2c 72 65 6e 74 6c 79 20 68 65 6c 64 2c 20 69 74 20 rently held, it
1ca2d 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a is necessary to.
1ca2e 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 ** obtain the
1ca2f 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20 61 mutexes on all a
1ca30 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
1ca31 73 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69 6e s before checkin
1ca32 67 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 73 63 g if. ** the sc
1ca33 68 65 6d 61 20 6f 66 20 69 44 62 20 69 73 20 6c hema of iDb is l
1ca34 6f 61 64 65 64 2e 20 54 68 69 73 20 69 73 20 62 oaded. This is b
1ca35 65 63 61 75 73 65 2c 20 61 74 20 74 68 65 20 73 ecause, at the s
1ca36 74 61 72 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 tart of. ** the
1ca37 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
1ca38 63 61 6c 6c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 call below, SQLi
1ca39 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a te will invoke .
1ca3a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 ** sqlite3Btre
1ca3b 65 45 6e 74 65 72 41 6c 6c 28 29 2e 20 49 66 20 eEnterAll(). If
1ca3c 61 6c 6c 20 6d 75 74 65 78 65 73 20 61 72 65 20 all mutexes are
1ca3d 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 not already held
1ca3e 2c 20 74 68 65 0a 20 20 2a 2a 20 69 44 62 20 6d , the. ** iDb m
1ca3f 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 6d 70 utex may be temp
1ca40 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 65 64 orarily released
1ca41 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f to avoid deadlo
1ca42 63 6b 2e 20 49 66 20 0a 20 20 2a 2a 20 74 68 69 ck. If . ** thi
1ca43 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 s happens, then
1ca44 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 some other threa
1ca45 64 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 65 d may delete the
1ca46 20 69 6e 2d 6d 65 6d 6f 72 79 20 0a 20 20 2a 2a in-memory . **
1ca47 20 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 schema of datab
1ca48 61 73 65 20 69 44 62 20 62 65 66 6f 72 65 20 74 ase iDb before t
1ca49 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1ca4a 20 72 75 6e 73 2e 20 54 68 65 20 73 63 68 65 6d runs. The schem
1ca4b 61 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 a. ** will not
1ca4c 62 65 20 72 65 6c 6f 61 64 65 64 20 62 65 63 75 be reloaded becu
1ca4d 61 73 65 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 ase the db->init
1ca4e 2e 62 75 73 79 20 66 6c 61 67 20 69 73 20 73 65 .busy flag is se
1ca4f 74 2e 20 54 68 69 73 0a 20 20 2a 2a 20 63 61 6e t. This. ** can
1ca50 20 72 65 73 75 6c 74 20 69 6e 20 61 20 22 6e 6f result in a "no
1ca51 20 73 75 63 68 20 74 61 62 6c 65 3a 20 73 71 6c such table: sql
1ca52 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 ite_master" or "
1ca53 6d 61 6c 66 6f 72 6d 65 64 0a 20 20 2a 2a 20 64 malformed. ** d
1ca54 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 22 20 atabase schema"
1ca55 65 72 72 6f 72 20 62 65 69 6e 67 20 72 65 74 75 error being retu
1ca56 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 rned to the user
1ca57 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
1ca58 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
1ca59 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b dsMutex(db->aDb[
1ca5a 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 20 20 73 iDb].pBt) );. s
1ca5b 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1ca5c 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20 70 All(db);. if( p
1ca5d 4f 70 2d 3e 70 32 20 7c 7c 20 44 62 48 61 73 50 Op->p2 || DbHasP
1ca5e 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c roperty(db, iDb,
1ca5f 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 DB_SchemaLoaded
1ca60 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 ) ){. const c
1ca61 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 53 har *zMaster = S
1ca62 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 CHEMA_TABLE(iDb)
1ca63 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c ;. char *zSql
1ca64 3b 0a 20 20 20 20 49 6e 69 74 44 61 74 61 20 69 ;. InitData i
1ca65 6e 69 74 44 61 74 61 3b 0a 20 20 20 20 69 6e 69 nitData;. ini
1ca66 74 44 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a 20 tData.db = db;.
1ca67 20 20 20 69 6e 69 74 44 61 74 61 2e 69 44 62 20 initData.iDb
1ca68 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 69 = pOp->p1;. i
1ca69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 nitData.pzErrMsg
1ca6a 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0a = &p->zErrMsg;.
1ca6b 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 zSql = sqlit
1ca6c 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20 e3MPrintf(db,.
1ca6d 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d "SELECT nam
1ca6e 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c e, rootpage, sql
1ca6f 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20 57 48 FROM '%q'.%s WH
1ca70 45 52 45 20 25 73 22 2c 0a 20 20 20 20 20 20 20 ERE %s",.
1ca71 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 db->aDb[iDb].zNa
1ca72 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4f 70 me, zMaster, pOp
1ca73 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 69 66 28 ->p4.z);. if(
1ca74 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 zSql==0 ){.
1ca75 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
1ca76 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a MEM;. }else{.
1ca77 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 (void)sqli
1ca78 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1ca79 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ca7a 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 db->init.busy==0
1ca7b 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e );. db->in
1ca7c 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20 20 it.busy = 1;.
1ca7d 20 20 20 69 6e 69 74 44 61 74 61 2e 72 63 20 3d initData.rc =
1ca7e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
1ca7f 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d assert( !db->m
1ca80 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
1ca81 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1ca82 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 3_exec(db, zSql,
1ca83 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c sqlite3InitCall
1ca84 62 61 63 6b 2c 20 26 69 6e 69 74 44 61 74 61 2c back, &initData,
1ca85 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
1ca86 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c==SQLITE_OK ) r
1ca87 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b c = initData.rc;
1ca88 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1ca89 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a Free(db, zSql);.
1ca8a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 db->init.b
1ca8b 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 28 usy = 0;. (
1ca8c 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
1ca8d 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d 0a tyOn(db);. }.
1ca8e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
1ca8f 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
1ca90 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1ca91 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f _NOMEM ){. go
1ca92 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 to no_mem;. }.
1ca93 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 0a 23 69 66 break; .}..#if
1ca94 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1ca95 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 26 _OMIT_ANALYZE) &
1ca96 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
1ca97 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 29 0a 2f E_OMIT_PARSER)./
1ca98 2a 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e * Opcode: LoadAn
1ca99 61 6c 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 alysis P1 * * *
1ca9a 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 *.**.** Read the
1ca9b 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 sqlite_stat1 ta
1ca9c 62 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 ble for database
1ca9d 20 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 P1 and load the
1ca9e 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 content.** of t
1ca9f 68 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 hat table into t
1caa0 68 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 he internal inde
1caa1 78 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 x hash table. T
1caa2 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a his will cause.*
1caa3 2a 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74 * the analysis t
1caa4 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70 o be used when p
1caa5 72 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 reparing all sub
1caa6 73 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e sequent queries.
1caa7 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 .*/.case OP_Load
1caa8 41 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 20 69 6e Analysis: {. in
1caa9 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b t iDb = pOp->p1;
1caaa 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d . assert( iDb>=
1caab 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 0 && iDb<db->nDb
1caac 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
1caad 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 e3AnalysisLoad(d
1caae 62 2c 20 69 44 62 29 3b 0a 20 20 62 72 65 61 6b b, iDb);. break
1caaf 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ; .}.#endif /*
1cab0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1cab1 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 26 26 OMIT_ANALYZE) &&
1cab2 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1cab3 5f 4f 4d 49 54 5f 50 41 52 53 45 52 29 20 20 2a _OMIT_PARSER) *
1cab4 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 /../* Opcode: Dr
1cab5 6f 70 54 61 62 6c 65 20 50 31 20 2a 20 2a 20 50 opTable P1 * * P
1cab6 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 4 *.**.** Remove
1cab7 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 the internal (i
1cab8 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73 n-memory) data s
1cab9 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64 tructures that d
1caba 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 74 escribe.** the t
1cabb 61 62 6c 65 20 6e 61 6d 65 64 20 50 34 20 69 6e able named P4 in
1cabc 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 database P1. T
1cabd 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 his is called af
1cabe 74 65 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20 69 ter a table.** i
1cabf 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 s dropped in ord
1cac0 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 er to keep the i
1cac1 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e nternal represen
1cac2 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a tation of the.**
1cac3 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 schema consiste
1cac4 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20 nt with what is
1cac5 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 on disk..*/.case
1cac6 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20 7b OP_DropTable: {
1cac7 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b . sqlite3Unlink
1cac8 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 64 AndDeleteTable(d
1cac9 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d b, pOp->p1, pOp-
1caca 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b >p4.z);. break;
1cacb 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 .}../* Opcode: D
1cacc 72 6f 70 49 6e 64 65 78 20 50 31 20 2a 20 2a 20 ropIndex P1 * *
1cacd 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 P4 *.**.** Remov
1cace 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 e the internal (
1cacf 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 in-memory) data
1cad0 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 structures that
1cad1 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 describe.** the
1cad2 69 6e 64 65 78 20 6e 61 6d 65 64 20 50 34 20 69 index named P4 i
1cad3 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 n database P1.
1cad4 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 This is called a
1cad5 66 74 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a 2a fter an index.**
1cad6 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f is dropped in o
1cad7 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 rder to keep the
1cad8 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 internal repres
1cad9 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a entation of the.
1cada 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 ** schema consis
1cadb 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 tent with what i
1cadc 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 s on disk..*/.ca
1cadd 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 3a se OP_DropIndex:
1cade 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 {. sqlite3Unli
1cadf 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 nkAndDeleteIndex
1cae0 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f (db, pOp->p1, pO
1cae1 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 p->p4.z);. brea
1cae2 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1cae3 20 44 72 6f 70 54 72 69 67 67 65 72 20 50 31 20 DropTrigger P1
1cae4 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 * * P4 *.**.** R
1cae5 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e emove the intern
1cae6 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 al (in-memory) d
1cae7 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 ata structures t
1cae8 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 hat describe.**
1cae9 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 the trigger name
1caea 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 d P4 in database
1caeb 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 P1. This is ca
1caec 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 72 69 lled after a tri
1caed 67 67 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70 70 gger.** is dropp
1caee 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b ed in order to k
1caef 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c eep the internal
1caf0 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1caf1 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 of the.** schema
1caf2 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 consistent with
1caf3 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b what is on disk
1caf4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f ..*/.case OP_Dro
1caf5 70 54 72 69 67 67 65 72 3a 20 7b 0a 20 20 73 71 pTrigger: {. sq
1caf6 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 lite3UnlinkAndDe
1caf7 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 leteTrigger(db,
1caf8 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 pOp->p1, pOp->p4
1caf9 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a .z);. break;.}.
1cafa 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1cafb 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
1cafc 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a CHECK./* Opcode:
1cafd 20 49 6e 74 65 67 72 69 74 79 43 6b 20 50 31 20 IntegrityCk P1
1cafe 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a P2 P3 * P5.**.**
1caff 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 Do an analysis
1cb00 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 of the currently
1cb01 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20 open database.
1cb02 20 53 74 6f 72 65 20 69 6e 0a 2a 2a 20 72 65 67 Store in.** reg
1cb03 69 73 74 65 72 20 50 31 20 74 68 65 20 74 65 78 ister P1 the tex
1cb04 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 t of an error me
1cb05 73 73 61 67 65 20 64 65 73 63 72 69 62 69 6e 67 ssage describing
1cb06 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a any problems..*
1cb07 2a 20 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 * If no problems
1cb08 20 61 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f 72 are found, stor
1cb09 65 20 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67 69 e a NULL in regi
1cb0a 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 ster P1..**.** T
1cb0b 68 65 20 72 65 67 69 73 74 65 72 20 50 33 20 63 he register P3 c
1cb0c 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 78 69 ontains the maxi
1cb0d 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c mum number of al
1cb0e 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e 0a 2a 2a lowed errors..**
1cb0f 20 41 74 20 6d 6f 73 74 20 72 65 67 28 50 33 29 At most reg(P3)
1cb10 20 65 72 72 6f 72 73 20 77 69 6c 6c 20 62 65 20 errors will be
1cb11 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 reported..** In
1cb12 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
1cb13 20 61 6e 61 6c 79 73 69 73 20 73 74 6f 70 73 20 analysis stops
1cb14 61 73 20 73 6f 6f 6e 20 61 73 20 72 65 67 28 50 as soon as reg(P
1cb15 31 29 20 65 72 72 6f 72 73 20 61 72 65 20 0a 2a 1) errors are .*
1cb16 2a 20 73 65 65 6e 2e 20 20 52 65 67 28 50 31 29 * seen. Reg(P1)
1cb17 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68 is updated with
1cb18 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1cb19 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 2e rrors remaining.
1cb1a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 .**.** The root
1cb1b 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 page numbers of
1cb1c 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 all tables in th
1cb1d 65 20 64 61 74 61 62 61 73 65 20 61 72 65 20 69 e database are i
1cb1e 6e 74 65 67 65 72 0a 2a 2a 20 73 74 6f 72 65 64 nteger.** stored
1cb1f 20 69 6e 20 72 65 67 28 50 31 29 2c 20 72 65 67 in reg(P1), reg
1cb20 28 50 31 2b 31 29 2c 20 72 65 67 28 50 31 2b 32 (P1+1), reg(P1+2
1cb21 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 72 65 20 61 ), .... There a
1cb22 72 65 20 50 32 20 74 61 62 6c 65 73 0a 2a 2a 20 re P2 tables.**
1cb23 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 total..**.** If
1cb24 50 35 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 P5 is not zero,
1cb25 74 68 65 20 63 68 65 63 6b 20 69 73 20 64 6f 6e the check is don
1cb26 65 20 6f 6e 20 74 68 65 20 61 75 78 69 6c 69 61 e on the auxilia
1cb27 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 ry database.** f
1cb28 69 6c 65 2c 20 6e 6f 74 20 74 68 65 20 6d 61 69 ile, not the mai
1cb29 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
1cb2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1cb2b 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d de is used to im
1cb2c 70 6c 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 65 plement the inte
1cb2d 67 72 69 74 79 5f 63 68 65 63 6b 20 70 72 61 67 grity_check prag
1cb2e 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 ma..*/.case OP_I
1cb2f 6e 74 65 67 72 69 74 79 43 6b 3a 20 7b 0a 20 20 ntegrityCk: {.
1cb30 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 int nRoot;
1cb31 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 /* Number of tab
1cb32 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 les to check. (
1cb33 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 Number of root p
1cb34 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 6e 74 20 ages.) */. int
1cb35 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 *aRoot; /* A
1cb36 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 rray of rootpage
1cb37 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62 numbers for tab
1cb38 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65 les to be checke
1cb39 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 d */. int j;
1cb3a 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
1cb3b 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
1cb3c 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e nErr; /* N
1cb3d 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
1cb3e 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 63 68 reported */. ch
1cb3f 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a ar *z; /*
1cb40 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 72 72 Text of the err
1cb41 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20 4d or report */. M
1cb42 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f em *pnErr; /
1cb43 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70 69 * Register keepi
1cb44 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f ng track of erro
1cb45 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a rs remaining */.
1cb46 20 20 0a 20 20 6e 52 6f 6f 74 20 3d 20 70 4f 70 . nRoot = pOp
1cb47 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 ->p2;. assert(
1cb48 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20 61 52 6f nRoot>0 );. aRo
1cb49 6f 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ot = sqlite3DbMa
1cb4a 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 llocRaw(db, size
1cb4b 6f 66 28 69 6e 74 29 2a 28 6e 52 6f 6f 74 2b 31 of(int)*(nRoot+1
1cb4c 29 20 29 3b 0a 20 20 69 66 28 20 61 52 6f 6f 74 ) );. if( aRoot
1cb4d 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ==0 ) goto no_me
1cb4e 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 m;. assert( pOp
1cb4f 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
1cb50 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1cb51 70 6e 45 72 72 20 3d 20 26 70 2d 3e 61 4d 65 6d pnErr = &p->aMem
1cb52 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 [pOp->p3];. ass
1cb53 65 72 74 28 20 28 70 6e 45 72 72 2d 3e 66 6c 61 ert( (pnErr->fla
1cb54 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 gs & MEM_Int)!=0
1cb55 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
1cb56 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 28 4d nErr->flags & (M
1cb57 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 EM_Str|MEM_Blob)
1cb58 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 20 3d )==0 );. pIn1 =
1cb59 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1cb5a 31 5d 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 1];. for(j=0; j
1cb5b 3c 6e 52 6f 6f 74 3b 20 6a 2b 2b 29 7b 0a 20 20 <nRoot; j++){.
1cb5c 20 20 61 52 6f 6f 74 5b 6a 5d 20 3d 20 28 69 6e aRoot[j] = (in
1cb5d 74 29 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 t)sqlite3VdbeInt
1cb5e 56 61 6c 75 65 28 26 70 49 6e 31 5b 6a 5d 29 3b Value(&pIn1[j]);
1cb5f 0a 20 20 7d 0a 20 20 61 52 6f 6f 74 5b 6a 5d 20 . }. aRoot[j]
1cb60 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 = 0;. assert( p
1cb61 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62 20 29 Op->p5<db->nDb )
1cb62 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e ;. assert( (p->
1cb63 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c btreeMask & (1<<
1cb64 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20 29 3b 0a pOp->p5))!=0 );.
1cb65 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 z = sqlite3Btr
1cb66 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b eeIntegrityCheck
1cb67 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35 (db->aDb[pOp->p5
1cb68 5d 2e 70 42 74 2c 20 61 52 6f 6f 74 2c 20 6e 52 ].pBt, aRoot, nR
1cb69 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 oot,.
1cb6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cb6b 20 20 20 20 20 20 28 69 6e 74 29 70 6e 45 72 72 (int)pnErr
1cb6c 2d 3e 75 2e 69 2c 20 26 6e 45 72 72 29 3b 0a 20 ->u.i, &nErr);.
1cb6d 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1cb6e 62 2c 20 61 52 6f 6f 74 29 3b 0a 20 20 70 6e 45 b, aRoot);. pnE
1cb6f 72 72 2d 3e 75 2e 69 20 2d 3d 20 6e 45 72 72 3b rr->u.i -= nErr;
1cb70 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1cb71 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a mSetNull(pIn1);.
1cb72 20 20 69 66 28 20 6e 45 72 72 3d 3d 30 20 29 7b if( nErr==0 ){
1cb73 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 3d 3d . assert( z==
1cb74 30 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 0 );. }else if(
1cb75 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 z==0 ){. got
1cb76 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65 6c 73 o no_mem;. }els
1cb77 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 e{. sqlite3Vd
1cb78 62 65 4d 65 6d 53 65 74 53 74 72 28 70 49 6e 31 beMemSetStr(pIn1
1cb79 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f , z, -1, SQLITE_
1cb7a 55 54 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 72 UTF8, sqlite3_fr
1cb7b 65 65 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54 ee);. }. UPDAT
1cb7c 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1cb7d 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 In1);. sqlite3V
1cb7e 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
1cb7f 67 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 g(pIn1, encoding
1cb80 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 );. break;.}.#e
1cb81 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1cb82 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 MIT_INTEGRITY_CH
1cb83 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 ECK */../* Opcod
1cb84 65 3a 20 52 6f 77 53 65 74 41 64 64 20 50 31 20 e: RowSetAdd P1
1cb85 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 P2 * * *.**.** I
1cb86 6e 73 65 72 74 20 74 68 65 20 69 6e 74 65 67 65 nsert the intege
1cb87 72 20 76 61 6c 75 65 20 68 65 6c 64 20 62 79 20 r value held by
1cb88 72 65 67 69 73 74 65 72 20 50 32 20 69 6e 74 6f register P2 into
1cb89 20 61 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 a boolean index
1cb8a 0a 2a 2a 20 68 65 6c 64 20 69 6e 20 72 65 67 69 .** held in regi
1cb8b 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 ster P1..**.** A
1cb8c 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c n assertion fail
1cb8d 73 20 69 66 20 50 32 20 69 73 20 6e 6f 74 20 61 s if P2 is not a
1cb8e 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 n integer..*/.ca
1cb8f 73 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a se OP_RowSetAdd:
1cb90 20 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 { /* in2
1cb91 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a */. Mem *pIdx;.
1cb92 20 20 4d 65 6d 20 2a 70 56 61 6c 3b 0a 20 20 61 Mem *pVal;. a
1cb93 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 ssert( pOp->p1>0
1cb94 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e && pOp->p1<=p->
1cb95 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 64 78 20 3d nMem );. pIdx =
1cb96 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1cb97 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 1];. assert( pO
1cb98 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e p->p2>0 && pOp->
1cb99 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p2<=p->nMem );.
1cb9a 20 70 56 61 6c 20 3d 20 26 70 2d 3e 61 4d 65 6d pVal = &p->aMem
1cb9b 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 [pOp->p2];. ass
1cb9c 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 ert( (pVal->flag
1cb9d 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 s & MEM_Int)!=0
1cb9e 29 3b 0a 20 20 69 66 28 20 28 70 49 64 78 2d 3e );. if( (pIdx->
1cb9f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
1cba0 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 et)==0 ){. sq
1cba1 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52 lite3VdbeMemSetR
1cba2 6f 77 53 65 74 28 70 49 64 78 29 3b 0a 20 20 20 owSet(pIdx);.
1cba3 20 69 66 28 20 28 70 49 64 78 2d 3e 66 6c 61 67 if( (pIdx->flag
1cba4 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
1cba5 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
1cba6 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 ;. }. sqlite3R
1cba7 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49 64 78 owSetInsert(pIdx
1cba8 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70 56 61 ->u.pRowSet, pVa
1cba9 6c 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 6b l->u.i);. break
1cbaa 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cbab 52 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 32 RowSetRead P1 P2
1cbac 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 P3 * *.**.** Ex
1cbad 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65 tract the smalle
1cbae 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 6f st value from bo
1cbaf 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 61 olean index P1 a
1cbb0 6e 64 20 70 75 74 20 74 68 61 74 20 76 61 6c 75 nd put that valu
1cbb1 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 e into.** regist
1cbb2 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 62 er P3. Or, if b
1cbb3 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 oolean index P1
1cbb4 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 is initially emp
1cbb5 74 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a 20 ty, leave P3.**
1cbb6 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75 unchanged and ju
1cbb7 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f mp to instructio
1cbb8 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 n P2..*/.case OP
1cbb9 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20 _RowSetRead: {
1cbba 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 6f 75 /* jump, ou
1cbbb 74 33 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 t3 */. Mem *pId
1cbbc 78 3b 0a 20 20 69 36 34 20 76 61 6c 3b 0a 20 20 x;. i64 val;.
1cbbd 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1cbbe 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 0 && pOp->p1<=p-
1cbbf 3e 6e 4d 65 6d 20 29 3b 0a 20 20 43 48 45 43 4b >nMem );. CHECK
1cbc0 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a _FOR_INTERRUPT;.
1cbc1 20 20 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 pIdx = &p->aMe
1cbc2 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f m[pOp->p1];. pO
1cbc3 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1cbc4 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 70 p->p3];. if( (p
1cbc5 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Idx->flags & MEM
1cbc6 5f 52 6f 77 53 65 74 29 3d 3d 30 20 0a 20 20 20 _RowSet)==0 .
1cbc7 7c 7c 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 || sqlite3RowSet
1cbc8 4e 65 78 74 28 70 49 64 78 2d 3e 75 2e 70 52 6f Next(pIdx->u.pRo
1cbc9 77 53 65 74 2c 20 26 76 61 6c 29 3d 3d 30 0a 20 wSet, &val)==0.
1cbca 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 ){. /* The b
1cbcb 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 69 73 20 oolean index is
1cbcc 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 73 71 6c empty */. sql
1cbcd 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1cbce 6c 6c 28 70 49 64 78 29 3b 0a 20 20 20 20 70 63 ll(pIdx);. pc
1cbcf 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1cbd0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1cbd1 41 20 76 61 6c 75 65 20 77 61 73 20 70 75 6c 6c A value was pull
1cbd2 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 ed from the inde
1cbd3 78 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 x */. assert(
1cbd4 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f pOp->p3>0 && pO
1cbd5 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1cbd6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1cbd7 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 eMemSetInt64(pOu
1cbd8 74 2c 20 76 61 6c 29 3b 0a 20 20 7d 0a 20 20 62 t, val);. }. b
1cbd9 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 reak;.}...#ifnde
1cbda 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1cbdb 49 47 47 45 52 0a 2f 2a 20 4f 70 63 6f 64 65 3a IGGER./* Opcode:
1cbdc 20 43 6f 6e 74 65 78 74 50 75 73 68 20 2a 20 2a ContextPush * *
1cbdd 20 2a 20 0a 2a 2a 0a 2a 2a 20 53 61 76 65 20 74 * .**.** Save t
1cbde 68 65 20 63 75 72 72 65 6e 74 20 56 64 62 65 20 he current Vdbe
1cbdf 63 6f 6e 74 65 78 74 20 73 75 63 68 20 74 68 61 context such tha
1cbe0 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 74 t it can be rest
1cbe1 6f 72 65 64 20 62 79 20 61 20 43 6f 6e 74 65 78 ored by a Contex
1cbe2 74 50 6f 70 0a 2a 2a 20 6f 70 63 6f 64 65 2e 20 tPop.** opcode.
1cbe3 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 6f 72 The context stor
1cbe4 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 es the last inse
1cbe5 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 20 6c rt row id, the l
1cbe6 61 73 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68 ast statement ch
1cbe7 61 6e 67 65 0a 2a 2a 20 63 6f 75 6e 74 2c 20 61 ange.** count, a
1cbe8 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 nd the current s
1cbe9 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 tatement change
1cbea 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f count..*/.case O
1cbeb 50 5f 43 6f 6e 74 65 78 74 50 75 73 68 3a 20 7b P_ContextPush: {
1cbec 0a 20 20 69 6e 74 20 69 20 3d 20 70 2d 3e 63 6f . int i = p->co
1cbed 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 2b 2b 3b ntextStackTop++;
1cbee 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e . Context *pCon
1cbef 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 text;.. assert(
1cbf0 20 69 3e 3d 30 20 29 3b 0a 20 20 2f 2a 20 46 49 i>=0 );. /* FI
1cbf1 58 20 4d 45 3a 20 54 68 69 73 20 73 68 6f 75 6c X ME: This shoul
1cbf2 64 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 d be allocated a
1cbf3 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 76 64 s part of the vd
1cbf4 62 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 be at compile-ti
1cbf5 6d 65 20 2a 2f 0a 20 20 69 66 28 20 69 3e 3d 70 me */. if( i>=p
1cbf6 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 ->contextStackDe
1cbf7 70 74 68 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6f pth ){. p->co
1cbf8 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 68 20 ntextStackDepth
1cbf9 3d 20 69 2b 31 3b 0a 20 20 20 20 70 2d 3e 63 6f = i+1;. p->co
1cbfa 6e 74 65 78 74 53 74 61 63 6b 20 3d 20 73 71 6c ntextStack = sql
1cbfb 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 ite3DbReallocOrF
1cbfc 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f 6e 74 65 ree(db, p->conte
1cbfd 78 74 53 74 61 63 6b 2c 0a 20 20 20 20 20 20 20 xtStack,.
1cbfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cbff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cc00 20 20 20 73 69 7a 65 6f 66 28 43 6f 6e 74 65 78 sizeof(Contex
1cc01 74 29 2a 28 69 2b 31 29 29 3b 0a 20 20 20 20 69 t)*(i+1));. i
1cc02 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 f( p->contextSta
1cc03 63 6b 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f ck==0 ) goto no_
1cc04 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 43 6f 6e 74 mem;. }. pCont
1cc05 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74 65 78 ext = &p->contex
1cc06 74 53 74 61 63 6b 5b 69 5d 3b 0a 20 20 70 43 6f tStack[i];. pCo
1cc07 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f 77 69 64 ntext->lastRowid
1cc08 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 = db->lastRowid
1cc09 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 6e 43 ;. pContext->nC
1cc0a 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 61 6e hange = p->nChan
1cc0b 67 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a ge;. break;.}..
1cc0c 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65 /* Opcode: Conte
1cc0d 78 74 50 6f 70 20 2a 20 2a 20 2a 20 0a 2a 2a 0a xtPop * * * .**.
1cc0e 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 56 ** Restore the V
1cc0f 64 62 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 74 dbe context to t
1cc10 68 65 20 73 74 61 74 65 20 69 74 20 77 61 73 20 he state it was
1cc11 69 6e 20 77 68 65 6e 20 63 6f 6e 74 65 78 74 50 in when contextP
1cc12 75 73 68 20 77 61 73 20 6c 61 73 74 0a 2a 2a 20 ush was last.**
1cc13 65 78 65 63 75 74 65 64 2e 20 54 68 65 20 63 6f executed. The co
1cc14 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 ntext stores the
1cc15 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 last insert row
1cc16 20 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 id, the last st
1cc17 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 68 61 6e 67 atement.** chang
1cc18 65 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65 e count, and the
1cc19 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 current stateme
1cc1a 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e nt change count.
1cc1b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74 .*/.case OP_Cont
1cc1c 65 78 74 50 6f 70 3a 20 7b 0a 20 20 43 6f 6e 74 extPop: {. Cont
1cc1d 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 20 3d 20 ext *pContext =
1cc1e 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b &p->contextStack
1cc1f 5b 2d 2d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 [--p->contextSta
1cc20 63 6b 54 6f 70 5d 3b 0a 20 20 61 73 73 65 72 74 ckTop];. assert
1cc21 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 ( p->contextStac
1cc22 6b 54 6f 70 3e 3d 30 20 29 3b 0a 20 20 64 62 2d kTop>=0 );. db-
1cc23 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 43 6f >lastRowid = pCo
1cc24 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f 77 69 64 ntext->lastRowid
1cc25 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d ;. p->nChange =
1cc26 20 70 43 6f 6e 74 65 78 74 2d 3e 6e 43 68 61 6e pContext->nChan
1cc27 67 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 ge;. break;.}.#
1cc28 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 endif /* #ifndef
1cc29 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 SQLITE_OMIT_TRI
1cc2a 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 GGER */..#ifndef
1cc2b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1cc2c 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 OINCREMENT./* Op
1cc2d 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50 31 20 code: MemMax P1
1cc2e 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 P2 * * *.**.** S
1cc2f 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 et the value of
1cc30 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 register P1 to t
1cc31 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 69 74 he maximum of it
1cc32 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 0a s current value.
1cc33 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 ** and the value
1cc34 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
1cc35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 .**.** This inst
1cc36 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 20 61 ruction throws a
1cc37 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 6d n error if the m
1cc38 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 6e 6f emory cell is no
1cc39 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 61 t initially.** a
1cc3a 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 n integer..*/.ca
1cc3b 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b 20 se OP_MemMax: {
1cc3c 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20 69 /* in1, i
1cc3d 6e 32 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 n2 */. sqlite3V
1cc3e 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
1cc3f 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 (pIn1);. sqlite
1cc40 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 3VdbeMemIntegeri
1cc41 66 79 28 70 49 6e 32 29 3b 0a 20 20 69 66 28 20 fy(pIn2);. if(
1cc42 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32 2d 3e pIn1->u.i<pIn2->
1cc43 75 2e 69 29 7b 0a 20 20 20 20 70 49 6e 31 2d 3e u.i){. pIn1->
1cc44 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b u.i = pIn2->u.i;
1cc45 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1cc46 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1cc47 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
1cc48 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 ENT */../* Opcod
1cc49 65 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a e: IfPos P1 P2 *
1cc4a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 * *.**.** If th
1cc4b 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 e value of regis
1cc4c 74 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67 ter P1 is 1 or g
1cc4d 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20 reater, jump to
1cc4e 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 P2..**.** It is
1cc4f 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 illegal to use t
1cc50 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1cc51 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 on a register th
1cc52 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 at does.** not c
1cc53 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 ontain an intege
1cc54 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e r. An assertion
1cc55 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 fault will resu
1cc56 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a lt if you try..*
1cc57 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a /.case OP_IfPos:
1cc58 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1cc59 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 p, in1 */. asse
1cc5a 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 rt( pIn1->flags&
1cc5b 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 MEM_Int );. if(
1cc5c 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a pIn1->u.i>0 ){.
1cc5d 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1cc5e 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1cc5f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1cc60 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20 : IfNeg P1 P2 *
1cc61 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 * *.**.** If the
1cc62 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 value of regist
1cc63 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68 er P1 is less th
1cc64 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f an zero, jump to
1cc65 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 P2. .**.** It i
1cc66 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 s illegal to use
1cc67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1cc68 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 n on a register
1cc69 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 that does.** not
1cc6a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 contain an inte
1cc6b 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 ger. An asserti
1cc6c 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 on fault will re
1cc6d 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e sult if you try.
1cc6e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65 .*/.case OP_IfNe
1cc6f 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a g: { /* j
1cc70 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 ump, in1 */. as
1cc71 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1cc72 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 s&MEM_Int );. i
1cc73 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29 f( pIn1->u.i<0 )
1cc74 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1cc75 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1cc76 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cc77 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32 de: IfZero P1 P2
1cc78 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 * * *.**.** If
1cc79 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 the value of reg
1cc7a 69 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63 ister P1 is exac
1cc7b 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50 tly 0, jump to P
1cc7c 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2. .**.** It is
1cc7d 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 illegal to use t
1cc7e 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1cc7f 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 on a register th
1cc80 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 at does.** not c
1cc81 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 ontain an intege
1cc82 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e r. An assertion
1cc83 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 fault will resu
1cc84 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a lt if you try..*
1cc85 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f /.case OP_IfZero
1cc86 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1cc87 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 mp, in1 */. ass
1cc88 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 ert( pIn1->flags
1cc89 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 &MEM_Int );. if
1cc8a 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29 ( pIn1->u.i==0 )
1cc8b 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1cc8c 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1cc8d 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cc8e 64 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32 de: AggStep * P2
1cc8f 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1cc90 45 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70 Execute the step
1cc91 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e function for an
1cc92 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65 aggregate. The
1cc93 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 .** function has
1cc94 20 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 P5 arguments.
1cc95 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
1cc96 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a to the FuncDef.
1cc97 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ** structure tha
1cc98 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 t specifies the
1cc99 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 function. Use r
1cc9a 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73 egister.** P3 as
1cc9b 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 the accumulator
1cc9c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61 ..**.** The P5 a
1cc9d 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b rguments are tak
1cc9e 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 en from register
1cc9f 20 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73 P2 and its.** s
1cca0 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61 uccessors..*/.ca
1cca1 73 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b se OP_AggStep: {
1cca2 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e . int n = pOp->
1cca3 70 35 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d p5;. int i;. M
1cca4 65 6d 20 2a 70 4d 65 6d 2c 20 2a 70 52 65 63 3b em *pMem, *pRec;
1cca5 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
1cca6 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 xt ctx;. sqlite
1cca7 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 3_value **apVal;
1cca8 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 .. assert( n>=0
1cca9 20 29 3b 0a 20 20 70 52 65 63 20 3d 20 26 70 2d );. pRec = &p-
1ccaa 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
1ccab 20 20 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 apVal = p->apA
1ccac 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 61 70 rg;. assert( ap
1ccad 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 20 Val || n==0 );.
1ccae 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 for(i=0; i<n; i
1ccaf 2b 2b 2c 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20 ++, pRec++){.
1ccb0 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 52 65 63 apVal[i] = pRec
1ccb1 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 ;. storeTypeI
1ccb2 6e 66 6f 28 70 52 65 63 2c 20 65 6e 63 6f 64 69 nfo(pRec, encodi
1ccb3 6e 67 29 3b 0a 20 20 7d 0a 20 20 63 74 78 2e 70 ng);. }. ctx.p
1ccb4 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Func = pOp->p4.p
1ccb5 46 75 6e 63 3b 0a 20 20 61 73 73 65 72 74 28 20 Func;. assert(
1ccb6 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 pOp->p3>0 && pOp
1ccb7 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1ccb8 0a 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d . ctx.pMem = pM
1ccb9 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f em = &p->aMem[pO
1ccba 70 2d 3e 70 33 5d 3b 0a 20 20 70 4d 65 6d 2d 3e p->p3];. pMem->
1ccbb 6e 2b 2b 3b 0a 20 20 63 74 78 2e 73 2e 66 6c 61 n++;. ctx.s.fla
1ccbc 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1ccbd 20 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 ctx.s.z = 0;.
1ccbe 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 ctx.s.zMalloc =
1ccbf 30 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c 20 0;. ctx.s.xDel
1ccc0 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20 = 0;. ctx.s.db
1ccc1 3d 20 64 62 3b 0a 20 20 63 74 78 2e 69 73 45 72 = db;. ctx.isEr
1ccc2 72 6f 72 20 3d 20 30 3b 0a 20 20 63 74 78 2e 70 ror = 0;. ctx.p
1ccc3 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 Coll = 0;. if(
1ccc4 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 ctx.pFunc->flags
1ccc5 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e & SQLITE_FUNC_N
1ccc6 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 EEDCOLL ){. a
1ccc7 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f ssert( pOp>p->aO
1ccc8 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 p );. assert(
1ccc9 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d pOp[-1].p4type=
1ccca 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 =P4_COLLSEQ );.
1cccb 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d assert( pOp[-
1cccc 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 1].opcode==OP_Co
1cccd 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78 llSeq );. ctx
1ccce 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d .pColl = pOp[-1]
1cccf 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 .p4.pColl;. }.
1ccd0 20 28 63 74 78 2e 70 46 75 6e 63 2d 3e 78 53 74 (ctx.pFunc->xSt
1ccd1 65 70 29 28 26 63 74 78 2c 20 6e 2c 20 61 70 56 ep)(&ctx, n, apV
1ccd2 61 6c 29 3b 0a 20 20 69 66 28 20 63 74 78 2e 69 al);. if( ctx.i
1ccd3 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 sError ){. sq
1ccd4 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1ccd5 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1ccd6 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 "%s", sqlite3_va
1ccd7 6c 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73 29 lue_text(&ctx.s)
1ccd8 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 78 2e );. rc = ctx.
1ccd9 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 73 isError;. }. s
1ccda 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1ccdb 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 ease(&ctx.s);.
1ccdc 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1ccdd 6f 64 65 3a 20 41 67 67 46 69 6e 61 6c 20 50 31 ode: AggFinal P1
1ccde 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 * P4 *.**.**
1ccdf 20 45 78 65 63 75 74 65 20 74 68 65 20 66 69 6e Execute the fin
1cce0 61 6c 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 alizer function
1cce1 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 for an aggregate
1cce2 2e 20 20 50 31 20 69 73 0a 2a 2a 20 74 68 65 20 . P1 is.** the
1cce3 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 memory location
1cce4 74 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75 that is the accu
1cce5 6d 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20 mulator for the
1cce6 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a aggregate..**.**
1cce7 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 P2 is the numbe
1cce8 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 r of arguments t
1cce9 68 61 74 20 74 68 65 20 73 74 65 70 20 66 75 6e hat the step fun
1ccea 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64 0a ction takes and.
1cceb 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1ccec 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 er to the FuncDe
1cced 66 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 74 f for this funct
1ccee 69 6f 6e 2e 20 20 54 68 65 20 50 32 0a 2a 2a 20 ion. The P2.**
1ccef 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 argument is not
1ccf0 75 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 used by this opc
1ccf1 6f 64 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 ode. It is only
1ccf2 20 74 68 65 72 65 20 74 6f 20 64 69 73 61 6d 62 there to disamb
1ccf3 69 67 75 61 74 65 0a 2a 2a 20 66 75 6e 63 74 69 iguate.** functi
1ccf4 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 74 61 6b ons that can tak
1ccf5 65 20 76 61 72 79 69 6e 67 20 6e 75 6d 62 65 72 e varying number
1ccf6 73 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 s of arguments.
1ccf7 20 54 68 65 0a 2a 2a 20 50 34 20 61 72 67 75 6d The.** P4 argum
1ccf8 65 6e 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64 ent is only need
1ccf9 65 64 20 66 6f 72 20 74 68 65 20 64 65 67 65 6e ed for the degen
1ccfa 65 72 61 74 65 20 63 61 73 65 20 77 68 65 72 65 erate case where
1ccfb 0a 2a 2a 20 74 68 65 20 73 74 65 70 20 66 75 6e .** the step fun
1ccfc 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 70 72 ction was not pr
1ccfd 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2e eviously called.
1ccfe 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 46 .*/.case OP_AggF
1ccff 69 6e 61 6c 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 inal: {. Mem *p
1cd00 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 Mem;. assert( p
1cd01 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d Op->p1>0 && pOp-
1cd02 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p1<=p->nMem );.
1cd03 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 pMem = &p->aMe
1cd04 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 m[pOp->p1];. as
1cd05 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
1cd06 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c gs & ~(MEM_Null|
1cd07 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a MEM_Agg))==0 );.
1cd08 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1cd09 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 4d beMemFinalize(pM
1cd0a 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e em, pOp->p4.pFun
1cd0b 63 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 c);. if( rc==SQ
1cd0c 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20 20 LITE_ERROR ){.
1cd0d 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1cd0e 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1cd0f 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 db, "%s", sqlite
1cd10 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 4d 65 3_value_text(pMe
1cd11 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 m));. }. sqlit
1cd12 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
1cd13 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 6f 64 ding(pMem, encod
1cd14 69 6e 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d ing);. UPDATE_M
1cd15 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4d 65 6d AX_BLOBSIZE(pMem
1cd16 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
1cd17 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4d VdbeMemTooBig(pM
1cd18 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 em) ){. goto
1cd19 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62 too_big;. }. b
1cd1a 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 reak;.}...#if !d
1cd1b 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1cd1c 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64 IT_VACUUM) && !d
1cd1d 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1cd1e 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a 20 4f 70 IT_ATTACH)./* Op
1cd1f 63 6f 64 65 3a 20 56 61 63 75 75 6d 20 2a 20 2a code: Vacuum * *
1cd20 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 56 61 63 * * *.**.** Vac
1cd21 75 75 6d 20 74 68 65 20 65 6e 74 69 72 65 20 64 uum the entire d
1cd22 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 6f atabase. This o
1cd23 70 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65 pcode will cause
1cd24 20 6f 74 68 65 72 20 76 69 72 74 75 61 6c 0a 2a other virtual.*
1cd25 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f 20 62 65 * machines to be
1cd26 20 63 72 65 61 74 65 64 20 61 6e 64 20 72 75 6e created and run
1cd27 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74 20 62 65 . It may not be
1cd28 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
1cd29 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 hin.** a transac
1cd2a 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 tion..*/.case OP
1cd2b 5f 56 61 63 75 75 6d 3a 20 7b 0a 20 20 69 66 28 _Vacuum: {. if(
1cd2c 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1cd2d 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1cd2e 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1cd2f 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 ; . rc = sqlite
1cd30 33 52 75 6e 56 61 63 75 75 6d 28 26 70 2d 3e 7a 3RunVacuum(&p->z
1cd31 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a 20 20 69 ErrMsg, db);. i
1cd32 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1cd33 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 On(db) ) goto ab
1cd34 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1cd35 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 e;. break;.}.#e
1cd36 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e ndif..#if !defin
1cd37 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ed(SQLITE_OMIT_A
1cd38 55 54 4f 56 41 43 55 55 4d 29 0a 2f 2a 20 4f 70 UTOVACUUM)./* Op
1cd39 63 6f 64 65 3a 20 49 6e 63 72 56 61 63 75 75 6d code: IncrVacuum
1cd3a 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1cd3b 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73 69 6e ** Perform a sin
1cd3c 67 6c 65 20 73 74 65 70 20 6f 66 20 74 68 65 20 gle step of the
1cd3d 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
1cd3e 75 6d 20 70 72 6f 63 65 64 75 72 65 20 6f 6e 0a um procedure on.
1cd3f 2a 2a 20 74 68 65 20 50 31 20 64 61 74 61 62 61 ** the P1 databa
1cd40 73 65 2e 20 49 66 20 74 68 65 20 76 61 63 75 75 se. If the vacuu
1cd41 6d 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 m has finished,
1cd42 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 jump to instruct
1cd43 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f 74 68 65 72 ion.** P2. Other
1cd44 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75 wise, fall throu
1cd45 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 gh to the next i
1cd46 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 nstruction..*/.c
1cd47 61 73 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 ase OP_IncrVacuu
1cd48 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a m: { /* j
1cd49 75 6d 70 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a ump */. Btree *
1cd4a 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 pBt;.. assert(
1cd4b 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1cd4c 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b p->p1<db->nDb );
1cd4d 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 . assert( (p->b
1cd4e 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 treeMask & (1<<p
1cd4f 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 Op->p1))!=0 );.
1cd50 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 pBt = db->aDb[p
1cd51 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 72 Op->p1].pBt;. r
1cd52 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1cd53 49 6e 63 72 56 61 63 75 75 6d 28 70 42 74 29 3b IncrVacuum(pBt);
1cd54 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1cd55 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 70 63 E_DONE ){. pc
1cd56 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1cd57 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1cd58 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b OK;. }. break;
1cd59 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 .}.#endif../* Op
1cd5a 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50 31 20 code: Expire P1
1cd5b 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 * * * *.**.** Ca
1cd5c 75 73 65 20 70 72 65 63 6f 6d 70 69 6c 65 64 20 use precompiled
1cd5d 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 62 65 statements to be
1cd5e 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 41 6e come expired. An
1cd5f 20 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 expired stateme
1cd60 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68 nt.** fails with
1cd61 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f an error code o
1cd62 66 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 f SQLITE_SCHEMA
1cd63 69 66 20 69 74 20 69 73 20 65 76 65 72 20 65 78 if it is ever ex
1cd64 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 69 61 20 ecuted .** (via
1cd65 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 29 2e sqlite3_step()).
1cd66 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 20 69 73 .** .** If P1 is
1cd67 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 53 51 4c 0, then all SQL
1cd68 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65 63 6f statements beco
1cd69 6d 65 20 65 78 70 69 72 65 64 2e 20 49 66 20 50 me expired. If P
1cd6a 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 1 is non-zero,.*
1cd6b 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20 * then only the
1cd6c 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 currently execut
1cd6d 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 69 73 ing statement is
1cd6e 20 61 66 66 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 affected. .*/.c
1cd6f 61 73 65 20 4f 50 5f 45 78 70 69 72 65 3a 20 7b ase OP_Expire: {
1cd70 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e 70 31 20 . if( !pOp->p1
1cd71 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
1cd72 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 pirePreparedStat
1cd73 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 65 ements(db);. }e
1cd74 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 lse{. p->expi
1cd75 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 red = 1;. }. b
1cd76 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 reak;.}..#ifndef
1cd77 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1cd78 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 4f 70 63 RED_CACHE./* Opc
1cd79 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 6b 20 50 ode: TableLock P
1cd7a 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1cd7b 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b ** Obtain a lock
1cd7c 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 on a particular
1cd7d 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69 6e 73 table. This ins
1cd7e 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 truction is only
1cd7f 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68 used when.** th
1cd80 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 e shared-cache f
1cd81 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65 eature is enable
1cd82 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 d. .**.** If P1
1cd83 69 73 20 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
1cd84 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e the database in
1cd85 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20 6f sqlite3.aDb[] o
1cd86 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a f the database.*
1cd87 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 6c * on which the l
1cd88 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65 64 2e ock is acquired.
1cd89 20 20 41 20 72 65 61 64 6c 6f 63 6b 20 69 73 20 A readlock is
1cd8a 6f 62 74 61 69 6e 65 64 20 69 66 20 50 33 3d 3d obtained if P3==
1cd8b 30 20 6f 72 0a 2a 2a 20 61 20 77 72 69 74 65 20 0 or.** a write
1cd8c 6c 6f 63 6b 20 69 66 20 50 33 3d 3d 31 2e 0a 2a lock if P3==1..*
1cd8d 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74 61 69 6e 73 *.** P2 contains
1cd8e 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f the root-page o
1cd8f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c f the table to l
1cd90 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 63 6f ock..**.** P4 co
1cd91 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 ntains a pointer
1cd92 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 to the name of
1cd93 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
1cd94 6c 6f 63 6b 65 64 2e 20 54 68 69 73 20 69 73 20 locked. This is
1cd95 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64 20 74 6f 20 only.** used to
1cd96 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f generate an erro
1cd97 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68 65 r message if the
1cd98 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 lock cannot be
1cd99 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 63 61 73 obtained..*/.cas
1cd9a 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20 e OP_TableLock:
1cd9b 7b 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 {. int p1 = pOp
1cd9c 2d 3e 70 31 3b 20 0a 20 20 75 38 20 69 73 57 72 ->p1; . u8 isWr
1cd9d 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 29 70 4f iteLock = (u8)pO
1cd9e 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 p->p3;. assert(
1cd9f 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 64 62 2d p1>=0 && p1<db-
1cda0 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1cda1 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1cda2 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 20 29 3b & (1<<p1))!=0 );
1cda3 0a 20 20 61 73 73 65 72 74 28 20 69 73 57 72 69 . assert( isWri
1cda4 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 teLock==0 || isW
1cda5 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 riteLock==1 );.
1cda6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1cda7 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e eeLockTable(db->
1cda8 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70 aDb[p1].pBt, pOp
1cda9 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63 ->p2, isWriteLoc
1cdaa 6b 29 3b 0a 20 20 69 66 28 20 28 72 63 26 30 78 k);. if( (rc&0x
1cdab 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b FF)==SQLITE_LOCK
1cdac 45 44 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 ED ){. const
1cdad 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 char *z = pOp->p
1cdae 34 2e 7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4.z;. sqlite3
1cdaf 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1cdb0 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 61 74 61 rrMsg, db, "data
1cdb1 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c 6f base table is lo
1cdb2 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 cked: %s", z);.
1cdb3 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1cdb4 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1cdb5 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1cdb6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1cdb7 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1cdb8 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a TABLE./* Opcode:
1cdb9 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 50 34 VBegin * * * P4
1cdba 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 *.**.** P4 may
1cdbb 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 be a pointer to
1cdbc 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 an sqlite3_vtab
1cdbd 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 73 6f structure. If so
1cdbe 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 , call the .** x
1cdbf 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66 6f 72 Begin method for
1cdc0 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a that table..**.
1cdc1 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 68 65 72 ** Also, whether
1cdc2 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 20 73 65 or not P4 is se
1cdc3 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 t, check that th
1cdc4 69 73 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 is is not being
1cdc5 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 called from.** w
1cdc6 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 61 63 6b ithin a callback
1cdc7 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 to a virtual ta
1cdc8 62 6c 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 ble xSync() meth
1cdc9 6f 64 2e 20 49 66 20 69 74 20 69 73 2c 20 74 68 od. If it is, th
1cdca 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 e error.** code
1cdcb 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 53 will be set to S
1cdcc 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f QLITE_LOCKED..*/
1cdcd 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 69 6e 3a .case OP_VBegin:
1cdce 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 {. sqlite3_vta
1cdcf 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e b *pVtab = pOp->
1cdd0 70 34 2e 70 56 74 61 62 3b 0a 20 20 72 63 20 3d p4.pVtab;. rc =
1cdd1 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 sqlite3VtabBegi
1cdd2 6e 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 n(db, pVtab);.
1cdd3 69 66 28 20 70 56 74 61 62 20 29 7b 0a 20 20 20 if( pVtab ){.
1cdd4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1cdd5 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, p->zErrMsg);.
1cdd6 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d p->zErrMsg =
1cdd7 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b pVtab->zErrMsg;
1cdd8 0a 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 . pVtab->zErr
1cdd9 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 Msg = 0;. }. b
1cdda 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1cddb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1cddc 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1cddd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1cdde 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1cddf 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 /* Opcode: VCrea
1cde0 74 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a te P1 * * P4 *.*
1cde1 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e *.** P4 is the n
1cde2 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c ame of a virtual
1cde3 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 table in databa
1cde4 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 se P1. Call the
1cde5 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a xCreate method.*
1cde6 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 * for that table
1cde7 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 ..*/.case OP_VCr
1cde8 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 eate: {. rc = s
1cde9 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 qlite3VtabCallCr
1cdea 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 eate(db, pOp->p1
1cdeb 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d , pOp->p4.z, &p-
1cdec 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 >zErrMsg);. bre
1cded 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1cdee 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1cdef 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1cdf0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1cdf1 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1cdf2 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f Opcode: VDestro
1cdf3 79 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a y P1 * * P4 *.**
1cdf4 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 .** P4 is the na
1cdf5 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 me of a virtual
1cdf6 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 table in databas
1cdf7 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 e P1. Call the
1cdf8 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a xDestroy method.
1cdf9 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 ** of that table
1cdfa 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 ..*/.case OP_VDe
1cdfb 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e stroy: {. p->in
1cdfc 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a VtabMethod = 2;.
1cdfd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 rc = sqlite3Vt
1cdfe 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 abCallDestroy(db
1cdff 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e , pOp->p1, pOp->
1ce00 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 p4.z);. p->inVt
1ce01 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1ce02 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1ce03 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1ce04 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1ce05 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ce06 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1ce07 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 ./* Opcode: VOpe
1ce08 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a n P1 * * P4 *.**
1ce09 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e .** P4 is a poin
1ce0a 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c ter to a virtual
1ce0b 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 table object, a
1ce0c 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 n sqlite3_vtab s
1ce0d 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 tructure..** P1
1ce0e 69 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 is a cursor numb
1ce0f 65 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 er. This opcode
1ce10 20 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20 opens a cursor
1ce11 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a to the virtual.*
1ce12 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 * table and stor
1ce13 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 es that cursor i
1ce14 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 n P1..*/.case OP
1ce15 5f 56 4f 70 65 6e 3a 20 7b 0a 20 20 56 64 62 65 _VOpen: {. Vdbe
1ce16 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 30 Cursor *pCur = 0
1ce17 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 ;. sqlite3_vtab
1ce18 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 _cursor *pVtabCu
1ce19 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20 73 71 6c rsor = 0;.. sql
1ce1a 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1ce1b 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 = pOp->p4.pVtab
1ce1c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 ;. sqlite3_modu
1ce1d 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 28 73 le *pModule = (s
1ce1e 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 qlite3_module *)
1ce1f 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a pVtab->pModule;.
1ce20 0a 20 20 61 73 73 65 72 74 28 70 56 74 61 62 20 . assert(pVtab
1ce21 26 26 20 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 && pModule);. i
1ce22 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1ce23 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 Off(db) ) goto a
1ce24 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1ce25 73 65 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 se;. rc = pModu
1ce26 6c 65 2d 3e 78 4f 70 65 6e 28 70 56 74 61 62 2c le->xOpen(pVtab,
1ce27 20 26 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a &pVtabCursor);.
1ce28 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ce29 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
1ce2a 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 . p->zErrMsg =
1ce2b 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a pVtab->zErrMsg;.
1ce2c 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 pVtab->zErrMsg
1ce2d 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 = 0;. if( sqli
1ce2e 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1ce2f 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1ce30 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 69 66 _to_misuse;. if
1ce31 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 ( SQLITE_OK==rc
1ce32 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 ){. /* Initia
1ce33 6c 69 7a 65 20 73 71 6c 69 74 65 33 5f 76 74 61 lize sqlite3_vta
1ce34 62 5f 63 75 72 73 6f 72 20 62 61 73 65 20 63 6c b_cursor base cl
1ce35 61 73 73 20 2a 2f 0a 20 20 20 20 70 56 74 61 62 ass */. pVtab
1ce36 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 20 3d 20 Cursor->pVtab =
1ce37 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 pVtab;.. /* I
1ce38 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20 63 nitialise vdbe c
1ce39 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a ursor object */.
1ce3a 20 20 20 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63 pCur = alloc
1ce3b 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 ateCursor(p, pOp
1ce3c 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20 30 29 3b ->p1, 0, -1, 0);
1ce3d 0a 20 20 20 20 69 66 28 20 70 43 75 72 20 29 7b . if( pCur ){
1ce3e 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 56 74 . pCur->pVt
1ce3f 61 62 43 75 72 73 6f 72 20 3d 20 70 56 74 61 62 abCursor = pVtab
1ce40 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 43 Cursor;. pC
1ce41 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 56 ur->pModule = pV
1ce42 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1ce43 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d ->pModule;. }
1ce44 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e else{. db->
1ce45 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
1ce46 3b 0a 20 20 20 20 20 20 70 4d 6f 64 75 6c 65 2d ;. pModule-
1ce47 3e 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72 >xClose(pVtabCur
1ce48 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a sor);. }. }.
1ce49 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1ce4a 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1ce4b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1ce4c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1ce4d 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1ce4e 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46 LE./* Opcode: VF
1ce4f 69 6c 74 65 72 20 50 31 20 50 32 20 50 33 20 50 ilter P1 P2 P3 P
1ce50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 4 *.**.** P1 is
1ce51 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 a cursor opened
1ce52 75 73 69 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32 using VOpen. P2
1ce53 20 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 74 is an address t
1ce54 6f 20 6a 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20 o jump to if.**
1ce55 74 68 65 20 66 69 6c 74 65 72 65 64 20 72 65 73 the filtered res
1ce56 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 ult set is empty
1ce57 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69 ..**.** P4 is ei
1ce58 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 ther NULL or a s
1ce59 74 72 69 6e 67 20 74 68 61 74 20 77 61 73 20 67 tring that was g
1ce5a 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 enerated by the
1ce5b 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 xBestIndex.** me
1ce5c 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 thod of the modu
1ce5d 6c 65 2e 20 20 54 68 65 20 69 6e 74 65 72 70 72 le. The interpr
1ce5e 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 50 etation of the P
1ce5f 34 20 73 74 72 69 6e 67 20 69 73 20 6c 65 66 74 4 string is left
1ce60 0a 2a 2a 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c .** to the modul
1ce61 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
1ce62 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ..**.** This opc
1ce63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 ode invokes the
1ce64 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f xFilter method o
1ce65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 n the virtual ta
1ce66 62 6c 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a ble specified.**
1ce67 20 62 79 20 50 31 2e 20 20 54 68 65 20 69 6e 74 by P1. The int
1ce68 65 67 65 72 20 71 75 65 72 79 20 70 6c 61 6e 20 eger query plan
1ce69 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78 46 69 parameter to xFi
1ce6a 6c 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 69 lter is stored i
1ce6b 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 n register.** P3
1ce6c 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 31 20 . Register P3+1
1ce6d 73 74 6f 72 65 73 20 74 68 65 20 61 72 67 63 20 stores the argc
1ce6e 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 parameter to be
1ce6f 70 61 73 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a passed to the.**
1ce70 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e xFilter method.
1ce71 20 52 65 67 69 73 74 65 72 73 20 50 33 2b 32 2e Registers P3+2.
1ce72 2e 50 33 2b 31 2b 61 72 67 63 20 61 72 65 20 74 .P3+1+argc are t
1ce73 68 65 20 61 72 67 63 0a 2a 2a 20 61 64 64 69 74 he argc.** addit
1ce74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73 ional parameters
1ce75 20 77 68 69 63 68 20 61 72 65 20 70 61 73 73 65 which are passe
1ce76 64 20 74 6f 0a 2a 2a 20 78 46 69 6c 74 65 72 20 d to.** xFilter
1ce77 61 73 20 61 72 67 76 2e 20 52 65 67 69 73 74 65 as argv. Registe
1ce78 72 20 50 33 2b 32 20 62 65 63 6f 6d 65 73 20 61 r P3+2 becomes a
1ce79 72 67 76 5b 30 5d 20 77 68 65 6e 20 70 61 73 73 rgv[0] when pass
1ce7a 65 64 20 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a ed to xFilter..*
1ce7b 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 69 73 20 6d *.** A jump is m
1ce7c 61 64 65 20 74 6f 20 50 32 20 69 66 20 74 68 65 ade to P2 if the
1ce7d 20 72 65 73 75 6c 74 20 73 65 74 20 61 66 74 65 result set afte
1ce7e 72 20 66 69 6c 74 65 72 69 6e 67 20 77 6f 75 6c r filtering woul
1ce7f 64 20 62 65 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63 d be empty..*/.c
1ce80 61 73 65 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20 ase OP_VFilter:
1ce81 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 { /* jump */.
1ce82 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e 74 int nArg;. int
1ce83 20 69 51 75 65 72 79 3b 0a 20 20 63 6f 6e 73 74 iQuery;. const
1ce84 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1ce85 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 *pModule;. Mem
1ce86 2a 70 51 75 65 72 79 20 3d 20 26 70 2d 3e 61 4d *pQuery = &p->aM
1ce87 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d em[pOp->p3];. M
1ce88 65 6d 20 2a 70 41 72 67 63 20 3d 20 26 70 51 75 em *pArgc = &pQu
1ce89 65 72 79 5b 31 5d 3b 0a 20 20 73 71 6c 69 74 65 ery[1];. sqlite
1ce8a 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 3_vtab_cursor *p
1ce8b 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71 VtabCursor;. sq
1ce8c 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1ce8d 62 3b 0a 0a 20 20 56 64 62 65 43 75 72 73 6f 72 b;.. VdbeCursor
1ce8e 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 *pCur = p->apCs
1ce8f 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 20 52 r[pOp->p1];.. R
1ce90 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1ce91 70 2d 3e 70 33 2c 20 70 51 75 65 72 79 29 3b 0a p->p3, pQuery);.
1ce92 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1ce93 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 pVtabCursor );.
1ce94 20 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 pVtabCursor = p
1ce95 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 Cur->pVtabCursor
1ce96 3b 0a 20 20 70 56 74 61 62 20 3d 20 70 56 74 61 ;. pVtab = pVta
1ce97 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a bCursor->pVtab;.
1ce98 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 pModule = pVta
1ce99 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f b->pModule;.. /
1ce9a 2a 20 47 72 61 62 20 74 68 65 20 69 6e 64 65 78 * Grab the index
1ce9b 20 6e 75 6d 62 65 72 20 61 6e 64 20 61 72 67 63 number and argc
1ce9c 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 parameters */.
1ce9d 20 61 73 73 65 72 74 28 20 28 70 51 75 65 72 79 assert( (pQuery
1ce9e 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29 ->flags&MEM_Int)
1ce9f 21 3d 30 20 26 26 20 70 41 72 67 63 2d 3e 66 6c !=0 && pArgc->fl
1cea0 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a ags==MEM_Int );.
1cea1 20 20 6e 41 72 67 20 3d 20 28 69 6e 74 29 70 41 nArg = (int)pA
1cea2 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 69 51 75 65 rgc->u.i;. iQue
1cea3 72 79 20 3d 20 28 69 6e 74 29 70 51 75 65 72 79 ry = (int)pQuery
1cea4 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49 6e 76 ->u.i;.. /* Inv
1cea5 6f 6b 65 20 74 68 65 20 78 46 69 6c 74 65 72 20 oke the xFilter
1cea6 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a 20 20 method */. {.
1cea7 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 int res = 0;.
1cea8 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4d 65 int i;. Me
1cea9 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61 m **apArg = p->a
1ceaa 70 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 69 20 pArg;. for(i
1ceab 3d 20 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b 2b = 0; i<nArg; i++
1ceac 29 7b 0a 20 20 20 20 20 20 61 70 41 72 67 5b 69 ){. apArg[i
1cead 5d 20 3d 20 26 70 41 72 67 63 5b 69 2b 31 5d 3b ] = &pArgc[i+1];
1ceae 0a 20 20 20 20 20 20 73 74 6f 72 65 54 79 70 65 . storeType
1ceaf 49 6e 66 6f 28 61 70 41 72 67 5b 69 5d 2c 20 30 Info(apArg[i], 0
1ceb0 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 );. }.. if
1ceb1 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1ceb2 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1ceb3 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1ceb4 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 e;. sqlite3Vt
1ceb5 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 abLock(pVtab);.
1ceb6 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 p->inVtabMeth
1ceb7 6f 64 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d od = 1;. rc =
1ceb8 20 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65 pModule->xFilte
1ceb9 72 28 70 56 74 61 62 43 75 72 73 6f 72 2c 20 69 r(pVtabCursor, i
1ceba 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70 34 2e 7a Query, pOp->p4.z
1cebb 2c 20 6e 41 72 67 2c 20 61 70 41 72 67 29 3b 0a , nArg, apArg);.
1cebc 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 p->inVtabMet
1cebd 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c hod = 0;. sql
1cebe 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1cebf 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 ->zErrMsg);.
1cec0 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 p->zErrMsg = pVt
1cec1 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 ab->zErrMsg;.
1cec2 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 pVtab->zErrMsg
1cec3 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 0;. sqlite3
1cec4 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 VtabUnlock(db, p
1cec5 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20 72 Vtab);. if( r
1cec6 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1cec7 20 20 20 20 20 20 72 65 73 20 3d 20 70 4d 6f 64 res = pMod
1cec8 75 6c 65 2d 3e 78 45 6f 66 28 70 56 74 61 62 43 ule->xEof(pVtabC
1cec9 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 ursor);. }.
1ceca 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1cecb 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f etyOn(db) ) goto
1cecc 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1cecd 73 75 73 65 3b 0a 0a 20 20 20 20 69 66 28 20 72 suse;.. if( r
1cece 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d es ){. pc =
1cecf 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1ced0 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e }. }. pCur->
1ced1 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20 nullRow = 0;..
1ced2 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1ced3 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1ced4 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1ced5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ced6 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1ced7 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 6f 77 ./* Opcode: VRow
1ced8 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a id P1 P2 * * *.*
1ced9 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 74 6f 20 *.** Store into
1ceda 72 65 67 69 73 74 65 72 20 50 32 20 20 74 68 65 register P2 the
1cedb 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 74 68 65 rowid of.** the
1cedc 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 virtual-table t
1cedd 68 61 74 20 74 68 65 20 50 31 20 63 75 72 73 6f hat the P1 curso
1cede 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
1cedf 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 6f ..*/.case OP_VRo
1cee0 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 wid: {
1cee1 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1cee2 6c 65 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 lease */. sqlit
1cee3 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1cee4 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
1cee5 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1cee6 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 . sqlite_int64
1cee7 69 52 6f 77 3b 0a 20 20 56 64 62 65 43 75 72 73 iRow;. VdbeCurs
1cee8 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 or *pCur = p->ap
1cee9 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 Csr[pOp->p1];..
1ceea 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 assert( pCur->p
1ceeb 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 VtabCursor );.
1ceec 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f if( pCur->nullRo
1ceed 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a w ){. break;.
1ceee 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 }. pVtab = pC
1ceef 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d ur->pVtabCursor-
1cef0 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c >pVtab;. pModul
1cef1 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 e = pVtab->pModu
1cef2 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d le;. assert( pM
1cef3 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b odule->xRowid );
1cef4 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1cef5 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1cef6 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1cef7 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70 misuse;. rc = p
1cef8 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 70 Module->xRowid(p
1cef9 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 Cur->pVtabCursor
1cefa 2c 20 26 69 52 6f 77 29 3b 0a 20 20 73 71 6c 69 , &iRow);. sqli
1cefb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1cefc 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e >zErrMsg);. p->
1cefd 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d zErrMsg = pVtab-
1cefe 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 >zErrMsg;. pVta
1ceff 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a b->zErrMsg = 0;.
1cf00 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1cf01 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f etyOn(db) ) goto
1cf02 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1cf03 73 75 73 65 3b 0a 20 20 4d 65 6d 53 65 74 54 79 suse;. MemSetTy
1cf04 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1cf05 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 _Int);. pOut->u
1cf06 2e 69 20 3d 20 69 52 6f 77 3b 0a 20 20 62 72 65 .i = iRow;. bre
1cf07 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1cf08 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1cf09 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1cf0a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1cf0b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1cf0c 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d 6e Opcode: VColumn
1cf0d 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1cf0e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 76 61 .** Store the va
1cf0f 6c 75 65 20 6f 66 20 74 68 65 20 50 32 2d 74 68 lue of the P2-th
1cf10 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68 column of.** th
1cf11 65 20 72 6f 77 20 6f 66 20 74 68 65 20 76 69 72 e row of the vir
1cf12 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 74 20 tual-table that
1cf13 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 73 6f the .** P1 curso
1cf14 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
1cf15 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1cf16 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 3..*/.case OP_VC
1cf17 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 olumn: {. sqlit
1cf18 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1cf19 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
1cf1a 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1cf1b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 . Mem *pDest;.
1cf1c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1cf1d 20 73 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 56 64 sContext;.. Vd
1cf1e 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d beCursor *pCur =
1cf1f 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1cf20 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 1];. assert( pC
1cf21 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 ur->pVtabCursor
1cf22 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1cf23 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
1cf24 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1cf25 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pDest = &p->aMem
1cf26 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 [pOp->p3];. if(
1cf27 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 pCur->nullRow )
1cf28 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1cf29 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 44 65 73 eMemSetNull(pDes
1cf2a 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 t);. break;.
1cf2b 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75 }. pVtab = pCu
1cf2c 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e r->pVtabCursor->
1cf2d 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 pVtab;. pModule
1cf2e 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c = pVtab->pModul
1cf2f 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f e;. assert( pMo
1cf30 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b dule->xColumn );
1cf31 0a 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74 . memset(&sCont
1cf32 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 ext, 0, sizeof(s
1cf33 43 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 20 2f 2a Context));.. /*
1cf34 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c The output cell
1cf35 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 76 may already hav
1cf36 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 e a buffer alloc
1cf37 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 ated. Move. **
1cf38 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 the current cont
1cf39 65 6e 74 73 20 74 6f 20 73 43 6f 6e 74 65 78 74 ents to sContext
1cf3a 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 .s so in case th
1cf3b 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 e user-function
1cf3c 0a 20 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 . ** can use th
1cf3d 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 e already alloca
1cf3e 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 ted buffer inste
1cf3f 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 ad of allocating
1cf40 20 61 20 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 a . ** new one
1cf41 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
1cf42 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 43 6f VdbeMemMove(&sCo
1cf43 6e 74 65 78 74 2e 73 2c 20 70 44 65 73 74 29 3b ntext.s, pDest);
1cf44 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1cf45 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 4d g(&sContext.s, M
1cf46 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 EM_Null);.. if(
1cf47 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1cf48 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1cf49 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1cf4a 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 ;. rc = pModule
1cf4b 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e ->xColumn(pCur->
1cf4c 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 73 43 pVtabCursor, &sC
1cf4d 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 ontext, pOp->p2)
1cf4e 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1cf4f 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1cf50 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 );. p->zErrMsg
1cf51 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 = pVtab->zErrMsg
1cf52 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d ;. pVtab->zErrM
1cf53 73 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f sg = 0;.. /* Co
1cf54 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 py the result of
1cf55 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f the function to
1cf56 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72 the P3 register
1cf57 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 . We. ** do thi
1cf58 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 s regardless of
1cf59 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 whether or not a
1cf5a 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 n error occurred
1cf5b 20 74 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 to ensure any.
1cf5c 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f ** dynamic allo
1cf5d 63 61 74 69 6f 6e 20 69 6e 20 73 43 6f 6e 74 65 cation in sConte
1cf5e 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75 xt.s (a Mem stru
1cf5f 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65 64 ct) is released
1cf60 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
1cf61 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
1cf62 6e 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 ng(&sContext.s,
1cf63 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47 encoding);. REG
1cf64 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1cf65 3e 70 33 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 >p3, pDest);. s
1cf66 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1cf67 65 28 70 44 65 73 74 2c 20 26 73 43 6f 6e 74 65 e(pDest, &sConte
1cf68 78 74 2e 73 29 3b 0a 20 20 55 50 44 41 54 45 5f xt.s);. UPDATE_
1cf69 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 MAX_BLOBSIZE(pDe
1cf6a 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 st);.. if( sqli
1cf6b 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1cf6c 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 ){. goto abor
1cf6d 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1cf6e 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
1cf6f 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 e3VdbeMemTooBig(
1cf70 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f pDest) ){. go
1cf71 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1cf72 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1cf73 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1cf74 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1cf75 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1cf76 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1cf77 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e LE./* Opcode: VN
1cf78 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ext P1 P2 * * *.
1cf79 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 **.** Advance vi
1cf7a 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 rtual table P1 t
1cf7b 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 o the next row i
1cf7c 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 n its result set
1cf7d 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 and.** jump to
1cf7e 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 instruction P2.
1cf7f 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 Or, if the virt
1cf80 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 ual table has re
1cf81 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 ached.** the end
1cf82 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 of its result s
1cf83 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 et, then fall th
1cf84 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 rough to the nex
1cf85 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
1cf86 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a /.case OP_VNext:
1cf87 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a { /* jump */.
1cf88 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1cf89 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 pVtab;. const s
1cf8a 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
1cf8b 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 Module;. int re
1cf8c 73 20 3d 20 30 3b 0a 0a 20 20 56 64 62 65 43 75 s = 0;.. VdbeCu
1cf8d 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e rsor *pCur = p->
1cf8e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1cf8f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1cf90 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 pVtabCursor );.
1cf91 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 if( pCur->nullR
1cf92 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b ow ){. break;
1cf93 0a 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 . }. pVtab = p
1cf94 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 Cur->pVtabCursor
1cf95 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 ->pVtab;. pModu
1cf96 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 le = pVtab->pMod
1cf97 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ule;. assert( p
1cf98 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 20 29 3b Module->xNext );
1cf99 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 .. /* Invoke th
1cf9a 65 20 78 4e 65 78 74 28 29 20 6d 65 74 68 6f 64 e xNext() method
1cf9b 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 of the module.
1cf9c 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 There is no way
1cf9d 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64 for the. ** und
1cf9e 65 72 6c 79 69 6e 67 20 69 6d 70 6c 65 6d 65 6e erlying implemen
1cf9f 74 61 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e tation to return
1cfa0 20 61 6e 20 65 72 72 6f 72 20 69 66 20 6f 6e 65 an error if one
1cfa1 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 20 occurs during.
1cfa2 20 2a 2a 20 78 4e 65 78 74 28 29 2e 20 49 6e 73 ** xNext(). Ins
1cfa3 74 65 61 64 2c 20 69 66 20 61 6e 20 65 72 72 6f tead, if an erro
1cfa4 72 20 6f 63 63 75 72 73 2c 20 74 72 75 65 20 69 r occurs, true i
1cfa5 73 20 72 65 74 75 72 6e 65 64 20 28 69 6e 64 69 s returned (indi
1cfa6 63 61 74 69 6e 67 20 74 68 61 74 20 0a 20 20 2a cating that . *
1cfa7 2a 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 * data is availa
1cfa8 62 6c 65 29 20 61 6e 64 20 74 68 65 20 65 72 72 ble) and the err
1cfa9 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 or code returned
1cfaa 20 77 68 65 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 when xColumn or
1cfab 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72 . ** some other
1cfac 20 6d 65 74 68 6f 64 20 69 73 20 6e 65 78 74 20 method is next
1cfad 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 73 invoked on the s
1cfae 61 76 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c ave virtual tabl
1cfaf 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 e cursor.. */.
1cfb0 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1cfb1 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1cfb2 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1cfb3 73 75 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 56 suse;. sqlite3V
1cfb4 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a tabLock(pVtab);.
1cfb5 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f p->inVtabMetho
1cfb6 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 70 4d d = 1;. rc = pM
1cfb7 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 odule->xNext(pCu
1cfb8 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b r->pVtabCursor);
1cfb9 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 . p->inVtabMeth
1cfba 6f 64 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 od = 0;. sqlite
1cfbb 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1cfbc 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
1cfbd 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a rrMsg = pVtab->z
1cfbe 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d ErrMsg;. pVtab-
1cfbf 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1cfc0 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 sqlite3VtabUnloc
1cfc1 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 k(db, pVtab);.
1cfc2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1cfc3 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 70 K ){. res = p
1cfc4 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 70 43 75 Module->xEof(pCu
1cfc5 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b r->pVtabCursor);
1cfc6 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
1cfc7 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1cfc8 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1cfc9 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 to_misuse;.. if
1cfca 28 20 21 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a ( !res ){. /*
1cfcb 20 49 66 20 74 68 65 72 65 20 69 73 20 64 61 74 If there is dat
1cfcc 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f a, jump to P2 */
1cfcd 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1cfce 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1cfcf 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1cfd0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1cfd1 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1cfd2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1cfd3 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1cfd4 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d 65 Opcode: VRename
1cfd5 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 * * P4 *.**.
1cfd6 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1cfd7 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 er to a virtual
1cfd8 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e table object, an
1cfd9 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 sqlite3_vtab st
1cfda 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73 ructure..** This
1cfdb 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 opcode invokes
1cfdc 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1cfdd 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 g xRename method
1cfde 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 . The value.** i
1cfdf 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 n register P1 is
1cfe0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 7a passed as the z
1cfe1 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 6f Name argument to
1cfe2 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 the xRename met
1cfe3 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f hod..*/.case OP_
1cfe4 56 52 65 6e 61 6d 65 3a 20 7b 0a 20 20 73 71 6c VRename: {. sql
1cfe5 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1cfe6 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 = pOp->p4.pVtab
1cfe7 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 20 3d ;. Mem *pName =
1cfe8 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1cfe9 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 1];. assert( pV
1cfea 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 tab->pModule->xR
1cfeb 65 6e 61 6d 65 20 29 3b 0a 20 20 52 45 47 49 53 ename );. REGIS
1cfec 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1cfed 31 2c 20 70 4e 61 6d 65 29 3b 0a 0a 20 20 53 74 1, pName);.. St
1cfee 72 69 6e 67 69 66 79 28 70 4e 61 6d 65 2c 20 65 ringify(pName, e
1cfef 6e 63 6f 64 69 6e 67 29 3b 0a 0a 20 20 69 66 28 ncoding);.. if(
1cff0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1cff1 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1cff2 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1cff3 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c ;. sqlite3VtabL
1cff4 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 72 63 ock(pVtab);. rc
1cff5 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c = pVtab->pModul
1cff6 65 2d 3e 78 52 65 6e 61 6d 65 28 70 56 74 61 62 e->xRename(pVtab
1cff7 2c 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 , pName->z);. s
1cff8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1cff9 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1cffa 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 p->zErrMsg = pVt
1cffb 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 ab->zErrMsg;. p
1cffc 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 Vtab->zErrMsg =
1cffd 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 0;. sqlite3Vtab
1cffe 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 Unlock(db, pVtab
1cfff 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
1d000 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1d001 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1d002 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61 _misuse;.. brea
1d003 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 k;.}.#endif..#if
1d004 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d005 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1d006 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65 Opcode: VUpdate
1d007 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1d008 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 *.** P4 is a poi
1d009 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 nter to a virtua
1d00a 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 l table object,
1d00b 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 an sqlite3_vtab
1d00c 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 structure..** Th
1d00d 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 is opcode invoke
1d00e 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 s the correspond
1d00f 69 6e 67 20 78 55 70 64 61 74 65 20 6d 65 74 68 ing xUpdate meth
1d010 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a od. P2 values.**
1d011 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 are contiguous
1d012 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61 memory cells sta
1d013 72 74 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70 rting at P3 to p
1d014 61 73 73 20 74 6f 20 74 68 65 20 78 55 70 64 61 ass to the xUpda
1d015 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f te .** invocatio
1d016 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 n. The value in
1d017 72 65 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d register (P3+P2-
1d018 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 1) corresponds t
1d019 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65 o the .** p2th e
1d01a 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 lement of the ar
1d01b 67 76 20 61 72 72 61 79 20 70 61 73 73 65 64 20 gv array passed
1d01c 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a to xUpdate..**.*
1d01d 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 * The xUpdate me
1d01e 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44 thod will do a D
1d01f 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45 ELETE or an INSE
1d020 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 RT or both..** T
1d021 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65 he argv[0] eleme
1d022 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72 65 73 nt (which corres
1d023 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20 ponds to memory
1d024 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74 cell P3).** is t
1d025 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f he rowid of a ro
1d026 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66 w to delete. If
1d027 20 61 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c argv[0] is NULL
1d028 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c then no .** del
1d029 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54 etion occurs. T
1d02a 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65 he argv[1] eleme
1d02b 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 nt is the rowid
1d02c 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 of the new .** r
1d02d 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 ow. This can be
1d02e 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68 NULL to have th
1d02f 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
1d030 73 65 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a select the new .
1d031 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73 ** rowid for its
1d032 65 6c 66 2e 20 20 54 68 65 20 73 75 62 73 65 71 elf. The subseq
1d033 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e uent elements in
1d034 20 74 68 65 20 61 72 72 61 79 20 61 72 65 20 0a the array are .
1d035 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 ** the values of
1d036 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
1d037 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 new row..**.** I
1d038 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 f P2==1 then no
1d039 69 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f 72 insert is perfor
1d03a 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 med. argv[0] is
1d03b 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a the rowid of.**
1d03c 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 a row to delete
1d03d 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 ..**.** P1 is a
1d03e 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66 boolean flag. If
1d03f 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 it is set to tr
1d040 75 65 20 61 6e 64 20 74 68 65 20 78 55 70 64 61 ue and the xUpda
1d041 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 te call.** is su
1d042 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74 ccessful, then t
1d043 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
1d044 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73 d by sqlite3_las
1d045 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 t_insert_rowid()
1d046 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 .** is set to t
1d047 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1d048 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f rowid for the ro
1d049 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e w just inserted.
1d04a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64 .*/.case OP_VUpd
1d04b 61 74 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 ate: {. sqlite3
1d04c 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 _vtab *pVtab = p
1d04d 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 Op->p4.pVtab;.
1d04e 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
1d04f 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 pModule = (sqlit
1d050 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 e3_module *)pVta
1d051 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e b->pModule;. in
1d052 74 20 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 t nArg = pOp->p2
1d053 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1d054 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 >p4type==P4_VTAB
1d055 20 29 3b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c );. if( pModul
1d056 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 20 29 7b e->xUpdate==0 ){
1d057 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1d058 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1d059 67 2c 20 64 62 2c 20 22 72 65 61 64 2d 6f 6e 6c g, db, "read-onl
1d05a 79 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 72 y table");. r
1d05b 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1d05c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
1d05d 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 nt i;. sqlite
1d05e 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 _int64 rowid;.
1d05f 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 Mem **apArg =
1d060 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 4d 65 p->apArg;. Me
1d061 6d 20 2a 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d m *pX = &p->aMem
1d062 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 [pOp->p3];. f
1d063 6f 72 28 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 or(i=0; i<nArg;
1d064 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 i++){. stor
1d065 65 54 79 70 65 49 6e 66 6f 28 70 58 2c 20 30 29 eTypeInfo(pX, 0)
1d066 3b 0a 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d ;. apArg[i]
1d067 20 3d 20 70 58 3b 0a 20 20 20 20 20 20 70 58 2b = pX;. pX+
1d068 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 +;. }. if(
1d069 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1d06a 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1d06b 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1d06c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 ;. sqlite3Vta
1d06d 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 bLock(pVtab);.
1d06e 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e rc = pModule->
1d06f 78 55 70 64 61 74 65 28 70 56 74 61 62 2c 20 6e xUpdate(pVtab, n
1d070 41 72 67 2c 20 61 70 41 72 67 2c 20 26 72 6f 77 Arg, apArg, &row
1d071 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 id);. sqlite3
1d072 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1d073 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
1d074 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e ErrMsg = pVtab->
1d075 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 zErrMsg;. pVt
1d076 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1d077 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 . sqlite3Vtab
1d078 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 Unlock(db, pVtab
1d079 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 );. if( sqlit
1d07a 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1d07b 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1d07c 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 69 to_misuse;. i
1d07d 66 28 20 70 4f 70 2d 3e 70 31 20 26 26 20 72 63 f( pOp->p1 && rc
1d07e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1d07f 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72 assert( nAr
1d080 67 3e 31 20 26 26 20 61 70 41 72 67 5b 30 5d 20 g>1 && apArg[0]
1d081 26 26 20 28 61 70 41 72 67 5b 30 5d 2d 3e 66 6c && (apArg[0]->fl
1d082 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b ags&MEM_Null) );
1d083 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74 52 . db->lastR
1d084 6f 77 69 64 20 3d 20 72 6f 77 69 64 3b 0a 20 20 owid = rowid;.
1d085 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e }. p->nChan
1d086 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 ge++;. }. brea
1d087 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1d088 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1d089 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e ALTABLE */..#ifn
1d08a 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 def SQLITE_OMIT
1d08b 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f _PAGER_PRAGMAS./
1d08c 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f * Opcode: Pageco
1d08d 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a unt P1 P2 * * *.
1d08e 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
1d08f 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f current number o
1d090 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 f pages in datab
1d091 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 ase P1 to memory
1d092 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 cell P2..*/.cas
1d093 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 e OP_Pagecount:
1d094 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1d095 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1d096 2a 2f 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f */. int p1 = pO
1d097 70 2d 3e 70 31 3b 20 0a 20 20 69 6e 74 20 6e 50 p->p1; . int nP
1d098 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 age;. Pager *pP
1d099 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 ager = sqlite3Bt
1d09a 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 reePager(db->aDb
1d09b 5b 70 31 5d 2e 70 42 74 29 3b 0a 0a 20 20 72 63 [p1].pBt);.. rc
1d09c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 = sqlite3PagerP
1d09d 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
1d09e 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 &nPage);. if(
1d09f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1d0a0 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 . pOut->flags
1d0a1 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1d0a2 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 6e 50 61 67 pOut->u.i = nPag
1d0a3 65 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a e;. }. break;.
1d0a4 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
1d0a5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1d0a6 41 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 ACE./* Opcode: T
1d0a7 72 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a race * * * P4 *.
1d0a8 2a 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67 **.** If tracing
1d0a9 20 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20 is enabled (by
1d0aa 74 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 the sqlite3_trac
1d0ab 65 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20 e()) interface,
1d0ac 74 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d then.** the UTF-
1d0ad 38 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 8 string contain
1d0ae 65 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74 ed in P4 is emit
1d0af 74 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65 ted on the trace
1d0b0 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 callback..*/.ca
1d0b1 73 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 20 se OP_Trace: {.
1d0b2 20 63 68 61 72 20 2a 7a 54 72 61 63 65 20 3d 20 char *zTrace =
1d0b3 28 70 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70 (pOp->p4.z ? pOp
1d0b4 2d 3e 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c ->p4.z : p->zSql
1d0b5 29 3b 0a 20 20 69 66 28 20 7a 54 72 61 63 65 20 );. if( zTrace
1d0b6 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 ){. if( db->x
1d0b7 54 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 64 Trace ){. d
1d0b8 62 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 b->xTrace(db->pT
1d0b9 72 61 63 65 41 72 67 2c 20 7a 54 72 61 63 65 29 raceArg, zTrace)
1d0ba 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 ;. }.#ifdef S
1d0bb 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
1d0bc 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 if( (db->flags &
1d0bd 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 SQLITE_SqlTrace
1d0be 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )!=0 ){. sq
1d0bf 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
1d0c0 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 25 73 5c ("SQL-trace: %s\
1d0c1 6e 22 2c 20 7a 54 72 61 63 65 29 3b 0a 20 20 20 n", zTrace);.
1d0c2 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }.#endif /* SQL
1d0c3 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d ITE_DEBUG */. }
1d0c4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1d0c5 69 66 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 if.../* Opcode:
1d0c6 4e 6f 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a Noop * * * * *.*
1d0c7 2a 0a 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e *.** Do nothing.
1d0c8 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 This instructi
1d0c9 6f 6e 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 on is often usef
1d0ca 75 6c 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 ul as a jump.**
1d0cb 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a destination..*/.
1d0cc 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 /*.** The magic
1d0cd 45 78 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 Explain opcode a
1d0ce 72 65 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 re only inserted
1d0cf 20 77 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 when explain==2
1d0d0 20 28 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f (which.** is to
1d0d1 20 73 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 say when the EX
1d0d2 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e PLAIN QUERY PLAN
1d0d3 20 73 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e syntax is used.
1d0d4 29 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 ).** This opcode
1d0d5 20 72 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 records informa
1d0d6 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 tion from the op
1d0d7 74 69 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 timizer. It is
1d0d8 74 68 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 the.** the same
1d0d9 61 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 as a no-op. Thi
1d0da 73 20 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 s opcodesnever a
1d0db 70 70 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c ppears in a real
1d0dc 20 56 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a VM program..*/.
1d0dd 64 65 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 default: {
1d0de 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 /* This is r
1d0df 65 61 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e eally OP_Noop an
1d0e0 64 20 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a d OP_Explain */.
1d0e1 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a break;.}../***
1d0e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1d0e7 65 20 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 e cases of the s
1d0e8 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 witch statement
1d0e9 61 62 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 above this line
1d0ea 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e should all be in
1d0eb 64 65 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 dented.** by 6 s
1d0ec 70 61 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 paces. But the
1d0ed 6c 65 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 left-most 6 spac
1d0ee 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d es have been rem
1d0ef 6f 76 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 oved to improve
1d0f0 74 68 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 the.** readabili
1d0f1 74 79 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 ty. From this p
1d0f2 6f 69 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 oint on down, th
1d0f3 65 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 e normal indenta
1d0f4 74 69 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a tion rules are.*
1d0f5 2a 20 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a * restored..****
1d0f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d *********/. }
1d0fb 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 ..#ifdef VDBE_PR
1d0fc 4f 46 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 OFILE. {.
1d0fd 20 20 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 u64 elapsed =
1d0fe 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
1d0ff 2d 20 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 - start;. p
1d100 4f 70 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c Op->cycles += el
1d101 61 70 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 apsed;. pOp
1d102 2d 3e 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 ->cnt++;.#if 0.
1d103 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 fprintf(s
1d104 74 64 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 tdout, "%10llu "
1d105 2c 20 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 , elapsed);.
1d106 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 sqlite3VdbeP
1d107 72 69 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f rintOp(stdout, o
1d108 72 69 67 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f rigPc, &p->aOp[o
1d109 72 69 67 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a rigPc]);.#endif.
1d10a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
1d10b 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 /* The followi
1d10c 6e 67 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 ng code adds not
1d10d 68 69 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75 hing to the actu
1d10e 61 6c 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 al functionality
1d10f 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 . ** of the p
1d110 72 6f 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f rogram. It is o
1d111 6e 6c 79 20 68 65 72 65 20 66 6f 72 20 74 65 73 nly here for tes
1d112 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 ting and debuggi
1d113 6e 67 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 ng.. ** On th
1d114 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74 e other hand, it
1d115 20 64 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63 does burn CPU c
1d116 79 63 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65 ycles every time
1d117 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 through. **
1d118 74 68 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f the evaluator lo
1d119 6f 70 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c op. So we can l
1d11a 65 61 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e eave it out when
1d11b 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e NDEBUG is defin
1d11c 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 ed.. */.#ifnd
1d11d 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 61 73 ef NDEBUG. as
1d11e 73 65 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 sert( pc>=-1 &&
1d11f 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 pc<p->nOp );..#i
1d120 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1d121 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 G. if( p->tra
1d122 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ce ){. if(
1d123 72 63 21 3d 30 20 29 20 66 70 72 69 6e 74 66 28 rc!=0 ) fprintf(
1d124 70 2d 3e 74 72 61 63 65 2c 22 72 63 3d 25 64 5c p->trace,"rc=%d\
1d125 6e 22 2c 72 63 29 3b 0a 20 20 20 20 20 20 69 66 n",rc);. if
1d126 28 20 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f ( opProperty & O
1d127 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c PFLG_OUT2_PREREL
1d128 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 EASE ){.
1d129 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d registerTrace(p-
1d12a 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c >trace, pOp->p2,
1d12b 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a pOut);. }.
1d12c 20 20 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 if( opProp
1d12d 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 erty & OPFLG_OUT
1d12e 33 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 3 ){. reg
1d12f 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 isterTrace(p->tr
1d130 61 63 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f ace, pOp->p3, pO
1d131 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ut);. }.
1d132 20 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 }.#endif /* SQ
1d133 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 LITE_DEBUG */.#e
1d134 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 ndif /* NDEBUG
1d135 2a 2f 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 */. } /* The e
1d136 6e 64 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b nd of the for(;;
1d137 29 20 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 ) loop the loops
1d138 20 74 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 through opcodes
1d139 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 */.. /* If we
1d13a 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 reach this point
1d13b 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
1d13c 65 78 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e execution is fin
1d13d 69 73 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 ished with. **
1d13e 61 6e 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 an error of some
1d13f 20 6b 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 kind.. */.vdbe
1d140 5f 65 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 _error_halt:. a
1d141 73 73 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70 ssert( rc );. p
1d142 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c ->rc = rc;. sql
1d143 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b ite3VdbeHalt(p);
1d144 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1d145 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 E_IOERR_NOMEM )
1d146 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1d147 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c = 1;. rc = SQL
1d148 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a ITE_ERROR;.. /*
1d149 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c This is the onl
1d14a 79 20 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 y way out of thi
1d14b 73 20 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 s procedure. We
1d14c 20 68 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 have to. ** re
1d14d 6c 65 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 lease the mutexe
1d14e 73 20 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 s on btrees that
1d14f 20 77 65 72 65 20 61 63 71 75 69 72 65 64 20 61 were acquired a
1d150 74 20 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 t the. ** top.
1d151 2a 2f 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a */.vdbe_return:.
1d152 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 sqlite3BtreeMu
1d153 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 texArrayLeave(&p
1d154 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 ->aMutex);. ret
1d155 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 urn rc;.. /* Ju
1d156 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 mp to here if a
1d157 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c string or blob l
1d158 61 72 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 arger than SQLIT
1d159 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a E_MAX_LENGTH. *
1d15a 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 * is encountered
1d15b 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a .. */.too_big:.
1d15c 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1d15d 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1d15e 64 62 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 db, "string or b
1d15f 6c 6f 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 lob too big");.
1d160 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f rc = SQLITE_TOO
1d161 42 49 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 BIG;. goto vdbe
1d162 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 _error_halt;..
1d163 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 /* Jump to here
1d164 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 if a malloc() fa
1d165 69 6c 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d ils.. */.no_mem
1d166 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 :. db->mallocFa
1d167 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 iled = 1;. sqli
1d168 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1d169 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f >zErrMsg, db, "o
1d16a 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a ut of memory");.
1d16b 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
1d16c 4d 45 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 MEM;. goto vdbe
1d16d 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 _error_halt;..
1d16e 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 /* Jump to here
1d16f 66 6f 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 for an SQLITE_MI
1d170 53 55 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f SUSE error.. */
1d171 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 .abort_due_to_mi
1d172 73 75 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c suse:. rc = SQL
1d173 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a ITE_MISUSE;. /*
1d174 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 Fall thru into
1d175 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1d176 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 or */.. /* Jump
1d177 20 74 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 to here for any
1d178 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 other kind of f
1d179 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 atal error. The
1d17a 20 22 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 "rc" variable.
1d17b 20 2a 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 ** should hold
1d17c 74 68 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 the error number
1d17d 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 .. */.abort_due
1d17e 5f 74 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 _to_error:. ass
1d17f 65 72 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d ert( p->zErrMsg=
1d180 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e =0 );. if( db->
1d181 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 mallocFailed ) r
1d182 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1d183 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
1d184 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 TE_IOERR_NOMEM )
1d185 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 {. sqlite3Set
1d186 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1d187 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 sg, db, "%s", sq
1d188 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 lite3ErrStr(rc))
1d189 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 ;. }. goto vdb
1d18a 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 e_error_halt;..
1d18b 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 /* Jump to here
1d18c 20 69 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f if the sqlite3_
1d18d 69 6e 74 65 72 72 75 70 74 28 29 20 41 50 49 20 interrupt() API
1d18e 73 65 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 sets the interru
1d18f 70 74 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 pt. ** flag..
1d190 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f */.abort_due_to_
1d191 69 6e 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 interrupt:. ass
1d192 65 72 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e ert( db->u1.isIn
1d193 74 65 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 terrupted );. r
1d194 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 c = SQLITE_INTER
1d195 52 55 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 RUPT;. p->rc =
1d196 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 rc;. sqlite3Set
1d197 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1d198 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 sg, db, "%s", sq
1d199 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 lite3ErrStr(rc))
1d19a 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 ;. goto vdbe_er
1d19b 72 6f 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a ror_halt;.}../**
1d19c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
1d19d 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a of vdbe.c *****
1d19e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d19f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
1d1a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
1d1a2 69 6e 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 in file vdbeblob
1d1a3 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1d1a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
1d1a6 2a 2a 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a ** 2007 May 1.**
1d1a7 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1d1a8 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1d1a9 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1d1aa 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1d1ab 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1d1ac 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1d1ad 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1d1ae 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1d1af 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1d1b0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1d1b1 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1d1b2 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1d1b3 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1d1b4 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1d1b5 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1d1b6 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1d1b7 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1d1b8 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1d1b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
1d1bd 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1d1be 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 tains code used
1d1bf 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 to implement inc
1d1c0 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f remental BLOB I/
1d1c1 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 O..**.** $Id: vd
1d1c2 62 65 62 6c 6f 62 2e 63 2c 76 20 31 2e 33 31 20 beblob.c,v 1.31
1d1c3 32 30 30 39 2f 30 33 2f 32 34 20 31 35 3a 30 38 2009/03/24 15:08
1d1c4 3a 31 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :10 drh Exp $.*/
1d1c5 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1d1c6 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
1d1c7 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c ./*.** Valid sql
1d1c8 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c ite3_blob* handl
1d1c9 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 es point to Incr
1d1ca 62 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e blob structures.
1d1cb 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
1d1cc 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 ct Incrblob Incr
1d1cd 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 blob;.struct Inc
1d1ce 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c rblob {. int fl
1d1cf 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
1d1d0 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c /* Copy of "fl
1d1d1 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20 73 ags" passed to s
1d1d2 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
1d1d3 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 () */. int nByt
1d1d4 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1d1d5 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 /* Size of open
1d1d6 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a blob, in bytes *
1d1d7 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b /. int iOffset;
1d1d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
1d1d9 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c yte offset of bl
1d1da 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74 ob in cursor dat
1d1db 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 a */. BtCursor
1d1dc 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f *pCsr; /
1d1dd 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e * Cursor pointin
1d1de 67 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f g at blob row */
1d1df 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
1d1e0 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 *pStmt; /* St
1d1e1 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 atement holding
1d1e2 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 cursor open */.
1d1e3 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
1d1e4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1d1e5 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 associated datab
1d1e6 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ase */.};../*.**
1d1e7 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e Open a blob han
1d1e8 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 dle..*/.SQLITE_A
1d1e9 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1d1ea 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 lob_open(. sqli
1d1eb 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20 te3* db,
1d1ec 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1d1ed 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
1d1ee 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1d1ef 7a 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 zDb, /* T
1d1f0 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 he attached data
1d1f1 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 base containing
1d1f2 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f the blob */. co
1d1f3 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
1d1f4 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 , /* The tab
1d1f5 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 le containing th
1d1f6 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 e blob */. cons
1d1f7 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c t char *zColumn,
1d1f8 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d /* The colum
1d1f9 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 n containing the
1d1fa 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 blob */. sqlit
1d1fb 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 e_int64 iRow,
1d1fc 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f /* The row co
1d1fd 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f ntaining the glo
1d1fe 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 b */. int flags
1d1ff 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1d200 2a 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 * True -> read/w
1d201 72 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c rite access, fal
1d202 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 se -> read-only
1d203 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f */. sqlite3_blo
1d204 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 b **ppBlob /*
1d205 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 Handle for acces
1d206 73 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65 sing the blob re
1d207 74 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 turned here */.)
1d208 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 {. int nAttempt
1d209 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c = 0;. int iCol
1d20a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1d20b 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c /* Index of zCol
1d20c 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 umn in row-recor
1d20d 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 d */.. /* This
1d20e 56 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65 VDBE program see
1d20f 6b 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f ks a btree curso
1d210 72 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 r to the identif
1d211 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 ied . ** db/tab
1d212 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 le/row entry. Th
1d213 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 e reason for usi
1d214 6e 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 ng a vdbe progra
1d215 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f m instead. ** o
1d216 66 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74 f writing code t
1d217 6f 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65 o use the b-tree
1d218 20 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20 layer directly
1d219 69 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a is that the. **
1d21a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69 vdbe program wi
1d21b 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 ll take advantag
1d21c 65 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 e of the various
1d21d 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 transaction,.
1d21e 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 ** locking and e
1d21f 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e rror handling in
1d220 66 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69 frastructure bui
1d221 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 lt into the vdbe
1d222 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 .. **. ** Afte
1d223 72 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 r seeking the cu
1d224 72 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65 rsor, the vdbe e
1d225 78 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 xecutes an OP_Re
1d226 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f sultRow.. ** Co
1d227 64 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 de external to t
1d228 68 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f he Vdbe then "bo
1d229 72 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65 rrows" the b-tre
1d22a 65 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a e cursor and. *
1d22b 2a 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70 * uses it to imp
1d22c 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f lement the blob_
1d22d 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 read(), blob_wri
1d22e 74 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 te() and . ** b
1d22f 6c 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63 lob_bytes() func
1d230 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
1d231 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f The sqlite3_blo
1d232 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 b_close() functi
1d233 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 on finalizes the
1d234 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 vdbe program,.
1d235 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73 ** which closes
1d236 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73 the b-tree curs
1d237 6f 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 or and (possibly
1d238 29 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 ) commits the .
1d239 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ** transaction.
1d23a 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 . */. static c
1d23b 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 onst VdbeOpList
1d23c 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 openBlob[] = {.
1d23d 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 {OP_Transacti
1d23e 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 on, 0, 0, 0},
1d23f 20 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20 /* 0: Start a
1d240 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 transaction */.
1d241 20 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f {OP_VerifyCoo
1d242 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 kie, 0, 0, 0},
1d243 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 /* 1: Check th
1d244 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 e schema cookie
1d245 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f */.. /* One o
1d246 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
1d247 74 77 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 two instructions
1d248 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 is replaced by
1d249 61 6e 0a 20 20 20 20 2a 2a 20 4f 50 5f 4e 6f 6f an. ** OP_Noo
1d24a 70 20 62 65 66 6f 72 65 20 65 78 65 63 74 69 6f p before exectio
1d24b 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7b 4f n.. */. {O
1d24c 50 5f 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 P_OpenRead, 0, 0
1d24d 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 , 0}, /*
1d24e 32 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 2: Open cursor 0
1d24f 20 66 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a for reading */.
1d250 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 {OP_OpenWrit
1d251 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 e, 0, 0, 0},
1d252 20 20 20 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75 /* 3: Open cu
1d253 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 2f rsor 0 for read/
1d254 77 72 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f write */.. {O
1d255 50 5f 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 P_Variable, 1, 1
1d256 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 , 1}, /*
1d257 34 3a 20 50 75 73 68 20 74 68 65 20 72 6f 77 69 4: Push the rowi
1d258 64 20 74 6f 20 74 68 65 20 73 74 61 63 6b 20 2a d to the stack *
1d259 2f 0a 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 /. {OP_NotExi
1d25a 73 74 73 2c 20 30 2c 20 38 2c 20 31 7d 2c 20 20 sts, 0, 8, 1},
1d25b 20 20 20 20 20 2f 2a 20 35 3a 20 53 65 65 6b 20 /* 5: Seek
1d25c 74 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 the cursor */.
1d25d 20 20 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c {OP_Column, 0,
1d25e 20 30 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 0, 1},
1d25f 20 2f 2a 20 36 20 20 2a 2f 0a 20 20 20 20 7b 4f /* 6 */. {O
1d260 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 P_ResultRow, 1,
1d261 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 0, 0}, /*
1d262 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 7 */. {OP_Cl
1d263 6f 73 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 ose, 0, 0, 0},
1d264 20 20 20 20 20 20 20 20 20 2f 2a 20 38 20 20 2a /* 8 *
1d265 2f 0a 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 /. {OP_Halt,
1d266 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 0, 0, 0},
1d267 20 20 20 20 20 2f 2a 20 39 20 2a 2f 0a 20 20 7d /* 9 */. }
1d268 3b 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 ;.. Vdbe *v = 0
1d269 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
1d26a 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 7a ITE_OK;. char z
1d26b 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 7a 45 72 Err[128];.. zEr
1d26c 72 5b 30 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 r[0] = 0;. sqli
1d26d 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1d26e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 6f db->mutex);. do
1d26f 20 7b 0a 20 20 20 20 50 61 72 73 65 20 73 50 61 {. Parse sPa
1d270 72 73 65 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a rse;. Table *
1d271 70 54 61 62 3b 0a 0a 20 20 20 20 6d 65 6d 73 65 pTab;.. memse
1d272 74 28 26 73 50 61 72 73 65 2c 20 30 2c 20 73 69 t(&sParse, 0, si
1d273 7a 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20 zeof(Parse));.
1d274 20 20 73 50 61 72 73 65 2e 64 62 20 3d 20 64 62 sParse.db = db
1d275 3b 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ;.. if( sqlit
1d276 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1d277 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1d278 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e mutex_leave(db->
1d279 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65 mutex);. re
1d27a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
1d27b 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 SE;. }.. s
1d27c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1d27d 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 61 All(db);. pTa
1d27e 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 b = sqlite3Locat
1d27f 65 54 61 62 6c 65 28 26 73 50 61 72 73 65 2c 20 eTable(&sParse,
1d280 30 2c 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0, zTable, zDb);
1d281 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26 . if( pTab &&
1d282 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
1d283 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d ){. pTab =
1d284 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0;. sqlite
1d285 33 45 72 72 6f 72 4d 73 67 28 26 73 50 61 72 73 3ErrorMsg(&sPars
1d286 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 e, "cannot open
1d287 76 69 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 virtual table: %
1d288 73 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 s", zTable);.
1d289 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
1d28a 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 E_OMIT_VIEW.
1d28b 69 66 28 20 70 54 61 62 20 26 26 20 70 54 61 62 if( pTab && pTab
1d28c 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 ->pSelect ){.
1d28d 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 pTab = 0;.
1d28e 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1d28f 73 67 28 26 73 50 61 72 73 65 2c 20 22 63 61 6e sg(&sParse, "can
1d290 6e 6f 74 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 not open view: %
1d291 73 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 s", zTable);.
1d292 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 }.#endif. if
1d293 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20 ( !pTab ){.
1d294 20 69 66 28 20 73 50 61 72 73 65 2e 7a 45 72 72 if( sParse.zErr
1d295 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Msg ){. s
1d296 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1d297 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 sizeof(zErr), zE
1d298 72 72 2c 20 22 25 73 22 2c 20 73 50 61 72 73 65 rr, "%s", sParse
1d299 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 .zErrMsg);.
1d29a 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1d29b 44 62 46 72 65 65 28 64 62 2c 20 73 50 61 72 73 DbFree(db, sPars
1d29c 65 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 e.zErrMsg);.
1d29d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1d29e 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 ROR;. (void
1d29f 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 )sqlite3SafetyOf
1d2a0 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c f(db);. sql
1d2a1 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c ite3BtreeLeaveAl
1d2a2 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 l(db);. got
1d2a3 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b o blob_open_out;
1d2a4 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e . }.. /* N
1d2a5 6f 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66 ow search pTab f
1d2a6 6f 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c or the exact col
1d2a7 75 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 umn. */. for(
1d2a8 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 iCol=0; iCol < p
1d2a9 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b Tab->nCol; iCol+
1d2aa 2b 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 +) {. if( s
1d2ab 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 qlite3StrICmp(pT
1d2ac 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a ab->aCol[iCol].z
1d2ad 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d Name, zColumn)==
1d2ae 30 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 0 ){. bre
1d2af 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1d2b0 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d }. if( iCol==
1d2b1 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 pTab->nCol ){.
1d2b2 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1d2b3 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 intf(sizeof(zErr
1d2b4 29 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 ), zErr, "no suc
1d2b5 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 h column: \"%s\"
1d2b6 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 ", zColumn);.
1d2b7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1d2b8 52 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 RROR;. (voi
1d2b9 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
1d2ba 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 ff(db);. sq
1d2bb 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 lite3BtreeLeaveA
1d2bc 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f ll(db);. go
1d2bd 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 to blob_open_out
1d2be 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1d2bf 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 If the value is
1d2c0 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 being opened for
1d2c1 20 77 72 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 writing, check
1d2c2 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 that the. **
1d2c3 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e column is not in
1d2c4 64 65 78 65 64 2e 20 49 74 20 69 73 20 61 67 61 dexed. It is aga
1d2c5 69 6e 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 inst the rules t
1d2c6 6f 20 6f 70 65 6e 20 61 6e 0a 20 20 20 20 2a 2a o open an. **
1d2c7 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 indexed column
1d2c8 66 6f 72 20 77 72 69 74 69 6e 67 2e 0a 20 20 20 for writing..
1d2c9 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 67 */. if( flag
1d2ca 73 20 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 s ){. Index
1d2cb 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 66 6f *pIdx;. fo
1d2cc 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e r(pIdx=pTab->pIn
1d2cd 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d dex; pIdx; pIdx=
1d2ce 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 pIdx->pNext){.
1d2cf 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 int j;.
1d2d0 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c for(j=0; j<
1d2d1 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a pIdx->nColumn; j
1d2d2 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ++){. i
1d2d3 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d f( pIdx->aiColum
1d2d4 6e 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 n[j]==iCol ){.
1d2d5 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1d2d6 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
1d2d7 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20 f(zErr), zErr,.
1d2d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d2d9 20 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e "can
1d2da 6e 6f 74 20 6f 70 65 6e 20 69 6e 64 65 78 65 64 not open indexed
1d2db 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 column for writ
1d2dc 69 6e 67 22 29 3b 0a 20 20 20 20 20 20 20 20 20 ing");.
1d2dd 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1d2de 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 RROR;.
1d2df 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1d2e0 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 afetyOff(db);.
1d2e1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1d2e2 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 3BtreeLeaveAll(d
1d2e3 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 b);.
1d2e4 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f goto blob_open_o
1d2e5 75 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ut;. }.
1d2e6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1d2e7 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d }. }.. v =
1d2e8 20 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 sqlite3VdbeCrea
1d2e9 74 65 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 te(db);. if(
1d2ea 76 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 v ){. int i
1d2eb 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
1d2ec 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 maToIndex(db, pT
1d2ed 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 ab->pSchema);.
1d2ee 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1d2ef 64 64 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 ddOpList(v, size
1d2f0 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a of(openBlob)/siz
1d2f1 65 6f 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c eof(VdbeOpList),
1d2f2 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a 0a 20 20 20 openBlob);..
1d2f3 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 /* Configure
1d2f4 74 68 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 the OP_Transacti
1d2f5 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 on */. sqli
1d2f6 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 te3VdbeChangeP1(
1d2f7 76 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 v, 0, iDb);.
1d2f8 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1d2f9 6e 67 65 50 32 28 76 2c 20 30 2c 20 28 66 6c 61 ngeP2(v, 0, (fla
1d2fa 67 73 20 3f 20 31 20 3a 20 30 29 29 3b 0a 0a 20 gs ? 1 : 0));..
1d2fb 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
1d2fc 65 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43 e the OP_VerifyC
1d2fd 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 ookie */. s
1d2fe 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1d2ff 50 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 P1(v, 1, iDb);.
1d300 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1d301 43 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 ChangeP2(v, 1, p
1d302 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 Tab->pSchema->sc
1d303 68 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 hema_cookie);..
1d304 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 /* Make sur
1d305 65 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c e a mutex is hel
1d306 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 d on the table t
1d307 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f o be accessed */
1d308 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d309 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 beUsesBtree(v, i
1d30a 44 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 Db); .. /*
1d30b 52 65 6d 6f 76 65 20 65 69 74 68 65 72 20 74 68 Remove either th
1d30c 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 6f e OP_OpenWrite o
1d30d 72 20 4f 70 65 6e 52 65 61 64 2e 20 53 65 74 20 r OpenRead. Set
1d30e 74 68 65 20 50 32 20 0a 20 20 20 20 20 20 2a 2a the P2 . **
1d30f 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 parameter of th
1d310 65 20 6f 74 68 65 72 20 74 6f 20 70 54 61 62 2d e other to pTab-
1d311 3e 74 6e 75 6d 2e 20 0a 20 20 20 20 20 20 2a 2f >tnum. . */
1d312 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d313 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 beChangeToNoop(v
1d314 2c 20 28 66 6c 61 67 73 20 3f 20 32 20 3a 20 33 , (flags ? 2 : 3
1d315 29 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c ), 1);. sql
1d316 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 ite3VdbeChangeP2
1d317 28 76 2c 20 28 66 6c 61 67 73 20 3f 20 33 20 3a (v, (flags ? 3 :
1d318 20 32 29 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29 2), pTab->tnum)
1d319 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d31a 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 28 dbeChangeP3(v, (
1d31b 66 6c 61 67 73 20 3f 20 33 20 3a 20 32 29 2c 20 flags ? 3 : 2),
1d31c 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 iDb);.. /*
1d31d 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 6e 75 Configure the nu
1d31e 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e mber of columns.
1d31f 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 Configure the c
1d320 75 72 73 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a ursor to. *
1d321 2a 20 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 * think that the
1d322 20 74 61 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d table has one m
1d323 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 ore column than
1d324 69 74 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 it really.
1d325 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 ** does. An OP_C
1d326 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 olumn to retriev
1d327 65 20 74 68 69 73 20 69 6d 61 67 69 6e 61 72 79 e this imaginary
1d328 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 column will.
1d329 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 ** always ret
1d32a 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e urn an SQL NULL.
1d32b 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 20 This is useful
1d32c 62 65 63 61 75 73 65 20 69 74 20 6d 65 61 6e 73 because it means
1d32d 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 63 61 6e . ** we can
1d32e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d invoke OP_Colum
1d32f 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 n to fill in the
1d330 20 76 64 62 65 20 63 75 72 73 6f 72 73 20 74 79 vdbe cursors ty
1d331 70 65 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 pe . ** and
1d332 20 6f 66 66 73 65 74 20 63 61 63 68 65 20 77 69 offset cache wi
1d333 74 68 6f 75 74 20 63 61 75 73 69 6e 67 20 61 6e thout causing an
1d334 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 y IO.. */.
1d335 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1d336 43 68 61 6e 67 65 50 34 28 76 2c 20 66 6c 61 67 ChangeP4(v, flag
1d337 73 20 3f 20 33 20 3a 20 32 2c 20 53 51 4c 49 54 s ? 3 : 2, SQLIT
1d338 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 E_INT_TO_PTR(pTa
1d339 62 2d 3e 6e 43 6f 6c 2b 31 29 2c 20 50 34 5f 49 b->nCol+1), P4_I
1d33a 4e 54 33 32 29 3b 0a 20 20 20 20 20 20 73 71 6c NT32);. sql
1d33b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 ite3VdbeChangeP2
1d33c 28 76 2c 20 36 2c 20 70 54 61 62 2d 3e 6e 43 6f (v, 6, pTab->nCo
1d33d 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64 l);. if( !d
1d33e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1d33f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1d340 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 e3VdbeMakeReady(
1d341 76 2c 20 31 2c 20 31 2c 20 31 2c 20 30 29 3b 0a v, 1, 1, 1, 0);.
1d342 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1d343 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 . sqlite3Btr
1d344 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
1d345 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d346 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 SafetyOff(db);.
1d347 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1d348 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c E_OK || db->mall
1d349 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
1d34a 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e goto blob_open
1d34b 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 _out;. }..
1d34c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e sqlite3_bind_in
1d34d 74 36 34 28 28 73 71 6c 69 74 65 33 5f 73 74 6d t64((sqlite3_stm
1d34e 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f 77 29 3b t *)v, 1, iRow);
1d34f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1d350 33 5f 73 74 65 70 28 28 73 71 6c 69 74 65 33 5f 3_step((sqlite3_
1d351 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 69 stmt *)v);. i
1d352 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f f( rc!=SQLITE_RO
1d353 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 74 74 65 W ){. nAtte
1d354 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 mpt++;. rc
1d355 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 = sqlite3_finali
1d356 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ze((sqlite3_stmt
1d357 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 73 71 6c *)v);. sql
1d358 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
1d359 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 zeof(zErr), zErr
1d35a 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 , sqlite3_errmsg
1d35b 28 64 62 29 29 3b 0a 20 20 20 20 20 20 76 20 3d (db));. v =
1d35c 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 0;. }. } wh
1d35d 69 6c 65 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 ile( nAttempt<5
1d35e 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 && rc==SQLITE_SC
1d35f 48 45 4d 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 HEMA );.. if( r
1d360 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b c==SQLITE_ROW ){
1d361 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d . /* The row-
1d362 72 65 63 6f 72 64 20 68 61 73 20 62 65 65 6e 20 record has been
1d363 6f 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 opened successfu
1d364 6c 6c 79 2e 20 43 68 65 63 6b 20 74 68 61 74 20 lly. Check that
1d365 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d the. ** colum
1d366 6e 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f n in question co
1d367 6e 74 61 69 6e 73 20 74 65 78 74 20 6f 72 20 61 ntains text or a
1d368 20 62 6c 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e blob. If it con
1d369 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 tains. ** tex
1d36a 74 2c 20 69 74 20 69 73 20 75 70 20 74 6f 20 74 t, it is up to t
1d36b 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 he caller to get
1d36c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 the encoding ri
1d36d 67 68 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ght.. */.
1d36e 49 6e 63 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b Incrblob *pBlob;
1d36f 0a 20 20 20 20 75 33 32 20 74 79 70 65 20 3d 20 . u32 type =
1d370 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 v->apCsr[0]->aTy
1d371 70 65 5b 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 pe[iCol];.. i
1d372 66 28 20 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 f( type<12 ){.
1d373 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1d374 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 intf(sizeof(zErr
1d375 29 2c 20 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 ), zErr, "cannot
1d376 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74 open value of t
1d377 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20 ype %s",.
1d378 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c type==0?"null
1d379 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c ": type==7?"real
1d37a 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20 ": "integer".
1d37b 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d );. rc =
1d37c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1d37d 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1d37e 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
1d37f 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 pBlob = (Incr
1d380 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62 blob *)sqlite3Db
1d381 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
1d382 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29 izeof(Incrblob))
1d383 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 ;. if( db->ma
1d384 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1d385 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1d386 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 e(db, pBlob);.
1d387 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1d388 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 en_out;. }.
1d389 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d pBlob->flags =
1d38a 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f flags;. pBlo
1d38b 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70 b->pCsr = v->ap
1d38c 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b Csr[0]->pCursor;
1d38d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1d38e 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 6c eEnterCursor(pBl
1d38f 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 ob->pCsr);. s
1d390 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65 qlite3BtreeCache
1d391 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e Overflow(pBlob->
1d392 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 pCsr);. sqlit
1d393 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
1d394 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b or(pBlob->pCsr);
1d395 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d . pBlob->pStm
1d396 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d t = (sqlite3_stm
1d397 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f 62 t *)v;. pBlob
1d398 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e 61 ->iOffset = v->a
1d399 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 pCsr[0]->aOffset
1d39a 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f [iCol];. pBlo
1d39b 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 b->nByte = sqlit
1d39c 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1d39d 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 70 Len(type);. p
1d39e 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 Blob->db = db;.
1d39f 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 71 *ppBlob = (sq
1d3a0 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 6c lite3_blob *)pBl
1d3a1 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ob;. rc = SQL
1d3a2 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 ITE_OK;. }else
1d3a3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1d3a4 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 K ){. sqlite3
1d3a5 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
1d3a6 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 6e (zErr), zErr, "n
1d3a7 6f 20 73 75 63 68 20 72 6f 77 69 64 3a 20 25 6c o such rowid: %l
1d3a8 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 ld", iRow);.
1d3a9 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1d3aa 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 R;. }..blob_ope
1d3ab 6e 5f 6f 75 74 3a 0a 20 20 7a 45 72 72 5b 73 69 n_out:. zErr[si
1d3ac 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d 20 zeof(zErr)-1] =
1d3ad 27 5c 30 27 3b 0a 20 20 69 66 28 20 76 20 26 26 '\0';. if( v &&
1d3ae 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 (rc!=SQLITE_OK
1d3af 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
1d3b0 6c 65 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 led) ){. sqli
1d3b1 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 te3VdbeFinalize(
1d3b2 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 v);. }. sqlite
1d3b3 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 3Error(db, rc, (
1d3b4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 30 3a rc==SQLITE_OK?0:
1d3b5 7a 45 72 72 29 29 3b 0a 20 20 72 63 20 3d 20 73 zErr));. rc = s
1d3b6 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 qlite3ApiExit(db
1d3b7 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 , rc);. sqlite3
1d3b8 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d _mutex_leave(db-
1d3b9 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 >mutex);. retur
1d3ba 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
1d3bb 6c 6f 73 65 20 61 20 62 6c 6f 62 20 68 61 6e 64 lose a blob hand
1d3bc 6c 65 20 74 68 61 74 20 77 61 73 20 70 72 65 76 le that was prev
1d3bd 69 6f 75 73 6c 79 20 63 72 65 61 74 65 64 20 75 iously created u
1d3be 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f sing.** sqlite3_
1d3bf 62 6c 6f 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a blob_open()..*/.
1d3c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1d3c1 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 qlite3_blob_clos
1d3c2 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a e(sqlite3_blob *
1d3c3 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c pBlob){. Incrbl
1d3c4 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f ob *p = (Incrblo
1d3c5 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 b *)pBlob;. int
1d3c6 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a rc;. sqlite3 *
1d3c7 64 62 3b 0a 0a 20 20 64 62 20 3d 20 70 2d 3e 64 db;.. db = p->d
1d3c8 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 b;. sqlite3_mut
1d3c9 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 ex_enter(db->mut
1d3ca 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ex);. rc = sqli
1d3cb 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e te3_finalize(p->
1d3cc 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 pStmt);. sqlite
1d3cd 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 3DbFree(db, p);.
1d3ce 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1d3cf 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 leave(db->mutex)
1d3d0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1d3d1 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 ../*.** Perform
1d3d2 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 a read or write
1d3d3 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62 operation on a b
1d3d4 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e lob.*/.static in
1d3d5 74 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 t blobReadWrite(
1d3d6 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 . sqlite3_blob
1d3d7 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20 *pBlob, . void
1d3d8 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 *z, . int n, .
1d3d9 20 69 6e 74 20 69 4f 66 66 73 65 74 2c 20 0a 20 int iOffset, .
1d3da 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 28 42 74 int (*xCall)(Bt
1d3db 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33 Cursor*, u32, u3
1d3dc 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 2, void*).){. i
1d3dd 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f nt rc;. Incrblo
1d3de 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 b *p = (Incrblob
1d3df 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65 *)pBlob;. Vdbe
1d3e0 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a *v;. sqlite3 *
1d3e1 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 0a 0a 20 db = p->db; ..
1d3e2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1d3e3 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b nter(db->mutex);
1d3e4 0a 20 20 76 20 3d 20 28 56 64 62 65 2a 29 70 2d . v = (Vdbe*)p-
1d3e5 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 66 28 20 6e >pStmt;.. if( n
1d3e6 3c 30 20 7c 7c 20 69 4f 66 66 73 65 74 3c 30 20 <0 || iOffset<0
1d3e7 7c 7c 20 28 69 4f 66 66 73 65 74 2b 6e 29 3e 70 || (iOffset+n)>p
1d3e8 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 2f ->nByte ){. /
1d3e9 2a 20 52 65 71 75 65 73 74 20 69 73 20 6f 75 74 * Request is out
1d3ea 20 6f 66 20 72 61 6e 67 65 2e 20 52 65 74 75 72 of range. Retur
1d3eb 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20 65 72 n a transient er
1d3ec 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d ror. */. rc =
1d3ed 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1d3ee 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
1d3ef 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 db, SQLITE_ERROR
1d3f0 2c 20 30 29 3b 0a 20 20 7d 20 65 6c 73 65 20 69 , 0);. } else i
1d3f1 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2f f( v==0 ){. /
1d3f2 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f * If there is no
1d3f3 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c statement handl
1d3f4 65 2c 20 74 68 65 6e 20 74 68 65 20 62 6c 6f 62 e, then the blob
1d3f5 2d 68 61 6e 64 6c 65 20 68 61 73 0a 20 20 20 20 -handle has.
1d3f6 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ** already been
1d3f7 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 52 65 74 invalidated. Ret
1d3f8 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 urn SQLITE_ABORT
1d3f9 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 in this case..
1d3fa 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 */. rc = S
1d3fb 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d QLITE_ABORT;. }
1d3fc 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 61 6c else{. /* Cal
1d3fd 6c 20 65 69 74 68 65 72 20 42 74 72 65 65 44 61 l either BtreeDa
1d3fe 74 61 28 29 20 6f 72 20 42 74 72 65 65 50 75 74 ta() or BtreePut
1d3ff 44 61 74 61 28 29 2e 20 49 66 20 53 51 4c 49 54 Data(). If SQLIT
1d400 45 5f 41 42 4f 52 54 20 69 73 0a 20 20 20 20 2a E_ABORT is. *
1d401 2a 20 72 65 74 75 72 6e 65 64 2c 20 63 6c 65 61 * returned, clea
1d402 6e 2d 75 70 20 74 68 65 20 73 74 61 74 65 6d 65 n-up the stateme
1d403 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 2a nt handle.. *
1d404 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 /. assert( db
1d405 20 3d 3d 20 76 2d 3e 64 62 20 29 3b 0a 20 20 20 == v->db );.
1d406 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1d407 65 72 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 erCursor(p->pCsr
1d408 29 3b 0a 20 20 20 20 72 63 20 3d 20 78 43 61 6c );. rc = xCal
1d409 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f 66 66 73 l(p->pCsr, iOffs
1d40a 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74 2c 20 6e et+p->iOffset, n
1d40b 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , z);. sqlite
1d40c 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 3BtreeLeaveCurso
1d40d 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 r(p->pCsr);.
1d40e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 41 if( rc==SQLITE_A
1d40f 42 4f 52 54 20 29 7b 0a 20 20 20 20 20 20 73 71 BORT ){. sq
1d410 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a lite3VdbeFinaliz
1d411 65 28 76 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 e(v);. p->p
1d412 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65 Stmt = 0;. }e
1d413 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 65 lse{. db->e
1d414 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 rrCode = rc;.
1d415 20 20 20 76 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 v->rc = rc;.
1d416 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 }. }. rc =
1d417 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 sqlite3ApiExit(d
1d418 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 b, rc);. sqlite
1d419 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 3_mutex_leave(db
1d41a 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 ->mutex);. retu
1d41b 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1d41c 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 Read data from a
1d41d 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f blob handle..*/
1d41e 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1d41f 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 sqlite3_blob_rea
1d420 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a d(sqlite3_blob *
1d421 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a 7a 2c 20 pBlob, void *z,
1d422 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 int n, int iOffs
1d423 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c et){. return bl
1d424 6f 62 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f obReadWrite(pBlo
1d425 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74 b, z, n, iOffset
1d426 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 , sqlite3BtreeDa
1d427 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 ta);.}../*.** Wr
1d428 69 74 65 20 64 61 74 61 20 74 6f 20 61 20 62 6c ite data to a bl
1d429 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 ob handle..*/.SQ
1d42a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1d42b 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 ite3_blob_write(
1d42c 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 sqlite3_blob *pB
1d42d 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 lob, const void
1d42e 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 *z, int n, int i
1d42f 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 Offset){. retur
1d430 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 n blobReadWrite(
1d431 70 42 6c 6f 62 2c 20 28 76 6f 69 64 20 2a 29 7a pBlob, (void *)z
1d432 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 , n, iOffset, sq
1d433 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74 lite3BtreePutDat
1d434 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 a);.}../*.** Que
1d435 72 79 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 ry a blob handle
1d436 20 66 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 for the size of
1d437 20 74 68 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a the data..**.**
1d438 20 54 68 65 20 49 6e 63 72 62 6c 6f 62 2e 6e 42 The Incrblob.nB
1d439 79 74 65 20 66 69 65 6c 64 20 69 73 20 66 69 78 yte field is fix
1d43a 65 64 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74 ed for the lifet
1d43b 69 6d 65 20 6f 66 20 74 68 65 20 49 6e 63 72 62 ime of the Incrb
1d43c 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f 20 6d 75 74 lob.** so no mut
1d43d 65 78 20 69 73 20 72 65 71 75 69 72 65 64 20 66 ex is required f
1d43e 6f 72 20 61 63 63 65 73 73 2e 0a 2a 2f 0a 53 51 or access..*/.SQ
1d43f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1d440 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 ite3_blob_bytes(
1d441 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 sqlite3_blob *pB
1d442 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 lob){. Incrblob
1d443 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 *p = (Incrblob
1d444 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 65 74 75 72 *)pBlob;. retur
1d445 6e 20 70 2d 3e 6e 42 79 74 65 3b 0a 7d 0a 0a 23 n p->nByte;.}..#
1d446 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 endif /* #ifndef
1d447 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
1d448 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a RBLOB */../*****
1d449 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1d44a 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a vdbeblob.c ****
1d44b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d44c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d44d 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1d44e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1d44f 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a file journal.c *
1d450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d451 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d452 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1d453 32 30 30 37 20 41 75 67 75 73 74 20 32 32 0a 2a 2007 August 22.*
1d454 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
1d455 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
1d456 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
1d457 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
1d458 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
1d459 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
1d45a 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
1d45b 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
1d45c 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1d45d 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1d45e 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1d45f 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
1d460 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
1d461 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
1d462 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
1d463 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
1d464 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
1d465 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
1d466 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d467 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d468 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d469 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1d46a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6a 6f .** @(#) $Id: jo
1d46b 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 39 20 32 30 urnal.c,v 1.9 20
1d46c 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36 3a 32 09/01/20 17:06:2
1d46d 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 7 danielk1977 Ex
1d46e 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 p $.*/..#ifdef S
1d46f 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
1d470 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a MIC_WRITE../*.**
1d471 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
1d472 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c 20 ments a special
1d473 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f kind of sqlite3_
1d474 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64 file object used
1d475 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f .** by SQLite to
1d476 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20 create journal
1d477 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f files if the ato
1d478 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 mic-write optimi
1d479 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 zation.** is ena
1d47a 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 bled..**.** The
1d47b 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 61 72 distinctive char
1d47c 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 74 68 acteristic of th
1d47d 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 is sqlite3_file
1d47e 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 is that the.** a
1d47f 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 69 ctual on disk fi
1d480 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 6c 61 le is created la
1d481 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 66 zily. When the f
1d482 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 0a ile is created,.
1d483 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73 70 ** the caller sp
1d484 65 63 69 66 69 65 73 20 61 20 62 75 66 66 65 72 ecifies a buffer
1d485 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e 2d size for an in-
1d486 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f memory buffer to
1d487 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 73 .** be used to s
1d488 65 72 76 69 63 65 20 72 65 61 64 28 29 20 61 6e ervice read() an
1d489 64 20 77 72 69 74 65 28 29 20 72 65 71 75 65 73 d write() reques
1d48a 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20 66 ts. The actual f
1d48b 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 ile.** on disk i
1d48c 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72 s not created or
1d48d 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 6c populated until
1d48e 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 either:.**.**
1d48f 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 1) The in-memor
1d490 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e y representation
1d491 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 65 grows too large
1d492 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 74 for the allocat
1d493 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 66 ed .** buff
1d494 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 54 er, or.** 2) T
1d495 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 he sqlite3Journa
1d496 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 lCreate() functi
1d497 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f on is called..*/
1d498 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 ..../*.** A Jour
1d499 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69 nalFile object i
1d49a 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 s a subclass of
1d49b 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65 sqlite3_file use
1d49c 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 d by.** as an op
1d49d 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 en file handle f
1d49e 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 or journal files
1d49f 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72 ..*/.struct Jour
1d4a0 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 nalFile {. sqli
1d4a1 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
1d4a2 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49 pMethod; /* I
1d4a3 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f /O methods on jo
1d4a4 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 urnal files */.
1d4a5 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20 int nBuf;
1d4a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4a7 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66 /* Size of zBuf
1d4a8 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 [] in bytes */.
1d4a9 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20 char *zBuf;
1d4aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4ab 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66 /* Space to buf
1d4ac 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74 fer journal writ
1d4ad 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a es */. int iSiz
1d4ae 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1d4af 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e /* Amoun
1d4b0 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72 t of zBuf[] curr
1d4b1 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 ently used */.
1d4b2 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 int flags;
1d4b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4b4 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a /* xOpen flags *
1d4b5 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 /. sqlite3_vfs
1d4b6 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 *pVfs;
1d4b7 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c /* The "real
1d4b8 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 " underlying VFS
1d4b9 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
1d4ba 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 le *pReal;
1d4bb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 /* The "re
1d4bc 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 al" underlying f
1d4bd 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
1d4be 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1d4bf 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 zJournal;
1d4c0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
1d4c1 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1d4c2 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 */.};.typedef st
1d4c3 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 ruct JournalFile
1d4c4 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f JournalFile;../
1d4c5 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20 *.** If it does
1d4c6 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 not already exis
1d4c7 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70 ts, create and p
1d4c8 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64 opulate the on-d
1d4c9 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 isk file .** for
1d4ca 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a JournalFile p..
1d4cb 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 */.static int cr
1d4cc 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c eateFile(Journal
1d4cd 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 File *p){. int
1d4ce 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1d4cf 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 if( !p->pReal
1d4d0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
1d4d1 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71 ile *pReal = (sq
1d4d2 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b lite3_file *)&p[
1d4d3 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 1];. rc = sql
1d4d4 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 ite3OsOpen(p->pV
1d4d5 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c fs, p->zJournal,
1d4d6 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 pReal, p->flags
1d4d7 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
1d4d8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1d4d9 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20 p->pReal =
1d4da 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 pReal;. if(
1d4db 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20 p->iSize>0 ){.
1d4dc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 2d assert(p-
1d4dd 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 >iSize<=p->nBuf)
1d4de 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
1d4df 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d qlite3OsWrite(p-
1d4e0 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c >pReal, p->zBuf,
1d4e1 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20 p->iSize, 0);.
1d4e2 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1d4e3 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1d4e4 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 ./*.** Close the
1d4e5 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1d4e6 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 int jrnlClose(s
1d4e7 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1d4e8 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c d){. JournalFil
1d4e9 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 e *p = (JournalF
1d4ea 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 ile *)pJfd;. if
1d4eb 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 ( p->pReal ){.
1d4ec 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
1d4ed 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a (p->pReal);. }.
1d4ee 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
1d4ef 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 ->zBuf);. retur
1d4f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1d4f1 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 /*.** Read data
1d4f2 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a from the file..*
1d4f3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e /.static int jrn
1d4f4 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 lRead(. sqlite3
1d4f5 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 _file *pJfd,
1d4f6 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
1d4f7 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 ile from which t
1d4f8 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 o read */. void
1d4f9 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 *zBuf,
1d4fa 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 /* Put the re
1d4fb 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
1d4fc 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1d4fd 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1d4fe 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
1d4ff 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e d */. sqlite_in
1d500 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1d501 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
1d502 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
1d503 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 .){. int rc = S
1d504 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 QLITE_OK;. Jour
1d505 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1d506 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1d507 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c ;. if( p->pReal
1d508 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1d509 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52 ite3OsRead(p->pR
1d50a 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c eal, zBuf, iAmt,
1d50b 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65 iOfst);. }else
1d50c 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 73 74 if( (iAmt+iOfst
1d50d 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 )>p->iSize ){.
1d50e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
1d50f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a ERR_SHORT_READ;.
1d510 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d }else{. mem
1d511 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 cpy(zBuf, &p->zB
1d512 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29 uf[iOfst], iAmt)
1d513 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1d514 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 c;.}../*.** Writ
1d515 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 e data to the fi
1d516 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
1d517 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 73 t jrnlWrite(. s
1d518 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1d519 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 d, /* The jou
1d51a 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77 rnal file into w
1d51b 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a 2f hich to write */
1d51c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
1d51d 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b Buf, /* Tak
1d51e 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 e data to be wri
1d51f 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a tten from here *
1d520 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 /. int iAmt,
1d521 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1d522 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f mber of bytes to
1d523 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 write */. sqli
1d524 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 te_int64 iOfst
1d525 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 74 /* Begin writ
1d526 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 ing at this offs
1d527 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 et into the file
1d528 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 */.){. int rc
1d529 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a = SQLITE_OK;. J
1d52a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 ournalFile *p =
1d52b 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1d52c 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 Jfd;. if( !p->p
1d52d 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b 69 Real && (iOfst+i
1d52e 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a Amt)>p->nBuf ){.
1d52f 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 46 rc = createF
1d530 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 ile(p);. }. if
1d531 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1d532 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 52 ){. if( p->pR
1d533 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 eal ){. rc
1d534 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
1d535 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c (p->pReal, zBuf,
1d536 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 iAmt, iOfst);.
1d537 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1d538 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b memcpy(&p->zBuf[
1d539 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41 iOfst], zBuf, iA
1d53a 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 mt);. if( p
1d53b 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69 ->iSize<(iOfst+i
1d53c 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 Amt) ){.
1d53d 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f 66 73 p->iSize = (iOfs
1d53e 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d t+iAmt);. }
1d53f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1d540 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1d541 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 Truncate the fi
1d542 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
1d543 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 t jrnlTruncate(s
1d544 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1d545 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 d, sqlite_int64
1d546 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 size){. int rc
1d547 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a = SQLITE_OK;. J
1d548 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 ournalFile *p =
1d549 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1d54a 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 Jfd;. if( p->pR
1d54b 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 eal ){. rc =
1d54c 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
1d54d 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65 e(p->pReal, size
1d54e 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 );. }else if( s
1d54f 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a ize<p->iSize ){.
1d550 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73 p->iSize = s
1d551 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ize;. }. retur
1d552 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
1d553 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f ync the file..*/
1d554 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c .static int jrnl
1d555 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c Sync(sqlite3_fil
1d556 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61 e *pJfd, int fla
1d557 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 gs){. int rc;.
1d558 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 JournalFile *p
1d559 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a = (JournalFile *
1d55a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e )pJfd;. if( p->
1d55b 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 pReal ){. rc
1d55c 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
1d55d 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73 29 p->pReal, flags)
1d55e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
1d55f 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1d560 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1d561 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 }../*.** Query t
1d562 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 he size of the f
1d563 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f ile in bytes..*/
1d564 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c .static int jrnl
1d565 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 FileSize(sqlite3
1d566 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c _file *pJfd, sql
1d567 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 ite_int64 *pSize
1d568 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1d569 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1d56a 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1d56b 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1d56c 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 . if( p->pReal
1d56d 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1d56e 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d te3OsFileSize(p-
1d56f 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a >pReal, pSize);.
1d570 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53 }else{. *pS
1d571 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e ize = (sqlite_in
1d572 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 t64) p->iSize;.
1d573 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1d574 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f }../*.** Table o
1d575 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f f methods for Jo
1d576 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 74 65 urnalFile sqlite
1d577 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 3_file object..*
1d578 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
1d579 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
1d57a 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 ds JournalFileMe
1d57b 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 thods = {. 1,
1d57c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 /* iV
1d57d 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c ersion */. jrnl
1d57e 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 Close, /* xC
1d57f 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 lose */. jrnlRe
1d580 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61 ad, /* xRea
1d581 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65 d */. jrnlWrite
1d582 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 , /* xWrite
1d583 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74 */. jrnlTruncat
1d584 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 e, /* xTruncate
1d585 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 */. jrnlSync,
1d586 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f /* xSync */
1d587 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c . jrnlFileSize,
1d588 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a /* xFileSize *
1d589 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 /. 0,
1d58a 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 /* xLock */.
1d58b 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1d58c 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 /* xUnlock */.
1d58d 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1d58e 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
1d58f 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 Lock */. 0,
1d590 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c /* xFil
1d591 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c eControl */. 0,
1d592 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d593 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 xSectorSize */.
1d594 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
1d595 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 /* xDeviceCharac
1d596 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a teristics */.};.
1d597 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a ./* .** Open a j
1d598 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a ournal file..*/.
1d599 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1d59a 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 nt sqlite3Journa
1d59b 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 lOpen(. sqlite3
1d59c 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
1d59d 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 /* The VFS t
1d59e 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 61 6c o use for actual
1d59f 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 file I/O */. c
1d5a0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
1d5a1 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d , /* Nam
1d5a2 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
1d5a3 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 file */. sqlit
1d5a4 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1d5a5 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f /* Preallo
1d5a6 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c cated, blank fil
1d5a7 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e e handle */. in
1d5a8 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
1d5a9 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e /* Open
1d5aa 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 ing flags */. i
1d5ab 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 20 20 nt nBuf
1d5ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
1d5ad 65 73 20 62 75 66 66 65 72 65 64 20 62 65 66 6f es buffered befo
1d5ae 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 re opening the f
1d5af 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 ile */.){. Jour
1d5b0 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1d5b1 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1d5b2 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c ;. memset(p, 0,
1d5b3 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
1d5b4 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69 66 ize(pVfs));. if
1d5b5 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20 20 ( nBuf>0 ){.
1d5b6 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 p->zBuf = sqlite
1d5b7 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 3MallocZero(nBuf
1d5b8 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 7a );. if( !p->z
1d5b9 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65 74 Buf ){. ret
1d5ba 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1d5bb 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1d5bc 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
1d5bd 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
1d5be 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61 zName, pJfd, fla
1d5bf 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d gs, 0);. }. p-
1d5c0 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72 >pMethod = &Jour
1d5c1 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a nalFileMethods;.
1d5c2 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66 p->nBuf = nBuf
1d5c3 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66 ;. p->flags = f
1d5c4 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 lags;. p->zJour
1d5c5 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 nal = zName;. p
1d5c6 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 ->pVfs = pVfs;.
1d5c7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1d5c8 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 K;.}../*.** If t
1d5c9 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 6f he argument p po
1d5ca 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61 ints to a Journa
1d5cb 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c lFile structure,
1d5cc 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 and the underly
1d5cd 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 ing.** file has
1d5ce 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65 not yet been cre
1d5cf 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 ated, create it
1d5d0 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 now..*/.SQLITE_P
1d5d1 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1d5d2 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 e3JournalCreate(
1d5d3 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29 sqlite3_file *p)
1d5d4 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74 68 {. if( p->pMeth
1d5d5 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c ods!=&JournalFil
1d5d6 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 eMethods ){.
1d5d7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1d5d8 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 ;. }. return c
1d5d9 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e reateFile((Journ
1d5da 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a alFile *)p);.}..
1d5db 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 /* .** Return th
1d5dc 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1d5dd 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 s required to st
1d5de 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c ore a JournalFil
1d5df 65 20 74 68 61 74 20 75 73 65 73 20 76 66 73 0a e that uses vfs.
1d5e0 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 ** pVfs to creat
1d5e1 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 e the underlying
1d5e2 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a on-disk files..
1d5e3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1d5e4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 E int sqlite3Jou
1d5e5 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 rnalSize(sqlite3
1d5e6 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 72 _vfs *pVfs){. r
1d5e7 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f eturn (pVfs->szO
1d5e8 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 sFile+sizeof(Jou
1d5e9 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 rnalFile));.}.#e
1d5ea 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
1d5eb 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 ***** End of jou
1d5ec 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a rnal.c *********
1d5ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d5ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d5ef 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1d5f0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1d5f1 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a memjournal.c **
1d5f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d5f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d5f4 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
1d5f5 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a October 7.**.**
1d5f6 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1d5f7 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1d5f8 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1d5f9 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1d5fa 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1d5fb 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1d5fc 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1d5fd 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1d5fe 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1d5ff 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1d600 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1d601 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1d602 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1d603 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1d604 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1d605 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1d606 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1d607 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d60a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d60b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1d60c 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1d60d 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69 ns code use to i
1d60e 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d mplement an in-m
1d60f 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a emory rollback j
1d610 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 ournal..** The i
1d611 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 n-memory rollbac
1d612 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 k journal is use
1d613 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 61 d to journal tra
1d614 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a nsactions for.**
1d615 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 74 61 ":memory:" data
1d616 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e 20 74 bases and when t
1d617 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d he journal_mode=
1d618 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 69 73 MEMORY pragma is
1d619 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 used..**.** @(#
1d61a 29 20 24 49 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 ) $Id: memjourna
1d61b 6c 2e 63 2c 76 20 31 2e 38 20 32 30 30 38 2f 31 l.c,v 1.8 2008/1
1d61c 32 2f 32 30 20 30 32 3a 31 34 3a 34 30 20 64 72 2/20 02:14:40 dr
1d61d 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 h Exp $.*/../* F
1d61e 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 orward reference
1d61f 73 20 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 s to internal st
1d620 72 75 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 ructures */.type
1d621 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f def struct MemJo
1d622 75 72 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c urnal MemJournal
1d623 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
1d624 20 46 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 FilePoint FileP
1d625 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 oint;.typedef st
1d626 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 ruct FileChunk F
1d627 69 6c 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 ileChunk;../* Sp
1d628 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ace to hold the
1d629 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
1d62a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e is allocated in
1d62b 20 69 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a increments of.*
1d62c 2a 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 * this many byte
1d62d 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f s..*/.#define JO
1d62e 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 URNAL_CHUNKSIZE
1d62f 31 30 32 34 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 1024../* Macro t
1d630 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d o find the minim
1d631 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69 um of two numeri
1d632 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 c values..*/.#if
1d633 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e ndef MIN.# defin
1d634 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c e MIN(x,y) ((x)<
1d635 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e (y)?(x):(y)).#en
1d636 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 dif../*.** The r
1d637 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
1d638 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 is composed of a
1d639 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
1d63a 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 these structures
1d63b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 ..*/.struct File
1d63c 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 43 68 Chunk {. FileCh
1d63d 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 unk *pNext;
1d63e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 /* Nex
1d63f 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65 20 6a t chunk in the j
1d640 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 7a ournal */. u8 z
1d641 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 Chunk[JOURNAL_CH
1d642 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 UNKSIZE]; /* C
1d643 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 63 ontent of this c
1d644 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a hunk */.};../*.*
1d645 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
1d646 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 65 72 this object ser
1d647 76 65 73 20 61 73 20 61 20 63 75 72 73 6f 72 20 ves as a cursor
1d648 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 into the rollbac
1d649 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 k journal..** Th
1d64a 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 e cursor can be
1d64b 65 69 74 68 65 72 20 66 6f 72 20 72 65 61 64 69 either for readi
1d64c 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a ng or writing..*
1d64d 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50 6f 69 /.struct FilePoi
1d64e 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 nt {. sqlite3_i
1d64f 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 nt64 iOffset;
1d650 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
1d651 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e from the beginn
1d652 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ing of the file
1d653 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a */. FileChunk *
1d654 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 20 20 pChunk;
1d655 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 69 63 /* Specific
1d656 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68 69 63 chunk into whic
1d657 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 h cursor points
1d658 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 */.};../*.** Thi
1d659 73 20 73 75 62 63 6c 61 73 73 20 69 73 20 61 20 s subclass is a
1d65a 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
1d65b 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63 68 20 te3_file. Each
1d65c 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 open memory-jour
1d65d 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 73 nal.** is an ins
1d65e 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 63 6c tance of this cl
1d65f 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d ass..*/.struct M
1d660 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 73 71 emJournal {. sq
1d661 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
1d662 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a *pMethod; /*
1d663 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e 20 4d Parent class. M
1d664 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a UST BE FIRST */.
1d665 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 46 69 FileChunk *pFi
1d666 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 rst;
1d667 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69 6e 2d /* Head of in-
1d668 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c 69 73 memory chunk-lis
1d669 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 t */. FilePoint
1d66a 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 endpoint;
1d66b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1d66c 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 r to the end of
1d66d 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 the file */. Fi
1d66e 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f 69 6e lePoint readpoin
1d66f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
1d670 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
1d671 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 end of the last
1d672 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f xRead() */.};../
1d673 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 *.** Read data f
1d674 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f rom the file..*/
1d675 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a .static int memj
1d676 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 rnlRead(. sqlit
1d677 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1d678 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1d679 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 file from which
1d67a 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f to read */. vo
1d67b 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 id *zBuf,
1d67c 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 /* Put the
1d67d 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a results here */.
1d67e 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 int iAmt,
1d67f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1d680 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 er of bytes to r
1d681 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f ead */. sqlite_
1d682 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 int64 iOfst
1d683 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 /* Begin reading
1d684 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 at this offset
1d685 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e */.){. MemJourn
1d686 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 al *p = (MemJour
1d687 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 nal *)pJfd;. u8
1d688 20 2a 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 *zOut = zBuf;.
1d689 20 69 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d int nRead = iAm
1d68a 74 3b 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f t;. int iChunkO
1d68b 66 66 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 ffset;. FileChu
1d68c 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 61 nk *pChunk;.. a
1d68d 73 73 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d ssert( iOfst+iAm
1d68e 74 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 t<=p->endpoint.i
1d68f 4f 66 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 Offset );.. if(
1d690 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f p->readpoint.iO
1d691 66 66 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 ffset!=iOfst ||
1d692 69 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 iOfst==0 ){.
1d693 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f sqlite3_int64 iO
1d694 66 66 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 ff = 0;. for(
1d695 70 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 pChunk=p->pFirst
1d696 3b 20 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e ; . pChun
1d697 6b 20 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 4e k && (iOff+JOURN
1d698 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d 69 AL_CHUNKSIZE)<=i
1d699 4f 66 73 74 3b 0a 20 20 20 20 20 20 20 20 70 43 Ofst;. pC
1d69a 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 hunk=pChunk->pNe
1d69b 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 xt. ){.
1d69c 69 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f iOff += JOURNAL_
1d69d 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 7d CHUNKSIZE;. }
1d69e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 . }else{. pC
1d69f 68 75 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 6f hunk = p->readpo
1d6a0 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d 0a int.pChunk;. }.
1d6a1 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 . iChunkOffset
1d6a2 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a 4f = (int)(iOfst%JO
1d6a3 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 URNAL_CHUNKSIZE)
1d6a4 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74 ;. do {. int
1d6a5 20 69 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e 41 iSpace = JOURNA
1d6a6 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 L_CHUNKSIZE - iC
1d6a7 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20 20 hunkOffset;.
1d6a8 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 int nCopy = MIN(
1d6a9 6e 52 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c 5f nRead, (JOURNAL_
1d6aa 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 CHUNKSIZE - iChu
1d6ab 6e 6b 4f 66 66 73 65 74 29 29 3b 0a 20 20 20 20 nkOffset));.
1d6ac 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70 43 memcpy(zOut, &pC
1d6ad 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 hunk->zChunk[iCh
1d6ae 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f 70 unkOffset], nCop
1d6af 79 29 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d 20 y);. zOut +=
1d6b0 6e 43 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61 64 nCopy;. nRead
1d6b1 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 -= iSpace;.
1d6b2 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 30 iChunkOffset = 0
1d6b3 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 52 65 ;. } while( nRe
1d6b4 61 64 3e 3d 30 20 26 26 20 28 70 43 68 75 6e 6b ad>=0 && (pChunk
1d6b5 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29 21 =pChunk->pNext)!
1d6b6 3d 30 20 26 26 20 6e 52 65 61 64 3e 30 20 29 3b =0 && nRead>0 );
1d6b7 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e . p->readpoint.
1d6b8 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74 2b iOffset = iOfst+
1d6b9 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64 70 iAmt;. p->readp
1d6ba 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 43 oint.pChunk = pC
1d6bb 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e 20 hunk;.. return
1d6bc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
1d6bd 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 .** Write data t
1d6be 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 o the file..*/.s
1d6bf 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e tatic int memjrn
1d6c0 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 lWrite(. sqlite
1d6c1 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 3_file *pJfd,
1d6c2 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 /* The journal
1d6c3 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 file into which
1d6c4 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 6f to write */. co
1d6c5 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 nst void *zBuf,
1d6c6 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 74 /* Take dat
1d6c7 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 a to be written
1d6c8 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 69 from here */. i
1d6c9 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 nt iAmt,
1d6ca 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1d6cb 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 of bytes to writ
1d6cc 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e e */. sqlite_in
1d6cd 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1d6ce 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 Begin writing a
1d6cf 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 6e t this offset in
1d6d0 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 to the file */.)
1d6d1 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a {. MemJournal *
1d6d2 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 p = (MemJournal
1d6d3 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e 57 *)pJfd;. int nW
1d6d4 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20 75 rite = iAmt;. u
1d6d5 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38 20 8 *zWrite = (u8
1d6d6 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41 6e *)zBuf;.. /* An
1d6d7 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
1d6d8 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 6f al file should o
1d6d9 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70 65 nly ever be appe
1d6da 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d 0a nded to. Random.
1d6db 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69 74 ** access writ
1d6dc 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 es are not requi
1d6dd 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a 20 red by sqlite..
1d6de 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 69 4f 66 */. assert(iOf
1d6df 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e st==p->endpoint.
1d6e0 69 4f 66 66 73 65 74 29 3b 0a 20 20 55 4e 55 53 iOffset);. UNUS
1d6e1 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 4f 66 ED_PARAMETER(iOf
1d6e2 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e st);.. while( n
1d6e3 57 72 69 74 65 3e 30 20 29 7b 0a 20 20 20 20 46 Write>0 ){. F
1d6e4 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b ileChunk *pChunk
1d6e5 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 = p->endpoint.p
1d6e6 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e 74 20 69 Chunk;. int i
1d6e7 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 ChunkOffset = (i
1d6e8 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e nt)(p->endpoint.
1d6e9 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f iOffset%JOURNAL_
1d6ea 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 20 20 CHUNKSIZE);.
1d6eb 69 6e 74 20 69 53 70 61 63 65 20 3d 20 4d 49 4e int iSpace = MIN
1d6ec 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c (nWrite, JOURNAL
1d6ed 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 _CHUNKSIZE - iCh
1d6ee 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 unkOffset);..
1d6ef 20 69 66 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 if( iChunkOffse
1d6f0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a t==0 ){. /*
1d6f1 20 4e 65 77 20 63 68 75 6e 6b 20 69 73 20 72 65 New chunk is re
1d6f2 71 75 69 72 65 64 20 74 6f 20 65 78 74 65 6e 64 quired to extend
1d6f3 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 the file. */.
1d6f4 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 FileChunk *p
1d6f5 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 New = sqlite3_ma
1d6f6 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 lloc(sizeof(File
1d6f7 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 Chunk));. i
1d6f8 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 f( !pNew ){.
1d6f9 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1d6fa 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 E_IOERR_NOMEM;.
1d6fb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 }. pNe
1d6fc 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 w->pNext = 0;.
1d6fd 20 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20 29 if( pChunk )
1d6fe 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1d6ff 28 20 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 ( p->pFirst );.
1d700 20 20 20 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 pChunk->p
1d701 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 Next = pNew;.
1d702 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1d703 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 46 assert( !p->pF
1d704 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 irst );.
1d705 70 2d 3e 70 46 69 72 73 74 20 3d 20 70 4e 65 77 p->pFirst = pNew
1d706 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1d707 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 p->endpoint.pChu
1d708 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d nk = pNew;. }
1d709 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d .. memcpy(&p-
1d70a 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b >endpoint.pChunk
1d70b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f ->zChunk[iChunkO
1d70c 66 66 73 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 ffset], zWrite,
1d70d 69 53 70 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 iSpace);. zWr
1d70e 69 74 65 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 ite += iSpace;.
1d70f 20 20 20 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 nWrite -= iSp
1d710 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 ace;. p->endp
1d711 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 oint.iOffset +=
1d712 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 iSpace;. }.. r
1d713 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1d714 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 .}../*.** Trunca
1d715 74 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a te the file..*/.
1d716 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 static int memjr
1d717 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 nlTruncate(sqlit
1d718 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 e3_file *pJfd, s
1d719 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 qlite_int64 size
1d71a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ){. MemJournal
1d71b 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c *p = (MemJournal
1d71c 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 *)pJfd;. FileC
1d71d 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 hunk *pChunk;.
1d71e 61 73 73 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b assert(size==0);
1d71f 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1d720 54 45 52 28 73 69 7a 65 29 3b 0a 20 20 70 43 68 TER(size);. pCh
1d721 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b unk = p->pFirst;
1d722 0a 20 20 77 68 69 6c 65 28 20 70 43 68 75 6e 6b . while( pChunk
1d723 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e ){. FileChun
1d724 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b k *pTmp = pChunk
1d725 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 ;. pChunk = p
1d726 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 Chunk->pNext;.
1d727 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
1d728 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Tmp);. }. sqli
1d729 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 te3MemJournalOpe
1d72a 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65 74 75 72 n(pJfd);. retur
1d72b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1d72c 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 /*.** Close the
1d72d 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1d72e 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 int memjrnlClose
1d72f 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1d730 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 Jfd){. memjrnlT
1d731 72 75 6e 63 61 74 65 28 70 4a 66 64 2c 20 30 29 runcate(pJfd, 0)
1d732 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1d733 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 E_OK;.}.../*.**
1d734 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a Sync the file..*
1d735 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d /.static int mem
1d736 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 jrnlSync(sqlite3
1d737 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 _file *NotUsed,
1d738 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 int NotUsed2){.
1d739 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1d73a 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
1d73b 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 sed2);. return
1d73c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
1d73d 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69 .** Query the si
1d73e 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
1d73f 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 n bytes..*/.stat
1d740 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69 ic int memjrnlFi
1d741 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 leSize(sqlite3_f
1d742 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 ile *pJfd, sqlit
1d743 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b e_int64 *pSize){
1d744 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 . MemJournal *p
1d745 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a = (MemJournal *
1d746 29 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20 )pJfd;. *pSize
1d747 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 = (sqlite_int64)
1d748 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 p->endpoint.iOf
1d749 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 fset;. return S
1d74a 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1d74b 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 ** Table of meth
1d74c 6f 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e ods for MemJourn
1d74d 61 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 al sqlite3_file
1d74e 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 object..*/.stati
1d74f 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 c struct sqlite3
1d750 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a _io_methods MemJ
1d751 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 ournalMethods =
1d752 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 {. 1,
1d753 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
1d754 6e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c n */. memjrnlCl
1d755 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f ose, /* xClo
1d756 73 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 se */. memjrnlR
1d757 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 ead, /* xRe
1d758 61 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 ad */. memjrnlW
1d759 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 rite, /* xWr
1d75a 69 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c ite */. memjrnl
1d75b 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 Truncate, /* xT
1d75c 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d runcate */. mem
1d75d 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f jrnlSync, /
1d75e 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d * xSync */. mem
1d75f 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f jrnlFileSize, /
1d760 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 * xFileSize */.
1d761 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1d762 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 /* xLock */.
1d763 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1d764 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f /* xUnlock */
1d765 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 . 0,
1d766 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
1d767 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 servedLock */.
1d768 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1d769 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f /* xFileContro
1d76a 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 l */. 0,
1d76b 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 /* xSec
1d76c 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 torSize */. 0
1d76d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d76e 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 * xDeviceCharact
1d76f 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a eristics */.};..
1d770 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f /* .** Open a jo
1d771 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 urnal file..*/.S
1d772 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1d773 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 id sqlite3MemJou
1d774 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 rnalOpen(sqlite3
1d775 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 _file *pJfd){.
1d776 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 MemJournal *p =
1d777 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a (MemJournal *)pJ
1d778 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 fd;. memset(p,
1d779 30 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 0, sqlite3MemJou
1d77a 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 rnalSize());. p
1d77b 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d ->pMethod = &Mem
1d77c 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a JournalMethods;.
1d77d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1d77e 74 72 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 true if the file
1d77f 2d 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 -handle passed a
1d780 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 s an argument is
1d781 20 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 .** an in-memor
1d782 79 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 y journal .*/.SQ
1d783 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1d784 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 sqlite3IsMemJou
1d785 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c rnal(sqlite3_fil
1d786 65 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 e *pJfd){. retu
1d787 72 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 rn pJfd->pMethod
1d788 73 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 s==&MemJournalMe
1d789 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a thods;.}../* .**
1d78a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1d78b 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 er of bytes requ
1d78c 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 ired to store a
1d78d 4d 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 MemJournal that
1d78e 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 uses vfs.** pVfs
1d78f 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 to create the u
1d790 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 nderlying on-dis
1d791 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 k files..*/.SQLI
1d792 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1d793 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
1d794 53 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 Size(void){. re
1d795 74 75 72 6e 20 73 69 7a 65 6f 66 28 4d 65 6d 4a turn sizeof(MemJ
1d796 6f 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a ournal);.}../***
1d797 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1d798 6f 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 of memjournal.c
1d799 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d79a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d79b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1d79c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1d79d 6e 20 66 69 6c 65 20 77 61 6c 6b 65 72 2e 63 20 n file walker.c
1d79e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d79f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1d7a1 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 31 36 * 2008 August 16
1d7a2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1d7a3 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1d7a4 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1d7a5 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1d7a6 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1d7a7 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1d7a8 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1d7a9 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1d7aa 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1d7ab 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1d7ac 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1d7ad 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1d7ae 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1d7af 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1d7b0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1d7b1 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1d7b2 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1d7b3 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1d7b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d7b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d7b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d7b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1d7b8 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1d7b9 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 tains routines u
1d7ba 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 sed for walking
1d7bb 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20 the parser tree
1d7bc 66 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 73 74 for.** an SQL st
1d7bd 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 atement..**.** $
1d7be 49 64 3a 20 77 61 6c 6b 65 72 2e 63 2c 76 20 31 Id: walker.c,v 1
1d7bf 2e 32 20 32 30 30 39 2f 30 32 2f 31 39 20 31 34 .2 2009/02/19 14
1d7c0 3a 33 39 3a 32 35 20 64 61 6e 69 65 6c 6b 31 39 :39:25 danielk19
1d7c1 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 77 Exp $.*/.../*
1d7c2 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 .** Walk an expr
1d7c3 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e ession tree. In
1d7c4 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 voke the callbac
1d7c5 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 k once for each
1d7c6 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 node.** of the e
1d7c7 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 xpression, while
1d7c8 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e decending. (In
1d7c9 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 other words, th
1d7ca 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 e callback.** is
1d7cb 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 invoked before
1d7cc 76 69 73 69 74 69 6e 67 20 63 68 69 6c 64 72 65 visiting childre
1d7cd 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 n.).**.** The re
1d7ce 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 turn value from
1d7cf 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f the callback sho
1d7d0 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 uld be one of th
1d7d1 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 e WRC_*.** const
1d7d2 61 6e 74 73 20 74 6f 20 73 70 65 63 69 66 79 20 ants to specify
1d7d3 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 64 20 77 how to proceed w
1d7d4 69 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a ith the walk..**
1d7d5 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 .** WRC_Conti
1d7d6 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 nue Continu
1d7d7 65 20 64 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 e descending dow
1d7d8 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a n the tree..**.*
1d7d9 2a 20 20 20 20 57 52 43 5f 50 72 75 6e 65 20 20 * WRC_Prune
1d7da 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 Do not de
1d7db 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 scend into child
1d7dc 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 61 6c 6c nodes. But all
1d7dd 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ow.**
1d7de 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 77 the w
1d7df 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 alk to continue
1d7e0 77 69 74 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 with sibling nod
1d7e1 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 es..**.** WRC
1d7e2 5f 41 62 6f 72 74 20 20 20 20 20 20 20 20 20 44 _Abort D
1d7e3 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 o no more callba
1d7e4 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 cks. Unwind the
1d7e5 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 stack and.**
1d7e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d7e7 20 20 20 72 65 74 75 72 6e 20 74 68 65 20 74 6f return the to
1d7e8 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c p-level walk cal
1d7e9 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 l..**.** The ret
1d7ea 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 urn value from t
1d7eb 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 57 his routine is W
1d7ec 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 62 61 6e RC_Abort to aban
1d7ed 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c don the tree wal
1d7ee 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e k.** and WRC_Con
1d7ef 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 tinue to continu
1d7f0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1d7f1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1d7f2 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 WalkExpr(Walker
1d7f3 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a *pWalker, Expr *
1d7f4 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 pExpr){. int rc
1d7f5 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 ;. if( pExpr==0
1d7f6 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f ) return WRC_Co
1d7f7 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20 70 ntinue;. rc = p
1d7f8 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72 43 61 6c Walker->xExprCal
1d7f9 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 lback(pWalker, p
1d7fa 45 78 70 72 29 3b 0a 20 20 69 66 28 20 72 63 3d Expr);. if( rc=
1d7fb 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 29 7b =WRC_Continue ){
1d7fc 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1d7fd 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 WalkExpr(pWalker
1d7fe 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 20 , pExpr->pLeft)
1d7ff 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1d800 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 rt;. if( sqli
1d801 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1d802 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 ker, pExpr->pRig
1d803 68 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 ht) ) return WRC
1d804 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 _Abort;. if(
1d805 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
1d806 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c pExpr, EP_xIsSel
1d807 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 ect) ){. if
1d808 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c ( sqlite3WalkSel
1d809 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 ect(pWalker, pEx
1d80a 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 pr->x.pSelect) )
1d80b 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1d80c 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
1d80d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 if( sqlite3W
1d80e 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1d80f 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c ker, pExpr->x.pL
1d810 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 ist) ) return WR
1d811 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 C_Abort;. }.
1d812 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 }. return rc &
1d813 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f WRC_Abort;.}../
1d814 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 *.** Call sqlite
1d815 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 3WalkExpr() for
1d816 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e every expression
1d817 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 6e in list p or un
1d818 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 til.** an abort
1d819 72 65 71 75 65 73 74 20 69 73 20 73 65 65 6e 2e request is seen.
1d81a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1d81b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 TE int sqlite3Wa
1d81c 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 lkExprList(Walke
1d81d 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 r *pWalker, Expr
1d81e 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 List *p){. int
1d81f 69 2c 20 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 i, rc = WRC_Cont
1d820 69 6e 75 65 3b 0a 20 20 73 74 72 75 63 74 20 45 inue;. struct E
1d821 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
1d822 74 65 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a tem;. if( p ){.
1d823 20 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 for(i=p->nEx
1d824 70 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 pr, pItem=p->a;
1d825 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b i>0; i--, pItem+
1d826 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 +){. if( sq
1d827 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 lite3WalkExpr(pW
1d828 61 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 alker, pItem->pE
1d829 78 70 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 xpr) ) return WR
1d82a 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 C_Abort;. }.
1d82b 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 }. return rc &
1d82c 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
1d82d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6c 6c ../*.** Walk all
1d82e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 73 73 expressions ass
1d82f 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 45 4c ociated with SEL
1d830 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e ECT statement p.
1d831 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 6f Do.** not invo
1d832 6b 65 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 ke the SELECT ca
1d833 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 llback on p, but
1d834 20 64 6f 20 28 6f 66 20 63 6f 75 72 73 65 29 20 do (of course)
1d835 69 6e 76 6f 6b 65 0a 2a 2a 20 61 6e 79 20 65 78 invoke.** any ex
1d836 70 72 20 63 61 6c 6c 62 61 63 6b 73 20 61 6e 64 pr callbacks and
1d837 20 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b SELECT callback
1d838 73 20 74 68 61 74 20 63 6f 6d 65 20 66 72 6f 6d s that come from
1d839 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 subqueries..**
1d83a 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 Return WRC_Abort
1d83b 20 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 or WRC_Continue
1d83c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1d83d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
1d83e 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 57 61 alkSelectExpr(Wa
1d83f 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 lker *pWalker, S
1d840 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 66 28 elect *p){. if(
1d841 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
1d842 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d List(pWalker, p-
1d843 3e 70 45 4c 69 73 74 29 20 29 20 72 65 74 75 72 >pEList) ) retur
1d844 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 n WRC_Abort;. i
1d845 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 f( sqlite3WalkEx
1d846 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 pr(pWalker, p->p
1d847 57 68 65 72 65 29 20 29 20 72 65 74 75 72 6e 20 Where) ) return
1d848 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 WRC_Abort;. if(
1d849 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
1d84a 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d List(pWalker, p-
1d84b 3e 70 47 72 6f 75 70 42 79 29 20 29 20 72 65 74 >pGroupBy) ) ret
1d84c 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1d84d 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1d84e 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d Expr(pWalker, p-
1d84f 3e 70 48 61 76 69 6e 67 29 20 29 20 72 65 74 75 >pHaving) ) retu
1d850 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1d851 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 if( sqlite3WalkE
1d852 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c xprList(pWalker,
1d853 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 20 29 20 p->pOrderBy) )
1d854 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1d855 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 ;. if( sqlite3W
1d856 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c alkExpr(pWalker,
1d857 20 70 2d 3e 70 4c 69 6d 69 74 29 20 29 20 72 65 p->pLimit) ) re
1d858 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1d859 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1d85a 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 kExpr(pWalker, p
1d85b 2d 3e 70 4f 66 66 73 65 74 29 20 29 20 72 65 74 ->pOffset) ) ret
1d85c 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1d85d 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 return WRC_Cont
1d85e 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 inue;.}../*.** W
1d85f 61 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 72 alk the parse tr
1d860 65 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ees associated w
1d861 69 74 68 20 61 6c 6c 20 73 75 62 71 75 65 72 69 ith all subqueri
1d862 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 46 52 4f es in the.** FRO
1d863 4d 20 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 M clause of SELE
1d864 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 CT statement p.
1d865 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 Do not invoke t
1d866 68 65 20 73 65 6c 65 63 74 0a 2a 2a 20 63 61 6c he select.** cal
1d867 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20 lback on p, but
1d868 64 6f 20 69 6e 76 6f 6b 65 20 69 74 20 6f 6e 20 do invoke it on
1d869 65 61 63 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 each FROM clause
1d86a 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 61 6e 64 subquery.** and
1d86b 20 6f 6e 20 61 6e 79 20 73 75 62 71 75 65 72 69 on any subqueri
1d86c 65 73 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 es further down
1d86d 69 6e 20 74 68 65 20 74 72 65 65 2e 20 20 52 65 in the tree. Re
1d86e 74 75 72 6e 20 0a 2a 2a 20 57 52 43 5f 41 62 6f turn .** WRC_Abo
1d86f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e rt or WRC_Contin
1d870 75 65 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ue;.*/.SQLITE_PR
1d871 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1d872 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 3WalkSelectFrom(
1d873 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
1d874 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 Select *p){. S
1d875 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 rcList *pSrc;.
1d876 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
1d877 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
1d878 74 65 6d 3b 0a 0a 20 20 70 53 72 63 20 3d 20 70 tem;.. pSrc = p
1d879 2d 3e 70 53 72 63 3b 0a 20 20 69 66 28 20 70 53 ->pSrc;. if( pS
1d87a 72 63 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d rc ){. for(i=
1d87b 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 pSrc->nSrc, pIte
1d87c 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 m=pSrc->a; i>0;
1d87d 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i--, pItem++){.
1d87e 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1d87f 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b WalkSelect(pWalk
1d880 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 er, pItem->pSele
1d881 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ct) ){. r
1d882 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1d883 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1d884 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f }. return WRC_
1d885 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a Continue;.} ../*
1d886 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 .** Call sqlite3
1d887 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 WalkExpr() for e
1d888 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 very expression
1d889 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d in Select statem
1d88a 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 ent p..** Invoke
1d88b 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
1d88c 63 74 28 29 20 66 6f 72 20 73 75 62 71 75 65 72 ct() for subquer
1d88d 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 ies in the FROM
1d88e 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e clause and.** on
1d88f 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 the compound se
1d890 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 lect chain, p->p
1d891 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 Prior..**.** Ret
1d892 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 urn WRC_Continue
1d893 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f under normal co
1d894 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 nditions. Retur
1d895 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a n WRC_Abort if.*
1d896 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 62 * there is an ab
1d897 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a ort request..**.
1d898 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65 72 ** If the Walker
1d899 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 does not have a
1d89a 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 n xSelectCallbac
1d89b 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f k() then this ro
1d89c 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f utine.** is a no
1d89d 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57 52 -op returning WR
1d89e 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 C_Continue..*/.S
1d89f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1d8a0 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c t sqlite3WalkSel
1d8a1 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c ect(Walker *pWal
1d8a2 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b ker, Select *p){
1d8a3 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 . int rc;. if(
1d8a4 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 p==0 || pWalker
1d8a5 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 ->xSelectCallbac
1d8a6 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 k==0 ) return WR
1d8a7 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 C_Continue;. rc
1d8a8 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b = WRC_Continue;
1d8a9 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29 7b 0a . while( p ){.
1d8aa 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 rc = pWalker
1d8ab 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 ->xSelectCallbac
1d8ac 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 k(pWalker, p);.
1d8ad 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65 61 if( rc ) brea
1d8ae 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 k;. if( sqlit
1d8af 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 e3WalkSelectExpr
1d8b0 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 (pWalker, p) ) r
1d8b1 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1d8b2 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1d8b3 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 WalkSelectFrom(p
1d8b4 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 Walker, p) ) ret
1d8b5 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1d8b6 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 p = p->pPrior
1d8b7 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1d8b8 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d c & WRC_Abort;.}
1d8b9 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1d8ba 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e * End of walker.
1d8bb 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1d8bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8be 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1d8bf 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 65 73 * Begin file res
1d8c0 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a olve.c *********
1d8c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8c3 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 /./*.** 2008 Aug
1d8c4 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 18.**.** The
1d8c5 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1d8c6 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1d8c7 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1d8c8 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1d8c9 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1d8ca 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1d8cb 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1d8cc 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1d8cd 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1d8ce 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1d8cf 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1d8d0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1d8d1 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1d8d2 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1d8d3 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1d8d4 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1d8d5 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1d8d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8da 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1d8db 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 file contains r
1d8dc 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 outines used for
1d8dd 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 walking the par
1d8de 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 ser tree and.**
1d8df 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e resolve all iden
1d8e0 74 69 66 69 65 72 73 20 62 79 20 61 73 73 6f 63 tifiers by assoc
1d8e1 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69 74 68 iating them with
1d8e2 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
1d8e3 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d table and colum
1d8e4 6e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 n..**.** $Id: re
1d8e5 73 6f 6c 76 65 2e 63 2c 76 20 31 2e 32 30 20 32 solve.c,v 1.20 2
1d8e6 30 30 39 2f 30 33 2f 30 35 20 30 34 3a 32 33 3a 009/03/05 04:23:
1d8e7 34 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 47 shane Exp $.*
1d8e8 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 /../*.** Turn th
1d8e9 65 20 70 45 78 70 72 20 65 78 70 72 65 73 73 69 e pExpr expressi
1d8ea 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 on into an alias
1d8eb 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 for the iCol-th
1d8ec 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a column of the.*
1d8ed 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e 20 * result set in
1d8ee 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 pEList..**.** If
1d8ef 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1d8f0 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 column is a simp
1d8f1 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 le column refere
1d8f2 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 nce, then this r
1d8f3 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 outine.** makes
1d8f4 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 20 an exact copy.
1d8f5 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 But for any othe
1d8f6 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 73 r kind of expres
1d8f7 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f sion, this.** ro
1d8f8 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 utine make a cop
1d8f9 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 y of the result
1d8fa 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 set column as th
1d8fb 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 e argument to th
1d8fc 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 e.** TK_AS opera
1d8fd 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 tor. The TK_AS
1d8fe 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 20 operator causes
1d8ff 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 the expression t
1d900 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 o be.** evaluate
1d901 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 d just once and
1d902 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 20 then reused for
1d903 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a each alias..**.*
1d904 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 * The reason for
1d905 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65 suppressing the
1d906 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 6e TK_AS term when
1d907 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1d908 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 is a simple.** c
1d909 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 olumn reference
1d90a 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 is so that the c
1d90b 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 olumn reference
1d90c 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a will be recogniz
1d90d 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 ed as.** usable
1d90e 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69 by indices withi
1d90f 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 n the WHERE clau
1d910 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f se processing lo
1d911 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b gic. .**.** Hack
1d912 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 : The TK_AS ope
1d913 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 74 rator is inhibit
1d914 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d ed if zType[0]==
1d915 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 'G'. This means
1d916 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 52 .** that in a GR
1d917 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 74 OUP BY clause, t
1d918 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
1d919 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 65 evaluated twice
1d91a 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 . Hence:.**.**
1d91b 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f SELECT rando
1d91c 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e m()%5 AS x, coun
1d91d 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52 t(*) FROM tab GR
1d91e 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 OUP BY x.**.** I
1d91f 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a s equivalent to:
1d920 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 .**.** SELEC
1d921 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 T random()%5 AS
1d922 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d x, count(*) FROM
1d923 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 61 tab GROUP BY ra
1d924 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 ndom()%5.**.** T
1d925 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 6e he result of ran
1d926 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 47 dom()%5 in the G
1d927 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 ROUP BY clause i
1d928 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 65 s probably diffe
1d929 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 rent.** from the
1d92a 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 72 result in the r
1d92b 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d esult-set. We m
1d92c 69 67 68 74 20 66 69 78 20 74 68 69 73 20 73 6f ight fix this so
1d92d 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 meday. Or.** th
1d92e 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 en again, we mig
1d92f 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 ht not....*/.sta
1d930 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 tic void resolve
1d931 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 2a Alias(. Parse *
1d932 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
1d933 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
1d934 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 xt */. ExprList
1d935 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f *pEList, /
1d936 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a * A result set *
1d937 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 /. int iCol,
1d938 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 /* A
1d939 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 column in the re
1d93a 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 sult set. 0..pE
1d93b 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f List->nExpr-1 */
1d93c 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 . Expr *pExpr,
1d93d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 /* Tra
1d93e 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f nsform this into
1d93f 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 65 an alias to the
1d940 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
1d941 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 const char *zTy
1d942 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 pe /* "GROU
1d943 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 P" or "ORDER" or
1d944 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 "" */.){. Expr
1d945 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20 *pOrig;
1d946 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 /* The iCol-t
1d947 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 h column of the
1d948 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
1d949 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20 Expr *pDup;
1d94a 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
1d94b 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c f pOrig */. sql
1d94c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
1d94d 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1d94e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
1d94f 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f /.. assert( iCo
1d950 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c l>=0 && iCol<pEL
1d951 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 ist->nExpr );.
1d952 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e pOrig = pEList->
1d953 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 a[iCol].pExpr;.
1d954 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d assert( pOrig!=
1d955 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1d956 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 Orig->flags & EP
1d957 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 _Resolved );. d
1d958 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1d959 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 pDup = sqlite3
1d95a 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 ExprDup(db, pOri
1d95b 67 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 44 75 g, 0);. if( pDu
1d95c 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
1d95d 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 sqlite3TokenCop
1d95e 79 28 64 62 2c 20 26 70 44 75 70 2d 3e 74 6f 6b y(db, &pDup->tok
1d95f 65 6e 2c 20 26 70 4f 72 69 67 2d 3e 74 6f 6b 65 en, &pOrig->toke
1d960 6e 29 3b 0a 20 20 69 66 28 20 70 44 75 70 2d 3e n);. if( pDup->
1d961 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 op!=TK_COLUMN &&
1d962 20 7a 54 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 zType[0]!='G' )
1d963 7b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c {. pDup = sql
1d964 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 ite3PExpr(pParse
1d965 2c 20 54 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30 , TK_AS, pDup, 0
1d966 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44 , 0);. if( pD
1d967 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a up==0 ) return;.
1d968 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e if( pEList->
1d969 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d a[iCol].iAlias==
1d96a 30 20 29 7b 0a 20 20 20 20 20 20 70 45 4c 69 73 0 ){. pELis
1d96b 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 t->a[iCol].iAlia
1d96c 73 20 3d 20 28 75 31 36 29 28 2b 2b 70 50 61 72 s = (u16)(++pPar
1d96d 73 65 2d 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20 se->nAlias);.
1d96e 20 7d 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61 }. pDup->iTa
1d96f 62 6c 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b ble = pEList->a[
1d970 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 iCol].iAlias;.
1d971 7d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 }. if( pExpr->f
1d972 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c lags & EP_ExpCol
1d973 6c 61 74 65 20 29 7b 0a 20 20 20 20 70 44 75 70 late ){. pDup
1d974 2d 3e 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d ->pColl = pExpr-
1d975 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 70 >pColl;. pDup
1d976 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 ->flags |= EP_Ex
1d977 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 20 pCollate;. }.
1d978 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 sqlite3ExprClear
1d979 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d (db, pExpr);. m
1d97a 65 6d 63 70 79 28 70 45 78 70 72 2c 20 70 44 75 emcpy(pExpr, pDu
1d97b 70 2c 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 p, sizeof(*pExpr
1d97c 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 ));. sqlite3DbF
1d97d 72 65 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d ree(db, pDup);.}
1d97e 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 ../*.** Given th
1d97f 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 e name of a colu
1d980 6d 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 mn of the form X
1d981 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a .Y.Z or Y.Z or j
1d982 75 73 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a ust Z, look up.*
1d983 2a 20 74 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 * that name in t
1d984 68 65 20 73 65 74 20 6f 66 20 73 6f 75 72 63 65 he set of source
1d985 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63 4c tables in pSrcL
1d986 69 73 74 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 ist and make the
1d987 20 70 45 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 pExpr .** expre
1d988 73 73 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 ssion node refer
1d989 20 62 61 63 6b 20 74 6f 20 74 68 61 74 20 73 6f back to that so
1d98a 75 72 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 urce column. Th
1d98b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e e following chan
1d98c 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 ges.** are made
1d98d 74 6f 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 to pExpr:.**.**
1d98e 20 20 20 70 45 78 70 72 2d 3e 69 44 62 20 20 20 pExpr->iDb
1d98f 20 20 20 20 20 20 20 20 53 65 74 20 74 68 65 20 Set the
1d990 69 6e 64 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 index in db->aDb
1d991 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 [] of the databa
1d992 73 65 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 se X.**
1d993 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d994 28 65 76 65 6e 20 69 66 20 58 20 69 73 20 69 6d (even if X is im
1d995 70 6c 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 plied)..** pE
1d996 78 70 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 xpr->iTable
1d997 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 75 Set to the cu
1d998 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 rsor number for
1d999 74 68 65 20 74 61 62 6c 65 20 6f 62 74 61 69 6e the table obtain
1d99a 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ed.**
1d99b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 72 fr
1d99c 6f 6d 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 om pSrcList..**
1d99d 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 20 pExpr->pTab
1d99e 20 20 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 Points t
1d99f 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 o the Table stru
1d9a0 63 74 75 72 65 20 6f 66 20 58 2e 59 20 28 65 76 cture of X.Y (ev
1d9a1 65 6e 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 en if.**
1d9a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d9a3 20 58 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 X and/or Y are
1d9a4 69 6d 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 implied.).**
1d9a5 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 pExpr->iColumn
1d9a6 20 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 Set to the
1d9a7 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 column number wi
1d9a8 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a thin the table..
1d9a9 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 ** pExpr->op
1d9aa 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20 74 Set t
1d9ab 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 o TK_COLUMN..**
1d9ac 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 pExpr->pLeft
1d9ad 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 Any expr
1d9ae 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e ession this poin
1d9af 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 ts to is deleted
1d9b0 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 .** pExpr->pR
1d9b1 69 67 68 74 20 20 20 20 20 20 20 20 41 6e 79 20 ight Any
1d9b2 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73 20 expression this
1d9b3 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65 6c points to is del
1d9b4 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eted..**.** The
1d9b5 70 44 62 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 pDbToken is the
1d9b6 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
1d9b7 62 61 73 65 20 28 74 68 65 20 22 58 22 29 2e 20 base (the "X").
1d9b8 20 54 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20 This value may
1d9b9 62 65 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 be.** NULL meani
1d9ba 6e 67 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20 ng that name is
1d9bb 6f 66 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 of the form Y.Z
1d9bc 6f 72 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c or Z. Any avail
1d9bd 61 62 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a able database.**
1d9be 20 63 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54 can be used. T
1d9bf 68 65 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 he pTableToken i
1d9c0 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
1d9c1 65 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 e table (the "Y"
1d9c2 29 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 ). This.** valu
1d9c3 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 e can be NULL if
1d9c4 20 70 44 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73 pDbToken is als
1d9c5 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 70 54 61 62 o NULL. If pTab
1d9c6 6c 65 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 leToken is NULL
1d9c7 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 it.** means that
1d9c8 20 74 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 the form of the
1d9c9 20 6e 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 name is Z and t
1d9ca 68 61 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d hat columns from
1d9cb 20 61 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 any table.** ca
1d9cc 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a n be used..**.**
1d9cd 20 49 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e If the name can
1d9ce 6e 6f 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 not be resolved
1d9cf 75 6e 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c unambiguously, l
1d9d0 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 eave an error me
1d9d1 73 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 ssage.** in pPar
1d9d2 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f se and return no
1d9d3 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 n-zero. Return
1d9d4 7a 65 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e zero on success.
1d9d5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c .*/.static int l
1d9d6 6f 6f 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 ookupName(. Par
1d9d7 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
1d9d8 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 /* The parsing
1d9d9 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f context */. To
1d9da 6b 65 6e 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20 ken *pDbToken,
1d9db 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
1d9dc 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 e database conta
1d9dd 69 6e 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 ining table, or
1d9de 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 NULL */. Token
1d9df 2a 70 54 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f *pTableToken, /
1d9e0 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 * Name of table
1d9e1 63 6f 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d containing colum
1d9e2 6e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 n, or NULL */.
1d9e3 54 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f Token *pColumnTo
1d9e4 6b 65 6e 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 ken, /* Name of
1d9e5 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 the column. */.
1d9e6 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e NameContext *pN
1d9e7 43 2c 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d C, /* The nam
1d9e8 65 20 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74 e context used t
1d9e9 6f 20 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 o resolve the na
1d9ea 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 me */. Expr *pE
1d9eb 78 70 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 xpr /*
1d9ec 4d 61 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e Make this EXPR n
1d9ed 6f 64 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 ode point to the
1d9ee 20 73 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e selected column
1d9ef 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a */.){. char *z
1d9f0 44 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a Db = 0; /*
1d9f1 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
1d9f2 61 62 61 73 65 2e 20 20 54 68 65 20 22 58 22 20 abase. The "X"
1d9f3 69 6e 20 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68 in X.Y.Z */. ch
1d9f4 61 72 20 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20 ar *zTab = 0;
1d9f5 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
1d9f6 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 22 59 e table. The "Y
1d9f7 22 20 69 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e " in X.Y.Z or Y.
1d9f8 5a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f Z */. char *zCo
1d9f9 6c 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e l = 0; /* N
1d9fa 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d ame of the colum
1d9fb 6e 2e 20 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20 n. The "Z" */.
1d9fc 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 int i, j;
1d9fd 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
1d9fe 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 unters */. int
1d9ff 63 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 cnt = 0;
1da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1da01 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 Number of match
1da02 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 ing column names
1da03 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 */. int cntTab
1da04 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1da05 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1da06 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 74 61 r of matching ta
1da07 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 ble names */. s
1da08 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1da09 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 rse->db;
1da0a 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1da0b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
1da0c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
1da0d 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 item *pItem;
1da0e 20 20 20 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f /* Use for lo
1da0f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53 72 63 4c oping over pSrcL
1da10 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 ist items */. s
1da11 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
1da12 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 em *pMatch = 0;
1da13 20 2f 2a 20 54 68 65 20 6d 61 74 63 68 69 6e 67 /* The matching
1da14 20 70 53 72 63 4c 69 73 74 20 69 74 65 6d 20 2a pSrcList item *
1da15 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 /. NameContext
1da16 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 *pTopNC = pNC;
1da17 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6e /* First n
1da18 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 amecontext in th
1da19 65 20 6c 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 e list */. Sche
1da1a 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b ma *pSchema = 0;
1da1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1da1c 20 53 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 Schema of the e
1da1d 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 0a 20 20 xpression */..
1da1e 61 73 73 65 72 74 28 20 70 4e 43 20 29 3b 20 2f assert( pNC ); /
1da1f 2a 20 74 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 * the name conte
1da20 78 74 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c xt cannot be NUL
1da21 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 L. */. assert(
1da22 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 20 26 26 20 pColumnToken &&
1da23 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 2d 3e 7a 20 pColumnToken->z
1da24 29 3b 20 2f 2a 20 54 68 65 20 5a 20 69 6e 20 58 ); /* The Z in X
1da25 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20 62 65 20 4e .Y.Z cannot be N
1da26 55 4c 4c 20 2a 2f 0a 0a 20 20 2f 2a 20 44 65 71 ULL */.. /* Deq
1da27 75 6f 74 65 20 61 6e 64 20 7a 65 72 6f 2d 74 65 uote and zero-te
1da28 72 6d 69 6e 61 74 65 20 74 68 65 20 6e 61 6d 65 rminate the name
1da29 73 20 2a 2f 0a 20 20 7a 44 62 20 3d 20 73 71 6c s */. zDb = sql
1da2a 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
1da2b 6e 28 64 62 2c 20 70 44 62 54 6f 6b 65 6e 29 3b n(db, pDbToken);
1da2c 0a 20 20 7a 54 61 62 20 3d 20 73 71 6c 69 74 65 . zTab = sqlite
1da2d 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
1da2e 62 2c 20 70 54 61 62 6c 65 54 6f 6b 65 6e 29 3b b, pTableToken);
1da2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 . zCol = sqlite
1da30 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
1da31 62 2c 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 29 b, pColumnToken)
1da32 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c ;. if( db->mall
1da33 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
1da34 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f goto lookupname_
1da35 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 end;. }.. /* I
1da36 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e 6f nitialize the no
1da37 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63 68 20 2a de to no-match *
1da38 2f 0a 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c /. pExpr->iTabl
1da39 65 20 3d 20 2d 31 3b 0a 20 20 70 45 78 70 72 2d e = -1;. pExpr-
1da3a 3e 70 54 61 62 20 3d 20 30 3b 0a 0a 20 20 2f 2a >pTab = 0;.. /*
1da3b 20 53 74 61 72 74 20 61 74 20 74 68 65 20 69 6e Start at the in
1da3c 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 74 ner-most context
1da3d 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 72 and move outwar
1da3e 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 20 d until a match
1da3f 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 68 is found */. wh
1da40 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d ile( pNC && cnt=
1da41 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 =0 ){. ExprLi
1da42 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20 st *pEList;.
1da43 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 SrcList *pSrcLis
1da44 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 t = pNC->pSrcLis
1da45 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63 t;.. if( pSrc
1da46 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f List ){. fo
1da47 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72 r(i=0, pItem=pSr
1da48 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63 cList->a; i<pSrc
1da49 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c List->nSrc; i++,
1da4a 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1da4b 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a Table *pTab;.
1da4c 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b int iDb;
1da4d 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 . Column
1da4e 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20 *pCol;. .
1da4f 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e pTab = pItem->
1da50 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73 pTab;. as
1da51 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26 sert( pTab!=0 &&
1da52 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20 pTab->zName!=0
1da53 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d );. iDb =
1da54 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
1da55 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e Index(db, pTab->
1da56 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 pSchema);.
1da57 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e assert( pTab->
1da58 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 nCol>0 );.
1da59 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 if( zTab ){.
1da5a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 if( pIte
1da5b 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 m->zAlias ){.
1da5c 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a char *z
1da5d 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d TabName = pItem-
1da5e 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 >zAlias;.
1da5f 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1da60 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 StrICmp(zTabName
1da61 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e , zTab)!=0 ) con
1da62 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 tinue;.
1da63 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1da64 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 char *zTabNa
1da65 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 me = pTab->zName
1da66 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
1da67 28 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c ( zTabName==0 ||
1da68 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1da69 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 zTabName, zTab)!
1da6a 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 =0 ) continue;.
1da6b 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a if( z
1da6c 44 62 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 Db!=0 && sqlite3
1da6d 53 74 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b StrICmp(db->aDb[
1da6e 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 iDb].zName, zDb)
1da6f 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=0 ){.
1da70 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
1da71 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1da72 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1da73 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 }. if( 0
1da74 3d 3d 28 63 6e 74 54 61 62 2b 2b 29 20 29 7b 0a ==(cntTab++) ){.
1da75 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1da76 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d >iTable = pItem-
1da77 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 >iCursor;.
1da78 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 pExpr->pTab
1da79 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 = pTab;.
1da7a 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 pSchema = pTab
1da7b 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 ->pSchema;.
1da7c 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 pMatch = pI
1da7d 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 tem;. }.
1da7e 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 for(j=0,
1da7f 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b pCol=pTab->aCol;
1da80 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a j<pTab->nCol; j
1da81 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 ++, pCol++){.
1da82 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
1da83 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e e3StrICmp(pCol->
1da84 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 zName, zCol)==0
1da85 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 49 ){. I
1da86 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b 0a 20 dList *pUsing;.
1da87 20 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b cnt++
1da88 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 ;. pE
1da89 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 xpr->iTable = pI
1da8a 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 tem->iCursor;.
1da8b 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1da8c 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 >pTab = pTab;.
1da8d 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 pMatch
1da8e 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 = pItem;.
1da8f 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 pSchema =
1da90 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 pTab->pSchema;.
1da91 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 /* Su
1da92 62 73 74 69 74 75 74 65 20 74 68 65 20 72 6f 77 bstitute the row
1da93 69 64 20 28 63 6f 6c 75 6d 6e 20 2d 31 29 20 66 id (column -1) f
1da94 6f 72 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 or the INTEGER P
1da95 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 RIMARY KEY */.
1da96 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1da97 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a 3d 3d 70 54 >iColumn = j==pT
1da98 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a ab->iPKey ? -1 :
1da99 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 j;.
1da9a 69 66 28 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e if( i<pSrcList->
1da9b 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 nSrc-1 ){.
1da9c 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 if( pIte
1da9d 6d 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20 m[1].jointype &
1da9e 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20 JT_NATURAL ){.
1da9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1daa0 20 49 66 20 74 68 69 73 20 6d 61 74 63 68 20 6f If this match o
1daa1 63 63 75 72 72 65 64 20 69 6e 20 74 68 65 20 6c ccurred in the l
1daa2 65 66 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6e eft table of a n
1daa3 61 74 75 72 61 6c 20 6a 6f 69 6e 2c 0a 20 20 20 atural join,.
1daa4 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
1daa5 74 68 65 6e 20 73 6b 69 70 20 74 68 65 20 72 69 then skip the ri
1daa6 67 68 74 20 74 61 62 6c 65 20 74 6f 20 61 76 6f ght table to avo
1daa7 69 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d id a duplicate m
1daa8 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20 atch */.
1daa9 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b pItem++;
1daaa 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1daab 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 i++;.
1daac 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 }else if( (p
1daad 55 73 69 6e 67 20 3d 20 70 49 74 65 6d 5b 31 5d Using = pItem[1]
1daae 2e 70 55 73 69 6e 67 29 21 3d 30 20 29 7b 0a 20 .pUsing)!=0 ){.
1daaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1dab0 2a 20 49 66 20 74 68 69 73 20 6d 61 74 63 68 20 * If this match
1dab1 6f 63 63 75 72 73 20 6f 6e 20 61 20 63 6f 6c 75 occurs on a colu
1dab2 6d 6e 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 mn that is in th
1dab3 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 0a 20 e USING clause.
1dab4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
1dab5 2a 20 6f 66 20 61 20 6a 6f 69 6e 2c 20 73 6b 69 * of a join, ski
1dab6 70 20 74 68 65 20 73 65 61 72 63 68 20 6f 66 20 p the search of
1dab7 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 the right table
1dab8 6f 66 20 74 68 65 20 6a 6f 69 6e 0a 20 20 20 20 of the join.
1dab9 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ** t
1daba 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 o avoid a duplic
1dabb 61 74 65 20 6d 61 74 63 68 20 74 68 65 72 65 2e ate match there.
1dabc 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
1dabd 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 int k;.
1dabe 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b for(k
1dabf 3d 30 3b 20 6b 3c 70 55 73 69 6e 67 2d 3e 6e 49 =0; k<pUsing->nI
1dac0 64 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 d; k++){.
1dac1 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 if( s
1dac2 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 55 qlite3StrICmp(pU
1dac3 73 69 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 sing->a[k].zName
1dac4 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 , zCol)==0 ){.
1dac5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dac6 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 pItem++;.
1dac7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
1dac8 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ++;.
1dac9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1daca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dacb 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
1dacc 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
1dacd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
1dace 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 }. b
1dacf 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
1dad0 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1dad1 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 }. }..#ifnd
1dad2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1dad3 52 49 47 47 45 52 0a 20 20 20 20 2f 2a 20 49 66 RIGGER. /* If
1dad4 20 77 65 20 68 61 76 65 20 6e 6f 74 20 61 6c 72 we have not alr
1dad5 65 61 64 79 20 72 65 73 6f 6c 76 65 64 20 74 68 eady resolved th
1dad6 65 20 6e 61 6d 65 2c 20 74 68 65 6e 20 6d 61 79 e name, then may
1dad7 62 65 20 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 be . ** it is
1dad8 20 61 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e a new.* or old.
1dad9 2a 20 74 72 69 67 67 65 72 20 61 72 67 75 6d 65 * trigger argume
1dada 6e 74 20 72 65 66 65 72 65 6e 63 65 0a 20 20 20 nt reference.
1dadb 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62 3d */. if( zDb=
1dadc 3d 30 20 26 26 20 7a 54 61 62 21 3d 30 20 26 26 =0 && zTab!=0 &&
1dadd 20 63 6e 74 3d 3d 30 20 26 26 20 70 50 61 72 73 cnt==0 && pPars
1dade 65 2d 3e 74 72 69 67 53 74 61 63 6b 21 3d 30 20 e->trigStack!=0
1dadf 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 ){. Trigger
1dae0 53 74 61 63 6b 20 2a 70 54 72 69 67 67 65 72 53 Stack *pTriggerS
1dae1 74 61 63 6b 20 3d 20 70 50 61 72 73 65 2d 3e 74 tack = pParse->t
1dae2 72 69 67 53 74 61 63 6b 3b 0a 20 20 20 20 20 20 rigStack;.
1dae3 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b Table *pTab = 0;
1dae4 0a 20 20 20 20 20 20 75 33 32 20 2a 70 69 43 6f . u32 *piCo
1dae5 6c 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 20 lMask = 0;.
1dae6 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74 61 if( pTriggerSta
1dae7 63 6b 2d 3e 6e 65 77 49 64 78 20 21 3d 20 2d 31 ck->newIdx != -1
1dae8 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1dae9 6d 70 28 22 6e 65 77 22 2c 20 7a 54 61 62 29 20 mp("new", zTab)
1daea 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 == 0 ){.
1daeb 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
1daec 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e pTriggerStack->n
1daed 65 77 49 64 78 3b 0a 20 20 20 20 20 20 20 20 61 ewIdx;. a
1daee 73 73 65 72 74 28 20 70 54 72 69 67 67 65 72 53 ssert( pTriggerS
1daef 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20 tack->pTab );.
1daf0 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 72 pTab = pTr
1daf1 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 iggerStack->pTab
1daf2 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f 6c 4d ;. piColM
1daf3 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67 65 72 ask = &(pTrigger
1daf4 53 74 61 63 6b 2d 3e 6e 65 77 43 6f 6c 4d 61 73 Stack->newColMas
1daf5 6b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 k);. }else
1daf6 69 66 28 20 70 54 72 69 67 67 65 72 53 74 61 63 if( pTriggerStac
1daf7 6b 2d 3e 6f 6c 64 49 64 78 20 21 3d 20 2d 31 20 k->oldIdx != -1
1daf8 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
1daf9 70 28 22 6f 6c 64 22 2c 20 7a 54 61 62 29 3d 3d p("old", zTab)==
1dafa 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 0 ){. pEx
1dafb 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 54 72 pr->iTable = pTr
1dafc 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 49 iggerStack->oldI
1dafd 64 78 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 dx;. asse
1dafe 72 74 28 20 70 54 72 69 67 67 65 72 53 74 61 63 rt( pTriggerStac
1daff 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20 20 20 20 k->pTab );.
1db00 20 20 20 70 54 61 62 20 3d 20 70 54 72 69 67 67 pTab = pTrigg
1db01 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 erStack->pTab;.
1db02 20 20 20 20 20 20 20 70 69 43 6f 6c 4d 61 73 6b piColMask
1db03 20 3d 20 26 28 70 54 72 69 67 67 65 72 53 74 61 = &(pTriggerSta
1db04 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d 61 73 6b 29 3b ck->oldColMask);
1db05 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
1db06 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20 20 20 if( pTab ){ .
1db07 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 int iCol;.
1db08 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 Column *p
1db09 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c Col = pTab->aCol
1db0a 3b 0a 0a 20 20 20 20 20 20 20 20 70 53 63 68 65 ;.. pSche
1db0b 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 ma = pTab->pSche
1db0c 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 54 ma;. cntT
1db0d 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 66 6f ab++;. fo
1db0e 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c r(iCol=0; iCol <
1db0f 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f pTab->nCol; iCo
1db10 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 20 7b 0a 20 l++, pCol++) {.
1db11 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
1db12 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c ite3StrICmp(pCol
1db13 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d ->zName, zCol)==
1db14 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1db15 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 cnt++;.
1db16 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 pExpr->iColu
1db17 6d 6e 20 3d 20 69 43 6f 6c 3d 3d 70 54 61 62 2d mn = iCol==pTab-
1db18 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 69 43 >iPKey ? -1 : iC
1db19 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ol;.
1db1a 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 pExpr->pTab = pT
1db1b 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ab;.
1db1c 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20 if( iCol>=0 ){.
1db1d 20 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73 tes
1db1e 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 31 20 tcase( iCol==31
1db1f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
1db20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d testcase( iCol=
1db21 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 20 =32 );.
1db22 20 20 20 20 20 2a 70 69 43 6f 6c 4d 61 73 6b 20 *piColMask
1db23 7c 3d 20 28 28 75 33 32 29 31 3c 3c 69 43 6f 6c |= ((u32)1<<iCol
1db24 29 20 7c 20 28 69 43 6f 6c 3e 3d 33 32 3f 30 78 ) | (iCol>=32?0x
1db25 66 66 66 66 66 66 66 66 3a 30 29 3b 0a 20 20 20 ffffffff:0);.
1db26 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1db27 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1db28 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1db29 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1db2a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 }.#endif /* !def
1db2b 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1db2c 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20 20 _TRIGGER) */..
1db2d 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72 68 /*. ** Perh
1db2e 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73 20 aps the name is
1db2f 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 a reference to t
1db30 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f 0a he ROWID. */.
1db31 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 if( cnt==0 &
1db32 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20 73 & cntTab==1 && s
1db33 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 qlite3IsRowid(zC
1db34 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 ol) ){. cnt
1db35 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78 70 = 1;. pExp
1db36 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b r->iColumn = -1;
1db37 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 . pExpr->af
1db38 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f finity = SQLITE_
1db39 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 AFF_INTEGER;.
1db3a 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a }.. /*. *
1db3b 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 69 * If the input i
1db3c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a 20 s of the form Z
1db3d 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59 2e (not Y.Z or X.Y.
1db3e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65 Z) then the name
1db3f 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 Z. ** might
1db40 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73 75 refer to an resu
1db41 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 54 lt-set alias. T
1db42 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 his happens, for
1db43 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 20 example, when.
1db44 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 73 ** we are res
1db45 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 olving names in
1db46 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
1db47 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
1db48 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20 2a g command:. *
1db49 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45 4c *. ** SEL
1db4a 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52 4f ECT a+b AS x FRO
1db4b 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78 3c M table WHERE x<
1db4c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 10;. **. *
1db4d 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65 20 * In cases like
1db4e 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70 45 this, replace pE
1db4f 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 79 20 xpr with a copy
1db50 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
1db51 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66 6f n that. ** fo
1db52 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 rms the result s
1db53 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22 20 et entry ("a+b"
1db54 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29 20 in the example)
1db55 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 and return immed
1db56 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 4e iately.. ** N
1db57 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 78 70 ote that the exp
1db58 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 ression in the r
1db59 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c 64 esult set should
1db5a 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 have already be
1db5b 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76 en. ** resolv
1db5c 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 20 74 ed by the time t
1db5d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
1db5e 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20 is resolved..
1db5f 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d */. if( cnt=
1db60 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d 20 =0 && (pEList =
1db61 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30 20 pNC->pEList)!=0
1db62 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20 20 && zTab==0 ){.
1db63 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 for(j=0; j<p
1db64 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a 2b EList->nExpr; j+
1db65 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 +){. char
1db66 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e *zAs = pEList->
1db67 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 a[j].zName;.
1db68 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26 if( zAs!=0 &
1db69 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 & sqlite3StrICmp
1db6a 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 (zAs, zCol)==0 )
1db6b 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 {. Expr
1db6c 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 20 *pOrig;.
1db6d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1db6e 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 ->pLeft==0 && pE
1db6f 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 xpr->pRight==0 )
1db70 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 ;. asse
1db71 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 rt( pExpr->x.pLi
1db72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 st==0 );.
1db73 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1db74 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 29 ->x.pSelect==0 )
1db75 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 69 ;. pOri
1db76 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d g = pEList->a[j]
1db77 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 .pExpr;.
1db78 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f if( !pNC->allo
1db79 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73 50 wAgg && ExprHasP
1db7a 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 roperty(pOrig, E
1db7b 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 P_Agg) ){.
1db7c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1db7d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d orMsg(pParse, "m
1db7e 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64 isuse of aliased
1db7f 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c 20 aggregate %s",
1db80 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 zAs);.
1db81 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1db82 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 db, zCol);.
1db83 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b return 2;
1db84 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1db85 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c resolveAl
1db86 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 ias(pParse, pELi
1db87 73 74 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 st, j, pExpr, ""
1db88 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 );. cnt
1db89 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 = 1;.
1db8a 70 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 pMatch = 0;.
1db8b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 assert( zT
1db8c 61 62 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 ab==0 && zDb==0
1db8d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 );. got
1db8e 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 o lookupname_end
1db8f 5f 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 _2;. }.
1db90 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 } . }..
1db91 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 /* Advance to
1db92 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f the next name co
1db93 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 ntext. The loop
1db94 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 will exit when
1db95 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 either. ** we
1db96 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 28 63 have a match (c
1db97 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65 nt>0) or when we
1db98 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 run out of name
1db99 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a contexts.. *
1db9a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 /. if( cnt==0
1db9b 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 ){. pNC =
1db9c 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 pNC->pNext;.
1db9d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a }. }.. /*. **
1db9e 20 49 66 20 58 20 61 6e 64 20 59 20 61 72 65 20 If X and Y are
1db9f 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 NULL (in other w
1dba0 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 ords if only the
1dba1 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 column name Z i
1dba2 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 s. ** supplied)
1dba3 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f and the value o
1dba4 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 f Z is enclosed
1dba5 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 in double-quotes
1dba6 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 , then. ** Z is
1dba7 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 a string litera
1dba8 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 l if it doesn't
1dba9 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e match any column
1dbaa 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 names. In that
1dbab 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e . ** case, we n
1dbac 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69 eed to return ri
1dbad 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 ght away and not
1dbae 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 make any change
1dbaf 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e s to. ** pExpr.
1dbb0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 . **. ** Becau
1dbb1 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 se no reference
1dbb2 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 was made to oute
1dbb3 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 r contexts, the
1dbb4 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 pNC->nRef. ** f
1dbb5 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 ields are not ch
1dbb6 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e anged in any con
1dbb7 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 text.. */. if(
1dbb8 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d cnt==0 && zTab=
1dbb9 3d 30 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b =0 && pColumnTok
1dbba 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 7b en->z[0]=='"' ){
1dbbb 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1dbbc 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 ee(db, zCol);.
1dbbd 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b pExpr->op = TK
1dbbe 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 78 _STRING;. pEx
1dbbf 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 pr->pTab = 0;.
1dbc0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1dbc1 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d . /*. ** cnt==
1dbc2 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61 0 means there wa
1dbc3 73 20 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e s not match. cn
1dbc4 74 3e 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20 t>1 means there
1dbc5 77 65 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a 2a were two or. **
1dbc6 20 6d 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20 more matches.
1dbc7 45 69 74 68 65 72 20 77 61 79 2c 20 77 65 20 68 Either way, we h
1dbc8 61 76 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 ave an error..
1dbc9 2a 2f 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 20 */. if( cnt!=1
1dbca 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 ){. const cha
1dbcb 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 72 r *zErr;. zEr
1dbcc 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f r = cnt==0 ? "no
1dbcd 20 73 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 such column" :
1dbce 22 61 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 6d "ambiguous colum
1dbcf 6e 20 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 28 n name";. if(
1dbd0 20 7a 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71 zDb ){. sq
1dbd1 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1dbd2 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 arse, "%s: %s.%s
1dbd3 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 2c .%s", zErr, zDb,
1dbd4 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 zTab, zCol);.
1dbd5 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 62 }else if( zTab
1dbd6 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1dbd7 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1dbd8 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a , "%s: %s.%s", z
1dbd9 45 72 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 Err, zTab, zCol)
1dbda 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1dbdb 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1dbdc 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 sg(pParse, "%s:
1dbdd 25 73 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 %s", zErr, zCol)
1dbde 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 70 ;. }. pTop
1dbdf 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a NC->nErr++;. }.
1dbe0 0a 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d . /* If a colum
1dbe1 6e 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 69 n from a table i
1dbe2 6e 20 70 53 72 63 4c 69 73 74 20 69 73 20 72 65 n pSrcList is re
1dbe3 66 65 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 72 ferenced, then r
1dbe4 65 63 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 20 ecord. ** this
1dbe5 66 61 63 74 20 69 6e 20 74 68 65 20 70 53 72 63 fact in the pSrc
1dbe6 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 List.a[].colUsed
1dbe7 20 62 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d bitmask. Colum
1dbe8 6e 20 30 20 63 61 75 73 65 73 0a 20 20 2a 2a 20 n 0 causes. **
1dbe9 62 69 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e bit 0 to be set.
1dbea 20 20 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20 Column 1 sets
1dbeb 62 69 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66 bit 1. And so f
1dbec 6f 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 orth. If the.
1dbed 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 ** column number
1dbee 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
1dbef 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
1dbf0 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 its in the bitma
1dbf1 73 6b 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 sk. ** then set
1dbf2 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 the high-order
1dbf3 62 69 74 20 6f 66 20 74 68 65 20 62 69 74 6d 61 bit of the bitma
1dbf4 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 sk.. */. if( p
1dbf5 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 Expr->iColumn>=0
1dbf6 20 26 26 20 70 4d 61 74 63 68 21 3d 30 20 29 7b && pMatch!=0 ){
1dbf7 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 . int n = pEx
1dbf8 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 pr->iColumn;.
1dbf9 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 42 4d testcase( n==BM
1dbfa 53 2d 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e S-1 );. if( n
1dbfb 3e 3d 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 6e >=BMS ){. n
1dbfc 20 3d 20 42 4d 53 2d 31 3b 0a 20 20 20 20 7d 0a = BMS-1;. }.
1dbfd 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 61 74 assert( pMat
1dbfe 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d 70 45 78 ch->iCursor==pEx
1dbff 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 pr->iTable );.
1dc00 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c 55 73 65 pMatch->colUse
1dc01 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29 31 d |= ((Bitmask)1
1dc02 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 6c 6f 6f 6b 75 )<<n;. }..looku
1dc03 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 2f 2a 20 pname_end:. /*
1dc04 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74 Clean up and ret
1dc05 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 urn. */. sqlit
1dc06 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 44 62 e3DbFree(db, zDb
1dc07 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1dc08 65 65 28 64 62 2c 20 7a 54 61 62 29 3b 0a 20 20 ee(db, zTab);.
1dc09 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1dc0a 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 e(db, pExpr->pLe
1dc0b 66 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c ft);. pExpr->pL
1dc0c 65 66 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 eft = 0;. sqlit
1dc0d 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
1dc0e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b pExpr->pRight);
1dc0f 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 . pExpr->pRight
1dc10 20 3d 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e 6f = 0;. pExpr->o
1dc11 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 6c p = TK_COLUMN;.l
1dc12 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32 3a ookupname_end_2:
1dc13 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1dc14 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69 66 (db, zCol);. if
1dc15 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 20 ( cnt==1 ){.
1dc16 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 assert( pNC!=0 )
1dc17 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 ;. sqlite3Aut
1dc18 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 70 45 hRead(pParse, pE
1dc19 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 70 4e xpr, pSchema, pN
1dc1a 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20 20 C->pSrcList);.
1dc1b 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 /* Increment t
1dc1c 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f 6e he nRef value on
1dc1d 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65 78 all name contex
1dc1e 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75 70 ts from TopNC up
1dc1f 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 to. ** the p
1dc20 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 20 6e oint where the n
1dc21 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f 0a ame matched. */.
1dc22 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 for(;;){.
1dc23 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 70 4e assert( pTopN
1dc24 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 54 C!=0 );. pT
1dc25 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 opNC->nRef++;.
1dc26 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 3d 3d if( pTopNC==
1dc27 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 pNC ) break;.
1dc28 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f 70 pTopNC = pTop
1dc29 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d NC->pNext;. }
1dc2a 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
1dc2b 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 } else {. re
1dc2c 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 1;. }.}../
1dc2d 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1dc2e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f e is callback fo
1dc2f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 r sqlite3WalkExp
1dc30 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f 6c r()..**.** Resol
1dc31 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 ve symbolic name
1dc32 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e s into TK_COLUMN
1dc33 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 20 74 operators for t
1dc34 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e 6f he current.** no
1dc35 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 de in the expres
1dc36 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 sion tree. Retu
1dc37 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75 65 rn 0 to continue
1dc38 20 74 68 65 20 73 65 61 72 63 68 20 64 6f 77 6e the search down
1dc39 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f 72 20 .** the tree or
1dc3a 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20 74 2 to abort the t
1dc3b 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 ree walk..**.**
1dc3c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 This routine als
1dc3d 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 68 65 o does error che
1dc3e 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20 72 cking and name r
1dc3f 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a 2a esolution for.**
1dc40 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e function names.
1dc41 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 66 The operator f
1dc42 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 6e or aggregate fun
1dc43 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e 67 65 ctions is change
1dc44 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f 46 d.** to TK_AGG_F
1dc45 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 UNCTION..*/.stat
1dc46 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 45 78 ic int resolveEx
1dc47 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 prStep(Walker *p
1dc48 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 Walker, Expr *pE
1dc49 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 xpr){. NameCont
1dc4a 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72 73 ext *pNC;. Pars
1dc4b 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70 4e e *pParse;.. pN
1dc4c 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 C = pWalker->u.p
1dc4d 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e NC;. assert( pN
1dc4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65 C!=0 );. pParse
1dc4f 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a = pNC->pParse;.
1dc50 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
1dc51 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 ==pWalker->pPars
1dc52 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 70 72 e );.. if( Expr
1dc53 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 HasAnyProperty(p
1dc54 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 Expr, EP_Resolve
1dc55 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f d) ) return WRC_
1dc56 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 65 74 Prune;. ExprSet
1dc57 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
1dc58 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23 69 EP_Resolved);.#i
1dc59 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 fndef NDEBUG. i
1dc5a 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 f( pNC->pSrcList
1dc5b 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 && pNC->pSrcLis
1dc5c 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a 20 t->nAlloc>0 ){.
1dc5d 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 SrcList *pSrc
1dc5e 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 List = pNC->pSrc
1dc5f 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b List;. int i;
1dc60 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1dc61 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e pNC->pSrcList->n
1dc62 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Src; i++){.
1dc63 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 73 assert( pSrcLis
1dc64 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3e t->a[i].iCursor>
1dc65 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e =0 && pSrcList->
1dc66 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 61 a[i].iCursor<pPa
1dc67 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20 20 rse->nTab);.
1dc68 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 }. }.#endif. s
1dc69 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 witch( pExpr->op
1dc6a 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 ){..#if defined
1dc6b 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 (SQLITE_ENABLE_U
1dc6c 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d PDATE_DELETE_LIM
1dc6d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 IT) && !defined(
1dc6e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1dc6f 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 68 65 UERY). /* The
1dc70 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f special operato
1dc71 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20 75 r TK_ROW means u
1dc72 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 se the rowid for
1dc73 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 2a the first. *
1dc74 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 * column in the
1dc75 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 FROM clause. Th
1dc76 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68 is is used by th
1dc77 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44 45 e LIMIT and ORDE
1dc78 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75 R BY. ** clau
1dc79 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 6e se processing on
1dc7a 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 4c 45 UPDATE and DELE
1dc7b 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 TE statements..
1dc7c 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 */. case T
1dc7d 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20 53 K_ROW: {. S
1dc7e 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 rcList *pSrcList
1dc7f 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 = pNC->pSrcList
1dc80 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 ;. struct S
1dc81 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 rcList_item *pIt
1dc82 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 em;. assert
1dc83 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 70 53 ( pSrcList && pS
1dc84 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 rcList->nSrc==1
1dc85 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 20 3d );. pItem =
1dc86 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a 20 pSrcList->a; .
1dc87 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d pExpr->op =
1dc88 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 TK_COLUMN;.
1dc89 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 pExpr->pTab =
1dc8a 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 pItem->pTab;.
1dc8b 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1dc8c 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f = pItem->iCurso
1dc8d 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e r;. pExpr->
1dc8e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 iColumn = -1;.
1dc8f 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e pExpr->affin
1dc90 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 ity = SQLITE_AFF
1dc91 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 _INTEGER;.
1dc92 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
1dc93 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 dif /* defined(S
1dc94 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 QLITE_ENABLE_UPD
1dc95 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 ATE_DELETE_LIMIT
1dc96 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
1dc97 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1dc98 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 RY) */.. /* A
1dc99 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 65 72 lone identifier
1dc9a 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
1dc9b 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f a column.. */
1dc9c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a . case TK_ID:
1dc9d 20 7b 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 4e {. lookupN
1dc9e 61 6d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30 ame(pParse, 0, 0
1dc9f 2c 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2c , &pExpr->token,
1dca0 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 pNC, pExpr);.
1dca1 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 return WRC_P
1dca2 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 rune;. }. .
1dca3 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e 61 /* A table na
1dca4 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 me and column na
1dca5 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0a 20 20 me: ID.ID.
1dca6 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61 62 61 ** Or a databa
1dca7 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 63 6f se, table and co
1dca8 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44 0a lumn: ID.ID.ID.
1dca9 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 */. case
1dcaa 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 20 20 TK_DOT: {.
1dcab 54 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 3b 0a Token *pColumn;.
1dcac 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 61 Token *pTa
1dcad 62 6c 65 3b 0a 20 20 20 20 20 20 54 6f 6b 65 6e ble;. Token
1dcae 20 2a 70 44 62 3b 0a 20 20 20 20 20 20 45 78 70 *pDb;. Exp
1dcaf 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20 20 r *pRight;..
1dcb0 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c 69 73 /* if( pSrcLis
1dcb1 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a 2f t==0 ) break; */
1dcb2 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 . pRight =
1dcb3 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 pExpr->pRight;.
1dcb4 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d if( pRight-
1dcb5 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 >op==TK_ID ){.
1dcb6 20 20 20 20 20 20 70 44 62 20 3d 20 30 3b 0a 20 pDb = 0;.
1dcb7 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20 pTable =
1dcb8 26 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 74 &pExpr->pLeft->t
1dcb9 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 43 oken;. pC
1dcba 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 2d olumn = &pRight-
1dcbb 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65 >token;. }e
1dcbc 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
1dcbd 65 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d ert( pRight->op=
1dcbe 3d 54 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20 =TK_DOT );.
1dcbf 20 20 20 70 44 62 20 3d 20 26 70 45 78 70 72 2d pDb = &pExpr-
1dcc0 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 >pLeft->token;.
1dcc1 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20 pTable =
1dcc2 26 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e &pRight->pLeft->
1dcc3 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 token;. p
1dcc4 43 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 Column = &pRight
1dcc5 2d 3e 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b ->pRight->token;
1dcc6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c . }. l
1dcc7 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73 65 ookupName(pParse
1dcc8 2c 20 70 44 62 2c 20 70 54 61 62 6c 65 2c 20 70 , pDb, pTable, p
1dcc9 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 45 78 Column, pNC, pEx
1dcca 70 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 pr);. retur
1dccb 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 n WRC_Prune;.
1dccc 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c }.. /* Resol
1dccd 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 ve function name
1dcce 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 s. */. cas
1dccf 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a e TK_CONST_FUNC:
1dcd0 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e . case TK_FUN
1dcd1 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 CTION: {. E
1dcd2 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d xprList *pList =
1dcd3 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b pExpr->x.pList;
1dcd4 20 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d /* The argum
1dcd5 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20 20 ent list */.
1dcd6 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 20 int n = pList
1dcd7 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a ? pList->nExpr :
1dcd8 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 0; /* Number
1dcd9 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f of arguments */
1dcda 0a 20 20 20 20 20 20 69 6e 74 20 6e 6f 5f 73 75 . int no_su
1dcdb 63 68 5f 66 75 6e 63 20 3d 20 30 3b 20 20 20 20 ch_func = 0;
1dcdc 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f /* True if no
1dcdd 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 such function e
1dcde 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 xists */. i
1dcdf 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 nt wrong_num_arg
1dce0 73 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 s = 0; /* Tr
1dce1 75 65 20 69 66 20 77 72 6f 6e 67 20 6e 75 6d 62 ue if wrong numb
1dce2 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
1dce3 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 5f */. int is_
1dce4 61 67 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 agg = 0;
1dce5 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1dce6 69 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 is an aggregate
1dce7 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 function */.
1dce8 20 20 69 6e 74 20 61 75 74 68 3b 20 20 20 20 20 int auth;
1dce9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1dcea 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 Authorization t
1dceb 6f 20 75 73 65 20 74 68 65 20 66 75 6e 63 74 69 o use the functi
1dcec 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 on */. int
1dced 6e 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 nId;
1dcee 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1dcef 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 r of characters
1dcf0 69 6e 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 in function name
1dcf1 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 */. const
1dcf2 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 char *zId;
1dcf3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e /* The fun
1dcf4 63 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20 ction name. */.
1dcf5 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 FuncDef *pD
1dcf6 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
1dcf7 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
1dcf8 61 62 6f 75 74 20 74 68 65 20 66 75 6e 63 74 69 about the functi
1dcf9 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 on */. u8 e
1dcfa 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d nc = ENC(pParse-
1dcfb 3e 64 62 29 3b 20 20 20 2f 2a 20 54 68 65 20 64 >db); /* The d
1dcfc 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 atabase encoding
1dcfd 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72 */.. asser
1dcfe 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 t( !ExprHasPrope
1dcff 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 rty(pExpr, EP_xI
1dd00 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 sSelect) );.
1dd01 20 20 7a 49 64 20 3d 20 28 63 68 61 72 2a 29 70 zId = (char*)p
1dd02 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 Expr->token.z;.
1dd03 20 20 20 20 20 6e 49 64 20 3d 20 70 45 78 70 72 nId = pExpr
1dd04 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20 ->token.n;.
1dd05 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 pDef = sqlite3F
1dd06 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 indFunction(pPar
1dd07 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 se->db, zId, nId
1dd08 2c 20 6e 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 , n, enc, 0);.
1dd09 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 if( pDef==0
1dd0a 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 ){. pDef
1dd0b 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e = sqlite3FindFun
1dd0c 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 ction(pParse->db
1dd0d 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20 , zId, nId, -1,
1dd0e 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 enc, 0);.
1dd0f 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a if( pDef==0 ){.
1dd10 20 20 20 20 20 20 20 20 20 20 6e 6f 5f 73 75 63 no_suc
1dd11 68 5f 66 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20 h_func = 1;.
1dd12 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1dd13 20 20 20 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 wrong_num_a
1dd14 72 67 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 rgs = 1;.
1dd15 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1dd16 20 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d is_agg =
1dd17 20 70 44 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b pDef->xFunc==0;
1dd18 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 . }.#ifndef
1dd19 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1dd1a 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 HORIZATION.
1dd1b 20 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20 if( pDef ){.
1dd1c 20 20 20 20 20 61 75 74 68 20 3d 20 73 71 6c 69 auth = sqli
1dd1d 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
1dd1e 72 73 65 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 rse, SQLITE_FUNC
1dd1f 54 49 4f 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a TION, 0, pDef->z
1dd20 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Name, 0);.
1dd21 20 20 69 66 28 20 61 75 74 68 21 3d 53 51 4c 49 if( auth!=SQLI
1dd22 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1dd23 20 20 20 69 66 28 20 61 75 74 68 3d 3d 53 51 4c if( auth==SQL
1dd24 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 ITE_DENY ){.
1dd25 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1dd26 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1dd27 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 20 "not authorized
1dd28 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a to use function:
1dd29 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 %s",.
1dd2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd2b 20 20 20 20 20 20 20 20 20 20 70 44 65 66 2d 3e pDef->
1dd2c 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 zName);.
1dd2d 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b pNC->nErr++;
1dd2e 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1dd2f 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 pExpr->op
1dd30 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 = TK_NULL;.
1dd31 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1dd32 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 20 20 _Prune;.
1dd33 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
1dd34 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 . if( is_ag
1dd35 67 20 26 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 g && !pNC->allow
1dd36 41 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Agg ){. s
1dd37 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1dd38 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f Parse, "misuse o
1dd39 66 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 f aggregate func
1dd3a 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49 tion %.*s()", nI
1dd3b 64 2c 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 d,zId);.
1dd3c 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 pNC->nErr++;.
1dd3d 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 30 3b is_agg = 0;
1dd3e 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
1dd3f 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b no_such_func ){
1dd40 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1dd41 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1dd42 20 22 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 "no such functi
1dd43 6f 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 on: %.*s", nId,
1dd44 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e zId);. pN
1dd45 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 C->nErr++;.
1dd46 20 7d 65 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 }else if( wrong
1dd47 5f 6e 75 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20 _num_args ){.
1dd48 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1dd49 72 4d 73 67 28 70 50 61 72 73 65 2c 22 77 72 6f rMsg(pParse,"wro
1dd4a 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 ng number of arg
1dd4b 75 6d 65 6e 74 73 20 74 6f 20 66 75 6e 63 74 69 uments to functi
1dd4c 6f 6e 20 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20 on %.*s()",.
1dd4d 20 20 20 20 20 20 20 20 20 6e 49 64 2c 20 7a 49 nId, zI
1dd4e 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d d);. pNC-
1dd4f 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d >nErr++;. }
1dd50 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 . if( is_ag
1dd51 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 g ){. pEx
1dd52 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f pr->op = TK_AGG_
1dd53 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20 FUNCTION;.
1dd54 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 pNC->hasAgg =
1dd55 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
1dd56 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e if( is_agg ) pN
1dd57 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b C->allowAgg = 0;
1dd58 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 . sqlite3Wa
1dd59 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b lkExprList(pWalk
1dd5a 65 72 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 er, pList);.
1dd5b 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 if( is_agg ) p
1dd5c 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 NC->allowAgg = 1
1dd5d 3b 0a 20 20 20 20 20 20 2f 2a 20 46 49 58 20 4d ;. /* FIX M
1dd5e 45 3a 20 20 43 6f 6d 70 75 74 65 20 70 45 78 70 E: Compute pExp
1dd5f 72 2d 3e 61 66 66 69 6e 69 74 79 20 62 61 73 65 r->affinity base
1dd60 64 20 6f 6e 20 74 68 65 20 65 78 70 65 63 74 65 d on the expecte
1dd61 64 20 72 65 74 75 72 6e 0a 20 20 20 20 20 20 2a d return. *
1dd62 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 75 * type of the fu
1dd63 6e 63 74 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f nction . */
1dd64 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 . return WR
1dd65 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 C_Prune;. }.#
1dd66 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1dd67 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 IT_SUBQUERY.
1dd68 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a case TK_SELECT:.
1dd69 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 case TK_EXIS
1dd6a 54 53 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 TS:.#endif. c
1dd6b 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 ase TK_IN: {.
1dd6c 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
1dd6d 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1dd6e 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
1dd6f 20 20 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 int nRef
1dd70 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 = pNC->nRef;.#if
1dd71 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1dd72 5f 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69 _CHECK. i
1dd73 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 f( pNC->isCheck
1dd74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
1dd75 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1dd76 72 73 65 2c 22 73 75 62 71 75 65 72 69 65 73 20 rse,"subqueries
1dd77 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 prohibited in CH
1dd78 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 ECK constraints"
1dd79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e );. }.#en
1dd7a 64 69 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 dif. sqli
1dd7b 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 te3WalkSelect(pW
1dd7c 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e alker, pExpr->x.
1dd7d 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 pSelect);.
1dd7e 20 20 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e assert( pNC->n
1dd7f 52 65 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 Ref>=nRef );.
1dd80 20 20 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 if( nRef!=p
1dd81 4e 43 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 NC->nRef ){.
1dd82 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f ExprSetPro
1dd83 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1dd84 56 61 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 VarSelect);.
1dd85 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1dd86 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1dd87 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1dd88 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 OMIT_CHECK. c
1dd89 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a ase TK_VARIABLE:
1dd8a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 {. if( pNC
1dd8b 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 ->isCheck ){.
1dd8c 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1dd8d 72 4d 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 rMsg(pParse,"par
1dd8e 61 6d 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 ameters prohibit
1dd8f 65 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 ed in CHECK cons
1dd90 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 traints");.
1dd91 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1dd92 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
1dd93 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 . return (pPars
1dd94 65 2d 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 e->nErr || pPars
1dd95 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
1dd96 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 led) ? WRC_Abort
1dd97 20 3a 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b : WRC_Continue;
1dd98 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 .}../*.** pEList
1dd99 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 is a list of ex
1dd9a 70 72 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 pressions which
1dd9b 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 are really the r
1dd9c 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 esult set of the
1dd9d 0a 2a 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 .** a SELECT sta
1dd9e 74 65 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 tement. pE is a
1dd9f 20 74 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 term in an ORDE
1dda0 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 R BY or GROUP BY
1dda1 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 clause..** This
1dda2 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
1dda3 74 6f 20 73 65 65 20 69 66 20 70 45 20 69 73 20 to see if pE is
1dda4 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 a simple identif
1dda5 69 65 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 ier which corres
1dda6 70 6f 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 ponds.** to the
1dda7 41 53 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f AS-name of one o
1dda8 66 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 f the terms of t
1dda9 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 he expression li
1ddaa 73 74 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a st. If it is,.*
1ddab 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * this routine r
1ddac 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 eturn an integer
1ddad 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e between 1 and N
1ddae 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 where N is the
1ddaf 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 number of.** ele
1ddb0 6d 65 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c ments in pEList,
1ddb1 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1ddb2 6f 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 o the matching e
1ddb3 6e 74 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 ntry. If there
1ddb4 69 73 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 is.** no match,
1ddb5 6f 72 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 or if pE is not
1ddb6 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 a simple identif
1ddb7 69 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 ier, then this r
1ddb8 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e outine.** return
1ddb9 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 0..**.** pEList
1ddba 20 68 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 has been resolv
1ddbb 65 64 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e ed. pE has not.
1ddbc 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
1ddbd 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 esolveAsName(.
1ddbe 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1ddbf 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
1ddc0 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 ntext for error
1ddc1 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 messages */. Ex
1ddc2 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 prList *pEList,
1ddc3 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 /* List of expr
1ddc4 65 73 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 essions to scan
1ddc5 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 */. Expr *pE
1ddc6 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 /* Expre
1ddc7 73 73 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 ssion we are try
1ddc8 69 6e 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a ing to match */.
1ddc9 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
1ddca 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1ddcb 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 69 66 counter */.. if
1ddcc 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 ( pE->op==TK_ID
1ddcd 7c 7c 20 28 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 53 || (pE->op==TK_S
1ddce 54 52 49 4e 47 20 26 26 20 70 45 2d 3e 74 6f 6b TRING && pE->tok
1ddcf 65 6e 2e 7a 5b 30 5d 21 3d 27 5c 27 27 29 20 29 en.z[0]!='\'') )
1ddd0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 {. sqlite3 *d
1ddd1 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1ddd2 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d char *zCol =
1ddd3 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
1ddd4 54 6f 6b 65 6e 28 64 62 2c 20 26 70 45 2d 3e 74 Token(db, &pE->t
1ddd5 6f 6b 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a oken);. if( z
1ddd6 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Col==0 ){.
1ddd7 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d return -1;. }
1ddd8 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1ddd9 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 pEList->nExpr; i
1ddda 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 ++){. char
1dddb 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61 *zAs = pEList->a
1dddc 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 [i].zName;.
1dddd 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 if( zAs!=0 && s
1ddde 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 qlite3StrICmp(zA
1dddf 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 s, zCol)==0 ){.
1dde0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
1dde1 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a Free(db, zCol);.
1dde2 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 return i
1dde3 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 +1;. }.
1dde4 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 }. sqlite3DbF
1dde5 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 ree(db, zCol);.
1dde6 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
1dde7 0a 0a 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 ../*.** pE is a
1dde8 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 pointer to an ex
1dde9 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 pression which i
1ddea 73 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 s a single term
1ddeb 69 6e 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 in the.** ORDER
1ddec 42 59 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 BY of a compound
1dded 20 53 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 SELECT. The ex
1ddee 70 72 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 pression has not
1ddef 20 62 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 been.** name re
1ddf0 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 solved..**.** At
1ddf1 20 74 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 the point this
1ddf2 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
1ddf3 64 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e d, we already kn
1ddf4 6f 77 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f ow that the.** O
1ddf5 52 44 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 RDER BY term is
1ddf6 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 not an integer i
1ddf7 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 ndex into the re
1ddf8 73 75 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a sult set. That.
1ddf9 2a 2a 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c ** case is handl
1ddfa 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ed by the callin
1ddfb 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a g routine..**.**
1ddfc 20 41 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 Attempt to matc
1ddfd 68 20 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 h pE against res
1ddfe 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 ult set columns
1ddff 69 6e 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 in the left-most
1de00 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 .** SELECT state
1de01 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 ment. Return th
1de02 65 20 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 e index i of the
1de03 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e matching column
1de04 2c 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 ,.** as an indic
1de05 61 74 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c ation to the cal
1de06 6c 65 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 ler that it shou
1de07 6c 64 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 ld sort by the i
1de08 2d 74 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 -th column..** T
1de09 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c he left-most col
1de0a 75 6d 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 umn is 1. In ot
1de0b 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 her words, the v
1de0c 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
1de0d 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 the.** same int
1de0e 65 67 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 eger value that
1de0f 77 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e would be used in
1de10 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
1de11 6e 74 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a nt to indicate.*
1de12 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a * the column..**
1de13 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 .** If there is
1de14 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e no match, return
1de15 20 30 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 0. Return -1 i
1de16 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1de17 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
1de18 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 resolveOrderByT
1de19 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 ermToExprList(.
1de1a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
1de1b 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
1de1c 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 ontext for error
1de1d 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 messages */. S
1de1e 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 elect *pSelect,
1de1f 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 /* The SELECT
1de20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 statement with t
1de21 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
1de22 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 se */. Expr *pE
1de23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1de24 65 20 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 e specific ORDER
1de25 20 42 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 BY term */.){.
1de26 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1de27 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1de28 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ter */. ExprLis
1de29 74 20 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 t *pEList; /* T
1de2a 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 he columns of th
1de2b 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a e result set */.
1de2c 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 NameContext nc
1de2d 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e ; /* Name con
1de2e 74 65 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 text for resolvi
1de2f 6e 67 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 ng pE */.. asse
1de30 72 74 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 rt( sqlite3ExprI
1de31 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 sInteger(pE, &i)
1de32 3d 3d 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 ==0 );. pEList
1de33 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 = pSelect->pELis
1de34 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 t;.. /* Resolve
1de35 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 all names in th
1de36 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 e ORDER BY term
1de37 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a expression. */.
1de38 20 20 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c memset(&nc, 0,
1de39 20 73 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 sizeof(nc));.
1de3a 6e 63 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 nc.pParse = pPar
1de3b 73 65 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 se;. nc.pSrcLis
1de3c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 t = pSelect->pSr
1de3d 63 3b 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d c;. nc.pEList =
1de3e 20 70 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c pEList;. nc.al
1de3f 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 lowAgg = 1;. nc
1de40 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 .nErr = 0;. if(
1de41 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
1de42 78 70 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 xprNames(&nc, pE
1de43 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
1de44 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 ErrorClear(pPars
1de45 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 e);. return 0
1de46 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 ;. }.. /* Try
1de47 74 6f 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 to match the ORD
1de48 45 52 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e ER BY expression
1de49 20 61 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 against an expr
1de4a 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 ession. ** in t
1de4b 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 he result set.
1de4c 52 65 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 Return an 1-base
1de4d 64 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d d index of the m
1de4e 61 74 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 atching. ** res
1de4f 75 6c 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 ult-set entry..
1de50 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 */. for(i=0; i
1de51 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 <pEList->nExpr;
1de52 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 i++){. if( sq
1de53 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 lite3ExprCompare
1de54 28 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 (pEList->a[i].pE
1de55 78 70 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 xpr, pE) ){.
1de56 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 return i+1;.
1de57 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
1de58 20 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 no match, retur
1de59 6e 20 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e n 0. */. return
1de5a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 0;.}../*.** Gen
1de5b 65 72 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 erate an ORDER B
1de5c 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 Y or GROUP BY te
1de5d 72 6d 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 rm out-of-range
1de5e 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 error..*/.static
1de5f 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 void resolveOut
1de60 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 OfRangeError(.
1de61 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1de62 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 /* The er
1de63 72 6f 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f ror context into
1de64 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 which to write
1de65 74 68 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 the error */. c
1de66 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 onst char *zType
1de67 2c 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 , /* "ORDER"
1de68 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 or "GROUP" */.
1de69 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 int i,
1de6a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 /* The i
1de6b 6e 64 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f ndex (1-based) o
1de6c 66 20 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f f the term out o
1de6d 66 20 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 f range */. int
1de6e 20 6d 78 20 20 20 20 20 20 20 20 20 20 20 20 20 mx
1de6f 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 /* Largest p
1de70 65 72 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 ermissible value
1de71 20 6f 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 of i */.){. sq
1de72 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1de73 61 72 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 arse, . "%r %
1de74 73 20 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 s BY term out of
1de75 20 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 range - should
1de76 62 65 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 be ". "betwee
1de77 6e 20 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 n 1 and %d", i,
1de78 7a 54 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f zType, mx);.}../
1de79 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 *.** Analyze the
1de7a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
1de7b 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 in a compound S
1de7c 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
1de7d 20 20 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 Modify.** eac
1de7e 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 h term of the OR
1de7f 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 DER BY clause is
1de80 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 a constant inte
1de81 67 65 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a ger between 1.**
1de82 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 and N where N i
1de83 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
1de84 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 columns in the c
1de85 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a ompound SELECT..
1de86 2a 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 **.** ORDER BY t
1de87 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c erms that are al
1de88 72 65 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 ready an integer
1de89 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e between 1 and N
1de8a 20 61 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 are.** unmodifi
1de8b 65 64 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 ed. ORDER BY te
1de8c 72 6d 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 rms that are int
1de8d 65 67 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 egers outside th
1de8e 65 20 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 e range of.** 1
1de8f 74 68 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 through N genera
1de90 74 65 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 te an error. OR
1de91 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 DER BY terms tha
1de92 74 20 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e t are expression
1de93 73 0a 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 s.** are matched
1de94 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 against result
1de95 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 set expressions
1de96 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 of compound SELE
1de97 43 54 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 CT.** beginning
1de98 77 69 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f with the left-mo
1de99 73 74 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f st SELECT and wo
1de9a 72 6b 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 rking toward the
1de9b 20 72 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 right..** At th
1de9c 65 20 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 e first match, t
1de9d 68 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 he ORDER BY expr
1de9e 65 73 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 ession is transf
1de9f 6f 72 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 ormed into.** th
1dea0 65 20 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e e integer column
1dea1 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 number..**.** R
1dea2 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1dea3 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e of errors seen.
1dea4 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
1dea5 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 esolveCompoundOr
1dea6 64 65 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a derBy(. Parse *
1dea7 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f pParse, /
1dea8 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
1dea9 74 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 t. Leave error
1deaa 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f messages here */
1deab 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
1deac 63 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ct /* The
1dead 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1deae 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1deaf 4f 52 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 ORDER BY */.){.
1deb0 20 69 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 int i;. ExprLi
1deb1 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 st *pOrderBy;.
1deb2 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
1deb3 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
1deb4 0a 20 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 . int moreToDo
1deb5 3d 20 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 = 1;.. pOrderBy
1deb6 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 = pSelect->pOrd
1deb7 65 72 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 erBy;. if( pOrd
1deb8 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e erBy==0 ) return
1deb9 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 0;. db = pPars
1deba 65 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 e->db;.#if SQLIT
1debb 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 E_MAX_COLUMN. i
1debc 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 f( pOrderBy->nEx
1debd 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 pr>db->aLimit[SQ
1debe 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d LITE_LIMIT_COLUM
1debf 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 N] ){. sqlite
1dec0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1dec1 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d , "too many term
1dec2 73 20 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c s in ORDER BY cl
1dec3 61 75 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 ause");. retu
1dec4 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 rn 1;. }.#endif
1dec5 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f . for(i=0; i<pO
1dec6 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 rderBy->nExpr; i
1dec7 2b 2b 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 ++){. pOrderB
1dec8 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 y->a[i].done = 0
1dec9 3b 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d ;. }. pSelect-
1deca 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 >pNext = 0;. wh
1decb 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 ile( pSelect->pP
1decc 72 69 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c rior ){. pSel
1decd 65 63 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 ect->pPrior->pNe
1dece 78 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 xt = pSelect;.
1decf 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c pSelect = pSel
1ded0 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d ect->pPrior;. }
1ded1 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 . while( pSelec
1ded2 74 20 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b t && moreToDo ){
1ded3 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 . struct Expr
1ded4 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1ded5 3b 0a 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d ;. moreToDo =
1ded6 20 30 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 0;. pEList =
1ded7 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 pSelect->pEList
1ded8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 ;. assert( pE
1ded9 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 List!=0 );. f
1deda 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f or(i=0, pItem=pO
1dedb 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 rderBy->a; i<pOr
1dedc 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b derBy->nExpr; i+
1dedd 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 +, pItem++){.
1dede 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 int iCol = -1
1dedf 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 ;. Expr *pE
1dee0 2c 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 , *pDup;. i
1dee1 66 28 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 f( pItem->done )
1dee2 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 continue;.
1dee3 20 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 pE = pItem->pEx
1dee4 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 pr;. if( sq
1dee5 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 lite3ExprIsInteg
1dee6 65 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b er(pE, &iCol) ){
1dee7 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f . if( iCo
1dee8 6c 3c 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 l<0 || iCol>pELi
1dee9 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 st->nExpr ){.
1deea 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 resolveOu
1deeb 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 tOfRangeError(pP
1deec 61 72 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 arse, "ORDER", i
1deed 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 +1, pEList->nExp
1deee 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 r);. re
1deef 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 turn 1;.
1def0 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1def1 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 iCol = re
1def2 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 solveAsName(pPar
1def3 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b se, pEList, pE);
1def4 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f . if( iCo
1def5 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 l==0 ){.
1def6 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 pDup = sqlite3
1def7 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20 ExprDup(db, pE,
1def8 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 0);. if
1def9 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 ( !db->mallocFai
1defa 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 led ){.
1defb 20 20 20 61 73 73 65 72 74 28 70 44 75 70 29 3b assert(pDup);
1defc 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f . iCo
1defd 6c 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 l = resolveOrder
1defe 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 ByTermToExprList
1deff 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 (pParse, pSelect
1df00 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 , pDup);.
1df01 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 }. s
1df02 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1df03 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 (db, pDup);.
1df04 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
1df05 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 ( iCol<0 ){.
1df06 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
1df07 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1df08 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c }. if( iCol
1df09 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 43 6f >0 ){. Co
1df0a 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 llSeq *pColl = p
1df0b 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 E->pColl;.
1df0c 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 45 int flags = pE
1df0d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 ->flags & EP_Exp
1df0e 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 Collate;.
1df0f 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
1df10 74 65 28 64 62 2c 20 70 45 29 3b 0a 20 20 20 20 te(db, pE);.
1df11 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 pItem->pExpr
1df12 20 3d 20 70 45 20 3d 20 73 71 6c 69 74 65 33 45 = pE = sqlite3E
1df13 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45 47 xpr(db, TK_INTEG
1df14 45 52 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 ER, 0, 0, 0);.
1df15 20 20 20 20 20 20 69 66 28 20 70 45 3d 3d 30 20 if( pE==0
1df16 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
1df17 20 20 20 20 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20 pE->pColl =
1df18 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 pColl;. p
1df19 45 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 E->flags |= EP_I
1df1a 6e 74 56 61 6c 75 65 20 7c 20 66 6c 61 67 73 3b ntValue | flags;
1df1b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 69 54 61 . pE->iTa
1df1c 62 6c 65 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 ble = iCol;.
1df1d 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 pItem->iCol
1df1e 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 = (u16)iCol;.
1df1f 20 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 pItem->done
1df20 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 = 1;. }els
1df21 65 7b 0a 20 20 20 20 20 20 20 20 6d 6f 72 65 54 e{. moreT
1df22 6f 44 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d oDo = 1;. }
1df23 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 . }. pSele
1df24 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e ct = pSelect->pN
1df25 65 78 74 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 ext;. }. for(i
1df26 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e =0; i<pOrderBy->
1df27 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
1df28 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 if( pOrderBy->a
1df29 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a 20 [i].done==0 ){.
1df2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1df2b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 72 rMsg(pParse, "%r
1df2c 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 64 ORDER BY term d
1df2d 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e oes not match an
1df2e 79 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 y ".
1df2f 22 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 "column in the r
1df30 65 73 75 6c 74 20 73 65 74 22 2c 20 69 2b 31 29 esult set", i+1)
1df31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 ;. return 1
1df32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1df33 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
1df34 20 43 68 65 63 6b 20 65 76 65 72 79 20 74 65 72 Check every ter
1df35 6d 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 m in the ORDER B
1df36 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c Y or GROUP BY cl
1df37 61 75 73 65 20 70 4f 72 64 65 72 42 79 20 6f 66 ause pOrderBy of
1df38 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73 .** the SELECT s
1df39 74 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 tatement pSelect
1df3a 2e 20 20 49 66 20 61 6e 79 20 74 65 72 6d 20 69 . If any term i
1df3b 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 s reference to a
1df3c 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 65 .** result set e
1df3d 78 70 72 65 73 73 69 6f 6e 20 28 61 73 20 64 65 xpression (as de
1df3e 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 termined by the
1df3f 45 78 70 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20 ExprList.a.iCol
1df40 66 69 65 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 63 field).** then c
1df41 6f 6e 76 65 72 74 20 74 68 61 74 20 74 65 72 6d onvert that term
1df42 20 69 6e 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 into a copy of
1df43 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1df44 67 20 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 g result set.**
1df45 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 column..**.** If
1df46 20 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 20 any errors are
1df47 64 65 74 65 63 74 65 64 2c 20 61 64 64 20 61 6e detected, add an
1df48 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
1df49 6f 20 70 50 61 72 73 65 20 61 6e 64 0a 2a 2a 20 o pParse and.**
1df4a 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e return non-zero.
1df4b 20 20 52 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 Return zero if
1df4c 20 6e 6f 20 65 72 72 6f 72 73 20 61 72 65 20 73 no errors are s
1df4d 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 een..*/.SQLITE_P
1df4e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1df4f 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 e3ResolveOrderGr
1df50 6f 75 70 42 79 28 0a 20 20 50 61 72 73 65 20 2a oupBy(. Parse *
1df51 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f pParse, /
1df52 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
1df53 74 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 t. Leave error
1df54 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f messages here */
1df55 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
1df56 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ct, /* The
1df57 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1df58 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1df59 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
1df5a 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 List *pOrderBy,
1df5b 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 /* The ORDER B
1df5c 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c Y or GROUP BY cl
1df5d 61 75 73 65 20 74 6f 20 62 65 20 70 72 6f 63 65 ause to be proce
1df5e 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ssed */. const
1df5f 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 20 char *zType
1df60 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 /* "ORDER" or "G
1df61 52 4f 55 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e ROUP" */.){. in
1df62 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a t i;. sqlite3 *
1df63 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1df64 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c . ExprList *pEL
1df65 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 45 78 ist;. struct Ex
1df66 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
1df67 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 em;.. if( pOrde
1df68 72 42 79 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 rBy==0 || pParse
1df69 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1df6a 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 ed ) return 0;.#
1df6b 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f if SQLITE_MAX_CO
1df6c 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 LUMN. if( pOrde
1df6d 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 rBy->nExpr>db->a
1df6e 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1df6f 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 IT_COLUMN] ){.
1df70 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1df71 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d g(pParse, "too m
1df72 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 25 73 20 any terms in %s
1df73 42 59 20 63 6c 61 75 73 65 22 2c 20 7a 54 79 70 BY clause", zTyp
1df74 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 e);. return 1
1df75 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 ;. }.#endif. p
1df76 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d EList = pSelect-
1df77 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 >pEList;. asser
1df78 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 20 t( pEList!=0 );
1df79 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 /* sqlite3Selec
1df7a 74 4e 65 77 28 29 20 67 75 61 72 61 6e 74 65 65 tNew() guarantee
1df7b 73 20 74 68 69 73 20 2a 2f 0a 20 20 66 6f 72 28 s this */. for(
1df7c 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 i=0, pItem=pOrde
1df7d 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 rBy->a; i<pOrder
1df7e 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 By->nExpr; i++,
1df7f 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 pItem++){. if
1df80 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 7b ( pItem->iCol ){
1df81 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d . if( pItem
1df82 2d 3e 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e ->iCol>pEList->n
1df83 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 Expr ){.
1df84 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 resolveOutOfRang
1df85 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 7a eError(pParse, z
1df86 54 79 70 65 2c 20 69 2b 31 2c 20 70 45 4c 69 73 Type, i+1, pELis
1df87 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 t->nExpr);.
1df88 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1df89 20 20 20 7d 0a 20 20 20 20 20 20 72 65 73 6f 6c }. resol
1df8a 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 veAlias(pParse,
1df8b 70 45 4c 69 73 74 2c 20 70 49 74 65 6d 2d 3e 69 pEList, pItem->i
1df8c 43 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 45 Col-1, pItem->pE
1df8d 78 70 72 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 xpr, zType);.
1df8e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1df8f 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 0;.}../*.** pOrd
1df90 65 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45 52 erBy is an ORDER
1df91 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 BY or GROUP BY
1df92 63 6c 61 75 73 65 20 69 6e 20 53 45 4c 45 43 54 clause in SELECT
1df93 20 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 statement pSele
1df94 63 74 2e 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20 ct..** The Name
1df95 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 context of the S
1df96 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
1df97 69 73 20 70 4e 43 2e 20 20 7a 54 79 70 65 20 69 is pNC. zType i
1df98 73 20 65 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44 s either.** "ORD
1df99 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 64 ER" or "GROUP" d
1df9a 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 epending on whic
1df9b 68 20 74 79 70 65 20 6f 66 20 63 6c 61 75 73 65 h type of clause
1df9c 20 70 4f 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a pOrderBy is..**
1df9d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1df9e 20 72 65 73 6f 6c 76 65 73 20 65 61 63 68 20 74 resolves each t
1df9f 65 72 6d 20 6f 66 20 74 68 65 20 63 6c 61 75 73 erm of the claus
1dfa0 65 20 69 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 e into an expres
1dfa1 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 sion..** If the
1dfa2 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73 order-by term is
1dfa3 20 61 6e 20 69 6e 74 65 67 65 72 20 49 20 62 65 an integer I be
1dfa4 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 tween 1 and N (w
1dfa5 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a here N is the.**
1dfa6 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1dfa7 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ns in the result
1dfa8 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 set of the SELE
1dfa9 43 54 29 20 74 68 65 6e 20 74 68 65 20 65 78 70 CT) then the exp
1dfaa 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 ression.** in th
1dfab 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 e resolution is
1dfac 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 49 2d a copy of the I-
1dfad 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 th result-set ex
1dfae 70 72 65 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a pression. If.**
1dfaf 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74 65 the order-by te
1dfb0 72 6d 20 69 73 20 61 6e 20 69 64 65 6e 74 69 66 rm is an identif
1dfb1 79 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e y that correspon
1dfb2 64 73 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d ds to the AS-nam
1dfb3 65 20 6f 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74 e of.** a result
1dfb4 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2c -set expression,
1dfb5 20 74 68 65 6e 20 74 68 65 20 74 65 72 6d 20 72 then the term r
1dfb6 65 73 6f 6c 76 65 73 20 74 6f 20 61 20 63 6f 70 esolves to a cop
1dfb7 79 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 y of the.** resu
1dfb8 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f lt-set expressio
1dfb9 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 n. Otherwise, t
1dfba 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
1dfbb 20 72 65 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20 resolved in.**
1dfbc 74 68 65 20 75 73 75 61 6c 20 77 61 79 20 2d 20 the usual way -
1dfbd 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52 65 73 using sqlite3Res
1dfbe 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 2e olveExprNames().
1dfbf 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
1dfc0 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
1dfc1 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 number of errors
1dfc2 2e 20 20 49 66 20 65 72 72 6f 72 73 20 6f 63 63 . If errors occ
1dfc3 75 72 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61 ur, then.** an a
1dfc4 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 ppropriate error
1dfc5 20 6d 65 73 73 61 67 65 20 6d 69 67 68 74 20 62 message might b
1dfc6 65 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 e left in pParse
1dfc7 2e 20 20 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a . (OOM errors.*
1dfc8 2a 20 65 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a * excepted.).*/.
1dfc9 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c static int resol
1dfca 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a veOrderGroupBy(.
1dfcb 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
1dfcc 4e 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e NC, /* The n
1dfcd 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 ame context of t
1dfce 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
1dfcf 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 ent */. Select
1dfd0 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f *pSelect, /
1dfd1 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 * The SELECT sta
1dfd2 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 tement holding p
1dfd3 4f 72 64 65 72 42 79 20 2a 2f 0a 20 20 45 78 70 OrderBy */. Exp
1dfd4 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c rList *pOrderBy,
1dfd5 20 20 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 /* An ORDER B
1dfd6 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c Y or GROUP BY cl
1dfd7 61 75 73 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 ause to resolve
1dfd8 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1dfd9 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 45 69 *zType /* Ei
1dfda 74 68 65 72 20 22 4f 52 44 45 52 22 20 6f 72 20 ther "ORDER" or
1dfdb 22 47 52 4f 55 50 22 2c 20 61 73 20 61 70 70 72 "GROUP", as appr
1dfdc 6f 70 72 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 opriate */.){.
1dfdd 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
1dfde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1dfdf 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
1dfe0 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 /. int iCol;
1dfe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dfe2 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d /* Column num
1dfe3 62 65 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ber */. struct
1dfe4 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
1dfe5 49 74 65 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72 Item; /* A ter
1dfe6 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 m of the ORDER B
1dfe7 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61 Y clause */. Pa
1dfe8 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 rse *pParse;
1dfe9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1dfea 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
1dfeb 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 */. int nResult
1dfec 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1dfed 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1dfee 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65 terms in the re
1dfef 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69 sult set */.. i
1dff0 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 f( pOrderBy==0 )
1dff1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65 return 0;. nRe
1dff2 73 75 6c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e sult = pSelect->
1dff3 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 pEList->nExpr;.
1dff4 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 pParse = pNC->p
1dff5 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30 Parse;. for(i=0
1dff6 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 , pItem=pOrderBy
1dff7 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d ->a; i<pOrderBy-
1dff8 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 >nExpr; i++, pIt
1dff9 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 em++){. Expr
1dffa 2a 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 *pE = pItem->pEx
1dffb 70 72 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72 pr;. iCol = r
1dffc 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 esolveAsName(pPa
1dffd 72 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 rse, pSelect->pE
1dffe 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 69 List, pE);. i
1dfff 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 f( iCol<0 ){.
1e000 20 20 20 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a return 1; /*
1e001 20 4f 4f 4d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 OOM error */.
1e002 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c }. if( iCol
1e003 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 >0 ){. /* I
1e004 66 20 61 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 f an AS-name mat
1e005 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 ch is found, mar
1e006 6b 20 74 68 69 73 20 4f 52 44 45 52 20 42 59 20 k this ORDER BY
1e007 63 6f 6c 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a column as being.
1e008 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 ** a copy
1e009 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 of the iCol-th r
1e00a 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e esult-set column
1e00b 2e 20 20 54 68 65 20 73 75 62 73 65 71 75 65 6e . The subsequen
1e00c 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 t call to.
1e00d 2a 2a 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 ** sqlite3Resolv
1e00e 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 eOrderGroupBy()
1e00f 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 will convert the
1e010 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 expression to a
1e011 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f . ** copy o
1e012 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 f the iCol-th re
1e013 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
1e014 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 ion. */. pI
1e015 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 tem->iCol = (u16
1e016 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e )iCol;. con
1e017 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 tinue;. }.
1e018 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 if( sqlite3Expr
1e019 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 IsInteger(pE, &i
1e01a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a Col) ){. /*
1e01b 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 The ORDER BY te
1e01c 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 rm is an integer
1e01d 20 63 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 constant. Agai
1e01e 6e 2c 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d n, set the colum
1e01f 6e 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 n. ** numbe
1e020 72 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 r so that sqlite
1e021 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 3ResolveOrderGro
1e022 75 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 upBy() will conv
1e023 65 72 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a ert the. **
1e024 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 order-by term t
1e025 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 o a copy of the
1e026 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 result-set expre
1e027 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 ssion */. i
1e028 66 28 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 f( iCol<1 ){.
1e029 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f resolveOutO
1e02a 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 fRangeError(pPar
1e02b 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 se, zType, i+1,
1e02c 6e 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 nResult);.
1e02d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
1e02e 20 20 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d }. pItem-
1e02f 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f >iCol = (u16)iCo
1e030 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 l;. continu
1e031 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a e;. }.. /*
1e032 20 4f 74 68 65 72 77 69 73 65 2c 20 74 72 65 61 Otherwise, trea
1e033 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 t the ORDER BY t
1e034 65 72 6d 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 erm as an ordina
1e035 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f ry expression */
1e036 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c . pItem->iCol
1e037 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 = 0;. if( sq
1e038 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
1e039 4e 61 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 Names(pNC, pE) )
1e03a 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
1e03b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1e03c 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 73 6f turn sqlite3Reso
1e03d 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 lveOrderGroupBy(
1e03e 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c pParse, pSelect,
1e03f 20 70 4f 72 64 65 72 42 79 2c 20 7a 54 79 70 65 pOrderBy, zType
1e040 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f );.}../*.** Reso
1e041 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 lve names in the
1e042 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1e043 74 20 70 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 t p and all of i
1e044 74 73 20 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a ts descendents..
1e045 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
1e046 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 28 solveSelectStep(
1e047 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
1e048 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e Select *p){. N
1e049 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 ameContext *pOut
1e04a 65 72 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 erNC; /* Contex
1e04b 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 t that contains
1e04c 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 this SELECT */.
1e04d 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 NameContext sNC
1e04e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ; /* Name
1e04f 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 context of this
1e050 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 SELECT */. int
1e051 20 69 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 isCompound;
1e052 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1e053 70 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 p is a compound
1e054 73 65 6c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 select */. int
1e055 6e 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 nCompound;
1e056 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1e057 20 63 6f 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 compound terms
1e058 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 processed so far
1e059 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 */. Parse *pPa
1e05a 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a rse; /*
1e05b 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
1e05c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
1e05d 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a pEList; /*
1e05e 20 52 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 Result set expr
1e05f 65 73 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 ession list */.
1e060 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1e061 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1e062 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 counter */. Ex
1e063 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 prList *pGroupBy
1e064 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f ; /* The GRO
1e065 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a UP BY clause */.
1e066 20 20 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d Select *pLeftm
1e067 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 ost; /* Lef
1e068 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 t-most of SELECT
1e069 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a of a compound *
1e06a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b /. sqlite3 *db;
1e06b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
1e06c 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1e06d 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 on */. .. asse
1e06e 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 rt( p!=0 );. if
1e06f 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 ( p->selFlags &
1e070 53 46 5f 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 SF_Resolved ){.
1e071 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
1e072 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 une;. }. pOute
1e073 72 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 rNC = pWalker->u
1e074 2e 70 4e 43 3b 0a 20 20 70 50 61 72 73 65 20 3d .pNC;. pParse =
1e075 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 pWalker->pParse
1e076 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
1e077 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 >db;.. /* Norma
1e078 6c 6c 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 lly sqlite3Selec
1e079 74 45 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 tExpand() will b
1e07a 65 20 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61 e called first a
1e07b 6e 64 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a nd will have. *
1e07c 2a 20 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64 * already expand
1e07d 65 64 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20 ed this SELECT.
1e07e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69 However, if thi
1e07f 73 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 s is a subquery
1e080 77 69 74 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 within. ** an e
1e081 78 70 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 xpression, sqlit
1e082 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
1e083 65 73 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c es() will be cal
1e084 6c 65 64 20 77 69 74 68 6f 75 74 20 61 0a 20 20 led without a.
1e085 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f ** prior call to
1e086 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 sqlite3SelectEx
1e087 70 61 6e 64 28 29 2e 20 20 57 68 65 6e 20 74 68 pand(). When th
1e088 61 74 20 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a at happens, let.
1e089 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 ** sqlite3Sele
1e08a 63 74 50 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 ctPrep() do all
1e08b 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e of the processin
1e08c 67 20 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43 g for this SELEC
1e08d 54 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 T.. ** sqlite3S
1e08e 65 6c 65 63 74 50 72 65 70 28 29 20 77 69 6c 6c electPrep() will
1e08f 20 69 6e 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c invoke both sql
1e090 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 ite3SelectExpand
1e091 28 29 20 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 () and. ** this
1e092 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 65 20 routine in the
1e093 63 6f 72 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 correct order..
1e094 20 2a 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 */. if( (p->se
1e095 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 lFlags & SF_Expa
1e096 6e 64 65 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 nded)==0 ){.
1e097 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 sqlite3SelectPre
1e098 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 p(pParse, p, pOu
1e099 74 65 72 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 terNC);. retu
1e09a 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 rn (pParse->nErr
1e09b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
1e09c 69 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 iled) ? WRC_Abor
1e09d 74 20 3a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 t : WRC_Prune;.
1e09e 20 7d 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 }.. isCompound
1e09f 20 3d 20 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b = p->pPrior!=0;
1e0a0 0a 20 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 . nCompound = 0
1e0a1 3b 0a 20 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 ;. pLeftmost =
1e0a2 70 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b p;. while( p ){
1e0a3 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d . assert( (p-
1e0a4 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 >selFlags & SF_E
1e0a5 78 70 61 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 xpanded)!=0 );.
1e0a6 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 assert( (p->s
1e0a7 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 elFlags & SF_Res
1e0a8 6f 6c 76 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 olved)==0 );.
1e0a9 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 p->selFlags |=
1e0aa 53 46 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 SF_Resolved;..
1e0ab 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 /* Resolve the
1e0ac 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 expressions in
1e0ad 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 the LIMIT and OF
1e0ae 46 53 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 FSET clauses. Th
1e0af 65 73 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e ese. ** are n
1e0b0 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 ot allowed to re
1e0b1 66 65 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 fer to any names
1e0b2 2c 20 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 , so pass an emp
1e0b3 74 79 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a ty NameContext..
1e0b4 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 */. memse
1e0b5 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f t(&sNC, 0, sizeo
1e0b6 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 f(sNC));. sNC
1e0b7 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
1e0b8 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1e0b9 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
1e0ba 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 s(&sNC, p->pLimi
1e0bb 74 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 t) ||. sq
1e0bc 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
1e0bd 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 Names(&sNC, p->p
1e0be 4f 66 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 Offset) ){.
1e0bf 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1e0c0 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 t;. }. .
1e0c1 2f 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f /* Set up the lo
1e0c2 63 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 cal name-context
1e0c3 20 74 6f 20 70 61 73 73 20 74 6f 20 73 71 6c 69 to pass to sqli
1e0c4 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
1e0c5 6d 65 73 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 mes() to. **
1e0c6 72 65 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75 resolve the resu
1e0c7 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f lt-set expressio
1e0c8 6e 20 6c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 n list.. */.
1e0c9 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 sNC.allowAgg
1e0ca 3d 20 31 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 = 1;. sNC.pSr
1e0cb 63 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b cList = p->pSrc;
1e0cc 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d . sNC.pNext =
1e0cd 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 pOuterNC;. .
1e0ce 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d /* Resolve nam
1e0cf 65 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 es in the result
1e0d0 20 73 65 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c set. */. pEL
1e0d1 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b ist = p->pEList;
1e0d2 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c . assert( pEL
1e0d3 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f ist!=0 );. fo
1e0d4 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d r(i=0; i<pEList-
1e0d5 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 >nExpr; i++){.
1e0d6 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 Expr *pX = p
1e0d7 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 EList->a[i].pExp
1e0d8 72 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c r;. if( sql
1e0d9 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
1e0da 61 6d 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29 ames(&sNC, pX) )
1e0db 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
1e0dc 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
1e0dd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
1e0de 20 2f 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 /* Recursively
1e0df 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e resolve names in
1e0e0 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0a all subqueries.
1e0e1 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 */. for(i
1e0e2 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e =0; i<p->pSrc->n
1e0e3 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Src; i++){.
1e0e4 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
1e0e5 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 item *pItem = &p
1e0e6 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 ->pSrc->a[i];.
1e0e7 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 if( pItem->p
1e0e8 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 Select ){.
1e0e9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
1e0ea 61 76 65 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 avedContext = pP
1e0eb 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 arse->zAuthConte
1e0ec 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 xt;. if(
1e0ed 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 pItem->zName ) p
1e0ee 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 Parse->zAuthCont
1e0ef 65 78 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 ext = pItem->zNa
1e0f0 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 me;. sqli
1e0f1 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 te3ResolveSelect
1e0f2 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49 Names(pParse, pI
1e0f3 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f tem->pSelect, pO
1e0f4 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 uterNC);.
1e0f5 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f pParse->zAuthCo
1e0f6 6e 74 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f ntext = zSavedCo
1e0f7 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 ntext;. i
1e0f8 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 f( pParse->nErr
1e0f9 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
1e0fa 6c 65 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 led ) return WRC
1e0fb 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a _Abort;. }.
1e0fc 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
1e0fd 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 If there are no
1e0fe 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
1e0ff 6f 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c ons in the resul
1e100 74 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 t-set, and no GR
1e101 4f 55 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 OUP BY . ** e
1e102 78 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f xpression, do no
1e103 74 20 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 t allow aggregat
1e104 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 es in any of the
1e105 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f other expressio
1e106 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 ns.. */. a
1e107 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c ssert( (p->selFl
1e108 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 ags & SF_Aggrega
1e109 74 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 te)==0 );. pG
1e10a 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f roupBy = p->pGro
1e10b 75 70 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 upBy;. if( pG
1e10c 72 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 roupBy || sNC.ha
1e10d 73 41 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d sAgg ){. p-
1e10e 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f >selFlags |= SF_
1e10f 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d Aggregate;. }
1e110 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e else{. sNC.
1e111 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 allowAgg = 0;.
1e112 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
1e113 20 61 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 a HAVING clause
1e114 20 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 is present, the
1e115 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 n there must be
1e116 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 a GROUP BY claus
1e117 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 e.. */. if
1e118 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 ( p->pHaving &&
1e119 21 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 !pGroupBy ){.
1e11a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1e11b 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 sg(pParse, "a GR
1e11c 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 OUP BY clause is
1e11d 20 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 required before
1e11e 20 48 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 HAVING");.
1e11f 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1e120 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 t;. }. .
1e121 2f 2a 20 41 64 64 20 74 68 65 20 65 78 70 72 65 /* Add the expre
1e122 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 ssion list to th
1e123 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 e name-context b
1e124 65 66 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 efore parsing th
1e125 65 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 e. ** other e
1e126 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 xpressions in th
1e127 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
1e128 6e 74 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 nt. This is so t
1e129 68 61 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 hat. ** expre
1e12a 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 ssions in the WH
1e12b 45 52 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e ERE clause (etc.
1e12c 29 20 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 ) can refer to e
1e12d 78 70 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 xpressions by.
1e12e 20 20 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 ** aliases in
1e12f 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a the result set..
1e130 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 **. ** Mi
1e131 6e 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 nor point: If th
1e132 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 is is the case,
1e133 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 then the express
1e134 69 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 ion will be.
1e135 2a 2a 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 ** re-evaluated
1e136 66 6f 72 20 65 61 63 68 20 72 65 66 65 72 65 6e for each referen
1e137 63 65 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f ce to it.. */
1e138 0a 20 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 . sNC.pEList
1e139 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 = p->pEList;.
1e13a 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f if( sqlite3Reso
1e13b 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e lveExprNames(&sN
1e13c 43 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c C, p->pWhere) ||
1e13d 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 . sqlite3R
1e13e 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 esolveExprNames(
1e13f 26 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 &sNC, p->pHaving
1e140 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 ). ){. r
1e141 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1e142 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 . }.. /* T
1e143 68 65 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 he ORDER BY and
1e144 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 GROUP BY clauses
1e145 20 6d 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 may not refer t
1e146 6f 20 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a o terms in. *
1e147 2a 20 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 * outer queries
1e148 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e . */. sNC.
1e149 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 pNext = 0;. s
1e14a 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b NC.allowAgg = 1;
1e14b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 .. /* Process
1e14c 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
1e14d 61 75 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 ause for singlet
1e14e 6f 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d on SELECT statem
1e14f 65 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 ents.. ** The
1e150 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
1e151 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 for compounds S
1e152 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 ELECT statements
1e153 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 is handled.
1e154 2a 2a 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 ** below, after
1e155 61 6c 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c all of the resul
1e156 74 2d 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f t-sets for all o
1e157 66 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f f the elements o
1e158 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d f. ** the com
1e159 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 pound have been
1e15a 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f resolved.. */
1e15b 0a 20 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 . if( !isComp
1e15c 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f ound && resolveO
1e15d 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 rderGroupBy(&sNC
1e15e 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 , p, p->pOrderBy
1e15f 2c 20 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 , "ORDER") ){.
1e160 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
1e161 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 bort;. }.
1e162 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1e163 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 iled ){. re
1e164 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1e165 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
1e166 52 65 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 Resolve the GROU
1e167 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 P BY clause. At
1e168 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 the same time,
1e169 6d 61 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a make sure . *
1e16a 2a 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 * the GROUP BY c
1e16b 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 lause does not c
1e16c 6f 6e 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 ontain aggregate
1e16d 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 functions..
1e16e 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 */. if( pGrou
1e16f 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 pBy ){. str
1e170 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
1e171 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 m *pItem;. .
1e172 20 20 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 if( resolve
1e173 4f 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e OrderGroupBy(&sN
1e174 43 2c 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 C, p, pGroupBy,
1e175 22 47 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e "GROUP") || db->
1e176 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1e177 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 return W
1e178 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 RC_Abort;.
1e179 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c }. for(i=0,
1e17a 20 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d pItem=pGroupBy-
1e17b 3e 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e >a; i<pGroupBy->
1e17c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 nExpr; i++, pIte
1e17d 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 m++){. if
1e17e 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
1e17f 79 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 y(pItem->pExpr,
1e180 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 EP_Agg) ){.
1e181 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1e182 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 rMsg(pParse, "ag
1e183 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
1e184 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 s are not allowe
1e185 64 20 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 d in ".
1e186 20 20 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 "the GROUP
1e187 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 BY clause");.
1e188 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 return WR
1e189 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 C_Abort;.
1e18a 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1e18b 0a 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 .. /* Advance
1e18c 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 to the next ter
1e18d 6d 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e m of the compoun
1e18e 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d d. */. p =
1e18f 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 p->pPrior;.
1e190 6e 43 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d nCompound++;. }
1e191 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 .. /* Resolve t
1e192 68 65 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 he ORDER BY on a
1e193 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
1e194 20 61 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 after all terms
1e195 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d of. ** the com
1e196 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 pound have been
1e197 72 65 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 resolved.. */.
1e198 20 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 if( isCompound
1e199 26 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 && resolveCompou
1e19a 6e 64 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 ndOrderBy(pParse
1e19b 2c 20 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a , pLeftmost) ){.
1e19c 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
1e19d 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 bort;. }.. ret
1e19e 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d urn WRC_Prune;.}
1e19f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
1e1a0 74 69 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 tine walks an ex
1e1a1 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e pression tree an
1e1a2 64 20 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 d resolves refer
1e1a3 65 6e 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c ences to.** tabl
1e1a4 65 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 e columns and re
1e1a5 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 sult-set columns
1e1a6 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 . At the same t
1e1a7 69 6d 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a ime, do error.**
1e1a8 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e checking on fun
1e1a9 63 74 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 ction usage and
1e1aa 73 65 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e set a flag if an
1e1ab 79 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 y aggregate func
1e1ac 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 tions.** are see
1e1ad 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f n..**.** To reso
1e1ae 6c 76 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e lve table column
1e1af 73 20 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 s references we
1e1b0 6c 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 look for nodes (
1e1b1 6f 72 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 or subtrees) of
1e1b2 74 68 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 the .** form X.Y
1e1b3 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 .Z or Y.Z or jus
1e1b4 74 20 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 t Z where.**.**
1e1b5 20 20 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 X: The na
1e1b6 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 me of a database
1e1b7 2e 20 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f . Ex: "main" o
1e1b8 72 20 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 r "temp" or.**
1e1b9 20 20 20 20 20 20 20 20 20 74 68 65 20 73 79 6d the sym
1e1ba 62 6f 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 bolic name assig
1e1bb 6e 65 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 ned to an ATTACH
1e1bc 2d 65 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a -ed database..**
1e1bd 0a 2a 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 .** Y: Th
1e1be 65 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c e name of a tabl
1e1bf 65 20 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 e in a FROM clau
1e1c0 73 65 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 se. Or in a tri
1e1c1 67 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 gger.**
1e1c2 20 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 one of the spe
1e1c3 63 69 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 cial names "old"
1e1c4 20 6f 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a or "new"..**.**
1e1c5 20 20 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e Z: The n
1e1c6 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 ame of a column
1e1c7 69 6e 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a in table Y..**.*
1e1c8 2a 20 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 * The node at th
1e1c9 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 e root of the su
1e1ca 62 74 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 btree is modifie
1e1cb 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a d as follows:.**
1e1cc 0a 2a 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 .** Expr.op
1e1cd 20 20 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f Changed to
1e1ce 20 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 TK_COLUMN.**
1e1cf 20 45 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 Expr.pTab
1e1d0 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 Points to the Ta
1e1d1 62 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 ble object for X
1e1d2 2e 59 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 .Y.** Expr.iC
1e1d3 6f 6c 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 olumn The colu
1e1d4 6d 6e 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e mn index in X.Y.
1e1d5 20 20 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 -1 for the row
1e1d6 69 64 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 id..** Expr.i
1e1d7 54 61 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 Table The VDB
1e1d8 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 E cursor number
1e1d9 66 6f 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a for X.Y.**.**.**
1e1da 20 54 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 To resolve resu
1e1db 6c 74 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 lt-set reference
1e1dc 73 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 s, look for expr
1e1dd 65 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 ession nodes of
1e1de 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 the.** form Z (w
1e1df 69 74 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 ith no X and Y p
1e1e0 72 65 66 69 78 29 20 77 68 65 72 65 20 74 68 65 refix) where the
1e1e1 20 5a 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 Z matches the r
1e1e2 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a ight-hand.** siz
1e1e3 65 20 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 e of an AS claus
1e1e4 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d e in the result-
1e1e5 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e set of a SELECT.
1e1e6 20 20 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 The Z expressi
1e1e7 6f 6e 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 on.** is replace
1e1e8 64 20 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 d by a copy of t
1e1e9 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 he left-hand sid
1e1ea 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d e of the result-
1e1eb 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a set expression..
1e1ec 2a 2a 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e ** Table-name an
1e1ed 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c d function resol
1e1ee 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 ution occurs on
1e1ef 74 68 65 20 73 75 62 73 74 69 74 75 74 65 64 20 the substituted
1e1f0 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 expression.** tr
1e1f1 65 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 ee. For example
1e1f2 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 , in:.**.**
1e1f3 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 SELECT a+b AS x
1e1f4 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 , c+d AS y FROM
1e1f5 74 31 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a t1 ORDER BY x;.*
1e1f6 2a 0a 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 *.** The "x" ter
1e1f7 6d 20 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 m of the order b
1e1f8 79 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 y is replaced by
1e1f9 20 22 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 "a+b" to render
1e1fa 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c :.**.** SEL
1e1fb 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b ECT a+b AS x, c+
1e1fc 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f d AS y FROM t1 O
1e1fd 52 44 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a RDER BY a+b;.**.
1e1fe 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c ** Function call
1e1ff 73 20 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f s are checked to
1e200 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
1e201 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 the function is
1e202 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 .** defined and
1e203 74 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 that the correct
1e204 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d number of argum
1e205 65 6e 74 73 20 61 72 65 20 73 70 65 63 69 66 69 ents are specifi
1e206 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 ed..** If the fu
1e207 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 nction is an agg
1e208 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c regate function,
1e209 20 74 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 then the pNC->h
1e20a 61 73 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 asAgg is.** set
1e20b 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 and the opcode i
1e20c 73 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 s changed from T
1e20d 4b 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b K_FUNCTION to TK
1e20e 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a _AGG_FUNCTION..*
1e20f 2a 20 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 * If an expressi
1e210 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 on contains aggr
1e211 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
1e212 74 68 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a then the EP_Agg.
1e213 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 ** property on t
1e214 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
1e215 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 set..**.** An e
1e216 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 rror message is
1e217 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 left in pParse i
1e218 66 20 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d f anything is am
1e219 69 73 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 iss. The number
1e21a 0a 2a 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 .** if errors is
1e21b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 returned..*/.SQ
1e21c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1e21d 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
1e21e 78 70 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d xprNames( . Nam
1e21f 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 eContext *pNC,
1e220 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 /* Namespac
1e221 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 e to resolve exp
1e222 72 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a ressions in. */.
1e223 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 Expr *pExpr
1e224 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1e225 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 expression to b
1e226 65 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 e analyzed. */.)
1e227 7b 0a 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 {. int savedHas
1e228 41 67 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b Agg;. Walker w;
1e229 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 .. if( pExpr==0
1e22a 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 ) return 0;.#if
1e22b 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 SQLITE_MAX_EXPR
1e22c 5f 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 _DEPTH>0. {.
1e22d 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d Parse *pParse =
1e22e 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 pNC->pParse;.
1e22f 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 if( sqlite3Exp
1e230 72 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 rCheckHeight(pPa
1e231 72 73 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 rse, pExpr->nHei
1e232 67 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d ght+pNC->pParse-
1e233 3e 6e 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 >nHeight) ){.
1e234 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1e235 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e }. pParse->n
1e236 48 65 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d Height += pExpr-
1e237 3e 6e 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 >nHeight;. }.#e
1e238 6e 64 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 ndif. savedHasA
1e239 67 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 gg = pNC->hasAgg
1e23a 3b 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 ;. pNC->hasAgg
1e23b 3d 20 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 = 0;. w.xExprCa
1e23c 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 llback = resolve
1e23d 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 ExprStep;. w.xS
1e23e 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 electCallback =
1e23f 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 resolveSelectSte
1e240 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 p;. w.pParse =
1e241 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 pNC->pParse;. w
1e242 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 .u.pNC = pNC;.
1e243 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1e244 26 77 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 &w, pExpr);.#if
1e245 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f SQLITE_MAX_EXPR_
1e246 44 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 DEPTH>0. pNC->p
1e247 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d Parse->nHeight -
1e248 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 = pExpr->nHeight
1e249 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 ;.#endif. if( p
1e24a 4e 43 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 20 NC->nErr>0 ){.
1e24b 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 ExprSetPropert
1e24c 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f y(pExpr, EP_Erro
1e24d 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e r);. }. if( pN
1e24e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20 20 20 C->hasAgg ){.
1e24f 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 ExprSetProperty
1e250 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29 3b (pExpr, EP_Agg);
1e251 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 61 76 . }else if( sav
1e252 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20 20 20 edHasAgg ){.
1e253 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b pNC->hasAgg = 1;
1e254 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 45 78 . }. return Ex
1e255 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
1e256 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a xpr, EP_Error);.
1e257 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 }.../*.** Resolv
1e258 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 61 e all names in a
1e259 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f ll expressions o
1e25a 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64 20 69 f a SELECT and i
1e25b 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e 64 65 n all.** decende
1e25c 6e 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 nts of the SELEC
1e25d 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 6f 6d T, including com
1e25e 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20 70 2d pounds off of p-
1e25f 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75 62 71 >pPrior,.** subq
1e260 75 65 72 69 65 73 20 69 6e 20 65 78 70 72 65 73 ueries in expres
1e261 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62 71 75 sions, and subqu
1e262 65 72 69 65 73 20 75 73 65 64 20 61 73 20 46 52 eries used as FR
1e263 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74 65 72 OM clause.** ter
1e264 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 73 71 ms..**.** See sq
1e265 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
1e266 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 20 64 65 Names() for a de
1e267 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 scription of the
1e268 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74 72 61 kinds of.** tra
1e269 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74 68 61 nsformations tha
1e26a 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 t occur..**.** A
1e26b 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d ll SELECT statem
1e26c 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 61 76 65 ents should have
1e26d 20 62 65 65 6e 20 65 78 70 61 6e 64 65 64 20 75 been expanded u
1e26e 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 53 sing.** sqlite3S
1e26f 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 70 72 electExpand() pr
1e270 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 ior to invoking
1e271 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f this routine..*/
1e272 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1e273 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f void sqlite3Reso
1e274 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 0a lveSelectNames(.
1e275 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
1e276 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1e277 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a parser context *
1e278 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 /. Select *p,
1e279 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1e27a 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
1e27b 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 nt being coded.
1e27c 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 */. NameContext
1e27d 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e *pOuterNC /* N
1e27e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 ame context for
1e27f 70 61 72 65 6e 74 20 53 45 4c 45 43 54 20 73 74 parent SELECT st
1e280 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 atement */.){.
1e281 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61 73 73 Walker w;.. ass
1e282 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 77 ert( p!=0 );. w
1e283 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d .xExprCallback =
1e284 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 resolveExprStep
1e285 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c ;. w.xSelectCal
1e286 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 lback = resolveS
1e287 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 electStep;. w.p
1e288 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
1e289 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f 75 74 w.u.pNC = pOut
1e28a 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 erNC;. sqlite3W
1e28b 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 29 alkSelect(&w, p)
1e28c 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
1e28d 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 73 6f **** End of reso
1e28e 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a lve.c **********
1e28f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e291 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1e292 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1e293 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a expr.c *********
1e294 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e295 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e296 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 ***/./*.** 2001
1e297 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a September 15.**.
1e298 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1e299 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1e29a 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1e29b 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1e29c 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1e29d 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1e29e 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1e29f 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1e2a0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1e2a1 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1e2a2 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1e2a3 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1e2a4 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1e2a5 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1e2a6 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1e2a7 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1e2a8 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1e2a9 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1e2aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e2ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e2ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e2ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
1e2ae 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
1e2af 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 s routines used
1e2b0 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 65 78 for analyzing ex
1e2b1 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a 2a 2a pressions and.**
1e2b2 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e 67 20 for generating
1e2b3 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 65 VDBE code that e
1e2b4 76 61 6c 75 61 74 65 73 20 65 78 70 72 65 73 73 valuates express
1e2b5 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a ions in SQLite..
1e2b6 2a 2a 0a 2a 2a 20 24 49 64 3a 20 65 78 70 72 2e **.** $Id: expr.
1e2b7 63 2c 76 20 31 2e 34 32 34 20 32 30 30 39 2f 30 c,v 1.424 2009/0
1e2b8 33 2f 32 35 20 31 36 3a 35 31 3a 34 33 20 64 72 3/25 16:51:43 dr
1e2b9 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a h Exp $.*/../*.*
1e2ba 2a 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66 * Return the 'af
1e2bb 66 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65 finity' of the e
1e2bc 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 xpression pExpr
1e2bd 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 if any..**.** If
1e2be 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 pExpr is a colu
1e2bf 6d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20 mn, a reference
1e2c0 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 to a column via
1e2c1 61 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a an 'AS' alias,.*
1e2c2 2a 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63 * or a sub-selec
1e2c3 74 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 t with a column
1e2c4 61 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 as the return va
1e2c5 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a lue, then the .*
1e2c6 2a 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 * affinity of th
1e2c7 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 at column is ret
1e2c8 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 urned. Otherwise
1e2c9 2c 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e , 0x00 is return
1e2ca 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e ed,.** indicatin
1e2cb 67 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f g no affinity fo
1e2cc 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e r the expression
1e2cd 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 ..**.** i.e. the
1e2ce 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 WHERE clause ex
1e2cf 70 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 presssions in th
1e2d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 e following stat
1e2d1 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 ements all.** ha
1e2d2 76 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a ve an affinity:.
1e2d3 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 **.** CREATE TAB
1e2d4 4c 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c LE t1(a);.** SEL
1e2d5 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 ECT * FROM t1 WH
1e2d6 45 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 ERE a;.** SELECT
1e2d7 20 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20 a AS b FROM t1
1e2d8 57 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 WHERE b;.** SELE
1e2d9 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 CT * FROM t1 WHE
1e2da 52 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f RE (select a fro
1e2db 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 m t1);.*/.SQLITE
1e2dc 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 _PRIVATE char sq
1e2dd 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 lite3ExprAffinit
1e2de 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a y(Expr *pExpr){.
1e2df 20 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 int op = pExpr
1e2e0 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d ->op;. if( op==
1e2e1 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 TK_SELECT ){.
1e2e2 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1e2e3 66 6c 61 67 73 26 45 50 5f 78 49 73 53 65 6c 65 flags&EP_xIsSele
1e2e4 63 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ct );. return
1e2e5 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 sqlite3ExprAffi
1e2e6 6e 69 74 79 28 70 45 78 70 72 2d 3e 78 2e 70 53 nity(pExpr->x.pS
1e2e7 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 elect->pEList->a
1e2e8 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a [0].pExpr);. }.
1e2e9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1e2ea 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f MIT_CAST. if( o
1e2eb 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20 p==TK_CAST ){.
1e2ec 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1e2ed 41 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 AffinityType(&pE
1e2ee 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d xpr->token);. }
1e2ef 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f .#endif. if( (o
1e2f0 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
1e2f1 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d || op==TK_COLUM
1e2f2 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 N || op==TK_REGI
1e2f3 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78 STER) . && pEx
1e2f4 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b pr->pTab!=0. ){
1e2f5 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 . /* op==TK_R
1e2f6 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 EGISTER && pExpr
1e2f7 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e ->pTab!=0 happen
1e2f8 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 s when pExpr was
1e2f9 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 originally.
1e2fa 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 ** a TK_COLUMN b
1e2fb 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c ut was previousl
1e2fc 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 y evaluated and
1e2fd 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 cached in a regi
1e2fe 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ster */. int
1e2ff 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 j = pExpr->iColu
1e300 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20 mn;. if( j<0
1e301 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1e302 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 AFF_INTEGER;.
1e303 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1e304 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d pTab && j<pExpr-
1e305 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 >pTab->nCol );.
1e306 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d return pExpr-
1e307 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61 >pTab->aCol[j].a
1e308 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72 ffinity;. }. r
1e309 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66 eturn pExpr->aff
1e30a 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 inity;.}../*.**
1e30b 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e Set the collatin
1e30c 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 g sequence for e
1e30d 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 xpression pExpr
1e30e 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74 to be the collat
1e30f 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ing.** sequence
1e310 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e named by pToken.
1e311 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e Return a poin
1e312 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73 ter to the revis
1e313 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a ed expression..*
1e314 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 * The collating
1e315 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b sequence is mark
1e316 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22 ed as "explicit"
1e317 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78 using the EP_Ex
1e318 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67 pCollate.** flag
1e319 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63 . An explicit c
1e31a 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1e31b 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20 e will override
1e31c 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c implicit.** coll
1e31d 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e ating sequences.
1e31e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1e31f 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
1e320 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 ExprSetColl(Pars
1e321 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
1e322 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 *pExpr, Token *p
1e323 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 CollName){. cha
1e324 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 r *zColl = 0;
1e325 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 /* Dequ
1e326 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c oted name of col
1e327 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
1e328 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 */. CollSeq *pC
1e329 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a oll;. sqlite3 *
1e32a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1e32b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 . zColl = sqlit
1e32c 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
1e32d 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a db, pCollName);.
1e32e 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a if( pExpr && z
1e32f 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c Coll ){. pCol
1e330 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 l = sqlite3Locat
1e331 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c eCollSeq(pParse,
1e332 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20 zColl, -1);.
1e333 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
1e334 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c pExpr->pColl
1e335 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 = pColl;.
1e336 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 pExpr->flags |=
1e337 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 EP_ExpCollate;.
1e338 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
1e339 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f e3DbFree(db, zCo
1e33a 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 45 ll);. return pE
1e33b 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 xpr;.}../*.** Re
1e33c 74 75 72 6e 20 74 68 65 20 64 65 66 61 75 6c 74 turn the default
1e33d 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
1e33e 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78 70 72 nce for the expr
1e33f 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 ession pExpr. If
1e340 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 .** there is no
1e341 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f default collatio
1e342 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e 20 30 n type, return 0
1e343 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1e344 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c ATE CollSeq *sql
1e345 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 ite3ExprCollSeq(
1e346 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
1e347 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 43 xpr *pExpr){. C
1e348 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
1e349 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 70 0;. Expr *p = p
1e34a 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 Expr;. while( p
1e34b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a ){. int op;.
1e34c 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 pColl = p->p
1e34d 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20 70 43 Coll;. if( pC
1e34e 6f 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 oll ) break;.
1e34f 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20 op = p->op;.
1e350 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 if( (op==TK_AGG
1e351 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 _COLUMN || op==T
1e352 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d K_COLUMN || op==
1e353 54 4b 5f 52 45 47 49 53 54 45 52 29 20 26 26 20 TK_REGISTER) &&
1e354 70 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20 p->pTab!=0 ){.
1e355 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 /* op==TK_RE
1e356 47 49 53 54 45 52 20 26 26 20 70 2d 3e 70 54 61 GISTER && p->pTa
1e357 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 b!=0 happens whe
1e358 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 n pExpr was orig
1e359 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 inally. **
1e35a 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 a TK_COLUMN but
1e35b 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65 was previously e
1e35c 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 61 63 valuated and cac
1e35d 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 74 65 hed in a registe
1e35e 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 r */. const
1e35f 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 char *zColl;.
1e360 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d 3e 69 int j = p->i
1e361 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66 Column;. if
1e362 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 ( j>=0 ){.
1e363 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1e364 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 pParse->db;.
1e365 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d 3e 70 zColl = p->p
1e366 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f Tab->aCol[j].zCo
1e367 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c ll;. pCol
1e368 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 l = sqlite3FindC
1e369 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 ollSeq(db, ENC(d
1e36a 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 b), zColl, -1, 0
1e36b 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 );. pExpr
1e36c 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b ->pColl = pColl;
1e36d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
1e36e 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1e36f 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 if( op!=TK_CAST
1e370 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 && op!=TK_UPLUS
1e371 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a ){. break;.
1e372 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d }. p = p-
1e373 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 >pLeft;. }. if
1e374 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f ( sqlite3CheckCo
1e375 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 llSeq(pParse, pC
1e376 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f oll) ){ . pCo
1e377 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ll = 0;. }. re
1e378 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f turn pColl;.}../
1e379 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e *.** pExpr is an
1e37a 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f operand of a co
1e37b 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f mparison operato
1e37c 72 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a r. aff2 is the.
1e37d 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 ** type affinity
1e37e 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 of the other op
1e37f 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 erand. This rou
1e380 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
1e381 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 .** type affinit
1e382 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 y that should be
1e383 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f used for the co
1e384 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f mparison operato
1e385 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
1e386 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 VATE char sqlite
1e387 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 3CompareAffinity
1e388 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 (Expr *pExpr, ch
1e389 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 ar aff2){. char
1e38a 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 aff1 = sqlite3E
1e38b 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 xprAffinity(pExp
1e38c 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 r);. if( aff1 &
1e38d 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a & aff2 ){. /*
1e38e 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 Both sides of t
1e38f 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 he comparison ar
1e390 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e e columns. If on
1e391 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 e has numeric.
1e392 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 ** affinity, u
1e393 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 se that. Otherwi
1e394 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 se use no affini
1e395 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ty.. */. i
1e396 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 f( sqlite3IsNume
1e397 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 ricAffinity(aff1
1e398 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 ) || sqlite3IsNu
1e399 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 mericAffinity(af
1e39a 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 f2) ){. ret
1e39b 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e urn SQLITE_AFF_N
1e39c 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 UMERIC;. }els
1e39d 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
1e39e 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b SQLITE_AFF_NONE;
1e39f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
1e3a0 66 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 f( !aff1 && !aff
1e3a1 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 2 ){. /* Neit
1e3a2 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 her side of the
1e3a3 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 comparison is a
1e3a4 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 column. Compare
1e3a5 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 the. ** resu
1e3a6 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 lts directly..
1e3a7 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 */. return
1e3a8 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b SQLITE_AFF_NONE;
1e3a9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1e3aa 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 One side is a c
1e3ab 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 olumn, the other
1e3ac 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 is not. Use the
1e3ad 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 columns affinit
1e3ae 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 y. */. assert
1e3af 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 ( aff1==0 || aff
1e3b0 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 2==0 );. retu
1e3b1 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 rn (aff1 + aff2)
1e3b2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 ;. }.}../*.** p
1e3b3 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 Expr is a compar
1e3b4 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 ison operator.
1e3b5 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 Return the type
1e3b6 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
1e3b7 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 ould.** be appli
1e3b8 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 ed to both opera
1e3b9 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 nds prior to doi
1e3ba 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f ng the compariso
1e3bb 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 n..*/.static cha
1e3bc 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 r comparisonAffi
1e3bd 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 nity(Expr *pExpr
1e3be 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 ){. char aff;.
1e3bf 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1e3c0 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 op==TK_EQ || pEx
1e3c1 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c pr->op==TK_IN ||
1e3c2 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c pExpr->op==TK_L
1e3c3 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 T ||. p
1e3c4 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 Expr->op==TK_GT
1e3c5 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b || pExpr->op==TK
1e3c6 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 _GE || pExpr->op
1e3c7 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 ==TK_LE ||.
1e3c8 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d pExpr->op==
1e3c9 54 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 TK_NE );. asser
1e3ca 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 t( pExpr->pLeft
1e3cb 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 );. aff = sqlit
1e3cc 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 e3ExprAffinity(p
1e3cd 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 Expr->pLeft);.
1e3ce 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 if( pExpr->pRigh
1e3cf 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 t ){. aff = s
1e3d0 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 qlite3CompareAff
1e3d1 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 inity(pExpr->pRi
1e3d2 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c ght, aff);. }el
1e3d3 73 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72 se if( ExprHasPr
1e3d4 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1e3d5 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
1e3d6 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 aff = sqlite3
1e3d7 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
1e3d8 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 pExpr->x.pSelect
1e3d9 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 ->pEList->a[0].p
1e3da 45 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 Expr, aff);. }e
1e3db 6c 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a lse if( !aff ){.
1e3dc 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
1e3dd 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 _AFF_NONE;. }.
1e3de 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a return aff;.}..
1e3df 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 /*.** pExpr is a
1e3e0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 comparison expr
1e3e1 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c ession, eg. '=',
1e3e2 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 '<', IN(...) et
1e3e3 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 c..** idx_affini
1e3e4 74 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 ty is the affini
1e3e5 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 ty of an indexed
1e3e6 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 column. Return
1e3e7 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 true.** if the i
1e3e8 6e 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 ndex with affini
1e3e9 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 ty idx_affinity
1e3ea 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 may be used to i
1e3eb 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 mplement.** the
1e3ec 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 comparison in pE
1e3ed 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 xpr..*/.SQLITE_P
1e3ee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1e3ef 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f e3IndexAffinityO
1e3f0 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 k(Expr *pExpr, c
1e3f1 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 har idx_affinity
1e3f2 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 ){. char aff =
1e3f3 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 comparisonAffini
1e3f4 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 ty(pExpr);. swi
1e3f5 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 tch( aff ){.
1e3f6 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f case SQLITE_AFF_
1e3f7 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 NONE:. retu
1e3f8 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 rn 1;. case S
1e3f9 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a QLITE_AFF_TEXT:.
1e3fa 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 return idx
1e3fb 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 _affinity==SQLIT
1e3fc 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 E_AFF_TEXT;.
1e3fd 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 default:. r
1e3fe 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e eturn sqlite3IsN
1e3ff 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 umericAffinity(i
1e400 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 dx_affinity);.
1e401 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
1e402 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 n the P5 value t
1e403 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 hat should be us
1e404 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 ed for a binary
1e405 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 comparison.** op
1e406 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f code (OP_Eq, OP_
1e407 47 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f Ge etc.) used to
1e408 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 compare pExpr1
1e409 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 and pExpr2..*/.s
1e40a 74 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 tatic u8 binaryC
1e40b 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 ompareP5(Expr *p
1e40c 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 Expr1, Expr *pEx
1e40d 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e pr2, int jumpIfN
1e40e 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d ull){. u8 aff =
1e40f 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 (char)sqlite3Ex
1e410 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 prAffinity(pExpr
1e411 32 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 2);. aff = (u8)
1e412 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 sqlite3CompareAf
1e413 66 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 finity(pExpr1, a
1e414 66 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 ff) | (u8)jumpIf
1e415 4e 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 Null;. return a
1e416 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ff;.}../*.** Ret
1e417 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
1e418 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 the collation s
1e419 65 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f equence that sho
1e41a 75 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a uld be used by.*
1e41b 2a 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 * a binary compa
1e41c 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 rison operator c
1e41d 6f 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 omparing pLeft a
1e41e 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a nd pRight..**.**
1e41f 20 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e If the left han
1e420 64 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 d expression has
1e421 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 a collating seq
1e422 75 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e uence type, then
1e423 20 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 it is.** used.
1e424 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f Otherwise the co
1e425 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
1e426 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 for the right h
1e427 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a and expression.*
1e428 2a 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 * is used, or th
1e429 65 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 e default (BINAR
1e42a 59 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 Y) if neither ex
1e42b 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 pression has a c
1e42c 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 ollating.** type
1e42d 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 ..**.** Argument
1e42e 20 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 pRight (but not
1e42f 20 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 pLeft) may be a
1e430 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 null pointer. I
1e431 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 n this case,.**
1e432 69 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 it is not consid
1e433 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ered..*/.SQLITE_
1e434 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
1e435 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f *sqlite3BinaryCo
1e436 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 mpareCollSeq(.
1e437 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a Parse *pParse, .
1e438 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a Expr *pLeft, .
1e439 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 Expr *pRight.)
1e43a 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f {. CollSeq *pCo
1e43b 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c ll;. assert( pL
1e43c 65 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 eft );. if( pLe
1e43d 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 ft->flags & EP_E
1e43e 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 xpCollate ){.
1e43f 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e assert( pLeft->
1e440 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f pColl );. pCo
1e441 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c ll = pLeft->pCol
1e442 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 l;. }else if( p
1e443 52 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d Right && pRight-
1e444 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 >flags & EP_ExpC
1e445 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 ollate ){. as
1e446 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 sert( pRight->pC
1e447 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c oll );. pColl
1e448 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c = pRight->pColl
1e449 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1e44a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 Coll = sqlite3Ex
1e44b 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 prCollSeq(pParse
1e44c 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 , pLeft);. if
1e44d 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 ( !pColl ){.
1e44e 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
1e44f 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 3ExprCollSeq(pPa
1e450 72 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 rse, pRight);.
1e451 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1e452 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pColl;.}../*.**
1e453 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 Generate the op
1e454 65 72 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d erands for a com
1e455 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f parison operatio
1e456 6e 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 n. Before.** ge
1e457 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 nerating the cod
1e458 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 e for each opera
1e459 6e 64 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 nd, set the EP_A
1e45a 6e 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e nyAff.** flag on
1e45b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1e45c 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 so that it will
1e45d 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 be able to used
1e45e 61 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 a.** cached colu
1e45f 6d 6e 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 mn value that ha
1e460 73 20 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 s previously und
1e461 65 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 ergone an.** aff
1e462 69 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f inity change..*/
1e463 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 .static void cod
1e464 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 eCompareOperands
1e465 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1e466 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 e, /* Parsing
1e467 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 and code genera
1e468 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a ting context */.
1e469 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 Expr *pLeft,
1e46a 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 /* The left
1e46b 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 operand */. int
1e46c 20 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f *pRegLeft, /
1e46d 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 72 65 * Register where
1e46e 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 left operand is
1e46f 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 stored */. int
1e470 20 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f *pFreeLeft, /
1e471 2a 20 46 72 65 65 20 74 68 69 73 20 72 65 67 69 * Free this regi
1e472 73 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a ster when done *
1e473 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 /. Expr *pRight
1e474 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 , /* The rig
1e475 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 ht operand */.
1e476 69 6e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 int *pRegRight,
1e477 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 /* Register wh
1e478 65 72 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e ere right operan
1e479 64 20 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 d is stored */.
1e47a 20 69 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 int *pFreeRight
1e47b 20 20 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 /* Write temp
1e47c 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 register for ri
1e47d 67 68 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 ght operand ther
1e47e 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 e */.){. while(
1e47f 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 pLeft->op==TK_U
1e480 50 4c 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 PLUS ) pLeft = p
1e481 4c 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 Left->pLeft;. p
1e482 4c 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 Left->flags |= E
1e483 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 P_AnyAff;. *pRe
1e484 67 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 gLeft = sqlite3E
1e485 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 xprCodeTemp(pPar
1e486 73 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 se, pLeft, pFree
1e487 4c 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 Left);. while(
1e488 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 pRight->op==TK_U
1e489 50 4c 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 PLUS ) pRight =
1e48a 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 pRight->pLeft;.
1e48b 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c pRight->flags |
1e48c 3d 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a = EP_AnyAff;. *
1e48d 70 52 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 pRegRight = sqli
1e48e 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
1e48f 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 pParse, pRight,
1e490 70 46 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a pFreeRight);.}..
1e491 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
1e492 6f 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 ode for a compar
1e493 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a ison operator..*
1e494 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 /.static int cod
1e495 65 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 eCompare(. Pars
1e496 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a e *pParse, /*
1e497 20 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e The parsing (an
1e498 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e d code generatin
1e499 67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 g) context */.
1e49a 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 Expr *pLeft,
1e49b 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 /* The left op
1e49c 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 erand */. Expr
1e49d 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 *pRight, /*
1e49e 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e The right operan
1e49f 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 d */. int opcod
1e4a0 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 e, /* The
1e4a1 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 comparison opcod
1e4a2 65 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 e */. int in1,
1e4a3 69 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 int in2, /* Regi
1e4a4 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 ster holding ope
1e4a5 72 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 rands */. int d
1e4a6 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 est, /*
1e4a7 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 Jump here if tru
1e4a8 65 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d e. */. int jum
1e4a9 70 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 pIfNull /* If
1e4aa 20 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 true, jump if e
1e4ab 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1e4ac 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e NULL */.){. in
1e4ad 74 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 t p5;. int addr
1e4ae 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b ;. CollSeq *p4;
1e4af 0a 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 .. p4 = sqlite3
1e4b0 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c BinaryCompareCol
1e4b1 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 lSeq(pParse, pLe
1e4b2 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 ft, pRight);. p
1e4b3 35 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 5 = binaryCompar
1e4b4 65 50 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 eP5(pLeft, pRigh
1e4b5 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a t, jumpIfNull);.
1e4b6 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 addr = sqlite3
1e4b7 56 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 VdbeAddOp4(pPars
1e4b8 65 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 e->pVdbe, opcode
1e4b9 2c 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 , in2, dest, in1
1e4ba 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1e4bb 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f (vo
1e4bc 69 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 id*)p4, P4_COLLS
1e4bd 45 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 EQ);. sqlite3Vd
1e4be 62 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 beChangeP5(pPars
1e4bf 65 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 e->pVdbe, (u8)p5
1e4c0 29 3b 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 );. if( (p5 & S
1e4c1 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 QLITE_AFF_MASK)!
1e4c2 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 =SQLITE_AFF_NONE
1e4c3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1e4c4 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 xprCacheAffinity
1e4c5 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 Change(pParse, i
1e4c6 6e 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 n1, 1);. sqli
1e4c7 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 te3ExprCacheAffi
1e4c8 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 nityChange(pPars
1e4c9 65 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a e, in2, 1);. }.
1e4ca 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d return addr;.}
1e4cb 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ..#if SQLITE_MAX
1e4cc 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a _EXPR_DEPTH>0./*
1e4cd 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 .** Check that a
1e4ce 72 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 rgument nHeight
1e4cf 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
1e4d0 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 equal to the max
1e4d1 69 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 imum.** expressi
1e4d2 6f 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 on depth allowed
1e4d3 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 . If it is not,
1e4d4 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d leave an error m
1e4d5 65 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 essage in.** pPa
1e4d6 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rse..*/.SQLITE_P
1e4d7 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1e4d8 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 e3ExprCheckHeigh
1e4d9 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c t(Parse *pParse,
1e4da 20 69 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 int nHeight){.
1e4db 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1e4dc 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 _OK;. int mxHei
1e4dd 67 68 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 ght = pParse->db
1e4de 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1e4df 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 LIMIT_EXPR_DEPTH
1e4e0 5d 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 ];. if( nHeight
1e4e1 3e 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 >mxHeight ){.
1e4e2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1e4e3 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 (pParse, .
1e4e4 20 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 "Expression tre
1e4e5 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 e is too large (
1e4e6 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 maximum depth %d
1e4e7 29 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 )", mxHeight.
1e4e8 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c );. rc = SQL
1e4e9 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 ITE_ERROR;. }.
1e4ea 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1e4eb 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1e4ec 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c three functions,
1e4ed 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c heightOfExpr(),
1e4ee 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
1e4ef 74 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 t().** and heigh
1e4f0 74 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 tOfSelect(), are
1e4f1 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 used to determi
1e4f2 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 ne the maximum h
1e4f3 65 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 eight.** of any
1e4f4 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 expression tree
1e4f5 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 referenced by th
1e4f6 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 e structure pass
1e4f7 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 ed as the.** fir
1e4f8 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a st argument..**.
1e4f9 2a 2a 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d ** If this maxim
1e4fa 75 6d 20 68 65 69 67 68 74 20 69 73 20 67 72 65 um height is gre
1e4fb 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 ater than the cu
1e4fc 72 72 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e rrent value poin
1e4fd 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 ted.** to by pnH
1e4fe 65 69 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e eight, the secon
1e4ff 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 d parameter, the
1e500 6e 20 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 n set *pnHeight
1e501 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 to that.** value
1e502 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1e503 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 heightOfExpr(Ex
1e504 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 pr *p, int *pnHe
1e505 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 ight){. if( p )
1e506 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 {. if( p->nHe
1e507 69 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 ight>*pnHeight )
1e508 7b 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 {. *pnHeigh
1e509 74 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a t = p->nHeight;.
1e50a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 }. }.}.stat
1e50b 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 ic void heightOf
1e50c 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 ExprList(ExprLis
1e50d 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 t *p, int *pnHei
1e50e 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b ght){. if( p ){
1e50f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1e510 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 for(i=0; i<p->nE
1e511 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
1e512 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d heightOfExpr(p-
1e513 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 >a[i].pExpr, pnH
1e514 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 eight);. }.
1e515 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 }.}.static void
1e516 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 heightOfSelect(S
1e517 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 elect *p, int *p
1e518 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 nHeight){. if(
1e519 70 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f p ){. heightO
1e51a 66 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c fExpr(p->pWhere,
1e51b 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 pnHeight);.
1e51c 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e heightOfExpr(p->
1e51d 70 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 pHaving, pnHeigh
1e51e 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 t);. heightOf
1e51f 45 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 Expr(p->pLimit,
1e520 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 pnHeight);. h
1e521 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 eightOfExpr(p->p
1e522 4f 66 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 Offset, pnHeight
1e523 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 );. heightOfE
1e524 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 xprList(p->pELis
1e525 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 t, pnHeight);.
1e526 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 heightOfExprLi
1e527 73 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 st(p->pGroupBy,
1e528 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 pnHeight);. h
1e529 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 eightOfExprList(
1e52a 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 p->pOrderBy, pnH
1e52b 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 eight);. heig
1e52c 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 htOfSelect(p->pP
1e52d 72 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b rior, pnHeight);
1e52e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
1e52f 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 t the Expr.nHeig
1e530 68 74 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 ht variable in t
1e531 68 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 he structure pas
1e532 73 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 sed as an .** ar
1e533 67 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 gument. An expre
1e534 73 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 ssion with no ch
1e535 69 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 ildren, Expr.pLi
1e536 73 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 st or .** Expr.p
1e537 53 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 Select member ha
1e538 73 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e s a height of 1.
1e539 20 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 Any other expre
1e53a 73 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 ssion.** has a h
1e53b 65 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 eight equal to t
1e53c 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 he maximum heigh
1e53d 74 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a t of any other .
1e53e 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 ** referenced Ex
1e53f 70 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a pr plus one..*/.
1e540 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 static void expr
1e541 53 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a SetHeight(Expr *
1e542 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 p){. int nHeigh
1e543 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f t = 0;. heightO
1e544 66 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 fExpr(p->pLeft,
1e545 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 &nHeight);. hei
1e546 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 ghtOfExpr(p->pRi
1e547 67 68 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a ght, &nHeight);.
1e548 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
1e549 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 perty(p, EP_xIsS
1e54a 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65 elect) ){. he
1e54b 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e ightOfSelect(p->
1e54c 78 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 x.pSelect, &nHei
1e54d 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ght);. }else{.
1e54e 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c heightOfExprL
1e54f 69 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 ist(p->x.pList,
1e550 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 &nHeight);. }.
1e551 20 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 p->nHeight = nH
1e552 65 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a eight + 1;.}../*
1e553 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 .** Set the Expr
1e554 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c .nHeight variabl
1e555 65 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 e using the expr
1e556 53 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 SetHeight() func
1e557 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 tion. If.** the
1e558 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 height is greate
1e559 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d r than the maxim
1e55a 75 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 um allowed expre
1e55b 73 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 ssion depth,.**
1e55c 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 leave an error i
1e55d 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c n pParse..*/.SQL
1e55e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1e55f 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 sqlite3ExprSetH
1e560 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 eight(Parse *pPa
1e561 72 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 rse, Expr *p){.
1e562 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 exprSetHeight(p
1e563 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
1e564 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 CheckHeight(pPar
1e565 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b se, p->nHeight);
1e566 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1e567 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 the maximum hei
1e568 67 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 ght of any expre
1e569 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 ssion tree refer
1e56a 65 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 enced.** by the
1e56b 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 select statement
1e56c 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 passed as an ar
1e56d 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 gument..*/.SQLIT
1e56e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1e56f 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 lite3SelectExprH
1e570 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 eight(Select *p)
1e571 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 {. int nHeight
1e572 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 = 0;. heightOfS
1e573 65 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 elect(p, &nHeigh
1e574 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 t);. return nHe
1e575 69 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 ight;.}.#else.
1e576 23 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 #define exprSetH
1e577 65 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 eight(y).#endif
1e578 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 /* SQLITE_MAX_EX
1e579 50 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f PR_DEPTH>0 */../
1e57a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 *.** Construct a
1e57b 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 new expression
1e57c 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 72 6e 20 node and return
1e57d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
1e57e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72 20 Memory.** for
1e57f 74 68 69 73 20 6e 6f 64 65 20 69 73 20 6f 62 74 this node is obt
1e580 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
1e581 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 e3_malloc(). Th
1e582 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
1e583 6f 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 on.** is respons
1e584 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 ible for making
1e585 73 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 sure the node ev
1e586 65 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 entually gets fr
1e587 65 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 eed..*/.SQLITE_P
1e588 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
1e589 69 74 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69 ite3Expr(. sqli
1e58a 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
1e58b 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f /* Handle fo
1e58c 72 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f r sqlite3DbMallo
1e58d 63 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 cZero() (may be
1e58e 6e 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f null) */. int o
1e58f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
1e590 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e /* Expression
1e591 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 opcode */. Exp
1e592 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 r *pLeft,
1e593 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 /* Left ope
1e594 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a rand */. Expr *
1e595 70 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 pRight,
1e596 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 /* Right opera
1e597 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f nd */. const To
1e598 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 ken *pToken
1e599 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 /* Argument toke
1e59a 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a n */.){. Expr *
1e59b 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 pNew;. pNew = s
1e59c 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
1e59d 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 ro(db, sizeof(Ex
1e59e 70 72 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 pr));. if( pNew
1e59f 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 ==0 ){. /* Wh
1e5a0 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c en malloc fails,
1e5a1 20 64 65 6c 65 74 65 20 70 4c 65 66 74 20 61 6e delete pLeft an
1e5a2 64 20 70 52 69 67 68 74 2e 20 45 78 70 72 65 73 d pRight. Expres
1e5a3 73 69 6f 6e 73 20 70 61 73 73 65 64 20 74 6f 20 sions passed to
1e5a4 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 66 75 6e . ** this fun
1e5a5 63 74 69 6f 6e 20 6d 75 73 74 20 61 6c 77 61 79 ction must alway
1e5a6 73 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 77 s be allocated w
1e5a7 69 74 68 20 73 71 6c 69 74 65 33 45 78 70 72 28 ith sqlite3Expr(
1e5a8 29 20 66 6f 72 20 74 68 69 73 20 0a 20 20 20 20 ) for this .
1e5a9 2a 2a 20 72 65 61 73 6f 6e 2e 20 0a 20 20 20 20 ** reason. .
1e5aa 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 */. sqlite3Ex
1e5ab 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 65 prDelete(db, pLe
1e5ac 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ft);. sqlite3
1e5ad 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1e5ae 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 Right);. retu
1e5af 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 rn 0;. }. pNew
1e5b0 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 ->op = (u8)op;.
1e5b1 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70 pNew->pLeft = p
1e5b2 4c 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 Left;. pNew->pR
1e5b3 69 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 ight = pRight;.
1e5b4 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 pNew->iAgg = -1
1e5b5 3b 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a ;. pNew->span.z
1e5b6 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 69 66 = (u8*)"";. if
1e5b7 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 ( pToken ){.
1e5b8 69 6e 74 20 63 3b 0a 20 20 20 20 61 73 73 65 72 int c;. asser
1e5b9 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d t( pToken->dyn==
1e5ba 30 20 29 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 73 0 );. pNew->s
1e5bb 70 61 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 pan = *pToken;.
1e5bc 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 54 6f 6b . /* The pTok
1e5bd 65 6e 2d 3e 7a 20 76 61 6c 75 65 20 69 73 20 72 en->z value is r
1e5be 65 61 64 2d 6f 6e 6c 79 2e 20 20 42 75 74 20 74 ead-only. But t
1e5bf 68 65 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f he new expressio
1e5c0 6e 0a 20 20 20 20 2a 2a 20 6e 6f 64 65 20 63 72 n. ** node cr
1e5c1 65 61 74 65 64 20 68 65 72 65 20 6d 69 67 68 74 eated here might
1e5c2 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 be passed to sq
1e5c3 6c 69 74 65 33 44 65 71 75 6f 74 65 45 78 70 72 lite3DequoteExpr
1e5c4 28 29 20 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 () which. **
1e5c5 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 will attempt to
1e5c6 6d 6f 64 69 66 79 20 70 4e 65 77 2d 3e 74 6f 6b modify pNew->tok
1e5c7 65 6e 2e 7a 2e 20 20 48 65 6e 63 65 2c 20 69 66 en.z. Hence, if
1e5c8 20 74 68 65 20 74 6f 6b 65 6e 0a 20 20 20 20 2a the token. *
1e5c9 2a 20 69 73 20 71 75 6f 74 65 64 2c 20 6d 61 6b * is quoted, mak
1e5ca 65 20 61 20 63 6f 70 79 20 6e 6f 77 20 73 6f 20 e a copy now so
1e5cb 74 68 61 74 20 44 65 71 75 6f 74 65 45 78 70 72 that DequoteExpr
1e5cc 28 29 20 77 69 6c 6c 20 63 68 61 6e 67 65 0a 20 () will change.
1e5cd 20 20 20 2a 2a 20 74 68 65 20 63 6f 70 79 20 72 ** the copy r
1e5ce 61 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f ather than the o
1e5cf 72 69 67 69 6e 61 6c 20 74 65 78 74 2e 0a 20 20 riginal text..
1e5d0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 6f */. if( pTo
1e5d1 6b 65 6e 2d 3e 6e 3e 3d 32 20 0a 20 20 20 20 20 ken->n>=2 .
1e5d2 20 20 20 20 26 26 20 28 28 63 20 3d 20 70 54 6f && ((c = pTo
1e5d3 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 27 ken->z[0])=='\''
1e5d4 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63 3d || c=='"' || c=
1e5d5 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29 20 ='[' || c=='`')
1e5d6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1e5d7 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 TokenCopy(db, &p
1e5d8 4e 65 77 2d 3e 74 6f 6b 65 6e 2c 20 70 54 6f 6b New->token, pTok
1e5d9 65 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a en);. }else{.
1e5da 20 20 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 pNew->toke
1e5db 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 20 n = *pToken;.
1e5dc 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c pNew->flags |
1e5dd 3d 20 45 50 5f 44 65 71 75 6f 74 65 64 3b 0a 20 = EP_Dequoted;.
1e5de 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 VVA_ONLY( p
1e5df 4e 65 77 2d 3e 76 76 61 46 6c 61 67 73 20 7c 3d New->vvaFlags |=
1e5e0 20 45 56 56 41 5f 52 65 61 64 4f 6e 6c 79 54 6f EVVA_ReadOnlyTo
1e5e1 6b 65 6e 3b 20 29 0a 20 20 20 20 7d 0a 20 20 7d ken; ). }. }
1e5e2 65 6c 73 65 20 69 66 28 20 70 4c 65 66 74 20 29 else if( pLeft )
1e5e3 7b 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 {. if( pRight
1e5e4 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 52 ){. if( pR
1e5e5 69 67 68 74 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d ight->span.dyn==
1e5e6 30 20 26 26 20 70 4c 65 66 74 2d 3e 73 70 61 6e 0 && pLeft->span
1e5e7 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 .dyn==0 ){.
1e5e8 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 sqlite3ExprSp
1e5e9 61 6e 28 70 4e 65 77 2c 20 26 70 4c 65 66 74 2d an(pNew, &pLeft-
1e5ea 3e 73 70 61 6e 2c 20 26 70 52 69 67 68 74 2d 3e >span, &pRight->
1e5eb 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 span);. }.
1e5ec 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d if( pRight-
1e5ed 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 >flags & EP_ExpC
1e5ee 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 ollate ){.
1e5ef 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d pNew->flags |=
1e5f0 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a EP_ExpCollate;.
1e5f1 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 pNew->pC
1e5f2 6f 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 oll = pRight->pC
1e5f3 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 oll;. }.
1e5f4 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 }. if( pLeft
1e5f5 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 ->flags & EP_Exp
1e5f6 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 Collate ){.
1e5f7 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 pNew->flags |=
1e5f8 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 EP_ExpCollate;.
1e5f9 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c pNew->pColl
1e5fa 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b = pLeft->pColl;
1e5fb 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 65 78 . }. }.. ex
1e5fc 70 72 53 65 74 48 65 69 67 68 74 28 70 4e 65 77 prSetHeight(pNew
1e5fd 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 );. return pNew
1e5fe 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 6f 72 6b 73 ;.}../*.** Works
1e5ff 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 45 78 70 like sqlite3Exp
1e600 72 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 r() except that
1e601 69 74 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72 it takes an extr
1e602 61 20 50 61 72 73 65 2a 0a 2a 2a 20 61 72 67 75 a Parse*.** argu
1e603 6d 65 6e 74 20 61 6e 64 20 6e 6f 74 69 66 69 65 ment and notifie
1e604 73 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 s the associated
1e605 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 connection obje
1e606 63 74 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 ct if malloc fai
1e607 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ls..*/.SQLITE_PR
1e608 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
1e609 74 65 33 50 45 78 70 72 28 0a 20 20 50 61 72 73 te3PExpr(. Pars
1e60a 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
1e60b 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
1e60c 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
1e60d 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 op,
1e60e 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f /* Expressio
1e60f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 n opcode */. Ex
1e610 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 pr *pLeft,
1e611 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 /* Left op
1e612 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 erand */. Expr
1e613 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 *pRight,
1e614 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 /* Right oper
1e615 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 and */. const T
1e616 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 oken *pToken
1e617 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b /* Argument tok
1e618 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 en */.){. Expr
1e619 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 *p = sqlite3Expr
1e61a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c (pParse->db, op,
1e61b 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 pLeft, pRight,
1e61c 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70 pToken);. if( p
1e61d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1e61e 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 70 xprCheckHeight(p
1e61f 50 61 72 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 Parse, p->nHeigh
1e620 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
1e621 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 p;.}../*.** Whe
1e622 6e 20 64 6f 69 6e 67 20 61 20 6e 65 73 74 65 64 n doing a nested
1e623 20 70 61 72 73 65 2c 20 79 6f 75 20 63 61 6e 20 parse, you can
1e624 69 6e 63 6c 75 64 65 20 74 65 72 6d 73 20 69 6e include terms in
1e625 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a an expression.*
1e626 2a 20 74 68 61 74 20 6c 6f 6f 6b 20 6c 69 6b 65 * that look like
1e627 20 74 68 69 73 3a 20 20 20 23 31 20 23 32 20 2e this: #1 #2 .
1e628 2e 2e 20 20 54 68 65 73 65 20 74 65 72 6d 73 20 .. These terms
1e629 72 65 66 65 72 20 74 6f 20 72 65 67 69 73 74 65 refer to registe
1e62a 72 73 0a 2a 2a 20 69 6e 20 74 68 65 20 76 69 72 rs.** in the vir
1e62b 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 23 tual machine. #
1e62c 4e 20 69 73 20 74 68 65 20 4e 2d 74 68 20 72 65 N is the N-th re
1e62d 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 gister..**.** Th
1e62e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
1e62f 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 lled by the pars
1e630 65 72 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 er to deal with
1e631 6f 6e 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d on of those term
1e632 73 2e 0a 2a 2a 20 49 74 20 69 6d 6d 65 64 69 61 s..** It immedia
1e633 74 65 6c 79 20 67 65 6e 65 72 61 74 65 73 20 63 tely generates c
1e634 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ode to store the
1e635 20 76 61 6c 75 65 20 69 6e 20 61 20 6d 65 6d 6f value in a memo
1e636 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 ry location..**
1e637 54 68 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 The returns an e
1e638 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 xpression that w
1e639 69 6c 6c 20 63 6f 64 65 20 74 6f 20 65 78 74 72 ill code to extr
1e63a 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 66 72 act the value fr
1e63b 6f 6d 0a 2a 2a 20 74 68 61 74 20 6d 65 6d 6f 72 om.** that memor
1e63c 79 20 6c 6f 63 61 74 69 6f 6e 20 61 73 20 6e 65 y location as ne
1e63d 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eded..*/.SQLITE_
1e63e 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
1e63f 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78 70 lite3RegisterExp
1e640 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c r(Parse *pParse,
1e641 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b Token *pToken){
1e642 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 . Vdbe *v = pPa
1e643 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 45 78 rse->pVdbe;. Ex
1e644 70 72 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 61 pr *p;. if( pPa
1e645 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 rse->nested==0 )
1e646 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
1e647 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e orMsg(pParse, "n
1e648 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 ear \"%T\": synt
1e649 61 78 20 65 72 72 6f 72 22 2c 20 70 54 6f 6b 65 ax error", pToke
1e64a 6e 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 n);. return s
1e64b 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 qlite3PExpr(pPar
1e64c 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 se, TK_NULL, 0,
1e64d 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 0, 0);. }. if(
1e64e 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 v==0 ) return 0
1e64f 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 ;. p = sqlite3P
1e650 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f Expr(pParse, TK_
1e651 52 45 47 49 53 54 45 52 2c 20 30 2c 20 30 2c 20 REGISTER, 0, 0,
1e652 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70 pToken);. if( p
1e653 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
1e654 6e 20 30 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 n 0; /* Malloc
1e655 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 failed */. }.
1e656 70 2d 3e 69 54 61 62 6c 65 20 3d 20 61 74 6f 69 p->iTable = atoi
1e657 28 28 63 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d ((char*)&pToken-
1e658 3e 7a 5b 31 5d 29 3b 0a 20 20 72 65 74 75 72 6e >z[1]);. return
1e659 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69 p;.}../*.** Joi
1e65a 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e n two expression
1e65b 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f s using an AND o
1e65c 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74 perator. If eit
1e65d 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 her expression i
1e65e 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 s.** NULL, then
1e65f 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 just return the
1e660 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e other expression
1e661 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1e662 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
1e663 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65 33 3ExprAnd(sqlite3
1e664 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 66 *db, Expr *pLef
1e665 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 74 29 t, Expr *pRight)
1e666 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d 30 {. if( pLeft==0
1e667 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
1e668 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69 Right;. }else i
1e669 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a f( pRight==0 ){.
1e66a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66 74 return pLeft
1e66b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
1e66c 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 eturn sqlite3Exp
1e66d 72 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 70 4c r(db, TK_AND, pL
1e66e 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b eft, pRight, 0);
1e66f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
1e670 74 20 74 68 65 20 45 78 70 72 2e 73 70 61 6e 20 t the Expr.span
1e671 66 69 65 6c 64 20 6f 66 20 74 68 65 20 67 69 76 field of the giv
1e672 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f en expression to
1e673 20 73 70 61 6e 20 61 6c 6c 0a 2a 2a 20 74 65 78 span all.** tex
1e674 74 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 t between the tw
1e675 6f 20 67 69 76 65 6e 20 74 6f 6b 65 6e 73 2e 20 o given tokens.
1e676 20 42 6f 74 68 20 74 6f 6b 65 6e 73 20 6d 75 73 Both tokens mus
1e677 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a t be pointing.**
1e678 20 61 74 20 74 68 65 20 73 61 6d 65 20 73 74 72 at the same str
1e679 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ing..*/.SQLITE_P
1e67a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1e67b 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 70 72 te3ExprSpan(Expr
1e67c 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a *pExpr, Token *
1e67d 70 4c 65 66 74 2c 20 54 6f 6b 65 6e 20 2a 70 52 pLeft, Token *pR
1e67e 69 67 68 74 29 7b 0a 20 20 61 73 73 65 72 74 28 ight){. assert(
1e67f 20 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 pRight!=0 );.
1e680 61 73 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30 assert( pLeft!=0
1e681 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 );. if( pExpr
1e682 29 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e 73 70 ){. pExpr->sp
1e683 61 6e 2e 7a 20 3d 20 70 4c 65 66 74 2d 3e 7a 3b an.z = pLeft->z;
1e684 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c . /* The foll
1e685 6f 77 69 6e 67 20 61 73 73 65 72 74 28 29 20 6d owing assert() m
1e686 61 79 20 66 61 69 6c 20 77 68 65 6e 20 74 68 69 ay fail when thi
1e687 73 20 69 73 20 63 61 6c 6c 65 64 20 0a 20 20 20 s is called .
1e688 20 2a 2a 20 76 69 61 20 73 71 6c 69 74 65 33 50 ** via sqlite3P
1e689 45 78 70 72 28 29 2f 73 71 6c 69 74 65 33 45 78 Expr()/sqlite3Ex
1e68a 70 72 28 29 20 66 72 6f 6d 20 61 64 64 57 68 65 pr() from addWhe
1e68b 72 65 54 65 72 6d 28 29 2e 20 2a 2f 0a 20 20 20 reTerm(). */.
1e68c 20 2f 2a 20 61 73 73 65 72 74 28 70 52 69 67 68 /* assert(pRigh
1e68d 74 2d 3e 7a 20 3e 3d 20 70 4c 65 66 74 2d 3e 7a t->z >= pLeft->z
1e68e 29 3b 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d ); */. pExpr-
1e68f 3e 73 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68 74 >span.n = pRight
1e690 2d 3e 6e 20 2b 20 28 75 6e 73 69 67 6e 65 64 29 ->n + (unsigned)
1e691 28 70 52 69 67 68 74 2d 3e 7a 20 2d 20 70 4c 65 (pRight->z - pLe
1e692 66 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ft->z);. }.}../
1e693 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 *.** Construct a
1e694 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 new expression
1e695 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e 63 74 node for a funct
1e696 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69 70 6c ion with multipl
1e697 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a e.** arguments..
1e698 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1e699 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
1e69a 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 73 xprFunction(Pars
1e69b 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c e *pParse, ExprL
1e69c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 ist *pList, Toke
1e69d 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45 78 n *pToken){. Ex
1e69e 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 71 6c 69 pr *pNew;. sqli
1e69f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1e6a0 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 ->db;. assert(
1e6a1 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e 65 77 pToken );. pNew
1e6a2 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1e6a3 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
1e6a4 66 28 45 78 70 72 29 20 29 3b 0a 20 20 69 66 28 f(Expr) );. if(
1e6a5 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 pNew==0 ){.
1e6a6 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
1e6a7 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 elete(db, pList)
1e6a8 3b 20 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 ; /* Avoid memor
1e6a9 79 20 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c y leak when mall
1e6aa 6f 63 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 oc fails */.
1e6ab 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1e6ac 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 46 55 pNew->op = TK_FU
1e6ad 4e 43 54 49 4f 4e 3b 0a 20 20 70 4e 65 77 2d 3e NCTION;. pNew->
1e6ae 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b x.pList = pList;
1e6af 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 . assert( !Expr
1e6b0 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 HasProperty(pNew
1e6b1 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
1e6b2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f );. assert( pTo
1e6b3 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 ken->dyn==0 );.
1e6b4 20 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 2a 70 pNew->span = *p
1e6b5 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65 33 Token;. sqlite3
1e6b6 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 TokenCopy(db, &p
1e6b7 4e 65 77 2d 3e 74 6f 6b 65 6e 2c 20 70 54 6f 6b New->token, pTok
1e6b8 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 en);. sqlite3Ex
1e6b9 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72 prSetHeight(pPar
1e6ba 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72 65 74 se, pNew);. ret
1e6bb 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
1e6bc 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 72 69 ** Assign a vari
1e6bd 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 able number to a
1e6be 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 n expression tha
1e6bf 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 6c 64 t encodes a wild
1e6c0 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f card.** in the o
1e6c1 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 riginal SQL stat
1e6c2 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 ement. .**.** W
1e6c3 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69 73 74 ildcards consist
1e6c4 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 ing of a single
1e6c5 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 "?" are assigned
1e6c6 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e the next sequen
1e6c7 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 tial.** variable
1e6c8 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 number..**.** W
1e6c9 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 ildcards of the
1e6ca 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20 form "?nnn" are
1e6cb 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 75 6d assigned the num
1e6cc 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d ber "nnn". We m
1e6cd 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e ake.** sure "nnn
1e6ce 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20 " is not too be
1e6cf 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e 69 61 to avoid a denia
1e6d0 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61 74 74 l of service att
1e6d1 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 ack when.** the
1e6d2 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f SQL statement co
1e6d3 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78 74 65 mes from an exte
1e6d4 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a rnal source..**.
1e6d5 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 ** Wildcards of
1e6d6 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20 the form ":aaa"
1e6d7 6f 72 20 22 24 61 61 61 22 20 61 72 65 20 61 73 or "$aaa" are as
1e6d8 73 69 67 6e 65 64 20 74 68 65 20 73 61 6d 65 20 signed the same
1e6d9 6e 75 6d 62 65 72 0a 2a 2a 20 61 73 20 74 68 65 number.** as the
1e6da 20 70 72 65 76 69 6f 75 73 20 69 6e 73 74 61 6e previous instan
1e6db 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 77 ce of the same w
1e6dc 69 6c 64 63 61 72 64 2e 20 20 4f 72 20 69 66 20 ildcard. Or if
1e6dd 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 this is the firs
1e6de 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 t.** instance of
1e6df 20 74 68 65 20 77 69 6c 64 63 61 72 64 2c 20 74 the wildcard, t
1e6e0 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e 69 61 he next sequenia
1e6e1 6c 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 l variable numbe
1e6e2 72 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64 r is.** assigned
1e6e3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1e6e4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1e6e5 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d ExprAssignVarNum
1e6e6 62 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 ber(Parse *pPars
1e6e7 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b e, Expr *pExpr){
1e6e8 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e . Token *pToken
1e6e9 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
1e6ea 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 = pParse->db;..
1e6eb 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 if( pExpr==0 )
1e6ec 72 65 74 75 72 6e 3b 0a 20 20 70 54 6f 6b 65 6e return;. pToken
1e6ed 20 3d 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e = &pExpr->token
1e6ee 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b ;. assert( pTok
1e6ef 65 6e 2d 3e 6e 3e 3d 31 20 29 3b 0a 20 20 61 73 en->n>=1 );. as
1e6f0 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 21 sert( pToken->z!
1e6f1 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e6f2 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 21 3d 30 20 pToken->z[0]!=0
1e6f3 29 3b 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d );. if( pToken-
1e6f4 3e 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 >n==1 ){. /*
1e6f5 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 Wildcard of the
1e6f6 66 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 form "?". Assig
1e6f7 6e 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 n the next varia
1e6f8 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 ble number */.
1e6f9 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1e6fa 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 = ++pParse->nVar
1e6fb 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 ;. }else if( pT
1e6fc 6f 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 3f 27 20 oken->z[0]=='?'
1e6fd 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 ){. /* Wildca
1e6fe 72 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 rd of the form "
1e6ff 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20 ?nnn". Convert
1e700 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65 "nnn" to an inte
1e701 67 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75 ger and. ** u
1e702 73 65 20 69 74 20 61 73 20 74 68 65 20 76 61 72 se it as the var
1e703 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a iable number */.
1e704 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 int i;. p
1e705 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 Expr->iTable = i
1e706 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 = atoi((char*)&
1e707 70 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 pToken->z[1]);.
1e708 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d testcase( i==
1e709 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 0 );. testcas
1e70a 65 28 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 74 e( i==1 );. t
1e70b 65 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e estcase( i==db->
1e70c 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1e70d 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
1e70e 42 45 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 65 BER]-1 );. te
1e70f 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 stcase( i==db->a
1e710 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1e711 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 IT_VARIABLE_NUMB
1e712 45 52 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 69 ER] );. if( i
1e713 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d <1 || i>db->aLim
1e714 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1e715 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d VARIABLE_NUMBER]
1e716 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1e717 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1e718 2c 20 22 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 , "variable numb
1e719 65 72 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 er must be betwe
1e71a 65 6e 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c 0a en ?1 and ?%d",.
1e71b 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c db->aL
1e71c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1e71d 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
1e71e 52 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 R]);. }. i
1e71f 66 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 61 f( i>pParse->nVa
1e720 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 r ){. pPars
1e721 65 2d 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 20 e->nVar = i;.
1e722 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
1e723 2f 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 /* Wildcards of
1e724 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20 the form ":aaa"
1e725 6f 72 20 22 24 61 61 61 22 2e 20 20 52 65 75 73 or "$aaa". Reus
1e726 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 e the same varia
1e727 62 6c 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 ble. ** numbe
1e728 72 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 61 r as the prior a
1e729 70 70 65 61 72 61 6e 63 65 20 6f 66 20 74 68 65 ppearance of the
1e72a 20 73 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69 same name, or i
1e72b 66 20 74 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a f the name. *
1e72c 2a 20 68 61 73 20 6e 65 76 65 72 20 61 70 70 65 * has never appe
1e72d 61 72 65 64 20 62 65 66 6f 72 65 2c 20 72 65 75 ared before, reu
1e72e 73 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 se the same vari
1e72f 61 62 6c 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 able number.
1e730 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 */. int i;.
1e731 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d u32 n;. n =
1e732 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20 pToken->n;.
1e733 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 for(i=0; i<pPars
1e734 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b e->nVarExpr; i++
1e735 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 ){. Expr *p
1e736 45 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 45 E;. if( (pE
1e737 20 3d 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 = pParse->apVar
1e738 45 78 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20 Expr[i])!=0.
1e739 20 20 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b && pE->tok
1e73a 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20 en.n==n.
1e73b 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e && memcmp(pE->
1e73c 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d token.z, pToken-
1e73d 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 >z, n)==0 ){.
1e73e 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1e73f 6c 65 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b le = pE->iTable;
1e740 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1e741 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1e742 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d if( i>=pParse-
1e743 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20 >nVarExpr ){.
1e744 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1e745 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 = ++pParse->nVa
1e746 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 r;. if( pPa
1e747 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 rse->nVarExpr>=p
1e748 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 Parse->nVarExprA
1e749 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 lloc-1 ){.
1e74a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 pParse->nVarEx
1e74b 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 prAlloc += pPars
1e74c 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 e->nVarExprAlloc
1e74d 20 2b 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70 + 10;. p
1e74e 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
1e74f 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 =. s
1e750 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f qlite3DbReallocO
1e751 72 46 72 65 65 28 0a 20 20 20 20 20 20 20 20 20 rFree(.
1e752 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 db,.
1e753 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 pParse->a
1e754 70 56 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20 pVarExpr,.
1e755 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
1e756 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 nVarExprAlloc*si
1e757 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 zeof(pParse->apV
1e758 61 72 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 arExpr[0]).
1e759 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
1e75a 7d 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d }. if( !db-
1e75b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
1e75c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1e75d 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 pParse->apVarEx
1e75e 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 pr!=0 );.
1e75f 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 pParse->apVarEx
1e760 70 72 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 pr[pParse->nVarE
1e761 78 70 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a xpr++] = pExpr;.
1e762 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1e763 7d 20 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 } . if( !pParse
1e764 2d 3e 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 ->nErr && pParse
1e765 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 ->nVar>db->aLimi
1e766 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 t[SQLITE_LIMIT_V
1e767 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 ARIABLE_NUMBER]
1e768 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1e769 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1e76a 74 6f 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 too many SQL var
1e76b 69 61 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a iables");. }.}.
1e76c 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 ./*.** Clear an
1e76d 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 expression struc
1e76e 74 75 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c ture without del
1e76f 65 74 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 eting the struct
1e770 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 ure itself..** S
1e771 75 62 73 74 72 75 63 74 75 72 65 20 69 73 20 64 ubstructure is d
1e772 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 eleted..*/.SQLIT
1e773 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1e774 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 qlite3ExprClear(
1e775 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
1e776 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e r *p){. if( p->
1e777 74 6f 6b 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69 token.dyn ) sqli
1e778 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 te3DbFree(db, (c
1e779 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 har*)p->token.z)
1e77a 3b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 ;. if( !ExprHas
1e77b 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 AnyProperty(p, E
1e77c 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 P_TokenOnly|EP_S
1e77d 70 61 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 panOnly) ){.
1e77e 69 66 28 20 70 2d 3e 73 70 61 6e 2e 64 79 6e 20 if( p->span.dyn
1e77f 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 ) sqlite3DbFree(
1e780 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73 70 db, (char*)p->sp
1e781 61 6e 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 45 an.z);. if( E
1e782 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
1e783 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b , EP_Reduced) ){
1e784 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c . if( p->pL
1e785 65 66 74 20 29 20 73 71 6c 69 74 65 33 45 78 70 eft ) sqlite3Exp
1e786 72 43 6c 65 61 72 28 64 62 2c 20 70 2d 3e 70 4c rClear(db, p->pL
1e787 65 66 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 eft);. if(
1e788 70 2d 3e 70 52 69 67 68 74 20 29 20 73 71 6c 69 p->pRight ) sqli
1e789 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 2c te3ExprClear(db,
1e78a 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 p->pRight);.
1e78b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
1e78c 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1e78d 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 db, p->pLeft);.
1e78e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1e78f 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 Delete(db, p->pR
1e790 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ight);. }.
1e791 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
1e792 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 erty(p, EP_xIsSe
1e793 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73 lect) ){. s
1e794 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
1e795 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c te(db, p->x.pSel
1e796 65 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ect);. }else{
1e797 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
1e798 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c prListDelete(db,
1e799 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 p->x.pList);.
1e79a 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1e79b 20 52 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c Recursively del
1e79c 65 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f ete an expressio
1e79d 6e 20 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 n tree..*/.SQLIT
1e79e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1e79f 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1e7a0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 (sqlite3 *db, Ex
1e7a1 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d pr *p){. if( p=
1e7a2 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 =0 ) return;. s
1e7a3 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 qlite3ExprClear(
1e7a4 64 62 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 db, p);. sqlite
1e7a5 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 3DbFree(db, p);.
1e7a6 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 45 78 70 }../*.** The Exp
1e7a7 72 2e 74 6f 6b 65 6e 20 66 69 65 6c 64 20 6d 69 r.token field mi
1e7a8 67 68 74 20 62 65 20 61 20 73 74 72 69 6e 67 20 ght be a string
1e7a9 6c 69 74 65 72 61 6c 20 74 68 61 74 20 69 73 20 literal that is
1e7aa 71 75 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 73 6f quoted..** If so
1e7ab 2c 20 72 65 6d 6f 76 65 20 74 68 65 20 71 75 6f , remove the quo
1e7ac 74 61 74 69 6f 6e 20 6d 61 72 6b 73 2e 0a 2a 2f tation marks..*/
1e7ad 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1e7ae 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 71 75 void sqlite3Dequ
1e7af 6f 74 65 45 78 70 72 28 45 78 70 72 20 2a 70 29 oteExpr(Expr *p)
1e7b0 7b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 {. if( !ExprHas
1e7b1 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 AnyProperty(p, E
1e7b2 50 5f 44 65 71 75 6f 74 65 64 29 20 29 7b 0a 20 P_Dequoted) ){.
1e7b3 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 ExprSetProper
1e7b4 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f 74 65 ty(p, EP_Dequote
1e7b5 64 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 d);. assert(
1e7b6 28 70 2d 3e 76 76 61 46 6c 61 67 73 20 26 20 45 (p->vvaFlags & E
1e7b7 56 56 41 5f 52 65 61 64 4f 6e 6c 79 54 6f 6b 65 VVA_ReadOnlyToke
1e7b8 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c n)==0 );. sql
1e7b9 69 74 65 33 44 65 71 75 6f 74 65 28 28 63 68 61 ite3Dequote((cha
1e7ba 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a r*)p->token.z);.
1e7bb 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
1e7bc 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1e7bd 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 f bytes allocate
1e7be 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 d for the expres
1e7bf 73 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 0a sion structure .
1e7c0 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ** passed as the
1e7c1 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
1e7c2 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 This is always
1e7c3 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c one of EXPR_FULL
1e7c4 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 45 SIZE,.** EXPR_RE
1e7c5 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50 DUCEDSIZE or EXP
1e7c6 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e R_TOKENONLYSIZE.
1e7c7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 .*/.static int e
1e7c8 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 45 78 xprStructSize(Ex
1e7c9 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 pr *p){. if( Ex
1e7ca 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
1e7cb 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 EP_TokenOnly) )
1e7cc 20 72 65 74 75 72 6e 20 45 58 50 52 5f 54 4f 4b return EXPR_TOK
1e7cd 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 66 ENONLYSIZE;. if
1e7ce 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
1e7cf 79 28 70 2c 20 45 50 5f 53 70 61 6e 4f 6e 6c 79 y(p, EP_SpanOnly
1e7d0 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f ) ) return EXPR_
1e7d1 53 50 41 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 SPANONLYSIZE;.
1e7d2 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
1e7d3 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 rty(p, EP_Reduce
1e7d4 64 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 d) ) return EXPR
1e7d5 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20 20 _REDUCEDSIZE;.
1e7d6 72 65 74 75 72 6e 20 45 58 50 52 5f 46 55 4c 4c return EXPR_FULL
1e7d7 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 SIZE;.}../*.** s
1e7d8 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 20 qlite3ExprDup()
1e7d9 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 has been called
1e7da 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79 to create a copy
1e7db 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70 of expression p
1e7dc 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 45 58 50 with.** the EXP
1e7dd 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 20 70 RDUP_XXX flags p
1e7de 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 assed as the sec
1e7df 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 ond argument. Th
1e7e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 is function .**
1e7e1 72 65 74 75 72 6e 73 20 74 68 65 20 73 70 61 63 returns the spac
1e7e2 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 e required for t
1e7e3 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 he copy of the E
1e7e4 78 70 72 20 73 74 72 75 63 74 75 72 65 20 6f 6e xpr structure on
1e7e5 6c 79 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 ly..** This is a
1e7e6 6c 77 61 79 73 20 6f 6e 65 20 6f 66 20 45 58 50 lways one of EXP
1e7e7 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 45 58 50 52 R_FULLSIZE, EXPR
1e7e8 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20 _REDUCEDSIZE or
1e7e9 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 EXPR_TOKENONLYSI
1e7ea 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ZE..*/.static in
1e7eb 74 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 t dupedExprStruc
1e7ec 74 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 tSize(Expr *p, i
1e7ed 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 nt flags){. int
1e7ee 20 6e 53 69 7a 65 3b 0a 20 20 69 66 28 20 30 3d nSize;. if( 0=
1e7ef 3d 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f =(flags&EXPRDUP_
1e7f0 52 45 44 55 43 45 29 20 29 7b 0a 20 20 20 20 6e REDUCE) ){. n
1e7f1 53 69 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c 4c Size = EXPR_FULL
1e7f2 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 SIZE;. }else if
1e7f3 28 20 70 2d 3e 70 4c 65 66 74 20 7c 7c 20 70 2d ( p->pLeft || p-
1e7f4 3e 70 52 69 67 68 74 20 7c 7c 20 70 2d 3e 70 43 >pRight || p->pC
1e7f5 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 2e 70 4c 69 73 oll || p->x.pLis
1e7f6 74 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d t ){. nSize =
1e7f7 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a EXPR_REDUCEDSIZ
1e7f8 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 E;. }else if( f
1e7f9 6c 61 67 73 26 28 45 58 50 52 44 55 50 5f 53 50 lags&(EXPRDUP_SP
1e7fa 41 4e 7c 45 58 50 52 44 55 50 5f 44 49 53 54 49 AN|EXPRDUP_DISTI
1e7fb 4e 43 54 53 50 41 4e 29 20 29 7b 0a 20 20 20 20 NCTSPAN) ){.
1e7fc 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 53 50 41 nSize = EXPR_SPA
1e7fd 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 7d 65 6c NONLYSIZE;. }el
1e7fe 73 65 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 se{. nSize =
1e7ff 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 EXPR_TOKENONLYSI
1e800 5a 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ZE;. }. return
1e801 20 6e 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a nSize;.}../*.**
1e802 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 sqlite3ExprDup(
1e803 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 ) has been calle
1e804 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f d to create a co
1e805 70 79 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e py of expression
1e806 20 70 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 45 p with.** the E
1e807 58 50 52 44 55 50 5f 58 58 58 20 70 61 73 73 65 XPRDUP_XXX passe
1e808 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
1e809 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 66 argument. This f
1e80a 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a unction returns.
1e80b 2a 2a 20 74 68 65 20 73 70 61 63 65 20 69 6e 20 ** the space in
1e80c 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 bytes required t
1e80d 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 o store the copy
1e80e 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72 of the Expr str
1e80f 75 63 74 75 72 65 0a 2a 2a 20 61 6e 64 20 74 68 ucture.** and th
1e810 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 e copies of the
1e811 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 61 6e 64 Expr.token.z and
1e812 20 45 78 70 72 2e 73 70 61 6e 2e 7a 20 28 69 66 Expr.span.z (if
1e813 20 61 70 70 6c 69 63 61 62 6c 65 29 0a 2a 2a 20 applicable).**
1e814 73 74 72 69 6e 67 20 62 75 66 66 65 72 73 2e 0a string buffers..
1e815 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 */.static int du
1e816 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 pedExprNodeSize(
1e817 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 Expr *p, int fla
1e818 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 gs){. int nByte
1e819 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 72 75 = dupedExprStru
1e81a 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 ctSize(p, flags)
1e81b 20 2b 20 28 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 3f + (p->token.z ?
1e81c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 20 2b 20 31 20 p->token.n + 1
1e81d 3a 20 30 29 3b 0a 20 20 69 66 28 20 28 66 6c 61 : 0);. if( (fla
1e81e 67 73 26 45 58 50 52 44 55 50 5f 44 49 53 54 49 gs&EXPRDUP_DISTI
1e81f 4e 43 54 53 50 41 4e 29 0a 20 20 20 7c 7c 20 28 NCTSPAN). || (
1e820 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 53 50 flags&EXPRDUP_SP
1e821 41 4e 20 26 26 20 28 70 2d 3e 74 6f 6b 65 6e 2e AN && (p->token.
1e822 7a 21 3d 70 2d 3e 73 70 61 6e 2e 7a 20 7c 7c 20 z!=p->span.z ||
1e823 70 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 2d 3e 73 p->token.n!=p->s
1e824 70 61 6e 2e 6e 29 29 20 0a 20 20 29 7b 0a 20 20 pan.n)) . ){.
1e825 20 20 6e 42 79 74 65 20 2b 3d 20 70 2d 3e 73 70 nByte += p->sp
1e826 61 6e 2e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 an.n;. }. retu
1e827 72 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 rn ROUND8(nByte)
1e828 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1e829 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1e82a 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 bytes required t
1e82b 6f 20 63 72 65 61 74 65 20 61 20 64 75 70 6c 69 o create a dupli
1e82c 63 61 74 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 cate of the .**
1e82d 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73 73 65 expression passe
1e82e 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
1e82f 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 rgument. The sec
1e830 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ond argument is
1e831 61 0a 2a 2a 20 6d 61 73 6b 20 63 6f 6e 74 61 69 a.** mask contai
1e832 6e 69 6e 67 20 45 58 50 52 44 55 50 5f 58 58 58 ning EXPRDUP_XXX
1e833 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 flags..**.** Th
1e834 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
1e835 20 69 6e 63 6c 75 64 65 73 20 73 70 61 63 65 20 includes space
1e836 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79 to create a copy
1e837 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72 of the Expr str
1e838 75 63 74 0a 2a 2a 20 69 74 73 65 6c 66 20 61 6e uct.** itself an
1e839 64 20 74 68 65 20 62 75 66 66 65 72 20 72 65 66 d the buffer ref
1e83a 65 72 72 65 64 20 74 6f 20 62 79 20 45 78 70 72 erred to by Expr
1e83b 2e 74 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 20 .token, if any.
1e83c 49 66 20 74 68 65 20 0a 2a 2a 20 45 58 50 52 44 If the .** EXPRD
1e83d 55 50 5f 53 50 41 4e 20 66 6c 61 67 20 69 73 20 UP_SPAN flag is
1e83e 73 65 74 2c 20 74 68 65 6e 20 73 70 61 63 65 20 set, then space
1e83f 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79 to create a copy
1e840 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a of the buffer.*
1e841 2a 20 72 65 66 65 72 65 64 20 74 6f 20 62 79 20 * refered to by
1e842 45 78 70 72 2e 73 70 61 6e 20 69 73 20 61 6c 73 Expr.span is als
1e843 6f 20 69 6e 63 6c 75 64 65 64 2e 0a 2a 2a 0a 2a o included..**.*
1e844 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 * If the EXPRDUP
1e845 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 _REDUCE flag is
1e846 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65 set, then the re
1e847 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75 turn value inclu
1e848 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f des .** space to
1e849 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45 duplicate all E
1e84a 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65 xpr nodes in the
1e84b 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20 tree formed by
1e84c 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 Expr.pLeft .** a
1e84d 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 76 nd Expr.pRight v
1e84e 61 72 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f ariables (but no
1e84f 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74 t for any struct
1e850 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 ures pointed to
1e851 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64 or .** descended
1e852 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78 from the Expr.x
1e853 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 .pList or Expr.x
1e854 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c .pSelect variabl
1e855 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 es)..*/.static i
1e856 6e 74 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 nt dupedExprSize
1e857 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c (Expr *p, int fl
1e858 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ags){. int nByt
1e859 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 e = 0;. if( p )
1e85a 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75 {. nByte = du
1e85b 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 pedExprNodeSize(
1e85c 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 p, flags);. i
1e85d 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50 f( flags&EXPRDUP
1e85e 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20 _REDUCE ){.
1e85f 20 69 6e 74 20 66 20 3d 20 66 6c 61 67 73 26 28 int f = flags&(
1e860 7e 28 45 58 50 52 44 55 50 5f 53 50 41 4e 7c 45 ~(EXPRDUP_SPAN|E
1e861 58 50 52 44 55 50 5f 44 49 53 54 49 4e 43 54 53 XPRDUP_DISTINCTS
1e862 50 41 4e 29 29 3b 0a 20 20 20 20 20 20 6e 42 79 PAN));. nBy
1e863 74 65 20 2b 3d 20 64 75 70 65 64 45 78 70 72 53 te += dupedExprS
1e864 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20 66 29 ize(p->pLeft, f)
1e865 20 2b 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 + dupedExprSize
1e866 28 70 2d 3e 70 52 69 67 68 74 2c 20 66 29 3b 0a (p->pRight, f);.
1e867 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1e868 72 6e 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a rn nByte;.}../*.
1e869 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1e86a 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 is similar to s
1e86b 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 2c qlite3ExprDup(),
1e86c 20 65 78 63 65 70 74 20 74 68 61 74 20 69 66 20 except that if
1e86d 70 7a 42 75 66 66 65 72 20 0a 2a 2a 20 69 73 20 pzBuffer .** is
1e86e 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 not NULL then *p
1e86f 7a 42 75 66 66 65 72 20 69 73 20 61 73 73 75 6d zBuffer is assum
1e870 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 ed to point to a
1e871 20 62 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e buffer large en
1e872 6f 75 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f 72 ough .** to stor
1e873 65 20 74 68 65 20 63 6f 70 79 20 6f 66 20 65 78 e the copy of ex
1e874 70 72 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 20 pression p, the
1e875 63 6f 70 69 65 73 20 6f 66 20 70 2d 3e 74 6f 6b copies of p->tok
1e876 65 6e 20 61 6e 64 20 70 2d 3e 73 70 61 6e 20 0a en and p->span .
1e877 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c ** (if applicabl
1e878 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f 70 69 e), and the copi
1e879 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c 65 es of the p->pLe
1e87a 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 68 74 ft and p->pRight
1e87b 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a expressions,.**
1e87c 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 65 20 if any. Before
1e87d 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42 75 returning, *pzBu
1e87e 66 66 65 72 20 69 73 20 73 65 74 20 74 6f 20 74 ffer is set to t
1e87f 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 he first byte pa
1e880 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72 74 ssed the.** port
1e881 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 66 65 ion of the buffe
1e882 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 79 r copied into by
1e883 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
1e884 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20 2a */.static Expr *
1e885 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 exprDup(sqlite3
1e886 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e *db, Expr *p, in
1e887 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70 7a t flags, u8 **pz
1e888 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 72 20 Buffer){. Expr
1e889 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 *pNew = 0;
1e88a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e88b 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 /* Value to retu
1e88c 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b rn */. if( p ){
1e88d 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 . const int i
1e88e 73 52 65 71 75 69 72 65 44 69 73 74 69 6e 63 74 sRequireDistinct
1e88f 53 70 61 6e 20 3d 20 28 66 6c 61 67 73 26 45 58 Span = (flags&EX
1e890 50 52 44 55 50 5f 44 49 53 54 49 4e 43 54 53 50 PRDUP_DISTINCTSP
1e891 41 4e 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 AN);. const i
1e892 6e 74 20 69 73 52 65 71 75 69 72 65 53 70 61 6e nt isRequireSpan
1e893 20 3d 20 28 66 6c 61 67 73 26 28 45 58 50 52 44 = (flags&(EXPRD
1e894 55 50 5f 53 50 41 4e 7c 45 58 50 52 44 55 50 5f UP_SPAN|EXPRDUP_
1e895 44 49 53 54 49 4e 43 54 53 50 41 4e 29 29 3b 0a DISTINCTSPAN));.
1e896 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 const int is
1e897 52 65 64 75 63 65 64 20 3d 20 28 66 6c 61 67 73 Reduced = (flags
1e898 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 &EXPRDUP_REDUCE)
1e899 3b 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f 63 ;. u8 *zAlloc
1e89a 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ;.. assert( p
1e89b 7a 42 75 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73 zBuffer==0 || is
1e89c 52 65 64 75 63 65 64 20 29 3b 0a 0a 20 20 20 20 Reduced );..
1e89d 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 /* Figure out wh
1e89e 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 ere to write the
1e89f 20 6e 65 77 20 45 78 70 72 20 73 74 72 75 63 74 new Expr struct
1e8a0 75 72 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ure. */. if(
1e8a1 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20 pzBuffer ){.
1e8a2 20 20 7a 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 zAlloc = *pzBu
1e8a3 66 66 65 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ffer;. }else{
1e8a4 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 . zAlloc =
1e8a5 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
1e8a6 61 77 28 64 62 2c 20 64 75 70 65 64 45 78 70 72 aw(db, dupedExpr
1e8a7 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 29 3b Size(p, flags));
1e8a8 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20 . }. pNew
1e8a9 3d 20 28 45 78 70 72 20 2a 29 7a 41 6c 6c 6f 63 = (Expr *)zAlloc
1e8aa 3b 0a 0a 20 20 20 20 69 66 28 20 70 4e 65 77 20 ;.. if( pNew
1e8ab 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 ){. /* Set
1e8ac 6e 4e 65 77 53 69 7a 65 20 74 6f 20 74 68 65 20 nNewSize to the
1e8ad 73 69 7a 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 size allocated f
1e8ae 6f 72 20 74 68 65 20 73 74 72 75 63 74 75 72 65 or the structure
1e8af 20 70 6f 69 6e 74 65 64 20 74 6f 0a 20 20 20 20 pointed to.
1e8b0 20 20 2a 2a 20 62 79 20 70 4e 65 77 2e 20 54 68 ** by pNew. Th
1e8b1 69 73 20 69 73 20 65 69 74 68 65 72 20 45 58 50 is is either EXP
1e8b2 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 45 58 50 52 R_FULLSIZE, EXPR
1e8b3 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 0a _REDUCEDSIZE or.
1e8b4 20 20 20 20 20 20 2a 2a 20 45 58 50 52 5f 54 4f ** EXPR_TO
1e8b5 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 20 6e 54 6f KENONLYSIZE. nTo
1e8b6 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 74 68 ken is set to th
1e8b7 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1e8b8 73 20 63 6f 6e 73 75 6d 65 64 0a 20 20 20 20 20 s consumed.
1e8b9 20 2a 2a 20 62 79 20 74 68 65 20 63 6f 70 79 20 ** by the copy
1e8ba 6f 66 20 74 68 65 20 70 2d 3e 74 6f 6b 65 6e 2e of the p->token.
1e8bb 7a 20 73 74 72 69 6e 67 20 28 69 66 20 61 6e 79 z string (if any
1e8bc 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ).. */.
1e8bd 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 const int nNew
1e8be 53 69 7a 65 20 3d 20 64 75 70 65 64 45 78 70 72 Size = dupedExpr
1e8bf 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c StructSize(p, fl
1e8c0 61 67 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 ags);. cons
1e8c1 74 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 28 t int nToken = (
1e8c2 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 3f 20 70 2d 3e p->token.z ? p->
1e8c3 74 6f 6b 65 6e 2e 6e 20 2b 20 31 20 3a 20 30 29 token.n + 1 : 0)
1e8c4 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 52 65 ;. if( isRe
1e8c5 64 75 63 65 64 20 29 7b 0a 20 20 20 20 20 20 20 duced ){.
1e8c6 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 assert( ExprHas
1e8c7 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 Property(p, EP_R
1e8c8 65 64 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20 educed)==0 );.
1e8c9 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c memcpy(zAl
1e8ca 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65 loc, p, nNewSize
1e8cb 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1e8cc 20 20 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a int nSiz
1e8cd 65 20 3d 20 65 78 70 72 53 74 72 75 63 74 53 69 e = exprStructSi
1e8ce 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 6d ze(p);. m
1e8cf 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c emcpy(zAlloc, p,
1e8d0 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 nSize);.
1e8d1 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b memset(&zAlloc[
1e8d2 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f nSize], 0, EXPR_
1e8d3 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b FULLSIZE-nSize);
1e8d4 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
1e8d5 2f 2a 20 53 65 74 20 74 68 65 20 45 50 5f 52 65 /* Set the EP_Re
1e8d6 64 75 63 65 64 20 61 6e 64 20 45 50 5f 54 6f 6b duced and EP_Tok
1e8d7 65 6e 4f 6e 6c 79 20 66 6c 61 67 73 20 61 70 70 enOnly flags app
1e8d8 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 ropriately. */.
1e8d9 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 pNew->flags
1e8da 20 26 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64 &= ~(EP_Reduced
1e8db 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 |EP_TokenOnly|EP
1e8dc 5f 53 70 61 6e 4f 6e 6c 79 29 3b 0a 20 20 20 20 _SpanOnly);.
1e8dd 20 20 73 77 69 74 63 68 28 20 6e 4e 65 77 53 69 switch( nNewSi
1e8de 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 ze ){. ca
1e8df 73 65 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 se EXPR_REDUCEDS
1e8e0 49 5a 45 3a 20 20 20 70 4e 65 77 2d 3e 66 6c 61 IZE: pNew->fla
1e8e1 67 73 20 7c 3d 20 45 50 5f 52 65 64 75 63 65 64 gs |= EP_Reduced
1e8e2 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
1e8e3 20 63 61 73 65 20 45 58 50 52 5f 54 4f 4b 45 4e case EXPR_TOKEN
1e8e4 4f 4e 4c 59 53 49 5a 45 3a 20 70 4e 65 77 2d 3e ONLYSIZE: pNew->
1e8e5 66 6c 61 67 73 20 7c 3d 20 45 50 5f 54 6f 6b 65 flags |= EP_Toke
1e8e6 6e 4f 6e 6c 79 3b 20 62 72 65 61 6b 3b 0a 20 20 nOnly; break;.
1e8e7 20 20 20 20 20 20 63 61 73 65 20 45 58 50 52 5f case EXPR_
1e8e8 53 50 41 4e 4f 4e 4c 59 53 49 5a 45 3a 20 20 70 SPANONLYSIZE: p
1e8e9 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 New->flags |= EP
1e8ea 5f 53 70 61 6e 4f 6e 6c 79 3b 20 62 72 65 61 6b _SpanOnly; break
1e8eb 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
1e8ec 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e /* Copy the p->
1e8ed 74 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 66 token string, if
1e8ee 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 any. */. i
1e8ef 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 f( nToken ){.
1e8f0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 unsigned ch
1e8f1 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 26 7a 41 ar *zToken = &zA
1e8f2 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a lloc[nNewSize];.
1e8f3 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a memcpy(z
1e8f4 54 6f 6b 65 6e 2c 20 70 2d 3e 74 6f 6b 65 6e 2e Token, p->token.
1e8f5 7a 2c 20 6e 54 6f 6b 65 6e 2d 31 29 3b 0a 20 20 z, nToken-1);.
1e8f6 20 20 20 20 20 20 7a 54 6f 6b 65 6e 5b 6e 54 6f zToken[nTo
1e8f7 6b 65 6e 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 ken-1] = '\0';.
1e8f8 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b pNew->tok
1e8f9 65 6e 2e 64 79 6e 20 3d 20 30 3b 0a 20 20 20 20 en.dyn = 0;.
1e8fa 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e pNew->token.
1e8fb 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 z = zToken;.
1e8fc 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30 }.. if( 0
1e8fd 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 ==((p->flags|pNe
1e8fe 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f 54 w->flags) & EP_T
1e8ff 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 okenOnly) ){.
1e900 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 /* Fill in
1e901 74 68 65 20 70 4e 65 77 2d 3e 73 70 61 6e 20 74 the pNew->span t
1e902 6f 6b 65 6e 2c 20 69 66 20 72 65 71 75 69 72 65 oken, if require
1e903 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 d. */. if
1e904 28 20 69 73 52 65 71 75 69 72 65 53 70 61 6e 20 ( isRequireSpan
1e905 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
1e906 20 69 73 52 65 71 75 69 72 65 44 69 73 74 69 6e isRequireDistin
1e907 63 74 53 70 61 6e 20 0a 20 20 20 20 20 20 20 20 ctSpan .
1e908 20 20 20 7c 7c 20 70 2d 3e 74 6f 6b 65 6e 2e 7a || p->token.z
1e909 21 3d 70 2d 3e 73 70 61 6e 2e 7a 20 7c 7c 20 70 !=p->span.z || p
1e90a 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 2d 3e 73 70 ->token.n!=p->sp
1e90b 61 6e 2e 6e 0a 20 20 20 20 20 20 20 20 20 20 29 an.n. )
1e90c 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4e {. pN
1e90d 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 26 7a 41 ew->span.z = &zA
1e90e 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 2b 6e 54 lloc[nNewSize+nT
1e90f 6f 6b 65 6e 5d 3b 0a 20 20 20 20 20 20 20 20 20 oken];.
1e910 20 20 20 6d 65 6d 63 70 79 28 28 63 68 61 72 20 memcpy((char
1e911 2a 29 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 2c 20 *)pNew->span.z,
1e912 70 2d 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70 p->span.z, p->sp
1e913 61 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 an.n);.
1e914 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 64 79 pNew->span.dy
1e915 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 n = 0;.
1e916 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1e917 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a pNew->span.z
1e918 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a = pNew->token.z
1e919 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4e ;. pN
1e91a 65 77 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 4e 65 ew->span.n = pNe
1e91b 77 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 w->token.n;.
1e91c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e91d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1e91e 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 pNew->span.z =
1e91f 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 0;. pNe
1e920 77 2d 3e 73 70 61 6e 2e 6e 20 3d 20 30 3b 0a 20 w->span.n = 0;.
1e921 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1e922 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 .. if( 0==(
1e923 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d 3e (p->flags|pNew->
1e924 66 6c 61 67 73 29 20 26 20 28 45 50 5f 54 6f 6b flags) & (EP_Tok
1e925 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 4f 6e enOnly|EP_SpanOn
1e926 6c 79 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ly)) ){.
1e927 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 /* Fill in the p
1e928 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 20 6f New->x.pSelect o
1e929 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 r pNew->x.pList
1e92a 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 member. */.
1e92b 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
1e92c 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 operty(p, EP_xIs
1e92d 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 Select) ){.
1e92e 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 53 65 pNew->x.pSe
1e92f 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 lect = sqlite3Se
1e930 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 lectDup(db, p->x
1e931 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 65 64 75 .pSelect, isRedu
1e932 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 ced);. }e
1e933 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 lse{. p
1e934 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 73 New->x.pList = s
1e935 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 qlite3ExprListDu
1e936 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 p(db, p->x.pList
1e937 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20 , isReduced);.
1e938 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1e939 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 . /* Fill i
1e93a 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 61 6e n pNew->pLeft an
1e93b 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 2e 20 d pNew->pRight.
1e93c 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 */. if( Exp
1e93d 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
1e93e 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 64 pNew, EP_Reduced
1e93f 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 |EP_TokenOnly|EP
1e940 5f 53 70 61 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 _SpanOnly) ){.
1e941 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 2b 3d 20 zAlloc +=
1e942 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a dupedExprNodeSiz
1e943 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 e(p, flags);.
1e944 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 if( ExprHas
1e945 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 Property(pNew, E
1e946 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0a 20 20 P_Reduced) ){.
1e947 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c pNew->pL
1e948 65 66 74 20 3d 20 65 78 70 72 44 75 70 28 64 62 eft = exprDup(db
1e949 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45 58 50 52 , p->pLeft, EXPR
1e94a 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c DUP_REDUCE, &zAl
1e94b 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 loc);.
1e94c 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 65 pNew->pRight = e
1e94d 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52 xprDup(db, p->pR
1e94e 69 67 68 74 2c 20 45 58 50 52 44 55 50 5f 52 45 ight, EXPRDUP_RE
1e94f 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a DUCE, &zAlloc);.
1e950 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e951 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20 29 if( pzBuffer )
1e952 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a 42 {. *pzB
1e953 75 66 66 65 72 20 3d 20 7a 41 6c 6c 6f 63 3b 0a uffer = zAlloc;.
1e954 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e955 7d 65 6c 73 65 20 69 66 28 20 21 45 78 70 72 48 }else if( !ExprH
1e956 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c asAnyProperty(p,
1e957 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 EP_TokenOnly|EP
1e958 5f 53 70 61 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 _SpanOnly) ){.
1e959 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 pNew->pLef
1e95a 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 t = sqlite3ExprD
1e95b 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c up(db, p->pLeft,
1e95c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 0);. pNe
1e95d 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c 69 w->pRight = sqli
1e95e 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
1e95f 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 ->pRight, 0);.
1e960 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1e961 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
1e962 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
1e963 6f 77 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 72 owing group of r
1e964 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 65 outines make dee
1e965 70 20 63 6f 70 69 65 73 20 6f 66 20 65 78 70 72 p copies of expr
1e966 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 essions,.** expr
1e967 65 73 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 ession lists, ID
1e968 20 6c 69 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 lists, and sele
1e969 63 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 ct statements.
1e96a 54 68 65 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a The copies can.*
1e96b 2a 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 79 * be deleted (by
1e96c 20 62 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f being passed to
1e96d 20 74 68 65 69 72 20 72 65 73 70 65 63 74 69 76 their respectiv
1e96e 65 20 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f e ...Delete() ro
1e96f 75 74 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f utines).** witho
1e970 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 65 ut effecting the
1e971 20 6f 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a originals..**.*
1e972 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
1e973 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 73 list, ID, and s
1e974 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74 75 ource lists retu
1e975 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78 70 rn by sqlite3Exp
1e976 72 4c 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73 rListDup(),.** s
1e977 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 qlite3IdListDup(
1e978 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 53 72 ), and sqlite3Sr
1e979 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e 20 6e cListDup() can n
1e97a 6f 74 20 62 65 20 66 75 72 74 68 65 72 20 65 78 ot be further ex
1e97b 70 61 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 75 panded .** by su
1e97c 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
1e97d 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 70 o sqlite*ListApp
1e97e 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a end() routines..
1e97f 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73 **.** Any tables
1e980 20 74 68 61 74 20 74 68 65 20 53 72 63 4c 69 73 that the SrcLis
1e981 74 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f t might point to
1e982 20 61 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 61 are not duplica
1e983 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ted..**.** The f
1e984 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 lags parameter c
1e985 6f 6e 74 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e ontains a combin
1e986 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 45 58 50 ation of the EXP
1e987 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 20 RDUP_XXX flags.
1e988 49 66 0a 2a 2a 20 74 68 65 20 45 58 50 52 44 55 If.** the EXPRDU
1e989 50 5f 53 50 41 4e 20 66 6c 61 67 20 69 73 20 73 P_SPAN flag is s
1e98a 65 74 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 et in the argume
1e98b 6e 74 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 nt parameter, th
1e98c 65 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e en the .** Expr.
1e98d 73 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74 68 span field of th
1e98e 65 20 69 6e 70 75 74 20 65 78 70 72 65 73 73 69 e input expressi
1e98f 6f 6e 20 69 73 20 63 6f 70 69 65 64 2e 20 49 66 on is copied. If
1e990 20 45 58 50 52 44 55 50 5f 53 50 41 4e 20 69 73 EXPRDUP_SPAN is
1e991 0a 2a 2a 20 63 6c 65 61 72 2c 20 74 68 65 6e 20 .** clear, then
1e992 74 68 65 20 45 78 70 72 2e 73 70 61 6e 20 66 69 the Expr.span fi
1e993 65 6c 64 20 6f 66 20 74 68 65 20 72 65 74 75 72 eld of the retur
1e994 6e 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 73 ned expression s
1e995 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 7a tructure.** is z
1e996 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 eroed..**.** If
1e997 74 68 65 20 45 58 50 52 44 55 50 5f 52 45 44 55 the EXPRDUP_REDU
1e998 43 45 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 CE flag is set,
1e999 74 68 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 then the structu
1e99a 72 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 re returned is a
1e99b 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 76 65 .** truncated ve
1e99c 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 75 73 75 rsion of the usu
1e99d 61 6c 20 45 78 70 72 20 73 74 72 75 63 74 75 72 al Expr structur
1e99e 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 e that will be s
1e99f 74 6f 72 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 tored as.** part
1e9a0 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 of the in-memor
1e9a1 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e y representation
1e9a2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1e9a3 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49 schema..*/.SQLI
1e9a4 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
1e9a5 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 *sqlite3ExprDup(
1e9a6 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
1e9a7 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 r *p, int flags)
1e9a8 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 44 {. return exprD
1e9a9 75 70 28 64 62 2c 20 70 2c 20 66 6c 61 67 73 2c up(db, p, flags,
1e9aa 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 0);.}.SQLITE_PR
1e9ab 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1e9ac 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69 e3TokenCopy(sqli
1e9ad 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a te3 *db, Token *
1e9ae 70 54 6f 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e pTo, const Token
1e9af 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66 28 20 *pFrom){. if(
1e9b0 70 54 6f 2d 3e 64 79 6e 20 29 20 73 71 6c 69 74 pTo->dyn ) sqlit
1e9b1 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 e3DbFree(db, (ch
1e9b2 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b 0a 20 20 69 ar*)pTo->z);. i
1e9b3 66 28 20 70 46 72 6f 6d 2d 3e 7a 20 29 7b 0a 20 f( pFrom->z ){.
1e9b4 20 20 20 70 54 6f 2d 3e 6e 20 3d 20 70 46 72 6f pTo->n = pFro
1e9b5 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54 6f 2d 3e 7a m->n;. pTo->z
1e9b6 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 44 = (u8*)sqlite3D
1e9b7 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 bStrNDup(db, (ch
1e9b8 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a 2c 20 70 46 ar*)pFrom->z, pF
1e9b9 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 6f rom->n);. pTo
1e9ba 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c ->dyn = 1;. }el
1e9bb 73 65 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d se{. pTo->z =
1e9bc 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 0;. }.}.SQLITE
1e9bd 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 _PRIVATE ExprLis
1e9be 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 t *sqlite3ExprLi
1e9bf 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 stDup(sqlite3 *d
1e9c0 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 b, ExprList *p,
1e9c1 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 45 78 int flags){. Ex
1e9c2 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 prList *pNew;.
1e9c3 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1e9c4 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 4f item *pItem, *pO
1e9c5 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b ldItem;. int i;
1e9c6 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
1e9c7 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d turn 0;. pNew =
1e9c8 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1e9c9 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a Raw(db, sizeof(*
1e9ca 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70 pNew) );. if( p
1e9cb 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 New==0 ) return
1e9cc 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 43 75 72 0;. pNew->iECur
1e9cd 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d sor = 0;. pNew-
1e9ce 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77 2d 3e 6e >nExpr = pNew->n
1e9cf 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 72 Alloc = p->nExpr
1e9d0 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 70 49 ;. pNew->a = pI
1e9d1 74 65 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d tem = sqlite3DbM
1e9d2 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 20 70 2d allocRaw(db, p-
1e9d3 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d >nExpr*sizeof(p-
1e9d4 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 >a[0]) );. if(
1e9d5 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 pItem==0 ){.
1e9d6 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1e9d7 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 , pNew);. ret
1e9d8 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f urn 0;. } . pO
1e9d9 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 ldItem = p->a;.
1e9da 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
1e9db 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
1e9dc 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b ++, pOldItem++){
1e9dd 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45 . Expr *pNewE
1e9de 78 70 72 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 xpr;. Expr *p
1e9df 4f 6c 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 OldExpr = pOldIt
1e9e0 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 70 em->pExpr;. p
1e9e1 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 4e Item->pExpr = pN
1e9e2 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 ewExpr = sqlite3
1e9e3 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 ExprDup(db, pOld
1e9e4 45 78 70 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 Expr, flags);.
1e9e5 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d pItem->zName =
1e9e6 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
1e9e7 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a (db, pOldItem->z
1e9e8 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 6d Name);. pItem
1e9e9 2d 3e 73 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f ->sortOrder = pO
1e9ea 6c 64 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 ldItem->sortOrde
1e9eb 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f r;. pItem->do
1e9ec 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 49 74 65 ne = 0;. pIte
1e9ed 6d 2d 3e 69 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 m->iCol = pOldIt
1e9ee 65 6d 2d 3e 69 43 6f 6c 3b 0a 20 20 20 20 70 49 em->iCol;. pI
1e9ef 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 70 4f tem->iAlias = pO
1e9f0 6c 64 49 74 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a ldItem->iAlias;.
1e9f1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
1e9f2 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 w;.}../*.** If c
1e9f3 75 72 73 6f 72 73 2c 20 74 72 69 67 67 65 72 73 ursors, triggers
1e9f4 2c 20 76 69 65 77 73 20 61 6e 64 20 73 75 62 71 , views and subq
1e9f5 75 65 72 69 65 73 20 61 72 65 20 61 6c 6c 20 6f ueries are all o
1e9f6 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 mitted from.** t
1e9f7 68 65 20 62 75 69 6c 64 2c 20 74 68 65 6e 20 6e he build, then n
1e9f8 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
1e9f9 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 2c 20 65 wing routines, e
1e9fa 78 63 65 70 74 20 66 6f 72 20 0a 2a 2a 20 73 71 xcept for .** sq
1e9fb 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 lite3SelectDup()
1e9fc 2c 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e , can be called.
1e9fd 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 sqlite3SelectDu
1e9fe 70 28 29 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 p() is sometimes
1e9ff 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 20 .** called with
1ea00 61 20 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e a NULL argument.
1ea01 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
1ea02 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 (SQLITE_OMIT_VIE
1ea03 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 W) || !defined(S
1ea04 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
1ea05 45 52 29 20 5c 0a 20 7c 7c 20 21 64 65 66 69 6e ER) \. || !defin
1ea06 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
1ea07 55 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f UBQUERY).SQLITE_
1ea08 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 PRIVATE SrcList
1ea09 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 *sqlite3SrcListD
1ea0a 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
1ea0b 53 72 63 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 SrcList *p, int
1ea0c 66 6c 61 67 73 29 7b 0a 20 20 53 72 63 4c 69 73 flags){. SrcLis
1ea0d 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 t *pNew;. int i
1ea0e 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 ;. int nByte;.
1ea0f 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
1ea10 72 6e 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 rn 0;. nByte =
1ea11 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 28 70 2d sizeof(*p) + (p-
1ea12 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a 65 6f 66 >nSrc>0 ? sizeof
1ea13 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28 70 2d 3e (p->a[0]) * (p->
1ea14 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b 0a 20 20 nSrc-1) : 0);.
1ea15 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
1ea16 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 MallocRaw(db, nB
1ea17 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 yte );. if( pNe
1ea18 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b w==0 ) return 0;
1ea19 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 . pNew->nSrc =
1ea1a 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 pNew->nAlloc = p
1ea1b 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d ->nSrc;. for(i=
1ea1c 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 0; i<p->nSrc; i+
1ea1d 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 +){. struct S
1ea1e 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 rcList_item *pNe
1ea1f 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 wItem = &pNew->a
1ea20 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 [i];. struct
1ea21 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f SrcList_item *pO
1ea22 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 ldItem = &p->a[i
1ea23 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 ];. Table *pT
1ea24 61 62 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d ab;. pNewItem
1ea25 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 ->zDatabase = sq
1ea26 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
1ea27 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 , pOldItem->zDat
1ea28 61 62 61 73 65 29 3b 0a 20 20 20 20 70 4e 65 77 abase);. pNew
1ea29 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 Item->zName = sq
1ea2a 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
1ea2b 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d , pOldItem->zNam
1ea2c 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d e);. pNewItem
1ea2d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 ->zAlias = sqlit
1ea2e 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 e3DbStrDup(db, p
1ea2f 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 OldItem->zAlias)
1ea30 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
1ea31 6a 6f 69 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49 jointype = pOldI
1ea32 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 tem->jointype;.
1ea33 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75 pNewItem->iCu
1ea34 72 73 6f 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d rsor = pOldItem-
1ea35 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 4e >iCursor;. pN
1ea36 65 77 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 ewItem->isPopula
1ea37 74 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e ted = pOldItem->
1ea38 69 73 50 6f 70 75 6c 61 74 65 64 3b 0a 20 20 20 isPopulated;.
1ea39 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 pNewItem->zInde
1ea3a 78 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 x = sqlite3DbStr
1ea3b 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d Dup(db, pOldItem
1ea3c 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 70 ->zIndex);. p
1ea3d 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 NewItem->notInde
1ea3e 78 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e xed = pOldItem->
1ea3f 6e 6f 74 49 6e 64 65 78 65 64 3b 0a 20 20 20 20 notIndexed;.
1ea40 70 4e 65 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78 pNewItem->pIndex
1ea41 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e = pOldItem->pIn
1ea42 64 65 78 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 dex;. pTab =
1ea43 70 4e 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d pNewItem->pTab =
1ea44 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b pOldItem->pTab;
1ea45 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b . if( pTab ){
1ea46 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 . pTab->nRe
1ea47 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 f++;. }. p
1ea48 4e 65 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 NewItem->pSelect
1ea49 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 = sqlite3Select
1ea4a 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d Dup(db, pOldItem
1ea4b 2d 3e 70 53 65 6c 65 63 74 2c 20 66 6c 61 67 73 ->pSelect, flags
1ea4c 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d );. pNewItem-
1ea4d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 >pOn = sqlite3Ex
1ea4e 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 prDup(db, pOldIt
1ea4f 65 6d 2d 3e 70 4f 6e 2c 20 66 6c 61 67 73 29 3b em->pOn, flags);
1ea50 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 . pNewItem->p
1ea51 55 73 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 Using = sqlite3I
1ea52 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c dListDup(db, pOl
1ea53 64 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a dItem->pUsing);.
1ea54 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f pNewItem->co
1ea55 6c 55 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d lUsed = pOldItem
1ea56 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 ->colUsed;. }.
1ea57 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
1ea58 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 SQLITE_PRIVATE I
1ea59 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 dList *sqlite3Id
1ea5a 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 ListDup(sqlite3
1ea5b 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b *db, IdList *p){
1ea5c 0a 20 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b . IdList *pNew;
1ea5d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
1ea5e 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b p==0 ) return 0;
1ea5f 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
1ea60 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
1ea61 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 sizeof(*pNew) )
1ea62 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
1ea63 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
1ea64 65 77 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e ew->nId = pNew->
1ea65 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b nAlloc = p->nId;
1ea66 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c . pNew->a = sql
1ea67 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
1ea68 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f db, p->nId*sizeo
1ea69 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 f(p->a[0]) );.
1ea6a 69 66 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 if( pNew->a==0 )
1ea6b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
1ea6c 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 ree(db, pNew);.
1ea6d 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1ea6e 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d . for(i=0; i<p-
1ea6f 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nId; i++){.
1ea70 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 struct IdList_it
1ea71 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 em *pNewItem = &
1ea72 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 pNew->a[i];.
1ea73 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 struct IdList_it
1ea74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 em *pOldItem = &
1ea75 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 p->a[i];. pNe
1ea76 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 wItem->zName = s
1ea77 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
1ea78 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 b, pOldItem->zNa
1ea79 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 me);. pNewIte
1ea7a 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 m->idx = pOldIte
1ea7b 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 m->idx;. }. re
1ea7c 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c turn pNew;.}.SQL
1ea7d 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 ITE_PRIVATE Sele
1ea7e 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 ct *sqlite3Selec
1ea7f 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 tDup(sqlite3 *db
1ea80 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 , Select *p, int
1ea81 20 66 6c 61 67 73 29 7b 0a 20 20 53 65 6c 65 63 flags){. Selec
1ea82 74 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 t *pNew;. if( p
1ea83 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1ea84 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
1ea85 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
1ea86 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 sizeof(*p) );.
1ea87 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 if( pNew==0 ) re
1ea88 74 75 72 6e 20 30 3b 0a 20 20 2f 2a 20 41 6c 77 turn 0;. /* Alw
1ea89 61 79 73 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 ays make a copy
1ea8a 6f 66 20 74 68 65 20 73 70 61 6e 20 66 6f 72 20 of the span for
1ea8b 74 6f 70 2d 6c 65 76 65 6c 20 65 78 70 72 65 73 top-level expres
1ea8c 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20 2a sions in the. *
1ea8d 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 * expression lis
1ea8e 74 2e 20 20 54 68 65 20 6c 6f 67 69 63 20 69 6e t. The logic in
1ea8f 20 53 45 4c 45 43 54 20 70 72 6f 63 65 73 73 69 SELECT processi
1ea90 6e 67 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e ng that determin
1ea91 65 73 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 es. ** the name
1ea92 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 s of columns in
1ea93 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6e the result set n
1ea94 65 65 64 73 20 74 68 69 73 20 69 6e 66 6f 72 6d eeds this inform
1ea95 61 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e 65 77 2d ation */. pNew-
1ea96 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 >pEList = sqlite
1ea97 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 3ExprListDup(db,
1ea98 20 70 2d 3e 70 45 4c 69 73 74 2c 20 66 6c 61 67 p->pEList, flag
1ea99 73 7c 45 58 50 52 44 55 50 5f 53 50 41 4e 29 3b s|EXPRDUP_SPAN);
1ea9a 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 . pNew->pSrc =
1ea9b 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 sqlite3SrcListDu
1ea9c 70 28 64 62 2c 20 70 2d 3e 70 53 72 63 2c 20 66 p(db, p->pSrc, f
1ea9d 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 lags);. pNew->p
1ea9e 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 Where = sqlite3E
1ea9f 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 xprDup(db, p->pW
1eaa0 68 65 72 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 here, flags);.
1eaa1 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d pNew->pGroupBy =
1eaa2 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
1eaa3 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 Dup(db, p->pGrou
1eaa4 70 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 pBy, flags);. p
1eaa5 4e 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 New->pHaving = s
1eaa6 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
1eaa7 2c 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c , p->pHaving, fl
1eaa8 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f ags);. pNew->pO
1eaa9 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 rderBy = sqlite3
1eaaa 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 ExprListDup(db,
1eaab 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 66 6c 61 p->pOrderBy, fla
1eaac 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 gs);. pNew->op
1eaad 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d = p->op;. pNew-
1eaae 3e 70 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 >pPrior = sqlite
1eaaf 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 3SelectDup(db, p
1eab0 2d 3e 70 50 72 69 6f 72 2c 20 66 6c 61 67 73 29 ->pPrior, flags)
1eab1 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 ;. pNew->pLimit
1eab2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
1eab3 70 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c p(db, p->pLimit,
1eab4 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d flags);. pNew-
1eab5 3e 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74 >pOffset = sqlit
1eab6 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d e3ExprDup(db, p-
1eab7 3e 70 4f 66 66 73 65 74 2c 20 66 6c 61 67 73 29 >pOffset, flags)
1eab8 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 ;. pNew->iLimit
1eab9 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f = 0;. pNew->iO
1eaba 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 4e 65 ffset = 0;. pNe
1eabb 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 70 2d w->selFlags = p-
1eabc 3e 73 65 6c 46 6c 61 67 73 20 26 20 7e 53 46 5f >selFlags & ~SF_
1eabd 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 UsesEphemeral;.
1eabe 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 pNew->pRightmos
1eabf 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 t = 0;. pNew->a
1eac0 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d ddrOpenEphm[0] =
1eac1 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 -1;. pNew->add
1eac2 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d rOpenEphm[1] = -
1eac3 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 1;. pNew->addrO
1eac4 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b penEphm[2] = -1;
1eac5 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
1eac6 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 }.#else.SQLITE_P
1eac7 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 RIVATE Select *s
1eac8 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 qlite3SelectDup(
1eac9 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c sqlite3 *db, Sel
1eaca 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 ect *p, int flag
1eacb 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d s){. assert( p=
1eacc 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 =0 );. return 0
1eacd 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a ;.}.#endif.../*.
1eace 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 ** Add a new ele
1eacf 6d 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64 20 ment to the end
1ead0 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e of an expression
1ead1 20 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73 74 list. If pList
1ead2 20 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 is.** initially
1ead3 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 NULL, then crea
1ead4 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 te a new express
1ead5 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c ion list..*/.SQL
1ead6 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
1ead7 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 List *sqlite3Exp
1ead8 72 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20 50 rListAppend(. P
1ead9 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
1eada 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
1eadb 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 g context */. E
1eadc 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 xprList *pList,
1eadd 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 /* List t
1eade 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 65 6e o which to appen
1eadf 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c d. Might be NULL
1eae0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 */. Expr *pExp
1eae1 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r, /*
1eae2 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 Expression to b
1eae3 65 20 61 70 70 65 6e 64 65 64 20 2a 2f 0a 20 20 e appended */.
1eae4 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 Token *pName
1eae5 20 20 20 20 20 20 20 20 2f 2a 20 41 53 20 6b 65 /* AS ke
1eae6 79 77 6f 72 64 20 66 6f 72 20 74 68 65 20 65 78 yword for the ex
1eae7 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 pression */.){.
1eae8 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1eae9 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 Parse->db;. if(
1eaea 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 pList==0 ){.
1eaeb 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 pList = sqlite3
1eaec 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
1eaed 20 73 69 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 sizeof(ExprList
1eaee 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 ) );. if( pLi
1eaef 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 st==0 ){. g
1eaf0 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 oto no_mem;.
1eaf1 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c }. assert( pL
1eaf2 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 ist->nAlloc==0 )
1eaf3 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 ;. }. if( pLis
1eaf4 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 t->nAlloc<=pList
1eaf5 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 ->nExpr ){. s
1eaf6 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
1eaf7 74 65 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 tem *a;. int
1eaf8 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f n = pList->nAllo
1eaf9 63 2a 32 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d c*2 + 4;. a =
1eafa 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
1eafb 63 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 c(db, pList->a,
1eafc 6e 2a 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e n*sizeof(pList->
1eafd 61 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 a[0]));. if(
1eafe 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f a==0 ){. go
1eaff 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d to no_mem;. }
1eb00 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 . pList->a =
1eb01 61 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 a;. pList->nA
1eb02 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 lloc = sqlite3Db
1eb03 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 MallocSize(db, a
1eb04 29 2f 73 69 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a )/sizeof(a[0]);.
1eb05 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4c }. assert( pL
1eb06 69 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 69 ist->a!=0 );. i
1eb07 66 28 20 70 45 78 70 72 20 7c 7c 20 70 4e 61 6d f( pExpr || pNam
1eb08 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 e ){. struct
1eb09 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
1eb0a 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 Item = &pList->a
1eb0b 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d [pList->nExpr++]
1eb0c 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 74 ;. memset(pIt
1eb0d 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 em, 0, sizeof(*p
1eb0e 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 65 Item));. pIte
1eb0f 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 m->zName = sqlit
1eb10 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
1eb11 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 db, pName);.
1eb12 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 pItem->pExpr = p
1eb13 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d Expr;. pItem-
1eb14 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 7d >iAlias = 0;. }
1eb15 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b . return pList;
1eb16 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 ..no_mem: .
1eb17 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b 69 6e /* Avoid leakin
1eb18 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61 6c 6c g memory if mall
1eb19 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2e 20 2a oc has failed. *
1eb1a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 /. sqlite3ExprD
1eb1b 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 29 elete(db, pExpr)
1eb1c 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c ;. sqlite3ExprL
1eb1d 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c istDelete(db, pL
1eb1e 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 ist);. return 0
1eb1f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 ;.}../*.** If th
1eb20 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 e expression lis
1eb21 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69 6e t pEList contain
1eb22 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69 6d s more than iLim
1eb23 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20 it elements,.**
1eb24 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d leave an error m
1eb25 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 essage in pParse
1eb26 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1eb27 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1eb28 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e ExprListCheckLen
1eb29 67 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 gth(. Parse *pP
1eb2a 61 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73 74 arse,. ExprList
1eb2b 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e 73 *pEList,. cons
1eb2c 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74 0a t char *zObject.
1eb2d 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70 50 ){. int mx = pP
1eb2e 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 arse->db->aLimit
1eb2f 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f [SQLITE_LIMIT_CO
1eb30 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61 73 LUMN];. testcas
1eb31 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c e( pEList && pEL
1eb32 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20 29 ist->nExpr==mx )
1eb33 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45 ;. testcase( pE
1eb34 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e List && pEList->
1eb35 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20 nExpr==mx+1 );.
1eb36 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70 if( pEList && p
1eb37 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78 20 EList->nExpr>mx
1eb38 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1eb39 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1eb3a 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 too many columns
1eb3b 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63 74 in %s", zObject
1eb3c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
1eb3d 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 Delete an entire
1eb3e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
1eb3f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1eb40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1eb41 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 ExprListDelete(s
1eb42 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 qlite3 *db, Expr
1eb43 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 List *pList){.
1eb44 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
1eb45 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
1eb46 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 Item;. if( pLis
1eb47 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 t==0 ) return;.
1eb48 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e assert( pList->
1eb49 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d 3e a!=0 || (pList->
1eb4a 6e 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 73 nExpr==0 && pLis
1eb4b 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b t->nAlloc==0) );
1eb4c 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 . assert( pList
1eb4d 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d 3e ->nExpr<=pList->
1eb4e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 28 nAlloc );. for(
1eb4f 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 pItem=pList->a,
1eb50 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 i=0; i<pList->nE
1eb51 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b xpr; i++, pItem+
1eb52 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 +){. sqlite3E
1eb53 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49 xprDelete(db, pI
1eb54 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 tem->pExpr);.
1eb55 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1eb56 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 b, pItem->zName)
1eb57 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 ;. }. sqlite3D
1eb58 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d bFree(db, pList-
1eb59 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 >a);. sqlite3Db
1eb5a 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b Free(db, pList);
1eb5b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 .}../*.** These
1eb5c 72 6f 75 74 69 6e 65 73 20 61 72 65 20 57 61 6c routines are Wal
1eb5d 6b 65 72 20 63 61 6c 6c 62 61 63 6b 73 2e 20 20 ker callbacks.
1eb5e 57 61 6c 6b 65 72 2e 75 2e 70 69 20 69 73 20 61 Walker.u.pi is a
1eb5f 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 pointer.** to a
1eb60 6e 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65 73 n integer. Thes
1eb61 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 e routines are c
1eb62 68 65 63 6b 69 6e 67 20 61 6e 20 65 78 70 72 65 hecking an expre
1eb63 73 73 69 6f 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 ssion to see.**
1eb64 69 66 20 69 74 20 69 73 20 61 20 63 6f 6e 73 74 if it is a const
1eb65 61 6e 74 2e 20 20 53 65 74 20 2a 57 61 6c 6b 65 ant. Set *Walke
1eb66 72 2e 75 2e 70 69 20 74 6f 20 30 20 69 66 20 74 r.u.pi to 0 if t
1eb67 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
1eb68 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 .** not constant
1eb69 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 ..**.** These ca
1eb6a 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 73 20 llback routines
1eb6b 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c are used to impl
1eb6c 65 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 ement the follow
1eb6d 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 ing:.**.** s
1eb6e 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
1eb6f 74 61 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 tant().** sq
1eb70 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
1eb71 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 antNotJoin().**
1eb72 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 sqlite3ExprI
1eb73 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 sConstantOrFunct
1eb74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 ion().**.*/.stat
1eb75 69 63 20 69 6e 74 20 65 78 70 72 4e 6f 64 65 49 ic int exprNodeI
1eb76 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72 sConstant(Walker
1eb77 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 *pWalker, Expr
1eb78 2a 70 45 78 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 *pExpr){.. /* I
1eb79 66 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 f pWalker->u.i i
1eb7a 73 20 33 20 74 68 65 6e 20 61 6e 79 20 74 65 72 s 3 then any ter
1eb7b 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 m of the express
1eb7c 69 6f 6e 20 74 68 61 74 20 63 6f 6d 65 73 20 66 ion that comes f
1eb7d 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e 20 rom. ** the ON
1eb7e 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 or USING clauses
1eb7f 20 6f 66 20 61 20 6a 6f 69 6e 20 64 69 73 71 75 of a join disqu
1eb80 61 6c 69 66 69 65 73 20 74 68 65 20 65 78 70 72 alifies the expr
1eb81 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d ession. ** from
1eb82 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 being considere
1eb83 64 20 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 d constant. */.
1eb84 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e if( pWalker->u.
1eb85 69 3d 3d 33 20 26 26 20 45 78 70 72 48 61 73 41 i==3 && ExprHasA
1eb86 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 nyProperty(pExpr
1eb87 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 , EP_FromJoin) )
1eb88 7b 0a 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 {. pWalker->u
1eb89 2e 69 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 .i = 0;. retu
1eb8a 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1eb8b 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 }.. switch( pEx
1eb8c 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f 2a pr->op ){. /*
1eb8d 20 43 6f 6e 73 69 64 65 72 20 66 75 6e 63 74 69 Consider functi
1eb8e 6f 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 ons to be consta
1eb8f 6e 74 20 69 66 20 61 6c 6c 20 74 68 65 69 72 20 nt if all their
1eb90 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 63 6f arguments are co
1eb91 6e 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61 6e nstant. ** an
1eb92 64 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d d pWalker->u.i==
1eb93 32 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 2 */. case TK
1eb94 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 20 _FUNCTION:.
1eb95 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e if( pWalker->u.
1eb96 69 3d 3d 32 20 29 20 72 65 74 75 72 6e 20 30 3b i==2 ) return 0;
1eb97 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 . /* Fall t
1eb98 68 72 6f 75 67 68 20 2a 2f 0a 20 20 20 20 63 61 hrough */. ca
1eb99 73 65 20 54 4b 5f 49 44 3a 0a 20 20 20 20 63 61 se TK_ID:. ca
1eb9a 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 se TK_COLUMN:.
1eb9b 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 case TK_AGG_FU
1eb9c 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 NCTION:. case
1eb9d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a TK_AGG_COLUMN:.
1eb9e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1eb9f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 MIT_SUBQUERY.
1eba0 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a case TK_SELECT:
1eba1 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 . case TK_EXI
1eba2 53 54 53 3a 0a 20 20 20 20 20 20 74 65 73 74 63 STS:. testc
1eba3 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d ase( pExpr->op==
1eba4 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 TK_SELECT );.
1eba5 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 testcase( pEx
1eba6 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 pr->op==TK_EXIST
1eba7 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 S );.#endif.
1eba8 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
1eba9 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a r->op==TK_ID );.
1ebaa 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ebab 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f pExpr->op==TK_CO
1ebac 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 LUMN );. te
1ebad 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
1ebae 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 p==TK_AGG_FUNCTI
1ebaf 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ON );. test
1ebb0 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
1ebb1 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 =TK_AGG_COLUMN )
1ebb2 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 72 2d ;. pWalker-
1ebb3 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 >u.i = 0;.
1ebb4 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1ebb5 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 ;. default:.
1ebb6 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
1ebb7 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a Continue;. }.}.
1ebb8 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 static int selec
1ebb9 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 tNodeIsConstant(
1ebba 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
1ebbb 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65 64 Select *NotUsed
1ebbc 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
1ebbd 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
1ebbe 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d pWalker->u.i =
1ebbf 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 0;. return WRC
1ebc0 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 63 _Abort;.}.static
1ebc1 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 74 int exprIsConst
1ebc2 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 6e (Expr *p, int in
1ebc3 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b 65 itFlag){. Walke
1ebc4 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 69 r w;. w.u.i = i
1ebc5 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 78 nitFlag;. w.xEx
1ebc6 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 prCallback = exp
1ebc7 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b rNodeIsConstant;
1ebc8 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c . w.xSelectCall
1ebc9 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f 64 back = selectNod
1ebca 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 73 eIsConstant;. s
1ebcb 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 qlite3WalkExpr(&
1ebcc 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 w, p);. return
1ebcd 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 w.u.i;.}../*.**
1ebce 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 Walk an expressi
1ebcf 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e on tree. Return
1ebd0 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73 1 if the expres
1ebd1 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 sion is constant
1ebd2 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74 20 .** and 0 if it
1ebd3 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c involves variabl
1ebd4 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63 es or function c
1ebd5 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 alls..**.** For
1ebd6 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
1ebd7 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 this function, a
1ebd8 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 double-quoted s
1ebd9 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 tring (ex: "abc"
1ebda 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 ).** is consider
1ebdb 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 ed a variable bu
1ebdc 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 t a single-quote
1ebdd 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 d string (ex: 'a
1ebde 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e bc') is.** a con
1ebdf 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 stant..*/.SQLITE
1ebe0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1ebe1 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
1ebe2 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 nt(Expr *p){. r
1ebe3 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 eturn exprIsCons
1ebe4 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a t(p, 1);.}../*.*
1ebe5 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 * Walk an expres
1ebe6 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 sion tree. Retu
1ebe7 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 rn 1 if the expr
1ebe8 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 ession is consta
1ebe9 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 nt.** that does
1ebea 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72 6f no originate fro
1ebeb 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e m the ON or USIN
1ebec 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a G clauses of a j
1ebed 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 oin..** Return 0
1ebee 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 if it involves
1ebef 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e variables or fun
1ebf0 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 74 ction calls or t
1ebf1 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 erms from.** an
1ebf2 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 ON or USING clau
1ebf3 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
1ebf4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1ebf5 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 3ExprIsConstantN
1ebf6 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 7b otJoin(Expr *p){
1ebf7 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 . return exprIs
1ebf8 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a Const(p, 3);.}..
1ebf9 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 /*.** Walk an ex
1ebfa 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 pression tree.
1ebfb 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
1ebfc 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f expression is co
1ebfd 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66 nstant.** or a f
1ebfe 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74 unction call wit
1ebff 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d h constant argum
1ec00 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e ents. Return an
1ec01 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 d 0 if there.**
1ec02 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65 are any variable
1ec03 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 s..**.** For the
1ec04 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 purposes of thi
1ec05 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f s function, a do
1ec06 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 uble-quoted stri
1ec07 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a ng (ex: "abc").*
1ec08 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 * is considered
1ec09 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 a variable but a
1ec0a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 single-quoted s
1ec0b 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 tring (ex: 'abc'
1ec0c 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 ) is.** a consta
1ec0d 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
1ec0e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1ec0f 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 3ExprIsConstantO
1ec10 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a rFunction(Expr *
1ec11 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 p){. return exp
1ec12 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b 0a rIsConst(p, 2);.
1ec13 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
1ec14 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64 expression p cod
1ec15 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e es a constant in
1ec16 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73 6d teger that is sm
1ec17 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f all enough.** to
1ec18 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74 fit in a 32-bit
1ec19 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e integer, return
1ec1a 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 76 1 and put the v
1ec1b 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 alue of the inte
1ec1c 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 ger.** in *pValu
1ec1d 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 65 e. If the expre
1ec1e 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20 ssion is not an
1ec1f 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 74 integer or if it
1ec20 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 is too big.** t
1ec21 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65 o fit in a signe
1ec22 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 d 32-bit integer
1ec23 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c , return 0 and l
1ec24 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63 eave *pValue unc
1ec25 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 hanged..*/.SQLIT
1ec26 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1ec27 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 lite3ExprIsInteg
1ec28 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 er(Expr *p, int
1ec29 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 *pValue){. int
1ec2a 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d rc = 0;. if( p-
1ec2b 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 >flags & EP_IntV
1ec2c 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 61 alue ){. *pVa
1ec2d 6c 75 65 20 3d 20 70 2d 3e 69 54 61 62 6c 65 3b lue = p->iTable;
1ec2e 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
1ec2f 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e }. switch( p->
1ec30 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
1ec31 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 K_INTEGER: {.
1ec32 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 rc = sqlite3G
1ec33 65 74 49 6e 74 33 32 28 28 63 68 61 72 2a 29 70 etInt32((char*)p
1ec34 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 56 61 6c 75 ->token.z, pValu
1ec35 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b e);. break;
1ec36 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1ec37 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 TK_UPLUS: {.
1ec38 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 45 78 rc = sqlite3Ex
1ec39 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 prIsInteger(p->p
1ec3a 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20 Left, pValue);.
1ec3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1ec3c 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d }. case TK_UM
1ec3d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e INUS: {. in
1ec3e 74 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 73 t v;. if( s
1ec3f 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 qlite3ExprIsInte
1ec40 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 76 ger(p->pLeft, &v
1ec41 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 56 ) ){. *pV
1ec42 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 alue = -v;.
1ec43 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 rc = 1;.
1ec44 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1ec45 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
1ec46 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 t: break;. }.
1ec47 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 2d if( rc ){. p-
1ec48 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52 >op = TK_INTEGER
1ec49 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c ;. p->flags |
1ec4a 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 = EP_IntValue;.
1ec4b 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 2a p->iTable = *
1ec4c 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 20 72 65 pValue;. }. re
1ec4d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1ec4e 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
1ec4f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e the given strin
1ec50 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63 6f g is a row-id co
1ec51 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 lumn name..*/.SQ
1ec52 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1ec53 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 sqlite3IsRowid(
1ec54 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a const char *z){.
1ec55 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 if( sqlite3Str
1ec56 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 44 5f ICmp(z, "_ROWID_
1ec57 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 ")==0 ) return 1
1ec58 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 ;. if( sqlite3S
1ec59 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 49 44 trICmp(z, "ROWID
1ec5a 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 ")==0 ) return 1
1ec5b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 ;. if( sqlite3S
1ec5c 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 22 29 trICmp(z, "OID")
1ec5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a ==0 ) return 1;.
1ec5e 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
1ec5f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
1ec60 20 69 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 if the IN opera
1ec61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e tor optimization
1ec62 20 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 0a is enabled and.
1ec63 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 ** the SELECT st
1ec64 61 74 65 6d 65 6e 74 20 70 20 65 78 69 73 74 73 atement p exists
1ec65 20 61 6e 64 20 69 73 20 6f 66 20 74 68 65 0a 2a and is of the.*
1ec66 2a 20 73 69 6d 70 6c 65 20 66 6f 72 6d 3a 0a 2a * simple form:.*
1ec67 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 *.** SELECT
1ec68 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 <column> FROM <t
1ec69 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 able>.**.** If t
1ec6a 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c his is the case,
1ec6b 20 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 it may be possi
1ec6c 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78 ble to use an ex
1ec6d 69 73 74 69 6e 67 20 74 61 62 6c 65 0a 2a 2a 20 isting table.**
1ec6e 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61 64 or index instead
1ec6f 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20 61 of generating a
1ec70 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c n epheremal tabl
1ec71 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 e..*/.#ifndef SQ
1ec72 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1ec73 52 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 RY.static int is
1ec74 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 CandidateForInOp
1ec75 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 t(Select *p){.
1ec76 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 SrcList *pSrc;.
1ec77 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
1ec78 74 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 t;. Table *pTab
1ec79 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
1ec7a 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
1ec7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 69 /* ri
1ec7c 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 ght-hand side of
1ec7d 20 49 4e 20 69 73 20 53 45 4c 45 43 54 20 2a 2f IN is SELECT */
1ec7e 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 . if( p->pPrior
1ec7f 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
1ec80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 /* Not
1ec81 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 a compound SELE
1ec82 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 CT */. if( p->s
1ec83 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 elFlags & (SF_Di
1ec84 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 stinct|SF_Aggreg
1ec85 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ate) ){. re
1ec86 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 49 turn 0; /* No DI
1ec87 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 61 STINCT keyword a
1ec88 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 nd no aggregate
1ec89 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d functions */. }
1ec8a 0a 20 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 70 . if( p->pGroup
1ec8b 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 By ) return 0;
1ec8c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
1ec8d 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61 no GROUP BY cla
1ec8e 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e use */. if( p->
1ec8f 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20 pLimit ) return
1ec90 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1ec91 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54 20 /* Has no LIMIT
1ec92 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 clause */. if(
1ec93 70 2d 3e 70 4f 66 66 73 65 74 20 29 20 72 65 74 p->pOffset ) ret
1ec94 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e urn 0;. if( p->
1ec95 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 20 pWhere ) return
1ec96 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1ec97 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 52 45 20 /* Has no WHERE
1ec98 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 53 72 63 clause */. pSrc
1ec99 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73 = p->pSrc;. as
1ec9a 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29 3b sert( pSrc!=0 );
1ec9b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 . if( pSrc->nSr
1ec9c 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b c!=1 ) return 0;
1ec9d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 6e /* Sin
1ec9e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f 4d gle term in FROM
1ec9f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 clause */. if(
1eca0 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c pSrc->a[0].pSel
1eca1 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 ect ) return 0;
1eca2 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 /* FROM clau
1eca3 73 65 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71 se is not a subq
1eca4 75 65 72 79 20 2a 2f 0a 20 20 70 54 61 62 20 3d uery */. pTab =
1eca5 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 pSrc->a[0].pTab
1eca6 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 ;. if( pTab==0
1eca7 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 ) return 0;. if
1eca8 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 ( pTab->pSelect
1eca9 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
1ecaa 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 /* FROM cla
1ecab 75 73 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65 use is not a vie
1ecac 77 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 w */. if( IsVir
1ecad 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 tual(pTab) ) ret
1ecae 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a urn 0; /*
1ecaf 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 FROM clause not
1ecb0 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
1ecb1 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 */. pEList = p
1ecb2 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 ->pEList;. if(
1ecb3 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 pEList->nExpr!=1
1ecb4 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
1ecb5 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e /* One column
1ecb6 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1ecb7 65 74 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69 et */. if( pELi
1ecb8 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e st->a[0].pExpr->
1ecb9 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 op!=TK_COLUMN )
1ecba 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73 return 0; /* Res
1ecbb 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 ult is a column
1ecbc 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d */. return 1;.}
1ecbd 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1ecbe 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 E_OMIT_SUBQUERY
1ecbf 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 */../*.** This f
1ecc0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
1ecc1 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 by the implement
1ecc2 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 ation of the IN
1ecc3 28 2e 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a (...) operator..
1ecc4 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69 73 20 74 ** It's job is t
1ecc5 6f 20 66 69 6e 64 20 6f 72 20 63 72 65 61 74 65 o find or create
1ecc6 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 a b-tree struct
1ecc7 75 72 65 20 74 68 61 74 20 6d 61 79 20 62 65 20 ure that may be
1ecc8 75 73 65 64 0a 2a 2a 20 65 69 74 68 65 72 20 74 used.** either t
1ecc9 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 6d 62 65 o test for membe
1ecca 72 73 68 69 70 20 6f 66 20 74 68 65 20 28 2e 2e rship of the (..
1eccb 2e 29 20 73 65 74 20 6f 72 20 74 6f 20 69 74 65 .) set or to ite
1eccc 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 rate through.**
1eccd 69 74 73 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 its members, ski
1ecce 70 70 69 6e 67 20 64 75 70 6c 69 63 61 74 65 73 pping duplicates
1eccf 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 ..**.** The curs
1ecd0 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 or opened on the
1ecd1 20 73 74 72 75 63 74 75 72 65 20 28 64 61 74 61 structure (data
1ecd2 62 61 73 65 20 74 61 62 6c 65 2c 20 64 61 74 61 base table, data
1ecd3 62 61 73 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6f base index .** o
1ecd4 72 20 65 70 68 65 72 6d 61 6c 20 74 61 62 6c 65 r ephermal table
1ecd5 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 ) is stored in p
1ecd6 58 2d 3e 69 54 61 62 6c 65 20 62 65 66 6f 72 65 X->iTable before
1ecd7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
1ecd8 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 eturns..** The r
1ecd9 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e eturned value in
1ecda 64 69 63 61 74 65 73 20 74 68 65 20 73 74 72 75 dicates the stru
1ecdb 63 74 75 72 65 20 74 79 70 65 2c 20 61 73 20 66 cture type, as f
1ecdc 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
1ecdd 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 2d IN_INDEX_ROWID -
1ecde 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 The cursor was
1ecdf 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 61 opened on a data
1ece0 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 base table..**
1ece1 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 IN_INDEX_INDEX
1ece2 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 - The cursor was
1ece3 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 opened on a dat
1ece4 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 abase index..**
1ece5 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 2d IN_INDEX_EPH -
1ece6 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 The cursor wa
1ece7 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73 70 s opened on a sp
1ece8 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64 20 ecially created
1ece9 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
1ecea 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c 61 popula
1eceb 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74 61 ted epheremal ta
1ecec 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 ble..**.** An ex
1eced 69 73 74 69 6e 67 20 73 74 72 75 63 74 75 72 65 isting structure
1ecee 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 may only be use
1ecef 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 d if the SELECT
1ecf0 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 is of the simple
1ecf1 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 .** form:.**.**
1ecf2 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75 SELECT <colu
1ecf3 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e mn> FROM <table>
1ecf4 0a 2a 2a 0a 2a 2a 20 49 66 20 70 72 4e 6f 74 46 .**.** If prNotF
1ecf5 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 ound parameter i
1ecf6 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 73 74 s 0, then the st
1ecf7 72 75 63 74 75 72 65 20 77 69 6c 6c 20 62 65 20 ructure will be
1ecf8 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 0a used to iterate.
1ecf9 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 73 ** through the s
1ecfa 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 et members, skip
1ecfb 70 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 61 ping any duplica
1ecfc 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 tes. In this cas
1ecfd 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d 61 e an.** epherema
1ecfe 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 l table must be
1ecff 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20 used unless the
1ed00 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d 6e selected <column
1ed01 3e 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a > is guaranteed.
1ed02 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 20 ** to be unique
1ed03 2d 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 - either because
1ed04 20 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 45 it is an INTEGE
1ed05 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 R PRIMARY KEY or
1ed06 20 69 74 0a 2a 2a 20 69 73 20 75 6e 69 71 75 65 it.** is unique
1ed07 20 62 79 20 76 69 72 74 75 65 20 6f 66 20 61 20 by virtue of a
1ed08 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 20 69 6d constraint or im
1ed09 70 6c 69 63 69 74 20 69 6e 64 65 78 2e 0a 2a 2a plicit index..**
1ed0a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f 74 .** If the prNot
1ed0b 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 Found parameter
1ed0c 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74 is not 0, then t
1ed0d 68 65 20 73 74 72 75 63 74 75 72 65 20 77 69 6c he structure wil
1ed0e 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f l be used .** fo
1ed0f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65 r fast set membe
1ed10 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20 rship tests. In
1ed11 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 70 68 this case an eph
1ed12 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 eremal table mus
1ed13 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e t .** be used un
1ed14 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 less <column> is
1ed15 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d an INTEGER PRIM
1ed16 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e ARY KEY or an in
1ed17 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 66 dex can .** be f
1ed18 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d ound with <colum
1ed19 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d n> as its left-m
1ed1a 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a ost column..**.*
1ed1b 2a 20 57 68 65 6e 20 74 68 65 20 73 74 72 75 63 * When the struc
1ed1c 74 75 72 65 20 69 73 20 62 65 69 6e 67 20 75 73 ture is being us
1ed1d 65 64 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 ed for set membe
1ed1e 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 68 65 rship tests, the
1ed1f 20 75 73 65 72 0a 2a 2a 20 6e 65 65 64 73 20 74 user.** needs t
1ed20 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f o know whether o
1ed21 72 20 6e 6f 74 20 74 68 65 20 73 74 72 75 63 74 r not the struct
1ed22 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 ure contains an
1ed23 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c SQL NULL .** val
1ed24 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 ue in order to c
1ed25 6f 72 72 65 63 74 6c 79 20 65 76 61 6c 75 61 74 orrectly evaluat
1ed26 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 69 e expressions li
1ed27 6b 65 20 22 58 20 49 4e 20 28 59 2c 20 5a 29 22 ke "X IN (Y, Z)"
1ed28 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 ..** If there is
1ed29 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 a chance that t
1ed2a 68 65 20 73 74 72 75 63 74 75 72 65 20 6d 61 79 he structure may
1ed2b 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20 contain a NULL
1ed2c 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 75 6e 74 value at.** runt
1ed2d 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 65 67 69 ime, then a regi
1ed2e 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 ster is allocate
1ed2f 64 20 61 6e 64 20 74 68 65 20 72 65 67 69 73 74 d and the regist
1ed30 65 72 20 6e 75 6d 62 65 72 20 77 72 69 74 74 65 er number writte
1ed31 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f 74 46 6f n.** to *prNotFo
1ed32 75 6e 64 2e 20 49 66 20 74 68 65 72 65 20 69 73 und. If there is
1ed33 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20 no chance that
1ed34 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63 6f the structure co
1ed35 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c ntains a.** NULL
1ed36 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 72 value, then *pr
1ed37 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 74 NotFound is left
1ed38 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a unchanged..**.*
1ed39 2a 20 49 66 20 61 20 72 65 67 69 73 74 65 72 20 * If a register
1ed3a 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 is allocated and
1ed3b 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 74 its location st
1ed3c 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 6f ored in *prNotFo
1ed3d 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 73 und, then.** its
1ed3e 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 69 initial value i
1ed3f 73 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 s NULL. If the s
1ed40 74 72 75 63 74 75 72 65 20 64 6f 65 73 20 6e 6f tructure does no
1ed41 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e t remain constan
1ed42 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72 t.** for the dur
1ed43 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65 ation of the que
1ed44 72 79 20 28 69 2e 65 2e 20 74 68 65 20 73 65 74 ry (i.e. the set
1ed45 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 is a correlated
1ed46 20 73 75 62 2d 73 65 6c 65 63 74 29 2c 20 0a 2a sub-select), .*
1ed47 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 * the value of t
1ed48 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67 he allocated reg
1ed49 69 73 74 65 72 20 69 73 20 72 65 73 65 74 20 74 ister is reset t
1ed4a 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 6d 65 o NULL each time
1ed4b 20 74 68 65 20 0a 2a 2a 20 73 74 72 75 63 74 75 the .** structu
1ed4c 72 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74 65 re is repopulate
1ed4d 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 74 d. This allows t
1ed4e 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 he caller to use
1ed4f 20 76 64 62 65 20 63 6f 64 65 20 0a 2a 2a 20 65 vdbe code .** e
1ed50 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65 quivalent to the
1ed51 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
1ed52 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72 * if( register
1ed53 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 ==NULL ){.**
1ed54 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 has_null = <tes
1ed55 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74 t if data struct
1ed56 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c ure contains nul
1ed57 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74 l>.** regist
1ed58 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a er = 1.** }.**
1ed59 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 .** in order to
1ed5a 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68 avoid running th
1ed5b 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 e <test if data
1ed5c 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
1ed5d 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 ns null>.** test
1ed5e 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e more often than
1ed5f 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a is necessary..*
1ed60 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1ed61 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 _OMIT_SUBQUERY.S
1ed62 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1ed63 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 t sqlite3FindInI
1ed64 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 ndex(Parse *pPar
1ed65 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e se, Expr *pX, in
1ed66 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a t *prNotFound){.
1ed67 20 20 53 65 6c 65 63 74 20 2a 70 3b 0a 20 20 69 Select *p;. i
1ed68 6e 74 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 20 nt eType = 0;.
1ed69 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 int iTab = pPars
1ed6a 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 6e 74 e->nTab++;. int
1ed6b 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d 20 mustBeUnique =
1ed6c 21 70 72 4e 6f 74 46 6f 75 6e 64 3b 0a 0a 20 20 !prNotFound;..
1ed6d 2f 2a 20 54 68 65 20 66 6f 6c 6c 77 69 6e 67 20 /* The follwing
1ed6e 69 66 28 2e 2e 2e 29 20 65 78 70 72 65 73 73 69 if(...) expressi
1ed6f 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74 68 on is true if th
1ed70 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 e SELECT is of t
1ed71 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 he . ** simple
1ed72 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 form:. **. **
1ed73 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75 SELECT <colu
1ed74 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e mn> FROM <table>
1ed75 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 . **. ** If th
1ed76 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 is is the case,
1ed77 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 62 it may be possib
1ed78 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78 69 le to use an exi
1ed79 73 74 69 6e 67 20 74 61 62 6c 65 0a 20 20 2a 2a sting table. **
1ed7a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61 or index instea
1ed7b 64 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20 d of generating
1ed7c 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 an epheremal tab
1ed7d 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 28 le.. */. p = (
1ed7e 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
1ed7f 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pX, EP_xIsSelect
1ed80 29 20 3f 20 70 58 2d 3e 78 2e 70 53 65 6c 65 63 ) ? pX->x.pSelec
1ed81 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 69 73 t : 0);. if( is
1ed82 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 CandidateForInOp
1ed83 74 28 70 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 t(p) ){. sqli
1ed84 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1ed85 2d 3e 64 62 3b 0a 20 20 20 20 49 6e 64 65 78 20 ->db;. Index
1ed86 2a 70 49 64 78 3b 0a 20 20 20 20 45 78 70 72 20 *pIdx;. Expr
1ed87 2a 70 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 *pExpr = p->pELi
1ed88 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a st->a[0].pExpr;.
1ed89 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 int iCol = p
1ed8a 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 Expr->iColumn;.
1ed8b 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c Vdbe *v = sql
1ed8c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 ite3GetVdbe(pPar
1ed8d 73 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 se);.. /* Thi
1ed8e 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e s function is on
1ed8f 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 ly called from t
1ed90 77 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f wo places. In bo
1ed91 74 68 20 63 61 73 65 73 20 74 68 65 20 76 64 62 th cases the vdb
1ed92 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 e. ** has alr
1ed93 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 eady been alloca
1ed94 74 65 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 ted. So assume s
1ed95 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 29 20 qlite3GetVdbe()
1ed96 69 73 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a is always. **
1ed97 20 73 75 63 63 65 73 73 66 75 6c 20 68 65 72 65 successful here
1ed98 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
1ed99 65 72 74 28 76 29 3b 0a 20 20 20 20 69 66 28 20 ert(v);. if(
1ed9a 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 iCol<0 ){.
1ed9b 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 int iMem = ++pPa
1ed9c 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 rse->nMem;.
1ed9d 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 int iAddr;.
1ed9e 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
1ed9f 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 p->pSrc->a[0].pT
1eda0 61 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 ab;. int iD
1eda1 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
1eda2 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 aToIndex(db, pTa
1eda3 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 b->pSchema);.
1eda4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 sqlite3VdbeUs
1eda5 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b esBtree(v, iDb);
1eda6 0a 0a 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 .. iAddr =
1eda7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1eda8 31 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 1(v, OP_If, iMem
1eda9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1edaa 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1edab 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 _Integer, 1, iMe
1edac 6d 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 m);.. sqlit
1edad 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 e3OpenTable(pPar
1edae 73 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 se, iTab, iDb, p
1edaf 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 Tab, OP_OpenRead
1edb0 29 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d );. eType =
1edb1 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b IN_INDEX_ROWID;
1edb2 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 .. sqlite3V
1edb3 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 dbeJumpHere(v, i
1edb4 41 64 64 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Addr);. }else
1edb5 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 {. /* The c
1edb6 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
1edb7 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 6f e used by the co
1edb8 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20 mparison. If an
1edb9 69 6e 64 65 78 20 69 73 20 74 6f 20 0a 20 20 20 index is to .
1edba 20 20 20 2a 2a 20 62 65 20 75 73 65 64 20 69 6e ** be used in
1edbb 20 70 6c 61 63 65 20 6f 66 20 61 20 74 65 6d 70 place of a temp
1edbc 2d 74 61 62 6c 65 2c 20 69 74 20 6d 75 73 74 20 -table, it must
1edbd 62 65 20 6f 72 64 65 72 65 64 20 61 63 63 6f 72 be ordered accor
1edbe 64 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 6f ding. ** to
1edbf 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 this collation
1edc0 73 65 71 75 65 6e 63 65 2e 0a 20 20 20 20 20 20 sequence..
1edc1 2a 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 */. CollSeq
1edc2 20 2a 70 52 65 71 20 3d 20 73 71 6c 69 74 65 33 *pReq = sqlite3
1edc3 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c BinaryCompareCol
1edc4 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58 2d lSeq(pParse, pX-
1edc5 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 29 3b 0a >pLeft, pExpr);.
1edc6 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 . /* Check
1edc7 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 74 that the affinit
1edc8 79 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 y that will be u
1edc9 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 sed to perform t
1edca 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d he . ** com
1edcb 70 61 72 69 73 6f 6e 20 69 73 20 74 68 65 20 73 parison is the s
1edcc 61 6d 65 20 61 73 20 74 68 65 20 61 66 66 69 6e ame as the affin
1edcd 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d ity of the colum
1edce 6e 2e 20 49 66 0a 20 20 20 20 20 20 2a 2a 20 69 n. If. ** i
1edcf 74 20 69 73 20 6e 6f 74 2c 20 69 74 20 69 73 20 t is not, it is
1edd0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
1edd1 75 73 65 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20 use any index..
1edd2 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54 61 */. Ta
1edd3 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70 ble *pTab = p->p
1edd4 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a Src->a[0].pTab;.
1edd5 20 20 20 20 20 20 63 68 61 72 20 61 66 66 20 3d char aff =
1edd6 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e comparisonAffin
1edd7 69 74 79 28 70 58 29 3b 0a 20 20 20 20 20 20 69 ity(pX);. i
1edd8 6e 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d nt affinity_ok =
1edd9 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f (pTab->aCol[iCo
1edda 6c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 l].affinity==aff
1eddb 7c 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 ||aff==SQLITE_AF
1eddc 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 F_NONE);..
1eddd 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 for(pIdx=pTab->p
1edde 49 6e 64 65 78 3b 20 70 49 64 78 20 26 26 20 65 Index; pIdx && e
1eddf 54 79 70 65 3d 3d 30 20 26 26 20 61 66 66 69 6e Type==0 && affin
1ede0 69 74 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 ity_ok; pIdx=pId
1ede1 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 x->pNext){.
1ede2 20 20 20 69 66 28 20 28 70 49 64 78 2d 3e 61 69 if( (pIdx->ai
1ede3 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 Column[0]==iCol)
1ede4 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70 52 . && (pR
1ede5 65 71 3d 3d 73 71 6c 69 74 65 33 46 69 6e 64 43 eq==sqlite3FindC
1ede6 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 ollSeq(db, ENC(d
1ede7 62 29 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c b), pIdx->azColl
1ede8 5b 30 5d 2c 20 2d 31 2c 20 30 29 29 0a 20 20 20 [0], -1, 0)).
1ede9 20 20 20 20 20 20 26 26 20 28 21 6d 75 73 74 42 && (!mustB
1edea 65 55 6e 69 71 75 65 20 7c 7c 20 28 70 49 64 78 eUnique || (pIdx
1edeb 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 ->nColumn==1 &&
1edec 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f pIdx->onError!=O
1eded 45 5f 4e 6f 6e 65 29 29 0a 20 20 20 20 20 20 20 E_None)).
1edee 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e ){. in
1edef 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 20 t iDb;.
1edf0 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 int iMem = ++pP
1edf1 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
1edf2 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b int iAddr;
1edf3 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 . char
1edf4 2a 70 4b 65 79 3b 0a 20 20 0a 20 20 20 20 20 20 *pKey;. .
1edf5 20 20 20 20 70 4b 65 79 20 3d 20 28 63 68 61 72 pKey = (char
1edf6 20 2a 29 73 71 6c 69 74 65 33 49 6e 64 65 78 4b *)sqlite3IndexK
1edf7 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 eyinfo(pParse, p
1edf8 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Idx);.
1edf9 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
1edfa 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 emaToIndex(db, p
1edfb 49 64 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Idx->pSchema);.
1edfc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1edfd 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c VdbeUsesBtree(v,
1edfe 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 20 20 iDb);..
1edff 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 iAddr = sqlite
1ee00 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
1ee01 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 P_If, iMem);.
1ee02 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1ee03 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
1ee04 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29 nteger, 1, iMem)
1ee05 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 73 ;. . s
1ee06 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1ee07 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c (v, OP_OpenRead,
1ee08 20 69 54 61 62 2c 20 70 49 64 78 2d 3e 74 6e 75 iTab, pIdx->tnu
1ee09 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 m, iDb,.
1ee0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee0b 20 20 20 20 20 20 20 70 4b 65 79 2c 50 34 5f 4b pKey,P4_K
1ee0c 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b EYINFO_HANDOFF);
1ee0d 0a 20 20 20 20 20 20 20 20 20 20 56 64 62 65 43 . VdbeC
1ee0e 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c omment((v, "%s",
1ee0f 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a pIdx->zName));.
1ee10 20 20 20 20 20 20 20 20 20 20 65 54 79 70 65 20 eType
1ee11 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 = IN_INDEX_INDEX
1ee12 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ;.. sql
1ee13 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
1ee14 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20 (v, iAddr);.
1ee15 20 20 20 20 20 20 69 66 28 20 70 72 4e 6f 74 46 if( prNotF
1ee16 6f 75 6e 64 20 26 26 20 21 70 54 61 62 2d 3e 61 ound && !pTab->a
1ee17 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e 75 6c Col[iCol].notNul
1ee18 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 l ){.
1ee19 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b *prNotFound = +
1ee1a 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
1ee1b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1ee1c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1ee1d 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 54 }. }.. if( eT
1ee1e 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e ype==0 ){. in
1ee1f 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d t rMayHaveNull =
1ee20 20 30 3b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 0;. eType =
1ee21 49 4e 5f 49 4e 44 45 58 5f 45 50 48 3b 0a 20 20 IN_INDEX_EPH;.
1ee22 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 if( prNotFound
1ee23 20 29 7b 0a 20 20 20 20 20 20 2a 70 72 4e 6f 74 ){. *prNot
1ee24 46 6f 75 6e 64 20 3d 20 72 4d 61 79 48 61 76 65 Found = rMayHave
1ee25 4e 75 6c 6c 20 3d 20 2b 2b 70 50 61 72 73 65 2d Null = ++pParse-
1ee26 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 >nMem;. }else
1ee27 20 69 66 28 20 70 58 2d 3e 70 4c 65 66 74 2d 3e if( pX->pLeft->
1ee28 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 45 78 iColumn<0 && !Ex
1ee29 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
1ee2a 28 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 (pX, EP_xIsSelec
1ee2b 74 29 20 29 7b 0a 20 20 20 20 20 20 65 54 79 70 t) ){. eTyp
1ee2c 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 e = IN_INDEX_ROW
1ee2d 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 ID;. }. sq
1ee2e 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 lite3CodeSubsele
1ee2f 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c 20 72 ct(pParse, pX, r
1ee30 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65 54 79 MayHaveNull, eTy
1ee31 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 pe==IN_INDEX_ROW
1ee32 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ID);. }else{.
1ee33 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20 69 pX->iTable = i
1ee34 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Tab;. }. retur
1ee35 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64 69 n eType;.}.#endi
1ee36 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 f../*.** Generat
1ee37 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 6c 61 e code for scala
1ee38 72 20 73 75 62 71 75 65 72 69 65 73 20 75 73 65 r subqueries use
1ee39 64 20 61 73 20 61 6e 20 65 78 70 72 65 73 73 69 d as an expressi
1ee3a 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f 70 65 on.** and IN ope
1ee3b 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c 65 rators. Example
1ee3c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53 45 s:.**.** (SE
1ee3d 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 LECT a FROM b)
1ee3e 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62 71 75 -- subqu
1ee3f 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 53 54 ery.** EXIST
1ee40 53 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d S (SELECT a FROM
1ee41 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54 53 20 b) -- EXISTS
1ee42 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 subquery.**
1ee43 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 20 20 x IN (4,5,11)
1ee44 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 49 4e -- IN
1ee45 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 6c operator with l
1ee46 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 61 6e ist on right-han
1ee47 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 78 20 d side.** x
1ee48 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f IN (SELECT a FRO
1ee49 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e 20 6f M b) -- IN o
1ee4a 70 65 72 61 74 6f 72 20 77 69 74 68 20 73 75 62 perator with sub
1ee4b 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72 69 67 query on the rig
1ee4c 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45 78 ht.**.** The pEx
1ee4d 70 72 20 70 61 72 61 6d 65 74 65 72 20 64 65 73 pr parameter des
1ee4e 63 72 69 62 65 73 20 74 68 65 20 65 78 70 72 65 cribes the expre
1ee4f 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 61 ssion that conta
1ee50 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f 70 ins the IN.** op
1ee51 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71 75 65 erator or subque
1ee52 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 ry..**.** If par
1ee53 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64 20 69 ameter isRowid i
1ee54 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1ee55 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 expression pExp
1ee56 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a r is guaranteed.
1ee57 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 65 20 ** to be of the
1ee58 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49 4e form "<rowid> IN
1ee59 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68 65 (?, ?, ?)", whe
1ee5a 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 61 20 re <rowid> is a
1ee5b 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f 20 reference.** to
1ee5c 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b 65 79 some integer key
1ee5d 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 62 column of a tab
1ee5e 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 le B-Tree. In th
1ee5f 69 73 20 63 61 73 65 2c 20 75 73 65 20 61 6e 0a is case, use an.
1ee60 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 ** intkey B-Tree
1ee61 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 to store the se
1ee62 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61 6c t of IN(...) val
1ee63 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 74 ues instead of t
1ee64 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c 6f he usual.** (slo
1ee65 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 6c 65 wer) variable le
1ee66 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 65 65 ngth keys B-Tree
1ee67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
1ee68 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 ITE_OMIT_SUBQUER
1ee69 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 Y.SQLITE_PRIVATE
1ee6a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 void sqlite3Cod
1ee6b 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20 50 61 eSubselect(. Pa
1ee6c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 rse *pParse, .
1ee6d 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a 20 20 Expr *pExpr, .
1ee6e 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c int rMayHaveNull
1ee6f 2c 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 64 0a ,. int isRowid.
1ee70 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64 64 ){. int testAdd
1ee71 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 r = 0;
1ee72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ee73 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61 64 One-time test ad
1ee74 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 62 65 20 dress */. Vdbe
1ee75 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 *v = sqlite3GetV
1ee76 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 dbe(pParse);. i
1ee77 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e f( v==0 ) return
1ee78 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f ;... /* This co
1ee79 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20 69 de must be run i
1ee7a 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 20 65 n its entirety e
1ee7b 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 73 20 very time it is
1ee7c 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a 2a encountered. **
1ee7d 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 if any of the f
1ee7e 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65 ollowing is true
1ee7f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a :. **. ** *
1ee80 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 The right-hand
1ee81 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 72 65 side is a corre
1ee82 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 0a 20 lated subquery.
1ee83 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69 ** * The ri
1ee84 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 ght-hand side is
1ee85 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c an expression l
1ee86 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 ist containing v
1ee87 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20 20 ariables. **
1ee88 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69 64 * We are insid
1ee89 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a 2a e a trigger. **
1ee8a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 20 . ** If all of
1ee8b 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66 61 the above are fa
1ee8c 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61 6e lse, then we can
1ee8d 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 6a run this code j
1ee8e 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 61 ust once. ** sa
1ee8f 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c 20 ve the results,
1ee90 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 73 61 and reuse the sa
1ee91 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75 62 me result on sub
1ee92 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 sequent invocati
1ee93 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ons.. */. if(
1ee94 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 !ExprHasAnyPrope
1ee95 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 rty(pExpr, EP_Va
1ee96 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 50 61 rSelect) && !pPa
1ee97 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 rse->trigStack )
1ee98 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d 20 {. int mem =
1ee99 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
1ee9a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1ee9b 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 ddOp1(v, OP_If,
1ee9c 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 41 64 mem);. testAd
1ee9d 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
1ee9e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
1ee9f 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a 20 eger, 1, mem);.
1eea0 20 20 20 61 73 73 65 72 74 28 20 74 65 73 74 41 assert( testA
1eea1 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 65 2d ddr>0 || pParse-
1eea2 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1eea3 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 d );. }.. swit
1eea4 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b ch( pExpr->op ){
1eea5 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a . case TK_IN:
1eea6 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 {. char af
1eea7 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b 65 finity;. Ke
1eea8 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a 20 yInfo keyInfo;.
1eea9 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20 20 int addr;
1eeaa 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
1eeab 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d of OP_OpenEphem
1eeac 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e eral instruction
1eead 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a */. Expr *
1eeae 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 pLeft = pExpr->p
1eeaf 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 66 28 Left;.. if(
1eeb0 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 7b rMayHaveNull ){
1eeb1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1eeb2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1eeb3 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 48 61 _Null, 0, rMayHa
1eeb4 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 7d veNull);. }
1eeb5 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 .. affinity
1eeb6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 = sqlite3ExprAf
1eeb7 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b 0a 0a finity(pLeft);..
1eeb8 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68 65 72 /* Whether
1eeb9 20 74 68 69 73 20 69 73 20 61 6e 20 27 78 20 49 this is an 'x I
1eeba 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 6f 72 N(SELECT...)' or
1eebb 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 72 6c an 'x IN(<exprl
1eebc 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a 2a 20 ist>)'. **
1eebd 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20 69 73 expression it is
1eebe 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73 61 6d handled the sam
1eebf 65 20 77 61 79 2e 20 41 20 76 69 72 74 75 61 6c e way. A virtual
1eec0 20 74 61 62 6c 65 20 69 73 20 0a 20 20 20 20 20 table is .
1eec1 20 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68 20 ** filled with
1eec2 73 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 6e 64 single-field ind
1eec3 65 78 20 6b 65 79 73 20 72 65 70 72 65 73 65 6e ex keys represen
1eec4 74 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 73 ting the results
1eec5 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 . ** from t
1eec6 68 65 20 53 45 4c 45 43 54 20 6f 72 20 74 68 65 he SELECT or the
1eec7 20 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 20 20 <exprlist>..
1eec8 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 **. ** I
1eec9 66 20 74 68 65 20 27 78 27 20 65 78 70 72 65 73 f the 'x' expres
1eeca 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e sion is a column
1eecb 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20 53 value, or the S
1eecc 45 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 20 2a ELECT.... *
1eecd 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75 * statement retu
1eece 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c rns a column val
1eecf 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66 66 ue, then the aff
1eed0 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0a 20 20 inity of that.
1eed1 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 ** column is
1eed2 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 74 used to build t
1eed3 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 he index keys. I
1eed4 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64 20 74 f both 'x' and t
1eed5 68 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c 45 he. ** SELE
1eed6 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 CT... statement
1eed7 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 are columns, the
1eed8 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 n numeric affini
1eed9 74 79 20 69 73 20 75 73 65 64 0a 20 20 20 20 20 ty is used.
1eeda 20 2a 2a 20 69 66 20 65 69 74 68 65 72 20 63 6f ** if either co
1eedb 6c 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 49 43 lumn has NUMERIC
1eedc 20 6f 72 20 49 4e 54 45 47 45 52 20 61 66 66 69 or INTEGER affi
1eedd 6e 69 74 79 2e 20 49 66 20 6e 65 69 74 68 65 72 nity. If neither
1eede 0a 20 20 20 20 20 20 2a 2a 20 27 78 27 20 6e 6f . ** 'x' no
1eedf 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 r the SELECT...
1eee0 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f statement are co
1eee1 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 lumns, then nume
1eee2 72 69 63 20 61 66 66 69 6e 69 74 79 0a 20 20 20 ric affinity.
1eee3 20 20 20 2a 2a 20 69 73 20 75 73 65 64 2e 0a 20 ** is used..
1eee4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 45 */. pE
1eee5 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 50 xpr->iTable = pP
1eee6 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 arse->nTab++;.
1eee7 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 addr = sqlit
1eee8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1eee9 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
1eeea 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c , pExpr->iTable,
1eeeb 20 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 20 20 !isRowid);.
1eeec 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e 66 memset(&keyInf
1eeed 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79 o, 0, sizeof(key
1eeee 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b 65 Info));. ke
1eeef 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20 31 yInfo.nField = 1
1eef0 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 ;.. if( Exp
1eef1 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
1eef2 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pr, EP_xIsSelect
1eef3 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
1eef4 43 61 73 65 20 31 3a 20 20 20 20 20 65 78 70 72 Case 1: expr
1eef5 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 IN (SELECT ...)
1eef6 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
1eef7 20 20 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 20 ** Generate
1eef8 63 6f 64 65 20 74 6f 20 77 72 69 74 65 20 74 68 code to write th
1eef9 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 e results of the
1eefa 20 73 65 6c 65 63 74 20 69 6e 74 6f 20 74 68 65 select into the
1eefb 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 20 temporary.
1eefc 20 20 20 2a 2a 20 74 61 62 6c 65 20 61 6c 6c 6f ** table allo
1eefd 63 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 cated and opened
1eefe 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 above..
1eeff 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63 */. Selec
1ef00 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 tDest dest;.
1ef01 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 ExprList *pE
1ef02 4c 69 73 74 3b 0a 0a 20 20 20 20 20 20 20 20 61 List;.. a
1ef03 73 73 65 72 74 28 20 21 69 73 52 6f 77 69 64 20 ssert( !isRowid
1ef04 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1ef05 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 e3SelectDestInit
1ef06 28 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74 2c (&dest, SRT_Set,
1ef07 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b pExpr->iTable);
1ef08 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 61 66 . dest.af
1ef09 66 69 6e 69 74 79 20 3d 20 28 75 38 29 61 66 66 finity = (u8)aff
1ef0a 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 61 inity;. a
1ef0b 73 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e 69 ssert( (pExpr->i
1ef0c 54 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 46 Table&0x0000FFFF
1ef0d 29 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 )==pExpr->iTable
1ef0e 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
1ef0f 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 sqlite3Select(pP
1ef10 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 78 2e 70 arse, pExpr->x.p
1ef11 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29 20 29 Select, &dest) )
1ef12 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
1ef13 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rn;. }.
1ef14 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 pEList = p
1ef15 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d Expr->x.pSelect-
1ef16 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 >pEList;.
1ef17 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70 if( pEList && p
1ef18 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29 EList->nExpr>0 )
1ef19 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79 { . key
1ef1a 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 Info.aColl[0] =
1ef1b 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d sqlite3BinaryCom
1ef1c 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 pareCollSeq(pPar
1ef1d 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
1ef1e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1ef1f 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 pEList->a[0].pEx
1ef20 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 pr);. }.
1ef21 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
1ef22 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 29 7b Expr->x.pList ){
1ef23 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 . /* Case
1ef24 20 32 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 2: expr IN
1ef25 28 65 78 70 72 6c 69 73 74 29 0a 20 20 20 20 20 (exprlist).
1ef26 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
1ef27 20 46 6f 72 20 65 61 63 68 20 65 78 70 72 65 73 For each expres
1ef28 73 69 6f 6e 2c 20 62 75 69 6c 64 20 61 6e 20 69 sion, build an i
1ef29 6e 64 65 78 20 6b 65 79 20 66 72 6f 6d 20 74 68 ndex key from th
1ef2a 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 61 6e 64 e evaluation and
1ef2b 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 . ** stor
1ef2c 65 20 69 74 20 69 6e 20 74 68 65 20 74 65 6d 70 e it in the temp
1ef2d 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 49 66 20 orary table. If
1ef2e 3c 65 78 70 72 3e 20 69 73 20 61 20 63 6f 6c 75 <expr> is a colu
1ef2f 6d 6e 2c 20 74 68 65 6e 20 75 73 65 0a 20 20 20 mn, then use.
1ef30 20 20 20 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c ** that col
1ef31 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 20 77 68 umns affinity wh
1ef32 65 6e 20 62 75 69 6c 64 69 6e 67 20 69 6e 64 65 en building inde
1ef33 78 20 6b 65 79 73 2e 20 49 66 20 3c 65 78 70 72 x keys. If <expr
1ef34 3e 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 20 > is not.
1ef35 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e 2c 20 75 73 ** a column, us
1ef36 65 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 e numeric affini
1ef37 74 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ty.. */.
1ef38 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 int i;.
1ef39 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a ExprList *
1ef3a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 pList = pExpr->x
1ef3b 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 .pList;.
1ef3c 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1ef3d 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 item *pItem;.
1ef3e 20 20 20 20 20 69 6e 74 20 72 31 2c 20 72 32 2c int r1, r2,
1ef3f 20 72 33 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 r3;.. if
1ef40 28 20 21 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 ( !affinity ){.
1ef41 20 20 20 20 20 20 20 20 20 61 66 66 69 6e 69 74 affinit
1ef42 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e y = SQLITE_AFF_N
1ef43 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ONE;. }.
1ef44 20 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 keyInfo.a
1ef45 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 Coll[0] = sqlite
1ef46 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 3ExprCollSeq(pPa
1ef47 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1ef48 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 t);.. /*
1ef49 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61 63 Loop through eac
1ef4a 68 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 h expression in
1ef4b 3c 65 78 70 72 6c 69 73 74 3e 2e 20 2a 2f 0a 20 <exprlist>. */.
1ef4c 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 r1 = sqli
1ef4d 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
1ef4e 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 arse);. r
1ef4f 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 2 = sqlite3GetTe
1ef50 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 mpReg(pParse);.
1ef51 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1ef52 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
1ef53 75 6c 6c 2c 20 30 2c 20 72 32 29 3b 0a 20 20 20 ull, 0, r2);.
1ef54 20 20 20 20 20 66 6f 72 28 69 3d 70 4c 69 73 74 for(i=pList
1ef55 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 ->nExpr, pItem=p
1ef56 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d List->a; i>0; i-
1ef57 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 -, pItem++){.
1ef58 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 32 Expr *pE2
1ef59 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b = pItem->pExpr;
1ef5a 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 .. /* I
1ef5b 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1ef5c 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 is not constant
1ef5d 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e 65 then we will ne
1ef5e 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 ed to.
1ef5f 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 ** disable the t
1ef60 65 73 74 20 74 68 61 74 20 77 61 73 20 67 65 6e est that was gen
1ef61 65 72 61 74 65 64 20 61 62 6f 76 65 20 74 68 61 erated above tha
1ef62 74 20 6d 61 6b 65 73 20 73 75 72 65 0a 20 20 20 t makes sure.
1ef63 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63 ** this c
1ef64 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 75 74 65 ode only execute
1ef65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 75 73 65 s once. Because
1ef66 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f 6e 73 74 for a non-const
1ef67 61 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ant. **
1ef68 20 65 78 70 72 65 73 73 69 6f 6e 20 77 65 20 6e expression we n
1ef69 65 65 64 20 74 6f 20 72 65 72 75 6e 20 74 68 69 eed to rerun thi
1ef6a 73 20 63 6f 64 65 20 65 61 63 68 20 74 69 6d 65 s code each time
1ef6b 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
1ef6c 20 20 20 20 20 20 20 20 20 69 66 28 20 74 65 73 if( tes
1ef6d 74 41 64 64 72 20 26 26 20 21 73 71 6c 69 74 65 tAddr && !sqlite
1ef6e 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 3ExprIsConstant(
1ef6f 70 45 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 pE2) ){.
1ef70 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1ef71 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 74 hangeToNoop(v, t
1ef72 65 73 74 41 64 64 72 2d 31 2c 20 32 29 3b 0a 20 estAddr-1, 2);.
1ef73 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 41 testA
1ef74 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 ddr = 0;.
1ef75 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 }..
1ef76 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 /* Evaluate the
1ef77 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 69 expression and i
1ef78 6e 73 65 72 74 20 69 74 20 69 6e 74 6f 20 74 68 nsert it into th
1ef79 65 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a e temp table */.
1ef7a 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 pParse
1ef7b 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 ->disableColCach
1ef7c 65 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 72 e++;. r
1ef7d 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 3 = sqlite3ExprC
1ef7e 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 odeTarget(pParse
1ef7f 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 20 , pE2, r1);.
1ef80 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1ef81 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1ef82 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 Cache>0 );.
1ef83 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 pParse->dis
1ef84 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a ableColCache--;.
1ef85 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 . if( i
1ef86 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 sRowid ){.
1ef87 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1ef88 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 eAddOp2(v, OP_Mu
1ef89 73 74 42 65 49 6e 74 2c 20 72 33 2c 20 73 71 6c stBeInt, r3, sql
1ef8a 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 ite3VdbeCurrentA
1ef8b 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 ddr(v)+2);.
1ef8c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1ef8d 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 beAddOp3(v, OP_I
1ef8e 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 nsert, pExpr->iT
1ef8f 61 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a 20 able, r2, r3);.
1ef90 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
1ef91 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
1ef92 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1ef93 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 OP_MakeRecord,
1ef94 72 33 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69 r3, 1, r2, &affi
1ef95 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 nity, 1);.
1ef96 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1ef97 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 rCacheAffinityCh
1ef98 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 33 2c ange(pParse, r3,
1ef99 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 1);.
1ef9a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1ef9b 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 p2(v, OP_IdxInse
1ef9c 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c rt, pExpr->iTabl
1ef9d 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 e, r2);.
1ef9e 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1ef9f 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c sqlite3Rel
1efa0 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
1efa1 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 se, r1);.
1efa2 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
1efa3 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
1efa4 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 2);. }.
1efa5 20 20 69 66 28 20 21 69 73 52 6f 77 69 64 20 29 if( !isRowid )
1efa6 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1efa7 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 3VdbeChangeP4(v,
1efa8 20 61 64 64 72 2c 20 28 76 6f 69 64 20 2a 29 26 addr, (void *)&
1efa9 6b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 keyInfo, P4_KEYI
1efaa 4e 46 4f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 NFO);. }.
1efab 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1efac 0a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 .. case TK_EX
1efad 49 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 ISTS:. case T
1efae 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 K_SELECT: {.
1efaf 20 20 2f 2a 20 54 68 69 73 20 68 61 73 20 74 6f /* This has to
1efb0 20 62 65 20 61 20 73 63 61 6c 61 72 20 53 45 4c be a scalar SEL
1efb1 45 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63 ECT. Generate c
1efb2 6f 64 65 20 74 6f 20 70 75 74 20 74 68 65 0a 20 ode to put the.
1efb3 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 ** value of
1efb4 20 74 68 69 73 20 73 65 6c 65 63 74 20 69 6e 20 this select in
1efb5 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e a memory cell an
1efb6 64 20 72 65 63 6f 72 64 20 74 68 65 20 6e 75 6d d record the num
1efb7 62 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 ber. ** of
1efb8 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
1efb9 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20 in iColumn..
1efba 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 */. stati
1efbb 63 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e c const Token on
1efbc 65 20 3d 20 7b 20 28 75 38 2a 29 22 31 22 2c 20 e = { (u8*)"1",
1efbd 30 2c 20 31 20 7d 3b 0a 20 20 20 20 20 20 53 65 0, 1 };. Se
1efbe 6c 65 63 74 20 2a 70 53 65 6c 3b 0a 20 20 20 20 lect *pSel;.
1efbf 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 SelectDest des
1efc0 74 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 t;.. assert
1efc1 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
1efc2 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
1efc3 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 elect) );.
1efc4 70 53 65 6c 20 3d 20 70 45 78 70 72 2d 3e 78 2e pSel = pExpr->x.
1efc5 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 73 pSelect;. s
1efc6 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 qlite3SelectDest
1efc7 49 6e 69 74 28 26 64 65 73 74 2c 20 30 2c 20 2b Init(&dest, 0, +
1efc8 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a +pParse->nMem);.
1efc9 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
1efca 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 >op==TK_SELECT )
1efcb 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65 {. dest.e
1efcc 44 65 73 74 20 3d 20 53 52 54 5f 4d 65 6d 3b 0a Dest = SRT_Mem;.
1efcd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1efce 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1efcf 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 74 2e 69 50 Null, 0, dest.iP
1efd0 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64 arm);. Vd
1efd1 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 beComment((v, "I
1efd2 6e 69 74 20 73 75 62 71 75 65 72 79 20 72 65 73 nit subquery res
1efd3 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d 65 ult"));. }e
1efd4 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 65 73 lse{. des
1efd5 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 45 78 t.eDest = SRT_Ex
1efd6 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20 73 71 ists;. sq
1efd7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1efd8 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
1efd9 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 , dest.iParm);.
1efda 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 VdbeComme
1efdb 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 45 58 49 nt((v, "Init EXI
1efdc 53 54 53 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 STS result"));.
1efdd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
1efde 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 ite3ExprDelete(p
1efdf 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c 2d Parse->db, pSel-
1efe0 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 >pLimit);.
1efe1 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 pSel->pLimit = s
1efe2 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 qlite3PExpr(pPar
1efe3 73 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 se, TK_INTEGER,
1efe4 30 2c 20 30 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 0, 0, &one);.
1efe5 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65 if( sqlite3Se
1efe6 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 lect(pParse, pSe
1efe7 6c 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 l, &dest) ){.
1efe8 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
1efe9 20 20 20 7d 0a 20 20 20 20 20 20 70 45 78 70 72 }. pExpr
1efea 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 64 65 73 74 ->iColumn = dest
1efeb 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 62 72 .iParm;. br
1efec 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a eak;. }. }..
1efed 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 29 if( testAddr )
1efee 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1efef 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 65 73 eJumpHere(v, tes
1eff0 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d 0a 0a 20 tAddr-1);. }..
1eff1 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 return;.}.#endi
1eff2 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1eff3 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a _SUBQUERY */../*
1eff4 0a 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 61 6e .** Duplicate an
1eff5 20 38 2d 62 79 74 65 20 76 61 6c 75 65 0a 2a 2f 8-byte value.*/
1eff6 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 75 .static char *du
1eff7 70 38 62 79 74 65 73 28 56 64 62 65 20 2a 76 2c p8bytes(Vdbe *v,
1eff8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 29 const char *in)
1eff9 7b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 {. char *out =
1effa 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
1effb 61 77 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 aw(sqlite3VdbeDb
1effc 28 76 29 2c 20 38 29 3b 0a 20 20 69 66 28 20 6f (v), 8);. if( o
1effd 75 74 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 ut ){. memcpy
1effe 28 6f 75 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 (out, in, 8);.
1efff 7d 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a }. return out;.
1f000 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
1f001 65 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e e an instruction
1f002 20 74 68 61 74 20 77 69 6c 6c 20 70 75 74 20 74 that will put t
1f003 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e he floating poin
1f004 74 0a 2a 2a 20 76 61 6c 75 65 20 64 65 73 63 72 t.** value descr
1f005 69 62 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 ibed by z[0..n-1
1f006 5d 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 ] into register
1f007 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 iMem..**.** The
1f008 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 z[] string will
1f009 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 probably not be
1f00a 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e zero-terminated.
1f00b 20 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b But the .** z[
1f00c 6e 5d 20 63 68 61 72 61 63 74 65 72 20 69 73 20 n] character is
1f00d 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 guaranteed to be
1f00e 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 something that
1f00f 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a does not look.**
1f010 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e like the contin
1f011 75 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 uation of the nu
1f012 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mber..*/.static
1f013 76 6f 69 64 20 63 6f 64 65 52 65 61 6c 28 56 64 void codeReal(Vd
1f014 62 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 be *v, const cha
1f015 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 r *z, int n, int
1f016 20 6e 65 67 61 74 65 46 6c 61 67 2c 20 69 6e 74 negateFlag, int
1f017 20 69 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 iMem){. assert
1f018 28 20 7a 20 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73 ( z || v==0 || s
1f019 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2d qlite3VdbeDb(v)-
1f01a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
1f01b 0a 20 20 61 73 73 65 72 74 28 20 21 7a 20 7c 7c . assert( !z ||
1f01c 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 !sqlite3Isdigit
1f01d 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20 55 4e 55 53 (z[n]) );. UNUS
1f01e 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b ED_PARAMETER(n);
1f01f 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 . if( z ){.
1f020 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20 20 double value;.
1f021 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 char *zV;.
1f022 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20 26 sqlite3AtoF(z, &
1f023 76 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20 value);. if(
1f024 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c sqlite3IsNaN(val
1f025 75 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ue) ){. sql
1f026 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1f027 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d , OP_Null, 0, iM
1f028 65 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a em);. }else{.
1f029 20 20 20 20 20 20 69 66 28 20 6e 65 67 61 74 65 if( negate
1f02a 46 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d Flag ) value = -
1f02b 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 value;. zV
1f02c 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28 = dup8bytes(v, (
1f02d 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 char*)&value);.
1f02e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f02f 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61 AddOp4(v, OP_Rea
1f030 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a l, 0, iMem, 0, z
1f031 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 20 V, P4_REAL);.
1f032 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a }. }.}.../*.**
1f033 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 Generate an ins
1f034 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69 truction that wi
1f035 6c 6c 20 70 75 74 20 74 68 65 20 69 6e 74 65 67 ll put the integ
1f036 65 72 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a er describe by.*
1f037 2a 20 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d * text z[0..n-1]
1f038 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 into register i
1f039 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a Mem..**.** The z
1f03a 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 [] string will p
1f03b 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a robably not be z
1f03c 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 ero-terminated.
1f03d 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e But the .** z[n
1f03e 5d 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67 ] character is g
1f03f 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 uaranteed to be
1f040 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 something that d
1f041 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 oes not look.**
1f042 6c 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 like the continu
1f043 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d ation of the num
1f044 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ber..*/.static v
1f045 6f 69 64 20 63 6f 64 65 49 6e 74 65 67 65 72 28 oid codeInteger(
1f046 56 64 62 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 Vdbe *v, Expr *p
1f047 45 78 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 Expr, int negFla
1f048 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 g, int iMem){.
1f049 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 const char *z;.
1f04a 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 if( pExpr->flag
1f04b 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 s & EP_IntValue
1f04c 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 ){. int i = p
1f04d 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 Expr->iTable;.
1f04e 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 if( negFlag )
1f04f 69 20 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 i = -i;. sqli
1f050 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1f051 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 OP_Integer, i,
1f052 69 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 iMem);. }else i
1f053 66 28 20 28 7a 20 3d 20 28 63 68 61 72 2a 29 70 f( (z = (char*)p
1f054 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 29 21 3d Expr->token.z)!=
1f055 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0 ){. int i;.
1f056 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 int n = pExp
1f057 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 r->token.n;.
1f058 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 assert( !sqlite3
1f059 49 73 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b Isdigit(z[n]) );
1f05a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1f05b 47 65 74 49 6e 74 33 32 28 7a 2c 20 26 69 29 20 GetInt32(z, &i)
1f05c 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 ){. if( neg
1f05d 46 6c 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 Flag ) i = -i;.
1f05e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f05f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
1f060 65 67 65 72 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a eger, i, iMem);.
1f061 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 }else if( sq
1f062 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 lite3FitsIn64Bit
1f063 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b s(z, negFlag) ){
1f064 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 . i64 value
1f065 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56 ;. char *zV
1f066 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 ;. sqlite3A
1f067 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 toi64(z, &value)
1f068 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 ;. if( negF
1f069 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 lag ) value = -v
1f06a 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d alue;. zV =
1f06b 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 dup8bytes(v, (c
1f06c 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 har*)&value);.
1f06d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1f06e 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 ddOp4(v, OP_Int6
1f06f 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 4, 0, iMem, 0, z
1f070 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 V, P4_INT64);.
1f071 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 }else{. c
1f072 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 2c odeReal(v, z, n,
1f073 20 6e 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b negFlag, iMem);
1f074 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f . }. }.}.../
1f075 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
1f076 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74 de that will ext
1f077 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75 6d 6e ract the iColumn
1f078 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a -th column from.
1f079 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e ** table pTab an
1f07a 64 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 d store the colu
1f07b 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61 20 72 65 mn value in a re
1f07c 67 69 73 74 65 72 2e 20 20 41 6e 20 65 66 66 6f gister. An effo
1f07d 72 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f rt.** is made to
1f07e 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d store the colum
1f07f 6e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 n value in regis
1f080 74 65 72 20 69 52 65 67 2c 20 62 75 74 20 74 68 ter iReg, but th
1f081 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 is is.** not gua
1f082 72 61 6e 74 65 65 64 2e 20 20 54 68 65 20 6c 6f ranteed. The lo
1f083 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f cation of the co
1f084 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 65 lumn value is re
1f085 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 turned..**.** Th
1f086 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f ere must be an o
1f087 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20 70 54 pen cursor to pT
1f088 61 62 20 69 6e 20 69 54 61 62 6c 65 20 77 68 65 ab in iTable whe
1f089 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a n this routine.*
1f08a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 49 66 * is called. If
1f08b 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 iColumn<0 then
1f08c 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 code is generate
1f08d 64 20 74 68 61 74 20 65 78 74 72 61 63 74 73 20 d that extracts
1f08e 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a the rowid..**.**
1f08f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 This routine mi
1f090 67 68 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72 ght attempt to r
1f091 65 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f euse the value o
1f092 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 f the column tha
1f093 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 t.** has already
1f094 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e 74 been loaded int
1f095 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 54 o a register. T
1f096 68 65 20 76 61 6c 75 65 20 77 69 6c 6c 20 61 6c he value will al
1f097 77 61 79 73 0a 2a 2a 20 62 65 20 75 73 65 64 20 ways.** be used
1f098 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 75 6e if it has not un
1f099 64 65 72 67 6f 6e 65 20 61 6e 79 20 61 66 66 69 dergone any affi
1f09a 6e 69 74 79 20 63 68 61 6e 67 65 73 2e 20 20 42 nity changes. B
1f09b 75 74 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66 69 ut if.** an affi
1f09c 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 20 nity change has
1f09d 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 occurred, then t
1f09e 68 65 20 63 61 63 68 65 64 20 76 61 6c 75 65 20 he cached value
1f09f 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 will only be.**
1f0a0 75 73 65 64 20 69 66 20 61 6c 6c 6f 77 41 66 66 used if allowAff
1f0a1 43 68 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a 2f Chng is true..*/
1f0a2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f0a3 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
1f0a4 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 odeGetColumn(.
1f0a5 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1f0a6 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 /* Parsing and
1f0a7 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 code generating
1f0a8 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 context */. Tab
1f0a9 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a le *pTab, /*
1f0aa 20 44 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 Description of
1f0ab 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 the table we are
1f0ac 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f reading from */
1f0ad 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 . int iColumn,
1f0ae 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
1f0af 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e the table column
1f0b0 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
1f0b1 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 , /* The cu
1f0b2 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f rsor pointing to
1f0b3 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
1f0b4 69 6e 74 20 69 52 65 67 2c 20 20 20 20 20 20 20 int iReg,
1f0b5 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 /* Store result
1f0b6 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 s here */. int
1f0b7 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a 20 allowAffChng /*
1f0b8 54 72 75 65 20 69 66 20 70 72 69 6f 72 20 61 66 True if prior af
1f0b9 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 20 61 finity changes a
1f0ba 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 re OK */.){. Vd
1f0bb 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e be *v = pParse->
1f0bc 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a pVdbe;. int i;.
1f0bd 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 struct yColCac
1f0be 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d he *p;.. for(i=
1f0bf 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 0, p=pParse->aCo
1f0c0 6c 43 61 63 68 65 3b 20 69 3c 70 50 61 72 73 65 lCache; i<pParse
1f0c1 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b ->nColCache; i++
1f0c2 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 , p++){. if(
1f0c3 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c p->iTable==iTabl
1f0c4 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d e && p->iColumn=
1f0c5 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 =iColumn.
1f0c6 20 20 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 && (!p->affC
1f0c7 68 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 hange || allowAf
1f0c8 66 43 68 6e 67 29 20 29 7b 0a 23 69 66 20 30 0a fChng) ){.#if 0.
1f0c9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f0ca 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f eAddOp0(v, OP_No
1f0cb 6f 70 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 op);. VdbeC
1f0cc 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 50 54 3a omment((v, "OPT:
1f0cd 20 74 61 62 25 64 2e 63 6f 6c 25 64 20 2d 3e 20 tab%d.col%d ->
1f0ce 72 25 64 22 2c 20 69 54 61 62 6c 65 2c 20 69 43 r%d", iTable, iC
1f0cf 6f 6c 75 6d 6e 2c 20 70 2d 3e 69 52 65 67 29 29 olumn, p->iReg))
1f0d0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 ;.#endif. r
1f0d1 65 74 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 eturn p->iReg;.
1f0d2 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73 }. } . ass
1f0d3 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 ert( v!=0 );. i
1f0d4 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a f( iColumn<0 ){.
1f0d5 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 28 70 54 int op = (pT
1f0d6 61 62 20 26 26 20 49 73 56 69 72 74 75 61 6c 28 ab && IsVirtual(
1f0d7 70 54 61 62 29 29 20 3f 20 4f 50 5f 56 52 6f 77 pTab)) ? OP_VRow
1f0d8 69 64 20 3a 20 4f 50 5f 52 6f 77 69 64 3b 0a 20 id : OP_Rowid;.
1f0d9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1f0da 64 4f 70 32 28 76 2c 20 6f 70 2c 20 69 54 61 62 dOp2(v, op, iTab
1f0db 6c 65 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c le, iReg);. }el
1f0dc 73 65 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 se if( pTab==0 )
1f0dd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1f0de 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f eAddOp3(v, OP_Co
1f0df 6c 75 6d 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43 lumn, iTable, iC
1f0e0 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 olumn, iReg);.
1f0e1 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6f }else{. int o
1f0e2 70 20 3d 20 49 73 56 69 72 74 75 61 6c 28 70 54 p = IsVirtual(pT
1f0e3 61 62 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e ab) ? OP_VColumn
1f0e4 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 : OP_Column;.
1f0e5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f0e6 4f 70 33 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c Op3(v, op, iTabl
1f0e7 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 e, iColumn, iReg
1f0e8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f );. sqlite3Co
1f0e9 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 lumnDefault(v, p
1f0ea 54 61 62 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 23 Tab, iColumn);.#
1f0eb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f0ec 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
1f0ed 54 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e T. if( pTab->
1f0ee 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 aCol[iColumn].af
1f0ef 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
1f0f0 46 46 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20 20 FF_REAL ){.
1f0f1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f0f2 70 31 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 p1(v, OP_RealAff
1f0f3 69 6e 69 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 inity, iReg);.
1f0f4 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
1f0f5 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73 if( pParse->dis
1f0f6 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 ableColCache==0
1f0f7 29 7b 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73 ){. i = pPars
1f0f8 65 2d 3e 69 43 6f 6c 43 61 63 68 65 3b 0a 20 20 e->iColCache;.
1f0f9 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 p = &pParse->a
1f0fa 43 6f 6c 43 61 63 68 65 5b 69 5d 3b 0a 20 20 20 ColCache[i];.
1f0fb 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 p->iTable = iTa
1f0fc 62 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c ble;. p->iCol
1f0fd 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 umn = iColumn;.
1f0fe 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 p->iReg = iRe
1f0ff 67 3b 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 g;. p->affCha
1f100 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b nge = 0;. i++
1f101 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 41 72 72 ;. if( i>=Arr
1f102 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e 61 aySize(pParse->a
1f103 43 6f 6c 43 61 63 68 65 29 20 29 20 69 20 3d 20 ColCache) ) i =
1f104 30 3b 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 0;. if( i>pPa
1f105 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 20 29 rse->nColCache )
1f106 20 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 pParse->nColCac
1f107 68 65 20 3d 20 69 3b 0a 20 20 20 20 70 50 61 72 he = i;. pPar
1f108 73 65 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 se->iColCache =
1f109 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 i;. }. return
1f10a 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 iReg;.}../*.** C
1f10b 6c 65 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 lear all column
1f10c 63 61 63 68 65 20 65 6e 74 72 69 65 73 20 61 73 cache entries as
1f10d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
1f10e 65 20 76 64 62 65 0a 2a 2a 20 63 75 72 73 6f 72 e vdbe.** cursor
1f10f 20 77 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d with cursor num
1f110 62 65 72 20 69 54 61 62 6c 65 2e 0a 2a 2f 0a 53 ber iTable..*/.S
1f111 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1f112 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c id sqlite3ExprCl
1f113 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 earColumnCache(P
1f114 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
1f115 74 20 69 54 61 62 6c 65 29 7b 0a 20 20 69 66 28 t iTable){. if(
1f116 20 69 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 iTable<0 ){.
1f117 20 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 pParse->nColCac
1f118 68 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 he = 0;. pPar
1f119 73 65 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 se->iColCache =
1f11a 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
1f11b 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 int i;. for(i
1f11c 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 =0; i<pParse->nC
1f11d 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 olCache; i++){.
1f11e 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d if( pParse-
1f11f 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 54 >aColCache[i].iT
1f120 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a able==iTable ){.
1f121 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
1f122 28 20 69 3d 3d 70 50 61 72 73 65 2d 3e 6e 43 6f ( i==pParse->nCo
1f123 6c 43 61 63 68 65 2d 31 20 29 3b 0a 20 20 20 20 lCache-1 );.
1f124 20 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c pParse->aCol
1f125 43 61 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73 Cache[i] = pPars
1f126 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 e->aColCache[--p
1f127 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 Parse->nColCache
1f128 5d 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 ];. pPars
1f129 65 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 e->iColCache = p
1f12a 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 Parse->nColCache
1f12b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1f12c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 }.}../*.** Rec
1f12d 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 ord the fact tha
1f12e 74 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 t an affinity ch
1f12f 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 ange has occurre
1f130 64 20 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a 20 72 d on iCount.** r
1f131 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e egisters startin
1f132 67 20 77 69 74 68 20 69 53 74 61 72 74 2e 0a 2a g with iStart..*
1f133 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f134 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
1f135 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 rCacheAffinityCh
1f136 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 ange(Parse *pPar
1f137 73 65 2c 20 69 6e 74 20 69 53 74 61 72 74 2c 20 se, int iStart,
1f138 69 6e 74 20 69 43 6f 75 6e 74 29 7b 0a 20 20 69 int iCount){. i
1f139 6e 74 20 69 45 6e 64 20 3d 20 69 53 74 61 72 74 nt iEnd = iStart
1f13a 20 2b 20 69 43 6f 75 6e 74 20 2d 20 31 3b 0a 20 + iCount - 1;.
1f13b 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
1f13c 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 0; i<pParse->nCo
1f13d 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 lCache; i++){.
1f13e 20 20 69 6e 74 20 72 20 3d 20 70 50 61 72 73 65 int r = pParse
1f13f 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 ->aColCache[i].i
1f140 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d Reg;. if( r>=
1f141 69 53 74 61 72 74 20 26 26 20 72 3c 3d 69 45 6e iStart && r<=iEn
1f142 64 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 d ){. pPars
1f143 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e e->aColCache[i].
1f144 61 66 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 affChange = 1;.
1f145 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1f146 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
1f147 74 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 to move content
1f148 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 from registers i
1f149 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 From...iFrom+nRe
1f14a 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 g-1.** over to i
1f14b 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 To..iTo+nReg-1.
1f14c 4b 65 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 Keep the column
1f14d 63 61 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 cache up-to-date
1f14e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f14f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f150 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 ExprCodeMove(Par
1f151 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1f152 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 iFrom, int iTo,
1f153 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 int nReg){. int
1f154 20 69 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d 3d i;. if( iFrom=
1f155 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 =iTo ) return;.
1f156 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f157 70 33 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 p3(pParse->pVdbe
1f158 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d , OP_Move, iFrom
1f159 2c 20 69 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 , iTo, nReg);.
1f15a 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 for(i=0; i<pPars
1f15b 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b e->nColCache; i+
1f15c 2b 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 +){. int x =
1f15d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 pParse->aColCach
1f15e 65 5b 69 5d 2e 69 52 65 67 3b 0a 20 20 20 20 69 e[i].iReg;. i
1f15f 66 28 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 f( x>=iFrom && x
1f160 3c 69 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 <iFrom+nReg ){.
1f161 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f pParse->aCo
1f162 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 20 2b lCache[i].iReg +
1f163 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 = iTo-iFrom;.
1f164 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
1f165 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1f166 20 63 6f 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 copy content fr
1f167 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 72 om registers iFr
1f168 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d om...iFrom+nReg-
1f169 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 1.** over to iTo
1f16a 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f ..iTo+nReg-1..*/
1f16b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f16c 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1f16d 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65 20 2a CodeCopy(Parse *
1f16e 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f pParse, int iFro
1f16f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 m, int iTo, int
1f170 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a nReg){. int i;.
1f171 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f if( iFrom==iTo
1f172 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 ) return;. for
1f173 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b (i=0; i<nReg; i+
1f174 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 +){. sqlite3V
1f175 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 dbeAddOp2(pParse
1f176 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 ->pVdbe, OP_Copy
1f177 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 , iFrom+i, iTo+i
1f178 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
1f179 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 Return true if a
1f17a 6e 79 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 ny register in t
1f17b 68 65 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e he range iFrom..
1f17c 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a iTo (inclusive).
1f17d 2a 2a 20 69 73 20 75 73 65 64 20 61 73 20 70 61 ** is used as pa
1f17e 72 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e rt of the column
1f17f 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 cache..*/.stati
1f180 63 20 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 c int usedAsColu
1f181 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 mnCache(Parse *p
1f182 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d Parse, int iFrom
1f183 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e , int iTo){. in
1f184 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
1f185 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 i<pParse->nColCa
1f186 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 che; i++){. i
1f187 6e 74 20 72 20 3d 20 70 50 61 72 73 65 2d 3e 61 nt r = pParse->a
1f188 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 ColCache[i].iReg
1f189 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72 ;. if( r>=iFr
1f18a 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 om && r<=iTo ) r
1f18b 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 eturn 1;. }. r
1f18c 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
1f18d 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 61 6c * There is a val
1f18e 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 ue in register i
1f18f 52 65 67 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 72 Reg..**.** We ar
1f190 65 20 67 6f 69 6e 67 20 74 6f 20 6d 6f 64 69 66 e going to modif
1f191 79 20 74 68 65 20 76 61 6c 75 65 2c 20 73 6f 20 y the value, so
1f192 77 65 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 we need to make
1f193 73 75 72 65 20 69 74 0a 2a 2a 20 69 73 20 6e 6f sure it.** is no
1f194 74 20 61 20 63 61 63 68 65 64 20 72 65 67 69 73 t a cached regis
1f195 74 65 72 2e 20 20 49 66 20 69 52 65 67 20 69 73 ter. If iReg is
1f196 20 61 20 63 61 63 68 65 64 20 72 65 67 69 73 74 a cached regist
1f197 65 72 2c 0a 2a 2a 20 74 68 65 6e 20 63 6c 65 61 er,.** then clea
1f198 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 r the correspond
1f199 69 6e 67 20 63 61 63 68 65 20 6c 69 6e 65 2e 0a ing cache line..
1f19a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f19b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
1f19c 70 72 57 72 69 74 61 62 6c 65 52 65 67 69 73 74 prWritableRegist
1f19d 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 er(Parse *pParse
1f19e 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 , int iReg){. i
1f19f 6e 74 20 69 3b 0a 20 20 69 66 28 20 75 73 65 64 nt i;. if( used
1f1a0 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 AsColumnCache(pP
1f1a1 61 72 73 65 2c 20 69 52 65 67 2c 20 69 52 65 67 arse, iReg, iReg
1f1a2 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ) ){. for(i=0
1f1a3 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c ; i<pParse->nCol
1f1a4 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 Cache; i++){.
1f1a5 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 if( pParse->a
1f1a6 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 ColCache[i].iReg
1f1a7 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 ==iReg ){.
1f1a8 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 pParse->aColCa
1f1a9 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d che[i] = pParse-
1f1aa 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61 >aColCache[--pPa
1f1ab 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b rse->nColCache];
1f1ac 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d . pParse-
1f1ad 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61 >iColCache = pPa
1f1ae 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a rse->nColCache;.
1f1af 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1f1b0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 }.}../*.** If th
1f1b1 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 e last instructi
1f1b2 6f 6e 20 63 6f 64 65 64 20 69 73 20 61 6e 20 65 on coded is an e
1f1b3 70 68 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66 phemeral copy of
1f1b4 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 72 any of.** the r
1f1b5 65 67 69 73 74 65 72 73 20 69 6e 20 74 68 65 20 egisters in the
1f1b6 6e 52 65 67 20 72 65 67 69 73 74 65 72 73 20 62 nReg registers b
1f1b7 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 52 eginning with iR
1f1b8 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 eg, then.** conv
1f1b9 65 72 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 ert the last ins
1f1ba 74 72 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f 50 truction from OP
1f1bb 5f 53 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f 70 _SCopy to OP_Cop
1f1bc 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
1f1bd 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1f1be 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 3ExprHardCopy(Pa
1f1bf 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
1f1c0 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 iReg, int nReg)
1f1c1 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 {. int addr;.
1f1c2 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 VdbeOp *pOp;. V
1f1c3 64 62 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 70 dbe *v;.. v = p
1f1c4 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 Parse->pVdbe;.
1f1c5 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 addr = sqlite3Vd
1f1c6 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 beCurrentAddr(v)
1f1c7 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 ;. pOp = sqlite
1f1c8 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 61 64 3VdbeGetOp(v, ad
1f1c9 64 72 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28 dr-1);. assert(
1f1ca 20 70 4f 70 20 7c 7c 20 70 50 61 72 73 65 2d 3e pOp || pParse->
1f1cb 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1f1cc 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 20 26 26 );. if( pOp &&
1f1cd 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1f1ce 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70 _SCopy && pOp->p
1f1cf 31 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e 1>=iReg && pOp->
1f1d0 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a p1<iReg+nReg ){.
1f1d1 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 pOp->opcode
1f1d2 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d = OP_Copy;. }.}
1f1d3 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1f1d4 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 code to store t
1f1d5 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1f1d6 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 iAlias-th alias
1f1d7 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 in register.** t
1f1d8 61 72 67 65 74 2e 20 20 54 68 65 20 66 69 72 73 arget. The firs
1f1d9 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 t time this is c
1f1da 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20 alled, pExpr is
1f1db 65 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d evaluated to com
1f1dc 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 pute.** the valu
1f1dd 65 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20 e of the alias.
1f1de 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 The value is st
1f1df 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c ored in an auxil
1f1e0 69 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a iary register.**
1f1e1 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 and the number
1f1e2 6f 66 20 74 68 61 74 20 72 65 67 69 73 74 65 72 of that register
1f1e3 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f is returned. O
1f1e4 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c n subsequent cal
1f1e5 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 ls,.** the regis
1f1e6 74 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65 ter number is re
1f1e7 74 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67 turned without g
1f1e8 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f enerating any co
1f1e9 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 de..**.** Note t
1f1ea 68 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 hat in order for
1f1eb 20 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63 this to work, c
1f1ec 6f 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65 ode must be gene
1f1ed 72 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 rated in the.**
1f1ee 73 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20 same order that
1f1ef 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a it is executed..
1f1f0 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72 **.** Aliases ar
1f1f1 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 e numbered start
1f1f2 69 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20 ing with 1. So
1f1f3 69 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65 iAlias is in the
1f1f4 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74 range.** of 1 t
1f1f5 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 o pParse->nAlias
1f1f6 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a inclusive. .**
1f1f7 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 .** pParse->aAli
1f1f8 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63 as[iAlias-1] rec
1f1f9 6f 72 64 73 20 74 68 65 20 72 65 67 69 73 74 65 ords the registe
1f1fa 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 r number where t
1f1fb 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 he value.** of t
1f1fc 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 he iAlias-th ali
1f1fd 61 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49 as is stored. I
1f1fe 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 f zero, that mea
1f1ff 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 ns that the.** a
1f200 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74 lias has not yet
1f201 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a been computed..
1f202 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f */.static int co
1f203 64 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70 deAlias(Parse *p
1f204 50 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61 Parse, int iAlia
1f205 73 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 s, Expr *pExpr,
1f206 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 73 int target){. s
1f207 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1f208 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 rse->db;. int i
1f209 52 65 67 3b 0a 20 20 69 66 28 20 70 50 61 72 73 Reg;. if( pPars
1f20a 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 e->nAliasAlloc<p
1f20b 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 7b Parse->nAlias ){
1f20c 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c . pParse->aAl
1f20d 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 52 ias = sqlite3DbR
1f20e 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c eallocOrFree(db,
1f20f 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 2c pParse->aAlias,
1f210 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1f211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f212 20 20 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d sizeof(pParse-
1f213 3e 61 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 >aAlias[0])*pPar
1f214 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 se->nAlias );.
1f215 20 20 74 65 73 74 63 61 73 65 28 20 64 62 2d 3e testcase( db->
1f216 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 mallocFailed &&
1f217 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c pParse->nAliasAl
1f218 6c 6f 63 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 loc>0 );. if(
1f219 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1f21a 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 d ) return 0;.
1f21b 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 memset(&pParse
1f21c 2d 3e 61 41 6c 69 61 73 5b 70 50 61 72 73 65 2d ->aAlias[pParse-
1f21d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 >nAliasAlloc], 0
1f21e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 70 50 ,. (pP
1f21f 61 72 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 arse->nAlias-pPa
1f220 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 rse->nAliasAlloc
1f221 29 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d )*sizeof(pParse-
1f222 3e 61 41 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 20 >aAlias[0]));.
1f223 20 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 pParse->nAlias
1f224 41 6c 6c 6f 63 20 3d 20 70 50 61 72 73 65 2d 3e Alloc = pParse->
1f225 6e 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 61 73 nAlias;. }. as
1f226 73 65 72 74 28 20 69 41 6c 69 61 73 3e 30 20 26 sert( iAlias>0 &
1f227 26 20 69 41 6c 69 61 73 3c 3d 70 50 61 72 73 65 & iAlias<=pParse
1f228 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 69 52 ->nAlias );. iR
1f229 65 67 20 3d 20 70 50 61 72 73 65 2d 3e 61 41 6c eg = pParse->aAl
1f22a 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 ias[iAlias-1];.
1f22b 20 69 66 28 20 69 52 65 67 3d 3d 30 20 29 7b 0a if( iReg==0 ){.
1f22c 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e if( pParse->
1f22d 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 20 disableColCache
1f22e 29 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 ){. iReg =
1f22f 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1f230 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 arget(pParse, pE
1f231 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 xpr, target);.
1f232 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
1f233 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e Reg = ++pParse->
1f234 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 nMem;. sqli
1f235 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 te3ExprCode(pPar
1f236 73 65 2c 20 70 45 78 70 72 2c 20 69 52 65 67 29 se, pExpr, iReg)
1f237 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e ;. pParse->
1f238 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d aAlias[iAlias-1]
1f239 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 = iReg;. }.
1f23a 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 }. return iReg
1f23b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 ;.}../*.** Gener
1f23c 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 ate code into th
1f23d 65 20 63 75 72 72 65 6e 74 20 56 64 62 65 20 74 e current Vdbe t
1f23e 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 67 o evaluate the g
1f23f 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 iven.** expressi
1f240 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20 on. Attempt to
1f241 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1f242 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 22 74 s in register "t
1f243 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65 74 75 72 arget"..** Retur
1f244 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 77 n the register w
1f245 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 here results are
1f246 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 stored..**.** W
1f247 69 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ith this routine
1f248 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 67 75 , there is no gu
1f249 61 72 61 6e 74 65 65 20 74 68 61 74 20 72 65 73 arantee that res
1f24a 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 ults will.** be
1f24b 73 74 6f 72 65 64 20 69 6e 20 74 61 72 67 65 74 stored in target
1f24c 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 69 . The result mi
1f24d 67 68 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e ght be stored in
1f24e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 72 some other.** r
1f24f 65 67 69 73 74 65 72 20 69 66 20 69 74 20 69 73 egister if it is
1f250 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 convenient to d
1f251 6f 20 73 6f 2e 20 20 54 68 65 20 63 61 6c 6c 69 o so. The calli
1f252 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d ng function.** m
1f253 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 ust check the re
1f254 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 6d 6f turn code and mo
1f255 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 74 ve the results t
1f256 6f 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a o the desired.**
1f257 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 register..*/.SQ
1f258 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1f259 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1f25a 54 61 72 67 65 74 28 50 61 72 73 65 20 2a 70 50 Target(Parse *pP
1f25b 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
1f25c 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a r, int target){.
1f25d 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
1f25e 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 54 se->pVdbe; /* T
1f25f 68 65 20 56 4d 20 75 6e 64 65 72 20 63 6f 6e 73 he VM under cons
1f260 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e truction */. in
1f261 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 t op;
1f262 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f /* The o
1f263 70 63 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65 pcode being code
1f264 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 65 67 d */. int inReg
1f265 20 3d 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 = target;
1f266 20 2f 2a 20 52 65 73 75 6c 74 73 20 73 74 6f 72 /* Results stor
1f267 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 ed in register i
1f268 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65 nReg */. int re
1f269 67 46 72 65 65 31 20 3d 20 30 3b 20 20 20 20 20 gFree1 = 0;
1f26a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 /* If non-ze
1f26b 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d ro free this tem
1f26c 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 porary register
1f26d 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 */. int regFree
1f26e 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2 = 0; /
1f26f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 * If non-zero fr
1f270 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 ee this temporar
1f271 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 y register */.
1f272 69 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 int r1, r2, r3,
1f273 72 34 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72 r4; /* Var
1f274 69 6f 75 73 20 72 65 67 69 73 74 65 72 20 6e 75 ious register nu
1f275 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69 74 mbers */. sqlit
1f276 65 33 20 2a 64 62 3b 0a 0a 20 20 64 62 20 3d 20 e3 *db;.. db =
1f277 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 pParse->db;. as
1f278 73 65 72 74 28 20 76 21 3d 30 20 7c 7c 20 64 62 sert( v!=0 || db
1f279 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1f27a 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67 ;. assert( targ
1f27b 65 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d et>0 && target<=
1f27c 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a pParse->nMem );.
1f27d 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 if( v==0 ) ret
1f27e 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 45 urn 0;.. if( pE
1f27f 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70 xpr==0 ){. op
1f280 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 = TK_NULL;. }e
1f281 6c 73 65 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 lse{. op = pE
1f282 78 70 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 xpr->op;. }. s
1f283 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 witch( op ){.
1f284 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c case TK_AGG_COL
1f285 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 UMN: {. Agg
1f286 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d Info *pAggInfo =
1f287 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f pExpr->pAggInfo
1f288 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 41 ;. struct A
1f289 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c ggInfo_col *pCol
1f28a 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 = &pAggInfo->aC
1f28b 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 3b ol[pExpr->iAgg];
1f28c 0a 20 20 20 20 20 20 69 66 28 20 21 70 41 67 67 . if( !pAgg
1f28d 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 Info->directMode
1f28e 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1f28f 72 74 28 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 rt( pCol->iMem>0
1f290 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 );. inRe
1f291 67 20 3d 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a g = pCol->iMem;.
1f292 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1f293 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
1f294 41 67 67 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 AggInfo->useSort
1f295 69 6e 67 49 64 78 20 29 7b 0a 20 20 20 20 20 20 ingIdx ){.
1f296 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f297 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e Op3(v, OP_Column
1f298 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 , pAggInfo->sort
1f299 69 6e 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 ingIdx,.
1f29a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f29b 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 pCol->iSor
1f29c 74 65 72 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 terColumn, targe
1f29d 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 t);. brea
1f29e 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
1f29f 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 /* Otherwise, f
1f2a0 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 all thru into th
1f2a1 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 e TK_COLUMN case
1f2a2 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 */. }. ca
1f2a3 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a se TK_COLUMN: {.
1f2a4 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
1f2a5 3e 69 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 >iTable<0 ){.
1f2a6 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c /* This onl
1f2a7 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 y happens when c
1f2a8 6f 64 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 oding check cons
1f2a9 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 traints */.
1f2aa 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 assert( pPars
1f2ab 65 2d 3e 63 6b 42 61 73 65 3e 30 20 29 3b 0a 20 e->ckBase>0 );.
1f2ac 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 inReg = p
1f2ad 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 Expr->iColumn +
1f2ae 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 3b 0a pParse->ckBase;.
1f2af 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1f2b0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 testcase( (
1f2b1 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 pExpr->flags & E
1f2b2 50 5f 41 6e 79 41 66 66 29 21 3d 30 20 29 3b 0a P_AnyAff)!=0 );.
1f2b3 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 inReg =
1f2b4 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 sqlite3ExprCodeG
1f2b5 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c etColumn(pParse,
1f2b6 20 70 45 78 70 72 2d 3e 70 54 61 62 2c 0a 20 20 pExpr->pTab,.
1f2b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f2b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1f2b9 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 Expr->iColumn, p
1f2ba 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 Expr->iTable, ta
1f2bb 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 rget,.
1f2bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f2bd 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c pExpr->fl
1f2be 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 ags & EP_AnyAff)
1f2bf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1f2c0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1f2c1 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 case TK_INTEGER
1f2c2 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 65 49 6e : {. codeIn
1f2c3 74 65 67 65 72 28 76 2c 20 70 45 78 70 72 2c 20 teger(v, pExpr,
1f2c4 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
1f2c5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1f2c6 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 case TK_FLOAT
1f2c7 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 65 52 65 : {. codeRe
1f2c8 61 6c 28 76 2c 20 28 63 68 61 72 2a 29 70 45 78 al(v, (char*)pEx
1f2c9 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 pr->token.z, pEx
1f2ca 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 30 2c 20 pr->token.n, 0,
1f2cb 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 target);. b
1f2cc 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1f2cd 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 case TK_STRING:
1f2ce 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
1f2cf 65 71 75 6f 74 65 45 78 70 72 28 70 45 78 70 72 equoteExpr(pExpr
1f2d0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f2d1 56 64 62 65 41 64 64 4f 70 34 28 76 2c 4f 50 5f VdbeAddOp4(v,OP_
1f2d2 53 74 72 69 6e 67 38 2c 20 30 2c 20 74 61 72 67 String8, 0, targ
1f2d3 65 74 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 et, 0,.
1f2d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
1f2d5 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b char*)pExpr->tok
1f2d6 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b en.z, pExpr->tok
1f2d7 65 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 en.n);. bre
1f2d8 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1f2d9 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 se TK_NULL: {.
1f2da 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1f2db 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c ddOp2(v, OP_Null
1f2dc 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 0, target);.
1f2dd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1f2de 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f2df 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
1f2e0 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c L. case TK_BL
1f2e1 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 OB: {. int
1f2e2 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 n;. const c
1f2e3 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68 har *z;. ch
1f2e4 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 ar *zBlob;.
1f2e5 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1f2e6 74 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 token.n>=3 );.
1f2e7 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
1f2e8 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 r->token.z[0]=='
1f2e9 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 6b x' || pExpr->tok
1f2ea 65 6e 2e 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a en.z[0]=='X' );.
1f2eb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
1f2ec 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d xpr->token.z[1]=
1f2ed 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 61 ='\'' );. a
1f2ee 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f ssert( pExpr->to
1f2ef 6b 65 6e 2e 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b ken.z[pExpr->tok
1f2f0 65 6e 2e 6e 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b en.n-1]=='\'' );
1f2f1 0a 20 20 20 20 20 20 6e 20 3d 20 70 45 78 70 72 . n = pExpr
1f2f2 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20 33 3b 0a 20 ->token.n - 3;.
1f2f3 20 20 20 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 z = (char*)
1f2f4 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b pExpr->token.z +
1f2f5 20 32 3b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 20 2;. zBlob
1f2f6 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c = sqlite3HexToBl
1f2f7 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 ob(sqlite3VdbeDb
1f2f8 28 76 29 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 (v), z, n);.
1f2f9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f2fa 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 Op4(v, OP_Blob,
1f2fb 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30 2c 20 n/2, target, 0,
1f2fc 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41 4d 49 zBlob, P4_DYNAMI
1f2fd 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b C);. break;
1f2fe 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1f2ff 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 case TK_VARIAB
1f300 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 LE: {. int
1f301 69 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 56 64 iPrior;. Vd
1f302 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 beOp *pOp;.
1f303 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 if( pExpr->toke
1f304 6e 2e 6e 3c 3d 31 0a 20 20 20 20 20 20 20 20 20 n.n<=1.
1f305 26 26 20 28 69 50 72 69 6f 72 20 3d 20 73 71 6c && (iPrior = sql
1f306 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 ite3VdbeCurrentA
1f307 64 64 72 28 76 29 2d 31 29 3e 3d 30 0a 20 20 20 ddr(v)-1)>=0.
1f308 20 20 20 20 20 20 26 26 20 28 70 4f 70 20 3d 20 && (pOp =
1f309 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 sqlite3VdbeGetOp
1f30a 28 76 2c 20 69 50 72 69 6f 72 29 29 2d 3e 6f 70 (v, iPrior))->op
1f30b 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c code==OP_Variabl
1f30c 65 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f e. && pO
1f30d 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d 3d 70 p->p1+pOp->p3==p
1f30e 45 78 70 72 2d 3e 69 54 61 62 6c 65 0a 20 20 20 Expr->iTable.
1f30f 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 32 && pOp->p2
1f310 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 67 65 74 +pOp->p3==target
1f311 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 . && pOp
1f312 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 20 20 20 ->p4.z==0.
1f313 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 ){. /* If
1f314 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e the previous in
1f315 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 20 struction was a
1f316 63 6f 70 79 20 6f 66 20 74 68 65 20 70 72 65 76 copy of the prev
1f317 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a 20 20 20 ious unnamed.
1f318 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 ** paramete
1f319 72 20 69 6e 74 6f 20 74 68 65 20 70 72 65 76 69 r into the previ
1f31a 6f 75 73 20 72 65 67 69 73 74 65 72 2c 20 74 68 ous register, th
1f31b 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63 72 65 6d en simply increm
1f31c 65 6e 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 ent the.
1f31d 2a 2a 20 72 65 70 65 61 74 20 63 6f 75 6e 74 20 ** repeat count
1f31e 6f 6e 20 74 68 65 20 70 72 69 6f 72 20 69 6e 73 on the prior ins
1f31f 74 72 75 63 74 69 6f 6e 20 72 61 74 68 65 72 20 truction rather
1f320 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61 20 6e 65 than making a ne
1f321 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 w. ** ins
1f322 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 truction..
1f323 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 70 */. pOp
1f324 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 ->p3++;. }e
1f325 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
1f326 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1f327 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 70 , OP_Variable, p
1f328 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 Expr->iTable, ta
1f329 72 67 65 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 rget, 1);.
1f32a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b if( pExpr->tok
1f32b 65 6e 2e 6e 3e 31 20 29 7b 0a 20 20 20 20 20 20 en.n>1 ){.
1f32c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1f32d 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 hangeP4(v, -1, (
1f32e 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b char*)pExpr->tok
1f32f 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b en.z, pExpr->tok
1f330 65 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d en.n);. }
1f331 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
1f332 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1f333 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52 case TK_REGISTER
1f334 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 : {. inReg
1f335 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b = pExpr->iTable;
1f336 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1f337 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1f338 41 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 AS: {. inRe
1f339 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 50 g = codeAlias(pP
1f33a 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 arse, pExpr->iTa
1f33b 62 6c 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 ble, pExpr->pLef
1f33c 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 t, target);.
1f33d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1f33e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f33f 49 54 5f 43 41 53 54 0a 20 20 20 20 63 61 73 65 IT_CAST. case
1f340 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20 20 20 20 TK_CAST: {.
1f341 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 /* Expressions
1f342 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 20 of the form:
1f343 43 41 53 54 28 70 4c 65 66 74 20 41 53 20 74 6f CAST(pLeft AS to
1f344 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e ken) */. in
1f345 74 20 61 66 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20 t aff, to_op;.
1f346 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 inReg = sqli
1f347 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 te3ExprCodeTarge
1f348 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d t(pParse, pExpr-
1f349 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b >pLeft, target);
1f34a 0a 20 20 20 20 20 20 61 66 66 20 3d 20 73 71 6c . aff = sql
1f34b 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 ite3AffinityType
1f34c 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b (&pExpr->token);
1f34d 0a 20 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 . to_op = a
1f34e 66 66 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f ff - SQLITE_AFF_
1f34f 54 45 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 TEXT + OP_ToText
1f350 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f351 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 to_op==OP_ToText
1f352 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 || aff!=SQLI
1f353 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29 TE_AFF_TEXT )
1f354 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f355 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 to_op==OP_ToBlob
1f356 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 || aff!=SQLI
1f357 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 TE_AFF_NONE )
1f358 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f359 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 to_op==OP_ToNume
1f35a 72 69 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 ric || aff!=SQLI
1f35b 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 TE_AFF_NUMERIC )
1f35c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f35d 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 to_op==OP_ToInt
1f35e 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 || aff!=SQLI
1f35f 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29 TE_AFF_INTEGER )
1f360 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f361 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c to_op==OP_ToReal
1f362 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 || aff!=SQLI
1f363 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29 TE_AFF_REAL )
1f364 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f365 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 ( to_op==OP_ToTe
1f366 78 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 xt );. test
1f367 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f case( to_op==OP_
1f368 54 6f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 ToBlob );.
1f369 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d testcase( to_op=
1f36a 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b =OP_ToNumeric );
1f36b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1f36c 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 to_op==OP_ToInt
1f36d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f36e 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f se( to_op==OP_To
1f36f 52 65 61 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 Real );. if
1f370 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 ( inReg!=target
1f371 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1f372 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1f373 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c OP_SCopy, inReg,
1f374 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1f375 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 inReg = target
1f376 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1f377 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f378 31 28 76 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 1(v, to_op, inRe
1f379 67 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 g);. testca
1f37a 73 65 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e se( usedAsColumn
1f37b 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e Cache(pParse, in
1f37c 52 65 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 Reg, inReg) );.
1f37d 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1f37e 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
1f37f 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 nge(pParse, inRe
1f380 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 g, 1);. bre
1f381 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ak;. }.#endif
1f382 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1f383 43 41 53 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 CAST */. case
1f384 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 TK_LT:. case
1f385 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 TK_LE:. case
1f386 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 TK_GT:. case
1f387 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 TK_GE:. case
1f388 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 TK_NE:. case
1f389 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 TK_EQ: {.
1f38a 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f assert( TK_LT==O
1f38b 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 P_Lt );. as
1f38c 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f sert( TK_LE==OP_
1f38d 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Le );. asse
1f38e 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 rt( TK_GT==OP_Gt
1f38f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1f390 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 ( TK_GE==OP_Ge )
1f391 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f392 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a TK_EQ==OP_Eq );.
1f393 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1f394 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 _NE==OP_Ne );.
1f395 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1f396 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 ==TK_LT );.
1f397 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1f398 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 K_LE );. te
1f399 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 stcase( op==TK_G
1f39a 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
1f39b 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 ase( op==TK_GE )
1f39c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f39d 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 ( op==TK_EQ );.
1f39e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1f39f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 p==TK_NE );.
1f3a0 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 codeCompareOpe
1f3a1 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 rands(pParse, pE
1f3a2 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c xpr->pLeft, &r1,
1f3a3 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 ®Free1,.
1f3a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f3a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
1f3a6 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 xpr->pRight, &r2
1f3a7 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
1f3a8 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 codeCompare(
1f3a9 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1f3aa 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 Left, pExpr->pRi
1f3ab 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 ght, op,.
1f3ac 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 r1, r
1f3ad 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 2, inReg, SQLITE
1f3ae 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 _STOREP2);.
1f3af 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
1f3b0 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee1==0 );.
1f3b1 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1f3b2 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 e2==0 );. b
1f3b3 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1f3b4 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20 case TK_AND:.
1f3b5 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20 case TK_OR:.
1f3b6 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20 case TK_PLUS:.
1f3b7 20 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a case TK_STAR:
1f3b8 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e . case TK_MIN
1f3b9 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f US:. case TK_
1f3ba 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b REM:. case TK
1f3bb 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 63 61 73 _BITAND:. cas
1f3bc 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20 e TK_BITOR:.
1f3bd 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20 case TK_SLASH:.
1f3be 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 48 49 46 case TK_LSHIF
1f3bf 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 T:. case TK_R
1f3c0 53 48 49 46 54 3a 20 0a 20 20 20 20 63 61 73 65 SHIFT: . case
1f3c1 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20 TK_CONCAT: {.
1f3c2 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 41 assert( TK_A
1f3c3 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20 ND==OP_And );.
1f3c4 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4f assert( TK_O
1f3c5 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20 R==OP_Or );.
1f3c6 20 20 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55 assert( TK_PLU
1f3c7 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20 S==OP_Add );.
1f3c8 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49 assert( TK_MI
1f3c9 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74 NUS==OP_Subtract
1f3ca 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1f3cb 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d ( TK_REM==OP_Rem
1f3cc 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 20 20 20 ainder );.
1f3cd 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41 4e assert( TK_BITAN
1f3ce 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a D==OP_BitAnd );.
1f3cf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1f3d0 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72 _BITOR==OP_BitOr
1f3d1 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1f3d2 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44 ( TK_SLASH==OP_D
1f3d3 69 76 69 64 65 20 29 3b 0a 20 20 20 20 20 20 61 ivide );. a
1f3d4 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 49 46 54 ssert( TK_LSHIFT
1f3d5 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29 ==OP_ShiftLeft )
1f3d6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f3d7 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68 TK_RSHIFT==OP_Sh
1f3d8 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20 iftRight );.
1f3d9 20 20 61 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e assert( TK_CON
1f3da 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29 CAT==OP_Concat )
1f3db 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f3dc 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a ( op==TK_AND );.
1f3dd 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1f3de 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20 op==TK_OR );.
1f3df 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1f3e0 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20 =TK_PLUS );.
1f3e1 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1f3e2 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20 TK_MINUS );.
1f3e3 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1f3e4 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20 20 20 20 TK_REM );.
1f3e5 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1f3e6 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20 20 20 20 _BITAND );.
1f3e7 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1f3e8 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20 20 20 20 K_BITOR );.
1f3e9 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1f3ea 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20 20 20 20 K_SLASH );.
1f3eb 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1f3ec 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20 20 20 20 K_LSHIFT );.
1f3ed 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1f3ee 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a 20 20 20 TK_RSHIFT );.
1f3ef 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1f3f0 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20 =TK_CONCAT );.
1f3f1 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 r1 = sqlite3
1f3f2 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 ExprCodeTemp(pPa
1f3f3 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1f3f4 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 t, ®Free1);.
1f3f5 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 r2 = sqlite
1f3f6 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1f3f7 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 arse, pExpr->pRi
1f3f8 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b ght, ®Free2);
1f3f9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1f3fa 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 beAddOp3(v, op,
1f3fb 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b r2, r1, target);
1f3fc 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1f3fd 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
1f3fe 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1f3ff 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 regFree2==0 );.
1f400 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1f401 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d }. case TK_UM
1f402 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 45 78 INUS: {. Ex
1f403 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 pr *pLeft = pExp
1f404 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 r->pLeft;.
1f405 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b assert( pLeft );
1f406 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 . if( pLeft
1f407 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 ->op==TK_FLOAT )
1f408 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 52 65 {. codeRe
1f409 61 6c 28 76 2c 20 28 63 68 61 72 2a 29 70 4c 65 al(v, (char*)pLe
1f40a 66 74 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 4c 65 ft->token.z, pLe
1f40b 66 74 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 31 2c 20 ft->token.n, 1,
1f40c 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d target);. }
1f40d 65 6c 73 65 20 69 66 28 20 70 4c 65 66 74 2d 3e else if( pLeft->
1f40e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 op==TK_INTEGER )
1f40f 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 49 6e {. codeIn
1f410 74 65 67 65 72 28 76 2c 20 70 4c 65 66 74 2c 20 teger(v, pLeft,
1f411 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 1, target);.
1f412 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1f413 20 72 65 67 46 72 65 65 31 20 3d 20 72 31 20 3d regFree1 = r1 =
1f414 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
1f415 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 eg(pParse);.
1f416 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1f417 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
1f418 67 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20 ger, 0, r1);.
1f419 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 r2 = sqlite
1f41a 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1f41b 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
1f41c 66 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a ft, ®Free2);.
1f41d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1f41e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
1f41f 53 75 62 74 72 61 63 74 2c 20 72 32 2c 20 72 31 Subtract, r2, r1
1f420 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1f421 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1f422 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 Free2==0 );.
1f423 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67 20 }. inReg
1f424 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 = target;.
1f425 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1f426 20 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54 3a case TK_BITNOT:
1f427 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 . case TK_NOT
1f428 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 : {. assert
1f429 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f ( TK_BITNOT==OP_
1f42a 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 BitNot );.
1f42b 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d 3d assert( TK_NOT==
1f42c 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 OP_Not );.
1f42d 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1f42e 5f 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20 20 20 _BITNOT );.
1f42f 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1f430 4b 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 72 K_NOT );. r
1f431 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 1 = sqlite3ExprC
1f432 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
1f433 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 pExpr->pLeft, &r
1f434 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 egFree1);.
1f435 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1f436 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 e1==0 );. i
1f437 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 nReg = target;.
1f438 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f439 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 AddOp2(v, op, r1
1f43a 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 , inReg);.
1f43b 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1f43c 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a case TK_ISNULL:
1f43d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 . case TK_NOT
1f43e 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e NULL: {. in
1f43f 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61 73 t addr;. as
1f440 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d sert( TK_ISNULL=
1f441 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 =OP_IsNull );.
1f442 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e assert( TK_N
1f443 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 OTNULL==OP_NotNu
1f444 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ll );. test
1f445 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e case( op==TK_ISN
1f446 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ULL );. tes
1f447 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f tcase( op==TK_NO
1f448 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 TNULL );. s
1f449 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1f44a 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
1f44b 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 1, target);.
1f44c 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 r1 = sqlite3Ex
1f44d 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
1f44e 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
1f44f 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 ®Free1);.
1f450 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1f451 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
1f452 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 addr = sqlite3
1f453 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 6f 70 VdbeAddOp1(v, op
1f454 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c , r1);. sql
1f455 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1f456 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 61 72 , OP_AddImm, tar
1f457 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 get, -1);.
1f458 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
1f459 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 ere(v, addr);.
1f45a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1f45b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 . case TK_AGG
1f45c 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 _FUNCTION: {.
1f45d 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 AggInfo *pInf
1f45e 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 o = pExpr->pAggI
1f45f 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 nfo;. if( p
1f460 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Info==0 ){.
1f461 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1f462 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 sg(pParse, "misu
1f463 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a se of aggregate:
1f464 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 20 20 %T",.
1f465 20 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b &pExpr->span);
1f466 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1f467 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 49 inReg = pI
1f468 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78 70 72 nfo->aFunc[pExpr
1f469 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20 ->iAgg].iMem;.
1f46a 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
1f46b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1f46c 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a e TK_CONST_FUNC:
1f46d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e . case TK_FUN
1f46e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 CTION: {. E
1f46f 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d xprList *pList =
1f470 20 28 0a 20 20 20 20 20 20 20 20 45 78 70 72 48 (. ExprH
1f471 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 asAnyProperty(pE
1f472 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c xpr, EP_TokenOnl
1f473 79 7c 45 50 5f 53 70 61 6e 4f 6e 6c 79 29 20 3f y|EP_SpanOnly) ?
1f474 20 30 20 3a 20 70 45 78 70 72 2d 3e 78 2e 70 4c 0 : pExpr->x.pL
1f475 69 73 74 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 ist. );.
1f476 20 20 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70 int nExpr = p
1f477 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 List ? pList->nE
1f478 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 46 xpr : 0;. F
1f479 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 uncDef *pDef;.
1f47a 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 int nId;.
1f47b 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1f47c 49 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f Id;. int co
1f47d 6e 73 74 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 nstMask = 0;.
1f47e 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
1f47f 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 u8 enc = ENC(db)
1f480 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 ;. CollSeq
1f481 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 0a 20 20 20 *pColl = 0;..
1f482 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
1f483 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
1f484 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
1f485 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f486 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 se( op==TK_CONST
1f487 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 20 20 74 _FUNC );. t
1f488 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1f489 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 FUNCTION );.
1f48a 20 20 7a 49 64 20 3d 20 28 63 68 61 72 2a 29 70 zId = (char*)p
1f48b 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 Expr->token.z;.
1f48c 20 20 20 20 20 6e 49 64 20 3d 20 70 45 78 70 72 nId = pExpr
1f48d 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20 ->token.n;.
1f48e 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 pDef = sqlite3F
1f48f 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 indFunction(db,
1f490 7a 49 64 2c 20 6e 49 64 2c 20 6e 45 78 70 72 2c zId, nId, nExpr,
1f491 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 enc, 0);.
1f492 61 73 73 65 72 74 28 20 70 44 65 66 21 3d 30 20 assert( pDef!=0
1f493 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 );. if( pLi
1f494 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 45 st ){. nE
1f495 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 xpr = pList->nEx
1f496 70 72 3b 0a 20 20 20 20 20 20 20 20 72 31 20 3d pr;. r1 =
1f497 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
1f498 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 45 78 ange(pParse, nEx
1f499 70 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c pr);. sql
1f49a 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 ite3ExprCodeExpr
1f49b 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4c 69 List(pParse, pLi
1f49c 73 74 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 st, r1, 1);.
1f49d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1f49e 20 6e 45 78 70 72 20 3d 20 72 31 20 3d 20 30 3b nExpr = r1 = 0;
1f49f 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 . }.#ifndef
1f4a0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1f4a1 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 TUALTABLE.
1f4a2 2f 2a 20 50 6f 73 73 69 62 6c 79 20 6f 76 65 72 /* Possibly over
1f4a3 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f load the functio
1f4a4 6e 20 69 66 20 74 68 65 20 66 69 72 73 74 20 61 n if the first a
1f4a5 72 67 75 6d 65 6e 74 20 69 73 0a 20 20 20 20 20 rgument is.
1f4a6 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 74 61 ** a virtual ta
1f4a7 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 ble column..
1f4a8 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f **. ** Fo
1f4a9 72 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e r infix function
1f4aa 73 20 28 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 s (LIKE, GLOB, R
1f4ab 45 47 45 58 50 2c 20 61 6e 64 20 4d 41 54 43 48 EGEXP, and MATCH
1f4ac 29 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 ) use the.
1f4ad 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 ** second argume
1f4ae 6e 74 2c 20 6e 6f 74 20 74 68 65 20 66 69 72 73 nt, not the firs
1f4af 74 2c 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 t, as the argume
1f4b0 6e 74 20 74 6f 20 74 65 73 74 20 74 6f 0a 20 20 nt to test to.
1f4b1 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74 ** see if it
1f4b2 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 is a column in
1f4b3 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e a virtual table.
1f4b4 20 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 This is done b
1f4b5 65 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 ecause. **
1f4b6 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 the left operand
1f4b7 20 6f 66 20 69 6e 66 69 78 20 66 75 6e 63 74 69 of infix functi
1f4b8 6f 6e 73 20 28 74 68 65 20 6f 70 65 72 61 6e 64 ons (the operand
1f4b9 20 77 65 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 we want to.
1f4ba 20 20 2a 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 ** control ove
1f4bb 72 6c 6f 61 64 69 6e 67 29 20 65 6e 64 73 20 75 rloading) ends u
1f4bc 70 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 p as the second
1f4bd 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a argument to the.
1f4be 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f ** functio
1f4bf 6e 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 69 n. The expressi
1f4c0 6f 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 69 73 on "A glob B" is
1f4c1 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a equivalent to .
1f4c2 20 20 20 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42 ** "glob(B
1f4c3 2c 41 29 2e 20 20 57 65 20 77 61 6e 74 20 74 6f ,A). We want to
1f4c4 20 75 73 65 20 74 68 65 20 41 20 69 6e 20 22 41 use the A in "A
1f4c5 20 67 6c 6f 62 20 42 22 20 74 6f 20 74 65 73 74 glob B" to test
1f4c6 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 66 75 . ** for fu
1f4c7 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 nction overloadi
1f4c8 6e 67 2e 20 20 42 75 74 20 77 65 20 75 73 65 20 ng. But we use
1f4c9 74 68 65 20 42 20 74 65 72 6d 20 69 6e 20 22 67 the B term in "g
1f4ca 6c 6f 62 28 42 2c 41 29 22 2e 0a 20 20 20 20 20 lob(B,A)"..
1f4cb 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 45 */. if( nE
1f4cc 78 70 72 3e 3d 32 20 26 26 20 28 70 45 78 70 72 xpr>=2 && (pExpr
1f4cd 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 ->flags & EP_Inf
1f4ce 69 78 46 75 6e 63 29 20 29 7b 0a 20 20 20 20 20 ixFunc) ){.
1f4cf 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 pDef = sqlite
1f4d0 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 3VtabOverloadFun
1f4d1 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 ction(db, pDef,
1f4d2 6e 45 78 70 72 2c 20 70 4c 69 73 74 2d 3e 61 5b nExpr, pList->a[
1f4d3 31 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 1].pExpr);.
1f4d4 20 7d 65 6c 73 65 20 69 66 28 20 6e 45 78 70 72 }else if( nExpr
1f4d5 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 >0 ){. pD
1f4d6 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 ef = sqlite3Vtab
1f4d7 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e OverloadFunction
1f4d8 28 64 62 2c 20 70 44 65 66 2c 20 6e 45 78 70 72 (db, pDef, nExpr
1f4d9 2c 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 , pList->a[0].pE
1f4da 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 xpr);. }.#e
1f4db 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72 28 69 ndif. for(i
1f4dc 3d 30 3b 20 69 3c 6e 45 78 70 72 20 26 26 20 69 =0; i<nExpr && i
1f4dd 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 <32; i++){.
1f4de 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 if( sqlite3Ex
1f4df 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 4c 69 prIsConstant(pLi
1f4e0 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 st->a[i].pExpr)
1f4e1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e ){. con
1f4e2 73 74 4d 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 stMask |= (1<<i)
1f4e3 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1f4e4 20 20 20 20 69 66 28 20 28 70 44 65 66 2d 3e 66 if( (pDef->f
1f4e5 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
1f4e6 4e 43 5f 4e 45 45 44 43 4f 4c 4c 29 21 3d 30 20 NC_NEEDCOLL)!=0
1f4e7 26 26 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 && !pColl ){.
1f4e8 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 pColl = s
1f4e9 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 qlite3ExprCollSe
1f4ea 71 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d q(pParse, pList-
1f4eb 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 >a[i].pExpr);.
1f4ec 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1f4ed 20 20 20 20 20 20 69 66 28 20 70 44 65 66 2d 3e if( pDef->
1f4ee 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 flags & SQLITE_F
1f4ef 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a UNC_NEEDCOLL ){.
1f4f0 20 20 20 20 20 20 20 20 69 66 28 20 21 70 43 6f if( !pCo
1f4f1 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d ll ) pColl = db-
1f4f2 3e 70 44 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 >pDfltColl; .
1f4f3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f4f4 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c AddOp4(v, OP_Col
1f4f5 6c 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 lSeq, 0, 0, 0, (
1f4f6 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 char *)pColl, P4
1f4f7 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 _COLLSEQ);.
1f4f8 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1f4f9 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1f4fa 5f 46 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 _Function, const
1f4fb 4d 61 73 6b 2c 20 72 31 2c 20 74 61 72 67 65 74 Mask, r1, target
1f4fc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1f4fd 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a (char*
1f4fe 29 70 44 65 66 2c 20 50 34 5f 46 55 4e 43 44 45 )pDef, P4_FUNCDE
1f4ff 46 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 F);. sqlite
1f500 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 3VdbeChangeP5(v,
1f501 20 28 75 38 29 6e 45 78 70 72 29 3b 0a 20 20 20 (u8)nExpr);.
1f502 20 20 20 69 66 28 20 6e 45 78 70 72 20 29 7b 0a if( nExpr ){.
1f503 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 sqlite3R
1f504 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 eleaseTempRange(
1f505 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 45 78 70 pParse, r1, nExp
1f506 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 r);. }.
1f507 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
1f508 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
1f509 28 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 45 78 (pParse, r1, nEx
1f50a 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b pr);. break
1f50b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
1f50c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1f50d 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b UERY. case TK
1f50e 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 73 _EXISTS:. cas
1f50f 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 e TK_SELECT: {.
1f510 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1f511 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a p==TK_EXISTS );.
1f512 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1f513 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b op==TK_SELECT );
1f514 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 . if( pExpr
1f515 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a ->iColumn==0 ){.
1f516 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 sqlite3C
1f517 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 odeSubselect(pPa
1f518 72 73 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30 rse, pExpr, 0, 0
1f519 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1f51a 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e inReg = pExpr->
1f51b 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 62 iColumn;. b
1f51c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1f51d 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 case TK_IN: {.
1f51e 20 20 20 20 69 6e 74 20 72 4e 6f 74 46 6f 75 6e int rNotFoun
1f51f 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 d = 0;. int
1f520 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 rMayHaveNull =
1f521 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 32 2c 0;. int j2,
1f522 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20 20 j3, j4, j5;.
1f523 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 char affinity
1f524 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 54 79 70 ;. int eTyp
1f525 65 3b 0a 0a 20 20 20 20 20 20 56 64 62 65 4e 6f e;.. VdbeNo
1f526 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62 opComment((v, "b
1f527 65 67 69 6e 20 49 4e 20 65 78 70 72 20 72 25 64 egin IN expr r%d
1f528 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20 20 ", target));.
1f529 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74 eType = sqlit
1f52a 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70 50 e3FindInIndex(pP
1f52b 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 4d arse, pExpr, &rM
1f52c 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 ayHaveNull);.
1f52d 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e if( rMayHaveN
1f52e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ull ){. r
1f52f 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 NotFound = ++pPa
1f530 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 rse->nMem;.
1f531 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 67 }.. /* Fig
1f532 75 72 65 20 6f 75 74 20 74 68 65 20 61 66 66 69 ure out the affi
1f533 6e 69 74 79 20 74 6f 20 75 73 65 20 74 6f 20 63 nity to use to c
1f534 72 65 61 74 65 20 61 20 6b 65 79 20 66 72 6f 6d reate a key from
1f535 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 the results.
1f536 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 ** of the exp
1f537 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 6e 69 74 ression. affinit
1f538 79 53 74 72 20 73 74 6f 72 65 73 20 61 20 73 74 yStr stores a st
1f539 61 74 69 63 20 73 74 72 69 6e 67 20 73 75 69 74 atic string suit
1f53a 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 20 20 2a able for. *
1f53b 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 * P4 of OP_MakeR
1f53c 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a ecord.. */.
1f53d 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 3d affinity =
1f53e 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e comparisonAffin
1f53f 69 74 79 28 70 45 78 70 72 29 3b 0a 0a 0a 20 20 ity(pExpr);...
1f540 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 /* Code the
1f541 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22 3c 65 78 <expr> from "<ex
1f542 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22 2e 20 54 pr> IN (...)". T
1f543 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 he temporary tab
1f544 6c 65 0a 20 20 20 20 20 20 2a 2a 20 70 45 78 70 le. ** pExp
1f545 72 2d 3e 69 54 61 62 6c 65 20 63 6f 6e 74 61 69 r->iTable contai
1f546 6e 73 20 74 68 65 20 76 61 6c 75 65 73 20 74 68 ns the values th
1f547 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 28 at make up the (
1f548 2e 2e 2e 29 20 73 65 74 2e 0a 20 20 20 20 20 20 ...) set..
1f549 2a 2f 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d */. pParse-
1f54a 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 >disableColCache
1f54b 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ++;. sqlite
1f54c 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 3ExprCode(pParse
1f54d 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
1f54e 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 70 target);. p
1f54f 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f Parse->disableCo
1f550 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 20 lCache--;.
1f551 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j2 = sqlite3Vdbe
1f552 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e AddOp1(v, OP_IsN
1f553 75 6c 6c 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 ull, target);.
1f554 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 if( eType==I
1f555 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b N_INDEX_ROWID ){
1f556 0a 20 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 . j3 = sq
1f557 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
1f558 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c v, OP_MustBeInt,
1f559 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1f55a 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 j4 = sqlite3Vd
1f55b 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e beAddOp3(v, OP_N
1f55c 6f 74 45 78 69 73 74 73 2c 20 70 45 78 70 72 2d otExists, pExpr-
1f55d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 74 61 72 67 >iTable, 0, targ
1f55e 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c et);. sql
1f55f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1f560 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c , OP_Integer, 1,
1f561 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1f562 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 j5 = sqlite3Vd
1f563 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 beAddOp0(v, OP_G
1f564 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 oto);. sq
1f565 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
1f566 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 e(v, j3);.
1f567 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1f568 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 pHere(v, j4);.
1f569 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f56a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
1f56b 74 65 67 65 72 2c 20 30 2c 20 74 61 72 67 65 74 teger, 0, target
1f56c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1f56d 20 20 20 20 20 20 20 20 72 32 20 3d 20 72 65 67 r2 = reg
1f56e 46 72 65 65 32 20 3d 20 73 71 6c 69 74 65 33 47 Free2 = sqlite3G
1f56f 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 etTempReg(pParse
1f570 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 );.. /* C
1f571 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 61 reate a record a
1f572 6e 64 20 74 65 73 74 20 66 6f 72 20 73 65 74 20 nd test for set
1f573 6d 65 6d 62 65 72 73 68 69 70 2e 20 49 66 20 74 membership. If t
1f574 68 65 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 0a he set contains.
1f575 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76 ** the v
1f576 61 6c 75 65 2c 20 74 68 65 6e 20 6a 75 6d 70 20 alue, then jump
1f577 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 to the end of th
1f578 65 20 74 65 73 74 20 63 6f 64 65 2e 20 54 68 65 e test code. The
1f579 20 74 61 72 67 65 74 0a 20 20 20 20 20 20 20 20 target.
1f57a 2a 2a 20 72 65 67 69 73 74 65 72 20 73 74 69 6c ** register stil
1f57b 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 l contains the t
1f57c 72 75 65 20 28 31 29 20 76 61 6c 75 65 20 77 72 rue (1) value wr
1f57d 69 74 74 65 6e 20 74 6f 20 69 74 20 65 61 72 6c itten to it earl
1f57e 69 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ier.. */.
1f57f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1f580 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1f581 4d 61 6b 65 52 65 63 6f 72 64 2c 20 74 61 72 67 MakeRecord, targ
1f582 65 74 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69 et, 1, r2, &affi
1f583 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 nity, 1);.
1f584 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f585 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
1f586 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 r, 1, target);.
1f587 20 20 20 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 j5 = sqli
1f588 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
1f589 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 OP_Found, pExpr
1f58a 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 32 29 ->iTable, 0, r2)
1f58b 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 ;.. /* If
1f58c 20 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 the set members
1f58d 68 69 70 20 74 65 73 74 20 66 61 69 6c 73 2c 20 hip test fails,
1f58e 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 then the result
1f58f 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 of the .
1f590 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e 29 22 20 ** "x IN (...)"
1f591 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 expression must
1f592 62 65 20 65 69 74 68 65 72 20 30 20 6f 72 20 4e be either 0 or N
1f593 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 65 74 0a ULL. If the set.
1f594 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 ** conta
1f595 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 ins no NULL valu
1f596 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 es, then the res
1f597 75 6c 74 20 69 73 20 30 2e 20 49 66 20 74 68 65 ult is 0. If the
1f598 20 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a set . **
1f599 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 contains one or
1f59a 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c 75 65 more NULL value
1f59b 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 s, then the resu
1f59c 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 lt of the.
1f59d 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 ** expression
1f59e 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 is also NULL..
1f59f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1f5a0 20 69 66 28 20 72 4e 6f 74 46 6f 75 6e 64 3d 3d if( rNotFound==
1f5a1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 0 ){. /
1f5a2 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 * This branch ru
1f5a3 6e 73 20 69 66 20 69 74 20 69 73 20 6b 6e 6f 77 ns if it is know
1f5a4 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d n at compile tim
1f5a5 65 20 28 6e 6f 77 29 20 74 68 61 74 20 0a 20 20 e (now) that .
1f5a6 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 ** the s
1f5a7 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e et contains no N
1f5a8 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54 68 69 73 ULL values. This
1f5a9 20 68 61 70 70 65 6e 73 20 61 73 20 74 68 65 20 happens as the
1f5aa 72 65 73 75 6c 74 0a 20 20 20 20 20 20 20 20 20 result.
1f5ab 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20 4e 55 ** of a "NOT NU
1f5ac 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 LL" constraint i
1f5ad 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 n the database s
1f5ae 63 68 65 6d 61 2e 20 4e 6f 20 6e 65 65 64 0a 20 chema. No need.
1f5af 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 ** to t
1f5b0 65 73 74 20 74 68 65 20 64 61 74 61 20 73 74 72 est the data str
1f5b1 75 63 74 75 72 65 20 61 74 20 72 75 6e 74 69 6d ucture at runtim
1f5b2 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a e in this case..
1f5b3 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
1f5b4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1f5b5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
1f5b6 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72 67 65 nteger, 0, targe
1f5b7 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 t);. }els
1f5b8 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e{. /*
1f5b9 54 68 69 73 20 62 6c 6f 63 6b 20 70 6f 70 75 6c This block popul
1f5ba 61 74 65 73 20 74 68 65 20 72 4e 6f 74 46 6f 75 ates the rNotFou
1f5bb 6e 64 20 72 65 67 69 73 74 65 72 20 77 69 74 68 nd register with
1f5bc 20 65 69 74 68 65 72 20 4e 55 4c 4c 0a 20 20 20 either NULL.
1f5bd 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 30 20 28 ** or 0 (
1f5be 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 an integer value
1f5bf 29 2e 20 49 66 20 74 68 65 20 64 61 74 61 20 73 ). If the data s
1f5c0 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
1f5c1 73 20 6f 6e 65 0a 20 20 20 20 20 20 20 20 20 20 s one.
1f5c2 2a 2a 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 73 ** or more NULLs
1f5c3 2c 20 74 68 65 6e 20 73 65 74 20 72 4e 6f 74 46 , then set rNotF
1f5c4 6f 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e 20 4f 74 ound to NULL. Ot
1f5c5 68 65 72 77 69 73 65 2c 20 73 65 74 20 69 74 0a herwise, set it.
1f5c6 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 ** to
1f5c7 30 2e 20 49 66 20 72 65 67 69 73 74 65 72 20 72 0. If register r
1f5c8 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 61 MayHaveNull is a
1f5c9 6c 72 65 61 64 79 20 73 65 74 20 74 6f 20 73 6f lready set to so
1f5ca 6d 65 20 76 61 6c 75 65 0a 20 20 20 20 20 20 20 me value.
1f5cb 20 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e ** other than
1f5cc 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 NULL, then the
1f5cd 74 65 73 74 20 68 61 73 20 61 6c 72 65 61 64 79 test has already
1f5ce 20 62 65 65 6e 20 72 75 6e 20 61 6e 64 20 0a 20 been run and .
1f5cf 20 20 20 20 20 20 20 20 20 2a 2a 20 72 4e 6f 74 ** rNot
1f5d0 46 6f 75 6e 64 20 69 73 20 61 6c 72 65 61 64 79 Found is already
1f5d1 20 70 6f 70 75 6c 61 74 65 64 2e 0a 20 20 20 20 populated..
1f5d2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1f5d3 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
1f5d4 63 68 61 72 20 6e 75 6c 6c 52 65 63 6f 72 64 5b char nullRecord[
1f5d5 5d 20 3d 20 7b 20 30 78 30 32 2c 20 30 78 30 30 ] = { 0x02, 0x00
1f5d6 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 33 };. j3
1f5d7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
1f5d8 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 dOp1(v, OP_NotNu
1f5d9 6c 6c 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c ll, rMayHaveNull
1f5da 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1f5db 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1f5dc 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4e , OP_Null, 0, rN
1f5dd 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 otFound);.
1f5de 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1f5df 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 ddOp4(v, OP_Blob
1f5e0 2c 20 32 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c , 2, rMayHaveNul
1f5e1 6c 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 l, 0, .
1f5e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5e3 20 20 20 20 6e 75 6c 6c 52 65 63 6f 72 64 2c 20 nullRecord,
1f5e4 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 P4_STATIC);.
1f5e5 20 20 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 j4 = sqlit
1f5e6 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
1f5e7 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d OP_Found, pExpr-
1f5e8 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 4d 61 79 >iTable, 0, rMay
1f5e9 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 HaveNull);.
1f5ea 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5eb 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
1f5ec 65 67 65 72 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 eger, 0, rNotFou
1f5ed 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 nd);. s
1f5ee 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
1f5ef 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 re(v, j4);.
1f5f0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5f1 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b JumpHere(v, j3);
1f5f2 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 .. /* C
1f5f3 6f 70 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66 opy the value of
1f5f4 20 72 65 67 69 73 74 65 72 20 72 4e 6f 74 46 6f register rNotFo
1f5f5 75 6e 64 20 28 77 68 69 63 68 20 69 73 20 65 69 und (which is ei
1f5f6 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 30 29 0a ther NULL or 0).
1f5f7 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 ** int
1f5f8 6f 20 74 68 65 20 74 61 72 67 65 74 20 72 65 67 o the target reg
1f5f9 69 73 74 65 72 2e 20 54 68 69 73 20 77 69 6c 6c ister. This will
1f5fa 20 62 65 20 74 68 65 20 72 65 73 75 6c 74 20 6f be the result o
1f5fb 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 f the.
1f5fc 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 ** expression..
1f5fd 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
1f5fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f5ff 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f eAddOp2(v, OP_Co
1f600 70 79 2c 20 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 py, rNotFound, t
1f601 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 arget);.
1f602 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
1f603 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
1f604 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 ere(v, j2);.
1f605 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1f606 70 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20 pHere(v, j5);.
1f607 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 VdbeComment(
1f608 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72 (v, "end IN expr
1f609 20 72 25 64 22 2c 20 74 61 72 67 65 74 29 29 3b r%d", target));
1f60a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1f60b 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f }.#endif. /
1f60c 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 20 42 45 *. ** x BE
1f60d 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 TWEEN y AND z.
1f60e 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 **. ** This
1f60f 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 is equivalent t
1f610 6f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 o. **. **
1f611 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a x>=y AND x<=z
1f612 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 . **. ** X
1f613 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 is stored in pE
1f614 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 xpr->pLeft..
1f615 2a 2a 20 59 20 69 73 20 73 74 6f 72 65 64 20 69 ** Y is stored i
1f616 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e n pExpr->pList->
1f617 61 5b 30 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 a[0].pExpr..
1f618 2a 2a 20 5a 20 69 73 20 73 74 6f 72 65 64 20 69 ** Z is stored i
1f619 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e n pExpr->pList->
1f61a 61 5b 31 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 a[1].pExpr..
1f61b 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 */. case TK_B
1f61c 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 ETWEEN: {.
1f61d 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 Expr *pLeft = pE
1f61e 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 xpr->pLeft;.
1f61f 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
1f620 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d t_item *pLItem =
1f621 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d pExpr->x.pList-
1f622 3e 61 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a >a;. Expr *
1f623 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d pRight = pLItem-
1f624 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 20 63 >pExpr;.. c
1f625 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e odeCompareOperan
1f626 64 73 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 ds(pParse, pLeft
1f627 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 , &r1, ®Free1
1f628 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1f629 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f62a 20 20 20 20 70 52 69 67 68 74 2c 20 26 72 32 2c pRight, &r2,
1f62b 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 ®Free2);.
1f62c 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1f62d 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
1f62e 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
1f62f 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree2==0 );.
1f630 20 72 33 20 3d 20 73 71 6c 69 74 65 33 47 65 74 r3 = sqlite3Get
1f631 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b TempReg(pParse);
1f632 0a 20 20 20 20 20 20 72 34 20 3d 20 73 71 6c 69 . r4 = sqli
1f633 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
1f634 61 72 73 65 29 3b 0a 20 20 20 20 20 20 63 6f 64 arse);. cod
1f635 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c eCompare(pParse,
1f636 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 pLeft, pRight,
1f637 4f 50 5f 47 65 2c 0a 20 20 20 20 20 20 20 20 20 OP_Ge,.
1f638 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c r1, r2,
1f639 20 72 33 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 r3, SQLITE_STOR
1f63a 45 50 32 29 3b 0a 20 20 20 20 20 20 70 4c 49 74 EP2);. pLIt
1f63b 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 70 52 69 67 em++;. pRig
1f63c 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 ht = pLItem->pEx
1f63d 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 pr;. sqlite
1f63e 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1f63f 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 pParse, regFree2
1f640 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 );. r2 = sq
1f641 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
1f642 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 p(pParse, pRight
1f643 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
1f644 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
1f645 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 gFree2==0 );.
1f646 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 codeCompare(p
1f647 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 Parse, pLeft, pR
1f648 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c ight, OP_Le, r1,
1f649 20 72 32 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f r2, r4, SQLITE_
1f64a 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 STOREP2);.
1f64b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f64c 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c 3(v, OP_And, r3,
1f64d 20 72 34 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 r4, target);.
1f64e 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 sqlite3Relea
1f64f 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 seTempReg(pParse
1f650 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 73 71 6c , r3);. sql
1f651 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
1f652 65 67 28 70 50 61 72 73 65 2c 20 72 34 29 3b 0a eg(pParse, r4);.
1f653 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1f654 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 }. case TK_U
1f655 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e PLUS: {. in
1f656 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 Reg = sqlite3Exp
1f657 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 rCodeTarget(pPar
1f658 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
1f659 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1f65a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 break;. }..
1f65b 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 /*. ** For
1f65c 6d 20 41 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 m A:. ** CA
1f65d 53 45 20 78 20 57 48 45 4e 20 65 31 20 54 48 45 SE x WHEN e1 THE
1f65e 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45 N r1 WHEN e2 THE
1f65f 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e N r2 ... WHEN eN
1f660 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 THEN rN ELSE y
1f661 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a END. **. *
1f662 2a 20 46 6f 72 6d 20 42 3a 0a 20 20 20 20 2a 2a * Form B:. **
1f663 20 20 20 43 41 53 45 20 57 48 45 4e 20 65 31 20 CASE WHEN e1
1f664 54 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20 THEN r1 WHEN e2
1f665 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e THEN r2 ... WHEN
1f666 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 eN THEN rN ELSE
1f667 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 y END. **.
1f668 20 20 2a 2a 20 46 6f 72 6d 20 41 20 69 73 20 63 ** Form A is c
1f669 61 6e 20 62 65 20 74 72 61 6e 73 66 6f 72 6d 65 an be transforme
1f66a 64 20 69 6e 74 6f 20 74 68 65 20 65 71 75 69 76 d into the equiv
1f66b 61 6c 65 6e 74 20 66 6f 72 6d 20 42 20 61 73 20 alent form B as
1f66c 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 20 follows:. **
1f66d 20 20 43 41 53 45 20 57 48 45 4e 20 78 3d 65 31 CASE WHEN x=e1
1f66e 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 78 3d THEN r1 WHEN x=
1f66f 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 0a 20 e2 THEN r2 ....
1f670 20 20 20 2a 2a 20 20 20 20 20 20 20 20 57 48 45 ** WHE
1f671 4e 20 78 3d 65 4e 20 54 48 45 4e 20 72 4e 20 45 N x=eN THEN rN E
1f672 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a LSE y END. **
1f673 0a 20 20 20 20 2a 2a 20 58 20 28 69 66 20 69 74 . ** X (if it
1f674 20 65 78 69 73 74 73 29 20 69 73 20 69 6e 20 70 exists) is in p
1f675 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 Expr->pLeft..
1f676 20 2a 2a 20 59 20 69 73 20 69 6e 20 70 45 78 70 ** Y is in pExp
1f677 72 2d 3e 70 52 69 67 68 74 2e 20 20 54 68 65 20 r->pRight. The
1f678 59 20 69 73 20 61 6c 73 6f 20 6f 70 74 69 6f 6e Y is also option
1f679 61 6c 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 al. If there is
1f67a 20 6e 6f 0a 20 20 20 20 2a 2a 20 45 4c 53 45 20 no. ** ELSE
1f67b 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 20 6f 74 clause and no ot
1f67c 68 65 72 20 74 65 72 6d 20 6d 61 74 63 68 65 73 her term matches
1f67d 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c , then the resul
1f67e 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 t of the. **
1f67f 65 78 70 72 73 73 69 6f 6e 20 69 73 20 4e 55 4c exprssion is NUL
1f680 4c 2e 0a 20 20 20 20 2a 2a 20 45 69 20 69 73 20 L.. ** Ei is
1f681 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d in pExpr->pList-
1f682 3e 61 5b 69 2a 32 5d 20 61 6e 64 20 52 69 20 69 >a[i*2] and Ri i
1f683 73 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e s pExpr->pList->
1f684 61 5b 69 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a a[i*2+1].. **
1f685 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 75 . ** The resu
1f686 6c 74 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 lt of the expres
1f687 73 69 6f 6e 20 69 73 20 74 68 65 20 52 69 20 66 sion is the Ri f
1f688 6f 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 or the first mat
1f689 63 68 69 6e 67 20 45 69 2c 0a 20 20 20 20 2a 2a ching Ei,. **
1f68a 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73 20 or if there is
1f68b 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45 69 2c 20 no matching Ei,
1f68c 74 68 65 20 45 4c 53 45 20 74 65 72 6d 20 59 2c the ELSE term Y,
1f68d 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73 0a or if there is.
1f68e 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53 45 20 74 ** no ELSE t
1f68f 65 72 6d 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a erm, NULL.. *
1f690 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 41 /. case TK_CA
1f691 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 SE: {. int
1f692 65 6e 64 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 endLabel;
1f693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f694 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 GOTO label for
1f695 65 6e 64 20 6f 66 20 43 41 53 45 20 73 74 6d 74 end of CASE stmt
1f696 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 */. int ne
1f697 78 74 43 61 73 65 3b 20 20 20 20 20 20 20 20 20 xtCase;
1f698 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 /* G
1f699 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65 OTO label for ne
1f69a 78 74 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a xt WHEN clause *
1f69b 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70 /. int nExp
1f69c 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
1f69d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 78 20 /* 2x
1f69e 6e 75 6d 62 65 72 20 6f 66 20 57 48 45 4e 20 74 number of WHEN t
1f69f 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e erms */. in
1f6a0 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
1f6a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6a2 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1f6a3 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 */. ExprLis
1f6a4 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 t *pEList;
1f6a5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 /* Li
1f6a6 73 74 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 st of WHEN terms
1f6a7 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 */. struct
1f6a8 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1f6a9 61 4c 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 aListelem; /* A
1f6aa 72 72 61 79 20 6f 66 20 57 48 45 4e 20 74 65 72 rray of WHEN ter
1f6ab 6d 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 ms */. Expr
1f6ac 20 6f 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 opCompare;
1f6ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f6ae 20 54 68 65 20 58 3d 3d 45 69 20 65 78 70 72 65 The X==Ei expre
1f6af 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 ssion */. E
1f6b0 78 70 72 20 63 61 63 68 65 58 3b 20 20 20 20 20 xpr cacheX;
1f6b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6b2 20 2f 2a 20 43 61 63 68 65 64 20 65 78 70 72 65 /* Cached expre
1f6b3 73 73 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20 ssion X */.
1f6b4 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20 Expr *pX;
1f6b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6b6 20 20 20 2f 2a 20 54 68 65 20 58 20 65 78 70 72 /* The X expr
1f6b7 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 ession */.
1f6b8 45 78 70 72 20 2a 70 54 65 73 74 20 3d 20 30 3b Expr *pTest = 0;
1f6b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6ba 20 20 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d /* X==Ei (form
1f6bb 20 41 29 20 6f 72 20 6a 75 73 74 20 45 69 20 28 A) or just Ei (
1f6bc 66 6f 72 6d 20 42 29 20 2a 2f 0a 0a 20 20 20 20 form B) */..
1f6bd 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
1f6be 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
1f6bf 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
1f6c0 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 && pExpr->x.pLis
1f6c1 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 t );. asser
1f6c2 74 28 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 t((pExpr->x.pLis
1f6c3 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20 3d 3d t->nExpr % 2) ==
1f6c4 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0);. asser
1f6c5 74 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 t(pExpr->x.pList
1f6c6 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 20 ->nExpr > 0);.
1f6c7 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 pEList = pEx
1f6c8 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 pr->x.pList;.
1f6c9 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20 70 aListelem = p
1f6ca 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 EList->a;.
1f6cb 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e nExpr = pEList->
1f6cc 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 65 6e 64 nExpr;. end
1f6cd 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 56 Label = sqlite3V
1f6ce 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b dbeMakeLabel(v);
1f6cf 0a 20 20 20 20 20 20 69 66 28 20 28 70 58 20 3d . if( (pX =
1f6d0 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 21 3d pExpr->pLeft)!=
1f6d1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 63 0 ){. cac
1f6d2 68 65 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20 20 heX = *pX;.
1f6d3 20 20 20 74 65 73 74 63 61 73 65 28 20 70 58 2d testcase( pX-
1f6d4 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c >op==TK_COLUMN |
1f6d5 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 | pX->op==TK_REG
1f6d6 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 ISTER );.
1f6d7 20 63 61 63 68 65 58 2e 69 54 61 62 6c 65 20 3d cacheX.iTable =
1f6d8 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1f6d9 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2c Temp(pParse, pX,
1f6da 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 ®Free1);.
1f6db 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1f6dc 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
1f6dd 20 20 20 20 20 20 63 61 63 68 65 58 2e 6f 70 20 cacheX.op
1f6de 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 = TK_REGISTER;.
1f6df 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 opCompare
1f6e0 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 .op = TK_EQ;.
1f6e1 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 opCompare.p
1f6e2 4c 65 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0a Left = &cacheX;.
1f6e3 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 pTest =
1f6e4 26 6f 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 &opCompare;.
1f6e5 20 20 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65 }. pParse
1f6e6 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 ->disableColCach
1f6e7 65 2b 2b 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 e++;. for(i
1f6e8 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 =0; i<nExpr; i=i
1f6e9 2b 32 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 +2){. if(
1f6ea 20 70 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 pX ){.
1f6eb 20 61 73 73 65 72 74 28 20 70 54 65 73 74 21 3d assert( pTest!=
1f6ec 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 0 );. o
1f6ed 70 43 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20 pCompare.pRight
1f6ee 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 = aListelem[i].p
1f6ef 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65 Expr;. }e
1f6f0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 lse{. p
1f6f1 54 65 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d Test = aListelem
1f6f2 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [i].pExpr;.
1f6f3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78 }. nex
1f6f4 74 43 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56 tCase = sqlite3V
1f6f5 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b dbeMakeLabel(v);
1f6f6 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
1f6f7 65 28 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b e( pTest->op==TK
1f6f8 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 54 65 73 74 _COLUMN || pTest
1f6f9 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 ->op==TK_REGISTE
1f6fa 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c R );. sql
1f6fb 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
1f6fc 70 50 61 72 73 65 2c 20 70 54 65 73 74 2c 20 6e pParse, pTest, n
1f6fd 65 78 74 43 61 73 65 2c 20 53 51 4c 49 54 45 5f extCase, SQLITE_
1f6fe 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 JUMPIFNULL);.
1f6ff 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 testcase( a
1f700 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 Listelem[i+1].pE
1f701 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 xpr->op==TK_COLU
1f702 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 MN );. te
1f703 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65 stcase( aListele
1f704 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 m[i+1].pExpr->op
1f705 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b ==TK_REGISTER );
1f706 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1f707 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c ExprCode(pParse,
1f708 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e aListelem[i+1].
1f709 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a pExpr, target);.
1f70a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1f70b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1f70c 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62 65 Goto, 0, endLabe
1f70d 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 l);. sqli
1f70e 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
1f70f 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 73 65 29 bel(v, nextCase)
1f710 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1f711 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 if( pExpr->pRigh
1f712 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c t ){. sql
1f713 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
1f714 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 rse, pExpr->pRig
1f715 68 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 ht, target);.
1f716 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1f717 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f718 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 Op2(v, OP_Null,
1f719 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
1f71a 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1f71b 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
1f71c 6c 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a l(v, endLabel);.
1f71d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1f71e 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1f71f 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 Cache>0 );.
1f720 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 pParse->disable
1f721 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 ColCache--;.
1f722 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1f723 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f724 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 63 IT_TRIGGER. c
1f725 61 73 65 20 54 4b 5f 52 41 49 53 45 3a 20 7b 0a ase TK_RAISE: {.
1f726 20 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73 if( !pPars
1f727 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a e->trigStack ){.
1f728 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1f729 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a rrorMsg(pParse,.
1f72a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f72b 20 20 20 20 20 20 20 22 52 41 49 53 45 28 29 20 "RAISE()
1f72c 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 may only be used
1f72d 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 within a trigge
1f72e 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 r-program");.
1f72f 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
1f730 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1f731 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 pExpr->affinity
1f732 21 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 !=OE_Ignore ){.
1f733 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1f734 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d pExpr->affinity=
1f735 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 0a =OE_Rollback ||.
1f736 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f737 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 pExpr->affinity
1f738 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 20 7c 7c 0a == OE_Abort ||.
1f739 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f73a 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 pExpr->affinity
1f73b 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 29 3b 0a 20 == OE_Fail );.
1f73c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1f73d 65 71 75 6f 74 65 45 78 70 72 28 70 45 78 70 72 equoteExpr(pExpr
1f73e 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
1f73f 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1f740 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 OP_Halt, SQLITE
1f741 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 70 45 78 _CONSTRAINT, pEx
1f742 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c 20 30 2c pr->affinity, 0,
1f743 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1f744 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 (char*)
1f745 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 pExpr->token.z,
1f746 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b pExpr->token.n);
1f747 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a . } else {.
1f748 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
1f749 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 pExpr->affinity
1f74a 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72 65 20 29 3b == OE_Ignore );
1f74b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 . sqlite
1f74c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1f74d 50 5f 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30 2c P_ContextPop, 0,
1f74e 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 0);. sq
1f74f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1f750 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 v, OP_Goto, 0, p
1f751 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b Parse->trigStack
1f752 2d 3e 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20 ->ignoreJump);.
1f753 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d VdbeComm
1f754 65 6e 74 28 28 76 2c 20 22 72 61 69 73 65 28 49 ent((v, "raise(I
1f755 47 4e 4f 52 45 29 22 29 29 3b 0a 20 20 20 20 20 GNORE)"));.
1f756 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1f757 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
1f758 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 . sqlite3Releas
1f759 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
1f75a 20 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 regFree1);. sq
1f75b 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
1f75c 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 Reg(pParse, regF
1f75d 72 65 65 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 ree2);. return
1f75e 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 inReg;.}../*.**
1f75f 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1f760 20 65 76 61 6c 75 61 74 65 20 61 6e 20 65 78 70 evaluate an exp
1f761 72 65 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f 72 ression and stor
1f762 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a e the results.**
1f763 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 into a register
1f764 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 . Return the re
1f765 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 gister number wh
1f766 65 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a ere the results.
1f767 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a ** are stored..*
1f768 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69 *.** If the regi
1f769 73 74 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72 ster is a tempor
1f76a 61 72 79 20 72 65 67 69 73 74 65 72 20 74 68 61 ary register tha
1f76b 74 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 t can be dealloc
1f76c 61 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 ated,.** then wr
1f76d 69 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69 ite its number i
1f76e 6e 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74 nto *pReg. If t
1f76f 68 65 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 he result regist
1f770 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 er is not.** a t
1f771 65 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73 emporary, then s
1f772 65 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f et *pReg to zero
1f773 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f774 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
1f775 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73 xprCodeTemp(Pars
1f776 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
1f777 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 65 *pExpr, int *pRe
1f778 67 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 g){. int r1 = s
1f779 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
1f77a 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 (pParse);. int
1f77b 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r2 = sqlite3Expr
1f77c 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 CodeTarget(pPars
1f77d 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 e, pExpr, r1);.
1f77e 20 69 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a 20 if( r2==r1 ){.
1f77f 20 20 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a 20 *pReg = r1;.
1f780 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
1f781 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
1f782 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 g(pParse, r1);.
1f783 20 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 20 *pReg = 0;.
1f784 7d 0a 20 20 72 65 74 75 72 6e 20 72 32 3b 0a 7d }. return r2;.}
1f785 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1f786 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 code that will
1f787 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 evaluate express
1f788 69 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 ion pExpr and st
1f789 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c ore the.** resul
1f78a 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 ts in register t
1f78b 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 arget. The resu
1f78c 6c 74 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 lts are guarante
1f78d 65 64 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a 20 ed to appear.**
1f78e 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 in register targ
1f78f 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 et..*/.SQLITE_PR
1f790 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1f791 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 20 3ExprCode(Parse
1f792 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1f793 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 Expr, int target
1f794 29 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a ){. int inReg;.
1f795 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65 . assert( targe
1f796 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 t>0 && target<=p
1f797 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 Parse->nMem );.
1f798 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 inReg = sqlite3
1f799 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 ExprCodeTarget(p
1f79a 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 Parse, pExpr, ta
1f79b 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 rget);. assert(
1f79c 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 7c pParse->pVdbe |
1f79d 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 | pParse->db->ma
1f79e 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
1f79f 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 if( inReg!=targe
1f7a0 74 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64 t && pParse->pVd
1f7a1 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 be ){. sqlite
1f7a2 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 3VdbeAddOp2(pPar
1f7a3 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 se->pVdbe, OP_SC
1f7a4 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 opy, inReg, targ
1f7a5 65 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 et);. }. retur
1f7a6 6e 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a n target;.}../*.
1f7a7 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1f7a8 20 74 68 61 74 20 65 76 61 6c 75 74 65 73 20 74 that evalutes t
1f7a9 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 he given express
1f7aa 69 6f 6e 20 61 6e 64 20 70 75 74 73 20 74 68 65 ion and puts the
1f7ab 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 result.** in re
1f7ac 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a gister target..*
1f7ad 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61 *.** Also make a
1f7ae 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 copy of the exp
1f7af 72 65 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 20 ression results
1f7b0 69 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 61 into another "ca
1f7b1 63 68 65 22 20 72 65 67 69 73 74 65 72 0a 2a 2a che" register.**
1f7b2 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 and modify the
1f7b3 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 expression so th
1f7b4 61 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 at the next time
1f7b5 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 it is evaluated
1f7b6 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 ,.** the result
1f7b7 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 is a copy of the
1f7b8 20 63 61 63 68 65 20 72 65 67 69 73 74 65 72 2e cache register.
1f7b9 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
1f7ba 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 ine is used for
1f7bb 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 expressions that
1f7bc 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69 70 are used multip
1f7bd 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 54 le .** times. T
1f7be 68 65 79 20 61 72 65 20 65 76 61 6c 75 61 74 65 hey are evaluate
1f7bf 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 72 d once and the r
1f7c0 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 esults of the ex
1f7c1 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 pression.** are
1f7c2 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 reused..*/.SQLIT
1f7c3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1f7c4 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 lite3ExprCodeAnd
1f7c5 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 Cache(Parse *pPa
1f7c6 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
1f7c7 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 , int target){.
1f7c8 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 Vdbe *v = pPars
1f7c9 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 e->pVdbe;. int
1f7ca 69 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 3d inReg;. inReg =
1f7cb 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1f7cc 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
1f7cd 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 target);. asser
1f7ce 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 t( target>0 );.
1f7cf 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d if( pExpr->op!=
1f7d0 54 4b 5f 52 45 47 49 53 54 45 52 20 29 7b 20 20 TK_REGISTER ){
1f7d1 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 20 . int iMem;.
1f7d2 20 20 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 iMem = ++pPar
1f7d3 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 se->nMem;. sq
1f7d4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1f7d5 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 65 v, OP_Copy, inRe
1f7d6 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 45 g, iMem);. pE
1f7d7 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 4d xpr->iTable = iM
1f7d8 65 6d 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f em;. pExpr->o
1f7d9 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b p = TK_REGISTER;
1f7da 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e . }. return in
1f7db 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 Reg;.}../*.** Re
1f7dc 74 75 72 6e 20 54 52 55 45 20 69 66 20 70 45 78 turn TRUE if pEx
1f7dd 70 72 20 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e pr is an constan
1f7de 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 t expression tha
1f7df 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 t is appropriate
1f7e0 0a 2a 2a 20 66 6f 72 20 66 61 63 74 6f 72 69 6e .** for factorin
1f7e1 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e g out of a loop.
1f7e2 20 20 41 70 70 72 6f 70 72 69 61 74 65 20 65 78 Appropriate ex
1f7e3 70 72 65 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a pressions are:.*
1f7e4 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 65 *.** * Any e
1f7e5 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 xpression that e
1f7e6 76 61 6c 75 61 74 65 73 20 74 6f 20 74 77 6f 20 valuates to two
1f7e7 6f 72 20 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e or more opcodes.
1f7e8 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 .**.** * Any
1f7e9 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f OP_Integer, OP_
1f7ea 52 65 61 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c Real, OP_String,
1f7eb 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c OP_Blob, OP_Nul
1f7ec 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20 l, .** or
1f7ed 4f 50 5f 56 61 72 69 61 62 6c 65 20 74 68 61 74 OP_Variable that
1f7ee 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
1f7ef 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61 o be placed in a
1f7f0 20 0a 2a 2a 20 20 20 20 20 20 20 73 70 65 63 69 .** speci
1f7f1 66 69 63 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a fic register..**
1f7f2 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 .** There is no
1f7f3 70 6f 69 6e 74 20 69 6e 20 66 61 63 74 6f 72 69 point in factori
1f7f4 6e 67 20 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e ng out single-in
1f7f5 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 struction consta
1f7f6 6e 74 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e nt.** expression
1f7f7 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 s that need to b
1f7f8 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 70 61 e placed in a pa
1f7f9 72 74 69 63 75 6c 61 72 20 72 65 67 69 73 74 65 rticular registe
1f7fa 72 2e 20 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 r. .** We could
1f7fb 20 66 61 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 factor them out
1f7fc 2c 20 62 75 74 20 74 68 65 6e 20 77 65 20 77 6f , but then we wo
1f7fd 75 6c 64 20 65 6e 64 20 75 70 20 61 64 64 69 6e uld end up addin
1f7fe 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 g an.** OP_SCopy
1f7ff 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 instruction to
1f800 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 69 move the value i
1f801 6e 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 nto the correct
1f802 72 65 67 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65 register.** late
1f803 72 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 20 r. We might as
1f804 77 65 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68 well just use th
1f805 65 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 e original instr
1f806 75 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 uction and.** av
1f807 6f 69 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79 oid the OP_SCopy
1f808 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1f809 69 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 isAppropriateFor
1f80a 46 61 63 74 6f 72 69 6e 67 28 45 78 70 72 20 2a Factoring(Expr *
1f80b 70 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 p){. if( !sqlit
1f80c 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
1f80d 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20 NotJoin(p) ){.
1f80e 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 return 0; /*
1f80f 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e 74 20 65 78 Only constant ex
1f810 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 61 70 pressions are ap
1f811 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 66 61 propriate for fa
1f812 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 ctoring */. }.
1f813 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 if( (p->flags &
1f814 20 45 50 5f 46 69 78 65 64 44 65 73 74 29 3d 3d EP_FixedDest)==
1f815 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1f816 31 3b 20 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 1; /* Any const
1f817 61 6e 74 20 77 69 74 68 6f 75 74 20 61 20 66 69 ant without a fi
1f818 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 xed destination
1f819 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a is appropriate *
1f81a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 /. }. while( p
1f81b 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 ->op==TK_UPLUS )
1f81c 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 p = p->pLeft;.
1f81d 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 switch( p->op )
1f81e 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
1f81f 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 _OMIT_BLOB_LITER
1f820 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 AL. case TK_B
1f821 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 LOB:.#endif.
1f822 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 case TK_VARIABLE
1f823 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e :. case TK_IN
1f824 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 TEGER:. case
1f825 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61 TK_FLOAT:. ca
1f826 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 se TK_NULL:.
1f827 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 case TK_STRING:
1f828 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
1f829 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 ( p->op==TK_BLOB
1f82a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f82b 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 se( p->op==TK_VA
1f82c 52 49 41 42 4c 45 20 29 3b 0a 20 20 20 20 20 20 RIABLE );.
1f82d 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d testcase( p->op=
1f82e 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 =TK_INTEGER );.
1f82f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
1f830 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 ->op==TK_FLOAT )
1f831 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f832 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c ( p->op==TK_NULL
1f833 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f834 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 se( p->op==TK_ST
1f835 52 49 4e 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a RING );. /*
1f836 20 53 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 Single-instruct
1f837 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 ion constants wi
1f838 74 68 20 61 20 66 69 78 65 64 20 64 65 73 74 69 th a fixed desti
1f839 6e 61 74 69 6f 6e 20 61 72 65 0a 20 20 20 20 20 nation are.
1f83a 20 2a 2a 20 62 65 74 74 65 72 20 64 6f 6e 65 20 ** better done
1f83b 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 65 20 in-line. If we
1f83c 66 61 63 74 6f 72 20 74 68 65 6d 2c 20 74 68 65 factor them, the
1f83d 79 20 77 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a y will just end.
1f83e 20 20 20 20 20 20 2a 2a 20 75 70 20 67 65 6e 65 ** up gene
1f83f 72 61 74 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f rating an OP_SCo
1f840 70 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 py to move the v
1f841 61 6c 75 65 20 74 6f 20 74 68 65 20 64 65 73 74 alue to the dest
1f842 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a ination. **
1f843 20 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 register. */.
1f844 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1f845 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1f846 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 UMINUS: {.
1f847 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f if( p->pLeft->o
1f848 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 p==TK_FLOAT || p
1f849 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f ->pLeft->op==TK_
1f84a 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 INTEGER ){.
1f84b 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1f84c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 62 72 }. br
1f84d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
1f84e 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
1f84f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
1f850 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a . return 1;.}..
1f851 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 /*.** If pExpr i
1f852 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 s a constant exp
1f853 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 ression that is
1f854 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a appropriate for.
1f855 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 ** factoring out
1f856 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e of a loop, then
1f857 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 evaluate the ex
1f858 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f pression.** into
1f859 20 61 20 72 65 67 69 73 74 65 72 20 61 6e 64 20 a register and
1f85a 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 convert the expr
1f85b 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b ession into a TK
1f85c 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70 _REGISTER.** exp
1f85d 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 ression..*/.stat
1f85e 69 63 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 ic int evalConst
1f85f 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 Expr(Walker *pWa
1f860 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 lker, Expr *pExp
1f861 72 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 r){. Parse *pPa
1f862 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 rse = pWalker->p
1f863 50 61 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28 Parse;. switch(
1f864 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 pExpr->op ){.
1f865 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 case TK_REGIST
1f866 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 ER: {. retu
1f867 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
1f868 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e case TK_FUNCTION
1f869 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 :. case TK_AG
1f86a 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 G_FUNCTION:.
1f86b 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 case TK_CONST_FU
1f86c 4e 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 NC: {. /* T
1f86d 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 he arguments to
1f86e 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20 a function have
1f86f 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 a fixed destinat
1f870 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 61 ion.. ** Ma
1f871 72 6b 20 74 68 65 6d 20 74 68 69 73 20 77 61 79 rk them this way
1f872 20 74 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 61 to avoid genera
1f873 74 65 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 5f ted unneeded OP_
1f874 53 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 69 SCopy. ** i
1f875 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 20 nstructions. .
1f876 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 */. Exp
1f877 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 rList *pList = p
1f878 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 Expr->x.pList;.
1f879 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
1f87a 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
1f87b 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
1f87c 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 t) );. if(
1f87d 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 pList ){.
1f87e 20 69 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d 3e int i = pList->
1f87f 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 73 nExpr;. s
1f880 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
1f881 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 4c 69 tem *pItem = pLi
1f882 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 st->a;. f
1f883 6f 72 28 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 or(; i>0; i--, p
1f884 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 Item++){.
1f885 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 45 if( pItem->pE
1f886 78 70 72 20 29 20 70 49 74 65 6d 2d 3e 70 45 78 xpr ) pItem->pEx
1f887 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f pr->flags |= EP_
1f888 46 69 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20 FixedDest;.
1f889 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1f88a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1f88b 20 20 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72 }. if( isAppr
1f88c 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72 opriateForFactor
1f88d 69 6e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20 ing(pExpr) ){.
1f88e 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61 int r1 = ++pPa
1f88f 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 rse->nMem;. i
1f890 6e 74 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20 nt r2;. r2 =
1f891 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1f892 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 arget(pParse, pE
1f893 78 70 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66 xpr, r1);. if
1f894 28 20 72 31 21 3d 72 32 20 29 20 73 71 6c 69 74 ( r1!=r2 ) sqlit
1f895 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
1f896 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 (pParse, r1);.
1f897 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b pExpr->op = TK
1f898 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 70 _REGISTER;. p
1f899 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 72 Expr->iTable = r
1f89a 32 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 2;. return WR
1f89b 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 72 C_Prune;. }. r
1f89c 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
1f89d 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 ue;.}../*.** Pre
1f89e 65 76 61 6c 75 61 74 65 20 63 6f 6e 73 74 61 6e evaluate constan
1f89f 74 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 t subexpressions
1f8a0 20 77 69 74 68 69 6e 20 70 45 78 70 72 20 61 6e within pExpr an
1f8a1 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 d store the.** r
1f8a2 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 esults in regist
1f8a3 65 72 73 2e 20 20 4d 6f 64 69 66 79 20 70 45 78 ers. Modify pEx
1f8a4 70 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 pr so that the c
1f8a5 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 onstant subexpre
1f8a6 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 54 4b 5f sions.** are TK_
1f8a7 52 45 47 49 53 54 45 52 20 6f 70 63 6f 64 65 73 REGISTER opcodes
1f8a8 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 that refer to t
1f8a9 68 65 20 70 72 65 63 6f 6d 70 75 74 65 64 20 76 he precomputed v
1f8aa 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 alues..*/.SQLITE
1f8ab 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1f8ac 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e lite3ExprCodeCon
1f8ad 73 74 61 6e 74 73 28 50 61 72 73 65 20 2a 70 50 stants(Parse *pP
1f8ae 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
1f8af 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a r){. Walker w;.
1f8b0 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 w.xExprCallbac
1f8b1 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 k = evalConstExp
1f8b2 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 r;. w.xSelectCa
1f8b3 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77 2e llback = 0;. w.
1f8b4 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b pParse = pParse;
1f8b5 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 . sqlite3WalkEx
1f8b6 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d pr(&w, pExpr);.}
1f8b7 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 .../*.** Generat
1f8b8 65 20 63 6f 64 65 20 74 68 61 74 20 70 75 73 68 e code that push
1f8b9 65 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 es the value of
1f8ba 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f 66 every element of
1f8bb 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 the given.** ex
1f8bc 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 69 6e pression list in
1f8bd 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 to a sequence of
1f8be 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e registers begin
1f8bf 6e 69 6e 67 20 61 74 20 74 61 72 67 65 74 2e 0a ning at target..
1f8c0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 **.** Return the
1f8c1 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 number of eleme
1f8c2 6e 74 73 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a nts evaluated..*
1f8c3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f8c4 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
1f8c5 43 6f 64 65 45 78 70 72 4c 69 73 74 28 0a 20 20 CodeExprList(.
1f8c6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1f8c7 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
1f8c8 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c ntext */. ExprL
1f8c9 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a ist *pList, /*
1f8ca 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
1f8cb 6c 69 73 74 20 74 6f 20 62 65 20 63 6f 64 65 64 list to be coded
1f8cc 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72 67 65 74 */. int target
1f8cd 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 , /* Wher
1f8ce 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c e to write resul
1f8cf 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48 61 ts */. int doHa
1f8d0 72 64 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d 61 rdCopy /* Ma
1f8d1 6b 65 20 61 20 68 61 72 64 20 63 6f 70 79 20 6f ke a hard copy o
1f8d2 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 f every element
1f8d3 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 45 */.){. struct E
1f8d4 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
1f8d5 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b tem;. int i, n;
1f8d6 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 . assert( pList
1f8d7 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1f8d8 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 6e target>0 );. n
1f8d9 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b = pList->nExpr;
1f8da 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 . for(pItem=pLi
1f8db 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e 3b st->a, i=0; i<n;
1f8dc 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i++, pItem++){.
1f8dd 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 if( pItem->i
1f8de 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 69 Alias ){. i
1f8df 6e 74 20 69 52 65 67 20 3d 20 63 6f 64 65 41 6c nt iReg = codeAl
1f8e0 69 61 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 ias(pParse, pIte
1f8e1 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49 74 65 6d m->iAlias, pItem
1f8e2 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 2b ->pExpr, target+
1f8e3 69 29 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a i);. Vdbe *
1f8e4 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
1f8e5 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 be(pParse);.
1f8e6 20 20 69 66 28 20 69 52 65 67 21 3d 74 61 72 67 if( iReg!=targ
1f8e7 65 74 2b 69 20 29 7b 0a 20 20 20 20 20 20 20 20 et+i ){.
1f8e8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f8e9 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 2(v, OP_SCopy, i
1f8ea 52 65 67 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a Reg, target+i);.
1f8eb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
1f8ec 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
1f8ed 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c ExprCode(pParse,
1f8ee 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 pItem->pExpr, t
1f8ef 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 7d 0a arget+i);. }.
1f8f0 20 20 20 20 69 66 28 20 64 6f 48 61 72 64 43 6f if( doHardCo
1f8f1 70 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 py ){. sqli
1f8f2 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 te3ExprHardCopy(
1f8f3 70 50 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20 pParse, target,
1f8f4 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 n);. }. }.
1f8f5 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a return n;.}../*.
1f8f6 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1f8f7 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 for a boolean e
1f8f8 78 70 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 xpression such t
1f8f9 68 61 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 hat a jump is ma
1f8fa 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 de.** to the lab
1f8fb 65 6c 20 22 64 65 73 74 22 20 69 66 20 74 68 65 el "dest" if the
1f8fc 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 expression is t
1f8fd 72 75 65 20 62 75 74 20 65 78 65 63 75 74 69 6f rue but executio
1f8fe 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 n.** continues s
1f8ff 74 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20 traight thru if
1f900 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
1f901 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 s false..**.** I
1f902 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1f903 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 evaluates to NU
1f904 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72 75 65 LL (neither true
1f905 20 6e 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65 nor false), the
1f906 6e 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 n.** take the ju
1f907 6d 70 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66 mp if the jumpIf
1f908 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 53 51 4c Null flag is SQL
1f909 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a ITE_JUMPIFNULL..
1f90a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 **.** This code
1f90b 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 66 depends on the f
1f90c 61 63 74 20 74 68 61 74 20 63 65 72 74 61 69 6e act that certain
1f90d 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28 65 token values (e
1f90e 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65 x: TK_EQ).** are
1f90f 20 74 68 65 20 73 61 6d 65 20 61 73 20 6f 70 63 the same as opc
1f910 6f 64 65 20 76 61 6c 75 65 73 20 28 65 78 3a 20 ode values (ex:
1f911 4f 50 5f 45 71 29 20 74 68 61 74 20 69 6d 70 6c OP_Eq) that impl
1f912 65 6d 65 6e 74 20 74 68 65 20 63 6f 72 72 65 73 ement the corres
1f913 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 ponding.** opera
1f914 74 69 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20 63 tion. Special c
1f915 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64 62 65 2e omments in vdbe.
1f916 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f c and the mkopco
1f917 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 69 deh.awk script i
1f918 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70 72 n.** the make pr
1f919 6f 63 65 73 73 20 63 61 75 73 65 20 74 68 65 73 ocess cause thes
1f91a 65 20 76 61 6c 75 65 73 20 74 6f 20 61 6c 69 67 e values to alig
1f91b 6e 2e 20 20 41 73 73 65 72 74 28 29 73 20 69 6e n. Assert()s in
1f91c 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c the code.** bel
1f91d 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 74 ow verify that t
1f91e 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 he numbers are a
1f91f 6c 69 67 6e 65 64 20 63 6f 72 72 65 63 74 6c 79 ligned correctly
1f920 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f921 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f922 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73 65 ExprIfTrue(Parse
1f923 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
1f924 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c pExpr, int dest,
1f925 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 int jumpIfNull)
1f926 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 {. Vdbe *v = pP
1f927 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 arse->pVdbe;. i
1f928 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 nt op = 0;. int
1f929 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 regFree1 = 0;.
1f92a 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 int regFree2 =
1f92b 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0;. int r1, r2;
1f92c 0a 0a 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 .. assert( jump
1f92d 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a IfNull==SQLITE_J
1f92e 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d UMPIFNULL || jum
1f92f 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
1f930 69 66 28 20 76 3d 3d 30 20 7c 7c 20 70 45 78 70 if( v==0 || pExp
1f931 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 r==0 ) return;.
1f932 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b op = pExpr->op;
1f933 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b . switch( op ){
1f934 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 . case TK_AND
1f935 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 : {. int d2
1f936 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
1f937 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 keLabel(v);.
1f938 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 testcase( jump
1f939 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 IfNull==0 );.
1f93a 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 testcase( pPa
1f93b 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1f93c 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ache==0 );.
1f93d 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 sqlite3ExprIfFa
1f93e 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 lse(pParse, pExp
1f93f 72 2d 3e 70 4c 65 66 74 2c 20 64 32 2c 6a 75 6d r->pLeft, d2,jum
1f940 70 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a pIfNull^SQLITE_J
1f941 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 UMPIFNULL);.
1f942 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c pParse->disabl
1f943 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 eColCache++;.
1f944 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 sqlite3ExprIf
1f945 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 True(pParse, pEx
1f946 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 pr->pRight, dest
1f947 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
1f948 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
1f949 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1f94a 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 ache>0 );.
1f94b 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 pParse->disableC
1f94c 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 olCache--;.
1f94d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f sqlite3VdbeReso
1f94e 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b lveLabel(v, d2);
1f94f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1f950 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1f951 4f 52 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 OR: {. test
1f952 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c case( jumpIfNull
1f953 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ==0 );. tes
1f954 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e 64 tcase( pParse->d
1f955 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d isableColCache==
1f956 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 );. sqlit
1f957 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 e3ExprIfTrue(pPa
1f958 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1f959 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e t, dest, jumpIfN
1f95a 75 6c 6c 29 3b 0a 20 20 20 20 20 20 70 50 61 72 ull);. pPar
1f95b 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 se->disableColCa
1f95c 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c che++;. sql
1f95d 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 ite3ExprIfTrue(p
1f95e 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 Parse, pExpr->pR
1f95f 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 ight, dest, jump
1f960 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 61 IfNull);. a
1f961 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 ssert( pParse->d
1f962 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 isableColCache>0
1f963 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 );. pParse
1f964 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 ->disableColCach
1f965 65 2d 2d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b e--;. break
1f966 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1f967 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 TK_NOT: {.
1f968 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
1f969 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
1f96a 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
1f96b 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 alse(pParse, pEx
1f96c 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c pr->pLeft, dest,
1f96d 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
1f96e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1f96f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a . case TK_LT:
1f970 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a . case TK_LE:
1f971 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54 3a . case TK_GT:
1f972 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45 3a . case TK_GE:
1f973 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a . case TK_NE:
1f974 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a . case TK_EQ:
1f975 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
1f976 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b TK_LT==OP_Lt );
1f977 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
1f978 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 K_LE==OP_Le );.
1f979 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1f97a 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 20 GT==OP_Gt );.
1f97b 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 assert( TK_GE
1f97c 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20 20 ==OP_Ge );.
1f97d 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d assert( TK_EQ==
1f97e 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 20 61 OP_Eq );. a
1f97f 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 ssert( TK_NE==OP
1f980 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _Ne );. tes
1f981 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 tcase( op==TK_LT
1f982 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f983 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b se( op==TK_LE );
1f984 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1f985 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 op==TK_GT );.
1f986 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1f987 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 ==TK_GE );.
1f988 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1f989 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65 K_EQ );. te
1f98a 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e stcase( op==TK_N
1f98b 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 E );. testc
1f98c 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d ase( jumpIfNull=
1f98d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 =0 );. code
1f98e 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 CompareOperands(
1f98f 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1f990 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 Left, &r1, ®F
1f991 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 ree1,.
1f992 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f993 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1f994 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 Right, &r2, ®
1f995 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f Free2);. co
1f996 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 deCompare(pParse
1f997 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
1f998 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f pExpr->pRight, o
1f999 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 p,.
1f99a 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 r1, r2, des
1f99b 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a t, jumpIfNull);.
1f99c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1f99d 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
1f99e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1f99f 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 egFree2==0 );.
1f9a0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1f9a1 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e . case TK_ISN
1f9a2 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b ULL:. case TK
1f9a3 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 _NOTNULL: {.
1f9a4 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e assert( TK_ISN
1f9a5 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 ULL==OP_IsNull )
1f9a6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f9a7 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e TK_NOTNULL==OP_N
1f9a8 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 otNull );.
1f9a9 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1f9aa 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 _ISNULL );.
1f9ab 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1f9ac 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 K_NOTNULL );.
1f9ad 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 r1 = sqlite3E
1f9ae 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 xprCodeTemp(pPar
1f9af 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
1f9b0 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 , ®Free1);.
1f9b1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1f9b2 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c ddOp2(v, op, r1,
1f9b3 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 dest);. te
1f9b4 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 stcase( regFree1
1f9b5 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
1f9b6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1f9b7 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b se TK_BETWEEN: {
1f9b8 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42 . /* x B
1f9b9 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 ETWEEN y AND z.
1f9ba 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
1f9bb 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 Is equivalent t
1f9bc 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 o . **.
1f9bd 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 ** x>=y AND
1f9be 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 x<=z. **.
1f9bf 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20 ** Code it
1f9c0 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 as such, taking
1f9c1 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63 care to do the c
1f9c2 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73 ommon subexpress
1f9c3 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 ion. ** ele
1f9c4 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a mentation of x..
1f9c5 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 */. E
1f9c6 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20 xpr exprAnd;.
1f9c7 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 Expr compLeft
1f9c8 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d ;. Expr com
1f9c9 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78 pRight;. Ex
1f9ca 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20 pr exprX;..
1f9cb 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
1f9cc 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
1f9cd 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 EP_xIsSelect) )
1f9ce 3b 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20 ;. exprX =
1f9cf 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 *pExpr->pLeft;.
1f9d0 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 exprAnd.op
1f9d1 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 = TK_AND;.
1f9d2 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 exprAnd.pLeft =
1f9d3 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 &compLeft;.
1f9d4 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20 exprAnd.pRight
1f9d5 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 = &compRight;.
1f9d6 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 compLeft.op
1f9d7 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 = TK_GE;. c
1f9d8 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 ompLeft.pLeft =
1f9d9 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f &exprX;. co
1f9da 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20 mpLeft.pRight =
1f9db 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e pExpr->x.pList->
1f9dc 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 a[0].pExpr;.
1f9dd 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d compRight.op =
1f9de 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f TK_LE;. co
1f9df 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 mpRight.pLeft =
1f9e0 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f &exprX;. co
1f9e1 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d mpRight.pRight =
1f9e2 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d pExpr->x.pList-
1f9e3 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 >a[1].pExpr;.
1f9e4 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20 exprX.iTable
1f9e5 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
1f9e6 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65 eTemp(pParse, &e
1f9e7 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29 xprX, ®Free1)
1f9e8 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f9e9 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
1f9ea 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20 . exprX.op
1f9eb 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 = TK_REGISTER;.
1f9ec 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a testcase( j
1f9ed 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a umpIfNull==0 );.
1f9ee 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1f9ef 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 rIfTrue(pParse,
1f9f0 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 &exprAnd, dest,
1f9f1 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 jumpIfNull);.
1f9f2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1f9f3 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
1f9f4 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 r1 = sqlite
1f9f5 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1f9f6 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 arse, pExpr, &re
1f9f7 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 gFree1);. s
1f9f8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1f9f9 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64 (v, OP_If, r1, d
1f9fa 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 est, jumpIfNull!
1f9fb 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 =0);. testc
1f9fc 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
1f9fd 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f9fe 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d se( jumpIfNull==
1f9ff 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
1fa00 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
1fa01 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
1fa02 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 Reg(pParse, regF
1fa03 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 ree1);. sqlite3
1fa04 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
1fa05 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 Parse, regFree2)
1fa06 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e ; .}../*.** Gen
1fa07 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 erate code for a
1fa08 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 boolean express
1fa09 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20 ion such that a
1fa0a 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 jump is made.**
1fa0b 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65 to the label "de
1fa0c 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65 st" if the expre
1fa0d 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62 ssion is false b
1fa0e 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 ut execution.**
1fa0f 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 continues straig
1fa10 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 ht thru if the e
1fa11 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 xpression is tru
1fa12 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
1fa13 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 expression evalu
1fa14 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 ates to NULL (ne
1fa15 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66 ither true nor f
1fa16 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 alse) then.** ju
1fa17 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c mp if jumpIfNull
1fa18 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 is SQLITE_JUMPI
1fa19 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68 FNULL or fall th
1fa1a 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49 66 4e rough if jumpIfN
1fa1b 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a ull.** is 0..*/.
1fa1c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1fa1d 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
1fa1e 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a 70 50 fFalse(Parse *pP
1fa1f 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
1fa20 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 r, int dest, int
1fa21 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 jumpIfNull){.
1fa22 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 Vdbe *v = pParse
1fa23 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f ->pVdbe;. int o
1fa24 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 p = 0;. int reg
1fa25 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 Free1 = 0;. int
1fa26 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 regFree2 = 0;.
1fa27 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 int r1, r2;..
1fa28 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 assert( jumpIfNu
1fa29 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 ll==SQLITE_JUMPI
1fa2a 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e FNULL || jumpIfN
1fa2b 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 ull==0 );. if(
1fa2c 76 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30 v==0 || pExpr==0
1fa2d 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a ) return;.. /*
1fa2e 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 The value of pE
1fa2f 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61 xpr->op and op a
1fa30 72 65 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f re related as fo
1fa31 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a llows:. **. **
1fa32 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 pExpr->op
1fa33 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20 op.
1fa34 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d ** ------
1fa35 2d 2d 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d --- ---
1fa36 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 -------. **
1fa37 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 TK_ISNULL
1fa38 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c OP_NotNull
1fa39 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e . ** TK_N
1fa3a 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f OTNULL O
1fa3b 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 P_IsNull. **
1fa3c 20 20 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 TK_NE
1fa3d 20 20 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a OP_Eq. *
1fa3e 2a 20 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20 * TK_EQ
1fa3f 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 OP_Ne
1fa40 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 . ** TK_G
1fa41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f T O
1fa42 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 P_Le. **
1fa43 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 TK_LE
1fa44 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 OP_Gt. **
1fa45 20 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20 TK_GE
1fa46 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a OP_Lt. *
1fa47 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20 * TK_LT
1fa48 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65 OP_Ge
1fa49 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f . **. ** For o
1fa4a 74 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70 ther values of p
1fa4b 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 Expr->op, op is
1fa4c 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e undefined and un
1fa4d 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76 used.. ** The v
1fa4e 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20 alue of TK_ and
1fa4f 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 OP_ constants ar
1fa50 65 20 61 72 72 61 6e 67 65 64 20 73 75 63 68 20 e arranged such
1fa51 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e that we. ** can
1fa52 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70 compute the map
1fa53 70 69 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67 ping above using
1fa54 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 the following e
1fa55 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 xpression.. **
1fa56 41 73 73 65 72 74 28 29 73 20 76 65 72 69 66 79 Assert()s verify
1fa57 20 74 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74 that the comput
1fa58 61 74 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74 ation is correct
1fa59 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 .. */. op = ((
1fa5a 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 pExpr->op+(TK_IS
1fa5b 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f NULL&1))^1)-(TK_
1fa5c 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a ISNULL&1);.. /*
1fa5d 20 56 65 72 69 66 79 20 63 6f 72 72 65 63 74 20 Verify correct
1fa5e 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f alignment of TK_
1fa5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e and OP_ constan
1fa60 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ts. */. assert
1fa61 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f ( pExpr->op!=TK_
1fa62 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 ISNULL || op==OP
1fa63 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 _NotNull );. as
1fa64 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 sert( pExpr->op!
1fa65 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f =TK_NOTNULL || o
1fa66 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a p==OP_IsNull );.
1fa67 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
1fa68 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 >op!=TK_NE || op
1fa69 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73 ==OP_Eq );. ass
1fa6a 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d ert( pExpr->op!=
1fa6b 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f TK_EQ || op==OP_
1fa6c 4e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Ne );. assert(
1fa6d 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 pExpr->op!=TK_LT
1fa6e 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b || op==OP_Ge );
1fa6f 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 . assert( pExpr
1fa70 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f ->op!=TK_LE || o
1fa71 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73 p==OP_Gt );. as
1fa72 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 sert( pExpr->op!
1fa73 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 =TK_GT || op==OP
1fa74 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 _Le );. assert(
1fa75 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 pExpr->op!=TK_G
1fa76 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 E || op==OP_Lt )
1fa77 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 ;.. switch( pEx
1fa78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 pr->op ){. ca
1fa79 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 se TK_AND: {.
1fa7a 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d testcase( jum
1fa7b 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
1fa7c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50 testcase( pP
1fa7d 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1fa7e 43 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 Cache==0 );.
1fa7f 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
1fa80 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 alse(pParse, pEx
1fa81 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c pr->pLeft, dest,
1fa82 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
1fa83 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 pParse->disa
1fa84 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 bleColCache++;.
1fa85 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1fa86 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 IfFalse(pParse,
1fa87 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 pExpr->pRight, d
1fa88 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 est, jumpIfNull)
1fa89 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1fa8a 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 pParse->disableC
1fa8b 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 olCache>0 );.
1fa8c 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 pParse->disab
1fa8d 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 leColCache--;.
1fa8e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1fa8f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a . case TK_OR:
1fa90 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20 {. int d2
1fa91 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b = sqlite3VdbeMak
1fa92 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 eLabel(v);.
1fa93 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
1fa94 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
1fa95 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72 testcase( pPar
1fa96 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 se->disableColCa
1fa97 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 che==0 );.
1fa98 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 sqlite3ExprIfTru
1fa99 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
1fa9a 3e 70 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70 >pLeft, d2, jump
1fa9b 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 IfNull^SQLITE_JU
1fa9c 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 MPIFNULL);.
1fa9d 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 pParse->disable
1fa9e 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 ColCache++;.
1fa9f 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
1faa0 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 alse(pParse, pEx
1faa1 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 pr->pRight, dest
1faa2 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
1faa3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
1faa4 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1faa5 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 ache>0 );.
1faa6 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 pParse->disableC
1faa7 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 olCache--;.
1faa8 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f sqlite3VdbeReso
1faa9 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b lveLabel(v, d2);
1faaa 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1faab 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1faac 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c NOT: {. sql
1faad 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 ite3ExprIfTrue(p
1faae 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
1faaf 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 eft, dest, jumpI
1fab0 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 fNull);. br
1fab1 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1fab2 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 ase TK_LT:. c
1fab3 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 ase TK_LE:. c
1fab4 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 ase TK_GT:. c
1fab5 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 ase TK_GE:. c
1fab6 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 ase TK_NE:. c
1fab7 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 ase TK_EQ: {.
1fab8 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1fab9 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 =TK_LT );.
1faba 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1fabb 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _LE );. tes
1fabc 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 tcase( op==TK_GT
1fabd 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1fabe 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b se( op==TK_GE );
1fabf 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fac0 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 op==TK_EQ );.
1fac1 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1fac2 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 ==TK_NE );.
1fac3 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
1fac4 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
1fac5 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 codeCompareOpe
1fac6 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 rands(pParse, pE
1fac7 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c xpr->pLeft, &r1,
1fac8 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 ®Free1,.
1fac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1faca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
1facb 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 xpr->pRight, &r2
1facc 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
1facd 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 codeCompare(
1face 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1facf 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 Left, pExpr->pRi
1fad0 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 ght, op,.
1fad1 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 r1, r
1fad2 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 2, dest, jumpIfN
1fad3 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ull);. test
1fad4 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1fad5 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
1fad6 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
1fad7 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
1fad8 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1fad9 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 TK_ISNULL:. c
1fada 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 ase TK_NOTNULL:
1fadb 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
1fadc 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 ( op==TK_ISNULL
1fadd 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1fade 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c e( op==TK_NOTNUL
1fadf 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 L );. r1 =
1fae0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1fae1 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
1fae2 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 r->pLeft, ®Fr
1fae3 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ee1);. sqli
1fae4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1fae5 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a op, r1, dest);.
1fae6 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1fae7 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
1fae8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1fae9 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 }. case TK_BE
1faea 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f TWEEN: {. /
1faeb 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 * x BETWEEN y
1faec 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a AND z. **.
1faed 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 69 ** Is equi
1faee 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 valent to .
1faef 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 **. **
1faf0 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 x>=y AND x<=z.
1faf1 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
1faf2 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 2c Code it as such,
1faf3 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 taking care to
1faf4 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 do the common su
1faf5 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 bexpression.
1faf6 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f ** elementatio
1faf7 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f n of x.. */
1faf8 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 . Expr expr
1faf9 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 20 And;. Expr
1fafa 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 compLeft;.
1fafb 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a Expr compRight;.
1fafc 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 58 Expr exprX
1fafd 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ;.. assert(
1fafe 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
1faff 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
1fb00 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 elect) );.
1fb01 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d 3e exprX = *pExpr->
1fb02 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70 pLeft;. exp
1fb03 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 rAnd.op = TK_AND
1fb04 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e ;. exprAnd.
1fb05 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65 66 pLeft = &compLef
1fb06 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 t;. exprAnd
1fb07 2e 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70 52 .pRight = &compR
1fb08 69 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d 70 ight;. comp
1fb09 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b Left.op = TK_GE;
1fb0a 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e . compLeft.
1fb0b 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b 0a pLeft = &exprX;.
1fb0c 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 compLeft.p
1fb0d 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 Right = pExpr->x
1fb0e 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 .pList->a[0].pEx
1fb0f 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 pr;. compRi
1fb10 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a ght.op = TK_LE;.
1fb11 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e compRight.
1fb12 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b 0a pLeft = &exprX;.
1fb13 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e compRight.
1fb14 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e pRight = pExpr->
1fb15 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 x.pList->a[1].pE
1fb16 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 58 xpr;. exprX
1fb17 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 .iTable = sqlite
1fb18 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1fb19 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26 72 arse, &exprX, &r
1fb1a 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 egFree1);.
1fb1b 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1fb1c 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 e1==0 );. e
1fb1d 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 xprX.op = TK_REG
1fb1e 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65 73 ISTER;. tes
1fb1f 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c tcase( jumpIfNul
1fb20 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 l==0 );. sq
1fb21 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 lite3ExprIfFalse
1fb22 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e (pParse, &exprAn
1fb23 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e d, dest, jumpIfN
1fb24 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 ull);. brea
1fb25 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
1fb26 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 ault: {. r1
1fb27 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1fb28 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
1fb29 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 29 Expr, ®Free1)
1fb2a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1fb2b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
1fb2c 49 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73 74 2c IfNot, r1, dest,
1fb2d 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b jumpIfNull!=0);
1fb2e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fb2f 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
1fb30 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1fb31 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b jumpIfNull==0 );
1fb32 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1fb33 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
1fb34 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1fb35 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 pParse, regFree1
1fb36 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 );. sqlite3Rele
1fb37 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
1fb38 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 7d 0a e, regFree2);.}.
1fb39 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 70 ./*.** Do a deep
1fb3a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 comparison of t
1fb3b 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 wo expression tr
1fb3c 65 65 73 2e 20 20 52 65 74 75 72 6e 20 54 52 55 ees. Return TRU
1fb3d 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a 20 E (non-zero).**
1fb3e 69 66 20 74 68 65 79 20 61 72 65 20 69 64 65 6e if they are iden
1fb3f 74 69 63 61 6c 20 61 6e 64 20 72 65 74 75 72 6e tical and return
1fb40 20 46 41 4c 53 45 20 69 66 20 74 68 65 79 20 64 FALSE if they d
1fb41 69 66 66 65 72 20 69 6e 20 61 6e 79 20 77 61 79 iffer in any way
1fb42 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 ..**.** Sometime
1fb43 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 s this routine w
1fb44 69 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c 53 45 ill return FALSE
1fb45 20 65 76 65 6e 20 69 66 20 74 68 65 20 74 77 6f even if the two
1fb46 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 expressions.**
1fb47 72 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69 76 really are equiv
1fb48 61 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63 61 alent. If we ca
1fb49 6e 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74 20 nnot prove that
1fb4a 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 the expressions
1fb4b 61 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c are.** identical
1fb4c 2c 20 77 65 20 72 65 74 75 72 6e 20 46 41 4c 53 , we return FALS
1fb4d 45 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66 E just to be saf
1fb4e 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20 72 e. So if this r
1fb4f 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e outine.** return
1fb50 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 79 6f s false, then yo
1fb51 75 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 u do not really
1fb52 6b 6e 6f 77 20 66 6f 72 20 63 65 72 74 61 69 6e know for certain
1fb53 20 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a 20 65 if the two.** e
1fb54 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 74 xpressions are t
1fb55 68 65 20 73 61 6d 65 2e 20 20 42 75 74 20 69 66 he same. But if
1fb56 20 79 6f 75 20 67 65 74 20 61 20 54 52 55 45 20 you get a TRUE
1fb57 72 65 74 75 72 6e 2c 20 74 68 65 6e 20 79 6f 75 return, then you
1fb58 0a 2a 2a 20 63 61 6e 20 62 65 20 73 75 72 65 20 .** can be sure
1fb59 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 the expressions
1fb5a 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 49 are the same. I
1fb5b 6e 20 74 68 65 20 70 6c 61 63 65 73 20 77 68 65 n the places whe
1fb5c 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 re.** this routi
1fb5d 6e 65 20 69 73 20 75 73 65 64 2c 20 69 74 20 64 ne is used, it d
1fb5e 6f 65 73 20 6e 6f 74 20 68 75 72 74 20 74 6f 20 oes not hurt to
1fb5f 67 65 74 20 61 6e 20 65 78 74 72 61 20 46 41 4c get an extra FAL
1fb60 53 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 73 SE - that.** jus
1fb61 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 t might result i
1fb62 6e 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c 79 20 n some slightly
1fb63 73 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20 42 75 slower code. Bu
1fb64 74 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 61 t returning.** a
1fb65 6e 20 69 6e 63 6f 72 72 65 63 74 20 54 52 55 45 n incorrect TRUE
1fb66 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 61 could lead to a
1fb67 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f malfunction..*/
1fb68 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1fb69 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
1fb6a 6f 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41 2c ompare(Expr *pA,
1fb6b 20 45 78 70 72 20 2a 70 42 29 7b 0a 20 20 69 6e Expr *pB){. in
1fb6c 74 20 69 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30 t i;. if( pA==0
1fb6d 7c 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72 ||pB==0 ){. r
1fb6e 65 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 20 eturn pB==pA;.
1fb6f 7d 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 }. if( ExprHasP
1fb70 72 6f 70 65 72 74 79 28 70 41 2c 20 45 50 5f 78 roperty(pA, EP_x
1fb71 49 73 53 65 6c 65 63 74 29 20 7c 7c 20 45 78 70 IsSelect) || Exp
1fb72 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c rHasProperty(pB,
1fb73 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 EP_xIsSelect) )
1fb74 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
1fb75 20 20 7d 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 }. if( (pA->f
1fb76 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e lags & EP_Distin
1fb77 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 ct)!=(pB->flags
1fb78 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 20 29 & EP_Distinct) )
1fb79 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
1fb7a 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 pA->op!=pB->op
1fb7b 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 ) return 0;. if
1fb7c 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f ( !sqlite3ExprCo
1fb7d 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c mpare(pA->pLeft,
1fb7e 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 pB->pLeft) ) re
1fb7f 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 turn 0;. if( !s
1fb80 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 qlite3ExprCompar
1fb81 65 28 70 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 e(pA->pRight, pB
1fb82 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65 74 75 ->pRight) ) retu
1fb83 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d rn 0;.. if( pA-
1fb84 3e 78 2e 70 4c 69 73 74 20 26 26 20 70 42 2d 3e >x.pList && pB->
1fb85 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 69 x.pList ){. i
1fb86 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e f( pA->x.pList->
1fb87 6e 45 78 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69 nExpr!=pB->x.pLi
1fb88 73 74 2d 3e 6e 45 78 70 72 20 29 20 72 65 74 75 st->nExpr ) retu
1fb89 72 6e 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d rn 0;. for(i=
1fb8a 30 3b 20 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74 0; i<pA->x.pList
1fb8b 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 ->nExpr; i++){.
1fb8c 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 Expr *pExpr
1fb8d 41 20 3d 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d A = pA->x.pList-
1fb8e 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 >a[i].pExpr;.
1fb8f 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 42 20 Expr *pExprB
1fb90 3d 20 70 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 = pB->x.pList->a
1fb91 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [i].pExpr;.
1fb92 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 if( !sqlite3Exp
1fb93 72 43 6f 6d 70 61 72 65 28 70 45 78 70 72 41 2c rCompare(pExprA,
1fb94 20 70 45 78 70 72 42 29 20 29 20 72 65 74 75 72 pExprB) ) retur
1fb95 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c n 0;. }. }el
1fb96 73 65 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 se if( pA->x.pLi
1fb97 73 74 20 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73 st || pB->x.pLis
1fb98 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
1fb99 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 41 0;. }.. if( pA
1fb9a 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54 ->iTable!=pB->iT
1fb9b 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c able || pA->iCol
1fb9c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e umn!=pB->iColumn
1fb9d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 ) return 0;. i
1fb9e 66 28 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f f( pA->op!=TK_CO
1fb9f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e 74 6f 6b 65 LUMN && pA->toke
1fba0 6e 2e 7a 20 29 7b 0a 20 20 20 20 69 66 28 20 70 n.z ){. if( p
1fba1 42 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29 20 B->token.z==0 )
1fba2 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 return 0;. if
1fba3 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 ( pB->token.n!=p
1fba4 41 2d 3e 74 6f 6b 65 6e 2e 6e 20 29 20 72 65 74 A->token.n ) ret
1fba5 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 73 urn 0;. if( s
1fba6 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 qlite3StrNICmp((
1fba7 63 68 61 72 2a 29 70 41 2d 3e 74 6f 6b 65 6e 2e char*)pA->token.
1fba8 7a 2c 28 63 68 61 72 2a 29 70 42 2d 3e 74 6f 6b z,(char*)pB->tok
1fba9 65 6e 2e 7a 2c 70 42 2d 3e 74 6f 6b 65 6e 2e 6e en.z,pB->token.n
1fbaa 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 )!=0 ){. re
1fbab 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
1fbac 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a }. return 1;.}.
1fbad 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 ../*.** Add a ne
1fbae 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 w element to the
1fbaf 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b pAggInfo->aCol[
1fbb0 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e ] array. Return
1fbb1 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a the index of.**
1fbb2 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 the new element
1fbb3 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61 . Return a nega
1fbb4 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d tive number if m
1fbb5 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a alloc fails..*/.
1fbb6 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 67 static int addAg
1fbb7 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 73 71 6c 69 gInfoColumn(sqli
1fbb8 74 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f te3 *db, AggInfo
1fbb9 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 *pInfo){. int
1fbba 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c i;. pInfo->aCol
1fbbb 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 = sqlite3ArrayA
1fbbc 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20 llocate(.
1fbbd 64 62 2c 0a 20 20 20 20 20 20 20 70 49 6e 66 6f db,. pInfo
1fbbe 2d 3e 61 43 6f 6c 2c 0a 20 20 20 20 20 20 20 73 ->aCol,. s
1fbbf 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 43 6f izeof(pInfo->aCo
1fbc0 6c 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c l[0]),. 3,
1fbc1 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e . &pInfo->
1fbc2 6e 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 nColumn,.
1fbc3 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 41 &pInfo->nColumnA
1fbc4 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a lloc,. &i.
1fbc5 20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b );. return i;
1fbc6 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 41 64 .} ../*.** Ad
1fbc7 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 d a new element
1fbc8 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d to the pAggInfo-
1fbc9 3e 61 46 75 6e 63 5b 5d 20 61 72 72 61 79 2e 20 >aFunc[] array.
1fbca 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 Return the inde
1fbcb 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 x of.** the new
1fbcc 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e element. Return
1fbcd 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 a negative numb
1fbce 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 er if malloc fai
1fbcf 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ls..*/.static in
1fbd0 74 20 61 64 64 41 67 67 49 6e 66 6f 46 75 6e 63 t addAggInfoFunc
1fbd1 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 (sqlite3 *db, Ag
1fbd2 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 gInfo *pInfo){.
1fbd3 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d int i;. pInfo-
1fbd4 3e 61 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 >aFunc = sqlite3
1fbd5 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 ArrayAllocate(.
1fbd6 20 20 20 20 20 20 64 62 2c 20 0a 20 20 20 20 20 db, .
1fbd7 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 0a pInfo->aFunc,.
1fbd8 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49 sizeof(pI
1fbd9 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 29 2c 0a nfo->aFunc[0]),.
1fbda 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20 3,.
1fbdb 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2c 0a &pInfo->nFunc,.
1fbdc 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e &pInfo->n
1fbdd 46 75 6e 63 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 FuncAlloc,.
1fbde 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 &i. );. retu
1fbdf 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a rn i;.} ../*.
1fbe0 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 78 ** This is the x
1fbe1 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 66 6f 72 ExprCallback for
1fbe2 20 61 20 74 72 65 65 20 77 61 6c 6b 65 72 2e 20 a tree walker.
1fbe3 20 49 74 20 69 73 20 75 73 65 64 20 74 6f 0a 2a It is used to.*
1fbe4 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 73 71 6c 69 * implement sqli
1fbe5 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 te3ExprAnalyzeAg
1fbe6 67 72 65 67 61 74 65 73 28 29 2e 20 20 53 65 65 gregates(). See
1fbe7 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c sqlite3ExprAnal
1fbe8 79 7a 65 41 67 67 72 65 67 61 74 65 73 0a 2a 2a yzeAggregates.**
1fbe9 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
1fbea 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
1fbeb 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 static int analy
1fbec 7a 65 41 67 67 72 65 67 61 74 65 28 57 61 6c 6b zeAggregate(Walk
1fbed 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 er *pWalker, Exp
1fbee 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 r *pExpr){. int
1fbef 20 69 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 i;. NameContex
1fbf0 74 20 2a 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 t *pNC = pWalker
1fbf1 2d 3e 75 2e 70 4e 43 3b 0a 20 20 50 61 72 73 65 ->u.pNC;. Parse
1fbf2 20 2a 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e *pParse = pNC->
1fbf3 70 50 61 72 73 65 3b 0a 20 20 53 72 63 4c 69 73 pParse;. SrcLis
1fbf4 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e t *pSrcList = pN
1fbf5 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 41 C->pSrcList;. A
1fbf6 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f ggInfo *pAggInfo
1fbf7 20 3d 20 70 4e 43 2d 3e 70 41 67 67 49 6e 66 6f = pNC->pAggInfo
1fbf8 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 ;.. switch( pEx
1fbf9 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 pr->op ){. ca
1fbfa 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e se TK_AGG_COLUMN
1fbfb 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f :. case TK_CO
1fbfc 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 LUMN: {. te
1fbfd 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
1fbfe 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
1fbff 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1fc00 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
1fc01 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 K_COLUMN );.
1fc02 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 /* Check to se
1fc03 65 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 e if the column
1fc04 69 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 is in one of the
1fc05 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 tables in the F
1fc06 52 4f 4d 0a 20 20 20 20 20 20 2a 2a 20 63 6c 61 ROM. ** cla
1fc07 75 73 65 20 6f 66 20 74 68 65 20 61 67 67 72 65 use of the aggre
1fc08 67 61 74 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 gate query */.
1fc09 20 20 20 20 69 66 28 20 70 53 72 63 4c 69 73 74 if( pSrcList
1fc0a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 ){. stru
1fc0b 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1fc0c 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 *pItem = pSrcLis
1fc0d 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f t->a;. fo
1fc0e 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 4c 69 73 r(i=0; i<pSrcLis
1fc0f 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 t->nSrc; i++, pI
1fc10 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 tem++){.
1fc11 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f struct AggInfo
1fc12 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 _col *pCol;.
1fc13 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
1fc14 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e >iTable==pItem->
1fc15 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 iCursor ){.
1fc16 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 /* If we
1fc17 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 reach this point
1fc18 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
1fc19 70 45 78 70 72 20 72 65 66 65 72 73 20 74 6f 20 pExpr refers to
1fc1a 61 20 74 61 62 6c 65 0a 20 20 20 20 20 20 20 20 a table.
1fc1b 20 20 20 20 2a 2a 20 74 68 61 74 20 69 73 20 69 ** that is i
1fc1c 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
1fc1d 65 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 e of the aggrega
1fc1e 74 65 20 71 75 65 72 79 2e 20 20 0a 20 20 20 20 te query. .
1fc1f 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
1fc20 20 20 20 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61 ** Make a
1fc21 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 n entry for the
1fc22 63 6f 6c 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e column in pAggIn
1fc23 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68 fo->aCol[] if th
1fc24 65 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 ere.
1fc25 2a 2a 20 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74 ** is not an ent
1fc26 72 79 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 ry there already
1fc27 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f .. */
1fc28 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
1fc29 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 k;.
1fc2a 70 43 6f 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d pCol = pAggInfo-
1fc2b 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 >aCol;.
1fc2c 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 for(k=0; k<pA
1fc2d 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b ggInfo->nColumn;
1fc2e 20 6b 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 k++, pCol++){.
1fc2f 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 if(
1fc30 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 pCol->iTable==p
1fc31 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a Expr->iTable &&.
1fc32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc33 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d pCol->iColumn=
1fc34 3d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 =pExpr->iColumn
1fc35 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1fc36 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1fc37 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1fc38 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fc39 20 20 20 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 if( (k>=pAgg
1fc3a 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 Info->nColumn).
1fc3b 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 && (
1fc3c 6b 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f k = addAggInfoCo
1fc3d 6c 75 6d 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c lumn(pParse->db,
1fc3e 20 70 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a pAggInfo))>=0 .
1fc3f 20 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 ){.
1fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f pCo
1fc41 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 l = &pAggInfo->a
1fc42 43 6f 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 Col[k];.
1fc43 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 pCol->pTab
1fc44 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a = pExpr->pTab;.
1fc45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 pC
1fc46 6f 6c 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 78 ol->iTable = pEx
1fc47 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 pr->iTable;.
1fc48 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e pCol->
1fc49 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d iColumn = pExpr-
1fc4a 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 >iColumn;.
1fc4b 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d pCol->iM
1fc4c 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e em = ++pParse->n
1fc4d 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 Mem;.
1fc4e 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 pCol->iSorter
1fc4f 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 Column = -1;.
1fc50 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d pCol-
1fc51 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a >pExpr = pExpr;.
1fc52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
1fc53 28 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f ( pAggInfo->pGro
1fc54 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 upBy ){.
1fc55 20 20 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e int j, n
1fc56 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1fc57 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 42 20 ExprList *pGB
1fc58 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f = pAggInfo->pGro
1fc59 75 70 42 79 3b 0a 20 20 20 20 20 20 20 20 20 20 upBy;.
1fc5a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 struct Exp
1fc5b 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 rList_item *pTer
1fc5c 6d 20 3d 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20 m = pGB->a;.
1fc5d 20 20 20 20 20 20 20 20 20 20 20 20 6e 20 3d 20 n =
1fc5e 70 47 42 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 pGB->nExpr;.
1fc5f 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 for(
1fc60 6a 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 j=0; j<n; j++, p
1fc61 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 Term++){.
1fc62 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 Expr
1fc63 2a 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 *pE = pTerm->pEx
1fc64 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 pr;.
1fc65 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 if( pE->op
1fc66 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 ==TK_COLUMN && p
1fc67 45 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 E->iTable==pExpr
1fc68 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 ->iTable &&.
1fc69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc6a 20 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 pE->iColumn==p
1fc6b 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b Expr->iColumn ){
1fc6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fc6d 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 pCol->iSort
1fc6e 65 72 43 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 erColumn = j;.
1fc6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc70 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1fc71 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1fc72 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }.
1fc73 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }.
1fc74 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 if(
1fc75 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f pCol->iSorterCo
1fc76 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 lumn<0 ){.
1fc77 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e pCol->
1fc78 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 iSorterColumn =
1fc79 70 41 67 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 pAggInfo->nSorti
1fc7a 6e 67 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 ngColumn++;.
1fc7b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1fc7c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1fc7d 20 20 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 /* There i
1fc7e 73 20 6e 6f 77 20 61 6e 20 65 6e 74 72 79 20 66 s now an entry f
1fc7f 6f 72 20 70 45 78 70 72 20 69 6e 20 70 41 67 67 or pExpr in pAgg
1fc80 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 Info->aCol[] (ei
1fc81 74 68 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 ther.
1fc82 20 2a 2a 20 62 65 63 61 75 73 65 20 69 74 20 77 ** because it w
1fc83 61 73 20 74 68 65 72 65 20 62 65 66 6f 72 65 20 as there before
1fc84 6f 72 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 or because we ju
1fc85 73 74 20 63 72 65 61 74 65 64 20 69 74 29 2e 0a st created it)..
1fc86 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 43 ** C
1fc87 6f 6e 76 65 72 74 20 74 68 65 20 70 45 78 70 72 onvert the pExpr
1fc88 20 74 6f 20 62 65 20 61 20 54 4b 5f 41 47 47 5f to be a TK_AGG_
1fc89 43 4f 4c 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 COLUMN referring
1fc8a 20 74 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 to that.
1fc8b 20 20 20 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f ** pAggInfo
1fc8c 2d 3e 61 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a ->aCol[] entry..
1fc8d 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 */.
1fc8e 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
1fc8f 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 ->pAggInfo = pAg
1fc90 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 20 gInfo;.
1fc91 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
1fc92 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 K_AGG_COLUMN;.
1fc93 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1fc94 3e 69 41 67 67 20 3d 20 6b 3b 0a 20 20 20 20 20 >iAgg = k;.
1fc95 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1fc96 20 20 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 } /* end
1fc97 69 66 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 if pExpr->iTable
1fc98 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 ==pItem->iCursor
1fc99 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 20 2f 2a */. } /*
1fc9a 20 65 6e 64 20 6c 6f 6f 70 20 6f 76 65 72 20 70 end loop over p
1fc9b 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 20 20 20 SrcList */.
1fc9c 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 }. return
1fc9d 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d WRC_Prune;. }
1fc9e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 . case TK_AGG
1fc9f 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 _FUNCTION: {.
1fca0 20 20 20 2f 2a 20 54 68 65 20 70 4e 43 2d 3e 6e /* The pNC->n
1fca1 44 65 70 74 68 3d 3d 30 20 74 65 73 74 20 63 61 Depth==0 test ca
1fca2 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66 uses aggregate f
1fca3 75 6e 63 74 69 6f 6e 73 20 69 6e 20 73 75 62 71 unctions in subq
1fca4 75 65 72 69 65 73 0a 20 20 20 20 20 20 2a 2a 20 ueries. **
1fca5 74 6f 20 62 65 20 69 67 6e 6f 72 65 64 20 2a 2f to be ignored */
1fca6 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e . if( pNC->
1fca7 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20 nDepth==0 ){.
1fca8 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f /* Check to
1fca9 20 73 65 65 20 69 66 20 70 45 78 70 72 20 69 73 see if pExpr is
1fcaa 20 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 20 a duplicate of
1fcab 61 6e 6f 74 68 65 72 20 61 67 67 72 65 67 61 74 another aggregat
1fcac 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 e . ** fu
1fcad 6e 63 74 69 6f 6e 20 74 68 61 74 20 69 73 20 61 nction that is a
1fcae 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 70 41 lready in the pA
1fcaf 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 ggInfo structure
1fcb0 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1fcb1 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e struct AggIn
1fcb2 66 6f 5f 66 75 6e 63 20 2a 70 49 74 65 6d 20 3d fo_func *pItem =
1fcb3 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 pAggInfo->aFunc
1fcb4 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d ;. for(i=
1fcb5 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 0; i<pAggInfo->n
1fcb6 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Func; i++, pItem
1fcb7 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ++){. i
1fcb8 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f f( sqlite3ExprCo
1fcb9 6d 70 61 72 65 28 70 49 74 65 6d 2d 3e 70 45 78 mpare(pItem->pEx
1fcba 70 72 2c 20 70 45 78 70 72 29 20 29 7b 0a 20 20 pr, pExpr) ){.
1fcbb 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
1fcbc 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1fcbd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1fcbe 66 28 20 69 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e f( i>=pAggInfo->
1fcbf 6e 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 nFunc ){.
1fcc0 20 20 20 2f 2a 20 70 45 78 70 72 20 69 73 20 6f /* pExpr is o
1fcc1 72 69 67 69 6e 61 6c 2e 20 20 4d 61 6b 65 20 61 riginal. Make a
1fcc2 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 70 41 new entry in pA
1fcc3 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 0a ggInfo->aFunc[].
1fcc4 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
1fcc5 20 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 u8 enc =
1fcc6 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b ENC(pParse->db);
1fcc7 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20 61 . i = a
1fcc8 64 64 41 67 67 49 6e 66 6f 46 75 6e 63 28 70 50 ddAggInfoFunc(pP
1fcc9 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e arse->db, pAggIn
1fcca 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 fo);. i
1fccb 66 28 20 69 3e 3d 30 20 29 7b 0a 20 20 20 20 20 f( i>=0 ){.
1fccc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 assert( !
1fccd 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
1fcce 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c pExpr, EP_xIsSel
1fccf 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 20 20 ect) );.
1fcd0 20 20 20 20 70 49 74 65 6d 20 3d 20 26 70 41 67 pItem = &pAg
1fcd1 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b gInfo->aFunc[i];
1fcd2 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 . pIt
1fcd3 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70 em->pExpr = pExp
1fcd4 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 r;. p
1fcd5 49 74 65 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 Item->iMem = ++p
1fcd6 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
1fcd7 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e pItem->
1fcd8 70 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 pFunc = sqlite3F
1fcd9 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 indFunction(pPar
1fcda 73 65 2d 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 se->db,.
1fcdb 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 (char
1fcdc 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a *)pExpr->token.z
1fcdd 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e , pExpr->token.n
1fcde 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1fcdf 20 20 20 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c pExpr->x.pL
1fce0 69 73 74 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70 ist ? pExpr->x.p
1fce1 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c List->nExpr : 0,
1fce2 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 enc, 0);.
1fce3 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
1fce4 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74 >flags & EP_Dist
1fce5 69 6e 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 inct ){.
1fce6 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 pItem->iDi
1fce7 73 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d stinct = pParse-
1fce8 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 >nTab++;.
1fce9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1fcea 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d pItem-
1fceb 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b >iDistinct = -1;
1fcec 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
1fced 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fcee 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
1fcef 4d 61 6b 65 20 70 45 78 70 72 20 70 6f 69 6e 74 Make pExpr point
1fcf0 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 to the appropri
1fcf1 61 74 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 ate pAggInfo->aF
1fcf2 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20 unc[] entry.
1fcf3 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 */. p
1fcf4 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 69 3b 0a Expr->iAgg = i;.
1fcf5 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1fcf6 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e AggInfo = pAggIn
1fcf7 66 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 fo;. retu
1fcf8 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
1fcf9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1fcfa 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e return WRC_Con
1fcfb 74 69 6e 75 65 3b 0a 7d 0a 73 74 61 74 69 63 20 tinue;.}.static
1fcfc 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 int analyzeAggre
1fcfd 67 61 74 65 73 49 6e 53 65 6c 65 63 74 28 57 61 gatesInSelect(Wa
1fcfe 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 lker *pWalker, S
1fcff 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b elect *pSelect){
1fd00 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a . NameContext *
1fd01 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 pNC = pWalker->u
1fd02 2e 70 4e 43 3b 0a 20 20 69 66 28 20 70 4e 43 2d .pNC;. if( pNC-
1fd03 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20 >nDepth==0 ){.
1fd04 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b pNC->nDepth++;
1fd05 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b . sqlite3Walk
1fd06 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 Select(pWalker,
1fd07 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e pSelect);. pN
1fd08 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 20 C->nDepth--;.
1fd09 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
1fd0a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
1fd0b 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
1fd0c 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a nue;. }.}../*.*
1fd0d 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 67 69 * Analyze the gi
1fd0e 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c ven expression l
1fd0f 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 67 67 72 65 ooking for aggre
1fd10 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 gate functions a
1fd11 6e 64 0a 2a 2a 20 66 6f 72 20 76 61 72 69 61 62 nd.** for variab
1fd12 6c 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f les that need to
1fd13 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 be added to the
1fd14 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20 pParse->aAgg[]
1fd15 61 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61 array..** Make a
1fd16 64 64 69 74 69 6f 6e 61 6c 20 65 6e 74 72 69 65 dditional entrie
1fd17 73 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 2d s to the pParse-
1fd18 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 20 61 73 >aAgg[] array as
1fd19 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a necessary..**.*
1fd1a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 * This routine s
1fd1b 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 hould only be ca
1fd1c 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 65 lled after the e
1fd1d 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 62 65 xpression has be
1fd1e 65 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 62 en.** analyzed b
1fd1f 79 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 y sqlite3Resolve
1fd20 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a ExprNames()..*/.
1fd21 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1fd22 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 oid sqlite3ExprA
1fd23 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 nalyzeAggregates
1fd24 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e (NameContext *pN
1fd25 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b C, Expr *pExpr){
1fd26 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 . Walker w;. w
1fd27 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d .xExprCallback =
1fd28 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 analyzeAggregat
1fd29 65 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 e;. w.xSelectCa
1fd2a 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 llback = analyze
1fd2b 41 67 67 72 65 67 61 74 65 73 49 6e 53 65 6c 65 AggregatesInSele
1fd2c 63 74 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 ct;. w.u.pNC =
1fd2d 70 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 pNC;. sqlite3Wa
1fd2e 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 lkExpr(&w, pExpr
1fd2f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c );.}../*.** Call
1fd30 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c sqlite3ExprAnal
1fd31 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 20 yzeAggregates()
1fd32 66 6f 72 20 65 76 65 72 79 20 65 78 70 72 65 73 for every expres
1fd33 73 69 6f 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65 78 sion in an.** ex
1fd34 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 pression list.
1fd35 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1fd36 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a r of errors..**.
1fd37 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69 ** If an error i
1fd38 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61 s found, the ana
1fd39 6c 79 73 69 73 20 69 73 20 63 75 74 20 73 68 6f lysis is cut sho
1fd3a 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 rt..*/.SQLITE_PR
1fd3b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1fd3c 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 e3ExprAnalyzeAgg
1fd3d 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 List(NameContext
1fd3e 20 2a 70 4e 43 2c 20 45 78 70 72 4c 69 73 74 20 *pNC, ExprList
1fd3f 2a 70 4c 69 73 74 29 7b 0a 20 20 73 74 72 75 63 *pList){. struc
1fd40 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
1fd41 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b *pItem;. int i;
1fd42 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a . if( pList ){.
1fd43 20 20 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c for(pItem=pL
1fd44 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 ist->a, i=0; i<p
1fd45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b List->nExpr; i++
1fd46 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
1fd47 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 sqlite3ExprAna
1fd48 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 70 lyzeAggregates(p
1fd49 4e 43 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 NC, pItem->pExpr
1fd4a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
1fd4b 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f /*.** Allocate o
1fd4c 72 20 64 65 61 6c 6c 6f 63 61 74 65 20 74 65 6d r deallocate tem
1fd4d 70 6f 72 61 72 79 20 75 73 65 20 72 65 67 69 73 porary use regis
1fd4e 74 65 72 73 20 64 75 72 69 6e 67 20 63 6f 64 65 ters during code
1fd4f 20 67 65 6e 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a generation..*/.
1fd50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1fd51 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d nt sqlite3GetTem
1fd52 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72 pReg(Parse *pPar
1fd53 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 se){. if( pPars
1fd54 65 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29 e->nTempReg==0 )
1fd55 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b 70 {. return ++p
1fd56 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d Parse->nMem;. }
1fd57 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 . return pParse
1fd58 2d 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70 50 61 ->aTempReg[--pPa
1fd59 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a rse->nTempReg];.
1fd5a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
1fd5b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c void sqlite3Rel
1fd5c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72 73 easeTempReg(Pars
1fd5d 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
1fd5e 52 65 67 29 7b 0a 20 20 69 66 28 20 69 52 65 67 Reg){. if( iReg
1fd5f 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d && pParse->nTem
1fd60 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70 pReg<ArraySize(p
1fd61 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 Parse->aTempReg)
1fd62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1fd63 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 69 73 xprWritableRegis
1fd64 74 65 72 28 70 50 61 72 73 65 2c 20 69 52 65 67 ter(pParse, iReg
1fd65 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 );. pParse->a
1fd66 54 65 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e TempReg[pParse->
1fd67 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 69 52 nTempReg++] = iR
1fd68 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a eg;. }.}../*.**
1fd69 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61 Allocate or dea
1fd6a 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 llocate a block
1fd6b 6f 66 20 6e 52 65 67 20 63 6f 6e 73 65 63 75 74 of nReg consecut
1fd6c 69 76 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2f ive registers.*/
1fd6d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1fd6e 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 int sqlite3GetTe
1fd6f 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70 mpRange(Parse *p
1fd70 50 61 72 73 65 2c 20 69 6e 74 20 6e 52 65 67 29 Parse, int nReg)
1fd71 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 {. int i, n;.
1fd72 69 20 3d 20 70 50 61 72 73 65 2d 3e 69 52 61 6e i = pParse->iRan
1fd73 67 65 52 65 67 3b 0a 20 20 6e 20 3d 20 70 50 61 geReg;. n = pPa
1fd74 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 3b 0a rse->nRangeReg;.
1fd75 20 20 69 66 28 20 6e 52 65 67 3c 3d 6e 20 26 26 if( nReg<=n &&
1fd76 20 21 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 !usedAsColumnCa
1fd77 63 68 65 28 70 50 61 72 73 65 2c 20 69 2c 20 69 che(pParse, i, i
1fd78 2b 6e 2d 31 29 20 29 7b 0a 20 20 20 20 70 50 61 +n-1) ){. pPa
1fd79 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 2b rse->iRangeReg +
1fd7a 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 61 72 = nReg;. pPar
1fd7b 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 2d 3d se->nRangeReg -=
1fd7c 20 6e 52 65 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a nReg;. }else{.
1fd7d 20 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d 3e i = pParse->
1fd7e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72 nMem+1;. pPar
1fd7f 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 se->nMem += nReg
1fd80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 ;. }. return i
1fd81 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
1fd82 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
1fd83 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 eleaseTempRange(
1fd84 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
1fd85 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 nt iReg, int nRe
1fd86 67 29 7b 0a 20 20 69 66 28 20 6e 52 65 67 3e 70 g){. if( nReg>p
1fd87 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 Parse->nRangeReg
1fd88 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e ){. pParse->
1fd89 6e 52 61 6e 67 65 52 65 67 20 3d 20 6e 52 65 67 nRangeReg = nReg
1fd8a 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 ;. pParse->iR
1fd8b 61 6e 67 65 52 65 67 20 3d 20 69 52 65 67 3b 0a angeReg = iReg;.
1fd8c 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a }.}../********
1fd8d 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 65 78 ****** End of ex
1fd8e 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a pr.c ***********
1fd8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fd91 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1fd92 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1fd93 65 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a e alter.c ******
1fd94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fd95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fd96 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1fd97 35 20 46 65 62 72 75 61 72 79 20 31 35 0a 2a 2a 5 February 15.**
1fd98 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1fd99 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1fd9a 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1fd9b 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1fd9c 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1fd9d 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1fd9e 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1fd9f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1fda0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1fda1 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1fda2 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1fda3 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1fda4 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1fda5 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1fda6 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1fda7 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1fda8 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1fda9 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1fdaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
1fdae 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1fdaf 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e ns C code routin
1fdb0 65 73 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 es that used to
1fdb1 67 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f generate VDBE co
1fdb2 64 65 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 de.** that imple
1fdb3 6d 65 6e 74 73 20 74 68 65 20 41 4c 54 45 52 20 ments the ALTER
1fdb4 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a TABLE command..*
1fdb5 2a 0a 2a 2a 20 24 49 64 3a 20 61 6c 74 65 72 2e *.** $Id: alter.
1fdb6 63 2c 76 20 31 2e 35 35 20 32 30 30 39 2f 30 33 c,v 1.55 2009/03
1fdb7 2f 32 34 20 31 35 3a 30 38 3a 31 30 20 64 72 68 /24 15:08:10 drh
1fdb8 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
1fdb9 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 The code in thi
1fdba 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69 73 s file only exis
1fdbb 74 73 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74 ts if we are not
1fdbc 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a omitting the.**
1fdbd 20 41 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f 67 ALTER TABLE log
1fdbe 69 63 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c ic from the buil
1fdbf 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 d..*/.#ifndef SQ
1fdc0 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 LITE_OMIT_ALTERT
1fdc1 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ABLE.../*.** Thi
1fdc2 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
1fdc3 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61 ed by SQL genera
1fdc4 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 ted to implement
1fdc5 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45 52 20 54 the .** ALTER T
1fdc6 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 ABLE command. Th
1fdc7 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
1fdc8 20 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 is the text of
1fdc9 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f a CREATE TABLE o
1fdca 72 0a 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 r.** CREATE INDE
1fdcb 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 73 X command. The s
1fdcc 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c 65 econd is a table
1fdcd 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c 65 name. The table
1fdce 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 74 68 65 name in .** the
1fdcf 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 CREATE TABLE or
1fdd0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 CREATE INDEX st
1fdd1 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c 61 atement is repla
1fdd2 63 65 64 20 77 69 74 68 20 74 68 65 20 74 68 69 ced with the thi
1fdd3 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61 rd.** argument a
1fdd4 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 72 65 nd the result re
1fdd5 74 75 72 6e 65 64 2e 20 45 78 61 6d 70 6c 65 73 turned. Examples
1fdd6 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 :.**.** sqlite_r
1fdd7 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 ename_table('CRE
1fdd8 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 2c ATE TABLE abc(a,
1fdd9 20 62 2c 20 63 29 27 2c 20 27 64 65 66 27 29 0a b, c)', 'def').
1fdda 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54 ** -> 'CREAT
1fddb 45 20 54 41 42 4c 45 20 64 65 66 28 61 2c 20 62 E TABLE def(a, b
1fddc 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 , c)'.**.** sqli
1fddd 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 te_rename_table(
1fdde 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 'CREATE INDEX i
1fddf 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 64 65 66 ON abc(a)', 'def
1fde0 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 ').** -> 'CR
1fde1 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20 EATE INDEX i ON
1fde2 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2f def(a, b, c)'.*/
1fde3 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e .static void ren
1fde4 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 0a 20 20 ameTableFunc(.
1fde5 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1fde6 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
1fde7 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 NotUsed,. sqlit
1fde8 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
1fde9 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ){. unsigned ch
1fdea 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d ar const *zSql =
1fdeb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1fdec 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
1fded 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f unsigned char co
1fdee 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 nst *zTableName
1fdef 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1fdf0 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a text(argv[1]);..
1fdf1 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 int token;. T
1fdf2 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e oken tname;. un
1fdf3 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 signed char cons
1fdf4 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a t *zCsr = zSql;.
1fdf5 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 int len = 0;.
1fdf6 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 char *zRet;..
1fdf7 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
1fdf8 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
1fdf9 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
1fdfa 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ;.. UNUSED_PARA
1fdfb 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
1fdfc 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 . /* The princi
1fdfd 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 ple used to loca
1fdfe 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d te the table nam
1fdff 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 e in the CREATE
1fe00 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74 TABLE . ** stat
1fe01 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68 ement is that th
1fe02 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 e table name is
1fe03 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70 the first non-sp
1fe04 61 63 65 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20 ace token that.
1fe05 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69 61 74 65 ** is immediate
1fe06 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 ly followed by a
1fe07 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f 55 53 49 TK_LP or TK_USI
1fe08 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 NG token.. */.
1fe09 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 if( zSql ){.
1fe0a 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 66 28 20 do {. if(
1fe0b 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20 !*zCsr ){.
1fe0c 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 /* Ran out of
1fe0d 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e input before fin
1fe0e 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 6e 67 20 ding an opening
1fe0f 62 72 61 63 6b 65 74 2e 20 52 65 74 75 72 6e 20 bracket. Return
1fe10 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 NULL. */.
1fe11 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
1fe12 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 .. /* Store
1fe13 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 the token that
1fe14 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 zCsr points to i
1fe15 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 n tname. */.
1fe16 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 tname.z = zCsr
1fe17 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 ;. tname.n
1fe18 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a = len;.. /*
1fe19 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f Advance zCsr to
1fe1a 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e the next token.
1fe1b 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 Store that toke
1fe1c 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e n type in 'token
1fe1d 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 ',. ** and
1fe1e 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c its length in 'l
1fe1f 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 en' (to be used
1fe20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f next iteration o
1fe21 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 f this loop)..
1fe22 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 */. do
1fe23 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b {. zCsr +
1fe24 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c = len;. l
1fe25 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 en = sqlite3GetT
1fe26 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 oken(zCsr, &toke
1fe27 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77 68 69 6c n);. } whil
1fe28 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 e( token==TK_SPA
1fe29 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 CE );. asse
1fe2a 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 20 20 20 rt( len>0 );.
1fe2b 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 21 } while( token!
1fe2c 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b 65 6e 21 =TK_LP && token!
1fe2d 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a 0a 20 20 =TK_USING );..
1fe2e 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65 33 zRet = sqlite3
1fe2f 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 2e 2a MPrintf(db, "%.*
1fe30 73 5c 22 25 77 5c 22 25 73 22 2c 20 74 6e 61 6d s\"%w\"%s", tnam
1fe31 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c e.z - zSql, zSql
1fe32 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65 , . zTable
1fe33 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e Name, tname.z+tn
1fe34 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 ame.n);. sqli
1fe35 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
1fe36 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d context, zRet, -
1fe37 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 1, SQLITE_DYNAMI
1fe38 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 C);. }.}..#ifnd
1fe39 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1fe3a 52 49 47 47 45 52 0a 2f 2a 20 54 68 69 73 20 66 RIGGER./* This f
1fe3b 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
1fe3c 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 by SQL generated
1fe3d 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
1fe3e 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 e.** ALTER TABLE
1fe3f 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69 command. The fi
1fe40 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
1fe41 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43 52 the text of a CR
1fe42 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 2a 2a EATE TRIGGER .**
1fe43 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 statement. The
1fe44 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c second is a tabl
1fe45 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c e name. The tabl
1fe46 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 e name in the CR
1fe47 45 41 54 45 20 0a 2a 2a 20 54 52 49 47 47 45 52 EATE .** TRIGGER
1fe48 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 statement is re
1fe49 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20 placed with the
1fe4a 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 61 third argument a
1fe4b 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 0a 2a nd the result .*
1fe4c 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 * returned. This
1fe4d 20 69 73 20 61 6e 61 6c 61 67 6f 75 73 20 74 6f is analagous to
1fe4e 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 renameTableFunc
1fe4f 28 29 20 61 62 6f 76 65 2c 20 65 78 63 65 70 74 () above, except
1fe50 20 66 6f 72 20 43 52 45 41 54 45 0a 2a 2a 20 54 for CREATE.** T
1fe51 52 49 47 47 45 52 2c 20 6e 6f 74 20 43 52 45 41 RIGGER, not CREA
1fe52 54 45 20 49 4e 44 45 58 20 61 6e 64 20 43 52 45 TE INDEX and CRE
1fe53 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f 0a 73 74 ATE TABLE..*/.st
1fe54 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 atic void rename
1fe55 54 72 69 67 67 65 72 46 75 6e 63 28 0a 20 20 73 TriggerFunc(. s
1fe56 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1fe57 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e context,. int N
1fe58 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 otUsed,. sqlite
1fe59 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
1fe5a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 {. unsigned cha
1fe5b 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 r const *zSql =
1fe5c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1fe5d 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 xt(argv[0]);. u
1fe5e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e nsigned char con
1fe5f 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d st *zTableName =
1fe60 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1fe61 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 ext(argv[1]);..
1fe62 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f int token;. To
1fe63 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 69 6e 74 ken tname;. int
1fe64 20 64 69 73 74 20 3d 20 33 3b 0a 20 20 75 6e 73 dist = 3;. uns
1fe65 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 igned char const
1fe66 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 *zCsr = zSql;.
1fe67 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 int len = 0;.
1fe68 63 68 61 72 20 2a 7a 52 65 74 3b 0a 20 20 73 71 char *zRet;. sq
1fe69 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
1fe6a 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
1fe6b 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
1fe6c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1fe6d 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 TER(NotUsed);..
1fe6e 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c /* The principl
1fe6f 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 e used to locate
1fe70 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 the table name
1fe71 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 52 in the CREATE TR
1fe72 49 47 47 45 52 20 0a 20 20 2a 2a 20 73 74 61 74 IGGER . ** stat
1fe73 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68 ement is that th
1fe74 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 e table name is
1fe75 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 the first token
1fe76 74 68 61 74 20 69 73 20 69 6d 6d 65 64 69 61 74 that is immediat
1fe77 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65 63 65 64 edly. ** preced
1fe78 65 64 20 62 79 20 65 69 74 68 65 72 20 54 4b 5f ed by either TK_
1fe79 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20 61 6e 64 ON or TK_DOT and
1fe7a 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 20 66 6f immediatedly fo
1fe7b 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65 0a 20 20 llowed by one.
1fe7c 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e 2c 20 54 ** of TK_WHEN, T
1fe7d 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b 5f 46 4f K_BEGIN or TK_FO
1fe7e 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 R.. */. if( zS
1fe7f 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 0a ql ){. do {..
1fe80 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72 if( !*zCsr
1fe81 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 ){. /* R
1fe82 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 an out of input
1fe83 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 74 before finding t
1fe84 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 52 he table name. R
1fe85 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 eturn NULL. */.
1fe86 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
1fe87 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
1fe88 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e Store the token
1fe89 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e 74 that zCsr point
1fe8a 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a s to in tname. *
1fe8b 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20 /. tname.z
1fe8c 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e = zCsr;. tn
1fe8d 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 ame.n = len;..
1fe8e 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a /* Advance z
1fe8f 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 Csr to the next
1fe90 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 token. Store tha
1fe91 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 t token type in
1fe92 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 'token',. *
1fe93 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 * and its length
1fe94 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 in 'len' (to be
1fe95 20 75 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 used next itera
1fe96 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f tion of this loo
1fe97 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 p).. */.
1fe98 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 do {.
1fe99 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 zCsr += len;.
1fe9a 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 len = sqlit
1fe9b 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c e3GetToken(zCsr,
1fe9c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 &token);.
1fe9d 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 }while( token==T
1fe9e 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 K_SPACE );.
1fe9f 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 assert( len>0 )
1fea0 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 56 61 72 69 ;.. /* Vari
1fea1 61 62 6c 65 20 27 64 69 73 74 27 20 73 74 6f 72 able 'dist' stor
1fea2 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 es the number of
1fea3 20 74 6f 6b 65 6e 73 20 72 65 61 64 20 73 69 6e tokens read sin
1fea4 63 65 20 74 68 65 20 6d 6f 73 74 0a 20 20 20 20 ce the most.
1fea5 20 20 2a 2a 20 72 65 63 65 6e 74 20 54 4b 5f 44 ** recent TK_D
1fea6 4f 54 20 6f 72 20 54 4b 5f 4f 4e 2e 20 54 68 69 OT or TK_ON. Thi
1fea7 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 s means that whe
1fea8 6e 20 61 20 57 48 45 4e 2c 20 46 4f 52 20 6f 72 n a WHEN, FOR or
1fea9 20 42 45 47 49 4e 20 0a 20 20 20 20 20 20 2a 2a BEGIN . **
1feaa 20 74 6f 6b 65 6e 20 69 73 20 72 65 61 64 20 61 token is read a
1feab 6e 64 20 27 64 69 73 74 27 20 65 71 75 61 6c 73 nd 'dist' equals
1feac 20 32 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 2, the conditio
1fead 6e 20 73 74 61 74 65 64 20 61 62 6f 76 65 0a 20 n stated above.
1feae 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6d 65 ** to be me
1feaf 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 t.. **.
1feb0 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 4f ** Note that O
1feb1 4e 20 63 61 6e 6e 6f 74 20 62 65 20 61 20 64 61 N cannot be a da
1feb2 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 6f 72 tabase, table or
1feb3 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20 73 6f column name, so
1feb4 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20 . ** there
1feb5 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 6f is no need to wo
1feb6 72 72 79 20 61 62 6f 75 74 20 73 79 6e 74 61 78 rry about syntax
1feb7 20 6c 69 6b 65 20 0a 20 20 20 20 20 20 2a 2a 20 like . **
1feb8 22 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 "CREATE TRIGGER
1feb9 2e 2e 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 42 45 47 ... ON ON.ON BEG
1feba 49 4e 20 2e 2e 2e 22 20 65 74 63 2e 0a 20 20 20 IN ..." etc..
1febb 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 69 73 74 */. dist
1febc 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 74 6f ++;. if( to
1febd 6b 65 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74 ken==TK_DOT || t
1febe 6f 6b 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0a 20 oken==TK_ON ){.
1febf 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 30 3b dist = 0;
1fec0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 77 . }. } w
1fec1 68 69 6c 65 28 20 64 69 73 74 21 3d 32 20 7c 7c hile( dist!=2 ||
1fec2 20 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45 4e (token!=TK_WHEN
1fec3 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46 4f && token!=TK_FO
1fec4 52 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 42 R && token!=TK_B
1fec5 45 47 49 4e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a EGIN) );.. /*
1fec6 20 56 61 72 69 61 62 6c 65 20 74 6e 61 6d 65 20 Variable tname
1fec7 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 now contains the
1fec8 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 74 token that is t
1fec9 68 65 20 6f 6c 64 20 74 61 62 6c 65 2d 6e 61 6d he old table-nam
1feca 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 e. ** in the
1fecb 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73 CREATE TRIGGER s
1fecc 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f tatement.. */
1fecd 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 . zRet = sqli
1fece 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 te3MPrintf(db, "
1fecf 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 74 %.*s\"%w\"%s", t
1fed0 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a name.z - zSql, z
1fed1 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 Sql, . zTa
1fed2 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a bleName, tname.z
1fed3 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 +tname.n);. s
1fed4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
1fed5 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 xt(context, zRet
1fed6 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e , -1, SQLITE_DYN
1fed7 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e AMIC);. }.}.#en
1fed8 64 69 66 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 dif /* !SQLITE
1fed9 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f _OMIT_TRIGGER */
1feda 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 ../*.** Register
1fedb 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 built-in functi
1fedc 6f 6e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 ons used to help
1fedd 20 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52 implement ALTER
1fede 20 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54 45 TABLE.*/.SQLITE
1fedf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1fee0 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 lite3AlterFuncti
1fee1 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ons(sqlite3 *db)
1fee2 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 {. sqlite3Creat
1fee3 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 eFunc(db, "sqlit
1fee4 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22 2c e_rename_table",
1fee5 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 2, SQLITE_UTF8,
1fee6 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 0,.
1fee7 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e ren
1fee8 61 6d 65 54 61 62 6c 65 46 75 6e 63 2c 20 30 2c ameTableFunc, 0,
1fee9 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 0);.#ifndef SQL
1feea 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1feeb 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 . sqlite3Create
1feec 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65 Func(db, "sqlite
1feed 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 22 _rename_trigger"
1feee 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , 2, SQLITE_UTF8
1feef 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 , 0,.
1fef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
1fef1 6e 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 2c nameTriggerFunc,
1fef2 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0, 0);.#endif.}
1fef3 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1fef4 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 57 the text of a W
1fef5 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20 HERE expression
1fef6 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 which can be use
1fef7 64 20 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a d to select all.
1fef8 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 ** temporary tri
1fef9 67 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 ggers on table p
1fefa 54 61 62 20 66 72 6f 6d 20 74 68 65 20 73 71 6c Tab from the sql
1fefb 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 ite_temp_master
1fefc 74 61 62 6c 65 2e 20 49 66 0a 2a 2a 20 74 61 62 table. If.** tab
1fefd 6c 65 20 70 54 61 62 20 68 61 73 20 6e 6f 20 74 le pTab has no t
1fefe 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 emporary trigger
1feff 73 2c 20 6f 72 20 69 73 20 69 74 73 65 6c 66 20 s, or is itself
1ff00 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 0a 2a stored in the .*
1ff01 2a 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 * temporary data
1ff02 62 61 73 65 2c 20 4e 55 4c 4c 20 69 73 20 72 65 base, NULL is re
1ff03 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 turned..*/.stati
1ff04 63 20 63 68 61 72 20 2a 77 68 65 72 65 54 65 6d c char *whereTem
1ff05 70 54 72 69 67 67 65 72 73 28 50 61 72 73 65 20 pTriggers(Parse
1ff06 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a *pParse, Table *
1ff07 70 54 61 62 29 7b 0a 20 20 54 72 69 67 67 65 72 pTab){. Trigger
1ff08 20 2a 70 54 72 69 67 3b 0a 20 20 63 68 61 72 20 *pTrig;. char
1ff09 2a 7a 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 63 *zWhere = 0;. c
1ff0a 68 61 72 20 2a 74 6d 70 20 3d 20 30 3b 0a 20 20 har *tmp = 0;.
1ff0b 63 6f 6e 73 74 20 53 63 68 65 6d 61 20 2a 70 54 const Schema *pT
1ff0c 65 6d 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72 empSchema = pPar
1ff0d 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 se->db->aDb[1].p
1ff0e 53 63 68 65 6d 61 3b 20 2f 2a 20 54 65 6d 70 20 Schema; /* Temp
1ff0f 64 62 20 73 63 68 65 6d 61 20 2a 2f 0a 0a 20 20 db schema */..
1ff10 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 /* If the table
1ff11 69 73 20 6e 6f 74 20 6c 6f 63 61 74 65 64 20 69 is not located i
1ff12 6e 20 74 68 65 20 74 65 6d 70 2d 64 62 20 28 69 n the temp-db (i
1ff13 6e 20 77 68 69 63 68 20 63 61 73 65 20 4e 55 4c n which case NUL
1ff14 4c 20 69 73 20 0a 20 20 2a 2a 20 72 65 74 75 72 L is . ** retur
1ff15 6e 65 64 2c 20 6c 6f 6f 70 20 74 68 72 6f 75 67 ned, loop throug
1ff16 68 20 74 68 65 20 74 61 62 6c 65 73 20 6c 69 73 h the tables lis
1ff17 74 20 6f 66 20 74 72 69 67 67 65 72 73 2e 20 46 t of triggers. F
1ff18 6f 72 20 65 61 63 68 20 74 72 69 67 67 65 72 0a or each trigger.
1ff19 20 20 2a 2a 20 74 68 61 74 20 69 73 20 6e 6f 74 ** that is not
1ff1a 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 65 6d part of the tem
1ff1b 70 2d 64 62 20 73 63 68 65 6d 61 2c 20 61 64 64 p-db schema, add
1ff1c 20 61 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 a clause to the
1ff1d 20 57 48 45 52 45 20 0a 20 20 2a 2a 20 65 78 70 WHERE . ** exp
1ff1e 72 65 73 73 69 6f 6e 20 62 65 69 6e 67 20 62 75 ression being bu
1ff1f 69 6c 74 20 75 70 20 69 6e 20 7a 57 68 65 72 65 ilt up in zWhere
1ff20 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 .. */. if( pTa
1ff21 62 2d 3e 70 53 63 68 65 6d 61 21 3d 70 54 65 6d b->pSchema!=pTem
1ff22 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 73 pSchema ){. s
1ff23 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1ff24 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 66 6f 72 rse->db;. for
1ff25 28 70 54 72 69 67 3d 73 71 6c 69 74 65 33 54 72 (pTrig=sqlite3Tr
1ff26 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 iggerList(pParse
1ff27 2c 20 70 54 61 62 29 3b 20 70 54 72 69 67 3b 20 , pTab); pTrig;
1ff28 70 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 pTrig=pTrig->pNe
1ff29 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 xt){. if( p
1ff2a 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 Trig->pSchema==p
1ff2b 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 TempSchema ){.
1ff2c 20 20 20 20 20 20 69 66 28 20 21 7a 57 68 65 72 if( !zWher
1ff2d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a e ){. z
1ff2e 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d Where = sqlite3M
1ff2f 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65 Printf(db, "name
1ff30 3d 25 51 22 2c 20 70 54 72 69 67 2d 3e 6e 61 6d =%Q", pTrig->nam
1ff31 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 e);. }els
1ff32 65 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6d 70 e{. tmp
1ff33 20 3d 20 7a 57 68 65 72 65 3b 0a 20 20 20 20 20 = zWhere;.
1ff34 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73 71 zWhere = sq
1ff35 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
1ff36 20 22 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22 "%s OR name=%Q"
1ff37 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 69 67 2d , zWhere, pTrig-
1ff38 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 >name);.
1ff39 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ff3a 64 62 2c 20 74 6d 70 29 3b 0a 20 20 20 20 20 20 db, tmp);.
1ff3b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1ff3c 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a }. }. return z
1ff3d 57 68 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Where;.}../*.**
1ff3e 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1ff3f 20 64 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 drop and reload
1ff40 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 the internal re
1ff41 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
1ff42 74 61 62 6c 65 0a 2a 2a 20 70 54 61 62 20 66 72 table.** pTab fr
1ff43 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c om the database,
1ff44 20 69 6e 63 6c 75 64 69 6e 67 20 74 72 69 67 67 including trigg
1ff45 65 72 73 20 61 6e 64 20 74 65 6d 70 6f 72 61 72 ers and temporar
1ff46 79 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 20 41 y triggers..** A
1ff47 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65 20 69 73 rgument zName is
1ff48 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1ff49 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 table in the da
1ff4a 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 74 tabase schema at
1ff4b 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 74 68 65 .** the time the
1ff4c 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 generated code
1ff4d 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68 69 is executed. Thi
1ff4e 73 20 63 61 6e 20 62 65 20 64 69 66 66 65 72 65 s can be differe
1ff4f 6e 74 20 66 72 6f 6d 0a 2a 2a 20 70 54 61 62 2d nt from.** pTab-
1ff50 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69 73 20 66 >zName if this f
1ff51 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 unction is being
1ff52 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64 65 20 called to code
1ff53 70 61 72 74 20 6f 66 20 61 6e 20 0a 2a 2a 20 22 part of an .** "
1ff54 41 4c 54 45 52 20 54 41 42 4c 45 20 52 45 4e 41 ALTER TABLE RENA
1ff55 4d 45 20 54 4f 22 20 73 74 61 74 65 6d 65 6e 74 ME TO" statement
1ff56 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1ff57 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 reloadTableSche
1ff58 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ma(Parse *pParse
1ff59 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 , Table *pTab, c
1ff5a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
1ff5b 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 ){. Vdbe *v;.
1ff5c 63 68 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 char *zWhere;.
1ff5d 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
1ff5e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1ff5f 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 dex of database
1ff60 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20 containing pTab
1ff61 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1ff62 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 E_OMIT_TRIGGER.
1ff63 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b Trigger *pTrig;
1ff64 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73 .#endif.. v = s
1ff65 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
1ff66 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 arse);. if( !v
1ff67 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
1ff68 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1ff69 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
1ff6a 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 pParse->db) );.
1ff6b 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
1ff6c 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 hemaToIndex(pPar
1ff6d 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 se->db, pTab->pS
1ff6e 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 chema);. assert
1ff6f 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 23 69 66 ( iDb>=0 );..#if
1ff70 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1ff71 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 44 72 _TRIGGER. /* Dr
1ff72 6f 70 20 61 6e 79 20 74 61 62 6c 65 20 74 72 69 op any table tri
1ff73 67 67 65 72 73 20 66 72 6f 6d 20 74 68 65 20 69 ggers from the i
1ff74 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20 nternal schema.
1ff75 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69 67 3d 73 */. for(pTrig=s
1ff76 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 qlite3TriggerLis
1ff77 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b t(pParse, pTab);
1ff78 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 pTrig; pTrig=pT
1ff79 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 rig->pNext){.
1ff7a 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d 20 73 int iTrigDb = s
1ff7b 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
1ff7c 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 dex(pParse->db,
1ff7d 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b pTrig->pSchema);
1ff7e 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 54 72 . assert( iTr
1ff7f 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54 72 igDb==iDb || iTr
1ff80 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 73 igDb==1 );. s
1ff81 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1ff82 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 (v, OP_DropTrigg
1ff83 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30 2c 20 er, iTrigDb, 0,
1ff84 30 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c 20 0, pTrig->name,
1ff85 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0);. }.#endif..
1ff86 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 61 /* Drop the ta
1ff87 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 66 72 ble and index fr
1ff88 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 om the internal
1ff89 73 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 schema */. sqli
1ff8a 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1ff8b 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 69 OP_DropTable, i
1ff8c 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e Db, 0, 0, pTab->
1ff8d 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 20 20 2f 2a zName, 0);.. /*
1ff8e 20 52 65 6c 6f 61 64 20 74 68 65 20 74 61 62 6c Reload the tabl
1ff8f 65 2c 20 69 6e 64 65 78 20 61 6e 64 20 70 65 72 e, index and per
1ff90 6d 61 6e 65 6e 74 20 74 72 69 67 67 65 72 20 73 manent trigger s
1ff91 63 68 65 6d 61 73 2e 20 2a 2f 0a 20 20 7a 57 68 chemas. */. zWh
1ff92 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 ere = sqlite3MPr
1ff93 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62 2c intf(pParse->db,
1ff94 20 22 74 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c 20 "tbl_name=%Q",
1ff95 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a zName);. if( !z
1ff96 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 3b 0a Where ) return;.
1ff97 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1ff98 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 Op4(v, OP_ParseS
1ff99 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 chema, iDb, 0, 0
1ff9a 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e , zWhere, P4_DYN
1ff9b 41 4d 49 43 29 3b 0a 0a 23 69 66 6e 64 65 66 20 AMIC);..#ifndef
1ff9c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
1ff9d 47 45 52 0a 20 20 2f 2a 20 4e 6f 77 2c 20 69 66 GER. /* Now, if
1ff9e 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f the table is no
1ff9f 74 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 t stored in the
1ffa0 74 65 6d 70 20 64 61 74 61 62 61 73 65 2c 20 72 temp database, r
1ffa1 65 6c 6f 61 64 20 61 6e 79 20 74 65 6d 70 20 0a eload any temp .
1ffa2 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 44 ** triggers. D
1ffa3 6f 6e 27 74 20 75 73 65 20 49 4e 28 2e 2e 2e 29 on't use IN(...)
1ffa4 20 69 6e 20 63 61 73 65 20 53 51 4c 49 54 45 5f in case SQLITE_
1ffa5 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 69 73 OMIT_SUBQUERY is
1ffa6 20 64 65 66 69 6e 65 64 2e 20 0a 20 20 2a 2f 0a defined. . */.
1ffa7 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68 if( (zWhere=wh
1ffa8 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 28 ereTempTriggers(
1ffa9 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d pParse, pTab))!=
1ffaa 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1ffab 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1ffac 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 31 2c _ParseSchema, 1,
1ffad 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 0, 0, zWhere, P
1ffae 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 4_DYNAMIC);. }.
1ffaf 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
1ffb0 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1ffb1 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 22 implement the "
1ffb2 41 4c 54 45 52 20 54 41 42 4c 45 20 78 78 78 20 ALTER TABLE xxx
1ffb3 52 45 4e 41 4d 45 20 54 4f 20 79 79 79 22 20 0a RENAME TO yyy" .
1ffb4 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a ** command. .*/.
1ffb5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1ffb6 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 oid sqlite3Alter
1ffb7 52 65 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20 50 RenameTable(. P
1ffb8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
1ffb9 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
1ffba 65 72 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 er context. */.
1ffbb 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 SrcList *pSrc,
1ffbc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1ffbd 65 20 74 61 62 6c 65 20 74 6f 20 72 65 6e 61 6d e table to renam
1ffbe 65 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 e. */. Token *p
1ffbf 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 Name
1ffc0 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 62 /* The new tab
1ffc1 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 le name. */.){.
1ffc2 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
1ffc3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
1ffc4 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74 tabase that cont
1ffc5 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 20 2a ains the table *
1ffc6 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 /. char *zDb;
1ffc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ffc8 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 Name of databas
1ffc9 65 20 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c 65 e iDb */. Table
1ffca 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 *pTab;
1ffcb 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 /* Table be
1ffcc 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 ing renamed */.
1ffcd 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 char *zName = 0
1ffce 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 55 ; /* NU
1ffcf 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20 76 65 LL-terminated ve
1ffd0 72 73 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20 2a rsion of pName *
1ffd1 2f 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 / . sqlite3 *db
1ffd2 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f = pParse->db; /
1ffd3 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * Database conne
1ffd4 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e ction */. int n
1ffd5 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 TabName;
1ffd6 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1ffd7 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 f UTF-8 characte
1ffd8 72 73 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20 2a rs in zTabName *
1ffd9 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1ffda 7a 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f 2a zTabName; /*
1ffdb 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20 6f Original name o
1ffdc 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 f the table */.
1ffdd 20 56 64 62 65 20 2a 76 3b 0a 23 69 66 6e 64 65 Vdbe *v;.#ifnde
1ffde 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1ffdf 49 47 47 45 52 0a 20 20 63 68 61 72 20 2a 7a 57 IGGER. char *zW
1ffe0 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 here = 0;
1ffe1 20 20 2f 2a 20 57 68 65 72 65 20 63 6c 61 75 73 /* Where claus
1ffe2 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d 70 e to locate temp
1ffe3 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e triggers */.#en
1ffe4 64 69 66 0a 20 20 69 6e 74 20 69 73 56 69 72 74 dif. int isVirt
1ffe5 75 61 6c 52 65 6e 61 6d 65 20 3d 20 30 3b 20 20 ualRename = 0;
1ffe6 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
1ffe7 69 73 20 61 20 76 2d 74 61 62 6c 65 20 77 69 74 is a v-table wit
1ffe8 68 20 61 6e 20 78 52 65 6e 61 6d 65 28 29 20 2a h an xRename() *
1ffe9 2f 0a 20 20 0a 20 20 69 66 28 20 64 62 2d 3e 6d /. . if( db->m
1ffea 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f allocFailed ) go
1ffeb 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 to exit_rename_t
1ffec 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 able;. assert(
1ffed 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b pSrc->nSrc==1 );
1ffee 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1ffef 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d e3BtreeHoldsAllM
1fff0 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 utexes(pParse->d
1fff1 62 29 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20 b) );.. pTab =
1fff2 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
1fff3 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 le(pParse, 0, pS
1fff4 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 rc->a[0].zName,
1fff5 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 pSrc->a[0].zData
1fff6 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 base);. if( !pT
1fff7 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 ab ) goto exit_r
1fff8 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 69 ename_table;. i
1fff9 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
1fffa 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 maToIndex(pParse
1fffb 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 ->db, pTab->pSch
1fffc 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 ema);. zDb = db
1fffd 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
1fffe 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e 55 ;.. /* Get a NU
1ffff 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64 20 76 65 LL terminated ve
20000 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 rsion of the new
20001 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a table name. */.
20002 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 zName = sqlite
20003 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
20004 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 b, pName);. if(
20005 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 65 !zName ) goto e
20006 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
20007 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 ;.. /* Check th
20008 61 74 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e at a table or in
20009 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d 65 dex named 'zName
2000a 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 ' does not alrea
2000b 64 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 6e dy exist. ** in
2000c 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 49 database iDb. I
2000d 66 20 73 6f 2c 20 74 68 69 73 20 69 73 20 61 6e f so, this is an
2000e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 error.. */. i
2000f 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 f( sqlite3FindTa
20010 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a ble(db, zName, z
20011 44 62 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 69 Db) || sqlite3Fi
20012 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d ndIndex(db, zNam
20013 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 e, zDb) ){. s
20014 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
20015 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 Parse, .
20016 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 "there is alread
20017 79 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 y another table
20018 6f 72 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 or index with th
20019 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e is name: %s", zN
2001a 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ame);. goto e
2001b 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
2001c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
2001d 20 73 75 72 65 20 69 74 20 69 73 20 6e 6f 74 20 sure it is not
2001e 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 62 a system table b
2001f 65 69 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f 72 eing altered, or
20020 20 61 20 72 65 73 65 72 76 65 64 20 6e 61 6d 65 a reserved name
20021 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 74 . ** that the t
20022 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 72 65 able is being re
20023 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20 named to.. */.
20024 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 6c if( sqlite3Strl
20025 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 en30(pTab->zName
20026 29 3e 36 20 0a 20 20 20 26 26 20 30 3d 3d 73 71 )>6 . && 0==sq
20027 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 lite3StrNICmp(pT
20028 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 ab->zName, "sqli
20029 74 65 5f 22 2c 20 37 29 0a 20 20 29 7b 0a 20 20 te_", 7). ){.
2002a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
2002b 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 g(pParse, "table
2002c 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 %s may not be a
2002d 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a ltered", pTab->z
2002e 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Name);. goto
2002f 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c exit_rename_tabl
20030 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c e;. }. if( SQL
20031 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 ITE_OK!=sqlite3C
20032 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 heckObjectName(p
20033 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b Parse, zName) ){
20034 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 . goto exit_r
20035 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d ename_table;. }
20036 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
20037 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 _OMIT_VIEW. if(
20038 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 pTab->pSelect )
20039 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
2003a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 orMsg(pParse, "v
2003b 69 65 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 iew %s may not b
2003c 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 e altered", pTab
2003d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f ->zName);. go
2003e 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 to exit_rename_t
2003f 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 able;. }.#endif
20040 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
20041 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
20042 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 ION. /* Invoke
20043 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f the authorizatio
20044 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 n callback. */.
20045 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 if( sqlite3Auth
20046 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 Check(pParse, SQ
20047 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 LITE_ALTER_TABLE
20048 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 , zDb, pTab->zNa
20049 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f me, 0) ){. go
2004a 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 to exit_rename_t
2004b 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 able;. }.#endif
2004c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
2004d 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
2004e 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 LE. if( sqlite3
2004f 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d ViewGetColumnNam
20050 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 es(pParse, pTab)
20051 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 ){. goto exi
20052 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a t_rename_table;.
20053 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 }. if( IsVirt
20054 75 61 6c 28 70 54 61 62 29 20 26 26 20 70 54 61 ual(pTab) && pTa
20055 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 b->pMod->pModule
20056 2d 3e 78 52 65 6e 61 6d 65 20 29 7b 0a 20 20 20 ->xRename ){.
20057 20 69 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 isVirtualRename
20058 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 = 1;. }.#endif
20059 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 .. /* Begin a t
2005a 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 63 ransaction and c
2005b 6f 64 65 20 74 68 65 20 56 65 72 69 66 79 43 6f ode the VerifyCo
2005c 6f 6b 69 65 20 66 6f 72 20 64 61 74 61 62 61 73 okie for databas
2005d 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54 68 65 e iDb. . ** The
2005e 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63 68 n modify the sch
2005f 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69 6e 63 ema cookie (sinc
20060 65 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c e the ALTER TABL
20061 45 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0a 20 E modifies the.
20062 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f 70 65 ** schema). Ope
20063 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 n a statement tr
20064 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 ansaction if the
20065 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 table is a virt
20066 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 0a ual. ** table..
20067 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 */. v = sqlit
20068 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
20069 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b );. if( v==0 ){
2006a 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 . goto exit_r
2006b 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d ename_table;. }
2006c 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 . sqlite3BeginW
2006d 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 riteOperation(pP
2006e 61 72 73 65 2c 20 69 73 56 69 72 74 75 61 6c 52 arse, isVirtualR
2006f 65 6e 61 6d 65 2c 20 69 44 62 29 3b 0a 20 20 73 ename, iDb);. s
20070 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b qlite3ChangeCook
20071 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b ie(pParse, iDb);
20072 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
20073 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c s a virtual tabl
20074 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52 e, invoke the xR
20075 65 6e 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e ename() function
20076 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20 if. ** one is
20077 64 65 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65 defined. The xRe
20078 6e 61 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 name() callback
20079 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 will modify the
2007a 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e names. ** of an
2007b 79 20 72 65 73 6f 75 72 63 65 73 20 75 73 65 64 y resources used
2007c 20 62 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20 by the v-table
2007d 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 implementation (
2007e 69 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a including other.
2007f 20 20 2a 2a 20 53 51 4c 69 74 65 20 74 61 62 6c ** SQLite tabl
20080 65 73 29 20 74 68 61 74 20 61 72 65 20 69 64 65 es) that are ide
20081 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 6e ntified by the n
20082 61 6d 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 ame of the virtu
20083 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 al table.. */.#
20084 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20085 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
20086 20 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 52 if( isVirtualR
20087 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74 ename ){. int
20088 20 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e i = ++pParse->n
20089 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Mem;. sqlite3
2008a 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
2008b 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 69 2c 20 _String8, 0, i,
2008c 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 0, zName, 0);.
2008d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2008e 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d Op4(v, OP_VRenam
2008f 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73 e, i, 0, 0,(cons
20090 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56 t char*)pTab->pV
20091 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 tab, P4_VTAB);.
20092 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
20093 66 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d figure out how m
20094 61 6e 79 20 55 54 46 2d 38 20 63 68 61 72 61 63 any UTF-8 charac
20095 74 65 72 73 20 61 72 65 20 69 6e 20 7a 4e 61 6d ters are in zNam
20096 65 20 2a 2f 0a 20 20 7a 54 61 62 4e 61 6d 65 20 e */. zTabName
20097 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 = pTab->zName;.
20098 20 6e 54 61 62 4e 61 6d 65 20 3d 20 73 71 6c 69 nTabName = sqli
20099 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a te3Utf8CharLen(z
2009a 54 61 62 4e 61 6d 65 2c 20 2d 31 29 3b 0a 0a 20 TabName, -1);..
2009b 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 73 /* Modify the s
2009c 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
2009d 6c 65 20 74 6f 20 75 73 65 20 74 68 65 20 6e 65 le to use the ne
2009e 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f w table name. */
2009f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 . sqlite3Nested
200a0 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 Parse(pParse,.
200a1 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 "UPDATE %Q.%
200a2 73 20 53 45 54 20 22 0a 23 69 66 64 65 66 20 53 s SET ".#ifdef S
200a3 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
200a4 45 52 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 ER. "sq
200a5 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d l = sqlite_renam
200a6 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 e_table(sql, %Q)
200a7 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 , ".#else.
200a8 20 20 20 20 22 73 71 6c 20 3d 20 43 41 53 45 20 "sql = CASE
200a9 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 ". "W
200aa 48 45 4e 20 74 79 70 65 20 3d 20 27 74 72 69 67 HEN type = 'trig
200ab 67 65 72 27 20 54 48 45 4e 20 73 71 6c 69 74 65 ger' THEN sqlite
200ac 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 28 _rename_trigger(
200ad 73 71 6c 2c 20 25 51 29 22 0a 20 20 20 20 20 20 sql, %Q)".
200ae 20 20 20 20 20 20 22 45 4c 53 45 20 73 71 6c 69 "ELSE sqli
200af 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 te_rename_table(
200b0 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20 22 0a sql, %Q) END, ".
200b1 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 #endif.
200b2 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 2c "tbl_name = %Q,
200b3 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 6e 61 ". "na
200b4 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20 me = CASE ".
200b5 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 74 79 "WHEN ty
200b6 70 65 3d 27 74 61 62 6c 65 27 20 54 48 45 4e 20 pe='table' THEN
200b7 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 %Q ".
200b8 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49 4b 45 "WHEN name LIKE
200b9 20 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 'sqlite_autoind
200ba 65 78 25 25 27 20 41 4e 44 20 74 79 70 65 3d 27 ex%%' AND type='
200bb 69 6e 64 65 78 27 20 54 48 45 4e 20 22 0a 20 20 index' THEN ".
200bc 20 20 20 20 20 20 20 20 20 20 20 22 27 73 71 6c "'sql
200bd 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 27 20 ite_autoindex_'
200be 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72 28 || %Q || substr(
200bf 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22 0a 20 20 name,%d+18) ".
200c0 20 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 "ELSE
200c1 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20 20 20 20 name END ".
200c2 20 22 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65 "WHERE tbl_name
200c3 3d 25 51 20 41 4e 44 20 22 0a 20 20 20 20 20 20 =%Q AND ".
200c4 20 20 20 20 22 28 74 79 70 65 3d 27 74 61 62 6c "(type='tabl
200c5 65 27 20 4f 52 20 74 79 70 65 3d 27 69 6e 64 65 e' OR type='inde
200c6 78 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69 67 x' OR type='trig
200c7 67 65 72 27 29 3b 22 2c 20 0a 20 20 20 20 20 20 ger');", .
200c8 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c zDb, SCHEMA_TABL
200c9 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c 20 7a E(iDb), zName, z
200ca 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 0a 23 69 Name, zName, .#i
200cb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
200cc 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 T_TRIGGER.
200cd 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66 0a 20 20 zName,.#endif.
200ce 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 61 62 4e zName, nTabN
200cf 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 0a 20 20 ame, zTabName.
200d0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
200d1 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 TE_OMIT_AUTOINCR
200d2 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66 20 74 68 EMENT. /* If th
200d3 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 e sqlite_sequenc
200d4 65 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 69 e table exists i
200d5 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2c n this database,
200d6 20 74 68 65 6e 20 75 70 64 61 74 65 20 0a 20 20 then update .
200d7 2a 2a 20 69 74 20 77 69 74 68 20 74 68 65 20 6e ** it with the n
200d8 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 0a 20 ew table name..
200d9 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
200da 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 3FindTable(db, "
200db 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 sqlite_sequence"
200dc 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71 , zDb) ){. sq
200dd 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
200de 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 (pParse,.
200df 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e "UPDATE \"%w\".
200e0 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 sqlite_sequence
200e1 73 65 74 20 6e 61 6d 65 20 3d 20 25 51 20 57 48 set name = %Q WH
200e2 45 52 45 20 6e 61 6d 65 20 3d 20 25 51 22 2c 0a ERE name = %Q",.
200e3 20 20 20 20 20 20 20 20 7a 44 62 2c 20 7a 4e 61 zDb, zNa
200e4 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 me, pTab->zName)
200e5 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ;. }.#endif..#i
200e6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
200e7 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 49 T_TRIGGER. /* I
200e8 66 20 74 68 65 72 65 20 61 72 65 20 54 45 4d 50 f there are TEMP
200e9 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 triggers on thi
200ea 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20 s table, modify
200eb 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f the sqlite_temp_
200ec 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 62 6c master. ** tabl
200ed 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68 69 73 e. Don't do this
200ee 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 if the table be
200ef 69 6e 67 20 41 4c 54 45 52 65 64 20 69 73 20 69 ing ALTERed is i
200f0 74 73 65 6c 66 20 6c 6f 63 61 74 65 64 20 69 6e tself located in
200f1 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 20 64 . ** the temp d
200f2 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 atabase.. */.
200f3 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72 if( (zWhere=wher
200f4 65 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50 eTempTriggers(pP
200f5 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20 arse, pTab))!=0
200f6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 ){. sqlite3Ne
200f7 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 stedParse(pParse
200f8 2c 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 , . "UPDA
200f9 54 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d TE sqlite_temp_m
200fa 61 73 74 65 72 20 53 45 54 20 22 0a 20 20 20 20 aster SET ".
200fb 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 "sql = s
200fc 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 qlite_rename_tri
200fd 67 67 65 72 28 73 71 6c 2c 20 25 51 29 2c 20 22 gger(sql, %Q), "
200fe 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 74 62 . "tb
200ff 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a 20 20 l_name = %Q ".
20100 20 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45 "WHERE
20101 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20 7a 4e %s;", zName, zN
20102 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 ame, zWhere);.
20103 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
20104 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 7d db, zWhere);. }
20105 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 .#endif.. /* Dr
20106 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 op and reload th
20107 65 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 e internal table
20108 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65 schema. */. re
20109 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 loadTableSchema(
2010a 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 4e pParse, pTab, zN
2010b 61 6d 65 29 3b 0a 0a 65 78 69 74 5f 72 65 6e 61 ame);..exit_rena
2010c 6d 65 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 me_table:. sqli
2010d 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 te3SrcListDelete
2010e 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 73 71 (db, pSrc);. sq
2010f 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
20110 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a zName);.}.../*.*
20111 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
20112 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
20113 61 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 an "ALTER TABLE
20114 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65 ... ADD" stateme
20115 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 70 nt.** has been p
20116 61 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 arsed. Argument
20117 70 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73 pColDef contains
20118 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 the text of the
20119 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 new.** column d
2011a 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a efinition..**.**
2011b 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 The Table struc
2011c 74 75 72 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 ture pParse->pNe
2011d 77 54 61 62 6c 65 20 77 61 73 20 65 78 74 65 6e wTable was exten
2011e 64 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a ded to include.*
2011f 2a 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e * the new column
20120 20 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 2e during parsing.
20121 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20122 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
20123 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c lterFinishAddCol
20124 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 umn(Parse *pPars
20125 65 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65 e, Token *pColDe
20126 66 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 f){. Table *pNe
20127 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w;
20128 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50 61 72 73 /* Copy of pPars
20129 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a e->pNewTable */.
2012a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 Table *pTab;
2012b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
2012c 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 able being alter
2012d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b ed */. int iDb;
2012e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2012f 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 /* Database nu
20130 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 mber */. const
20131 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 char *zDb;
20132 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
20133 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 name */. const
20134 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 char *zTab;
20135 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d /* Table nam
20136 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f e */. char *zCo
20137 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
20138 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 /* Null-termina
20139 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e ted column defin
2013a 69 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d ition */. Colum
2013b 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 n *pCol;
2013c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 /* The new
2013d 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 column */. Expr
2013e 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 20 20 20 *pDflt;
2013f 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 /* Default
20140 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e value for the n
20141 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73 ew column */. s
20142 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
20143 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
20144 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
20145 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 ion; */.. db =
20146 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 pParse->db;. if
20147 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c ( pParse->nErr |
20148 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
20149 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 ed ) return;. p
2014a 4e 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e New = pParse->pN
2014b 65 77 54 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 ewTable;. asser
2014c 74 28 20 70 4e 65 77 20 29 3b 0a 0a 20 20 61 73 t( pNew );.. as
2014d 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
2014e 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 eeHoldsAllMutexe
2014f 73 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d s(db) );. iDb =
20150 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
20151 49 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e Index(db, pNew->
20152 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 pSchema);. zDb
20153 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a = db->aDb[iDb].z
20154 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 26 Name;. zTab = &
20155 70 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b pNew->zName[16];
20156 20 20 2f 2a 20 53 6b 69 70 20 74 68 65 20 22 73 /* Skip the "s
20157 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 qlite_altertab_"
20158 20 70 72 65 66 69 78 20 6f 6e 20 74 68 65 20 6e prefix on the n
20159 61 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c 20 3d 20 ame */. pCol =
2015a 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 &pNew->aCol[pNew
2015b 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66 ->nCol-1];. pDf
2015c 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 lt = pCol->pDflt
2015d 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 ;. pTab = sqlit
2015e 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 e3FindTable(db,
2015f 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20 20 61 73 zTab, zDb);. as
20160 73 65 72 74 28 20 70 54 61 62 20 29 3b 0a 0a 23 sert( pTab );..#
20161 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20162 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
20163 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 . /* Invoke the
20164 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 authorization c
20165 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 allback. */. if
20166 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 ( sqlite3AuthChe
20167 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ck(pParse, SQLIT
20168 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a E_ALTER_TABLE, z
20169 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c Db, pTab->zName,
2016a 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 0) ){. retur
2016b 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 n;. }.#endif..
2016c 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 /* If the defau
2016d 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
2016e 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77 61 73 20 new column was
2016f 73 70 65 63 69 66 69 65 64 20 77 69 74 68 20 61 specified with a
20170 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61 6c 20 4e . ** literal N
20171 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 70 44 ULL, then set pD
20172 66 6c 74 20 74 6f 20 30 2e 20 54 68 69 73 20 73 flt to 0. This s
20173 69 6d 70 6c 69 66 69 65 73 20 63 68 65 63 6b 69 implifies checki
20174 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61 6e 20 53 ng. ** for an S
20175 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 QL NULL default
20176 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 below.. */. if
20177 28 20 70 44 66 6c 74 20 26 26 20 70 44 66 6c 74 ( pDflt && pDflt
20178 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b ->op==TK_NULL ){
20179 0a 20 20 20 20 70 44 66 6c 74 20 3d 20 30 3b 0a . pDflt = 0;.
2017a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
2017b 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 6f 6c that the new col
2017c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70 65 63 69 umn is not speci
2017d 66 69 65 64 20 61 73 20 50 52 49 4d 41 52 59 20 fied as PRIMARY
2017e 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 2e 0a 20 KEY or UNIQUE..
2017f 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 ** If there is
20180 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 a NOT NULL const
20181 72 61 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 raint, then the
20182 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f default value fo
20183 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d r the. ** colum
20184 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 4e 55 n must not be NU
20185 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 LL.. */. if( p
20186 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 20 29 Col->isPrimKey )
20187 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
20188 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 orMsg(pParse, "C
20189 61 6e 6e 6f 74 20 61 64 64 20 61 20 50 52 49 4d annot add a PRIM
2018a 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 22 29 ARY KEY column")
2018b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
2018c 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 }. if( pNew->pI
2018d 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 ndex ){. sqli
2018e 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
2018f 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 se, "Cannot add
20190 61 20 55 4e 49 51 55 45 20 63 6f 6c 75 6d 6e 22 a UNIQUE column"
20191 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
20192 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e 6e }. if( pCol->n
20193 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44 66 6c 74 otNull && !pDflt
20194 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
20195 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
20196 0a 20 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74 . "Cannot
20197 20 61 64 64 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 add a NOT NULL
20198 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64 65 66 61 column with defa
20199 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c 4c 22 29 ult value NULL")
2019a 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
2019b 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 }.. /* Ensure t
2019c 68 65 20 64 65 66 61 75 6c 74 20 65 78 70 72 65 he default expre
2019d 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65 74 68 69 ssion is somethi
2019e 6e 67 20 74 68 61 74 20 73 71 6c 69 74 65 33 56 ng that sqlite3V
2019f 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29 0a 20 alueFromExpr().
201a0 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c 65 20 28 ** can handle (
201a1 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52 45 4e 54 i.e. not CURRENT
201a2 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20 20 2a 2f _TIME etc.). */
201a3 0a 20 20 69 66 28 20 70 44 66 6c 74 20 29 7b 0a . if( pDflt ){.
201a4 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 sqlite3_valu
201a5 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20 69 66 28 e *pVal;. if(
201a6 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f sqlite3ValueFro
201a7 6d 45 78 70 72 28 64 62 2c 20 70 44 66 6c 74 2c mExpr(db, pDflt,
201a8 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
201a9 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2c 20 26 LITE_AFF_NONE, &
201aa 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 64 pVal) ){. d
201ab 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
201ac 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 = 1;. retur
201ad 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 n;. }. if(
201ae 20 21 70 56 61 6c 20 29 7b 0a 20 20 20 20 20 20 !pVal ){.
201af 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
201b0 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 pParse, "Cannot
201b1 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 77 69 74 add a column wit
201b2 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 20 64 h non-constant d
201b3 65 66 61 75 6c 74 22 29 3b 0a 20 20 20 20 20 20 efault");.
201b4 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
201b5 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 sqlite3ValueFr
201b6 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 0a 20 ee(pVal);. }..
201b7 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 43 /* Modify the C
201b8 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
201b9 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a 43 6f 6c ement. */. zCol
201ba 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e = sqlite3DbStrN
201bb 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 Dup(db, (char*)p
201bc 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43 6f 6c 44 ColDef->z, pColD
201bd 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 7a 43 ef->n);. if( zC
201be 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a ol ){. char *
201bf 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b 70 43 6f zEnd = &zCol[pCo
201c0 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20 20 20 20 lDef->n-1];.
201c1 77 68 69 6c 65 28 20 28 7a 45 6e 64 3e 7a 43 6f while( (zEnd>zCo
201c2 6c 20 26 26 20 2a 7a 45 6e 64 3d 3d 27 3b 27 29 l && *zEnd==';')
201c3 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 73 70 61 || sqlite3Isspa
201c4 63 65 28 2a 7a 45 6e 64 29 20 29 7b 0a 20 20 20 ce(*zEnd) ){.
201c5 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30 *zEnd-- = '\0
201c6 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c ';. }. sql
201c7 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
201c8 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
201c9 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e "UPDATE \"%w\".
201ca 25 73 20 53 45 54 20 22 0a 20 20 20 20 20 20 20 %s SET ".
201cb 20 20 20 22 73 71 6c 20 3d 20 73 75 62 73 74 72 "sql = substr
201cc 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c 20 27 2c (sql,1,%d) || ',
201cd 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 ' || %Q || subs
201ce 74 72 28 73 71 6c 2c 25 64 29 20 22 0a 20 20 20 tr(sql,%d) ".
201cf 20 20 20 20 20 22 57 48 45 52 45 20 74 79 70 65 "WHERE type
201d0 20 3d 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e = 'table' AND n
201d1 61 6d 65 20 3d 20 25 51 22 2c 20 0a 20 20 20 20 ame = %Q", .
201d2 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 zDb, SCHEMA_TA
201d3 42 4c 45 28 69 44 62 29 2c 20 70 4e 65 77 2d 3e BLE(iDb), pNew->
201d4 61 64 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43 addColOffset, zC
201d5 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c ol, pNew->addCol
201d6 4f 66 66 73 65 74 2b 31 2c 0a 20 20 20 20 20 20 Offset+1,.
201d7 7a 54 61 62 0a 20 20 20 20 29 3b 0a 20 20 20 20 zTab. );.
201d8 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
201d9 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20 , zCol);. }..
201da 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c /* If the defaul
201db 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e t value of the n
201dc 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c ew column is NUL
201dd 4c 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 L, then set the
201de 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61 74 file. ** format
201df 20 74 6f 20 32 2e 20 49 66 20 74 68 65 20 64 65 to 2. If the de
201e0 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 fault value of t
201e1 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 he new column is
201e2 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a 20 not NULL,. **
201e3 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 the file format
201e4 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f 0a becomes 3.. */.
201e5 20 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d sqlite3Minimum
201e6 46 69 6c 65 46 6f 72 6d 61 74 28 70 50 61 72 73 FileFormat(pPars
201e7 65 2c 20 69 44 62 2c 20 70 44 66 6c 74 20 3f 20 e, iDb, pDflt ?
201e8 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52 65 3 : 2);.. /* Re
201e9 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 load the schema
201ea 6f 66 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20 of the modified
201eb 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c 6f table. */. relo
201ec 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50 adTableSchema(pP
201ed 61 72 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62 arse, pTab, pTab
201ee 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a ->zName);.}../*.
201ef 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
201f0 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
201f1 65 20 70 61 72 73 65 72 20 61 66 74 65 72 20 74 e parser after t
201f2 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 6e he table-name in
201f3 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 41 .** an "ALTER TA
201f4 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 3e BLE <table-name>
201f5 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 20 ADD" statement
201f6 69 73 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d is parsed. Argum
201f7 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 20 ent .** pSrc is
201f8 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f 66 the full-name of
201f9 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
201fa 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 altered..**.**
201fb 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b This routine mak
201fc 65 73 20 61 20 28 70 61 72 74 69 61 6c 29 20 63 es a (partial) c
201fd 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 opy of the Table
201fe 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 6f structure.** fo
201ff 72 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e r the table bein
20200 67 20 61 6c 74 65 72 65 64 20 61 6e 64 20 73 65 g altered and se
20201 74 73 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 ts Parse.pNewTab
20202 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74 le to point.** t
20203 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 63 o it. Routines c
20204 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 alled by the par
20205 73 65 72 20 61 73 20 74 68 65 20 63 6f 6c 75 6d ser as the colum
20206 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20 n definition.**
20207 69 73 20 70 61 72 73 65 64 20 28 69 2e 65 2e 20 is parsed (i.e.
20208 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e sqlite3AddColumn
20209 28 29 29 20 61 64 64 20 74 68 65 20 6e 65 77 20 ()) add the new
2020a 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 0a Column data to .
2020b 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20 54 68 65 ** the copy. The
2020c 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 copy of the Tab
2020d 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 le structure is
2020e 64 65 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 6e deleted by token
2020f 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 20 ize.c .** after
20210 70 61 72 73 69 6e 67 20 69 73 20 66 69 6e 69 73 parsing is finis
20211 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 69 hed..**.** Routi
20212 6e 65 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 ne sqlite3AlterF
20213 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29 inishAddColumn()
20214 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 will be called
20215 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 to complete.** c
20216 6f 64 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52 oding the "ALTER
20217 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 TABLE ... ADD"
20218 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 statement..*/.SQ
20219 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
2021a 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 d sqlite3AlterBe
2021b 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 ginAddColumn(Par
2021c 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c se *pParse, SrcL
2021d 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61 ist *pSrc){. Ta
2021e 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61 62 ble *pNew;. Tab
2021f 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 le *pTab;. Vdbe
20220 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a *v;. int iDb;.
20221 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e int i;. int n
20222 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 Alloc;. sqlite3
20223 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
20224 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 b;.. /* Look up
20225 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
20226 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a 20 20 61 altered. */. a
20227 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 ssert( pParse->p
20228 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 NewTable==0 );.
20229 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
2022a 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 BtreeHoldsAllMut
2022b 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69 66 exes(db) );. if
2022c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
2022d 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 ed ) goto exit_b
2022e 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b egin_add_column;
2022f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 . pTab = sqlite
20230 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 3LocateTable(pPa
20231 72 73 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b rse, 0, pSrc->a[
20232 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 0].zName, pSrc->
20233 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b a[0].zDatabase);
20234 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 20 67 . if( !pTab ) g
20235 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 oto exit_begin_a
20236 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66 6e dd_column;..#ifn
20237 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
20238 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 VIRTUALTABLE. i
20239 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 f( IsVirtual(pTa
2023a 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 b) ){. sqlite
2023b 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
2023c 2c 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 , "virtual table
2023d 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 s may not be alt
2023e 65 72 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f ered");. goto
2023f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f exit_begin_add_
20240 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e 64 column;. }.#end
20241 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 if.. /* Make su
20242 72 65 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 re this is not a
20243 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 41 4c 54 n attempt to ALT
20244 45 52 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 ER a view. */.
20245 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 if( pTab->pSelec
20246 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
20247 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
20248 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 "Cannot add a c
20249 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69 65 77 22 olumn to a view"
2024a 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 );. goto exit
2024b 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d _begin_add_colum
2024c 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 n;. }.. assert
2024d 28 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 ( pTab->addColOf
2024e 66 73 65 74 3e 30 20 29 3b 0a 20 20 69 44 62 20 fset>0 );. iDb
2024f 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
20250 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d oIndex(db, pTab-
20251 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a >pSchema);.. /*
20252 20 50 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74 Put a copy of t
20253 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 20 he Table struct
20254 69 6e 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 in Parse.pNewTab
20255 6c 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 le for the. **
20256 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e sqlite3AddColumn
20257 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 () function and
20258 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66 friends to modif
20259 79 2e 20 20 42 75 74 20 6d 6f 64 69 66 79 0a 20 y. But modify.
2025a 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 62 79 20 ** the name by
2025b 61 64 64 69 6e 67 20 61 6e 20 22 73 71 6c 69 74 adding an "sqlit
2025c 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 70 72 65 e_altertab_" pre
2025d 66 69 78 2e 20 20 42 79 20 61 64 64 69 6e 67 20 fix. By adding
2025e 74 68 69 73 0a 20 20 2a 2a 20 70 72 65 66 69 78 this. ** prefix
2025f 2c 20 77 65 20 69 6e 73 75 72 65 20 74 68 61 74 , we insure that
20260 20 74 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 6e the name will n
20261 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 ot collide with
20262 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a an existing. **
20263 20 74 61 62 6c 65 20 62 65 63 61 75 73 65 20 75 table because u
20264 73 65 72 20 74 61 62 6c 65 20 61 72 65 20 6e 6f ser table are no
20265 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 68 61 76 t allowed to hav
20266 65 20 74 68 65 20 22 73 71 6c 69 74 65 5f 22 0a e the "sqlite_".
20267 20 20 2a 2a 20 70 72 65 66 69 78 20 6f 6e 20 74 ** prefix on t
20268 68 65 69 72 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a heir name.. */.
20269 20 20 70 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a pNew = (Table*
2026a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 )sqlite3DbMalloc
2026b 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
2026c 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 Table));. if( !
2026d 70 4e 65 77 20 29 20 67 6f 74 6f 20 65 78 69 74 pNew ) goto exit
2026e 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d _begin_add_colum
2026f 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 n;. pParse->pNe
20270 77 54 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 wTable = pNew;.
20271 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b pNew->nRef = 1;
20272 0a 20 20 70 4e 65 77 2d 3e 64 62 4d 65 6d 20 3d . pNew->dbMem =
20273 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3b 0a 20 20 pTab->dbMem;.
20274 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54 61 pNew->nCol = pTa
20275 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 65 72 b->nCol;. asser
20276 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30 20 t( pNew->nCol>0
20277 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 28 );. nAlloc = ((
20278 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f 38 (pNew->nCol-1)/8
20279 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65 72 74 )*8)+8;. assert
2027a 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d 3e ( nAlloc>=pNew->
2027b 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25 38 nCol && nAlloc%8
2027c 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70 4e ==0 && nAlloc-pN
2027d 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20 20 ew->nCol<8 );.
2027e 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 43 6f pNew->aCol = (Co
2027f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44 62 4d lumn*)sqlite3DbM
20280 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
20281 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e 41 6c zeof(Column)*nAl
20282 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e loc);. pNew->zN
20283 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 ame = sqlite3MPr
20284 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65 intf(db, "sqlite
20285 5f 61 6c 74 65 72 74 61 62 5f 25 73 22 2c 20 70 _altertab_%s", p
20286 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 Tab->zName);. i
20287 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c f( !pNew->aCol |
20288 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29 | !pNew->zName )
20289 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 {. db->malloc
2028a 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
2028b 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f goto exit_begin_
2028c 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a add_column;. }.
2028d 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 61 memcpy(pNew->a
2028e 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c Col, pTab->aCol,
2028f 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a sizeof(Column)*
20290 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66 pNew->nCol);. f
20291 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e or(i=0; i<pNew->
20292 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCol; i++){.
20293 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 Column *pCol = &
20294 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 pNew->aCol[i];.
20295 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d pCol->zName =
20296 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
20297 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 (db, pCol->zName
20298 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f );. pCol->zCo
20299 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c ll = 0;. pCol
2029a 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 ->zType = 0;.
2029b 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 pCol->pDflt = 0
2029c 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 53 ;. }. pNew->pS
2029d 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b chema = db->aDb[
2029e 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 iDb].pSchema;.
2029f 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 pNew->addColOffs
202a0 65 74 20 3d 20 70 54 61 62 2d 3e 61 64 64 43 6f et = pTab->addCo
202a1 6c 4f 66 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d lOffset;. pNew-
202a2 3e 6e 52 65 66 20 3d 20 31 3b 0a 0a 20 20 2f 2a >nRef = 1;.. /*
202a3 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 Begin a transac
202a4 74 69 6f 6e 20 61 6e 64 20 69 6e 63 72 65 6d 65 tion and increme
202a5 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f nt the schema co
202a6 6f 6b 69 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 okie. */. sqli
202a7 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 te3BeginWriteOpe
202a8 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 ration(pParse, 0
202a9 2c 20 69 44 62 29 3b 0a 20 20 76 20 3d 20 73 71 , iDb);. v = sq
202aa 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
202ab 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 29 rse);. if( !v )
202ac 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e goto exit_begin
202ad 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 73 _add_column;. s
202ae 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b qlite3ChangeCook
202af 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b ie(pParse, iDb);
202b0 0a 0a 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 ..exit_begin_add
202b1 5f 63 6f 6c 75 6d 6e 3a 0a 20 20 73 71 6c 69 74 _column:. sqlit
202b2 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 e3SrcListDelete(
202b3 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 72 65 74 db, pSrc);. ret
202b4 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f urn;.}.#endif /
202b5 2a 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 * SQLITE_ALTER_T
202b6 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a ABLE */../******
202b7 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
202b8 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a alter.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 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
202bc 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
202bd 69 6c 65 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a ile analyze.c **
202be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
202c1 30 30 35 20 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a 005 July 8.**.**
202c2 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
202c3 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
202c4 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
202c5 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
202c6 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
202c7 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
202c8 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
202c9 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
202ca 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
202cb 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
202cc 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
202cd 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
202ce 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
202cf 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
202d0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
202d1 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
202d2 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
202d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
202d8 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
202d9 63 6f 64 65 20 61 73 73 6f 63 69 61 74 65 64 20 code associated
202da 77 69 74 68 20 74 68 65 20 41 4e 41 4c 59 5a 45 with the ANALYZE
202db 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 command..**.**
202dc 40 28 23 29 20 24 49 64 3a 20 61 6e 61 6c 79 7a @(#) $Id: analyz
202dd 65 2e 63 2c 76 20 31 2e 35 31 20 32 30 30 39 2f e.c,v 1.51 2009/
202de 30 32 2f 32 38 20 31 30 3a 34 37 3a 34 32 20 64 02/28 10:47:42 d
202df 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
202e0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
202e1 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a TE_OMIT_ANALYZE.
202e2 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
202e3 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f ine generates co
202e4 64 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 68 de that opens th
202e5 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 e sqlite_stat1 t
202e6 61 62 6c 65 20 6f 6e 20 63 75 72 73 6f 72 0a 2a able on cursor.*
202e7 2a 20 69 53 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a * iStatCur..**.*
202e8 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f * If the sqlite_
202e9 73 74 61 74 31 20 74 61 62 6c 65 73 20 64 6f 65 stat1 tables doe
202ea 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 s not previously
202eb 20 65 78 69 73 74 2c 20 69 74 20 69 73 20 63 72 exist, it is cr
202ec 65 61 74 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 eated..** If it
202ed 64 6f 65 73 20 70 72 65 76 69 6f 75 73 6c 79 20 does previously
202ee 65 78 69 73 74 2c 20 61 6c 6c 20 65 6e 74 69 72 exist, all entir
202ef 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
202f0 74 68 20 74 61 62 6c 65 20 7a 57 68 65 72 65 0a th table zWhere.
202f1 2a 2a 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 20 ** are removed.
202f2 20 49 66 20 7a 57 68 65 72 65 3d 3d 30 20 74 68 If zWhere==0 th
202f3 65 6e 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 61 en all entries a
202f4 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 re removed..*/.s
202f5 74 61 74 69 63 20 76 6f 69 64 20 6f 70 65 6e 53 tatic void openS
202f6 74 61 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73 tatTable(. Pars
202f7 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
202f8 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
202f9 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
202fa 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 iDb,
202fb 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
202fc 61 73 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 ase we are looki
202fd 6e 67 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 ng in */. int i
202fe 53 74 61 74 43 75 72 2c 20 20 20 20 20 20 20 20 StatCur,
202ff 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 /* Open the s
20300 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c qlite_stat1 tabl
20301 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 e on this cursor
20302 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
20303 20 2a 7a 57 68 65 72 65 20 20 20 20 20 20 2f 2a *zWhere /*
20304 20 44 65 6c 65 74 65 20 65 6e 74 72 69 65 73 20 Delete entries
20305 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
20306 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b this table */.){
20307 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
20308 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 pParse->db;. D
20309 62 20 2a 70 44 62 3b 0a 20 20 69 6e 74 20 69 52 b *pDb;. int iR
2030a 6f 6f 74 50 61 67 65 3b 0a 20 20 75 38 20 63 72 ootPage;. u8 cr
2030b 65 61 74 65 53 74 61 74 31 20 3d 20 30 3b 0a 20 eateStat1 = 0;.
2030c 20 54 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 Table *pStat;.
2030d 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
2030e 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
2030f 29 3b 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 );.. if( v==0 )
20310 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 return;. asser
20311 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
20312 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 oldsAllMutexes(d
20313 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 b) );. assert(
20314 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 sqlite3VdbeDb(v)
20315 3d 3d 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 ==db );. pDb =
20316 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 &db->aDb[iDb];.
20317 20 69 66 28 20 28 70 53 74 61 74 20 3d 20 73 71 if( (pStat = sq
20318 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 lite3FindTable(d
20319 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 b, "sqlite_stat1
2031a 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d ", pDb->zName))=
2031b 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 =0 ){. /* The
2031c 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 sqlite_stat1 ta
2031d 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74 20 65 78 bles does not ex
2031e 69 73 74 2e 20 20 43 72 65 61 74 65 20 69 74 2e ist. Create it.
2031f 20 20 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 . ** Note t
20320 68 61 74 20 61 20 73 69 64 65 2d 65 66 66 65 63 hat a side-effec
20321 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 t of the CREATE
20322 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 TABLE statement
20323 69 73 20 74 6f 20 6c 65 61 76 65 0a 20 20 20 20 is to leave.
20324 2a 2a 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 ** the rootpage
20325 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
20326 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 in register pPa
20327 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54 rse->regRoot. T
20328 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d his is. ** im
20329 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 portant because
2032a 74 68 65 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 the OpenWrite op
2032b 63 6f 64 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 code below will
2032c 62 65 20 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a be needing it. *
2032d 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 /. sqlite3Nes
2032e 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
2032f 0a 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54 . "CREATE T
20330 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 ABLE %Q.sqlite_s
20331 74 61 74 31 28 74 62 6c 2c 69 64 78 2c 73 74 61 tat1(tbl,idx,sta
20332 74 29 22 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e t)",. pDb->
20333 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 zName. );.
20334 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 50 61 iRootPage = pPa
20335 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20 rse->regRoot;.
20336 20 20 63 72 65 61 74 65 53 74 61 74 31 20 3d 20 createStat1 =
20337 31 3b 20 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f 1; /* Cause roo
20338 74 70 61 67 65 20 74 6f 20 62 65 20 74 61 6b 65 tpage to be take
20339 6e 20 66 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74 n from top of st
2033a 61 63 6b 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 ack */. }else i
2033b 66 28 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 f( zWhere ){.
2033c 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 /* The sqlite_s
2033d 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74 tat1 table exist
2033e 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 65 s. Delete all e
2033f 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 ntries associate
20340 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 d with. ** th
20341 65 20 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 e table zWhere.
20342 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 */. sqlite3Ne
20343 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 stedParse(pParse
20344 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 ,. "DELETE
20345 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f FROM %Q.sqlite_
20346 73 74 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d stat1 WHERE tbl=
20347 25 51 22 2c 0a 20 20 20 20 20 20 20 70 44 62 2d %Q",. pDb-
20348 3e 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20 >zName, zWhere.
20349 20 20 20 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50 );. iRootP
2034a 61 67 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 age = pStat->tnu
2034b 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 m;. }else{.
2034c 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 /* The sqlite_st
2034d 61 74 31 20 74 61 62 6c 65 20 61 6c 72 65 61 64 at1 table alread
2034e 79 20 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74 y exists. Delet
2034f 65 20 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 e all rows. */.
20350 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 iRootPage = p
20351 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 Stat->tnum;.
20352 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20353 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 2(v, OP_Clear, p
20354 53 74 61 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 Stat->tnum, iDb)
20355 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e ;. }.. /* Open
20356 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
20357 31 20 74 61 62 6c 65 20 66 6f 72 20 77 72 69 74 1 table for writ
20358 69 6e 67 2e 20 55 6e 6c 65 73 73 20 69 74 20 77 ing. Unless it w
20359 61 73 20 63 72 65 61 74 65 64 0a 20 20 2a 2a 20 as created. **
2035a 62 79 20 74 68 69 73 20 76 64 62 65 20 70 72 6f by this vdbe pro
2035b 67 72 61 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f gram, lock it fo
2035c 72 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 65 r writing at the
2035d 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 shared-cache le
2035e 76 65 6c 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68 vel. . ** If th
2035f 69 73 20 76 64 62 65 20 64 69 64 20 63 72 65 61 is vdbe did crea
20360 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 te the sqlite_st
20361 61 74 31 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 at1 table, then
20362 69 74 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20 it must have .
20363 2a 2a 20 61 6c 72 65 61 64 79 20 6f 62 74 61 69 ** already obtai
20364 6e 65 64 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 ned a schema-loc
20365 6b 2c 20 6d 61 6b 69 6e 67 20 74 68 65 20 77 72 k, making the wr
20366 69 74 65 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61 ite-lock redunda
20367 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 nt.. */. if( !
20368 63 72 65 61 74 65 53 74 61 74 31 20 29 7b 0a 20 createStat1 ){.
20369 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c sqlite3TableL
2036a 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c ock(pParse, iDb,
2036b 20 69 52 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22 iRootPage, 1, "
2036c 73 71 6c 69 74 65 5f 73 74 61 74 31 22 29 3b 0a sqlite_stat1");.
2036d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
2036e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 eAddOp3(v, OP_Op
2036f 65 6e 57 72 69 74 65 2c 20 69 53 74 61 74 43 75 enWrite, iStatCu
20370 72 2c 20 69 52 6f 6f 74 50 61 67 65 2c 20 69 44 r, iRootPage, iD
20371 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 b);. sqlite3Vdb
20372 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c eChangeP4(v, -1,
20373 20 28 63 68 61 72 20 2a 29 33 2c 20 50 34 5f 49 (char *)3, P4_I
20374 4e 54 33 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 NT32);. sqlite3
20375 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 VdbeChangeP5(v,
20376 63 72 65 61 74 65 53 74 61 74 31 29 3b 0a 7d 0a createStat1);.}.
20377 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
20378 63 6f 64 65 20 74 6f 20 64 6f 20 61 6e 20 61 6e code to do an an
20379 61 6c 79 73 69 73 20 6f 66 20 61 6c 6c 20 69 6e alysis of all in
2037a 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 dices associated
2037b 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c with.** a singl
2037c 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 e table..*/.stat
2037d 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 4f ic void analyzeO
2037e 6e 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 neTable(. Parse
2037f 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 *pParse, /* P
20380 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f arser context */
20381 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 . Table *pTab,
20382 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 77 68 6f /* Table who
20383 73 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 74 se indices are t
20384 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f o be analyzed */
20385 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 2c . int iStatCur,
20386 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
20387 56 64 62 65 43 75 72 73 6f 72 20 74 68 61 74 20 VdbeCursor that
20388 77 72 69 74 65 73 20 74 68 65 20 73 71 6c 69 74 writes the sqlit
20389 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f e_stat1 table */
2038a 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20 . int iMem
2038b 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 /* Available
2038c 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e memory location
2038d 73 20 62 65 67 69 6e 20 68 65 72 65 20 2a 2f 0a s begin here */.
2038e 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 ){. Index *pIdx
2038f 3b 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 ; /* An inde
20390 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61 6c 79 x to being analy
20391 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 zed */. int iId
20392 78 43 75 72 3b 20 20 20 20 20 2f 2a 20 49 6e 64 xCur; /* Ind
20393 65 78 20 6f 66 20 56 64 62 65 43 75 72 73 6f 72 ex of VdbeCursor
20394 20 66 6f 72 20 69 6e 64 65 78 20 62 65 69 6e 67 for index being
20395 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 analyzed */. i
20396 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 nt nCol;
20397 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c /* Number of col
20398 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65 umns in the inde
20399 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 x */. Vdbe *v;
2039a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 /* The v
2039b 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 irtual machine b
2039c 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 2a 2f eing built up */
2039d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
2039e 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
2039f 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 ter */. int top
203a0 4f 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 OfLoop; /* The
203a1 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 top of the loop
203a2 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66 4c */. int endOfL
203a3 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 20 65 6e oop; /* The en
203a4 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f d of the loop */
203a5 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 . int addr;
203a6 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 72 65 /* The addre
203a7 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 75 63 ss of an instruc
203a8 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 tion */. int iD
203a9 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e b; /* In
203aa 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 dex of database
203ab 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20 containing pTab
203ac 2a 2f 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */.. v = sqlite
203ad 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
203ae 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20 ;. if( v==0 ||
203af 70 54 61 62 3d 3d 30 20 7c 7c 20 70 54 61 62 2d pTab==0 || pTab-
203b0 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 >pIndex==0 ){.
203b1 20 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61 6c 79 /* Do no analy
203b2 73 69 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 sis for tables t
203b3 68 61 74 20 68 61 76 65 20 6e 6f 20 69 6e 64 69 hat have no indi
203b4 63 65 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 ces */. retur
203b5 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 n;. }. assert(
203b6 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
203b7 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 dsAllMutexes(pPa
203b8 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44 rse->db) );. iD
203b9 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
203ba 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d aToIndex(pParse-
203bb 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 >db, pTab->pSche
203bc 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 ma);. assert( i
203bd 44 62 3e 3d 30 20 29 3b 0a 23 69 66 6e 64 65 66 Db>=0 );.#ifndef
203be 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
203bf 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 HORIZATION. if(
203c0 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
203c1 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 k(pParse, SQLITE
203c2 5f 41 4e 41 4c 59 5a 45 2c 20 70 54 61 62 2d 3e _ANALYZE, pTab->
203c3 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 zName, 0,.
203c4 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b pParse->db->aDb[
203c5 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a iDb].zName ) ){.
203c6 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
203c7 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 73 74 #endif.. /* Est
203c8 61 62 6c 69 73 68 20 61 20 72 65 61 64 2d 6c 6f ablish a read-lo
203c9 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 ck on the table
203ca 61 74 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 at the shared-ca
203cb 63 68 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a 20 20 che level. */.
203cc 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
203cd 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 (pParse, iDb, pT
203ce 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 ab->tnum, 0, pTa
203cf 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 49 b->zName);.. iI
203d0 64 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e dxCur = pParse->
203d1 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 nTab++;. for(pI
203d2 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b dx=pTab->pIndex;
203d3 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 pIdx; pIdx=pIdx
203d4 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 4b 65 ->pNext){. Ke
203d5 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 yInfo *pKey = sq
203d6 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 lite3IndexKeyinf
203d7 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b o(pParse, pIdx);
203d8 0a 20 20 20 20 69 6e 74 20 72 65 67 46 69 65 6c . int regFiel
203d9 64 73 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74 ds; /* Regist
203da 65 72 20 62 6c 6f 63 6b 20 66 6f 72 20 62 75 69 er block for bui
203db 6c 64 69 6e 67 20 72 65 63 6f 72 64 73 20 2a 2f lding records */
203dc 0a 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b . int regRec;
203dd 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
203de 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6d 70 6c er holding compl
203df 65 74 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 eted record */.
203e0 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 3b 20 int regTemp;
203e1 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 /* Temporar
203e2 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20 2a y use register *
203e3 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 43 6f 6c /. int regCol
203e4 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 ; /* Conte
203e5 6e 74 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 66 nt of a column f
203e6 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 62 65 rom the table be
203e7 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a ing analyzed */.
203e8 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 int regRowid
203e9 3b 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 66 ; /* Rowid f
203ea 6f 72 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 or the inserted
203eb 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e record */. in
203ec 74 20 72 65 67 46 32 3b 0a 0a 20 20 20 20 2f 2a t regF2;.. /*
203ed 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72 20 74 Open a cursor t
203ee 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 62 o the index to b
203ef 65 20 61 6e 61 6c 79 7a 65 64 0a 20 20 20 20 2a e analyzed. *
203f0 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 /. assert( iD
203f1 62 3d 3d 73 71 6c 69 74 65 33 53 63 68 65 6d 61 b==sqlite3Schema
203f2 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e ToIndex(pParse->
203f3 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d db, pIdx->pSchem
203f4 61 29 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d a) );. nCol =
203f5 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a pIdx->nColumn;.
203f6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
203f7 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp4(v, OP_Open
203f8 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70 Read, iIdxCur, p
203f9 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a Idx->tnum, iDb,.
203fa 20 20 20 20 20 20 20 20 28 63 68 61 72 20 2a 29 (char *)
203fb 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f pKey, P4_KEYINFO
203fc 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 _HANDOFF);. V
203fd 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
203fe 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 %s", pIdx->zName
203ff 29 29 3b 0a 20 20 20 20 72 65 67 46 69 65 6c 64 ));. regField
20400 73 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 3b s = iMem+nCol*2;
20401 0a 20 20 20 20 72 65 67 54 65 6d 70 20 3d 20 72 . regTemp = r
20402 65 67 52 6f 77 69 64 20 3d 20 72 65 67 43 6f 6c egRowid = regCol
20403 20 3d 20 72 65 67 46 69 65 6c 64 73 2b 33 3b 0a = regFields+3;.
20404 20 20 20 20 72 65 67 52 65 63 20 3d 20 72 65 67 regRec = reg
20405 43 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28 20 72 Col+1;. if( r
20406 65 67 52 65 63 3e 70 50 61 72 73 65 2d 3e 6e 4d egRec>pParse->nM
20407 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 em ){. pPar
20408 73 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65 se->nMem = regRe
20409 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a c;. }.. /*
2040a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 61 72 Memory cells ar
2040b 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 e used as follow
2040c 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s:. **. **
2040d 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a 20 20 mem[iMem]:
2040e 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 74 The t
2040f 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 otal number of r
20410 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ows in the table
20411 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b .. ** mem[
20412 69 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 20 20 iMem+1]:
20413 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73 Number of dis
20414 74 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20 tinct values in
20415 63 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 column 1. **
20416 20 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 .... **
20417 20 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a mem[iMem+nCol]:
20418 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f Number o
20419 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 f distinct value
2041a 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 s in column N.
2041b 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d ** mem[iMem
2041c 2b 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 20 4c +nCol+1] L
2041d 61 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c ast observed val
2041e 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 0a 20 ue of column 1.
2041f 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 20 ** ....
20420 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b ** mem[iMem+
20421 6e 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 4c 61 nCol+nCol]: La
20422 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75 st observed valu
20423 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 e of column N.
20424 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c **. ** Cell
20425 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 s iMem through i
20426 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 Mem+nCol are ini
20427 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 20 tialized to 0.
20428 54 68 65 20 6f 74 68 65 72 73 0a 20 20 20 20 2a The others. *
20429 2a 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 * are initialize
2042a 64 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a d to NULL.. *
2042b 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 /. for(i=0; i
2042c 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <=nCol; i++){.
2042d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2042e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
2042f 67 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b ger, 0, iMem+i);
20430 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
20431 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 =0; i<nCol; i++)
20432 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
20433 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
20434 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 Null, 0, iMem+nC
20435 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a ol+i+1);. }..
20436 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 61 6e /* Do the an
20437 61 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f 0a 20 alysis.. */.
20438 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 endOfLoop = s
20439 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 qlite3VdbeMakeLa
2043a 62 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 bel(v);. sqli
2043b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
2043c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78 OP_Rewind, iIdx
2043d 43 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b Cur, endOfLoop);
2043e 0a 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d . topOfLoop =
2043f 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
20440 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 entAddr(v);.
20441 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20442 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 2(v, OP_AddImm,
20443 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 66 6f iMem, 1);. fo
20444 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 r(i=0; i<nCol; i
20445 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
20446 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
20447 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 OP_Column, iIdxC
20448 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b 0a ur, i, regCol);.
20449 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
2044a 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 eAddOp3(v, OP_Ne
2044b 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65 , regCol, 0, iMe
2044c 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 m+nCol+i+1);.
2044d 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20 /**** TODO:
2044e 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 add collating se
2044f 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20 quence *****/.
20450 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
20451 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 hangeP5(v, SQLIT
20452 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 E_JUMPIFNULL);.
20453 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
20454 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
20455 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c _Goto, 0, endOfL
20456 6f 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d oop);. for(i=
20457 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0; i<nCol; i++){
20458 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
20459 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 6f beJumpHere(v, to
2045a 70 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69 20 2b pOfLoop + 2*(i +
2045b 20 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 1));. sqli
2045c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
2045d 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d OP_AddImm, iMem
2045e 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 +i+1, 1);.
2045f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20460 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 3(v, OP_Column,
20461 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d iIdxCur, i, iMem
20462 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 +nCol+i+1);.
20463 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
20464 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c eResolveLabel(v,
20465 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 endOfLoop);.
20466 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20467 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 p2(v, OP_Next, i
20468 49 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f IdxCur, topOfLoo
20469 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 p);. sqlite3V
2046a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
2046b 43 6c 6f 73 65 2c 20 69 49 64 78 43 75 72 29 3b Close, iIdxCur);
2046c 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 .. /* Store t
2046d 68 65 20 72 65 73 75 6c 74 73 2e 20 20 0a 20 20 he results. .
2046e 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
2046f 72 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67 result is a sing
20470 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71 le row of the sq
20471 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 lite_stat1 table
20472 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20 . The first.
20473 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20 ** two columns
20474 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 are the names of
20475 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 the table and i
20476 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64 ndex. The third
20477 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69 column. ** i
20478 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f s a string compo
20479 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 sed of a list of
2047a 20 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74 integer statist
2047b 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20 ics about the.
2047c 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65 ** index. The
2047d 20 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69 first integer i
2047e 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68 n the list is th
2047f 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
20480 66 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a f entries. **
20481 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20 in the index.
20482 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64 There is one add
20483 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 itional integer
20484 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 in the list for
20485 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 each. ** colu
20486 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e mn of the table.
20487 20 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61 This additiona
20488 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67 l integer is a g
20489 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79 uess of how many
2048a 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20 . ** rows of
2048b 74 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e the table the in
2048c 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e dex will select.
2048d 20 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f If D is the co
2048e 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a unt of distinct.
2048f 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e ** values an
20490 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c d K is the total
20491 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c number of rows,
20492 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65 then the intege
20493 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20 r is computed.
20494 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a ** as:. **.
20495 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20 ** I
20496 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20 = (K+D-1)/D.
20497 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d **. ** If K==
20498 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20 0 then no entry
20499 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65 is made into the
2049a 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 sqlite_stat1 ta
2049b 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66 ble. . ** If
2049c 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 K>0 then it is
2049d 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20 always the case
2049e 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73 the D>0 so divis
2049f 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20 ion by zero.
204a0 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73 ** is never poss
204a1 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ible.. */.
204a2 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 addr = sqlite3V
204a3 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
204a4 49 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20 IfNot, iMem);.
204a5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
204a6 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 Op4(v, OP_String
204a7 38 2c 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2c 8, 0, regFields,
204a8 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0, pTab->zName,
204a9 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
204aa 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
204ab 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 _String8, 0, reg
204ac 46 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70 49 64 Fields+1, 0, pId
204ad 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 x->zName, 0);.
204ae 20 20 72 65 67 46 32 20 3d 20 72 65 67 46 69 65 regF2 = regFie
204af 6c 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 lds+2;. sqlit
204b0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
204b1 4f 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 OP_SCopy, iMem,
204b2 72 65 67 46 32 29 3b 0a 20 20 20 20 66 6f 72 28 regF2);. for(
204b3 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b i=0; i<nCol; i++
204b4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
204b5 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
204b6 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 _String8, 0, reg
204b7 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30 29 Temp, 0, " ", 0)
204b8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
204b9 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
204ba 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c Concat, regTemp,
204bb 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a regF2, regF2);.
204bc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
204bd 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 eAddOp3(v, OP_Ad
204be 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b d, iMem, iMem+i+
204bf 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 1, regTemp);.
204c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
204c1 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d dOp2(v, OP_AddIm
204c2 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b m, regTemp, -1);
204c3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
204c4 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 beAddOp3(v, OP_D
204c5 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c ivide, iMem+i+1,
204c6 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d regTemp, regTem
204c7 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 p);. sqlite
204c8 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
204c9 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70 P_ToInt, regTemp
204ca 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
204cb 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
204cc 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 _Concat, regTemp
204cd 2c 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b , regF2, regF2);
204ce 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
204cf 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
204d0 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 OP_MakeRecord, r
204d1 65 67 46 69 65 6c 64 73 2c 20 33 2c 20 72 65 67 egFields, 3, reg
204d2 52 65 63 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a Rec, "aaa", 0);.
204d3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
204d4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 ddOp2(v, OP_NewR
204d5 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20 owid, iStatCur,
204d6 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 regRowid);. s
204d7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
204d8 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 (v, OP_Insert, i
204d9 53 74 61 74 43 75 72 2c 20 72 65 67 52 65 63 2c StatCur, regRec,
204da 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 regRowid);.
204db 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
204dc 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 eP5(v, OPFLAG_AP
204dd 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 PEND);. sqlit
204de 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
204df 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a , addr);. }.}..
204e0 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
204e1 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61 ode that will ca
204e2 75 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 use the most rec
204e3 65 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 ent index analys
204e4 69 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64 is to.** be laod
204e5 65 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c ed into internal
204e6 20 68 61 73 68 20 74 61 62 6c 65 73 20 77 68 65 hash tables whe
204e7 72 65 20 69 73 20 63 61 6e 20 62 65 20 75 73 65 re is can be use
204e8 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
204e9 64 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 50 d loadAnalysis(P
204ea 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
204eb 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a t iDb){. Vdbe *
204ec 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
204ed 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 be(pParse);. if
204ee 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( v ){. sqlit
204ef 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
204f0 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c OP_LoadAnalysis,
204f1 20 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a iDb);. }.}../*
204f2 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
204f3 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 e that will do a
204f4 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e n analysis of an
204f5 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 entire database
204f6 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
204f7 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 analyzeDatabase(
204f8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
204f9 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 nt iDb){. sqlit
204fa 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
204fb 3e 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 >db;. Schema *p
204fc 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 Schema = db->aDb
204fd 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 [iDb].pSchema;
204fe 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 /* Schema of d
204ff 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 atabase iDb */.
20500 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 HashElem *k;.
20501 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20 int iStatCur;.
20502 69 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c int iMem;.. sql
20503 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 ite3BeginWriteOp
20504 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 eration(pParse,
20505 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 0, iDb);. iStat
20506 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 Cur = pParse->nT
20507 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74 ab++;. openStat
20508 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 Table(pParse, iD
20509 62 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b b, iStatCur, 0);
2050a 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 . iMem = pParse
2050b 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28 ->nMem+1;. for(
2050c 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 k=sqliteHashFirs
2050d 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 t(&pSchema->tblH
2050e 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 ash); k; k=sqlit
2050f 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 eHashNext(k)){.
20510 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d Table *pTab =
20511 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 (Table*)sqliteH
20512 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 ashData(k);.
20513 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 analyzeOneTable(
20514 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 pParse, pTab, iS
20515 74 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 tatCur, iMem);.
20516 20 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 }. loadAnalysi
20517 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a s(pParse, iDb);.
20518 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
20519 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c e code that will
2051a 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 do an analysis
2051b 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c of a single tabl
2051c 65 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 e in.** a databa
2051d 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f se..*/.static vo
2051e 69 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 id analyzeTable(
2051f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 Parse *pParse, T
20520 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 able *pTab){. i
20521 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 nt iDb;. int iS
20522 74 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 tatCur;.. asser
20523 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 t( pTab!=0 );.
20524 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
20525 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
20526 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 xes(pParse->db)
20527 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 );. iDb = sqlit
20528 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
20529 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 pParse->db, pTab
2052a 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 ->pSchema);. sq
2052b 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f lite3BeginWriteO
2052c 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c peration(pParse,
2052d 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 0, iDb);. iSta
2052e 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e tCur = pParse->n
2052f 54 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 Tab++;. openSta
20530 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 tTable(pParse, i
20531 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54 Db, iStatCur, pT
20532 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e ab->zName);. an
20533 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 alyzeOneTable(pP
20534 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61 arse, pTab, iSta
20535 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d tCur, pParse->nM
20536 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61 em+1);. loadAna
20537 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44 lysis(pParse, iD
20538 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e b);.}../*.** Gen
20539 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 erate code for t
2053a 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 he ANALYZE comma
2053b 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 nd. The parser
2053c 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 calls this routi
2053d 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65 ne.** when it re
2053e 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c cognizes an ANAL
2053f 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a YZE command..**.
20540 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a ** ANALYZ
20541 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
20542 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 --
20543 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 1.** ANAL
20544 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20 YZE <database>
20545 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d -
20546 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e - 2.** AN
20547 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73 ALYZE ?<databas
20548 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 e>.?<tablename>
20549 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d -- 3.**.** Form
2054a 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 1 causes all in
2054b 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 dices in all att
2054c 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 ached databases
2054d 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a to be analyzed..
2054e 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a ** Form 2 analyz
2054f 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74 es all indices t
20550 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 he single databa
20551 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72 se named..** For
20552 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c m 3 analyzes all
20553 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 indices associa
20554 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d ted with the nam
20555 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c ed table..*/.SQL
20556 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
20557 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 sqlite3Analyze(
20558 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 Parse *pParse, T
20559 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f oken *pName1, To
2055a 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 ken *pName2){.
2055b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
2055c 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 arse->db;. int
2055d 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 iDb;. int i;.
2055e 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20 char *z, *zDb;.
2055f 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 Table *pTab;.
20560 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d Token *pTableNam
20561 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 e;.. /* Read th
20562 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
20563 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f a. If an error o
20564 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 ccurs, leave an
20565 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 error message.
20566 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 ** and code in p
20567 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e Parse and return
20568 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 NULL. */. asse
20569 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
2056a 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
2056b 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 pParse->db) );.
2056c 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
2056d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d sqlite3ReadSchem
2056e 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 a(pParse) ){.
2056f 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 return;. }..
20570 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b if( pName1==0 ){
20571 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a 20 . /* Form 1:
20572 20 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74 68 Analyze everyth
20573 69 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 ing */. for(i
20574 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
20575 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 ++){. if( i
20576 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 ==1 ) continue;
20577 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c 79 /* Do not analy
20578 7a 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 ze the TEMP data
20579 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 6e base */. an
2057a 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 alyzeDatabase(pP
2057b 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a arse, i);. }.
2057c 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d }else if( pNam
2057d 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d e2==0 || pName2-
2057e 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 >n==0 ){. /*
2057f 46 6f 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65 Form 2: Analyze
20580 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 the database or
20581 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a table named */.
20582 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 iDb = sqlite
20583 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 3FindDb(db, pNam
20584 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 e1);. if( iDb
20585 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61 >=0 ){. ana
20586 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 lyzeDatabase(pPa
20587 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d rse, iDb);. }
20588 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 else{. z =
20589 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
2058a 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 oken(db, pName1)
2058b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b ;. if( z ){
2058c 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 . pTab =
2058d 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
2058e 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c le(pParse, 0, z,
2058f 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 0);. sql
20590 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
20591 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
20592 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Tab ){.
20593 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 analyzeTable(pP
20594 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 arse, pTab);.
20595 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
20596 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
20597 20 20 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61 /* Form 3: Ana
20598 6c 79 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71 lyze the fully q
20599 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e ualified table n
2059a 61 6d 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d ame */. iDb =
2059b 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e sqlite3TwoPartN
2059c 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d ame(pParse, pNam
2059d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61 e1, pName2, &pTa
2059e 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 bleName);. if
2059f 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 ( iDb>=0 ){.
205a0 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b zDb = db->aDb[
205a1 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 iDb].zName;.
205a2 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d z = sqlite3Nam
205a3 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 eFromToken(db, p
205a4 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 TableName);.
205a5 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 if( z ){.
205a6 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 pTab = sqlite
205a7 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 3LocateTable(pPa
205a8 72 73 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b rse, 0, z, zDb);
205a9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
205aa 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 DbFree(db, z);.
205ab 20 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 if( pTab
205ac 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 ){. ana
205ad 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 lyzeTable(pParse
205ae 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 , pTab);.
205af 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
205b0 20 20 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a . }.}../*.**
205b1 20 55 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e Used to pass in
205b2 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 formation from t
205b3 68 65 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64 he analyzer read
205b4 65 72 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 er through to th
205b5 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f e.** callback ro
205b6 75 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 utine..*/.typede
205b7 66 20 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69 f struct analysi
205b8 73 49 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e sInfo analysisIn
205b9 66 6f 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79 fo;.struct analy
205ba 73 69 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 sisInfo {. sqli
205bb 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 te3 *db;. const
205bc 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 char *zDatabase
205bd 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ;.};../*.** This
205be 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 callback is inv
205bf 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 oked once for ea
205c0 63 68 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65 ch index when re
205c1 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c ading the.** sql
205c2 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e ite_stat1 table.
205c3 20 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67 .**.** arg
205c4 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 v[0] = name of t
205c5 68 65 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 he index.**
205c6 61 72 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74 argv[1] = result
205c7 73 20 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20 s of analysis -
205c8 6f 6e 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65 on integer for e
205c9 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 ach column.*/.st
205ca 61 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69 atic int analysi
205cb 73 4c 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44 sLoader(void *pD
205cc 61 74 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 ata, int argc, c
205cd 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 har **argv, char
205ce 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 61 **NotUsed){. a
205cf 6e 61 6c 79 73 69 73 49 6e 66 6f 20 2a 70 49 6e nalysisInfo *pIn
205d0 66 6f 20 3d 20 28 61 6e 61 6c 79 73 69 73 49 6e fo = (analysisIn
205d1 66 6f 2a 29 70 44 61 74 61 3b 0a 20 20 49 6e 64 fo*)pData;. Ind
205d2 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e ex *pIndex;. in
205d3 74 20 69 2c 20 63 3b 0a 20 20 75 6e 73 69 67 6e t i, c;. unsign
205d4 65 64 20 69 6e 74 20 76 3b 0a 20 20 63 6f 6e 73 ed int v;. cons
205d5 74 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 t char *z;.. as
205d6 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20 29 3b sert( argc==2 );
205d7 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
205d8 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 TER2(NotUsed, ar
205d9 67 63 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 76 gc);.. if( argv
205da 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d ==0 || argv[0]==
205db 30 20 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20 0 || argv[1]==0
205dc 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
205dd 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 . }. pIndex =
205de 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 sqlite3FindIndex
205df 28 70 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 (pInfo->db, argv
205e0 5b 30 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 [0], pInfo->zDat
205e1 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49 abase);. if( pI
205e2 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 ndex==0 ){. r
205e3 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a eturn 0;. }. z
205e4 20 3d 20 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f = argv[1];. fo
205e5 72 28 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d r(i=0; *z && i<=
205e6 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b pIndex->nColumn;
205e7 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30 i++){. v = 0
205e8 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d ;. while( (c=
205e9 7a 5b 30 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c z[0])>='0' && c<
205ea 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 76 20 ='9' ){. v
205eb 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 = v*10 + c - '0'
205ec 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 ;. z++;.
205ed 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 }. pIndex->a
205ee 69 52 6f 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a iRowEst[i] = v;.
205ef 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20 if( *z==' '
205f0 29 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 ) z++;. }. ret
205f1 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
205f2 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 Load the content
205f3 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 of the sqlite_s
205f4 74 61 74 31 20 74 61 62 6c 65 20 69 6e 74 6f 20 tat1 table into
205f5 74 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 the index hash t
205f6 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ables..*/.SQLITE
205f7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
205f8 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 ite3AnalysisLoad
205f9 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e (sqlite3 *db, in
205fa 74 20 69 44 62 29 7b 0a 20 20 61 6e 61 6c 79 73 t iDb){. analys
205fb 69 73 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20 20 isInfo sInfo;.
205fc 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 63 HashElem *i;. c
205fd 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 har *zSql;. int
205fe 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
205ff 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 iDb>=0 && iDb<db
20600 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
20601 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e t( db->aDb[iDb].
20602 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 pBt!=0 );. asse
20603 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
20604 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 HoldsMutex(db->a
20605 44 62 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a Db[iDb].pBt) );.
20606 0a 20 20 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20 . /* Clear any
20607 70 72 69 6f 72 20 73 74 61 74 69 73 74 69 63 73 prior statistics
20608 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 */. for(i=sqli
20609 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d teHashFirst(&db-
2060a 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d >aDb[iDb].pSchem
2060b 61 2d 3e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d a->idxHash);i;i=
2060c 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 sqliteHashNext(i
2060d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 )){. Index *p
2060e 49 64 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68 Idx = sqliteHash
2060f 44 61 74 61 28 69 29 3b 0a 20 20 20 20 73 71 6c Data(i);. sql
20610 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 ite3DefaultRowEs
20611 74 28 70 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 t(pIdx);. }..
20612 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 /* Check to make
20613 20 73 75 72 65 20 74 68 65 20 73 71 6c 69 74 65 sure the sqlite
20614 5f 73 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 _stat1 table exi
20615 73 74 73 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e stss */. sInfo.
20616 64 62 20 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f db = db;. sInfo
20617 2e 7a 44 61 74 61 62 61 73 65 20 3d 20 64 62 2d .zDatabase = db-
20618 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b >aDb[iDb].zName;
20619 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 . if( sqlite3Fi
2061a 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c ndTable(db, "sql
2061b 69 74 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 ite_stat1", sInf
2061c 6f 2e 7a 44 61 74 61 62 61 73 65 29 3d 3d 30 20 o.zDatabase)==0
2061d 29 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
2061e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
2061f 0a 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 ... /* Load new
20620 20 73 74 61 74 69 73 74 69 63 73 20 6f 75 74 20 statistics out
20621 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 of the sqlite_st
20622 61 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a at1 table */. z
20623 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Sql = sqlite3MPr
20624 69 6e 74 66 28 64 62 2c 20 22 53 45 4c 45 43 54 intf(db, "SELECT
20625 20 69 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 idx, stat FROM
20626 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22 %Q.sqlite_stat1"
20627 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
20628 20 20 20 20 20 20 20 20 20 20 73 49 6e 66 6f 2e sInfo.
20629 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 zDatabase);. if
2062a 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 ( zSql==0 ){.
2062b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
2062c 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 EM;. }else{.
2062d 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
2062e 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 fetyOff(db);.
2062f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 rc = sqlite3_ex
20630 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 61 6e 61 ec(db, zSql, ana
20631 6c 79 73 69 73 4c 6f 61 64 65 72 2c 20 26 73 49 lysisLoader, &sI
20632 6e 66 6f 2c 20 30 29 3b 0a 20 20 20 20 28 76 6f nfo, 0);. (vo
20633 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
20634 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 On(db);. sqli
20635 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 te3DbFree(db, zS
20636 71 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ql);. if( rc=
20637 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 =SQLITE_NOMEM )
20638 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
20639 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
2063a 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 rn rc;.}...#endi
2063b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
2063c 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a _ANALYZE */../**
2063d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
2063e 20 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a of analyze.c **
2063f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20641 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
20642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
20643 69 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e 63 in file attach.c
20644 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
20645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20646 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
20647 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a ** 2003 April 6.
20648 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
20649 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
2064a 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
2064b 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
2064c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
2064d 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
2064e 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
2064f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
20650 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
20651 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
20652 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
20653 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
20654 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
20655 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
20656 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
20657 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
20658 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
20659 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
2065a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2065b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2065c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2065d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
2065e 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
2065f 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 ains code used t
20660 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
20661 41 54 54 41 43 48 20 61 6e 64 20 44 45 54 41 43 ATTACH and DETAC
20662 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a 2a H commands..**.*
20663 2a 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63 2c * $Id: attach.c,
20664 76 20 31 2e 38 33 20 32 30 30 39 2f 30 32 2f 31 v 1.83 2009/02/1
20665 39 20 31 34 3a 33 39 3a 32 35 20 64 61 6e 69 65 9 14:39:25 danie
20666 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
20667 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
20668 4f 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a OMIT_ATTACH./*.*
20669 2a 20 52 65 73 6f 6c 76 65 20 61 6e 20 65 78 70 * Resolve an exp
2066a 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 61 73 ression that was
2066b 20 70 61 72 74 20 6f 66 20 61 6e 20 41 54 54 41 part of an ATTA
2066c 43 48 20 6f 72 20 44 45 54 41 43 48 20 73 74 61 CH or DETACH sta
2066d 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 tement. This.**
2066e 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 is slightly diff
2066f 65 72 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f 6c erent from resol
20670 76 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 51 ving a normal SQ
20671 4c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 65 L expression, be
20672 63 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 cause simple.**
20673 69 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 20 identifiers are
20674 74 72 65 61 74 65 64 20 61 73 20 73 74 72 69 6e treated as strin
20675 67 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 gs, not possible
20676 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 column names or
20677 20 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 aliases..**.**
20678 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72 73 i.e. if the pars
20679 65 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 er sees:.**.**
2067a 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 ATTACH DATABA
2067b 53 45 20 61 62 63 20 41 53 20 64 65 66 0a 2a 2a SE abc AS def.**
2067c 0a 2a 2a 20 69 74 20 74 72 65 61 74 73 20 74 68 .** it treats th
2067d 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e e two expression
2067e 73 20 61 73 20 6c 69 74 65 72 61 6c 20 73 74 72 s as literal str
2067f 69 6e 67 73 20 27 61 62 63 27 20 61 6e 64 20 27 ings 'abc' and '
20680 64 65 66 27 20 69 6e 73 74 65 61 64 20 6f 66 0a def' instead of.
20681 2a 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 ** looking for c
20682 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 61 olumns of the sa
20683 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 me name..**.** T
20684 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 his only applies
20685 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 to the root nod
20686 65 20 6f 66 20 70 45 78 70 72 2c 20 73 6f 20 74 e of pExpr, so t
20687 68 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a he statement:.**
20688 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 44 .** ATTACH D
20689 41 54 41 42 41 53 45 20 61 62 63 7c 7c 64 65 66 ATABASE abc||def
2068a 20 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 AS 'db2'.**.**
2068b 77 69 6c 6c 20 66 61 69 6c 20 62 65 63 61 75 73 will fail becaus
2068c 65 20 6e 65 69 74 68 65 72 20 61 62 63 20 6f 72 e neither abc or
2068d 20 64 65 66 20 63 61 6e 20 62 65 20 72 65 73 6f def can be reso
2068e 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lved..*/.static
2068f 69 6e 74 20 72 65 73 6f 6c 76 65 41 74 74 61 63 int resolveAttac
20690 68 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 78 hExpr(NameContex
20691 74 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 2a t *pName, Expr *
20692 70 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20 72 pExpr).{. int r
20693 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
20694 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 if( pExpr ){.
20695 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 if( pExpr->op!
20696 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 =TK_ID ){.
20697 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 6f rc = sqlite3Reso
20698 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 61 lveExprNames(pNa
20699 6d 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 me, pExpr);.
2069a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
2069b 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33 45 _OK && !sqlite3E
2069c 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 45 xprIsConstant(pE
2069d 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 xpr) ){.
2069e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
2069f 70 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20 22 pName->pParse, "
206a0 69 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 invalid name: \"
206a1 25 54 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e 73 %T\"", &pExpr->s
206a2 70 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 pan);. re
206a3 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
206a4 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d R;. }. }
206a5 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 else{. pExp
206a6 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e r->op = TK_STRIN
206a7 47 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 G;. }. }. r
206a8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
206a9 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 ** An SQL user-f
206aa 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 unction register
206ab 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 ed to do the wor
206ac 6b 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73 k of an ATTACH s
206ad 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a tatement. The.**
206ae 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 three arguments
206af 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e to the function
206b0 20 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 come directly f
206b1 72 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74 rom an attach st
206b2 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 atement:.**.**
206b3 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 ATTACH DATABA
206b4 53 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a SE x AS y KEY z.
206b5 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 **.** SELECT
206b6 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78 sqlite_attach(x
206b7 2c 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 , y, z).**.** If
206b8 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b the optional "K
206b9 45 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20 EY z" syntax is
206ba 6f 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20 omitted, an SQL
206bb 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61 NULL is passed a
206bc 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 s the.** third a
206bd 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 rgument..*/.stat
206be 69 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 75 ic void attachFu
206bf 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
206c0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
206c1 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
206c2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
206c3 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 *argv.){. int i
206c4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a ;. int rc = 0;.
206c5 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
206c6 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f sqlite3_context_
206c7 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 db_handle(contex
206c8 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 t);. const char
206c9 20 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 *zName;. const
206ca 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 char *zFile;.
206cb 44 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 72 Db *aNew;. char
206cc 20 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a 20 *zErrDyn = 0;.
206cd 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b char zErr[128];
206ce 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d .. UNUSED_PARAM
206cf 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a ETER(NotUsed);..
206d0 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 zFile = (const
206d1 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f char *)sqlite3_
206d2 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
206d3 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 0]);. zName = (
206d4 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c const char *)sql
206d5 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
206d6 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 argv[1]);. if(
206d7 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65 zFile==0 ) zFile
206d8 20 3d 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e 61 = "";. if( zNa
206d9 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 me==0 ) zName =
206da 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 "";.. /* Check
206db 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e for the followin
206dc 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20 g errors:. **.
206dd 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d 61 ** * Too ma
206de 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74 61 ny attached data
206df 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20 20 bases,. **
206e0 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 * Transaction cu
206e1 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 2a rrently open. *
206e2 2a 20 20 20 20 20 2a 20 53 70 65 63 69 66 69 65 * * Specifie
206e3 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 d database name
206e4 61 6c 72 65 61 64 79 20 62 65 69 6e 67 20 75 73 already being us
206e5 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 ed.. */. if( d
206e6 62 2d 3e 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d b->nDb>=db->aLim
206e7 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
206e8 41 54 54 41 43 48 45 44 5d 2b 32 20 29 7b 0a 20 ATTACHED]+2 ){.
206e9 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
206ea 6e 74 66 28 0a 20 20 20 20 20 20 73 69 7a 65 6f ntf(. sizeo
206eb 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 f(zErr), zErr, "
206ec 74 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65 too many attache
206ed 64 20 64 61 74 61 62 61 73 65 73 20 2d 20 6d 61 d databases - ma
206ee 78 20 25 64 22 2c 20 0a 20 20 20 20 20 20 64 62 x %d", . db
206ef 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
206f0 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 0a LIMIT_ATTACHED].
206f1 20 20 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 );. goto
206f2 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 attach_error;.
206f3 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 }. if( !db->aut
206f4 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 oCommit ){. s
206f5 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
206f6 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 sizeof(zErr), zE
206f7 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 rr,.
206f8 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 "cannot
206f9 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65 ATTACH database
206fa 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 within transact
206fb 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 ion");. goto
206fc 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 attach_error;.
206fd 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 }. for(i=0; i<d
206fe 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
206ff 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d 3e char *z = db->
20700 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 aDb[i].zName;.
20701 20 20 69 66 28 20 7a 20 26 26 20 7a 4e 61 6d 65 if( z && zName
20702 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
20703 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 mp(z, zName)==0
20704 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
20705 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
20706 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a 20 (zErr), zErr, .
20707 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20708 20 20 20 20 20 20 22 64 61 74 61 62 61 73 65 20 "database
20709 25 73 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e %s is already in
2070a 20 75 73 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 use", zName);.
2070b 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 goto attach
2070c 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 _error;. }.
2070d 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 }.. /* Allocate
2070e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 the new entry i
2070f 6e 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 n the db->aDb[]
20710 61 72 72 61 79 20 61 6e 64 20 69 6e 69 74 69 61 array and initia
20711 6c 69 73 65 20 74 68 65 20 73 63 68 65 6d 61 0a lise the schema.
20712 20 20 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 73 ** hash tables
20713 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d .. */. if( db-
20714 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 >aDb==db->aDbSta
20715 74 69 63 20 29 7b 0a 20 20 20 20 61 4e 65 77 20 tic ){. aNew
20716 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
20717 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
20718 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b db->aDb[0])*3 );
20719 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 . if( aNew==0
2071a 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6d ) return;. m
2071b 65 6d 63 70 79 28 61 4e 65 77 2c 20 64 62 2d 3e emcpy(aNew, db->
2071c 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e aDb, sizeof(db->
2071d 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d 65 aDb[0])*2);. }e
2071e 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 lse{. aNew =
2071f 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
20720 28 64 62 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 (db, db->aDb, si
20721 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 zeof(db->aDb[0])
20722 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a *(db->nDb+1) );.
20723 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 if( aNew==0
20724 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 ) return;. }.
20725 64 62 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a db->aDb = aNew;.
20726 20 20 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44 aNew = &db->aD
20727 62 5b 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20 20 b[db->nDb++];.
20728 6d 65 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c 20 memset(aNew, 0,
20729 73 69 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b 0a sizeof(*aNew));.
2072a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64 . /* Open the d
2072b 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
2072c 20 74 68 65 20 62 74 72 65 65 20 69 73 20 73 75 the btree is su
2072d 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 ccessfully opene
2072e 64 2c 20 75 73 65 0a 20 20 2a 2a 20 69 74 20 74 d, use. ** it t
2072f 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 64 61 74 o obtain the dat
20730 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 41 74 abase schema. At
20731 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 this point the
20732 73 63 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a 20 schema may. **
20733 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e or may not be in
20734 69 74 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f 0a itialised.. */.
20735 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
20736 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 7a reeFactory(db, z
20737 46 69 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f File, 0, SQLITE_
20738 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 DEFAULT_CACHE_SI
20739 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ZE,.
2073a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
2073b 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20 53 b->openFlags | S
2073c 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
2073d 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 DB,.
2073e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
2073f 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69 66 aNew->pBt);. if
20740 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
20741 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 ){. Pager *pP
20742 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e ager;. aNew->
20743 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 pSchema = sqlite
20744 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 61 3SchemaGet(db, a
20745 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 New->pBt);. i
20746 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d f( !aNew->pSchem
20747 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 a ){. rc =
20748 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
20749 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77 }else if( aNew
2074a 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f ->pSchema->file_
2074b 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e format && aNew->
2074c 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e pSchema->enc!=EN
2074d 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 C(db) ){. s
2074e 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
2074f 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 sizeof(zErr), zE
20750 72 72 2c 20 0a 20 20 20 20 20 20 20 20 22 61 74 rr, . "at
20751 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 tached databases
20752 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73 61 must use the sa
20753 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 me text encoding
20754 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73 as main databas
20755 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 e");. goto
20756 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 attach_error;.
20757 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 20 3d }. pPager =
20758 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 sqlite3BtreePag
20759 65 72 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 er(aNew->pBt);.
2075a 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c sqlite3PagerL
2075b 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 ockingMode(pPage
2075c 72 2c 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d r, db->dfltLockM
2075d 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ode);. sqlite
2075e 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 3PagerJournalMod
2075f 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 e(pPager, db->df
20760 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a ltJournalMode);.
20761 20 20 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d }. aNew->zNam
20762 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 e = sqlite3DbStr
20763 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a Dup(db, zName);.
20764 20 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c aNew->safety_l
20765 65 76 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 evel = 3;..#if S
20766 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a QLITE_HAS_CODEC.
20767 20 20 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 {. extern i
20768 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 nt sqlite3CodecA
20769 74 74 61 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 ttach(sqlite3*,
2076a 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a int, const void*
2076b 2c 20 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 , int);. exte
2076c 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 rn void sqlite3C
2076d 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74 odecGetKey(sqlit
2076e 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a e3*, int, void**
2076f 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 , int*);. int
20770 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20 nKey;. char
20771 2a 7a 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 *zKey;. int t
20772 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
20773 5f 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a _type(argv[2]);.
20774 20 20 20 20 73 77 69 74 63 68 28 20 74 20 29 7b switch( t ){
20775 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 . case SQLI
20776 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 TE_INTEGER:.
20777 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c case SQLITE_FL
20778 4f 41 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 OAT:. zEr
20779 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 rDyn = sqlite3Db
2077a 53 74 72 44 75 70 28 64 62 2c 20 22 49 6e 76 61 StrDup(db, "Inva
2077b 6c 69 64 20 6b 65 79 20 76 61 6c 75 65 22 29 3b lid key value");
2077c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
2077d 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
2077e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
2077f 20 20 20 0a 20 20 20 20 20 20 63 61 73 65 20 53 . case S
20780 51 4c 49 54 45 5f 54 45 58 54 3a 0a 20 20 20 20 QLITE_TEXT:.
20781 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c case SQLITE_BL
20782 4f 42 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 OB:. nKey
20783 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
20784 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b _bytes(argv[2]);
20785 0a 20 20 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 . zKey =
20786 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f (char *)sqlite3_
20787 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b value_blob(argv[
20788 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 2]);. sql
20789 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 28 ite3CodecAttach(
2078a 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a db, db->nDb-1, z
2078b 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 Key, nKey);.
2078c 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 break;..
2078d 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 case SQLITE_NU
2078e 4c 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e LL:. /* N
2078f 6f 20 6b 65 79 20 73 70 65 63 69 66 69 65 64 2e o key specified.
20790 20 20 55 73 65 20 74 68 65 20 6b 65 79 20 66 72 Use the key fr
20791 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 om the main data
20792 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 base */.
20793 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b sqlite3CodecGetK
20794 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a ey(db, 0, (void*
20795 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b *)&zKey, &nKey);
20796 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
20797 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 20 CodecAttach(db,
20798 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c db->nDb-1, zKey,
20799 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 nKey);.
2079a 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
2079b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 .#endif.. /* If
2079c 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70 the file was op
2079d 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c ened successfull
2079e 79 2c 20 72 65 61 64 20 74 68 65 20 73 63 68 65 y, read the sche
2079f 6d 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 ma for the new d
207a0 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 atabase.. ** If
207a1 20 74 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 this fails, or
207a2 69 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 if opening the f
207a3 69 6c 65 20 66 61 69 6c 65 64 2c 20 74 68 65 6e ile failed, then
207a4 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 close the file
207a5 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 and . ** remove
207a6 20 74 68 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 the entry from
207a7 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 the db->aDb[] ar
207a8 72 61 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 ray. i.e. put ev
207a9 65 72 79 74 68 69 6e 67 20 62 61 63 6b 20 74 68 erything back th
207aa 65 20 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f e way. ** we fo
207ab 75 6e 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 und it.. */. i
207ac 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
207ad 20 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 ){. (void)sq
207ae 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
207af 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
207b0 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b reeEnterAll(db);
207b1 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
207b2 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 44 3Init(db, &zErrD
207b3 79 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 yn);. sqlite3
207b4 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 BtreeLeaveAll(db
207b5 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c );. (void)sql
207b6 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
207b7 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 );. }. if( rc
207b8 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20 3d ){. int iDb =
207b9 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 db->nDb - 1;.
207ba 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 32 assert( iDb>=2
207bb 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e );. if( db->
207bc 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a aDb[iDb].pBt ){.
207bd 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
207be 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b eeClose(db->aDb[
207bf 69 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 iDb].pBt);.
207c0 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 db->aDb[iDb].pB
207c1 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 2d t = 0;. db-
207c2 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d >aDb[iDb].pSchem
207c3 61 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 a = 0;. }.
207c4 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 sqlite3ResetInt
207c5 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 ernalSchema(db,
207c6 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20 0);. db->nDb
207c7 3d 20 69 44 62 3b 0a 20 20 20 20 69 66 28 20 72 = iDb;. if( r
207c8 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
207c9 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f || rc==SQLITE_IO
207ca 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 ERR_NOMEM ){.
207cb 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
207cc 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 led = 1;. s
207cd 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
207ce 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 sizeof(zErr),zEr
207cf 72 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 r, "out of memor
207d0 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a y");. }else{.
207d1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
207d2 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 printf(sizeof(zE
207d3 72 72 29 2c 7a 45 72 72 2c 20 22 75 6e 61 62 6c rr),zErr, "unabl
207d4 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 e to open databa
207d5 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29 3b se: %s", zFile);
207d6 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 . }. goto
207d7 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 attach_error;.
207d8 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a 0a }. . return;..
207d9 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20 attach_error:.
207da 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 /* Return an err
207db 6f 72 20 69 66 20 77 65 20 67 65 74 20 68 65 72 or if we get her
207dc 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72 44 e */. if( zErrD
207dd 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 yn ){. sqlite
207de 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 3_result_error(c
207df 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c ontext, zErrDyn,
207e0 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 -1);. sqlite
207e1 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 3DbFree(db, zErr
207e2 44 79 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Dyn);. }else{.
207e3 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a zErr[sizeof(z
207e4 45 72 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20 20 Err)-1] = 0;.
207e5 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
207e6 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a error(context, z
207e7 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 Err, -1);. }.
207e8 69 66 28 20 72 63 20 29 20 73 71 6c 69 74 65 33 if( rc ) sqlite3
207e9 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f _result_error_co
207ea 64 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b de(context, rc);
207eb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c .}../*.** An SQL
207ec 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 user-function r
207ed 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 egistered to do
207ee 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44 the work of an D
207ef 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e ETACH statement.
207f0 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 The.** three ar
207f1 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 guments to the f
207f2 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 unction come dir
207f3 65 63 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 74 ectly from a det
207f4 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a ach statement:.*
207f5 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 *.** DETACH
207f6 44 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a DATABASE x.**.**
207f7 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 SELECT sqli
207f8 74 65 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a te_detach(x).*/.
207f9 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 74 61 static void deta
207fa 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 chFunc(. sqlite
207fb 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
207fc 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
207fd 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
207fe 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 ue **argv.){. c
207ff 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
20800 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a = (const char *
20801 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
20802 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
20803 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
20804 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
20805 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
20806 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44 62 20 ;. int i;. Db
20807 2a 70 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 *pDb = 0;. char
20808 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 55 zErr[128];.. U
20809 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
2080a 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 NotUsed);.. if(
2080b 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d zName==0 ) zNam
2080c 65 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 3d e = "";. for(i=
2080d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
2080e 2b 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64 +){. pDb = &d
2080f 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 b->aDb[i];. i
20810 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 f( pDb->pBt==0 )
20811 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 continue;. i
20812 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
20813 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e p(pDb->zName, zN
20814 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b ame)==0 ) break;
20815 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d 64 . }.. if( i>=d
20816 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73 71 b->nDb ){. sq
20817 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
20818 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 izeof(zErr),zErr
20819 2c 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61 62 , "no such datab
2081a 61 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 ase: %s", zName)
2081b 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 ;. goto detac
2081c 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 h_error;. }. i
2081d 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 73 71 f( i<2 ){. sq
2081e 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
2081f 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 izeof(zErr),zErr
20820 2c 20 22 63 61 6e 6e 6f 74 20 64 65 74 61 63 68 , "cannot detach
20821 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 7a database %s", z
20822 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Name);. goto
20823 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 detach_error;.
20824 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 }. if( !db->aut
20825 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 oCommit ){. s
20826 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
20827 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 sizeof(zErr), zE
20828 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 rr,.
20829 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 "cannot
2082a 20 44 45 54 41 43 48 20 64 61 74 61 62 61 73 65 DETACH database
2082b 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 within transact
2082c 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 ion");. goto
2082d 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 detach_error;.
2082e 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 }. if( sqlite3B
2082f 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e treeIsInReadTran
20830 73 28 70 44 62 2d 3e 70 42 74 29 20 7c 7c 20 73 s(pDb->pBt) || s
20831 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 qlite3BtreeIsInB
20832 61 63 6b 75 70 28 70 44 62 2d 3e 70 42 74 29 20 ackup(pDb->pBt)
20833 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ){. sqlite3_s
20834 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
20835 45 72 72 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 Err),zErr, "data
20836 62 61 73 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 base %s is locke
20837 64 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 d", zName);.
20838 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f goto detach_erro
20839 72 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 r;. }.. sqlite
2083a 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3BtreeClose(pDb-
2083b 3e 70 42 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 >pBt);. pDb->pB
2083c 74 20 3d 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 t = 0;. pDb->pS
2083d 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c chema = 0;. sql
2083e 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
2083f 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a lSchema(db, 0);.
20840 20 20 72 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 return;..detac
20841 68 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 h_error:. sqlit
20842 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
20843 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d context, zErr, -
20844 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 1);.}../*.** Thi
20845 73 20 70 72 6f 63 65 64 75 72 65 20 67 65 6e 65 s procedure gene
20846 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 rates VDBE code
20847 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 for a single inv
20848 6f 63 61 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 ocation of eithe
20849 72 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f r the.** sqlite_
2084a 64 65 74 61 63 68 28 29 20 6f 72 20 73 71 6c 69 detach() or sqli
2084b 74 65 5f 61 74 74 61 63 68 28 29 20 53 51 4c 20 te_attach() SQL
2084c 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a user functions..
2084d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
2084e 6f 64 65 41 74 74 61 63 68 28 0a 20 20 50 61 72 odeAttach(. Par
2084f 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
20850 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 /* The parser
20851 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 context */. int
20852 20 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 type,
20853 20 20 2f 2a 20 45 69 74 68 65 72 20 53 51 4c 49 /* Either SQLI
20854 54 45 5f 41 54 54 41 43 48 20 6f 72 20 53 51 4c TE_ATTACH or SQL
20855 49 54 45 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 ITE_DETACH */.
20856 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 FuncDef *pFunc,
20857 20 20 20 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 /* FuncDef
20858 77 72 61 70 70 65 72 20 66 6f 72 20 64 65 74 61 wrapper for deta
20859 63 68 46 75 6e 63 28 29 20 6f 72 20 61 74 74 61 chFunc() or atta
2085a 63 68 46 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 chFunc() */. Ex
2085b 70 72 20 2a 70 41 75 74 68 41 72 67 2c 20 20 20 pr *pAuthArg,
2085c 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e /* Expression
2085d 20 74 6f 20 70 61 73 73 20 74 6f 20 61 75 74 68 to pass to auth
2085e 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 orization callba
2085f 63 6b 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 ck */. Expr *pF
20860 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 ilename, /*
20861 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 Name of database
20862 20 66 69 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 file */. Expr
20863 2a 70 44 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 *pDbname,
20864 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 /* Name of the d
20865 61 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 69 atabase to use i
20866 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 nternally */. E
20867 78 70 72 20 2a 70 4b 65 79 20 20 20 20 20 20 20 xpr *pKey
20868 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
20869 6b 65 79 20 66 6f 72 20 65 6e 63 72 79 70 74 69 key for encrypti
2086a 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a on extension */.
2086b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e ){. int rc;. N
2086c 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 ameContext sName
2086d 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 ;. Vdbe *v;. s
2086e 71 6c 69 74 65 33 2a 20 64 62 20 3d 20 70 50 61 qlite3* db = pPa
2086f 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 rse->db;. int r
20870 65 67 41 72 67 73 3b 0a 0a 23 69 66 6e 64 65 66 egArgs;..#ifndef
20871 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
20872 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 HORIZATION. ass
20873 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ert( db->mallocF
20874 61 69 6c 65 64 20 7c 7c 20 70 41 75 74 68 41 72 ailed || pAuthAr
20875 67 20 29 3b 0a 20 20 69 66 28 20 70 41 75 74 68 g );. if( pAuth
20876 41 72 67 20 29 7b 0a 20 20 20 20 63 68 61 72 20 Arg ){. char
20877 2a 7a 41 75 74 68 41 72 67 20 3d 20 73 71 6c 69 *zAuthArg = sqli
20878 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
20879 28 64 62 2c 20 26 70 41 75 74 68 41 72 67 2d 3e (db, &pAuthArg->
2087a 73 70 61 6e 29 3b 0a 20 20 20 20 69 66 28 20 21 span);. if( !
2087b 7a 41 75 74 68 41 72 67 20 29 7b 0a 20 20 20 20 zAuthArg ){.
2087c 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e goto attach_en
2087d 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 d;. }. rc
2087e 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 = sqlite3AuthChe
2087f 63 6b 28 70 50 61 72 73 65 2c 20 74 79 70 65 2c ck(pParse, type,
20880 20 7a 41 75 74 68 41 72 67 2c 20 30 2c 20 30 29 zAuthArg, 0, 0)
20881 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
20882 72 65 65 28 64 62 2c 20 7a 41 75 74 68 41 72 67 ree(db, zAuthArg
20883 29 3b 0a 20 20 20 20 69 66 28 72 63 21 3d 53 51 );. if(rc!=SQ
20884 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
20885 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 goto attach_end
20886 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
20887 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
20888 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 T_AUTHORIZATION
20889 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e */.. memset(&sN
2088a 61 6d 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e ame, 0, sizeof(N
2088b 61 6d 65 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 ameContext));.
2088c 73 4e 61 6d 65 2e 70 50 61 72 73 65 20 3d 20 70 sName.pParse = p
2088d 50 61 72 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20 Parse;.. if( .
2088e 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d SQLITE_OK!=
2088f 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 (rc = resolveAtt
20890 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 achExpr(&sName,
20891 70 46 69 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 pFilename)) ||.
20892 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d SQLITE_OK!=
20893 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 (rc = resolveAtt
20894 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 achExpr(&sName,
20895 70 44 62 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 pDbname)) ||.
20896 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 SQLITE_OK!=(r
20897 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 c = resolveAttac
20898 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b hExpr(&sName, pK
20899 65 79 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 50 ey)). ){. pP
2089a 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 arse->nErr++;.
2089b 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e goto attach_en
2089c 64 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 d;. }.. v = sq
2089d 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
2089e 72 73 65 29 3b 0a 20 20 72 65 67 41 72 67 73 20 rse);. regArgs
2089f 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 = sqlite3GetTemp
208a0 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 34 29 Range(pParse, 4)
208a1 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 ;. sqlite3ExprC
208a2 6f 64 65 28 70 50 61 72 73 65 2c 20 70 46 69 6c ode(pParse, pFil
208a3 65 6e 61 6d 65 2c 20 72 65 67 41 72 67 73 29 3b ename, regArgs);
208a4 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f . sqlite3ExprCo
208a5 64 65 28 70 50 61 72 73 65 2c 20 70 44 62 6e 61 de(pParse, pDbna
208a6 6d 65 2c 20 72 65 67 41 72 67 73 2b 31 29 3b 0a me, regArgs+1);.
208a7 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 sqlite3ExprCod
208a8 65 28 70 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 e(pParse, pKey,
208a9 72 65 67 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 regArgs+2);.. a
208aa 73 73 65 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e ssert( v || db->
208ab 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
208ac 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 if( v ){. s
208ad 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
208ae 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c (v, OP_Function,
208af 20 30 2c 20 72 65 67 41 72 67 73 2b 33 2d 70 46 0, regArgs+3-pF
208b0 75 6e 63 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 unc->nArg, regAr
208b1 67 73 2b 33 29 3b 0a 20 20 20 20 61 73 73 65 72 gs+3);. asser
208b2 74 28 20 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d t( pFunc->nArg==
208b3 2d 31 20 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 -1 || (pFunc->nA
208b4 72 67 26 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d rg&0xff)==pFunc-
208b5 3e 6e 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c >nArg );. sql
208b6 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 ite3VdbeChangeP5
208b7 28 76 2c 20 28 75 38 29 28 70 46 75 6e 63 2d 3e (v, (u8)(pFunc->
208b8 6e 41 72 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 nArg));. sqli
208b9 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
208ba 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 v, -1, (char *)p
208bb 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 Func, P4_FUNCDEF
208bc 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 );.. /* Code
208bd 61 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f an OP_Expire. Fo
208be 72 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 r an ATTACH stat
208bf 65 6d 65 6e 74 2c 20 73 65 74 20 50 31 20 74 6f ement, set P1 to
208c0 20 74 72 75 65 20 28 65 78 70 69 72 65 20 74 68 true (expire th
208c1 69 73 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d is. ** statem
208c2 65 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 ent only). For D
208c3 45 54 41 43 48 2c 20 73 65 74 20 69 74 20 74 6f ETACH, set it to
208c4 20 66 61 6c 73 65 20 28 65 78 70 69 72 65 20 61 false (expire a
208c5 6c 6c 20 65 78 69 73 74 69 6e 67 0a 20 20 20 20 ll existing.
208c6 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a ** statements)..
208c7 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
208c8 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
208c9 4f 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65 OP_Expire, (type
208ca 3d 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29 ==SQLITE_ATTACH)
208cb 29 3b 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 );. }. .attach
208cc 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 _end:. sqlite3E
208cd 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 46 xprDelete(db, pF
208ce 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 ilename);. sqli
208cf 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
208d0 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 , pDbname);. sq
208d1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
208d2 64 62 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a db, pKey);.}../*
208d3 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68 .** Called by th
208d4 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 e parser to comp
208d5 69 6c 65 20 61 20 44 45 54 41 43 48 20 73 74 61 ile a DETACH sta
208d6 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 tement..**.**
208d7 20 20 44 45 54 41 43 48 20 70 44 62 6e 61 6d 65 DETACH pDbname
208d8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
208d9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
208da 65 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 etach(Parse *pPa
208db 72 73 65 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 rse, Expr *pDbna
208dc 6d 65 29 7b 0a 20 20 73 74 61 74 69 63 20 46 75 me){. static Fu
208dd 6e 63 44 65 66 20 64 65 74 61 63 68 5f 66 75 6e ncDef detach_fun
208de 63 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 c = {. 1,
208df 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e /* n
208e0 41 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 Arg */. SQLIT
208e1 45 5f 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 E_UTF8, /*
208e2 69 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 iPrefEnc */.
208e3 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
208e4 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 /* flags */.
208e5 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
208e6 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61 /* pUserData
208e7 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 */. 0,
208e8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 /* pNe
208e9 78 74 20 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 xt */. detach
208ea 46 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 Func, /* x
208eb 46 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 Func */. 0,
208ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
208ed 20 78 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c xStep */. 0,
208ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
208ef 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a /* xFinalize */.
208f0 20 20 20 20 22 73 71 6c 69 74 65 5f 64 65 74 61 "sqlite_deta
208f1 63 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a ch", /* zName *
208f2 2f 0a 20 20 20 20 30 20 20 20 20 20 20 20 20 20 /. 0
208f3 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 /* pHash
208f4 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 */. };. codeA
208f5 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 53 51 ttach(pParse, SQ
208f6 4c 49 54 45 5f 44 45 54 41 43 48 2c 20 26 64 65 LITE_DETACH, &de
208f7 74 61 63 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 tach_func, pDbna
208f8 6d 65 2c 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d me, 0, 0, pDbnam
208f9 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c e);.}../*.** Cal
208fa 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 led by the parse
208fb 72 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 r to compile an
208fc 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 ATTACH statement
208fd 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 ..**.** ATTA
208fe 43 48 20 70 20 41 53 20 70 44 62 6e 61 6d 65 20 CH p AS pDbname
208ff 4b 45 59 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 KEY pKey.*/.SQLI
20900 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
20901 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 sqlite3Attach(Pa
20902 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
20903 72 20 2a 70 2c 20 45 78 70 72 20 2a 70 44 62 6e r *p, Expr *pDbn
20904 61 6d 65 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 ame, Expr *pKey)
20905 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 44 {. static FuncD
20906 65 66 20 61 74 74 61 63 68 5f 66 75 6e 63 20 3d ef attach_func =
20907 20 7b 0a 20 20 20 20 33 2c 20 20 20 20 20 20 20 {. 3,
20908 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 /* nArg
20909 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 */. SQLITE_U
2090a 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 TF8, /* iPr
2090b 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 efEnc */. 0,
2090c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2090d 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 * flags */. 0
2090e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
2090f 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f /* pUserData */
20910 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
20911 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 /* pNext
20912 2a 2f 0a 20 20 20 20 61 74 74 61 63 68 46 75 6e */. attachFun
20913 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e c, /* xFun
20914 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 c */. 0,
20915 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 /* xS
20916 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 tep */. 0,
20917 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20918 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 xFinalize */.
20919 20 22 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22 "sqlite_attach"
2091a 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 , /* zName */.
2091b 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 0
2091c 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f /* pHash */
2091d 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 . };. codeAtta
2091e 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ch(pParse, SQLIT
2091f 45 5f 41 54 54 41 43 48 2c 20 26 61 74 74 61 63 E_ATTACH, &attac
20920 68 5f 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 h_func, p, p, pD
20921 62 6e 61 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a bname, pKey);.}.
20922 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
20923 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a _OMIT_ATTACH */.
20924 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
20925 65 20 61 20 44 62 46 69 78 65 72 20 73 74 72 75 e a DbFixer stru
20926 63 74 75 72 65 2e 20 20 54 68 69 73 20 72 6f 75 cture. This rou
20927 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c tine must be cal
20928 6c 65 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 led prior.** to
20929 70 61 73 73 69 6e 67 20 74 68 65 20 73 74 72 75 passing the stru
2092a 63 74 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 cture to one of
2092b 74 68 65 20 73 71 6c 69 74 65 46 69 78 41 41 41 the sqliteFixAAA
2092c 41 28 29 20 72 6f 75 74 69 6e 65 73 20 62 65 6c A() routines bel
2092d 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 ow..**.** The re
2092e 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 turn value indic
2092f 61 74 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 ates whether or
20930 6e 6f 74 20 66 69 78 61 74 69 6f 6e 20 69 73 20 not fixation is
20931 72 65 71 75 69 72 65 64 2e 20 20 54 52 55 45 0a required. TRUE.
20932 2a 2a 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e ** means we do n
20933 65 65 64 20 74 6f 20 66 69 78 20 74 68 65 20 64 eed to fix the d
20934 61 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 atabase referenc
20935 65 73 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 es, FALSE means
20936 77 65 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 we do not..*/.SQ
20937 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
20938 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 sqlite3FixInit(
20939 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 . DbFixer *pFix
2093a 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 , /* The fi
2093b 78 65 72 20 74 6f 20 62 65 20 69 6e 69 74 69 61 xer to be initia
2093c 6c 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 lized */. Parse
2093d 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f *pParse, /
2093e 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 * Error messages
2093f 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e will be written
20940 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 here */. int i
20941 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f Db, /
20942 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64 61 * This is the da
20943 74 61 62 61 73 65 20 74 68 61 74 20 6d 75 73 74 tabase that must
20944 20 62 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f be used */. co
20945 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c nst char *zType,
20946 20 20 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 72 /* "view", "tr
20947 69 67 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 igger", or "inde
20948 78 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f x" */. const To
20949 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e ken *pName /* N
2094a 61 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c ame of the view,
2094b 20 74 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 trigger, or ind
2094c 65 78 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 ex */.){. sqlit
2094d 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 69 e3 *db;.. if( i
2094e 44 62 3c 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 Db<0 || iDb==1 )
2094f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 return 0;. db
20950 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
20951 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e assert( db->nDb>
20952 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70 iDb );. pFix->p
20953 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
20954 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 pFix->zDb = db
20955 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
20956 3b 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 ;. pFix->zType
20957 3d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 2d = zType;. pFix-
20958 3e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a >pName = pName;.
20959 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f return 1;.}../
2095a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
2095b 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e ng set of routin
2095c 65 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 20 es walk through
2095d 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 61 the parse tree a
2095e 6e 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73 nd assign.** a s
2095f 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73 65 pecific database
20960 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 65 to all table re
20961 66 65 72 65 6e 63 65 73 20 77 68 65 72 65 20 74 ferences where t
20962 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 he database name
20963 0a 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e 73 .** was left uns
20964 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 pecified in the
20965 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 original SQL sta
20966 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69 tement. The pFi
20967 78 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d x structure.** m
20968 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e ust have been in
20969 69 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20 70 itialized by a p
2096a 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c rior call to sql
2096b 69 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a ite3FixInit()..*
2096c 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
2096d 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 nes are used to
2096e 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 make sure that a
2096f 6e 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65 72 n index, trigger
20970 2c 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20 , or.** view in
20971 6f 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 one database doe
20972 73 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 6f s not refer to o
20973 62 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66 66 bjects in a diff
20974 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e 0a erent database..
20975 2a 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 69 ** (Exception: i
20976 6e 64 69 63 65 73 2c 20 74 72 69 67 67 65 72 73 ndices, triggers
20977 2c 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20 74 , and views in t
20978 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
20979 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 are.** allowed
2097a 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 74 to refer to anyt
2097b 68 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65 66 hing.) If a ref
2097c 65 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69 63 erence is explic
2097d 69 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 itly made.** to
2097e 61 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 an object in a d
2097f 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 ifferent databas
20980 65 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 e, an error mess
20981 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a age is added to.
20982 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d ** pParse->zErrM
20983 73 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75 sg and these rou
20984 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e tines return non
20985 2d 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79 -zero. If every
20986 74 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20 thing.** checks
20987 6f 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69 out, these routi
20988 6e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f nes return 0..*/
20989 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
2098a 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 int sqlite3FixSr
2098b 63 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 cList(. DbFixer
2098c 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a *pFix, /*
2098d 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 Context of the
2098e 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72 fixation */. Sr
2098f 63 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 cList *pList
20990 20 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 65 /* The Source
20991 20 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20 61 list to check a
20992 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a nd modify */.){.
20993 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 int i;. const
20994 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74 char *zDb;. st
20995 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
20996 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 m *pItem;.. if(
20997 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 pList==0 ) retu
20998 72 6e 20 30 3b 0a 20 20 7a 44 62 20 3d 20 70 46 rn 0;. zDb = pF
20999 69 78 2d 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 69 ix->zDb;. for(i
2099a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d =0, pItem=pList-
2099b 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 >a; i<pList->nSr
2099c 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 c; i++, pItem++)
2099d 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d {. if( pItem-
2099e 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30 20 29 7b >zDatabase==0 ){
2099f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44 . pItem->zD
209a0 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 atabase = sqlite
209a1 33 44 62 53 74 72 44 75 70 28 70 46 69 78 2d 3e 3DbStrDup(pFix->
209a2 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 44 62 29 pParse->db, zDb)
209a3 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
209a4 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 sqlite3StrICmp(p
209a5 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c Item->zDatabase,
209a6 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 zDb)!=0 ){.
209a7 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
209a8 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2c 0a 20 (pFix->pParse,.
209a9 20 20 20 20 20 20 20 20 22 25 73 20 25 54 20 63 "%s %T c
209aa 61 6e 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 annot reference
209ab 6f 62 6a 65 63 74 73 20 69 6e 20 64 61 74 61 62 objects in datab
209ac 61 73 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20 ase %s",.
209ad 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 2c 20 70 pFix->zType, p
209ae 46 69 78 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 65 Fix->pName, pIte
209af 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 m->zDatabase);.
209b0 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
209b1 20 20 20 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 }.#if !define
209b2 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
209b3 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 EW) || !defined(
209b4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
209b5 47 45 52 29 0a 20 20 20 20 69 66 28 20 73 71 6c GER). if( sql
209b6 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 ite3FixSelect(pF
209b7 69 78 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 ix, pItem->pSele
209b8 63 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a ct) ) return 1;.
209b9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 if( sqlite3F
209ba 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 74 ixExpr(pFix, pIt
209bb 65 6d 2d 3e 70 4f 6e 29 20 29 20 72 65 74 75 72 em->pOn) ) retur
209bc 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a n 1;.#endif. }.
209bd 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 69 return 0;.}.#i
209be 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
209bf 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 E_OMIT_VIEW) ||
209c0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
209c1 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51 OMIT_TRIGGER).SQ
209c2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
209c3 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 sqlite3FixSelec
209c4 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 t(. DbFixer *pF
209c5 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e ix, /* Con
209c6 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 text of the fixa
209c7 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 tion */. Select
209c8 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 2f *pSelect /
209c9 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 * The SELECT sta
209ca 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 66 69 78 tement to be fix
209cb 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 ed to one databa
209cc 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 se */.){. while
209cd 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 ( pSelect ){.
209ce 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 if( sqlite3FixE
209cf 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53 xprList(pFix, pS
209d0 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 29 20 29 elect->pEList) )
209d1 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
209d2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
209d3 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 sqlite3FixSrcLis
209d4 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d t(pFix, pSelect-
209d5 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20 20 20 >pSrc) ){.
209d6 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
209d7 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 if( sqlite3F
209d8 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 65 ixExpr(pFix, pSe
209d9 6c 65 63 74 2d 3e 70 57 68 65 72 65 29 20 29 7b lect->pWhere) ){
209da 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
209db 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
209dc 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 qlite3FixExpr(pF
209dd 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 48 61 ix, pSelect->pHa
209de 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 20 20 72 ving) ){. r
209df 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
209e0 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 pSelect = pSe
209e1 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 lect->pPrior;.
209e2 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
209e3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
209e4 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 nt sqlite3FixExp
209e5 72 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 r(. DbFixer *pF
209e6 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 ix, /* Conte
209e7 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 xt of the fixati
209e8 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 on */. Expr *pE
209e9 78 70 72 20 20 20 20 20 20 20 20 2f 2a 20 54 68 xpr /* Th
209ea 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 e expression to
209eb 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 be fixed to one
209ec 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 database */.){.
209ed 20 77 68 69 6c 65 28 20 70 45 78 70 72 20 29 7b while( pExpr ){
209ee 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 . if( ExprHas
209ef 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 AnyProperty(pExp
209f0 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c r, EP_TokenOnly|
209f1 45 50 5f 53 70 61 6e 4f 6e 6c 79 29 20 29 20 62 EP_SpanOnly) ) b
209f2 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 45 78 reak;. if( Ex
209f3 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
209f4 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
209f5 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 t) ){. if(
209f6 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 sqlite3FixSelect
209f7 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 78 2e (pFix, pExpr->x.
209f8 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72 pSelect) ) retur
209f9 6e 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a n 1;. }else{.
209fa 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
209fb 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 3FixExprList(pFi
209fc 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 x, pExpr->x.pLis
209fd 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 t) ) return 1;.
209fe 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
209ff 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 ite3FixExpr(pFix
20a00 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 , pExpr->pRight)
20a01 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
20a02 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 1;. }. pE
20a03 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 xpr = pExpr->pLe
20a04 66 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ft;. }. return
20a05 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 0;.}.SQLITE_PRI
20a06 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
20a07 46 69 78 45 78 70 72 4c 69 73 74 28 0a 20 20 44 FixExprList(. D
20a08 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 bFixer *pFix,
20a09 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 /* Context of
20a0a 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a the fixation */.
20a0b 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 ExprList *pLis
20a0c 74 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 t /* The expr
20a0d 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 ession to be fix
20a0e 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 ed to one databa
20a0f 73 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 se */.){. int i
20a10 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c ;. struct ExprL
20a11 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
20a12 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 . if( pList==0
20a13 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f ) return 0;. fo
20a14 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 r(i=0, pItem=pLi
20a15 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e st->a; i<pList->
20a16 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 nExpr; i++, pIte
20a17 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 m++){. if( sq
20a18 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 lite3FixExpr(pFi
20a19 78 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 x, pItem->pExpr)
20a1a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
20a1b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
20a1c 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 return 0;.}.#end
20a1d 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
20a1e 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
20a1f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
20a20 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 nt sqlite3FixTri
20a21 67 67 65 72 53 74 65 70 28 0a 20 20 44 62 46 69 ggerStep(. DbFi
20a22 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f xer *pFix, /
20a23 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 * Context of the
20a24 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 fixation */. T
20a25 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 riggerStep *pSte
20a26 70 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 p /* The trigger
20a27 20 73 74 65 70 20 62 65 20 66 69 78 65 64 20 74 step be fixed t
20a28 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a o one database *
20a29 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 /.){. while( pS
20a2a 74 65 70 20 29 7b 0a 20 20 20 20 69 66 28 20 73 tep ){. if( s
20a2b 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 qlite3FixSelect(
20a2c 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 53 65 pFix, pStep->pSe
20a2d 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 lect) ){. r
20a2e 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
20a2f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
20a30 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 74 65 xExpr(pFix, pSte
20a31 70 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 p->pWhere) ){.
20a32 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
20a33 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
20a34 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 te3FixExprList(p
20a35 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 Fix, pStep->pExp
20a36 72 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 rList) ){.
20a37 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
20a38 20 20 20 20 70 53 74 65 70 20 3d 20 70 53 74 65 pStep = pSte
20a39 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 p->pNext;. }.
20a3a 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 return 0;.}.#end
20a3b 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
20a3c 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 74 74 61 63 *** End of attac
20a3d 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h.c ************
20a3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a40 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
20a41 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 *** Begin file a
20a42 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a uth.c **********
20a43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a45 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a **/./*.** 2003 J
20a46 61 6e 75 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 anuary 11.**.**
20a47 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
20a48 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
20a49 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
20a4a 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
20a4b 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
20a4c 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
20a4d 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
20a4e 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
20a4f 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
20a50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
20a51 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
20a52 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
20a53 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
20a54 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
20a55 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
20a56 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
20a57 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
20a58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a5c 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
20a5d 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
20a5e 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c ode used to impl
20a5f 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65 ement the sqlite
20a60 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
20a61 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69 73 ().** API. This
20a62 20 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e 20 facility is an
20a63 6f 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72 65 optional feature
20a64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e of the library.
20a65 20 20 45 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79 Embedded.** sy
20a66 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f stems that do no
20a67 74 20 6e 65 65 64 20 74 68 69 73 20 66 61 63 69 t need this faci
20a68 6c 69 74 79 20 6d 61 79 20 6f 6d 69 74 20 69 74 lity may omit it
20a69 20 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a by recompiling.
20a6a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 ** the library w
20a6b 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 ith -DSQLITE_OMI
20a6c 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d T_AUTHORIZATION=
20a6d 31 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 75 74 1.**.** $Id: aut
20a6e 68 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 37 2f h.c,v 1.29 2007/
20a6f 30 39 2f 31 38 20 31 35 3a 35 35 3a 30 37 20 64 09/18 15:55:07 d
20a70 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a rh Exp $.*/../*.
20a71 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f ** All of the co
20a72 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
20a73 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62 may be omitted b
20a74 79 20 64 65 66 69 6e 69 6e 67 20 61 20 73 69 6e y defining a sin
20a75 67 6c 65 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f gle.** macro..*/
20a76 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
20a77 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
20a78 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 ON../*.** Set or
20a79 20 63 6c 65 61 72 20 74 68 65 20 61 63 63 65 73 clear the acces
20a7a 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 s authorization
20a7b 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
20a7c 54 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f The access autho
20a7d 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f rization functio
20a7e 6e 20 69 73 20 62 65 20 63 61 6c 6c 65 64 20 64 n is be called d
20a7f 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c uring the compil
20a80 61 74 69 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 74 ation.** phase t
20a81 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 o verify that th
20a82 65 20 75 73 65 72 20 68 61 73 20 72 65 61 64 20 e user has read
20a83 61 6e 64 2f 6f 72 20 77 72 69 74 65 20 61 63 63 and/or write acc
20a84 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 6f ess permission o
20a85 6e 0a 2a 2a 20 76 61 72 69 6f 75 73 20 66 69 65 n.** various fie
20a86 6c 64 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 lds of the datab
20a87 61 73 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 ase. The first
20a88 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
20a89 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a auth function.**
20a8a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 is a copy of th
20a8b 65 20 33 72 64 20 61 72 67 75 6d 65 6e 74 20 74 e 3rd argument t
20a8c 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 o this routine.
20a8d 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
20a8e 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 61 ment.** to the a
20a8f 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 uth function is
20a90 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f 6e one of these con
20a91 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 stants:.**.**
20a92 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 SQLITE_CREAT
20a93 45 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 E_INDEX.**
20a94 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
20a95 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 ABLE.** SQ
20a96 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 LITE_CREATE_TEMP
20a97 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 _INDEX.**
20a98 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 SQLITE_CREATE_TE
20a99 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 MP_TABLE.**
20a9a 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f SQLITE_CREATE_
20a9b 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 TEMP_TRIGGER.**
20a9c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 SQLITE_CRE
20a9d 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a ATE_TEMP_VIEW.**
20a9e 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 SQLITE_CR
20a9f 45 41 54 45 5f 54 52 49 47 47 45 52 0a 2a 2a 20 EATE_TRIGGER.**
20aa0 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 SQLITE_CRE
20aa1 41 54 45 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 ATE_VIEW.**
20aa2 20 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 0a SQLITE_DELETE.
20aa3 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
20aa4 44 52 4f 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 DROP_INDEX.**
20aa5 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f SQLITE_DROP_
20aa6 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 TABLE.** S
20aa7 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f QLITE_DROP_TEMP_
20aa8 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 INDEX.** S
20aa9 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f QLITE_DROP_TEMP_
20aaa 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 TABLE.** S
20aab 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f QLITE_DROP_TEMP_
20aac 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 TRIGGER.**
20aad 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
20aae 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 P_VIEW.**
20aaf 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 SQLITE_DROP_TRIG
20ab0 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c GER.** SQL
20ab1 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 0a 2a 2a ITE_DROP_VIEW.**
20ab2 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e SQLITE_IN
20ab3 53 45 52 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 SERT.** SQ
20ab4 4c 49 54 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 20 LITE_PRAGMA.**
20ab5 20 20 20 20 20 53 51 4c 49 54 45 5f 52 45 41 44 SQLITE_READ
20ab6 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
20ab7 5f 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 _SELECT.**
20ab8 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 SQLITE_TRANSACT
20ab9 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c ION.** SQL
20aba 49 54 45 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a ITE_UPDATE.**.**
20abb 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 The third and f
20abc 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 ourth arguments
20abd 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 to the auth func
20abe 74 69 6f 6e 20 61 72 65 20 74 68 65 20 6e 61 6d tion are the nam
20abf 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c e of.** the tabl
20ac0 65 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e e and the column
20ac1 20 74 68 61 74 20 61 72 65 20 62 65 69 6e 67 20 that are being
20ac2 61 63 63 65 73 73 65 64 2e 20 20 54 68 65 20 61 accessed. The a
20ac3 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 uth function.**
20ac4 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69 should return ei
20ac5 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 ther SQLITE_OK,
20ac6 53 51 4c 49 54 45 5f 44 45 4e 59 2c 20 6f 72 20 SQLITE_DENY, or
20ac7 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2e 20 20 SQLITE_IGNORE.
20ac8 49 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 If.** SQLITE_OK
20ac9 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 20 is returned, it
20aca 6d 65 61 6e 73 20 74 68 61 74 20 61 63 63 65 73 means that acces
20acb 73 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20 53 s is allowed. S
20acc 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65 QLITE_DENY.** me
20acd 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 4c ans that the SQL
20ace 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 statement will
20acf 6e 65 76 65 72 2d 72 75 6e 20 2d 20 74 68 65 20 never-run - the
20ad0 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 sqlite3_exec() c
20ad1 61 6c 6c 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 all.** will retu
20ad2 72 6e 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 rn with an error
20ad3 2e 20 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 . SQLITE_IGNORE
20ad4 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
20ad5 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a SQL statement.**
20ad6 20 73 68 6f 75 6c 64 20 72 75 6e 20 62 75 74 20 should run but
20ad7 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61 64 attempts to read
20ad8 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 the specified c
20ad9 6f 6c 75 6d 6e 20 77 69 6c 6c 20 72 65 74 75 72 olumn will retur
20ada 6e 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74 n NULL.** and at
20adb 74 65 6d 70 74 73 20 74 6f 20 77 72 69 74 65 20 tempts to write
20adc 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 the column will
20add 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a be ignored..**.*
20ade 2a 20 53 65 74 74 69 6e 67 20 74 68 65 20 61 75 * Setting the au
20adf 74 68 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e th function to N
20ae0 55 4c 4c 20 64 69 73 61 62 6c 65 73 20 74 68 69 ULL disables thi
20ae1 73 20 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66 s hook. The def
20ae2 61 75 6c 74 0a 2a 2a 20 73 65 74 74 69 6e 67 20 ault.** setting
20ae3 6f 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 of the auth func
20ae4 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f tion is NULL..*/
20ae5 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
20ae6 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
20ae7 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 orizer(. sqlite
20ae8 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 3 *db,. int (*x
20ae9 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c Auth)(void*,int,
20aea 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
20aeb 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t char*,const ch
20aec 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 ar*,const char*)
20aed 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 ,. void *pArg.)
20aee 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
20aef 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 x_enter(db->mute
20af0 78 29 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 x);. db->xAuth
20af1 3d 20 78 41 75 74 68 3b 0a 20 20 64 62 2d 3e 70 = xAuth;. db->p
20af2 41 75 74 68 41 72 67 20 3d 20 70 41 72 67 3b 0a AuthArg = pArg;.
20af3 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 sqlite3ExpireP
20af4 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 reparedStatement
20af5 73 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 s(db);. sqlite3
20af6 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d _mutex_leave(db-
20af7 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 >mutex);. retur
20af8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
20af9 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 /*.** Write an e
20afa 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 rror message int
20afb 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 o pParse->zErrMs
20afc 67 20 74 68 61 74 20 65 78 70 6c 61 69 6e 73 20 g that explains
20afd 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 73 65 72 that the.** user
20afe 2d 73 75 70 70 6c 69 65 64 20 61 75 74 68 6f 72 -supplied author
20aff 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ization function
20b00 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6c 6c returned an ill
20b01 65 67 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 egal value..*/.s
20b02 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
20b03 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f eAuthBadReturnCo
20b04 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 de(Parse *pParse
20b05 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 73 71 6c , int rc){. sql
20b06 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
20b07 72 73 65 2c 20 22 69 6c 6c 65 67 61 6c 20 72 65 rse, "illegal re
20b08 74 75 72 6e 20 76 61 6c 75 65 20 28 25 64 29 20 turn value (%d)
20b09 66 72 6f 6d 20 74 68 65 20 22 0a 20 20 20 20 22 from the ". "
20b0a 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 authorization fu
20b0b 6e 63 74 69 6f 6e 20 2d 20 73 68 6f 75 6c 64 20 nction - should
20b0c 62 65 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51 be SQLITE_OK, SQ
20b0d 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 22 0a 20 LITE_IGNORE, ".
20b0e 20 20 20 22 6f 72 20 53 51 4c 49 54 45 5f 44 45 "or SQLITE_DE
20b0f 4e 59 22 2c 20 72 63 29 3b 0a 20 20 70 50 61 72 NY", rc);. pPar
20b10 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
20b11 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ERROR;.}../*.**
20b12 54 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c 64 The pExpr should
20b13 20 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 be a TK_COLUMN
20b14 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65 expression. The
20b15 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 table referred
20b16 74 6f 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61 62 to.** is in pTab
20b17 4c 69 73 74 20 6f 72 20 65 6c 73 65 20 69 74 20 List or else it
20b18 69 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c is the NEW or OL
20b19 44 20 74 61 62 6c 65 20 6f 66 20 61 20 74 72 69 D table of a tri
20b1a 67 67 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b gger. .** Check
20b1b 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 69 73 to see if it is
20b1c 20 4f 4b 20 74 6f 20 72 65 61 64 20 74 68 69 73 OK to read this
20b1d 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 particular colu
20b1e 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 mn..**.** If the
20b1f 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 auth function r
20b20 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47 eturns SQLITE_IG
20b21 4e 4f 52 45 2c 20 63 68 61 6e 67 65 20 74 68 65 NORE, change the
20b22 20 54 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 TK_COLUMN .** i
20b23 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20 nstruction into
20b24 61 20 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74 a TK_NULL. If t
20b25 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e he auth function
20b26 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
20b27 44 45 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65 DENY,.** then ge
20b28 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e nerate an error.
20b29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20b2a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
20b2b 75 74 68 52 65 61 64 28 0a 20 20 50 61 72 73 65 uthRead(. Parse
20b2c 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
20b2d 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 /* The parser c
20b2e 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
20b2f 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 *pExpr,
20b30 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 /* The express
20b31 69 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75 74 ion to check aut
20b32 68 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f horization on */
20b33 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 . Schema *pSche
20b34 6d 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ma, /* The
20b35 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 schema of the ex
20b36 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72 pression */. Sr
20b37 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20 cList *pTabList
20b38 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 /* All table
20b39 20 74 68 61 74 20 70 45 78 70 72 20 6d 69 67 68 that pExpr migh
20b3a 74 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b t refer to */.){
20b3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
20b3c 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
20b3d 6e 74 20 72 63 3b 0a 20 20 54 61 62 6c 65 20 2a nt rc;. Table *
20b3e 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 2f pTab = 0; /
20b3f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e * The table bein
20b40 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 g read */. cons
20b41 74 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 t char *zCol;
20b42 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
20b43 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 column of the t
20b44 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 able */. int iS
20b45 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
20b46 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 54 61 62 /* Index in pTab
20b47 4c 69 73 74 2d 3e 61 5b 5d 20 6f 66 20 74 61 62 List->a[] of tab
20b48 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f le being read */
20b49 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
20b4a 44 42 61 73 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 DBase; /* Name
20b4b 20 6f 66 20 64 61 74 61 62 61 73 65 20 62 65 69 of database bei
20b4c 6e 67 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 ng accessed */.
20b4d 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70 TriggerStack *p
20b4e 53 74 61 63 6b 3b 20 2f 2a 20 54 68 65 20 73 74 Stack; /* The st
20b4f 61 63 6b 20 6f 66 20 63 75 72 72 65 6e 74 20 74 ack of current t
20b50 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 riggers */. int
20b51 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 iDb;
20b52 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 /* The index
20b53 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
20b54 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 the expression r
20b55 65 66 65 72 73 20 74 6f 20 2a 2f 0a 0a 20 20 69 efers to */.. i
20b56 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 f( db->xAuth==0
20b57 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
20b58 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f pExpr->op!=TK_CO
20b59 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 3b 0a 20 LUMN ) return;.
20b5a 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
20b5b 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 hemaToIndex(pPar
20b5c 73 65 2d 3e 64 62 2c 20 70 53 63 68 65 6d 61 29 se->db, pSchema)
20b5d 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b ;. if( iDb<0 ){
20b5e 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74 74 65 6d . /* An attem
20b5f 70 74 20 74 6f 20 72 65 61 64 20 61 20 63 6f 6c pt to read a col
20b60 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20 73 75 62 umn out of a sub
20b61 71 75 65 72 79 20 6f 72 20 6f 74 68 65 72 0a 20 query or other.
20b62 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 ** temporary
20b63 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 65 table. */. re
20b64 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 turn;. }. for(
20b65 69 53 72 63 3d 30 3b 20 70 54 61 62 4c 69 73 74 iSrc=0; pTabList
20b66 20 26 26 20 69 53 72 63 3c 70 54 61 62 4c 69 73 && iSrc<pTabLis
20b67 74 2d 3e 6e 53 72 63 3b 20 69 53 72 63 2b 2b 29 t->nSrc; iSrc++)
20b68 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d {. if( pExpr-
20b69 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69 73 >iTable==pTabLis
20b6a 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72 73 t->a[iSrc].iCurs
20b6b 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a or ) break;. }.
20b6c 20 20 69 66 28 20 69 53 72 63 3e 3d 30 20 26 26 if( iSrc>=0 &&
20b6d 20 70 54 61 62 4c 69 73 74 20 26 26 20 69 53 72 pTabList && iSr
20b6e 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 c<pTabList->nSrc
20b6f 20 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 ){. pTab = p
20b70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d TabList->a[iSrc]
20b71 2e 70 54 61 62 3b 0a 20 20 7d 65 6c 73 65 20 69 .pTab;. }else i
20b72 66 28 20 28 70 53 74 61 63 6b 20 3d 20 70 50 61 f( (pStack = pPa
20b73 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 29 21 rse->trigStack)!
20b74 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 =0 ){. /* Thi
20b75 73 20 6d 75 73 74 20 62 65 20 61 6e 20 61 74 74 s must be an att
20b76 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 65 empt to read the
20b77 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 NEW or OLD pseu
20b78 64 6f 2d 74 61 62 6c 65 73 0a 20 20 20 20 2a 2a do-tables. **
20b79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a 20 of a trigger..
20b7a 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
20b7b 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d ( pExpr->iTable=
20b7c 3d 70 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 =pStack->newIdx
20b7d 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 || pExpr->iTable
20b7e 3d 3d 70 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 ==pStack->oldIdx
20b7f 20 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 );. pTab = p
20b80 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 7d Stack->pTab;. }
20b81 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 . if( pTab==0 )
20b82 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 return;. if( p
20b83 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 Expr->iColumn>=0
20b84 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
20b85 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 pExpr->iColumn<p
20b86 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 Tab->nCol );.
20b87 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 zCol = pTab->aC
20b88 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d ol[pExpr->iColum
20b89 6e 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 n].zName;. }els
20b8a 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b 65 e if( pTab->iPKe
20b8b 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 y>=0 ){. asse
20b8c 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c rt( pTab->iPKey<
20b8d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 pTab->nCol );.
20b8e 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 zCol = pTab->a
20b8f 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d Col[pTab->iPKey]
20b90 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b .zName;. }else{
20b91 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f 57 . zCol = "ROW
20b92 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 ID";. }. asser
20b93 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 t( iDb>=0 && iDb
20b94 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 <db->nDb );. zD
20b95 42 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 Base = db->aDb[i
20b96 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 20 Db].zName;. rc
20b97 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e = db->xAuth(db->
20b98 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54 45 pAuthArg, SQLITE
20b99 5f 52 45 41 44 2c 20 70 54 61 62 2d 3e 7a 4e 61 _READ, pTab->zNa
20b9a 6d 65 2c 20 7a 43 6f 6c 2c 20 7a 44 42 61 73 65 me, zCol, zDBase
20b9b 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
20b9c 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 pParse->zAut
20b9d 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 hContext);. if(
20b9e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f rc==SQLITE_IGNO
20b9f 52 45 20 29 7b 0a 20 20 20 20 70 45 78 70 72 2d RE ){. pExpr-
20ba0 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 >op = TK_NULL;.
20ba1 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 }else if( rc==S
20ba2 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 QLITE_DENY ){.
20ba3 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32 20 if( db->nDb>2
20ba4 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20 20 20 || iDb!=0 ){.
20ba5 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
20ba6 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63 65 sg(pParse, "acce
20ba7 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73 20 69 ss to %s.%s.%s i
20ba8 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20 0a s prohibited", .
20ba9 20 20 20 20 20 20 20 20 20 7a 44 42 61 73 65 2c zDBase,
20baa 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 pTab->zName, zC
20bab 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ol);. }else{.
20bac 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
20bad 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 orMsg(pParse, "a
20bae 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20 69 ccess to %s.%s i
20baf 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 70 54 s prohibited",pT
20bb0 61 62 2d 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29 3b ab->zName,zCol);
20bb1 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 . }. pPars
20bb2 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 e->rc = SQLITE_A
20bb3 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 UTH;. }else if(
20bb4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
20bb5 7b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 {. sqliteAuth
20bb6 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 BadReturnCode(pP
20bb7 61 72 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 7d arse, rc);. }.}
20bb8 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 ../*.** Do an au
20bb9 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 thorization chec
20bba 6b 20 75 73 69 6e 67 20 74 68 65 20 63 6f 64 65 k using the code
20bbb 20 61 6e 64 20 61 72 67 75 6d 65 6e 74 73 20 67 and arguments g
20bbc 69 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a iven. Return.**
20bbd 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f either SQLITE_O
20bbe 4b 20 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 K (zero) or SQLI
20bbf 54 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c TE_IGNORE or SQL
20bc0 49 54 45 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 ITE_DENY. If SQ
20bc1 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 LITE_DENY.** is
20bc2 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 returned, then t
20bc3 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 he error count a
20bc4 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 nd error message
20bc5 20 69 6e 20 70 50 61 72 73 65 20 61 72 65 0a 2a in pParse are.*
20bc6 2a 20 6d 6f 64 69 66 69 65 64 20 61 70 70 72 6f * modified appro
20bc7 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c priately..*/.SQL
20bc8 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
20bc9 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b sqlite3AuthCheck
20bca 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
20bcb 65 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 e,. int code,.
20bcc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 const char *zAr
20bcd 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 g1,. const char
20bce 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 *zArg2,. const
20bcf 20 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a char *zArg3.){.
20bd0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
20bd1 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e pParse->db;. in
20bd2 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 t rc;.. /* Don'
20bd3 74 20 64 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 t do any authori
20bd4 7a 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 zation checks if
20bd5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
20bd6 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 initialising.
20bd7 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 72 ** or if the par
20bd8 73 65 72 20 69 73 20 62 65 69 6e 67 20 69 6e 76 ser is being inv
20bd9 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e oked from within
20bda 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 sqlite3_declare
20bdb 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 _vtab.. */. if
20bdc 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 ( db->init.busy
20bdd 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 || IN_DECLARE_VT
20bde 41 42 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e AB ){. return
20bdf 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
20be0 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68 . if( db->xAuth
20be1 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
20be2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
20be3 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 . rc = db->xAut
20be4 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 h(db->pAuthArg,
20be5 63 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 code, zArg1, zAr
20be6 67 32 2c 20 7a 41 72 67 33 2c 20 70 50 61 72 73 g2, zArg3, pPars
20be7 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 e->zAuthContext)
20be8 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
20be9 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 TE_DENY ){. s
20bea 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
20beb 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 Parse, "not auth
20bec 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 orized");. pP
20bed 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 arse->rc = SQLIT
20bee 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 E_AUTH;. }else
20bef 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
20bf0 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f K && rc!=SQLITE_
20bf1 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 IGNORE ){. rc
20bf2 20 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a = SQLITE_DENY;.
20bf3 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61 sqliteAuthBa
20bf4 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72 dReturnCode(pPar
20bf5 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 se, rc);. }. r
20bf6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
20bf7 2a 2a 20 50 75 73 68 20 61 6e 20 61 75 74 68 6f ** Push an autho
20bf8 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 rization context
20bf9 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 6f . After this ro
20bfa 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
20bfb 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72 the.** zArg3 ar
20bfc 67 75 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f 72 gument to author
20bfd 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b ization callback
20bfe 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65 s will be zConte
20bff 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70 xt until.** popp
20c00 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73 ed. Or if pPars
20c01 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69 e==0, this routi
20c02 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
20c03 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20c04 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 void sqlite3Aut
20c05 68 43 6f 6e 74 65 78 74 50 75 73 68 28 0a 20 20 hContextPush(.
20c06 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 Parse *pParse,.
20c07 20 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 AuthContext *pC
20c08 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 ontext, . const
20c09 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a char *zContext.
20c0a 29 7b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 ){. pContext->p
20c0b 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
20c0c 20 20 69 66 28 20 70 50 61 72 73 65 20 29 7b 0a if( pParse ){.
20c0d 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 pContext->zA
20c0e 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 uthContext = pPa
20c0f 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 rse->zAuthContex
20c10 74 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a t;. pParse->z
20c11 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43 AuthContext = zC
20c12 6f 6e 74 65 78 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f ontext;. }.}../
20c13 2a 0a 2a 2a 20 50 6f 70 20 61 6e 20 61 75 74 68 *.** Pop an auth
20c14 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 orization contex
20c15 74 20 74 68 61 74 20 77 61 73 20 70 72 65 76 69 t that was previ
20c16 6f 75 73 6c 79 20 70 75 73 68 65 64 0a 2a 2a 20 ously pushed.**
20c17 62 79 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f by sqlite3AuthCo
20c18 6e 74 65 78 74 50 75 73 68 0a 2a 2f 0a 53 51 4c ntextPush.*/.SQL
20c19 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
20c1a 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 sqlite3AuthCont
20c1b 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 extPop(AuthConte
20c1c 78 74 20 2a 70 43 6f 6e 74 65 78 74 29 7b 0a 20 xt *pContext){.
20c1d 20 69 66 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70 if( pContext->p
20c1e 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f Parse ){. pCo
20c1f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a ntext->pParse->z
20c20 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 43 AuthContext = pC
20c21 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e ontext->zAuthCon
20c22 74 65 78 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65 text;. pConte
20c23 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 30 3b 0a xt->pParse = 0;.
20c24 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a }.}..#endif /*
20c25 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
20c26 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 2f HORIZATION */../
20c27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
20c28 6e 64 20 6f 66 20 61 75 74 68 2e 63 20 2a 2a 2a nd of auth.c ***
20c29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20c2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20c2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
20c2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
20c2d 65 67 69 6e 20 66 69 6c 65 20 62 75 69 6c 64 2e egin file build.
20c2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
20c2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
20c31 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d *.** 2001 Septem
20c32 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 15.**.** The
20c33 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
20c34 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
20c35 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
20c36 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
20c37 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
20c38 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
20c39 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
20c3a 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
20c3b 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
20c3c 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
20c3d 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
20c3e 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
20c3f 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
20c40 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
20c41 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
20c42 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
20c43 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
20c44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20c45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20c46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20c47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20c48 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
20c49 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f le contains C co
20c4a 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 de routines that
20c4b 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 are called by t
20c4c 68 65 20 53 51 4c 69 74 65 20 70 61 72 73 65 72 he SQLite parser
20c4d 0a 2a 2a 20 77 68 65 6e 20 73 79 6e 74 61 78 20 .** when syntax
20c4e 72 75 6c 65 73 20 61 72 65 20 72 65 64 75 63 65 rules are reduce
20c4f 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 d. The routines
20c50 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 68 61 in this file ha
20c51 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c ndle the.** foll
20c52 6f 77 69 6e 67 20 6b 69 6e 64 73 20 6f 66 20 53 owing kinds of S
20c53 51 4c 20 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a QL syntax:.**.**
20c54 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c CREATE TABL
20c55 45 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 54 41 E.** DROP TA
20c56 42 4c 45 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 BLE.** CREAT
20c57 45 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 44 E INDEX.** D
20c58 52 4f 50 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 ROP INDEX.**
20c59 20 63 72 65 61 74 69 6e 67 20 49 44 20 6c 69 73 creating ID lis
20c5a 74 73 0a 2a 2a 20 20 20 20 20 42 45 47 49 4e 20 ts.** BEGIN
20c5b 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 TRANSACTION.**
20c5c 20 20 20 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 20 COMMIT.**
20c5d 20 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a 20 ROLLBACK.**.**
20c5e 24 49 64 3a 20 62 75 69 6c 64 2e 63 2c 76 20 31 $Id: build.c,v 1
20c5f 2e 35 32 37 20 32 30 30 39 2f 30 33 2f 33 31 20 .527 2009/03/31
20c60 30 33 3a 34 31 3a 35 37 20 73 68 61 6e 65 20 45 03:41:57 shane E
20c61 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 xp $.*/../*.** T
20c62 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
20c63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 6e 65 77 alled when a new
20c64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 SQL statement i
20c65 73 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a s beginning to.*
20c66 2a 20 62 65 20 70 61 72 73 65 64 2e 20 20 49 6e * be parsed. In
20c67 69 74 69 61 6c 69 7a 65 20 74 68 65 20 70 50 61 itialize the pPa
20c68 72 73 65 20 73 74 72 75 63 74 75 72 65 20 61 73 rse structure as
20c69 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 needed..*/.SQLI
20c6a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
20c6b 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 sqlite3BeginPars
20c6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
20c6d 20 69 6e 74 20 65 78 70 6c 61 69 6e 46 6c 61 67 int explainFlag
20c6e 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 65 78 70 ){. pParse->exp
20c6f 6c 61 69 6e 20 3d 20 28 75 38 29 65 78 70 6c 61 lain = (u8)expla
20c70 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61 72 73 65 inFlag;. pParse
20c71 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a 0a 23 ->nVar = 0;.}..#
20c72 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20c73 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
20c74 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 4c /*.** The TableL
20c75 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20 69 73 ock structure is
20c76 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 74 68 only used by th
20c77 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f e sqlite3TableLo
20c78 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 6f 64 65 ck() and.** code
20c79 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66 75 6e TableLocks() fun
20c7a 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 ctions..*/.struc
20c7b 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a 20 20 t TableLock {.
20c7c 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
20c7d 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
20c7e 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 base containing
20c7f 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 the table to be
20c80 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 locked */. int
20c81 69 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 iTab;
20c82 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 /* The root pag
20c83 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 e of the table t
20c84 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 o be locked */.
20c85 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b u8 isWriteLock;
20c86 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
20c87 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e 20 20 46 r write lock. F
20c88 61 6c 73 65 20 66 6f 72 20 61 20 72 65 61 64 20 alse for a read
20c89 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 lock */. const
20c8a 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f char *zName; /
20c8b 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 * Name of the ta
20c8c 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ble */.};../*.**
20c8d 20 52 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 Record the fact
20c8e 20 74 68 61 74 20 77 65 20 77 61 6e 74 20 74 6f that we want to
20c8f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20 61 74 lock a table at
20c90 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a 2a 0a run-time. .**.
20c91 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 ** The table to
20c92 62 65 20 6c 6f 63 6b 65 64 20 68 61 73 20 72 6f be locked has ro
20c93 6f 74 20 70 61 67 65 20 69 54 61 62 20 61 6e 64 ot page iTab and
20c94 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 64 61 74 is found in dat
20c95 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 20 abase iDb..** A
20c96 72 65 61 64 20 6f 72 20 61 20 77 72 69 74 65 20 read or a write
20c97 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74 61 6b 65 lock can be take
20c98 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 69 n depending on i
20c99 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a sWritelock..**.*
20c9a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a * This routine j
20c9b 75 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 ust records the
20c9c 66 61 63 74 20 74 68 61 74 20 74 68 65 20 6c 6f fact that the lo
20c9d 63 6b 20 69 73 20 64 65 73 69 72 65 64 2e 20 20 ck is desired.
20c9e 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 6d The.** code to m
20c9f 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f 63 63 ake the lock occ
20ca0 75 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 ur is generated
20ca1 62 79 20 61 20 6c 61 74 65 72 20 63 61 6c 6c 20 by a later call
20ca2 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c to.** codeTableL
20ca3 6f 63 6b 73 28 29 20 77 68 69 63 68 20 6f 63 63 ocks() which occ
20ca4 75 72 73 20 64 75 72 69 6e 67 20 73 71 6c 69 74 urs during sqlit
20ca5 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 e3FinishCoding()
20ca6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20ca7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20ca8 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50 61 72 TableLock(. Par
20ca9 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
20caa 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
20cab 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c xt */. int iDb,
20cac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
20cad 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 dex of the datab
20cae 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ase containing t
20caf 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b he table to lock
20cb0 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 */. int iTab,
20cb1 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
20cb2 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
20cb3 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 the table to be
20cb4 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 locked */. u8 i
20cb5 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20 20 2f sWriteLock, /
20cb6 2a 20 54 72 75 65 20 66 6f 72 20 61 20 77 72 69 * True for a wri
20cb7 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e te lock */. con
20cb8 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 st char *zName
20cb9 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 /* Name of the t
20cba 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 able to be locke
20cbb 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b d */.){. int i;
20cbc 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 0a 20 . int nBytes;.
20cbd 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0a 0a TableLock *p;..
20cbe 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 if( iDb<0 ){.
20cbf 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a return;. }..
20cc0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
20cc1 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b rse->nTableLock;
20cc2 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 26 i++){. p = &
20cc3 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f pParse->aTableLo
20cc4 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 ck[i];. if( p
20cc5 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26 20 70 2d ->iDb==iDb && p-
20cc6 3e 69 54 61 62 3d 3d 69 54 61 62 20 29 7b 0a 20 >iTab==iTab ){.
20cc7 20 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c p->isWriteL
20cc8 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57 72 69 74 ock = (p->isWrit
20cc9 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 69 74 65 eLock || isWrite
20cca 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 72 65 74 Lock);. ret
20ccb 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a urn;. }. }..
20ccc 20 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f nBytes = sizeo
20ccd 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20 2a 20 28 f(TableLock) * (
20cce 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f pParse->nTableLo
20ccf 63 6b 2b 31 29 3b 0a 20 20 70 50 61 72 73 65 2d ck+1);. pParse-
20cd0 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 0a 20 >aTableLock = .
20cd1 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 sqlite3DbRe
20cd2 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 50 61 72 allocOrFree(pPar
20cd3 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e se->db, pParse->
20cd4 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e 42 79 74 aTableLock, nByt
20cd5 65 73 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 es);. if( pPars
20cd6 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 29 7b e->aTableLock ){
20cd7 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 73 65 . p = &pParse
20cd8 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 70 50 61 ->aTableLock[pPa
20cd9 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b rse->nTableLock+
20cda 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d +];. p->iDb =
20cdb 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 iDb;. p->iTa
20cdc 62 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 70 2d b = iTab;. p-
20cdd 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69 >isWriteLock = i
20cde 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 sWriteLock;.
20cdf 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 p->zName = zName
20ce0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
20ce1 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 Parse->nTableLoc
20ce2 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 k = 0;. pPars
20ce3 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
20ce4 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a led = 1;. }.}..
20ce5 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 /*.** Code an OP
20ce6 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 _TableLock instr
20ce7 75 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 uction for each
20ce8 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62 79 20 table locked by
20ce9 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 the.** statement
20cea 20 28 63 6f 6e 66 69 67 75 72 65 64 20 62 79 20 (configured by
20ceb 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
20cec 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a 2a 2f TableLock())..*/
20ced 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 .static void cod
20cee 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50 61 72 73 eTableLocks(Pars
20cef 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e e *pParse){. in
20cf0 74 20 69 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 t i;. Vdbe *pVd
20cf1 62 65 3b 20 0a 0a 20 20 69 66 28 20 30 3d 3d 28 be; .. if( 0==(
20cf2 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 47 pVdbe = sqlite3G
20cf3 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29 20 etVdbe(pParse))
20cf4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 ){. return;.
20cf5 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }.. for(i=0; i
20cf6 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c <pParse->nTableL
20cf7 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 54 ock; i++){. T
20cf8 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d 20 26 70 ableLock *p = &p
20cf9 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 Parse->aTableLoc
20cfa 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 70 31 k[i];. int p1
20cfb 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20 20 20 73 = p->iDb;. s
20cfc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
20cfd 28 70 56 64 62 65 2c 20 4f 50 5f 54 61 62 6c 65 (pVdbe, OP_Table
20cfe 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e 69 54 61 Lock, p1, p->iTa
20cff 62 2c 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 b, p->isWriteLoc
20d00 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 k,.
20d01 20 20 20 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d p->zNam
20d02 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 e, P4_STATIC);.
20d03 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 }.}.#else. #de
20d04 66 69 6e 65 20 63 6f 64 65 54 61 62 6c 65 4c 6f fine codeTableLo
20d05 63 6b 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f cks(x).#endif../
20d06 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
20d07 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 e is called afte
20d08 72 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 r a single SQL s
20d09 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65 tatement has bee
20d0a 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61 6e 64 20 n.** parsed and
20d0b 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 74 a VDBE program t
20d0c 6f 20 65 78 65 63 75 74 65 20 74 68 61 74 20 73 o execute that s
20d0d 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65 tatement has bee
20d0e 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64 2e 20 20 n.** prepared.
20d0f 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 75 74 This routine put
20d10 73 20 74 68 65 20 66 69 6e 69 73 68 69 6e 67 20 s the finishing
20d11 74 6f 75 63 68 65 73 20 6f 6e 20 74 68 65 0a 2a touches on the.*
20d12 2a 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 * VDBE program a
20d13 6e 64 20 72 65 73 65 74 73 20 74 68 65 20 70 50 nd resets the pP
20d14 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66 arse structure f
20d15 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 70 or the next.** p
20d16 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 arse..**.** Note
20d17 20 74 68 61 74 20 69 66 20 61 6e 20 65 72 72 6f that if an erro
20d18 72 20 6f 63 63 75 72 72 65 64 2c 20 69 74 20 6d r occurred, it m
20d19 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65 ight be the case
20d1a 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 45 that.** no VDBE
20d1b 20 63 6f 64 65 20 77 61 73 20 67 65 6e 65 72 61 code was genera
20d1c 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
20d1d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20d1e 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 te3FinishCoding(
20d1f 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a Parse *pParse){.
20d20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
20d21 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 64 62 20 Vdbe *v;.. db
20d22 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
20d23 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
20d24 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 iled ) return;.
20d25 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 if( pParse->nes
20d26 74 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ted ) return;.
20d27 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 if( pParse->nErr
20d28 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a ) return;.. /*
20d29 20 42 65 67 69 6e 20 62 79 20 67 65 6e 65 72 61 Begin by genera
20d2a 74 69 6e 67 20 73 6f 6d 65 20 74 65 72 6d 69 6e ting some termin
20d2b 61 74 69 6f 6e 20 63 6f 64 65 20 61 74 20 74 68 ation code at th
20d2c 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a e end of the. *
20d2d 2a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 0a 20 * vdbe program.
20d2e 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */. v = sqlite
20d2f 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
20d30 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 ;. if( v ){.
20d31 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20d32 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a p0(v, OP_Halt);.
20d33 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6f 6b . /* The cook
20d34 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 73 ie mask contains
20d35 20 6f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 one bit for eac
20d36 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 h database file
20d37 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 28 42 69 open.. ** (Bi
20d38 74 20 30 20 69 73 20 66 6f 72 20 6d 61 69 6e 2c t 0 is for main,
20d39 20 62 69 74 20 31 20 69 73 20 66 6f 72 20 74 65 bit 1 is for te
20d3a 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 mp, and so forth
20d3b 2e 29 20 20 42 69 74 73 20 61 72 65 0a 20 20 20 .) Bits are.
20d3c 20 2a 2a 20 73 65 74 20 66 6f 72 20 65 61 63 68 ** set for each
20d3d 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 69 database that i
20d3e 73 20 75 73 65 64 2e 20 20 47 65 6e 65 72 61 74 s used. Generat
20d3f 65 20 63 6f 64 65 20 74 6f 20 73 74 61 72 74 20 e code to start
20d40 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 a. ** transac
20d41 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20 75 73 65 tion on each use
20d42 64 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 d database and t
20d43 6f 20 76 65 72 69 66 79 20 74 68 65 20 73 63 68 o verify the sch
20d44 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a ema cookie. *
20d45 2a 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20 64 * on each used d
20d46 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a atabase.. */.
20d47 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e if( pParse->
20d48 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 29 7b 0a cookieGoto>0 ){.
20d49 20 20 20 20 20 20 75 33 32 20 6d 61 73 6b 3b 0a u32 mask;.
20d4a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 int iDb;.
20d4b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20d4c 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 50 61 72 JumpHere(v, pPar
20d4d 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 2d 31 se->cookieGoto-1
20d4e 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 44 62 );. for(iDb
20d4f 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 44 62 3c =0, mask=1; iDb<
20d50 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b 3c 3c 3d db->nDb; mask<<=
20d51 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20 1, iDb++){.
20d52 20 20 20 69 66 28 20 28 6d 61 73 6b 20 26 20 70 if( (mask & p
20d53 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 Parse->cookieMas
20d54 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 k)==0 ) continue
20d55 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
20d56 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 3VdbeUsesBtree(v
20d57 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 , iDb);.
20d58 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20d59 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61 63 74 69 2(v,OP_Transacti
20d5a 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73 6b 20 26 on, iDb, (mask &
20d5b 20 70 50 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 pParse->writeMa
20d5c 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 sk)!=0);.
20d5d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20d5e 70 32 28 76 2c 4f 50 5f 56 65 72 69 66 79 43 6f p2(v,OP_VerifyCo
20d5f 6f 6b 69 65 2c 20 69 44 62 2c 20 70 50 61 72 73 okie, iDb, pPars
20d60 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 e->cookieValue[i
20d61 44 62 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 69 Db]);. }.#i
20d62 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
20d63 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
20d64 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 {. i
20d65 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f nt i;. fo
20d66 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d r(i=0; i<pParse-
20d67 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 >nVtabLock; i++)
20d68 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 {. char
20d69 20 2a 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a *vtab = (char *
20d6a 29 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c )pParse->apVtabL
20d6b 6f 63 6b 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20 ock[i]->pVtab;.
20d6c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
20d6d 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
20d6e 5f 56 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 _VBegin, 0, 0, 0
20d6f 2c 20 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29 , vtab, P4_VTAB)
20d70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
20d71 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61 pParse->nVta
20d72 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 bLock = 0;.
20d73 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 }.#endif..
20d74 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 /* Once all the
20d75 20 63 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65 cookies have be
20d76 65 6e 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 en verified and
20d77 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 transactions ope
20d78 6e 65 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f ned, . ** o
20d79 62 74 61 69 6e 20 74 68 65 20 72 65 71 75 69 72 btain the requir
20d7a 65 64 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 ed table-locks.
20d7b 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 This is a no-op
20d7c 75 6e 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20 unless the .
20d7d 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 ** shared-cach
20d7e 65 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 e feature is ena
20d7f 62 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 bled.. */.
20d80 20 20 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f codeTableLo
20d81 63 6b 73 28 70 50 61 72 73 65 29 3b 0a 20 20 20 cks(pParse);.
20d82 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20d83 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c dOp2(v, OP_Goto,
20d84 20 30 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 0, pParse->cook
20d85 69 65 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 20 ieGoto);. }.
20d86 20 7d 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 }... /* Get th
20d87 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72 e VDBE program r
20d88 65 61 64 79 20 66 6f 72 20 65 78 65 63 75 74 69 eady for executi
20d89 6f 6e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76 20 on. */. if( v
20d8a 26 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d && pParse->nErr=
20d8b 3d 30 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f =0 && !db->mallo
20d8c 63 46 61 69 6c 65 64 20 29 7b 0a 23 69 66 64 65 cFailed ){.#ifde
20d8d 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
20d8e 20 20 20 46 49 4c 45 20 2a 74 72 61 63 65 20 3d FILE *trace =
20d8f 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 (db->flags & SQ
20d90 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 29 21 LITE_VdbeTrace)!
20d91 3d 30 20 3f 20 73 74 64 6f 75 74 20 3a 20 30 3b =0 ? stdout : 0;
20d92 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
20d93 54 72 61 63 65 28 76 2c 20 74 72 61 63 65 29 3b Trace(v, trace);
20d94 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 .#endif. asse
20d95 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 rt( pParse->disa
20d96 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 bleColCache==0 )
20d97 3b 20 20 2f 2a 20 44 69 73 61 62 6c 65 73 20 61 ; /* Disables a
20d98 6e 64 20 72 65 2d 65 6e 61 62 6c 65 73 20 6d 61 nd re-enables ma
20d99 74 63 68 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 tch */. sqlit
20d9a 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 e3VdbeMakeReady(
20d9b 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c v, pParse->nVar,
20d9c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2c 0a 20 pParse->nMem,.
20d9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d9e 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
20d9f 6e 54 61 62 2c 20 70 50 61 72 73 65 2d 3e 65 78 nTab, pParse->ex
20da0 70 6c 61 69 6e 29 3b 0a 20 20 20 20 70 50 61 72 plain);. pPar
20da1 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
20da2 44 4f 4e 45 3b 0a 20 20 20 20 70 50 61 72 73 65 DONE;. pParse
20da3 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 ->colNamesSet =
20da4 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 0;. }else if( p
20da5 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 Parse->rc==SQLIT
20da6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 E_OK ){. pPar
20da7 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
20da8 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61 ERROR;. }. pPa
20da9 72 73 65 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 rse->nTab = 0;.
20daa 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 pParse->nMem =
20dab 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 0;. pParse->nSe
20dac 74 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d t = 0;. pParse-
20dad 3e 6e 56 61 72 20 3d 20 30 3b 0a 20 20 70 50 61 >nVar = 0;. pPa
20dae 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 rse->cookieMask
20daf 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 = 0;. pParse->c
20db0 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d ookieGoto = 0;.}
20db1 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 ../*.** Run the
20db2 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65 20 parser and code
20db3 67 65 6e 65 72 61 74 6f 72 20 72 65 63 75 72 73 generator recurs
20db4 69 76 65 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 ively in order t
20db5 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f o generate.** co
20db6 64 65 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73 de for the SQL s
20db7 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f tatement given o
20db8 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 nto the end of t
20db9 68 65 20 70 50 61 72 73 65 20 63 6f 6e 74 65 78 he pParse contex
20dba 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 t.** currently u
20dbb 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
20dbc 6e 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 n. When the par
20dbd 73 65 72 20 69 73 20 72 75 6e 20 72 65 63 75 72 ser is run recur
20dbe 73 69 76 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77 sively.** this w
20dbf 61 79 2c 20 74 68 65 20 66 69 6e 61 6c 20 4f 50 ay, the final OP
20dc0 5f 48 61 6c 74 20 69 73 20 6e 6f 74 20 61 70 70 _Halt is not app
20dc1 65 6e 64 65 64 20 61 6e 64 20 6f 74 68 65 72 20 ended and other
20dc2 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a initialization.*
20dc3 2a 20 61 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69 * and finalizati
20dc4 6f 6e 20 73 74 65 70 73 20 61 72 65 20 6f 6d 69 on steps are omi
20dc5 74 74 65 64 20 62 65 63 61 75 73 65 20 74 68 6f tted because tho
20dc6 73 65 20 61 72 65 20 68 61 6e 64 6c 69 6e 67 20 se are handling
20dc7 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d by the.** outerm
20dc8 6f 73 74 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a ost parser..**.*
20dc9 2a 20 4e 6f 74 20 65 76 65 72 79 74 68 69 6e 67 * Not everything
20dca 20 69 73 20 6e 65 73 74 61 62 6c 65 2e 20 20 54 is nestable. T
20dcb 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 his facility is
20dcc 64 65 73 69 67 6e 65 64 20 74 6f 20 70 65 72 6d designed to perm
20dcd 69 74 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 it.** INSERT, UP
20dce 44 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 DATE, and DELETE
20dcf 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 67 61 69 operations agai
20dd0 6e 73 74 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 nst SQLITE_MASTE
20dd1 52 2e 20 20 55 73 65 0a 2a 2a 20 63 61 72 65 20 R. Use.** care
20dd2 69 66 20 79 6f 75 20 64 65 63 69 64 65 20 74 6f if you decide to
20dd3 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 69 73 try to use this
20dd4 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d routine for som
20dd5 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 e other purposes
20dd6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20dd7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20dd8 4e 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73 NestedParse(Pars
20dd9 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
20dda 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
20ddb 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
20ddc 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c ap;. char *zSql
20ddd 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 ;. char *zErrMs
20dde 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 g = 0;. sqlite3
20ddf 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
20de0 62 3b 0a 23 20 64 65 66 69 6e 65 20 53 41 56 45 b;.# define SAVE
20de1 5f 53 5a 20 20 28 73 69 7a 65 6f 66 28 50 61 72 _SZ (sizeof(Par
20de2 73 65 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 50 se) - offsetof(P
20de3 61 72 73 65 2c 6e 56 61 72 29 29 0a 20 20 63 68 arse,nVar)). ch
20de4 61 72 20 73 61 76 65 42 75 66 5b 53 41 56 45 5f ar saveBuf[SAVE_
20de5 53 5a 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 SZ];.. if( pPar
20de6 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 se->nErr ) retur
20de7 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 n;. assert( pPa
20de8 72 73 65 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29 rse->nested<10 )
20de9 3b 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68 ; /* Nesting sh
20dea 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20 ould only be of
20deb 6c 69 6d 69 74 65 64 20 64 65 70 74 68 20 2a 2f limited depth */
20dec 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 . va_start(ap,
20ded 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c zFormat);. zSql
20dee 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
20def 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 tf(db, zFormat,
20df0 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
20df1 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 );. if( zSql==0
20df2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 ){. return;
20df3 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 /* A malloc mu
20df4 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a st have failed *
20df5 2f 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e /. }. pParse->
20df6 6e 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 nested++;. memc
20df7 70 79 28 73 61 76 65 42 75 66 2c 20 26 70 50 61 py(saveBuf, &pPa
20df8 72 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f rse->nVar, SAVE_
20df9 53 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 SZ);. memset(&p
20dfa 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 Parse->nVar, 0,
20dfb 53 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 SAVE_SZ);. sqli
20dfc 74 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 te3RunParser(pPa
20dfd 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 rse, zSql, &zErr
20dfe 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 Msg);. sqlite3D
20dff 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 bFree(db, zErrMs
20e00 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 g);. sqlite3DbF
20e01 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 ree(db, zSql);.
20e02 20 6d 65 6d 63 70 79 28 26 70 50 61 72 73 65 2d memcpy(&pParse-
20e03 3e 6e 56 61 72 2c 20 73 61 76 65 42 75 66 2c 20 >nVar, saveBuf,
20e04 53 41 56 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72 SAVE_SZ);. pPar
20e05 73 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a se->nested--;.}.
20e06 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 ./*.** Locate th
20e07 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 e in-memory stru
20e08 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 cture that descr
20e09 69 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 ibes a particula
20e0a 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 r database.** ta
20e0b 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 ble given the na
20e0c 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 me of that table
20e0d 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 and (optionally
20e0e 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 ) the name of th
20e0f 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f e.** database co
20e10 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 ntaining the tab
20e11 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c le. Return NULL
20e12 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a if not found..*
20e13 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 *.** If zDatabas
20e14 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 e is 0, all data
20e15 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 bases are search
20e16 65 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 ed for the table
20e17 20 61 6e 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 and the.** firs
20e18 74 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 t matching table
20e19 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 is returned. (
20e1a 4e 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 No checking for
20e1b 64 75 70 6c 69 63 61 74 65 20 74 61 62 6c 65 0a duplicate table.
20e1c 2a 2a 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 ** names is done
20e1d 2e 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f .) The search o
20e1e 72 64 65 72 20 69 73 20 54 45 4d 50 20 66 69 72 rder is TEMP fir
20e1f 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 st, then MAIN, t
20e20 68 65 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c hen any.** auxil
20e21 69 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61 iary databases a
20e22 64 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 dded using the A
20e23 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a TTACH command..*
20e24 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 *.** See also sq
20e25 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
20e26 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
20e27 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c IVATE Table *sql
20e28 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 ite3FindTable(sq
20e29 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
20e2a 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f char *zName, co
20e2b 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62 nst char *zDatab
20e2c 61 73 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 ase){. Table *p
20e2d 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 = 0;. int i;.
20e2e 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 int nName;. as
20e2f 73 65 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29 sert( zName!=0 )
20e30 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 ;. nName = sqli
20e31 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e te3Strlen(db, zN
20e32 61 6d 65 29 20 2b 20 31 3b 0a 20 20 66 6f 72 28 ame) + 1;. for(
20e33 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 i=OMIT_TEMPDB; i
20e34 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
20e35 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 int j = (i<2
20e36 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f ) ? i^1 : i; /
20e37 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 * Search TEMP be
20e38 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 fore MAIN */.
20e39 20 69 66 28 20 7a 44 61 74 61 62 61 73 65 21 3d if( zDatabase!=
20e3a 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 0 && sqlite3StrI
20e3b 43 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64 Cmp(zDatabase, d
20e3c 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 b->aDb[j].zName)
20e3d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
20e3e 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 p = sqlite3Hash
20e3f 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d Find(&db->aDb[j]
20e40 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 .pSchema->tblHas
20e41 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 h, zName, nName)
20e42 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72 ;. if( p ) br
20e43 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 eak;. }. retur
20e44 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f n p;.}../*.** Lo
20e45 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f cate the in-memo
20e46 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ry structure tha
20e47 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61 t describes a pa
20e48 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 rticular databas
20e49 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e e.** table given
20e4a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 the name of tha
20e4b 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 t table and (opt
20e4c 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d ionally) the nam
20e4d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 e of the.** data
20e4e 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 base containing
20e4f 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 the table. Retu
20e50 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 rn NULL if not f
20e51 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61 76 ound. Also leav
20e52 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 e an.** error me
20e53 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d ssage in pParse-
20e54 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 >zErrMsg..**.**
20e55 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 The difference b
20e56 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74 etween this rout
20e57 69 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33 46 ine and sqlite3F
20e58 69 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74 68 indTable() is th
20e59 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 at this.** routi
20e5a 6e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72 72 ne leaves an err
20e5b 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 or message in pP
20e5c 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77 68 arse->zErrMsg wh
20e5d 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69 ere.** sqlite3Fi
20e5e 6e 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20 6e ndTable() does n
20e5f 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ot..*/.SQLITE_PR
20e60 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c IVATE Table *sql
20e61 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 ite3LocateTable(
20e62 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20e63 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6e , /* con
20e64 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 74 6f text in which to
20e65 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 20 2a report errors *
20e66 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20 /. int isView,
20e67 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
20e68 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f ue if looking fo
20e69 72 20 61 20 56 49 45 57 20 72 61 74 68 65 72 20 r a VIEW rather
20e6a 74 68 61 6e 20 61 20 54 41 42 4c 45 20 2a 2f 0a than a TABLE */.
20e6b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
20e6c 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ame, /* Name
20e6d 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65 of the table we
20e6e 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 are looking for
20e6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
20e70 20 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a 20 *zDbase /*
20e71 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
20e72 62 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65 20 base. Might be
20e73 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 NULL */.){. Tab
20e74 6c 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65 61 le *p;.. /* Rea
20e75 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 d the database s
20e76 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 chema. If an err
20e77 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 or occurs, leave
20e78 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
20e79 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 e. ** and code
20e7a 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 in pParse and re
20e7b 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 turn NULL. */.
20e7c 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
20e7d 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 qlite3ReadSchema
20e7e 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 (pParse) ){.
20e7f 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
20e80 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 p = sqlite3Find
20e81 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 Table(pParse->db
20e82 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65 29 , zName, zDbase)
20e83 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a ;. if( p==0 ){.
20e84 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
20e85 7a 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f 20 zMsg = isView ?
20e86 22 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20 3a "no such view" :
20e87 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 22 "no such table"
20e88 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73 65 ;. if( zDbase
20e89 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
20e8a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
20e8b 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a , "%s: %s.%s", z
20e8c 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e 61 Msg, zDbase, zNa
20e8d 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a me);. }else{.
20e8e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
20e8f 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 orMsg(pParse, "%
20e90 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e s: %s", zMsg, zN
20e91 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ame);. }.
20e92 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 pParse->checkSch
20e93 65 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 ema = 1;. }. r
20e94 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
20e95 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d * Locate the in-
20e96 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 memory structure
20e97 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
20e98 0a 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72 .** a particular
20e99 20 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65 index given the
20e9a 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e name of that in
20e9b 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e dex.** and the n
20e9c 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
20e9d 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ase that contain
20e9e 73 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 s the index..**
20e9f 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e Return NULL if n
20ea0 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 ot found..**.**
20ea1 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20 If zDatabase is
20ea2 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0, all databases
20ea3 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f are searched fo
20ea4 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61 r the.** table a
20ea5 6e 64 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 nd the first mat
20ea6 63 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72 ching index is r
20ea7 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 eturned. (No ch
20ea8 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 ecking.** for du
20ea9 70 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61 plicate index na
20eaa 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 mes is done.) T
20eab 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20 he search order
20eac 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74 is.** TEMP first
20ead 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 , then MAIN, the
20eae 6e 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20 n any auxiliary
20eaf 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 0a databases added.
20eb0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 ** using the ATT
20eb1 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a ACH command..*/.
20eb2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 SQLITE_PRIVATE I
20eb3 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e ndex *sqlite3Fin
20eb4 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a dIndex(sqlite3 *
20eb5 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a db, const char *
20eb6 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 zName, const cha
20eb7 72 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78 r *zDb){. Index
20eb8 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 *p = 0;. int i
20eb9 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 ;. int nName =
20eba 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 sqlite3Strlen(db
20ebb 2c 20 7a 4e 61 6d 65 29 2b 31 3b 0a 20 20 66 6f , zName)+1;. fo
20ebc 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b r(i=OMIT_TEMPDB;
20ebd 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
20ebe 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 {. int j = (i
20ebf 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 <2) ? i^1 : i;
20ec0 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 /* Search TEMP b
20ec1 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 efore MAIN */.
20ec2 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
20ec3 61 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 a = db->aDb[j].p
20ec4 53 63 68 65 6d 61 3b 0a 20 20 20 20 69 66 28 20 Schema;. if(
20ec5 7a 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 zDb && sqlite3St
20ec6 72 49 43 6d 70 28 7a 44 62 2c 20 64 62 2d 3e 61 rICmp(zDb, db->a
20ec7 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 Db[j].zName) ) c
20ec8 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 73 ontinue;. ass
20ec9 65 72 74 28 20 70 53 63 68 65 6d 61 20 7c 7c 20 ert( pSchema ||
20eca 28 6a 3d 3d 31 20 26 26 20 21 64 62 2d 3e 61 44 (j==1 && !db->aD
20ecb 62 5b 31 5d 2e 70 42 74 29 20 29 3b 0a 20 20 20 b[1].pBt) );.
20ecc 20 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a if( pSchema ){.
20ecd 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 p = sqlite
20ece 33 48 61 73 68 46 69 6e 64 28 26 70 53 63 68 65 3HashFind(&pSche
20ecf 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 ma->idxHash, zNa
20ed0 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 me, nName);.
20ed1 7d 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72 }. if( p ) br
20ed2 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 eak;. }. retur
20ed3 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n p;.}../*.** Re
20ed4 63 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72 79 claim the memory
20ed5 20 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64 65 used by an inde
20ed6 78 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 x.*/.static void
20ed7 20 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65 78 freeIndex(Index
20ed8 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 *p){. sqlite3
20ed9 2a 64 62 20 3d 20 70 2d 3e 70 54 61 62 6c 65 2d *db = p->pTable-
20eda 3e 64 62 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 >dbMem;. sqlite
20edb 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
20edc 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 ColAff);. sqlit
20edd 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b e3DbFree(db, p);
20ede 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 .}../*.** Remove
20edf 20 74 68 65 20 67 69 76 65 6e 20 69 6e 64 65 78 the given index
20ee0 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 from the index
20ee1 68 61 73 68 20 74 61 62 6c 65 2c 20 61 6e 64 20 hash table, and
20ee2 66 72 65 65 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f free.** its memo
20ee3 72 79 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a ry structures..*
20ee4 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 69 *.** The index i
20ee5 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 s removed from t
20ee6 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 68 he database hash
20ee7 20 74 61 62 6c 65 73 20 62 75 74 0a 2a 2a 20 69 tables but.** i
20ee8 74 20 69 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65 t is not unlinke
20ee9 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 d from the Table
20eea 20 74 68 61 74 20 69 74 20 69 6e 64 65 78 65 73 that it indexes
20eeb 2e 0a 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20 66 ..** Unlinking f
20eec 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 6d 75 rom the Table mu
20eed 73 74 20 62 65 20 64 6f 6e 65 20 62 79 20 74 68 st be done by th
20eee 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
20eef 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f on..*/.static vo
20ef0 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 id sqlite3Delete
20ef1 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b Index(Index *p){
20ef2 0a 20 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b 0a . Index *pOld;.
20ef3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
20ef4 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a ame = p->zName;.
20ef5 0a 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 . pOld = sqlite
20ef6 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 3HashInsert(&p->
20ef7 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 pSchema->idxHash
20ef8 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 , zName,.
20ef9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20efa 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 sqlite3Strle
20efb 6e 33 30 28 7a 4e 61 6d 65 29 2b 31 2c 20 30 29 n30(zName)+1, 0)
20efc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 6c 64 ;. assert( pOld
20efd 3d 3d 30 20 7c 7c 20 70 4f 6c 64 3d 3d 70 20 29 ==0 || pOld==p )
20efe 3b 0a 20 20 66 72 65 65 49 6e 64 65 78 28 70 29 ;. freeIndex(p)
20eff 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 ;.}../*.** For t
20f00 68 65 20 69 6e 64 65 78 20 63 61 6c 6c 65 64 20 he index called
20f01 7a 49 64 78 4e 61 6d 65 20 77 68 69 63 68 20 69 zIdxName which i
20f02 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 s found in the d
20f03 61 74 61 62 61 73 65 20 69 44 62 2c 0a 2a 2a 20 atabase iDb,.**
20f04 75 6e 6c 69 6b 65 20 74 68 61 74 20 69 6e 64 65 unlike that inde
20f05 78 20 66 72 6f 6d 20 69 74 73 20 54 61 62 6c 65 x from its Table
20f06 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 then remove the
20f07 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 index from.** t
20f08 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 he index hash ta
20f09 62 6c 65 20 61 6e 64 20 66 72 65 65 20 61 6c 6c ble and free all
20f0a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 memory structur
20f0b 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a es associated.**
20f0c 20 77 69 74 68 20 74 68 65 20 69 6e 64 65 78 2e with the index.
20f0d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20f0e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
20f0f 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e nlinkAndDeleteIn
20f10 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c dex(sqlite3 *db,
20f11 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 int iDb, const
20f12 63 68 61 72 20 2a 7a 49 64 78 4e 61 6d 65 29 7b char *zIdxName){
20f13 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
20f14 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 48 ;. int len;. H
20f15 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64 62 ash *pHash = &db
20f16 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 ->aDb[iDb].pSche
20f17 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 0a 20 20 ma->idxHash;..
20f18 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 len = sqlite3Str
20f19 6c 65 6e 28 64 62 2c 20 7a 49 64 78 4e 61 6d 65 len(db, zIdxName
20f1a 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 );. pIndex = sq
20f1b 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 lite3HashInsert(
20f1c 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 6d 65 2c pHash, zIdxName,
20f1d 20 6c 65 6e 2b 31 2c 20 30 29 3b 0a 20 20 69 66 len+1, 0);. if
20f1e 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 ( pIndex ){.
20f1f 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 if( pIndex->pTab
20f20 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e 64 le->pIndex==pInd
20f21 65 78 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 ex ){. pInd
20f22 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 ex->pTable->pInd
20f23 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 ex = pIndex->pNe
20f24 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 xt;. }else{.
20f25 20 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 Index *p;.
20f26 20 20 20 20 20 66 6f 72 28 70 3d 70 49 6e 64 65 for(p=pInde
20f27 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 x->pTable->pInde
20f28 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 x; p && p->pNext
20f29 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70 2d 3e 70 !=pIndex; p=p->p
20f2a 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20 20 69 66 Next){}. if
20f2b 28 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d ( p && p->pNext=
20f2c 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 =pIndex ){.
20f2d 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49 p->pNext = pI
20f2e 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 ndex->pNext;.
20f2f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 }. }. f
20f30 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 reeIndex(pIndex)
20f31 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 ;. }. db->flag
20f32 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 s |= SQLITE_Inte
20f33 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a rnChanges;.}../*
20f34 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 63 .** Erase all sc
20f35 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e hema information
20f36 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d from the in-mem
20f37 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65 73 20 ory hash tables
20f38 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64 of.** a single d
20f39 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 atabase. This r
20f3a 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
20f3b 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f to reclaim memo
20f3c 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 ry.** before the
20f3d 20 64 61 74 61 62 61 73 65 20 63 6c 6f 73 65 73 database closes
20f3e 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 20 63 61 . It is also ca
20f3f 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 72 6f lled during a ro
20f40 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 74 68 65 llback.** if the
20f41 72 65 20 77 65 72 65 20 73 63 68 65 6d 61 20 63 re were schema c
20f42 68 61 6e 67 65 73 20 64 75 72 69 6e 67 20 74 68 hanges during th
20f43 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 e transaction or
20f44 20 69 66 20 61 0a 2a 2a 20 73 63 68 65 6d 61 2d if a.** schema-
20f45 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74 63 68 20 cookie mismatch
20f46 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 occurs..**.** If
20f47 20 69 44 62 3d 3d 30 20 74 68 65 6e 20 72 65 73 iDb==0 then res
20f48 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 et the internal
20f49 73 63 68 65 6d 61 20 74 61 62 6c 65 73 20 66 6f schema tables fo
20f4a 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a r all database.*
20f4b 2a 20 66 69 6c 65 73 2e 20 20 49 66 20 69 44 62 * files. If iDb
20f4c 3e 3d 31 20 74 68 65 6e 20 72 65 73 65 74 20 74 >=1 then reset t
20f4d 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 he internal sche
20f4e 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74 68 65 0a ma for only the.
20f4f 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 69 ** single file i
20f50 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c ndicated..*/.SQL
20f51 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
20f52 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 sqlite3ResetInt
20f53 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 ernalSchema(sqli
20f54 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 te3 *db, int iDb
20f55 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 ){. int i, j;.
20f56 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
20f57 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
20f58 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20 ;.. if( iDb==0
20f59 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
20f5a 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b reeEnterAll(db);
20f5b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 44 62 . }. for(i=iDb
20f5c 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
20f5d 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d ){. Db *pDb =
20f5e 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 &db->aDb[i];.
20f5f 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 if( pDb->pSche
20f60 6d 61 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ma ){. asse
20f61 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70 44 62 2d rt(i==1 || (pDb-
20f62 3e 70 42 74 20 26 26 20 73 71 6c 69 74 65 33 42 >pBt && sqlite3B
20f63 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
20f64 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20 20 20 20 Db->pBt)));.
20f65 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 sqlite3SchemaF
20f66 72 65 65 28 70 44 62 2d 3e 70 53 63 68 65 6d 61 ree(pDb->pSchema
20f67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
20f68 20 69 44 62 3e 30 20 29 20 72 65 74 75 72 6e 3b iDb>0 ) return;
20f69 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 . }. assert( i
20f6a 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 66 Db==0 );. db->f
20f6b 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f lags &= ~SQLITE_
20f6c 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 InternChanges;.
20f6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
20f6e 76 65 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a veAll(db);.. /*
20f6f 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 If one or more
20f70 6f 66 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 of the auxiliary
20f71 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 database files
20f72 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c has been closed,
20f73 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76 . ** then remov
20f74 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20 e them from the
20f75 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 auxiliary databa
20f76 73 65 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b se list. We tak
20f77 65 20 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72 e the. ** oppor
20f78 74 75 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69 tunity to do thi
20f79 73 20 68 65 72 65 20 73 69 6e 63 65 20 77 65 20 s here since we
20f7a 68 61 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65 have just delete
20f7b 64 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a d all of the. *
20f7c 2a 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61 * schema hash ta
20f7d 62 6c 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f bles and therefo
20f7e 72 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 re do not have t
20f7f 6f 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 o make any chang
20f80 65 73 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f es. ** to any o
20f81 66 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a f those tables..
20f82 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
20f83 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
20f84 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a . struct Db *
20f85 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 pDb = &db->aDb[i
20f86 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e ];. if( pDb->
20f87 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pBt==0 ){.
20f88 69 66 28 20 70 44 62 2d 3e 70 41 75 78 20 26 26 if( pDb->pAux &&
20f89 20 70 44 62 2d 3e 78 46 72 65 65 41 75 78 20 29 pDb->xFreeAux )
20f8a 20 70 44 62 2d 3e 78 46 72 65 65 41 75 78 28 70 pDb->xFreeAux(p
20f8b 44 62 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 20 Db->pAux);.
20f8c 20 70 44 62 2d 3e 70 41 75 78 20 3d 20 30 3b 0a pDb->pAux = 0;.
20f8d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 }. }. for(
20f8e 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e 6e 44 62 i=j=2; i<db->nDb
20f8f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 ; i++){. stru
20f90 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 ct Db *pDb = &db
20f91 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 ->aDb[i];. if
20f92 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b ( pDb->pBt==0 ){
20f93 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
20f94 46 72 65 65 28 64 62 2c 20 70 44 62 2d 3e 7a 4e Free(db, pDb->zN
20f95 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 44 62 2d ame);. pDb-
20f96 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 >zName = 0;.
20f97 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
20f98 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69 20 29 7b }. if( j<i ){
20f99 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 6a . db->aDb[j
20f9a 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a ] = db->aDb[i];.
20f9b 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20 }. j++;.
20f9c 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 64 62 2d }. memset(&db-
20f9d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28 64 62 2d >aDb[j], 0, (db-
20f9e 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f 66 28 64 >nDb-j)*sizeof(d
20f9f 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a 20 20 64 b->aDb[j]));. d
20fa0 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 69 66 b->nDb = j;. if
20fa1 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 26 20 ( db->nDb<=2 &&
20fa2 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 44 62 db->aDb!=db->aDb
20fa3 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 6d 65 Static ){. me
20fa4 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 61 74 mcpy(db->aDbStat
20fa5 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 2a 73 ic, db->aDb, 2*s
20fa6 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d izeof(db->aDb[0]
20fa7 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ));. sqlite3D
20fa8 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 44 bFree(db, db->aD
20fa9 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 20 b);. db->aDb
20faa 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b = db->aDbStatic;
20fab 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
20fac 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
20fad 6c 6c 65 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d lled when a comm
20fae 69 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 it occurs..*/.SQ
20faf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20fb0 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 d sqlite3CommitI
20fb1 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 nternalChanges(s
20fb2 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64 qlite3 *db){. d
20fb3 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c b->flags &= ~SQL
20fb4 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 ITE_InternChange
20fb5 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 s;.}../*.** Clea
20fb6 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d r the column nam
20fb7 65 73 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 es from a table
20fb8 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a 73 74 61 74 or view..*/.stat
20fb9 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 52 65 ic void sqliteRe
20fba 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 54 setColumnNames(T
20fbb 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 able *pTable){.
20fbc 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e int i;. Column
20fbd 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 *pCol;. sqlite
20fbe 33 20 2a 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 3 *db = pTable->
20fbf 64 62 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 dbMem;. assert(
20fc0 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20 pTable!=0 );.
20fc1 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62 if( (pCol = pTab
20fc2 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a le->aCol)!=0 ){.
20fc3 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
20fc4 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b Table->nCol; i++
20fc5 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 , pCol++){.
20fc6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
20fc7 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b b, pCol->zName);
20fc8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
20fc9 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f prDelete(db, pCo
20fca 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 l->pDflt);.
20fcb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
20fcc 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b b, pCol->zType);
20fcd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
20fce 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a Free(db, pCol->z
20fcf 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Coll);. }.
20fd0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
20fd1 62 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 b, pTable->aCol)
20fd2 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e ;. }. pTable->
20fd3 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62 aCol = 0;. pTab
20fd4 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a le->nCol = 0;.}.
20fd5 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 ./*.** Remove th
20fd6 65 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 e memory data st
20fd7 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 ructures associa
20fd8 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 ted with the giv
20fd9 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f en.** Table. No
20fda 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 changes are mad
20fdb 65 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69 e to disk by thi
20fdc 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a s routine..**.**
20fdd 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 This routine ju
20fde 73 74 20 64 65 6c 65 74 65 73 20 74 68 65 20 64 st deletes the d
20fdf 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 20 20 ata structure.
20fe0 49 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69 It does not unli
20fe1 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 nk.** the table
20fe2 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66 data structure f
20fe3 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 rom the hash tab
20fe4 6c 65 2e 20 20 4e 6f 72 20 64 6f 65 73 20 69 74 le. Nor does it
20fe5 20 72 65 6d 6f 76 65 0a 2a 2a 20 66 6f 72 65 69 remove.** forei
20fe6 67 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74 68 65 gn keys from the
20fe7 20 73 71 6c 69 74 65 2e 61 46 4b 65 79 20 68 61 sqlite.aFKey ha
20fe8 73 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69 sh table. But i
20fe9 74 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a t does destroy.*
20fea 2a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 * memory structu
20feb 72 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63 res of the indic
20fec 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b es and foreign k
20fed 65 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 eys associated w
20fee 69 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c ith .** the tabl
20fef 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
20ff0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
20ff1 33 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 3DeleteTable(Tab
20ff2 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49 le *pTable){. I
20ff3 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70 ndex *pIndex, *p
20ff4 4e 65 78 74 3b 0a 20 20 46 4b 65 79 20 2a 70 46 Next;. FKey *pF
20ff5 4b 65 79 2c 20 2a 70 4e 65 78 74 46 4b 65 79 3b Key, *pNextFKey;
20ff6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
20ff7 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 . if( pTable==0
20ff8 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 ) return;. db
20ff9 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b = pTable->dbMem;
20ffa 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 65 .. /* Do not de
20ffb 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65 20 75 lete the table u
20ffc 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72 65 6e ntil the referen
20ffd 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 ce count reaches
20ffe 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54 61 62 zero. */. pTab
20fff 6c 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 le->nRef--;. if
21000 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e 30 ( pTable->nRef>0
21001 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
21002 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 }. assert( pT
21003 61 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b able->nRef==0 );
21004 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c .. /* Delete al
21005 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 l indices associ
21006 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74 ated with this t
21007 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 able. */. for(
21008 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c 65 2d pIndex = pTable-
21009 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b >pIndex; pIndex;
2100a 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0a pIndex=pNext){.
2100b 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49 6e 64 pNext = pInd
2100c 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61 ex->pNext;. a
2100d 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 70 ssert( pIndex->p
2100e 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65 2d 3e Schema==pTable->
2100f 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 pSchema );. s
21010 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 qlite3DeleteInde
21011 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a x(pIndex);. }..
21012 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
21013 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a MIT_FOREIGN_KEY.
21014 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 /* Delete all
21015 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 73 foreign keys ass
21016 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 ociated with thi
21017 73 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6b 65 s table. The ke
21018 79 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 ys. ** should h
21019 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e ave already been
2101a 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74 unlinked from t
2101b 68 65 20 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 he pSchema->aFKe
2101c 79 20 68 61 73 68 20 74 61 62 6c 65 20 0a 20 20 y hash table .
2101d 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 */. for(pFKey=p
2101e 54 61 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 46 Table->pFKey; pF
2101f 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74 Key; pFKey=pNext
21020 46 4b 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 74 FKey){. pNext
21021 46 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 4e FKey = pFKey->pN
21022 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 61 73 73 extFrom;. ass
21023 65 72 74 28 20 73 71 6c 69 74 65 33 48 61 73 68 ert( sqlite3Hash
21024 46 69 6e 64 28 26 70 54 61 62 6c 65 2d 3e 70 53 Find(&pTable->pS
21025 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 0a 20 20 chema->aFKey,.
21026 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21027 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e pFKey->
21028 7a 54 6f 2c 20 73 71 6c 69 74 65 33 53 74 72 6c zTo, sqlite3Strl
21029 65 6e 33 30 28 70 46 4b 65 79 2d 3e 7a 54 6f 29 en30(pFKey->zTo)
2102a 2b 31 29 21 3d 70 46 4b 65 79 20 29 3b 0a 20 20 +1)!=pFKey );.
2102b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
2102c 64 62 2c 20 70 46 4b 65 79 29 3b 0a 20 20 7d 0a db, pFKey);. }.
2102d 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 65 6c #endif.. /* Del
2102e 65 74 65 20 74 68 65 20 54 61 62 6c 65 20 73 74 ete the Table st
2102f 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a ructure itself..
21030 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65 73 */. sqliteRes
21031 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 etColumnNames(pT
21032 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 able);. sqlite3
21033 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c DbFree(db, pTabl
21034 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c e->zName);. sql
21035 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
21036 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 3b Table->zColAff);
21037 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 . sqlite3Select
21038 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c Delete(db, pTabl
21039 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 23 69 66 e->pSelect);.#if
2103a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
2103b 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69 74 65 33 _CHECK. sqlite3
2103c 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
2103d 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b 0a Table->pCheck);.
2103e 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 #endif. sqlite3
2103f 56 74 61 62 43 6c 65 61 72 28 70 54 61 62 6c 65 VtabClear(pTable
21040 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
21041 65 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a ee(db, pTable);.
21042 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 }../*.** Unlink
21043 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 the given table
21044 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 from the hash ta
21045 62 6c 65 73 20 61 6e 64 20 74 68 65 20 64 65 6c bles and the del
21046 65 74 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 ete the.** table
21047 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 20 structure with
21048 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 20 all its indices
21049 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 and foreign keys
2104a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
2104b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
2104c 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 UnlinkAndDeleteT
2104d 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 able(sqlite3 *db
2104e 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 , int iDb, const
2104f 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 29 char *zTabName)
21050 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 {. Table *p;.
21051 46 4b 65 79 20 2a 70 46 31 2c 20 2a 70 46 32 3b FKey *pF1, *pF2;
21052 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a 20 20 61 . Db *pDb;.. a
21053 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
21054 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 assert( iDb>=0
21055 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 && iDb<db->nDb
21056 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 54 61 );. assert( zTa
21057 62 4e 61 6d 65 20 26 26 20 7a 54 61 62 4e 61 6d bName && zTabNam
21058 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62 20 3d 20 e[0] );. pDb =
21059 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 &db->aDb[iDb];.
2105a 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 p = sqlite3Hash
2105b 49 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 63 Insert(&pDb->pSc
2105c 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a hema->tblHash, z
2105d 54 61 62 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 TabName,.
2105e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2105f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
21060 28 7a 54 61 62 4e 61 6d 65 29 2b 31 2c 30 29 3b (zTabName)+1,0);
21061 0a 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 6e . if( p ){.#ifn
21062 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
21063 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 FOREIGN_KEY.
21064 66 6f 72 28 70 46 31 3d 70 2d 3e 70 46 4b 65 79 for(pF1=p->pFKey
21065 3b 20 70 46 31 3b 20 70 46 31 3d 70 46 31 2d 3e ; pF1; pF1=pF1->
21066 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 pNextFrom){.
21067 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 71 6c 69 int nTo = sqli
21068 74 65 33 53 74 72 6c 65 6e 33 30 28 70 46 31 2d te3Strlen30(pF1-
21069 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20 >zTo) + 1;.
2106a 20 70 46 32 20 3d 20 73 71 6c 69 74 65 33 48 61 pF2 = sqlite3Ha
2106b 73 68 46 69 6e 64 28 26 70 44 62 2d 3e 70 53 63 shFind(&pDb->pSc
2106c 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 31 hema->aFKey, pF1
2106d 2d 3e 7a 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20 ->zTo, nTo);.
2106e 20 20 20 69 66 28 20 70 46 32 3d 3d 70 46 31 20 if( pF2==pF1
2106f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
21070 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 44 e3HashInsert(&pD
21071 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 b->pSchema->aFKe
21072 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20 6e 54 6f y, pF1->zTo, nTo
21073 2c 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f 29 3b , pF1->pNextTo);
21074 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
21075 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46 32 while( pF2
21076 20 26 26 20 70 46 32 2d 3e 70 4e 65 78 74 54 6f && pF2->pNextTo
21077 21 3d 70 46 31 20 29 7b 20 70 46 32 3d 70 46 32 !=pF1 ){ pF2=pF2
21078 2d 3e 70 4e 65 78 74 54 6f 3b 20 7d 0a 20 20 20 ->pNextTo; }.
21079 20 20 20 20 20 69 66 28 20 70 46 32 20 29 7b 0a if( pF2 ){.
2107a 20 20 20 20 20 20 20 20 20 20 70 46 32 2d 3e 70 pF2->p
2107b 4e 65 78 74 54 6f 20 3d 20 70 46 31 2d 3e 70 4e NextTo = pF1->pN
2107c 65 78 74 54 6f 3b 0a 20 20 20 20 20 20 20 20 7d extTo;. }
2107d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
2107e 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 endif. sqlite
2107f 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 29 3b 3DeleteTable(p);
21080 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 . }. db->flags
21081 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 |= SQLITE_Inter
21082 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a nChanges;.}../*.
21083 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e ** Given a token
21084 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72 69 6e , return a strin
21085 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20 g that consists
21086 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 of the text of t
21087 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 20 77 69 74 hat.** token wit
21088 68 20 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e 73 h any quotations
21089 20 72 65 6d 6f 76 65 64 2e 20 20 53 70 61 63 65 removed. Space
2108a 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 to hold the ret
2108b 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 urned string.**
2108c 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d is obtained from
2108d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 sqliteMalloc()
2108e 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 and must be free
2108f 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 d by the calling
21090 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a .** function..**
21091 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f .** Tokens are o
21092 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65 ften just pointe
21093 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 rs into the orig
21094 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61 6e inal SQL text an
21095 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 d so.** are not
21096 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20 \000 terminated
21097 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72 73 and are not pers
21098 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65 74 istent. The ret
21099 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 urned string.**
2109a 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 is \000 terminat
2109b 65 64 20 61 6e 64 20 69 73 20 70 65 72 73 69 73 ed and is persis
2109c 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tent..*/.SQLITE_
2109d 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
2109e 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
2109f 65 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 en(sqlite3 *db,
210a0 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 Token *pName){.
210a1 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 char *zName;.
210a2 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 if( pName ){.
210a3 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 zName = sqlite3
210a4 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 DbStrNDup(db, (c
210a5 68 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 har*)pName->z, p
210a6 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 Name->n);. sq
210a7 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 lite3Dequote(zNa
210a8 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 me);. }else{.
210a9 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d zName = 0;. }
210aa 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b . return zName;
210ab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 .}../*.** Open t
210ac 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 he sqlite_master
210ad 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69 6e table stored in
210ae 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 database number
210af 20 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74 iDb for.** writ
210b0 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20 69 ing. The table i
210b1 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 63 s opened using c
210b2 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 ursor 0..*/.SQLI
210b3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
210b4 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 sqlite3OpenMaste
210b5 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 2c rTable(Parse *p,
210b6 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62 int iDb){. Vdb
210b7 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 e *v = sqlite3Ge
210b8 74 56 64 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 tVdbe(p);. sqli
210b9 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 te3TableLock(p,
210ba 69 44 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 iDb, MASTER_ROOT
210bb 2c 20 31 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c , 1, SCHEMA_TABL
210bc 45 28 69 44 62 29 29 3b 0a 20 20 73 71 6c 69 74 E(iDb));. sqlit
210bd 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
210be 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c OP_OpenWrite, 0,
210bf 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44 MASTER_ROOT, iD
210c0 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 b);. sqlite3Vdb
210c1 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c eChangeP4(v, -1,
210c2 20 28 63 68 61 72 20 2a 29 35 2c 20 50 34 5f 49 (char *)5, P4_I
210c3 4e 54 33 32 29 3b 20 20 2f 2a 20 35 20 63 6f 6c NT32); /* 5 col
210c4 75 6d 6e 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 umn table */. i
210c5 66 28 20 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b f( p->nTab==0 ){
210c6 0a 20 20 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31 . p->nTab = 1
210c7 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ;. }.}../*.** P
210c8 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 arameter zName p
210c9 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74 oints to a nul-t
210ca 65 72 6d 69 6e 61 74 65 64 20 62 75 66 66 65 72 erminated buffer
210cb 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
210cc 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74 name.** of a dat
210cd 61 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 abase ("main", "
210ce 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d temp" or the nam
210cf 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 e of an attached
210d0 20 64 62 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75 db). This.** fu
210d1 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
210d2 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
210d3 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 69 named database i
210d4 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a n db->aDb[], or.
210d5 2a 2a 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d ** -1 if the nam
210d6 65 64 20 64 62 20 63 61 6e 6e 6f 74 20 62 65 20 ed db cannot be
210d7 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 found..*/.SQLITE
210d8 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
210d9 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 ite3FindDbName(s
210da 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
210db 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a t char *zName){.
210dc 20 20 69 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20 int i = -1;
210dd 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
210de 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 66 e number */. if
210df 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 44 ( zName ){. D
210e0 62 20 2a 70 44 62 3b 0a 20 20 20 20 69 6e 74 20 b *pDb;. int
210e1 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
210e2 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 n30(zName);.
210e3 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 for(i=(db->nDb-1
210e4 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b ), pDb=&db->aDb[
210e5 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 i]; i>=0; i--, p
210e6 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 28 Db--){. if(
210e7 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c (!OMIT_TEMPDB |
210e8 7c 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73 | i!=1 ) && n==s
210e9 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 qlite3Strlen30(p
210ea 44 62 2d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20 Db->zName) && .
210eb 20 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c 69 0==sqli
210ec 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e te3StrICmp(pDb->
210ed 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b zName, zName) ){
210ee 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
210ef 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
210f0 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a }. return i;.}.
210f1 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e ./*.** The token
210f2 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73 *pName contains
210f3 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 the name of a d
210f4 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20 atabase (either
210f5 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65 "main" or.** "te
210f6 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 mp" or the name
210f7 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 of an attached d
210f8 62 29 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 b). This routine
210f9 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 returns the.**
210fa 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d index of the nam
210fb 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 ed database in d
210fc 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20 b->aDb[], or -1
210fd 69 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20 if the named db
210fe 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 .** does not exi
210ff 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 st..*/.SQLITE_PR
21100 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
21101 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 20 3FindDb(sqlite3
21102 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d *db, Token *pNam
21103 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 e){. int i;
21104 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21105 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
21106 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f tabase number */
21107 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
21108 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21109 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
2110a 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 we are searching
2110b 20 66 6f 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 for */. zName
2110c 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
2110d 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 mToken(db, pName
2110e 29 3b 0a 20 20 69 20 3d 20 73 71 6c 69 74 65 33 );. i = sqlite3
2110f 46 69 6e 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a FindDbName(db, z
21110 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 Name);. sqlite3
21111 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 DbFree(db, zName
21112 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d );. return i;.}
21113 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f ../* The table o
21114 72 20 76 69 65 77 20 6f 72 20 74 72 69 67 67 65 r view or trigge
21115 72 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65 64 r name is passed
21116 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
21117 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70 via tokens.** p
21118 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 Name1 and pName2
21119 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e . If the table n
2111a 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75 ame was fully qu
2111b 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61 alified, for exa
2111c 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 mple:.**.** CREA
2111d 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79 79 TE TABLE xxx.yyy
2111e 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 (...);.** .** T
2111f 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 hen pName1 is se
21120 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20 70 t to "xxx" and p
21121 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 Name2 "yyy". On
21122 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20 69 the other hand i
21123 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e f.** the table n
21124 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 ame is not fully
21125 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e qualified, i.e.
21126 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 :.**.** CREATE T
21127 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a ABLE yyy(...);.*
21128 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 *.** Then pName1
21129 20 69 73 20 73 65 74 20 74 6f 20 22 79 79 79 22 is set to "yyy"
2112a 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22 and pName2 is "
2112b 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f "..**.** This ro
2112c 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 2a utine sets the *
2112d 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72 ppUnqual pointer
2112e 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 to point at the
2112f 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f token (pName1 o
21130 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 r.** pName2) tha
21131 74 20 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71 t stores the unq
21132 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e ualified table n
21133 61 6d 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20 ame. The index
21134 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 of the.** databa
21135 73 65 20 22 78 78 78 22 20 69 73 20 72 65 74 75 se "xxx" is retu
21136 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
21137 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
21138 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a te3TwoPartName(.
21139 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
2113a 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 /* Parsing
2113b 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 and code genera
2113c 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a ting context */.
2113d 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c Token *pName1,
2113e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 78 78 /* The "xx
2113f 78 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 x" in the name "
21140 78 78 78 2e 79 79 79 22 20 6f 72 20 22 78 78 78 xxx.yyy" or "xxx
21141 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e " */. Token *pN
21142 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 ame2, /* Th
21143 65 20 22 79 79 79 22 20 69 6e 20 74 68 65 20 6e e "yyy" in the n
21144 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 2a 2f ame "xxx.yyy" */
21145 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 . Token **pUnqu
21146 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 al /* Write
21147 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 the unqualified
21148 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68 65 72 65 object name here
21149 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 */.){. int iDb
2114a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
2114b 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
2114c 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a holding the obj
2114d 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ect */. sqlite3
2114e 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
2114f 62 3b 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 32 b;.. if( pName2
21150 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 && pName2->n>0
21151 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 69 ){. if( db->i
21152 6e 69 74 2e 62 75 73 79 20 29 20 7b 0a 20 20 20 nit.busy ) {.
21153 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
21154 73 67 28 70 50 61 72 73 65 2c 20 22 63 6f 72 72 sg(pParse, "corr
21155 75 70 74 20 64 61 74 61 62 61 73 65 22 29 3b 0a upt database");.
21156 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 pParse->nE
21157 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 rr++;. retu
21158 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 rn -1;. }.
21159 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d *pUnqual = pNam
2115a 65 32 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71 e2;. iDb = sq
2115b 6c 69 74 65 33 46 69 6e 64 44 62 28 64 62 2c 20 lite3FindDb(db,
2115c 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 pName1);. if(
2115d 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 iDb<0 ){.
2115e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
2115f 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e pParse, "unknown
21160 20 64 61 74 61 62 61 73 65 20 25 54 22 2c 20 70 database %T", p
21161 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 70 50 Name1);. pP
21162 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 arse->nErr++;.
21163 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 return -1;.
21164 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
21165 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e assert( db->in
21166 69 74 2e 69 44 62 3d 3d 30 20 7c 7c 20 64 62 2d it.iDb==0 || db-
21167 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 >init.busy );.
21168 20 20 69 44 62 20 3d 20 64 62 2d 3e 69 6e 69 74 iDb = db->init
21169 2e 69 44 62 3b 0a 20 20 20 20 2a 70 55 6e 71 75 .iDb;. *pUnqu
2116a 61 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d al = pName1;. }
2116b 0a 20 20 72 65 74 75 72 6e 20 69 44 62 3b 0a 7d . return iDb;.}
2116c 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
2116d 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 tine is used to
2116e 63 68 65 63 6b 20 69 66 20 74 68 65 20 55 54 46 check if the UTF
2116f 2d 38 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 20 -8 string zName
21170 69 73 20 61 20 6c 65 67 61 6c 0a 2a 2a 20 75 6e is a legal.** un
21171 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 66 qualified name f
21172 6f 72 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20 or a new schema
21173 6f 62 6a 65 63 74 20 28 74 61 62 6c 65 2c 20 69 object (table, i
21174 6e 64 65 78 2c 20 76 69 65 77 20 6f 72 0a 2a 2a ndex, view or.**
21175 20 74 72 69 67 67 65 72 29 2e 20 41 6c 6c 20 6e trigger). All n
21176 61 6d 65 73 20 61 72 65 20 6c 65 67 61 6c 20 65 ames are legal e
21177 78 63 65 70 74 20 74 68 6f 73 65 20 74 68 61 74 xcept those that
21178 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 begin with the
21179 73 74 72 69 6e 67 0a 2a 2a 20 22 73 71 6c 69 74 string.** "sqlit
2117a 65 5f 22 20 28 69 6e 20 75 70 70 65 72 2c 20 6c e_" (in upper, l
2117b 6f 77 65 72 20 6f 72 20 6d 69 78 65 64 20 63 61 ower or mixed ca
2117c 73 65 29 2e 20 54 68 69 73 20 70 6f 72 74 69 6f se). This portio
2117d 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65 73 70 61 n of the namespa
2117e 63 65 0a 2a 2a 20 69 73 20 72 65 73 65 72 76 65 ce.** is reserve
2117f 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 d for internal u
21180 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
21181 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
21182 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 3CheckObjectName
21183 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
21184 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
21185 65 29 7b 0a 20 20 69 66 28 20 21 70 50 61 72 73 e){. if( !pPars
21186 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 e->db->init.busy
21187 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 && pParse->nest
21188 65 64 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20 ed==0 .
21189 20 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d && (pParse->db-
2118a 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
2118b 57 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a WriteSchema)==0.
2118c 20 20 20 20 20 20 20 20 20 20 26 26 20 30 3d 3d && 0==
2118d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 sqlite3StrNICmp(
2118e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 zName, "sqlite_"
2118f 2c 20 37 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 , 7) ){. sqli
21190 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
21191 73 65 2c 20 22 6f 62 6a 65 63 74 20 6e 61 6d 65 se, "object name
21192 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e reserved for in
21193 74 65 72 6e 61 6c 20 75 73 65 3a 20 25 73 22 2c ternal use: %s",
21194 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 zName);. ret
21195 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
21196 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
21197 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
21198 2a 2a 20 42 65 67 69 6e 20 63 6f 6e 73 74 72 75 ** Begin constru
21199 63 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c cting a new tabl
2119a 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e e representation
2119b 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 in memory. Thi
2119c 73 20 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 s is.** the firs
2119d 74 20 6f 66 20 73 65 76 65 72 61 6c 20 61 63 74 t of several act
2119e 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 68 61 ion routines tha
2119f 74 20 67 65 74 20 63 61 6c 6c 65 64 20 69 6e 20 t get called in
211a0 72 65 73 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20 61 response.** to a
211a1 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
211a2 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 70 61 72 atement. In par
211a3 74 69 63 75 6c 61 72 2c 20 74 68 69 73 20 72 6f ticular, this ro
211a4 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 0a utine is called.
211a5 2a 2a 20 61 66 74 65 72 20 73 65 65 69 6e 67 20 ** after seeing
211a6 74 6f 6b 65 6e 73 20 22 43 52 45 41 54 45 22 20 tokens "CREATE"
211a7 61 6e 64 20 22 54 41 42 4c 45 22 20 61 6e 64 20 and "TABLE" and
211a8 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 the table name.
211a9 54 68 65 20 69 73 54 65 6d 70 0a 2a 2a 20 66 6c The isTemp.** fl
211aa 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74 68 ag is true if th
211ab 65 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 e table should b
211ac 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 e stored in the
211ad 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 auxiliary databa
211ae 73 65 0a 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 se.** file inste
211af 61 64 20 6f 66 20 69 6e 20 74 68 65 20 6d 61 69 ad of in the mai
211b0 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
211b1 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c This is normal
211b2 6c 79 20 74 68 65 20 63 61 73 65 0a 2a 2a 20 77 ly the case.** w
211b3 68 65 6e 20 74 68 65 20 22 54 45 4d 50 22 20 6f hen the "TEMP" o
211b4 72 20 22 54 45 4d 50 4f 52 41 52 59 22 20 6b 65 r "TEMPORARY" ke
211b5 79 77 6f 72 64 20 6f 63 63 75 72 73 20 69 6e 20 yword occurs in
211b6 62 65 74 77 65 65 6e 0a 2a 2a 20 43 52 45 41 54 between.** CREAT
211b7 45 20 61 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a 0a E and TABLE..**.
211b8 2a 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65 ** The new table
211b9 20 72 65 63 6f 72 64 20 69 73 20 69 6e 69 74 69 record is initi
211ba 61 6c 69 7a 65 64 20 61 6e 64 20 70 75 74 20 69 alized and put i
211bb 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 n pParse->pNewTa
211bc 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65 20 ble..** As more
211bd 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 of the CREATE TA
211be 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 BLE statement is
211bf 20 70 61 72 73 65 64 2c 20 61 64 64 69 74 69 6f parsed, additio
211c0 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f nal action.** ro
211c1 75 74 69 6e 65 73 20 77 69 6c 6c 20 62 65 20 63 utines will be c
211c2 61 6c 6c 65 64 20 74 6f 20 61 64 64 20 6d 6f 72 alled to add mor
211c3 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f e information to
211c4 20 74 68 69 73 20 72 65 63 6f 72 64 2e 0a 2a 2a this record..**
211c5 20 41 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 At the end of t
211c6 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 he CREATE TABLE
211c7 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 73 statement, the s
211c8 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 29 qlite3EndTable()
211c9 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 routine.** is c
211ca 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 alled to complet
211cb 65 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74 69 e the constructi
211cc 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 on of the new ta
211cd 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 ble record..*/.S
211ce 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
211cf 69 64 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 id sqlite3StartT
211d0 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 able(. Parse *p
211d1 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 Parse, /* Pars
211d2 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 er context */.
211d3 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 Token *pName1,
211d4 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f /* First part o
211d5 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 f the name of th
211d6 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 e table or view
211d7 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d */. Token *pNam
211d8 65 32 2c 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 e2, /* Second
211d9 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 part of the name
211da 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 of the table or
211db 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 view */. int i
211dc 73 54 65 6d 70 2c 20 20 20 20 20 20 2f 2a 20 54 sTemp, /* T
211dd 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 rue if this is a
211de 20 54 45 4d 50 20 74 61 62 6c 65 20 2a 2f 0a 20 TEMP table */.
211df 20 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20 int isView,
211e0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
211e1 73 20 69 73 20 61 20 56 49 45 57 20 2a 2f 0a 20 s is a VIEW */.
211e2 20 69 6e 74 20 69 73 56 69 72 74 75 61 6c 2c 20 int isVirtual,
211e3 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
211e4 73 20 69 73 20 61 20 56 49 52 54 55 41 4c 20 74 s is a VIRTUAL t
211e5 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f able */. int no
211e6 45 72 72 20 20 20 20 20 20 20 20 2f 2a 20 44 6f Err /* Do
211e7 20 6e 6f 74 68 69 6e 67 20 69 66 20 74 61 62 6c nothing if tabl
211e8 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 e already exists
211e9 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a */.){. Table *
211ea 70 54 61 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a pTable;. char *
211eb 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f 2a 20 54 68 zName = 0; /* Th
211ec 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 e name of the ne
211ed 77 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c w table */. sql
211ee 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
211ef 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 e->db;. Vdbe *v
211f0 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 ;. int iDb;
211f1 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
211f2 20 6e 75 6d 62 65 72 20 74 6f 20 63 72 65 61 74 number to creat
211f3 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 2a e the table in *
211f4 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
211f5 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 ; /* Unqualif
211f6 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 ied name of the
211f7 74 61 62 6c 65 20 74 6f 20 63 72 65 61 74 65 20 table to create
211f8 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 74 61 62 */.. /* The tab
211f9 6c 65 20 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 le or view name
211fa 74 6f 20 63 72 65 61 74 65 20 69 73 20 70 61 73 to create is pas
211fb 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74 sed to this rout
211fc 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 20 ine via tokens.
211fd 20 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 ** pName1 and p
211fe 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74 61 Name2. If the ta
211ff 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75 6c ble name was ful
21200 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66 6f ly qualified, fo
21201 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a r example:. **.
21202 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c ** CREATE TABL
21203 45 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b E xxx.yyy (...);
21204 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 54 68 65 6e . ** . ** Then
21205 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 pName1 is set t
21206 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d o "xxx" and pNam
21207 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 e2 "yyy". On the
21208 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 20 other hand if.
21209 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 ** the table na
2120a 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 me is not fully
2120b 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a qualified, i.e.:
2120c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 . **. ** CREAT
2120d 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 E TABLE yyy(...)
2120e 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e ;. **. ** Then
2120f 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 pName1 is set t
21210 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d o "yyy" and pNam
21211 65 32 20 69 73 20 22 22 2e 0a 20 20 2a 2a 0a 20 e2 is "".. **.
21212 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 62 65 6c ** The call bel
21213 6f 77 20 73 65 74 73 20 74 68 65 20 70 4e 61 6d ow sets the pNam
21214 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 e pointer to poi
21215 6e 74 20 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 nt at the token
21216 28 70 4e 61 6d 65 31 20 6f 72 0a 20 20 2a 2a 20 (pName1 or. **
21217 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73 74 6f pName2) that sto
21218 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c 69 66 res the unqualif
21219 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 ied table name.
2121a 54 68 65 20 76 61 72 69 61 62 6c 65 20 69 44 62 The variable iDb
2121b 20 69 73 0a 20 20 2a 2a 20 73 65 74 20 74 6f 20 is. ** set to
2121c 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
2121d 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 74 database that t
2121e 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 he table or view
2121f 20 69 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 is to be. ** c
21220 72 65 61 74 65 64 20 69 6e 2e 0a 20 20 2a 2f 0a reated in.. */.
21221 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 iDb = sqlite3T
21222 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 woPartName(pPars
21223 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 e, pName1, pName
21224 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 66 2, &pName);. if
21225 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e ( iDb<0 ) return
21226 3b 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 ;. if( !OMIT_TE
21227 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 26 MPDB && isTemp &
21228 26 20 69 44 62 3e 31 20 29 7b 0a 20 20 20 20 2f & iDb>1 ){. /
21229 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20 * If creating a
2122a 74 65 6d 70 20 74 61 62 6c 65 2c 20 74 68 65 20 temp table, the
2122b 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 name may not be
2122c 71 75 61 6c 69 66 69 65 64 20 2a 2f 0a 20 20 20 qualified */.
2122d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
2122e 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 (pParse, "tempor
2122f 61 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d ary table name m
21230 75 73 74 20 62 65 20 75 6e 71 75 61 6c 69 66 69 ust be unqualifi
21231 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ed");. return
21232 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 4f 4d 49 ;. }. if( !OMI
21233 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 T_TEMPDB && isTe
21234 6d 70 20 29 20 69 44 62 20 3d 20 31 3b 0a 0a 20 mp ) iDb = 1;..
21235 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f pParse->sNameTo
21236 6b 65 6e 20 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20 ken = *pName;.
21237 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e zName = sqlite3N
21238 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
21239 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a pName);. if( z
2123a 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e Name==0 ) return
2123b 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f ;. if( SQLITE_O
2123c 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f K!=sqlite3CheckO
2123d 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 bjectName(pParse
2123e 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 , zName) ){.
2123f 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 goto begin_table
21240 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 _error;. }. if
21241 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d ( db->init.iDb==
21242 31 20 29 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 1 ) isTemp = 1;.
21243 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
21244 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
21245 4e 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 54 N. assert( (isT
21246 65 6d 70 20 26 20 31 29 3d 3d 69 73 54 65 6d 70 emp & 1)==isTemp
21247 20 29 3b 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 );. {. int
21248 63 6f 64 65 3b 0a 20 20 20 20 63 68 61 72 20 2a code;. char *
21249 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 zDb = db->aDb[iD
2124a 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 b].zName;. if
2124b 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 ( sqlite3AuthChe
2124c 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ck(pParse, SQLIT
2124d 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 E_INSERT, SCHEMA
2124e 5f 54 41 42 4c 45 28 69 73 54 65 6d 70 29 2c 20 _TABLE(isTemp),
2124f 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 0, zDb) ){.
21250 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c goto begin_tabl
21251 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 e_error;. }.
21252 20 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b if( isView ){
21253 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 . if( !OMIT
21254 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d _TEMPDB && isTem
21255 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 p ){. cod
21256 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 e = SQLITE_CREAT
21257 45 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 E_TEMP_VIEW;.
21258 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
21259 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f code = SQLITE_
2125a 43 52 45 41 54 45 5f 56 49 45 57 3b 0a 20 20 20 CREATE_VIEW;.
2125b 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
2125c 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f if( !OMIT_
2125d 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 TEMPDB && isTemp
2125e 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 ){. code
2125f 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 = SQLITE_CREATE
21260 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 _TEMP_TABLE;.
21261 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
21262 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f code = SQLITE_
21263 43 52 45 41 54 45 5f 54 41 42 4c 45 3b 0a 20 20 CREATE_TABLE;.
21264 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
21265 69 66 28 20 21 69 73 56 69 72 74 75 61 6c 20 26 if( !isVirtual &
21266 26 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 & sqlite3AuthChe
21267 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c ck(pParse, code,
21268 20 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 zName, 0, zDb)
21269 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 ){. goto be
2126a 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b gin_table_error;
2126b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
2126c 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 f.. /* Make sur
2126d 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 e the new table
2126e 6e 61 6d 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f name does not co
2126f 6c 6c 69 64 65 20 77 69 74 68 20 61 6e 20 65 78 llide with an ex
21270 69 73 74 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 isting. ** inde
21271 78 20 6f 72 20 74 61 62 6c 65 20 6e 61 6d 65 20 x or table name
21272 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 in the same data
21273 62 61 73 65 2e 20 20 49 73 73 75 65 20 61 6e 20 base. Issue an
21274 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66 error message if
21275 0a 20 20 2a 2a 20 69 74 20 64 6f 65 73 2e 20 54 . ** it does. T
21276 68 65 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 he exception is
21277 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 if the statement
21278 20 62 65 69 6e 67 20 70 61 72 73 65 64 20 77 61 being parsed wa
21279 73 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 74 6f s passed. ** to
2127a 20 61 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c an sqlite3_decl
2127b 61 72 65 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e are_vtab() call.
2127c 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 6f 6e In that case on
2127d 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 ly the column na
2127e 6d 65 73 0a 20 20 2a 2a 20 61 6e 64 20 74 79 70 mes. ** and typ
2127f 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2c es will be used,
21280 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 so there is no
21281 6e 65 65 64 20 74 6f 20 74 65 73 74 20 66 6f 72 need to test for
21282 20 6e 61 6d 65 73 70 61 63 65 0a 20 20 2a 2a 20 namespace. **
21283 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 20 20 2a 2f collisions.. */
21284 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c 41 . if( !IN_DECLA
21285 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 69 RE_VTAB ){. i
21286 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 f( SQLITE_OK!=sq
21287 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 lite3ReadSchema(
21288 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 pParse) ){.
21289 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c goto begin_tabl
2128a 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 e_error;. }.
2128b 20 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 pTable = sqli
2128c 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c te3FindTable(db,
2128d 20 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b zName, db->aDb[
2128e 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 iDb].zName);.
2128f 20 69 66 28 20 70 54 61 62 6c 65 20 29 7b 0a 20 if( pTable ){.
21290 20 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20 if( !noErr
21291 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
21292 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
21293 65 2c 20 22 74 61 62 6c 65 20 25 54 20 61 6c 72 e, "table %T alr
21294 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 70 4e eady exists", pN
21295 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ame);. }.
21296 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 goto begin_t
21297 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 able_error;.
21298 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
21299 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 3FindIndex(db, z
2129a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 26 26 20 28 Name, 0)!=0 && (
2129b 69 44 62 3d 3d 30 20 7c 7c 20 21 64 62 2d 3e 69 iDb==0 || !db->i
2129c 6e 69 74 2e 62 75 73 79 29 20 29 7b 0a 20 20 20 nit.busy) ){.
2129d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
2129e 73 67 28 70 50 61 72 73 65 2c 20 22 74 68 65 72 sg(pParse, "ther
2129f 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e 20 e is already an
212a0 69 6e 64 65 78 20 6e 61 6d 65 64 20 25 73 22 2c index named %s",
212a1 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 zName);. g
212a2 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f oto begin_table_
212a3 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d error;. }. }
212a4 0a 0a 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c .. pTable = sql
212a5 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
212a6 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c (db, sizeof(Tabl
212a7 65 29 29 3b 0a 20 20 69 66 28 20 70 54 61 62 6c e));. if( pTabl
212a8 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e e==0 ){. db->
212a9 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
212aa 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 ;. pParse->rc
212ab 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
212ac 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 . pParse->nEr
212ad 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 r++;. goto be
212ae 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b gin_table_error;
212af 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a . }. pTable->z
212b0 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 Name = zName;.
212b1 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 pTable->iPKey =
212b2 2d 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 -1;. pTable->pS
212b3 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b chema = db->aDb[
212b4 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 iDb].pSchema;.
212b5 70 54 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 pTable->nRef = 1
212b6 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 ;. pTable->dbMe
212b7 6d 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 m = db->lookasid
212b8 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 64 62 20 e.bEnabled ? db
212b9 3a 20 30 3b 0a 20 20 69 66 28 20 70 50 61 72 73 : 0;. if( pPars
212ba 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 29 20 73 e->pNewTable ) s
212bb 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c qlite3DeleteTabl
212bc 65 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 e(pParse->pNewTa
212bd 62 6c 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e ble);. pParse->
212be 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 54 61 62 pNewTable = pTab
212bf 6c 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 le;.. /* If thi
212c0 73 20 69 73 20 74 68 65 20 6d 61 67 69 63 20 73 s is the magic s
212c1 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 qlite_sequence t
212c2 61 62 6c 65 20 75 73 65 64 20 62 79 20 61 75 74 able used by aut
212c3 6f 69 6e 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a oincrement,. **
212c4 20 74 68 65 6e 20 72 65 63 6f 72 64 20 61 20 70 then record a p
212c5 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 74 ointer to this t
212c6 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e able in the main
212c7 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 database struct
212c8 75 72 65 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 ure. ** so that
212c9 20 49 4e 53 45 52 54 20 63 61 6e 20 66 69 6e 64 INSERT can find
212ca 20 74 68 65 20 74 61 62 6c 65 20 65 61 73 69 6c the table easil
212cb 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 y.. */.#ifndef
212cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
212cd 49 4e 43 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 INCREMENT. if(
212ce 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 !pParse->nested
212cf 26 26 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c && strcmp(zName,
212d0 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 "sqlite_sequenc
212d1 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 e")==0 ){. pT
212d2 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 able->pSchema->p
212d3 53 65 71 54 61 62 20 3d 20 70 54 61 62 6c 65 3b SeqTab = pTable;
212d4 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
212d5 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 * Begin generati
212d6 6e 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 ng the code that
212d7 20 77 69 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 will insert the
212d8 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 69 6e table record in
212d9 74 6f 0a 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 to. ** the SQLI
212da 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e TE_MASTER table.
212db 20 20 4e 6f 74 65 20 69 6e 20 70 61 72 74 69 63 Note in partic
212dc 75 6c 61 72 20 74 68 61 74 20 77 65 20 6d 75 73 ular that we mus
212dd 74 20 67 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 t go ahead. **
212de 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 and allocate the
212df 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 record number f
212e0 6f 72 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 or the table ent
212e1 72 79 20 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 ry now. Before
212e2 61 6e 79 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 any. ** PRIMARY
212e3 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 6b KEY or UNIQUE k
212e4 65 79 77 6f 72 64 73 20 61 72 65 20 70 61 72 73 eywords are pars
212e5 65 64 2e 20 20 54 68 6f 73 65 20 6b 65 79 77 6f ed. Those keywo
212e6 72 64 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 rds will cause.
212e7 20 2a 2a 20 69 6e 64 69 63 65 73 20 74 6f 20 62 ** indices to b
212e8 65 20 63 72 65 61 74 65 64 20 61 6e 64 20 74 68 e created and th
212e9 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 6d e table record m
212ea 75 73 74 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 ust come before
212eb 74 68 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 the . ** indice
212ec 73 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 72 s. Hence, the r
212ed 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 ecord number for
212ee 20 74 68 65 20 74 61 62 6c 65 20 6d 75 73 74 20 the table must
212ef 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a be allocated. *
212f0 2a 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 * now.. */. if
212f1 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 ( !db->init.busy
212f2 20 26 26 20 28 76 20 3d 20 73 71 6c 69 74 65 33 && (v = sqlite3
212f3 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29 GetVdbe(pParse))
212f4 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a !=0 ){. int j
212f5 31 3b 0a 20 20 20 20 69 6e 74 20 66 69 6c 65 46 1;. int fileF
212f6 6f 72 6d 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 ormat;. int r
212f7 65 67 31 2c 20 72 65 67 32 2c 20 72 65 67 33 3b eg1, reg2, reg3;
212f8 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 . sqlite3Begi
212f9 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 nWriteOperation(
212fa 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b pParse, 0, iDb);
212fb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
212fc 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
212fd 4c 45 0a 20 20 20 20 69 66 28 20 69 73 56 69 72 LE. if( isVir
212fe 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 tual ){. sq
212ff 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
21300 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 v, OP_VBegin);.
21301 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
21302 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 /* If the file
21303 66 6f 72 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 format and encod
21304 69 6e 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 ing in the datab
21305 61 73 65 20 68 61 76 65 20 6e 6f 74 20 62 65 65 ase have not bee
21306 6e 20 73 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 n set, . ** s
21307 65 74 20 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 et them now..
21308 20 2a 2f 0a 20 20 20 20 72 65 67 31 20 3d 20 70 */. reg1 = p
21309 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 Parse->regRowid
2130a 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
2130b 3b 0a 20 20 20 20 72 65 67 32 20 3d 20 70 50 61 ;. reg2 = pPa
2130c 72 73 65 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b rse->regRoot = +
2130d 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
2130e 20 20 20 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 reg3 = ++pPar
2130f 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 se->nMem;. sq
21310 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21311 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 v, OP_ReadCookie
21312 2c 20 69 44 62 2c 20 72 65 67 33 2c 20 31 29 3b , iDb, reg3, 1);
21313 20 20 20 2f 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 /* file_forma
21314 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 t */. sqlite3
21315 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c VdbeUsesBtree(v,
21316 20 69 44 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20 iDb);. j1 =
21317 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21318 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 1(v, OP_If, reg3
21319 29 3b 0a 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 );. fileForma
2131a 74 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 t = (db->flags &
2131b 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 SQLITE_LegacyFi
2131c 6c 65 46 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20 leFmt)!=0 ?.
2131d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 1
2131e 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c : SQLITE_MAX_FIL
2131f 45 5f 46 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71 E_FORMAT;. sq
21320 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21321 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66 v, OP_Integer, f
21322 69 6c 65 46 6f 72 6d 61 74 2c 20 72 65 67 33 29 ileFormat, reg3)
21323 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21324 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 eAddOp3(v, OP_Se
21325 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c tCookie, iDb, 1,
21326 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 reg3);. sqli
21327 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
21328 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43 OP_Integer, ENC
21329 28 64 62 29 2c 20 72 65 67 33 29 3b 0a 20 20 20 (db), reg3);.
2132a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2132b 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b p3(v, OP_SetCook
2132c 69 65 2c 20 69 44 62 2c 20 34 2c 20 72 65 67 33 ie, iDb, 4, reg3
2132d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
2132e 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 beJumpHere(v, j1
2132f 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 );.. /* This
21330 6a 75 73 74 20 63 72 65 61 74 65 73 20 61 20 70 just creates a p
21331 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f lace-holder reco
21332 72 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 rd in the sqlite
21333 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 _master table..
21334 20 20 20 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 ** The record
21335 20 63 72 65 61 74 65 64 20 64 6f 65 73 20 6e 6f created does no
21336 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69 t contain anythi
21337 6e 67 20 79 65 74 2e 20 20 49 74 20 77 69 6c 6c ng yet. It will
21338 20 62 65 20 72 65 70 6c 61 63 65 64 0a 20 20 20 be replaced.
21339 20 2a 2a 20 62 79 20 74 68 65 20 72 65 61 6c 20 ** by the real
2133a 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 20 67 65 entry in code ge
2133b 6e 65 72 61 74 65 64 20 61 74 20 73 71 6c 69 74 nerated at sqlit
2133c 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20 e3EndTable()..
2133d 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
2133e 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 6e 65 rowid for the ne
2133f 77 20 65 6e 74 72 79 20 69 73 20 6c 65 66 74 20 w entry is left
21340 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72 in register pPar
21341 73 65 2d 3e 72 65 67 52 6f 77 69 64 2e 0a 20 20 se->regRowid..
21342 20 20 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 ** The root pa
21343 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
21344 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20 6c 65 new table is le
21345 66 74 20 69 6e 20 72 65 67 20 70 50 61 72 73 65 ft in reg pParse
21346 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a ->regRoot.. *
21347 2a 20 54 68 65 20 72 6f 77 69 64 20 61 6e 64 20 * The rowid and
21348 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
21349 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 65 64 values are need
2134a 65 64 20 62 79 20 74 68 65 20 63 6f 64 65 20 74 ed by the code t
2134b 68 61 74 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 hat. ** sqlit
2134c 65 33 45 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20 e3EndTable will
2134d 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f generate.. */
2134e 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
2134f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
21350 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
21351 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
21352 41 42 4c 45 29 0a 20 20 20 20 69 66 28 20 69 73 ABLE). if( is
21353 56 69 65 77 20 7c 7c 20 69 73 56 69 72 74 75 61 View || isVirtua
21354 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 l ){. sqlit
21355 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21356 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 OP_Integer, 0, r
21357 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a eg2);. }else.
21358 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 #endif. {.
21359 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2135a 64 4f 70 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 dOp2(v, OP_Creat
2135b 65 54 61 62 6c 65 2c 20 69 44 62 2c 20 72 65 67 eTable, iDb, reg
2135c 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 2);. }. sq
2135d 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 lite3OpenMasterT
2135e 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 able(pParse, iDb
2135f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
21360 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
21361 65 77 52 6f 77 69 64 2c 20 30 2c 20 72 65 67 31 ewRowid, 0, reg1
21362 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
21363 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
21364 75 6c 6c 2c 20 30 2c 20 72 65 67 33 29 3b 0a 20 ull, 0, reg3);.
21365 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21366 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 dOp3(v, OP_Inser
21367 74 2c 20 30 2c 20 72 65 67 33 2c 20 72 65 67 31 t, 0, reg3, reg1
21368 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
21369 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 beChangeP5(v, OP
2136a 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 FLAG_APPEND);.
2136b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2136c 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 Op0(v, OP_Close)
2136d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d ;. }.. /* Norm
2136e 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72 29 20 72 al (non-error) r
2136f 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 72 65 74 75 eturn. */. retu
21370 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 rn;.. /* If an
21371 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 65 error occurs, we
21372 20 6a 75 6d 70 20 68 65 72 65 20 2a 2f 0a 62 65 jump here */.be
21373 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3a gin_table_error:
21374 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
21375 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 (db, zName);. r
21376 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eturn;.}../*.**
21377 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 This macro is us
21378 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 77 ed to compare tw
21379 6f 20 73 74 72 69 6e 67 73 20 69 6e 20 61 20 63 o strings in a c
2137a 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 65 20 ase-insensitive
2137b 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49 74 20 69 73 manner..** It is
2137c 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72 slightly faster
2137d 20 74 68 61 6e 20 63 61 6c 6c 69 6e 67 20 73 71 than calling sq
2137e 6c 69 74 65 33 53 74 72 49 43 6d 70 28 29 20 64 lite3StrICmp() d
2137f 69 72 65 63 74 6c 79 2c 20 62 75 74 0a 2a 2a 20 irectly, but.**
21380 70 72 6f 64 75 63 65 73 20 6c 61 72 67 65 72 20 produces larger
21381 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e code..**.** WARN
21382 49 4e 47 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 ING: This macro
21383 69 73 20 6e 6f 74 20 63 6f 6d 70 61 74 69 62 6c is not compatibl
21384 65 20 77 69 74 68 20 74 68 65 20 73 74 72 63 6d e with the strcm
21385 70 28 29 20 66 61 6d 69 6c 79 2e 20 49 74 0a 2a p() family. It.*
21386 2a 20 72 65 74 75 72 6e 73 20 74 72 75 65 20 69 * returns true i
21387 66 20 74 68 65 20 74 77 6f 20 73 74 72 69 6e 67 f the two string
21388 73 20 61 72 65 20 65 71 75 61 6c 2c 20 6f 74 68 s are equal, oth
21389 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f erwise false..*/
2138a 0a 23 64 65 66 69 6e 65 20 53 54 52 49 43 4d 50 .#define STRICMP
2138b 28 78 2c 20 79 29 20 28 5c 0a 73 71 6c 69 74 65 (x, y) (\.sqlite
2138c 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 3UpperToLower[*(
2138d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
2138e 28 78 29 5d 3d 3d 20 20 20 5c 0a 73 71 6c 69 74 (x)]== \.sqlit
2138f 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a e3UpperToLower[*
21390 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
21391 29 28 79 29 5d 20 20 20 20 20 5c 0a 26 26 20 73 )(y)] \.&& s
21392 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 28 78 qlite3StrICmp((x
21393 29 2b 31 2c 28 79 29 2b 31 29 3d 3d 30 20 29 0a )+1,(y)+1)==0 ).
21394 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
21395 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20 74 column to the t
21396 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 62 able currently b
21397 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 eing constructed
21398 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 ..**.** The pars
21399 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f er calls this ro
2139a 75 74 69 6e 65 20 6f 6e 63 65 20 66 6f 72 20 65 utine once for e
2139b 61 63 68 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 ach column decla
2139c 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 20 43 ration.** in a C
2139d 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
2139e 65 6d 65 6e 74 2e 20 20 73 71 6c 69 74 65 33 53 ement. sqlite3S
2139f 74 61 72 74 54 61 62 6c 65 28 29 20 67 65 74 73 tartTable() gets
213a0 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 69 72 73 74 called.** first
213a1 20 74 6f 20 67 65 74 20 74 68 69 6e 67 73 20 67 to get things g
213a2 6f 69 6e 67 2e 20 20 54 68 65 6e 20 74 68 69 73 oing. Then this
213a3 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
213a4 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 63 ed for each.** c
213a5 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 olumn..*/.SQLITE
213a6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
213a7 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 lite3AddColumn(P
213a8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f arse *pParse, To
213a9 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 54 ken *pName){. T
213aa 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 able *p;. int i
213ab 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 43 ;. char *z;. C
213ac 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 olumn *pCol;. s
213ad 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
213ae 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 rse->db;. if( (
213af 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 p = pParse->pNew
213b0 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 Table)==0 ) retu
213b1 72 6e 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d rn;.#if SQLITE_M
213b2 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 AX_COLUMN. if(
213b3 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c p->nCol+1>db->aL
213b4 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
213b5 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 T_COLUMN] ){.
213b6 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
213b7 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 (pParse, "too ma
213b8 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73 ny columns on %s
213b9 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 ", p->zName);.
213ba 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 return;. }.#e
213bb 6e 64 69 66 0a 20 20 7a 20 3d 20 73 71 6c 69 74 ndif. z = sqlit
213bc 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
213bd 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 db, pName);. if
213be 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( z==0 ) return;
213bf 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d . for(i=0; i<p-
213c0 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nCol; i++){.
213c1 20 69 66 28 20 53 54 52 49 43 4d 50 28 7a 2c 20 if( STRICMP(z,
213c2 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 p->aCol[i].zName
213c3 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) ){. sqlit
213c4 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
213c5 65 2c 20 22 64 75 70 6c 69 63 61 74 65 20 63 6f e, "duplicate co
213c6 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25 73 22 2c 20 lumn name: %s",
213c7 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 z);. sqlite
213c8 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 3DbFree(db, z);.
213c9 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
213ca 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 }. }. if( (p
213cb 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d 3d 30 ->nCol & 0x7)==0
213cc 20 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a ){. Column *
213cd 61 4e 65 77 3b 0a 20 20 20 20 61 4e 65 77 20 3d aNew;. aNew =
213ce 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
213cf 63 28 64 62 2c 70 2d 3e 61 43 6f 6c 2c 28 70 2d c(db,p->aCol,(p-
213d0 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65 6f 66 28 >nCol+8)*sizeof(
213d1 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 p->aCol[0]));.
213d2 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b if( aNew==0 ){
213d3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
213d4 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 Free(db, z);.
213d5 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
213d6 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 61 . p->aCol = a
213d7 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 20 New;. }. pCol
213d8 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 = &p->aCol[p->nC
213d9 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43 ol];. memset(pC
213da 6f 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d ol, 0, sizeof(p-
213db 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 >aCol[0]));. pC
213dc 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20 ol->zName = z;.
213dd 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 . /* If there i
213de 73 20 6e 6f 20 74 79 70 65 20 73 70 65 63 69 66 s no type specif
213df 69 65 64 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76 ied, columns hav
213e0 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 61 66 e the default af
213e1 66 69 6e 69 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e finity. ** 'NON
213e2 45 27 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 E'. If there is
213e3 61 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64 a type specified
213e4 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 41 64 , then sqlite3Ad
213e5 64 43 6f 6c 75 6d 6e 54 79 70 65 28 29 20 77 69 dColumnType() wi
213e6 6c 6c 0a 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 ll. ** be calle
213e7 64 20 6e 65 78 74 20 74 6f 20 73 65 74 20 70 43 d next to set pC
213e8 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 63 6f 72 ol->affinity cor
213e9 72 65 63 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70 rectly.. */. p
213ea 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 Col->affinity =
213eb 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b SQLITE_AFF_NONE;
213ec 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a . p->nCol++;.}.
213ed 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
213ee 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ine is called by
213ef 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c the parser whil
213f0 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 e in the middle
213f1 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 of.** parsing a
213f2 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 CREATE TABLE sta
213f3 74 65 6d 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20 tement. A "NOT
213f4 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 NULL" constraint
213f5 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65 has.** been see
213f6 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 n on a column.
213f7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 This routine set
213f8 73 20 74 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c s the notNull fl
213f9 61 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c ag on.** the col
213fa 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e umn currently un
213fb 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e der construction
213fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
213fd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
213fe 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 AddNotNull(Parse
213ff 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e *pParse, int on
21400 45 72 72 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20 Error){. Table
21401 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 *p;. int i;. i
21402 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e f( (p = pParse->
21403 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 pNewTable)==0 )
21404 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d return;. i = p-
21405 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 >nCol-1;. if( i
21406 3e 3d 30 20 29 20 70 2d 3e 61 43 6f 6c 5b 69 5d >=0 ) p->aCol[i]
21407 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 28 75 38 29 6f .notNull = (u8)o
21408 6e 45 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a nError;.}../*.**
21409 20 53 63 61 6e 20 74 68 65 20 63 6f 6c 75 6d 6e Scan the column
2140a 20 74 79 70 65 20 6e 61 6d 65 20 7a 54 79 70 65 type name zType
2140b 20 28 6c 65 6e 67 74 68 20 6e 54 79 70 65 29 20 (length nType)
2140c 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 0a 2a and return the.*
2140d 2a 20 61 73 73 6f 63 69 61 74 65 64 20 61 66 66 * associated aff
2140e 69 6e 69 74 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a inity type..**.*
2140f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
21410 6f 65 73 20 61 20 63 61 73 65 2d 69 6e 64 65 70 oes a case-indep
21411 65 6e 64 65 6e 74 20 73 65 61 72 63 68 20 6f 66 endent search of
21412 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65 20 0a zType for the .
21413 2a 2a 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e ** substrings in
21414 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
21415 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20 6f 66 20 able. If one of
21416 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 the substrings i
21417 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65 20 s.** found, the
21418 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 66 corresponding af
21419 66 69 6e 69 74 79 20 69 73 20 72 65 74 75 72 6e finity is return
2141a 65 64 2e 20 49 66 20 7a 54 79 70 65 20 63 6f 6e ed. If zType con
2141b 74 61 69 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74 68 tains.** more th
2141c 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 an one of the su
2141d 62 73 74 72 69 6e 67 73 2c 20 65 6e 74 72 69 65 bstrings, entrie
2141e 73 20 74 6f 77 61 72 64 20 74 68 65 20 74 6f 70 s toward the top
2141f 20 6f 66 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c of .** the tabl
21420 65 20 74 61 6b 65 20 70 72 69 6f 72 69 74 79 2e e take priority.
21421 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 For example, if
21422 20 7a 54 79 70 65 20 69 73 20 27 42 4c 4f 42 49 zType is 'BLOBI
21423 4e 54 27 2c 20 0a 2a 2a 20 53 51 4c 49 54 45 5f NT', .** SQLITE_
21424 41 46 46 5f 49 4e 54 45 47 45 52 20 69 73 20 72 AFF_INTEGER is r
21425 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 eturned..**.** S
21426 75 62 73 74 72 69 6e 67 20 20 20 20 20 7c 20 41 ubstring | A
21427 66 66 69 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d ffinity.** -----
21428 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
21429 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27 -----------.** '
2142a 49 4e 54 27 20 20 20 20 20 20 20 20 20 7c 20 53 INT' | S
2142b 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
2142c 52 0a 2a 2a 20 27 43 48 41 52 27 20 20 20 20 20 R.** 'CHAR'
2142d 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f | SQLITE_AFF_
2142e 54 45 58 54 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 TEXT.** 'CLOB'
2142f 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 | SQLITE_A
21430 46 46 5f 54 45 58 54 0a 2a 2a 20 27 54 45 58 54 FF_TEXT.** 'TEXT
21431 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 ' | SQLIT
21432 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 42 E_AFF_TEXT.** 'B
21433 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 LOB' | SQ
21434 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a 2a LITE_AFF_NONE.**
21435 20 27 52 45 41 4c 27 20 20 20 20 20 20 20 20 7c 'REAL' |
21436 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
21437 0a 2a 2a 20 27 46 4c 4f 41 27 20 20 20 20 20 20 .** 'FLOA'
21438 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 | SQLITE_AFF_R
21439 45 41 4c 0a 2a 2a 20 27 44 4f 55 42 27 20 20 20 EAL.** 'DOUB'
2143a 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 | SQLITE_AF
2143b 46 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20 F_REAL.**.** If
2143c 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 none of the subs
2143d 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 61 62 trings in the ab
2143e 6f 76 65 20 74 61 62 6c 65 20 61 72 65 20 66 6f ove table are fo
2143f 75 6e 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 und,.** SQLITE_A
21440 46 46 5f 4e 55 4d 45 52 49 43 20 69 73 20 72 65 FF_NUMERIC is re
21441 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 turned..*/.SQLIT
21442 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 E_PRIVATE char s
21443 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 qlite3AffinityTy
21444 70 65 28 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a pe(const Token *
21445 70 54 79 70 65 29 7b 0a 20 20 75 33 32 20 68 20 pType){. u32 h
21446 3d 20 30 3b 0a 20 20 63 68 61 72 20 61 66 66 20 = 0;. char aff
21447 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d = SQLITE_AFF_NUM
21448 45 52 49 43 3b 0a 20 20 63 6f 6e 73 74 20 75 6e ERIC;. const un
21449 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e signed char *zIn
2144a 20 3d 20 70 54 79 70 65 2d 3e 7a 3b 0a 20 20 63 = pType->z;. c
2144b 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
2144c 61 72 20 2a 7a 45 6e 64 20 3d 20 26 70 54 79 70 ar *zEnd = &pTyp
2144d 65 2d 3e 7a 5b 70 54 79 70 65 2d 3e 6e 5d 3b 0a e->z[pType->n];.
2144e 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a . while( zIn!=z
2144f 45 6e 64 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 End ){. h = (
21450 68 3c 3c 38 29 20 2b 20 73 71 6c 69 74 65 33 55 h<<8) + sqlite3U
21451 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 49 6e pperToLower[*zIn
21452 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 ];. zIn++;.
21453 20 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c if( h==(('c'<<
21454 32 34 29 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27 24)+('h'<<16)+('
21455 61 27 3c 3c 38 29 2b 27 72 27 29 20 29 7b 20 20 a'<<8)+'r') ){
21456 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 48 /* CH
21457 41 52 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 AR */. aff
21458 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 = SQLITE_AFF_TEX
21459 54 3b 20 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 T; . }else if
2145a 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b ( h==(('c'<<24)+
2145b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c ('l'<<16)+('o'<<
2145c 38 29 2b 27 62 27 29 20 29 7b 20 20 20 20 20 20 8)+'b') ){
2145d 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20 /* CLOB */.
2145e 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 aff = SQLITE_A
2145f 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c FF_TEXT;. }el
21460 73 65 20 69 66 28 20 68 3d 3d 28 28 27 74 27 3c se if( h==(('t'<
21461 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 <24)+('e'<<16)+(
21462 27 78 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 'x'<<8)+'t') ){
21463 20 20 20 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f /* TEXT */
21464 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c . aff = SQL
21465 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 ITE_AFF_TEXT;.
21466 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 }else if( h==(
21467 28 27 62 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c ('b'<<24)+('l'<<
21468 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 16)+('o'<<8)+'b'
21469 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 4c ) /* BL
2146a 4f 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 OB */. &&
2146b 20 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 (aff==SQLITE_AF
2146c 46 5f 4e 55 4d 45 52 49 43 20 7c 7c 20 61 66 66 F_NUMERIC || aff
2146d 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 ==SQLITE_AFF_REA
2146e 4c 29 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 L) ){. aff
2146f 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e = SQLITE_AFF_NON
21470 45 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 E;.#ifndef SQLIT
21471 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
21472 50 4f 49 4e 54 0a 20 20 20 20 7d 65 6c 73 65 20 POINT. }else
21473 69 66 28 20 68 3d 3d 28 28 27 72 27 3c 3c 32 34 if( h==(('r'<<24
21474 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 61 27 )+('e'<<16)+('a'
21475 3c 3c 38 29 2b 27 6c 27 29 20 20 20 20 20 20 20 <<8)+'l')
21476 20 20 20 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 20 /* REAL */.
21477 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 && aff==SQ
21478 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
21479 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 ){. aff =
2147a 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b SQLITE_AFF_REAL;
2147b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 . }else if( h
2147c 3d 3d 28 28 27 66 27 3c 3c 32 34 29 2b 28 27 6c ==(('f'<<24)+('l
2147d 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b '<<16)+('o'<<8)+
2147e 27 61 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 'a') /*
2147f 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 20 20 20 20 FLOA */.
21480 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f && aff==SQLITE_
21481 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 AFF_NUMERIC ){.
21482 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 aff = SQLIT
21483 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 E_AFF_REAL;.
21484 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 }else if( h==(('
21485 64 27 3c 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 36 d'<<24)+('o'<<16
21486 29 2b 28 27 75 27 3c 3c 38 29 2b 27 62 27 29 20 )+('u'<<8)+'b')
21487 20 20 20 20 20 20 20 20 20 2f 2a 20 44 4f 55 42 /* DOUB
21488 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 */. && a
21489 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ff==SQLITE_AFF_N
2148a 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 UMERIC ){.
2148b 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 aff = SQLITE_AFF
2148c 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 20 _REAL;.#endif.
2148d 20 20 7d 65 6c 73 65 20 69 66 28 20 28 68 26 30 }else if( (h&0
2148e 78 30 30 46 46 46 46 46 46 29 3d 3d 28 28 27 69 x00FFFFFF)==(('i
2148f 27 3c 3c 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b '<<16)+('n'<<8)+
21490 27 74 27 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e 't') ){ /* IN
21491 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d T */. aff =
21492 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
21493 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b GER;. break
21494 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 ;. }. }.. r
21495 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a eturn aff;.}../*
21496 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
21497 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
21498 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 e parser while i
21499 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a n the middle of.
2149a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 ** parsing a CRE
2149b 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
2149c 65 6e 74 2e 20 20 54 68 65 20 70 46 69 72 73 74 ent. The pFirst
2149d 20 74 6f 6b 65 6e 20 69 73 20 74 68 65 20 66 69 token is the fi
2149e 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 rst.** token in
2149f 74 68 65 20 73 65 71 75 65 6e 63 65 20 6f 66 20 the sequence of
214a0 74 6f 6b 65 6e 73 20 74 68 61 74 20 64 65 73 63 tokens that desc
214a1 72 69 62 65 20 74 68 65 20 74 79 70 65 20 6f 66 ribe the type of
214a2 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63 the.** column c
214a3 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 urrently under c
214a4 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 20 70 onstruction. p
214a5 4c 61 73 74 20 69 73 20 74 68 65 20 6c 61 73 74 Last is the last
214a6 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 token.** in the
214a7 20 73 65 71 75 65 6e 63 65 2e 20 20 55 73 65 20 sequence. Use
214a8 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e this information
214a9 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 61 20 to construct a
214aa 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 63 string.** that c
214ab 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 79 70 65 ontains the type
214ac 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 name of the colu
214ad 6d 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68 61 mn and store tha
214ae 74 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 7a t string.** in z
214af 54 79 70 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 Type..*/ .SQLITE
214b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
214b1 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 lite3AddColumnTy
214b2 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 pe(Parse *pParse
214b3 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b , Token *pType){
214b4 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 . Table *p;. i
214b5 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a nt i;. Column *
214b6 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 pCol;. sqlite3
214b7 2a 64 62 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d *db;.. if( (p =
214b8 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
214b9 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b le)==0 ) return;
214ba 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 . i = p->nCol-1
214bb 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 20 72 65 ;. if( i<0 ) re
214bc 74 75 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 turn;. pCol = &
214bd 70 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 64 62 p->aCol[i];. db
214be 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
214bf 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
214c0 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b b, pCol->zType);
214c1 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d . pCol->zType =
214c2 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
214c3 54 6f 6b 65 6e 28 64 62 2c 20 70 54 79 70 65 29 Token(db, pType)
214c4 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 ;. pCol->affini
214c5 74 79 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69 ty = sqlite3Affi
214c6 6e 69 74 79 54 79 70 65 28 70 54 79 70 65 29 3b nityType(pType);
214c7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78 .}../*.** The ex
214c8 70 72 65 73 73 69 6f 6e 20 69 73 20 74 68 65 20 pression is the
214c9 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f default value fo
214ca 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e r the most recen
214cb 74 6c 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e tly added column
214cc 0a 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 .** of the table
214cd 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 currently under
214ce 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a construction..*
214cf 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 76 61 6c *.** Default val
214d0 75 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6d ue expressions m
214d1 75 73 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e ust be constant.
214d2 20 20 52 61 69 73 65 20 61 6e 20 65 78 63 65 70 Raise an excep
214d3 74 69 6f 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 tion if this.**
214d4 69 73 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2e is not the case.
214d5 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
214d6 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ine is called by
214d7 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c the parser whil
214d8 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 e in the middle
214d9 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 of.** parsing a
214da 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 CREATE TABLE sta
214db 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 tement..*/.SQLIT
214dc 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
214dd 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 qlite3AddDefault
214de 56 61 6c 75 65 28 50 61 72 73 65 20 2a 70 50 61 Value(Parse *pPa
214df 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
214e0 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 ){. Table *p;.
214e1 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 Column *pCol;.
214e2 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
214e3 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 Parse->db;. if(
214e4 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e (p = pParse->pN
214e5 65 77 54 61 62 6c 65 29 21 3d 30 20 29 7b 0a 20 ewTable)!=0 ){.
214e6 20 20 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 pCol = &(p->a
214e7 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b Col[p->nCol-1]);
214e8 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
214e9 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 3ExprIsConstantO
214ea 72 46 75 6e 63 74 69 6f 6e 28 70 45 78 70 72 29 rFunction(pExpr)
214eb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
214ec 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
214ed 2c 20 22 64 65 66 61 75 6c 74 20 76 61 6c 75 65 , "default value
214ee 20 6f 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 of column [%s]
214ef 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22 is not constant"
214f0 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c ,. pCol
214f1 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 ->zName);. }e
214f2 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 lse{. /* A
214f3 63 6f 70 79 20 6f 66 20 70 45 78 70 72 20 69 73 copy of pExpr is
214f4 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 used instead of
214f5 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 2c 20 61 the original, a
214f6 73 20 70 45 78 70 72 20 63 6f 6e 74 61 69 6e 73 s pExpr contains
214f7 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 73 . ** tokens
214f8 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 76 that point to v
214f9 6f 6c 61 74 69 6c 65 20 6d 65 6d 6f 72 79 2e 20 olatile memory.
214fa 54 68 65 20 27 73 70 61 6e 27 20 6f 66 20 74 68 The 'span' of th
214fb 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 e expression.
214fc 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 ** is require
214fd 64 20 62 79 20 70 72 61 67 6d 61 20 74 61 62 6c d by pragma tabl
214fe 65 5f 69 6e 66 6f 2e 0a 20 20 20 20 20 20 2a 2f e_info.. */
214ff 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
21500 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f prDelete(db, pCo
21501 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 l->pDflt);.
21502 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 73 pCol->pDflt = s
21503 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 0a 20 qlite3ExprDup(.
21504 20 20 20 20 20 20 20 20 20 64 62 2c 20 70 45 78 db, pEx
21505 70 72 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 pr, EXPRDUP_REDU
21506 43 45 7c 45 58 50 52 44 55 50 5f 44 49 53 54 49 CE|EXPRDUP_DISTI
21507 4e 43 54 53 50 41 4e 0a 20 20 20 20 20 20 29 3b NCTSPAN. );
21508 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
21509 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
2150a 62 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a b, pExpr);.}../*
2150b 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74 68 .** Designate th
2150c 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 66 6f e PRIMARY KEY fo
2150d 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20 70 4c r the table. pL
2150e 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 ist is a list of
2150f 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 6f names .** of co
21510 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 lumns that form
21511 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e the primary key.
21512 20 20 49 66 20 70 4c 69 73 74 20 69 73 20 4e 55 If pList is NU
21513 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 LL, then the.**
21514 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 most recently ad
21515 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 ded column of th
21516 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 e table is the p
21517 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a rimary key..**.*
21518 2a 20 41 20 74 61 62 6c 65 20 63 61 6e 20 68 61 * A table can ha
21519 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 70 ve at most one p
2151a 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20 rimary key. If
2151b 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 the table alread
2151c 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d 61 y has.** a prima
2151d 72 79 20 6b 65 79 20 28 61 6e 64 20 74 68 69 73 ry key (and this
2151e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 is the second p
2151f 72 69 6d 61 72 79 20 6b 65 79 29 20 74 68 65 6e rimary key) then
21520 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65 72 create an.** er
21521 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ror..**.** If th
21522 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 e PRIMARY KEY is
21523 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c on a single col
21524 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 61 74 79 umn whose dataty
21525 70 65 20 69 73 20 49 4e 54 45 47 45 52 2c 0a 2a pe is INTEGER,.*
21526 2a 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 74 * then we will t
21527 72 79 20 74 6f 20 75 73 65 20 74 68 61 74 20 63 ry to use that c
21528 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 6f 77 olumn as the row
21529 69 64 2e 20 20 53 65 74 20 74 68 65 20 54 61 62 id. Set the Tab
2152a 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65 6c le.iPKey.** fiel
2152b 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 75 d of the table u
2152c 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
2152d 6e 20 74 6f 20 62 65 20 74 68 65 20 69 6e 64 65 n to be the inde
2152e 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54 45 x of the.** INTE
2152f 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 GER PRIMARY KEY
21530 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e 69 column. Table.i
21531 50 4b 65 79 20 69 73 20 73 65 74 20 74 6f 20 2d PKey is set to -
21532 31 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 1 if there is.**
21533 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d no INTEGER PRIM
21534 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 ARY KEY..**.** I
21535 66 20 74 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 f the key is not
21536 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d an INTEGER PRIM
21537 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 63 72 ARY KEY, then cr
21538 65 61 74 65 20 61 20 75 6e 69 71 75 65 0a 2a 2a eate a unique.**
21539 20 69 6e 64 65 78 20 66 6f 72 20 74 68 65 20 6b index for the k
2153a 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69 73 ey. No index is
2153b 20 63 72 65 61 74 65 64 20 66 6f 72 20 49 4e 54 created for INT
2153c 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
2153d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
2153e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
2153f 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 0a 3AddPrimaryKey(.
21540 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
21541 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
21542 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
21543 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a List *pList, /*
21544 20 4c 69 73 74 20 6f 66 20 66 69 65 6c 64 20 6e List of field n
21545 61 6d 65 73 20 74 6f 20 62 65 20 69 6e 64 65 78 ames to be index
21546 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 ed */. int onEr
21547 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57 68 61 ror, /* Wha
21548 74 20 74 6f 20 64 6f 20 77 69 74 68 20 61 20 75 t to do with a u
21549 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 niqueness confli
2154a 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74 6f ct */. int auto
2154b 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 Inc, /* Tru
2154c 65 20 69 66 20 74 68 65 20 41 55 54 4f 49 4e 43 e if the AUTOINC
2154d 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20 69 REMENT keyword i
2154e 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 s present */. i
2154f 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 20 20 20 nt sortOrder
21550 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 /* SQLITE_SO_AS
21551 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f 44 C or SQLITE_SO_D
21552 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c ESC */.){. Tabl
21553 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65 e *pTab = pParse
21554 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 63 ->pNewTable;. c
21555 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a har *zType = 0;.
21556 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 2c int iCol = -1,
21557 20 69 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d i;. if( pTab==
21558 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 0 || IN_DECLARE_
21559 56 54 41 42 20 29 20 67 6f 74 6f 20 70 72 69 6d VTAB ) goto prim
2155a 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 ary_key_exit;.
2155b 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 if( pTab->tabFla
2155c 67 73 20 26 20 54 46 5f 48 61 73 50 72 69 6d 61 gs & TF_HasPrima
2155d 72 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c ryKey ){. sql
2155e 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
2155f 72 73 65 2c 20 0a 20 20 20 20 20 20 22 74 61 62 rse, . "tab
21560 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 20 6d 6f le \"%s\" has mo
21561 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 72 69 6d re than one prim
21562 61 72 79 20 6b 65 79 22 2c 20 70 54 61 62 2d 3e ary key", pTab->
21563 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f zName);. goto
21564 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 primary_key_exi
21565 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e 74 t;. }. pTab->t
21566 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61 abFlags |= TF_Ha
21567 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20 69 sPrimaryKey;. i
21568 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 f( pList==0 ){.
21569 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e iCol = pTab->
2156a 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70 54 nCol - 1;. pT
2156b 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 ab->aCol[iCol].i
2156c 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 sPrimKey = 1;.
2156d 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 }else{. for(i
2156e 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 =0; i<pList->nEx
2156f 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 pr; i++){.
21570 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c for(iCol=0; iCol
21571 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f <pTab->nCol; iCo
21572 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 l++){. if
21573 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
21574 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 (pList->a[i].zNa
21575 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 me, pTab->aCol[i
21576 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 Col].zName)==0 )
21577 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 {. brea
21578 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
21579 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 }. if( i
2157a 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 Col<pTab->nCol )
2157b 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e {. pTab->
2157c 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 aCol[iCol].isPri
2157d 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 mKey = 1;.
2157e 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
2157f 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20 29 pList->nExpr>1 )
21580 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d 0a iCol = -1;. }.
21581 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 if( iCol>=0 &&
21582 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c iCol<pTab->nCol
21583 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 ){. zType =
21584 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d pTab->aCol[iCol]
21585 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69 66 .zType;. }. if
21586 28 20 7a 54 79 70 65 20 26 26 20 73 71 6c 69 74 ( zType && sqlit
21587 65 33 53 74 72 49 43 6d 70 28 7a 54 79 70 65 2c e3StrICmp(zType,
21588 20 22 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a 20 "INTEGER")==0.
21589 20 20 20 20 20 20 20 26 26 20 73 6f 72 74 4f 72 && sortOr
2158a 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 der==SQLITE_SO_A
2158b 53 43 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e SC ){. pTab->
2158c 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20 20 iPKey = iCol;.
2158d 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 20 pTab->keyConf
2158e 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 = (u8)onError;.
2158f 20 20 20 61 73 73 65 72 74 28 20 61 75 74 6f 49 assert( autoI
21590 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63 nc==0 || autoInc
21591 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d ==1 );. pTab-
21592 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74 >tabFlags |= aut
21593 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72 oInc*TF_Autoincr
21594 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 ement;. }else i
21595 66 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69 f( autoInc ){.#i
21596 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
21597 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a T_AUTOINCREMENT.
21598 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
21599 4d 73 67 28 70 50 61 72 73 65 2c 20 22 41 55 54 Msg(pParse, "AUT
2159a 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e OINCREMENT is on
2159b 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e ly allowed on an
2159c 20 22 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47 ". "INTEG
2159d 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29 ER PRIMARY KEY")
2159e 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 ;.#endif. }else
2159f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 {. sqlite3Cre
215a0 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c ateIndex(pParse,
215a1 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c 0, 0, 0, pList,
215a2 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20 onError, 0, 0,
215a3 73 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20 sortOrder, 0);.
215a4 20 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 pList = 0;.
215a5 7d 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 }..primary_key_e
215a6 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 xit:. sqlite3Ex
215a7 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 prListDelete(pPa
215a8 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b rse->db, pList);
215a9 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a . return;.}../*
215aa 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 .** Add a new CH
215ab 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 ECK constraint t
215ac 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 o the table curr
215ad 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 ently under cons
215ae 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c truction..*/.SQL
215af 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
215b0 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b sqlite3AddCheck
215b1 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 Constraint(. Pa
215b2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
215b3 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
215b4 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 xt */. Expr *pC
215b5 68 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 heckExpr /* The
215b6 20 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f check expressio
215b7 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 n */.){. sqlite
215b8 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
215b9 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 db;.#ifndef SQLI
215ba 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 TE_OMIT_CHECK.
215bb 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 Table *pTab = pP
215bc 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b arse->pNewTable;
215bd 0a 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21 . if( pTab && !
215be 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
215bf 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 43 48 ){. /* The CH
215c0 45 43 4b 20 65 78 70 72 65 73 73 69 6f 6e 20 6d ECK expression m
215c1 75 73 74 20 62 65 20 64 75 70 6c 69 63 61 74 65 ust be duplicate
215c2 64 20 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e 73 d so that tokens
215c3 20 72 65 66 65 72 0a 20 20 20 20 2a 2a 20 74 6f refer. ** to
215c4 20 6d 61 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 malloced space
215c5 61 6e 64 20 6e 6f 74 20 74 68 65 20 28 65 70 68 and not the (eph
215c6 65 6d 65 72 61 6c 29 20 74 65 78 74 20 6f 66 20 emeral) text of
215c7 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
215c8 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e . ** statemen
215c9 74 20 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e 70 t */. pTab->p
215ca 43 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 Check = sqlite3E
215cb 78 70 72 41 6e 64 28 64 62 2c 20 70 54 61 62 2d xprAnd(db, pTab-
215cc 3e 70 43 68 65 63 6b 2c 20 0a 20 20 20 20 20 20 >pCheck, .
215cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
215ce 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
215cf 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
215d0 43 68 65 63 6b 45 78 70 72 2c 20 30 29 29 3b 0a CheckExpr, 0));.
215d1 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c }.#endif. sql
215d2 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
215d3 62 2c 20 70 43 68 65 63 6b 45 78 70 72 29 3b 0a b, pCheckExpr);.
215d4 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
215d5 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 collation funct
215d6 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 ion of the most
215d7 72 65 63 65 6e 74 6c 79 20 70 61 72 73 65 64 20 recently parsed
215d8 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 table column.**
215d9 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65 71 20 67 to the CollSeq g
215da 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f iven..*/.SQLITE_
215db 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
215dc 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 ite3AddCollateTy
215dd 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 pe(Parse *pParse
215de 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 , Token *pToken)
215df 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 {. Table *p;.
215e0 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a int i;. char *z
215e1 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Coll;
215e2 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e /* Dequoted n
215e3 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e ame of collation
215e4 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 73 sequence */. s
215e5 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 qlite3 *db;.. i
215e6 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e f( (p = pParse->
215e7 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 pNewTable)==0 )
215e8 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d return;. i = p-
215e9 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 >nCol-1;. db =
215ea 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 pParse->db;. zC
215eb 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d oll = sqlite3Nam
215ec 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 eFromToken(db, p
215ed 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a Token);. if( !z
215ee 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 0a Coll ) return;..
215ef 20 20 69 66 28 20 73 71 6c 69 74 65 33 4c 6f 63 if( sqlite3Loc
215f0 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 ateCollSeq(pPars
215f1 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b e, zColl, -1) ){
215f2 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 . Index *pIdx
215f3 3b 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d ;. p->aCol[i]
215f4 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a .zColl = zColl;.
215f5 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
215f6 20 63 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 column is decla
215f7 72 65 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 red as "<name> P
215f8 52 49 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 RIMARY KEY COLLA
215f9 54 45 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 TE <type>",.
215fa 2a 2a 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 ** then an index
215fb 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 may have been c
215fc 72 65 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 reated on this c
215fd 6f 6c 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 olumn before the
215fe 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f . ** collatio
215ff 6e 20 74 79 70 65 20 77 61 73 20 61 64 64 65 64 n type was added
21600 2e 20 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 . Correct this i
21601 66 20 69 74 20 69 73 20 74 68 65 20 63 61 73 65 f it is the case
21602 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
21603 28 70 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b (pIdx=p->pIndex;
21604 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 pIdx; pIdx=pIdx
21605 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
21606 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 assert( pIdx->nC
21607 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 olumn==1 );.
21608 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f if( pIdx->aiCo
21609 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 lumn[0]==i ){.
2160a 20 20 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f pIdx->azCo
2160b 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b ll[0] = p->aCol[
2160c 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 i].zColl;.
2160d 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b }. }. }else{
2160e 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
2160f 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 ee(db, zColl);.
21610 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
21611 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
21612 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 s the collation
21613 73 65 71 75 65 6e 63 65 20 66 6f 72 20 64 61 74 sequence for dat
21614 61 62 61 73 65 20 6e 61 74 69 76 65 20 74 65 78 abase native tex
21615 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 t.** encoding id
21616 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 entified by the
21617 73 74 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 string zName, le
21618 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a ngth nName..**.*
21619 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 * If the request
2161a 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 ed collation seq
2161b 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 uence is not ava
2161c 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 ilable, or not a
2161d 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 vailable.** in t
2161e 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 he database nati
2161f 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 ve encoding, the
21620 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f collation facto
21621 72 79 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f ry is invoked to
21622 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 74 2e 20 .** request it.
21623 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e If the collation
21624 20 66 61 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f factory does no
21625 74 20 73 75 70 70 6c 79 20 73 75 63 68 20 61 20 t supply such a
21626 73 65 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 sequence,.** and
21627 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 69 73 the sequence is
21628 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e available in an
21629 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 other text encod
2162a 69 6e 67 2c 20 74 68 65 6e 20 74 68 61 74 20 69 ing, then that i
2162b 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e s.** returned in
2162c 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 stead..**.** If
2162d 6e 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 no versions of t
2162e 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c he requested col
2162f 6c 61 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 lations sequence
21630 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 are available,
21631 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 or.** another er
21632 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c ror occurs, NULL
21633 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
21634 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
21635 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a e written into.*
21636 2a 20 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 * pParse..**.**
21637 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
21638 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
21639 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c sqlite3FindColl
2163a 53 65 71 28 29 2e 20 20 54 68 69 73 20 72 6f 75 Seq(). This rou
2163b 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 tine.** invokes
2163c 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 the collation fa
2163d 63 74 6f 72 79 20 69 66 20 74 68 65 20 6e 61 6d ctory if the nam
2163e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e ed collation can
2163f 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 not be found.**
21640 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e and generates an
21641 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a error message..
21642 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21643 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 E CollSeq *sqlit
21644 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 e3LocateCollSeq(
21645 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 Parse *pParse, c
21646 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
21647 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 , int nName){.
21648 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
21649 61 72 73 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65 arse->db;. u8 e
2164a 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 nc = ENC(db);.
2164b 75 38 20 69 6e 69 74 62 75 73 79 20 3d 20 64 62 u8 initbusy = db
2164c 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43 ->init.busy;. C
2164d 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a ollSeq *pColl;..
2164e 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
2164f 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 3FindCollSeq(db,
21650 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 enc, zName, nNa
21651 6d 65 2c 20 69 6e 69 74 62 75 73 79 29 3b 0a 20 me, initbusy);.
21652 20 69 66 28 20 21 69 6e 69 74 62 75 73 79 20 26 if( !initbusy &
21653 26 20 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 & (!pColl || !pC
21654 6f 6c 6c 2d 3e 78 43 6d 70 29 20 29 7b 0a 20 20 oll->xCmp) ){.
21655 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
21656 33 47 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 3GetCollSeq(db,
21657 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 6e 4e pColl, zName, nN
21658 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 ame);. if( !p
21659 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 Coll ){. if
2165a 28 20 6e 4e 61 6d 65 3c 30 20 29 7b 0a 20 20 20 ( nName<0 ){.
2165b 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c nName = sql
2165c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a ite3Strlen(db, z
2165d 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Name);. }.
2165e 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
2165f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f rMsg(pParse, "no
21660 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 such collation
21661 73 65 71 75 65 6e 63 65 3a 20 25 2e 2a 73 22 2c sequence: %.*s",
21662 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a nName, zName);.
21663 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b pColl = 0;
21664 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 . }. }.. re
21665 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a turn pColl;.}...
21666 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
21667 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e ode that will in
21668 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 crement the sche
21669 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a ma cookie..**.**
2166a 20 54 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b The schema cook
2166b 69 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 ie is used to de
2166c 74 65 72 6d 69 6e 65 20 77 68 65 6e 20 74 68 65 termine when the
2166d 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65 0a schema for the.
2166e 2a 2a 20 64 61 74 61 62 61 73 65 20 63 68 61 6e ** database chan
2166f 67 65 73 2e 20 20 41 66 74 65 72 20 65 61 63 68 ges. After each
21670 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2c 20 schema change,
21671 74 68 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 the cookie value
21672 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 57 68 .** changes. Wh
21673 65 6e 20 61 20 70 72 6f 63 65 73 73 20 66 69 72 en a process fir
21674 73 74 20 72 65 61 64 73 20 74 68 65 20 73 63 68 st reads the sch
21675 65 6d 61 20 69 74 20 72 65 63 6f 72 64 73 20 74 ema it records t
21676 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 he.** cookie. T
21677 68 65 72 65 61 66 74 65 72 2c 20 77 68 65 6e 65 hereafter, whene
21678 76 65 72 20 69 74 20 67 6f 65 73 20 74 6f 20 61 ver it goes to a
21679 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62 61 ccess the databa
2167a 73 65 2c 0a 2a 2a 20 69 74 20 63 68 65 63 6b 73 se,.** it checks
2167b 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 6d the cookie to m
2167c 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 63 68 ake sure the sch
2167d 65 6d 61 20 68 61 73 20 6e 6f 74 20 63 68 61 6e ema has not chan
2167e 67 65 64 0a 2a 2a 20 73 69 6e 63 65 20 69 74 20 ged.** since it
2167f 77 61 73 20 6c 61 73 74 20 72 65 61 64 2e 0a 2a was last read..*
21680 2a 0a 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20 69 *.** This plan i
21681 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 s not completely
21682 20 62 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 bullet-proof.
21683 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 It is possible f
21684 6f 72 0a 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 or.** the schema
21685 20 74 6f 20 63 68 61 6e 67 65 20 6d 75 6c 74 69 to change multi
21686 70 6c 65 20 74 69 6d 65 73 20 61 6e 64 20 66 6f ple times and fo
21687 72 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 r the cookie to
21688 62 65 0a 2a 2a 20 73 65 74 20 62 61 63 6b 20 74 be.** set back t
21689 6f 20 70 72 69 6f 72 20 76 61 6c 75 65 2e 20 20 o prior value.
2168a 42 75 74 20 73 63 68 65 6d 61 20 63 68 61 6e 67 But schema chang
2168b 65 73 20 61 72 65 20 69 6e 66 72 65 71 75 65 6e es are infrequen
2168c 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 6f t.** and the pro
2168d 62 61 62 69 6c 69 74 79 20 6f 66 20 68 69 74 74 bability of hitt
2168e 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 6f 6f ing the same coo
2168f 6b 69 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 6c kie value is onl
21690 79 0a 2a 2a 20 31 20 63 68 61 6e 63 65 20 69 6e y.** 1 chance in
21691 20 32 5e 33 32 2e 20 20 53 6f 20 77 65 27 72 65 2^32. So we're
21692 20 73 61 66 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f safe enough..*/
21693 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21694 76 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e void sqlite3Chan
21695 67 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 20 2a geCookie(Parse *
21696 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 pParse, int iDb)
21697 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c {. int r1 = sql
21698 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
21699 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 Parse);. sqlite
2169a 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
2169b 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 db;. Vdbe *v =
2169c 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
2169d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2169e 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
2169f 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 , db->aDb[iDb].p
216a0 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 Schema->schema_c
216a1 6f 6f 6b 69 65 2b 31 2c 20 72 31 29 3b 0a 20 20 ookie+1, r1);.
216a2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
216a3 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 3(v, OP_SetCooki
216a4 65 2c 20 69 44 62 2c 20 30 2c 20 72 31 29 3b 0a e, iDb, 0, r1);.
216a5 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
216a6 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
216a7 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 r1);.}../*.** Me
216a8 61 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 asure the number
216a9 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 6e of characters n
216aa 65 65 64 65 64 20 74 6f 20 6f 75 74 70 75 74 20 eeded to output
216ab 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 69 64 65 the given.** ide
216ac 6e 74 69 66 69 65 72 2e 20 20 54 68 65 20 6e 75 ntifier. The nu
216ad 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 69 6e mber returned in
216ae 63 6c 75 64 65 73 20 61 6e 79 20 71 75 6f 74 65 cludes any quote
216af 73 20 75 73 65 64 0a 2a 2a 20 62 75 74 20 64 6f s used.** but do
216b0 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 es not include t
216b1 68 65 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 he null terminat
216b2 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 or..**.** The es
216b3 74 69 6d 61 74 65 20 69 73 20 63 6f 6e 73 65 72 timate is conser
216b4 76 61 74 69 76 65 2e 20 20 49 74 20 6d 69 67 68 vative. It migh
216b5 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 74 t be larger that
216b6 20 77 68 61 74 20 69 73 0a 2a 2a 20 72 65 61 6c what is.** real
216b7 6c 79 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 ly needed..*/.st
216b8 61 74 69 63 20 69 6e 74 20 69 64 65 6e 74 4c 65 atic int identLe
216b9 6e 67 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 ngth(const char
216ba 2a 7a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 *z){. int n;.
216bb 66 6f 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b for(n=0; *z; n++
216bc 2c 20 7a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 , z++){. if(
216bd 2a 7a 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 *z=='"' ){ n++;
216be 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e }. }. return n
216bf 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 + 2;.}../*.** T
216c0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
216c1 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
216c2 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e sqlite3GetToken
216c3 28 29 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 69 () used by .** i
216c4 73 56 61 6c 69 64 44 69 6d 65 6e 73 69 6f 6e 28 sValidDimension(
216c5 29 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ). This function
216c6 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 73 71 differs from sq
216c7 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 29 20 lite3GetToken()
216c8 69 6e 0a 2a 2a 20 74 68 61 74 3a 0a 2a 2a 0a 2a in.** that:.**.*
216c9 2a 20 20 20 2a 20 57 68 69 74 65 73 70 61 63 65 * * Whitespace
216ca 20 69 73 20 69 67 6e 6f 72 65 64 2c 20 61 6e 64 is ignored, and
216cb 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6f 75 74 70 .** * The outp
216cc 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70 65 54 ut variable *peT
216cd 6f 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 30 oken is set to 0
216ce 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 if the end of t
216cf 68 65 0a 2a 2a 20 20 20 20 20 6e 75 6c 2d 74 65 he.** nul-te
216d0 72 6d 69 6e 61 74 65 64 20 69 6e 70 75 74 20 73 rminated input s
216d1 74 72 69 6e 67 20 69 73 20 72 65 61 63 68 65 64 tring is reached
216d2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
216d3 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65 28 getTokenNoSpace(
216d4 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
216d5 2c 20 69 6e 74 20 2a 70 65 54 6f 6b 65 6e 29 7b , int *peToken){
216d6 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 . int n = 0;.
216d7 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
216d8 73 70 61 63 65 28 7a 5b 6e 5d 29 20 29 20 6e 2b space(z[n]) ) n+
216d9 2b 3b 0a 20 20 69 66 28 20 21 7a 5b 6e 5d 20 29 +;. if( !z[n] )
216da 7b 0a 20 20 20 20 2a 70 65 54 6f 6b 65 6e 20 3d {. *peToken =
216db 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 0;. return 0
216dc 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e ;. }. return n
216dd 20 2b 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b + sqlite3GetTok
216de 65 6e 28 26 7a 5b 6e 5d 2c 20 70 65 54 6f 6b 65 en(&z[n], peToke
216df 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 n);.}../*.** Par
216e0 61 6d 65 74 65 72 20 7a 20 70 6f 69 6e 74 73 20 ameter z points
216e1 74 6f 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 to a nul-termina
216e2 74 65 64 20 73 74 72 69 6e 67 2e 20 52 65 74 75 ted string. Retu
216e3 72 6e 20 74 72 75 65 20 69 66 2c 20 77 68 65 6e rn true if, when
216e4 0a 2a 2a 20 77 68 69 74 65 73 70 61 63 65 20 69 .** whitespace i
216e5 73 20 69 67 6e 6f 72 65 64 2c 20 74 68 65 20 63 s ignored, the c
216e6 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73 20 ontents of this
216e7 73 74 72 69 6e 67 20 6d 61 74 63 68 65 73 20 6f string matches o
216e8 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 66 6f ne of .** the fo
216e9 6c 6c 6f 77 69 6e 67 20 70 61 74 74 65 72 6e 73 llowing patterns
216ea 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 22 0a 2a :.**.** "".*
216eb 2a 20 20 20 20 20 22 28 6e 75 6d 62 65 72 29 22 * "(number)"
216ec 0a 2a 2a 20 20 20 20 20 22 28 6e 75 6d 62 65 72 .** "(number
216ed 2c 6e 75 6d 62 65 72 29 22 0a 2a 2f 0a 73 74 61 ,number)".*/.sta
216ee 74 69 63 20 69 6e 74 20 69 73 56 61 6c 69 64 44 tic int isValidD
216ef 69 6d 65 6e 73 69 6f 6e 28 75 6e 73 69 67 6e 65 imension(unsigne
216f0 64 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e d char *z){. in
216f1 74 20 65 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 t eToken;. int
216f2 6e 20 3d 20 30 3b 0a 20 20 6e 20 2b 3d 20 67 65 n = 0;. n += ge
216f3 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65 28 26 7a tTokenNoSpace(&z
216f4 5b 6e 5d 2c 20 26 65 54 6f 6b 65 6e 29 3b 0a 20 [n], &eToken);.
216f5 20 69 66 28 20 65 54 6f 6b 65 6e 20 29 7b 0a 20 if( eToken ){.
216f6 20 20 20 69 66 28 20 65 54 6f 6b 65 6e 21 3d 54 if( eToken!=T
216f7 4b 5f 4c 50 20 29 20 72 65 74 75 72 6e 20 30 3b K_LP ) return 0;
216f8 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b . n += getTok
216f9 65 6e 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c enNoSpace(&z[n],
216fa 20 26 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 69 &eToken);. i
216fb 66 28 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f 50 4c f( eToken==TK_PL
216fc 55 53 20 7c 7c 20 65 54 6f 6b 65 6e 3d 3d 54 4b US || eToken==TK
216fd 5f 4d 49 4e 55 53 20 29 7b 0a 20 20 20 20 20 20 _MINUS ){.
216fe 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 n += getTokenNoS
216ff 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f pace(&z[n], &eTo
21700 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ken);. }.
21701 69 66 28 20 65 54 6f 6b 65 6e 21 3d 54 4b 5f 49 if( eToken!=TK_I
21702 4e 54 45 47 45 52 20 26 26 20 65 54 6f 6b 65 6e NTEGER && eToken
21703 21 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 72 65 74 !=TK_FLOAT ) ret
21704 75 72 6e 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 urn 0;. n +=
21705 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65 28 getTokenNoSpace(
21706 26 7a 5b 6e 5d 2c 20 26 65 54 6f 6b 65 6e 29 3b &z[n], &eToken);
21707 0a 20 20 20 20 69 66 28 20 65 54 6f 6b 65 6e 3d . if( eToken=
21708 3d 54 4b 5f 43 4f 4d 4d 41 20 29 7b 0a 20 20 20 =TK_COMMA ){.
21709 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e n += getToken
2170a 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 NoSpace(&z[n], &
2170b 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 69 eToken);. i
2170c 66 28 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f 50 4c f( eToken==TK_PL
2170d 55 53 20 7c 7c 20 65 54 6f 6b 65 6e 3d 3d 54 4b US || eToken==TK
2170e 5f 4d 49 4e 55 53 20 29 7b 0a 20 20 20 20 20 20 _MINUS ){.
2170f 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e n += getTokenN
21710 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 oSpace(&z[n], &e
21711 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 0a Token);. }.
21712 20 20 20 20 20 20 69 66 28 20 65 54 6f 6b 65 6e if( eToken
21713 21 3d 54 4b 5f 49 4e 54 45 47 45 52 20 26 26 20 !=TK_INTEGER &&
21714 65 54 6f 6b 65 6e 21 3d 54 4b 5f 46 4c 4f 41 54 eToken!=TK_FLOAT
21715 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
21716 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e n += getToken
21717 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 NoSpace(&z[n], &
21718 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 eToken);. }.
21719 20 20 20 69 66 28 20 65 54 6f 6b 65 6e 21 3d 54 if( eToken!=T
2171a 4b 5f 52 50 20 29 20 72 65 74 75 72 6e 20 30 3b K_RP ) return 0;
2171b 0a 20 20 20 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 . getTokenNoS
2171c 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f pace(&z[n], &eTo
2171d 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ken);. }. if(
2171e 65 54 6f 6b 65 6e 20 29 20 72 65 74 75 72 6e 20 eToken ) return
2171f 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0;. return 1;.}
21720 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ../*.** The firs
21721 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 t parameter is a
21722 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f pointer to an o
21723 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 54 68 utput buffer. Th
21724 65 20 73 65 63 6f 6e 64 20 0a 2a 2a 20 70 61 72 e second .** par
21725 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e ameter is a poin
21726 74 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 ter to an intege
21727 72 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 r that contains
21728 74 68 65 20 6f 66 66 73 65 74 20 61 74 0a 2a 2a the offset at.**
21729 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 which to write
2172a 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 into the output
2172b 62 75 66 66 65 72 2e 20 54 68 69 73 20 66 75 6e buffer. This fun
2172c 63 74 69 6f 6e 20 63 6f 70 69 65 73 20 74 68 65 ction copies the
2172d 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 .** nul-terminat
2172e 65 64 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 ed string pointe
2172f 64 20 74 6f 20 62 79 20 74 68 65 20 74 68 69 72 d to by the thir
21730 64 20 70 61 72 61 6d 65 74 65 72 2c 20 7a 53 69 d parameter, zSi
21731 67 6e 65 64 49 64 65 6e 74 2c 0a 2a 2a 20 74 6f gnedIdent,.** to
21732 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6f the specified o
21733 66 66 73 65 74 20 69 6e 20 74 68 65 20 62 75 66 ffset in the buf
21734 66 65 72 20 61 6e 64 20 75 70 64 61 74 65 73 20 fer and updates
21735 2a 70 49 64 78 20 74 6f 20 72 65 66 65 72 0a 2a *pIdx to refer.*
21736 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 * to the first b
21737 79 74 65 20 61 66 74 65 72 20 74 68 65 20 6c 61 yte after the la
21738 73 74 20 62 79 74 65 20 77 72 69 74 74 65 6e 20 st byte written
21739 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
2173a 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 ..** .** If the
2173b 73 74 72 69 6e 67 20 7a 53 69 67 6e 65 64 49 64 string zSignedId
2173c 65 6e 74 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 ent consists ent
2173d 69 72 65 6c 79 20 6f 66 20 61 6c 70 68 61 2d 6e irely of alpha-n
2173e 75 6d 65 72 69 63 0a 2a 2a 20 63 68 61 72 61 63 umeric.** charac
2173f 74 65 72 73 2c 20 64 6f 65 73 20 6e 6f 74 20 62 ters, does not b
21740 65 67 69 6e 20 77 69 74 68 20 61 20 64 69 67 69 egin with a digi
21741 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 6e 20 t and is not an
21742 53 51 4c 20 6b 65 79 77 6f 72 64 2c 0a 2a 2a 20 SQL keyword,.**
21743 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 then it is copie
21744 64 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 d to the output
21745 62 75 66 66 65 72 20 65 78 61 63 74 6c 79 20 61 buffer exactly a
21746 73 20 69 74 20 69 73 2e 20 4f 74 68 65 72 77 69 s it is. Otherwi
21747 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 71 75 6f se,.** it is quo
21748 74 65 64 20 75 73 69 6e 67 20 64 6f 75 62 6c 65 ted using double
21749 2d 71 75 6f 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 -quotes..*/.stat
2174a 69 63 20 76 6f 69 64 20 69 64 65 6e 74 50 75 74 ic void identPut
2174b 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 (char *z, int *p
2174c 49 64 78 2c 20 63 68 61 72 20 2a 7a 53 69 67 6e Idx, char *zSign
2174d 65 64 49 64 65 6e 74 2c 20 69 6e 74 20 69 73 54 edIdent, int isT
2174e 79 70 65 6e 61 6d 65 29 7b 0a 20 20 75 6e 73 69 ypename){. unsi
2174f 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 64 65 6e gned char *zIden
21750 74 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 t = (unsigned ch
21751 61 72 2a 29 7a 53 69 67 6e 65 64 49 64 65 6e 74 ar*)zSignedIdent
21752 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 ;. int i, j, ne
21753 65 64 51 75 6f 74 65 3b 0a 20 20 69 20 3d 20 2a edQuote;. i = *
21754 70 49 64 78 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 pIdx;.. for(j=0
21755 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b ; zIdent[j]; j++
21756 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 ){. if( !sqli
21757 74 65 33 49 73 61 6c 6e 75 6d 28 7a 49 64 65 6e te3Isalnum(zIden
21758 74 5b 6a 5d 29 20 26 26 20 7a 49 64 65 6e 74 5b t[j]) && zIdent[
21759 6a 5d 21 3d 27 5f 27 20 29 20 62 72 65 61 6b 3b j]!='_' ) break;
2175a 0a 20 20 7d 0a 20 20 6e 65 65 64 51 75 6f 74 65 . }. needQuote
2175b 20 3d 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 = sqlite3Isdigi
2175c 74 28 7a 49 64 65 6e 74 5b 30 5d 29 20 7c 7c 20 t(zIdent[0]) ||
2175d 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f sqlite3KeywordCo
2175e 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54 de(zIdent, j)!=T
2175f 4b 5f 49 44 3b 0a 20 20 69 66 28 20 21 6e 65 65 K_ID;. if( !nee
21760 64 51 75 6f 74 65 20 29 7b 0a 20 20 20 20 69 66 dQuote ){. if
21761 28 20 69 73 54 79 70 65 6e 61 6d 65 20 29 7b 0a ( isTypename ){.
21762 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 /* If this
21763 20 69 73 20 61 20 74 79 70 65 2d 6e 61 6d 65 2c is a type-name,
21764 20 61 6c 6c 6f 77 20 61 20 6c 69 74 74 6c 65 20 allow a little
21765 6d 6f 72 65 20 66 6c 65 78 69 62 69 6c 69 74 79 more flexibility
21766 2e 20 49 6e 20 53 51 4c 69 74 65 2c 0a 20 20 20 . In SQLite,.
21767 20 20 20 2a 2a 20 61 20 74 79 70 65 2d 6e 61 6d ** a type-nam
21768 65 20 69 73 20 73 70 65 63 69 66 69 65 64 20 61 e is specified a
21769 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 s:. **.
2176a 20 20 2a 2a 20 20 20 69 64 73 20 5b 69 64 73 5d ** ids [ids]
2176b 20 5b 28 6e 75 6d 62 65 72 20 5b 2c 20 6e 75 6d [(number [, num
2176c 62 65 72 5d 29 5d 0a 20 20 20 20 20 20 2a 2a 0a ber])]. **.
2176d 20 20 20 20 20 20 2a 2a 20 77 68 65 72 65 20 22 ** where "
2176e 69 64 73 22 20 69 73 20 65 69 74 68 65 72 20 61 ids" is either a
2176f 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 6f quoted string o
21770 72 20 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 r a simple ident
21771 69 66 69 65 72 20 28 69 6e 20 74 68 65 0a 20 20 ifier (in the.
21772 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 6e 6f 74 ** above not
21773 61 74 69 6f 6e 2c 20 5b 5d 20 6d 65 61 6e 73 20 ation, [] means
21774 6f 70 74 69 6f 6e 61 6c 29 2e 20 49 74 20 69 73 optional). It is
21775 20 61 20 62 69 74 20 74 72 69 63 6b 79 20 74 6f a bit tricky to
21776 20 63 68 65 63 6b 0a 20 20 20 20 20 20 2a 2a 20 check. **
21777 66 6f 72 20 61 6c 6c 20 63 61 73 65 73 2c 20 62 for all cases, b
21778 75 74 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f ut it is good to
21779 20 61 76 6f 69 64 20 75 6e 6e 65 63 65 73 73 61 avoid unnecessa
2177a 72 69 6c 79 20 71 75 6f 74 69 6e 67 20 63 6f 6d rily quoting com
2177b 6d 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79 70 mon. ** typ
2177c 65 6e 61 6d 65 73 20 6c 69 6b 65 20 56 41 52 43 enames like VARC
2177d 48 41 52 28 31 30 29 2e 0a 20 20 20 20 20 20 2a HAR(10).. *
2177e 2f 0a 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 /. needQuot
2177f 65 20 3d 20 21 69 73 56 61 6c 69 64 44 69 6d 65 e = !isValidDime
21780 6e 73 69 6f 6e 28 26 7a 49 64 65 6e 74 5b 6a 5d nsion(&zIdent[j]
21781 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
21782 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 needQuote =
21783 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 7d zIdent[j];. }
21784 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 65 65 64 . }.. if( need
21785 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d Quote ) z[i++] =
21786 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b '"';. for(j=0;
21787 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 zIdent[j]; j++)
21788 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a {. z[i++] = z
21789 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 Ident[j];. if
2178a 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 ( zIdent[j]=='"'
2178b 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b ) z[i++] = '"';
2178c 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51 . }. if( needQ
2178d 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 uote ) z[i++] =
2178e 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b '"';. z[i] = 0;
2178f 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a . *pIdx = i;.}.
21790 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
21791 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 a CREATE TABLE s
21792 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72 tatement appropr
21793 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 69 76 iate for the giv
21794 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65 en.** table. Me
21795 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 mory to hold the
21796 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 74 61 text of the sta
21797 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e tement is obtain
21798 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 ed.** from sqlit
21799 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 eMalloc() and mu
2179a 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 st be freed by t
2179b 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
2179c 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 ion..*/.static c
2179d 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65 har *createTable
2179e 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62 Stmt(sqlite3 *db
2179f 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69 , Table *p){. i
217a0 6e 74 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 nt i, k, n;. ch
217a1 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61 ar *zStmt;. cha
217a2 72 20 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c r *zSep, *zSep2,
217a3 20 2a 7a 45 6e 64 2c 20 2a 7a 3b 0a 20 20 43 6f *zEnd, *z;. Co
217a4 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 lumn *pCol;. n
217a5 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 20 = 0;. for(pCol
217a6 3d 20 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 = p->aCol, i=0;
217a7 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 i<p->nCol; i++,
217a8 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b pCol++){. n +
217a9 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 43 = identLength(pC
217aa 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ol->zName);.
217ab 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b z = pCol->zType;
217ac 0a 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 . if( z ){.
217ad 20 20 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 n += identLe
217ae 6e 67 74 68 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 ngth(z);. }.
217af 20 7d 0a 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c }. n += identL
217b0 65 6e 67 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b ength(p->zName);
217b1 0a 20 20 69 66 28 20 6e 3c 35 30 20 29 7b 0a 20 . if( n<50 ){.
217b2 20 20 20 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 zSep = "";.
217b3 20 20 7a 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 zSep2 = ",";.
217b4 20 20 20 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 zEnd = ")";.
217b5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 }else{. zSep
217b6 20 3d 20 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a = "\n ";. z
217b7 53 65 70 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a Sep2 = ",\n ";.
217b8 20 20 20 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 zEnd = "\n)"
217b9 3b 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 ;. }. n += 35
217ba 2b 20 36 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a + 6*p->nCol;. z
217bb 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 Stmt = sqlite3Ma
217bc 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 69 66 28 lloc( n );. if(
217bd 20 7a 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 zStmt==0 ){.
217be 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
217bf 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 d = 1;. retur
217c0 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 n 0;. }. sqlit
217c1 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a e3_snprintf(n, z
217c2 53 74 6d 74 2c 20 22 43 52 45 41 54 45 20 54 41 Stmt, "CREATE TA
217c3 42 4c 45 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71 BLE ");. k = sq
217c4 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 53 lite3Strlen30(zS
217c5 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50 75 74 tmt);. identPut
217c6 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a (zStmt, &k, p->z
217c7 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7a 53 74 6d Name, 0);. zStm
217c8 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 t[k++] = '(';.
217c9 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c for(pCol=p->aCol
217ca 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c , i=0; i<p->nCol
217cb 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a ; i++, pCol++){.
217cc 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
217cd 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 intf(n-k, &zStmt
217ce 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20 20 [k], zSep);.
217cf 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c k += sqlite3Strl
217d0 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b en30(&zStmt[k]);
217d1 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a 53 65 70 . zSep = zSep
217d2 32 3b 0a 20 20 20 20 69 64 65 6e 74 50 75 74 28 2;. identPut(
217d3 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d zStmt, &k, pCol-
217d4 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 >zName, 0);.
217d5 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c 2d 3e 7a if( (z = pCol->z
217d6 54 79 70 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Type)!=0 ){.
217d7 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 zStmt[k++] = '
217d8 20 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ';. assert
217d9 28 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33 53 ( (int)(sqlite3S
217da 74 72 6c 65 6e 33 30 28 7a 29 2b 6b 2b 31 29 3c trlen30(z)+k+1)<
217db 3d 6e 20 29 3b 0a 20 20 20 20 20 20 69 64 65 6e =n );. iden
217dc 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 tPut(zStmt, &k,
217dd 7a 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d z, 1);. }. }
217de 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
217df 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b ntf(n-k, &zStmt[
217e0 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 3b k], "%s", zEnd);
217e1 0a 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 3b . return zStmt;
217e2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
217e3 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
217e4 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 66 to report the f
217e5 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 65 inal ")" that te
217e6 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 52 rminates.** a CR
217e7 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
217e8 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
217e9 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 table structure
217ea 74 68 61 74 20 6f 74 68 65 72 20 61 63 74 69 6f that other actio
217eb 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 n routines have
217ec 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a 2a been building.**
217ed 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 is added to the
217ee 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 internal hash t
217ef 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67 20 ables, assuming
217f0 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a 2a no errors have.*
217f1 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a * occurred..**.*
217f2 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 * An entry for t
217f3 68 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64 65 he table is made
217f4 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 74 in the master t
217f5 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75 6e able on disk, un
217f6 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73 20 less.** this is
217f7 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c a temporary tabl
217f8 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62 75 e or db->init.bu
217f9 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62 2d sy==1. When db-
217fa 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a >init.busy==1.**
217fb 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 it means we are
217fc 20 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c reading the sql
217fd 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
217fe 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 because we just
217ff 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f .** connected to
21800 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 the database or
21801 20 62 65 63 61 75 73 65 20 74 68 65 20 73 71 6c because the sql
21802 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
21803 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c 79 has.** recently
21804 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 65 changed, so the
21805 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20 entry for this
21806 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 table already ex
21807 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 ists in.** the s
21808 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
21809 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 le. We do not w
2180a 61 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 74 ant to create it
2180b 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 again..**.** If
2180c 20 74 68 65 20 70 53 65 6c 65 63 74 20 61 72 67 the pSelect arg
2180d 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c ument is not NUL
2180e 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 L, it means that
2180f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
21810 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 was called to c
21811 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 67 65 reate a table ge
21812 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20 0a nerated from a .
21813 2a 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 ** "CREATE TABLE
21814 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20 2e ... AS SELECT .
21815 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 .." statement.
21816 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 The column names
21817 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 74 of.** the new t
21818 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68 20 able will match
21819 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f the result set o
2181a 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f f the SELECT..*/
2181b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
2181c 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54 void sqlite3EndT
2181d 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 able(. Parse *p
2181e 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
2181f 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 /* Parse context
21820 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f */. Token *pCo
21821 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ns, /*
21822 20 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20 61 The ',' token a
21823 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 6f fter the last co
21824 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20 20 lumn defn. */.
21825 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20 Token *pEnd,
21826 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
21827 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20 69 inal ')' token i
21828 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 n the CREATE TAB
21829 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a LE */. Select *
2182a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 20 20 pSelect
2182b 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 61 /* Select from a
2182c 20 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 20 "CREATE ... AS
2182d 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 20 SELECT" */.){.
2182e 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69 Table *p;. sqli
2182f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
21830 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b ->db;. int iDb;
21831 0a 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d 30 .. if( (pEnd==0
21832 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 20 && pSelect==0)
21833 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 || pParse->nErr
21834 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
21835 6c 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74 75 led ) {. retu
21836 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 50 rn;. }. p = pP
21837 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b arse->pNewTable;
21838 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
21839 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 turn;.. assert(
2183a 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 !db->init.busy
2183b 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a 0a || !pSelect );..
2183c 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
2183d 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
2183e 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 p->pSchema);..#
2183f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
21840 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 65 IT_CHECK. /* Re
21841 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 solve names in a
21842 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 ll CHECK constra
21843 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e int expressions.
21844 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 . */. if( p->p
21845 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72 63 Check ){. Src
21846 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20 20 20 List sSrc;
21847 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21848 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f 72 Fake SrcList for
21849 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
2184a 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f le */. NameCo
2184b 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 ntext sNC;
2184c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
2184d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 50 e context for pP
2184e 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 arse->pNewTable
2184f 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 */.. memset(&
21850 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 sNC, 0, sizeof(s
21851 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 NC));. memset
21852 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 6f (&sSrc, 0, sizeo
21853 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73 53 f(sSrc));. sS
21854 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 rc.nSrc = 1;.
21855 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d 65 sSrc.a[0].zName
21856 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 = p->zName;.
21857 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62 20 sSrc.a[0].pTab
21858 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b = p;. sSrc.a[
21859 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0].iCursor = -1;
2185a 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 . sNC.pParse
2185b 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e = pParse;. sN
2185c 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73 53 C.pSrcList = &sS
2185d 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43 68 rc;. sNC.isCh
2185e 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 eck = 1;. if(
2185f 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
21860 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 xprNames(&sNC, p
21861 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 20 ->pCheck) ){.
21862 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
21863 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 . }.#endif /* !
21864 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
21865 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 MIT_CHECK) */..
21866 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69 /* If the db->i
21867 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 69 74 nit.busy is 1 it
21868 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 means we are re
21869 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f 66 ading the SQL of
2186a 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c 69 f the. ** "sqli
2186b 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 73 te_master" or "s
2186c 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 qlite_temp_maste
2186d 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 r" table on the
2186e 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64 6f disk.. ** So do
2186f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 not write to th
21870 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20 45 e disk again. E
21871 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74 20 xtract the root
21872 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a page number. **
21873 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 66 for the table f
21874 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 rom the db->init
21875 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e 20 .newTnum field.
21876 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 (The page numbe
21877 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 r. ** should ha
21878 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 65 72 ve been put ther
21879 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 4f e by the sqliteO
2187a 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29 0a penCb routine.).
2187b 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 */. if( db->i
2187c 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 nit.busy ){.
2187d 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e p->tnum = db->in
2187e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d 0a it.newTnum;. }.
2187f 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e 69 . /* If not ini
21880 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e 20 tializing, then
21881 63 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 create a record
21882 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c for the new tabl
21883 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53 51 e. ** in the SQ
21884 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c LITE_MASTER tabl
21885 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
21886 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 e.. **. ** If
21887 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f 52 this is a TEMPOR
21888 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74 65 ARY table, write
21889 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 the entry into
2188a 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 the auxiliary.
2188b 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 ** file instead
2188c 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e of into the main
2188d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
2188e 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e */. if( !db->
2188f 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 init.busy ){.
21890 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62 65 int n;. Vdbe
21891 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a *v;. char *z
21892 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69 65 Type; /* "vie
21893 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a 2f w" or "table" */
21894 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 . char *zType
21895 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20 6f 2; /* "VIEW" o
21896 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20 20 r "TABLE" */.
21897 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20 20 char *zStmt;
21898 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
21899 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20 CREATE TABLE or
2189a 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 CREATE VIEW stat
2189b 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76 20 ement */.. v
2189c 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
2189d 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 (pParse);. if
2189e 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( v==0 ) return;
2189f 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 .. sqlite3Vdb
218a0 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c eAddOp1(v, OP_Cl
218a1 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a ose, 0);.. /*
218a2 20 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69 61 6c . ** Initial
218a3 69 7a 65 20 7a 54 79 70 65 20 66 6f 72 20 74 68 ize zType for th
218a4 65 20 6e 65 77 20 76 69 65 77 20 6f 72 20 74 61 e new view or ta
218a5 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ble.. */.
218a6 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d 3d if( p->pSelect==
218a7 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 0 ){. /* A
218a8 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20 2a 2f regular table */
218a9 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 . zType = "
218aa 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a 54 table";. zT
218ab 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22 3b 0a ype2 = "TABLE";.
218ac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
218ad 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 6c MIT_VIEW. }el
218ae 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 76 se{. /* A v
218af 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 iew */. zTy
218b0 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 20 pe = "view";.
218b1 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56 49 45 zType2 = "VIE
218b2 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d W";.#endif. }
218b3 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 .. /* If this
218b4 20 69 73 20 61 20 43 52 45 41 54 45 20 54 41 42 is a CREATE TAB
218b5 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43 54 20 LE xx AS SELECT
218b6 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68 65 ..., execute the
218b7 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 73 SELECT. ** s
218b8 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 75 tatement to popu
218b9 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74 61 62 late the new tab
218ba 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 le. The root-pag
218bb 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 e number for the
218bc 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 6c . ** new tabl
218bd 65 20 69 73 20 69 6e 20 72 65 67 69 73 74 65 72 e is in register
218be 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 pParse->regRoot
218bf 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
218c0 4f 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54 20 Once the SELECT
218c1 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20 62 has been coded b
218c2 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 y sqlite3Select(
218c3 29 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20 20 ), it is in a.
218c4 20 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73 74 ** suitable st
218c5 61 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f 72 ate to query for
218c6 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 the column name
218c7 73 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20 62 s and types to b
218c8 65 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62 79 e used. ** by
218c9 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a the new table..
218ca 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 **. ** A
218cb 73 68 61 72 65 64 2d 63 61 63 68 65 20 77 72 69 shared-cache wri
218cc 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 te-lock is not r
218cd 65 71 75 69 72 65 64 20 74 6f 20 77 72 69 74 65 equired to write
218ce 20 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62 6c to the new tabl
218cf 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 73 e,. ** as a s
218d0 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74 20 chema-lock must
218d1 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 have already bee
218d2 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63 72 n obtained to cr
218d3 65 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a 20 eate it. Since.
218d4 20 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d 6c ** a schema-l
218d5 6f 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c 6c ock excludes all
218d6 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 other database
218d7 75 73 65 72 73 2c 20 74 68 65 20 77 72 69 74 65 users, the write
218d8 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20 20 -lock would.
218d9 2a 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74 2e ** be redundant.
218da 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
218db 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 pSelect ){.
218dc 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 SelectDest dest
218dd 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 ;. Table *p
218de 53 65 6c 54 61 62 3b 0a 0a 20 20 20 20 20 20 61 SelTab;.. a
218df 73 73 65 72 74 28 70 50 61 72 73 65 2d 3e 6e 54 ssert(pParse->nT
218e0 61 62 3d 3d 31 29 3b 0a 20 20 20 20 20 20 73 71 ab==1);. sq
218e1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
218e2 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c v, OP_OpenWrite,
218e3 20 31 2c 20 70 50 61 72 73 65 2d 3e 72 65 67 52 1, pParse->regR
218e4 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 oot, iDb);.
218e5 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
218e6 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 20 20 geP5(v, 1);.
218e7 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d pParse->nTab =
218e8 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2;. sqlite
218e9 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 3SelectDestInit(
218ea 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65 &dest, SRT_Table
218eb 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , 1);. sqli
218ec 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 te3Select(pParse
218ed 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 , pSelect, &dest
218ee 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
218ef 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
218f0 5f 43 6c 6f 73 65 2c 20 31 29 3b 0a 20 20 20 20 _Close, 1);.
218f1 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 if( pParse->nE
218f2 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 rr==0 ){.
218f3 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 pSelTab = sqlit
218f4 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c e3ResultSetOfSel
218f5 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c ect(pParse, pSel
218f6 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 ect);. if
218f7 28 20 70 53 65 6c 54 61 62 3d 3d 30 20 29 20 72 ( pSelTab==0 ) r
218f8 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 61 eturn;. a
218f9 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d ssert( p->aCol==
218fa 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 0 );. p->
218fb 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e nCol = pSelTab->
218fc 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d nCol;. p-
218fd 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d >aCol = pSelTab-
218fe 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 >aCol;. p
218ff 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 SelTab->nCol = 0
21900 3b 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 ;. pSelTa
21901 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 b->aCol = 0;.
21902 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 sqlite3Dele
21903 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29 teTable(pSelTab)
21904 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
21905 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 . /* Compute
21906 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 the complete tex
21907 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 t of the CREATE
21908 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 statement */.
21909 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a if( pSelect ){.
2190a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 72 zStmt = cr
2190b 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28 64 62 eateTableStmt(db
2190c 2c 20 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , p);. }else{
2190d 0a 20 20 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 . n = (int)
2190e 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 73 (pEnd->z - pPars
2190f 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 e->sNameToken.z)
21910 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74 6d + 1;. zStm
21911 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e t = sqlite3MPrin
21912 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 tf(db, .
21913 20 20 22 43 52 45 41 54 45 20 25 73 20 25 2e 2a "CREATE %s %.*
21914 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 70 s", zType2, n, p
21915 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 Parse->sNameToke
21916 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 n.z. );.
21917 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f }.. /* A slo
21918 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 t for the record
21919 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
2191a 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74 n allocated in t
2191b 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 he . ** SQLIT
2191c 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 E_MASTER table.
2191d 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f We just need to
2191e 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f update that slo
2191f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a t with all. *
21920 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f * the informatio
21921 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65 n we've collecte
21922 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 d.. */. sq
21923 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
21924 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 (pParse,. "
21925 55 50 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20 UPDATE %Q.%s ".
21926 20 20 20 20 20 20 20 20 22 53 45 54 20 74 79 70 "SET typ
21927 65 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25 51 2c e='%s', name=%Q,
21928 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f tbl_name=%Q, ro
21929 6f 74 70 61 67 65 3d 23 25 64 2c 20 73 71 6c 3d otpage=#%d, sql=
2192a 25 51 20 22 0a 20 20 20 20 20 20 20 22 57 48 45 %Q ". "WHE
2192b 52 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a 20 RE rowid=#%d",.
2192c 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 db->aDb[iDb
2192d 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f ].zName, SCHEMA_
2192e 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 TABLE(iDb),.
2192f 20 20 7a 54 79 70 65 2c 0a 20 20 20 20 20 20 70 zType,. p
21930 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 ->zName,. p
21931 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 ->zName,. p
21932 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a Parse->regRoot,.
21933 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20 20 zStmt,.
21934 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f pParse->regRo
21935 77 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 wid. );. s
21936 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
21937 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c zStmt);. sql
21938 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 ite3ChangeCookie
21939 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a (pParse, iDb);..
2193a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
2193b 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e MIT_AUTOINCREMEN
2193c 54 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 T. /* Check t
2193d 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 65 64 o see if we need
2193e 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 73 71 to create an sq
2193f 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 lite_sequence ta
21940 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a 20 6b ble for. ** k
21941 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 eeping track of
21942 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6b 65 autoincrement ke
21943 79 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ys.. */. i
21944 66 28 20 70 2d 3e 74 61 62 46 6c 61 67 73 20 26 f( p->tabFlags &
21945 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e TF_Autoincremen
21946 74 20 29 7b 0a 20 20 20 20 20 20 44 62 20 2a 70 t ){. Db *p
21947 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 Db = &db->aDb[iD
21948 62 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 b];. if( pD
21949 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 b->pSchema->pSeq
2194a 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Tab==0 ){.
2194b 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 sqlite3NestedP
2194c 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 arse(pParse,.
2194d 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54 "CREATE T
2194e 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 ABLE %Q.sqlite_s
2194f 65 71 75 65 6e 63 65 28 6e 61 6d 65 2c 73 65 71 equence(name,seq
21950 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 44 )",. pD
21951 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 20 b->zName.
21952 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 );. }.
21953 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a }.#endif.. /*
21954 20 52 65 70 61 72 73 65 20 65 76 65 72 79 74 68 Reparse everyth
21955 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20 6f 75 ing to update ou
21956 72 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20 r internal data
21957 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 20 20 structures */.
21958 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21959 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 Op4(v, OP_ParseS
2195a 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 chema, iDb, 0, 0
2195b 2c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ,. sqlite
2195c 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 62 3MPrintf(db, "tb
2195d 6c 5f 6e 61 6d 65 3d 27 25 71 27 22 2c 70 2d 3e l_name='%q'",p->
2195e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d zName), P4_DYNAM
2195f 49 43 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 IC);. }... /*
21960 41 64 64 20 74 68 65 20 74 61 62 6c 65 20 74 6f Add the table to
21961 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 the in-memory r
21962 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
21963 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 the database..
21964 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e */. if( db->in
21965 69 74 2e 62 75 73 79 20 26 26 20 70 50 61 72 73 it.busy && pPars
21966 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 e->nErr==0 ){.
21967 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 Table *pOld;.
21968 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 FKey *pFKey;
21969 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 . Schema *pSc
2196a 68 65 6d 61 20 3d 20 70 2d 3e 70 53 63 68 65 6d hema = p->pSchem
2196b 61 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 a;. pOld = sq
2196c 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 lite3HashInsert(
2196d 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 &pSchema->tblHas
2196e 68 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 h, p->zName,.
2196f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21970 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
21971 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 7a 4e 61 3Strlen30(p->zNa
21972 6d 65 29 2b 31 2c 70 29 3b 0a 20 20 20 20 69 66 me)+1,p);. if
21973 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 ( pOld ){.
21974 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 20 assert( p==pOld
21975 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 ); /* Malloc mu
21976 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 69 st have failed i
21977 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 74 nside HashInsert
21978 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e () */. db->
21979 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
2197a 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
2197b 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
2197c 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 LITE_OMIT_FOREIG
2197d 4e 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70 46 N_KEY. for(pF
2197e 4b 65 79 3d 70 2d 3e 70 46 4b 65 79 3b 20 70 46 Key=p->pFKey; pF
2197f 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79 Key; pFKey=pFKey
21980 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 ->pNextFrom){.
21981 20 20 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 0a void *data;.
21982 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20 int nTo =
21983 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
21984 70 46 4b 65 79 2d 3e 7a 54 6f 29 20 2b 20 31 3b pFKey->zTo) + 1;
21985 0a 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 70 4e . pFKey->pN
21986 65 78 74 54 6f 20 3d 20 73 71 6c 69 74 65 33 48 extTo = sqlite3H
21987 61 73 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61 ashFind(&pSchema
21988 2d 3e 61 46 4b 65 79 2c 20 70 46 4b 65 79 2d 3e ->aFKey, pFKey->
21989 7a 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20 20 20 zTo, nTo);.
2198a 20 64 61 74 61 20 3d 20 73 71 6c 69 74 65 33 48 data = sqlite3H
2198b 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68 65 ashInsert(&pSche
2198c 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 4b 65 79 ma->aFKey, pFKey
2198d 2d 3e 7a 54 6f 2c 20 6e 54 6f 2c 20 70 46 4b 65 ->zTo, nTo, pFKe
2198e 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 61 y);. if( da
2198f 74 61 3d 3d 28 76 6f 69 64 20 2a 29 70 46 4b 65 ta==(void *)pFKe
21990 79 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d y ){. db-
21991 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
21992 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
21993 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61 72 .#endif. pPar
21994 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 se->pNewTable =
21995 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 54 61 62 6c 0;. db->nTabl
21996 65 2b 2b 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 e++;. db->fla
21997 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 gs |= SQLITE_Int
21998 65 72 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66 ernChanges;..#if
21999 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
2199a 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 20 _ALTERTABLE.
2199b 69 66 28 20 21 70 2d 3e 70 53 65 6c 65 63 74 20 if( !p->pSelect
2199c 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 ){. const c
2199d 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f har *zName = (co
2199e 6e 73 74 20 63 68 61 72 20 2a 29 70 50 61 72 73 nst char *)pPars
2199f 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b e->sNameToken.z;
219a0 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 . int nName
219a1 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
219a2 21 70 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e !pSelect && pCon
219a3 73 20 26 26 20 70 45 6e 64 20 29 3b 0a 20 20 20 s && pEnd );.
219a4 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d if( pCons->z=
219a5 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 =0 ){. pC
219a6 6f 6e 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20 ons = pEnd;.
219a7 20 20 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 }. nName
219a8 3d 20 28 69 6e 74 29 28 28 63 6f 6e 73 74 20 63 = (int)((const c
219a9 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d har *)pCons->z -
219aa 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 zName);. p
219ab 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d ->addColOffset =
219ac 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66 13 + sqlite3Utf
219ad 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 8CharLen(zName,
219ae 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65 nName);. }.#e
219af 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e ndif. }.}..#ifn
219b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
219b1 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 VIEW./*.** The p
219b2 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 arser calls this
219b3 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 routine in orde
219b4 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 r to create a ne
219b5 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45 w VIEW.*/.SQLITE
219b6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
219b7 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28 lite3CreateView(
219b8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
219b9 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 , /* The par
219ba 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
219bb 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c Token *pBegin,
219bc 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 /* The CREA
219bd 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 TE token that be
219be 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65 gins the stateme
219bf 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 nt */. Token *p
219c0 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68 Name1, /* Th
219c1 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c e token that hol
219c2 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 ds the name of t
219c3 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b he view */. Tok
219c4 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 en *pName2,
219c5 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61 /* The token tha
219c6 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 t holds the name
219c7 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a of the view */.
219c8 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 Select *pSelec
219c9 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 t, /* A SELECT
219ca 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 statement that
219cb 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 will become the
219cc 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e new view */. in
219cd 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20 t isTemp,
219ce 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54 /* TRUE for a T
219cf 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f EMPORARY view */
219d0 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 . int noErr
219d1 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73 /* Suppres
219d2 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 s error messages
219d3 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 79 if VIEW already
219d4 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 exists */.){.
219d5 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 Table *p;. int
219d6 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 n;. const unsig
219d7 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54 ned char *z;. T
219d8 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46 oken sEnd;. DbF
219d9 69 78 65 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b ixer sFix;. Tok
219da 65 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e 74 en *pName;. int
219db 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 iDb;. sqlite3
219dc 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
219dd 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d ;.. if( pParse-
219de 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 73 >nVar>0 ){. s
219df 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
219e0 50 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74 65 Parse, "paramete
219e1 72 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 rs are not allow
219e2 65 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a 20 ed in views");.
219e3 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
219e4 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 Delete(db, pSele
219e5 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b ct);. return;
219e6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 . }. sqlite3St
219e7 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c artTable(pParse,
219e8 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c pName1, pName2,
219e9 20 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e isTemp, 1, 0, n
219ea 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 61 oErr);. p = pPa
219eb 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
219ec 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50 if( p==0 || pP
219ed 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 arse->nErr ){.
219ee 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
219ef 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 elete(db, pSelec
219f0 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a t);. return;.
219f1 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 54 77 6f }. sqlite3Two
219f2 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c PartName(pParse,
219f3 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c pName1, pName2,
219f4 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20 &pName);. iDb
219f5 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
219f6 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 oIndex(db, p->pS
219f7 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 73 71 chema);. if( sq
219f8 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 lite3FixInit(&sF
219f9 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c ix, pParse, iDb,
219fa 20 22 76 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a "view", pName).
219fb 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69 && sqlite3Fi
219fc 78 53 65 6c 65 63 74 28 26 73 46 69 78 2c 20 70 xSelect(&sFix, p
219fd 53 65 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20 20 Select). ){.
219fe 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
219ff 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 lete(db, pSelect
21a00 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
21a01 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20 }.. /* Make a
21a02 63 6f 70 79 20 6f 66 20 74 68 65 20 65 6e 74 69 copy of the enti
21a03 72 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d re SELECT statem
21a04 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 ent that defines
21a05 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2a 20 the view.. **
21a06 54 68 69 73 20 77 69 6c 6c 20 66 6f 72 63 65 20 This will force
21a07 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b all the Expr.tok
21a08 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20 62 en.z values to b
21a09 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 e dynamically.
21a0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 72 61 74 ** allocated rat
21a0b 68 65 72 20 74 68 61 6e 20 70 6f 69 6e 74 20 74 her than point t
21a0c 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 o the input stri
21a0d 6e 67 20 2d 20 77 68 69 63 68 20 6d 65 61 6e 73 ng - which means
21a0e 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 20 that. ** they
21a0f 77 69 6c 6c 20 70 65 72 73 69 73 74 20 61 66 74 will persist aft
21a10 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 er the current s
21a11 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 qlite3_exec() ca
21a12 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f ll returns.. */
21a13 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 . p->pSelect =
21a14 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 sqlite3SelectDup
21a15 28 64 62 2c 20 70 53 65 6c 65 63 74 2c 20 45 58 (db, pSelect, EX
21a16 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 PRDUP_REDUCE);.
21a17 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
21a18 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 lete(db, pSelect
21a19 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c );. if( db->mal
21a1a 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
21a1b 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
21a1c 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 f( !db->init.bus
21a1d 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 y ){. sqlite3
21a1e 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d ViewGetColumnNam
21a1f 65 73 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 es(pParse, p);.
21a20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 }.. /* Locate
21a21 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 the end of the C
21a22 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65 REATE VIEW state
21a23 6d 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64 ment. Make sEnd
21a24 20 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 point to. ** t
21a25 68 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 he end.. */. s
21a26 45 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c End = pParse->sL
21a27 61 73 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 astToken;. if(
21a28 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30 20 26 26 20 sEnd.z[0]!=0 &&
21a29 73 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 sEnd.z[0]!=';' )
21a2a 7b 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 {. sEnd.z +=
21a2b 73 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 sEnd.n;. }. sE
21a2c 6e 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 nd.n = 0;. n =
21a2d 28 69 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20 70 (int)(sEnd.z - p
21a2e 42 65 67 69 6e 2d 3e 7a 29 3b 0a 20 20 7a 20 3d Begin->z);. z =
21a2f 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 (const unsigned
21a30 20 63 68 61 72 2a 29 70 42 65 67 69 6e 2d 3e 7a char*)pBegin->z
21a31 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 26 ;. while( n>0 &
21a32 26 20 28 7a 5b 6e 2d 31 5d 3d 3d 27 3b 27 20 7c & (z[n-1]==';' |
21a33 7c 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 | sqlite3Isspace
21a34 28 7a 5b 6e 2d 31 5d 29 29 20 29 7b 20 6e 2d 2d (z[n-1])) ){ n--
21a35 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20 26 ; }. sEnd.z = &
21a36 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 2e 6e z[n-1];. sEnd.n
21a37 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 = 1;.. /* Use
21a38 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 sqlite3EndTable(
21a39 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 69 65 ) to add the vie
21a3a 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 45 5f w to the SQLITE_
21a3b 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a 2f 0a MASTER table */.
21a3c 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c sqlite3EndTabl
21a3d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 73 45 e(pParse, 0, &sE
21a3e 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e nd, 0);. return
21a3f 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
21a40 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a LITE_OMIT_VIEW *
21a41 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 /..#if !defined(
21a42 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
21a43 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 ) || !defined(SQ
21a44 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
21a45 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 68 LTABLE)./*.** Th
21a46 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 e Table structur
21a47 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 61 6c e pTable is real
21a48 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c 6c ly a VIEW. Fill
21a49 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 in the names of
21a4a 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 .** the columns
21a4b 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e 20 74 of the view in t
21a4c 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 63 74 he pTable struct
21a4d 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 ure. Return the
21a4e 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65 72 number.** of er
21a4f 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72 72 rors. If an err
21a50 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76 65 or is seen leave
21a51 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
21a52 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 e in pParse->zEr
21a53 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rMsg..*/.SQLITE_
21a54 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
21a55 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e te3ViewGetColumn
21a56 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50 61 Names(Parse *pPa
21a57 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 rse, Table *pTab
21a58 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 53 le){. Table *pS
21a59 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66 61 elTab; /* A fa
21a5a 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 ke table from wh
21a5b 69 63 68 20 77 65 20 67 65 74 20 74 68 65 20 72 ich we get the r
21a5c 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 53 esult set */. S
21a5d 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 20 elect *pSel;
21a5e 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65 20 /* Copy of the
21a5f 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70 6c SELECT that impl
21a60 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 20 ements the view
21a61 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 */. int nErr =
21a62 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 0; /* Number
21a63 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 of errors encou
21a64 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 ntered */. int
21a65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a n; /*
21a66 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 6f 6c Temporarily hol
21a67 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ds the number of
21a68 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 6e 65 cursors assigne
21a69 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a d */. sqlite3 *
21a6a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
21a6b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f /* Database co
21a6c 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d 61 6c nnection for mal
21a6d 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 loc errors */.
21a6e 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 int (*xAuth)(voi
21a6f 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 d*,int,const cha
21a70 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 r*,const char*,c
21a71 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
21a72 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73 73 65 char*);.. asse
21a73 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 0a 23 rt( pTable );..#
21a74 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
21a75 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
21a76 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 74 61 if( sqlite3Vta
21a77 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50 61 bCallConnect(pPa
21a78 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29 7b 0a rse, pTable) ){.
21a79 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
21a7a 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69 E_ERROR;. }. i
21a7b 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 f( IsVirtual(pTa
21a7c 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20 30 3b ble) ) return 0;
21a7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
21a7e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 SQLITE_OMIT_VIE
21a7f 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 69 76 W. /* A positiv
21a80 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68 65 e nCol means the
21a81 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 66 columns names f
21a82 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 72 65 or this view are
21a83 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6b 6e . ** already kn
21a84 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 own.. */. if(
21a85 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 20 29 pTable->nCol>0 )
21a86 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a return 0;.. /*
21a87 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c A negative nCol
21a88 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 6d 61 is a special ma
21a89 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 68 61 rker meaning tha
21a8a 74 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 t we are current
21a8b 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 ly. ** trying t
21a8c 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 63 6f o compute the co
21a8d 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 66 20 lumn names. If
21a8e 77 65 20 65 6e 74 65 72 20 74 68 69 73 20 72 6f we enter this ro
21a8f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a 2a 20 utine with. **
21a90 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c 2c a negative nCol,
21a91 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 6f 72 it means two or
21a92 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f 72 6d more views form
21a93 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 74 68 a loop, like th
21a94 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 is:. **. **
21a95 20 20 43 52 45 41 54 45 20 56 49 45 57 20 6f 6e CREATE VIEW on
21a96 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 e AS SELECT * FR
21a97 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 20 20 OM two;. **
21a98 20 43 52 45 41 54 45 20 56 49 45 57 20 74 77 6f CREATE VIEW two
21a99 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f AS SELECT * FRO
21a9a 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a M one;. **. **
21a9b 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 69 73 20 Actually, this
21a9c 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74 20 error is caught
21a9d 70 72 65 76 69 6f 75 73 6c 79 20 61 6e 64 20 73 previously and s
21a9e 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
21a9f 74 65 73 74 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 test. ** should
21aa0 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 42 always fail. B
21aa1 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 ut we will leave
21aa2 20 69 74 20 69 6e 20 70 6c 61 63 65 20 6a 75 73 it in place jus
21aa3 74 20 74 6f 20 62 65 20 73 61 66 65 2e 0a 20 20 t to be safe..
21aa4 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d */. if( pTable-
21aa5 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 73 >nCol<0 ){. s
21aa6 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
21aa7 50 61 72 73 65 2c 20 22 76 69 65 77 20 25 73 20 Parse, "view %s
21aa8 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20 64 65 is circularly de
21aa9 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 2d 3e fined", pTable->
21aaa 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 zName);. retu
21aab 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 rn 1;. }. asse
21aac 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c rt( pTable->nCol
21aad 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 >=0 );.. /* If
21aae 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c we get this far,
21aaf 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e 65 65 it means we nee
21ab0 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 d to compute the
21ab1 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a 20 20 table names..
21ab2 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
21ab3 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
21ab4 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 ResultSetOfSelec
21ab5 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e 64 20 t() will expand
21ab6 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 6c 65 any. ** "*" ele
21ab7 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72 65 73 ments in the res
21ab8 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68 65 20 ults set of the
21ab9 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 61 73 view and will as
21aba 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 20 2a sign cursors. *
21abb 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e 74 * to the element
21abc 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c s of the FROM cl
21abd 61 75 73 65 2e 20 20 42 75 74 20 77 65 20 64 6f ause. But we do
21abe 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 65 20 not want these
21abf 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20 changes. ** to
21ac0 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20 53 be permanent. S
21ac1 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f o the computatio
21ac2 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20 63 n is done on a c
21ac3 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 opy of the SELEC
21ac4 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 T. ** statement
21ac5 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
21ac6 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 20 61 e view.. */. a
21ac7 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70 ssert( pTable->p
21ac8 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 65 6c Select );. pSel
21ac9 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 = sqlite3Select
21aca 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e Dup(db, pTable->
21acb 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69 pSelect, 0);. i
21acc 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 75 f( pSel ){. u
21acd 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 8 enableLookasid
21ace 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 e = db->lookasid
21acf 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20 20 e.bEnabled;.
21ad0 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 n = pParse->nTab
21ad1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 ;. sqlite3Src
21ad2 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 ListAssignCursor
21ad3 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e s(pParse, pSel->
21ad4 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c pSrc);. pTabl
21ad5 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 e->nCol = -1;.
21ad6 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e db->lookaside.
21ad7 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23 69 bEnabled = 0;.#i
21ad8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
21ad9 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a T_AUTHORIZATION.
21ada 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e xAuth = db->
21adb 78 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 xAuth;. db->x
21adc 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53 Auth = 0;. pS
21add 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 elTab = sqlite3R
21ade 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 esultSetOfSelect
21adf 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a (pParse, pSel);.
21ae0 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 db->xAuth =
21ae1 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20 xAuth;.#else.
21ae2 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 pSelTab = sqlit
21ae3 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c e3ResultSetOfSel
21ae4 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c ect(pParse, pSel
21ae5 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 62 );.#endif. db
21ae6 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 ->lookaside.bEna
21ae7 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f bled = enableLoo
21ae8 6b 61 73 69 64 65 3b 0a 20 20 20 20 70 50 61 72 kaside;. pPar
21ae9 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 20 se->nTab = n;.
21aea 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29 7b if( pSelTab ){
21aeb 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
21aec 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29 Table->aCol==0 )
21aed 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e ;. pTable->
21aee 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e nCol = pSelTab->
21aef 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 62 nCol;. pTab
21af0 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 le->aCol = pSelT
21af1 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 ab->aCol;.
21af2 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 pSelTab->nCol =
21af3 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 0;. pSelTab
21af4 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 ->aCol = 0;.
21af5 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 sqlite3DeleteT
21af6 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a 20 able(pSelTab);.
21af7 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 pTable->pSc
21af8 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 44 hema->flags |= D
21af9 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0a B_UnresetViews;.
21afa 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21afb 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 pTable->nCol =
21afc 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0;. nErr++;
21afd 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
21afe 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 e3SelectDelete(d
21aff 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65 6c b, pSel);. } el
21b00 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b se {. nErr++;
21b01 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
21b02 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 QLITE_OMIT_VIEW
21b03 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 */. return nErr
21b04 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ; .}.#endif /*
21b05 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
21b06 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 OMIT_VIEW) || !d
21b07 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
21b08 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 IT_VIRTUALTABLE)
21b09 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
21b0a 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a ITE_OMIT_VIEW./*
21b0b 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f .** Clear the co
21b0c 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 lumn names from
21b0d 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61 every VIEW in da
21b0e 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73 tabase idx..*/.s
21b0f 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
21b10 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71 eViewResetAll(sq
21b11 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 lite3 *db, int i
21b12 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 dx){. HashElem
21b13 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73 *i;. if( !DbHas
21b14 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78 Property(db, idx
21b15 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 , DB_UnresetView
21b16 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 s) ) return;. f
21b17 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 or(i=sqliteHashF
21b18 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64 irst(&db->aDb[id
21b19 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 x].pSchema->tblH
21b1a 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65 ash); i;i=sqlite
21b1b 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 HashNext(i)){.
21b1c 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
21b1d 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 sqliteHashData(i
21b1e 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d );. if( pTab-
21b1f 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 >pSelect ){.
21b20 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c sqliteResetCol
21b21 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b 0a umnNames(pTab);.
21b22 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c }. }. DbCl
21b23 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20 earProperty(db,
21b24 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 idx, DB_UnresetV
21b25 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 iews);.}.#else.#
21b26 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69 define sqliteVi
21b27 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a ewResetAll(A,B).
21b28 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
21b29 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f _OMIT_VIEW */../
21b2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
21b2b 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 on is called by
21b2c 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75 the VDBE to adju
21b2d 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 st the internal
21b2e 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62 schema.** used b
21b2f 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68 y SQLite when th
21b30 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f e btree layer mo
21b31 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74 ves a table root
21b32 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f page. The.** ro
21b33 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62 ot-page of a tab
21b34 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64 le or index in d
21b35 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20 atabase iDb has
21b36 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72 changed from iFr
21b37 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a om.** to iTo..**
21b38 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38 .** Ticket #1728
21b39 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61 : The symbol ta
21b3a 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 ble might still
21b3b 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74 contain informat
21b3c 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73 ion.** on tables
21b3d 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20 and/or indices
21b3e 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f that are the pro
21b3f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65 cess of being de
21b40 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75 leted..** If you
21b41 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e are unlucky, on
21b42 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74 e of those delet
21b43 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61 ed indices or ta
21b44 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61 bles might.** ha
21b45 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 ve the same root
21b46 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74 page number as t
21b47 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72 he real table or
21b48 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a index that is.*
21b49 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20 * being moved.
21b4a 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f So we cannot sto
21b4b 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65 p searching afte
21b4c 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 r the first matc
21b4d 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68 h .** because th
21b4e 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69 e first match mi
21b4f 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f ght be for one o
21b50 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e f the deleted in
21b51 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c dices.** or tabl
21b52 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74 es and not the t
21b53 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20 able/index that
21b54 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e is actually bein
21b55 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d g moved..** We m
21b56 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f ust continue loo
21b57 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74 ping until all t
21b58 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 ables and indice
21b59 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61 s with.** rootpa
21b5a 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62 ge==iFrom have b
21b5b 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f een converted to
21b5c 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65 have a rootpage
21b5d 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72 of iTo.** in or
21b5e 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69 der to be certai
21b5f 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68 n that we got th
21b60 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a e right one..*/.
21b61 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
21b62 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 53 MIT_AUTOVACUUM.S
21b63 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
21b64 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 id sqlite3RootPa
21b65 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 2c geMoved(Db *pDb,
21b66 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 int iFrom, int
21b67 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d iTo){. HashElem
21b68 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20 *pElem;. Hash
21b69 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 68 *pHash;.. pHash
21b6a 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 = &pDb->pSchema
21b6b 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72 ->tblHash;. for
21b6c 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 (pElem=sqliteHas
21b6d 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70 hFirst(pHash); p
21b6e 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 Elem; pElem=sqli
21b6f 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d teHashNext(pElem
21b70 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 )){. Table *p
21b71 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 Tab = sqliteHash
21b72 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 Data(pElem);.
21b73 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d if( pTab->tnum=
21b74 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 =iFrom ){.
21b75 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f pTab->tnum = iTo
21b76 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48 ;. }. }. pH
21b77 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 ash = &pDb->pSch
21b78 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20 ema->idxHash;.
21b79 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 for(pElem=sqlite
21b7a 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29 HashFirst(pHash)
21b7b 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 ; pElem; pElem=s
21b7c 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 qliteHashNext(pE
21b7d 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 lem)){. Index
21b7e 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48 *pIdx = sqliteH
21b7f 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a ashData(pElem);.
21b80 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e if( pIdx->tn
21b81 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 um==iFrom ){.
21b82 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20 pIdx->tnum =
21b83 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d iTo;. }. }.}
21b84 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
21b85 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61 rite code to era
21b86 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 se the table wit
21b87 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 h root-page iTab
21b88 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 le from database
21b89 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72 iDb..** Also wr
21b8a 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69 ite code to modi
21b8b 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 fy the sqlite_ma
21b8c 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69 ster table and i
21b8d 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a nternal schema.*
21b8e 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65 * if a root-page
21b8f 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c of another tabl
21b90 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68 e is moved by th
21b91 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68 e btree-layer wh
21b92 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20 ilst.** erasing
21b93 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e iTable (this can
21b94 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 happen with an
21b95 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
21b96 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 base)..*/ .stati
21b97 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f c void destroyRo
21b98 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50 otPage(Parse *pP
21b99 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65 arse, int iTable
21b9a 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 , int iDb){. Vd
21b9b 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 be *v = sqlite3G
21b9c 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
21b9d 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 int r1 = sqlit
21b9e 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
21b9f 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 rse);. sqlite3V
21ba0 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
21ba1 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c Destroy, iTable,
21ba2 20 72 31 2c 20 69 44 62 29 3b 0a 23 69 66 6e 64 r1, iDb);.#ifnd
21ba3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
21ba4 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 4f UTOVACUUM. /* O
21ba5 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65 73 P_Destroy stores
21ba6 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20 72 an in integer r
21ba7 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65 67 1. If this integ
21ba8 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d 7a er. ** is non-z
21ba9 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73 20 ero, then it is
21baa 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 the root page nu
21bab 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65 20 mber of a table
21bac 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c 6f moved to. ** lo
21bad 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20 54 cation iTable. T
21bae 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 he following cod
21baf 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20 73 e modifies the s
21bb0 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
21bb1 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c 65 le to. ** refle
21bb2 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20 20 ct this.. **.
21bb3 2a 2a 20 54 68 65 20 22 23 4e 4e 4e 22 20 69 6e ** The "#NNN" in
21bb4 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 73 70 the SQL is a sp
21bb5 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 74 ecial constant t
21bb6 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65 76 hat means whatev
21bb7 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69 73 er value. ** is
21bb8 20 69 6e 20 72 65 67 69 73 74 65 72 20 4e 4e 4e in register NNN
21bb9 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 52 65 . See sqlite3Re
21bba 67 69 73 74 65 72 45 78 70 72 28 29 2e 0a 20 20 gisterExpr()..
21bbb 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 */. sqlite3Nest
21bbc 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 edParse(pParse,
21bbd 0a 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 . "UPDATE %Q
21bbe 2e 25 73 20 53 45 54 20 72 6f 6f 74 70 61 67 65 .%s SET rootpage
21bbf 3d 25 64 20 57 48 45 52 45 20 23 25 64 20 41 4e =%d WHERE #%d AN
21bc0 44 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 22 2c D rootpage=#%d",
21bc1 0a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 . pParse->db
21bc2 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
21bc3 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 , SCHEMA_TABLE(i
21bc4 44 62 29 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c Db), iTable, r1,
21bc5 20 72 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 r1);.#endif. s
21bc6 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
21bc7 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 pReg(pParse, r1)
21bc8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
21bc9 20 56 44 42 45 20 63 6f 64 65 20 74 6f 20 65 72 VDBE code to er
21bca 61 73 65 20 74 61 62 6c 65 20 70 54 61 62 20 61 ase table pTab a
21bcb 6e 64 20 61 6c 6c 20 61 73 73 6f 63 69 61 74 65 nd all associate
21bcc 64 20 69 6e 64 69 63 65 73 20 6f 6e 20 64 69 73 d indices on dis
21bcd 6b 2e 0a 2a 2a 20 43 6f 64 65 20 74 6f 20 75 70 k..** Code to up
21bce 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f date the sqlite_
21bcf 6d 61 73 74 65 72 20 74 61 62 6c 65 73 20 61 6e master tables an
21bd0 64 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d d internal schem
21bd1 61 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a a definitions.**
21bd2 20 69 6e 20 63 61 73 65 20 61 20 72 6f 6f 74 2d in case a root-
21bd3 70 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74 page belonging t
21bd4 6f 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 o another table
21bd5 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68 65 20 is moved by the
21bd6 62 74 72 65 65 20 6c 61 79 65 72 0a 2a 2a 20 69 btree layer.** i
21bd7 73 20 61 6c 73 6f 20 61 64 64 65 64 20 28 74 68 s also added (th
21bd8 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69 is can happen wi
21bd9 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 th an auto-vacuu
21bda 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a m database)..*/.
21bdb 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 73 74 static void dest
21bdc 72 6f 79 54 61 62 6c 65 28 50 61 72 73 65 20 2a royTable(Parse *
21bdd 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 pParse, Table *p
21bde 54 61 62 29 7b 0a 23 69 66 64 65 66 20 53 51 4c Tab){.#ifdef SQL
21bdf 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
21be0 55 55 4d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 UUM. Index *pId
21be1 78 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 73 x;. int iDb = s
21be2 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
21be3 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 dex(pParse->db,
21be4 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
21be5 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 destroyRootPag
21be6 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e e(pParse, pTab->
21be7 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 66 6f tnum, iDb);. fo
21be8 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e r(pIdx=pTab->pIn
21be9 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d dex; pIdx; pIdx=
21bea 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 pIdx->pNext){.
21beb 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 destroyRootPag
21bec 65 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e e(pParse, pIdx->
21bed 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d 0a tnum, iDb);. }.
21bee 23 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20 74 68 #else. /* If th
21bef 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 e database may b
21bf0 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 e auto-vacuum ca
21bf1 70 61 62 6c 65 20 28 69 66 20 53 51 4c 49 54 45 pable (if SQLITE
21bf2 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
21bf3 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 . ** is not def
21bf4 69 6e 65 64 29 2c 20 74 68 65 6e 20 69 74 20 69 ined), then it i
21bf5 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 s important to c
21bf6 61 6c 6c 20 4f 50 5f 44 65 73 74 72 6f 79 20 6f all OP_Destroy o
21bf7 6e 20 74 68 65 0a 20 20 2a 2a 20 74 61 62 6c 65 n the. ** table
21bf8 20 61 6e 64 20 69 6e 64 65 78 20 72 6f 6f 74 2d and index root-
21bf9 70 61 67 65 73 20 69 6e 20 6f 72 64 65 72 2c 20 pages in order,
21bfa 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 68 starting with th
21bfb 65 20 6e 75 6d 65 72 69 63 61 6c 6c 79 20 0a 20 e numerically .
21bfc 20 2a 2a 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 ** largest root
21bfd 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 54 68 -page number. Th
21bfe 69 73 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 is guarantees th
21bff 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 72 at none of the r
21c00 6f 6f 74 2d 70 61 67 65 73 0a 20 20 2a 2a 20 74 oot-pages. ** t
21c01 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 69 o be destroyed i
21c02 73 20 72 65 6c 6f 63 61 74 65 64 20 62 79 20 61 s relocated by a
21c03 6e 20 65 61 72 6c 69 65 72 20 4f 50 5f 44 65 73 n earlier OP_Des
21c04 74 72 6f 79 2e 20 69 2e 65 2e 20 69 66 20 74 68 troy. i.e. if th
21c05 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 e. ** following
21c06 20 77 65 72 65 20 63 6f 64 65 64 3a 0a 20 20 2a were coded:. *
21c07 2a 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f *. ** OP_Destro
21c08 79 20 34 20 30 0a 20 20 2a 2a 20 2e 2e 2e 0a 20 y 4 0. ** ....
21c09 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 35 ** OP_Destroy 5
21c0a 20 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 0. **. ** and
21c0b 20 72 6f 6f 74 20 70 61 67 65 20 35 20 68 61 70 root page 5 hap
21c0c 70 65 6e 65 64 20 74 6f 20 62 65 20 74 68 65 20 pened to be the
21c0d 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 largest root-pag
21c0e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a e number in the.
21c0f 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74 ** database, t
21c10 68 65 6e 20 72 6f 6f 74 20 70 61 67 65 20 35 20 hen root page 5
21c11 77 6f 75 6c 64 20 62 65 20 6d 6f 76 65 64 20 74 would be moved t
21c12 6f 20 70 61 67 65 20 34 20 62 79 20 74 68 65 20 o page 4 by the
21c13 0a 20 20 2a 2a 20 22 4f 50 5f 44 65 73 74 72 6f . ** "OP_Destro
21c14 79 20 34 20 30 22 20 6f 70 63 6f 64 65 2e 20 54 y 4 0" opcode. T
21c15 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 22 4f he subsequent "O
21c16 50 5f 44 65 73 74 72 6f 79 20 35 20 30 22 20 77 P_Destroy 5 0" w
21c17 6f 75 6c 64 20 68 69 74 0a 20 20 2a 2a 20 61 20 ould hit. ** a
21c18 66 72 65 65 2d 6c 69 73 74 20 70 61 67 65 2e 0a free-list page..
21c19 20 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 */. int iTab
21c1a 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 0a 20 20 = pTab->tnum;.
21c1b 69 6e 74 20 69 44 65 73 74 72 6f 79 65 64 20 3d int iDestroyed =
21c1c 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20 31 20 0;.. while( 1
21c1d 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 ){. Index *pI
21c1e 64 78 3b 0a 20 20 20 20 69 6e 74 20 69 4c 61 72 dx;. int iLar
21c1f 67 65 73 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69 gest = 0;.. i
21c20 66 28 20 69 44 65 73 74 72 6f 79 65 64 3d 3d 30 f( iDestroyed==0
21c21 20 7c 7c 20 69 54 61 62 3c 69 44 65 73 74 72 6f || iTab<iDestro
21c22 79 65 64 20 29 7b 0a 20 20 20 20 20 20 69 4c 61 yed ){. iLa
21c23 72 67 65 73 74 20 3d 20 69 54 61 62 3b 0a 20 20 rgest = iTab;.
21c24 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 49 64 78 }. for(pIdx
21c25 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
21c26 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
21c27 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e pNext){. in
21c28 74 20 69 49 64 78 20 3d 20 70 49 64 78 2d 3e 74 t iIdx = pIdx->t
21c29 6e 75 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 num;. asser
21c2a 74 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 t( pIdx->pSchema
21c2b 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 ==pTab->pSchema
21c2c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 44 );. if( (iD
21c2d 65 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 28 estroyed==0 || (
21c2e 69 49 64 78 3c 69 44 65 73 74 72 6f 79 65 64 29 iIdx<iDestroyed)
21c2f 29 20 26 26 20 69 49 64 78 3e 69 4c 61 72 67 65 ) && iIdx>iLarge
21c30 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c st ){. iL
21c31 61 72 67 65 73 74 20 3d 20 69 49 64 78 3b 0a 20 argest = iIdx;.
21c32 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
21c33 20 69 66 28 20 69 4c 61 72 67 65 73 74 3d 3d 30 if( iLargest==0
21c34 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
21c35 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
21c36 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c int iDb = sql
21c37 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 ite3SchemaToInde
21c38 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 x(pParse->db, pT
21c39 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 ab->pSchema);.
21c3a 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 destroyRootP
21c3b 61 67 65 28 70 50 61 72 73 65 2c 20 69 4c 61 72 age(pParse, iLar
21c3c 67 65 73 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 gest, iDb);.
21c3d 20 20 69 44 65 73 74 72 6f 79 65 64 20 3d 20 69 iDestroyed = i
21c3e 4c 61 72 67 65 73 74 3b 0a 20 20 20 20 7d 0a 20 Largest;. }.
21c3f 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a }.#endif.}../*.
21c40 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
21c41 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 6f 20 is called to do
21c42 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 20 44 52 the work of a DR
21c43 4f 50 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 OP TABLE stateme
21c44 6e 74 2e 0a 2a 2a 20 70 4e 61 6d 65 20 69 73 20 nt..** pName is
21c45 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
21c46 74 61 62 6c 65 20 74 6f 20 62 65 20 64 72 6f 70 table to be drop
21c47 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ped..*/.SQLITE_P
21c48 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21c49 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 te3DropTable(Par
21c4a 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c se *pParse, SrcL
21c4b 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 ist *pName, int
21c4c 69 73 56 69 65 77 2c 20 69 6e 74 20 6e 6f 45 72 isView, int noEr
21c4d 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 r){. Table *pTa
21c4e 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 b;. Vdbe *v;.
21c4f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
21c50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 arse->db;. int
21c51 69 44 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 iDb;.. if( pPar
21c52 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e se->nErr || db->
21c53 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
21c54 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 goto exit_dr
21c55 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 op_table;. }.
21c56 61 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e assert( pName->n
21c57 53 72 63 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62 Src==1 );. pTab
21c58 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
21c59 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 73 Table(pParse, is
21c5a 56 69 65 77 2c 20 0a 20 20 20 20 20 20 20 20 20 View, .
21c5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c5c 20 20 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a pName->a[0].z
21c5d 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 Name, pName->a[0
21c5e 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 0a 20 ].zDatabase);..
21c5f 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a if( pTab==0 ){.
21c60 20 20 20 20 69 66 28 20 6e 6f 45 72 72 20 29 7b if( noErr ){
21c61 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
21c62 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 rorClear(pParse)
21c63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f ;. }. goto
21c64 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 exit_drop_table
21c65 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 ;. }. iDb = sq
21c66 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
21c67 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 ex(db, pTab->pSc
21c68 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 hema);. assert(
21c69 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 iDb>=0 && iDb<d
21c6a 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 b->nDb );.. /*
21c6b 49 66 20 70 54 61 62 20 69 73 20 61 20 76 69 72 If pTab is a vir
21c6c 74 75 61 6c 20 74 61 62 6c 65 2c 20 63 61 6c 6c tual table, call
21c6d 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 ViewGetColumnNa
21c6e 6d 65 73 28 29 20 74 6f 20 65 6e 73 75 72 65 0a mes() to ensure.
21c6f 20 20 2a 2a 20 69 74 20 69 73 20 69 6e 69 74 69 ** it is initi
21c70 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 alized.. */. i
21c71 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 f( IsVirtual(pTa
21c72 62 29 20 26 26 20 73 71 6c 69 74 65 33 56 69 65 b) && sqlite3Vie
21c73 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 wGetColumnNames(
21c74 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b pParse, pTab) ){
21c75 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 . goto exit_d
21c76 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 rop_table;. }.#
21c77 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
21c78 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
21c79 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 . {. int cod
21c7a 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 e;. const cha
21c7b 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 r *zTab = SCHEMA
21c7c 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 _TABLE(iDb);.
21c7d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
21c7e 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
21c7f 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 zName;. const
21c80 20 63 68 61 72 20 2a 7a 41 72 67 32 20 3d 20 30 char *zArg2 = 0
21c81 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
21c82 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 3AuthCheck(pPars
21c83 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 e, SQLITE_DELETE
21c84 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 29 , zTab, 0, zDb))
21c85 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 {. goto exi
21c86 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 t_drop_table;.
21c87 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69 }. if( isVi
21c88 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ew ){. if(
21c89 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 !OMIT_TEMPDB &&
21c8a 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 iDb==1 ){.
21c8b 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f code = SQLITE_
21c8c 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 3b 0a DROP_TEMP_VIEW;.
21c8d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
21c8e 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 code = SQLI
21c8f 54 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a 20 20 TE_DROP_VIEW;.
21c90 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
21c91 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
21c92 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 LTABLE. }else
21c93 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 if( IsVirtual(p
21c94 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 63 6f Tab) ){. co
21c95 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 de = SQLITE_DROP
21c96 5f 56 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7a _VTABLE;. z
21c97 41 72 67 32 20 3d 20 70 54 61 62 2d 3e 70 4d 6f Arg2 = pTab->pMo
21c98 64 2d 3e 7a 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 d->zName;.#endif
21c99 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
21c9a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 if( !OMIT_TEMP
21c9b 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a DB && iDb==1 ){.
21c9c 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 code = S
21c9d 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f QLITE_DROP_TEMP_
21c9e 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c TABLE;. }el
21c9f 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 se{. code
21ca0 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 = SQLITE_DROP_T
21ca1 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ABLE;. }.
21ca2 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
21ca3 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
21ca4 72 73 65 2c 20 63 6f 64 65 2c 20 70 54 61 62 2d rse, code, pTab-
21ca5 3e 7a 4e 61 6d 65 2c 20 7a 41 72 67 32 2c 20 7a >zName, zArg2, z
21ca6 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 Db) ){. got
21ca7 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c o exit_drop_tabl
21ca8 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 e;. }. if(
21ca9 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
21caa 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 k(pParse, SQLITE
21cab 5f 44 45 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a _DELETE, pTab->z
21cac 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b Name, 0, zDb) ){
21cad 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 . goto exit
21cae 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 _drop_table;.
21caf 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }. }.#endif.
21cb0 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 if( sqlite3StrNI
21cb1 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c Cmp(pTab->zName,
21cb2 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d "sqlite_", 7)==
21cb3 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
21cb4 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
21cb5 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e "table %s may n
21cb6 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 2c 20 ot be dropped",
21cb7 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pTab->zName);.
21cb8 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 goto exit_drop
21cb9 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 _table;. }..#if
21cba 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
21cbb 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e 73 75 72 _VIEW. /* Ensur
21cbc 65 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20 e DROP TABLE is
21cbd 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61 20 76 69 not used on a vi
21cbe 65 77 2c 20 61 6e 64 20 44 52 4f 50 20 56 49 45 ew, and DROP VIE
21cbf 57 20 69 73 20 6e 6f 74 20 75 73 65 64 0a 20 20 W is not used.
21cc0 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65 2e 0a 20 ** on a table..
21cc1 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 */. if( isView
21cc2 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 && pTab->pSelec
21cc3 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 t==0 ){. sqli
21cc4 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
21cc5 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 54 41 se, "use DROP TA
21cc6 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 20 74 61 BLE to delete ta
21cc7 62 6c 65 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a ble %s", pTab->z
21cc8 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Name);. goto
21cc9 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b exit_drop_table;
21cca 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73 56 69 . }. if( !isVi
21ccb 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c ew && pTab->pSel
21ccc 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ect ){. sqlit
21ccd 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
21cce 65 2c 20 22 75 73 65 20 44 52 4f 50 20 56 49 45 e, "use DROP VIE
21ccf 57 20 74 6f 20 64 65 6c 65 74 65 20 76 69 65 77 W to delete view
21cd0 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d %s", pTab->zNam
21cd1 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 e);. goto exi
21cd2 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 t_drop_table;.
21cd3 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 }.#endif.. /* G
21cd4 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
21cd5 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 remove the table
21cd6 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 65 72 from the master
21cd7 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 6e 20 64 table. ** on d
21cd8 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 isk.. */. v =
21cd9 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
21cda 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 Parse);. if( v
21cdb 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72 20 2a ){. Trigger *
21cdc 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20 44 62 pTrigger;. Db
21cdd 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 *pDb = &db->aDb
21cde 5b 69 44 62 5d 3b 0a 20 20 20 20 73 71 6c 69 74 [iDb];. sqlit
21cdf 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 e3BeginWriteOper
21ce0 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c ation(pParse, 1,
21ce1 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 iDb);..#ifndef
21ce2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
21ce3 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 UALTABLE. if(
21ce4 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
21ce5 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 76 20 ){. if( v
21ce6 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
21ce7 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 e3VdbeAddOp0(v,
21ce8 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 OP_VBegin);.
21ce9 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
21cea 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c .. /* Drop al
21ceb 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 l triggers assoc
21cec 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 iated with the t
21ced 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 able being dropp
21cee 65 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20 ed. Code. **
21cef 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 is generated to
21cf0 72 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66 remove entries f
21cf1 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 rom sqlite_maste
21cf2 72 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20 r and/or. **
21cf3 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 sqlite_temp_mast
21cf4 65 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a er if required..
21cf5 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67 */. pTrig
21cf6 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 ger = sqlite3Tri
21cf7 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c ggerList(pParse,
21cf8 20 70 54 61 62 29 3b 0a 20 20 20 20 77 68 69 6c pTab);. whil
21cf9 65 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 e( pTrigger ){.
21cfa 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 assert( pTr
21cfb 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d igger->pSchema==
21cfc 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c pTab->pSchema ||
21cfd 20 0a 20 20 20 20 20 20 20 20 20 20 70 54 72 69 . pTri
21cfe 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 gger->pSchema==d
21cff 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d b->aDb[1].pSchem
21d00 61 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 a );. sqlit
21d01 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 e3DropTriggerPtr
21d02 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 (pParse, pTrigge
21d03 72 29 3b 0a 20 20 20 20 20 20 70 54 72 69 67 67 r);. pTrigg
21d04 65 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 er = pTrigger->p
21d05 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 Next;. }..#if
21d06 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
21d07 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 _AUTOINCREMENT.
21d08 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 /* Remove any
21d09 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 entries of the
21d0a 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 sqlite_sequence
21d0b 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 table associated
21d0c 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 with. ** the
21d0d 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f table being dro
21d0e 70 70 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f pped. This is do
21d0f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 61 ne before the ta
21d10 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 0a 20 ble is dropped.
21d11 20 20 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72 ** at the btr
21d12 65 65 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 ee level, in cas
21d13 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 e the sqlite_seq
21d14 75 65 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64 uence table need
21d15 73 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 s to. ** move
21d16 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 as a result of
21d17 74 68 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61 the drop (can ha
21d18 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 ppen in auto-vac
21d19 75 75 6d 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a uum mode).. *
21d1a 2f 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e /. if( pTab->
21d1b 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 tabFlags & TF_Au
21d1c 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 toincrement ){.
21d1d 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 sqlite3Nest
21d1e 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a edParse(pParse,.
21d1f 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 "DELETE
21d20 46 52 4f 4d 20 25 73 2e 73 71 6c 69 74 65 5f 73 FROM %s.sqlite_s
21d21 65 71 75 65 6e 63 65 20 57 48 45 52 45 20 6e 61 equence WHERE na
21d22 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 me=%Q",.
21d23 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 pDb->zName, pTab
21d24 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b ->zName. );
21d25 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
21d26 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53 /* Drop all S
21d27 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 QLITE_MASTER tab
21d28 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 le and index ent
21d29 72 69 65 73 20 74 68 61 74 20 72 65 66 65 72 20 ries that refer
21d2a 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 to the. ** ta
21d2b 62 6c 65 2e 20 54 68 65 20 70 72 6f 67 72 61 6d ble. The program
21d2c 20 6e 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f name loops thro
21d2d 75 67 68 20 74 68 65 20 6d 61 73 74 65 72 20 74 ugh the master t
21d2e 61 62 6c 65 20 61 6e 64 20 64 65 6c 65 74 65 73 able and deletes
21d2f 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20 72 6f . ** every ro
21d30 77 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f w that refers to
21d31 20 61 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 a table of the
21d32 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 74 68 65 same name as the
21d33 20 6f 6e 65 20 62 65 69 6e 67 0a 20 20 20 20 2a one being. *
21d34 2a 20 64 72 6f 70 70 65 64 2e 20 54 72 69 67 67 * dropped. Trigg
21d35 65 72 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20 ers are handled
21d36 73 65 70 65 72 61 74 65 6c 79 20 62 65 63 61 75 seperately becau
21d37 73 65 20 61 20 74 72 69 67 67 65 72 20 63 61 6e se a trigger can
21d38 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 be. ** creat
21d39 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64 ed in the temp d
21d3a 61 74 61 62 61 73 65 20 74 68 61 74 20 72 65 66 atabase that ref
21d3b 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 20 69 ers to a table i
21d3c 6e 20 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a n another. **
21d3d 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a database.. *
21d3e 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 /. sqlite3Nes
21d3f 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
21d40 20 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 . "DELET
21d41 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 E FROM %Q.%s WHE
21d42 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 RE tbl_name=%Q a
21d43 6e 64 20 74 79 70 65 21 3d 27 74 72 69 67 67 65 nd type!='trigge
21d44 72 27 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62 r'",. pDb
21d45 2d 3e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f ->zName, SCHEMA_
21d46 54 41 42 4c 45 28 69 44 62 29 2c 20 70 54 61 62 TABLE(iDb), pTab
21d47 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f ->zName);.. /
21d48 2a 20 44 72 6f 70 20 61 6e 79 20 73 74 61 74 69 * Drop any stati
21d49 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65 20 73 stics from the s
21d4a 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c qlite_stat1 tabl
21d4b 65 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 20 e, if it exists
21d4c 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
21d4d 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 e3FindTable(db,
21d4e 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 "sqlite_stat1",
21d4f 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 db->aDb[iDb].zNa
21d50 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c me) ){. sql
21d51 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
21d52 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 pParse,.
21d53 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e "DELETE FROM %Q.
21d54 73 71 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45 sqlite_stat1 WHE
21d55 52 45 20 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d RE tbl=%Q", pDb-
21d56 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e >zName, pTab->zN
21d57 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 ame. );.
21d58 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56 }.. if( !isV
21d59 69 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61 iew && !IsVirtua
21d5a 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 l(pTab) ){.
21d5b 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50 destroyTable(pP
21d5c 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 arse, pTab);.
21d5d 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 }.. /* Remov
21d5e 65 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 e the table entr
21d5f 79 20 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20 y from SQLite's
21d60 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 internal schema
21d61 61 6e 64 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a and modify. *
21d62 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f * the schema coo
21d63 6b 69 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 kie.. */.
21d64 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 if( IsVirtual(pT
21d65 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ab) ){. sql
21d66 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
21d67 2c 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 , OP_VDestroy, i
21d68 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e Db, 0, 0, pTab->
21d69 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d zName, 0);. }
21d6a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
21d6b 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f AddOp4(v, OP_Dro
21d6c 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 pTable, iDb, 0,
21d6d 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 0, pTab->zName,
21d6e 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 0);. sqlite3C
21d6f 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 hangeCookie(pPar
21d70 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 se, iDb);. }.
21d71 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 sqliteViewResetA
21d72 6c 6c 28 64 62 2c 20 69 44 62 29 3b 0a 0a 65 78 ll(db, iDb);..ex
21d73 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3a 0a 20 it_drop_table:.
21d74 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 sqlite3SrcListD
21d75 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 elete(db, pName)
21d76 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
21d77 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
21d78 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 d to create a ne
21d79 77 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e w foreign key on
21d7a 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 the table.** cu
21d7b 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f rrently under co
21d7c 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 nstruction. pFr
21d7d 6f 6d 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 omCol determines
21d7e 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a which columns.*
21d7f 2a 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 * in the current
21d80 20 74 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 table point to
21d81 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e the foreign key.
21d82 20 20 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 If pFromCol==0
21d83 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 then.** connect
21d84 20 74 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20 the key to the
21d85 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 last column inse
21d86 72 74 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68 rted. pTo is th
21d87 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 e name of.** the
21d88 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 table referred
21d89 74 6f 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 to. pToCol is a
21d8a 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 list of tables
21d8b 69 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 in the other.**
21d8c 70 54 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74 pTo table that t
21d8d 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 he foreign key p
21d8e 6f 69 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 oints to. flags
21d8f 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a contains all.**
21d90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
21d91 75 74 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 ut the conflict
21d92 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 resolution algor
21d93 69 74 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a ithms specified.
21d94 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c ** in the ON DEL
21d95 45 54 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 ETE, ON UPDATE a
21d96 6e 64 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 nd ON INSERT cla
21d97 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 uses..**.** An F
21d98 4b 65 79 20 73 74 72 75 63 74 75 72 65 20 69 73 Key structure is
21d99 20 63 72 65 61 74 65 64 20 61 6e 64 20 61 64 64 created and add
21d9a 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 ed to the table
21d9b 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 currently.** und
21d9c 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 er construction
21d9d 69 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70 in the pParse->p
21d9e 4e 65 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 20 NewTable field.
21d9f 20 54 68 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a The new FKey.**
21da0 20 69 73 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69 is not linked i
21da1 6e 74 6f 20 64 62 2d 3e 61 46 4b 65 79 20 61 74 nto db->aFKey at
21da2 20 74 68 69 73 20 70 6f 69 6e 74 20 2d 20 74 68 this point - th
21da3 61 74 20 64 6f 65 73 20 6e 6f 74 20 68 61 70 70 at does not happ
21da4 65 6e 0a 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69 en.** until sqli
21da5 74 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a te3EndTable()..*
21da6 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 65 69 67 6e *.** The foreign
21da7 20 6b 65 79 20 69 73 20 73 65 74 20 66 6f 72 20 key is set for
21da8 49 4d 4d 45 44 49 41 54 45 20 70 72 6f 63 65 73 IMMEDIATE proces
21da9 73 69 6e 67 2e 20 20 41 20 73 75 62 73 65 71 75 sing. A subsequ
21daa 65 6e 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 ent call.** to s
21dab 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 qlite3DeferForei
21dac 67 6e 4b 65 79 28 29 20 6d 69 67 68 74 20 63 68 gnKey() might ch
21dad 61 6e 67 65 20 74 68 69 73 20 74 6f 20 44 45 46 ange this to DEF
21dae 45 52 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ERRED..*/.SQLITE
21daf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
21db0 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 lite3CreateForei
21db1 67 6e 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a gnKey(. Parse *
21db2 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a pParse, /*
21db3 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
21db4 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
21db5 70 46 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f pFromCol, /* Co
21db6 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 lumns in this ta
21db7 62 6c 65 20 74 68 61 74 20 70 6f 69 6e 74 20 74 ble that point t
21db8 6f 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f o other table */
21db9 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 . Token *pTo,
21dba 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
21dbb 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 of the other tab
21dbc 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 le */. ExprList
21dbd 20 2a 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 *pToCol, /*
21dbe 43 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f Columns in the o
21dbf 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 ther table */.
21dc0 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 int flags
21dc1 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 /* Conflict
21dc2 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f resolution algo
21dc3 72 69 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20 rithms. */.){.
21dc4 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
21dc5 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 arse->db;.#ifnde
21dc6 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f f SQLITE_OMIT_FO
21dc7 52 45 49 47 4e 5f 4b 45 59 0a 20 20 46 4b 65 79 REIGN_KEY. FKey
21dc8 20 2a 70 46 4b 65 79 20 3d 20 30 3b 0a 20 20 54 *pFKey = 0;. T
21dc9 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72 73 65 able *p = pParse
21dca 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 ->pNewTable;. i
21dcb 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 6e 74 20 nt nByte;. int
21dcc 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 i;. int nCol;.
21dcd 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 char *z;.. ass
21dce 65 72 74 28 20 70 54 6f 21 3d 30 20 29 3b 0a 20 ert( pTo!=0 );.
21dcf 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61 if( p==0 || pPa
21dd0 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 49 4e 5f rse->nErr || IN_
21dd1 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67 DECLARE_VTAB ) g
21dd2 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 69 66 oto fk_end;. if
21dd3 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b ( pFromCol==0 ){
21dd4 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 . int iCol =
21dd5 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20 20 69 p->nCol-1;. i
21dd6 66 28 20 69 43 6f 6c 3c 30 20 29 20 67 6f 74 6f f( iCol<0 ) goto
21dd7 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69 66 28 fk_end;. if(
21dd8 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f pToCol && pToCo
21dd9 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 l->nExpr!=1 ){.
21dda 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
21ddb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 66 6f rMsg(pParse, "fo
21ddc 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25 73 22 reign key on %s"
21ddd 0a 20 20 20 20 20 20 20 20 20 22 20 73 68 6f 75 . " shou
21dde 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c ld reference onl
21ddf 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 y one column of
21de0 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20 20 20 table %T",.
21de1 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43 6f 6c p->aCol[iCol
21de2 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b 0a 20 ].zName, pTo);.
21de3 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 goto fk_end
21de4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c ;. }. nCol
21de5 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 1;. }else if
21de6 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 ( pToCol && pToC
21de7 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72 6f 6d ol->nExpr!=pFrom
21de8 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 Col->nExpr ){.
21de9 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
21dea 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 g(pParse,.
21deb 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c "number of col
21dec 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67 6e 20 umns in foreign
21ded 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 key does not mat
21dee 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ch the number of
21def 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f 6c 75 ". "colu
21df0 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65 72 mns in the refer
21df1 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b 0a 20 enced table");.
21df2 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a goto fk_end;.
21df3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 43 6f }else{. nCo
21df4 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 l = pFromCol->nE
21df5 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 xpr;. }. nByte
21df6 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b 65 79 = sizeof(*pFKey
21df7 29 20 2b 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 ) + nCol*sizeof(
21df8 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 pFKey->aCol[0])
21df9 2b 20 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 + pTo->n + 1;.
21dfa 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 if( pToCol ){.
21dfb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f for(i=0; i<pTo
21dfc 43 6f 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 Col->nExpr; i++)
21dfd 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d {. nByte +=
21dfe 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
21dff 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e (pToCol->a[i].zN
21e00 61 6d 65 29 20 2b 20 31 3b 0a 20 20 20 20 7d 0a ame) + 1;. }.
21e01 20 20 7d 0a 20 20 70 46 4b 65 79 20 3d 20 73 71 }. pFKey = sq
21e02 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
21e03 6f 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20 o(db, nByte );.
21e04 20 69 66 28 20 70 46 4b 65 79 3d 3d 30 20 29 7b if( pFKey==0 ){
21e05 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 . goto fk_end
21e06 3b 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 70 ;. }. pFKey->p
21e07 46 72 6f 6d 20 3d 20 70 3b 0a 20 20 70 46 4b 65 From = p;. pFKe
21e08 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 3d 20 70 y->pNextFrom = p
21e09 2d 3e 70 46 4b 65 79 3b 0a 20 20 7a 20 3d 20 28 ->pFKey;. z = (
21e0a 63 68 61 72 2a 29 26 70 46 4b 65 79 5b 31 5d 3b char*)&pFKey[1];
21e0b 0a 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 20 3d . pFKey->aCol =
21e0c 20 28 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 70 (struct sColMap
21e0d 2a 29 7a 3b 0a 20 20 7a 20 2b 3d 20 73 69 7a 65 *)z;. z += size
21e0e 6f 66 28 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 of(struct sColMa
21e0f 70 29 2a 6e 43 6f 6c 3b 0a 20 20 70 46 4b 65 79 p)*nCol;. pFKey
21e10 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20 20 6d 65 6d ->zTo = z;. mem
21e11 63 70 79 28 7a 2c 20 70 54 6f 2d 3e 7a 2c 20 70 cpy(z, pTo->z, p
21e12 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b 70 54 6f 2d To->n);. z[pTo-
21e13 3e 6e 5d 20 3d 20 30 3b 0a 20 20 7a 20 2b 3d 20 >n] = 0;. z +=
21e14 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65 pTo->n+1;. pFKe
21e15 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 30 3b 0a y->pNextTo = 0;.
21e16 20 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 20 3d 20 pFKey->nCol =
21e17 6e 43 6f 6c 3b 0a 20 20 69 66 28 20 70 46 72 6f nCol;. if( pFro
21e18 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 70 mCol==0 ){. p
21e19 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 FKey->aCol[0].iF
21e1a 72 6f 6d 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b rom = p->nCol-1;
21e1b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f . }else{. fo
21e1c 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 r(i=0; i<nCol; i
21e1d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a ++){. int j
21e1e 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b ;. for(j=0;
21e1f 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 j<p->nCol; j++)
21e20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 {. if( sq
21e21 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 2d 3e lite3StrICmp(p->
21e22 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 aCol[j].zName, p
21e23 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e FromCol->a[i].zN
21e24 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ame)==0 ){.
21e25 20 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c pFKey->aCol
21e26 5b 69 5d 2e 69 46 72 6f 6d 20 3d 20 6a 3b 0a 20 [i].iFrom = j;.
21e27 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
21e28 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
21e29 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 }. if( j>=p
21e2a 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 ->nCol ){.
21e2b 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
21e2c 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 g(pParse, .
21e2d 20 20 20 20 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f "unknown co
21e2e 6c 75 6d 6e 20 5c 22 25 73 5c 22 20 69 6e 20 66 lumn \"%s\" in f
21e2f 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e oreign key defin
21e30 69 74 69 6f 6e 22 2c 20 0a 20 20 20 20 20 20 20 ition", .
21e31 20 20 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 pFromCol->a[i
21e32 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 ].zName);.
21e33 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 goto fk_end;.
21e34 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
21e35 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b . if( pToCol ){
21e36 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
21e37 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCol; i++){.
21e38 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 int n = sqlite
21e39 33 53 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 3Strlen30(pToCol
21e3a 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 ->a[i].zName);.
21e3b 20 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c pFKey->aCol
21e3c 5b 69 5d 2e 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 [i].zCol = z;.
21e3d 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 memcpy(z, pT
21e3e 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 oCol->a[i].zName
21e3f 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d , n);. z[n]
21e40 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 20 2b 3d = 0;. z +=
21e41 20 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n+1;. }. }.
21e42 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 pFKey->isDefer
21e43 72 65 64 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79 red = 0;. pFKey
21e44 2d 3e 64 65 6c 65 74 65 43 6f 6e 66 20 3d 20 28 ->deleteConf = (
21e45 75 38 29 28 66 6c 61 67 73 20 26 20 30 78 66 66 u8)(flags & 0xff
21e46 29 3b 0a 20 20 70 46 4b 65 79 2d 3e 75 70 64 61 );. pFKey->upda
21e47 74 65 43 6f 6e 66 20 3d 20 28 75 38 29 28 28 66 teConf = (u8)((f
21e48 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78 lags >> 8 ) & 0x
21e49 66 66 29 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e ff);. pFKey->in
21e4a 73 65 72 74 43 6f 6e 66 20 3d 20 28 75 38 29 28 sertConf = (u8)(
21e4b 28 66 6c 61 67 73 20 3e 3e 20 31 36 20 29 20 26 (flags >> 16 ) &
21e4c 20 30 78 66 66 29 3b 0a 0a 20 20 2f 2a 20 4c 69 0xff);.. /* Li
21e4d 6e 6b 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b nk the foreign k
21e4e 65 79 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 ey to the table
21e4f 61 73 20 74 68 65 20 6c 61 73 74 20 73 74 65 70 as the last step
21e50 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 46 4b 65 .. */. p->pFKe
21e51 79 20 3d 20 70 46 4b 65 79 3b 0a 20 20 70 46 4b y = pFKey;. pFK
21e52 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e 64 3a ey = 0;..fk_end:
21e53 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
21e54 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 23 65 6e (db, pFKey);.#en
21e55 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
21e56 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 SQLITE_OMIT_FORE
21e57 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20 73 71 IGN_KEY) */. sq
21e58 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
21e59 65 74 65 28 64 62 2c 20 70 46 72 6f 6d 43 6f 6c ete(db, pFromCol
21e5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
21e5b 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
21e5c 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ToCol);.}../*.**
21e5d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
21e5e 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 called when an
21e5f 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 INITIALLY IMMEDI
21e60 41 54 45 20 6f 72 20 49 4e 49 54 49 41 4c 4c 59 ATE or INITIALLY
21e61 20 44 45 46 45 52 52 45 44 0a 2a 2a 20 63 6c 61 DEFERRED.** cla
21e62 75 73 65 20 69 73 20 73 65 65 6e 20 61 73 20 70 use is seen as p
21e63 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e art of a foreign
21e64 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e key definition.
21e65 20 20 54 68 65 20 69 73 44 65 66 65 72 72 65 64 The isDeferred
21e66 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 .** parameter is
21e67 20 31 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 1 for INITIALLY
21e68 20 44 45 46 45 52 52 45 44 20 61 6e 64 20 30 20 DEFERRED and 0
21e69 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d for INITIALLY IM
21e6a 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 54 68 65 20 MEDIATE..** The
21e6b 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 behavior of the
21e6c 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 most recently cr
21e6d 65 61 74 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 eated foreign ke
21e6e 79 20 69 73 20 61 64 6a 75 73 74 65 64 0a 2a 2a y is adjusted.**
21e6f 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2f accordingly..*/
21e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21e71 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 void sqlite3Defe
21e72 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 rForeignKey(Pars
21e73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
21e74 73 44 65 66 65 72 72 65 64 29 7b 0a 23 69 66 6e sDeferred){.#ifn
21e75 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
21e76 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 54 61 FOREIGN_KEY. Ta
21e77 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 46 4b 65 ble *pTab;. FKe
21e78 79 20 2a 70 46 4b 65 79 3b 0a 20 20 69 66 28 20 y *pFKey;. if(
21e79 28 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e (pTab = pParse->
21e7a 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 7c 7c pNewTable)==0 ||
21e7b 20 28 70 46 4b 65 79 20 3d 20 70 54 61 62 2d 3e (pFKey = pTab->
21e7c 70 46 4b 65 79 29 3d 3d 30 20 29 20 72 65 74 75 pFKey)==0 ) retu
21e7d 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 rn;. assert( is
21e7e 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c 20 69 Deferred==0 || i
21e7f 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29 3b 0a sDeferred==1 );.
21e80 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 pFKey->